branch: externals/org-transclusion
commit 9a49f9c0288a2aa26b5ada66d949db9d8e135e41
Author: Noboru Ota <m...@nobiot.com>
Commit: Noboru Ota <m...@nobiot.com>

    refactor: add org-transclusion-at-keyword-p
---
 org-transclusion.el | 51 ++++++++++++++++++++++++++++++++++++---------------
 test/test-2.0.org   |  1 +
 2 files changed, 37 insertions(+), 15 deletions(-)

diff --git a/org-transclusion.el b/org-transclusion.el
index db9a271100..6c83a1c50e 100644
--- a/org-transclusion.el
+++ b/org-transclusion.el
@@ -1389,21 +1389,19 @@ Currently the following cases are prevented:
 Case 1. Element at point is NOT #+transclude:
         Element is in a block - e.g. example
 Case 2. #+transclude inside another transclusion"
-  (let ((elm (org-element-at-point)))
-    (cond
-     ;; Case 1. Element at point is NOT #+transclude:
-     ((not (and (string-equal "keyword" (org-element-type elm))
-                (string-equal "TRANSCLUDE" (org-element-property :key elm))))
-      (user-error
-       "Not at a transclude keyword or transclusion in a block at point %d, 
line %d"
-       (point) (org-current-line)))
-     ;; Case 2. #+transclude inside another transclusion
-     ((org-transclusion-within-transclusion-p)
-      (user-error
-       "Cannot transclude in another transclusion at point %d, line %d"
-       (point) (org-current-line)))
-     (t
-      t))))
+  (cond
+   ;; Case 1. Element at point is NOT #+transclude:
+   ((not (org-transclusion-at-keyword-p))
+    (user-error
+     "Not at a transclude keyword or transclusion in a block at point %d, line 
%d"
+     (point) (org-current-line)))
+   ;; Case 2. #+transclude inside another transclusion
+   ((org-transclusion-within-transclusion-p)
+    (user-error
+     "Cannot transclude in another transclusion at point %d, line %d"
+     (point) (org-current-line)))
+   (t
+    t)))
 
 (defun org-transclusion-fix-common-misspelling ()
   "Fix \"#+transclude\" by appending a colon \":\".
@@ -1467,6 +1465,29 @@ used."
                (beg (prop-match-beginning prop-match-backward)))
       (list :id id :location (cons beg end)))))
 
+(defun org-transclusion-at-keyword-p ()
+  "Return non-nil if the current line is on #+TRANSCLUDE: keyword."
+  ;;
+  ;; BUG (I believe): The following edge case is considered part of keyword
+  ;; where "|" is the cursor.
+  ;;
+  ;; Avoid the following situation to be recognized as "t"
+  ;;
+  ;;   #+transclude: [[link]]
+  ;;   |
+  ;;   New paragraph starts
+  (let ((edge-case-p
+         (save-excursion
+           (and (looking-at-p "$")
+                (not (bobp))
+                (progn (forward-char -1)
+                       (looking-at-p "$")))))
+        (element (org-element-at-point)))
+    ;; If edge-case, do not transclude.
+    (unless edge-case-p
+      (and (string-equal "keyword" (org-element-type element))
+           (string-equal "TRANSCLUDE" (org-element-property :key element))))))
+
 (defun org-transclusion-within-transclusion-p ()
   "Return t if the current point is within a transclusion region."
   (when (get-char-property (point) 'org-transclusion-type) t))
diff --git a/test/test-2.0.org b/test/test-2.0.org
index abe11926f4..230b2b9a20 100644
--- a/test/test-2.0.org
+++ b/test/test-2.0.org
@@ -7,6 +7,7 @@ This is a link to a [[id:2022-05-30T203553][Bertrand Russell]] 
wikipedia excerpt
 
 ** test text
 
+#+transclude: [[file:test.txt][text file]]
 #+transclude: [[file:test.txt][text file]]
 
 Below are tesing the new ~org-transclusion-fix-common-misspelling~ function.

Reply via email to