Custom link type
The manual has some more information on custom types, for example customization options. Note that the current guide uses plists to define the link type via org-add-link-type
, but that's a recent(?) addition. I opted for the earlier, still valid syntax from 9.1.
Advice for org-link-open
This advice was tested in Org version 9.3.6 and 9.4-19. It will definitely not work in 9.1.9. Other versions are untested.
(defun my-org-expand-file-link (link)
"Expand a pattern in LINK to the first matching file.
Returns the original LINK if no file matches the pattern in LINK or if it is not
a link of type 'file:'. See info node `(org)External links' for more information."
(let ((type (org-element-property :type link))
(path (org-element-property :path link)))
(cond
((equal type "file")
(let ((candidates (file-expand-wildcards path)))
(if candidates
(org-element-put-property (org-element-copy link) :path (car candidates))
link)))
(t link))))
(defun my-org-link-open-advice (orig-fun link &rest args)
"Advice for ORIG-FUN that expands patterns in 'file:' LINKs.
See `my-org-expand-file-link' for more information.
Optional argument ARGS are passed as-is."
(apply orig-fun (my-org-expand-file-link link) args))
(advice-add 'org-link-open :around #'my-org-link-open-advice)