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

From: Matthew Leach <[email-masked]>
Date: Sat, 15 Nov 2014 23:08:12 +0000

Hello,

Ole Laursen <[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.
-- 
Matt

lisp/ChangeLog:

2014-11-15  Matthew Leach  <[email-masked]>

	* minibuffer.el
	(completion-use-stored-completions-when-no-input): New.
	(completion--complete-and-exit): Use the above to decide whether
	to use the car of `completion-all-sorted-completions' as the
	candidate.

	* icomplete.el (icomplete-minibuffer-setup): set
	`completion-use-stored-completions-when-no-input' when
	`icomplete-show-matches-on-no-input' is t.

diff --git a/lisp/icomplete.el b/lisp/icomplete.el
index 95a6e1b..dc90f0e 100644
--- a/lisp/icomplete.el
+++ b/lisp/icomplete.el
@@ -262,6 +262,7 @@ Usually run by inclusion in `minibuffer-setup-hook'."
     (add-hook 'post-command-hook #'icomplete-post-command-hook nil t)
     (run-hooks 'icomplete-minibuffer-setup-hook)
     (when icomplete-show-matches-on-no-input
+      (setq completion-use-stored-completions-when-no-input t)
       (icomplete-exhibit))))
 
 (defvar icomplete--in-region-buffer nil)
diff --git a/lisp/minibuffer.el b/lisp/minibuffer.el
index c9ce381..4ea0530 100644
--- a/lisp/minibuffer.el
+++ b/lisp/minibuffer.el
@@ -969,6 +969,7 @@ completion candidates than this number."
 (defvar-local completion-all-sorted-completions nil)
 (defvar-local completion--all-sorted-completions-location nil)
 (defvar completion-cycling nil)
+(defvar completion-use-stored-completions-when-no-input nil)
 
 (defvar completion-fail-discreetly nil
   "If non-nil, stay quiet when there  is no match.")
@@ -1332,8 +1333,15 @@ If `minibuffer-completion-confirm' is `confirm-after-completion',
 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)