branch: externals/denote
commit a9621ff851346964bf418953c3b41e7c1357aaf4
Merge: f543f305ad 8a61780089
Author: Protesilaos Stavrou <i...@protesilaos.com>
Commit: GitHub <nore...@github.com>

    Merge pull request #594 from lmq-10/sort-query-buffer
    
    Add support for sorting files in query buffers
---
 denote.el | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++---------
 1 file changed, 66 insertions(+), 11 deletions(-)

diff --git a/denote.el b/denote.el
index 60d6d7b616..5dd63d48a4 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)
@@ -5211,7 +5247,7 @@ Optional DISPLAY-BUFFER-ACTION is a `display-buffer' 
action and
 concomitant alist, such as `denote-backlinks-display-buffer-action'."
   (let* ((inhibit-read-only t)
          (file buffer-file-name)
-         (buffer (or buffer-name (format-message "Denote query for `%s'" 
query)))
+         (buffer (or buffer-name (format-message "*Denote query for `%s'*" 
query)))
          ;; We retrieve results in absolute form and change the
          ;; absolute path to a relative path below. We could add a
          ;; suitable function and the results would be automatically
@@ -5307,7 +5343,9 @@ see `denote-query-exclude-files-with-keywords'."
                file)
         (push file final-files)))
     (if final-files
-        (denote-make-links-buffer denote-query--last-query final-files)
+        (denote-make-links-buffer denote-query--last-query final-files
+                                  (and (eq major-mode 'denote-query-mode) 
(buffer-name))
+                                  '(display-buffer-same-window))
       (user-error "No remaining files when applying that filter"))
     (message "Excluding files matching `%s'" regexp)))
 
@@ -5329,7 +5367,9 @@ when REGEXP is a list."
              file)
         (push file final-files)))
     (if final-files
-        (denote-make-links-buffer denote-query--last-query final-files)
+        (denote-make-links-buffer denote-query--last-query final-files
+                                  (and (eq major-mode 'denote-query-mode) 
(buffer-name))
+                                  '(display-buffer-same-window))
       (user-error "No remaining files when applying that filter"))
     (message "Only including files matching `%s'" regexp)))
 
@@ -5368,9 +5408,24 @@ means of e.g. `denote-query-exclude-files')."
   (interactive nil denote-query-mode)
   (unless (derived-mode-p 'denote-query-mode)
     (user-error "Only use this command inside the `denote-query-mode'"))
-  (denote-make-links-buffer denote-query--last-query)
+  (denote-make-links-buffer denote-query--last-query nil
+                            (and (eq major-mode 'denote-query-mode) 
(buffer-name))
+                            '(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 denote-query--last-files
+                              (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