(if (string= event "exited abnormally with code 1\n")
(message "Error"))))
+(defun counsel-locate-action-extern (x)
+ "Use xdg-open shell command on X."
+ (call-process shell-file-name nil
+ nil nil
+ shell-command-switch
+ (format "xdg-open %s" (shell-quote-argument x))))
+
+(defun counsel-locate-action-dired (x)
+ "Use `dired-jump' on X."
+ (dired-jump nil x))
+
;;;###autoload
(defun counsel-locate ()
- "Call locate."
+ "Call locate shell command."
(interactive)
- (ivy-read "pattern: " nil
+ (ivy-read "Locate: " nil
:dynamic-collection #'counsel-locate-function
- :action (lambda (val)
- (when val
- (find-file val)))))
+ :action
+ (cons
+ 1
+ '(("default" (lambda (val)
+ (when val
+ (find-file val))))
+ ("xdg-open" counsel-locate-action-extern)
+ ("dired" counsel-locate-action-dired)))))
(defun counsel--generic (completion-fn)
"Complete thing at point with COMPLETION-FN."
(defhydra hydra-ivy (:hint nil
:color pink)
"
-^^^^^^ ^Yes^ ^No^ ^Maybe^
-^^^^^^^^^^^^^^---------------------------------------
-^ ^ _k_ ^ ^ _f_ollow _i_nsert _c_: calling %s(if ivy-calling \"on\" \"off\")
+^^^^^^ ^Yes^ ^No^ ^Maybe^ ^Action^
+^^^^^^^^^^^^^^---------------------------------------------------
+^ ^ _k_ ^ ^ _f_ollow _i_nsert _c_: calling %s(if ivy-calling \"on\" \"off\") _w_/_s_: %s(ivy-action-name)
_h_ ^+^ _l_ _d_one _o_ops _m_: matcher %s(if (eq ivy--regex-function 'ivy--regex-fuzzy) \"fuzzy\" \"ivy\")
^ ^ _j_ ^ ^ ^ ^ ^ ^ _<_/_>_: shrink/grow window
"
("c" ivy-toggle-calling)
("m" ivy-toggle-fuzzy)
(">" ivy-minibuffer-grow)
- ("<" ivy-minibuffer-shrink))
+ ("<" ivy-minibuffer-shrink)
+ ("w" ivy-prev-action)
+ ("s" ivy-next-action))
(provide 'ivy-hydra)
(when (setq ivy-calling (not ivy-calling))
(ivy-call)))
+(defun ivy--get-action (state)
+ "Get the action function from STATE."
+ (let ((action (ivy-state-action state)))
+ (when action
+ (if (functionp action)
+ action
+ (cadr (nth (car action) action))))))
+
+(defun ivy--actionp (x)
+ "Return non-nil when X is a list of actions."
+ (and x (listp x) (not (eq (car x) 'closure))))
+
+(defun ivy-next-action ()
+ "When the current action is a list, scroll it forwards."
+ (interactive)
+ (let ((action (ivy-state-action ivy-last)))
+ (when (ivy--actionp action)
+ (unless (>= (car action) (1- (length action)))
+ (cl-incf (car action))))))
+
+(defun ivy-prev-action ()
+ "When the current action is a list, scroll it backwards."
+ (interactive)
+ (let ((action (ivy-state-action ivy-last)))
+ (when (ivy--actionp action)
+ (unless (<= (car action) 1)
+ (cl-decf (car action))))))
+
+(defun ivy-action-name ()
+ "Return the name associated with the current action."
+ (let ((action (ivy-state-action ivy-last)))
+ (if (ivy--actionp action)
+ (format "[%d/%d] %s"
+ (car action)
+ (1- (length action))
+ (car (nth (car action) action)))
+ "[1/1] default")))
+
(defun ivy-call ()
"Call the current action without exiting completion."
- (when (ivy-state-action ivy-last)
- (with-selected-window (ivy-state-window ivy-last)
- (funcall (ivy-state-action ivy-last) ivy--current))))
+ (let ((action (ivy--get-action ivy-last)))
+ (when action
+ (with-selected-window (ivy-state-window ivy-last)
+ (funcall action ivy--current)))))
(defun ivy-next-line-and-call (&optional arg)
"Move cursor vertically down ARG candidates.
(remove-hook 'post-command-hook #'ivy--exhibit)
(when (setq unwind (ivy-state-unwind ivy-last))
(funcall unwind)))
- (when (setq action (ivy-state-action ivy-last))
+ (when (setq action (ivy--get-action ivy-last))
(funcall action ivy--current))))
(defun ivy--reset-state (state)