From 66a0f47fa3400a87f4f20b56ddafdf8ccc5a4543 Mon Sep 17 00:00:00 2001 From: "Basil L. Contovounesios" Date: Thu, 30 Sep 2021 15:17:14 +0100 Subject: [PATCH] Reduce ivy-switch-buffer action duplication * 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: #2915. --- counsel.el | 2 +- ivy.el | 57 +++++++++++++++++++++++++++++++----------------------- 2 files changed, 34 insertions(+), 25 deletions(-) diff --git a/counsel.el b/counsel.el index 85b7db89..7d3428ff 100644 --- a/counsel.el +++ b/counsel.el @@ -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' diff --git a/ivy.el b/ivy.el index 23b550b9..a8152936 100644 --- a/ivy.el +++ b/ivy.el @@ -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." @@ -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")