From: Eshel Yaron Date: Thu, 25 Jan 2024 19:38:13 +0000 (+0100) Subject: New command 'minibuffer-narrow-completions-to-history' X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=d49631814aabd1f53b2d6e24d3b58d65184e759c;p=emacs.git New command 'minibuffer-narrow-completions-to-history' 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. --- diff --git a/doc/emacs/mini.texi b/doc/emacs/mini.texi index ad130223e63..9c13bc3a557 100644 --- a/doc/emacs/mini.texi +++ b/doc/emacs/mini.texi @@ -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 diff --git a/lisp/minibuffer.el b/lisp/minibuffer.el index fe8205a940e..232bd2ca18d 100644 --- a/lisp/minibuffer.el +++ b/lisp/minibuffer.el @@ -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.