GNU bug report logs - #65137
29.1; completion-substring-try-completion doesn't return the longest common substring

Previous Next

Package: emacs;

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

Date: Mon, 7 Aug 2023 23:25:01 UTC

Severity: normal

Found in version 29.1

Fixed in version 30.1

Done: Spencer Baugh <sbaugh <at> janestreet.com>

Bug is archived. No further changes may be made.

Full log


View this message in rfc822 format

From: Spencer Baugh <sbaugh <at> janestreet.com>
To: 65137 <at> debbugs.gnu.org
Subject: bug#65137: 29.1; completion-substring-try-completion doesn't return the longest common substring
Date: Mon, 07 Aug 2023 20:41:12 -0400
[Message part 1 (text/plain, inline)]
Here's a patch fixing this.  It's definitely just a bug in the
implementation of completion-pcm--merge-completions - see my commit
message for details.

[0001-Correctly-handle-common-prefixes-in-substring-comple.patch (text/x-patch, inline)]
From ea63217c1c85f1ca4f1f22b9c4781167de6cf00d Mon Sep 17 00:00:00 2001
From: Spencer Baugh <sbaugh <at> janestreet.com>
Date: Mon, 7 Aug 2023 20:20:34 -0400
Subject: [PATCH] Correctly handle common prefixes in substring completion

substring completion is implemented by passing the `prefix' symbol as
part of the pattern passed to completion-pcm--merge-completions.  This
symbol is supposed to "grow" the completion only as a suffix, not as a
prefix.

The old behavior of completion-pcm--merge-completions when processing
a `prefix' element in the pattern was to find the common prefix of all
the completions in that part of the pattern (using try-completion) and
then completely discard that common prefix.  Then the actual logic for
`prefix' would run with completion--common-suffix.

However, if all the completion candidates had a common prefix while
processing a `prefix' element, then it would both discard the common
prefix *and* skip the completion--common-suffix logic.  If there was
also a common suffix, e.g. with the following invocation:

  (completion-pcm--merge-completions '("axbc" "aybc") '(prefix "c"))

then this would produce the wrong result by ignoring the common suffix
"b".

The new behavior is to simply not bother generating the common prefix
for `prefix' elements, since we don't use it anyway, and just pretend
it's always empty for `prefix' elements.  Then the
completion--common-suffix logic always runs.

* lisp/minibuffer.el (completion-pcm--merge-completions): Don't ignore
a common suffix in a `prefix' pattern element when there's also a
common prefix.
---
 lisp/minibuffer.el | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/lisp/minibuffer.el b/lisp/minibuffer.el
index d2c7e66d2a0..1aa29318bd2 100644
--- a/lisp/minibuffer.el
+++ b/lisp/minibuffer.el
@@ -4029,12 +4029,15 @@ completion-pcm--merge-completions
               ;; different capitalizations in different parts.
               ;; In practice, it doesn't seem to make any difference.
               (setq ccs (nreverse ccs))
-              (let* ((prefix (try-completion fixed comps))
+              (let* ((prefix
+                      ;; We pretend as if there's no common prefix at all for
+                      ;; `prefix', so we go to completion--common-suffix
+                      (if (eq elem 'prefix) "" (try-completion fixed comps)))
                      (unique (or (and (eq prefix t) (setq prefix fixed))
                                  (and (stringp prefix)
+                                      (not (string-empty-p prefix))
                                       (eq t (try-completion prefix comps))))))
-                (unless (or (eq elem 'prefix)
-                            (equal prefix ""))
+                (unless (equal prefix "")
                   (push prefix res))
                 ;; If there's only one completion, `elem' is not useful
                 ;; any more: it can only match the empty string.
-- 
2.39.3


This bug report was last modified 1 year and 281 days ago.

Previous Next


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