branch: externals/denote
commit 8abe1bd4f191f0ebb960ac377babc2ea28dfab4b
Merge: ba79556066 9be887a963
Author: Protesilaos Stavrou <i...@protesilaos.com>
Commit: GitHub <nore...@github.com>

    Merge pull request #392 from ashton314/main
    
    Add indicator for buffer having backlinks to denote-rename-buffer
---
 README.org              |  8 ++++++--
 denote-rename-buffer.el | 41 ++++++++++++++++++++++++++---------------
 denote.el               |  4 ++++
 3 files changed, 36 insertions(+), 17 deletions(-)

diff --git a/README.org b/README.org
index 78e3af40a3..f64bd65438 100644
--- a/README.org
+++ b/README.org
@@ -3009,6 +3009,7 @@ are placeholders for Denote file name components 
([[#h:4e9c7512-84dc-4dfb-9fa9-e
 - The =%d= is the same as =%i= (=DATE= mnemonic).
 - The =%s= is the Denote =SIGNATURE= of the file.
 - The =%k= is the Denote =KEYWORDS= of the file.
+- The =%b= is an indicator of whether or not the file has backlinks pointing 
to it.
 - The =%%= is a literal percent sign.
 
 In addition, the following flags are available for each of the specifiers:
@@ -3029,8 +3030,11 @@ include some text that makes Denote buffers stand out, 
such as
 a =[D]= prefix.  Examples:
 
 #+begin_src emacs-lisp
-;; Use the title (default)
-(setq denote-rename-buffer-format "%t")
+;; Use the title prefixed with the backlink indicator (default)
+(setq denote-rename-buffer-format "%b%t")
+
+;; Customize what the backlink indicator looks like
+(setq denote-buffer-has-backlinks-string "!! ")
 
 ;; Use the title and keywords with some emoji in between
 (setq denote-rename-buffer-format "%t 🤨 %k")
diff --git a/denote-rename-buffer.el b/denote-rename-buffer.el
index 1335d3a22c..3e157f193d 100644
--- a/denote-rename-buffer.el
+++ b/denote-rename-buffer.el
@@ -38,11 +38,18 @@
   :link '(info-link "(denote) Top")
   :link '(url-link :tag "Homepage" "https://protesilaos.com/emacs/denote";))
 
-(defcustom denote-rename-buffer-format "%t"
+(defcustom denote-buffer-has-backlinks-string "→ "
+  "A string used to indicate that a buffer has backlinks pointing to it."
+  :type 'string
+  :package-version '(denote . "2.1.0")
+  :group 'denote-rename-buffer)
+
+(defcustom denote-rename-buffer-format "%b%t"
   "The format of the buffer name `denote-rename-buffer' should use.
 Thie value is a string that treats specially the following
 specifiers:
 
+- The %b inserts `denote-buffer-has-backlinks-string'.
 - The %t is the Denote TITLE of the file.
 - The %i is the Denote IDENTIFIER of the file.
 - The %d is the same as %i (DATE mnemonic).
@@ -91,20 +98,24 @@ 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)))
-    (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 ?i (or (denote-retrieve-filename-identifier 
file) ""))
-                        (cons ?d (or (denote-retrieve-filename-identifier 
file) ""))
-                        (cons ?s (or (denote-retrieve-filename-signature file) 
""))
-                        (cons ?k (if-let ((kws 
(denote-retrieve-front-matter-keywords-value file type)))
-                                     (denote-keywords-combine kws)
-                                   (or (denote-retrieve-filename-keywords 
file) "")))
-                        (cons ?% "%"))
-                  'delete))))
+    (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 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) ""))
+                          (cons ?k (if-let ((kws 
(denote-retrieve-front-matter-keywords-value file type)))
+                                       (denote-keywords-combine kws)
+                                     (or (denote-retrieve-filename-keywords 
file) "")))
+                          (cons ?% "%"))
+                    'delete)))))
 
 (defun denote-rename-buffer (&optional buffer)
   "Rename current buffer or optional BUFFER with `denote-rename-buffer-format'.
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