GNU bug report logs - #70217
[PATCH] Add substring-partial-completion style

Previous Next

Package: emacs;

Reported by: Spencer Baugh <sbaugh <at> janestreet.com>

Date: Fri, 5 Apr 2024 12:43:02 UTC

Severity: wishlist

Tags: patch

Done: Eli Zaretskii <eliz <at> gnu.org>

Bug is archived. No further changes may be made.

Full log


Message #62 received at 70217 <at> debbugs.gnu.org (full text, mbox):

From: Spencer Baugh <sbaugh <at> janestreet.com>
To: Eli Zaretskii <eliz <at> gnu.org>
Cc: 70217 <at> debbugs.gnu.org, monnier <at> iro.umontreal.ca
Subject: Re: bug#70217: [PATCH] Add substring-partial-completion style
Date: Tue, 28 May 2024 14:16:30 -0400
[Message part 1 (text/plain, inline)]
Eli Zaretskii <eliz <at> gnu.org> writes:

>> From: Spencer Baugh <sbaugh <at> janestreet.com>
>> Cc: 70217 <at> debbugs.gnu.org,  monnier <at> iro.umontreal.ca
>> Date: Tue, 28 May 2024 10:39:52 -0400
>> 
>> Eli Zaretskii <eliz <at> gnu.org> writes:
>> 
>> >> b/c does match bbb/ccc at the beginning, according to the
>> >> partial-completion rules.  Explained as a glob, partial-completion turns
>> >> b/c into b*c which then can expand to bbb/ccc.
>> >
>> > Sorry, I don't understand what you are saying and how it addresses my
>> > concern.  To me, this example contradicts what was explained in the
>> > documentation earlier, so we must clarify this, whether in the example
>> > or in the preceding descriptions.  Please re-read how you described
>> > the effect of this option, and go from there.
>> 
>> Okay, how about this completely reworked explanation?  (It also changes
>> the name of the variable and inverts its effect)
>
> Thanks, this is more clear now.
> [snip]
> and (b) please do not use examples with repeated characters, because
> they can lead readers to make the wrong conclusions due to accidental
> situations.  For example, AFAIU valid candidates for "b*/c*" include
> "bcdxyz/c1234" and also "b/x/y/z/c/1/2/3", but readers might
> mistakenly think that "b*" stands for a string made only of "b", or
> that there can be only one slash and it must precede "c".  Avoiding
> repeated characters prevents such misunderstandings.

Excellent point, fixed.

> But please (a) don't use "glob" and file wildcard notation, use
> regexps instead;

True, I removed the word "glob", I agree that's confusing since
e.g. [a-z] or {foo,bar} are valid globs but not valid in
partial-completion.

Note however that "*" is literally valid syntax with partial-completion,
where as the regexp notation (".*") is not.  The partial-completion
documentation already mentions this in (info "(emacs) Completion
Styles").  So I slightly reworded it and continued using "*".

[0001-Allow-customizing-partial-completion-to-be-more-like.patch (text/x-patch, inline)]
From b789f57aefd565bdbb8626fce129aa65e7a23af3 Mon Sep 17 00:00:00 2001
From: Spencer Baugh <sbaugh <at> janestreet.com>
Date: Sun, 26 May 2024 08:57:37 -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            | 20 +++++++++++++++++++
 lisp/minibuffer.el  | 48 ++++++++++++++++++++++++++++++++++++++-------
 3 files changed, 75 insertions(+), 9 deletions(-)

diff --git a/doc/emacs/mini.texi b/doc/emacs/mini.texi
index 4557f41c3f7..cfb5df4d586 100644
--- a/doc/emacs/mini.texi
+++ b/doc/emacs/mini.texi
@@ -535,8 +535,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):
@@ -561,6 +567,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 d058acc3572..8eecc1d2aa3 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -1848,6 +1848,26 @@ customization group control exactly when Emacs displays this preview.
 'completion-preview-mode' is buffer-local, to enable it globally use
 'global-completion-preview-mode'.
 
++++
+*** 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'.
+
 ---
 ** The highly accessible Modus themes collection has eight items.
 The 'modus-operandi' and 'modus-vivendi' are the main themes that have
diff --git a/lisp/minibuffer.el b/lisp/minibuffer.el
index f62cb2566b2..355fd23eed8 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)
@@ -3864,6 +3879,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), the partial-completion style completes a string
+like \"o/t\" as if it was \"o*/t*\".  This means \"one/two\" is a valid
+completion, but not \"zero/one/two\" or \"zeroone/two\".
+
+If non-nil, the partial-completion style completes \"o/t\" as if it was
+\"*o*/t*\".  This means \"one/two\", \"zero/one/two\" and
+\"zeroone/two\" are all valid completions.  Note that this can be slower
+to compute since less filtering of the completion candidates is
+possible."
+  :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
@@ -3914,7 +3944,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


This bug report was last modified 327 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.