Subject: bug#71883: [PATCH] Fix tab-bar-auto-width with customized tab-bar-tab-face-function

From: Joseph Turner <[email-masked]>
Date: Thu, 25 Jul 2024 11:11:01 -0700

Juri Linkov <[email-masked]> writes:

>> I'm not sure I understand why they need to be installed on emacs-30.
>> Is this a regression in Emacs 29 or Emacs 30?  What bad things will
>> happen if we install the changes on master instead>

No regression.

> Replacing hard-coded logic with customizable variable
> for external packages like activities.el is needed
> as soon as possible on emacs-30 because hard-coded logic
> hinders the use of packages.
>
> However, there is no hurry to change the default behavior
> to match a symbol name instead of checking face names.
> Therefore I think better to move the existing code
>
>   (memq (get-text-property 0 'face (nth 2 item)) tab-bar-auto-width-faces)
>
> to the new predicate function on emacs-30.  Then activities.el
> can change it to another function that matches a symbol.
>
> Then on master the default body on the new predicate
> could be replaced from checking the face to match a symbol.
> Also changes in tab-bar--format-tab-group should be on master as well.

Much as I'd like to use these changes asap, I think this patchset should
go entirely on master.  I see these changes as adding functionality
(making tab-bar tabs more extensible) rather than bug fixes.

>>> Does this change warrant a NEWS entry?
>>
>> Yes, since you are adding a hook variable.  Obsolescence of a variable
>> also requires a NEWS entry.

Please see attached patches.

Thanks!

Joseph

>From 050a574bcab371ee87b1ed6c15e6431a3c2ed4d8 Mon Sep 17 00:00:00 2001
From: Juri Linkov <[email-masked]>
Date: Mon, 15 Jul 2024 21:23:39 -0700
Subject: [PATCH 1/4] Use current-group symbol for current tab group item

* lisp/tab-bar.el (tab-bar--format-tab-group):
---
 lisp/tab-bar.el | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lisp/tab-bar.el b/lisp/tab-bar.el
index edec6543a82..66fb9490ce8 100644
--- a/lisp/tab-bar.el
+++ b/lisp/tab-bar.el
@@ -1044,7 +1044,7 @@ tab-bar--format-tab-group
 when the tab is current.  Return the result as a keymap."
   (append
    `((,(intern (format "sep-%i" i)) menu-item ,(tab-bar-separator) ignore))
-   `((,(intern (format "group-%i" i))
+   `((,(intern (if current-p "current-group" (format "group-%i" i)))
       menu-item
       ,(if current-p
            (condition-case nil
-- 
2.41.0

>From f85b67595b91f90fdd96231d18b73edcf96c30be Mon Sep 17 00:00:00 2001
From: Joseph Turner <[email-masked]>
Date: Mon, 15 Jul 2024 21:55:35 -0700
Subject: [PATCH 2/4] Add abnormal hook to determine which tabs to auto-widen

* lisp/tab-bar.el (tab-bar-auto-width-predicate-default):  Default value
for tab-bar-auto-width-functions.
(tab-bar-auto-width-functions): New abnormal hook.
(tab-bar-auto-width): Run new abnormal hook until success instead of
comparing text properties.
---
 lisp/tab-bar.el | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/lisp/tab-bar.el b/lisp/tab-bar.el
index 66fb9490ce8..9ad59339aa1 100644
--- a/lisp/tab-bar.el
+++ b/lisp/tab-bar.el
@@ -1222,6 +1222,18 @@ tab-bar-auto-width-faces
      tab-bar-tab-group-inactive)
   "Resize tabs only with these faces.")
 
+(defun tab-bar-auto-width-predicate-default (item)
+  "Accepts tab ITEM and returns non-nil for tabs and tab groups."
+  (string-match-p
+   ;; (rx bos (or "current-tab" "current-group" "tab-" "group-"))
+   "\\`\\(?:current-\\(?:group\\|tab\\)\\|\\(?:group\\|tab\\)-\\)"
+   (symbol-name (nth 0 item))))
+
+(defvar tab-bar-auto-width-functions '(tab-bar-auto-width-predicate-default)
+  "List of functions for `tab-bar-auto-width' to call with a tab ITEM.
+If any of these functions returns non-nil for a given tab ITEM, that
+tab's width will be auto-sized.")
+
 (defvar tab-bar--auto-width-hash nil
   "Memoization table for `tab-bar-auto-width'.")
 
@@ -1250,8 +1262,7 @@ tab-bar-auto-width
         (width 0))    ;; resize tab names to this width
     (dolist (item items)
       (when (and (eq (nth 1 item) 'menu-item) (stringp (nth 2 item)))
-        (if (memq (get-text-property 0 'face (nth 2 item))
-                  tab-bar-auto-width-faces)
+        (if (run-hook-with-args-until-success 'tab-bar-auto-width-functions item)
             (push item tabs)
           (unless (eq (nth 0 item) 'align-right)
             (setq non-tabs (concat non-tabs (nth 2 item)))))))
-- 
2.41.0

>From f4419f46ab96476537fe377baf5950bb8ef22b83 Mon Sep 17 00:00:00 2001
From: Joseph Turner <[email-masked]>
Date: Mon, 15 Jul 2024 22:05:40 -0700
Subject: [PATCH 3/4] Mark tab-bar-auto-width-faces obsolete

* lisp/tab-bar.el: (tab-bar-auto-width-faces) Obsolete on >=30.
---
 lisp/tab-bar.el | 1 +
 1 file changed, 1 insertion(+)

diff --git a/lisp/tab-bar.el b/lisp/tab-bar.el
index 9ad59339aa1..503df82539d 100644
--- a/lisp/tab-bar.el
+++ b/lisp/tab-bar.el
@@ -1216,6 +1216,7 @@ tab-bar-auto-width-min
 It's not recommended to change this value since with larger values, the
 tab bar might wrap to the second line when it shouldn't.")
 
+(make-obsolete-variable 'tab-bar-auto-width-faces 'tab-bar-auto-width-functions "30")
 (defvar tab-bar-auto-width-faces
   '( tab-bar-tab tab-bar-tab-inactive
      tab-bar-tab-ungrouped
-- 
2.41.0

>From 1d677b03e1ebb38b8287adf1a6cc4fe5ab4712b2 Mon Sep 17 00:00:00 2001
From: Joseph Turner <[email-masked]>
Date: Thu, 25 Jul 2024 10:58:38 -0700
Subject: [PATCH 4/4] ; * etc/NEWS: Announce tab-bar-auto-width-functions

---
 etc/NEWS | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/etc/NEWS b/etc/NEWS
index 5c08bc7550f..300b79c50b2 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -30,6 +30,14 @@ applies, and please also update docstrings as needed.
 
 * Changes in Emacs 31.1
 
+** Tab Bars and Tab Lines
+
+*** New abnormal hook 'tab-bar-auto-width-functions'.  This hook
+allows you to control which tab-bar tabs are automatically resized.
+
+*** The 'tab-bar-auto-width-faces' variable is now obsolete.
+Use 'tab-bar-auto-width-functions' instead.
+
 
 * Editing Changes in Emacs 31.1
 
-- 
2.41.0