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

counsel-find-file: Support opening in other tab #2915

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions counsel.el
Original file line number Diff line number Diff line change
Expand Up @@ -1935,8 +1935,10 @@ choose between `yes-or-no-p' and `y-or-n-p'; otherwise default to

(ivy-set-actions
'counsel-find-file
'(("j" find-file-other-window "other window")
`(("j" find-file-other-window "other window")
("f" find-file-other-frame "other frame")
,@(and (fboundp 'find-file-other-tab)
'(("t" find-file-other-tab "other tab")))
("b" counsel-find-file-cd-bookmark-action "cd bookmark")
("x" counsel-find-file-extern "open externally")
("r" counsel-find-file-as-root "open as root")
Expand Down Expand Up @@ -2331,6 +2333,8 @@ https://www.freedesktop.org/wiki/Specifications/desktop-bookmark-spec"))
'counsel-recentf
`(("j" find-file-other-window "other window")
("f" find-file-other-frame "other frame")
,@(and (fboundp 'find-file-other-tab)
'(("t" find-file-other-tab "other tab")))
("x" counsel-find-file-extern "open externally")
("d" ,(lambda (file) (setq recentf-list (delete file recentf-list)))
"delete from recentf")))
Expand Down Expand Up @@ -2439,8 +2443,10 @@ This function uses the `dom' library from Emacs 25.1 or later."

(ivy-set-actions
'counsel-buffer-or-recentf
'(("j" find-file-other-window "other window")
`(("j" find-file-other-window "other window")
("f" find-file-other-frame "other frame")
,@(and (fboundp 'find-file-other-tab)
'(("t" find-file-other-tab "other tab")))
("x" counsel-find-file-extern "open externally")))

(defun counsel-buffer-or-recentf-transformer (var)
Expand Down Expand Up @@ -2488,9 +2494,15 @@ By default `counsel-bookmark' opens a dired buffer for directories."
(lambda (bookmark)
(funcall fn (bookmark-location bookmark))))

(defun counsel--bookmark-jump-other-tab (bookmark)
"Jump to BOOKMARK in another tab."
(bookmark-jump bookmark 'switch-to-buffer-other-tab))

(ivy-set-actions
'counsel-bookmark
`(("j" bookmark-jump-other-window "other window")
,@(and (fboundp 'switch-to-buffer-other-tab)
`(("t" counsel--bookmark-jump-other-tab "other tab")))
("d" bookmark-delete "delete")
("e" bookmark-rename "edit")
("s" bookmark-set "overwrite")
Expand Down Expand Up @@ -6346,8 +6358,10 @@ in the current window."

(ivy-set-actions
'counsel-switch-buffer
'(("x" counsel-open-buffer-file-externally "open externally")
("j" ivy--switch-buffer-other-window-action "other window")))
`(("x" counsel-open-buffer-file-externally "open externally")
("j" ivy--switch-buffer-other-window-action "other window")
,@(and (fboundp 'ivy--switch-buffer-other-tab-action)
'(("t" ivy--switch-buffer-other-tab-action "other tab")))))

;;** `counsel-compile'
(defvar counsel-compile-history nil
Expand Down
44 changes: 34 additions & 10 deletions ivy.el
Original file line number Diff line number Diff line change
Expand Up @@ -4430,16 +4430,38 @@ BUFFER may be a string or nil."
(switch-to-buffer
buffer nil 'force-same-window))))))

(defun ivy--switch-buffer-elsewhere (bufname switch visit)
"Switch to BUFNAME in other window/frame/tab/etc.
If BUFNAME is nil or empty use `ivy-text' in its place.
SWITCH and VISIT are the desired buffer-switching and
file-visiting functions to use, respectively. Which one is used
depends on whether BUFNAME corresponds to a virtual buffer."
(let* ((empty (zerop (length bufname)))
(virtual (and (not empty)
(assoc bufname ivy--virtual-buffers))))
(if (and virtual (not (get-buffer bufname)))
(funcall visit (cdr virtual))
(funcall switch (if empty ivy-text bufname)))))

(defun ivy--switch-buffer-other-window-action (buffer)
"Switch to BUFFER in other window.
BUFFER may be a string or nil."
(if (zerop (length buffer))
(switch-to-buffer-other-window ivy-text)
(let ((virtual (assoc buffer ivy--virtual-buffers)))
(if (and virtual
(not (get-buffer buffer)))
(find-file-other-window (cdr virtual))
(switch-to-buffer-other-window buffer)))))
"Switch to BUFFER (a string or nil) in other window.
If BUFFER is nil or empty use `ivy-text' in its place.
This function also handles the case where BUFFER is virtual;
see `ivy-use-virtual-buffers'."
(ivy--switch-buffer-elsewhere buffer
#'switch-to-buffer-other-window
#'find-file-other-window))

(when (and (fboundp 'switch-to-buffer-other-tab)
(fboundp 'find-file-other-tab))
(defun ivy--switch-buffer-other-tab-action (buffer)
"Switch to BUFFER (a string or nil) in other tab.
If BUFFER is nil or empty use `ivy-text' in its place.
This function also handles the case where BUFFER is virtual;
see `ivy-use-virtual-buffers'."
(ivy--switch-buffer-elsewhere buffer
#'switch-to-buffer-other-tab
#'find-file-other-tab)))

(defun ivy--rename-buffer-action (buffer)
"Rename BUFFER."
Expand Down Expand Up @@ -4505,12 +4527,14 @@ Otherwise, forward to `ivy-kill-line'."

(ivy-set-actions
'ivy-switch-buffer
'(("f"
`(("f"
ivy--find-file-action
"find file")
("j"
ivy--switch-buffer-other-window-action
"other window")
,@(and (fboundp 'ivy--switch-buffer-other-tab-action)
'(("t" ivy--switch-buffer-other-tab-action "other tab")))
("k"
ivy--kill-buffer-action
"kill")
Expand Down