branch: externals/denote
commit 90fcf37de645a82ccf176b4a27fb63df1ba94294
Author: Ashton Wiersdorf <m...@wiersdorf.dev>
Commit: Ashton Wiersdorf <m...@wiersdorf.dev>

    Add denote--file-has-backlinks-p; search for backlinks iff needed
    
    This makes it so that denote-rename-buffer--format only searches for
    backlinks to the current file if "%b" is part of the format string.
    This is because searching for backlinks might be a little expensive,
    and it is good to avoid this cost if the user doesn't want it.
---
 denote-rename-buffer.el | 6 ++++--
 denote.el               | 4 ++++
 2 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/denote-rename-buffer.el b/denote-rename-buffer.el
index f85a7da57b..3e157f193d 100644
--- a/denote-rename-buffer.el
+++ b/denote-rename-buffer.el
@@ -98,14 +98,16 @@ buffer will be used, if available."
   "Parse the BUFFER through the `denote-rename-buffer-format'."
   (when-let ((file (buffer-file-name buffer))
              (type (denote-filetype-heuristics file)))
-    (let ((has-backlinks (not (zerop (length (denote-link-return-backlinks 
file))))))
+    (let ((should-show-backlink-indicator (and ; only do search if format 
contains "%b"
+                                           (string-match-p "%b" 
denote-rename-buffer-format)
+                                           (denote--file-has-backlinks-p 
file))))
       (string-trim
        (format-spec denote-rename-buffer-format
                     (list (cons ?t (cond
                                     ((denote-retrieve-front-matter-title-value 
file type))
                                     ((denote-retrieve-filename-title file))
                                     (t  "")))
-                          (cons ?b (if has-backlinks 
denote-buffer-has-backlinks-string ""))
+                          (cons ?b (if should-show-backlink-indicator 
denote-buffer-has-backlinks-string ""))
                           (cons ?i (or (denote-retrieve-filename-identifier 
file) ""))
                           (cons ?d (or (denote-retrieve-filename-identifier 
file) ""))
                           (cons ?s (or (denote-retrieve-filename-signature 
file) ""))
diff --git a/denote.el b/denote.el
index a191cb2402..b1078f1816 100644
--- a/denote.el
+++ b/denote.el
@@ -4015,6 +4015,10 @@ Also see `denote-link-return-links'."
              (id (denote-retrieve-filename-identifier-with-error 
current-file)))
     (delete current-file (denote--retrieve-files-in-xrefs id))))
 
+(defun denote--file-has-backlinks-p (file)
+  "Return whether or not there exists a file with a link to IDENTIFIER."
+  (not (zerop (length (denote-link-return-backlinks file)))))
+
 ;;;###autoload
 (defun denote-find-backlink ()
   "Use minibuffer completion to visit backlink to current file.

Reply via email to