Browse Source

org-element-cache: Fix some edits right after indent at :begin

* lisp/org-element.el (org-element--cache-after-change): Extend
changed region to bol when we are editing near beginning of an element
within or right after indentation.  Such edits potentially change
:post-blank value of the previous element.
* testing/lisp/test-org-element.el (test-org-element/cache): Add test
checking one of such cases.
Ihor Radchenko 3 years ago
parent
commit
d267486002
2 changed files with 18 additions and 0 deletions
  1. 9 0
      lisp/org-element.el
  2. 9 0
      testing/lisp/test-org-element.el

+ 9 - 0
lisp/org-element.el

@@ -6585,6 +6585,15 @@ that range.  See `after-change-functions' for more information."
       (when (not (eq org-element--cache-change-tic (buffer-chars-modified-tick)))
         (org-element--cache-log-message "After change")
         (setq org-element--cache-change-warning (org-element--cache-before-change beg end))
+        ;; If beg is right after spaces in front of an element, we
+        ;; risk affecting previous element, so move beg to bol, making
+        ;; sure that we capture preceding element.
+        (setq beg (save-excursion
+                    (goto-char beg)
+                    (skip-chars-backward " \t")
+                    (if (not (bolp)) beg
+                      (cl-incf pre (- beg (point)))
+                      (point))))
         ;; Store synchronization request.
         (let ((offset (- end beg pre)))
           (save-match-data

+ 9 - 0
testing/lisp/test-org-element.el

@@ -4004,6 +4004,15 @@ Text
 	 (equal (cons (org-element-property :begin element)
 		      (org-element-property :end element))
 		(cons (point-min) (point-max)))))))
+  (org-test-with-temp-text ":DRAWER:\ntest\n:END:\n <point>#\nParagraph"
+    (let ((org-element-use-cache t))
+      (org-element-cache-map #'ignore :granularity 'element)
+      (should (eq 'comment (org-element-type (org-element-at-point))))
+      (should (eq 0 (org-element-property :post-blank (org-element-at-point (point-min)))))
+      (insert " ") (delete-char -1)
+      (org-element-cache-map #'ignore :granularity 'element)
+      (delete-char 1)
+      (should (eq 1 (org-element-property :post-blank (org-element-at-point (point-min)))))))
   ;; Sensitive change: adding a line alters document structure both
   ;; above and below.
   (should