summaryrefslogtreecommitdiff
path: root/lisp/emacs-lisp
diff options
context:
space:
mode:
authorJoost Kremers <joostkremers@fastmail.com>2024-05-31 01:38:54 +0200
committerEli Zaretskii <eliz@gnu.org>2024-11-09 12:09:49 +0200
commit35e1ab970c1cfc6a1b62fbb920e0d2bb031765da (patch)
tree58c44f479a76880d6bdea5a19a5c833b7e892f76 /lisp/emacs-lisp
parent425f244738e784fbf46e3fcc3461759340cfb928 (diff)
downloademacs-35e1ab970c1cfc6a1b62fbb920e0d2bb031765da.tar.gz
emacs-35e1ab970c1cfc6a1b62fbb920e0d2bb031765da.tar.bz2
emacs-35e1ab970c1cfc6a1b62fbb920e0d2bb031765da.zip
vtable: allow resetting column alignment when table data changes
* lisp/emacs-lisp/vtable.el (vtable--compute-columns): If a column was not created with an explicit 'align' property, allow changing this property when the column data changes from numeric to non-numeric (or vice versa). This makes it possible to add data to an empty table, because in a table without data all columns are assumed to be numeric and right-aligned. (Bug#73775)
Diffstat (limited to 'lisp/emacs-lisp')
-rw-r--r--lisp/emacs-lisp/vtable.el24
1 files changed, 20 insertions, 4 deletions
diff --git a/lisp/emacs-lisp/vtable.el b/lisp/emacs-lisp/vtable.el
index cd874e7c2c1..a839f3d70b8 100644
--- a/lisp/emacs-lisp/vtable.el
+++ b/lisp/emacs-lisp/vtable.el
@@ -45,7 +45,8 @@
getter
formatter
displayer
- -numerical)
+ -numerical
+ -aligned)
(defclass vtable ()
((columns :initarg :columns :accessor vtable-columns)
@@ -473,7 +474,17 @@ This also updates the displayed table."
(t
(elt object index))))
-(defun vtable--compute-columns (table)
+(defun vtable--compute-columns (table &optional recompute)
+ "Compute column specs for TABLE.
+Set the `align', `-aligned' and `-numerical' properties of each column.
+If the column contains only numerical data, set `-numerical' to t,
+otherwise to nil. `-aligned' indicates whether the column has an
+`align' property set by the user. If it does, `align' is not touched,
+otherwise it is set to `right' for numeric columns and to `left' for
+non-numeric columns.
+
+If RECOMPUTE is non-nil, do not set `-aligned'. This can be used to
+recompute the column specs when the table data has changed."
(let ((numerical (make-vector (length (vtable-columns table)) t))
(columns (vtable-columns table)))
;; First determine whether there are any all-numerical columns.
@@ -484,11 +495,16 @@ This also updates the displayed table."
table))
(setf (elt numerical index) nil)))
(vtable-columns table)))
+ ;; Check if any columns have an explicit `align' property.
+ (unless recompute
+ (dolist (column (vtable-columns table))
+ (when (vtable-column-align column)
+ (setf (vtable-column--aligned column) t))))
;; Then fill in defaults.
(seq-map-indexed
(lambda (column index)
;; This is used when displaying.
- (unless (vtable-column-align column)
+ (unless (vtable-column--aligned column)
(setf (vtable-column-align column)
(if (elt numerical index)
'right
@@ -813,7 +829,7 @@ If NEXT, do the next column."
(setq recompute t)))
line)
(when recompute
- (vtable--compute-columns table))))
+ (vtable--compute-columns table t))))
(defun vtable--set-header-line (table widths spacer)
(setq header-line-format