Toggle tab-line for all buffers that share a major mode.

Overview

In Emacs sometimes, it's convenient to switch between frequently visited buffers using the mouse. Although this is use-case is quite rare, I decided to write a command that helps me to keep track of such buffers when I use the mouse or any other pointing device.

(defun my-toggle-tab-line (&optional buffer)
  (interactive)
  (let* ((buf (or buffer
				  (current-buffer)))
		 (mode (with-current-buffer buf
				 major-mode))
		 (switchp (with-current-buffer buf
					(bound-and-true-p tab-line-mode))))
	(dolist (b (buffer-list))
	  (with-current-buffer b
		(when (equal major-mode mode)
		  (if switchp
			  (progn
				(remove-hook (intern (format "%s-hook" mode))
							 'tab-line-mode)
				(tab-line-mode -1))
			(tab-line-mode 1)
			(add-hook (intern (format "%s-hook" mode))
					'tab-line-mode)) )))))

;; configure tab-line to display buffers that share a major mode
(setq tab-line-tabs-function 'tab-line-tabs-mode-buffers)

This function toggles tab-line in all buffers that share the major mode that is currently enabled. Behavior of tab-line can be customized to change the selection of buffers.

I recommend binding that function to a key combination or putting it in the menu-bar.

A note about appearance of tabs

Very few themes support tab-line out of the box. Because of that, I recommend choosing a theme that supports tab-bar, and manually customize the faces used by tab-line like that:

(custom-set-faces '(tab-line ((t :inherit 'tab-bar)))
				  '(tab-line-tab ((t :inherit tab-bar-tab)))
				  '(tab-line-tab-current ((t :inherit tab-bar-tab)))
				  '(tab-line-tab-inactive ((t :inherit tab-bar-tab-inactive)))
				  '(tab-line-highlight ((t :inherit tab-bar-tab))))

Resources

Created: 2022-02-13 Вс 18:39