org-element-cache: Fix transforming keywords to affiliated

* lisp/org-element.el (org-element--cache-for-removal): Consider
preceding keywords to be updated unconditionally.
(org-element-cache-map): Fix infinite loop revealed by the new test.

* testing/lisp/test-org-element.el (test-org-element/cache-affiliated):
New test.
This commit is contained in:
Ihor Radchenko 2022-01-07 21:24:43 +08:00
parent a98ae424d2
commit 515ce56d4e
No known key found for this signature in database
GPG Key ID: 6470762A7DA11D8B
2 changed files with 24 additions and 1 deletions

View File

@ -6694,6 +6694,14 @@ known element in cache (it may start after END)."
(before (car elements))
(after (cdr elements)))
(if (not before) after
;; If BEFORE is a keyword, it may need to be removed to become
;; an affiliated keyword.
(when (eq 'keyword (org-element-type before))
(let ((prev before))
(while (eq 'keyword (org-element-type prev))
(setq before prev
beg (org-element-property :begin prev))
(setq prev (org-element--cache-find (1- (org-element-property :begin before)))))))
(let ((up before)
(robust-flag t))
(while up
@ -7288,7 +7296,8 @@ the cache."
(setq start (max (or start -1)
(or (org-element-property :begin data) -1)
(or (org-element-property :begin (element-match-at-point)) -1))))
(when (>= start to-pos) (cache-walk-abort)))
(when (>= start to-pos) (cache-walk-abort))
(when (eq start -1) (setq start nil)))
(cache-walk-abort))))
;; Find expected begin position of an element after
;; DATA.

View File

@ -4128,6 +4128,20 @@ Text
:end (org-element-property :parent (org-element-at-point)))
(+ parent-end 3))))))
(ert-deftest test-org-element/cache-affiliated ()
"Test updating affiliated keywords."
;; Inserting a line right after other keywords.
(let ((org-element-use-cache t))
(org-test-with-temp-text "
#+caption: test
#+name: test
<point>
line"
(org-element-cache-map #'ignore :granularity 'element)
(should (eq 'keyword (org-element-type (org-element-at-point))))
(insert "#")
(should (eq 2 (org-element-property :begin (org-element-at-point)))))))
(ert-deftest test-org-element/cache-table ()
"Test handling edits in tables."
;; Unindented second row of the table should not be re-parented by