]> git.eshelyaron.com Git - emacs.git/commitdiff
* lisp/tab-bar.el (tab-bar-mode-map): New keymap (bug#69578).
authorJuri Linkov <juri@linkov.net>
Fri, 5 Apr 2024 16:23:08 +0000 (19:23 +0300)
committerEshel Yaron <me@eshelyaron.com>
Sat, 6 Apr 2024 06:42:02 +0000 (08:42 +0200)
(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

index 05631c3c8f3f6b605dfa1ba4f5c7ce3b6841f8b0..cd076664faf5b3334280d6040fe1269240ba9911 100644 (file)
@@ -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