From 17102212c99272259e2f78d9662a0ada0d0c17be Mon Sep 17 00:00:00 2001 From: Juri Linkov Date: Fri, 5 Apr 2024 19:23:08 +0300 Subject: [PATCH] * lisp/tab-bar.el (tab-bar-mode-map): New keymap (bug#69578). (tab-bar-select-tab-modifiers): Call tab-bar--undefine-keys before set-default in :set of defcustom. (tab-bar--define-keys, tab-bar--undefine-keys): Change keybindings in tab-bar-mode-map instead of the global map. Move checking of global-key-binding to tab-bar-mode--tab-key-bind. (tab-bar-mode--tab-key-bind): New internal function. (cherry picked from commit 50fab7062d9edf1a2698f3d468dcbcd1c9a3ee9b) --- lisp/tab-bar.el | 59 +++++++++++++++++++++++++++++++------------------ 1 file changed, 38 insertions(+), 21 deletions(-) diff --git a/lisp/tab-bar.el b/lisp/tab-bar.el index 05631c3c8f3..cd076664faf 100644 --- a/lisp/tab-bar.el +++ b/lisp/tab-bar.el @@ -104,10 +104,11 @@ For easier selection of tabs by their numbers, consider customizing (const alt)) :initialize #'custom-initialize-default :set (lambda (sym val) + (when tab-bar-mode + (tab-bar--undefine-keys)) (set-default sym val) ;; Reenable the tab-bar with new keybindings (when tab-bar-mode - (tab-bar--undefine-keys) (tab-bar--define-keys))) :group 'tab-bar :version "27.1") @@ -115,21 +116,17 @@ For easier selection of tabs by their numbers, consider customizing (defun tab-bar--define-keys () "Install key bindings to switch between tabs if so configured." (when tab-bar-select-tab-modifiers - (global-set-key (vector (append tab-bar-select-tab-modifiers (list ?0))) - #'tab-recent) + (define-key tab-bar-mode-map + (vector (append tab-bar-select-tab-modifiers (list ?0))) + #'tab-recent) (dotimes (i 8) - (global-set-key (vector (append tab-bar-select-tab-modifiers - (list (+ i 1 ?0)))) - #'tab-bar-select-tab)) - (global-set-key (vector (append tab-bar-select-tab-modifiers (list ?9))) - #'tab-last)) - ;; Don't override user customized key bindings - (unless (global-key-binding [(control tab)]) - (global-set-key [(control tab)] #'tab-next)) - (unless (global-key-binding [(control shift tab)]) - (global-set-key [(control shift tab)] #'tab-previous)) - (unless (global-key-binding [(control shift iso-lefttab)]) - (global-set-key [(control shift iso-lefttab)] #'tab-previous)) + (define-key tab-bar-mode-map + (vector (append tab-bar-select-tab-modifiers + (list (+ i 1 ?0)))) + #'tab-bar-select-tab)) + (define-key tab-bar-mode-map + (vector (append tab-bar-select-tab-modifiers (list ?9))) + #'tab-last)) ;; Replace default value with a condition that supports displaying ;; global-mode-string in the tab bar instead of the mode line. @@ -144,12 +141,18 @@ For easier selection of tabs by their numbers, consider customizing (defun tab-bar--undefine-keys () "Uninstall key bindings previously bound by `tab-bar--define-keys'." - (when (eq (global-key-binding [(control tab)]) 'tab-next) - (global-unset-key [(control tab)])) - (when (eq (global-key-binding [(control shift tab)]) 'tab-previous) - (global-unset-key [(control shift tab)])) - (when (eq (global-key-binding [(control shift iso-lefttab)]) 'tab-previous) - (global-unset-key [(control shift iso-lefttab)]))) + (when tab-bar-select-tab-modifiers + (define-key tab-bar-mode-map + (vector (append tab-bar-select-tab-modifiers (list ?0))) + nil t) + (dotimes (i 8) + (define-key tab-bar-mode-map + (vector (append tab-bar-select-tab-modifiers + (list (+ i 1 ?0)))) + nil t)) + (define-key tab-bar-mode-map + (vector (append tab-bar-select-tab-modifiers (list ?9))) + nil t))) (defun tab-bar--load-buttons () "Load the icons for the tab buttons." @@ -239,6 +242,20 @@ a list of frames to update." (if (and tab-bar-mode (eq tab-bar-show t)) 1 0)) (assq-delete-all 'tab-bar-lines default-frame-alist))))) +(defun tab-bar-mode--tab-key-bind (map key binding) + ;; Don't override user customized global key bindings + (define-key map key + `(menu-item "" ,binding + :filter ,(lambda (cmd) (unless (global-key-binding key) cmd))))) + +(defvar tab-bar-mode-map + (let ((map (make-sparse-keymap))) + (tab-bar-mode--tab-key-bind map [(control tab)] #'tab-next) + (tab-bar-mode--tab-key-bind map [(control shift tab)] #'tab-previous) + (tab-bar-mode--tab-key-bind map [(control shift iso-lefttab)] #'tab-previous) + map) + "Tab Bar mode map.") + (define-minor-mode tab-bar-mode "Toggle the tab bar in all graphical frames (Tab Bar mode)." :global t -- 2.39.5