Subject: Re: bug#47699: [PATCH] Improve completion-list-mode-map

From: Gregory Heytings <[email-masked]>
Date: Mon, 12 Apr 2021 07:24:25 +0000

>> Right here, I think.
>
> Okay, so what do you and others think of the following:
>

Here is the complete patch, with an improved 
read-expression-switch-to-completions function, so that you can try it 
out.

From 3027053dbd3759f0c12bf5855e95931bf51dd492 Mon Sep 17 00:00:00 2001
From: Gregory Heytings <[email-masked]>
Date: Mon, 12 Apr 2021 07:15:03 +0000
Subject: [PATCH] Switch between minibuffer and *Completions*

* lisp/simple.el (completion-list-mode-map): Make special-mode-map its
parent, unbind the 'g' revert key, bind the 'n' and 'p' keys to
next-completion and previous-completion, and the M-g key to
switch-to-minibuffer.
(read-expression-map): Bing M-g to read-expression-switch-to-completions.
(switch-to-minibuffer, read-expression-switch-to-completions): New
functions.

* lisp/minibuffer.el (minibuffer-local-completion-map): Bind the M-g key
to switch-to-completion.

* doc/emacs/windows.texi (Temporary Displays): Document the change.
---
 doc/emacs/windows.texi |  8 +++++---
 lisp/minibuffer.el     |  1 +
 lisp/simple.el         | 18 ++++++++++++++++++
 3 files changed, 24 insertions(+), 3 deletions(-)

diff --git a/doc/emacs/windows.texi b/doc/emacs/windows.texi
index c66deb7748..2e942e4082 100644
--- a/doc/emacs/windows.texi
+++ b/doc/emacs/windows.texi
@@ -541,9 +541,11 @@ Temporary Displays
 @findex temp-buffer-resize-mode
   The @file{*Completions*} buffer is also special in the sense that
 Emacs usually tries to make its window just as large as necessary to
-display all of its contents.  To resize windows showing other
-temporary displays, like, for example, the @file{*Help*} buffer, turn
-on the minor mode (@pxref{Minor Modes}) @code{temp-buffer-resize-mode}
+display all of its contents, and that you can switch back and forth
+between the minibuffer and the @file{*Completions*} buffer by typing
+@key{M-g}.  To resize windows showing other temporary displays, like,
+for example, the @file{*Help*} buffer, turn on the minor mode
+(@pxref{Minor Modes}) @code{temp-buffer-resize-mode}
 (@pxref{Temporary Displays,,Temporary Displays, elisp, The Emacs Lisp
 Reference Manual}).
 
diff --git a/lisp/minibuffer.el b/lisp/minibuffer.el
index 5f594679ca..d555784133 100644
--- a/lisp/minibuffer.el
+++ b/lisp/minibuffer.el
@@ -2413,6 +2413,7 @@ minibuffer-local-completion-map
     (define-key map "?" 'minibuffer-completion-help)
     (define-key map [prior] 'switch-to-completions)
     (define-key map "\M-v"  'switch-to-completions)
+    (define-key map "\M-g"  'switch-to-completions)
     map)
   "Local keymap for minibuffer input with completion.")
 
diff --git a/lisp/simple.el b/lisp/simple.el
index 999755a642..57e9e868cb 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -1658,6 +1658,7 @@ read-expression-map
     (define-key m "\t" 'completion-at-point)
     (define-key m "\r" 'read--expression-try-read)
     (define-key m "\n" 'read--expression-try-read)
+    (define-key m "\M-g" 'read-expression-switch-to-completions)
     (set-keymap-parent m minibuffer-local-map)
     m))
 
@@ -8767,6 +8768,8 @@ set-variable
 
 (defvar completion-list-mode-map
   (let ((map (make-sparse-keymap)))
+    (set-keymap-parent map special-mode-map)
+    (define-key map "g" nil) ;; There's nothing to revert from.
     (define-key map [mouse-2] 'choose-completion)
     (define-key map [follow-link] 'mouse-face)
     (define-key map [down-mouse-2] nil)
@@ -8778,6 +8781,9 @@ completion-list-mode-map
     (define-key map [backtab] 'previous-completion)
     (define-key map "q" 'quit-window)
     (define-key map "z" 'kill-current-buffer)
+    (define-key map "n" 'next-completion)
+    (define-key map "p" 'previous-completion)
+    (define-key map "\M-g" 'switch-to-minibuffer)
     map)
   "Local map for completion list buffers.")
 
@@ -9067,6 +9073,18 @@ switch-to-completions
       ;; FIXME: Perhaps this should be done in `minibuffer-completion-help'.
       (when (bobp)
 	(next-completion 1)))))
+
+(defun read-expression-switch-to-completions ()
+  "Select the completion list window while reading an expression."
+  (interactive)
+  (completion-help-at-point)
+  (switch-to-completions))
+
+(defun switch-to-minibuffer ()
+  "Select the minibuffer window."
+  (interactive)
+  (when (active-minibuffer-window)
+    (select-window (active-minibuffer-window))))
 
 ;;; Support keyboard commands to turn on various modifiers.
 
-- 
2.30.2