emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: History completion


From: Juri Linkov
Subject: Re: History completion
Date: Thu, 09 Jun 2022 10:01:53 +0300
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/29.0.50 (x86_64-pc-linux-gnu)

> So instead of this, tried to show history completions only after
> typing a special key.  TAB can't be reused because in some minibuffers
> such as ‘M-!’ (shell-command), TAB completes the command/file names.
>
> There is ‘C-tab’ bound to file-cache-minibuffer-complete.
> When file-cache is not used, then ‘C-tab’ could be rebound
> to a command that completes on the minibuffer history.

Actually two keys are needed: one to complete on the history,
and another to complete on the list of default values.
So maybe like the ‘up’ key navigates through the history,
‘C-x up’ could complete on the history, and like ‘down’ key
navigates through the future history, ‘C-x down’ could
complete on it:

diff --git a/lisp/minibuffer.el b/lisp/minibuffer.el
index cdbde2d340..e0e9893367 100644
--- a/lisp/minibuffer.el
+++ b/lisp/minibuffer.el
@@ -4418,6 +4499,36 @@ minibuffer-choose-completion
     (let ((completion-use-base-affixes t))
       (choose-completion nil no-exit no-quit))))
 
+(defun minibuffer-complete-history ()
+  "Complete the minibuffer history as far as possible.
+Like `minibuffer-complete' but completes on the history items
+instead of the default completion table."
+  (interactive)
+  (let ((completions-sort nil)
+        (history (mapcar (lambda (h)
+                           ;; Support e.g. `C-x ESC ESC TAB' as
+                           ;; a replacement of `list-command-history'
+                           (if (consp h) (format "%S" h) h))
+                         (symbol-value minibuffer-history-variable))))
+    (completion-in-region (minibuffer--completion-prompt-end) (point-max)
+                          history nil)))
+
+(defun minibuffer-complete-defaults ()
+  "Complete minibuffer defaults as far as possible.
+Like `minibuffer-complete' but completes on the default items
+instead of the completion table."
+  (interactive)
+  (let ((completions-sort nil))
+    (when (and (not minibuffer-default-add-done)
+               (functionp minibuffer-default-add-function))
+      (setq minibuffer-default-add-done t
+            minibuffer-default (funcall minibuffer-default-add-function)))
+    (completion-in-region (minibuffer--completion-prompt-end) (point-max)
+                          (ensure-list minibuffer-default) nil)))
+
+(define-key minibuffer-local-map [?\C-x up] 'minibuffer-complete-history)
+(define-key minibuffer-local-map [?\C-x down] 'minibuffer-complete-defaults)
+
 (defcustom minibuffer-default-prompt-format " (default %s)"
   "Format string used to output \"default\" values.
 When prompting for input, there will often be a default value,

reply via email to

[Prev in Thread] Current Thread [Next in Thread]