Subject: Re: bug#79219: [PATCH] Insert * on TAB with completion-pcm-leading-wildcard=t

From: Spencer Baugh <[email-masked]>
Date: Mon, 18 Aug 2025 15:01:32 -0400

Dmitry Gutov <[email-masked]> writes:

> Spencer, hi,
>
> On 11/08/2025 19:44, Spencer Baugh wrote:
>> Improve completion-pcm-leading-wildcard by actually inserting
>> a * when try-completion runs.  completion-pcm--merge-try will
>> automatically remove the wildcard anywhere that it has no
>> matches, so the * is inserted only where it actually had an
>> effect.
>
> Would it be difficult to add a test for this?
>
> I can see that you authored some of most recent ones in
> minibuffer-tests.el, but completion-pcm-leading-wildcard does not seem
> to be exercised in our suite yet.

Certainly, easy enough to do a basic test, attached.

Though, after working on this some more, I think there's a better thing
to be doing here, which should always apply to pcm rather than be
conditioned on completion-pcm-leading-wildcard: insert a * when a non-*
wildcard is matching against an empty string.

For example, when doing (completion-pcm-try-completion "a/b" '("a/bbb"
"a-test/bbb") nil 3) return "a*/bbb" instead of the current "a/bbb".

This matches up with this comment in completion-pcm--merge-completions:

  ;; FIXME: in some cases, it may be necessary to turn an
  ;; `any' into a `star' because the surrounding context has
  ;; changed such that string->pattern wouldn't add an `any'
  ;; here any more.

I tried doing that change instead, and it works pretty nicely, but
before landing it I found I had to fix bug#79265 first.  So let's settle
that one first then come back to this bug.

>From e3e8e2f90f33f03127e091388305251d26e5c233 Mon Sep 17 00:00:00 2001
From: Spencer Baugh <[email-masked]>
Date: Mon, 18 Aug 2025 14:51:33 -0400
Subject: [PATCH] add some leading-wildcard tests

---
 test/lisp/minibuffer-tests.el | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/test/lisp/minibuffer-tests.el b/test/lisp/minibuffer-tests.el
index 59b72899e22..2b22ee20b3f 100644
--- a/test/lisp/minibuffer-tests.el
+++ b/test/lisp/minibuffer-tests.el
@@ -332,6 +332,22 @@ completion-pcm-test-8
                   "" '("fooxbar" "fooybar") nil 0)
                  '("foobar" . 3))))
 
+(ert-deftest completion-pcm-test-9 ()
+  ;; By default completions must match the begining of the string.
+  (should (equal (completion-pcm-try-completion
+                  "oo" '("foo") nil 2)
+                 nil))
+  ;; But not with `completion-pcm-leading-wildcard'.
+  (should (equal (let ((completion-pcm-leading-wildcard t))
+                   (completion-pcm-try-completion
+                    "oo" '("foo") nil 2))
+                 '("foo" . 3)))
+  ;; The wildcard is actually inserted into the text.
+  (should (equal (let ((completion-pcm-leading-wildcard t))
+                   (completion-pcm-try-completion
+                    "oo" '("foo" "goo") nil 2))
+                 '("*oo" . 1))))
+
 (ert-deftest completion-substring-test-1 ()
   ;; One third of a match!
   (should (equal
-- 
2.43.7