Skip to content

Commit 3ba0db4

Browse files
committed
Allow optional truncation of tab names in tab-bar and tab-line (bug#38693)
* lisp/tab-line.el (tab-line-tab-name-truncated-max): New defcustom. (tab-line-tab-name-truncated-buffer): Use tab-line-tab-name-truncated-max consistently with similar options in tab-bar.el. (tab-line-tabs-limit): Remove variable. (tab-line-tabs-window-buffers): Remove use of tab-line-tabs-limit that was an experimental feature before horizontal scrolling was implemented. (tab-line-close-tab-function): Rename from tab-line-close-tab-action and allow a customizaed function as option. (tab-line-close-tab): Call function if tab-line-close-tab-function is customized to a function. * lisp/tab-bar.el (tab-bar-tab-name-function): Add option tab-bar-tab-name-truncated. (tab-bar-tab-name-truncated-max): New defcustom. (tab-bar-tab-name-truncated-ellipsis): New variable. (tab-bar-tab-name-truncated): New function.
1 parent 7dd065f commit 3ba0db4

File tree

2 files changed

+59
-39
lines changed

2 files changed

+59
-39
lines changed

lisp/tab-bar.el

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,8 @@ from all windows in the window configuration."
319319
tab-bar-tab-name-current)
320320
(const :tag "Selected window buffer with window count"
321321
tab-bar-tab-name-current-with-count)
322+
(const :tag "Truncated buffer name"
323+
tab-bar-tab-name-truncated)
322324
(const :tag "All window buffers"
323325
tab-bar-tab-name-all)
324326
(function :tag "Function"))
@@ -350,6 +352,29 @@ Also add the number of windows in the window configuration."
350352
'nomini)))
351353
", "))
352354

355+
(defcustom tab-bar-tab-name-truncated-max 20
356+
"Maximum length of the tab name from the current buffer.
357+
Effective when `tab-bar-tab-name-function' is customized
358+
to `tab-bar-tab-name-truncated'."
359+
:type 'integer
360+
:group 'tab-bar
361+
:version "27.1")
362+
363+
(defvar tab-bar-tab-name-truncated-ellipsis
364+
(if (char-displayable-p ?…) "" "..."))
365+
366+
(defun tab-bar-tab-name-truncated ()
367+
"Generate tab name from the buffer of the selected window.
368+
Truncate it to the length specified by `tab-bar-tab-name-truncated-max'.
369+
Append ellipsis `tab-bar-tab-name-truncated-ellipsis' in this case."
370+
(let ((tab-name (buffer-name (window-buffer (minibuffer-selected-window)))))
371+
(if (< (length tab-name) tab-bar-tab-name-truncated-max)
372+
tab-name
373+
(propertize (truncate-string-to-width
374+
tab-name tab-bar-tab-name-truncated-max nil nil
375+
tab-bar-tab-name-truncated-ellipsis)
376+
'help-echo tab-name))))
377+
353378

354379
(defvar tab-bar-tabs-function #'tab-bar-tabs
355380
"Function to get a list of tabs to display in the tab bar.

lisp/tab-line.el

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,6 @@ If nil, don't show it at all."
213213

214214
(defvar tab-line-separator nil)
215215

216-
(defvar tab-line-tab-name-ellipsis
217-
(if (char-displayable-p ?…) "" "..."))
218-
219216

220217
(defcustom tab-line-tab-name-function #'tab-line-tab-name-buffer
221218
"Function to get a tab name.
@@ -240,23 +237,30 @@ This function can be overridden by changing the default value of the
240237
variable `tab-line-tab-name-function'."
241238
(buffer-name buffer))
242239

243-
(defun tab-line-tab-name-truncated-buffer (buffer &optional buffers)
240+
(defcustom tab-line-tab-name-truncated-max 20
241+
"Maximum length of the tab name from the current buffer.
242+
Effective when `tab-line-tab-name-function' is customized
243+
to `tab-line-tab-name-truncated-buffer'."
244+
:type 'integer
245+
:group 'tab-line
246+
:version "27.1")
247+
248+
(defvar tab-line-tab-name-ellipsis
249+
(if (char-displayable-p ?…) "" "..."))
250+
251+
(defun tab-line-tab-name-truncated-buffer (buffer &optional _buffers)
244252
"Generate tab name from BUFFER.
245-
Reduce tab width proportionally to space taken by other tabs."
246-
(let ((tab-name (buffer-name buffer))
247-
(limit (when buffers
248-
(max 1 (- (/ (window-width) (length buffers)) 3)))))
249-
(if (or (not limit) (< (length tab-name) limit))
253+
Truncate it to the length specified by `tab-line-tab-name-truncated-max'.
254+
Append ellipsis `tab-line-tab-name-ellipsis' in this case."
255+
(let ((tab-name (buffer-name buffer)))
256+
(if (< (length tab-name) tab-line-tab-name-truncated-max)
250257
tab-name
251-
(propertize (truncate-string-to-width tab-name limit nil nil
252-
tab-line-tab-name-ellipsis)
258+
(propertize (truncate-string-to-width
259+
tab-name tab-line-tab-name-truncated-max nil nil
260+
tab-line-tab-name-ellipsis)
253261
'help-echo tab-name))))
254262

255263

256-
(defvar tab-line-tabs-limit nil
257-
"Maximum number of buffer tabs displayed in the tab line.
258-
If nil, no limit.")
259-
260264
(defcustom tab-line-tabs-function #'tab-line-tabs-window-buffers
261265
"Function to get a list of tabs to display in the tab line.
262266
This function should return either a list of buffers whose names will
@@ -395,22 +399,9 @@ variable `tab-line-tabs-function'."
395399
(prev-buffers (seq-filter #'buffer-live-p prev-buffers))
396400
;; Remove next-buffers from prev-buffers
397401
(prev-buffers (seq-difference prev-buffers next-buffers)))
398-
(if (natnump tab-line-tabs-limit)
399-
(let* ((half-limit (/ tab-line-tabs-limit 2))
400-
(prev-buffers-limit
401-
(if (> (length prev-buffers) half-limit)
402-
(if (> (length next-buffers) half-limit)
403-
half-limit
404-
(+ half-limit (- half-limit (length next-buffers))))
405-
(length prev-buffers)))
406-
(next-buffers-limit
407-
(- tab-line-tabs-limit prev-buffers-limit)))
408-
(append (reverse (seq-take prev-buffers prev-buffers-limit))
409-
(list buffer)
410-
(seq-take next-buffers next-buffers-limit)))
411-
(append (reverse prev-buffers)
412-
(list buffer)
413-
next-buffers))))
402+
(append (reverse prev-buffers)
403+
(list buffer)
404+
next-buffers)))
414405

415406

416407
(defun tab-line-format-template (tabs)
@@ -681,15 +672,17 @@ Its effect is the same as using the `next-buffer' command
681672
(switch-to-buffer buffer)))))))
682673

683674

684-
(defcustom tab-line-close-tab-action 'bury-buffer
675+
(defcustom tab-line-close-tab-function 'bury-buffer
685676
"Defines what to do on closing the tab.
686677
If `bury-buffer', put the tab's buffer at the end of the list of all
687678
buffers that effectively hides the buffer's tab from the tab line.
688679
If `kill-buffer', kills the tab's buffer.
680+
When a function, it is called with the tab as its argument.
689681
This option is useful when `tab-line-tabs-function' has the value
690682
`tab-line-tabs-window-buffers'."
691683
:type '(choice (const :tag "Bury buffer" bury-buffer)
692-
(const :tag "Kill buffer" kill-buffer))
684+
(const :tag "Kill buffer" kill-buffer)
685+
(function :tag "Function"))
693686
:group 'tab-line
694687
:version "27.1")
695688

@@ -703,18 +696,20 @@ from the tab line."
703696
(window (and posnp (posn-window posnp)))
704697
(tab (get-pos-property 1 'tab (car (posn-string posnp))))
705698
(buffer (if (bufferp tab) tab (cdr (assq 'buffer tab))))
706-
(close-action (unless (bufferp tab) (cdr (assq 'close tab)))))
699+
(close-function (unless (bufferp tab) (cdr (assq 'close tab)))))
707700
(with-selected-window (or window (selected-window))
708701
(cond
709-
((functionp close-action)
710-
(funcall close-action))
711-
((eq tab-line-close-tab-action 'kill-buffer)
702+
((functionp close-function)
703+
(funcall close-function))
704+
((eq tab-line-close-tab-function 'kill-buffer)
712705
(kill-buffer buffer))
713-
((eq tab-line-close-tab-action 'bury-buffer)
706+
((eq tab-line-close-tab-function 'bury-buffer)
714707
(if (eq buffer (current-buffer))
715708
(bury-buffer)
716709
(set-window-prev-buffers nil (assq-delete-all buffer (window-prev-buffers)))
717-
(set-window-next-buffers nil (delq buffer (window-next-buffers))))))
710+
(set-window-next-buffers nil (delq buffer (window-next-buffers)))))
711+
((functionp tab-line-close-tab-function)
712+
(funcall tab-line-close-tab-function)))
718713
(force-mode-line-update))))
719714

720715

0 commit comments

Comments
 (0)