From 44b31c1ed7f9c6668942ea122c0dd37b825ef29c Mon Sep 17 00:00:00 2001 From: Lars Ingebrigtsen Date: Sat, 8 Aug 2020 11:37:43 +0200 Subject: [PATCH] Make the name column in 'list-buffers' have a dynamic width * lisp/buff-menu.el (Buffer-menu--dynamic-name-width): New function (bug#30692). (Buffer-menu-name-width): Default to using it. (list-buffers--refresh): Call it. * lisp/emacs-lisp/seq.el (seq-max): Add autoload cookie. --- etc/NEWS | 8 ++++++ lisp/buff-menu.el | 57 +++++++++++++++++++++++++++--------------- lisp/emacs-lisp/seq.el | 1 + 3 files changed, 46 insertions(+), 20 deletions(-) diff --git a/etc/NEWS b/etc/NEWS index 850b1660690..de10b4a6131 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -130,6 +130,14 @@ same for a button. * Changes in Specialized Modes and Packages in Emacs 28.1 +** Miscellaneous + +--- +*** The width of the buffer-name column in 'list-buffers' is now dynamic. +The width now depends of the width of the window, but will never be +wider than the length of the longest buffer name, except that it will +never be narrower than 19 characters. + ** Windows *** The key prefix 'C-x 4 1' displays next command buffer in the same window. diff --git a/lisp/buff-menu.el b/lisp/buff-menu.el index 9fe0dbae381..359d6381e8b 100644 --- a/lisp/buff-menu.el +++ b/lisp/buff-menu.el @@ -69,11 +69,26 @@ minus `Buffer-menu-size-width'. This use is deprecated." "use `Buffer-menu-name-width' and `Buffer-menu-size-width' instead." "24.3") -(defcustom Buffer-menu-name-width 19 - "Width of buffer name column in the Buffer Menu." - :type 'number +(defun Buffer-menu--dynamic-name-width (buffers) + "Return a name column width based on the current window width. +The width will never exceed the actual width of the buffer names, +but will never be narrower than 19 characters." + (max 19 + ;; This gives 19 on an 80 column window, and take up + ;; proportionally more space as the window widens. + (min (truncate (/ (window-width) 4.2)) + (seq-max (mapcar (lambda (b) + (length (buffer-name b))) + buffers))))) + +(defcustom Buffer-menu-name-width #'Buffer-menu--dynamic-name-width + "Width of buffer name column in the Buffer Menu. +This can either be a number (used directly) or a function that +will be called with the list of buffers and should return a +number." + :type '(choice function number) :group 'Buffer-menu - :version "24.3") + :version "28.1") (defcustom Buffer-menu-size-width 7 "Width of buffer size column in the Buffer Menu." @@ -646,25 +661,11 @@ means list those buffers and no others." (defun list-buffers--refresh (&optional buffer-list old-buffer) ;; Set up `tabulated-list-format'. - (let ((name-width Buffer-menu-name-width) - (size-width Buffer-menu-size-width) + (let ((size-width Buffer-menu-size-width) (marked-buffers (Buffer-menu-marked-buffers)) (buffer-menu-buffer (current-buffer)) (show-non-file (not Buffer-menu-files-only)) - entries) - ;; Handle obsolete variable: - (if Buffer-menu-buffer+size-width - (setq name-width (- Buffer-menu-buffer+size-width size-width))) - (setq tabulated-list-format - (vector '("C" 1 t :pad-right 0) - '("R" 1 t :pad-right 0) - '("M" 1 t) - `("Buffer" ,name-width t) - `("Size" ,size-width tabulated-list-entry-size-> - :right-align t) - `("Mode" ,Buffer-menu-mode-width t) - '("File" 1 t))) - (setq tabulated-list-use-header-line Buffer-menu-use-header-line) + entries name-width) ;; Collect info for each buffer we're interested in. (dolist (buffer (or buffer-list (buffer-list (if Buffer-menu-use-frame-buffer-list @@ -694,6 +695,22 @@ means list those buffers and no others." nil nil buffer))) (Buffer-menu--pretty-file-name file))) entries))))) + (setq name-width (if (functionp Buffer-menu-name-width) + (funcall Buffer-menu-name-width (mapcar #'car entries)) + Buffer-menu-name-width)) + ;; Handle obsolete variable: + (if Buffer-menu-buffer+size-width + (setq name-width (- Buffer-menu-buffer+size-width size-width))) + (setq tabulated-list-format + (vector '("C" 1 t :pad-right 0) + '("R" 1 t :pad-right 0) + '("M" 1 t) + `("Buffer" ,name-width t) + `("Size" ,size-width tabulated-list-entry-size-> + :right-align t) + `("Mode" ,Buffer-menu-mode-width t) + '("File" 1 t))) + (setq tabulated-list-use-header-line Buffer-menu-use-header-line) (setq tabulated-list-entries (nreverse entries))) (tabulated-list-init-header)) diff --git a/lisp/emacs-lisp/seq.el b/lisp/emacs-lisp/seq.el index 4c1a1797adc..1cc68e19edd 100644 --- a/lisp/emacs-lisp/seq.el +++ b/lisp/emacs-lisp/seq.el @@ -492,6 +492,7 @@ keys. Keys are compared using `equal'." SEQUENCE must be a sequence of numbers or markers." (apply #'min (seq-into sequence 'list))) +;;;###autoload (cl-defgeneric seq-max (sequence) "Return the largest element of SEQUENCE. SEQUENCE must be a sequence of numbers or markers." -- 2.39.2