*  lisp/ol.el (org-link-precise-link-target): Store context when
   storing a link on a target.

This makes it possible to restore a link using ID and anchor when
org-id-link-to-org-use-id is true.

Reported-by: "Christian Barthel" <[email protected]
Link: https://list.orgmode.org/[email protected]/
---
 etc/ORG-NEWS            |  7 +++++++
 lisp/ol.el              | 21 ++++++---------------
 testing/lisp/test-ol.el | 24 ++++++++++++++++++++++++
 3 files changed, 37 insertions(+), 15 deletions(-)

diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 2dcd86aee..e2733d33d 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -47,6 +47,13 @@ the correct ~(FILE . ...)~ cons.
 # We list the most important features, and the features that may
 # require user action to be used.
 
+*** Use Property ID in ~org-link-precise-link-target~ when available
+
+When storing a link to an Org-Mode anchor, use the Org-Mode ID
+Property when available and ~org-id-link-to-org-use-id~ is set to
+true.  Restoring the link combines the ID and the anchor forming a
+unique place (when ID is unique).
+
 *** New actions in the ~org-mouse~ priority menus
 
 Priorities can now be increased, decreased, set to the default, and
diff --git a/lisp/ol.el b/lisp/ol.el
index 5fdff1de8..89cb2955d 100644
--- a/lisp/ol.el
+++ b/lisp/ol.el
@@ -1960,9 +1960,14 @@ matches."
            ((derived-mode-p 'org-mode)
             (let* ((element (org-element-at-point))
                    (name (org-element-property :name element))
+                   (context (org-element-context element))
                    (heading (org-element-lineage element '(headline 
inlinetask) t))
                    (custom-id (org-entry-get heading "CUSTOM_ID")))
               (cond
+               ((org-element-type-p context 'target)
+                (list (org-element-property :value context)
+                      (org-element-property :value context)
+                      (org-element-begin context)))
                (name
                 (list name
                       name
@@ -2653,24 +2658,10 @@ NAME."
        ;; buffers
        ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 
'org-mode))
        (org-with-limited-levels
-        (cond
-         ;; Store a link using the target at point
-         ((org-in-regexp "[^<]<<\\([^<>]+\\)>>[^>]" 1)
-          (setq link
-                (concat "file:"
-                        (abbreviate-file-name
-                         (buffer-file-name (buffer-base-buffer)))
-                        "::" (match-string 1))
-                 ;; Target may be shortened when link is inserted.
-                 ;; Avoid [[target][file:~/org/test.org::target]]
-                 ;; links.  Maybe the case of identical target and
-                 ;; description should be handled by `org-insert-link'.
-                 desc nil))
-          (t
           ;; Just link to current headline.
            (let ((here (org-link--file-link-to-here)))
              (setq link (car here))
-             (setq desc (cdr here)))))))
+             (setq desc (cdr here)))))
 
        ;; Buffer linked to file, but not an org-mode buffer.
        ((buffer-file-name (buffer-base-buffer))
diff --git a/testing/lisp/test-ol.el b/testing/lisp/test-ol.el
index 75bd537c0..331a22a1f 100644
--- a/testing/lisp/test-ol.el
+++ b/testing/lisp/test-ol.el
@@ -411,6 +411,30 @@ See https://github.com/yantar92/org/issues/4.";
 
 (ert-deftest test-org-link/id-store-link ()
   "Test `org-id-store-link' specifications."
+   (let ((org-id-link-to-org-use-id t))
+     (should
+      (equal '("id:abc::myanchor" "myanchor")
+             (test-ol-stored-link-with-text "* H1\n:PROPERTIES:\n:ID: 
abc\n:END:\n<point><<myanchor>>\n"
+               (org-id-store-link))))
+     )
+   (let ((org-id-link-to-org-use-id t))
+     (should
+      (equal '("id:abc" "H1")
+             (test-ol-stored-link-with-text "<point>* H1\n:PROPERTIES:\n:ID: 
abc\n:END:\n<<myanchor>>\n"
+               (org-id-store-link))))
+     )
+   (let ((org-id-link-to-org-use-id t))
+     (should
+      (equal '("id:abc" "H1")
+             (test-ol-stored-link-with-text "* H1\n:PROPERTIES:\n:ID: 
abc\n:END:\n<<myanchor>>\n<point>"
+               (org-id-store-link))))
+     )
+   (let ((org-id-link-to-org-use-id nil))
+     (should
+      (equal '("id:abc::myanchor" "myanchor")
+             (test-ol-stored-link-with-text "* H1\n:PROPERTIES:\n:ID: 
abc\n:END:\n<point><<myanchor>>\n"
+               (org-id-store-link))))
+     )
   (let ((org-id-link-to-org-use-id nil))
     (should
      (equal '(nil nil)
-- 
2.47.3

Reply via email to