branch: externals/denote
commit 921cb936f3d545667c2ca861a6ade9bcbc982c67
Author: Lucas Quintana <lm...@protonmail.com>
Commit: Lucas Quintana <lm...@protonmail.com>

    Add support for sorting query buffers
---
 denote.el | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 56 insertions(+), 7 deletions(-)

diff --git a/denote.el b/denote.el
index ab898c13b3..5035b501c9 100644
--- a/denote.el
+++ b/denote.el
@@ -2495,13 +2495,20 @@ regular expression, which means to use the text files 
in the variable
 
 If FILES is not given, use all text files as returned by
 `denote-directory-files'."
-  (let ((xref-file-name-display 'abs))
-    (xref--analyze
-     (xref-matches-in-files
-      query
-      (if (and files (listp files))
-          files
-        (denote-directory-files files denote-query--omit-current 
:text-only))))))
+  (let* ((xref-file-name-display 'abs)
+         (data
+          (xref--analyze
+           (xref-matches-in-files
+            query
+            (if (and files (listp files))
+                files
+              (denote-directory-files files denote-query--omit-current 
:text-only))))))
+    (if (not denote-query-sorting)
+        data
+      ;; Sort results
+      (let* ((files-matched (mapcar #'car data))
+             (files-sorted (denote-sort-files files-matched 
denote-query-sorting)))
+        (mapcar (lambda (x) (assoc x data)) files-sorted)))))
 
 ;;;; New note
 
@@ -5070,6 +5077,7 @@ file's title.  This has the same meaning as in 
`denote-link'."
     (define-key map "j" #'outline-next-heading)
     (define-key map "o" #'delete-other-windows)
     (define-key map "s" #'denote-grep)
+    (define-key map "S" #'denote-query-sort-last-search)
     (define-key map "v" #'outline-cycle)
     (define-key map "x" #'denote-query-exclude-files)
     (define-key map "i" #'denote-query-only-include-files)
@@ -5111,6 +5119,34 @@ This is used by the commands `denote-backlinks', 
`denote-grep',
   "Integration between Denote and Xref for grep/query/backlink buffers."
   :group 'denote)
 
+(defcustom denote-query-sorting nil
+  "How to sort files in query buffers.
+
+This applies to buffers generated by `denote-grep' and query links, as
+well as backlinks.
+
+By default, no sorting is performed, so matching files are displayed as
+they are returned by the search program (which often, but not always,
+means they are sorted by file name).
+
+The user can set this variable to a non-nil value in order to sort the
+files in a custom manner.  The value is passed as the COMPONENT argument
+to the `denote-sort-files' function.  That means it can be a symbol
+among `denote-sort-components' or a function which performs the sorting.
+
+The user can also call the function `denote-query-sort-last-search' in
+order to sort file names interactively and temporarily in the query
+buffer."
+  :type '(radio
+          (const :tag "Don't sort (default)" nil)
+          (const :tag "Sort by identifier" identifier)
+          (const :tag "Sort by title" title)
+          (const :tag "Sort by keywords" keywords)
+          (const :tag "Sort by signature" signature)
+          (const :tag "Random order" random))
+  :package-version '(denote . "4.1.0")
+  :group 'denote-query)
+
 (defcustom denote-backlinks-display-buffer-action
   '((display-buffer-reuse-mode-window display-buffer-below-selected)
     (mode . denote-query-mode)
@@ -5377,6 +5413,19 @@ means of e.g. `denote-query-exclude-files')."
                             '(display-buffer-same-window))
   (message "Cleared all filters"))
 
+(defun denote-query-sort-last-search (component)
+  "Sort the files matched by the last search according to COMPONENT.
+
+Interactively, prompt for COMPONENT among `denote-sort-components'.
+
+When called from Lisp, any argument as accepted by `denote-sort-files'
+is allowed."
+  (interactive (list (denote-sort-component-prompt)))
+  (let ((denote-query-sorting component))
+    (denote-make-links-buffer denote-query--last-query nil
+                              (and (eq major-mode 'denote-query-mode) 
(buffer-name))
+                              '(display-buffer-same-window))))
+
 ;;;;;; Additional features for searching file contents
 
 (defvar denote-grep-history nil

Reply via email to