]> git.eshelyaron.com Git - emacs.git/commitdiff
New command 'minibuffer-narrow-completions-to-history'
authorEshel Yaron <me@eshelyaron.com>
Thu, 25 Jan 2024 19:38:13 +0000 (20:38 +0100)
committerEshel Yaron <me@eshelyaron.com>
Thu, 25 Jan 2024 19:38:13 +0000 (20:38 +0100)
This command filters completions by whether or not they appear in the
minibuffer history list.

* lisp/minibuffer.el (minibuffer-narrow-completions-to-history): New
command.
(minibuffer-narrow-completions-map): Bind it to 'C-x n h'.

* doc/emacs/mini.texi (Completion Commands, Narrow Completions):
Document it.

doc/emacs/mini.texi
lisp/minibuffer.el

index ad130223e6383b16c2afdb805eb84edc9dade296..9c13bc3a557db2e809ec033f1beaf47e165b4940 100644 (file)
@@ -363,7 +363,7 @@ Cycle minibuffer input among possible completion candidates
 (@code{minibuffer-cycle-completion}).
 @item C-l
 Restore the minibuffer input that Emacs used to compute the current
-set of completion candidates.
+set of completion candidates
 (@code{minibuffer-restore-completion-input}).
 @item C-x C-v
 Change the order of the list of possible completions
@@ -375,6 +375,9 @@ current minibuffer input
 @item C-x n m
 Narrow the list of possible completions in a command-specific manner
 (@code{minibuffer-narrow-completions}).
+@item C-x n h
+Restrict the list of possible completions according to the minibuffer
+history list (@code{minibuffer-narrow-completions-to-history}).
 @item C-x n w
 Remove restrictions on the list of possible completions
 (@code{minibuffer-widen-completions}).
@@ -477,6 +480,15 @@ that input.  @xref{Narrow Completions}.
 narrow the completions list depending on the kind of candidates in
 that list.  @xref{Narrow Completions}, for more details.
 
+@kindex C-x n h @r{(completion)}
+@findex minibuffer-narrow-completions-to-history
+  @kbd{C-x n h} (@code{minibuffer-narrow-completions-to-history})
+narrows the list of possible completions to only include past inputs,
+i.e., completions that also appear in the minibuffer history list.
+@xref{Minibuffer History}.  With a negative prefix argument (@kbd{C--
+C-x n h}), this command does the opposite: instead of keeping only
+past inputs, it excludes them and keeps the rest.
+
 @kindex C-x n w @r{(completion)}
 @findex minibuffer-widen-completions
   @kbd{C-x n w} (@code{minibuffer-widen-completions}) removes
@@ -801,8 +813,17 @@ n m} prompts you for a regular expression and narrows the completions
 list to only include candidates which match that regular expression.
 @xref{Regexps}.
 
-When you narrow the completions list with @kbd{C-x n n} or with
-@kbd{C-x n m}, Emacs extends the completions heading line with a
+  You can also restrict completions to only include completions that
+you've previously provided as input (that is, members of the current
+minibuffer history list, @pxref{Minibuffer History}).  You do this by
+typing @kbd{C-x n h} (@code{minibuffer-narrow-completions-to-history})
+in the minibuffer.  This command filters the completions list, keeping
+only your past inputs.  If you invoke it with a negative prefix
+argument (@kbd{C-- C-x n h}), this command instead does the opposite:
+it removes past inputs from the completions list, and keeps the rest.
+
+When you narrow the completions list with @kbd{C-x n n}, @kbd{C-x n m}
+or @kbd{C-x n m}, Emacs extends the completions heading line with a
 description of the restriction that is currently in effect
 (@pxref{Completions Heading Line}).  The mode line of the
 @file{*Completions*} buffer also indicates the restriction with the
index fe8205a940e192aa16d6530c9ea65d0373cfe5dd..232bd2ca18df937f9cc1395253e1711287a970bf 100644 (file)
@@ -3490,6 +3490,7 @@ with `minibuffer-local-must-match-map'."
 (defvar-keymap minibuffer-narrow-completions-map
   :doc "Keymap for completions narrowing commands."
   "n" #'minibuffer-narrow-completions-to-current
+  "h" #'minibuffer-narrow-completions-to-history
   "m" #'minibuffer-narrow-completions
   "w" #'minibuffer-widen-completions)
 
@@ -5625,6 +5626,36 @@ exclude matches to current input from completions list."
            (gethash key table)))
        (concat "narrowing to " (prin1-to-string current))))))
 
+(defun minibuffer-narrow-completions-to-history (&optional exclude)
+  "EXCLUDE or keep only members of the minibuffer history as completions.
+
+If EXCLUDE is nil, restrict completions to only those that are
+also in the minibuffer history list.  In other words, keep only
+your past inputs as completion candidates.  Otherwise, if EXCLUDE
+is non-nil, keep only \"new\" completion candidates, excluding
+members of the minibuffer history list."
+  (interactive "P" minibuffer-mode)
+  (if-let ((hist
+            (mapcar
+             (lambda (string)
+               (substring string
+                          (car (completion-boundaries
+                                string
+                                minibuffer-completion-table
+                                minibuffer-completion-predicate
+                                ""))))
+             (and (not (eq minibuffer-history-variable t))
+                  (symbol-value minibuffer-history-variable))))
+           (func (if exclude (lambda (k h) (not (member k h))) #'member)))
+      (minibuffer--add-completions-predicate
+       (lambda (cand &rest _)
+         (let* ((key (cond
+                      ((stringp cand)              cand)
+                      ((symbolp cand) (symbol-name cand))
+                      (t              (car         cand)))))
+           (funcall func key hist)))
+       (concat (when exclude "not ") "previously used"))))
+
 (defun minibuffer-widen-completions (&optional all)
   "Remove restrictions on current minibuffer completions list.