[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/consult cb5aea3e5e 5/5: Enable setting an index var fro
From: |
ELPA Syncer |
Subject: |
[elpa] externals/consult cb5aea3e5e 5/5: Enable setting an index var from consult-mode-histories (#669) |
Date: |
Sat, 15 Oct 2022 16:57:25 -0400 (EDT) |
branch: externals/consult
commit cb5aea3e5e3d3b434705b1662df8cce316460264
Author: JD Smith <93749+jdtsmith@users.noreply.github.com>
Commit: Daniel Mendler <mail@daniel-mendler.de>
Enable setting an index var from consult-mode-histories (#669)
---
consult.el | 109 ++++++++++++++++++++++++++++++++++---------------------------
1 file changed, 60 insertions(+), 49 deletions(-)
diff --git a/consult.el b/consult.el
index c1a7fa96d7..ecd8d3f4bb 100644
--- a/consult.el
+++ b/consult.el
@@ -135,12 +135,18 @@ This applies to asynchronous commands, e.g.,
`consult-grep'."
:type '(alist :key-type symbol :value-type plist))
(defcustom consult-mode-histories
- '((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 '(alist :key-type symbol :value-type symbol))
+ '((eshell-mode eshell-history-ring eshell-history-index)
+ (comint-mode comint-input-ring comint-input-ring-index)
+ (term-mode term-input-ring term-input-ring-index))
+ "Alist of mode histories as (mode . history) or (mode history index).
+The histories can be rings or lists. INDEX, if provided, is a
+variable to set to the index of the selection within the ring or
+list."
+ :type '(alist :key-type symbol
+ :value-type (choice (symbol :tag "List or Ring Name")
+ (group :tag "Include Index"
+ (symbol :tag "List/Ring")
+ (symbol :tag "Index Var")))))
(defcustom consult-themes nil
"List of themes (symbols or regexps) to be presented for selection.
@@ -3721,65 +3727,70 @@ This command can act as a drop-in replacement for
`repeat-complex-command'."
;;;;; Command: consult-history
(declare-function ring-elements "ring")
-(defun consult--current-history (&optional history)
- "Return the normalized HISTORY or the history relevant to the current buffer.
-
-If the minibuffer is active, returns the minibuffer history,
-otherwise the history corresponding to the mode is returned.
-There is a special case for `repeat-complex-command',
-for which the command history is used."
+(defun consult--current-history (history index)
+ "Return the HISTORY or the history relevant to the current buffer.
+Returns the cons (HISTORY . INDEX). If the minibuffer is active,
+HISTORY is the minibuffer history, otherwise the history
+corresponding to the mode. There is a special case for
+`repeat-complex-command', for which the command history is used.
+INDEX is the name of the index variable to update, if any."
(cond
- (history)
+ (history (cons history index))
;; If pressing "C-x M-:", i.e., `repeat-complex-command',
;; we are instead querying the `command-history' and get a full
s-expression.
;; Alternatively you might want to use `consult-complex-command',
;; which can also be bound to "C-x M-:"!
((eq last-command 'repeat-complex-command)
- (setq history (mapcar #'prin1-to-string command-history)))
+ (list (mapcar #'prin1-to-string command-history)))
;; In the minibuffer we use the current minibuffer history,
;; which can be configured by setting `minibuffer-history-variable'.
((minibufferp)
- (if (eq minibuffer-history-variable t)
- (user-error "Minibuffer history is disabled for `%s'" this-command)
- (setq history (mapcar #'consult--tofu-hide (symbol-value
minibuffer-history-variable)))))
+ (when (eq minibuffer-history-variable t)
+ (user-error "Minibuffer history is disabled for `%s'" this-command))
+ (list (mapcar #'consult--tofu-hide (symbol-value
minibuffer-history-variable))))
;; Otherwise we use a mode-specific history, see `consult-mode-histories'.
- (t (when-let (found
- (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)))
- (setq history (symbol-value (cdr found))))))
- (consult--remove-dups (if (ring-p history) (ring-elements history) history)))
-
-;; This command has been adopted from
https://github.com/oantolin/completing-history/.
+ (t (setq history (seq-find (lambda (h)
+ (and (derived-mode-p (car h))
+ (boundp (if (consp (cdr h)) (cadr h) (cdr
h)))))
+ consult-mode-histories))
+ (unless history
+ (user-error "No history configured for `%s', see
`consult-mode-histories'"
+ major-mode))
+ (if (consp (cdr history))
+ (cons (symbol-value (cadr history)) (caddr history))
+ (list (symbol-value (cdr history)))))))
+
;;;###autoload
-(defun consult-history (&optional history)
+(defun consult-history (&optional history index)
"Insert string from HISTORY of current buffer.
In order to select from a specific HISTORY, pass the history variable
-as argument. See also `cape-history' from the Cape package."
+as argument. INDEX is the name of the index variable to update, if any.
+See also `cape-history' from the Cape package."
(interactive)
- (let ((str (consult--local-let ((enable-recursive-minibuffers t))
- (consult--read
- (or (consult--current-history history)
- (user-error "History is empty"))
- :prompt "History: "
- :history t ;; disable history
- :category ;; Report category depending on history variable
- (and (minibufferp)
- (pcase minibuffer-history-variable
- ('extended-command-history 'command)
- ('buffer-name-history 'buffer)
- ('face-name-history 'face)
- ('read-envvar-name-history 'environment-variable)
- ('bookmark-history 'bookmark)
- ('file-name-history 'file)))
- :sort nil
- :state (consult--insertion-preview (point) (point))))))
+ (let* ((pair (consult--current-history history index))
+ (history (if (ring-p (car pair)) (ring-elements (car pair)) (car
pair)))
+ (index (cdr pair))
+ (str (consult--local-let ((enable-recursive-minibuffers t))
+ (consult--read
+ (or (consult--remove-dups history)
+ (user-error "History is empty"))
+ :prompt "History: "
+ :history t ;; disable history
+ :category ;; Report category depending on history variable
+ (and (minibufferp)
+ (pcase minibuffer-history-variable
+ ('extended-command-history 'command)
+ ('buffer-name-history 'buffer)
+ ('face-name-history 'face)
+ ('read-envvar-name-history 'environment-variable)
+ ('bookmark-history 'bookmark)
+ ('file-name-history 'file)))
+ :sort nil
+ :state (consult--insertion-preview (point) (point))))))
(when (minibufferp)
(delete-minibuffer-contents))
+ (when index
+ (set index (seq-position history str)))
(insert (substring-no-properties str))))
;;;;; Command: consult-isearch-history