summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThierry Volpiatto <thierry.volpiatto@gmail.com>2019-10-04 09:38:56 +0200
committerThierry Volpiatto <thierry.volpiatto@gmail.com>2019-10-04 09:55:54 +0200
commit529447a53817bf0f791f9684865b527049dbcabc (patch)
tree0080986551bfc1986d34fd689befd5468fce0b0f
parent884cbe97dc587c0e38680bbe3279b4cf0bbc82bc (diff)
Allow switching to buffer in its tab
* helm-buffers.el (helm-buffers-maybe-switch-to-tab): New user var. * helm-utils.el (helm-window-show-buffers): Do it. (helm-buffers-switch-to-buffer-or-tab): New. (helm-buffers--get-tab-from-name): New. (helm-buffers--buffer-in-tab-p): New.
-rw-r--r--helm-buffers.el7
-rw-r--r--helm-utils.el34
2 files changed, 40 insertions, 1 deletions
diff --git a/helm-buffers.el b/helm-buffers.el
index 4fd9991d..b8fc6c7f 100644
--- a/helm-buffers.el
+++ b/helm-buffers.el
@@ -125,6 +125,13 @@ Also if some pretty names are too long you can add your own
abbreviation here."
:type '(alist :key-type symbol :value-type string)
:group 'helm-buffers)
+
+(defcustom helm-buffers-maybe-switch-to-tab t
+ "Switch to buffer in its tab when non nil.
+This have no effect when `tab-bar-mode' is not available."
+ :group 'helm-buffers
+ :type 'boolean)
+
;;; Faces
;;
diff --git a/helm-utils.el b/helm-utils.el
index 5593447b..b006f208 100644
--- a/helm-utils.el
+++ b/helm-utils.el
@@ -27,6 +27,8 @@
(declare-function markdown-show-subtree "outline.el")
(declare-function outline-show-subtree "outline.el")
(declare-function org-reveal "org.el")
+(declare-function tab-bar-tabs "tab-bar.el")
+(declare-function tab-bar-select-tab "tab-bar.el")
(defvar org-directory)
(defvar winner-boring-buffers)
(defvar helm-show-completion-overlay)
@@ -268,7 +270,37 @@ If a prefix arg is given split windows vertically."
(and other-window initial-ow-fn))
(if other-window
(funcall initial-ow-fn (car buffers))
- (switch-to-buffer (car buffers))))))
+ (helm-buffers-switch-to-buffer-or-tab (car buffers))))))
+
+(defun helm-buffers-switch-to-buffer-or-tab (buffer)
+ "Switch to BUFFER in its tab if some."
+ (if (and (fboundp 'tab-bar-mode)
+ helm-buffers-maybe-switch-to-tab)
+ (let* ((tabs (tab-bar-tabs))
+ (tab-names (mapcar (lambda (tab)
+ (cdr (assq 'name tab)))
+ (tab-bar-tabs)))
+ (bname (buffer-name (get-buffer buffer)))
+ (tab (helm-buffers--get-tab-from-name bname tabs)))
+ (if (helm-buffers--buffer-in-tab-p bname tab-names)
+ (progn
+ (tab-bar-select-tab tab)
+ (select-window (get-buffer-window bname)))
+ (switch-to-buffer buffer)))
+ (switch-to-buffer buffer)))
+
+(defun helm-buffers--get-tab-from-name (tab-name tabs)
+ "Return tab from TABS when it contains TAB-NAME."
+ (cl-loop for tab in tabs
+ when (member tab-name (split-string (cdr (assq 'name tab)) ", " t))
+ return tab))
+
+(defun helm-buffers--buffer-in-tab-p (buffer-name tab-names)
+ "Check if BUFFER-NAME is in TAB-NAMES list."
+ (cl-loop for name in tab-names
+ ;; Buf names are separated with "," in TAB-NAMES
+ ;; e.g. '("tab-bar.el" "*scratch*, helm-buffers.el").
+ thereis (member buffer-name (split-string name ", " t))))
(defun helm-window-default-split-fn (candidates &optional other-window-fn)
"Split windows in one direction and balance them.