branch: master
commit d289b78a9301f24b12a803358cdc251375d142cc
Author: Oleh Krehel <[email protected]>
Commit: Oleh Krehel <[email protected]>
Add a new interface to customize additional exit points
* ivy.el (ivy--actions-list): New defvar. Store the exit points per
command.
(ivy-set-actions): New defun. Use this to set the extra exit points for
each command.
(ivy-read): Account for `ivy--actions-list'.
(ivy-switch-buffer): Set extra action to kill the buffer. Update the
call to `ivy-read'.
* counsel.el (counsel-locate): Use the single action in the function and
customize the rest via `ivy-set-actions'.
Re #164
---
counsel.el | 15 ++++++++-------
ivy.el | 36 ++++++++++++++++++++++++++----------
2 files changed, 34 insertions(+), 17 deletions(-)
diff --git a/counsel.el b/counsel.el
index cf33413..5f48f30 100644
--- a/counsel.el
+++ b/counsel.el
@@ -446,6 +446,11 @@ Skip some dotfiles unless `ivy-text' requires them."
(defvar counsel-locate-history nil
"History for `counsel-locate'.")
+(ivy-set-actions
+ 'counsel-locate
+ '(("xdg-open" counsel-locate-action-extern)
+ ("dired" counsel-locate-action-dired)))
+
;;;###autoload
(defun counsel-locate ()
"Call locate shell command."
@@ -454,13 +459,9 @@ Skip some dotfiles unless `ivy-text' requires them."
:dynamic-collection #'counsel-locate-function
:history 'counsel-locate-history
:action
- (cons
- 1
- '(("default" (lambda (val)
- (when val
- (find-file val))))
- ("xdg-open" counsel-locate-action-extern)
- ("dired" counsel-locate-action-dired)))))
+ (lambda (val)
+ (when val
+ (find-file val)))))
(defun counsel--generic (completion-fn)
"Complete thing at point with COMPLETION-FN."
diff --git a/ivy.el b/ivy.el
index 27f76d1..dc6c412 100644
--- a/ivy.el
+++ b/ivy.el
@@ -90,6 +90,14 @@ Only \"./\" and \"../\" apply here. They appear in reverse
order."
"When non-nil, add `recentf-mode' and bookmarks to the list of buffers."
:type 'boolean)
+(defvar ivy--actions-list nil
+ "A list of extra actions per command.")
+
+(defun ivy-set-actions (cmd actions)
+ "Set CMD extra exit points to ACTIONS."
+ (setq ivy--actions-list
+ (plist-put ivy--actions-list cmd actions)))
+
;;* Keymap
(require 'delsel)
(defvar ivy-minibuffer-map
@@ -783,6 +791,12 @@ MATCHER can completely override matching.
DYNAMIC-COLLECTION is a function to call to update the list of
candidates with each input."
+ (let ((extra-actions (plist-get ivy--actions-list this-command)))
+ (when extra-actions
+ (setq action
+ `(1
+ ("default" ,action)
+ ,@extra-actions))))
(setq ivy-last
(make-ivy-state
:prompt prompt
@@ -1487,21 +1501,23 @@ BUFFER may be a string or nil."
(defvar ivy-switch-buffer-map (make-sparse-keymap))
+(ivy-set-actions
+ 'ivy-switch-buffer
+ '(("kill"
+ (lambda (x)
+ (kill-buffer x)
+ (ivy--reset-state ivy-last)))))
+
(defun ivy-switch-buffer ()
"Switch to another buffer."
(interactive)
(if (not ivy-mode)
(call-interactively 'switch-to-buffer)
- (ivy-read "Switch to buffer: " 'internal-complete-buffer
- :preselect (buffer-name (other-buffer (current-buffer)))
- :action (cons
- 1
- '(("default" ivy--switch-buffer-action)
- ("kill"
- (lambda (x)
- (kill-buffer x)
- (ivy--reset-state ivy-last)))))
- :keymap ivy-switch-buffer-map)))
+ (let ((this-command 'ivy-switch-buffer))
+ (ivy-read "Switch to buffer: " 'internal-complete-buffer
+ :preselect (buffer-name (other-buffer (current-buffer)))
+ :action #'ivy--switch-buffer-action
+ :keymap ivy-switch-buffer-map))))
(defun ivy-recentf ()
"Find a file on `recentf-list'."