org-element-cache-map: Fix an edge case with org-element-cache-continue-from

* lisp/org-element.el (org-element-cache-map): Make sure that START is
never set to -1.  It may cause infinite loops in some scenarios.
* testing/lisp/test-org.el (test-org/map-entries): Add a test catching
the reported situation.

Reported in https://list.orgmode.org/CADywB5JHAyPX99Vr02SvAqiMTD+7ss4VWVipOhKfm=iGirDPhA@mail.gmail.com/T/#t
This commit is contained in:
Ihor Radchenko 2022-01-07 22:10:22 +08:00
parent 515ce56d4e
commit dc4b2772e3
No known key found for this signature in database
GPG Key ID: 6470762A7DA11D8B
2 changed files with 16 additions and 2 deletions

View File

@ -7580,14 +7580,15 @@ the cache."
;; inside an element. We want to move
;; it to real beginning then despite
;; START being larger.
(setq start -1)
(setq start nil)
(move-start-to-next-match nil)
;; The new element may now start before
;; or at already processed position.
;; Make sure that we continue from an
;; element past already processed
;; place.
(when (and (<= start (org-element-property :begin data))
(when (and start
(<= start (org-element-property :begin data))
(not org-element-cache-map-continue-from))
(goto-char start)
(setq data (element-match-at-point))

View File

@ -2414,6 +2414,19 @@ SCHEDULED: <2014-03-04 tue.>"
(lambda ()
(org-cut-subtree)
(setq org-map-continue-from (point))))
(buffer-string))))
(should
(string= "* H1\n* H2\n* H3\n"
(org-test-with-temp-text "* H1\n* H2\n* H3\n* H4"
(org-map-entries
(lambda ()
(when (string= "H4"
(org-element-property
:raw-value (org-element-at-point)))
(org-cut-subtree)
(setq org-map-continue-from
(org-element-property
:begin (org-element-at-point))))))
(buffer-string)))))
(ert-deftest test-org/edit-headline ()