Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update relevant index in consult-history #83

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 52 additions & 33 deletions consult.el
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,22 @@
:type 'function)

(defcustom consult-mode-histories
minad marked this conversation as resolved.
Show resolved Hide resolved
'((eshell-mode . eshell-history-ring)
(comint-mode . comint-input-ring)
(term-mode . term-input-ring))
"Alist of (mode . history) pairs of mode histories.
The histories can be rings or lists."
:type '(list (cons symbol symbol)))
'((eshell-mode eshell-history-ring eshell-history-index)
(comint-mode comint-input-ring comint-input-ring-index)
(term-mode term-input-ring))
minad marked this conversation as resolved.
Show resolved Hide resolved
"Alist of (mode history-ring history-index?) triples of mode histories.
The histories can be rings or lists.

When the optional history-index variable is a non-nil symbol, then during a
`consult-history' invocation, the value of that symbol will be set to a
zero-based index corresponding to the candidate selected during that invocation.
This is useful to replay input history via `comint-get-next-from-history' within
`comint', or equivalently for `eshell'. For `term-mode', the index adjustment is
not configured, since there is no equivalent replay keybinding.

If the history-index variable in a triple is nil or absent, then
no index reassignment is performed."
:type '(list (list symbol)))

(defcustom consult-themes nil
"List of themes to be presented for selection.
Expand Down Expand Up @@ -1203,43 +1213,52 @@ for which the command history is used."
;; Alternatively you might want to use `consult-complex-command',
;; which can also be bound to "C-x M-:"!
((eq last-command 'repeat-complex-command)
(mapcar #'prin1-to-string command-history))
(cons (mapcar #'prin1-to-string command-history) nil))
;; In the minibuffer we use the current minibuffer history,
;; which can be configured by setting `minibuffer-history-variable'.
;; `minibuffer-history-value' is Emacs 27 only, therefore we use `symbol-value'
((minibufferp)
(symbol-value minibuffer-history-variable)) ;; (minibuffer-history-value) is Emacs 27 only
(cons (symbol-value minibuffer-history-variable) nil))
;; Otherwise we use a mode-specific history, see `consult-mode-histories'.
(t (when-let (history
(or (seq-find (lambda (ring)
(and (derived-mode-p (car ring))
(boundp (cdr ring))))
consult-mode-histories)
(user-error
"No history configured for `%s', see `consult-mode-histories'"
major-mode)))
(symbol-value (cdr history))))))
(t (pcase-let ((`(_ ,h ,i)
(or (seq-find (pcase-lambda (`(,m ,h _))
(and (derived-mode-p m) (boundp h)))
consult-mode-histories)
(user-error
"No history configured for `%s', see `consult-mode-histories'"
major-mode))))
(cons (symbol-value h) i)))))

;; This command has been adopted from https://github.com/oantolin/completing-history/.
;;;###autoload
(defun consult-history (&optional history)
"Insert string from buffer HISTORY."
(defun consult-history (&optional history index)
"Insert string from buffer HISTORY.
INDEX is an optional history variable."
(interactive)
(let* ((enable-recursive-minibuffers t)
(str (consult--read "History: "
(let ((history (or history (consult--current-history))))
(or (consult--remove-dups (if (ring-p history)
(ring-elements history)
history))
(user-error "History is empty")))
:history t ;; disable history
:category ;; Report command category for M-x history
(and (minibufferp)
(eq minibuffer-history-variable 'extended-command-history)
'command)
:sort nil)))
(unless history
(pcase-let ((`(,h . ,i) (consult--current-history)))
(setq history h index i)))
(let* ((cands-list (if (ring-p history)
(ring-elements history)
history))
(cands-indexed (consult--remove-dups
(seq-map-indexed #'cons cands-list)
#'car))
(enable-recursive-minibuffers t)
(selected (consult--read "History: "
(or cands-indexed (user-error "History is empty"))
:history t ;; disable history
:lookup #'consult--lookup-candidate
:category ;; Report command category for M-x history
(and (minibufferp)
(eq minibuffer-history-variable 'extended-command-history)
'command)
:sort nil)))
(when (minibufferp)
(delete-minibuffer-contents))
(insert (substring-no-properties str))))
(when index
(setf (symbol-value index) selected))
(insert (substring-no-properties (nth selected cands-list)))))

(defun consult--minor-mode-candidates ()
"Return list of minor-mode candidate strings."
Expand Down