branch: externals/denote commit 3b62f8e167f3b0f4d57cffc91f99bcab24103cf0 Author: Protesilaos Stavrou <i...@protesilaos.com> Commit: Protesilaos Stavrou <i...@protesilaos.com>
Implement denote-link-clear-stale-backlinks --- denote-link.el | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/denote-link.el b/denote-link.el index c0875cf463..4c2674aac6 100644 --- a/denote-link.el +++ b/denote-link.el @@ -66,7 +66,10 @@ Both are supplied by `denote-link'." "Format of Org link to note.") (defconst denote-link--backlink-format "[[denote:%s][backlink: %s (%s)]]" - "Format of Org link to note.") + "Format of Org backlink to note.") + +(defconst denote-link--backlink-regexp "\\[\\[denote:\\(.*?\\)\\]\\[backlink: \\(.*?\\) (\\(.*?\\))\\]\\]" + "Regexp of `denote-link--backlink-format'.") (defun denote-link--retrieve-value (note regexp) "Return REGEXP value from NOTE." @@ -100,9 +103,6 @@ Run `denote-link-insert-functions' afterwards." (insert target-link) (run-hook-with-args 'denote-link-insert-functions target origin-link))) -;; TODO 2022-06-07: have a `denote-unlink' command or a -;; `denote-clean-backlinks' for invalid links. - (defconst denote-link-backlink-heading "Denote backlinks" "String of the backlink's heading. This heading is appended to a file when another links to it.") @@ -125,5 +125,16 @@ This heading is appended to a file when another links to it.") (delete-duplicate-lines heading-point (point-max) nil nil t) (widen))))) +(defun denote-link-clear-stale-backlinks () + "Delete backlinks that no longer point to files." + (interactive) + (let ((default-directory (denote--directory))) + (save-excursion + (goto-char (point-min)) + (when (re-search-forward denote-link-backlink-heading nil t)) + (while (re-search-forward denote-link--backlink-regexp nil t) + (unless (file-exists-p (match-string-no-properties 1)) + (delete-line)))))) + (provide 'denote-link) ;;; denote-link.el ends here