]> git.eshelyaron.com Git - emacs.git/commitdiff
Add column sorting order indicators to vtable
authorLars Ingebrigtsen <larsi@gnus.org>
Sat, 19 Feb 2022 13:21:10 +0000 (14:21 +0100)
committerLars Ingebrigtsen <larsi@gnus.org>
Sat, 19 Feb 2022 13:21:19 +0000 (14:21 +0100)
* lisp/emacs-lisp/vtable.el (vtable--indicator): New function.
(vtable--insert-header-line): Use it to display sorting order
indicators.

doc/misc/vtable.texi
lisp/emacs-lisp/vtable.el

index 5c010a1f79c56f9d4e16379e37878b5f23d32c27..71c021da28e448df361942fd734f22ae58d27462 100644 (file)
@@ -517,5 +517,3 @@ Return the column name of the @var{index}th column in @var{table}.
 @printindex cp
 
 @bye
-
-@c todo up/down markers
index 9107c4439c0f38d8c8a7b81ddac3482f633387a1..088498603078e0e340022a6258d4e4a575cf903d 100644 (file)
@@ -474,21 +474,41 @@ This also updates the displayed table."
       (when (eq direction 'descend)
         (setcar cache (nreverse (car cache)))))))
 
+(defun vtable--indicator (table index)
+  (let ((order (car (last (vtable-sort-by table)))))
+    (if (eq index (car order))
+        ;; We're sorting by this column last, so return an indicator.
+        (catch 'found
+          (dolist (candidate (nth (if (eq (cdr order) 'ascend)
+                                      1
+                                    0)
+                                  '((?▼ ?v)
+                                    (?▲ ?^))))
+            (when (char-displayable-p candidate)
+              (throw 'found (string candidate)))))
+      "")))
+
 (defun vtable--insert-header-line (table widths spacer)
   ;; Insert the header directly into the buffer.
-  (let ((start (point)))
+  (let* ((start (point)))
     (seq-do-indexed
      (lambda (column index)
-       (let ((name (propertize
-                    (vtable-column-name column)
-                    'face (list 'header-line (vtable-face table))))
-             (start (point))
-             displayed)
+       (let* ((name (propertize
+                     (vtable-column-name column)
+                     'face (list 'header-line (vtable-face table))))
+              (start (point))
+              (indicator (vtable--indicator table index))
+              (indicator-width (string-pixel-width indicator))
+              displayed)
          (insert
           (setq displayed
-                (if (> (string-pixel-width name) (elt widths index))
-                    (vtable--limit-string name (elt widths index))
-                  name))
+                (concat
+                 (if (> (string-pixel-width name)
+                        (- (elt widths index) indicator-width))
+                     (vtable--limit-string
+                      name (- (elt widths index) indicator-width))
+                   name)
+                 indicator))
           (propertize " " 'display
                       (list 'space :width
                             (list (+ (- (elt widths index)