Skip to content

Commit

Permalink
replace consult-minibuffer-history by consult-history
Browse files Browse the repository at this point in the history
  • Loading branch information
minad committed Dec 9, 2020
1 parent dadc87f commit b5cbc61
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 26 deletions.
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ information (e.g. `M-x`, `describe-face`, `describe-symbol`, `helpful-function`,
### Histories

* `consult-command-history`: Select a command from the `command-history`.
* `consult-minibuffer-history`: Insert a string from the `minibuffer-history`.
* `consult-history`: Insert a string from the current buffer history.
This command can be invoked from the minibuffer. In that case the history
stored in the minibuffer-history-variable is used.

### Jumping and Search

Expand Down Expand Up @@ -127,7 +129,8 @@ order to use the enhanced commands, you must configure the keybindings yourself.
;; Example configuration for Consult
(use-package consult
;; Replace bindings. Lazily loaded due to use-package.
:bind (("C-c o" . consult-outline)
:bind (("C-c h" . consult-history)
("C-c o" . consult-outline)
("C-x b" . consult-buffer)
("C-x 4 b" . consult-buffer-other-window)
("C-x 5 b" . consult-buffer-other-frame)
Expand Down Expand Up @@ -167,14 +170,14 @@ order to use the enhanced commands, you must configure the keybindings yourself.

| Variable | Def | Description |
|----------------------------|-----|---------------------------------------------------------|
| consult-annotate-alist || Functions which should get a richer completion display. |
| consult-line-numbers-widen | t | Show absolute line numbers when narrowing is active. |
| consult-mode-histories || Mode-specific history variables |
| consult-preview-buffer | t | Enable buffer preview during selection |
| consult-preview-theme | t | Enable theme preview during selection |
| consult-preview-yank | t | Enable yank preview during selection |
| consult-preview-mark | t | Enable mark preview during selection |
| consult-preview-line | t | Enable line preview during selection |
| consult-preview-mark | t | Enable mark preview during selection |
| consult-preview-outline | t | Enable outline preview during selection |
| consult-preview-theme | t | Enable theme preview during selection |
| consult-preview-yank | t | Enable yank preview during selection |
| consult-themes | nil | List of themes to be presented for selection |

## Acknowledgements
Expand Down
85 changes: 65 additions & 20 deletions consult.el
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
(require 'kmacro)
(require 'outline)
(require 'recentf)
(require 'ring)
(require 'seq)
(require 'subr-x)

Expand Down Expand Up @@ -98,6 +99,15 @@

;;;; Customization

(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 '(list (cons symbol symbol))
:group 'completing-history)

(defcustom consult-preview-buffer t
"Enable buffer preview during selection."
:type 'boolean
Expand Down Expand Up @@ -161,12 +171,6 @@ nil shows all `custom-available-themes'."
(defvar consult-apropos-history nil
"History for the command `consult-apropos'.")

(defvar consult-minibuffer-history nil
"History for the command `consult-minibuffer-history'.")

(defvar consult-command-history nil
"History for the command `consult-command-history'.")

(defvar consult-register-history nil
"History for the command `consult-register'.")

Expand Down Expand Up @@ -759,22 +763,63 @@ Otherwise replace the just-yanked text with the selected text."
(defun consult-command-history ()
"Select and evaluate command from the command history."
(interactive)
(eval
(read
(consult--read "Command: "
(delete-dups (mapcar #'prin1-to-string command-history))
:category 'expression
:history 'consult-command-history))))

(eval (read (consult--read
"Command: "
(or (delete-dups (mapcar #'prin1-to-string command-history))
(user-error "History is empty"))
:sort nil
:category 'expression))))

(defun consult--current-history ()
"Return 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."
(cond
;; If pressing "C-x M-:", i.e., `repeat-complex-command',
;; we are instead querying the `command-history' and get a full s-expression.
((eq last-command 'repeat-complex-command)
(delete-dups (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 (fboundp 'minibuffer-history-value)
(minibuffer-history-value) ;; Emacs 27 only
(symbol-value minibuffer-history-variable)))
;; 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))))))

(defun consult--history-elements (history)
"Return elements from HISTORY.
Can handle lists and rings."
(delete-dups (seq-copy (if (ring-p history)
(ring-elements history)
history))))

;; This command has been adopted from https://github.com/oantolin/completing-history/.
;;;###autoload
(defun consult-minibuffer-history ()
"Insert string from minibuffer history."
(defun consult-history (&optional history)
"Insert string from buffer HISTORY."
(interactive)
(insert
(substring-no-properties
(consult--read "Minibuffer: "
(delete-dups (seq-copy minibuffer-history))
:history 'consult-minibuffer-history))))
(let* ((enable-recursive-minibuffers t)
(str (consult--read "History: "
(or (consult--history-elements
(or history (consult--current-history)))
(user-error "History is empty"))
:sort nil)))
(when (minibufferp)
(delete-minibuffer-contents))
(insert (substring-no-properties str))))

;;;###autoload
(defun consult-minor-mode-menu ()
Expand Down

0 comments on commit b5cbc61

Please sign in to comment.