]> git.eshelyaron.com Git - emacs.git/commitdiff
Fix tab-bar-format-align-right to not call tab-bar-format-global twice
authorJuri Linkov <juri@linkov.net>
Wed, 9 Jul 2025 06:28:05 +0000 (09:28 +0300)
committerEshel Yaron <me@eshelyaron.com>
Thu, 24 Jul 2025 07:52:03 +0000 (09:52 +0200)
* lisp/tab-bar.el (tab-bar-format-align-right): Add optional arg 'rest'
to handle it specially in 'tab-bar-format-list'.  Use the value from 'rest'
instead of calling format functions twice by 'tab-bar-format-list'.
(tab-bar-format-list): When 'format-list' contains the item
'tab-bar-format-align-right', collect the rest of formatted items
and call 'tab-bar-format-align-right' explicitly with the collected
list (bug#78953).

(cherry picked from commit 8b1978fa6e929101b6a766cfbb94c3b27b142fa7)

lisp/tab-bar.el

index b498e9d1b08fc657f6f4a4bfb62a9d3996b7dd2d..7ffb07e6cd5a952aa0a192df415e5c2bff2ec26c 100644 (file)
@@ -1194,10 +1194,15 @@ when the tab is current.  Return the result as a keymap."
     `((add-tab menu-item ,tab-bar-new-button tab-bar-new-tab
                :help "New tab"))))
 
-(defun tab-bar-format-align-right ()
-  "Align the rest of tab bar items to the right."
-  (let* ((rest (cdr (memq 'tab-bar-format-align-right tab-bar-format)))
-         (rest (tab-bar-format-list rest))
+(defun tab-bar-format-align-right (&optional rest)
+  "Align the rest of tab bar items to the right.
+The argument `rest' is used for special handling of this item
+by `tab-bar-format-list' that collects the rest of formatted items.
+This prevents calling other non-idempotent items like
+`tab-bar-format-global' twice."
+  (let* ((rest (or rest (tab-bar-format-list
+                         (cdr (memq 'tab-bar-format-align-right
+                                    tab-bar-format)))))
          (rest (mapconcat (lambda (item) (nth 2 item)) rest ""))
          (hpos (progn
                  (add-face-text-property 0 (length rest) 'tab-bar t rest)
@@ -1223,19 +1228,33 @@ on the tab bar instead."
           global-mode-string))
 
 (defun tab-bar-format-list (format-list)
-  (let ((i 0))
-    (apply #'append
-           (mapcar
-            (lambda (format)
-              (setq i (1+ i))
-              (cond
-               ((functionp format)
-                (let ((ret (funcall format)))
-                  (when (stringp ret)
-                    (setq ret `((,(intern (format "str-%i" i))
-                                 menu-item ,ret ignore))))
-                  ret))))
-            format-list))))
+  "Return a list of items formatted from `format-list'.
+The item `tab-bar-format-align-right' has special formatting."
+  (let* ((i 0) align-right-p rest
+         (res (apply #'append
+                     (mapcar
+                      (lambda (format)
+                        (setq i (1+ i))
+                        (cond
+                         ((eq format 'tab-bar-format-align-right)
+                          (setq align-right-p t)
+                          (list format))
+                         ((functionp format)
+                          (let ((ret (funcall format)))
+                            (when (stringp ret)
+                              (setq ret `((,(intern (format "str-%i" i))
+                                           menu-item ,ret ignore))))
+                            (when align-right-p
+                              (setq rest (append rest ret)))
+                            ret))))
+                      format-list))))
+    (when align-right-p
+      (setq res (mapcan (lambda (format)
+                          (if (eq format 'tab-bar-format-align-right)
+                              (tab-bar-format-align-right rest)
+                            (list format)))
+                        res)))
+    res))
 
 (defun tab-bar-make-keymap-1 ()
   "Generate an actual keymap from `tab-bar-map', without caching."