From 199e64cf8264025cc78f79c3bdb278920685281f Mon Sep 17 00:00:00 2001 Message-Id: <199e64cf8264025cc78f79c3bdb278920685281f.1633271912.git.yantar92@gmail.com> From: Ihor Radchenko Date: Sun, 3 Oct 2021 22:10:31 +0800 Subject: [PATCH] org.el: Do not unconditionally inherit from headline right at BOB * lisp/org.el (org-entry-get-with-inheritance): Consider scenario when there is a headline starting at BOB and we are getting an inherited property at non-child headline below. Previous implementation would erroneously inherit the property value from the first headline in buffer. * testing/lisp/test-org.el (test-org/entry-get): Add test and fix an existing test that worked because this bug existed. Fixes https://list.orgmode.org/87zgrqqlcs.fsf@toloe.se/T/#mfcab9bd710d837a0cd9d4cf331655ee39b8ad3ca --- lisp/org.el | 14 ++++++++++++-- testing/lisp/test-org.el | 6 +++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/lisp/org.el b/lisp/org.el index bc0ea24be..bcb38f07f 100644 --- a/lisp/org.el +++ b/lisp/org.el @@ -13163,7 +13163,7 @@ (defun org-entry-get-with-inheritance (property &optional literal-nil) However, if LITERAL-NIL is set, return the string value \"nil\" instead." (move-marker org-entry-property-inherited-from nil) (org-with-wide-buffer - (let (value) + (let (value at-bob-no-heading) (catch 'exit (while t (let ((v (org--property-local-values property literal-nil))) @@ -13177,7 +13177,17 @@ (defun org-entry-get-with-inheritance (property &optional literal-nil) (org-back-to-heading-or-point-min t) (move-marker org-entry-property-inherited-from (point)) (throw 'exit nil)) - ((org-up-heading-or-point-min)) + ((or (org-up-heading-safe) + (and (not (bobp)) + (goto-char (point-min)) + nil) + ;; `org-up-heading-safe' returned nil. We are at low + ;; level heading or bob. If there is headline + ;; there, do not try to fetch its properties. + (and (bobp) + (not at-bob-no-heading) + (not (org-at-heading-p)) + (setq at-bob-no-heading t)))) (t (let ((global (org--property-global-or-keyword-value property literal-nil))) (cond ((not global)) diff --git a/testing/lisp/test-org.el b/testing/lisp/test-org.el index 18d41a0d2..7b1ce8cd0 100644 --- a/testing/lisp/test-org.el +++ b/testing/lisp/test-org.el @@ -5831,6 +5831,10 @@ (ert-deftest test-org/entry-get () (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** H2" (let ((org-use-property-inheritance nil)) (org-entry-get (point-max) "A" 'selective)))) + (should-not + (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n* H2" + (let ((org-use-property-inheritance t)) + (org-entry-get (point-max) "A" t)))) (should (equal "1 2" @@ -5853,7 +5857,7 @@ (ert-deftest test-org/entry-get () (equal "1 2" (org-test-with-temp-text - "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2.1\n* H2.2\n:PROPERTIES:\n:A+: 2\n:END:" + "* H1\n:PROPERTIES:\n:A: 1\n:END:\n** H2.1\n** H2.2\n:PROPERTIES:\n:A+: 2\n:END:" (org-entry-get (point-max) "A" t)))) (should (equal "1" -- 2.32.0