Subject: Re: bug#78944: 31.0.50; Minibuffer completion

From: Stefan Monnier <[email-masked]>
Date: Tue, 22 Jul 2025 16:08:57 -0400

Dani Moncayo [2025-07-19 09:47:31] wrote:

> On Sat, Jul 19, 2025 at 9:03 AM Eli Zaretskii <[email-masked]> wrote:
>>
>> [...]
>>
>> Ping!  How should we make some progress with this issue?
>
> FWIW: I don't see a good solution for this (minor) issue.  But I'd
> like to say that I'd rather not complicate the (already complex) logic
> for minibuffer completion.

Maybe the patch below?  It doesn't actually fix the problem, but with
Dani's recipe it adds a message

    Switched from style ‘partial-completion’ back to ‘basic’

at the end of the minibuffer, to try and explain what's going on.

        Stefan
diff --git a/lisp/minibuffer.el b/lisp/minibuffer.el
index 122459be062..524c4ffe1d1 100644
--- a/lisp/minibuffer.el
+++ b/lisp/minibuffer.el
@@ -1298,6 +1298,8 @@ completion--styles
         (delete-dups (append (cdr over) (copy-sequence completion-styles)))
        completion-styles)))
 
+(defvar completion--last-style nil)
+
 (defun completion--nth-completion (n string table pred point metadata)
   "Call the Nth method of completion styles."
   ;; We provide special support for quoting/unquoting here because it cannot
@@ -1310,7 +1310,8 @@
   ;; The quote/unquote function needs to come from the completion table (rather
   ;; than from completion-extra-properties) because it may apply only to some
   ;; part of the string (e.g. substitute-in-file-name).
-  (let* ((md (or metadata
+  (pcase-let*
+      ((md (or metadata
                  (completion-metadata (substring string 0 point) table pred)))
          (requote
           (when (and
@@ -1329,7 +1330,7 @@
               (setq point (pop new))
               (cl-assert (<= point (length string)))
               (pop new))))
-         (result-and-style
+       (`(,result . ,style)
           (seq-some
            (lambda (style)
              (let (symbols values)
@@ -1345,18 +1346,26 @@
                                string table pred point)))
                    (and probe (cons probe style))))))
            (completion--styles md)))
-         (adjust-fn (get (cdr result-and-style) 'completion--adjust-metadata))
+       (adjust-fn (get style 'completion--adjust-metadata))
          (adjusted (completion-metadata-get
                     metadata 'completion--adjusted-metadata)))
     (when (and adjust-fn metadata
                ;; Avoid re-applying the same adjustment (bug#74718).
-               (not (memq (cdr result-and-style) adjusted)))
+               (not (memq style adjusted)))
       (setcdr metadata `((completion--adjusted-metadata
-                          ,(cdr result-and-style) . ,adjusted)
+                          ,style . ,adjusted)
                          . ,(cdr (funcall adjust-fn metadata)))))
-    (if requote
-        (funcall requote (car result-and-style) n)
-      (car result-and-style))))
+    (let ((res (if requote (funcall requote result n) result)))
+      (when (and completion--last-style
+                 (not (eq style (car completion--last-style)))
+                 (equal (cadr completion--last-style) string)
+                 (equal (cddr completion--last-style) point))
+        (message "Switched from style `%S' back to `%S'" ;; bug#78944.
+                 (car completion--last-style) style))
+      (setq completion--last-style
+            (when (stringp (car-safe res))
+              (cons style res)))
+      res)))
 
 (defun completion-try-completion (string table pred point &optional metadata)
   "Try to complete STRING using completion table TABLE.