Subject: bug#46299: 28.0.50; Value of tab-bar-show not respected in new frames.

From: Bastian Beranek <[email-masked]>
Date: Mon, 8 Feb 2021 00:03:26 +0100

Hello Juri,

On Sun, Feb 7, 2021 at 8:48 PM Juri Linkov <[email-masked]> wrote:
>
> > I added code to make the frame setting of tab-bar-lines as well as the
> > default-frame-alist value dependent on tab-bar-show to the
> > tab-bar-mode function.
> > I think with this the part to which sets frame parameters in
> > tab-bar-show :set is not needed because (tab-bar-mode 1) is called
> > anyway, which already does everything.
> >
> > What do you think about the attached patch?
>
> I think your earlier idea was better - to have a dynamic function which
> counts the numbers of tabs and adapts the number of tab-bar-lines
> according to the value of tab-bar-show.
>
> Then such a function could be called from many places:
> - tab-bar-mode
> - tab-bar-show :set
> - from the end of tab-bar-new-tab-to
> - from the end of tab-bar-close-tab
> - from the end of tab-bar-close-other-tabs
>
> to sync the actual tabs with the value of tab-bar-show.

Makes sense! How about the attached?

Cheers
Bastian
diff --git a/lisp/tab-bar.el b/lisp/tab-bar.el
index 6720d82b47..03ddd75881 100644
--- a/lisp/tab-bar.el
+++ b/lisp/tab-bar.el
@@ -134,21 +134,30 @@ Possible modifier keys are `control', `meta', `shift', `hyper', `super' and
                                           :ascent center))
                          tab-bar-close-button)))
 
+(defun tab-bar--update-tab-bar-lines ()
+  ;; Set frame parameters.
+  (let ((defaultval (if (and tab-bar-mode tab-bar-show) 1 0)))
+    (dolist (frame (frame-list))
+      (let ((frameval (if (natnump tab-bar-show)
+                          (if (and defaultval
+                                   (> (length (funcall tab-bar-tabs-function frame)) tab-bar-show))
+                              1 0)
+                        defaultval)))
+        (set-frame-parameter frame 'tab-bar-lines frameval)))
+    (let ((newframeval (if (and defaultval (eq tab-bar-show t)) 1 0)))
+      (setq default-frame-alist
+            (cons (cons 'tab-bar-lines newframeval)
+                  (assq-delete-all 'tab-bar-lines default-frame-alist))))))
+
 (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)))))
+
+  ;; Recalculate tab-bar-lines for all frames
+  (tab-bar--update-tab-bar-lines)
+
   (when tab-bar-mode
     (tab-bar--load-buttons))
   (if tab-bar-mode
@@ -252,15 +261,7 @@ you can use the command `toggle-frame-tab-bar'."
          (set-default sym val)
          ;; Preload button images
          (tab-bar-mode 1)
-         ;; Then handle each frame individually
-         (dolist (frame (frame-list))
-           (set-frame-parameter
-            frame 'tab-bar-lines
-            (if (or (eq val t)
-                    (and (natnump val)
-                         (> (length (funcall tab-bar-tabs-function frame))
-                            val)))
-                1 0))))
+         (tab-bar--update-tab-bar-lines))
   :group 'tab-bar
   :version "27.1")
 
@@ -852,16 +853,11 @@ After the tab is created, the hooks in
       (run-hook-with-args 'tab-bar-tab-post-open-functions
                           (nth to-index tabs)))
 
-    (cond
-     ((eq tab-bar-show t)
-      (tab-bar-mode 1))
-     ((and (natnump tab-bar-show)
-           (> (length (funcall tab-bar-tabs-function)) tab-bar-show)
-           (zerop (frame-parameter nil 'tab-bar-lines)))
-      (progn
-        (tab-bar--load-buttons)
-        (tab-bar--define-keys)
-        (set-frame-parameter nil 'tab-bar-lines 1))))
+    ;; Recalculate tab-bar-lines and update frames
+    (tab-bar--update-tab-bar-lines)
+    (when tab-bar-mode
+      (tab-bar--load-buttons)
+      (tab-bar--define-keys))
 
     (force-mode-line-update)
     (unless tab-bar-mode
@@ -996,11 +992,8 @@ for the last tab on a frame is determined by
                 tab-bar-closed-tabs)
           (set-frame-parameter nil 'tabs (delq close-tab tabs)))
 
-        (when (and (not (zerop (frame-parameter nil 'tab-bar-lines)))
-                   (natnump tab-bar-show)
-                   (<= (length (funcall tab-bar-tabs-function))
-                       tab-bar-show))
-          (set-frame-parameter nil 'tab-bar-lines 0))
+        ;; Recalculate tab-bar-lines and update frames
+        (tab-bar--update-tab-bar-lines)
 
         (force-mode-line-update)
         (unless tab-bar-mode
@@ -1036,11 +1029,8 @@ for the last tab on a frame is determined by
           (run-hook-with-args 'tab-bar-tab-pre-close-functions (nth index tabs) nil)))
       (set-frame-parameter nil 'tabs (list (nth current-index tabs)))
 
-      (when (and (not (zerop (frame-parameter nil 'tab-bar-lines)))
-                 (natnump tab-bar-show)
-                 (<= (length (funcall tab-bar-tabs-function))
-                     tab-bar-show))
-        (set-frame-parameter nil 'tab-bar-lines 0))
+      ;; Recalculate tab-bar-lines and update frames
+      (tab-bar--update-tab-bar-lines)
 
       (force-mode-line-update)
       (unless tab-bar-mode