emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

master 6c46e2a3631 2/3: Add new user option tab-bar-define-keys


From: Juri Linkov
Subject: master 6c46e2a3631 2/3: Add new user option tab-bar-define-keys
Date: Thu, 30 Jan 2025 13:38:01 -0500 (EST)

branch: master
commit 6c46e2a363195fea338bc89cdc8fa9a46b63e63a
Author: shipmints <shipmints@gmail.com>
Commit: Juri Linkov <juri@linkov.net>

    Add new user option tab-bar-define-keys
    
    * 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).
---
 etc/NEWS        | 11 ++++++++++
 lisp/tab-bar.el | 68 +++++++++++++++++++++++++++++++++++++++------------------
 2 files changed, 58 insertions(+), 21 deletions(-)

diff --git a/etc/NEWS b/etc/NEWS
index 2f1057a1307..1e7365258a3 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -245,6 +245,17 @@ This hook allows you to operate on a reopened tab.
 This is useful when you define custom tab parameters that may need
 adjustment when a tab is restored, and avoids advice.
 
+---
+*** New user option 'tab-bar-define-keys'.
+This controls which key bindings tab-bar creates.  Values are t, the
+default, which defines all keys and is backwards compatible, 'numeric'
+(tab number selection only), 'tab' (TAB and SHIFT-TAB keys only), nil
+(which defines none).
+
+This is useful to avoid key binding conflicts, such as when folding in
+outline mode using TAB keys, or when a user wants to define her own
+tab-bar keys without first having to remove the defaults.
+
 ** Project
 
 ---
diff --git a/lisp/tab-bar.el b/lisp/tab-bar.el
index f220ee768ae..85b8e3da1bf 100644
--- a/lisp/tab-bar.el
+++ b/lisp/tab-bar.el
@@ -86,6 +86,35 @@
   :group 'tab-bar-faces)
 
 
+
+(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).
 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]