Package: emacs;
Reported by: Jean Louis <bugs <at> gnu.support>
Date: Tue, 22 Dec 2020 07:15:02 UTC
Severity: normal
Tags: notabug
Found in version 28.0.50
Done: Jean Louis <bugs <at> gnu.support>
Bug is archived. No further changes may be made.
View this message in rfc822 format
From: Jean Louis <bugs <at> gnu.support> To: 45361 <at> debbugs.gnu.org Subject: bug#45361: 28.0.50; tabulated-list-mode: should be sorting by specified sort function Date: Tue, 22 Dec 2020 08:13:50 +0100
PROBLEM: The variable `tabulated-list-format' provides for programmer option to sort columns and I would like to sort number as strings "12" as numbers, not as strings. I do not know how to properly provide the sorting function to `tabulated-list-format' so that it works when actually sorting. DATA: The tabulated list mode works as this: (let ((buffer "*tabulated*")) (switch-to-buffer-other-window buffer) (setq tabulated-list-format [("Count" 10 t :right-align t) ("Name" 60 t)]) (setq tabulated-list-entries '((6 ["6" "Mercury"]) (7 ["7" "GNU Emacs"]) (8 ["8" "YouTube Videos"]) (9 ["9" "Speedy worker cleaning tables in a restaurant"]) (71 ["71" "Trenching"]) (82 ["82" "Easier trenching idea"]))) (tabulated-list-mode) (tabulated-list-init-header) (hl-line-mode) (tabulated-list-print)) When evaluated I would get the list such as: 6 Mercury 7 GNU Emacs 71 Trenching 8 YouTube Videos 82 Easier trenching idea 9 Speedy worker cleaning tables in a restaurant and my goal would be to be able to sort the column "Count" so that I get a result sorted by the number (which is string): 6 Mercury 7 GNU Emacs 8 YouTube Videos 9 Speedy worker cleaning tables in a restaurant 71 Trenching 82 Easier trenching idea The description of `tabulated-list-format' says: The format of the current Tabulated List mode buffer. This should be a vector of elements (NAME WIDTH SORT . PROPS), where: - NAME is a string describing the column. This is the label for the column in the header line. Different columns must have non-‘equal’ names. - WIDTH is the width to reserve for the column. For the final element, its numerical value is ignored. - SORT specifies how to sort entries by this column. If nil, this column cannot be used for sorting. If t, sort by comparing the string value printed in the column. Otherwise, it should be a predicate function suitable for ‘sort’, accepting arguments with the same form as the elements of ‘tabulated-list-entries’. I could make a function to list what elements are accepted on click: (defun my-sort (&rest args) (message "%s" args)) and include it here: (let ((buffer "*tabulated*")) (switch-to-buffer-other-window buffer) (setq tabulated-list-format [("Count" 10 my-sort :right-align t) ("Name" 60 t)]) (setq tabulated-list-entries '((1 ["6" "Mercury"]) (2 ["7" "GNU Emacs"]) (3 ["8" "YouTube Videos"]) (4 ["9" "Speedy worker cleaning tables in a restaurant"]) (5 ["71" "Trenching"]) (6 ["82" "Easier trenching idea"]))) (tabulated-list-mode) (tabulated-list-init-header) (hl-line-mode) (tabulated-list-print)) Then I can see something like this in `*Messages*' buffer: ((6 [82 Easier trenching idea]) (5 [71 Trenching])) ((6 [82 Easier trenching idea]) (4 [9 Speedy worker cleaning tables in a restaurant])) ((5 [71 Trenching]) (4 [9 Speedy worker cleaning tables in a restaurant])) ((3 [8 YouTube Videos]) (2 [7 GNU Emacs])) ((3 [8 YouTube Videos]) (1 [6 Mercury])) ((2 [7 GNU Emacs]) (1 [6 Mercury])) ((6 [82 Easier trenching idea]) (3 [8 YouTube Videos])) ((5 [71 Trenching]) (3 [8 YouTube Videos])) ((4 [9 Speedy worker cleaning tables in a restaurant]) (3 [8 YouTube Videos])) Then I would like to use the function `string-collate-lessp' as that seem to understand how numbers should be compared. For example this is giving me correct result: (sort '("121" "117" "1") 'string-collate-lessp) => ("1" "117" "121") Then I am attempting to make the sorting function: (defun my-sort (s1 s2) (let* ((s1-number (elt (cadr s1) 0)) (s2-number (elt (cadr s2) 0))) (string-collate-lessp s1-number s2-number))) (my-sort '(2 ["7" "GNU Emacs"]) '(1 ["6" "Mercury"])) Now this works well first time as when the list is displayed it is already sorted by "Count": (let ((buffer "*tabulated*")) (switch-to-buffer-other-window buffer) (setq tabulated-list-format [("Count" 10 my-sort :right-align t) ("Name" 60 nil)]) (setq tabulated-list-entries '((1 ["6" "Mercury"]) (2 ["7" "GNU Emacs"]) (3 ["8" "YouTube Videos"]) (4 ["9" "Speedy worker cleaning tables in a restaurant"]) (5 ["71" "Trenching"]) (6 ["82" "Easier trenching idea"]))) (tabulated-list-mode) (tabulated-list-init-header) (hl-line-mode) (tabulated-list-print)) But when I click on "Count" column or press S then it again gets sorted rather alphabetic, not by using my function. That is where I would need help to understand what is happening. Because when I press S in the column "Count" when cursor is on the number, it invokes this function: (defun tabulated-list-sort (&optional n) "Sort Tabulated List entries by the column at point. With a numeric prefix argument N, sort the Nth column." (interactive "P") (let ((name (if n (car (aref tabulated-list-format n)) (get-text-property (point) 'tabulated-list-column-name)))) (if (nth 2 (assoc name (append tabulated-list-format nil))) (tabulated-list--sort-by-column-name name) (user-error "Cannot sort by %s" name)))) Then this one: (defun tabulated-list--sort-by-column-name (name) (when (and name (derived-mode-p 'tabulated-list-mode)) ;; Flip the sort order on a second click. (if (equal name (car tabulated-list-sort-key)) (setcdr tabulated-list-sort-key (not (cdr tabulated-list-sort-key))) (setq tabulated-list-sort-key (cons name nil))) (tabulated-list-init-header) (tabulated-list-print t))) And if I read well those functions do not search for my function and invoke `my-sort', but they should. Am I right there? Jean In GNU Emacs 28.0.50 (build 2, x86_64-pc-linux-gnu, X toolkit, cairo version 1.14.8, Xaw3d scroll bars) of 2020-11-25 built on protected.rcdrun.com Repository revision: 30c437752df0a3a9410f1249fa0f237110811af2 Repository branch: master Windowing system distributor 'The X.Org Foundation', version 11.0.11907000 System Description: Hyperbola GNU/Linux-libre Configured using: 'configure --prefix=/package/text/emacs --with-modules --with-x-toolkit=lucid' Configured features: XAW3D XPM JPEG TIFF GIF PNG RSVG CAIRO SOUND GPM DBUS GSETTINGS GLIB NOTIFY INOTIFY ACL GNUTLS LIBXML2 FREETYPE HARFBUZZ M17N_FLT LIBOTF ZLIB TOOLKIT_SCROLL_BARS LUCID X11 XDBE XIM MODULES THREADS JSON PDUMPER LCMS2 Important settings: value of $LC_ALL: en_US.UTF-8 value of $LANG: de_DE.UTF-8 value of $XMODIFIERS: @im=exwm-xim locale-coding-system: utf-8-unix -- Thanks, Jean Louis ⎔ λ 🄯 𝍄 𝌡 𝌚
GNU bug tracking system
Copyright (C) 1999 Darren O. Benham,
1997,2003 nCipher Corporation Ltd,
1994-97 Ian Jackson.