Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ivy-set-default-action #363

Closed
drorbemet opened this issue Jan 28, 2016 · 7 comments
Closed

ivy-set-default-action #363

drorbemet opened this issue Jan 28, 2016 · 7 comments

Comments

@drorbemet
Copy link

After using ivy-set-actions for a while some lists of actions got too long.
Now I would like to try the other way around and set the default action within a wrapper function for a call to an ivy- or a counsel-command.
What would be the best way to do that?

  • example
(defun run-my-special-action-on-results-of-ivy-or-cousel-call (initial-input)
  (interactive)
  (progn 
     (ivy-set-default-action 'my-special-action)
     (counsel-command initial-input)))
@abo-abo
Copy link
Owner

abo-abo commented Jan 29, 2016

You can't change the default action. However, you can modify all the other ones. See this code:

(defun ivy-set-actions (cmd actions)
  "Set CMD extra exit points to ACTIONS."
  (setq ivy--actions-list
        (plist-put ivy--actions-list cmd actions)))

You'll have to let-bind ivy--actions-list in your run-my-special-action-on-results-of-ivy-or-cousel-call.

@drorbemet
Copy link
Author

Until now I am using three kind of definitions: the action command, the interactive function and a defadvice. An example:

(defun action--vlc-snapshot (x)
  (call-process "vlc" nil 0 nil "--snapshot-path" (f-dir x) x))

(defun counsel-locate-with-vlc ()
  (interactive)
  (counsel-locate "preconfigure a pattern .mp4"))

(defadvice vlc-actions (before counsel-locate-with-vlc-before activate)
  (ivy-set-actions 'counsel-locate-with-vlc
                   `(("i" action--vlc-snapshot "vlc with snapshotconfig"))))

I'll keep using the above. But just to make sure that I am using the optimal solution here let me continue with two more questions.

In order to avoid defadvice. Is that what you suggested?
I tried to follow your hint. But I didn't get the following to work.

(defun open-with-vlc ()
  (interactive)
  (let ((ivy--actions-list '()))
    (setq ivy--actions-list
          (plist-put ivy--actions-list
                     'counsel-locate
                     `(("i" action--one-of-many "vlc with snapshotconfig"))))
    (counsel-locate "preconfigure a pattern .mp4")))

I am not quite sure why this would help.

Actually I just want to have to use just one keybinding to call an action instead of having to use M-o and then i. Because in this case this is the only action that I decided to use before calling counsel-locate. Should and could I assign a keybinding to the action within the ivy-buffer to skip the one-custom-action-menu?
This might sound stingy but there are cases where I would welcome the option of being able to use counsel-commands within a function for just one action repeatedly or even just one quick call of one custom action.

@abo-abo
Copy link
Owner

abo-abo commented Jan 29, 2016

Since the dispatch is on this-command (see ivy-read code), the above code fixes the problem:

(defun open-with-vlc ()
  (interactive)
  (let ((ivy--actions-list '()))
    (ivy-set-actions 'open-with-vlc
                     '(("i" action--one-of-many "vlc with snapshotconfig")))
    (counsel-locate "preconfigure a pattern .mp4")))

However, with the new variable, you can code like this:

(defun open-with-vlc ()
  (interactive)
  (let* ((ivy-inhibit-action t)
         (file (counsel-locate "foo")))
    (open-with-vlc file)))

Now, all you get from counsel-locate is a string, do with it whatever you want.

Thanks for the request, by the way, I think it resulted in a nice improvement in the interface.

@drorbemet
Copy link
Author

Thanks a lot that's what I have been missing.

@drorbemet
Copy link
Author

If I put it in a let* block the default action doesn't seem to work. Meaning after selecting a search result and hitting enter no file-buffer appears.

I think this is a new issue. #371

@abo-abo
Copy link
Owner

abo-abo commented Jan 30, 2016

You have to bind it back:

(defun counsel-locate-ag ()
  (interactive)
  (let* ((ivy-inhibit-action t)
         (file (counsel-locate ""))
         (ivy-inhibit-action nil))
    (counsel-ag "" (f-dir file))))

@drorbemet
Copy link
Author

Ah, ok I get it, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants