branch: externals/denote commit 1fa853acc04fc55f346b1cde2bbab27bf979bfff Author: Protesilaos Stavrou <i...@protesilaos.com> Commit: Protesilaos Stavrou <i...@protesilaos.com>
Introduce the function denote-directories-get-common-root --- denote.el | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/denote.el b/denote.el index 6770521009..4e463425ed 100644 --- a/denote.el +++ b/denote.el @@ -1015,12 +1015,30 @@ to override what this function returns." "Return non-nil if the variable `denote-directory' is a single item." (not (cdr (denote-directories)))) +(defun denote--get-common-root-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 "/")) + "/")) + +(defun denote-directories-get-common-root () + "Get the common root directory of `denote-directories'." + (denote--get-common-root-directory (denote-directories))) + ;; FIXME 2025-08-05: There are many parts in the code where we ;; hardcode `(car (denote-directories))' and others where ;; `(denote-directory)' is used. This is flawed. We need to either ;; prompt for one when one is absolutely necessary (is this ever the ;; case?) or fall back to the common root as in the function -;; `denote-sort-dired--find-common-directory'. +;; `denote-directories-get-common-root'. (defun denote-directory () "Return the `car' of `denote-directories'. Unless this is definitely what you need, use the `denote-directories' @@ -1893,20 +1911,6 @@ BUFFER-NAME is the name of the resulting buffer." (denote-dired-empty-mode))))) buffer-name)) -(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'. @@ -1944,7 +1948,7 @@ When called from Lisp, the arguments are a string, a symbol among files))))) (if-let* ((directory (if relative-p ; see comment in `denote-file-prompt' (car (denote-directories)) - (denote-sort-dired--find-common-directory (denote-directories)))) + (denote-directories-get-common-root (denote-directories)))) (files (funcall files-fn)) (dired-name (format-message files-matching-regexp)) (buffer-name (funcall denote-sort-dired-buffer-name-function files-matching-regexp sort-by-component reverse-sort exclude-regexp)))