From 3ba0db41e3fcfdc47368e9e6fd7cbe45230ba88b Mon Sep 17 00:00:00 2001 From: Juri Linkov Date: Tue, 21 Jan 2020 01:03:37 +0200 Subject: [PATCH] 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. --- lisp/tab-bar.el | 25 +++++++++++++++++ lisp/tab-line.el | 73 ++++++++++++++++++++++-------------------------- 2 files changed, 59 insertions(+), 39 deletions(-) diff --git a/lisp/tab-bar.el b/lisp/tab-bar.el index e4ff7325844..f70fb6baeee 100644 --- a/lisp/tab-bar.el +++ b/lisp/tab-bar.el @@ -319,6 +319,8 @@ from all windows in the window configuration." tab-bar-tab-name-current) (const :tag "Selected window buffer with window count" tab-bar-tab-name-current-with-count) + (const :tag "Truncated buffer name" + tab-bar-tab-name-truncated) (const :tag "All window buffers" tab-bar-tab-name-all) (function :tag "Function")) @@ -350,6 +352,29 @@ Also add the number of windows in the window configuration." 'nomini))) ", ")) +(defcustom tab-bar-tab-name-truncated-max 20 + "Maximum length of the tab name from the current buffer. +Effective when `tab-bar-tab-name-function' is customized +to `tab-bar-tab-name-truncated'." + :type 'integer + :group 'tab-bar + :version "27.1") + +(defvar tab-bar-tab-name-truncated-ellipsis + (if (char-displayable-p ?…) "…" "...")) + +(defun tab-bar-tab-name-truncated () + "Generate tab name from the buffer of the selected window. +Truncate it to the length specified by `tab-bar-tab-name-truncated-max'. +Append ellipsis `tab-bar-tab-name-truncated-ellipsis' in this case." + (let ((tab-name (buffer-name (window-buffer (minibuffer-selected-window))))) + (if (< (length tab-name) tab-bar-tab-name-truncated-max) + tab-name + (propertize (truncate-string-to-width + tab-name tab-bar-tab-name-truncated-max nil nil + tab-bar-tab-name-truncated-ellipsis) + 'help-echo tab-name)))) + (defvar tab-bar-tabs-function #'tab-bar-tabs "Function to get a list of tabs to display in the tab bar. diff --git a/lisp/tab-line.el b/lisp/tab-line.el index 5bbc45b0aae..ad4050fec59 100644 --- a/lisp/tab-line.el +++ b/lisp/tab-line.el @@ -213,9 +213,6 @@ If nil, don't show it at all." (defvar tab-line-separator nil) -(defvar tab-line-tab-name-ellipsis - (if (char-displayable-p ?…) "…" "...")) - (defcustom tab-line-tab-name-function #'tab-line-tab-name-buffer "Function to get a tab name. @@ -240,23 +237,30 @@ This function can be overridden by changing the default value of the variable `tab-line-tab-name-function'." (buffer-name buffer)) -(defun tab-line-tab-name-truncated-buffer (buffer &optional buffers) +(defcustom tab-line-tab-name-truncated-max 20 + "Maximum length of the tab name from the current buffer. +Effective when `tab-line-tab-name-function' is customized +to `tab-line-tab-name-truncated-buffer'." + :type 'integer + :group 'tab-line + :version "27.1") + +(defvar tab-line-tab-name-ellipsis + (if (char-displayable-p ?…) "…" "...")) + +(defun tab-line-tab-name-truncated-buffer (buffer &optional _buffers) "Generate tab name from BUFFER. -Reduce tab width proportionally to space taken by other tabs." - (let ((tab-name (buffer-name buffer)) - (limit (when buffers - (max 1 (- (/ (window-width) (length buffers)) 3))))) - (if (or (not limit) (< (length tab-name) limit)) +Truncate it to the length specified by `tab-line-tab-name-truncated-max'. +Append ellipsis `tab-line-tab-name-ellipsis' in this case." + (let ((tab-name (buffer-name buffer))) + (if (< (length tab-name) tab-line-tab-name-truncated-max) tab-name - (propertize (truncate-string-to-width tab-name limit nil nil - tab-line-tab-name-ellipsis) + (propertize (truncate-string-to-width + tab-name tab-line-tab-name-truncated-max nil nil + tab-line-tab-name-ellipsis) 'help-echo tab-name)))) -(defvar tab-line-tabs-limit nil - "Maximum number of buffer tabs displayed in the tab line. -If nil, no limit.") - (defcustom tab-line-tabs-function #'tab-line-tabs-window-buffers "Function to get a list of tabs to display in the tab line. This function should return either a list of buffers whose names will @@ -395,22 +399,9 @@ variable `tab-line-tabs-function'." (prev-buffers (seq-filter #'buffer-live-p prev-buffers)) ;; Remove next-buffers from prev-buffers (prev-buffers (seq-difference prev-buffers next-buffers))) - (if (natnump tab-line-tabs-limit) - (let* ((half-limit (/ tab-line-tabs-limit 2)) - (prev-buffers-limit - (if (> (length prev-buffers) half-limit) - (if (> (length next-buffers) half-limit) - half-limit - (+ half-limit (- half-limit (length next-buffers)))) - (length prev-buffers))) - (next-buffers-limit - (- tab-line-tabs-limit prev-buffers-limit))) - (append (reverse (seq-take prev-buffers prev-buffers-limit)) - (list buffer) - (seq-take next-buffers next-buffers-limit))) - (append (reverse prev-buffers) - (list buffer) - next-buffers)))) + (append (reverse prev-buffers) + (list buffer) + next-buffers))) (defun tab-line-format-template (tabs) @@ -681,15 +672,17 @@ Its effect is the same as using the `next-buffer' command (switch-to-buffer buffer))))))) -(defcustom tab-line-close-tab-action 'bury-buffer +(defcustom tab-line-close-tab-function 'bury-buffer "Defines what to do on closing the tab. If `bury-buffer', put the tab's buffer at the end of the list of all buffers that effectively hides the buffer's tab from the tab line. If `kill-buffer', kills the tab's buffer. +When a function, it is called with the tab as its argument. This option is useful when `tab-line-tabs-function' has the value `tab-line-tabs-window-buffers'." :type '(choice (const :tag "Bury buffer" bury-buffer) - (const :tag "Kill buffer" kill-buffer)) + (const :tag "Kill buffer" kill-buffer) + (function :tag "Function")) :group 'tab-line :version "27.1") @@ -703,18 +696,20 @@ from the tab line." (window (and posnp (posn-window posnp))) (tab (get-pos-property 1 'tab (car (posn-string posnp)))) (buffer (if (bufferp tab) tab (cdr (assq 'buffer tab)))) - (close-action (unless (bufferp tab) (cdr (assq 'close tab))))) + (close-function (unless (bufferp tab) (cdr (assq 'close tab))))) (with-selected-window (or window (selected-window)) (cond - ((functionp close-action) - (funcall close-action)) - ((eq tab-line-close-tab-action 'kill-buffer) + ((functionp close-function) + (funcall close-function)) + ((eq tab-line-close-tab-function 'kill-buffer) (kill-buffer buffer)) - ((eq tab-line-close-tab-action 'bury-buffer) + ((eq tab-line-close-tab-function 'bury-buffer) (if (eq buffer (current-buffer)) (bury-buffer) (set-window-prev-buffers nil (assq-delete-all buffer (window-prev-buffers))) - (set-window-next-buffers nil (delq buffer (window-next-buffers)))))) + (set-window-next-buffers nil (delq buffer (window-next-buffers))))) + ((functionp tab-line-close-tab-function) + (funcall tab-line-close-tab-function))) (force-mode-line-update)))) -- 2.39.2