From 268ddeb1aaa55202dc16ab59c9acc20176675537 Mon Sep 17 00:00:00 2001 From: Daniel Borchmann Date: Wed, 12 May 2021 17:47:45 +0200 Subject: [PATCH] Avoid complete links in descriptions when adding links to Org items When inserting links to other Org items or to the currently clocked-in item, the complete target headline is used as a description in the newly inserted link. When that target headline is itself a link, the newly inserted link will contain the complete link as a description, rendering it unreadable and also malformed (it's not allowed to have two consequtive brackets in the description of a link). To remedy this, we now explicitly check the target headline for being a link, and if so, only use the description of it as the description in the newly inserted link. --- site-lisp/db-org.el | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/site-lisp/db-org.el b/site-lisp/db-org.el index 9fece55..5db1c4b 100644 --- a/site-lisp/db-org.el +++ b/site-lisp/db-org.el @@ -722,6 +722,28 @@ item." (org-with-point-at (db/org--get-location) (list (org-id-get) (org-entry-get nil "CUSTOM_ID"))))))) +(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) + (org-with-point-at pom + (setq item (org-entry-get (point) "ITEM") + id (org-id-get-create))) + + ;; When item is a link, only use it's description when available; otherwise + ;; use the link part + (save-match-data + (when (string-match org-link-bracket-re item) + (setq item (or (match-string-no-properties 2 item) + (match-string-no-properties 1 item))))) + + (org-insert-link nil (format "id:%s" id) item))) + (defun db/org-add-link-to-other-item (arg) "Interactively query for item and add link to it at point. @@ -733,12 +755,7 @@ variables `org-agenda-files' and (interactive "P") (unless (derived-mode-p 'org-mode) (user-error "Not in Org Mode")) - (let* ((pom (db/org--get-location arg)) - item id) - (org-with-point-at pom - (setq item (org-entry-get (point) "ITEM") - id (org-id-get-create))) - (insert (format "[[%s][%s]]" id item)))) + (db/org-insert-link-to-pom (db/org--get-location arg))) (defun db/org-add-link-to-current-clock () "Insert link to currently clocked-in item at point. @@ -749,11 +766,7 @@ active." (user-error "Not in Org Mode, aborting")) (unless org-clock-marker (user-error "No clocked-in task, aborting")) - (let (item id) - (org-with-point-at org-clock-marker - (setq item (org-entry-get (point) "ITEM") - id (org-id-get-create))) - (insert (format "[[%s][%s]]" id item)))) + (db/org-insert-link-to-pom org-clock-marker)) (defhydra hydra-org-linking (:color blue :hint none) "