org-element--current-element: Fix edge case with affiliated keywords

* lisp/org-element.el (org-element--collect-affiliated-keywords): Fix
edge case when a keyword matching affiliated keyword is preceding an
element that is not allowed to have such.  We need to handle this case
specially here rather than in `org-element--current-element' to avoid
the default paragraph fallback.
(org-element--current-element): Add a comment describing the pitfall.
*
testing/lisp/test-org-element.el (test-org-element/affiliated-keywords-parser):
Add more tests.

Reported-by: Tom Alexander <tom@fizz.buzz>
Link: https://orgmode.org/list/e2be976d-1bcf-4136-9968-33212dcd1f83@app.fastmail.com
This commit is contained in:
Ihor Radchenko 2023-10-13 15:14:57 +03:00
parent f3de4c3e04
commit 0d214ef008
No known key found for this signature in database
GPG Key ID: 6470762A7DA11D8B
2 changed files with 25 additions and 2 deletions

View File

@ -4610,6 +4610,10 @@ element it has to parse."
;; Inlinetask.
(at-task? (org-element-inlinetask-parser limit raw-secondary-p))
;; From there, elements can have affiliated keywords.
;; Note an edge case with a keyword followed by element that
;; cannot have affiliated keywords attached (the above).
;; `org-element--collect-affiliated-keywords' must have a
;; special check to fall back to parsing proper keyword.
(t (let ((affiliated (org-element--collect-affiliated-keywords
limit (memq granularity '(nil object)))))
(cond
@ -4792,7 +4796,11 @@ When PARSE is non-nil, values from keywords belonging to
;; They will be parsed as a paragraph.
(when (or (looking-at-p "[ \t]*$")
;; Affiliated keywords are not allowed before comments.
(looking-at-p org-comment-regexp))
(looking-at-p org-comment-regexp)
;; Clock lines are also not allowed.
(looking-at-p org-clock-line-re)
;; Inlinetasks not allowed.
(looking-at-p "^\\*+ "))
(goto-char origin) (setq output nil))
;; Return value.
(cons origin output))))

View File

@ -878,7 +878,22 @@ Some other text
(should-not
(org-test-with-temp-text "#+name: foo\n# bar"
(progn (search-forward "bar")
(org-element-property :name (org-element-at-point))))))
(org-element-property :name (org-element-at-point)))))
;; Headlines cannot have affiliated keywords.
(should
(org-test-with-temp-text "<point>#+name: foo\n* Heading"
(org-element-type-p (org-element-at-point) 'keyword)))
;; Clocks cannot have affiliated keywords.
(should
(org-test-with-temp-text "<point>#+name: foo
CLOCK: [2023-10-13 Fri 14:40]--[2023-10-13 Fri 14:51] => 0:11"
(org-element-type-p (org-element-at-point) 'keyword)))
;; Inlinetasks cannot have affiliated keywords.
(should
(let ((org-inlinetask-min-level 4))
(org-test-with-temp-text "<point>#+name: foo
**** Inlinetask"
(org-element-type-p (org-element-at-point) 'keyword)))))
;;;; Babel Call