org-element-cache-map: Fix when :next-re is provided

* lisp/org-element.el (org-element-cache-map): Fix searching next-re
in move-start-to-next-match.  The if confition was erroneously always
true due to the last `cl-incf'.  Do not map over failing entry when
next-re did not match and continue-flag is set.  Do not move point to
next entry before calling FUNC.
* testing/lisp/test-org.el (test-org/map-entries): Add new test case.
This commit is contained in:
Ihor Radchenko 2024-02-06 01:42:54 +01:00
parent df1f9be7f8
commit 7319136420
No known key found for this signature in database
GPG Key ID: 6470762A7DA11D8B
2 changed files with 25 additions and 17 deletions

View File

@ -7989,10 +7989,10 @@ the cache."
(if org-element--cache-map-statistics
(progn
(setq before-time (float-time))
(re-search-forward (or (car-safe ,re) ,re) nil 'move)
(cl-incf re-search-time
(- (float-time)
before-time)))
(prog1 (re-search-forward (or (car-safe ,re) ,re) nil 'move)
(cl-incf re-search-time
(- (float-time)
before-time))))
(re-search-forward (or (car-safe ,re) ,re) nil 'move)))
(unless (or (< (point) (or start -1))
(and data
@ -8176,22 +8176,21 @@ the cache."
(move-start-to-next-match
(if last-match next-re fail-re)))
(when (and (or (not start) (eq (org-element-begin data) start))
(< (org-element-begin data) to-pos))
(< (org-element-begin data) to-pos)
(not continue-flag))
;; Calculate where next possible element
;; starts and update START if needed.
(setq start (next-element-start))
(goto-char start)
;; Move START further if possible.
(when (and next-element-re
;; Do not move if we know for
;; sure that cache does not
;; contain gaps. Regexp
;; searches are not cheap.
(not (cache-gapless-p)))
(move-start-to-next-match next-element-re)
;; Make sure that point is at START
;; before running FUNC.
(goto-char start))
(save-excursion
(when (and next-element-re
;; Do not move if we know for
;; sure that cache does not
;; contain gaps. Regexp
;; searches are not cheap.
(not (cache-gapless-p)))
(move-start-to-next-match next-element-re)))
;; Try FUNC if DATA matches all the
;; restrictions. Calculate new START.
(when (or (not restrict-elements)

View File

@ -2855,7 +2855,7 @@ test <point>
(should (org-find-olp '("Headline" "headline8") t))))
(ert-deftest test-org/map-entries ()
"Test `org-map-entries' specifications."
"Test `org-map-entries' and `org-element-cache-map' specifications."
(dolist (org-element-use-cache '(t nil))
;; Full match.
(should
@ -3097,7 +3097,16 @@ Lets stop here
(lambda ()
(delete-region (point) (line-beginning-position 2))
(setq org-map-continue-from (point))))
(buffer-string))))))
(buffer-string))))
;; :next-re in `org-element-cache-map'
(org-test-with-temp-text
"* one
* TODO two
* three
* four
"
(should (equal '("two")
(org-element-cache-map (lambda (el) (org-element-property :title el)) :next-re "TODO"))))))
(ert-deftest test-org/edit-headline ()
"Test `org-edit-headline' specifications."