branch: externals/denote commit d8fd1cb2531e804b373277300f45346b726ad7e7 Merge: c69210be6c afb4a9807e Author: Protesilaos Stavrou <i...@protesilaos.com> Commit: GitHub <nore...@github.com>
Merge pull request #621 from jeanphilippegg/cleanup Various commits --- denote.el | 105 ++++++++++++++++++++++++++++++++------------------------------ 1 file changed, 55 insertions(+), 50 deletions(-) diff --git a/denote.el b/denote.el index 2196b49d2a..284fa08d92 100644 --- a/denote.el +++ b/denote.el @@ -2448,6 +2448,11 @@ To create a new one from a date, refer to the function referred by (or (denote-retrieve-filename-identifier file) (error "Cannot find `%s' as a file with a Denote identifier" file))) +(make-obsolete + 'denote-retrieve-filename-identifier-with-error + 'denote-retrieve-filename-identifier + "4.1.0") + (define-obsolete-variable-alias 'denote--used-ids 'denote-used-identifiers @@ -2891,7 +2896,7 @@ available id is found." (time-add (date-to-time current-id) 1)))) current-id)) -(defun denote-generate-identifier-as-date (&optional initial-identifier date) +(defun denote-generate-identifier-as-date (initial-identifier date) "Generate an identifier based on DATE. If INITIAL-IDENTIFIER is not already used, return it. Else, if it is @@ -2925,7 +2930,7 @@ If ID is already used, increment it until an available id is found." (setq current-id (number-to-string (1+ (string-to-number current-id))))) current-id)) -(defun denote-generate-identifier-as-number (&optional initial-identifier _date) +(defun denote-generate-identifier-as-number (initial-identifier _date) "Generate an increasing number identifier. If INITIAL-IDENTIFIER is not already used, return it. Else, if it is @@ -5538,50 +5543,21 @@ Intended to be used as `denote-query-format-heading-function'." title denote-query-untitled-string)) -;; NOTE 2025-03-24: The `&rest' is there because we used to have an -;; extra SHOW-CONTEXT parameter. This way we do not break anybody's -;; code, even if we slightly modify the behaviour. -(defun denote-make-links-buffer (query &optional files buffer-name display-buffer-action &rest _) - "Create links' buffer called BUFFER-NAME for QUERY. - -Optional FILES can be a list of files to search for. It can also be a -regexp, which limits the files accordingly per `denote-directory-files'. +(defun denote--display-buffer-from-xref-alist (xref-alist buffer-name display-buffer-action) + "Create buffer called BUFFER-NAME for XREF-ALIST. -Optional DISPLAY-BUFFER-ACTION is a `display-buffer' action and -concomitant alist, such as `denote-backlinks-display-buffer-action'." +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 - (denote-format-buffer-name (format-message "query for `%s'" query) :special-buffer))) - ;; 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 - ;; in relative form, but eventually notes may not be all - ;; under a common directory (or project). - (xref-alist (denote-retrieve-xref-alist query files)) (dirs (denote-directories))) (unless xref-alist - (error "No matches for query `%s'" query)) - ;; Update internal variables - (setq denote-query--last-files nil) - (setq denote-query--last-query query) + (error "No results to display")) + ;; Update group of each item in xref-alist (dolist (x xref-alist) - (let* ((file-xref (car x)) - (file - ;; NOTE: Unfortunately, the car of the xref construct is - ;; not reliable; sometimes it's absolute, sometimes it - ;; is not - (if (file-name-absolute-p file-xref) - file-xref - (xref-location-group - (xref-match-item-location (car (last x))))))) - ;; Add to current set of files - (push file denote-query--last-files) - ;; Format heading - (setf (car x) (funcall denote-query-format-heading-function file)))) - (delete-dups denote-query--last-files) - ;; Insert results - (with-current-buffer (get-buffer-create buffer) + (let* ((file-xref (car x))) + (setf (car x) (funcall denote-query-format-heading-function file-xref)))) + (with-current-buffer (get-buffer-create buffer-name) (erase-buffer) (denote-query-mode) ;; In the links' buffer, the values of variables set in a @@ -5597,8 +5573,31 @@ concomitant alist, such as `denote-backlinks-display-buffer-action'." (setq-local revert-buffer-function (lambda (_ignore-auto _noconfirm) (when-let* ((buffer-file-name file)) - (denote-make-links-buffer query files buffer-name display-buffer-action))))) - (display-buffer buffer display-buffer-action))) + (denote--display-buffer xref-alist buffer-name display-buffer-action))))) + (display-buffer buffer-name display-buffer-action))) + +;; NOTE 2025-03-24: The `&rest' is there because we used to have an +;; extra SHOW-CONTEXT parameter. This way we do not break anybody's +;; code, even if we slightly modify the behaviour. +(defun denote-make-links-buffer (query &optional files buffer-name display-buffer-action &rest _) + "Create links' buffer called BUFFER-NAME for QUERY. + +Optional FILES can be a list of files to search for. It can also be a +regexp, which limits the files accordingly per `denote-directory-files'. + +Optional DISPLAY-BUFFER-ACTION is a `display-buffer' action and +concomitant alist, such as `denote-backlinks-display-buffer-action'." + (let* ((inhibit-read-only t) + (buffer (or buffer-name + (denote-format-buffer-name (format-message "query for `%s'" query) :special-buffer))) + (xref-alist (denote-retrieve-xref-alist query files))) + (unless xref-alist + (error "No matches for query `%s'" query)) + (setq denote-query--last-query query) + (setq denote-query--last-files + (delete-dups + (mapcar #'car xref-alist))) + (denote--display-buffer-from-xref-alist xref-alist buffer display-buffer-action))) (defvar denote-query-links-buffer-function #'denote-make-links-buffer "Function to make an Xref buffer showing query link results. @@ -5878,11 +5877,12 @@ Place the buffer below the current window or wherever the user option `denote-backlinks-display-buffer-action' specifies." (interactive) (if-let* ((file buffer-file-name)) - (when-let* ((identifier (denote-retrieve-filename-identifier-with-error file))) - (funcall denote-query-links-buffer-function - identifier nil - (denote--backlinks-get-buffer-name file identifier) - denote-backlinks-display-buffer-action)) + (if-let* ((identifier (denote-retrieve-filename-identifier file))) + (funcall denote-query-links-buffer-function + identifier nil + (denote--backlinks-get-buffer-name file identifier) + denote-backlinks-display-buffer-action) + (user-error "The current file does not have a Denote identifier")) (user-error "Buffer `%s' is not associated with a file" (current-buffer)))) (defalias 'denote-show-backlinks-buffer 'denote-backlinks @@ -5897,7 +5897,8 @@ Place the buffer below the current window or wherever the user option "Return list of backlinks in current or optional FILE. Also see `denote-get-links'." (when-let* ((current-file (or file (buffer-file-name))) - (id (denote-retrieve-filename-identifier-with-error current-file))) + (id (or (denote-retrieve-filename-identifier current-file) + (user-error "The file does not have a Denote identifier")))) (delete current-file (denote-retrieve-files-xref-query id)))) ;; TODO 2024-09-04: Instead of using `denote-get-backlinks' we @@ -5916,7 +5917,10 @@ context-sensitive operation, use `denote-find-backlink-with-location'. Alo see `denote-find-link'." (declare (interactive-only t)) (interactive) - (when-let* ((links (or (denote-get-backlinks) + (when-let* ((current-file buffer-file-name) + (_ (or (denote-retrieve-filename-identifier current-file) + (user-error "The current file does not have a Denote identifier"))) + (links (or (denote-get-backlinks current-file) (user-error "No backlinks found"))) (selected (denote-select-from-files-prompt links "Select among BACKLINKS"))) (find-file selected))) @@ -5927,7 +5931,8 @@ Alo see `denote-find-link'." (declare (interactive-only t)) (interactive) (when-let* ((current-file buffer-file-name) - (id (denote-retrieve-filename-identifier-with-error current-file)) + (id (or (denote-retrieve-filename-identifier current-file) + (user-error "The current file does not have a Denote identifier"))) (files (denote-directory-files nil :omit-current :text-only)) (fetcher (lambda () (xref-matches-in-files id files)))) (xref-show-definitions-completing-read fetcher nil)))