Subject: bug#19032: 24.4; icomplete cannot select matches with C-x b with no input

From: Lars Ingebrigtsen <[email-masked]>
Date: Thu, 13 Aug 2020 11:34:22 +0200

Matthew Leach <[email-masked]> writes:

>> Run emacs -Q, evaluate
>>
>>   (icomplete-mode 1)
>>   (setq icomplete-show-matches-on-no-input t)
>>
>> Make sure you have three buffers, e.g. by finding three files. Then
>> hit C-x b which should immediately show all buffers and use C-. or C-,
>> to select any other buffer than  what the modeline declares to be the
>> default, and hit C-j or RET.
>>
>> Emacs then switches to the default buffer instead of the selected buffer.
>
> I'm not sure if this is expected behaviour, since the "default" prompt
> in the minibuffer doesn't disappear when cycling through results.
> Nevertheless attached is a patch that fixes this.

I've respun the patch for Emacs 28, and I think the new behaviour makes
a lot more sense than the old behaviour, which I couldn't understand the
point of at all.

But I'm not an icomplete user, so perhaps the old behaviour is what
people want?

This also has possible ramifications for other completion styles
(although only icomplete enables it with this patch), so I wonder
whether Stefan has any comments here?

diff --git a/lisp/icomplete.el b/lisp/icomplete.el
index 3747ae3d28..e3f7043e2c 100644
--- a/lisp/icomplete.el
+++ b/lisp/icomplete.el
@@ -432,6 +432,8 @@ icomplete-minibuffer-setup
     					 (current-local-map)))
     (add-hook 'pre-command-hook  #'icomplete-pre-command-hook  nil t)
     (add-hook 'post-command-hook #'icomplete-post-command-hook nil t)
+    (when icomplete-show-matches-on-no-input
+      (setq-local completion-use-stored-completions-when-no-input t))
     (run-hooks 'icomplete-minibuffer-setup-hook)))
 
 (defvar icomplete--in-region-buffer nil)
diff --git a/lisp/minibuffer.el b/lisp/minibuffer.el
index d2c3f9045e..c3e5440956 100644
--- a/lisp/minibuffer.el
+++ b/lisp/minibuffer.el
@@ -1126,6 +1126,7 @@ completion--cycle-threshold
 (defvar-local completion-all-sorted-completions nil)
 (defvar-local completion--all-sorted-completions-location nil)
 (defvar completion-cycling nil)      ;Function that takes down the cycling map.
+(defvar completion-use-stored-completions-when-no-input nil)
 
 (defvar completion-fail-discreetly nil
   "If non-nil, stay quiet when there  is no match.")
@@ -1510,8 +1511,15 @@ completion--complete-and-exit
 COMPLETION-FUNCTION is called if the current buffer's content does not
 appear to be a match."
     (cond
-     ;; Allow user to specify null string
-   ((= beg end) (funcall exit-function))
+     ;; Allow user to specify null string.  In the case that
+     ;; `completion-use-stored-completions-when-no-input' is t, use
+     ;; the car of `completion-all-sorted-completions' as the
+     ;; candidate.
+     ((= beg end)
+      (when completion-use-stored-completions-when-no-input
+        (completion--replace beg end (car completion-all-sorted-completions)))
+      (funcall exit-function))
+
      ((test-completion (buffer-substring beg end)
                        minibuffer-completion-table
                        minibuffer-completion-predicate)

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no