4

It's basically "What links here?".

Using Org mode links, I'd like to be able to get the list of headings which contain a link to a specific heading.

Example file:

* Target
  :PROPERTIES:
  :ID: 509039F8-5B43-4C9B-8FA3-F37E3A2EEC10
  :END:

* Heading containing link

  id:509039F8-5B43-4C9B-8FA3-F37E3A2EEC10

* Another heading containing the same link

  id:509039F8-5B43-4C9B-8FA3-F37E3A2EEC10

When the cursor is at Target, I'd like to get the list of all headings that link to it:

* Heading containing link
* Another heading containing the same link
1
  • This should be enough to get you started: (let ((id (org-entry-get (point) "ID")) result) (save-excursion (goto-char (point-min)) (while (re-search-forward (concat "id:" id) nil t) (push (org-get-heading) result)) result)) You can look at org-id-update-id-locations to see how to get a listing of all files that may contain ID and you can loop through each file. You can create a variation of org-get-heading if you need something a little different -- e.g., gather the stars, or use match-string-no-properties if the text properties are not to your liking.
    – lawlist
    Commented Feb 20, 2017 at 23:26

2 Answers 2

1

A different approach in a single file is below. It avoids the direct use of regular expressions.

(defun get-headings-with-links ()
  (let ((tid (org-id-get)))
    (org-element-map (org-element-parse-buffer) 'link
      (lambda (link)
    (when (and (string= (org-element-property :type link) "id")
           (string= (org-element-property :path link) tid))
      (save-excursion
        (goto-char (org-element-property :begin link))
        (org-get-heading)))))))
0

I ended up simply using:

(defun what-links-here ()
  (interactive)
  (let ((id (org-entry-get nil "ID")))
    (if (> (length id) 0)
        (org-search-view nil (concat "id:" id))
      (message "No ID - nothing can be linking here"))))
1
  • Result is exactly what I needed, but perhaps there's room for improvement.
    – ooi
    Commented Feb 22, 2017 at 5:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.