branch: externals/denote
commit d4804f87abffe8858d0bdb7fb40060db4400e262
Author: Protesilaos Stavrou <i...@protesilaos.com>
Commit: Protesilaos Stavrou <i...@protesilaos.com>

    Make denote-sort-dired work with multiple directories of denote-directory
    
    This build on top of the new feature that allows the user option
    'denote-directory' to accept a list of directories. It was contributed
    by Jean-Philippe Gagné Guay in pull request 609: 
<https://github.com/protesilaos/denote/pull/609>.
---
 denote.el | 25 ++++++++++++++++++++-----
 1 file changed, 20 insertions(+), 5 deletions(-)

diff --git a/denote.el b/denote.el
index 4e2c40d384..67a6c5b86a 100644
--- a/denote.el
+++ b/denote.el
@@ -1753,6 +1753,20 @@ Process them to return the buffer name."
     (if exclude-regexp (format-message "; exclude-regexp `%s'" exclude-regexp) 
""))
    :special-buffer))
 
+(defun denote-sort-dired--find-common-directory (directories)
+  "Return common root directory among DIRECTORIES."
+  (if-let* ((parts (mapcar (lambda (directory) (split-string directory "/" 
:omit-nulls)) directories))
+            (common-parent (seq-reduce
+                            (lambda (dir-parts comparison-parts)
+                              (let ((common nil))
+                                (dolist (part dir-parts)
+                                  (when (member part comparison-parts)
+                                    (push part common)))
+                                (nreverse common)))
+                            parts (car parts))))
+      (format "/%s/" (mapconcat #'identity common-parent "/"))
+    "/"))
+
 ;;;###autoload
 (defun denote-sort-dired (files-matching-regexp sort-by-component reverse 
exclude-regexp)
   "Produce Dired buffer with sorted files from variable `denote-directory'.
@@ -1782,19 +1796,20 @@ When called from Lisp, the arguments are a string, a 
symbol among
 `denote-sort-components', a non-nil value, and a string, respectively."
   (interactive
    (append (list (denote-files-matching-regexp-prompt)) 
(denote-sort-dired--prompts)))
-  (unless (denote-has-single-denote-directory-p)
-    (user-error "This command does not work when `denote-directory' has 
multiple directories"))
   (let ((component (or sort-by-component
                        denote-sort-dired-default-sort-component
                        'identifier))
         (reverse-sort (or reverse
                           denote-sort-dired-default-reverse-sort
                           nil))
-        (exclude-rx (or exclude-regexp nil)))
+        (exclude-rx (or exclude-regexp nil))
+        (single-dir-p (denote-has-single-denote-directory-p)))
     (if-let* (;; See comment in `denote-file-prompt'.
-              (default-directory (car (denote-directories)))
+              (default-directory (if single-dir-p
+                                     (car (denote-directories))
+                                   (denote-sort-dired--find-common-directory 
(denote-directories))))
               (files (denote-sort-get-directory-files files-matching-regexp 
component reverse-sort nil exclude-rx)))
-        (let ((dired-buffer (dired (cons (car (denote-directories)) (mapcar 
#'file-relative-name files))))
+        (let ((dired-buffer (dired (cons default-directory (if single-dir-p 
(mapcar #'file-relative-name files) files))))
               (buffer-name (funcall denote-sort-dired-buffer-name-function 
files-matching-regexp component reverse-sort exclude-rx)))
           (setq denote-sort--dired-buffer dired-buffer)
           (with-current-buffer dired-buffer

Reply via email to