Skip to content

Instantly share code, notes, and snippets.

@lucasvreis
Last active January 8, 2023 20:17
Show Gist options
  • Save lucasvreis/24f378f6f9a95f8c17906cb351bab2e1 to your computer and use it in GitHub Desktop.
Save lucasvreis/24f378f6f9a95f8c17906cb351bab2e1 to your computer and use it in GitHub Desktop.
support [[attach:file::anchor]] and [[id:something::anchor]] links in org-transclusion
(defun org-transclusion-add-better-id (link plist)
"Return a list for Org-ID LINK object and PLIST.
Return nil if not found."
(when (string= "id" (org-element-property :type link))
;; when type is id, the value of path is the id
(let* ((both (split-string (org-element-property :path link) "::"))
(id (cl-first both))
(search (cl-second both))
(mkr (ignore-errors (org-id-find id t)))
(payload '(:tc-type "org-id")))
(if mkr
(append payload (org-transclusion-content-better-marker mkr search plist))
(message
(format "No transclusion done for this ID. Ensure it works at point %d, line %d"
(point) (org-current-line)))
nil))))
(defun org-transclusion-content-better-marker (marker search plist)
"Return a list of payload from MARKER and PLIST.
This function is intended to be used for Org-ID. It delates the
work to
`org-transclusion-content-org-buffer-or-element'."
(if (and marker (marker-buffer marker)
(buffer-live-p (marker-buffer marker)))
(progn
(with-current-buffer (marker-buffer marker)
(org-with-wide-buffer
(goto-char marker)
(when search
(org-link-search search))
(if (and (not search) (org-before-first-heading-p))
(org-transclusion-content-org-buffer-or-element
nil plist)
(org-transclusion-content-org-buffer-or-element
'only-element plist)))))
(message "Nothing done. Cannot find marker for the ID.")))
;; Also support attach: links
(defun org-transclusion-add-org-attach (link plist)
"Return a list for attached file LINK object and PLIST.
Return nil if not found."
(when (string= "attachment" (org-element-property :type link))
(let* ((both (split-string (org-element-property :path link) "::"))
(path (org-attach-expand (cl-first both)))
(search (cl-second both))
(link_ (org-element-put-property link :path path))
(link__ (org-element-put-property link_ :search-string search)))
(or (org-transclusion-add-org-file link__ plist)
(org-transclusion-add-other-file link__ plist)))))
(after! org-transclusion
(setq! org-transclusion-add-functions
'(org-transclusion-add-src-lines
org-transclusion-add-better-id
org-transclusion-add-org-attach
org-transclusion-add-org-file
org-transclusion-add-other-file)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment