Subject: Re: bug#79159: [PATCH] Do not display in the tab line tabs that are excluded.

From: Elijah Gabe Pérez <[email-masked]>
Date: Fri, 08 Aug 2025 13:14:16 -0600

Juri Linkov <[email-masked]> writes:

>>> 1. C-x b SPC *bar* RET
>>> 2. C-x b foo RET
>>> 3. C-x <left>
>>>
>>> It switches to the initial buffer *scratch*,
>>> skipping the excluded buffer " *bar*".
>>>
>>> Without the proposed patch the tab-line is broken,
>>> because it displays the excluded buffer,
>>> but 'C-x <left>' can't switch to it.
>>>
>>> So tab-line.el should follow the logic of window.el
>>> and omit excluded buffers as well.
>>
>> Yes, I've noticed this "bug", i have many buffers excluded, and when (for
>> some reason) they appear in my tab line (or I accidentally switch to
>> them), they are going to end up flooding my tab line, and it will be
>> hard to switch buffers via "C-x <left>|<right>".
>
> Do you mean that you customize not only 'tab-line-exclude-buffers',
> but also 'switch-to-prev-buffer-skip' and 'switch-to-prev-buffer-skip-regexp'
> to exclude the same buffers for both tab-line and "C-x <left>|<right>"?

No, i don't use 'switch-to-prev-buffer-skip' and
'switch-to-prev-buffer-skip-regexp' because they don't work in tab-line
(from what I've tested).

>>> Maybe the existing 'tab-line-tabs-buffer-list-function'
>>> should be promoted to the user option.
>>
>> I like the idea, but the problem is that
>> `tab-line-tabs-buffer-list-function' is used only for
>> `tab-line-tabs-mode-buffers' and `tab-line-tabs-buffer-groups'.
>>
>> Maybe the function should take an optional argument if it should list
>> only window buffers, so it can be used in
>> 'tab-line-tabs-fixed-window-buffers' and others.
>
> In fact it's needed only for 'tab-line-tabs-window-buffers'
> and 'tab-line-tabs-fixed-window-buffers', but not for other,
> because other such as 'tab-line-tabs-mode-buffers' and
> 'tab-line-tabs-buffer-groups' have own means to exclude buffers
> from the tab-line using 'tab-line-tabs-buffer-list-function'.
>
> So we could add a similar function e.g. 'tab-line-tabs-window-buffers-function'
> for 'tab-line-tabs-window-buffers' and 'tab-line-tabs-fixed-window-buffers'
> only.

Here is what I have done, I'm not sure of the result.
>From c9ae80e5e77f3cdb0cc4ade8197f3827cd2fb9d2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?El=C3=ADas=20Gabriel=20P=C3=A9rez?= <[email-masked]>
Date: Fri, 1 Aug 2025 20:14:59 -0600
Subject: [PATCH] tab-line: hide excluded buffers in the tab line.  (Bug#79159)

* etc/NEWS: Announce changes.
* lisp/tab-line.el (tab-line-tabs-window-buffers-filter-function):
New variable.
(tab-line-tabs-window-buffers-filter-default): New function.
(tab-line-tabs-window-buffers-function): New variable.
(tab-line-tabs-window-buffers): Move code ...
(tab-line-tabs-window-buffers-default): ... to this new function.
(tab-line-tabs-fixed-window-buffers): Rework.
---
 etc/NEWS         |  6 ++++++
 lisp/tab-line.el | 45 ++++++++++++++++++++++++++++++++++++++-------
 2 files changed, 44 insertions(+), 7 deletions(-)

diff --git a/etc/NEWS b/etc/NEWS
index e12a9dcb127..20182cdd367 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -436,6 +436,12 @@ a buffer.  The value must be a condition which is passed to
 With this user option, if non-nil (the default), the tab close button
 will change its appearance if the tab buffer has been modified.
 
+---
+*** Excluded buffers are no longer shown in tab-lines of other buffers.
+Buffers that match 'tab-line-exclude-buffers' and buffers whose
+modes are in 'tab-line-exclude-modes' will not appear in tab-lines
+of other buffers.
+
 ** Project
 
 ---
diff --git a/lisp/tab-line.el b/lisp/tab-line.el
index 51675dbbb29..1ddcc2fce6e 100644
--- a/lisp/tab-line.el
+++ b/lisp/tab-line.el
@@ -565,12 +565,30 @@ tab-line-tabs-buffer-groups
                          sorted-buffers)))
       (cons group-tab tabs))))
 
-(defun tab-line-tabs-window-buffers ()
-  "Return a list of tabs that should be displayed in the tab line.
-By default returns a list of window buffers, i.e. buffers previously
-shown in the same window where the tab line is displayed.
-This list can be overridden by changing the default value of the
-variable `tab-line-tabs-function'."
+(defvar tab-line-exclude-buffers)
+(defvar tab-line-exclude-modes)
+
+(defvar tab-line-tabs-window-buffers-filter-function
+  #'tab-line-tabs-window-buffers-filter-default
+  "Filter which buffers should be displayed in the tab line.")
+
+(defun tab-line-tabs-window-buffers-filter-default (buffers)
+  "Filter BUFFERS and return non-excluded buffers list."
+  (seq-remove
+   (lambda (b)
+     (or (memq (buffer-local-value 'major-mode b)
+               tab-line-exclude-modes)
+         (buffer-match-p tab-line-exclude-buffers b)))
+   buffers))
+
+(defvar tab-line-tabs-window-buffers-function
+  #'tab-line-tabs-window-buffers-default
+  "Function for get a list of window buffers.
+Used only for `tab-line-tabs-fixed-window-buffers'.")
+
+(defun tab-line-tabs-window-buffers-default ()
+  "Default function for `tab-line-tabs-window-buffers-function'.
+Return a list of buffers that should be displayed in the tab line."
   (let* ((window (selected-window))
          (buffer (window-buffer window))
          (next-buffers (seq-remove (lambda (b) (eq b buffer))
@@ -585,6 +603,16 @@ tab-line-tabs-window-buffers
             (list buffer)
             next-buffers)))
 
+(defun tab-line-tabs-window-buffers ()
+  "Return a list of tabs that should be displayed in the tab line.
+By default returns a list of window buffers, i.e. buffers previously
+shown in the same window where the tab line is displayed.
+This list can be overridden by changing the default value of the
+variable `tab-line-tabs-function'."
+  (funcall
+   tab-line-tabs-window-buffers-filter-function
+   (funcall tab-line-tabs-window-buffers-function)))
+
 (defun tab-line-tabs-fixed-window-buffers ()
   "Like `tab-line-tabs-window-buffers' but keep stable sorting order.
 This means that switching to a buffer previously shown in the same
@@ -598,7 +626,10 @@ tab-line-tabs-fixed-window-buffers
                               (lambda (buf idx) (puthash buf idx index-table))
                               old-buffers)
                              index-table))
-         (new-buffers (sort (tab-line-tabs-window-buffers)
+         (buffers (funcall
+                   tab-line-tabs-window-buffers-filter-function
+                   (funcall tab-line-tabs-window-buffers-function)))
+         (new-buffers (sort buffers
                             :in-place t
                             :key (lambda (buffer)
                                    (gethash buffer buffer-positions
-- 
2.50.1

-- 
- E.G via Gnus and Org.