Subject: Re: bug#42052: 28.0.50; tab-bar-mode should be frame-local

From: James N. V. Cash <[email-masked]>
Date: Thu, 09 Jul 2020 13:06:41 -0400

Juri Linkov <[email-masked]> writes:

> Thanks, pushed to master.  If you have more ideas for further development,
> this report could be left open to address these issues too.

I've attached another patch that makes the key bindings & xpm icons get
loaded when tab-bar-show is 1 as well. It doesn't un-load the
keybindings in the way that globalling toggling tab-bar-mode off, but I
don't really see a sensible way of doing that frame-locally.

--- a/lisp/tab-bar.el
+++ b/lisp/tab-bar.el
@@ -95,23 +95,27 @@ tab-bar-select-tab-modifiers
   :version "27.1")
 
 
-(define-minor-mode tab-bar-mode
-  "Toggle the tab bar in all graphical frames (Tab Bar mode)."
-  :global t
-  ;; It's defined in C/cus-start, this stops the d-m-m macro defining it again.
-  :variable tab-bar-mode
-  (let ((val (if tab-bar-mode 1 0)))
-    (dolist (frame (frame-list))
-      (set-frame-parameter frame 'tab-bar-lines val))
-    ;; If the user has given `default-frame-alist' a `tab-bar-lines'
-    ;; parameter, replace it.
-    (if (assq 'tab-bar-lines default-frame-alist)
-        (setq default-frame-alist
-              (cons (cons 'tab-bar-lines val)
-                    (assq-delete-all 'tab-bar-lines
-                                     default-frame-alist)))))
-
-  (when (and tab-bar-mode tab-bar-new-button
+(defun tab-bar--define-keys ()
+  "Install key bindings for switching between tabs if the user has
+configured them."
+  (when tab-bar-select-tab-modifiers
+    (global-set-key (vector (append tab-bar-select-tab-modifiers (list ?0)))
+                    'tab-bar-switch-to-recent-tab)
+    (dotimes (i 9)
+      (global-set-key (vector (append tab-bar-select-tab-modifiers
+                                      (list (+ i 1 ?0))))
+                      'tab-bar-select-tab)))
+  ;; 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)))
+
+(defun tab-bar--load-buttons ()
+  "Load the icons for the tab buttons."
+  (when (and tab-bar-new-button
              (not (get-text-property 0 'display tab-bar-new-button)))
     ;; This file is pre-loaded so only here we can use the right data-directory:
     (add-text-properties 0 (length tab-bar-new-button)
@@ -121,7 +125,7 @@ tab-bar-mode
                                           :ascent center))
                          tab-bar-new-button))
 
-  (when (and tab-bar-mode tab-bar-close-button
+  (when (and tab-bar-close-button
              (not (get-text-property 0 'display tab-bar-close-button)))
     ;; This file is pre-loaded so only here we can use the right data-directory:
     (add-text-properties 0 (length tab-bar-close-button)
@@ -129,24 +133,27 @@ tab-bar-mode
                                           :file "tabs/close.xpm"
                                           :margin (2 . 0)
                                           :ascent center))
-                         tab-bar-close-button))
+                         tab-bar-close-button)))
 
+(define-minor-mode tab-bar-mode
+  "Toggle the tab bar in all graphical frames (Tab Bar mode)."
+  :global t
+  ;; It's defined in C/cus-start, this stops the d-m-m macro defining it again.
+  :variable tab-bar-mode
+  (let ((val (if tab-bar-mode 1 0)))
+    (dolist (frame (frame-list))
+      (set-frame-parameter frame 'tab-bar-lines val))
+    ;; If the user has given `default-frame-alist' a `tab-bar-lines'
+    ;; parameter, replace it.
+    (if (assq 'tab-bar-lines default-frame-alist)
+        (setq default-frame-alist
+              (cons (cons 'tab-bar-lines val)
+                    (assq-delete-all 'tab-bar-lines
+                                     default-frame-alist)))))
+  (when tab-bar-mode
+    (tab-bar--load-buttons))
   (if tab-bar-mode
-      (progn
-        (when tab-bar-select-tab-modifiers
-          (global-set-key (vector (append tab-bar-select-tab-modifiers (list ?0)))
-                          'tab-bar-switch-to-recent-tab)
-          (dotimes (i 9)
-            (global-set-key (vector (append tab-bar-select-tab-modifiers
-                                            (list (+ i 1 ?0))))
-                            'tab-bar-select-tab)))
-        ;; 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)))
+      (tab-bar--define-keys)
     ;; Unset only keys bound by tab-bar
     (when (eq (global-key-binding [(control tab)]) 'tab-next)
       (global-unset-key [(control tab)]))
@@ -806,7 +813,10 @@ tab-bar-new-tab-to
      ((and (natnump tab-bar-show)
            (> (length (funcall tab-bar-tabs-function)) tab-bar-show)
            (zerop (frame-parameter nil 'tab-bar-lines)))
-      (set-frame-parameter nil 'tab-bar-lines 1)))
+      (progn
+        (tab-bar--load-buttons)
+        (tab-bar--define-keys)
+        (set-frame-parameter nil 'tab-bar-lines 1))))
 
     (force-mode-line-update)
     (unless tab-bar-mode