aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoost Kremers2024-05-31 01:38:54 +0200
committerEli Zaretskii2024-11-09 12:09:49 +0200
commit35e1ab970c1cfc6a1b62fbb920e0d2bb031765da (patch)
tree58c44f479a76880d6bdea5a19a5c833b7e892f76
parent425f244738e784fbf46e3fcc3461759340cfb928 (diff)
downloademacs-35e1ab970c1cfc6a1b62fbb920e0d2bb031765da.tar.gz
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)
-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 @@
45 getter 45 getter
46 formatter 46 formatter
47 displayer 47 displayer
48 -numerical) 48 -numerical
49 -aligned)
49 50
50(defclass vtable () 51(defclass vtable ()
51 ((columns :initarg :columns :accessor vtable-columns) 52 ((columns :initarg :columns :accessor vtable-columns)
@@ -473,7 +474,17 @@ This also updates the displayed table."
473 (t 474 (t
474 (elt object index)))) 475 (elt object index))))
475 476
476(defun vtable--compute-columns (table) 477(defun vtable--compute-columns (table &optional recompute)
478 "Compute column specs for TABLE.
479Set the `align', `-aligned' and `-numerical' properties of each column.
480If the column contains only numerical data, set `-numerical' to t,
481otherwise to nil. `-aligned' indicates whether the column has an
482`align' property set by the user. If it does, `align' is not touched,
483otherwise it is set to `right' for numeric columns and to `left' for
484non-numeric columns.
485
486If RECOMPUTE is non-nil, do not set `-aligned'. This can be used to
487recompute the column specs when the table data has changed."
477 (let ((numerical (make-vector (length (vtable-columns table)) t)) 488 (let ((numerical (make-vector (length (vtable-columns table)) t))
478 (columns (vtable-columns table))) 489 (columns (vtable-columns table)))
479 ;; First determine whether there are any all-numerical columns. 490 ;; First determine whether there are any all-numerical columns.
@@ -484,11 +495,16 @@ This also updates the displayed table."
484 table)) 495 table))
485 (setf (elt numerical index) nil))) 496 (setf (elt numerical index) nil)))
486 (vtable-columns table))) 497 (vtable-columns table)))
498 ;; Check if any columns have an explicit `align' property.
499 (unless recompute
500 (dolist (column (vtable-columns table))
501 (when (vtable-column-align column)
502 (setf (vtable-column--aligned column) t))))
487 ;; Then fill in defaults. 503 ;; Then fill in defaults.
488 (seq-map-indexed 504 (seq-map-indexed
489 (lambda (column index) 505 (lambda (column index)
490 ;; This is used when displaying. 506 ;; This is used when displaying.
491 (unless (vtable-column-align column) 507 (unless (vtable-column--aligned column)
492 (setf (vtable-column-align column) 508 (setf (vtable-column-align column)
493 (if (elt numerical index) 509 (if (elt numerical index)
494 'right 510 'right
@@ -813,7 +829,7 @@ If NEXT, do the next column."
813 (setq recompute t))) 829 (setq recompute t)))
814 line) 830 line)
815 (when recompute 831 (when recompute
816 (vtable--compute-columns table)))) 832 (vtable--compute-columns table t))))
817 833
818(defun vtable--set-header-line (table widths spacer) 834(defun vtable--set-header-line (table widths spacer)
819 (setq header-line-format 835 (setq header-line-format