From fef547ece1d5cd3eebbc2fb7f51e51f15cfc2b4a Mon Sep 17 00:00:00 2001 From: Spencer Baugh Date: Sat, 14 Oct 2023 14:27:23 -0400 Subject: [PATCH] Add completions-auto-update It can be useful for the *Completions* buffer to automatically update as you type. That way you can see immediately what the next completion operation will do, even if you've changed text in the buffer since triggering completion. * lisp/minibuffer.el (completions-auto-update): Add. (completions-no-auto-update-commands): Add. (completions--post-command): Add. (minibuffer-completion-help): Add completions--post-command to post-command-hook. --- lisp/minibuffer.el | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/lisp/minibuffer.el b/lisp/minibuffer.el index 3e30b68d5e9..9995da9e4b7 100644 --- a/lisp/minibuffer.el +++ b/lisp/minibuffer.el @@ -2376,6 +2376,40 @@ completions--fit-window-to-buffer (resize-temp-buffer-window win)) (fit-window-to-buffer win completions-max-height))) +(defcustom completions-auto-update t + "If non-nil, update the *Completions* buffer as you type. + +This only affects the *Completions* buffer if it is already +displayed." + :type '(choice (const :tag "*Completions* doesn't change as you type" nil) + (const :tag "*Completions* updates as you type" t)) + :version "30.1") + +(defconst completions-no-auto-update-commands + '(previous-history-element + next-history-element + previous-line-or-history-element + next-line-or-history-element + completion-at-point + minibuffer-complete-and-exit + minibuffer-force-complete-and-exit + minibuffer-next-completion + minibuffer-previous-completion + minibuffer-choose-completion) + "Commands to skip updating *Completions*") + +(defun completions--post-command () + "Update a displayed *Completions* buffer after a change" + (when completions-auto-update + (while-no-input + (let ((non-essential t)) + (when (and (get-buffer-window "*Completions*" 0) + (not (memq this-command completions-no-auto-update-commands))) + (redisplay) + (if completion-in-region-mode + (completion-help-at-point) + (minibuffer-completion-help))))))) + (defun minibuffer-completion-help (&optional start end) "Display a list of possible completions of the current minibuffer contents." (interactive) @@ -2398,6 +2432,7 @@ minibuffer-completion-help ;; If there are no completions, or if the current input is already ;; the sole completion, then hide (previous&stale) completions. (minibuffer-hide-completions) + (remove-hook 'post-command-hook #'completions--post-command t) (if completions (completion--message "Sole completion") (unless completion-fail-discreetly @@ -2449,6 +2484,9 @@ minibuffer-completion-help (body-function . ,#'(lambda (_window) (with-current-buffer mainbuf + (when completions-auto-update + (add-hook 'post-command-hook #'completions--post-command nil t)) + ;; Remove the base-size tail because `sort' requires a properly ;; nil-terminated list. (when last (setcdr last nil)) -- 2.41.0