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

From: Bastian Beranek <[email-masked]>
Date: Tue, 9 Feb 2021 12:44:47 +0100

On Tue, Feb 9, 2021 at 10:45 AM martin rudalics <[email-masked]> wrote:
>
>  > +  "Determine the value of tab-bar-lines for FRAME.
>  > +The returned value will either be 1 or 0, meaning whether to show
>  > +the tab-bar or not. If tab-bar-mode is off, the returned value is
>  > +0. Otherwise the result depends on the value of the customizable
>  > +variable `tab-bar-show'."
>
> Please use active voice and always make sure to leave two spaces in
> front of a new sentence.  I would write it as
>
> (defun tab-bar--tab-bar-lines-for-frame (frame)
>    "Return new value of `tab-bar-lines' parameter for specified FRAME.
> Return 0 if `tab-bar-mode' is not enabled on FRAME or ....
> Return 1 if `tab-bar-mode' is enabled on FRAME and the variable
> `tab-bar-show' ...
>
> Call this function when ..."
>
> where the last sentence would describe when and why this function should
> be called, justifying the "new" in the doc-string.  Instead of "new" you
> can also use "adjusted" if the value gets just adjusted or your earlier
> "correct" provided an earlier calculation was "incorrect".

Thanks for your comments. I went with

(defun tab-bar--tab-bar-lines-for-frame (frame)
  "Determine and return the value of `tab-bar-lines' for FRAME.
Return 0 if `tab-bar-mode' is not enabled.  Otherwise return
either 1 or 0 depending on the value of the customizable variable
`tab-bar-show', which see."

Please see the attached v5 of the complete patch.

I don't think tab-bar-mode can vary from frame to frame? I also did
not add the "Call this function" part, because this is an internal
helper function that is only used from
"tab-bar--update-tab-bar-lines", just below. It just returns 0 or 1
and does not modify anything, the actual frame parameter modification
happens in the other function.

Hope this is satisfactory, if not please feel free to adjust as you wish.

Do I need to do the contribution paperworks for this patch? So far I
haven't done that.

Best
Bastian
commit c642812d035dddb0dce0ed8c8ba895dd3559bf4c
Author: Bastian Beranek <[email-masked]>
Date:   Mon Feb 8 18:12:33 2021 +0100

    Fix issues with tab bar show.

diff --git a/lisp/tab-bar.el b/lisp/tab-bar.el
index 6720d82b47..37ca6dcff4 100644
--- a/lisp/tab-bar.el
+++ b/lisp/tab-bar.el
@@ -134,21 +134,44 @@ Possible modifier keys are `control', `meta', `shift', `hyper', `super' and
                                           :ascent center))
                          tab-bar-close-button)))
 
+(defun tab-bar--tab-bar-lines-for-frame (frame)
+  "Determine and return the value of `tab-bar-lines' for FRAME.
+Return 0 if `tab-bar-mode' is not enabled.  Otherwise return
+either 1 or 0 depending on the value of the customizable variable
+`tab-bar-show', which see."
+  (if (not tab-bar-mode)
+      0
+    (cond
+     ((eq tab-bar-show t) 1)
+     ((natnump tab-bar-show)
+      (if (> (length (funcall tab-bar-tabs-function frame)) tab-bar-show) 1 0))
+     (t 0))))
+
+(defun tab-bar--update-tab-bar-lines (&optional frame)
+  "Update the `tab-bar-lines' parameter in frames.
+When the optional parameter FRAME is omitted all frames as well
+as the default for new frames are updated.  Otherwise only FRAME
+is modified."
+  (if frame
+      ;; Set frame parameters for the given frame
+      (set-frame-parameter frame 'tab-bar-lines (tab-bar--tab-bar-lines-for-frame frame))
+    (progn
+      ;; Loop over all frames and update default-frame-alist
+      (dolist (frame (frame-list))
+        (set-frame-parameter frame 'tab-bar-lines (tab-bar--tab-bar-lines-for-frame frame)))
+      (setq default-frame-alist
+            (cons (cons 'tab-bar-lines (if (and tab-bar-mode (eq tab-bar-show t)) 1 0))
+                  (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
@@ -251,16 +274,8 @@ you can use the command `toggle-frame-tab-bar'."
   :set (lambda (sym val)
          (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))))
+         ;; Note: tab-bar-mode updates tab-bar-lines as well.
+         (tab-bar-mode 1))
   :group 'tab-bar
   :version "27.1")
 
@@ -852,16 +867,15 @@ 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)
+    ;; Switch on tab-bar-mode, since a tab was created
+    (when tab-bar-show
       (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 (selected-frame))
+    (when tab-bar-mode
+      (tab-bar--load-buttons)
+      (tab-bar--define-keys))
 
     (force-mode-line-update)
     (unless tab-bar-mode
@@ -996,11 +1010,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 (selected-frame))
 
         (force-mode-line-update)
         (unless tab-bar-mode
@@ -1036,11 +1047,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 (selected-frame))
 
       (force-mode-line-update)
       (unless tab-bar-mode