Fix elements being joined together after replacing

This commit is contained in:
louie 2018-01-28 16:04:20 +08:00
parent 338cf93f1e
commit 8e81543781
2 changed files with 18 additions and 2 deletions

View file

@ -507,10 +507,27 @@ If DEMOTE is t, demote the inserted note heading."
(org-set-tags-to tags)
(org-fix-tags-on-the-fly))
(defun anki-editor--effective-end (node)
"Get the effective end of NODE.
org-element considers whitespaces or newlines after an element or
object still belong to it, which is to say :end property of an
element matches :begin property of the following one at the same
level, if any. This will make it unable to separate elements with
their following ones after replacing. This function 'fixes' this
by resetting the end to the point after the last character that's
not blank. I'm not sure if this works for all cases though :)"
(let ((end (org-element-property :end node)))
(while (and (>= end (point-min))
;; check if character before END is blank
(string-match-p "[[:blank:]\r\n]" (buffer-substring (1- end) end)))
(setq end (1- end)))
end))
(defun anki-editor--replace-node (node replacer)
"Replace contents of NODE with the result from applying REPLACER to the contents of NODE."
(let* ((begin (org-element-property :begin node))
(end (- (org-element-property :end node) (org-element-property :post-blank node)))
(end (anki-editor--effective-end node))
(original (delete-and-extract-region begin end))
(replacement (funcall replacer original)))
(goto-char begin)

View file

@ -75,7 +75,6 @@
\alpha = \{a_1, a_2, a_3\}, \beta = \{b_1, b_2, b_3\}
\end{equation*}
What's the result of $\alpha \cdot \beta$ ?
*** Back