From 3c324a0e18ba66febaed3678deb6571219579e1a Mon Sep 17 00:00:00 2001 From: Joseph Turner Date: Mon, 15 Jul 2024 21:55:35 -0700 Subject: [PATCH] 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. (cherry picked from commit 9ed761a793f0f11ffea1d2cfa9b7ff38a58d3fe1) --- etc/NEWS | 6 ++++++ lisp/tab-bar.el | 15 +++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/etc/NEWS b/etc/NEWS index 82b150e2ebc..1e4d8bb6e06 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -68,6 +68,12 @@ that the actual killing or burying of the buffer is done by the caller. With this option set, 'quit-restore-window' will delete its window more aggressively rather than switching to some other buffer in it. +** Tab Bars and Tab Lines + +--- +*** New abnormal hook 'tab-bar-auto-width-functions'. +This hook allows you to control which tab-bar tabs are auto-resized. + * Editing Changes in Emacs 31.1 diff --git a/lisp/tab-bar.el b/lisp/tab-bar.el index 29bf2a957e8..eb80e2c95fb 100644 --- a/lisp/tab-bar.el +++ b/lisp/tab-bar.el @@ -1244,6 +1244,18 @@ tab bar might wrap to the second line when it shouldn't.") 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'.") @@ -1272,8 +1284,7 @@ be scaled for display on the current frame." (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.39.2