From 7319136420f58a8dec24c812460608b1f98d66bf Mon Sep 17 00:00:00 2001 From: Ihor Radchenko Date: Tue, 6 Feb 2024 01:42:54 +0100 Subject: [PATCH] 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. --- lisp/org-element.el | 29 ++++++++++++++--------------- testing/lisp/test-org.el | 13 +++++++++++-- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/lisp/org-element.el b/lisp/org-element.el index 6e36aba9a..e6df9743a 100644 --- a/lisp/org-element.el +++ b/lisp/org-element.el @@ -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) diff --git a/testing/lisp/test-org.el b/testing/lisp/test-org.el index 337367c0b..fc38cc666 100644 --- a/testing/lisp/test-org.el +++ b/testing/lisp/test-org.el @@ -2855,7 +2855,7 @@ test (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 @@ Let’s 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."