Refactor Org Mode link finder function

The main entry point is now `db/org-find-links-to-current-item', which decides
how to obtain the ID and CUSTOM_ID of the item to look for.  The main work is
done by `db/org-find-items-linking-to-id', which does some checks, build the
query, and then calls `org-search-view' (which, indeed, does the actual work).
Users should call `db/org-find-links-to-current-item' only.
This commit is contained in:
Daniel - 2020-09-26 15:20:11 +02:00
parent c219d77ab9
commit b0a36749e6
No known key found for this signature in database
GPG Key ID: 1C7071A75BB72D64
2 changed files with 21 additions and 14 deletions

View File

@ -716,7 +716,7 @@
db/org-mark-current-default-task
db/export-diary
db/org-copy-template-for-periodic-task
db/org-find-items-linking-to-id))
db/org-find-links-to-current-item))
(use-package org
:pin "gnu"

View File

@ -595,22 +595,13 @@ 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-by-id (&optional id custom-id)
(defun db/org-find-items-linking-by-id (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', and CUSTOM-ID
must consist of ASCII letters, numbers, and hyphens only. When
called interactively in Org Mode, use the values ID and CUSTOM_ID
of the current item. When called interactively outside of Org
Mode, prompt the user for the user for a headline."
(interactive (if (derived-mode-p 'org-mode)
(list (org-id-get) (org-entry-get nil "CUSTOM_ID"))
(let ((pom (nth 3 (org-refile-get-location))))
(if (not pom)
(user-error "Invalid location")
(org-with-point-at pom
(list (org-id-get) (org-entry-get nil "CUSTOM_ID")))))))
must consist of ASCII letters, numbers, and hyphens only. Each
of ID and CUSTOM-ID may be nil, but at least one of them must be
not."
(unless (or (not id)
(and (stringp id)
(string-match-p "^[a-f0-9]\\{8\\}-[a-f0-9]\\{4\\}-[a-f0-9]\\{4\\}-[a-f0-9]\\{4\\}-[a-f0-9]\\{12\\}$" id)))
@ -628,6 +619,22 @@ Mode, prompt the user for the user for a headline."
(t (user-error "No ID given and not in Org Mode.")))))
(org-search-view nil query)))
(defun db/org-find-links-to-current-item (arg)
"Find links to current item.
Only links using the ID or CUSTOM_ID property are considered.
If ARG is given, or if not in an Org Mode buffer, interactively
prompt for an item."
(interactive "P")
(apply #'db/org-find-items-linking-by-id
(if (and (derived-mode-p 'org-mode) (not arg))
(list (org-id-get) (org-entry-get nil "CUSTOM_ID"))
(let ((pom (nth 3 (org-refile-get-location))))
(if (not pom)
(user-error "Invalid location")
(org-with-point-at pom
(list (org-id-get) (org-entry-get nil "CUSTOM_ID"))))))))
;;; End