From e2a8e95576598443bbb5ae425615d8b429a8b28b Mon Sep 17 00:00:00 2001 From: Ihor Radchenko Date: Fri, 17 Dec 2021 20:23:50 +0800 Subject: [PATCH] org-element-cache: Fix when edit extends previous element * lisp/org-element.el (org-element--cache-after-change): Always extend changed region to bol. * testing/lisp/test-org-element.el (test-org-element/cache): Add test checking the new fix. Amend some tests around making sure that cache is active during testing. --- lisp/org-element.el | 6 ++---- testing/lisp/test-org-element.el | 29 ++++++++++++++++++++--------- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/lisp/org-element.el b/lisp/org-element.el index 874b25e19..112e335b0 100644 --- a/lisp/org-element.el +++ b/lisp/org-element.el @@ -6602,10 +6602,8 @@ that range. See `after-change-functions' for more information." ;; sure that we capture preceding element. (setq beg (save-excursion (goto-char beg) - (skip-chars-backward " \t") - (if (not (bolp)) beg - (cl-incf pre (- beg (point))) - (point)))) + (cl-incf pre (- beg (line-beginning-position))) + (line-beginning-position))) ;; Store synchronization request. (let ((offset (- end beg pre))) (save-match-data diff --git a/testing/lisp/test-org-element.el b/testing/lisp/test-org-element.el index 83a96e1a2..3d320b83b 100644 --- a/testing/lisp/test-org-element.el +++ b/testing/lisp/test-org-element.el @@ -4080,19 +4080,30 @@ Text ;; Test edits near :end of element (should-not (eq 'headline (org-test-with-temp-text "* H1\nP1\n*H2\n" - (org-element-cache-map #'ignore :granularity 'element) - (insert "Blah") - (org-element-type (org-element-at-point))))) + (let ((org-element-use-cache t)) + (org-element-cache-map #'ignore :granularity 'element) + (insert "Blah") + (org-element-type (org-element-at-point)))))) (should-not (eq 'headline (org-test-with-temp-text "* H1\nP1\n*H2\n" - (org-element-cache-map #'ignore :granularity 'element) - (backward-delete-char 1) - (org-element-type (org-element-at-point))))) + (let ((org-element-use-cache t)) + (org-element-cache-map #'ignore :granularity 'element) + (backward-delete-char 1) + (org-element-type (org-element-at-point)))))) + (org-test-with-temp-text "Paragraph.\n # comment" + (let ((org-element-use-cache t)) + (org-element-cache-map #'ignore :granularity 'element) + (should (eq 'comment (org-element-type (org-element-at-point)))) + (insert "not comment anymore") + (org-element-cache-map #'ignore :granularity 'element) + (should-not (eq 'comment (org-element-type (org-element-at-point)))) + (should (eq (org-element-at-point) (org-element-at-point 1))))) (should (eq 'headline (org-test-with-temp-text "* H1\nP1\n