* bug#70217: [PATCH] Add substring-partial-completion style
@ 2024-04-05 12:41 Spencer Baugh
2024-04-05 18:35 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
0 siblings, 1 reply; 31+ messages in thread
From: Spencer Baugh @ 2024-04-05 12:41 UTC (permalink / raw)
To: 70217; +Cc: monnier
[-- Attachment #1: Type: text/plain, Size: 1458 bytes --]
Tags: patch
The substring completion style completes "foo-bar" as "*foo-bar*".
The partial-completion completion style completes "foo-bar" as
"foo*bar*".
It is currently not possible to get completion as "*foo*bar*",
e.g. combining the two.
This patch adds a completion style that combines the two. (It's a bit
rough right now, just a quick implementation, will clean it up with
feedback)
It's preferable for this to be a separate style from partial-completion
rather than a customization for partial-completion, because completing
with a leading glob is inefficient: it doesn't allow the completion
table to do any filtering at all. So, for example, one might want the
regular partial-completion style to run first, and if it doesn't find
anything then the substring-partial-completion style can run.
Further claim: I think the substring completion style should be replaced
with the substring-partial-completion style in most places. But that
may be a bit more controversial.
In GNU Emacs 29.2.50 (build 6, x86_64-pc-linux-gnu, X toolkit, cairo
version 1.15.12, Xaw scroll bars) of 2024-03-25 built on
igm-qws-u22796a
Repository revision: 5d867aca5ea016c07fe9e8b60f55345f83012333
Repository branch: emacs-29
Windowing system distributor 'The X.Org Foundation', version 11.0.12011000
System Description: Rocky Linux 8.9 (Green Obsidian)
Configured using:
'configure -C --with-x-toolkit=lucid --with-native-compilation
--with-gif=ifavailable'
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Add-substring-partial-completion-style.patch --]
[-- Type: text/patch, Size: 4926 bytes --]
From 6446dfddb240e206ae24d0a3124325121fd1ffe5 Mon Sep 17 00:00:00 2001
From: Spencer Baugh <sbaugh@janestreet.com>
Date: Fri, 5 Apr 2024 08:34:59 -0400
Subject: [PATCH] Add substring-partial-completion style
---
lisp/minibuffer.el | 46 ++++++++++++++++++++++++++++++++++++++++------
1 file changed, 40 insertions(+), 6 deletions(-)
diff --git a/lisp/minibuffer.el b/lisp/minibuffer.el
index a1df6f4c5ab..76de97e0ad9 100644
--- a/lisp/minibuffer.el
+++ b/lisp/minibuffer.el
@@ -1061,6 +1061,14 @@ completion-styles-alist
I.e. when completing \"foo_bar\" (where _ is the position of point),
it will consider all completions candidates matching the glob
pattern \"*foo*bar*\".")
+ (substring-partial-completion
+ completion-substring-pcm-try-completion completion-substring-pcm-all-completions
+ "A combination of the partial-completion and substring styles.
+This is like the partial-completion style, but it will also
+expand at the start of a completion string.
+I.e. when completing \"l-co_h\" (where _ is the position of point),
+it will consider all completions candidates matching the glob
+pattern \"*l*-co*h*\".")
(flex
completion-flex-try-completion completion-flex-all-completions
"Completion of an in-order subset of characters.
@@ -3727,10 +3735,13 @@ completion-pcm--pattern-trivial-p
(setq trivial nil)))
trivial)))
-(defun completion-pcm--string->pattern (string &optional point)
+(defun completion-pcm--string->pattern (string &optional point startglob)
"Split STRING into a pattern.
A pattern is a list where each element is either a string
-or a symbol, see `completion-pcm--merge-completions'."
+or a symbol, see `completion-pcm--merge-completions'.
+
+If STARTGLOB is non-nil, the pattern will start with the symbol
+`prefix' if it would otherwise start with a string."
(if (and point (< point (length string)))
(let ((prefix (substring string 0 point))
(suffix (substring string point)))
@@ -3777,7 +3788,10 @@ completion-pcm--string->pattern
(when (> (length string) p0)
(if pending (push pending pattern))
(push (substring string p0) pattern))
- (nreverse pattern))))
+ (setq pattern (nreverse pattern))
+ (if (not (stringp (car pattern)))
+ pattern
+ (cons 'prefix pattern)))))
(defun completion-pcm--optimize-pattern (p)
;; Remove empty strings in a separate phase since otherwise a ""
@@ -3976,11 +3990,12 @@ completion-pcm--hilit-commonality
(t completions)))
(defun completion-pcm--find-all-completions (string table pred point
- &optional filter)
+ &optional filter startglob)
"Find all completions for STRING at POINT in TABLE, satisfying PRED.
POINT is a position inside STRING.
FILTER is a function applied to the return value, that can be used, e.g. to
-filter out additional entries (because TABLE might not obey PRED)."
+filter out additional entries (because TABLE might not obey PRED).
+STARTGLOB controls whether there's a leading glob in the pattern."
(unless filter (setq filter 'identity))
(let* ((beforepoint (substring string 0 point))
(afterpoint (substring string point))
@@ -3991,7 +4006,7 @@ completion-pcm--find-all-completions
(setq string (substring string (car bounds) (+ point (cdr bounds))))
(let* ((relpoint (- point (car bounds)))
(pattern (completion-pcm--optimize-pattern
- (completion-pcm--string->pattern string relpoint)))
+ (completion-pcm--string->pattern string relpoint startglob)))
(all (condition-case-unless-debug err
(funcall filter
(completion-pcm--all-completions
@@ -4255,6 +4270,25 @@ completion-pcm-try-completion
'completion-pcm--filename-try-filter))))
(completion-pcm--merge-try pattern all prefix suffix)))
+;; Substring-pcm completion
+;; A trivial copy of pcm completion, passing startglob=t
+
+(defun completion-substring-pcm-all-completions (string table pred point)
+ (pcase-let ((`(,pattern ,all ,prefix ,_suffix)
+ (completion-pcm--find-all-completions string table pred point nil t)))
+ (when all
+ (nconc (completion-pcm--hilit-commonality pattern all)
+ (length prefix)))))
+
+(defun completion-substring-pcm-try-completion (string table pred point)
+ (pcase-let ((`(,pattern ,all ,prefix ,suffix)
+ (completion-pcm--find-all-completions
+ string table pred point
+ (if minibuffer-completing-file-name
+ 'completion-pcm--filename-try-filter)
+ t)))
+ (completion-pcm--merge-try pattern all prefix suffix)))
+
;;; Substring completion
;; Mostly derived from the code of `basic' completion.
--
2.39.3
^ permalink raw reply related [flat|nested] 31+ messages in thread
* bug#70217: [PATCH] Add substring-partial-completion style
2024-04-05 12:41 bug#70217: [PATCH] Add substring-partial-completion style Spencer Baugh
@ 2024-04-05 18:35 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-04-05 19:46 ` Drew Adams via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-04-18 15:19 ` Spencer Baugh
0 siblings, 2 replies; 31+ messages in thread
From: Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2024-04-05 18:35 UTC (permalink / raw)
To: Spencer Baugh; +Cc: 70217
> It's preferable for this to be a separate style from partial-completion
> rather than a customization for partial-completion, because completing
> with a leading glob is inefficient: it doesn't allow the completion
> table to do any filtering at all. So, for example, one might want the
> regular partial-completion style to run first, and if it doesn't find
> anything then the substring-partial-completion style can run.
FWIW, I think the "language of completion styles" would benefit from
being a bit richer. I.e. instead of a completion style (i.e. an entry
in `completion-styles`) being limited to a symbol, we could
extend it so a style can take arguments.
E.g. the `substring` style could take an argument which when set to the
symbol `pcm` would cause it to behave like your "substring-partial-completion":
(setq completion-styles '(basic (substring pcm) emacs22))
Alternatively, it could be an argument to `partial-completion`, e.g.:
(setq completion-styles '(basic (partial-completion notanchored) emacs22))
- Stefan
^ permalink raw reply [flat|nested] 31+ messages in thread
* bug#70217: [PATCH] Add substring-partial-completion style
2024-04-05 18:35 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
@ 2024-04-05 19:46 ` Drew Adams via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-04-06 8:10 ` Eshel Yaron via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-04-18 15:19 ` Spencer Baugh
1 sibling, 1 reply; 31+ messages in thread
From: Drew Adams via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2024-04-05 19:46 UTC (permalink / raw)
To: Stefan Monnier, Spencer Baugh; +Cc: 70217@debbugs.gnu.org
> > It's preferable for this to be a separate style from partial-completion
> > rather than a customization for partial-completion, because completing
> > with a leading glob is inefficient: it doesn't allow the completion
> > table to do any filtering at all. So, for example, one might want the
> > regular partial-completion style to run first, and if it doesn't find
> > anything then the substring-partial-completion style can run.
>
> FWIW, I think the "language of completion styles"
> would benefit from being a bit richer.
FWIW, I agree. Richer in different ways/dimensions.
> I.e. instead of a completion style (i.e. an entry
> in `completion-styles`) being limited to a symbol,
> we could extend it so a style can take arguments.
>
> E.g. the `substring` style could take an argument
> which when set to the symbol `pcm` would cause it
> to behave like your "substring-partial-completion":
> (setq completion-styles '(basic (substring pcm) emacs22))
>
> Alternatively, it could be an argument to `partial-completion`, e.g.:
> (setq completion-styles '(basic (partial-completion notanchored) emacs22))
I'm not sure that's the best way/place to provide
for that, but yes, it's one possible direction for
enrichment.
___
FWIW, I think `completion-styles' is too rudimentary
in additional ways. (I've mentioned this before.)
1. There's only one set of completion styles that's
ever in effect. Each style is tried, in turn, until
one of them successfully completes your input.
They can only be used together - all or none; they
can never serve as alternatives that you can choose
interactively (runtime).
2. All completions you see come from the same style.
You have no control over which style will actually
be used for any given input, other than ordering
the styles ahead of time.
And you have no way of knowing which style was
actually used to produce a given set of candidates.
For users, the link (cognitive and otherwise) is
lost between a style and its completions for a
given input pattern.
The relation between your input pattern and the
matches is thus sometimes not so clear. There's
no way to know, e.g., that initial matching failed
and partial matching succeeded.
3. Instead of a single list of styles, Emacs should
support a set (list) of such lists, which you can
choose from at runtime. You can try completing
using one styles list and, if that doesn't succeed,
switch to another.
Since any such set can be a singleton, allowing
multiple sets also means you can use just a single
style.
4. Users should be able to define, for a specific
_command_, the possible styles sets to choose
from, and do so interactively.
___
I'd say Emacs should think about providing more
flexibility for what kinds of completing (and
sequences of such kinds, per `completing-styles')
are available and how users and code can move
among them or conditionalize them per context.
___
As an illustration, Icicles has long had such
possibilities (even before `completion-styles').
https://www.emacswiki.org/emacs/Icicles_-_Completion_Methods_and_Styles
^ permalink raw reply [flat|nested] 31+ messages in thread
* bug#70217: [PATCH] Add substring-partial-completion style
2024-04-05 19:46 ` Drew Adams via Bug reports for GNU Emacs, the Swiss army knife of text editors
@ 2024-04-06 8:10 ` Eshel Yaron via Bug reports for GNU Emacs, the Swiss army knife of text editors
0 siblings, 0 replies; 31+ messages in thread
From: Eshel Yaron via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2024-04-06 8:10 UTC (permalink / raw)
To: 70217; +Cc: sbaugh, monnier, drew.adams
Drew Adams writes:
>> > It's preferable for this to be a separate style from partial-completion
>> > rather than a customization for partial-completion, because completing
>> > with a leading glob is inefficient: it doesn't allow the completion
>> > table to do any filtering at all. So, for example, one might want the
>> > regular partial-completion style to run first, and if it doesn't find
>> > anything then the substring-partial-completion style can run.
>>
>> FWIW, I think the "language of completion styles"
>> would benefit from being a bit richer.
>
> FWIW, I agree. Richer in different ways/dimensions.
>
>> I.e. instead of a completion style (i.e. an entry
>> in `completion-styles`) being limited to a symbol,
>> we could extend it so a style can take arguments.
One thing to keep in mind is that such parameterized completion styles
would make user interaction with completion styles more complicated.
It's much easier to indicate the effective completion style to the user,
and to allow them to pick and choose their completion styles on the fly,
when these completion styles are mere symbols. (Currently Emacs doesn't
provide such UI for completion styles, but I proposed something along
these lines in branch feature/minibuffer-completion-enhancements.)
>>
>> E.g. the `substring` style could take an argument
>> which when set to the symbol `pcm` would cause it
>> to behave like your "substring-partial-completion":
>> (setq completion-styles '(basic (substring pcm) emacs22))
>>
>> Alternatively, it could be an argument to `partial-completion`, e.g.:
>> (setq completion-styles '(basic (partial-completion notanchored) emacs22))
[...]
> ...you have no way of knowing which style was
> actually used to produce a given set of candidates.
> For users, the link (cognitive and otherwise) is
> lost between a style and its completions for a
> given input pattern.
+1
I agree it's quite important to indicate which completion style produced
what you're seeing, at the very least such indication makes it clear
where you should look if you don't like the completions you got.
Best,
Eshel
^ permalink raw reply [flat|nested] 31+ messages in thread
* bug#70217: [PATCH] Add substring-partial-completion style
2024-04-05 18:35 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-04-05 19:46 ` Drew Adams via Bug reports for GNU Emacs, the Swiss army knife of text editors
@ 2024-04-18 15:19 ` Spencer Baugh
2024-05-08 16:46 ` Spencer Baugh
1 sibling, 1 reply; 31+ messages in thread
From: Spencer Baugh @ 2024-04-18 15:19 UTC (permalink / raw)
To: Stefan Monnier; +Cc: 70217
Stefan Monnier <monnier@iro.umontreal.ca> writes:
>> It's preferable for this to be a separate style from partial-completion
>> rather than a customization for partial-completion, because completing
>> with a leading glob is inefficient: it doesn't allow the completion
>> table to do any filtering at all. So, for example, one might want the
>> regular partial-completion style to run first, and if it doesn't find
>> anything then the substring-partial-completion style can run.
>
> FWIW, I think the "language of completion styles" would benefit from
> being a bit richer. I.e. instead of a completion style (i.e. an entry
> in `completion-styles`) being limited to a symbol, we could
> extend it so a style can take arguments.
>
> E.g. the `substring` style could take an argument which when set to the
> symbol `pcm` would cause it to behave like your "substring-partial-completion":
>
> (setq completion-styles '(basic (substring pcm) emacs22))
>
> Alternatively, it could be an argument to `partial-completion`, e.g.:
>
> (setq completion-styles '(basic (partial-completion notanchored) emacs22))
This is a good point, because I think there's a few things I want which
fit neatly with this:
- '(substring pcm) (i.e. the behavior added in this feature)
- '(emacs22 pcm) (i.e. ignoring the suffix after point, but using pcm
for what's before point)
- '(emacs22 pcm substring) (i.e. ignoring the suffix after point, but
also globbing at the start of the completion like substring, and using
pcm for it)
But, also, I realized that I basically always want PCM for both the
substring and emacs22 completion styles. So what about having two
customizations, defaulting to nil?
completion-substring-use-pcm
completion-emacs22-use-pcm
These customization will affect all usage of those styles. Without this
approach, I'd have to use completion-category-overrides to override the
completion styles for every single category, just to make the styles use
PCM. (which IMO should be the default anyway)
This doesn't give me the substring-emacs22 style that I want, but maybe
that should actually be a separate style, since its behavior would be
fairly different from both emacs22 and substring, and so probably
doesn't make sense as an option.
^ permalink raw reply [flat|nested] 31+ messages in thread
* bug#70217: [PATCH] Add substring-partial-completion style
2024-04-18 15:19 ` Spencer Baugh
@ 2024-05-08 16:46 ` Spencer Baugh
2024-05-08 17:14 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
0 siblings, 1 reply; 31+ messages in thread
From: Spencer Baugh @ 2024-05-08 16:46 UTC (permalink / raw)
To: Stefan Monnier; +Cc: 70217
[-- Attachment #1: Type: text/plain, Size: 361 bytes --]
Spencer Baugh <sbaugh@janestreet.com> writes:
> But, also, I realized that I basically always want PCM for both the
> substring and emacs22 completion styles. So what about having two
> customizations, defaulting to nil?
>
> completion-substring-use-pcm
> completion-emacs22-use-pcm
Here is a patch implementing this approach for both substring and
emacs22.
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Support-using-partial-completion-in-emacs22-and-subs.patch --]
[-- Type: text/x-patch, Size: 9327 bytes --]
From 1a10582f1d41109a8a84451fe847fd0ab685cacb Mon Sep 17 00:00:00 2001
From: Spencer Baugh <sbaugh@janestreet.com>
Date: Wed, 8 May 2024 12:45:19 -0400
Subject: [PATCH] Support using partial-completion in emacs22 and substring
styles
The partial-completion completion style is useful, and so are the
emacs22 and substring completion styles. Now they can be used at the
same time.
* lisp/minibuffer.el (completion-emacs22-use-pcm)
(completion-substring-use-pcm): Add. (bug#70217)
(completion-emacs22-try-completion)
(completion-emacs22-all-completions): Check completion-emacs22-use-pcm.
(completion-pcm--string->pattern, completion-pcm--find-all-completions)
(completion-pcm-all-completions, completion-pcm--merge-try)
(completion-pcm-try-completion): Add "startglob" optional argument and
pass through.
(completion-substring-try-completion)
(completion-substring-all-completions): Check
completion-substring-use-pcm and pass startglob=t.
---
lisp/minibuffer.el | 93 ++++++++++++++++++++++++++++++++--------------
1 file changed, 65 insertions(+), 28 deletions(-)
diff --git a/lisp/minibuffer.el b/lisp/minibuffer.el
index ad6a0928cda..d80cd91320c 100644
--- a/lisp/minibuffer.el
+++ b/lisp/minibuffer.el
@@ -3738,9 +3738,25 @@ completion-emacs21-all-completions
(length string)
(car (completion-boundaries string table pred ""))))
+(defcustom completion-emacs22-use-pcm nil
+ "If non-nil, the emacs22 completion style performs partial-completion.
+
+This means that in addition to ignoring the text after point
+during completion, the text before point is expanded following
+the partial-completion rules.")
+
(defun completion-emacs22-try-completion (string table pred point)
- (let ((suffix (substring string point))
- (completion (try-completion (substring string 0 point) table pred)))
+ (let* ((suffix (substring string point))
+ (prefix (substring string 0 point))
+ (completion
+ (if completion-emacs22-use-pcm
+ (let ((ret (completion-pcm-try-completion prefix table pred point)))
+ (if (consp ret)
+ ;; Ignore any changes to point; that would change
+ ;; what text we're ignoring
+ (car ret)
+ ret))
+ (try-completion prefix table pred))))
(cond
((eq completion t)
(if (equal "" suffix)
@@ -3765,10 +3781,12 @@ completion-emacs22-try-completion
(defun completion-emacs22-all-completions (string table pred point)
(let ((beforepoint (substring string 0 point)))
- (completion-hilit-commonality
- (all-completions beforepoint table pred)
- point
- (car (completion-boundaries beforepoint table pred "")))))
+ (if completion-emacs22-use-pcm
+ (completion-pcm-all-completions beforepoint table pred point)
+ (completion-hilit-commonality
+ (all-completions beforepoint table pred)
+ point
+ (car (completion-boundaries beforepoint table pred ""))))))
;;; Basic completion.
@@ -3875,10 +3893,13 @@ completion-pcm--pattern-trivial-p
(setq trivial nil)))
trivial)))
-(defun completion-pcm--string->pattern (string &optional point)
+(defun completion-pcm--string->pattern (string &optional point startglob)
"Split STRING into a pattern.
A pattern is a list where each element is either a string
-or a symbol, see `completion-pcm--merge-completions'."
+or a symbol, see `completion-pcm--merge-completions'.
+
+If STARTGLOB is non-nil, the pattern will start with the symbol
+`prefix' if it would otherwise start with a string."
(if (and point (< point (length string)))
(let ((prefix (substring string 0 point))
(suffix (substring string point)))
@@ -3925,7 +3946,10 @@ 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 (and startglob (stringp (car pattern)))
+ (push 'prefix pattern))
+ pattern)))
(defun completion-pcm--optimize-pattern (p)
;; Remove empty strings in a separate phase since otherwise a ""
@@ -4218,11 +4242,12 @@ completion-pcm--hilit-commonality
(t completions)))
(defun completion-pcm--find-all-completions (string table pred point
- &optional filter)
+ &optional filter startglob)
"Find all completions for STRING at POINT in TABLE, satisfying PRED.
POINT is a position inside STRING.
FILTER is a function applied to the return value, that can be used, e.g. to
-filter out additional entries (because TABLE might not obey PRED)."
+filter out additional entries (because TABLE might not obey PRED).
+STARTGLOB controls whether there's a leading glob in the pattern."
(unless filter (setq filter 'identity))
(let* ((beforepoint (substring string 0 point))
(afterpoint (substring string point))
@@ -4233,7 +4258,7 @@ completion-pcm--find-all-completions
(setq string (substring string (car bounds) (+ point (cdr bounds))))
(let* ((relpoint (- point (car bounds)))
(pattern (completion-pcm--optimize-pattern
- (completion-pcm--string->pattern string relpoint)))
+ (completion-pcm--string->pattern string relpoint startglob)))
(all (condition-case-unless-debug err
(funcall filter
(completion-pcm--all-completions
@@ -4311,9 +4336,9 @@ completion-pcm--find-all-completions
(signal (car firsterror) (cdr firsterror))
(list pattern all prefix suffix)))))
-(defun completion-pcm-all-completions (string table pred point)
+(defun completion-pcm-all-completions (string table pred point &optional startglob)
(pcase-let ((`(,pattern ,all ,prefix ,_suffix)
- (completion-pcm--find-all-completions string table pred point)))
+ (completion-pcm--find-all-completions string table pred point nil startglob)))
(when all
(nconc (completion-pcm--hilit-commonality pattern all)
(length prefix)))))
@@ -4489,17 +4514,25 @@ completion-pcm--merge-try
merged (max 0 (1- (length merged))) suffix))
(cons (concat prefix merged suffix) (+ newpos (length prefix)))))))
-(defun completion-pcm-try-completion (string table pred point)
+(defun completion-pcm-try-completion (string table pred point &optional startglob)
(pcase-let ((`(,pattern ,all ,prefix ,suffix)
(completion-pcm--find-all-completions
string table pred point
(if minibuffer-completing-file-name
- 'completion-pcm--filename-try-filter))))
+ 'completion-pcm--filename-try-filter)
+ startglob)))
(completion-pcm--merge-try pattern all prefix suffix)))
;;; Substring completion
;; Mostly derived from the code of `basic' completion.
+(defcustom completion-substring-use-pcm nil
+ "If non-nil, the substring completion style performs partial-completion.
+
+This means that in addition to expanding at the start of the
+completion region, all text will be expanded following the
+partial-completion rules.")
+
(defun completion-substring--all-completions
(string table pred point &optional transform-pattern-fn)
"Match the presumed substring STRING to the entries in TABLE.
@@ -4524,20 +4557,24 @@ completion-substring--all-completions
(list all pattern prefix suffix (car bounds))))
(defun completion-substring-try-completion (string table pred point)
- (pcase-let ((`(,all ,pattern ,prefix ,suffix ,_carbounds)
- (completion-substring--all-completions
- string table pred point)))
- (if minibuffer-completing-file-name
- (setq all (completion-pcm--filename-try-filter all)))
- (completion-pcm--merge-try pattern all prefix suffix)))
+ (if completion-substring-use-pcm
+ (completion-pcm-try-completion string table pred point t)
+ (pcase-let ((`(,all ,pattern ,prefix ,suffix ,_carbounds)
+ (completion-substring--all-completions
+ string table pred point)))
+ (if minibuffer-completing-file-name
+ (setq all (completion-pcm--filename-try-filter all)))
+ (completion-pcm--merge-try pattern all prefix suffix))))
(defun completion-substring-all-completions (string table pred point)
- (pcase-let ((`(,all ,pattern ,prefix ,_suffix ,_carbounds)
- (completion-substring--all-completions
- string table pred point)))
- (when all
- (nconc (completion-pcm--hilit-commonality pattern all)
- (length prefix)))))
+ (if completion-substring-use-pcm
+ (completion-pcm-all-completions string table pred point t)
+ (pcase-let ((`(,all ,pattern ,prefix ,_suffix ,_carbounds)
+ (completion-substring--all-completions
+ string table pred point)))
+ (when all
+ (nconc (completion-pcm--hilit-commonality pattern all)
+ (length prefix))))))
;;; "flex" completion, also known as flx/fuzzy/scatter completion
;; Completes "foo" to "frodo" and "farfromsober"
--
2.39.3
^ permalink raw reply related [flat|nested] 31+ messages in thread
* bug#70217: [PATCH] Add substring-partial-completion style
2024-05-08 16:46 ` Spencer Baugh
@ 2024-05-08 17:14 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-05-16 20:26 ` Spencer Baugh
0 siblings, 1 reply; 31+ messages in thread
From: Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2024-05-08 17:14 UTC (permalink / raw)
To: Spencer Baugh; +Cc: 70217
> * lisp/minibuffer.el (completion-emacs22-use-pcm)
> (completion-substring-use-pcm): Add. (bug#70217)
> (completion-emacs22-try-completion)
> (completion-emacs22-all-completions): Check completion-emacs22-use-pcm.
> (completion-pcm--string->pattern, completion-pcm--find-all-completions)
> (completion-pcm-all-completions, completion-pcm--merge-try)
> (completion-pcm-try-completion): Add "startglob" optional argument and
> pass through.
> (completion-substring-try-completion)
> (completion-substring-all-completions): Check
> completion-substring-use-pcm and pass startglob=t.
I'm not super happy about this `startglob` everywhere.
Two things bother me about it:
- Its name and doc (in my view of what "glob" means, there's no such
thing as a "leading glob"; I think you're talking about a leading
wildcard (which is one of the things that can appear in a glob
pattern)).
- Its spreading all over the place.
I don't have the time to dig into the second problem (which might be
fixable by combining this info into the `string` argument (which
wouldn't be just a string as more, so it could have too-far reaching
consequences)), but for the first I suspect a "standard" name for this
idea is `anchored`.
Other than that, I don't have a strong opinion on whether to introduce
the feature via new styles or as custom vars that affect
existing styles. Either way works for me.
Stefan
^ permalink raw reply [flat|nested] 31+ messages in thread
* bug#70217: [PATCH] Add substring-partial-completion style
2024-05-08 17:14 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
@ 2024-05-16 20:26 ` Spencer Baugh
2024-05-16 22:09 ` Daniel Mendler via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-05-17 6:23 ` Eli Zaretskii
0 siblings, 2 replies; 31+ messages in thread
From: Spencer Baugh @ 2024-05-16 20:26 UTC (permalink / raw)
To: Stefan Monnier; +Cc: 70217
Stefan Monnier <monnier@iro.umontreal.ca> writes:
>> * lisp/minibuffer.el (completion-emacs22-use-pcm)
>> (completion-substring-use-pcm): Add. (bug#70217)
>> (completion-emacs22-try-completion)
>> (completion-emacs22-all-completions): Check completion-emacs22-use-pcm.
>> (completion-pcm--string->pattern, completion-pcm--find-all-completions)
>> (completion-pcm-all-completions, completion-pcm--merge-try)
>> (completion-pcm-try-completion): Add "startglob" optional argument and
>> pass through.
>> (completion-substring-try-completion)
>> (completion-substring-all-completions): Check
>> completion-substring-use-pcm and pass startglob=t.
>
> I'm not super happy about this `startglob` everywhere.
> Two things bother me about it:
>
> - Its name and doc (in my view of what "glob" means, there's no such
> thing as a "leading glob"; I think you're talking about a leading
> wildcard (which is one of the things that can appear in a glob
> pattern)).
Yes, very true, changed to "anchored".
> - Its spreading all over the place.
In the attached diff it's a dynamic variable completion-pcm-anchored
checked in one place. How's that?
> I don't have the time to dig into the second problem (which might be
> fixable by combining this info into the `string` argument (which
> wouldn't be just a string as more, so it could have too-far reaching
> consequences)), but for the first I suspect a "standard" name for this
> idea is `anchored`.
>
> Other than that, I don't have a strong opinion on whether to introduce
> the feature via new styles or as custom vars that affect
> existing styles. Either way works for me.
As part of making it a dynamic variable, I also changed the usage. In
the attached diff, an element in completion-styles can contain
additional dynamic variable bindings:
(setq completion-styles '(basic (partial-completion (completion-pcm-anchored nil)) emacs22))
This is powerful and elegant in some ways - for example now a user could
use completion-ignore-case only for an individual style. Also, the
overall diff is now much shorter.
But it may be a bit too powerful.
Also, now completion uses cl-progv, which internally uses eval, which
might be undesirable. Maybe that could be solved with a C
implementation of cl-progv? I think a C implementation of cl-progv
might also be useful for my native modules, so I'd be happy to implement
that if it seems like a good idea.
Also, it's somewhat hard to expose this new power via customize (I
haven't bothered to update the customize type for it yet in this diff)
What do you think?
diff --git a/lisp/minibuffer.el b/lisp/minibuffer.el
index 5ce5aab5c7e..7110e7f573d 100644
--- a/lisp/minibuffer.el
+++ b/lisp/minibuffer.el
@@ -1218,11 +1218,18 @@ completion--nth-completion
(result-and-style
(completion--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 (cdr 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)
@@ -3780,6 +3787,17 @@ completion-pcm--pattern-trivial-p
(setq trivial nil)))
trivial)))
+(defcustom completion-pcm-anchored t
+ "If nil, the partial-completion style expands at the start of a string.
+
+If non-nil, then expansion at the start of a string only happens
+if the string begins with a wildcard.
+
+For example, if this is nil then \"b/c\" will match
+\"aaa/bbb/ccc\"."
+ :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
@@ -3830,7 +3848,12 @@ completion-pcm--string->pattern
(when (> (length string) p0)
(if pending (push pending pattern))
(push (substring string p0) pattern))
- (nreverse pattern))))
+ (setq pattern (nreverse pattern))
+ (unless completion-pcm-anchored
+ (when (stringp (car pattern))
+ (push 'prefix pattern)))
+ pattern)))
(defun completion-pcm--optimize-pattern (p)
;; Remove empty strings in a separate phase since otherwise a ""
^ permalink raw reply related [flat|nested] 31+ messages in thread
* bug#70217: [PATCH] Add substring-partial-completion style
2024-05-16 20:26 ` Spencer Baugh
@ 2024-05-16 22:09 ` Daniel Mendler via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-05-17 6:23 ` Eli Zaretskii
1 sibling, 0 replies; 31+ messages in thread
From: Daniel Mendler via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2024-05-16 22:09 UTC (permalink / raw)
To: Spencer Baugh; +Cc: 70217, Stefan Monnier
Spencer Baugh <sbaugh@janestreet.com> writes:
> Stefan Monnier <monnier@iro.umontreal.ca> writes:
>
>>> * lisp/minibuffer.el (completion-emacs22-use-pcm)
>>> (completion-substring-use-pcm): Add. (bug#70217)
>>> (completion-emacs22-try-completion)
>>> (completion-emacs22-all-completions): Check completion-emacs22-use-pcm.
>>> (completion-pcm--string->pattern, completion-pcm--find-all-completions)
>>> (completion-pcm-all-completions, completion-pcm--merge-try)
>>> (completion-pcm-try-completion): Add "startglob" optional argument and
>>> pass through.
>>> (completion-substring-try-completion)
>>> (completion-substring-all-completions): Check
>>> completion-substring-use-pcm and pass startglob=t.
>>
>> I'm not super happy about this `startglob` everywhere.
>> Two things bother me about it:
>>
>> - Its name and doc (in my view of what "glob" means, there's no such
>> thing as a "leading glob"; I think you're talking about a leading
>> wildcard (which is one of the things that can appear in a glob
>> pattern)).
>
> Yes, very true, changed to "anchored".
>
>> - Its spreading all over the place.
>
> In the attached diff it's a dynamic variable completion-pcm-anchored
> checked in one place. How's that?
>
>> I don't have the time to dig into the second problem (which might be
>> fixable by combining this info into the `string` argument (which
>> wouldn't be just a string as more, so it could have too-far reaching
>> consequences)), but for the first I suspect a "standard" name for this
>> idea is `anchored`.
>>
>> Other than that, I don't have a strong opinion on whether to introduce
>> the feature via new styles or as custom vars that affect
>> existing styles. Either way works for me.
>
> As part of making it a dynamic variable, I also changed the usage. In
> the attached diff, an element in completion-styles can contain
> additional dynamic variable bindings:
>
> (setq completion-styles '(basic (partial-completion (completion-pcm-anchored nil)) emacs22))
>
> This is powerful and elegant in some ways - for example now a user could
> use completion-ignore-case only for an individual style. Also, the
> overall diff is now much shorter.
This sounds great. It will also be useful in other settings. For example
the Orderless completion style can be configured via a bunch of dynamic
variables. Right now, in orderless.el we provide the helper macro
`orderless-define-completion-style' which creates a derived completion
style with a bunch of let-bound dynamic variables. Your new feature will
make it easier and more direct to configure custom derived completion
styles.
Daniel
^ permalink raw reply [flat|nested] 31+ messages in thread
* bug#70217: [PATCH] Add substring-partial-completion style
2024-05-16 20:26 ` Spencer Baugh
2024-05-16 22:09 ` Daniel Mendler via Bug reports for GNU Emacs, the Swiss army knife of text editors
@ 2024-05-17 6:23 ` Eli Zaretskii
2024-05-25 21:22 ` Spencer Baugh
1 sibling, 1 reply; 31+ messages in thread
From: Eli Zaretskii @ 2024-05-17 6:23 UTC (permalink / raw)
To: Spencer Baugh; +Cc: 70217, monnier
> Cc: 70217@debbugs.gnu.org
> From: Spencer Baugh <sbaugh@janestreet.com>
> Date: Thu, 16 May 2024 16:26:32 -0400
>
> +(defcustom completion-pcm-anchored t
> + "If nil, the partial-completion style expands at the start of a string.
How can a style "expand at the start of a string"? This sentence
needs to be reworded to tell the effect of the variable more clear.
> +If non-nil, then expansion at the start of a string only happens
> +if the string begins with a wildcard.
What is a "wildcard" in this context?
This is a user option, so the doc string should be understandable by
users, not just by Lisp programmers who tinker with completion stuff.
Also, this new option should be in NEWS, I think.
Thanks.
^ permalink raw reply [flat|nested] 31+ messages in thread
* bug#70217: [PATCH] Add substring-partial-completion style
2024-05-17 6:23 ` Eli Zaretskii
@ 2024-05-25 21:22 ` Spencer Baugh
2024-05-26 7:56 ` Michael Albinus via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-05-26 9:11 ` Eli Zaretskii
0 siblings, 2 replies; 31+ messages in thread
From: Spencer Baugh @ 2024-05-25 21:22 UTC (permalink / raw)
To: Eli Zaretskii; +Cc: 70217, monnier
[-- Attachment #1: Type: text/plain, Size: 932 bytes --]
Eli Zaretskii <eliz@gnu.org> writes:
>> Cc: 70217@debbugs.gnu.org
>> From: Spencer Baugh <sbaugh@janestreet.com>
>> Date: Thu, 16 May 2024 16:26:32 -0400
>>
>> +(defcustom completion-pcm-anchored t
>> + "If nil, the partial-completion style expands at the start of a string.
>
> How can a style "expand at the start of a string"? This sentence
> needs to be reworded to tell the effect of the variable more clear.
>
>> +If non-nil, then expansion at the start of a string only happens
>> +if the string begins with a wildcard.
>
> What is a "wildcard" in this context?
>
> This is a user option, so the doc string should be understandable by
> users, not just by Lisp programmers who tinker with completion stuff.
OK, reworded that docstring heavily. Maybe it's better now?
>
> Also, this new option should be in NEWS, I think.
Added to NEWS and documentation, and the same for the new possible
values of completion-styles.
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Allow-customizing-partial-completion-to-be-more-like.patch --]
[-- Type: text/x-patch, Size: 9090 bytes --]
From d042041accf927b6425a5e0331b19facbff20983 Mon Sep 17 00:00:00 2001
From: Spencer Baugh <sbaugh@janestreet.com>
Date: Sat, 25 May 2024 16:29:43 -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 | 17 ++++++++++--
etc/NEWS | 20 ++++++++++++++
lisp/minibuffer.el | 63 +++++++++++++++++++++++++++++++++++++++------
3 files changed, 90 insertions(+), 10 deletions(-)
diff --git a/doc/emacs/mini.texi b/doc/emacs/mini.texi
index 4557f41c3f7..2e9766eb50c 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,13 @@ Completion Styles
@dfn{wildcard}---it matches any string of characters at the
corresponding position in the completion alternative.
+@vindex completion-pcm-anchored
+By default, a matching completion alternative must have the same
+beginning as the first word of the minibuffer text, but if
+@code{completion-pcm-anchored} is nil, the first word of the minibuffer
+text can start anywhere in a completion alternative. For example,
+@samp{l-m} completes 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..be59cf37ad0 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-anchored'.
+This option configures how the partial-completion style does completion.
+It defaults to t, which preserves the existing behavior. When it is set
+to nil, 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
+An element of 'completion-styles' can be a symbol naming a completion
+style in 'completion-styles-alist'. Now it can also 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 duplicating a
+completion style with different values for completion-affecting
+variables like 'completion-pcm-anchored' 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..10bab12b3ba 100644
--- a/lisp/minibuffer.el
+++ b/lisp/minibuffer.el
@@ -1141,10 +1141,22 @@ 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."
+styles for specific categories, such as files, buffers, etc.
+
+Note that Tramp host name completion (e.g., \"/ssh:ho<TAB>\")
+currently doesn't work if this list doesn't contain at least one
+of `basic', `emacs22' or `emacs21'."
:type completion--styles-type
:version "23.1")
@@ -1284,11 +1296,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 +3883,30 @@ completion-pcm--pattern-trivial-p
(setq trivial nil)))
trivial)))
+(defcustom completion-pcm-anchored t
+ "If non-nil, PCM matches only against the start of completions.
+
+The partial-completion completion style filters the completion
+candidates based on whether they match the string being
+completed, according to the partial-completion rules.
+
+If this variable is non-nil, the string's match must begin at the
+start of a completion candidate.
+
+If this variable is nil, the string's match can begin anywhere in
+the completion candidate, similar to the substring completion
+style. The behavior is identical to inserting a \"*\" at the
+start of the completion string. Note that this can be slower to
+compute since less filtering of the completion candidates is
+possible.
+
+For example: when the string being completed is \"b/c\",
+\"bbb/ccc\" is a valid completion according to
+partial-completion. If this is nil, then \"aaa/bbb/ccc\" and
+\"aaabbb/ccc\" are also valid completions."
+ :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 +3957,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))
+ (unless completion-pcm-anchored
+ (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
^ permalink raw reply related [flat|nested] 31+ messages in thread
* bug#70217: [PATCH] Add substring-partial-completion style
2024-05-25 21:22 ` Spencer Baugh
@ 2024-05-26 7:56 ` Michael Albinus via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-05-26 12:49 ` Spencer Baugh
2024-05-26 9:11 ` Eli Zaretskii
1 sibling, 1 reply; 31+ messages in thread
From: Michael Albinus via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2024-05-26 7:56 UTC (permalink / raw)
To: Spencer Baugh; +Cc: Eli Zaretskii, 70217, monnier
Spencer Baugh <sbaugh@janestreet.com> writes:
Hi Spencer,
> --- a/lisp/minibuffer.el
> +++ b/lisp/minibuffer.el
> @@ -1141,10 +1141,22 @@ 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."
> +styles for specific categories, such as files, buffers, etc.
> +
> +Note that Tramp host name completion (e.g., \"/ssh:ho<TAB>\")
> +currently doesn't work if this list doesn't contain at least one
> +of `basic', `emacs22' or `emacs21'."
Are you sure? I thought this general problem was fixed in Tramp 2.6.1
(Emacs 29.2). What's left are problems of Tramp with the `initials' and
`shorthand' completion styles. See
`tramp-test26-interactive-file-name-completion':
--8<---------------cut here---------------start------------->8---
(dolist
(style
(if (tramp--test-expensive-test-p)
;; It doesn't work for `initials' and `shorthand'
;; completion styles. Should it?
;; `orderless' passes the tests, but it is an ELPA package.
'(emacs21 emacs22 basic partial-completion substring flex)
'(basic)))
...
--8<---------------cut here---------------end--------------->8---
Shall I extend the test for `substring-partial-completion'?
Best regards, Michael.
^ permalink raw reply [flat|nested] 31+ messages in thread
* bug#70217: [PATCH] Add substring-partial-completion style
2024-05-25 21:22 ` Spencer Baugh
2024-05-26 7:56 ` Michael Albinus via Bug reports for GNU Emacs, the Swiss army knife of text editors
@ 2024-05-26 9:11 ` Eli Zaretskii
2024-05-26 13:02 ` Spencer Baugh
1 sibling, 1 reply; 31+ messages in thread
From: Eli Zaretskii @ 2024-05-26 9:11 UTC (permalink / raw)
To: Spencer Baugh; +Cc: 70217, monnier
> From: Spencer Baugh <sbaugh@janestreet.com>
> Cc: 70217@debbugs.gnu.org, monnier@iro.umontreal.ca
> Date: Sat, 25 May 2024 17:22:14 -0400
>
> > This is a user option, so the doc string should be understandable by
> > users, not just by Lisp programmers who tinker with completion stuff.
>
> OK, reworded that docstring heavily. Maybe it's better now?
See below.
> > Also, this new option should be in NEWS, I think.
>
> Added to NEWS and documentation, and the same for the new possible
> values of completion-styles.
Thanks.
> ++++
> +*** 'completion-styles' now can contain lists of bindings
^
A period is missing there
> +An element of 'completion-styles' can be a symbol naming a completion
> +style in 'completion-styles-alist'. Now it can also be a list of the
This is better rephrased like this:
In addition to being a symbol naming a completion style, an element
of 'completion-styles' can now be a list of the form...
> +form '(style ((variable value) ...))' where style is a symbol naming a
We usually up-case the meta-syntactic variables in these cases, so
(STYLE ((VARIABLE VALUE) ...)
> +completion style. 'variable' will be bound to 'value' (without
Up-case "variable" and "value", and don't quote them.
> +evaluating it) while the style is executing. This allows duplicating a
> +completion style with different values for completion-affecting
Not "duplicating" (which sounds negative), but something like
"multiple references to the same style".
> +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.
Here, too, up-case STYLE, VARIABLE, and VALUE, and don't quote them,
since they are not literal symbols, they are "placeholders" --
references to something else.
> +(defcustom completion-pcm-anchored t
> + "If non-nil, PCM matches only against the start of completions.
What is a "PCM"? And what are "completions"? I'm guessing you meant
something like
If non-nil, completion candidates must match at beginning of completed string.
> +For example: when the string being completed is \"b/c\",
> +\"bbb/ccc\" is a valid completion according to
> +partial-completion. If this is nil, then \"aaa/bbb/ccc\" and
> +\"aaabbb/ccc\" are also valid completions."
Isn't this not the best example? "b/c" does NOT match "bbb/ccc" at
the beginning. Or what am I missing?
Thanks.
^ permalink raw reply [flat|nested] 31+ messages in thread
* bug#70217: [PATCH] Add substring-partial-completion style
2024-05-26 7:56 ` Michael Albinus via Bug reports for GNU Emacs, the Swiss army knife of text editors
@ 2024-05-26 12:49 ` Spencer Baugh
0 siblings, 0 replies; 31+ messages in thread
From: Spencer Baugh @ 2024-05-26 12:49 UTC (permalink / raw)
To: Michael Albinus; +Cc: Eli Zaretskii, 70217, monnier
Michael Albinus <michael.albinus@gmx.de> writes:
> Spencer Baugh <sbaugh@janestreet.com> writes:
>
> Hi Spencer,
>
>> --- a/lisp/minibuffer.el
>> +++ b/lisp/minibuffer.el
>> @@ -1141,10 +1141,22 @@ 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."
>> +styles for specific categories, such as files, buffers, etc.
>> +
>> +Note that Tramp host name completion (e.g., \"/ssh:ho<TAB>\")
>> +currently doesn't work if this list doesn't contain at least one
>> +of `basic', `emacs22' or `emacs21'."
>
> Are you sure? I thought this general problem was fixed in Tramp 2.6.1
> (Emacs 29.2).
Oh, sorry, this was just an accident, I wrote this docstring on emacs-29
(which has this note about Tramp) then copy-pasted it to master. Will
delete.
^ permalink raw reply [flat|nested] 31+ messages in thread
* bug#70217: [PATCH] Add substring-partial-completion style
2024-05-26 9:11 ` Eli Zaretskii
@ 2024-05-26 13:02 ` Spencer Baugh
2024-05-26 15:51 ` Eli Zaretskii
0 siblings, 1 reply; 31+ messages in thread
From: Spencer Baugh @ 2024-05-26 13:02 UTC (permalink / raw)
To: Eli Zaretskii; +Cc: 70217, monnier
[-- Attachment #1: Type: text/plain, Size: 3406 bytes --]
Eli Zaretskii <eliz@gnu.org> writes:
>> From: Spencer Baugh <sbaugh@janestreet.com>
>> Cc: 70217@debbugs.gnu.org, monnier@iro.umontreal.ca
>> Date: Sat, 25 May 2024 17:22:14 -0400
>>
>> > This is a user option, so the doc string should be understandable by
>> > users, not just by Lisp programmers who tinker with completion stuff.
>>
>> OK, reworded that docstring heavily. Maybe it's better now?
>
> See below.
>
>> > Also, this new option should be in NEWS, I think.
>>
>> Added to NEWS and documentation, and the same for the new possible
>> values of completion-styles.
>
> Thanks.
>
>> ++++
>> +*** 'completion-styles' now can contain lists of bindings
> ^
> A period is missing there
Fixed.
>> +An element of 'completion-styles' can be a symbol naming a completion
>> +style in 'completion-styles-alist'. Now it can also be a list of the
>
> This is better rephrased like this:
>
> In addition to being a symbol naming a completion style, an element
> of 'completion-styles' can now be a list of the form...
Fixed
>> +form '(style ((variable value) ...))' where style is a symbol naming a
>
> We usually up-case the meta-syntactic variables in these cases, so
>
> (STYLE ((VARIABLE VALUE) ...)
Fixed
>> +completion style. 'variable' will be bound to 'value' (without
>
> Up-case "variable" and "value", and don't quote them.
Fixed
>> +evaluating it) while the style is executing. This allows duplicating a
>> +completion style with different values for completion-affecting
>
> Not "duplicating" (which sounds negative), but something like
> "multiple references to the same style".
Fixed
>> +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.
>
> Here, too, up-case STYLE, VARIABLE, and VALUE, and don't quote them,
> since they are not literal symbols, they are "placeholders" --
> references to something else.
Fixed
>> +(defcustom completion-pcm-anchored t
>> + "If non-nil, PCM matches only against the start of completions.
>
> What is a "PCM"? And what are "completions"? I'm guessing you meant
> something like
>
> If non-nil, completion candidates must match at beginning of completed string.
PCM is the abbreviation for partial-completion. This variable only
affects the partial-completion style. I was having trouble including
the entire string "partial-completion" in the first line, and also
explaining what the variable does, in under 80 characters.
Changed to not mention partial-completion in the first line for now at
least.
>> +For example: when the string being completed is \"b/c\",
>> +\"bbb/ccc\" is a valid completion according to
>> +partial-completion. If this is nil, then \"aaa/bbb/ccc\" and
>> +\"aaabbb/ccc\" are also valid completions."
>
> Isn't this not the best example? "b/c" does NOT match "bbb/ccc" at
> the beginning. Or what am I missing?
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.
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Allow-customizing-partial-completion-to-be-more-like.patch --]
[-- Type: text/x-patch, Size: 8797 bytes --]
From 04d8524363c90a79a8ebb82ede85c977bdfeebd7 Mon Sep 17 00:00:00 2001
From: Spencer Baugh <sbaugh@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 | 17 ++++++++++++--
etc/NEWS | 20 ++++++++++++++++
lisp/minibuffer.el | 57 +++++++++++++++++++++++++++++++++++++++------
3 files changed, 85 insertions(+), 9 deletions(-)
diff --git a/doc/emacs/mini.texi b/doc/emacs/mini.texi
index 4557f41c3f7..2e9766eb50c 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,13 @@ Completion Styles
@dfn{wildcard}---it matches any string of characters at the
corresponding position in the completion alternative.
+@vindex completion-pcm-anchored
+By default, a matching completion alternative must have the same
+beginning as the first word of the minibuffer text, but if
+@code{completion-pcm-anchored} is nil, the first word of the minibuffer
+text can start anywhere in a completion alternative. For example,
+@samp{l-m} completes 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..596a2186db5 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-anchored'.
+This option configures how the partial-completion style does completion.
+It defaults to t, which preserves the existing behavior. When it is set
+to nil, 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-anchored' 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..48e41492515 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,30 @@ completion-pcm--pattern-trivial-p
(setq trivial nil)))
trivial)))
+(defcustom completion-pcm-anchored t
+ "If non-nil, completion candidates must match at beginning of completed string.
+
+This only affects the partial-completion style, which filters the
+completion candidates based on whether they match the string being
+completed, according to the partial-completion rules.
+
+If this variable is non-nil, the string's match must begin at the
+start of a completion candidate.
+
+If this variable is nil, the string's match can begin anywhere in
+the completion candidate, similar to the substring completion
+style. The behavior is identical to inserting a \"*\" at the
+start of the completion string. Note that this can be slower to
+compute since less filtering of the completion candidates is
+possible.
+
+For example: when the string being completed is \"b/c\",
+\"bbb/ccc\" is a valid completion according to
+partial-completion. If this is nil, then \"aaa/bbb/ccc\" and
+\"aaabbb/ccc\" are also valid completions."
+ :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 +3953,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))
+ (unless completion-pcm-anchored
+ (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
^ permalink raw reply related [flat|nested] 31+ messages in thread
* bug#70217: [PATCH] Add substring-partial-completion style
2024-05-26 13:02 ` Spencer Baugh
@ 2024-05-26 15:51 ` Eli Zaretskii
2024-05-28 14:39 ` Spencer Baugh
0 siblings, 1 reply; 31+ messages in thread
From: Eli Zaretskii @ 2024-05-26 15:51 UTC (permalink / raw)
To: Spencer Baugh; +Cc: 70217, monnier
> From: Spencer Baugh <sbaugh@janestreet.com>
> Cc: 70217@debbugs.gnu.org, monnier@iro.umontreal.ca
> Date: Sun, 26 May 2024 09:02:13 -0400
>
> >> +For example: when the string being completed is \"b/c\",
> >> +\"bbb/ccc\" is a valid completion according to
> >> +partial-completion. If this is nil, then \"aaa/bbb/ccc\" and
> >> +\"aaabbb/ccc\" are also valid completions."
> >
> > Isn't this not the best example? "b/c" does NOT match "bbb/ccc" at
> > the beginning. Or what am I missing?
>
> 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.
^ permalink raw reply [flat|nested] 31+ messages in thread
* bug#70217: [PATCH] Add substring-partial-completion style
2024-05-26 15:51 ` Eli Zaretskii
@ 2024-05-28 14:39 ` Spencer Baugh
2024-05-28 15:11 ` Eli Zaretskii
0 siblings, 1 reply; 31+ messages in thread
From: Spencer Baugh @ 2024-05-28 14:39 UTC (permalink / raw)
To: Eli Zaretskii; +Cc: 70217, monnier
[-- Attachment #1: Type: text/plain, Size: 1194 bytes --]
Eli Zaretskii <eliz@gnu.org> writes:
>> From: Spencer Baugh <sbaugh@janestreet.com>
>> Cc: 70217@debbugs.gnu.org, monnier@iro.umontreal.ca
>> Date: Sun, 26 May 2024 09:02:13 -0400
>>
>> >> +For example: when the string being completed is \"b/c\",
>> >> +\"bbb/ccc\" is a valid completion according to
>> >> +partial-completion. If this is nil, then \"aaa/bbb/ccc\" and
>> >> +\"aaabbb/ccc\" are also valid completions."
>> >
>> > Isn't this not the best example? "b/c" does NOT match "bbb/ccc" at
>> > the beginning. Or what am I missing?
>>
>> 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)
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Allow-customizing-partial-completion-to-be-more-like.patch --]
[-- Type: text/x-patch, Size: 8443 bytes --]
From 2190bd795c907e100636ffecd7822d7cd760bf10 Mon Sep 17 00:00:00 2001
From: Spencer Baugh <sbaugh@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..c59f1edd410 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 \"b/c\" as if it was the glob \"b*/c*\". This means \"bbb/ccc\" is
+a valid completion, but not \"aaa/bbb/ccc\" or \"aaabbb/ccc\".
+
+If non-nil, the partial-completion style completes \"b/c\" as if it was
+the glob \"*b*/c*\". This means \"bbb/ccc\", \"aaa/bbb/ccc\" and
+\"aaabbb/ccc\" 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
^ permalink raw reply related [flat|nested] 31+ messages in thread
* bug#70217: [PATCH] Add substring-partial-completion style
2024-05-28 14:39 ` Spencer Baugh
@ 2024-05-28 15:11 ` Eli Zaretskii
2024-05-28 18:16 ` Spencer Baugh
0 siblings, 1 reply; 31+ messages in thread
From: Eli Zaretskii @ 2024-05-28 15:11 UTC (permalink / raw)
To: Spencer Baugh; +Cc: 70217, monnier
> From: Spencer Baugh <sbaugh@janestreet.com>
> Cc: 70217@debbugs.gnu.org, monnier@iro.umontreal.ca
> Date: Tue, 28 May 2024 10:39:52 -0400
>
> Eli Zaretskii <eliz@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. But please (a) don't use "glob" and
file wildcard notation, use regexps instead; 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.
^ permalink raw reply [flat|nested] 31+ messages in thread
* bug#70217: [PATCH] Add substring-partial-completion style
2024-05-28 15:11 ` Eli Zaretskii
@ 2024-05-28 18:16 ` Spencer Baugh
2024-05-28 18:36 ` Eli Zaretskii
0 siblings, 1 reply; 31+ messages in thread
From: Spencer Baugh @ 2024-05-28 18:16 UTC (permalink / raw)
To: Eli Zaretskii; +Cc: 70217, monnier
[-- Attachment #1: Type: text/plain, Size: 1950 bytes --]
Eli Zaretskii <eliz@gnu.org> writes:
>> From: Spencer Baugh <sbaugh@janestreet.com>
>> Cc: 70217@debbugs.gnu.org, monnier@iro.umontreal.ca
>> Date: Tue, 28 May 2024 10:39:52 -0400
>>
>> Eli Zaretskii <eliz@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 "*".
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Allow-customizing-partial-completion-to-be-more-like.patch --]
[-- Type: text/x-patch, Size: 8429 bytes --]
From b789f57aefd565bdbb8626fce129aa65e7a23af3 Mon Sep 17 00:00:00 2001
From: Spencer Baugh <sbaugh@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
^ permalink raw reply related [flat|nested] 31+ messages in thread
* bug#70217: [PATCH] Add substring-partial-completion style
2024-05-28 18:16 ` Spencer Baugh
@ 2024-05-28 18:36 ` Eli Zaretskii
2024-05-28 18:51 ` Spencer Baugh
0 siblings, 1 reply; 31+ messages in thread
From: Eli Zaretskii @ 2024-05-28 18:36 UTC (permalink / raw)
To: Spencer Baugh; +Cc: 70217, monnier
> From: Spencer Baugh <sbaugh@janestreet.com>
> Cc: 70217@debbugs.gnu.org, monnier@iro.umontreal.ca
> Date: Tue, 28 May 2024 14:16:30 -0400
>
> > 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.
Thanks.
> > 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 "*".
Please don't. I really meant what I wrote: "glob" is confusing to
users, because of the file-name wildcards connotation.
The natural way of describing string patterns in Emacs is regular
expressions, not globs.
^ permalink raw reply [flat|nested] 31+ messages in thread
* bug#70217: [PATCH] Add substring-partial-completion style
2024-05-28 18:36 ` Eli Zaretskii
@ 2024-05-28 18:51 ` Spencer Baugh
2024-05-28 19:21 ` Eli Zaretskii
0 siblings, 1 reply; 31+ messages in thread
From: Spencer Baugh @ 2024-05-28 18:51 UTC (permalink / raw)
To: Eli Zaretskii; +Cc: 70217, monnier
Eli Zaretskii <eliz@gnu.org> writes:
>> From: Spencer Baugh <sbaugh@janestreet.com>
>> Cc: 70217@debbugs.gnu.org, monnier@iro.umontreal.ca
>> Date: Tue, 28 May 2024 14:16:30 -0400
>>
>> > 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.
>
> Thanks.
>
>> > 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 "*".
>
> Please don't. I really meant what I wrote: "glob" is confusing to
> users, because of the file-name wildcards connotation.
>
> The natural way of describing string patterns in Emacs is regular
> expressions, not globs.
Just to be clear, if you type C-h v ffap-*-path TAB it will complete to
variables whose name starts with "ffap-" and end with "-path". This is
a partial-completion feature which has nothing to do with globs.
I agree that the natural way of describing string patterns in Emacs is
regular expressions, not globs. There are no globs in this docstring.
I am mentioning only * which is what partial-completion natively
supports. * has nothing to do with globs, it is a feature of
partial-completion which is similar but distinct from shell globs.
partial-completion works in terms of * not regular expressions, so it
would be confusing to use a regular expression here.
^ permalink raw reply [flat|nested] 31+ messages in thread
* bug#70217: [PATCH] Add substring-partial-completion style
2024-05-28 18:51 ` Spencer Baugh
@ 2024-05-28 19:21 ` Eli Zaretskii
2024-05-28 20:01 ` Spencer Baugh
0 siblings, 1 reply; 31+ messages in thread
From: Eli Zaretskii @ 2024-05-28 19:21 UTC (permalink / raw)
To: Spencer Baugh; +Cc: 70217, monnier
> From: Spencer Baugh <sbaugh@janestreet.com>
> Cc: 70217@debbugs.gnu.org, monnier@iro.umontreal.ca
> Date: Tue, 28 May 2024 14:51:37 -0400
>
> Eli Zaretskii <eliz@gnu.org> writes:
>
> >> From: Spencer Baugh <sbaugh@janestreet.com>
> >> Cc: 70217@debbugs.gnu.org, monnier@iro.umontreal.ca
> >> Date: Tue, 28 May 2024 14:16:30 -0400
> >>
> >> > 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.
> >
> > Thanks.
> >
> >> > 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 "*".
> >
> > Please don't. I really meant what I wrote: "glob" is confusing to
> > users, because of the file-name wildcards connotation.
> >
> > The natural way of describing string patterns in Emacs is regular
> > expressions, not globs.
>
> Just to be clear, if you type C-h v ffap-*-path TAB it will complete to
> variables whose name starts with "ffap-" and end with "-path". This is
> a partial-completion feature which has nothing to do with globs.
>
> I agree that the natural way of describing string patterns in Emacs is
> regular expressions, not globs. There are no globs in this docstring.
> I am mentioning only * which is what partial-completion natively
> supports. * has nothing to do with globs, it is a feature of
> partial-completion which is similar but distinct from shell globs.
>
> partial-completion works in terms of * not regular expressions, so it
> would be confusing to use a regular expression here.
I know. But you are not talking about partial completion in that
text, you are talking about strings that match or don't match. The
natural way of describing those is regular expressions.
If you don't want to use regular expressions here, you will need to
explain them in words. That's fine by me, but it will make the job
harder for you, and the text will be longer. That's why I suggested
to use regexps.
^ permalink raw reply [flat|nested] 31+ messages in thread
* bug#70217: [PATCH] Add substring-partial-completion style
2024-05-28 19:21 ` Eli Zaretskii
@ 2024-05-28 20:01 ` Spencer Baugh
2024-06-01 14:20 ` Eli Zaretskii
0 siblings, 1 reply; 31+ messages in thread
From: Spencer Baugh @ 2024-05-28 20:01 UTC (permalink / raw)
To: Eli Zaretskii; +Cc: 70217, monnier
Eli Zaretskii <eliz@gnu.org> writes:
>> From: Spencer Baugh <sbaugh@janestreet.com>
>> Cc: 70217@debbugs.gnu.org, monnier@iro.umontreal.ca
>> Date: Tue, 28 May 2024 14:51:37 -0400
>>
>> Eli Zaretskii <eliz@gnu.org> writes:
>>
>> >> From: Spencer Baugh <sbaugh@janestreet.com>
>> >> Cc: 70217@debbugs.gnu.org, monnier@iro.umontreal.ca
>> >> Date: Tue, 28 May 2024 14:16:30 -0400
>> >>
>> >> > 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.
>> >
>> > Thanks.
>> >
>> >> > 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 "*".
>> >
>> > Please don't. I really meant what I wrote: "glob" is confusing to
>> > users, because of the file-name wildcards connotation.
>> >
>> > The natural way of describing string patterns in Emacs is regular
>> > expressions, not globs.
>>
>> Just to be clear, if you type C-h v ffap-*-path TAB it will complete to
>> variables whose name starts with "ffap-" and end with "-path". This is
>> a partial-completion feature which has nothing to do with globs.
>>
>> I agree that the natural way of describing string patterns in Emacs is
>> regular expressions, not globs. There are no globs in this docstring.
>> I am mentioning only * which is what partial-completion natively
>> supports. * has nothing to do with globs, it is a feature of
>> partial-completion which is similar but distinct from shell globs.
>>
>> partial-completion works in terms of * not regular expressions, so it
>> would be confusing to use a regular expression here.
>
> I know. But you are not talking about partial completion in that
> text, you are talking about strings that match or don't match. The
> natural way of describing those is regular expressions.
Okay, I'll remove mention of matching, and try to make it clear that I'm
just talking about completion. How about one of the following two
options?
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.
Or:
If non-nil, partial-completion completes as if there's a leading wildcard.
partial-completion interprets "*" as a wildcard, replacing it with any
string of characters while completing. partial-completion also adds
wildcards before characters in `completion-pcm-word-delimiters', and
adds a wildcard at the end of the minibuffer text; the behavior is
identical to typing "*" at all of those sites.
If nil (the default), "a/b" will complete the same as if you had typed
"a*/b*".
If non-nil, partial-completion additionally adds a wildcard at the start
of the string; then "a/b" will complete the same as if you had typed
"*a*/b*". This makes partial-completion behave more like the substring
completion style.
^ permalink raw reply [flat|nested] 31+ messages in thread
* bug#70217: [PATCH] Add substring-partial-completion style
2024-05-28 20:01 ` Spencer Baugh
@ 2024-06-01 14:20 ` Eli Zaretskii
2024-06-02 12:16 ` Spencer Baugh
0 siblings, 1 reply; 31+ messages in thread
From: Eli Zaretskii @ 2024-06-01 14:20 UTC (permalink / raw)
To: Spencer Baugh; +Cc: 70217, monnier
> From: Spencer Baugh <sbaugh@janestreet.com>
> Cc: 70217@debbugs.gnu.org, monnier@iro.umontreal.ca
> Date: Tue, 28 May 2024 16:01:53 -0400
>
> Okay, I'll remove mention of matching, and try to make it clear that I'm
> just talking about completion. How about one of the following two
> options?
Thanks, I think the first one is better.
^ permalink raw reply [flat|nested] 31+ messages in thread
* bug#70217: [PATCH] Add substring-partial-completion style
2024-06-01 14:20 ` Eli Zaretskii
@ 2024-06-02 12:16 ` Spencer Baugh
2024-06-02 14:34 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
0 siblings, 1 reply; 31+ messages in thread
From: Spencer Baugh @ 2024-06-02 12:16 UTC (permalink / raw)
To: Eli Zaretskii; +Cc: 70217, monnier
[-- Attachment #1: Type: text/plain, Size: 448 bytes --]
It seems like we're almost ready to install this patch.
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.
Eli Zaretskii <eliz@gnu.org> writes:
> Thanks, I think the first one is better.
Updated with the new docstring.
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Allow-customizing-partial-completion-to-be-more-like.patch --]
[-- Type: text/x-patch, Size: 8458 bytes --]
From 9f90123afe89e64861d6dca6d4c39a6387e0e720 Mon Sep 17 00:00:00 2001
From: Spencer Baugh <sbaugh@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 3c672ffed8f..db1582b86f6 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -1868,6 +1868,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..62d113499dd 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), 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
@@ -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
^ permalink raw reply related [flat|nested] 31+ messages in thread
* bug#70217: [PATCH] Add substring-partial-completion style
2024-06-02 12:16 ` Spencer Baugh
@ 2024-06-02 14:34 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-08-08 13:13 ` Spencer Baugh
0 siblings, 1 reply; 31+ messages in thread
From: Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2024-06-02 14:34 UTC (permalink / raw)
To: Spencer Baugh; +Cc: Eli Zaretskii, 70217
> 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
^ permalink raw reply [flat|nested] 31+ messages in thread
* bug#70217: [PATCH] Add substring-partial-completion style
2024-06-02 14:34 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
@ 2024-08-08 13:13 ` Spencer Baugh
2024-08-10 8:02 ` Eli Zaretskii
0 siblings, 1 reply; 31+ messages in thread
From: Spencer Baugh @ 2024-08-08 13:13 UTC (permalink / raw)
To: Stefan Monnier; +Cc: Eli Zaretskii, 70217
Stefan Monnier <monnier@iro.umontreal.ca> 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?
^ permalink raw reply [flat|nested] 31+ messages in thread
* bug#70217: [PATCH] Add substring-partial-completion style
2024-08-08 13:13 ` Spencer Baugh
@ 2024-08-10 8:02 ` Eli Zaretskii
2024-08-21 16:01 ` Spencer Baugh
0 siblings, 1 reply; 31+ messages in thread
From: Eli Zaretskii @ 2024-08-10 8:02 UTC (permalink / raw)
To: Spencer Baugh; +Cc: 70217, monnier
> From: Spencer Baugh <sbaugh@janestreet.com>
> Cc: Eli Zaretskii <eliz@gnu.org>, 70217@debbugs.gnu.org
> Date: Thu, 08 Aug 2024 09:13:16 -0400
>
> Stefan Monnier <monnier@iro.umontreal.ca> 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?
^ permalink raw reply [flat|nested] 31+ messages in thread
* bug#70217: [PATCH] Add substring-partial-completion style
2024-08-10 8:02 ` Eli Zaretskii
@ 2024-08-21 16:01 ` Spencer Baugh
2024-08-21 16:16 ` Spencer Baugh
0 siblings, 1 reply; 31+ messages in thread
From: Spencer Baugh @ 2024-08-21 16:01 UTC (permalink / raw)
To: Eli Zaretskii; +Cc: 70217, monnier
[-- Attachment #1: Type: text/plain, Size: 772 bytes --]
Eli Zaretskii <eliz@gnu.org> writes:
>> From: Spencer Baugh <sbaugh@janestreet.com>
>> Cc: Eli Zaretskii <eliz@gnu.org>, 70217@debbugs.gnu.org
>> Date: Thu, 08 Aug 2024 09:13:16 -0400
>>
>> Stefan Monnier <monnier@iro.umontreal.ca> 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:
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Allow-customizing-partial-completion-to-be-more-like.patch --]
[-- Type: text/x-patch, Size: 8398 bytes --]
From 02a8b19c0e2d9650185a5ea1a16f540d38b1ead2 Mon Sep 17 00:00:00 2001
From: Spencer Baugh <sbaugh@janestreet.com>
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'.
+
\f
** 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
^ permalink raw reply related [flat|nested] 31+ messages in thread
* bug#70217: [PATCH] Add substring-partial-completion style
2024-08-21 16:01 ` Spencer Baugh
@ 2024-08-21 16:16 ` Spencer Baugh
2024-08-24 9:23 ` Eli Zaretskii
0 siblings, 1 reply; 31+ messages in thread
From: Spencer Baugh @ 2024-08-21 16:16 UTC (permalink / raw)
To: Eli Zaretskii; +Cc: 70217, monnier
[-- Attachment #1: Type: text/plain, Size: 885 bytes --]
Spencer Baugh <sbaugh@janestreet.com> writes:
> Eli Zaretskii <eliz@gnu.org> writes:
>
>>> From: Spencer Baugh <sbaugh@janestreet.com>
>>> Cc: Eli Zaretskii <eliz@gnu.org>, 70217@debbugs.gnu.org
>>> Date: Thu, 08 Aug 2024 09:13:16 -0400
>>>
>>> Stefan Monnier <monnier@iro.umontreal.ca> 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:
Oops, also updated the commit message:
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Allow-customizing-partial-completion-to-be-more-like.patch --]
[-- Type: text/x-patch, Size: 8471 bytes --]
From f2eaf8a1694696dba4a5208620223cd6ac556fe7 Mon Sep 17 00:00:00 2001
From: Spencer Baugh <sbaugh@janestreet.com>
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-leading-wildcard variable
to a non-nil value.
Furthermore, it's convenient to be able to run
regular (completion-pcm-leading-wildcard=t, non-substring)
partial-completion before running completion-pcm-leading-wildcard=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-leading-wildcard t))) and
'(partial-completion ((completion-pcm-leading-wildcard nil))) in that
order. Then the completion machinery will first run partial-completion
with completion-pcm-leading-wildcard=t, and if that returns no
completions, run partial-completion with
completion-pcm-leading-wildcard=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-leading-wildcard): Add.
(completion-pcm--string->pattern): Check completion-pcm-leading-wildcard.
(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'.
+
\f
** 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
^ permalink raw reply related [flat|nested] 31+ messages in thread
* bug#70217: [PATCH] Add substring-partial-completion style
2024-08-21 16:16 ` Spencer Baugh
@ 2024-08-24 9:23 ` Eli Zaretskii
0 siblings, 0 replies; 31+ messages in thread
From: Eli Zaretskii @ 2024-08-24 9:23 UTC (permalink / raw)
To: Spencer Baugh; +Cc: 70217-done, monnier
> From: Spencer Baugh <sbaugh@janestreet.com>
> Cc: 70217@debbugs.gnu.org, monnier@iro.umontreal.ca
> Date: Wed, 21 Aug 2024 12:16:15 -0400
>
> Spencer Baugh <sbaugh@janestreet.com> writes:
>
> > Eli Zaretskii <eliz@gnu.org> writes:
> >
> >>> 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:
>
> Oops, also updated the commit message:
Thanks, installed on master, and closing the bug.
^ permalink raw reply [flat|nested] 31+ messages in thread
end of thread, other threads:[~2024-08-24 9:23 UTC | newest]
Thread overview: 31+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-04-05 12:41 bug#70217: [PATCH] Add substring-partial-completion style Spencer Baugh
2024-04-05 18:35 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-04-05 19:46 ` Drew Adams via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-04-06 8:10 ` Eshel Yaron via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-04-18 15:19 ` Spencer Baugh
2024-05-08 16:46 ` Spencer Baugh
2024-05-08 17:14 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-05-16 20:26 ` Spencer Baugh
2024-05-16 22:09 ` Daniel Mendler via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-05-17 6:23 ` Eli Zaretskii
2024-05-25 21:22 ` Spencer Baugh
2024-05-26 7:56 ` Michael Albinus via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-05-26 12:49 ` Spencer Baugh
2024-05-26 9:11 ` Eli Zaretskii
2024-05-26 13:02 ` Spencer Baugh
2024-05-26 15:51 ` Eli Zaretskii
2024-05-28 14:39 ` Spencer Baugh
2024-05-28 15:11 ` Eli Zaretskii
2024-05-28 18:16 ` Spencer Baugh
2024-05-28 18:36 ` Eli Zaretskii
2024-05-28 18:51 ` Spencer Baugh
2024-05-28 19:21 ` Eli Zaretskii
2024-05-28 20:01 ` Spencer Baugh
2024-06-01 14:20 ` Eli Zaretskii
2024-06-02 12:16 ` Spencer Baugh
2024-06-02 14:34 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-08-08 13:13 ` Spencer Baugh
2024-08-10 8:02 ` Eli Zaretskii
2024-08-21 16:01 ` Spencer Baugh
2024-08-21 16:16 ` Spencer Baugh
2024-08-24 9:23 ` Eli Zaretskii
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).