aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--doc/misc/vtable.texi10
-rw-r--r--lisp/emacs-lisp/vtable.el59
2 files changed, 51 insertions, 18 deletions
diff --git a/doc/misc/vtable.texi b/doc/misc/vtable.texi
index 5a3957758c9..296dc520a1b 100644
--- a/doc/misc/vtable.texi
+++ b/doc/misc/vtable.texi
@@ -392,16 +392,18 @@ If present, this should be a list of color names to be used as the
392background color on the rows. If there are fewer colors here than 392background color on the rows. If there are fewer colors here than
393there are rows, the rows will be repeated. The most common use 393there are rows, the rows will be repeated. The most common use
394case here is to have alternating background colors on the rows, so 394case here is to have alternating background colors on the rows, so
395this would usually be a list of two colors. 395this would usually be a list of two colors. This can also be a list
396of faces to be used.
396 397
397@item :column-colors 398@item :column-colors
398If present, this should be a list of color names to be used as the 399If present, this should be a list of color names to be used as the
399background color on the columns. If there are fewer colors here than 400background color on the columns. If there are fewer colors here than
400there are columns, the colors will be repeated. The most common use 401there are columns, the colors will be repeated. The most common use
401case here is to have alternating background colors on the columns, so 402case here is to have alternating background colors on the columns, so
402this would usually be a list of two colors. If both 403this would usually be a list of two colors. This can also be a list
403@code{:row-colors} and @code{:column-colors} is present, the colors 404of faces to be used. If both @code{:row-colors} and
404will be ``blended'' to produce the final colors in the table. 405@code{:column-colors} is present, the colors will be ``blended'' to
406produce the final colors in the table.
405 407
406@item :actions 408@item :actions
407This uses the same syntax as @code{define-keymap}, but doesn't refer 409This uses the same syntax as @code{define-keymap}, but doesn't refer
diff --git a/lisp/emacs-lisp/vtable.el b/lisp/emacs-lisp/vtable.el
index 5b868440108..f2c20b6a806 100644
--- a/lisp/emacs-lisp/vtable.el
+++ b/lisp/emacs-lisp/vtable.el
@@ -145,8 +145,8 @@ See info node `(vtable)Top' for vtable documentation."
145 :ellipsis ellipsis))) 145 :ellipsis ellipsis)))
146 ;; Compute missing column data. 146 ;; Compute missing column data.
147 (setf (vtable-columns table) (vtable--compute-columns table)) 147 (setf (vtable-columns table) (vtable--compute-columns table))
148 ;; Compute colors if we have to mix them. 148 ;; Compute the colors.
149 (when (and row-colors column-colors) 149 (when (or row-colors column-colors)
150 (setf (slot-value table '-cached-colors) 150 (setf (slot-value table '-cached-colors)
151 (vtable--compute-colors row-colors column-colors))) 151 (vtable--compute-colors row-colors column-colors)))
152 ;; Compute the divider. 152 ;; Compute the divider.
@@ -175,9 +175,41 @@ See info node `(vtable)Top' for vtable documentation."
175 table)) 175 table))
176 176
177(defun vtable--compute-colors (row-colors column-colors) 177(defun vtable--compute-colors (row-colors column-colors)
178 (cl-loop for row in row-colors 178 (cond
179 collect (cl-loop for column in column-colors 179 ((null column-colors)
180 collect (vtable--color-blend row column)))) 180 (mapcar #'vtable--make-color-face row-colors))
181 ((null row-colors)
182 (mapcar #'vtable--make-color-face column-colors))
183 (t
184 (cl-loop for row in row-colors
185 collect (cl-loop for column in column-colors
186 collect (vtable--face-blend
187 (vtable--make-color-face row)
188 (vtable--make-color-face column)))))))
189
190(defun vtable--make-color-face (object)
191 (if (stringp object)
192 (list :background object)
193 object))
194
195(defun vtable--face-blend (face1 face2)
196 (let ((foreground (vtable--face-color face1 face2 #'face-foreground
197 :foreground))
198 (background (vtable--face-color face1 face2 #'face-background
199 :background)))
200 `(,@(and foreground (list :foreground foreground))
201 ,@(and background (list :background background)))))
202
203(defun vtable--face-color (face1 face2 accessor slot)
204 (let ((col1 (if (facep face1)
205 (funcall accessor face1)
206 (plist-get face1 slot)))
207 (col2 (if (facep face2)
208 (funcall accessor face2)
209 (plist-get face2 slot))))
210 (if (and col1 col2)
211 (vtable--color-blend col1 col2)
212 (or col1 col2))))
181 213
182;;; FIXME: This is probably not the right way to blend two colors, is 214;;; FIXME: This is probably not the right way to blend two colors, is
183;;; it? 215;;; it?
@@ -441,10 +473,11 @@ This also updates the displayed table."
441 (let ((start (point)) 473 (let ((start (point))
442 (columns (vtable-columns table)) 474 (columns (vtable-columns table))
443 (column-colors 475 (column-colors
444 (if (vtable-row-colors table) 476 (and (vtable-column-colors table)
445 (elt (slot-value table '-cached-colors) 477 (if (vtable-row-colors table)
446 (mod line-number (length (vtable-row-colors table)))) 478 (elt (slot-value table '-cached-colors)
447 (vtable-column-colors table))) 479 (mod line-number (length (vtable-row-colors table))))
480 (slot-value table '-cached-colors))))
448 (divider (vtable-divider table)) 481 (divider (vtable-divider table))
449 (keymap (slot-value table '-cached-keymap))) 482 (keymap (slot-value table '-cached-keymap)))
450 (seq-do-indexed 483 (seq-do-indexed
@@ -517,8 +550,7 @@ This also updates the displayed table."
517 (when column-colors 550 (when column-colors
518 (add-face-text-property 551 (add-face-text-property
519 start (point) 552 start (point)
520 (list :background 553 (elt column-colors (mod index (length column-colors)))))
521 (elt column-colors (mod index (length column-colors))))))
522 (when (and divider (not last)) 554 (when (and divider (not last))
523 (insert divider) 555 (insert divider)
524 (setq start (point)))))) 556 (setq start (point))))))
@@ -526,11 +558,10 @@ This also updates the displayed table."
526 (insert "\n") 558 (insert "\n")
527 (put-text-property start (point) 'vtable-object (car line)) 559 (put-text-property start (point) 'vtable-object (car line))
528 (unless column-colors 560 (unless column-colors
529 (when-let ((row-colors (vtable-row-colors table))) 561 (when-let ((row-colors (slot-value table '-cached-colors)))
530 (add-face-text-property 562 (add-face-text-property
531 start (point) 563 start (point)
532 (list :background 564 (elt row-colors (mod line-number (length row-colors))))))))
533 (elt row-colors (mod line-number (length row-colors)))))))))
534 565
535(defun vtable--cache-key () 566(defun vtable--cache-key ()
536 (cons (frame-terminal) (window-width))) 567 (cons (frame-terminal) (window-width)))