diff options
author | Joost Kremers <joostkremers@fastmail.com> | 2024-11-06 09:46:00 +0100 |
---|---|---|
committer | Eli Zaretskii <eliz@gnu.org> | 2024-11-09 12:08:18 +0200 |
commit | d94bb481ba1bcf8602fc600572e5779b25cd896d (patch) | |
tree | 6d902175fc4c9b2119d321239cf9ea9e1f18d8e6 /lisp/emacs-lisp/vtable.el | |
parent | 764db76072847d2650f5b07349723a2802ccd45e (diff) | |
download | emacs-d94bb481ba1bcf8602fc600572e5779b25cd896d.tar.gz emacs-d94bb481ba1bcf8602fc600572e5779b25cd896d.tar.bz2 emacs-d94bb481ba1bcf8602fc600572e5779b25cd896d.zip |
Allow empty vtable
* lisp/emacs-lisp/vtable.el (vtable--compute-widths): Set default
width for columns that have no explicit width and no data.
(Bug#73775)
Diffstat (limited to 'lisp/emacs-lisp/vtable.el')
-rw-r--r-- | lisp/emacs-lisp/vtable.el | 68 |
1 files changed, 42 insertions, 26 deletions
diff --git a/lisp/emacs-lisp/vtable.el b/lisp/emacs-lisp/vtable.el index 925961f012c..043b9ca55c4 100644 --- a/lisp/emacs-lisp/vtable.el +++ b/lisp/emacs-lisp/vtable.el @@ -850,32 +850,48 @@ If NEXT, do the next column." (error "Invalid spec: %s" spec)))) (defun vtable--compute-widths (table cache) - "Compute the display widths for TABLE." - (seq-into - (seq-map-indexed - (lambda (column index) - (let ((width - (or - ;; Explicit widths. - (and (vtable-column-width column) - (vtable--compute-width table (vtable-column-width column))) - ;; Compute based on the displayed widths of - ;; the data. - (seq-max (seq-map (lambda (elem) - (nth 1 (elt (cdr elem) index))) - cache))))) - ;; Let min-width/max-width specs have their say. - (when-let* ((min-width (and (vtable-column-min-width column) - (vtable--compute-width - table (vtable-column-min-width column))))) - (setq width (max width min-width))) - (when-let* ((max-width (and (vtable-column-max-width column) - (vtable--compute-width - table (vtable-column-max-width column))))) - (setq width (min width max-width))) - width)) - (vtable-columns table)) - 'vector)) + "Compute the display widths for TABLE. +CACHE is TABLE's cache data as returned by `vtable--compute-cache'." + (let* ((n-0cols 0) ; Count the number of zero-width columns. + (widths (seq-map-indexed + (lambda (column index) + (let ((width + (or + ;; Explicit widths. + (and (vtable-column-width column) + (vtable--compute-width table (vtable-column-width column))) + ;; If the vtable is empty and no explicit width is given, + ;; set its width to 0 and deal with it below. + (when (null cache) + (setq n-0cols (1+ n-0cols)) + 0) + ;; Otherwise, compute based on the displayed widths of the + ;; data. + (seq-max (seq-map (lambda (elem) + (nth 1 (elt (cdr elem) index))) + cache))))) + ;; Let min-width/max-width specs have their say. + (when-let* ((min-width (and (vtable-column-min-width column) + (vtable--compute-width + table (vtable-column-min-width column))))) + (setq width (max width min-width))) + (when-let* ((max-width (and (vtable-column-max-width column) + (vtable--compute-width + table (vtable-column-max-width column))))) + (setq width (min width max-width))) + width)) + (vtable-columns table)))) + ;; If there are any zero-width columns, divide the remaining window + ;; width evenly over them. + (when (> n-0cols 0) + (let* ((combined-width (apply #'+ widths)) + (default-width (/ (- (window-width nil t) combined-width) n-0cols))) + (setq widths (mapcar (lambda (width) + (if (zerop width) + default-width + width)) + widths)))) + (seq-into widths 'vector))) (defun vtable--compute-cache (table) (seq-map |