Add optional command to sshow backlinks for an individual Org heading

Thanks to Harold Ollivier for suggesting this idea in issue 297:
<https://github.com/protesilaos/denote/issues/297>.
This commit is contained in:
Protesilaos Stavrou 2024-04-20 21:00:30 +03:00
parent 2a24919055
commit 7a16d7bb69
No known key found for this signature in database
GPG key ID: 99BD6459CD5CA3EA
2 changed files with 39 additions and 1 deletions

View file

@ -2425,6 +2425,20 @@ Reproducing it here for the sake of convenience:
(window-width . 0.3)))
#+end_src
*** Backlinks for Org headings
:PROPERTIES:
:CUSTOM_ID: h:604bf92a-908a-485c-98b8-37ccae559afd
:END:
[ This is part of {{{development-version}}}. ]
The optional =denote-org-extras.el= can produce Denote links to
individual headings ([[#h:fc1ad245-ec08-41be-8d1e-7153d99daf02][Insert link to an Org file with a further pointer to a heading]]).
It is then possible to produce a corresponding backlinks buffer with
the command ~denote-org-extras-backlinks-for-heading~. The resulting
buffer behaves the same way as the standard backlinks buffer we
provide ([[#h:c73f1f68-e214-49d5-b369-e694f6a5d708][The backlinks' buffer]]).
** Writing metanotes
:PROPERTIES:
:CUSTOM_ID: h:6060a7e6-f179-4d42-a9de-a9968aaebecc

View file

@ -102,7 +102,9 @@ to the current heading. Such links look the same as those of
this command, though the functionality defined herein is
independent of it.
To only link to a file, use the `denote-link' command."
To only link to a file, use the `denote-link' command.
Also see `denote-org-extras-backlinks-for-heading'."
(declare (interactive-only t))
(interactive nil org-mode)
(unless (derived-mode-p 'org-mode)
@ -117,6 +119,28 @@ To only link to a file, use the `denote-link' command."
(description (denote-link-format-heading-description file-text heading-text)))
(insert (denote-org-extras-format-link-with-heading file heading-id description))))
;;;; Heading backlinks
(defun denote-org-extras--get-file-id-and-heading-id (file)
"Return IDENTIFIER::#ORG-HEADING-CUSTOM-ID string for FILE heading at point."
(if-let ((heading-id (org-entry-get (point) "CUSTOM_ID")))
(concat (denote-retrieve-filename-identifier-with-error file) "::#" heading-id)
(error "No CUSTOM_ID for heading at point in file `%s'" file)))
;;;###autoload
(defun denote-org-extras-backlinks-for-heading ()
"Produce backlinks for the current heading.
This otherwise has the same behaviour as `denote-backlinks'---refer to
that for the details.
Also see `denote-org-extras-link-to-heading'."
(interactive)
(when-let ((heading-id (denote-org-extras--get-file-id-and-heading-id buffer-file-name)))
(denote-link--prepare-backlinks
(apply-partially #'xref-matches-in-files heading-id
(denote-directory-files nil :omit-current :text-only))
nil heading-id)))
;;;; Extract subtree into its own note
(defun denote-org-extras--get-heading-date ()