branch: externals/org-transclusion
commit 44b63720452a750c288f459cab30277d6557215b
Author: Noboru Ota <[email protected]>
Commit: Noboru Ota <[email protected]>

    fix id:<id>::<target> linking in the target same buffer #273
    
    This fixes the issue reported by k4r4b3y (on Sep 25)
    
        I have been testing this branch. I noticed that id:<id>::<target> 
linking
        doesn't work for the same-file targets.
    
    In addition, this improves  function `org-transclusion-add-target-marker`
    
    1. It now issues better error message when the link does not work
    
    2. It now does not re-configure the buffer and windows, which was 
disrupting for
       end users
---
 org-transclusion.el  | 52 +++++++++++++++++++++++++++++-----------------------
 test/273/test273.org | 23 +++++++++++++++++++++++
 test/test-2.0.org    |  2 ++
 3 files changed, 54 insertions(+), 23 deletions(-)

diff --git a/org-transclusion.el b/org-transclusion.el
index bb1beebd85..3cd696767b 100644
--- a/org-transclusion.el
+++ b/org-transclusion.el
@@ -17,7 +17,7 @@
 
 ;; Author:        Noboru Ota <[email protected]>
 ;; Created:       10 October 2020
-;; Last modified: 28 September 2025
+;; Last modified: 18 December 2025
 
 ;; URL: https://github.com/nobiot/org-transclusion
 ;; Keywords: org-mode, transclusion, writing
@@ -947,30 +947,41 @@ inserted when more than one space is inserted between 
symbols."
 ;;;; Add-at-point functions
 
 (defun org-transclusion-add-target-marker (link)
+  "Return the marker of transclusion target by opening LINK.
+LINK must be Org's link object that `org-link-open' can act on. As long
+as `org-link-open' opens a buffer within Emacs, this function should
+return a marker."
   ;; Assume the point now is the transcluding buffer
   (let ((cur-buf (current-buffer))
         (cur-marker (move-marker (make-marker) (line-beginning-position))))
-    (save-excursion
-      ;; Don't ever prompt to create a headline when transcluding.
-      ;; t is a less surprising default than nil - fuzzy search.
-      (let ((org-link-search-must-match-exact-headline t))
-        (condition-case nil
-            (progn
-              (org-link-open link)
-              ;; In the target buffer temporarily. If the target and source are
-              ;; the same buffer, do not move the point and return the curent
-              ;; maker.
-              (if (eq cur-buf (current-buffer)) cur-marker
-                (move-marker (make-marker) (point))))
-          ;; TODO add more link info
-          (error (message "Cannot open link")))))))
+    ;; Note 2025-12-18 `org-link-open' does not necessarily obey
+    ;; `display-buffer-alist' and can open the target buffer in the currently
+    ;; selected window. This is disruptive for users. We want transclusions to
+    ;; keep the current buffer in the current window. To do this, it seems
+    ;; `save-window-excursion' is the only way.
+    (save-window-excursion
+      ;; This `save-excursion' is needed for the case where the target and
+      ;; source are the same buffer.
+      (save-excursion
+        ;; Don't ever prompt to create a headline when transcluding.
+        ;; t is a less surprising default than nil - fuzzy search.
+        (let ((org-link-search-must-match-exact-headline t))
+          (condition-case nil
+              (progn
+                (org-link-open link)
+                ;; In the target buffer temporarily.
+                (save-excursion
+                  (move-marker (make-marker) (point))))
+            (error (user-error
+                    "Org-transclusion: `org-link-open' cannot open link, %s"
+                    (org-element-property :raw-link link)))))))))
 
 (defun org-transclusion-add-org-id (link plist)
   "Return a list for Org-ID LINK object and PLIST.
 Return nil if not found."
   (and-let*
       ((_ (string= "id" (org-element-property :type link)))
-       (mkr (ignore-errors (org-transclusion-add-target-marker link)))
+       (mkr (org-transclusion-add-target-marker link))
        (buf (marker-buffer mkr))
        (_ (buffer-live-p (marker-buffer mkr))))
     (with-current-buffer buf
@@ -1000,14 +1011,9 @@ Return nil if not found."
   ;; already open with a point. If the search option is present, the point will
   ;; move to the appropriate point and get the element. If the search option is
   ;; not present, the whole buffer needs to be obtained.
-             (mkr (ignore-errors (org-transclusion-add-target-marker link)))
+             (mkr (org-transclusion-add-target-marker link))
              (buf (marker-buffer mkr)))
-    ;; FIXME `org-transclusion-add-target-marker'
-    ;;
-    ;; - Change name. It's not just for org file being targeted.
-
     ;; - Silly to go back to the buffer here.
-
     ;; - `org-transclusion-content-org-filtered' should not return other
     ;;   properties -- confusing.
     (with-current-buffer buf
@@ -1027,7 +1033,7 @@ Return nil if not found."
   "Return a list for non-Org file LINK object and PLIST.
 Return nil if not found."
   (and-let* (;; (_ (string= "file" (org-element-property :type link)))
-             (mkr (ignore-errors (org-transclusion-add-target-marker link)))
+             (mkr (org-transclusion-add-target-marker link))
              (buf (marker-buffer mkr)))
     ;; FIXME It's silly to revisit the buffer when it was already visited.
     (with-current-buffer buf
diff --git a/test/273/test273.org b/test/273/test273.org
new file mode 100644
index 0000000000..4f472d7fa0
--- /dev/null
+++ b/test/273/test273.org
@@ -0,0 +1,23 @@
+* 08:49 Example of learning human languages via anki and youtube 
:german:language:journals:2025_09_25:
+:PROPERTIES:
+:CREATED: [2025-09-25 Thu 08:49]
+:ID:       2025-12-18T060626
+:END:
+
+The [[https://cjauvin.github.io/posts/learning-persian/][blog post]] is short 
and simple.  However, showcases an effective
+method of using youtube and spaced repetition with langauage learning.
+The gist of it is that <<gist>>
+
+* 08:53 Showcasing in-file targeting with org-transclusion
+:PROPERTIES:
+:ID: d697a00d-62b9-4d49-8196-b1ebadfdccda
+:CREATED: [2025-09-25 Thu 08:53]
+:END:
+
+See this one:
+
+#+transclude: [[id:2025-12-18T060626::gist]]
+
+or this one:
+
+#+transclude: [[id:ccf3c642-fbef-4485-a21b-e83784c3303f::test]]
diff --git a/test/test-2.0.org b/test/test-2.0.org
index 04051e31e3..78050504df 100644
--- a/test/test-2.0.org
+++ b/test/test-2.0.org
@@ -20,6 +20,8 @@ This is the first section before the first headline.
    #+transclude: [[file:paragraph.org::non-exisitent]]
    => Should not transclude anything
 
+#+transclude: [[paragraph.tx]]
+
 ** make from link -- ID and ID with search option
 This is a link to a [[id:2022-05-30T203553][Bertrand Russell]] wikipedia 
excerpt
 #+transclude: [[id:2022-05-30T203553][Bertrand Russell]]

Reply via email to