]> git.eshelyaron.com Git - emacs.git/commitdiff
Add new user option tab-bar-define-keys
authorshipmints <shipmints@gmail.com>
Wed, 29 Jan 2025 18:43:04 +0000 (13:43 -0500)
committerEshel Yaron <me@eshelyaron.com>
Mon, 3 Feb 2025 11:10:40 +0000 (12:10 +0100)
* lisp/tab-bar.el (tab-bar-define-keys):
Add new defcustom tab-bar-define-keys.  Reorganize key binding functions
to accommodate.  Also remove checks for tab-bar-mode enabled in
'tab-bar-select-tab-modifiers', as unnecessary and which prevented user
changes from being accepted in cases where the user defers enabling
tab-bar-mode (bug#75918).

(cherry picked from commit 6c46e2a363195fea338bc89cdc8fa9a46b63e63a)

lisp/tab-bar.el

index bb2720297a0d2aa9122dac4db0290dba18cebcf6..567cdfbc2103df706e4fe9cbc6fb91fba97fba7c 100644 (file)
   :group 'tab-bar-faces)
 
 \f
+
+(defvar tab-bar-mode-map (make-sparse-keymap)
+  "Tab Bar mode map.")
+
+(defcustom tab-bar-define-keys t
+  "Define specified tab-bar key bindings.
+If t, the default, all key mappings are defined.
+
+If \\='numeric, define only numeric select-tab key mappings, and in
+conjunction with `tab-bar-select-tab-modifiers', which see.
+
+If \\='tab, define only TAB and SHIFT-TAB tab-selection key mappings.
+
+If nil, do not define any key mappings.
+
+Customize this option, or use `setopt' to ensure it will take effect."
+  :type '(choice (const :tag "All keys" t)
+                 (const :tag "Numeric tab selection keys" numeric)
+                 (const :tag "TAB and SHIFT-TAB selection keys" tab)
+                 (const :tag "None" nil))
+  :initialize #'custom-initialize-default
+  :set (lambda (sym val)
+         (tab-bar--undefine-keys)
+         (set-default sym val)
+         ;; Enable the new keybindings
+         (tab-bar--define-keys))
+  :group 'tab-bar
+  :version "31.1")
+
 (defcustom tab-bar-select-tab-modifiers '()
   "List of modifier keys for selecting tab-bar tabs by their numbers.
 Possible modifier keys are `control', `meta', `shift', `hyper', `super' and
@@ -104,18 +133,17 @@ 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))
+         (tab-bar--undefine-keys)
          (set-default sym val)
-         ;; Reenable the tab-bar with new keybindings
-         (when tab-bar-mode
-           (tab-bar--define-keys)))
+         ;; Enable the new keybindings
+         (tab-bar--define-keys))
   :group 'tab-bar
   :version "27.1")
 
 (defun tab-bar--define-keys ()
   "Install key bindings to switch between tabs if so configured."
-  (when tab-bar-select-tab-modifiers
+  (when (and (memq tab-bar-define-keys '(t numeric))
+             tab-bar-select-tab-modifiers)
     (define-key tab-bar-mode-map
                 (vector (append tab-bar-select-tab-modifiers (list ?0)))
                 #'tab-recent)
@@ -128,6 +156,14 @@ For easier selection of tabs by their numbers, consider customizing
                 (vector (append tab-bar-select-tab-modifiers (list ?9)))
                 #'tab-last))
 
+  (when (memq tab-bar-define-keys '(t tab))
+    (unless (global-key-binding [(control tab)])
+      (define-key tab-bar-mode-map [(control tab)] #'tab-next))
+    (unless (global-key-binding [(control shift tab)])
+      (define-key tab-bar-mode-map [(control shift tab)] #'tab-previous))
+    (unless (global-key-binding [(control shift iso-lefttab)])
+      (define-key tab-bar-mode-map [(control shift iso-lefttab)] #'tab-previous)))
+
   ;; Replace default value with a condition that supports displaying
   ;; global-mode-string in the tab bar instead of the mode line.
   (when (and (memq 'tab-bar-format-global tab-bar-format)
@@ -152,7 +188,11 @@ For easier selection of tabs by their numbers, consider customizing
                   nil t))
     (define-key tab-bar-mode-map
                 (vector (append tab-bar-select-tab-modifiers (list ?9)))
-                nil t)))
+                nil t))
+
+  (define-key tab-bar-mode-map [(control tab)] nil t)
+  (define-key tab-bar-mode-map [(control shift tab)] nil t)
+  (define-key tab-bar-mode-map [(control shift iso-lefttab)] nil t))
 
 (defun tab-bar--load-buttons ()
   "Load the icons for the tab buttons."
@@ -242,20 +282,6 @@ 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).