From de9a11255d34a2635e61b150725cef242077bd06 Mon Sep 17 00:00:00 2001
From: Gregory Heytings <gregory@heytings.org>
Date: Tue, 25 May 2021 06:38:15 +0200
Subject: [PATCH] Improve completion-list-mode-map

* doc/emacs/mini.texi (Completion Commands): Mention it.

* lisp/minibuffer.el (minibuffer-local-completion-map): Add the M-g key
for switch-to-completion (bug#47699).

* lisp/simple.el (completion-list-mode-map): Make special-mode-map its
parent, unbind the 'g' revert key, add the 'n' and 'p' keys for
next-completion and previous-completion, and the M-g key for
switch-to-minibuffer.
(switch-to-minibuffer): New function.
---
 doc/emacs/mini.texi |  4 ++--
 etc/NEWS            |  5 +++++
 lisp/minibuffer.el  |  1 +
 lisp/simple.el      | 12 +++++++++++-
 4 files changed, 19 insertions(+), 3 deletions(-)

diff --git a/doc/emacs/mini.texi b/doc/emacs/mini.texi
index 03db6698fe5..564e5763001 100644
--- a/doc/emacs/mini.texi
+++ b/doc/emacs/mini.texi
@@ -374,8 +374,8 @@ used with the completion list:
 @itemx @key{prior}
 Typing @kbd{M-v}, while in the minibuffer, selects the window showing
 the completion list (@code{switch-to-completions}).  This paves the
-way for using the commands below.  @key{PageUp} or @key{prior} does
-the same.  You can also select the window in other ways
+way for using the commands below.  @key{PageUp}, @key{prior} and
+@kbd{M-g} does the same.  You can also select the window in other ways
 (@pxref{Windows}).
 
 @findex choose-completion
diff --git a/etc/NEWS b/etc/NEWS
index e11b8606160..d163c188718 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -442,6 +442,11 @@ major mode.
 
 * Changes in Specialized Modes and Packages in Emacs 28.1
 
+** Completion List Mode
+New key bindings have been added: 'n' and 'p' to navigate completions,
+and 'M-g' to switch to the minibuffer, and you can also switch back
+to the completion list buffer with 'M-g'.
+
 ** Benchmark
 *** New function 'benchmark-call' to measure the execution time of a function.
 Additionally, the number of repetitions can be expressed as a minimal duration
diff --git a/lisp/minibuffer.el b/lisp/minibuffer.el
index e04f1040b38..36fb8e72c15 100644
--- a/lisp/minibuffer.el
+++ b/lisp/minibuffer.el
@@ -2603,6 +2603,7 @@ The completion method is determined by `completion-at-point-functions'."
     (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 2a90a076315..b3470ac7b09 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -8834,6 +8834,8 @@ makes it easier to edit it."
 
 (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)
@@ -8843,8 +8845,10 @@ makes it easier to edit it."
     (define-key map [right] 'next-completion)
     (define-key map [?\t] 'next-completion)
     (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.")
 
@@ -9133,6 +9137,12 @@ select the completion near point.\n\n"))))))
       ;; FIXME: Perhaps this should be done in `minibuffer-completion-help'.
       (when (bobp)
 	(next-completion 1)))))
+
+(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.39.5