Skip to content

Commit

Permalink
Reduce ivy-switch-buffer action duplication
Browse files Browse the repository at this point in the history
* ivy.el (ivy--switch-buffer-elsewhere): New subroutine generalized
from ivy--switch-buffer-other-window-action.
(ivy--switch-buffer-other-window-action): Use it.  Expand docstring.
(ivy--switch-buffer-other-tab-action): Ditto.  Define function only
in Emacs 27+.
(ivy-switch-buffer):
* counsel.el (counsel-switch-buffer): Make "other tab" feature
detection explicit.

Re: abo-abo#2915.
  • Loading branch information
basil-conto committed Sep 30, 2021
1 parent d295e0c commit 66a0f47
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 25 deletions.
2 changes: 1 addition & 1 deletion counsel.el
Original file line number Diff line number Diff line change
Expand Up @@ -6357,7 +6357,7 @@ in the current window."
'counsel-switch-buffer
`(("x" counsel-open-buffer-file-externally "open externally")
("j" ivy--switch-buffer-other-window-action "other window")
,@(and (fboundp 'find-file-other-tab)
,@(and (fboundp 'ivy--switch-buffer-other-tab-action)
'(("t" ivy--switch-buffer-other-tab-action "other tab")))))

;;** `counsel-compile'
Expand Down
57 changes: 33 additions & 24 deletions ivy.el
Original file line number Diff line number Diff line change
Expand Up @@ -4430,27 +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)))))

(defun ivy--switch-buffer-other-tab-action (buffer)
"Switch to BUFFER in other tab.
BUFFER may be a string or nil."
(if (zerop (length buffer))
(switch-to-buffer-other-tab ivy-text)
(let ((virtual (assoc buffer ivy--virtual-buffers)))
(if (and virtual
(not (get-buffer buffer)))
(find-file-other-tab (cdr virtual))
(switch-to-buffer-other-tab 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 @@ -4522,10 +4533,8 @@ Otherwise, forward to `ivy-kill-line'."
("j"
ivy--switch-buffer-other-window-action
"other window")
,@(and (fboundp 'find-file-other-tab)
'(("t"
ivy--switch-buffer-other-tab-action
"other tab")))
,@(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

0 comments on commit 66a0f47

Please sign in to comment.