org-element--parse-to: Fix limit when pasting top-comment and no cache

* lisp/org-element.el (org-element--parse-to): Manually parse org-data
and set appropriate parser mode when POS is before first headline and
cache is disabled.
(org-element-org-data-parser--recurse): New variable flagging if
`org-element-data-parser' is called recursively.
(org-element-org-data-parser): Handle recursive calls from inside `org-element-at-point-no-context'.

This also fixes false-positives in `org-element--cache-verify-element'.
This commit is contained in:
Ihor Radchenko 2022-01-14 14:10:02 +08:00
parent 1537bb4029
commit 6631b3f7bb
No known key found for this signature in database
GPG Key ID: 6470762A7DA11D8B
1 changed files with 15 additions and 8 deletions

View File

@ -1253,6 +1253,8 @@ parser (e.g. `:end' and :END:). Return value is a plist."
(while (and (org-at-comment-p) (bolp)) (forward-line))
(org-element--get-node-properties t)))
(defvar org-element-org-data-parser--recurse nil)
(defun org-element-org-data-parser (&optional _)
"Parse org-data."
(org-with-wide-buffer
@ -1286,13 +1288,17 @@ parser (e.g. `:end' and :END:). Return value is a plist."
((symbolp org-category) (symbol-name org-category))
(t org-category)))
(category (catch 'buffer-category
(org-with-point-at end
(while (re-search-backward "^[ \t]*#\\+CATEGORY:" (point-min) t)
(org-element-with-disabled-cache
(let ((element (org-element-at-point-no-context)))
(when (eq (org-element-type element) 'keyword)
(throw 'buffer-category
(org-element-property :value element)))))))
(unless org-element-org-data-parser--recurse
(org-with-point-at end
;; Avoid recusrive calls from
;; `org-element-at-point-no-context'.
(let ((org-element-org-data-parser--recurse t))
(while (re-search-backward "^[ \t]*#\\+CATEGORY:" (point-min) t)
(org-element-with-disabled-cache
(let ((element (org-element-at-point-no-context)))
(when (eq (org-element-type element) 'keyword)
(throw 'buffer-category
(org-element-property :value element)))))))))
category))
(properties (org-element--get-global-node-properties)))
(unless (plist-get properties :CATEGORY)
@ -6378,7 +6384,8 @@ the expected result."
(setq element (org-element-headline-parser nil 'fast))
(setq mode 'planning)
(forward-line))
(setq mode 'top-comment))
(setq element (org-element-org-data-parser))
(setq mode 'org-data))
(org-skip-whitespace)
(beginning-of-line))
;; Check if CACHED or any of its ancestors contain point.