Refactor org link formatting functions to avoid duplicate code

This commit is contained in:
Daniel - 2022-05-20 19:34:56 +02:00
parent 2c01e1e09c
commit de314f6ac2
No known key found for this signature in database
GPG Key ID: 1C7071A75BB72D64
1 changed files with 31 additions and 28 deletions

View File

@ -882,34 +882,20 @@ item using `db/org-get-location', which see."
(db/org-get-location))) (db/org-get-location)))
(list (org-id-get) (org-entry-get nil "CUSTOM_ID"))))) (list (org-id-get) (org-entry-get nil "CUSTOM_ID")))))
(defun db/org--format-link-with-headline (id) (defun db/org--format-link-from-pom (pom)
"Format ID as an Org mode link [[ID][item headline]]. "Return Org link pointing to Org item at POM.
If the headline of the item pointed to by ID contains any links, POM must be point or mark to a valid Org item. The link will be
those are replaced by their description before formatting." of the format [[id][item-headline]], where `id' is the value of
(let ((item-headline (org-entry-get (org-id-find id 'marker) "ITEM"))) the ID property of the item. If the item does not have such a
property, is is generated automatically.
;; When item-headline contains links, replace them by teir description (when If `item-headline' contains any links itself, those will be
;; available); otherwise use the link part only replaced by the description when available, and otherwise by
;; FIXME: this is code duplicated from `db/org-insert-link-to-pom' their plain link part."
(save-match-data (unless (or (markerp pom) (integerp pom))
(while (string-match org-link-bracket-re item-headline) (user-error "POM must be point or mark"))
(let ((desc (or (match-string-no-properties 2 item-headline)
(match-string-no-properties 1 item-headline))))
(setq item-headline (concat (substring item-headline 0 (match-beginning 0))
desc
(substring item-headline (match-end 0)))))))
(org-link-make-string (format "id:%s" id) item-headline)))
(defun db/org-insert-link-to-pom (pom)
"Insert an Org link to headline at POM.
If headline consists of a link with description, only the
description of that link will be included in the description of
the newly inserted link instead of the complete headline. This
avoids containing a link in the description of the newly inserted
link."
(let (item id) (let (item id)
(org-with-point-at pom (org-with-point-at pom
(setq item (org-entry-get (point) "ITEM") (setq item (org-entry-get (point) "ITEM")
@ -925,7 +911,24 @@ link."
desc desc
(substring item (match-end 0))))))) (substring item (match-end 0)))))))
(org-insert-link nil (format "id:%s" id) item))) (org-link-make-string (format "id:%s" id) item)))
(defun db/org--format-link-from-org-id (id)
"Format ID as an Org mode link [[ID][item-headline]].
If the headline of the item pointed to by ID contains any links,
those are replaced by their description before formatting."
(db/org--format-link-from-pom (org-id-find id 'marker)))
(defun db/org-insert-link-to-pom (pom)
"Insert an Org link to headline at POM.
If headline consists of a link with description, only the
description of that link will be included in the description of
the newly inserted link instead of the complete headline. This
avoids containing a link in the description of the newly inserted
link."
(insert (db/org--format-link-from-pom pom)))
(defun db/org-add-link-to-other-item (arg) (defun db/org-add-link-to-other-item (arg)
"Interactively query for item and add link to it at point. "Interactively query for item and add link to it at point.
@ -1062,9 +1065,9 @@ PARAMS may contain the following values:
(insert (format "| Item | Backlinks | Priority |\n|---|")) (insert (format "| Item | Backlinks | Priority |\n|---|"))
(dolist (headline headlines) (dolist (headline headlines)
(when (cdr headline) ; do not print backlinks if there are none (when (cdr headline) ; do not print backlinks if there are none
(insert (format "\n| %s |\n|---|" (db/org--format-link-with-headline (car headline)))) (insert (format "\n| %s |\n|---|" (db/org--format-link-from-org-id (car headline))))
(let ((backlink-lines (-> (mapcar #'(lambda (backlink-id) (let ((backlink-lines (-> (mapcar #'(lambda (backlink-id)
(list (db/org--format-link-with-headline backlink-id) (list (db/org--format-link-from-org-id backlink-id)
(org-entry-get (org-id-find backlink-id 'marker) (org-entry-get (org-id-find backlink-id 'marker)
"PRIORITY"))) "PRIORITY")))
(cdr headline)) (cdr headline))