Subject: Re: bug#72176: 30.0.60; icomplete-vertical-mode failed to work with Error

From: Eshel Yaron <[email-masked]>
Date: Sat, 20 Jul 2024 18:48:22 +0200

Eli Zaretskii <[email-masked]> writes:

>> From: João Távora <[email-masked]>
>> Date: Sat, 20 Jul 2024 10:09:38 +0100
>> Cc: Robert Pluim <[email-masked]>, [email-masked], [email-masked], 
>> 	[email-masked]
>> 
>> On Sat, Jul 20, 2024 at 7:05 AM Eli Zaretskii <[email-masked]> wrote:
>> 
>> > Adding João, who I think wrote this code.
>> 
>> Don't understand root cause, but it's in the whereabouts of
>> the current analysis, yes. I recall regexp is supposed to always 
>> match, because it has (presumably) been used for a previous
>> filtering. This breakage would seem to be a consequence of 
>> 3b07d33059150 by Eshel Yaron which in turn seems to be fixing 
>> something stemming from some feature Dmitry added in 
>> 6b7ff60a5e71c in June. I went one commit before that one and 
>> the recipe didn't break.
>
> Thanks, I'm adding the guilty parties to this discussion.

I'm afraid you missed your mark, because AFAICT it is Stefan Monnier
that's behind the root cause of this issue, which goes back way before
Dmitry's change or mine :)

It is a bug in completion--sifn-requote, that leads to incorrect
completion strings to be passed to completion-lazy-hilit.  It is not
related to icomplete per se.  To see the issue, try the following:

1. emacs -Q
2. say C-x C-f, clear the minibuffer, and insert ~/.config/~/f
3. hit ? to pop the completions list
4. see something like:

--8<---------------cut here---------------start------------->8---
Click or type M-RET on a completion to select it.
Type M-<down> or M-<up> to move point between completions.

6 possible completions:
.config/~/foo.json 	.config/~/foo.org 	.config/~/foo.php
.config/~/foo.py 	.config/~/foo.py~ 	.config/~/foo.rs
--8<---------------cut here---------------end--------------->8---

Note the incorrect prefix ".config/~/", completions should appear as
"foo.json", "foo.org", etc.

The following diff fixes this for me (including the icomplete symptom),
although I can't claim to fully understand completion--sifn-requote yet:

diff --git a/lisp/minibuffer.el b/lisp/minibuffer.el
index 31c365bf850..d0eb6b43c80 100644
--- a/lisp/minibuffer.el
+++ b/lisp/minibuffer.el
@@ -3855,13 +3855,13 @@ completion--sifn-requote
       ;; Second assumptions: If qpos is far from the end this can be a bit slow,
       ;; so we speed it up by doing a first loop that skips a word at a time.
       ;; This word-sized loop is careful not to cut in the middle of env-vars.
-      (while (let ((boundary (string-match "\\(\\$+{?\\)?\\w+\\W*\\'" qstr)))
-               (and boundary
-                    (progn
-                      (setq qprefix (substring qstr 0 boundary))
-                      (string-prefix-p uprefix
-                                       (substitute-in-file-name qprefix)))))
-        (setq qstr qprefix))
+      ;; (while (let ((boundary (string-match "\\(\\$+{?\\)?\\w+\\W*\\'" qstr)))
+      ;;          (and boundary
+      ;;               (progn
+      ;;                 (setq qprefix (substring qstr 0 boundary))
+      ;;                 (string-prefix-p uprefix
+      ;;                                  (substitute-in-file-name qprefix)))))
+      ;;   (setq qstr qprefix))
       (let ((qpos (length qstr)))
         (while (and (> qpos 0)
                     (string-prefix-p uprefix