Extend backward link searching function

Now support CUSTOM_ID as well, and check arguments for plausibility.
This commit is contained in:
Daniel - 2020-09-26 15:00:34 +02:00
parent a6f77f8d71
commit cde56c819e
No known key found for this signature in database
GPG Key ID: 1C7071A75BB72D64
1 changed files with 22 additions and 8 deletions

View File

@ -595,16 +595,30 @@ This is done only if the value of this variable is not null."
;;; Find items by link to current headline
(defun db/org-find-items-linking-to-id (&optional id)
;; TODO: expand: let the user choose the item that is to be linked to
;; interactively
(defun db/org-find-items-linking-by-id (&optional id custom-id)
"List all Org Mode items that link to ID.
Uses `org-search-view' to conduct the actual search. ID must be
a UUID as generated by, e.g., `org-id-get-create'. If ID is not
given, use the ID of the current item, if in Org Mode."
(interactive (list (when (derived-mode-p 'org-mode)
(org-id-get))))
(if id
(org-search-view nil (format "[id:%s]" id))
(user-error "No ID given and not in Org Mode.")))
a UUID as generated by, e.g., `org-id-get-create', and CUSTOM-ID
must consist of ASCII letters, numbers, and hyphens only. If ID
and CUSTOM-ID are not given, use the values of the current item
if in Org Mode."
(interactive (when (derived-mode-p 'org-mode)
(list (org-id-get) (org-entry-get nil "CUSTOM_ID"))))
(unless (string-match-p "^[a-f0-9]\\{8\\}-[a-f0-9]\\{4\\}-[a-f0-9]\\{4\\}-[a-f0-9]\\{4\\}-[a-f0-9]\\{12\\}$" id)
(user-error "Given ID is not a valid UUID: %s" id))
(unless (string-match-p "[-a-zA-Z0-9]" custom-id)
;; sorry, only ASCII right now …
(user-error "CUSTOM_ID must consist of alphanumeric charaters only"))
(let ((query (cond
((and id custom-id) (format "{\\[\\[id:%s\\]\\|\\[\\[#%s\\]}" id custom-id))
(id (format "[[id:%s]" id))
(custom-id (format "[[#%s]" custom-id))
(t (user-error "No ID given and not in Org Mode.")))))
(org-search-view nil query)))
;;; End