org-element--cache-before-change: Fix edge case creating heading

* lisp/org-element.el (org-element--cache-before-change): Fix edge
case when a heading is created because we insert a newline right
before ****.
* testing/lisp/test-org-element.el (test-org-element/cache-headline):
Add tests.

Reported-by: Rodrigo Morales <moralesrodrigo1100@gmail.com>
Link: https://orgmode.org/list/CAGxMbPbbqc33iaqJ=EceyKrLaf4maJAxaUmJGaPOvG_Rpw+xcQ@mail.gmail.com
This commit is contained in:
Ihor Radchenko 2023-08-16 11:41:31 +03:00
parent 561c1d0db0
commit 27a41d418d
No known key found for this signature in database
GPG Key ID: 6470762A7DA11D8B
2 changed files with 28 additions and 15 deletions

View File

@ -6705,20 +6705,8 @@ The function returns the new value of `org-element--cache-change-warning'."
(setq org-element--cache-change-tic (buffer-chars-modified-tick))
(setq org-element--cache-last-buffer-size (buffer-size))
(goto-char beg)
(beginning-of-line)
(let ((bottom (save-excursion
(goto-char end)
(if (and (bolp)
;; When beg == end, still extent to eol.
(> (point) beg))
;; FIXME: Potential pitfall.
;; We are appending to an element end.
;; Unless the last inserted char is not
;; newline, the next element is not broken
;; and does not need to be purged from the
;; cache.
end
(line-end-position)))))
(forward-line 0)
(let ((bottom (save-excursion (goto-char end) (line-end-position))))
(prog1
;; Use the worst change warning to not miss important edits.
;; This function is called before edit and after edit by

View File

@ -4372,7 +4372,32 @@ paragraph
(let ((org-element-use-cache t))
(org-element-at-point (point-max))
(delete-region (point) (point-max))
(should (eq 'paragraph (org-element-type (org-element-at-point)))))))
(should (eq 'paragraph (org-element-type (org-element-at-point))))))
;; Remove/re-introduce heading.
(org-test-with-temp-text
"
* 1
** 1-1
a
<point>** 1-2
a
"
(let ((org-element-use-cache t))
(org-element-at-point (point-max))
(insert "FOO")
(should
(equal
"1-1"
(org-element-property
:title
(org-element-lineage (org-element-at-point) '(headline)))))
(insert "\n")
(should
(equal
"1"
(org-element-property
:title
(org-element-lineage (org-element-at-point) '(headline))))))))
(provide 'test-org-element)