Skip to content

Commit

Permalink
refactor: (#177) workaround org-transclusion-fix-common-misspelling
Browse files Browse the repository at this point in the history
It is a common mistake for users to omit the colon. It is a workaround to
minimize the chance for users experience the known infinite issue. Refer to
issue #177 on the GitHub repository:
#177."
  • Loading branch information
nobiot committed Apr 20, 2024
1 parent 0e35273 commit 97c5a35
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
37 changes: 34 additions & 3 deletions org-transclusion.el
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,8 @@ does not support all the elements.
\\{org-transclusion-map}"
(interactive "P")
(when (org-transclusion-check-add)
(when (progn (org-transclusion-fix-common-misspelling)
(org-transclusion-check-add))
;; Turn on the minor mode to load extensions before staring to add.
(unless org-transclusion-mode
(let ((org-transclusion-add-all-on-activate nil))
Expand Down Expand Up @@ -1398,8 +1399,8 @@ Case 2. #+transclude inside another transclusion"
(let ((elm (org-element-at-point)))
(cond
;; Case 1. Element at point is NOT #+transclude:
((not (and (string= "keyword" (org-element-type elm))
(string= "TRANSCLUDE" (org-element-property :key elm))))
((not (and (string-equal "keyword" (org-element-type elm))
(string-equal "TRANSCLUDE" (org-element-property :key elm))))
(user-error (format "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
Expand All @@ -1409,6 +1410,36 @@ Case 2. #+transclude inside another transclusion"
(t
t))))

(defun org-transclusion-fix-common-misspelling ()
"Fix \"#+transclude\" by appending a colon \":\".
When `org-element-at-point' is a paragraph and the first string
of the line after spaces and tabs is \"transclude\", this
function appends a colon \":\". This function does not change the
case, so both \"#+TRANSCLUDE\" and \"#+transclude\" work and the
case will be kept unchanged.
It is a common mistake for users to omit the colon. It is a
workaround to minimize the chance for users experience the known
infinite issue. Refer to issue #177 on the GitHub repository:
https://github.com/nobiot/org-transclusion/issues/177."
(let ((elm (org-element-at-point)))
(when (string-equal "paragraph" (org-element-type elm))
(save-excursion
(save-match-data
(let ((bol (line-beginning-position))
(eol (line-end-position))
(case-fold-search t))
(goto-char bol)
(when (and (re-search-forward "^[[:blank:]]*#\\+\\(\\S-*\\)" eol :noerror)
(string-equal-ignore-case "transclude" (match-string-no-properties 1)))
(replace-match
(concat (match-string-no-properties 1) ":")
t nil nil 1)
;; return t when the string replaced
(message "A colon \":\" added to \"#+TRANSCLUDE\" keyword")
t)))))))

(defun org-transclusion-within-transclusion-p ()
"Return t if the current point is within a tranclusion region."
(when (get-char-property (point) 'org-transclusion-type) t))
Expand Down
9 changes: 9 additions & 0 deletions test/test-2.0.org
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,17 @@ This is a link to a [[id:2022-05-30T203553][Bertrand Russell]] wikipedia excerpt
#+transclude: [[file:empty.txt::2][empty text file]]

** test text

#+transclude: [[file:test.txt][text file]]

Below are tesing the new ~org-transclusion-fix-common-misspelling~ function.

#+transclude: [[file:test.txt][text file]]

#+transclude [[file:test.txt][text file]]

#+trans [[file:test.txt][text file]]

** test t/nil
t/nil will be dropped after remove-at-point

Expand Down

0 comments on commit 97c5a35

Please sign in to comment.