From 63d63112abd71d4ef75d89426c428e02cbfe3f15 Mon Sep 17 00:00:00 2001 From: Eshel Yaron Date: Thu, 25 Apr 2024 18:41:24 +0200 Subject: [PATCH] New commands 'minibuffer-first/last-completion' * lisp/minibuffer.el (minibuffer-completions-motion): New macro. (minibuffer-next-completion): Use it. (minibuffer-first-completion, minibuffer-last-completion): New commands. (minibuffer-local-completion-map): Bind them. (minibuffer-previous-completion, minibuffer-choose-completion): Doc fix. --- lisp/minibuffer.el | 49 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 13 deletions(-) diff --git a/lisp/minibuffer.el b/lisp/minibuffer.el index a22d73892e9..17b17d68498 100644 --- a/lisp/minibuffer.el +++ b/lisp/minibuffer.el @@ -3597,6 +3597,8 @@ The completion method is determined by `completion-at-point-functions'." "M-RET" #'minibuffer-choose-completion "M-j" #'minibuffer-force-complete-and-exit "C-x C-v" #'minibuffer-sort-completions + "C-x C-<" #'minibuffer-first-completion + "C-x C->" #'minibuffer-last-completion "C-x n" 'minibuffer-narrow-completions-map "C-x /" #'minibuffer-set-completion-styles "C-x ~" #'minibuffer-toggle-exceptional-candidates @@ -5563,27 +5565,48 @@ selected by these commands to the minibuffer." :type 'boolean :version "29.1") +(defmacro minibuffer-completions-motion (&rest body) + "Execute BODY in the minibuffer completions window. +When `minibuffer-completion-auto-choose' is non-nil in the minibuffer, +then also insert the completion candidate at point to the minibuffer +after executing BODY." + (declare (indent 0) (debug t)) + `(let ((auto-choose minibuffer-completion-auto-choose)) + (with-minibuffer-completions-window + (when completions-highlight-face + (setq-local cursor-face-highlight-nonselected-window t)) + ,@body + (when auto-choose + (let ((completion-use-base-affixes t)) + (choose-completion nil t t)))))) + +(defun minibuffer-first-completion () + "Move to the first item in the completions window from the minibuffer. +When `minibuffer-completion-auto-choose' is non-nil, then also +insert the selected completion candidate to the minibuffer." + (interactive nil minibuffer-mode) + (minibuffer-completions-motion (first-completion))) + +(defun minibuffer-last-completion () + "Move to the last item in the completions window from the minibuffer. +When `minibuffer-completion-auto-choose' is non-nil, then also +insert the selected completion candidate to the minibuffer." + (interactive nil minibuffer-mode) + (minibuffer-completions-motion (last-completion))) + (defun minibuffer-next-completion (&optional n vertical) - "Move to the next item in its completions window from the minibuffer. + "Move to the next item in the completions window from the minibuffer. When the optional argument VERTICAL is non-nil, move vertically to the next item on the next line using `next-line-completion'. Otherwise, move to the next item horizontally using `next-completion'. When `minibuffer-completion-auto-choose' is non-nil, then also insert the selected completion candidate to the minibuffer." (interactive "p") - (let ((auto-choose minibuffer-completion-auto-choose)) - (with-minibuffer-completions-window - (when completions-highlight-face - (setq-local cursor-face-highlight-nonselected-window t)) - (if vertical - (next-line-completion (or n 1)) - (next-completion (or n 1))) - (when auto-choose - (let ((completion-use-base-affixes t)) - (choose-completion nil t t)))))) + (minibuffer-completions-motion + (funcall (if vertical #'next-line-completion #'next-completion) (or n 1)))) (defun minibuffer-previous-completion (&optional n) - "Move to the previous item in its completions window from the minibuffer. + "Move to the previous item in the completions window from the minibuffer. When `minibuffer-completion-auto-choose' is non-nil, then also insert the selected completion candidate to the minibuffer." (interactive "p") @@ -5620,7 +5643,7 @@ insert the selected completion candidate to the minibuffer." (minibuffer-previous-line-or-call #'minibuffer-previous-completion arg)) (defun minibuffer-choose-completion (&optional no-exit no-quit) - "Run `choose-completion' from the minibuffer in its completions window. + "Run `choose-completion' from the minibuffer in the completions window. With prefix argument NO-EXIT, insert the completion candidate at point to the minibuffer, but don't exit the minibuffer. When the prefix argument is not provided, then whether to exit the minibuffer depends on the value -- 2.39.5