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

    Fix long-standing issue with denote-sort-dired buffer name
    
    The 'dired' function cannot be called with a 'cons' whose 'car'
    contains a forward slash because that breaks the resulting buffer. By
    renaming the buffer after it is created, we circumvent this problem
    and can thus use the format we want.
    
    Given this opportunity, the format of the buffer name is subject to a
    variable that users can modify. I am doing this in response to pull
    request 595 by mjkalyan: <https://github.com/protesilaos/denote/pull/595>.
---
 README.org |  4 ++--
 denote.el  | 31 +++++++++++++++++++++----------
 2 files changed, 23 insertions(+), 12 deletions(-)

diff --git a/README.org b/README.org
index 6b2e5571a5..4a206efb29 100644
--- a/README.org
+++ b/README.org
@@ -6380,8 +6380,8 @@ Denote is meant to be a collective effort.  Every bit of 
help matters.
   atanasj, azegas, babusri, bdillahu, coherentstate, doolio, duli,
   drcxd, elge70, elliottw, fingerknight, hpgisler,
   hyperfocus1337,johkneisl, jtpavlock, juh, leafarbelm,
-  mentalisttraceur, pRot0ta1p, rbenit68, relict007, sarcom-sar,
-  sienic, skissue, sundar bp, yetanotherfossman, zadca123
+  mentalisttraceur, mjkalyan, pRot0ta1p, rbenit68, relict007,
+  sarcom-sar, sienic, skissue, sundar bp, yetanotherfossman, zadca123
 
 Special thanks to Peter Povinec who helped refine the file-naming
 scheme, which is the cornerstone of this project.
diff --git a/denote.el b/denote.el
index 9ff12ab102..7bdcc58f4c 100644
--- a/denote.el
+++ b/denote.el
@@ -1667,6 +1667,23 @@ OMIT-CURRENT have been applied."
         ('exclude-regexp (setq exclude-rx 
(denote-sort-exclude-files-prompt)))))
     (list sort-by-component reverse-sort exclude-rx)))
 
+(defvar denote-sort-dired-buffer-name-function 
#'denote-sort-dired-format-buffer-name
+  "Function to format a buffer name for `denote-sort-dired'.
+It is called with all the arguments passed to `denote-sort-dired' and
+must return a string that is appropriate for a buffer name.")
+
+(defun denote-sort-dired-format-buffer-name (files-matching-regexp 
sort-by-component reverse exclude-regexp)
+  "Format buffer name for `denote-sort-dired'.
+The FILES-MATCHING-REGEXP, SORT-BY-COMPONENT, REVERSE, and
+EXCLUDE-REGEXP all have the same meaning as `denote-sort-dired'.
+Process them to return the buffer name."
+  (format-message
+   "*denote-dired: regexp `%s'; sort `%s'%s%s*"
+   files-matching-regexp
+   sort-by-component
+   (if reverse "; reverse t" "")
+   (if exclude-regexp (format-message "; exclude-regexp `%s'" exclude-regexp) 
"")))
+
 ;;;###autoload
 (defun denote-sort-dired (files-matching-regexp sort-by-component reverse 
exclude-regexp)
   "Produce Dired buffer with sorted files from variable `denote-directory'.
@@ -1704,18 +1721,12 @@ When called from Lisp, the arguments are a string, a 
symbol among
                           nil))
         (exclude-rx (or exclude-regexp nil)))
     (if-let* ((default-directory (denote-directory))
-              (files (denote-sort-get-directory-files files-matching-regexp 
component reverse-sort nil exclude-rx))
-              ;; NOTE 2023-12-04: Passing the FILES-MATCHING-REGEXP as
-              ;; buffer-name produces an error if the regexp contains a
-              ;; wildcard for a directory. I can reproduce this in emacs
-              ;; -Q and am not sure if it is a bug. Anyway, I will report
-              ;; it upstream, but even if it is fixed we cannot use it
-              ;; for now (whatever fix will be available for Emacs 30+).
-              (denote-sort-dired-buffer-name (format "Denote sort `%s' by 
`%s'" files-matching-regexp component))
-              (buffer-name (format "Denote sort by `%s' at %s" component 
(format-time-string "%T"))))
-        (let ((dired-buffer (dired (cons buffer-name (mapcar 
#'file-relative-name files)))))
+              (files (denote-sort-get-directory-files files-matching-regexp 
component reverse-sort nil exclude-rx)))
+        (let ((dired-buffer (dired (cons (denote-directory) (mapcar 
#'file-relative-name 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
+            (rename-buffer buffer-name :unique)
             (setq-local revert-buffer-function
                         (lambda (&rest _)
                           ;; FIXME 2025-01-04: Killing the buffer has

Reply via email to