From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Spencer Baugh Newsgroups: gmane.emacs.bugs Subject: bug#70217: [PATCH] Add substring-partial-completion style Date: Wed, 21 Aug 2024 12:01:06 -0400 Message-ID: References: <86a5kpgdo7.fsf@gnu.org> <86ttilvsy8.fsf@gnu.org> <86ikz0wozc.fsf@gnu.org> <86o78qt1hl.fsf@gnu.org> <86fru1u6km.fsf@gnu.org> <86bk4pu4ip.fsf@gnu.org> <86plt0oibv.fsf@gnu.org> <86ikw8kdoz.fsf@gnu.org> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="12396"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Cc: 70217@debbugs.gnu.org, monnier@iro.umontreal.ca To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Wed Aug 21 18:02:26 2024 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1sgnmz-00036b-Ba for geb-bug-gnu-emacs@m.gmane-mx.org; Wed, 21 Aug 2024 18:02:25 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sgnlz-00025T-2P; Wed, 21 Aug 2024 12:01:23 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sgnlu-00022N-6z for bug-gnu-emacs@gnu.org; Wed, 21 Aug 2024 12:01:20 -0400 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sgnlt-0001fv-8K for bug-gnu-emacs@gnu.org; Wed, 21 Aug 2024 12:01:17 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=debbugs.gnu.org; s=debbugs-gnu-org; h=MIME-Version:Date:References:In-Reply-To:From:To:Subject; bh=/tRKNiz0PS4Nssgz3WSypELVBP8EUOEz7kBrcWjSkMY=; b=l3SX2Voria+PmwCVfXHQ3imSzrj4J4mLFO1NIyCt6+MI7ibm5f1n+99l8RlYjerZA+epp6csN+HZSXg9OC/6hBX+jD3RGNPvoVd/DORuR7Vr0aoc3b3UnM9c1X8jgWHJR9UkSIerxYmKZxiCN+i0GDSs65rKjvF/9+qX7QHnwA77sHTMuF/ueGG994UVSRv17v6HSBkMcwqshhl/SF5DrtOLK6+VzvWj5W01xgW5MpuW7+dnK/GURltCFkXRzOc9V1iIfFksD4SPy++Hyo+wyZOFKSxXoG2YdUTSiC4dnX6um9Cz4AN/LPFxRF5Jd8W1N8TREaJUhjE4zrasShQBPg==; Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1sgnmb-00087F-Q3 for bug-gnu-emacs@gnu.org; Wed, 21 Aug 2024 12:02:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Spencer Baugh Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Wed, 21 Aug 2024 16:02:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 70217 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 70217-submit@debbugs.gnu.org id=B70217.172425612131191 (code B ref 70217); Wed, 21 Aug 2024 16:02:01 +0000 Original-Received: (at 70217) by debbugs.gnu.org; 21 Aug 2024 16:02:01 +0000 Original-Received: from localhost ([127.0.0.1]:36157 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1sgnma-000870-Dh for submit@debbugs.gnu.org; Wed, 21 Aug 2024 12:02:01 -0400 Original-Received: from mxout1.mail.janestreet.com ([38.105.200.78]:58175) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1sgnmW-00086h-Nx for 70217@debbugs.gnu.org; Wed, 21 Aug 2024 12:01:58 -0400 In-Reply-To: <86ikw8kdoz.fsf@gnu.org> (Eli Zaretskii's message of "Sat, 10 Aug 2024 11:02:52 +0300") DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=janestreet.com; s=waixah; t=1724256066; bh=/tRKNiz0PS4Nssgz3WSypELVBP8EUOEz7kBrcWjSkMY=; h=From:To:Cc:Subject:In-Reply-To:References:Date; b=NOvQVHu3p2zpVsXMiHUB3L9GwbigzrWV+imD6Yaie6SnHBkKiDuQVBzEnVHR45p8+ UXR70fd9HGX7I4w+e0xDmv2/gCxKGGCEQRXGPhmYRs42eolWovdzU12YoiKQV+idmr ZeTUSSKvjT7GMP1asgh/CSHcAOhBCf7jireMoOnqCHZkeba2hEt5BqsXpvk7rBoKRW AP0DPB/tG0aZzF71tkjwlAuwjQhAW9ogP/mqltf5cn0263WhV4OqAmwKm0SCNok5rV UGVN7vM5MJAqTNs5aEEXCNZGq1VYFKiw7cbBwfpjVzWYicqQri+hl02e4GnlD5Ka4c zoEL6bmhL2fkA== X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:290495 Archived-At: --=-=-= Content-Type: text/plain Eli Zaretskii writes: >> From: Spencer Baugh >> Cc: Eli Zaretskii , 70217@debbugs.gnu.org >> Date: Thu, 08 Aug 2024 09:13:16 -0400 >> >> Stefan Monnier writes: >> >> Stefan - it would be nice to know if you think this approach of >> >> including dynamic variable bindings in completion-styles is a good idea, >> >> or at least an acceptable idea. I can go back to the drawing board if >> >> this clashes with your vision/design/theory for completion styles. >> > >> > I think it's acceptable, yes. >> > >> > >> > Stefan >> >> Eli, could you please install this patch, then? > > I tried, but it no longer applies cleanly. Could you please rebase > and repost? Rebased on trunk: --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=0001-Allow-customizing-partial-completion-to-be-more-like.patch >From 02a8b19c0e2d9650185a5ea1a16f540d38b1ead2 Mon Sep 17 00:00:00 2001 From: Spencer Baugh Date: Wed, 21 Aug 2024 11:59:42 -0400 Subject: [PATCH] Allow customizing partial-completion to be more like substring The substring completion style completes "foo-bar" as "*foo-bar*". The partial-completion completion style completes "foo-bar" as "foo*bar*". Previously, it was not possible to get completion of "foo-bar" to act as "*foo*bar*", e.g. combining the partial-completion and substring styles. This would be especially useful for things like project-find-file. Now it is possible by customizing the completion-pcm-anchored variable to a non-nil value. Furthermore, it's convenient to be able to run regular (completion-pcm-anchored=t, non-substring) partial-completion before running completion-pcm-anchored=nil partial-completion, since the former provides more narrowly targeted completions. It's possible to do this by customizing completion-styles. Just add '(partial-completion ((completion-pcm-anchored t))) and '(partial-completion ((completion-pcm-anchored nil))) in that order. Then the completion machinery will first run partial-completion with completion-pcm-anchored=t, and if that returns no completions, run partial-completion with completion-pcm-anchored=nil. * lisp/minibuffer.el (completion--nth-completion): Allow an element of completion-styles to contain a list of bindings. (completion-styles): Document that. (completion-pcm-anchored): Add. (completion-pcm--string->pattern): Check completion-pcm-anchored. (bug#70217) --- doc/emacs/mini.texi | 16 +++++++++++++-- etc/NEWS | 22 +++++++++++++++++++++ lisp/minibuffer.el | 48 ++++++++++++++++++++++++++++++++++++++------- 3 files changed, 77 insertions(+), 9 deletions(-) diff --git a/doc/emacs/mini.texi b/doc/emacs/mini.texi index 9d012cd6fbc..6b604412e88 100644 --- a/doc/emacs/mini.texi +++ b/doc/emacs/mini.texi @@ -543,8 +543,14 @@ Completion Styles @vindex completion-styles The list variable @code{completion-styles} specifies the completion -styles to use. Each list element is the name of a completion style (a -Lisp symbol). The available style symbols are stored in the variable +styles to use. Each list element is either the name of a completion +style (a Lisp symbol) or a list starting with the name of a completion +style followed by @code{let}-style list of bindings which will be in +effect for that completion style. Multiple elements of +@code{completion-styles} can name the same completion style with +different variable bindings. + +The available style symbols are stored in the variable @code{completion-styles-alist} (@pxref{Completion Variables,,, elisp, The Emacs Lisp Reference Manual}). The default completion styles are (in order): @@ -569,6 +575,12 @@ Completion Styles @dfn{wildcard}---it matches any string of characters at the corresponding position in the completion alternative. +@vindex completion-pcm-leading-wildcard +If @code{completion-pcm-leading-wildcard} is set to @code{t}, this style +always acts as if a @dfn{wildcard} is present at the start of the +minibuffer text, similar to the @code{substring} style. For example, +@samp{l-m} will complete to @samp{emacs-lisp-mode}. + @item emacs22 @cindex @code{emacs22}, completion style This completion style is similar to @code{basic}, except that it diff --git a/etc/NEWS b/etc/NEWS index 9a9b6ead241..e8cb91e0c87 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -43,6 +43,28 @@ The 'find-function', 'find-library', 'find-face-definition', and 'find-variable' commands now allow retrieving previous input using the usual minibuffer history commands. Each command has a separate history. +** Minibuffer and Completions + ++++ +*** New user option 'completion-pcm-leading-wildcard'. +This option configures how the partial-completion style does completion. +It defaults to nil, which preserves the existing behavior. When it is set +to t, the partial-completion style behaves more like the substring +style, in that a string being completed can match against a candidate +anywhere in the candidate string. + ++++ +*** 'completion-styles' now can contain lists of bindings. +In addition to being a symbol naming a completion style, an element of +'completion-styles' can now be a list of the form '(STYLE ((VARIABLE +VALUE) ...))' where STYLE is a symbol naming a completion style. +VARIABLE will be bound to VALUE (without evaluating it) while the style +is executing. This allows multiple references to the same style with +different values for completion-affecting variables like +'completion-pcm-leading-wildcard or 'completion-ignore-case'. This also +applies for the styles configuration in 'completion-category-overrides' +and 'completion-category-defaults'. + ** Windows diff --git a/lisp/minibuffer.el b/lisp/minibuffer.el index 3beb3c06a18..6fae62b3904 100644 --- a/lisp/minibuffer.el +++ b/lisp/minibuffer.el @@ -1141,7 +1141,15 @@ completion-styles ;; and simply add "bar" to the end of the result. emacs22) "List of completion styles to use. -The available styles are listed in `completion-styles-alist'. +An element should be a symbol which is listed in +`completion-styles-alist'. + +An element can also be a list of the form +(STYLE ((VARIABLE VALUE) ...)) +STYLE must be a symbol listed in `completion-styles-alist', followed by +a `let'-style list of variable/value pairs. VARIABLE will be bound to +VALUE (without evaluating it) while the style is handling completion. +This allows repeating the same style with different configurations. Note that `completion-category-overrides' may override these styles for specific categories, such as files, buffers, etc." @@ -1284,11 +1292,18 @@ completion--nth-completion (result-and-style (seq-some (lambda (style) - (let ((probe (funcall - (or (nth n (assq style completion-styles-alist)) - (error "Invalid completion style %s" style)) - string table pred point))) - (and probe (cons probe style)))) + (let (symbols values) + (when (consp style) + (dolist (binding (cadr style)) + (push (car binding) symbols) + (push (cadr binding) values)) + (setq style (car style))) + (cl-progv symbols values + (let ((probe (funcall + (or (nth n (assq style completion-styles-alist)) + (error "Invalid completion style %s" style)) + string table pred point))) + (and probe (cons probe style)))))) (completion--styles md))) (adjust-fn (get (cdr result-and-style) 'completion--adjust-metadata))) (when (and adjust-fn metadata) @@ -3868,6 +3883,21 @@ completion-pcm--pattern-trivial-p (setq trivial nil))) trivial))) +(defcustom completion-pcm-leading-wildcard nil + "If non-nil, partial-completion completes as if there's a leading wildcard. + +If nil (the default), partial-completion requires a matching completion +alternative to have the same beginning as the first \"word\" in the +minibuffer text, where \"word\" is determined by +`completion-pcm-word-delimiters'. + +If non-nil, partial-completion allows any string of characters to occur +at the beginning of a completion alternative, as if a wildcard such as +\"*\" was present at the beginning of the minibuffer text. This makes +partial-completion behave more like the substring completion style." + :version "30.1" + :type 'boolean) + (defun completion-pcm--string->pattern (string &optional point) "Split STRING into a pattern. A pattern is a list where each element is either a string @@ -3918,7 +3948,11 @@ completion-pcm--string->pattern (when (> (length string) p0) (if pending (push pending pattern)) (push (substring string p0) pattern)) - (nreverse pattern)))) + (setq pattern (nreverse pattern)) + (when completion-pcm-leading-wildcard + (when (stringp (car pattern)) + (push 'prefix pattern))) + pattern))) (defun completion-pcm--optimize-pattern (p) ;; Remove empty strings in a separate phase since otherwise a "" -- 2.39.3 --=-=-=--