From efd4ceee1a00df80a6e5815148e300ef50ec5d41 Mon Sep 17 00:00:00 2001 From: Visuwesh Date: Sat, 29 Jan 2022 20:01:23 +0530 Subject: pdf-annot: Add function to sort the date field Previously, the `date` field was sorted alphabetically, instead of according to date. Changes: * lisp/pdf-annot.el (pdf-annot--make-entry-formatter): Propertize string to include the lisp date. (pdf-annot-list-mode): Add function to sort date. Notes: - Why use `propertize` instead of parsing the string back? + Because there are annotations with no associated dates. In this case, we display the string "No date". Our parsing code would have to handle that, `propertize` is just simpler and covers all edge-cases correctly. - What can we do better? + Currently, the index of the field we are looking up is hard-coded. So any change in the fields will impact this code silently. We need to add tests to ensure this is caught. Closes: #75 --- lisp/pdf-annot.el | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/lisp/pdf-annot.el b/lisp/pdf-annot.el index 86b483e..4a8c2e9 100644 --- a/lisp/pdf-annot.el +++ b/lisp/pdf-annot.el @@ -1642,7 +1642,9 @@ pretty-printed output." (lambda (str) (replace-regexp-in-string "\n" " " str t t)))) (cl-ecase entry-type - (date (pdf-annot-print-property a 'modified)) + (date (propertize (pdf-annot-print-property a 'modified) + 'date + (pdf-annot-get a 'modified))) (page (pdf-annot-print-property a 'page)) (label (funcall prune-newlines (pdf-annot-print-property a 'label))) @@ -1669,19 +1671,38 @@ pretty-printed output." pdf-annot-list-format)))) (define-derived-mode pdf-annot-list-mode tablist-mode "Annots" + ;; @TODO: Remove the hard-coded index values here, and figure out a + ;; way to properly link this to the index values of + ;; `pdf-annot-list-format'. + + ;; @TODO: Add tests for annotation formatting and display (let* ((page-sorter (lambda (a b) (< (string-to-number (aref (cadr a) 0)) (string-to-number (aref (cadr b) 0))))) + (date-sorter + (lambda (a b) + (time-less-p (get-text-property 0 'date (aref (cadr a) 3)) + (get-text-property 0 'date (aref (cadr b) 3))))) (format-generator (lambda (format) (let ((field (car format)) (width (cdr format))) (cl-case field - (page `("Pg." 3 ,page-sorter :read-only t :right-alight t)) + (page `("Pg." + ,width + ,page-sorter + :read-only t + :right-align t)) + (date `("Date" + ,width + ,date-sorter + :read-only t)) (t (list (capitalize (symbol-name field)) - width t :read-only t))))))) + width + t + :read-only t))))))) (setq tabulated-list-entries 'pdf-annot-list-entries tabulated-list-format (vconcat (mapcar -- cgit v1.0