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

Improvement: Start counsel-find-file from ivy switch to buffer #1295

Closed
Opioid opened this Issue Nov 20, 2017 · 12 comments

Comments

Projects
None yet
4 participants
@Opioid
Copy link

commented Nov 20, 2017

With ido it is possible to quickly change from "switch to buffer" to find-file by typing C-f. This is useful when you realize you haven't already opened the file you try switching to. Maybe this is already possible?
I tried (define-key ivy-minibuffer-map (kbd "C-f") 'counsel-find-file) but this led to the message

ivy-read: Command attempted to use minibuffer while in minibuffer

@basil-conto

This comment has been minimized.

Copy link
Contributor

commented Nov 20, 2017

Perhaps there's a more elegant way, but one way to achieve this is by adding a new action to do your bidding, e.g.

(ivy-add-actions #'ivy-switch-buffer
                 `(("f"
                    ,(lambda (_)
                       (call-interactively #'counsel-find-file))
                    "find file")))

With this in place, during ivy-switch-buffer completion you would type M-o (ivy-dispatching-done) followed by f to switch to counsel-find-file completion.

@abo-abo

This comment has been minimized.

Copy link
Owner

commented Nov 20, 2017

@basil-conto Thanks for answering. I would further update your answer to:

(ivy-add-actions
 'ivy-switch-buffer
 '(("f" (lambda (_) (counsel-find-file)) "find file")))

Changes:

  1. The argument to ivy-add-actions is a symbol, it doesn't have to be a function.
  2. counsel-find-file can work even without call-interactively.
  3. No need for the quasi-quote, lambda in Elisp is self-quoting.

Here's something closer to what @Opioid tried:

(define-key ivy-switch-buffer-map (kbd "C-f")
  (lambda ()
    (interactive)
    (ivy-exit-with-action
     (lambda (_) (counsel-find-file)))))

But I like the ivy-add-actions interface more, since it's extensible (you can have many actions anyway) and uniform (doesn't rob you of C-f binding for forward-char which I find useful to have).

@mookid

This comment has been minimized.

Copy link
Contributor

commented Nov 20, 2017

It would make sense to put the directory of the current match as initial input of counsel-find-file.

@basil-conto

This comment has been minimized.

Copy link
Contributor

commented Nov 20, 2017

@abo-abo

The argument to ivy-add-actions is a symbol, it doesn't have to be a function.

Indeed, a plain quote will do, but both in general and in this particular case the choice between quote and function for symbols comes down to personal style.

counsel-find-file can work even without call-interactively

Indeed, but again it comes down to personal preference. For example, the interactive-spec of counsel-find-file may change one day. Using call-interactively is more faithful to / consistent with the invocation of commands by key binding / ivy action.

No need for the quasi-quote, lambda in Elisp is self-quoting.

Indeed, which is precisely why I evaluate mine to a closure (or a regular anonymous function when lexical-binding is missing / disabled). Your example, on the other hand, is quoting it as a form, rather than as a function, which in general I consider inadvisable, even if it makes no difference in this simple scenario.

@abo-abo

This comment has been minimized.

Copy link
Owner

commented Nov 20, 2017

You're right. I wasn't sure it was the case, but #'foo and 'foo are the same key in an alist/hash:

(eq #'counsel-find-file 'counsel-find-file)
;; => t
@abo-abo

This comment has been minimized.

Copy link
Owner

commented Nov 20, 2017

@basil-conto And you're right about the lambdas too, yours is a closure and mine is actually quoted. Had a long day :)

@basil-conto

This comment has been minimized.

Copy link
Contributor

commented Nov 20, 2017

@abo-abo

#'foo and 'foo are the same key in an alist/hash

FWIW, I remember seeing a comment by Stefan Monnier somewhere (Emacs SE?) that he prefers to use function only where an anonymous function would be equivalent. I agree with this convention, but I also like to convey as much intent as possible, both to the reader and the byte-compiler. To each their own. :)

Had a long day

Hope you get some deserved rest; I'm always pleasantly surprised at how you keep up with all these popular projects. :)

@abo-abo

This comment has been minimized.

Copy link
Owner

commented Nov 20, 2017

I'm always pleasantly surprised at how you keep up with all these popular projects. :)

I try. But sometime this year things got out of hand with ivy: 184 issues and 19 pull requests is really a lot. I think it was around 20 open issues at the start of the year.

@basil-conto

This comment has been minimized.

Copy link
Contributor

commented Nov 20, 2017

@mookid

How about something like

(ivy-add-actions
 'ivy-switch-buffer
 `(("f"
    ,(lambda (str)
       (let* ((buf (get-buffer str))
              (default-directory
                (or (and buf (buffer-local-value 'default-directory buf))
                    default-directory)))
         (counsel-find-file)))
    "find file")))
@basil-conto

This comment has been minimized.

Copy link
Contributor

commented Nov 20, 2017

@abo-abo There's a reason the Haskell folks like to say "avoid success at all cost." :) FWIW, I would like to try and help out more from the summer onwards. In the meantime, as always, thanks for all your great work!

@basil-conto

This comment has been minimized.

Copy link
Contributor

commented Nov 20, 2017

I'm actually finding the suggestion by @mookid quite convenient and useful. Anyone else like it enough to warrant a PR?

@abo-abo

This comment has been minimized.

Copy link
Owner

commented Nov 20, 2017

FWIW, I would like to try and help out more from the summer onwards.

Thanks, any help is welcome and very appreciated.

In the meantime, as always, thanks for all your great work!

You're welcome.

I'm actually finding the suggestion by @mookid quite convenient and useful. Anyone else like it enough to warrant a PR?

Please PR.

basil-conto added a commit to basil-conto/swiper that referenced this issue Nov 21, 2017

ivy.el: Add buffer->file completion switch action
Based on the idea of Nathan Moreau <nathan.moreau@m4x.org>.

Re abo-abo#1295

@Opioid Opioid closed this Nov 22, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.