From dc4b2772e3d653b8a925a3ea565b8b8a8e0f9eaa Mon Sep 17 00:00:00 2001 From: Ihor Radchenko Date: Fri, 7 Jan 2022 22:10:22 +0800 Subject: [PATCH] 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 --- lisp/org-element.el | 5 +++-- testing/lisp/test-org.el | 13 +++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lisp/org-element.el b/lisp/org-element.el index dbf54454a..33f7f2767 100644 --- a/lisp/org-element.el +++ b/lisp/org-element.el @@ -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)) diff --git a/testing/lisp/test-org.el b/testing/lisp/test-org.el index 97a84f571..c50d95f65 100644 --- a/testing/lisp/test-org.el +++ b/testing/lisp/test-org.el @@ -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 ()