From b172049717ee2dd55e389bfa96d33a1850fe41ef Mon Sep 17 00:00:00 2001 From: Juri Linkov Date: Wed, 15 Sep 2021 09:30:11 +0300 Subject: [PATCH] =?utf8?q?*=20lisp/tab-bar.el=20(tab-bar-close-other-tabs)?= =?utf8?q?:=20Add=20arg=20=E2=80=98tab-number=E2=80=99.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit (tab-bar-mouse-context-menu): Bind menu-item "Close other tabs" to tab-bar-close-other-tabs with arg ‘tab-number’. --- lisp/tab-bar.el | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/lisp/tab-bar.el b/lisp/tab-bar.el index 89af3069024..d6173d9ca42 100644 --- a/lisp/tab-bar.el +++ b/lisp/tab-bar.el @@ -316,7 +316,12 @@ that closes only when clicked on the close button." (define-key-after menu [close] `(menu-item "Close" (lambda () (interactive) (tab-bar-close-tab ,tab-number)) - :help "Close the tab")))) + :help "Close the tab")) + (define-key-after menu [close-other] + `(menu-item "Close other tabs" + (lambda () (interactive) + (tab-bar-close-other-tabs ,tab-number)) + :help "Close all other tabs")))) (popup-menu menu event))) @@ -1405,15 +1410,25 @@ for the last tab on a frame is determined by (funcall tab-bar-tabs-function))))) (tab-bar-close-tab (1+ (tab-bar--tab-index-by-name name)))) -(defun tab-bar-close-other-tabs () - "Close all tabs on the selected frame, except the selected one." +(defun tab-bar-close-other-tabs (&optional tab-number) + "Close all tabs on the selected frame, except TAB-NUMBER. +TAB-NUMBER counts from 1 and defaults to the current tab." (interactive) (let* ((tabs (funcall tab-bar-tabs-function)) - (current-tab (tab-bar--current-tab-find tabs)) + (current-index (tab-bar--current-tab-index tabs)) + (keep-index (if (integerp tab-number) + (1- (max 0 (min tab-number (length tabs)))) + current-index)) + (keep-tab (nth keep-index tabs)) (index 0)) - (when current-tab + + (when keep-tab + (unless (eq keep-index current-index) + (tab-bar-select-tab (1+ keep-index)) + (setq tabs (funcall tab-bar-tabs-function))) + (dolist (tab tabs) - (unless (or (eq tab current-tab) + (unless (or (eq tab keep-tab) (run-hook-with-args-until-success 'tab-bar-tab-prevent-close-functions tab ;; `last-tab-p' logically can't ever be true -- 2.39.5