|
36 | 36 |
|
37 | 37 | ;;; Code: |
38 | 38 |
|
| 39 | +(eval-when-compile (require 'cl-lib)) |
| 40 | + |
39 | 41 | (defgroup tabulated-list nil |
40 | 42 | "Tabulated-list customization group." |
41 | 43 | :group 'convenience |
@@ -645,18 +647,41 @@ this is the vector stored within it." |
645 | 647 |
|
646 | 648 | (defun tabulated-list-sort (&optional n) |
647 | 649 | "Sort Tabulated List entries by the column at point. |
648 | | -With a numeric prefix argument N, sort the Nth column." |
| 650 | +With a numeric prefix argument N, sort the Nth column. |
| 651 | +
|
| 652 | +If the numeric prefix is -1, restore order the list was |
| 653 | +originally displayed in." |
649 | 654 | (interactive "P") |
650 | | - (let ((name (if n |
651 | | - (car (aref tabulated-list-format n)) |
652 | | - (get-text-property (point) |
653 | | - 'tabulated-list-column-name)))) |
654 | | - (if (nth 2 (assoc name (append tabulated-list-format nil))) |
655 | | - (tabulated-list--sort-by-column-name name) |
656 | | - (user-error "Cannot sort by %s" name)))) |
| 655 | + (if (equal n -1) |
| 656 | + ;; Restore original order. |
| 657 | + (progn |
| 658 | + (unless tabulated-list--original-order |
| 659 | + (error "Order is already in original order")) |
| 660 | + (setq tabulated-list-entries |
| 661 | + (sort tabulated-list-entries |
| 662 | + (lambda (e1 e2) |
| 663 | + (< (gethash e1 tabulated-list--original-order) |
| 664 | + (gethash e2 tabulated-list--original-order))))) |
| 665 | + (setq tabulated-list-sort-key nil) |
| 666 | + (tabulated-list-init-header) |
| 667 | + (tabulated-list-print t)) |
| 668 | + ;; Sort based on a column name. |
| 669 | + (let ((name (if n |
| 670 | + (car (aref tabulated-list-format n)) |
| 671 | + (get-text-property (point) |
| 672 | + 'tabulated-list-column-name)))) |
| 673 | + (if (nth 2 (assoc name (append tabulated-list-format nil))) |
| 674 | + (tabulated-list--sort-by-column-name name) |
| 675 | + (user-error "Cannot sort by %s" name))))) |
657 | 676 |
|
658 | 677 | (defun tabulated-list--sort-by-column-name (name) |
659 | 678 | (when (and name (derived-mode-p 'tabulated-list-mode)) |
| 679 | + (unless tabulated-list--original-order |
| 680 | + ;; Store the original order so that we can restore it later. |
| 681 | + (setq tabulated-list--original-order (make-hash-table)) |
| 682 | + (cl-loop for elem in tabulated-list-entries |
| 683 | + for i from 0 |
| 684 | + do (setf (gethash elem tabulated-list--original-order) i))) |
660 | 685 | ;; Flip the sort order on a second click. |
661 | 686 | (if (equal name (car tabulated-list-sort-key)) |
662 | 687 | (setcdr tabulated-list-sort-key |
@@ -717,6 +742,8 @@ Interactively, N is the prefix numeric argument, and defaults to |
717 | 742 |
|
718 | 743 | ;;; The mode definition: |
719 | 744 |
|
| 745 | +(defvar tabulated-list--original-order nil) |
| 746 | + |
720 | 747 | (define-derived-mode tabulated-list-mode special-mode "Tabulated" |
721 | 748 | "Generic major mode for browsing a list of items. |
722 | 749 | This mode is usually not used directly; instead, other major |
@@ -757,6 +784,7 @@ as the ewoc pretty-printer." |
757 | 784 | (setq-local glyphless-char-display |
758 | 785 | (tabulated-list-make-glyphless-char-display-table)) |
759 | 786 | (setq-local text-scale-remap-header-line t) |
| 787 | + (setq-local tabulated-list--original-order nil) |
760 | 788 | ;; Avoid messing up the entries' display just because the first |
761 | 789 | ;; column of the first entry happens to begin with a R2L letter. |
762 | 790 | (setq bidi-paragraph-direction 'left-to-right) |
|
0 commit comments