branch: master
commit 5ff5139f60de1939228617d7fc5c925fdc0c5ec6
Author: Oleh Krehel <[email protected]>
Commit: Oleh Krehel <[email protected]>
ivy.el (ivy-read-action-function): Allow to read actions using Ivy
Fixes #2176
---
ivy-hydra.el | 2 +-
ivy.el | 62 ++++++++++++++++++++++++++++++++++++++++--------------------
2 files changed, 43 insertions(+), 21 deletions(-)
diff --git a/ivy-hydra.el b/ivy-hydra.el
index c4fa573..1d586ee 100644
--- a/ivy-hydra.el
+++ b/ivy-hydra.el
@@ -124,7 +124,7 @@ _h_ ^+^ _l_ | _d_one ^ ^ | _o_ops | _M_: matcher
%-5s(ivy--matcher-desc)
(cdr actions))
,@extra-actions))))))
-(define-key ivy-minibuffer-map (kbd "M-o") 'ivy-dispatching-done-hydra)
+(setq ivy-read-action-function (lambda (_) (ivy-dispatching-done-hydra)))
(provide 'ivy-hydra)
diff --git a/ivy.el b/ivy.el
index 396d145..f1ffd66 100644
--- a/ivy.el
+++ b/ivy.el
@@ -873,6 +873,13 @@ key (a string), cmd and doc (a string)."
actions
"\n")))
+(defcustom ivy-read-action-function #'ivy-read-action-by-key
+ "Function used to read an action."
+ :type '(radio
+ (function-item ivy-read-action-by-key)
+ (function-item ivy-read-action-ivy)
+ (function-item ivy-read-action-hydra)))
+
(defun ivy-read-action ()
"Change the action to one of the available ones.
@@ -882,26 +889,41 @@ selection, non-nil otherwise."
(let ((actions (ivy-state-action ivy-last)))
(if (not (ivy--actionp actions))
t
- (let* ((hint (funcall ivy-read-action-format-function (cdr actions)))
- (resize-mini-windows t)
- (key "")
- action-idx)
- (while (and (setq action-idx (cl-position-if
- (lambda (x)
- (string-prefix-p key (car x)))
- (cdr actions)))
- (not (string= key (car (nth action-idx (cdr actions))))))
- (setq key (concat key (string (read-key hint)))))
- (ivy-shrink-after-dispatching)
- (cond ((member key '("" ""))
- nil)
- ((null action-idx)
- (message "%s is not bound" key)
- nil)
- (t
- (message "")
- (setcar actions (1+ action-idx))
- (ivy-set-action actions)))))))
+ (funcall ivy-read-action-function actions))))
+
+(defun ivy-read-action-by-key (actions)
+ (let* ((hint (funcall ivy-read-action-format-function (cdr actions)))
+ (resize-mini-windows t)
+ (key "")
+ action-idx)
+ (while (and (setq action-idx (cl-position-if
+ (lambda (x)
+ (string-prefix-p key (car x)))
+ (cdr actions)))
+ (not (string= key (car (nth action-idx (cdr actions))))))
+ (setq key (concat key (string (read-key hint)))))
+ (ivy-shrink-after-dispatching)
+ (cond ((member key '("" ""))
+ nil)
+ ((null action-idx)
+ (message "%s is not bound" key)
+ nil)
+ (t
+ (message "")
+ (setcar actions (1+ action-idx))
+ (ivy-set-action actions)))))
+
+(defun ivy-read-action-ivy (actions)
+ "Select an action from ACTIONS using Ivy."
+ (let ((enable-recursive-minibuffers t))
+ (ivy-read "action: "
+ (cl-mapcar
+ (lambda (a i) (cons (format "[%s] %s" (nth 0 a) (nth 2 a)) i))
+ (cdr actions) (number-sequence 1 (length (cdr actions))))
+ :action (lambda (a)
+ (setcar actions (cdr a))
+ (ivy-set-action actions))
+ :caller 'ivy-read-action-ivy)))
(defun ivy-shrink-after-dispatching ()
"Shrink the window after dispatching when action list is too large."