org-element: Remove outdated comments
[org-mode/org-tableheadings.git] / lisp / org-element.el
blob847e7989d9bf610aff9ee4e58020838d3a942d13
1 ;;; org-element.el --- Parser for Org Syntax -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2012-2017 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; See <http://orgmode.org/worg/dev/org-syntax.html> for details about
26 ;; Org syntax.
28 ;; Lisp-wise, a syntax object can be represented as a list.
29 ;; It follows the pattern (TYPE PROPERTIES CONTENTS), where:
30 ;; TYPE is a symbol describing the object.
31 ;; PROPERTIES is the property list attached to it. See docstring of
32 ;; appropriate parsing function to get an exhaustive list.
33 ;; CONTENTS is a list of syntax objects or raw strings contained
34 ;; in the current object, when applicable.
36 ;; For the whole document, TYPE is `org-data' and PROPERTIES is nil.
38 ;; The first part of this file defines constants for the Org syntax,
39 ;; while the second one provide accessors and setters functions.
41 ;; The next part implements a parser and an interpreter for each
42 ;; element and object type in Org syntax.
44 ;; The following part creates a fully recursive buffer parser. It
45 ;; also provides a tool to map a function to elements or objects
46 ;; matching some criteria in the parse tree. Functions of interest
47 ;; are `org-element-parse-buffer', `org-element-map' and, to a lesser
48 ;; extent, `org-element-parse-secondary-string'.
50 ;; The penultimate part is the cradle of an interpreter for the
51 ;; obtained parse tree: `org-element-interpret-data'.
53 ;; The library ends by furnishing `org-element-at-point' function, and
54 ;; a way to give information about document structure around point
55 ;; with `org-element-context'. A cache mechanism is also provided for
56 ;; these functions.
59 ;;; Code:
61 (require 'org)
62 (require 'avl-tree)
63 (require 'cl-lib)
67 ;;; Definitions And Rules
69 ;; Define elements, greater elements and specify recursive objects,
70 ;; along with the affiliated keywords recognized. Also set up
71 ;; restrictions on recursive objects combinations.
73 ;; `org-element-update-syntax' builds proper syntax regexps according
74 ;; to current setup.
76 (defvar org-element-paragraph-separate nil
77 "Regexp to separate paragraphs in an Org buffer.
78 In the case of lines starting with \"#\" and \":\", this regexp
79 is not sufficient to know if point is at a paragraph ending. See
80 `org-element-paragraph-parser' for more information.")
82 (defvar org-element--object-regexp nil
83 "Regexp possibly matching the beginning of an object.
84 This regexp allows false positives. Dedicated parser (e.g.,
85 `org-export-bold-parser') will take care of further filtering.
86 Radio links are not matched by this regexp, as they are treated
87 specially in `org-element--object-lex'.")
89 (defun org-element--set-regexps ()
90 "Build variable syntax regexps."
91 (setq org-element-paragraph-separate
92 (concat "^\\(?:"
93 ;; Headlines, inlinetasks.
94 org-outline-regexp "\\|"
95 ;; Footnote definitions.
96 "\\[fn:[-_[:word:]]+\\]" "\\|"
97 ;; Diary sexps.
98 "%%(" "\\|"
99 "[ \t]*\\(?:"
100 ;; Empty lines.
101 "$" "\\|"
102 ;; Tables (any type).
103 "|" "\\|"
104 "\\+\\(?:-+\\+\\)+[ \t]*$" "\\|"
105 ;; Comments, keyword-like or block-like constructs.
106 ;; Blocks and keywords with dual values need to be
107 ;; double-checked.
108 "#\\(?: \\|$\\|\\+\\(?:"
109 "BEGIN_\\S-+" "\\|"
110 "\\S-+\\(?:\\[.*\\]\\)?:[ \t]*\\)\\)"
111 "\\|"
112 ;; Drawers (any type) and fixed-width areas. Drawers
113 ;; need to be double-checked.
114 ":\\(?: \\|$\\|[-_[:word:]]+:[ \t]*$\\)" "\\|"
115 ;; Horizontal rules.
116 "-\\{5,\\}[ \t]*$" "\\|"
117 ;; LaTeX environments.
118 "\\\\begin{\\([A-Za-z0-9*]+\\)}" "\\|"
119 ;; Clock lines.
120 (regexp-quote org-clock-string) "\\|"
121 ;; Lists.
122 (let ((term (pcase org-plain-list-ordered-item-terminator
123 (?\) ")") (?. "\\.") (_ "[.)]")))
124 (alpha (and org-list-allow-alphabetical "\\|[A-Za-z]")))
125 (concat "\\(?:[-+*]\\|\\(?:[0-9]+" alpha "\\)" term "\\)"
126 "\\(?:[ \t]\\|$\\)"))
127 "\\)\\)")
128 org-element--object-regexp
129 (mapconcat #'identity
130 (let ((link-types (regexp-opt (org-link-types))))
131 (list
132 ;; Sub/superscript.
133 "\\(?:[_^][-{(*+.,[:alnum:]]\\)"
134 ;; Bold, code, italic, strike-through, underline
135 ;; and verbatim.
136 (concat "[*~=+_/]"
137 (format "[^%s]"
138 (nth 2 org-emphasis-regexp-components)))
139 ;; Plain links.
140 (concat "\\<" link-types ":")
141 ;; Objects starting with "[": regular link,
142 ;; footnote reference, statistics cookie,
143 ;; timestamp (inactive).
144 (concat "\\[\\(?:"
145 "fn:" "\\|"
146 "\\[" "\\|"
147 "[0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}" "\\|"
148 "[0-9]*\\(?:%\\|/[0-9]*\\)\\]"
149 "\\)")
150 ;; Objects starting with "@": export snippets.
151 "@@"
152 ;; Objects starting with "{": macro.
153 "{{{"
154 ;; Objects starting with "<" : timestamp
155 ;; (active, diary), target, radio target and
156 ;; angular links.
157 (concat "<\\(?:%%\\|<\\|[0-9]\\|" link-types "\\)")
158 ;; Objects starting with "$": latex fragment.
159 "\\$"
160 ;; Objects starting with "\": line break,
161 ;; entity, latex fragment.
162 "\\\\\\(?:[a-zA-Z[(]\\|\\\\[ \t]*$\\|_ +\\)"
163 ;; Objects starting with raw text: inline Babel
164 ;; source block, inline Babel call.
165 "\\(?:call\\|src\\)_"))
166 "\\|")))
168 (org-element--set-regexps)
170 ;;;###autoload
171 (defun org-element-update-syntax ()
172 "Update parser internals."
173 (interactive)
174 (org-element--set-regexps)
175 (org-element-cache-reset 'all))
177 (defconst org-element-all-elements
178 '(babel-call center-block clock comment comment-block diary-sexp drawer
179 dynamic-block example-block export-block fixed-width
180 footnote-definition headline horizontal-rule inlinetask item
181 keyword latex-environment node-property paragraph plain-list
182 planning property-drawer quote-block section
183 special-block src-block table table-row verse-block)
184 "Complete list of element types.")
186 (defconst org-element-greater-elements
187 '(center-block drawer dynamic-block footnote-definition headline inlinetask
188 item plain-list property-drawer quote-block section
189 special-block table)
190 "List of recursive element types aka Greater Elements.")
192 (defconst org-element-all-objects
193 '(bold code entity export-snippet footnote-reference inline-babel-call
194 inline-src-block italic line-break latex-fragment link macro
195 radio-target statistics-cookie strike-through subscript superscript
196 table-cell target timestamp underline verbatim)
197 "Complete list of object types.")
199 (defconst org-element-recursive-objects
200 '(bold footnote-reference italic link subscript radio-target strike-through
201 superscript table-cell underline)
202 "List of recursive object types.")
204 (defconst org-element-object-containers
205 (append org-element-recursive-objects '(paragraph table-row verse-block))
206 "List of object or element types that can directly contain objects.")
208 (defconst org-element-affiliated-keywords
209 '("CAPTION" "DATA" "HEADER" "HEADERS" "LABEL" "NAME" "PLOT" "RESNAME" "RESULT"
210 "RESULTS" "SOURCE" "SRCNAME" "TBLNAME")
211 "List of affiliated keywords as strings.
212 By default, all keywords setting attributes (e.g., \"ATTR_LATEX\")
213 are affiliated keywords and need not to be in this list.")
215 (defconst org-element-keyword-translation-alist
216 '(("DATA" . "NAME") ("LABEL" . "NAME") ("RESNAME" . "NAME")
217 ("SOURCE" . "NAME") ("SRCNAME" . "NAME") ("TBLNAME" . "NAME")
218 ("RESULT" . "RESULTS") ("HEADERS" . "HEADER"))
219 "Alist of usual translations for keywords.
220 The key is the old name and the value the new one. The property
221 holding their value will be named after the translated name.")
223 (defconst org-element-multiple-keywords '("CAPTION" "HEADER")
224 "List of affiliated keywords that can occur more than once in an element.
226 Their value will be consed into a list of strings, which will be
227 returned as the value of the property.
229 This list is checked after translations have been applied. See
230 `org-element-keyword-translation-alist'.
232 By default, all keywords setting attributes (e.g., \"ATTR_LATEX\")
233 allow multiple occurrences and need not to be in this list.")
235 (defconst org-element-parsed-keywords '("CAPTION")
236 "List of affiliated keywords whose value can be parsed.
238 Their value will be stored as a secondary string: a list of
239 strings and objects.
241 This list is checked after translations have been applied. See
242 `org-element-keyword-translation-alist'.")
244 (defconst org-element--parsed-properties-alist
245 (mapcar (lambda (k) (cons k (intern (concat ":" (downcase k)))))
246 org-element-parsed-keywords)
247 "Alist of parsed keywords and associated properties.
248 This is generated from `org-element-parsed-keywords', which
249 see.")
251 (defconst org-element-dual-keywords '("CAPTION" "RESULTS")
252 "List of affiliated keywords which can have a secondary value.
254 In Org syntax, they can be written with optional square brackets
255 before the colons. For example, RESULTS keyword can be
256 associated to a hash value with the following:
258 #+RESULTS[hash-string]: some-source
260 This list is checked after translations have been applied. See
261 `org-element-keyword-translation-alist'.")
263 (defconst org-element--affiliated-re
264 (format "[ \t]*#\\+\\(?:%s\\):[ \t]*"
265 (concat
266 ;; Dual affiliated keywords.
267 (format "\\(?1:%s\\)\\(?:\\[\\(.*\\)\\]\\)?"
268 (regexp-opt org-element-dual-keywords))
269 "\\|"
270 ;; Regular affiliated keywords.
271 (format "\\(?1:%s\\)"
272 (regexp-opt
273 (cl-remove-if
274 (lambda (k) (member k org-element-dual-keywords))
275 org-element-affiliated-keywords)))
276 "\\|"
277 ;; Export attributes.
278 "\\(?1:ATTR_[-_A-Za-z0-9]+\\)"))
279 "Regexp matching any affiliated keyword.
281 Keyword name is put in match group 1. Moreover, if keyword
282 belongs to `org-element-dual-keywords', put the dual value in
283 match group 2.
285 Don't modify it, set `org-element-affiliated-keywords' instead.")
287 (defconst org-element-object-restrictions
288 (let* ((standard-set (remq 'table-cell org-element-all-objects))
289 (standard-set-no-line-break (remq 'line-break standard-set)))
290 `((bold ,@standard-set)
291 (footnote-reference ,@standard-set)
292 (headline ,@standard-set-no-line-break)
293 (inlinetask ,@standard-set-no-line-break)
294 (italic ,@standard-set)
295 (item ,@standard-set-no-line-break)
296 (keyword ,@(remq 'footnote-reference standard-set))
297 ;; Ignore all links excepted plain links and angular links in
298 ;; a link description. Also ignore radio-targets and line
299 ;; breaks.
300 (link bold code entity export-snippet inline-babel-call inline-src-block
301 italic latex-fragment macro simple-link statistics-cookie
302 strike-through subscript superscript underline verbatim)
303 (paragraph ,@standard-set)
304 ;; Remove any variable object from radio target as it would
305 ;; prevent it from being properly recognized.
306 (radio-target bold code entity italic latex-fragment strike-through
307 subscript superscript underline superscript)
308 (strike-through ,@standard-set)
309 (subscript ,@standard-set)
310 (superscript ,@standard-set)
311 ;; Ignore inline babel call and inline src block as formulas are
312 ;; possible. Also ignore line breaks and statistics cookies.
313 (table-cell bold code entity export-snippet footnote-reference italic
314 latex-fragment link macro radio-target strike-through
315 subscript superscript target timestamp underline verbatim)
316 (table-row table-cell)
317 (underline ,@standard-set)
318 (verse-block ,@standard-set)))
319 "Alist of objects restrictions.
321 key is an element or object type containing objects and value is
322 a list of types that can be contained within an element or object
323 of such type.
325 For example, in a `radio-target' object, one can only find
326 entities, latex-fragments, subscript, superscript and text
327 markup.
329 This alist also applies to secondary string. For example, an
330 `headline' type element doesn't directly contain objects, but
331 still has an entry since one of its properties (`:title') does.")
333 (defconst org-element-secondary-value-alist
334 '((headline :title)
335 (inlinetask :title)
336 (item :tag))
337 "Alist between element types and locations of secondary values.")
339 (defconst org-element--pair-round-table
340 (let ((table (make-syntax-table)))
341 (modify-syntax-entry ?\( "()" table)
342 (modify-syntax-entry ?\) ")(" table)
343 (dolist (char '(?\{ ?\} ?\[ ?\] ?\< ?\>) table)
344 (modify-syntax-entry char " " table)))
345 "Table used internally to pair only round brackets.
346 Other brackets are treated as spaces.")
348 (defconst org-element--pair-square-table
349 (let ((table (make-syntax-table)))
350 (modify-syntax-entry ?\[ "(]" table)
351 (modify-syntax-entry ?\] ")[" table)
352 (dolist (char '(?\{ ?\} ?\( ?\) ?\< ?\>) table)
353 (modify-syntax-entry char " " table)))
354 "Table used internally to pair only square brackets.
355 Other brackets are treated as spaces.")
357 (defconst org-element--pair-curly-table
358 (let ((table (make-syntax-table)))
359 (modify-syntax-entry ?\{ "(}" table)
360 (modify-syntax-entry ?\} "){" table)
361 (dolist (char '(?\[ ?\] ?\( ?\) ?\< ?\>) table)
362 (modify-syntax-entry char " " table)))
363 "Table used internally to pair only curly brackets.
364 Other brackets are treated as spaces.")
366 (defun org-element--parse-paired-brackets (char)
367 "Parse paired brackets at point.
368 CHAR is the opening bracket to consider, as a character. Return
369 contents between brackets, as a string, or nil. Also move point
370 past the brackets."
371 (when (eq char (char-after))
372 (let ((syntax-table (pcase char
373 (?\{ org-element--pair-curly-table)
374 (?\[ org-element--pair-square-table)
375 (?\( org-element--pair-round-table)
376 (_ nil)))
377 (pos (point)))
378 (when syntax-table
379 (with-syntax-table syntax-table
380 (let ((end (ignore-errors (scan-lists pos 1 0))))
381 (when end
382 (goto-char end)
383 (buffer-substring-no-properties (1+ pos) (1- end)))))))))
386 ;;; Accessors and Setters
388 ;; Provide four accessors: `org-element-type', `org-element-property'
389 ;; `org-element-contents' and `org-element-restriction'.
391 ;; Setter functions allow modification of elements by side effect.
392 ;; There is `org-element-put-property', `org-element-set-contents'.
393 ;; These low-level functions are useful to build a parse tree.
395 ;; `org-element-adopt-elements', `org-element-set-element',
396 ;; `org-element-extract-element' and `org-element-insert-before' are
397 ;; high-level functions useful to modify a parse tree.
399 ;; `org-element-secondary-p' is a predicate used to know if a given
400 ;; object belongs to a secondary string. `org-element-class' tells if
401 ;; some parsed data is an element or an object, handling pseudo
402 ;; elements and objects. `org-element-copy' returns an element or
403 ;; object, stripping its parent property in the process.
405 (defsubst org-element-type (element)
406 "Return type of ELEMENT.
408 The function returns the type of the element or object provided.
409 It can also return the following special value:
410 `plain-text' for a string
411 `org-data' for a complete document
412 nil in any other case."
413 (cond
414 ((not (consp element)) (and (stringp element) 'plain-text))
415 ((symbolp (car element)) (car element))))
417 (defsubst org-element-property (property element)
418 "Extract the value from the PROPERTY of an ELEMENT."
419 (if (stringp element) (get-text-property 0 property element)
420 (plist-get (nth 1 element) property)))
422 (defsubst org-element-contents (element)
423 "Extract contents from an ELEMENT."
424 (cond ((not (consp element)) nil)
425 ((symbolp (car element)) (nthcdr 2 element))
426 (t element)))
428 (defsubst org-element-restriction (element)
429 "Return restriction associated to ELEMENT.
430 ELEMENT can be an element, an object or a symbol representing an
431 element or object type."
432 (cdr (assq (if (symbolp element) element (org-element-type element))
433 org-element-object-restrictions)))
435 (defsubst org-element-put-property (element property value)
436 "In ELEMENT set PROPERTY to VALUE.
437 Return modified element."
438 (if (stringp element) (org-add-props element nil property value)
439 (setcar (cdr element) (plist-put (nth 1 element) property value))
440 element))
442 (defsubst org-element-set-contents (element &rest contents)
443 "Set ELEMENT's contents to CONTENTS.
444 Return ELEMENT."
445 (cond ((null element) contents)
446 ((not (symbolp (car element))) contents)
447 ((cdr element) (setcdr (cdr element) contents) element)
448 (t (nconc element contents))))
450 (defun org-element-secondary-p (object)
451 "Non-nil when OBJECT directly belongs to a secondary string.
452 Return value is the property name, as a keyword, or nil."
453 (let* ((parent (org-element-property :parent object))
454 (properties (cdr (assq (org-element-type parent)
455 org-element-secondary-value-alist))))
456 (catch 'exit
457 (dolist (p properties)
458 (and (memq object (org-element-property p parent))
459 (throw 'exit p))))))
461 (defun org-element-class (datum &optional parent)
462 "Return class for ELEMENT, as a symbol.
463 Class is either `element' or `object'. Optional argument PARENT
464 is the element or object containing DATUM. It defaults to the
465 value of DATUM `:parent' property."
466 (let ((type (org-element-type datum))
467 (parent (or parent (org-element-property :parent datum))))
468 (cond
469 ;; Trivial cases.
470 ((memq type org-element-all-objects) 'object)
471 ((memq type org-element-all-elements) 'element)
472 ;; Special cases.
473 ((eq type 'org-data) 'element)
474 ((eq type 'plain-text) 'object)
475 ((not type) 'object)
476 ;; Pseudo object or elements. Make a guess about its class.
477 ;; Basically a pseudo object is contained within another object,
478 ;; a secondary string or a container element.
479 ((not parent) 'element)
481 (let ((parent-type (org-element-type parent)))
482 (cond ((not parent-type) 'object)
483 ((memq parent-type org-element-object-containers) 'object)
484 ((org-element-secondary-p datum) 'object)
485 (t 'element)))))))
487 (defsubst org-element-adopt-elements (parent &rest children)
488 "Append elements to the contents of another element.
490 PARENT is an element or object. CHILDREN can be elements,
491 objects, or a strings.
493 The function takes care of setting `:parent' property for CHILD.
494 Return parent element."
495 (if (not children) parent
496 ;; Link every child to PARENT. If PARENT is nil, it is a secondary
497 ;; string: parent is the list itself.
498 (dolist (child children)
499 (org-element-put-property child :parent (or parent children)))
500 ;; Add CHILDREN at the end of PARENT contents.
501 (when parent
502 (apply #'org-element-set-contents
503 parent
504 (nconc (org-element-contents parent) children)))
505 ;; Return modified PARENT element.
506 (or parent children)))
508 (defun org-element-extract-element (element)
509 "Extract ELEMENT from parse tree.
510 Remove element from the parse tree by side-effect, and return it
511 with its `:parent' property stripped out."
512 (let ((parent (org-element-property :parent element))
513 (secondary (org-element-secondary-p element)))
514 (if secondary
515 (org-element-put-property
516 parent secondary
517 (delq element (org-element-property secondary parent)))
518 (apply #'org-element-set-contents
519 parent
520 (delq element (org-element-contents parent))))
521 ;; Return ELEMENT with its :parent removed.
522 (org-element-put-property element :parent nil)))
524 (defun org-element-insert-before (element location)
525 "Insert ELEMENT before LOCATION in parse tree.
526 LOCATION is an element, object or string within the parse tree.
527 Parse tree is modified by side effect."
528 (let* ((parent (org-element-property :parent location))
529 (property (org-element-secondary-p location))
530 (siblings (if property (org-element-property property parent)
531 (org-element-contents parent)))
532 ;; Special case: LOCATION is the first element of an
533 ;; independent secondary string (e.g. :title property). Add
534 ;; ELEMENT in-place.
535 (specialp (and (not property)
536 (eq siblings parent)
537 (eq (car parent) location))))
538 ;; Install ELEMENT at the appropriate LOCATION within SIBLINGS.
539 (cond (specialp)
540 ((or (null siblings) (eq (car siblings) location))
541 (push element siblings))
542 ((null location) (nconc siblings (list element)))
544 (let ((index (cl-position location siblings)))
545 (unless index (error "No location found to insert element"))
546 (push element (cdr (nthcdr (1- index) siblings))))))
547 ;; Store SIBLINGS at appropriate place in parse tree.
548 (cond
549 (specialp (setcdr parent (copy-sequence parent)) (setcar parent element))
550 (property (org-element-put-property parent property siblings))
551 (t (apply #'org-element-set-contents parent siblings)))
552 ;; Set appropriate :parent property.
553 (org-element-put-property element :parent parent)))
555 (defun org-element-set-element (old new)
556 "Replace element or object OLD with element or object NEW.
557 The function takes care of setting `:parent' property for NEW."
558 ;; Ensure OLD and NEW have the same parent.
559 (org-element-put-property new :parent (org-element-property :parent old))
560 (if (or (memq (org-element-type old) '(plain-text nil))
561 (memq (org-element-type new) '(plain-text nil)))
562 ;; We cannot replace OLD with NEW since one of them is not an
563 ;; object or element. We take the long path.
564 (progn (org-element-insert-before new old)
565 (org-element-extract-element old))
566 ;; Since OLD is going to be changed into NEW by side-effect, first
567 ;; make sure that every element or object within NEW has OLD as
568 ;; parent.
569 (dolist (blob (org-element-contents new))
570 (org-element-put-property blob :parent old))
571 ;; Transfer contents.
572 (apply #'org-element-set-contents old (org-element-contents new))
573 ;; Overwrite OLD's properties with NEW's.
574 (setcar (cdr old) (nth 1 new))
575 ;; Transfer type.
576 (setcar old (car new))))
578 (defun org-element-create (type &optional props &rest children)
579 "Create a new element of type TYPE.
580 Optional argument PROPS, when non-nil, is a plist defining the
581 properties of the element. CHILDREN can be elements, objects or
582 strings."
583 (apply #'org-element-adopt-elements (list type props) children))
585 (defun org-element-copy (datum)
586 "Return a copy of DATUM.
587 DATUM is an element, object, string or nil. `:parent' property
588 is cleared and contents are removed in the process."
589 (when datum
590 (let ((type (org-element-type datum)))
591 (pcase type
592 (`org-data (list 'org-data nil))
593 (`plain-text (substring-no-properties datum))
594 (`nil (copy-sequence datum))
596 (list type (plist-put (copy-sequence (nth 1 datum)) :parent nil)))))))
600 ;;; Greater elements
602 ;; For each greater element type, we define a parser and an
603 ;; interpreter.
605 ;; A parser returns the element or object as the list described above.
606 ;; Most of them accepts no argument. Though, exceptions exist. Hence
607 ;; every element containing a secondary string (see
608 ;; `org-element-secondary-value-alist') will accept an optional
609 ;; argument to toggle parsing of these secondary strings. Moreover,
610 ;; `item' parser requires current list's structure as its first
611 ;; element.
613 ;; An interpreter accepts two arguments: the list representation of
614 ;; the element or object, and its contents. The latter may be nil,
615 ;; depending on the element or object considered. It returns the
616 ;; appropriate Org syntax, as a string.
618 ;; Parsing functions must follow the naming convention:
619 ;; org-element-TYPE-parser, where TYPE is greater element's type, as
620 ;; defined in `org-element-greater-elements'.
622 ;; Similarly, interpreting functions must follow the naming
623 ;; convention: org-element-TYPE-interpreter.
625 ;; With the exception of `headline' and `item' types, greater elements
626 ;; cannot contain other greater elements of their own type.
628 ;; Beside implementing a parser and an interpreter, adding a new
629 ;; greater element requires tweaking `org-element--current-element'.
630 ;; Moreover, the newly defined type must be added to both
631 ;; `org-element-all-elements' and `org-element-greater-elements'.
634 ;;;; Center Block
636 (defun org-element-center-block-parser (limit affiliated)
637 "Parse a center block.
639 LIMIT bounds the search. AFFILIATED is a list of which CAR is
640 the buffer position at the beginning of the first affiliated
641 keyword and CDR is a plist of affiliated keywords along with
642 their value.
644 Return a list whose CAR is `center-block' and CDR is a plist
645 containing `:begin', `:end', `:contents-begin', `:contents-end',
646 `:post-blank' and `:post-affiliated' keywords.
648 Assume point is at the beginning of the block."
649 (let ((case-fold-search t))
650 (if (not (save-excursion
651 (re-search-forward "^[ \t]*#\\+END_CENTER[ \t]*$" limit t)))
652 ;; Incomplete block: parse it as a paragraph.
653 (org-element-paragraph-parser limit affiliated)
654 (let ((block-end-line (match-beginning 0)))
655 (let* ((begin (car affiliated))
656 (post-affiliated (point))
657 ;; Empty blocks have no contents.
658 (contents-begin (progn (forward-line)
659 (and (< (point) block-end-line)
660 (point))))
661 (contents-end (and contents-begin block-end-line))
662 (pos-before-blank (progn (goto-char block-end-line)
663 (forward-line)
664 (point)))
665 (end (save-excursion
666 (skip-chars-forward " \r\t\n" limit)
667 (if (eobp) (point) (line-beginning-position)))))
668 (list 'center-block
669 (nconc
670 (list :begin begin
671 :end end
672 :contents-begin contents-begin
673 :contents-end contents-end
674 :post-blank (count-lines pos-before-blank end)
675 :post-affiliated post-affiliated)
676 (cdr affiliated))))))))
678 (defun org-element-center-block-interpreter (_ contents)
679 "Interpret a center-block element as Org syntax.
680 CONTENTS is the contents of the element."
681 (format "#+BEGIN_CENTER\n%s#+END_CENTER" contents))
684 ;;;; Drawer
686 (defun org-element-drawer-parser (limit affiliated)
687 "Parse a drawer.
689 LIMIT bounds the search. AFFILIATED is a list of which CAR is
690 the buffer position at the beginning of the first affiliated
691 keyword and CDR is a plist of affiliated keywords along with
692 their value.
694 Return a list whose CAR is `drawer' and CDR is a plist containing
695 `:drawer-name', `:begin', `:end', `:contents-begin',
696 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
698 Assume point is at beginning of drawer."
699 (let ((case-fold-search t))
700 (if (not (save-excursion (re-search-forward "^[ \t]*:END:[ \t]*$" limit t)))
701 ;; Incomplete drawer: parse it as a paragraph.
702 (org-element-paragraph-parser limit affiliated)
703 (save-excursion
704 (let* ((drawer-end-line (match-beginning 0))
705 (name (progn (looking-at org-drawer-regexp)
706 (match-string-no-properties 1)))
707 (begin (car affiliated))
708 (post-affiliated (point))
709 ;; Empty drawers have no contents.
710 (contents-begin (progn (forward-line)
711 (and (< (point) drawer-end-line)
712 (point))))
713 (contents-end (and contents-begin drawer-end-line))
714 (pos-before-blank (progn (goto-char drawer-end-line)
715 (forward-line)
716 (point)))
717 (end (progn (skip-chars-forward " \r\t\n" limit)
718 (if (eobp) (point) (line-beginning-position)))))
719 (list 'drawer
720 (nconc
721 (list :begin begin
722 :end end
723 :drawer-name name
724 :contents-begin contents-begin
725 :contents-end contents-end
726 :post-blank (count-lines pos-before-blank end)
727 :post-affiliated post-affiliated)
728 (cdr affiliated))))))))
730 (defun org-element-drawer-interpreter (drawer contents)
731 "Interpret DRAWER element as Org syntax.
732 CONTENTS is the contents of the element."
733 (format ":%s:\n%s:END:"
734 (org-element-property :drawer-name drawer)
735 contents))
738 ;;;; Dynamic Block
740 (defun org-element-dynamic-block-parser (limit affiliated)
741 "Parse a dynamic block.
743 LIMIT bounds the search. AFFILIATED is a list of which CAR is
744 the buffer position at the beginning of the first affiliated
745 keyword and CDR is a plist of affiliated keywords along with
746 their value.
748 Return a list whose CAR is `dynamic-block' and CDR is a plist
749 containing `:block-name', `:begin', `:end', `:contents-begin',
750 `:contents-end', `:arguments', `:post-blank' and
751 `:post-affiliated' keywords.
753 Assume point is at beginning of dynamic block."
754 (let ((case-fold-search t))
755 (if (not (save-excursion
756 (re-search-forward "^[ \t]*#\\+END:?[ \t]*$" limit t)))
757 ;; Incomplete block: parse it as a paragraph.
758 (org-element-paragraph-parser limit affiliated)
759 (let ((block-end-line (match-beginning 0)))
760 (save-excursion
761 (let* ((name (progn (looking-at org-dblock-start-re)
762 (match-string-no-properties 1)))
763 (arguments (match-string-no-properties 3))
764 (begin (car affiliated))
765 (post-affiliated (point))
766 ;; Empty blocks have no contents.
767 (contents-begin (progn (forward-line)
768 (and (< (point) block-end-line)
769 (point))))
770 (contents-end (and contents-begin block-end-line))
771 (pos-before-blank (progn (goto-char block-end-line)
772 (forward-line)
773 (point)))
774 (end (progn (skip-chars-forward " \r\t\n" limit)
775 (if (eobp) (point) (line-beginning-position)))))
776 (list 'dynamic-block
777 (nconc
778 (list :begin begin
779 :end end
780 :block-name name
781 :arguments arguments
782 :contents-begin contents-begin
783 :contents-end contents-end
784 :post-blank (count-lines pos-before-blank end)
785 :post-affiliated post-affiliated)
786 (cdr affiliated)))))))))
788 (defun org-element-dynamic-block-interpreter (dynamic-block contents)
789 "Interpret DYNAMIC-BLOCK element as Org syntax.
790 CONTENTS is the contents of the element."
791 (format "#+BEGIN: %s%s\n%s#+END:"
792 (org-element-property :block-name dynamic-block)
793 (let ((args (org-element-property :arguments dynamic-block)))
794 (if args (concat " " args) ""))
795 contents))
798 ;;;; Footnote Definition
800 (defconst org-element--footnote-separator
801 (concat org-outline-regexp-bol "\\|"
802 org-footnote-definition-re "\\|"
803 "^\\([ \t]*\n\\)\\{2,\\}")
804 "Regexp used as a footnote definition separator.")
806 (defun org-element-footnote-definition-parser (limit affiliated)
807 "Parse a footnote definition.
809 LIMIT bounds the search. AFFILIATED is a list of which CAR is
810 the buffer position at the beginning of the first affiliated
811 keyword and CDR is a plist of affiliated keywords along with
812 their value.
814 Return a list whose CAR is `footnote-definition' and CDR is
815 a plist containing `:label', `:begin' `:end', `:contents-begin',
816 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
818 Assume point is at the beginning of the footnote definition."
819 (save-excursion
820 (let* ((label (progn (looking-at org-footnote-definition-re)
821 (match-string-no-properties 1)))
822 (begin (car affiliated))
823 (post-affiliated (point))
824 (end
825 (save-excursion
826 (end-of-line)
827 (cond
828 ((not
829 (re-search-forward org-element--footnote-separator limit t))
830 limit)
831 ((eq ?\[ (char-after (match-beginning 0)))
832 ;; At a new footnote definition, make sure we end
833 ;; before any affiliated keyword above.
834 (forward-line -1)
835 (while (and (> (point) post-affiliated)
836 (looking-at-p org-element--affiliated-re))
837 (forward-line -1))
838 (line-beginning-position 2))
839 ((eq ?* (char-after (match-beginning 0))) (match-beginning 0))
840 (t (skip-chars-forward " \r\t\n" limit)
841 (if (= limit (point)) limit (line-beginning-position))))))
842 (contents-begin
843 (progn (search-forward "]")
844 (skip-chars-forward " \r\t\n" end)
845 (cond ((= (point) end) nil)
846 ((= (line-beginning-position) post-affiliated) (point))
847 (t (line-beginning-position)))))
848 (contents-end
849 (progn (goto-char end)
850 (skip-chars-backward " \r\t\n")
851 (line-beginning-position 2))))
852 (list 'footnote-definition
853 (nconc
854 (list :label label
855 :begin begin
856 :end end
857 :contents-begin contents-begin
858 :contents-end (and contents-begin contents-end)
859 :post-blank (count-lines contents-end end)
860 :post-affiliated post-affiliated)
861 (cdr affiliated))))))
863 (defun org-element-footnote-definition-interpreter (footnote-definition contents)
864 "Interpret FOOTNOTE-DEFINITION element as Org syntax.
865 CONTENTS is the contents of the footnote-definition."
866 (concat (format "[fn:%s]" (org-element-property :label footnote-definition))
868 contents))
871 ;;;; Headline
873 (defun org-element--get-node-properties ()
874 "Return node properties associated to headline at point.
875 Upcase property names. It avoids confusion between properties
876 obtained through property drawer and default properties from the
877 parser (e.g. `:end' and :END:). Return value is a plist."
878 (save-excursion
879 (forward-line)
880 (when (looking-at-p org-planning-line-re) (forward-line))
881 (when (looking-at org-property-drawer-re)
882 (forward-line)
883 (let ((end (match-end 0)) properties)
884 (while (< (line-end-position) end)
885 (looking-at org-property-re)
886 (push (match-string-no-properties 3) properties)
887 (push (intern (concat ":" (upcase (match-string 2)))) properties)
888 (forward-line))
889 properties))))
891 (defun org-element--get-time-properties ()
892 "Return time properties associated to headline at point.
893 Return value is a plist."
894 (save-excursion
895 (when (progn (forward-line) (looking-at org-planning-line-re))
896 (let ((end (line-end-position)) plist)
897 (while (re-search-forward org-keyword-time-not-clock-regexp end t)
898 (goto-char (match-end 1))
899 (skip-chars-forward " \t")
900 (let ((keyword (match-string 1))
901 (time (org-element-timestamp-parser)))
902 (cond ((equal keyword org-scheduled-string)
903 (setq plist (plist-put plist :scheduled time)))
904 ((equal keyword org-deadline-string)
905 (setq plist (plist-put plist :deadline time)))
906 (t (setq plist (plist-put plist :closed time))))))
907 plist))))
909 (defun org-element-headline-parser (limit &optional raw-secondary-p)
910 "Parse a headline.
912 Return a list whose CAR is `headline' and CDR is a plist
913 containing `:raw-value', `:title', `:begin', `:end',
914 `:pre-blank', `:contents-begin' and `:contents-end', `:level',
915 `:priority', `:tags', `:todo-keyword',`:todo-type', `:scheduled',
916 `:deadline', `:closed', `:archivedp', `:commentedp'
917 `:footnote-section-p', `:post-blank' and `:post-affiliated'
918 keywords.
920 The plist also contains any property set in the property drawer,
921 with its name in upper cases and colons added at the
922 beginning (e.g., `:CUSTOM_ID').
924 LIMIT is a buffer position bounding the search.
926 When RAW-SECONDARY-P is non-nil, headline's title will not be
927 parsed as a secondary string, but as a plain string instead.
929 Assume point is at beginning of the headline."
930 (save-excursion
931 (let* ((begin (point))
932 (level (prog1 (org-reduced-level (skip-chars-forward "*"))
933 (skip-chars-forward " \t")))
934 (todo (and org-todo-regexp
935 (let (case-fold-search) (looking-at org-todo-regexp))
936 (progn (goto-char (match-end 0))
937 (skip-chars-forward " \t")
938 (match-string 0))))
939 (todo-type
940 (and todo (if (member todo org-done-keywords) 'done 'todo)))
941 (priority (and (looking-at "\\[#.\\][ \t]*")
942 (progn (goto-char (match-end 0))
943 (aref (match-string 0) 2))))
944 (commentedp
945 (and (let (case-fold-search) (looking-at org-comment-string))
946 (goto-char (match-end 0))))
947 (title-start (point))
948 (tags (when (re-search-forward
949 "[ \t]+\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"
950 (line-end-position)
951 'move)
952 (goto-char (match-beginning 0))
953 (org-split-string (match-string 1) ":")))
954 (title-end (point))
955 (raw-value (org-trim
956 (buffer-substring-no-properties title-start title-end)))
957 (archivedp (member org-archive-tag tags))
958 (footnote-section-p (and org-footnote-section
959 (string= org-footnote-section raw-value)))
960 (standard-props (org-element--get-node-properties))
961 (time-props (org-element--get-time-properties))
962 (end (min (save-excursion (org-end-of-subtree t t)) limit))
963 (contents-begin (save-excursion
964 (forward-line)
965 (skip-chars-forward " \r\t\n" end)
966 (and (/= (point) end) (line-beginning-position))))
967 (contents-end (and contents-begin
968 (progn (goto-char end)
969 (skip-chars-backward " \r\t\n")
970 (line-beginning-position 2)))))
971 (let ((headline
972 (list 'headline
973 (nconc
974 (list :raw-value raw-value
975 :begin begin
976 :end end
977 :pre-blank
978 (if (not contents-begin) 0
979 (1- (count-lines begin contents-begin)))
980 :contents-begin contents-begin
981 :contents-end contents-end
982 :level level
983 :priority priority
984 :tags tags
985 :todo-keyword todo
986 :todo-type todo-type
987 :post-blank
988 (if contents-end
989 (count-lines contents-end end)
990 (1- (count-lines begin end)))
991 :footnote-section-p footnote-section-p
992 :archivedp archivedp
993 :commentedp commentedp
994 :post-affiliated begin)
995 time-props
996 standard-props))))
997 (org-element-put-property
998 headline :title
999 (if raw-secondary-p raw-value
1000 (org-element--parse-objects
1001 (progn (goto-char title-start)
1002 (skip-chars-forward " \t")
1003 (point))
1004 (progn (goto-char title-end)
1005 (skip-chars-backward " \t")
1006 (point))
1008 (org-element-restriction 'headline)
1009 headline)))))))
1011 (defun org-element-headline-interpreter (headline contents)
1012 "Interpret HEADLINE element as Org syntax.
1013 CONTENTS is the contents of the element."
1014 (let* ((level (org-element-property :level headline))
1015 (todo (org-element-property :todo-keyword headline))
1016 (priority (org-element-property :priority headline))
1017 (title (org-element-interpret-data
1018 (org-element-property :title headline)))
1019 (tags (let ((tag-list (org-element-property :tags headline)))
1020 (and tag-list
1021 (format ":%s:" (mapconcat #'identity tag-list ":")))))
1022 (commentedp (org-element-property :commentedp headline))
1023 (pre-blank (or (org-element-property :pre-blank headline) 0))
1024 (heading
1025 (concat (make-string (if org-odd-levels-only (1- (* level 2)) level)
1027 (and todo (concat " " todo))
1028 (and commentedp (concat " " org-comment-string))
1029 (and priority (format " [#%c]" priority))
1031 (if (and org-footnote-section
1032 (org-element-property :footnote-section-p headline))
1033 org-footnote-section
1034 title))))
1035 (concat
1036 heading
1037 ;; Align tags.
1038 (when tags
1039 (cond
1040 ((zerop org-tags-column) (format " %s" tags))
1041 ((< org-tags-column 0)
1042 (concat
1043 (make-string
1044 (max (- (+ org-tags-column (length heading) (length tags))) 1)
1045 ?\s)
1046 tags))
1048 (concat
1049 (make-string (max (- org-tags-column (length heading)) 1) ?\s)
1050 tags))))
1051 (make-string (1+ pre-blank) ?\n)
1052 contents)))
1055 ;;;; Inlinetask
1057 (defun org-element-inlinetask-parser (limit &optional raw-secondary-p)
1058 "Parse an inline task.
1060 Return a list whose CAR is `inlinetask' and CDR is a plist
1061 containing `:title', `:begin', `:end', `:pre-blank',
1062 `:contents-begin' and `:contents-end', `:level', `:priority',
1063 `:raw-value', `:tags', `:todo-keyword', `:todo-type',
1064 `:scheduled', `:deadline', `:closed', `:post-blank' and
1065 `:post-affiliated' keywords.
1067 The plist also contains any property set in the property drawer,
1068 with its name in upper cases and colons added at the
1069 beginning (e.g., `:CUSTOM_ID').
1071 When optional argument RAW-SECONDARY-P is non-nil, inline-task's
1072 title will not be parsed as a secondary string, but as a plain
1073 string instead.
1075 Assume point is at beginning of the inline task."
1076 (save-excursion
1077 (let* ((begin (point))
1078 (level (prog1 (org-reduced-level (skip-chars-forward "*"))
1079 (skip-chars-forward " \t")))
1080 (todo (and org-todo-regexp
1081 (let (case-fold-search) (looking-at org-todo-regexp))
1082 (progn (goto-char (match-end 0))
1083 (skip-chars-forward " \t")
1084 (match-string 0))))
1085 (todo-type (and todo
1086 (if (member todo org-done-keywords) 'done 'todo)))
1087 (priority (and (looking-at "\\[#.\\][ \t]*")
1088 (progn (goto-char (match-end 0))
1089 (aref (match-string 0) 2))))
1090 (title-start (point))
1091 (tags (when (re-search-forward
1092 "[ \t]+\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"
1093 (line-end-position)
1094 'move)
1095 (goto-char (match-beginning 0))
1096 (org-split-string (match-string 1) ":")))
1097 (title-end (point))
1098 (raw-value (org-trim
1099 (buffer-substring-no-properties title-start title-end)))
1100 (task-end (save-excursion
1101 (end-of-line)
1102 (and (re-search-forward org-outline-regexp-bol limit t)
1103 (looking-at-p "[ \t]*END[ \t]*$")
1104 (line-beginning-position))))
1105 (standard-props (and task-end (org-element--get-node-properties)))
1106 (time-props (and task-end (org-element--get-time-properties)))
1107 (contents-begin (and task-end
1108 (< (point) task-end)
1109 (progn
1110 (forward-line)
1111 (skip-chars-forward " \t\n")
1112 (line-beginning-position))))
1113 (contents-end (and contents-begin task-end))
1114 (end (progn (when task-end (goto-char task-end))
1115 (forward-line)
1116 (skip-chars-forward " \r\t\n" limit)
1117 (if (eobp) (point) (line-beginning-position))))
1118 (inlinetask
1119 (list 'inlinetask
1120 (nconc
1121 (list :raw-value raw-value
1122 :begin begin
1123 :end end
1124 :pre-blank
1125 (if (not contents-begin) 0
1126 (1- (count-lines begin contents-begin)))
1127 :contents-begin contents-begin
1128 :contents-end contents-end
1129 :level level
1130 :priority priority
1131 :tags tags
1132 :todo-keyword todo
1133 :todo-type todo-type
1134 :post-blank (1- (count-lines (or task-end begin) end))
1135 :post-affiliated begin)
1136 time-props
1137 standard-props))))
1138 (org-element-put-property
1139 inlinetask :title
1140 (if raw-secondary-p raw-value
1141 (org-element--parse-objects
1142 (progn (goto-char title-start)
1143 (skip-chars-forward " \t")
1144 (point))
1145 (progn (goto-char title-end)
1146 (skip-chars-backward " \t")
1147 (point))
1149 (org-element-restriction 'inlinetask)
1150 inlinetask))))))
1152 (defun org-element-inlinetask-interpreter (inlinetask contents)
1153 "Interpret INLINETASK element as Org syntax.
1154 CONTENTS is the contents of inlinetask."
1155 (let* ((level (org-element-property :level inlinetask))
1156 (todo (org-element-property :todo-keyword inlinetask))
1157 (priority (org-element-property :priority inlinetask))
1158 (title (org-element-interpret-data
1159 (org-element-property :title inlinetask)))
1160 (tags (let ((tag-list (org-element-property :tags inlinetask)))
1161 (and tag-list
1162 (format ":%s:" (mapconcat 'identity tag-list ":")))))
1163 (task (concat (make-string level ?*)
1164 (and todo (concat " " todo))
1165 (and priority (format " [#%c]" priority))
1166 (and title (concat " " title)))))
1167 (concat task
1168 ;; Align tags.
1169 (when tags
1170 (cond
1171 ((zerop org-tags-column) (format " %s" tags))
1172 ((< org-tags-column 0)
1173 (concat
1174 (make-string
1175 (max (- (+ org-tags-column (length task) (length tags))) 1)
1177 tags))
1179 (concat
1180 (make-string (max (- org-tags-column (length task)) 1) ? )
1181 tags))))
1182 ;; Prefer degenerate inlinetasks when there are no
1183 ;; contents.
1184 (when contents
1185 (concat "\n"
1186 contents
1187 (make-string level ?*) " END")))))
1190 ;;;; Item
1192 (defun org-element-item-parser (_ struct &optional raw-secondary-p)
1193 "Parse an item.
1195 STRUCT is the structure of the plain list.
1197 Return a list whose CAR is `item' and CDR is a plist containing
1198 `:bullet', `:begin', `:end', `:contents-begin', `:contents-end',
1199 `:checkbox', `:counter', `:tag', `:structure', `:post-blank' and
1200 `:post-affiliated' keywords.
1202 When optional argument RAW-SECONDARY-P is non-nil, item's tag, if
1203 any, will not be parsed as a secondary string, but as a plain
1204 string instead.
1206 Assume point is at the beginning of the item."
1207 (save-excursion
1208 (beginning-of-line)
1209 (looking-at org-list-full-item-re)
1210 (let* ((begin (point))
1211 (bullet (match-string-no-properties 1))
1212 (checkbox (let ((box (match-string 3)))
1213 (cond ((equal "[ ]" box) 'off)
1214 ((equal "[X]" box) 'on)
1215 ((equal "[-]" box) 'trans))))
1216 (counter (let ((c (match-string 2)))
1217 (save-match-data
1218 (cond
1219 ((not c) nil)
1220 ((string-match "[A-Za-z]" c)
1221 (- (string-to-char (upcase (match-string 0 c)))
1222 64))
1223 ((string-match "[0-9]+" c)
1224 (string-to-number (match-string 0 c)))))))
1225 (end (progn (goto-char (nth 6 (assq (point) struct)))
1226 (if (bolp) (point) (line-beginning-position 2))))
1227 (contents-begin
1228 (progn (goto-char
1229 ;; Ignore tags in un-ordered lists: they are just
1230 ;; a part of item's body.
1231 (if (and (match-beginning 4)
1232 (save-match-data (string-match "[.)]" bullet)))
1233 (match-beginning 4)
1234 (match-end 0)))
1235 (skip-chars-forward " \r\t\n" end)
1236 (cond ((= (point) end) nil)
1237 ;; If first line isn't empty, contents really
1238 ;; start at the text after item's meta-data.
1239 ((= (line-beginning-position) begin) (point))
1240 (t (line-beginning-position)))))
1241 (contents-end (and contents-begin
1242 (progn (goto-char end)
1243 (skip-chars-backward " \r\t\n")
1244 (line-beginning-position 2))))
1245 (item
1246 (list 'item
1247 (list :bullet bullet
1248 :begin begin
1249 :end end
1250 :contents-begin contents-begin
1251 :contents-end contents-end
1252 :checkbox checkbox
1253 :counter counter
1254 :structure struct
1255 :post-blank (count-lines (or contents-end begin) end)
1256 :post-affiliated begin))))
1257 (org-element-put-property
1258 item :tag
1259 (let ((raw (org-list-get-tag begin struct)))
1260 (when raw
1261 (if raw-secondary-p raw
1262 (org-element--parse-objects
1263 (match-beginning 4) (match-end 4) nil
1264 (org-element-restriction 'item)
1265 item))))))))
1267 (defun org-element-item-interpreter (item contents)
1268 "Interpret ITEM element as Org syntax.
1269 CONTENTS is the contents of the element."
1270 (let* ((bullet (let ((bullet (org-element-property :bullet item)))
1271 (org-list-bullet-string
1272 (cond ((not (string-match "[0-9a-zA-Z]" bullet)) "- ")
1273 ((eq org-plain-list-ordered-item-terminator ?\)) "1)")
1274 (t "1.")))))
1275 (checkbox (org-element-property :checkbox item))
1276 (counter (org-element-property :counter item))
1277 (tag (let ((tag (org-element-property :tag item)))
1278 (and tag (org-element-interpret-data tag))))
1279 ;; Compute indentation.
1280 (ind (make-string (length bullet) 32))
1281 (item-starts-with-par-p
1282 (eq (org-element-type (car (org-element-contents item)))
1283 'paragraph)))
1284 ;; Indent contents.
1285 (concat
1286 bullet
1287 (and counter (format "[@%d] " counter))
1288 (pcase checkbox
1289 (`on "[X] ")
1290 (`off "[ ] ")
1291 (`trans "[-] ")
1292 (_ nil))
1293 (and tag (format "%s :: " tag))
1294 (when contents
1295 (let ((contents (replace-regexp-in-string
1296 "\\(^\\)[ \t]*\\S-" ind contents nil nil 1)))
1297 (if item-starts-with-par-p (org-trim contents)
1298 (concat "\n" contents)))))))
1301 ;;;; Plain List
1303 (defun org-element--list-struct (limit)
1304 ;; Return structure of list at point. Internal function. See
1305 ;; `org-list-struct' for details.
1306 (let ((case-fold-search t)
1307 (top-ind limit)
1308 (item-re (org-item-re))
1309 (inlinetask-re (and (featurep 'org-inlinetask) "^\\*+ "))
1310 items struct)
1311 (save-excursion
1312 (catch 'exit
1313 (while t
1314 (cond
1315 ;; At limit: end all items.
1316 ((>= (point) limit)
1317 (throw 'exit
1318 (let ((end (progn (skip-chars-backward " \r\t\n")
1319 (forward-line)
1320 (point))))
1321 (dolist (item items (sort (nconc items struct)
1322 'car-less-than-car))
1323 (setcar (nthcdr 6 item) end)))))
1324 ;; At list end: end all items.
1325 ((looking-at org-list-end-re)
1326 (throw 'exit (dolist (item items (sort (nconc items struct)
1327 'car-less-than-car))
1328 (setcar (nthcdr 6 item) (point)))))
1329 ;; At a new item: end previous sibling.
1330 ((looking-at item-re)
1331 (let ((ind (save-excursion (skip-chars-forward " \t")
1332 (current-column))))
1333 (setq top-ind (min top-ind ind))
1334 (while (and items (<= ind (nth 1 (car items))))
1335 (let ((item (pop items)))
1336 (setcar (nthcdr 6 item) (point))
1337 (push item struct)))
1338 (push (progn (looking-at org-list-full-item-re)
1339 (let ((bullet (match-string-no-properties 1)))
1340 (list (point)
1342 bullet
1343 (match-string-no-properties 2) ; counter
1344 (match-string-no-properties 3) ; checkbox
1345 ;; Description tag.
1346 (and (save-match-data
1347 (string-match "[-+*]" bullet))
1348 (match-string-no-properties 4))
1349 ;; Ending position, unknown so far.
1350 nil)))
1351 items))
1352 (forward-line 1))
1353 ;; Skip empty lines.
1354 ((looking-at "^[ \t]*$") (forward-line))
1355 ;; Skip inline tasks and blank lines along the way.
1356 ((and inlinetask-re (looking-at inlinetask-re))
1357 (forward-line)
1358 (let ((origin (point)))
1359 (when (re-search-forward inlinetask-re limit t)
1360 (if (looking-at-p "END[ \t]*$") (forward-line)
1361 (goto-char origin)))))
1362 ;; At some text line. Check if it ends any previous item.
1364 (let ((ind (save-excursion (skip-chars-forward " \t")
1365 (current-column))))
1366 (when (<= ind top-ind)
1367 (skip-chars-backward " \r\t\n")
1368 (forward-line))
1369 (while (<= ind (nth 1 (car items)))
1370 (let ((item (pop items)))
1371 (setcar (nthcdr 6 item) (line-beginning-position))
1372 (push item struct)
1373 (unless items
1374 (throw 'exit (sort struct #'car-less-than-car))))))
1375 ;; Skip blocks (any type) and drawers contents.
1376 (cond
1377 ((and (looking-at "[ \t]*#\\+BEGIN\\(:\\|_\\S-+\\)")
1378 (re-search-forward
1379 (format "^[ \t]*#\\+END%s[ \t]*$" (match-string 1))
1380 limit t)))
1381 ((and (looking-at org-drawer-regexp)
1382 (re-search-forward "^[ \t]*:END:[ \t]*$" limit t))))
1383 (forward-line))))))))
1385 (defun org-element-plain-list-parser (limit affiliated structure)
1386 "Parse a plain list.
1388 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1389 the buffer position at the beginning of the first affiliated
1390 keyword and CDR is a plist of affiliated keywords along with
1391 their value. STRUCTURE is the structure of the plain list being
1392 parsed.
1394 Return a list whose CAR is `plain-list' and CDR is a plist
1395 containing `:type', `:begin', `:end', `:contents-begin' and
1396 `:contents-end', `:structure', `:post-blank' and
1397 `:post-affiliated' keywords.
1399 Assume point is at the beginning of the list."
1400 (save-excursion
1401 (let* ((struct (or structure (org-element--list-struct limit)))
1402 (type (cond ((looking-at-p "[ \t]*[A-Za-z0-9]") 'ordered)
1403 ((nth 5 (assq (point) struct)) 'descriptive)
1404 (t 'unordered)))
1405 (contents-begin (point))
1406 (begin (car affiliated))
1407 (contents-end (let* ((item (assq contents-begin struct))
1408 (ind (nth 1 item))
1409 (pos (nth 6 item)))
1410 (while (and (setq item (assq pos struct))
1411 (= (nth 1 item) ind))
1412 (setq pos (nth 6 item)))
1413 pos))
1414 (end (progn (goto-char contents-end)
1415 (skip-chars-forward " \r\t\n" limit)
1416 (if (= (point) limit) limit (line-beginning-position)))))
1417 ;; Return value.
1418 (list 'plain-list
1419 (nconc
1420 (list :type type
1421 :begin begin
1422 :end end
1423 :contents-begin contents-begin
1424 :contents-end contents-end
1425 :structure struct
1426 :post-blank (count-lines contents-end end)
1427 :post-affiliated contents-begin)
1428 (cdr affiliated))))))
1430 (defun org-element-plain-list-interpreter (_ contents)
1431 "Interpret plain-list element as Org syntax.
1432 CONTENTS is the contents of the element."
1433 (with-temp-buffer
1434 (insert contents)
1435 (goto-char (point-min))
1436 (org-list-repair)
1437 (buffer-string)))
1440 ;;;; Property Drawer
1442 (defun org-element-property-drawer-parser (limit)
1443 "Parse a property drawer.
1445 LIMIT bounds the search.
1447 Return a list whose car is `property-drawer' and cdr is a plist
1448 containing `:begin', `:end', `:contents-begin', `:contents-end',
1449 `:post-blank' and `:post-affiliated' keywords.
1451 Assume point is at the beginning of the property drawer."
1452 (save-excursion
1453 (let ((case-fold-search t)
1454 (begin (point))
1455 (contents-begin (line-beginning-position 2)))
1456 (re-search-forward "^[ \t]*:END:[ \t]*$" limit t)
1457 (let ((contents-end (and (> (match-beginning 0) contents-begin)
1458 (match-beginning 0)))
1459 (before-blank (progn (forward-line) (point)))
1460 (end (progn (skip-chars-forward " \r\t\n" limit)
1461 (if (eobp) (point) (line-beginning-position)))))
1462 (list 'property-drawer
1463 (list :begin begin
1464 :end end
1465 :contents-begin (and contents-end contents-begin)
1466 :contents-end contents-end
1467 :post-blank (count-lines before-blank end)
1468 :post-affiliated begin))))))
1470 (defun org-element-property-drawer-interpreter (_ contents)
1471 "Interpret property-drawer element as Org syntax.
1472 CONTENTS is the properties within the drawer."
1473 (format ":PROPERTIES:\n%s:END:" contents))
1476 ;;;; Quote Block
1478 (defun org-element-quote-block-parser (limit affiliated)
1479 "Parse a quote block.
1481 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1482 the buffer position at the beginning of the first affiliated
1483 keyword and CDR is a plist of affiliated keywords along with
1484 their value.
1486 Return a list whose CAR is `quote-block' and CDR is a plist
1487 containing `:begin', `:end', `:contents-begin', `:contents-end',
1488 `:post-blank' and `:post-affiliated' keywords.
1490 Assume point is at the beginning of the block."
1491 (let ((case-fold-search t))
1492 (if (not (save-excursion
1493 (re-search-forward "^[ \t]*#\\+END_QUOTE[ \t]*$" limit t)))
1494 ;; Incomplete block: parse it as a paragraph.
1495 (org-element-paragraph-parser limit affiliated)
1496 (let ((block-end-line (match-beginning 0)))
1497 (save-excursion
1498 (let* ((begin (car affiliated))
1499 (post-affiliated (point))
1500 ;; Empty blocks have no contents.
1501 (contents-begin (progn (forward-line)
1502 (and (< (point) block-end-line)
1503 (point))))
1504 (contents-end (and contents-begin block-end-line))
1505 (pos-before-blank (progn (goto-char block-end-line)
1506 (forward-line)
1507 (point)))
1508 (end (progn (skip-chars-forward " \r\t\n" limit)
1509 (if (eobp) (point) (line-beginning-position)))))
1510 (list 'quote-block
1511 (nconc
1512 (list :begin begin
1513 :end end
1514 :contents-begin contents-begin
1515 :contents-end contents-end
1516 :post-blank (count-lines pos-before-blank end)
1517 :post-affiliated post-affiliated)
1518 (cdr affiliated)))))))))
1520 (defun org-element-quote-block-interpreter (_ contents)
1521 "Interpret quote-block element as Org syntax.
1522 CONTENTS is the contents of the element."
1523 (format "#+BEGIN_QUOTE\n%s#+END_QUOTE" contents))
1526 ;;;; Section
1528 (defun org-element-section-parser (_)
1529 "Parse a section.
1531 Return a list whose CAR is `section' and CDR is a plist
1532 containing `:begin', `:end', `:contents-begin', `contents-end',
1533 `:post-blank' and `:post-affiliated' keywords."
1534 (save-excursion
1535 ;; Beginning of section is the beginning of the first non-blank
1536 ;; line after previous headline.
1537 (let ((begin (point))
1538 (end (progn (org-with-limited-levels (outline-next-heading))
1539 (point)))
1540 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
1541 (line-beginning-position 2))))
1542 (list 'section
1543 (list :begin begin
1544 :end end
1545 :contents-begin begin
1546 :contents-end pos-before-blank
1547 :post-blank (count-lines pos-before-blank end)
1548 :post-affiliated begin)))))
1550 (defun org-element-section-interpreter (_ contents)
1551 "Interpret section element as Org syntax.
1552 CONTENTS is the contents of the element."
1553 contents)
1556 ;;;; Special Block
1558 (defun org-element-special-block-parser (limit affiliated)
1559 "Parse a special block.
1561 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1562 the buffer position at the beginning of the first affiliated
1563 keyword and CDR is a plist of affiliated keywords along with
1564 their value.
1566 Return a list whose CAR is `special-block' and CDR is a plist
1567 containing `:type', `:begin', `:end', `:contents-begin',
1568 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
1570 Assume point is at the beginning of the block."
1571 (let* ((case-fold-search t)
1572 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
1573 (match-string-no-properties 1))))
1574 (if (not (save-excursion
1575 (re-search-forward
1576 (format "^[ \t]*#\\+END_%s[ \t]*$" (regexp-quote type))
1577 limit t)))
1578 ;; Incomplete block: parse it as a paragraph.
1579 (org-element-paragraph-parser limit affiliated)
1580 (let ((block-end-line (match-beginning 0)))
1581 (save-excursion
1582 (let* ((begin (car affiliated))
1583 (post-affiliated (point))
1584 ;; Empty blocks have no contents.
1585 (contents-begin (progn (forward-line)
1586 (and (< (point) block-end-line)
1587 (point))))
1588 (contents-end (and contents-begin block-end-line))
1589 (pos-before-blank (progn (goto-char block-end-line)
1590 (forward-line)
1591 (point)))
1592 (end (progn (skip-chars-forward " \r\t\n" limit)
1593 (if (eobp) (point) (line-beginning-position)))))
1594 (list 'special-block
1595 (nconc
1596 (list :type type
1597 :begin begin
1598 :end end
1599 :contents-begin contents-begin
1600 :contents-end contents-end
1601 :post-blank (count-lines pos-before-blank end)
1602 :post-affiliated post-affiliated)
1603 (cdr affiliated)))))))))
1605 (defun org-element-special-block-interpreter (special-block contents)
1606 "Interpret SPECIAL-BLOCK element as Org syntax.
1607 CONTENTS is the contents of the element."
1608 (let ((block-type (org-element-property :type special-block)))
1609 (format "#+BEGIN_%s\n%s#+END_%s" block-type contents block-type)))
1613 ;;; Elements
1615 ;; For each element, a parser and an interpreter are also defined.
1616 ;; Both follow the same naming convention used for greater elements.
1618 ;; Also, as for greater elements, adding a new element type is done
1619 ;; through the following steps: implement a parser and an interpreter,
1620 ;; tweak `org-element--current-element' so that it recognizes the new
1621 ;; type and add that new type to `org-element-all-elements'.
1624 ;;;; Babel Call
1626 (defun org-element-babel-call-parser (limit affiliated)
1627 "Parse a babel call.
1629 LIMIT bounds the search. AFFILIATED is a list of which car is
1630 the buffer position at the beginning of the first affiliated
1631 keyword and cdr is a plist of affiliated keywords along with
1632 their value.
1634 Return a list whose car is `babel-call' and cdr is a plist
1635 containing `:call', `:inside-header', `:arguments',
1636 `:end-header', `:begin', `:end', `:value', `:post-blank' and
1637 `:post-affiliated' as keywords."
1638 (save-excursion
1639 (let* ((begin (car affiliated))
1640 (post-affiliated (point))
1641 (value (progn (search-forward ":" nil t)
1642 (org-trim
1643 (buffer-substring-no-properties
1644 (point) (line-end-position)))))
1645 (pos-before-blank (progn (forward-line) (point)))
1646 (end (progn (skip-chars-forward " \r\t\n" limit)
1647 (if (eobp) (point) (line-beginning-position))))
1648 (valid-value
1649 (string-match
1650 "\\([^()\n]+?\\)\\(?:\\[\\(.*?\\)\\]\\)?(\\(.*\\))[ \t]*\\(.*\\)"
1651 value)))
1652 (list 'babel-call
1653 (nconc
1654 (list :call (and valid-value (match-string 1 value))
1655 :inside-header (and valid-value
1656 (org-string-nw-p (match-string 2 value)))
1657 :arguments (and valid-value
1658 (org-string-nw-p (match-string 3 value)))
1659 :end-header (and valid-value
1660 (org-string-nw-p (match-string 4 value)))
1661 :begin begin
1662 :end end
1663 :value value
1664 :post-blank (count-lines pos-before-blank end)
1665 :post-affiliated post-affiliated)
1666 (cdr affiliated))))))
1668 (defun org-element-babel-call-interpreter (babel-call _)
1669 "Interpret BABEL-CALL element as Org syntax."
1670 (concat "#+CALL: "
1671 (org-element-property :call babel-call)
1672 (let ((h (org-element-property :inside-header babel-call)))
1673 (and h (format "[%s]" h)))
1674 (concat "(" (org-element-property :arguments babel-call) ")")
1675 (let ((h (org-element-property :end-header babel-call)))
1676 (and h (concat " " h)))))
1679 ;;;; Clock
1681 (defun org-element-clock-parser (limit)
1682 "Parse a clock.
1684 LIMIT bounds the search.
1686 Return a list whose CAR is `clock' and CDR is a plist containing
1687 `:status', `:value', `:time', `:begin', `:end', `:post-blank' and
1688 `:post-affiliated' as keywords."
1689 (save-excursion
1690 (let* ((case-fold-search nil)
1691 (begin (point))
1692 (value (progn (search-forward org-clock-string (line-end-position) t)
1693 (skip-chars-forward " \t")
1694 (org-element-timestamp-parser)))
1695 (duration (and (search-forward " => " (line-end-position) t)
1696 (progn (skip-chars-forward " \t")
1697 (looking-at "\\(\\S-+\\)[ \t]*$"))
1698 (match-string-no-properties 1)))
1699 (status (if duration 'closed 'running))
1700 (post-blank (let ((before-blank (progn (forward-line) (point))))
1701 (skip-chars-forward " \r\t\n" limit)
1702 (skip-chars-backward " \t")
1703 (unless (bolp) (end-of-line))
1704 (count-lines before-blank (point))))
1705 (end (point)))
1706 (list 'clock
1707 (list :status status
1708 :value value
1709 :duration duration
1710 :begin begin
1711 :end end
1712 :post-blank post-blank
1713 :post-affiliated begin)))))
1715 (defun org-element-clock-interpreter (clock _)
1716 "Interpret CLOCK element as Org syntax."
1717 (concat org-clock-string " "
1718 (org-element-timestamp-interpreter
1719 (org-element-property :value clock) nil)
1720 (let ((duration (org-element-property :duration clock)))
1721 (and duration
1722 (concat " => "
1723 (apply 'format
1724 "%2s:%02s"
1725 (org-split-string duration ":")))))))
1728 ;;;; Comment
1730 (defun org-element-comment-parser (limit affiliated)
1731 "Parse a comment.
1733 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1734 the buffer position at the beginning of the first affiliated
1735 keyword and CDR is a plist of affiliated keywords along with
1736 their value.
1738 Return a list whose CAR is `comment' and CDR is a plist
1739 containing `:begin', `:end', `:value', `:post-blank',
1740 `:post-affiliated' keywords.
1742 Assume point is at comment beginning."
1743 (save-excursion
1744 (let* ((begin (car affiliated))
1745 (post-affiliated (point))
1746 (value (prog2 (looking-at "[ \t]*# ?")
1747 (buffer-substring-no-properties
1748 (match-end 0) (line-end-position))
1749 (forward-line)))
1750 (com-end
1751 ;; Get comments ending.
1752 (progn
1753 (while (and (< (point) limit) (looking-at "[ \t]*#\\( \\|$\\)"))
1754 ;; Accumulate lines without leading hash and first
1755 ;; whitespace.
1756 (setq value
1757 (concat value
1758 "\n"
1759 (buffer-substring-no-properties
1760 (match-end 0) (line-end-position))))
1761 (forward-line))
1762 (point)))
1763 (end (progn (goto-char com-end)
1764 (skip-chars-forward " \r\t\n" limit)
1765 (if (eobp) (point) (line-beginning-position)))))
1766 (list 'comment
1767 (nconc
1768 (list :begin begin
1769 :end end
1770 :value value
1771 :post-blank (count-lines com-end end)
1772 :post-affiliated post-affiliated)
1773 (cdr affiliated))))))
1775 (defun org-element-comment-interpreter (comment _)
1776 "Interpret COMMENT element as Org syntax.
1777 CONTENTS is nil."
1778 (replace-regexp-in-string "^" "# " (org-element-property :value comment)))
1781 ;;;; Comment Block
1783 (defun org-element-comment-block-parser (limit affiliated)
1784 "Parse an export block.
1786 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1787 the buffer position at the beginning of the first affiliated
1788 keyword and CDR is a plist of affiliated keywords along with
1789 their value.
1791 Return a list whose CAR is `comment-block' and CDR is a plist
1792 containing `:begin', `:end', `:value', `:post-blank' and
1793 `:post-affiliated' keywords.
1795 Assume point is at comment block beginning."
1796 (let ((case-fold-search t))
1797 (if (not (save-excursion
1798 (re-search-forward "^[ \t]*#\\+END_COMMENT[ \t]*$" limit t)))
1799 ;; Incomplete block: parse it as a paragraph.
1800 (org-element-paragraph-parser limit affiliated)
1801 (let ((contents-end (match-beginning 0)))
1802 (save-excursion
1803 (let* ((begin (car affiliated))
1804 (post-affiliated (point))
1805 (contents-begin (progn (forward-line) (point)))
1806 (pos-before-blank (progn (goto-char contents-end)
1807 (forward-line)
1808 (point)))
1809 (end (progn (skip-chars-forward " \r\t\n" limit)
1810 (if (eobp) (point) (line-beginning-position))))
1811 (value (buffer-substring-no-properties
1812 contents-begin contents-end)))
1813 (list 'comment-block
1814 (nconc
1815 (list :begin begin
1816 :end end
1817 :value value
1818 :post-blank (count-lines pos-before-blank end)
1819 :post-affiliated post-affiliated)
1820 (cdr affiliated)))))))))
1822 (defun org-element-comment-block-interpreter (comment-block _)
1823 "Interpret COMMENT-BLOCK element as Org syntax."
1824 (format "#+BEGIN_COMMENT\n%s#+END_COMMENT"
1825 (org-element-normalize-string
1826 (org-remove-indentation
1827 (org-element-property :value comment-block)))))
1830 ;;;; Diary Sexp
1832 (defun org-element-diary-sexp-parser (limit affiliated)
1833 "Parse a diary sexp.
1835 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1836 the buffer position at the beginning of the first affiliated
1837 keyword and CDR is a plist of affiliated keywords along with
1838 their value.
1840 Return a list whose CAR is `diary-sexp' and CDR is a plist
1841 containing `:begin', `:end', `:value', `:post-blank' and
1842 `:post-affiliated' keywords."
1843 (save-excursion
1844 (let ((begin (car affiliated))
1845 (post-affiliated (point))
1846 (value (progn (looking-at "\\(%%(.*\\)[ \t]*$")
1847 (match-string-no-properties 1)))
1848 (pos-before-blank (progn (forward-line) (point)))
1849 (end (progn (skip-chars-forward " \r\t\n" limit)
1850 (if (eobp) (point) (line-beginning-position)))))
1851 (list 'diary-sexp
1852 (nconc
1853 (list :value value
1854 :begin begin
1855 :end end
1856 :post-blank (count-lines pos-before-blank end)
1857 :post-affiliated post-affiliated)
1858 (cdr affiliated))))))
1860 (defun org-element-diary-sexp-interpreter (diary-sexp _)
1861 "Interpret DIARY-SEXP as Org syntax."
1862 (org-element-property :value diary-sexp))
1865 ;;;; Example Block
1867 (defun org-element-example-block-parser (limit affiliated)
1868 "Parse an example block.
1870 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1871 the buffer position at the beginning of the first affiliated
1872 keyword and CDR is a plist of affiliated keywords along with
1873 their value.
1875 Return a list whose CAR is `example-block' and CDR is a plist
1876 containing `:begin', `:end', `:number-lines', `:preserve-indent',
1877 `:retain-labels', `:use-labels', `:label-fmt', `:switches',
1878 `:value', `:post-blank' and `:post-affiliated' keywords."
1879 (let ((case-fold-search t))
1880 (if (not (save-excursion
1881 (re-search-forward "^[ \t]*#\\+END_EXAMPLE[ \t]*$" limit t)))
1882 ;; Incomplete block: parse it as a paragraph.
1883 (org-element-paragraph-parser limit affiliated)
1884 (let ((contents-end (match-beginning 0)))
1885 (save-excursion
1886 (let* ((switches
1887 (progn
1888 (looking-at "^[ \t]*#\\+BEGIN_EXAMPLE\\(?: +\\(.*\\)\\)?")
1889 (match-string-no-properties 1)))
1890 ;; Switches analysis.
1891 (number-lines
1892 (and switches
1893 (string-match "\\([-+]\\)n\\(?: *\\([0-9]+\\)\\)?\\>"
1894 switches)
1895 (cons
1896 (if (equal (match-string 1 switches) "-")
1897 'new
1898 'continued)
1899 (if (not (match-end 2)) 0
1900 ;; Subtract 1 to give number of lines before
1901 ;; first line.
1902 (1- (string-to-number (match-string 2 switches)))))))
1903 (preserve-indent
1904 (and switches (string-match "-i\\>" switches)))
1905 ;; Should labels be retained in (or stripped from) example
1906 ;; blocks?
1907 (retain-labels
1908 (or (not switches)
1909 (not (string-match "-r\\>" switches))
1910 (and number-lines (string-match "-k\\>" switches))))
1911 ;; What should code-references use - labels or
1912 ;; line-numbers?
1913 (use-labels
1914 (or (not switches)
1915 (and retain-labels
1916 (not (string-match "-k\\>" switches)))))
1917 (label-fmt
1918 (and switches
1919 (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
1920 (match-string 1 switches)))
1921 ;; Standard block parsing.
1922 (begin (car affiliated))
1923 (post-affiliated (point))
1924 (contents-begin (line-beginning-position 2))
1925 (value (org-unescape-code-in-string
1926 (buffer-substring-no-properties
1927 contents-begin contents-end)))
1928 (pos-before-blank (progn (goto-char contents-end)
1929 (forward-line)
1930 (point)))
1931 (end (progn (skip-chars-forward " \r\t\n" limit)
1932 (if (eobp) (point) (line-beginning-position)))))
1933 (list 'example-block
1934 (nconc
1935 (list :begin begin
1936 :end end
1937 :value value
1938 :switches switches
1939 :number-lines number-lines
1940 :preserve-indent preserve-indent
1941 :retain-labels retain-labels
1942 :use-labels use-labels
1943 :label-fmt label-fmt
1944 :post-blank (count-lines pos-before-blank end)
1945 :post-affiliated post-affiliated)
1946 (cdr affiliated)))))))))
1948 (defun org-element-example-block-interpreter (example-block _)
1949 "Interpret EXAMPLE-BLOCK element as Org syntax."
1950 (let ((switches (org-element-property :switches example-block))
1951 (value (org-element-property :value example-block)))
1952 (concat "#+BEGIN_EXAMPLE" (and switches (concat " " switches)) "\n"
1953 (org-element-normalize-string
1954 (org-escape-code-in-string
1955 (if (or org-src-preserve-indentation
1956 (org-element-property :preserve-indent example-block))
1957 value
1958 (org-remove-indentation value))))
1959 "#+END_EXAMPLE")))
1962 ;;;; Export Block
1964 (defun org-element-export-block-parser (limit affiliated)
1965 "Parse an export block.
1967 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1968 the buffer position at the beginning of the first affiliated
1969 keyword and CDR is a plist of affiliated keywords along with
1970 their value.
1972 Return a list whose CAR is `export-block' and CDR is a plist
1973 containing `:begin', `:end', `:type', `:value', `:post-blank' and
1974 `:post-affiliated' keywords.
1976 Assume point is at export-block beginning."
1977 (let* ((case-fold-search t))
1978 (if (not (save-excursion
1979 (re-search-forward "^[ \t]*#\\+END_EXPORT[ \t]*$" limit t)))
1980 ;; Incomplete block: parse it as a paragraph.
1981 (org-element-paragraph-parser limit affiliated)
1982 (save-excursion
1983 (let* ((contents-end (match-beginning 0))
1984 (backend
1985 (progn
1986 (looking-at
1987 "[ \t]*#\\+BEGIN_EXPORT\\(?:[ \t]+\\(\\S-+\\)\\)?[ \t]*$")
1988 (match-string-no-properties 1)))
1989 (begin (car affiliated))
1990 (post-affiliated (point))
1991 (contents-begin (progn (forward-line) (point)))
1992 (pos-before-blank (progn (goto-char contents-end)
1993 (forward-line)
1994 (point)))
1995 (end (progn (skip-chars-forward " \r\t\n" limit)
1996 (if (eobp) (point) (line-beginning-position))))
1997 (value (org-unescape-code-in-string
1998 (buffer-substring-no-properties contents-begin
1999 contents-end))))
2000 (list 'export-block
2001 (nconc
2002 (list :type (and backend (upcase backend))
2003 :begin begin
2004 :end end
2005 :value value
2006 :post-blank (count-lines pos-before-blank end)
2007 :post-affiliated post-affiliated)
2008 (cdr affiliated))))))))
2010 (defun org-element-export-block-interpreter (export-block _)
2011 "Interpret EXPORT-BLOCK element as Org syntax."
2012 (format "#+BEGIN_EXPORT %s\n%s#+END_EXPORT"
2013 (org-element-property :type export-block)
2014 (org-element-property :value export-block)))
2017 ;;;; Fixed-width
2019 (defun org-element-fixed-width-parser (limit affiliated)
2020 "Parse a fixed-width section.
2022 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2023 the buffer position at the beginning of the first affiliated
2024 keyword and CDR is a plist of affiliated keywords along with
2025 their value.
2027 Return a list whose CAR is `fixed-width' and CDR is a plist
2028 containing `:begin', `:end', `:value', `:post-blank' and
2029 `:post-affiliated' keywords.
2031 Assume point is at the beginning of the fixed-width area."
2032 (save-excursion
2033 (let* ((begin (car affiliated))
2034 (post-affiliated (point))
2035 value
2036 (end-area
2037 (progn
2038 (while (and (< (point) limit)
2039 (looking-at "[ \t]*:\\( \\|$\\)"))
2040 ;; Accumulate text without starting colons.
2041 (setq value
2042 (concat value
2043 (buffer-substring-no-properties
2044 (match-end 0) (point-at-eol))
2045 "\n"))
2046 (forward-line))
2047 (point)))
2048 (end (progn (skip-chars-forward " \r\t\n" limit)
2049 (if (eobp) (point) (line-beginning-position)))))
2050 (list 'fixed-width
2051 (nconc
2052 (list :begin begin
2053 :end end
2054 :value value
2055 :post-blank (count-lines end-area end)
2056 :post-affiliated post-affiliated)
2057 (cdr affiliated))))))
2059 (defun org-element-fixed-width-interpreter (fixed-width _)
2060 "Interpret FIXED-WIDTH element as Org syntax."
2061 (let ((value (org-element-property :value fixed-width)))
2062 (and value
2063 (replace-regexp-in-string
2064 "^" ": "
2065 (if (string-match "\n\\'" value) (substring value 0 -1) value)))))
2068 ;;;; Horizontal Rule
2070 (defun org-element-horizontal-rule-parser (limit affiliated)
2071 "Parse an horizontal rule.
2073 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2074 the buffer position at the beginning of the first affiliated
2075 keyword and CDR is a plist of affiliated keywords along with
2076 their value.
2078 Return a list whose CAR is `horizontal-rule' and CDR is a plist
2079 containing `:begin', `:end', `:post-blank' and `:post-affiliated'
2080 keywords."
2081 (save-excursion
2082 (let ((begin (car affiliated))
2083 (post-affiliated (point))
2084 (post-hr (progn (forward-line) (point)))
2085 (end (progn (skip-chars-forward " \r\t\n" limit)
2086 (if (eobp) (point) (line-beginning-position)))))
2087 (list 'horizontal-rule
2088 (nconc
2089 (list :begin begin
2090 :end end
2091 :post-blank (count-lines post-hr end)
2092 :post-affiliated post-affiliated)
2093 (cdr affiliated))))))
2095 (defun org-element-horizontal-rule-interpreter (&rest _)
2096 "Interpret HORIZONTAL-RULE element as Org syntax."
2097 "-----")
2100 ;;;; Keyword
2102 (defun org-element-keyword-parser (limit affiliated)
2103 "Parse a keyword at point.
2105 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2106 the buffer position at the beginning of the first affiliated
2107 keyword and CDR is a plist of affiliated keywords along with
2108 their value.
2110 Return a list whose CAR is `keyword' and CDR is a plist
2111 containing `:key', `:value', `:begin', `:end', `:post-blank' and
2112 `:post-affiliated' keywords."
2113 (save-excursion
2114 ;; An orphaned affiliated keyword is considered as a regular
2115 ;; keyword. In this case AFFILIATED is nil, so we take care of
2116 ;; this corner case.
2117 (let ((begin (or (car affiliated) (point)))
2118 (post-affiliated (point))
2119 (key (progn (looking-at "[ \t]*#\\+\\(\\S-+*\\):")
2120 (upcase (match-string-no-properties 1))))
2121 (value (org-trim (buffer-substring-no-properties
2122 (match-end 0) (point-at-eol))))
2123 (pos-before-blank (progn (forward-line) (point)))
2124 (end (progn (skip-chars-forward " \r\t\n" limit)
2125 (if (eobp) (point) (line-beginning-position)))))
2126 (list 'keyword
2127 (nconc
2128 (list :key key
2129 :value value
2130 :begin begin
2131 :end end
2132 :post-blank (count-lines pos-before-blank end)
2133 :post-affiliated post-affiliated)
2134 (cdr affiliated))))))
2136 (defun org-element-keyword-interpreter (keyword _)
2137 "Interpret KEYWORD element as Org syntax."
2138 (format "#+%s: %s"
2139 (org-element-property :key keyword)
2140 (org-element-property :value keyword)))
2143 ;;;; Latex Environment
2145 (defconst org-element--latex-begin-environment
2146 "^[ \t]*\\\\begin{\\([A-Za-z0-9*]+\\)}"
2147 "Regexp matching the beginning of a LaTeX environment.
2148 The environment is captured by the first group.
2150 See also `org-element--latex-end-environment'.")
2152 (defconst org-element--latex-end-environment
2153 "\\\\end{%s}[ \t]*$"
2154 "Format string matching the ending of a LaTeX environment.
2155 See also `org-element--latex-begin-environment'.")
2157 (defun org-element-latex-environment-parser (limit affiliated)
2158 "Parse a LaTeX environment.
2160 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2161 the buffer position at the beginning of the first affiliated
2162 keyword and CDR is a plist of affiliated keywords along with
2163 their value.
2165 Return a list whose CAR is `latex-environment' and CDR is a plist
2166 containing `:begin', `:end', `:value', `:post-blank' and
2167 `:post-affiliated' keywords.
2169 Assume point is at the beginning of the latex environment."
2170 (save-excursion
2171 (let ((case-fold-search t)
2172 (code-begin (point)))
2173 (looking-at org-element--latex-begin-environment)
2174 (if (not (re-search-forward (format org-element--latex-end-environment
2175 (regexp-quote (match-string 1)))
2176 limit t))
2177 ;; Incomplete latex environment: parse it as a paragraph.
2178 (org-element-paragraph-parser limit affiliated)
2179 (let* ((code-end (progn (forward-line) (point)))
2180 (begin (car affiliated))
2181 (value (buffer-substring-no-properties code-begin code-end))
2182 (end (progn (skip-chars-forward " \r\t\n" limit)
2183 (if (eobp) (point) (line-beginning-position)))))
2184 (list 'latex-environment
2185 (nconc
2186 (list :begin begin
2187 :end end
2188 :value value
2189 :post-blank (count-lines code-end end)
2190 :post-affiliated code-begin)
2191 (cdr affiliated))))))))
2193 (defun org-element-latex-environment-interpreter (latex-environment _)
2194 "Interpret LATEX-ENVIRONMENT element as Org syntax."
2195 (org-element-property :value latex-environment))
2198 ;;;; Node Property
2200 (defun org-element-node-property-parser (limit)
2201 "Parse a node-property at point.
2203 LIMIT bounds the search.
2205 Return a list whose CAR is `node-property' and CDR is a plist
2206 containing `:key', `:value', `:begin', `:end', `:post-blank' and
2207 `:post-affiliated' keywords."
2208 (looking-at org-property-re)
2209 (let ((case-fold-search t)
2210 (begin (point))
2211 (key (match-string-no-properties 2))
2212 (value (match-string-no-properties 3))
2213 (end (save-excursion
2214 (end-of-line)
2215 (if (re-search-forward org-property-re limit t)
2216 (line-beginning-position)
2217 limit))))
2218 (list 'node-property
2219 (list :key key
2220 :value value
2221 :begin begin
2222 :end end
2223 :post-blank 0
2224 :post-affiliated begin))))
2226 (defun org-element-node-property-interpreter (node-property _)
2227 "Interpret NODE-PROPERTY element as Org syntax."
2228 (format org-property-format
2229 (format ":%s:" (org-element-property :key node-property))
2230 (or (org-element-property :value node-property) "")))
2233 ;;;; Paragraph
2235 (defun org-element-paragraph-parser (limit affiliated)
2236 "Parse a paragraph.
2238 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2239 the buffer position at the beginning of the first affiliated
2240 keyword and CDR is a plist of affiliated keywords along with
2241 their value.
2243 Return a list whose CAR is `paragraph' and CDR is a plist
2244 containing `:begin', `:end', `:contents-begin' and
2245 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
2247 Assume point is at the beginning of the paragraph."
2248 (save-excursion
2249 (let* ((begin (car affiliated))
2250 (contents-begin (point))
2251 (before-blank
2252 (let ((case-fold-search t))
2253 (end-of-line)
2254 ;; A matching `org-element-paragraph-separate' is not
2255 ;; necessarily the end of the paragraph. In particular,
2256 ;; drawers, blocks or LaTeX environments opening lines
2257 ;; must be closed. Moreover keywords with a secondary
2258 ;; value must belong to "dual keywords".
2259 (while (not
2260 (cond
2261 ((not (and (re-search-forward
2262 org-element-paragraph-separate limit 'move)
2263 (progn (beginning-of-line) t))))
2264 ((looking-at org-drawer-regexp)
2265 (save-excursion
2266 (re-search-forward "^[ \t]*:END:[ \t]*$" limit t)))
2267 ((looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
2268 (save-excursion
2269 (re-search-forward
2270 (format "^[ \t]*#\\+END_%s[ \t]*$"
2271 (regexp-quote (match-string 1)))
2272 limit t)))
2273 ((looking-at org-element--latex-begin-environment)
2274 (save-excursion
2275 (re-search-forward
2276 (format org-element--latex-end-environment
2277 (regexp-quote (match-string 1)))
2278 limit t)))
2279 ((looking-at "[ \t]*#\\+\\(\\S-+\\)\\[.*\\]:")
2280 (member-ignore-case (match-string 1)
2281 org-element-dual-keywords))
2282 ;; Everything else is unambiguous.
2283 (t)))
2284 (end-of-line))
2285 (if (= (point) limit) limit
2286 (goto-char (line-beginning-position)))))
2287 (contents-end (save-excursion
2288 (skip-chars-backward " \r\t\n" contents-begin)
2289 (line-beginning-position 2)))
2290 (end (progn (skip-chars-forward " \r\t\n" limit)
2291 (if (eobp) (point) (line-beginning-position)))))
2292 (list 'paragraph
2293 (nconc
2294 (list :begin begin
2295 :end end
2296 :contents-begin contents-begin
2297 :contents-end contents-end
2298 :post-blank (count-lines before-blank end)
2299 :post-affiliated contents-begin)
2300 (cdr affiliated))))))
2302 (defun org-element-paragraph-interpreter (_ contents)
2303 "Interpret paragraph element as Org syntax.
2304 CONTENTS is the contents of the element."
2305 contents)
2308 ;;;; Planning
2310 (defun org-element-planning-parser (limit)
2311 "Parse a planning.
2313 LIMIT bounds the search.
2315 Return a list whose CAR is `planning' and CDR is a plist
2316 containing `:closed', `:deadline', `:scheduled', `:begin',
2317 `:end', `:post-blank' and `:post-affiliated' keywords."
2318 (save-excursion
2319 (let* ((case-fold-search nil)
2320 (begin (point))
2321 (post-blank (let ((before-blank (progn (forward-line) (point))))
2322 (skip-chars-forward " \r\t\n" limit)
2323 (skip-chars-backward " \t")
2324 (unless (bolp) (end-of-line))
2325 (count-lines before-blank (point))))
2326 (end (point))
2327 closed deadline scheduled)
2328 (goto-char begin)
2329 (while (re-search-forward org-keyword-time-not-clock-regexp end t)
2330 (goto-char (match-end 1))
2331 (skip-chars-forward " \t" end)
2332 (let ((keyword (match-string 1))
2333 (time (org-element-timestamp-parser)))
2334 (cond ((equal keyword org-closed-string) (setq closed time))
2335 ((equal keyword org-deadline-string) (setq deadline time))
2336 (t (setq scheduled time)))))
2337 (list 'planning
2338 (list :closed closed
2339 :deadline deadline
2340 :scheduled scheduled
2341 :begin begin
2342 :end end
2343 :post-blank post-blank
2344 :post-affiliated begin)))))
2346 (defun org-element-planning-interpreter (planning _)
2347 "Interpret PLANNING element as Org syntax."
2348 (mapconcat
2349 #'identity
2350 (delq nil
2351 (list (let ((deadline (org-element-property :deadline planning)))
2352 (when deadline
2353 (concat org-deadline-string " "
2354 (org-element-timestamp-interpreter deadline nil))))
2355 (let ((scheduled (org-element-property :scheduled planning)))
2356 (when scheduled
2357 (concat org-scheduled-string " "
2358 (org-element-timestamp-interpreter scheduled nil))))
2359 (let ((closed (org-element-property :closed planning)))
2360 (when closed
2361 (concat org-closed-string " "
2362 (org-element-timestamp-interpreter closed nil))))))
2363 " "))
2366 ;;;; Src Block
2368 (defun org-element-src-block-parser (limit affiliated)
2369 "Parse a src block.
2371 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2372 the buffer position at the beginning of the first affiliated
2373 keyword and CDR is a plist of affiliated keywords along with
2374 their value.
2376 Return a list whose CAR is `src-block' and CDR is a plist
2377 containing `:language', `:switches', `:parameters', `:begin',
2378 `:end', `:number-lines', `:retain-labels', `:use-labels',
2379 `:label-fmt', `:preserve-indent', `:value', `:post-blank' and
2380 `:post-affiliated' keywords.
2382 Assume point is at the beginning of the block."
2383 (let ((case-fold-search t))
2384 (if (not (save-excursion (re-search-forward "^[ \t]*#\\+END_SRC[ \t]*$"
2385 limit t)))
2386 ;; Incomplete block: parse it as a paragraph.
2387 (org-element-paragraph-parser limit affiliated)
2388 (let ((contents-end (match-beginning 0)))
2389 (save-excursion
2390 (let* ((begin (car affiliated))
2391 (post-affiliated (point))
2392 ;; Get language as a string.
2393 (language
2394 (progn
2395 (looking-at
2396 "^[ \t]*#\\+BEGIN_SRC\
2397 \\(?: +\\(\\S-+\\)\\)?\
2398 \\(\\(?: +\\(?:-\\(?:l \".+\"\\|[ikr]\\)\\|[-+]n\\(?: *[0-9]+\\)?\\)\\)+\\)?\
2399 \\(.*\\)[ \t]*$")
2400 (match-string-no-properties 1)))
2401 ;; Get switches.
2402 (switches (match-string-no-properties 2))
2403 ;; Get parameters.
2404 (parameters (match-string-no-properties 3))
2405 ;; Switches analysis.
2406 (number-lines
2407 (and switches
2408 (string-match "\\([-+]\\)n\\(?: *\\([0-9]+\\)\\)?\\>"
2409 switches)
2410 (cons
2411 (if (equal (match-string 1 switches) "-")
2412 'new
2413 'continued)
2414 (if (not (match-end 2)) 0
2415 ;; Subtract 1 to give number of lines before
2416 ;; first line.
2417 (1- (string-to-number (match-string 2 switches)))))))
2418 (preserve-indent (and switches
2419 (string-match "-i\\>" switches)))
2420 (label-fmt
2421 (and switches
2422 (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
2423 (match-string 1 switches)))
2424 ;; Should labels be retained in (or stripped from)
2425 ;; src blocks?
2426 (retain-labels
2427 (or (not switches)
2428 (not (string-match "-r\\>" switches))
2429 (and number-lines (string-match "-k\\>" switches))))
2430 ;; What should code-references use - labels or
2431 ;; line-numbers?
2432 (use-labels
2433 (or (not switches)
2434 (and retain-labels
2435 (not (string-match "-k\\>" switches)))))
2436 ;; Retrieve code.
2437 (value (org-unescape-code-in-string
2438 (buffer-substring-no-properties
2439 (line-beginning-position 2) contents-end)))
2440 (pos-before-blank (progn (goto-char contents-end)
2441 (forward-line)
2442 (point)))
2443 ;; Get position after ending blank lines.
2444 (end (progn (skip-chars-forward " \r\t\n" limit)
2445 (if (eobp) (point) (line-beginning-position)))))
2446 (list 'src-block
2447 (nconc
2448 (list :language language
2449 :switches (and (org-string-nw-p switches)
2450 (org-trim switches))
2451 :parameters (and (org-string-nw-p parameters)
2452 (org-trim parameters))
2453 :begin begin
2454 :end end
2455 :number-lines number-lines
2456 :preserve-indent preserve-indent
2457 :retain-labels retain-labels
2458 :use-labels use-labels
2459 :label-fmt label-fmt
2460 :value value
2461 :post-blank (count-lines pos-before-blank end)
2462 :post-affiliated post-affiliated)
2463 (cdr affiliated)))))))))
2465 (defun org-element-src-block-interpreter (src-block _)
2466 "Interpret SRC-BLOCK element as Org syntax."
2467 (let ((lang (org-element-property :language src-block))
2468 (switches (org-element-property :switches src-block))
2469 (params (org-element-property :parameters src-block))
2470 (value
2471 (let ((val (org-element-property :value src-block)))
2472 (cond
2473 ((or org-src-preserve-indentation
2474 (org-element-property :preserve-indent src-block))
2475 val)
2476 ((zerop org-edit-src-content-indentation)
2477 (org-remove-indentation val))
2479 (let ((ind (make-string org-edit-src-content-indentation ?\s)))
2480 (replace-regexp-in-string
2481 "^" ind (org-remove-indentation val))))))))
2482 (concat (format "#+BEGIN_SRC%s\n"
2483 (concat (and lang (concat " " lang))
2484 (and switches (concat " " switches))
2485 (and params (concat " " params))))
2486 (org-element-normalize-string (org-escape-code-in-string value))
2487 "#+END_SRC")))
2490 ;;;; Table
2492 (defun org-element-table-parser (limit affiliated)
2493 "Parse a table at point.
2495 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2496 the buffer position at the beginning of the first affiliated
2497 keyword and CDR is a plist of affiliated keywords along with
2498 their value.
2500 Return a list whose CAR is `table' and CDR is a plist containing
2501 `:begin', `:end', `:tblfm', `:type', `:contents-begin',
2502 `:contents-end', `:value', `:post-blank' and `:post-affiliated'
2503 keywords.
2505 Assume point is at the beginning of the table."
2506 (save-excursion
2507 (let* ((case-fold-search t)
2508 (table-begin (point))
2509 (type (if (looking-at "[ \t]*|") 'org 'table.el))
2510 (end-re (format "^[ \t]*\\($\\|[^| \t%s]\\)"
2511 (if (eq type 'org) "" "+")))
2512 (begin (car affiliated))
2513 (table-end
2514 (if (re-search-forward end-re limit 'move)
2515 (goto-char (match-beginning 0))
2516 (point)))
2517 (tblfm (let (acc)
2518 (while (looking-at "[ \t]*#\\+TBLFM: +\\(.*\\)[ \t]*$")
2519 (push (match-string-no-properties 1) acc)
2520 (forward-line))
2521 acc))
2522 (pos-before-blank (point))
2523 (end (progn (skip-chars-forward " \r\t\n" limit)
2524 (if (eobp) (point) (line-beginning-position)))))
2525 (list 'table
2526 (nconc
2527 (list :begin begin
2528 :end end
2529 :type type
2530 :tblfm tblfm
2531 ;; Only `org' tables have contents. `table.el' tables
2532 ;; use a `:value' property to store raw table as
2533 ;; a string.
2534 :contents-begin (and (eq type 'org) table-begin)
2535 :contents-end (and (eq type 'org) table-end)
2536 :value (and (eq type 'table.el)
2537 (buffer-substring-no-properties
2538 table-begin table-end))
2539 :post-blank (count-lines pos-before-blank end)
2540 :post-affiliated table-begin)
2541 (cdr affiliated))))))
2543 (defun org-element-table-interpreter (table contents)
2544 "Interpret TABLE element as Org syntax.
2545 CONTENTS is a string, if table's type is `org', or nil."
2546 (if (eq (org-element-property :type table) 'table.el)
2547 (org-remove-indentation (org-element-property :value table))
2548 (concat (with-temp-buffer (insert contents)
2549 (org-table-align)
2550 (buffer-string))
2551 (mapconcat (lambda (fm) (concat "#+TBLFM: " fm))
2552 (reverse (org-element-property :tblfm table))
2553 "\n"))))
2556 ;;;; Table Row
2558 (defun org-element-table-row-parser (_)
2559 "Parse table row at point.
2561 Return a list whose CAR is `table-row' and CDR is a plist
2562 containing `:begin', `:end', `:contents-begin', `:contents-end',
2563 `:type', `:post-blank' and `:post-affiliated' keywords."
2564 (save-excursion
2565 (let* ((type (if (looking-at "^[ \t]*|-") 'rule 'standard))
2566 (begin (point))
2567 ;; A table rule has no contents. In that case, ensure
2568 ;; CONTENTS-BEGIN matches CONTENTS-END.
2569 (contents-begin (and (eq type 'standard) (search-forward "|")))
2570 (contents-end (and (eq type 'standard)
2571 (progn
2572 (end-of-line)
2573 (skip-chars-backward " \t")
2574 (point))))
2575 (end (line-beginning-position 2)))
2576 (list 'table-row
2577 (list :type type
2578 :begin begin
2579 :end end
2580 :contents-begin contents-begin
2581 :contents-end contents-end
2582 :post-blank 0
2583 :post-affiliated begin)))))
2585 (defun org-element-table-row-interpreter (table-row contents)
2586 "Interpret TABLE-ROW element as Org syntax.
2587 CONTENTS is the contents of the table row."
2588 (if (eq (org-element-property :type table-row) 'rule) "|-"
2589 (concat "|" contents)))
2592 ;;;; Verse Block
2594 (defun org-element-verse-block-parser (limit affiliated)
2595 "Parse a verse block.
2597 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2598 the buffer position at the beginning of the first affiliated
2599 keyword and CDR is a plist of affiliated keywords along with
2600 their value.
2602 Return a list whose CAR is `verse-block' and CDR is a plist
2603 containing `:begin', `:end', `:contents-begin', `:contents-end',
2604 `:post-blank' and `:post-affiliated' keywords.
2606 Assume point is at beginning of the block."
2607 (let ((case-fold-search t))
2608 (if (not (save-excursion
2609 (re-search-forward "^[ \t]*#\\+END_VERSE[ \t]*$" limit t)))
2610 ;; Incomplete block: parse it as a paragraph.
2611 (org-element-paragraph-parser limit affiliated)
2612 (let ((contents-end (match-beginning 0)))
2613 (save-excursion
2614 (let* ((begin (car affiliated))
2615 (post-affiliated (point))
2616 (contents-begin (progn (forward-line) (point)))
2617 (pos-before-blank (progn (goto-char contents-end)
2618 (forward-line)
2619 (point)))
2620 (end (progn (skip-chars-forward " \r\t\n" limit)
2621 (if (eobp) (point) (line-beginning-position)))))
2622 (list 'verse-block
2623 (nconc
2624 (list :begin begin
2625 :end end
2626 :contents-begin contents-begin
2627 :contents-end contents-end
2628 :post-blank (count-lines pos-before-blank end)
2629 :post-affiliated post-affiliated)
2630 (cdr affiliated)))))))))
2632 (defun org-element-verse-block-interpreter (_ contents)
2633 "Interpret verse-block element as Org syntax.
2634 CONTENTS is verse block contents."
2635 (format "#+BEGIN_VERSE\n%s#+END_VERSE" contents))
2639 ;;; Objects
2641 ;; Unlike to elements, raw text can be found between objects. Hence,
2642 ;; `org-element--object-lex' is provided to find the next object in
2643 ;; buffer.
2645 ;; Some object types (e.g., `italic') are recursive. Restrictions on
2646 ;; object types they can contain will be specified in
2647 ;; `org-element-object-restrictions'.
2649 ;; Creating a new type of object requires to alter
2650 ;; `org-element--object-regexp' and `org-element--object-lex', add the
2651 ;; new type in `org-element-all-objects', and possibly add
2652 ;; restrictions in `org-element-object-restrictions'.
2654 ;;;; Bold
2656 (defun org-element-bold-parser ()
2657 "Parse bold object at point, if any.
2659 When at a bold object, return a list whose car is `bold' and cdr
2660 is a plist with `:begin', `:end', `:contents-begin' and
2661 `:contents-end' and `:post-blank' keywords. Otherwise, return
2662 nil.
2664 Assume point is at the first star marker."
2665 (save-excursion
2666 (unless (bolp) (backward-char 1))
2667 (when (looking-at org-emph-re)
2668 (let ((begin (match-beginning 2))
2669 (contents-begin (match-beginning 4))
2670 (contents-end (match-end 4))
2671 (post-blank (progn (goto-char (match-end 2))
2672 (skip-chars-forward " \t")))
2673 (end (point)))
2674 (list 'bold
2675 (list :begin begin
2676 :end end
2677 :contents-begin contents-begin
2678 :contents-end contents-end
2679 :post-blank post-blank))))))
2681 (defun org-element-bold-interpreter (_ contents)
2682 "Interpret bold object as Org syntax.
2683 CONTENTS is the contents of the object."
2684 (format "*%s*" contents))
2687 ;;;; Code
2689 (defun org-element-code-parser ()
2690 "Parse code object at point, if any.
2692 When at a code object, return a list whose car is `code' and cdr
2693 is a plist with `:value', `:begin', `:end' and `:post-blank'
2694 keywords. Otherwise, return nil.
2696 Assume point is at the first tilde marker."
2697 (save-excursion
2698 (unless (bolp) (backward-char 1))
2699 (when (looking-at org-emph-re)
2700 (let ((begin (match-beginning 2))
2701 (value (match-string-no-properties 4))
2702 (post-blank (progn (goto-char (match-end 2))
2703 (skip-chars-forward " \t")))
2704 (end (point)))
2705 (list 'code
2706 (list :value value
2707 :begin begin
2708 :end end
2709 :post-blank post-blank))))))
2711 (defun org-element-code-interpreter (code _)
2712 "Interpret CODE object as Org syntax."
2713 (format "~%s~" (org-element-property :value code)))
2716 ;;;; Entity
2718 (defun org-element-entity-parser ()
2719 "Parse entity at point, if any.
2721 When at an entity, return a list whose car is `entity' and cdr
2722 a plist with `:begin', `:end', `:latex', `:latex-math-p',
2723 `:html', `:latin1', `:utf-8', `:ascii', `:use-brackets-p' and
2724 `:post-blank' as keywords. Otherwise, return nil.
2726 Assume point is at the beginning of the entity."
2727 (catch 'no-object
2728 (when (looking-at "\\\\\\(?:\\(?1:_ +\\)\\|\\(?1:there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\(?2:$\\|{}\\|[^[:alpha:]]\\)\\)")
2729 (save-excursion
2730 (let* ((value (or (org-entity-get (match-string 1))
2731 (throw 'no-object nil)))
2732 (begin (match-beginning 0))
2733 (bracketsp (string= (match-string 2) "{}"))
2734 (post-blank (progn (goto-char (match-end 1))
2735 (when bracketsp (forward-char 2))
2736 (skip-chars-forward " \t")))
2737 (end (point)))
2738 (list 'entity
2739 (list :name (car value)
2740 :latex (nth 1 value)
2741 :latex-math-p (nth 2 value)
2742 :html (nth 3 value)
2743 :ascii (nth 4 value)
2744 :latin1 (nth 5 value)
2745 :utf-8 (nth 6 value)
2746 :begin begin
2747 :end end
2748 :use-brackets-p bracketsp
2749 :post-blank post-blank)))))))
2751 (defun org-element-entity-interpreter (entity _)
2752 "Interpret ENTITY object as Org syntax."
2753 (concat "\\"
2754 (org-element-property :name entity)
2755 (when (org-element-property :use-brackets-p entity) "{}")))
2758 ;;;; Export Snippet
2760 (defun org-element-export-snippet-parser ()
2761 "Parse export snippet at point.
2763 When at an export snippet, return a list whose car is
2764 `export-snippet' and cdr a plist with `:begin', `:end',
2765 `:back-end', `:value' and `:post-blank' as keywords. Otherwise,
2766 return nil.
2768 Assume point is at the beginning of the snippet."
2769 (save-excursion
2770 (let (contents-end)
2771 (when (and (looking-at "@@\\([-A-Za-z0-9]+\\):")
2772 (setq contents-end
2773 (save-match-data (goto-char (match-end 0))
2774 (re-search-forward "@@" nil t)
2775 (match-beginning 0))))
2776 (let* ((begin (match-beginning 0))
2777 (back-end (match-string-no-properties 1))
2778 (value (buffer-substring-no-properties
2779 (match-end 0) contents-end))
2780 (post-blank (skip-chars-forward " \t"))
2781 (end (point)))
2782 (list 'export-snippet
2783 (list :back-end back-end
2784 :value value
2785 :begin begin
2786 :end end
2787 :post-blank post-blank)))))))
2789 (defun org-element-export-snippet-interpreter (export-snippet _)
2790 "Interpret EXPORT-SNIPPET object as Org syntax."
2791 (format "@@%s:%s@@"
2792 (org-element-property :back-end export-snippet)
2793 (org-element-property :value export-snippet)))
2796 ;;;; Footnote Reference
2798 (defun org-element-footnote-reference-parser ()
2799 "Parse footnote reference at point, if any.
2801 When at a footnote reference, return a list whose car is
2802 `footnote-reference' and cdr a plist with `:label', `:type',
2803 `:begin', `:end', `:content-begin', `:contents-end' and
2804 `:post-blank' as keywords. Otherwise, return nil."
2805 (when (looking-at org-footnote-re)
2806 (let ((closing (with-syntax-table org-element--pair-square-table
2807 (ignore-errors (scan-lists (point) 1 0)))))
2808 (when closing
2809 (save-excursion
2810 (let* ((begin (point))
2811 (label (match-string-no-properties 1))
2812 (inner-begin (match-end 0))
2813 (inner-end (1- closing))
2814 (type (if (match-end 2) 'inline 'standard))
2815 (post-blank (progn (goto-char closing)
2816 (skip-chars-forward " \t")))
2817 (end (point)))
2818 (list 'footnote-reference
2819 (list :label label
2820 :type type
2821 :begin begin
2822 :end end
2823 :contents-begin (and (eq type 'inline) inner-begin)
2824 :contents-end (and (eq type 'inline) inner-end)
2825 :post-blank post-blank))))))))
2827 (defun org-element-footnote-reference-interpreter (footnote-reference contents)
2828 "Interpret FOOTNOTE-REFERENCE object as Org syntax.
2829 CONTENTS is its definition, when inline, or nil."
2830 (format "[fn:%s%s]"
2831 (or (org-element-property :label footnote-reference) "")
2832 (if contents (concat ":" contents) "")))
2835 ;;;; Inline Babel Call
2837 (defun org-element-inline-babel-call-parser ()
2838 "Parse inline babel call at point, if any.
2840 When at an inline babel call, return a list whose car is
2841 `inline-babel-call' and cdr a plist with `:call',
2842 `:inside-header', `:arguments', `:end-header', `:begin', `:end',
2843 `:value' and `:post-blank' as keywords. Otherwise, return nil.
2845 Assume point is at the beginning of the babel call."
2846 (save-excursion
2847 (catch :no-object
2848 (when (let ((case-fold-search nil))
2849 (looking-at "\\<call_\\([^ \t\n[(]+\\)[([]"))
2850 (goto-char (match-end 1))
2851 (let* ((begin (match-beginning 0))
2852 (call (match-string-no-properties 1))
2853 (inside-header
2854 (let ((p (org-element--parse-paired-brackets ?\[)))
2855 (and (org-string-nw-p p)
2856 (replace-regexp-in-string "\n[ \t]*" " " (org-trim p)))))
2857 (arguments (org-string-nw-p
2858 (or (org-element--parse-paired-brackets ?\()
2859 ;; Parenthesis are mandatory.
2860 (throw :no-object nil))))
2861 (end-header
2862 (let ((p (org-element--parse-paired-brackets ?\[)))
2863 (and (org-string-nw-p p)
2864 (replace-regexp-in-string "\n[ \t]*" " " (org-trim p)))))
2865 (value (buffer-substring-no-properties begin (point)))
2866 (post-blank (skip-chars-forward " \t"))
2867 (end (point)))
2868 (list 'inline-babel-call
2869 (list :call call
2870 :inside-header inside-header
2871 :arguments arguments
2872 :end-header end-header
2873 :begin begin
2874 :end end
2875 :value value
2876 :post-blank post-blank)))))))
2878 (defun org-element-inline-babel-call-interpreter (inline-babel-call _)
2879 "Interpret INLINE-BABEL-CALL object as Org syntax."
2880 (concat "call_"
2881 (org-element-property :call inline-babel-call)
2882 (let ((h (org-element-property :inside-header inline-babel-call)))
2883 (and h (format "[%s]" h)))
2884 "(" (org-element-property :arguments inline-babel-call) ")"
2885 (let ((h (org-element-property :end-header inline-babel-call)))
2886 (and h (format "[%s]" h)))))
2889 ;;;; Inline Src Block
2891 (defun org-element-inline-src-block-parser ()
2892 "Parse inline source block at point, if any.
2894 When at an inline source block, return a list whose car is
2895 `inline-src-block' and cdr a plist with `:begin', `:end',
2896 `:language', `:value', `:parameters' and `:post-blank' as
2897 keywords. Otherwise, return nil.
2899 Assume point is at the beginning of the inline src block."
2900 (save-excursion
2901 (catch :no-object
2902 (when (let ((case-fold-search nil))
2903 (looking-at "\\<src_\\([^ \t\n[{]+\\)[{[]"))
2904 (goto-char (match-end 1))
2905 (let ((begin (match-beginning 0))
2906 (language (match-string-no-properties 1))
2907 (parameters
2908 (let ((p (org-element--parse-paired-brackets ?\[)))
2909 (and (org-string-nw-p p)
2910 (replace-regexp-in-string "\n[ \t]*" " " (org-trim p)))))
2911 (value (or (org-element--parse-paired-brackets ?\{)
2912 (throw :no-object nil)))
2913 (post-blank (skip-chars-forward " \t")))
2914 (list 'inline-src-block
2915 (list :language language
2916 :value value
2917 :parameters parameters
2918 :begin begin
2919 :end (point)
2920 :post-blank post-blank)))))))
2922 (defun org-element-inline-src-block-interpreter (inline-src-block _)
2923 "Interpret INLINE-SRC-BLOCK object as Org syntax."
2924 (let ((language (org-element-property :language inline-src-block))
2925 (arguments (org-element-property :parameters inline-src-block))
2926 (body (org-element-property :value inline-src-block)))
2927 (format "src_%s%s{%s}"
2928 language
2929 (if arguments (format "[%s]" arguments) "")
2930 body)))
2932 ;;;; Italic
2934 (defun org-element-italic-parser ()
2935 "Parse italic object at point, if any.
2937 When at an italic object, return a list whose car is `italic' and
2938 cdr is a plist with `:begin', `:end', `:contents-begin' and
2939 `:contents-end' and `:post-blank' keywords. Otherwise, return
2940 nil.
2942 Assume point is at the first slash marker."
2943 (save-excursion
2944 (unless (bolp) (backward-char 1))
2945 (when (looking-at org-emph-re)
2946 (let ((begin (match-beginning 2))
2947 (contents-begin (match-beginning 4))
2948 (contents-end (match-end 4))
2949 (post-blank (progn (goto-char (match-end 2))
2950 (skip-chars-forward " \t")))
2951 (end (point)))
2952 (list 'italic
2953 (list :begin begin
2954 :end end
2955 :contents-begin contents-begin
2956 :contents-end contents-end
2957 :post-blank post-blank))))))
2959 (defun org-element-italic-interpreter (_ contents)
2960 "Interpret italic object as Org syntax.
2961 CONTENTS is the contents of the object."
2962 (format "/%s/" contents))
2965 ;;;; Latex Fragment
2967 (defun org-element-latex-fragment-parser ()
2968 "Parse LaTeX fragment at point, if any.
2970 When at a LaTeX fragment, return a list whose car is
2971 `latex-fragment' and cdr a plist with `:value', `:begin', `:end',
2972 and `:post-blank' as keywords. Otherwise, return nil.
2974 Assume point is at the beginning of the LaTeX fragment."
2975 (catch 'no-object
2976 (save-excursion
2977 (let* ((begin (point))
2978 (after-fragment
2979 (if (eq (char-after) ?$)
2980 (if (eq (char-after (1+ (point))) ?$)
2981 (search-forward "$$" nil t 2)
2982 (and (not (eq (char-before) ?$))
2983 (search-forward "$" nil t 2)
2984 (not (memq (char-before (match-beginning 0))
2985 '(?\s ?\t ?\n ?, ?.)))
2986 (looking-at-p
2987 "\\(\\s.\\|\\s-\\|\\s(\\|\\s)\\|\\s\"\\|'\\|$\\)")
2988 (point)))
2989 (pcase (char-after (1+ (point)))
2990 (?\( (search-forward "\\)" nil t))
2991 (?\[ (search-forward "\\]" nil t))
2993 ;; Macro.
2994 (and (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\
2995 \\|\\({[^{}\n]*}\\)\\)*")
2996 (match-end 0))))))
2997 (post-blank (if (not after-fragment) (throw 'no-object nil)
2998 (goto-char after-fragment)
2999 (skip-chars-forward " \t")))
3000 (end (point)))
3001 (list 'latex-fragment
3002 (list :value (buffer-substring-no-properties begin after-fragment)
3003 :begin begin
3004 :end end
3005 :post-blank post-blank))))))
3007 (defun org-element-latex-fragment-interpreter (latex-fragment _)
3008 "Interpret LATEX-FRAGMENT object as Org syntax."
3009 (org-element-property :value latex-fragment))
3011 ;;;; Line Break
3013 (defun org-element-line-break-parser ()
3014 "Parse line break at point, if any.
3016 When at a line break, return a list whose car is `line-break',
3017 and cdr a plist with `:begin', `:end' and `:post-blank' keywords.
3018 Otherwise, return nil.
3020 Assume point is at the beginning of the line break."
3021 (when (and (looking-at-p "\\\\\\\\[ \t]*$")
3022 (not (eq (char-before) ?\\)))
3023 (list 'line-break
3024 (list :begin (point)
3025 :end (line-beginning-position 2)
3026 :post-blank 0))))
3028 (defun org-element-line-break-interpreter (&rest _)
3029 "Interpret LINE-BREAK object as Org syntax."
3030 "\\\\\n")
3033 ;;;; Link
3035 (defun org-element-link-parser ()
3036 "Parse link at point, if any.
3038 When at a link, return a list whose car is `link' and cdr a plist
3039 with `:type', `:path', `:format', `:raw-link', `:application',
3040 `:search-option', `:begin', `:end', `:contents-begin',
3041 `:contents-end' and `:post-blank' as keywords. Otherwise, return
3042 nil.
3044 Assume point is at the beginning of the link."
3045 (catch 'no-object
3046 (let ((begin (point))
3047 end contents-begin contents-end link-end post-blank path type format
3048 raw-link search-option application)
3049 (cond
3050 ;; Type 1: Text targeted from a radio target.
3051 ((and org-target-link-regexp
3052 (save-excursion (or (bolp) (backward-char))
3053 (looking-at org-target-link-regexp)))
3054 (setq type "radio")
3055 (setq format 'plain)
3056 (setq link-end (match-end 1))
3057 (setq path (match-string-no-properties 1))
3058 (setq contents-begin (match-beginning 1))
3059 (setq contents-end (match-end 1)))
3060 ;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]]
3061 ((looking-at org-bracket-link-regexp)
3062 (setq format 'bracket)
3063 (setq contents-begin (match-beginning 3))
3064 (setq contents-end (match-end 3))
3065 (setq link-end (match-end 0))
3066 ;; RAW-LINK is the original link. Expand any
3067 ;; abbreviation in it.
3069 ;; Also treat any newline character and associated
3070 ;; indentation as a single space character. This is not
3071 ;; compatible with RFC 3986, which requires to ignore
3072 ;; them altogether. However, doing so would require
3073 ;; users to encode spaces on the fly when writing links
3074 ;; (e.g., insert [[shell:ls%20*.org]] instead of
3075 ;; [[shell:ls *.org]], which defeats Org's focus on
3076 ;; simplicity.
3077 (setq raw-link (org-link-expand-abbrev
3078 (replace-regexp-in-string
3079 "[ \t]*\n[ \t]*" " "
3080 (match-string-no-properties 1))))
3081 ;; Determine TYPE of link and set PATH accordingly. According
3082 ;; to RFC 3986, remove whitespaces from URI in external links.
3083 ;; In internal ones, treat indentation as a single space.
3084 (cond
3085 ;; File type.
3086 ((or (file-name-absolute-p raw-link)
3087 (string-match "\\`\\.\\.?/" raw-link))
3088 (setq type "file")
3089 (setq path raw-link))
3090 ;; Explicit type (http, irc, bbdb...).
3091 ((string-match org-link-types-re raw-link)
3092 (setq type (match-string 1 raw-link))
3093 (setq path (substring raw-link (match-end 0))))
3094 ;; Code-ref type: PATH is the name of the reference.
3095 ((and (string-match-p "\\`(" raw-link)
3096 (string-match-p ")\\'" raw-link))
3097 (setq type "coderef")
3098 (setq path (substring raw-link 1 -1)))
3099 ;; Custom-id type: PATH is the name of the custom id.
3100 ((= (string-to-char raw-link) ?#)
3101 (setq type "custom-id")
3102 (setq path (substring raw-link 1)))
3103 ;; Fuzzy type: Internal link either matches a target, an
3104 ;; headline name or nothing. PATH is the target or
3105 ;; headline's name.
3107 (setq type "fuzzy")
3108 (setq path raw-link))))
3109 ;; Type 3: Plain link, e.g., http://orgmode.org
3110 ((looking-at org-plain-link-re)
3111 (setq format 'plain)
3112 (setq raw-link (match-string-no-properties 0))
3113 (setq type (match-string-no-properties 1))
3114 (setq link-end (match-end 0))
3115 (setq path (match-string-no-properties 2)))
3116 ;; Type 4: Angular link, e.g., <http://orgmode.org>. Unlike to
3117 ;; bracket links, follow RFC 3986 and remove any extra
3118 ;; whitespace in URI.
3119 ((looking-at org-angle-link-re)
3120 (setq format 'angle)
3121 (setq type (match-string-no-properties 1))
3122 (setq link-end (match-end 0))
3123 (setq raw-link
3124 (buffer-substring-no-properties
3125 (match-beginning 1) (match-end 2)))
3126 (setq path (replace-regexp-in-string
3127 "[ \t]*\n[ \t]*" "" (match-string-no-properties 2))))
3128 (t (throw 'no-object nil)))
3129 ;; In any case, deduce end point after trailing white space from
3130 ;; LINK-END variable.
3131 (save-excursion
3132 (setq post-blank
3133 (progn (goto-char link-end) (skip-chars-forward " \t")))
3134 (setq end (point)))
3135 ;; Special "file" type link processing. Extract opening
3136 ;; application and search option, if any. Also normalize URI.
3137 (when (string-match "\\`file\\(?:\\+\\(.+\\)\\)?\\'" type)
3138 (setq application (match-string 1 type) type "file")
3139 (when (string-match "::\\(.*\\)\\'" path)
3140 (setq search-option (match-string 1 path))
3141 (setq path (replace-match "" nil nil path)))
3142 (setq path (replace-regexp-in-string "\\`///*\\(.:\\)?/" "\\1/" path)))
3143 ;; Translate link, if `org-link-translation-function' is set.
3144 (let ((trans (and (functionp org-link-translation-function)
3145 (funcall org-link-translation-function type path))))
3146 (when trans
3147 (setq type (car trans))
3148 (setq path (cdr trans))))
3149 (list 'link
3150 (list :type type
3151 :path path
3152 :format format
3153 :raw-link (or raw-link path)
3154 :application application
3155 :search-option search-option
3156 :begin begin
3157 :end end
3158 :contents-begin contents-begin
3159 :contents-end contents-end
3160 :post-blank post-blank)))))
3162 (defun org-element-link-interpreter (link contents)
3163 "Interpret LINK object as Org syntax.
3164 CONTENTS is the contents of the object, or nil."
3165 (let ((type (org-element-property :type link))
3166 (path (org-element-property :path link)))
3167 (if (string= type "radio") path
3168 (let ((fmt (pcase (org-element-property :format link)
3169 ;; Links with contents and internal links have to
3170 ;; use bracket syntax. Ignore `:format' in these
3171 ;; cases. This is also the default syntax when the
3172 ;; property is not defined, e.g., when the object
3173 ;; was crafted by the user.
3174 ((guard contents)
3175 (format "[[%%s][%s]]"
3176 ;; Since this is going to be used as
3177 ;; a format string, escape percent signs
3178 ;; in description.
3179 (replace-regexp-in-string "%" "%%" contents)))
3180 ((or `bracket
3181 `nil
3182 (guard (member type '("coderef" "custom-id" "fuzzy"))))
3183 "[[%s]]")
3184 ;; Otherwise, just obey to `:format'.
3185 (`angle "<%s>")
3186 (`plain "%s")
3187 (f (error "Wrong `:format' value: %s" f)))))
3188 (format fmt
3189 (pcase type
3190 ("coderef" (format "(%s)" path))
3191 ("custom-id" (concat "#" path))
3192 ("file"
3193 (let ((app (org-element-property :application link))
3194 (opt (org-element-property :search-option link)))
3195 (concat type (and app (concat "+" app)) ":"
3196 path
3197 (and opt (concat "::" opt)))))
3198 ("fuzzy" path)
3199 (_ (concat type ":" path))))))))
3202 ;;;; Macro
3204 (defun org-element-macro-parser ()
3205 "Parse macro at point, if any.
3207 When at a macro, return a list whose car is `macro' and cdr
3208 a plist with `:key', `:args', `:begin', `:end', `:value' and
3209 `:post-blank' as keywords. Otherwise, return nil.
3211 Assume point is at the macro."
3212 (save-excursion
3213 (when (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
3214 (let ((begin (point))
3215 (key (downcase (match-string-no-properties 1)))
3216 (value (match-string-no-properties 0))
3217 (post-blank (progn (goto-char (match-end 0))
3218 (skip-chars-forward " \t")))
3219 (end (point))
3220 (args (let ((args (match-string-no-properties 3)))
3221 (and args (org-macro-extract-arguments args)))))
3222 (list 'macro
3223 (list :key key
3224 :value value
3225 :args args
3226 :begin begin
3227 :end end
3228 :post-blank post-blank))))))
3230 (defun org-element-macro-interpreter (macro _)
3231 "Interpret MACRO object as Org syntax."
3232 (org-element-property :value macro))
3235 ;;;; Radio-target
3237 (defun org-element-radio-target-parser ()
3238 "Parse radio target at point, if any.
3240 When at a radio target, return a list whose car is `radio-target'
3241 and cdr a plist with `:begin', `:end', `:contents-begin',
3242 `:contents-end', `:value' and `:post-blank' as keywords.
3243 Otherwise, return nil.
3245 Assume point is at the radio target."
3246 (save-excursion
3247 (when (looking-at org-radio-target-regexp)
3248 (let ((begin (point))
3249 (contents-begin (match-beginning 1))
3250 (contents-end (match-end 1))
3251 (value (match-string-no-properties 1))
3252 (post-blank (progn (goto-char (match-end 0))
3253 (skip-chars-forward " \t")))
3254 (end (point)))
3255 (list 'radio-target
3256 (list :begin begin
3257 :end end
3258 :contents-begin contents-begin
3259 :contents-end contents-end
3260 :post-blank post-blank
3261 :value value))))))
3263 (defun org-element-radio-target-interpreter (_ contents)
3264 "Interpret target object as Org syntax.
3265 CONTENTS is the contents of the object."
3266 (concat "<<<" contents ">>>"))
3269 ;;;; Statistics Cookie
3271 (defun org-element-statistics-cookie-parser ()
3272 "Parse statistics cookie at point, if any.
3274 When at a statistics cookie, return a list whose car is
3275 `statistics-cookie', and cdr a plist with `:begin', `:end',
3276 `:value' and `:post-blank' keywords. Otherwise, return nil.
3278 Assume point is at the beginning of the statistics-cookie."
3279 (save-excursion
3280 (when (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
3281 (let* ((begin (point))
3282 (value (buffer-substring-no-properties
3283 (match-beginning 0) (match-end 0)))
3284 (post-blank (progn (goto-char (match-end 0))
3285 (skip-chars-forward " \t")))
3286 (end (point)))
3287 (list 'statistics-cookie
3288 (list :begin begin
3289 :end end
3290 :value value
3291 :post-blank post-blank))))))
3293 (defun org-element-statistics-cookie-interpreter (statistics-cookie _)
3294 "Interpret STATISTICS-COOKIE object as Org syntax."
3295 (org-element-property :value statistics-cookie))
3298 ;;;; Strike-Through
3300 (defun org-element-strike-through-parser ()
3301 "Parse strike-through object at point, if any.
3303 When at a strike-through object, return a list whose car is
3304 `strike-through' and cdr is a plist with `:begin', `:end',
3305 `:contents-begin' and `:contents-end' and `:post-blank' keywords.
3306 Otherwise, return nil.
3308 Assume point is at the first plus sign marker."
3309 (save-excursion
3310 (unless (bolp) (backward-char 1))
3311 (when (looking-at org-emph-re)
3312 (let ((begin (match-beginning 2))
3313 (contents-begin (match-beginning 4))
3314 (contents-end (match-end 4))
3315 (post-blank (progn (goto-char (match-end 2))
3316 (skip-chars-forward " \t")))
3317 (end (point)))
3318 (list 'strike-through
3319 (list :begin begin
3320 :end end
3321 :contents-begin contents-begin
3322 :contents-end contents-end
3323 :post-blank post-blank))))))
3325 (defun org-element-strike-through-interpreter (_ contents)
3326 "Interpret strike-through object as Org syntax.
3327 CONTENTS is the contents of the object."
3328 (format "+%s+" contents))
3331 ;;;; Subscript
3333 (defun org-element-subscript-parser ()
3334 "Parse subscript at point, if any.
3336 When at a subscript object, return a list whose car is
3337 `subscript' and cdr a plist with `:begin', `:end',
3338 `:contents-begin', `:contents-end', `:use-brackets-p' and
3339 `:post-blank' as keywords. Otherwise, return nil.
3341 Assume point is at the underscore."
3342 (save-excursion
3343 (unless (bolp) (backward-char))
3344 (when (looking-at org-match-substring-regexp)
3345 (let ((bracketsp (match-beginning 4))
3346 (begin (match-beginning 2))
3347 (contents-begin (or (match-beginning 4)
3348 (match-beginning 3)))
3349 (contents-end (or (match-end 4) (match-end 3)))
3350 (post-blank (progn (goto-char (match-end 0))
3351 (skip-chars-forward " \t")))
3352 (end (point)))
3353 (list 'subscript
3354 (list :begin begin
3355 :end end
3356 :use-brackets-p bracketsp
3357 :contents-begin contents-begin
3358 :contents-end contents-end
3359 :post-blank post-blank))))))
3361 (defun org-element-subscript-interpreter (subscript contents)
3362 "Interpret SUBSCRIPT object as Org syntax.
3363 CONTENTS is the contents of the object."
3364 (format
3365 (if (org-element-property :use-brackets-p subscript) "_{%s}" "_%s")
3366 contents))
3369 ;;;; Superscript
3371 (defun org-element-superscript-parser ()
3372 "Parse superscript at point, if any.
3374 When at a superscript object, return a list whose car is
3375 `superscript' and cdr a plist with `:begin', `:end',
3376 `:contents-begin', `:contents-end', `:use-brackets-p' and
3377 `:post-blank' as keywords. Otherwise, return nil.
3379 Assume point is at the caret."
3380 (save-excursion
3381 (unless (bolp) (backward-char))
3382 (when (looking-at org-match-substring-regexp)
3383 (let ((bracketsp (match-beginning 4))
3384 (begin (match-beginning 2))
3385 (contents-begin (or (match-beginning 4)
3386 (match-beginning 3)))
3387 (contents-end (or (match-end 4) (match-end 3)))
3388 (post-blank (progn (goto-char (match-end 0))
3389 (skip-chars-forward " \t")))
3390 (end (point)))
3391 (list 'superscript
3392 (list :begin begin
3393 :end end
3394 :use-brackets-p bracketsp
3395 :contents-begin contents-begin
3396 :contents-end contents-end
3397 :post-blank post-blank))))))
3399 (defun org-element-superscript-interpreter (superscript contents)
3400 "Interpret SUPERSCRIPT object as Org syntax.
3401 CONTENTS is the contents of the object."
3402 (format
3403 (if (org-element-property :use-brackets-p superscript) "^{%s}" "^%s")
3404 contents))
3407 ;;;; Table Cell
3409 (defun org-element-table-cell-parser ()
3410 "Parse table cell at point.
3411 Return a list whose car is `table-cell' and cdr is a plist
3412 containing `:begin', `:end', `:contents-begin', `:contents-end'
3413 and `:post-blank' keywords."
3414 (looking-at "[ \t]*\\(.*?\\)[ \t]*\\(?:|\\|$\\)")
3415 (let* ((begin (match-beginning 0))
3416 (end (match-end 0))
3417 (contents-begin (match-beginning 1))
3418 (contents-end (match-end 1)))
3419 (list 'table-cell
3420 (list :begin begin
3421 :end end
3422 :contents-begin contents-begin
3423 :contents-end contents-end
3424 :post-blank 0))))
3426 (defun org-element-table-cell-interpreter (_ contents)
3427 "Interpret table-cell element as Org syntax.
3428 CONTENTS is the contents of the cell, or nil."
3429 (concat " " contents " |"))
3432 ;;;; Target
3434 (defun org-element-target-parser ()
3435 "Parse target at point, if any.
3437 When at a target, return a list whose car is `target' and cdr
3438 a plist with `:begin', `:end', `:value' and `:post-blank' as
3439 keywords. Otherwise, return nil.
3441 Assume point is at the target."
3442 (save-excursion
3443 (when (looking-at org-target-regexp)
3444 (let ((begin (point))
3445 (value (match-string-no-properties 1))
3446 (post-blank (progn (goto-char (match-end 0))
3447 (skip-chars-forward " \t")))
3448 (end (point)))
3449 (list 'target
3450 (list :begin begin
3451 :end end
3452 :value value
3453 :post-blank post-blank))))))
3455 (defun org-element-target-interpreter (target _)
3456 "Interpret TARGET object as Org syntax."
3457 (format "<<%s>>" (org-element-property :value target)))
3460 ;;;; Timestamp
3462 (defconst org-element--timestamp-regexp
3463 (concat org-ts-regexp-both
3464 "\\|"
3465 "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
3466 "\\|"
3467 "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
3468 "Regexp matching any timestamp type object.")
3470 (defun org-element-timestamp-parser ()
3471 "Parse time stamp at point, if any.
3473 When at a time stamp, return a list whose car is `timestamp', and
3474 cdr a plist with `:type', `:raw-value', `:year-start',
3475 `:month-start', `:day-start', `:hour-start', `:minute-start',
3476 `:year-end', `:month-end', `:day-end', `:hour-end',
3477 `:minute-end', `:repeater-type', `:repeater-value',
3478 `:repeater-unit', `:warning-type', `:warning-value',
3479 `:warning-unit', `:begin', `:end' and `:post-blank' keywords.
3480 Otherwise, return nil.
3482 Assume point is at the beginning of the timestamp."
3483 (when (looking-at-p org-element--timestamp-regexp)
3484 (save-excursion
3485 (let* ((begin (point))
3486 (activep (eq (char-after) ?<))
3487 (raw-value
3488 (progn
3489 (looking-at "\\([<[]\\(%%\\)?.*?\\)[]>]\\(?:--\\([<[].*?[]>]\\)\\)?")
3490 (match-string-no-properties 0)))
3491 (date-start (match-string-no-properties 1))
3492 (date-end (match-string 3))
3493 (diaryp (match-beginning 2))
3494 (post-blank (progn (goto-char (match-end 0))
3495 (skip-chars-forward " \t")))
3496 (end (point))
3497 (time-range
3498 (and (not diaryp)
3499 (string-match
3500 "[012]?[0-9]:[0-5][0-9]\\(-\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)"
3501 date-start)
3502 (cons (string-to-number (match-string 2 date-start))
3503 (string-to-number (match-string 3 date-start)))))
3504 (type (cond (diaryp 'diary)
3505 ((and activep (or date-end time-range)) 'active-range)
3506 (activep 'active)
3507 ((or date-end time-range) 'inactive-range)
3508 (t 'inactive)))
3509 (repeater-props
3510 (and (not diaryp)
3511 (string-match "\\([.+]?\\+\\)\\([0-9]+\\)\\([hdwmy]\\)"
3512 raw-value)
3513 (list
3514 :repeater-type
3515 (let ((type (match-string 1 raw-value)))
3516 (cond ((equal "++" type) 'catch-up)
3517 ((equal ".+" type) 'restart)
3518 (t 'cumulate)))
3519 :repeater-value (string-to-number (match-string 2 raw-value))
3520 :repeater-unit
3521 (pcase (string-to-char (match-string 3 raw-value))
3522 (?h 'hour) (?d 'day) (?w 'week) (?m 'month) (_ 'year)))))
3523 (warning-props
3524 (and (not diaryp)
3525 (string-match "\\(-\\)?-\\([0-9]+\\)\\([hdwmy]\\)" raw-value)
3526 (list
3527 :warning-type (if (match-string 1 raw-value) 'first 'all)
3528 :warning-value (string-to-number (match-string 2 raw-value))
3529 :warning-unit
3530 (pcase (string-to-char (match-string 3 raw-value))
3531 (?h 'hour) (?d 'day) (?w 'week) (?m 'month) (_ 'year)))))
3532 year-start month-start day-start hour-start minute-start year-end
3533 month-end day-end hour-end minute-end)
3534 ;; Parse date-start.
3535 (unless diaryp
3536 (let ((date (org-parse-time-string date-start t)))
3537 (setq year-start (nth 5 date)
3538 month-start (nth 4 date)
3539 day-start (nth 3 date)
3540 hour-start (nth 2 date)
3541 minute-start (nth 1 date))))
3542 ;; Compute date-end. It can be provided directly in time-stamp,
3543 ;; or extracted from time range. Otherwise, it defaults to the
3544 ;; same values as date-start.
3545 (unless diaryp
3546 (let ((date (and date-end (org-parse-time-string date-end t))))
3547 (setq year-end (or (nth 5 date) year-start)
3548 month-end (or (nth 4 date) month-start)
3549 day-end (or (nth 3 date) day-start)
3550 hour-end (or (nth 2 date) (car time-range) hour-start)
3551 minute-end (or (nth 1 date) (cdr time-range) minute-start))))
3552 (list 'timestamp
3553 (nconc (list :type type
3554 :raw-value raw-value
3555 :year-start year-start
3556 :month-start month-start
3557 :day-start day-start
3558 :hour-start hour-start
3559 :minute-start minute-start
3560 :year-end year-end
3561 :month-end month-end
3562 :day-end day-end
3563 :hour-end hour-end
3564 :minute-end minute-end
3565 :begin begin
3566 :end end
3567 :post-blank post-blank)
3568 repeater-props
3569 warning-props))))))
3571 (defun org-element-timestamp-interpreter (timestamp _)
3572 "Interpret TIMESTAMP object as Org syntax."
3573 (let* ((repeat-string
3574 (concat
3575 (pcase (org-element-property :repeater-type timestamp)
3576 (`cumulate "+") (`catch-up "++") (`restart ".+"))
3577 (let ((val (org-element-property :repeater-value timestamp)))
3578 (and val (number-to-string val)))
3579 (pcase (org-element-property :repeater-unit timestamp)
3580 (`hour "h") (`day "d") (`week "w") (`month "m") (`year "y"))))
3581 (warning-string
3582 (concat
3583 (pcase (org-element-property :warning-type timestamp)
3584 (`first "--") (`all "-"))
3585 (let ((val (org-element-property :warning-value timestamp)))
3586 (and val (number-to-string val)))
3587 (pcase (org-element-property :warning-unit timestamp)
3588 (`hour "h") (`day "d") (`week "w") (`month "m") (`year "y"))))
3589 (build-ts-string
3590 ;; Build an Org timestamp string from TIME. ACTIVEP is
3591 ;; non-nil when time stamp is active. If WITH-TIME-P is
3592 ;; non-nil, add a time part. HOUR-END and MINUTE-END
3593 ;; specify a time range in the timestamp. REPEAT-STRING is
3594 ;; the repeater string, if any.
3595 (lambda (time activep &optional with-time-p hour-end minute-end)
3596 (let ((ts (format-time-string
3597 (funcall (if with-time-p #'cdr #'car)
3598 org-time-stamp-formats)
3599 time)))
3600 (when (and hour-end minute-end)
3601 (string-match "[012]?[0-9]:[0-5][0-9]" ts)
3602 (setq ts
3603 (replace-match
3604 (format "\\&-%02d:%02d" hour-end minute-end)
3605 nil nil ts)))
3606 (unless activep (setq ts (format "[%s]" (substring ts 1 -1))))
3607 (dolist (s (list repeat-string warning-string))
3608 (when (org-string-nw-p s)
3609 (setq ts (concat (substring ts 0 -1)
3612 (substring ts -1)))))
3613 ;; Return value.
3614 ts)))
3615 (type (org-element-property :type timestamp)))
3616 (pcase type
3617 ((or `active `inactive)
3618 (let* ((minute-start (org-element-property :minute-start timestamp))
3619 (minute-end (org-element-property :minute-end timestamp))
3620 (hour-start (org-element-property :hour-start timestamp))
3621 (hour-end (org-element-property :hour-end timestamp))
3622 (time-range-p (and hour-start hour-end minute-start minute-end
3623 (or (/= hour-start hour-end)
3624 (/= minute-start minute-end)))))
3625 (funcall
3626 build-ts-string
3627 (encode-time 0
3628 (or minute-start 0)
3629 (or hour-start 0)
3630 (org-element-property :day-start timestamp)
3631 (org-element-property :month-start timestamp)
3632 (org-element-property :year-start timestamp))
3633 (eq type 'active)
3634 (and hour-start minute-start)
3635 (and time-range-p hour-end)
3636 (and time-range-p minute-end))))
3637 ((or `active-range `inactive-range)
3638 (let ((minute-start (org-element-property :minute-start timestamp))
3639 (minute-end (org-element-property :minute-end timestamp))
3640 (hour-start (org-element-property :hour-start timestamp))
3641 (hour-end (org-element-property :hour-end timestamp)))
3642 (concat
3643 (funcall
3644 build-ts-string (encode-time
3646 (or minute-start 0)
3647 (or hour-start 0)
3648 (org-element-property :day-start timestamp)
3649 (org-element-property :month-start timestamp)
3650 (org-element-property :year-start timestamp))
3651 (eq type 'active-range)
3652 (and hour-start minute-start))
3653 "--"
3654 (funcall build-ts-string
3655 (encode-time 0
3656 (or minute-end 0)
3657 (or hour-end 0)
3658 (org-element-property :day-end timestamp)
3659 (org-element-property :month-end timestamp)
3660 (org-element-property :year-end timestamp))
3661 (eq type 'active-range)
3662 (and hour-end minute-end)))))
3663 (_ (org-element-property :raw-value timestamp)))))
3666 ;;;; Underline
3668 (defun org-element-underline-parser ()
3669 "Parse underline object at point, if any.
3671 When at an underline object, return a list whose car is
3672 `underline' and cdr is a plist with `:begin', `:end',
3673 `:contents-begin' and `:contents-end' and `:post-blank' keywords.
3674 Otherwise, return nil.
3676 Assume point is at the first underscore marker."
3677 (save-excursion
3678 (unless (bolp) (backward-char 1))
3679 (when (looking-at org-emph-re)
3680 (let ((begin (match-beginning 2))
3681 (contents-begin (match-beginning 4))
3682 (contents-end (match-end 4))
3683 (post-blank (progn (goto-char (match-end 2))
3684 (skip-chars-forward " \t")))
3685 (end (point)))
3686 (list 'underline
3687 (list :begin begin
3688 :end end
3689 :contents-begin contents-begin
3690 :contents-end contents-end
3691 :post-blank post-blank))))))
3693 (defun org-element-underline-interpreter (_ contents)
3694 "Interpret underline object as Org syntax.
3695 CONTENTS is the contents of the object."
3696 (format "_%s_" contents))
3699 ;;;; Verbatim
3701 (defun org-element-verbatim-parser ()
3702 "Parse verbatim object at point, if any.
3704 When at a verbatim object, return a list whose car is `verbatim'
3705 and cdr is a plist with `:value', `:begin', `:end' and
3706 `:post-blank' keywords. Otherwise, return nil.
3708 Assume point is at the first equal sign marker."
3709 (save-excursion
3710 (unless (bolp) (backward-char 1))
3711 (when (looking-at org-emph-re)
3712 (let ((begin (match-beginning 2))
3713 (value (match-string-no-properties 4))
3714 (post-blank (progn (goto-char (match-end 2))
3715 (skip-chars-forward " \t")))
3716 (end (point)))
3717 (list 'verbatim
3718 (list :value value
3719 :begin begin
3720 :end end
3721 :post-blank post-blank))))))
3723 (defun org-element-verbatim-interpreter (verbatim _)
3724 "Interpret VERBATIM object as Org syntax."
3725 (format "=%s=" (org-element-property :value verbatim)))
3729 ;;; Parsing Element Starting At Point
3731 ;; `org-element--current-element' is the core function of this section.
3732 ;; It returns the Lisp representation of the element starting at
3733 ;; point.
3735 ;; `org-element--current-element' makes use of special modes. They
3736 ;; are activated for fixed element chaining (e.g., `plain-list' >
3737 ;; `item') or fixed conditional element chaining (e.g., `headline' >
3738 ;; `section'). Special modes are: `first-section', `item',
3739 ;; `node-property', `section' and `table-row'.
3741 (defun org-element--current-element (limit &optional granularity mode structure)
3742 "Parse the element starting at point.
3744 Return value is a list like (TYPE PROPS) where TYPE is the type
3745 of the element and PROPS a plist of properties associated to the
3746 element.
3748 Possible types are defined in `org-element-all-elements'.
3750 LIMIT bounds the search.
3752 Optional argument GRANULARITY determines the depth of the
3753 recursion. Allowed values are `headline', `greater-element',
3754 `element', `object' or nil. When it is broader than `object' (or
3755 nil), secondary values will not be parsed, since they only
3756 contain objects.
3758 Optional argument MODE, when non-nil, can be either
3759 `first-section', `section', `planning', `item', `node-property'
3760 and `table-row'.
3762 If STRUCTURE isn't provided but MODE is set to `item', it will be
3763 computed.
3765 This function assumes point is always at the beginning of the
3766 element it has to parse."
3767 (save-excursion
3768 (let ((case-fold-search t)
3769 ;; Determine if parsing depth allows for secondary strings
3770 ;; parsing. It only applies to elements referenced in
3771 ;; `org-element-secondary-value-alist'.
3772 (raw-secondary-p (and granularity (not (eq granularity 'object)))))
3773 (cond
3774 ;; Item.
3775 ((eq mode 'item)
3776 (org-element-item-parser limit structure raw-secondary-p))
3777 ;; Table Row.
3778 ((eq mode 'table-row) (org-element-table-row-parser limit))
3779 ;; Node Property.
3780 ((eq mode 'node-property) (org-element-node-property-parser limit))
3781 ;; Headline.
3782 ((org-with-limited-levels (org-at-heading-p))
3783 (org-element-headline-parser limit raw-secondary-p))
3784 ;; Sections (must be checked after headline).
3785 ((eq mode 'section) (org-element-section-parser limit))
3786 ((eq mode 'first-section)
3787 (org-element-section-parser
3788 (or (save-excursion (org-with-limited-levels (outline-next-heading)))
3789 limit)))
3790 ;; Planning.
3791 ((and (eq mode 'planning)
3792 (eq ?* (char-after (line-beginning-position 0)))
3793 (looking-at org-planning-line-re))
3794 (org-element-planning-parser limit))
3795 ;; Property drawer.
3796 ((and (memq mode '(planning property-drawer))
3797 (eq ?* (char-after (line-beginning-position
3798 (if (eq mode 'planning) 0 -1))))
3799 (looking-at org-property-drawer-re))
3800 (org-element-property-drawer-parser limit))
3801 ;; When not at bol, point is at the beginning of an item or
3802 ;; a footnote definition: next item is always a paragraph.
3803 ((not (bolp)) (org-element-paragraph-parser limit (list (point))))
3804 ;; Clock.
3805 ((looking-at org-clock-line-re) (org-element-clock-parser limit))
3806 ;; Inlinetask.
3807 ((org-at-heading-p)
3808 (org-element-inlinetask-parser limit raw-secondary-p))
3809 ;; From there, elements can have affiliated keywords.
3810 (t (let ((affiliated (org-element--collect-affiliated-keywords limit)))
3811 (cond
3812 ;; Jumping over affiliated keywords put point off-limits.
3813 ;; Parse them as regular keywords.
3814 ((and (cdr affiliated) (>= (point) limit))
3815 (goto-char (car affiliated))
3816 (org-element-keyword-parser limit nil))
3817 ;; LaTeX Environment.
3818 ((looking-at org-element--latex-begin-environment)
3819 (org-element-latex-environment-parser limit affiliated))
3820 ;; Drawer and Property Drawer.
3821 ((looking-at org-drawer-regexp)
3822 (org-element-drawer-parser limit affiliated))
3823 ;; Fixed Width
3824 ((looking-at "[ \t]*:\\( \\|$\\)")
3825 (org-element-fixed-width-parser limit affiliated))
3826 ;; Inline Comments, Blocks, Babel Calls, Dynamic Blocks and
3827 ;; Keywords.
3828 ((looking-at "[ \t]*#")
3829 (goto-char (match-end 0))
3830 (cond
3831 ((looking-at "\\(?: \\|$\\)")
3832 (beginning-of-line)
3833 (org-element-comment-parser limit affiliated))
3834 ((looking-at "\\+BEGIN_\\(\\S-+\\)")
3835 (beginning-of-line)
3836 (funcall (pcase (upcase (match-string 1))
3837 ("CENTER" #'org-element-center-block-parser)
3838 ("COMMENT" #'org-element-comment-block-parser)
3839 ("EXAMPLE" #'org-element-example-block-parser)
3840 ("EXPORT" #'org-element-export-block-parser)
3841 ("QUOTE" #'org-element-quote-block-parser)
3842 ("SRC" #'org-element-src-block-parser)
3843 ("VERSE" #'org-element-verse-block-parser)
3844 (_ #'org-element-special-block-parser))
3845 limit
3846 affiliated))
3847 ((looking-at "\\+CALL:")
3848 (beginning-of-line)
3849 (org-element-babel-call-parser limit affiliated))
3850 ((looking-at "\\+BEGIN:? ")
3851 (beginning-of-line)
3852 (org-element-dynamic-block-parser limit affiliated))
3853 ((looking-at "\\+\\S-+:")
3854 (beginning-of-line)
3855 (org-element-keyword-parser limit affiliated))
3857 (beginning-of-line)
3858 (org-element-paragraph-parser limit affiliated))))
3859 ;; Footnote Definition.
3860 ((looking-at org-footnote-definition-re)
3861 (org-element-footnote-definition-parser limit affiliated))
3862 ;; Horizontal Rule.
3863 ((looking-at "[ \t]*-\\{5,\\}[ \t]*$")
3864 (org-element-horizontal-rule-parser limit affiliated))
3865 ;; Diary Sexp.
3866 ((looking-at "%%(")
3867 (org-element-diary-sexp-parser limit affiliated))
3868 ;; Table.
3869 ((looking-at "[ \t]*\\(|\\|\\+\\(-+\\+\\)+[ \t]*$\\)")
3870 (org-element-table-parser limit affiliated))
3871 ;; List.
3872 ((looking-at (org-item-re))
3873 (org-element-plain-list-parser
3874 limit affiliated
3875 (or structure (org-element--list-struct limit))))
3876 ;; Default element: Paragraph.
3877 (t (org-element-paragraph-parser limit affiliated)))))))))
3880 ;; Most elements can have affiliated keywords. When looking for an
3881 ;; element beginning, we want to move before them, as they belong to
3882 ;; that element, and, in the meantime, collect information they give
3883 ;; into appropriate properties. Hence the following function.
3885 (defun org-element--collect-affiliated-keywords (limit)
3886 "Collect affiliated keywords from point down to LIMIT.
3888 Return a list whose CAR is the position at the first of them and
3889 CDR a plist of keywords and values and move point to the
3890 beginning of the first line after them.
3892 As a special case, if element doesn't start at the beginning of
3893 the line (e.g., a paragraph starting an item), CAR is current
3894 position of point and CDR is nil."
3895 (if (not (bolp)) (list (point))
3896 (let ((case-fold-search t)
3897 (origin (point))
3898 ;; RESTRICT is the list of objects allowed in parsed
3899 ;; keywords value.
3900 (restrict (org-element-restriction 'keyword))
3901 output)
3902 (while (and (< (point) limit) (looking-at org-element--affiliated-re))
3903 (let* ((raw-kwd (upcase (match-string 1)))
3904 ;; Apply translation to RAW-KWD. From there, KWD is
3905 ;; the official keyword.
3906 (kwd (or (cdr (assoc raw-kwd
3907 org-element-keyword-translation-alist))
3908 raw-kwd))
3909 ;; Find main value for any keyword.
3910 (value
3911 (save-match-data
3912 (org-trim
3913 (buffer-substring-no-properties
3914 (match-end 0) (line-end-position)))))
3915 ;; PARSEDP is non-nil when keyword should have its
3916 ;; value parsed.
3917 (parsedp (member kwd org-element-parsed-keywords))
3918 ;; If KWD is a dual keyword, find its secondary
3919 ;; value. Maybe parse it.
3920 (dualp (member kwd org-element-dual-keywords))
3921 (dual-value
3922 (and dualp
3923 (let ((sec (match-string-no-properties 2)))
3924 (if (or (not sec) (not parsedp)) sec
3925 (save-match-data
3926 (org-element--parse-objects
3927 (match-beginning 2) (match-end 2) nil restrict))))))
3928 ;; Attribute a property name to KWD.
3929 (kwd-sym (and kwd (intern (concat ":" (downcase kwd))))))
3930 ;; Now set final shape for VALUE.
3931 (when parsedp
3932 (setq value
3933 (org-element--parse-objects
3934 (match-end 0)
3935 (progn (end-of-line) (skip-chars-backward " \t") (point))
3936 nil restrict)))
3937 (when dualp
3938 (setq value (and (or value dual-value) (cons value dual-value))))
3939 (when (or (member kwd org-element-multiple-keywords)
3940 ;; Attributes can always appear on multiple lines.
3941 (string-match "^ATTR_" kwd))
3942 (setq value (cons value (plist-get output kwd-sym))))
3943 ;; Eventually store the new value in OUTPUT.
3944 (setq output (plist-put output kwd-sym value))
3945 ;; Move to next keyword.
3946 (forward-line)))
3947 ;; If affiliated keywords are orphaned: move back to first one.
3948 ;; They will be parsed as a paragraph.
3949 (when (looking-at "[ \t]*$") (goto-char origin) (setq output nil))
3950 ;; Return value.
3951 (cons origin output))))
3955 ;;; The Org Parser
3957 ;; The two major functions here are `org-element-parse-buffer', which
3958 ;; parses Org syntax inside the current buffer, taking into account
3959 ;; region, narrowing, or even visibility if specified, and
3960 ;; `org-element-parse-secondary-string', which parses objects within
3961 ;; a given string.
3963 ;; The (almost) almighty `org-element-map' allows applying a function
3964 ;; on elements or objects matching some type, and accumulating the
3965 ;; resulting values. In an export situation, it also skips unneeded
3966 ;; parts of the parse tree.
3968 (defun org-element-parse-buffer (&optional granularity visible-only)
3969 "Recursively parse the buffer and return structure.
3970 If narrowing is in effect, only parse the visible part of the
3971 buffer.
3973 Optional argument GRANULARITY determines the depth of the
3974 recursion. It can be set to the following symbols:
3976 `headline' Only parse headlines.
3977 `greater-element' Don't recurse into greater elements except
3978 headlines and sections. Thus, elements
3979 parsed are the top-level ones.
3980 `element' Parse everything but objects and plain text.
3981 `object' Parse the complete buffer (default).
3983 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3984 elements.
3986 An element or object is represented as a list with the
3987 pattern (TYPE PROPERTIES CONTENTS), where :
3989 TYPE is a symbol describing the element or object. See
3990 `org-element-all-elements' and `org-element-all-objects' for an
3991 exhaustive list of such symbols. One can retrieve it with
3992 `org-element-type' function.
3994 PROPERTIES is the list of attributes attached to the element or
3995 object, as a plist. Although most of them are specific to the
3996 element or object type, all types share `:begin', `:end',
3997 `:post-blank' and `:parent' properties, which respectively
3998 refer to buffer position where the element or object starts,
3999 ends, the number of white spaces or blank lines after it, and
4000 the element or object containing it. Properties values can be
4001 obtained by using `org-element-property' function.
4003 CONTENTS is a list of elements, objects or raw strings
4004 contained in the current element or object, when applicable.
4005 One can access them with `org-element-contents' function.
4007 The Org buffer has `org-data' as type and nil as properties.
4008 `org-element-map' function can be used to find specific elements
4009 or objects within the parse tree.
4011 This function assumes that current major mode is `org-mode'."
4012 (save-excursion
4013 (goto-char (point-min))
4014 (org-skip-whitespace)
4015 (org-element--parse-elements
4016 (point-at-bol) (point-max)
4017 ;; Start in `first-section' mode so text before the first
4018 ;; headline belongs to a section.
4019 'first-section nil granularity visible-only (list 'org-data nil))))
4021 (defun org-element-parse-secondary-string (string restriction &optional parent)
4022 "Recursively parse objects in STRING and return structure.
4024 RESTRICTION is a symbol limiting the object types that will be
4025 looked after.
4027 Optional argument PARENT, when non-nil, is the element or object
4028 containing the secondary string. It is used to set correctly
4029 `:parent' property within the string.
4031 If STRING is the empty string or nil, return nil."
4032 (cond
4033 ((not string) nil)
4034 ((equal string "") nil)
4035 (t (let ((local-variables (buffer-local-variables)))
4036 (with-temp-buffer
4037 (dolist (v local-variables)
4038 (ignore-errors
4039 (if (symbolp v) (makunbound v)
4040 (set (make-local-variable (car v)) (cdr v)))))
4041 (insert string)
4042 (restore-buffer-modified-p nil)
4043 (org-element--parse-objects
4044 (point-min) (point-max) nil restriction parent))))))
4046 (defun org-element-map
4047 (data types fun &optional info first-match no-recursion with-affiliated)
4048 "Map a function on selected elements or objects.
4050 DATA is a parse tree, an element, an object, a string, or a list
4051 of such constructs. TYPES is a symbol or list of symbols of
4052 elements or objects types (see `org-element-all-elements' and
4053 `org-element-all-objects' for a complete list of types). FUN is
4054 the function called on the matching element or object. It has to
4055 accept one argument: the element or object itself.
4057 When optional argument INFO is non-nil, it should be a plist
4058 holding export options. In that case, parts of the parse tree
4059 not exportable according to that property list will be skipped.
4061 When optional argument FIRST-MATCH is non-nil, stop at the first
4062 match for which FUN doesn't return nil, and return that value.
4064 Optional argument NO-RECURSION is a symbol or a list of symbols
4065 representing elements or objects types. `org-element-map' won't
4066 enter any recursive element or object whose type belongs to that
4067 list. Though, FUN can still be applied on them.
4069 When optional argument WITH-AFFILIATED is non-nil, FUN will also
4070 apply to matching objects within parsed affiliated keywords (see
4071 `org-element-parsed-keywords').
4073 Nil values returned from FUN do not appear in the results.
4076 Examples:
4077 ---------
4079 Assuming TREE is a variable containing an Org buffer parse tree,
4080 the following example will return a flat list of all `src-block'
4081 and `example-block' elements in it:
4083 (org-element-map tree \\='(example-block src-block) #\\='identity)
4085 The following snippet will find the first headline with a level
4086 of 1 and a \"phone\" tag, and will return its beginning position:
4088 (org-element-map tree \\='headline
4089 (lambda (hl)
4090 (and (= (org-element-property :level hl) 1)
4091 (member \"phone\" (org-element-property :tags hl))
4092 (org-element-property :begin hl)))
4093 nil t)
4095 The next example will return a flat list of all `plain-list' type
4096 elements in TREE that are not a sub-list themselves:
4098 (org-element-map tree \\='plain-list #\\='identity nil nil \\='plain-list)
4100 Eventually, this example will return a flat list of all `bold'
4101 type objects containing a `latex-snippet' type object, even
4102 looking into captions:
4104 (org-element-map tree \\='bold
4105 (lambda (b)
4106 (and (org-element-map b \\='latex-snippet #\\='identity nil t) b))
4107 nil nil nil t)"
4108 ;; Ensure TYPES and NO-RECURSION are a list, even of one element.
4109 (let* ((types (if (listp types) types (list types)))
4110 (no-recursion (if (listp no-recursion) no-recursion
4111 (list no-recursion)))
4112 ;; Recursion depth is determined by --CATEGORY.
4113 (--category
4114 (catch :--found
4115 (let ((category 'greater-elements)
4116 (all-objects (cons 'plain-text org-element-all-objects)))
4117 (dolist (type types category)
4118 (cond ((memq type all-objects)
4119 ;; If one object is found, the function has
4120 ;; to recurse into every object.
4121 (throw :--found 'objects))
4122 ((not (memq type org-element-greater-elements))
4123 ;; If one regular element is found, the
4124 ;; function has to recurse, at least, into
4125 ;; every element it encounters.
4126 (and (not (eq category 'elements))
4127 (setq category 'elements))))))))
4128 --acc)
4129 (letrec ((--walk-tree
4130 (lambda (--data)
4131 ;; Recursively walk DATA. INFO, if non-nil, is a plist
4132 ;; holding contextual information.
4133 (let ((--type (org-element-type --data)))
4134 (cond
4135 ((not --data))
4136 ;; Ignored element in an export context.
4137 ((and info (memq --data (plist-get info :ignore-list))))
4138 ;; List of elements or objects.
4139 ((not --type) (mapc --walk-tree --data))
4140 ;; Unconditionally enter parse trees.
4141 ((eq --type 'org-data)
4142 (mapc --walk-tree (org-element-contents --data)))
4144 ;; Check if TYPE is matching among TYPES. If so,
4145 ;; apply FUN to --DATA and accumulate return value
4146 ;; into --ACC (or exit if FIRST-MATCH is non-nil).
4147 (when (memq --type types)
4148 (let ((result (funcall fun --data)))
4149 (cond ((not result))
4150 (first-match (throw :--map-first-match result))
4151 (t (push result --acc)))))
4152 ;; If --DATA has a secondary string that can contain
4153 ;; objects with their type among TYPES, look inside.
4154 (when (and (eq --category 'objects) (not (stringp --data)))
4155 (dolist (p (cdr (assq --type
4156 org-element-secondary-value-alist)))
4157 (funcall --walk-tree (org-element-property p --data))))
4158 ;; If --DATA has any parsed affiliated keywords and
4159 ;; WITH-AFFILIATED is non-nil, look for objects in
4160 ;; them.
4161 (when (and with-affiliated
4162 (eq --category 'objects)
4163 (eq (org-element-class --data) 'element))
4164 (dolist (kwd-pair org-element--parsed-properties-alist)
4165 (let ((kwd (car kwd-pair))
4166 (value (org-element-property (cdr kwd-pair) --data)))
4167 ;; Pay attention to the type of parsed
4168 ;; keyword. In particular, preserve order for
4169 ;; multiple keywords.
4170 (cond
4171 ((not value))
4172 ((member kwd org-element-dual-keywords)
4173 (if (member kwd org-element-multiple-keywords)
4174 (dolist (line (reverse value))
4175 (funcall --walk-tree (cdr line))
4176 (funcall --walk-tree (car line)))
4177 (funcall --walk-tree (cdr value))
4178 (funcall --walk-tree (car value))))
4179 ((member kwd org-element-multiple-keywords)
4180 (mapc --walk-tree (reverse value)))
4181 (t (funcall --walk-tree value))))))
4182 ;; Determine if a recursion into --DATA is possible.
4183 (cond
4184 ;; --TYPE is explicitly removed from recursion.
4185 ((memq --type no-recursion))
4186 ;; --DATA has no contents.
4187 ((not (org-element-contents --data)))
4188 ;; Looking for greater elements but --DATA is
4189 ;; simply an element or an object.
4190 ((and (eq --category 'greater-elements)
4191 (not (memq --type org-element-greater-elements))))
4192 ;; Looking for elements but --DATA is an object.
4193 ((and (eq --category 'elements)
4194 (eq (org-element-class --data) 'object)))
4195 ;; In any other case, map contents.
4196 (t (mapc --walk-tree (org-element-contents --data))))))))))
4197 (catch :--map-first-match
4198 (funcall --walk-tree data)
4199 ;; Return value in a proper order.
4200 (nreverse --acc)))))
4201 (put 'org-element-map 'lisp-indent-function 2)
4203 ;; The following functions are internal parts of the parser.
4205 ;; The first one, `org-element--parse-elements' acts at the element's
4206 ;; level.
4208 ;; The second one, `org-element--parse-objects' applies on all objects
4209 ;; of a paragraph or a secondary string. It calls
4210 ;; `org-element--object-lex' to find the next object in the current
4211 ;; container.
4213 (defsubst org-element--next-mode (type parentp)
4214 "Return next special mode according to TYPE, or nil.
4215 TYPE is a symbol representing the type of an element or object
4216 containing next element if PARENTP is non-nil, or before it
4217 otherwise. Modes can be either `first-section', `item',
4218 `node-property', `planning', `property-drawer', `section',
4219 `table-row' or nil."
4220 (if parentp
4221 (pcase type
4222 (`headline 'section)
4223 (`inlinetask 'planning)
4224 (`plain-list 'item)
4225 (`property-drawer 'node-property)
4226 (`section 'planning)
4227 (`table 'table-row))
4228 (pcase type
4229 (`item 'item)
4230 (`node-property 'node-property)
4231 (`planning 'property-drawer)
4232 (`table-row 'table-row))))
4234 (defun org-element--parse-elements
4235 (beg end mode structure granularity visible-only acc)
4236 "Parse elements between BEG and END positions.
4238 MODE prioritizes some elements over the others. It can be set to
4239 `first-section', `section', `planning', `item', `node-property'
4240 or `table-row'.
4242 When value is `item', STRUCTURE will be used as the current list
4243 structure.
4245 GRANULARITY determines the depth of the recursion. See
4246 `org-element-parse-buffer' for more information.
4248 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
4249 elements.
4251 Elements are accumulated into ACC."
4252 (save-excursion
4253 (goto-char beg)
4254 ;; Visible only: skip invisible parts at the beginning of the
4255 ;; element.
4256 (when (and visible-only (org-invisible-p2))
4257 (goto-char (min (1+ (org-find-visible)) end)))
4258 ;; When parsing only headlines, skip any text before first one.
4259 (when (and (eq granularity 'headline) (not (org-at-heading-p)))
4260 (org-with-limited-levels (outline-next-heading)))
4261 (let (elements)
4262 (while (< (point) end)
4263 ;; Find current element's type and parse it accordingly to
4264 ;; its category.
4265 (let* ((element (org-element--current-element
4266 end granularity mode structure))
4267 (type (org-element-type element))
4268 (cbeg (org-element-property :contents-begin element)))
4269 (goto-char (org-element-property :end element))
4270 ;; Visible only: skip invisible parts between siblings.
4271 (when (and visible-only (org-invisible-p2))
4272 (goto-char (min (1+ (org-find-visible)) end)))
4273 ;; Fill ELEMENT contents by side-effect.
4274 (cond
4275 ;; If element has no contents, don't modify it.
4276 ((not cbeg))
4277 ;; Greater element: parse it between `contents-begin' and
4278 ;; `contents-end'. Make sure GRANULARITY allows the
4279 ;; recursion, or ELEMENT is a headline, in which case going
4280 ;; inside is mandatory, in order to get sub-level headings.
4281 ((and (memq type org-element-greater-elements)
4282 (or (memq granularity '(element object nil))
4283 (and (eq granularity 'greater-element)
4284 (eq type 'section))
4285 (eq type 'headline)))
4286 (org-element--parse-elements
4287 cbeg (org-element-property :contents-end element)
4288 ;; Possibly switch to a special mode.
4289 (org-element--next-mode type t)
4290 (and (memq type '(item plain-list))
4291 (org-element-property :structure element))
4292 granularity visible-only element))
4293 ;; ELEMENT has contents. Parse objects inside, if
4294 ;; GRANULARITY allows it.
4295 ((memq granularity '(object nil))
4296 (org-element--parse-objects
4297 cbeg (org-element-property :contents-end element) element
4298 (org-element-restriction type))))
4299 (push (org-element-put-property element :parent acc) elements)
4300 ;; Update mode.
4301 (setq mode (org-element--next-mode type nil))))
4302 ;; Return result.
4303 (apply #'org-element-set-contents acc (nreverse elements)))))
4305 (defun org-element--object-lex (restriction)
4306 "Return next object in current buffer or nil.
4307 RESTRICTION is a list of object types, as symbols, that should be
4308 looked after. This function assumes that the buffer is narrowed
4309 to an appropriate container (e.g., a paragraph)."
4310 (if (memq 'table-cell restriction) (org-element-table-cell-parser)
4311 (let* ((start (point))
4312 (limit
4313 ;; Object regexp sometimes needs to have a peek at
4314 ;; a character ahead. Therefore, when there is a hard
4315 ;; limit, make it one more than the true beginning of the
4316 ;; radio target.
4317 (save-excursion
4318 (cond ((not org-target-link-regexp) nil)
4319 ((not (memq 'link restriction)) nil)
4320 ((progn
4321 (unless (bolp) (forward-char -1))
4322 (not (re-search-forward org-target-link-regexp nil t)))
4323 nil)
4324 ;; Since we moved backward, we do not want to
4325 ;; match again an hypothetical 1-character long
4326 ;; radio link before us. Realizing that this can
4327 ;; only happen if such a radio link starts at
4328 ;; beginning of line, we prevent this here.
4329 ((and (= start (1+ (line-beginning-position)))
4330 (= start (match-end 1)))
4331 (and (re-search-forward org-target-link-regexp nil t)
4332 (1+ (match-beginning 1))))
4333 (t (1+ (match-beginning 1))))))
4334 found)
4335 (save-excursion
4336 (while (and (not found)
4337 (re-search-forward org-element--object-regexp limit 'move))
4338 (goto-char (match-beginning 0))
4339 (let ((result (match-string 0)))
4340 (setq found
4341 (cond
4342 ((string-prefix-p "call_" result t)
4343 (and (memq 'inline-babel-call restriction)
4344 (org-element-inline-babel-call-parser)))
4345 ((string-prefix-p "src_" result t)
4346 (and (memq 'inline-src-block restriction)
4347 (org-element-inline-src-block-parser)))
4349 (pcase (char-after)
4350 (?^ (and (memq 'superscript restriction)
4351 (org-element-superscript-parser)))
4352 (?_ (or (and (memq 'subscript restriction)
4353 (org-element-subscript-parser))
4354 (and (memq 'underline restriction)
4355 (org-element-underline-parser))))
4356 (?* (and (memq 'bold restriction)
4357 (org-element-bold-parser)))
4358 (?/ (and (memq 'italic restriction)
4359 (org-element-italic-parser)))
4360 (?~ (and (memq 'code restriction)
4361 (org-element-code-parser)))
4362 (?= (and (memq 'verbatim restriction)
4363 (org-element-verbatim-parser)))
4364 (?+ (and (memq 'strike-through restriction)
4365 (org-element-strike-through-parser)))
4366 (?@ (and (memq 'export-snippet restriction)
4367 (org-element-export-snippet-parser)))
4368 (?{ (and (memq 'macro restriction)
4369 (org-element-macro-parser)))
4370 (?$ (and (memq 'latex-fragment restriction)
4371 (org-element-latex-fragment-parser)))
4373 (if (eq (aref result 1) ?<)
4374 (or (and (memq 'radio-target restriction)
4375 (org-element-radio-target-parser))
4376 (and (memq 'target restriction)
4377 (org-element-target-parser)))
4378 (or (and (memq 'timestamp restriction)
4379 (org-element-timestamp-parser))
4380 (and (or (memq 'link restriction)
4381 (memq 'simple-link restriction))
4382 (org-element-link-parser)))))
4383 (?\\
4384 (if (eq (aref result 1) ?\\)
4385 (and (memq 'line-break restriction)
4386 (org-element-line-break-parser))
4387 (or (and (memq 'entity restriction)
4388 (org-element-entity-parser))
4389 (and (memq 'latex-fragment restriction)
4390 (org-element-latex-fragment-parser)))))
4391 (?\[
4392 (if (eq (aref result 1) ?\[)
4393 (and (memq 'link restriction)
4394 (org-element-link-parser))
4395 (or (and (memq 'footnote-reference restriction)
4396 (org-element-footnote-reference-parser))
4397 (and (memq 'timestamp restriction)
4398 (org-element-timestamp-parser))
4399 (and (memq 'statistics-cookie restriction)
4400 (org-element-statistics-cookie-parser)))))
4401 ;; This is probably a plain link.
4402 (_ (and (or (memq 'link restriction)
4403 (memq 'simple-link restriction))
4404 (org-element-link-parser)))))))
4405 (or (eobp) (forward-char))))
4406 (cond (found)
4407 (limit (forward-char -1)
4408 (org-element-link-parser)) ;radio link
4409 (t nil))))))
4411 (defun org-element--parse-objects (beg end acc restriction &optional parent)
4412 "Parse objects between BEG and END and return recursive structure.
4414 Objects are accumulated in ACC. RESTRICTION is a list of object
4415 successors which are allowed in the current object.
4417 ACC becomes the parent for all parsed objects. However, if ACC
4418 is nil (i.e., a secondary string is being parsed) and optional
4419 argument PARENT is non-nil, use it as the parent for all objects.
4420 Eventually, if both ACC and PARENT are nil, the common parent is
4421 the list of objects itself."
4422 (save-excursion
4423 (save-restriction
4424 (narrow-to-region beg end)
4425 (goto-char (point-min))
4426 (let (next-object contents)
4427 (while (and (not (eobp))
4428 (setq next-object (org-element--object-lex restriction)))
4429 ;; Text before any object.
4430 (let ((obj-beg (org-element-property :begin next-object)))
4431 (unless (= (point) obj-beg)
4432 (let ((text (buffer-substring-no-properties (point) obj-beg)))
4433 (push (if acc (org-element-put-property text :parent acc) text)
4434 contents))))
4435 ;; Object...
4436 (let ((obj-end (org-element-property :end next-object))
4437 (cont-beg (org-element-property :contents-begin next-object)))
4438 (when acc (org-element-put-property next-object :parent acc))
4439 (push (if cont-beg
4440 ;; Fill contents of NEXT-OBJECT if possible.
4441 (org-element--parse-objects
4442 cont-beg
4443 (org-element-property :contents-end next-object)
4444 next-object
4445 (org-element-restriction next-object))
4446 next-object)
4447 contents)
4448 (goto-char obj-end)))
4449 ;; Text after last object.
4450 (unless (eobp)
4451 (let ((text (buffer-substring-no-properties (point) end)))
4452 (push (if acc (org-element-put-property text :parent acc) text)
4453 contents)))
4454 ;; Result. Set appropriate parent.
4455 (if acc (apply #'org-element-set-contents acc (nreverse contents))
4456 (let* ((contents (nreverse contents))
4457 (parent (or parent contents)))
4458 (dolist (datum contents contents)
4459 (org-element-put-property datum :parent parent))))))))
4463 ;;; Towards A Bijective Process
4465 ;; The parse tree obtained with `org-element-parse-buffer' is really
4466 ;; a snapshot of the corresponding Org buffer. Therefore, it can be
4467 ;; interpreted and expanded into a string with canonical Org syntax.
4468 ;; Hence `org-element-interpret-data'.
4470 ;; The function relies internally on
4471 ;; `org-element--interpret-affiliated-keywords'.
4473 ;;;###autoload
4474 (defun org-element-interpret-data (data)
4475 "Interpret DATA as Org syntax.
4476 DATA is a parse tree, an element, an object or a secondary string
4477 to interpret. Return Org syntax as a string."
4478 (letrec ((fun
4479 (lambda (data parent)
4480 (let* ((type (org-element-type data))
4481 ;; Find interpreter for current object or
4482 ;; element. If it doesn't exist (e.g. this is
4483 ;; a pseudo object or element), return contents,
4484 ;; if any.
4485 (interpret
4486 (let ((fun (intern
4487 (format "org-element-%s-interpreter" type))))
4488 (if (fboundp fun) fun (lambda (_ contents) contents))))
4489 (results
4490 (cond
4491 ;; Secondary string.
4492 ((not type)
4493 (mapconcat (lambda (obj) (funcall fun obj parent))
4494 data
4495 ""))
4496 ;; Full Org document.
4497 ((eq type 'org-data)
4498 (mapconcat (lambda (obj) (funcall fun obj parent))
4499 (org-element-contents data)
4500 ""))
4501 ;; Plain text: return it.
4502 ((stringp data) data)
4503 ;; Element or object without contents.
4504 ((not (org-element-contents data))
4505 (funcall interpret data nil))
4506 ;; Element or object with contents.
4508 (funcall
4509 interpret
4510 data
4511 ;; Recursively interpret contents.
4512 (mapconcat
4513 (lambda (datum) (funcall fun datum data))
4514 (org-element-contents
4515 (if (not (memq type '(paragraph verse-block)))
4516 data
4517 ;; Fix indentation of elements containing
4518 ;; objects. We ignore `table-row'
4519 ;; elements as they are one line long
4520 ;; anyway.
4521 (org-element-normalize-contents
4522 data
4523 ;; When normalizing first paragraph of
4524 ;; an item or a footnote-definition,
4525 ;; ignore first line's indentation.
4526 (and (eq type 'paragraph)
4527 (memq (org-element-type parent)
4528 '(footnote-definition item))
4529 (eq data
4530 (car (org-element-contents parent)))))))
4531 ""))))))
4532 (if (memq type '(org-data plain-text nil)) results
4533 ;; Build white spaces. If no `:post-blank' property
4534 ;; is specified, assume its value is 0.
4535 (let ((blank (or (org-element-property :post-blank data) 0)))
4536 (if (eq (org-element-class data parent) 'object)
4537 (concat results (make-string blank ?\s))
4538 (concat (org-element--interpret-affiliated-keywords data)
4539 (org-element-normalize-string results)
4540 (make-string blank ?\n)))))))))
4541 (funcall fun data nil)))
4543 (defun org-element--interpret-affiliated-keywords (element)
4544 "Return ELEMENT's affiliated keywords as Org syntax.
4545 If there is no affiliated keyword, return the empty string."
4546 (let ((keyword-to-org
4547 (function
4548 (lambda (key value)
4549 (let (dual)
4550 (when (member key org-element-dual-keywords)
4551 (setq dual (cdr value) value (car value)))
4552 (concat "#+" key
4553 (and dual
4554 (format "[%s]" (org-element-interpret-data dual)))
4555 ": "
4556 (if (member key org-element-parsed-keywords)
4557 (org-element-interpret-data value)
4558 value)
4559 "\n"))))))
4560 (mapconcat
4561 (lambda (prop)
4562 (let ((value (org-element-property prop element))
4563 (keyword (upcase (substring (symbol-name prop) 1))))
4564 (when value
4565 (if (or (member keyword org-element-multiple-keywords)
4566 ;; All attribute keywords can have multiple lines.
4567 (string-match "^ATTR_" keyword))
4568 (mapconcat (lambda (line) (funcall keyword-to-org keyword line))
4569 (reverse value)
4571 (funcall keyword-to-org keyword value)))))
4572 ;; List all ELEMENT's properties matching an attribute line or an
4573 ;; affiliated keyword, but ignore translated keywords since they
4574 ;; cannot belong to the property list.
4575 (cl-loop for prop in (nth 1 element) by 'cddr
4576 when (let ((keyword (upcase (substring (symbol-name prop) 1))))
4577 (or (string-match "^ATTR_" keyword)
4578 (and
4579 (member keyword org-element-affiliated-keywords)
4580 (not (assoc keyword
4581 org-element-keyword-translation-alist)))))
4582 collect prop)
4583 "")))
4585 ;; Because interpretation of the parse tree must return the same
4586 ;; number of blank lines between elements and the same number of white
4587 ;; space after objects, some special care must be given to white
4588 ;; spaces.
4590 ;; The first function, `org-element-normalize-string', ensures any
4591 ;; string different from the empty string will end with a single
4592 ;; newline character.
4594 ;; The second function, `org-element-normalize-contents', removes
4595 ;; global indentation from the contents of the current element.
4597 (defun org-element-normalize-string (s)
4598 "Ensure string S ends with a single newline character.
4600 If S isn't a string return it unchanged. If S is the empty
4601 string, return it. Otherwise, return a new string with a single
4602 newline character at its end."
4603 (cond
4604 ((not (stringp s)) s)
4605 ((string= "" s) "")
4606 (t (and (string-match "\\(\n[ \t]*\\)*\\'" s)
4607 (replace-match "\n" nil nil s)))))
4609 (defun org-element-normalize-contents (element &optional ignore-first)
4610 "Normalize plain text in ELEMENT's contents.
4612 ELEMENT must only contain plain text and objects.
4614 If optional argument IGNORE-FIRST is non-nil, ignore first line's
4615 indentation to compute maximal common indentation.
4617 Return the normalized element that is element with global
4618 indentation removed from its contents."
4619 (letrec ((find-min-ind
4620 ;; Return minimal common indentation within BLOB. This is
4621 ;; done by walking recursively BLOB and updating MIN-IND
4622 ;; along the way. FIRST-FLAG is non-nil when the next
4623 ;; object is expected to be a string that doesn't start
4624 ;; with a newline character. It happens for strings at
4625 ;; the beginnings of the contents or right after a line
4626 ;; break.
4627 (lambda (blob first-flag min-ind)
4628 (dolist (datum (org-element-contents blob) min-ind)
4629 (when first-flag
4630 (setq first-flag nil)
4631 (cond
4632 ;; Objects cannot start with spaces: in this
4633 ;; case, indentation is 0.
4634 ((not (stringp datum)) (throw :zero 0))
4635 ((not (string-match
4636 "\\`\\([ \t]+\\)\\([^ \t\n]\\|\n\\|\\'\\)" datum))
4637 (throw :zero 0))
4638 ((equal (match-string 2 datum) "\n")
4639 (put-text-property
4640 (match-beginning 1) (match-end 1) 'org-ind 'empty datum))
4642 (let ((i (string-width (match-string 1 datum))))
4643 (put-text-property
4644 (match-beginning 1) (match-end 1) 'org-ind i datum)
4645 (setq min-ind (min i min-ind))))))
4646 (cond
4647 ((stringp datum)
4648 (let ((s 0))
4649 (while (string-match
4650 "\n\\([ \t]*\\)\\([^ \t\n]\\|\n\\|\\'\\)" datum s)
4651 (setq s (match-end 1))
4652 (cond
4653 ((equal (match-string 1 datum) "")
4654 (unless (member (match-string 2 datum) '("" "\n"))
4655 (throw :zero 0)))
4656 ((equal (match-string 2 datum) "\n")
4657 (put-text-property (match-beginning 1) (match-end 1)
4658 'org-ind 'empty datum))
4660 (let ((i (string-width (match-string 1 datum))))
4661 (put-text-property (match-beginning 1) (match-end 1)
4662 'org-ind i datum)
4663 (setq min-ind (min i min-ind))))))))
4664 ((eq (org-element-type datum) 'line-break)
4665 (setq first-flag t))
4666 ((memq (org-element-type datum) org-element-recursive-objects)
4667 (setq min-ind
4668 (funcall find-min-ind datum first-flag min-ind)))))))
4669 (min-ind
4670 (catch :zero
4671 (funcall find-min-ind
4672 element (not ignore-first) most-positive-fixnum))))
4673 (if (or (zerop min-ind) (= min-ind most-positive-fixnum)) element
4674 ;; Build ELEMENT back, replacing each string with the same
4675 ;; string minus common indentation.
4676 (letrec ((build
4677 (lambda (datum)
4678 ;; Return DATUM with all its strings indentation
4679 ;; shortened from MIN-IND white spaces.
4680 (setcdr
4681 (cdr datum)
4682 (mapcar
4683 (lambda (object)
4684 (cond
4685 ((stringp object)
4686 (with-temp-buffer
4687 (insert object)
4688 (let ((s (point-min)))
4689 (while (setq s (text-property-not-all
4690 s (point-max) 'org-ind nil))
4691 (goto-char s)
4692 (let ((i (get-text-property s 'org-ind)))
4693 (delete-region s (progn
4694 (skip-chars-forward " \t")
4695 (point)))
4696 (when (integerp i) (indent-to (- i min-ind))))))
4697 (buffer-string)))
4698 ((memq (org-element-type object)
4699 org-element-recursive-objects)
4700 (funcall build object))
4701 (t object)))
4702 (org-element-contents datum)))
4703 datum)))
4704 (funcall build element)))))
4708 ;;; Cache
4710 ;; Implement a caching mechanism for `org-element-at-point' and
4711 ;; `org-element-context', which see.
4713 ;; A single public function is provided: `org-element-cache-reset'.
4715 ;; Cache is enabled by default, but can be disabled globally with
4716 ;; `org-element-use-cache'. `org-element-cache-sync-idle-time',
4717 ;; org-element-cache-sync-duration' and `org-element-cache-sync-break'
4718 ;; can be tweaked to control caching behaviour.
4720 ;; Internally, parsed elements are stored in an AVL tree,
4721 ;; `org-element--cache'. This tree is updated lazily: whenever
4722 ;; a change happens to the buffer, a synchronization request is
4723 ;; registered in `org-element--cache-sync-requests' (see
4724 ;; `org-element--cache-submit-request'). During idle time, requests
4725 ;; are processed by `org-element--cache-sync'. Synchronization also
4726 ;; happens when an element is required from the cache. In this case,
4727 ;; the process stops as soon as the needed element is up-to-date.
4729 ;; A synchronization request can only apply on a synchronized part of
4730 ;; the cache. Therefore, the cache is updated at least to the
4731 ;; location where the new request applies. Thus, requests are ordered
4732 ;; from left to right and all elements starting before the first
4733 ;; request are correct. This property is used by functions like
4734 ;; `org-element--cache-find' to retrieve elements in the part of the
4735 ;; cache that can be trusted.
4737 ;; A request applies to every element, starting from its original
4738 ;; location (or key, see below). When a request is processed, it
4739 ;; moves forward and may collide the next one. In this case, both
4740 ;; requests are merged into a new one that starts from that element.
4741 ;; As a consequence, the whole synchronization complexity does not
4742 ;; depend on the number of pending requests, but on the number of
4743 ;; elements the very first request will be applied on.
4745 ;; Elements cannot be accessed through their beginning position, which
4746 ;; may or may not be up-to-date. Instead, each element in the tree is
4747 ;; associated to a key, obtained with `org-element--cache-key'. This
4748 ;; mechanism is robust enough to preserve total order among elements
4749 ;; even when the tree is only partially synchronized.
4751 ;; Objects contained in an element are stored in a hash table,
4752 ;; `org-element--cache-objects'.
4755 (defvar org-element-use-cache nil
4756 "Non-nil when Org parser should cache its results.
4758 WARNING: for the time being, using cache sometimes triggers
4759 freezes. Therefore, it is disabled by default. Activate it if
4760 you want to help debugging the issue.")
4762 (defvar org-element-cache-sync-idle-time 0.6
4763 "Length, in seconds, of idle time before syncing cache.")
4765 (defvar org-element-cache-sync-duration (seconds-to-time 0.04)
4766 "Maximum duration, as a time value, for a cache synchronization.
4767 If the synchronization is not over after this delay, the process
4768 pauses and resumes after `org-element-cache-sync-break'
4769 seconds.")
4771 (defvar org-element-cache-sync-break (seconds-to-time 0.3)
4772 "Duration, as a time value, of the pause between synchronizations.
4773 See `org-element-cache-sync-duration' for more information.")
4776 ;;;; Data Structure
4778 (defvar org-element--cache nil
4779 "AVL tree used to cache elements.
4780 Each node of the tree contains an element. Comparison is done
4781 with `org-element--cache-compare'. This cache is used in
4782 `org-element-at-point'.")
4784 (defvar org-element--cache-objects nil
4785 "Hash table used as to cache objects.
4786 Key is an element, as returned by `org-element-at-point', and
4787 value is an alist where each association is:
4789 (PARENT COMPLETEP . OBJECTS)
4791 where PARENT is an element or object, COMPLETEP is a boolean,
4792 non-nil when all direct children of parent are already cached and
4793 OBJECTS is a list of such children, as objects, from farthest to
4794 closest.
4796 In the following example, \\alpha, bold object and \\beta are
4797 contained within a paragraph
4799 \\alpha *\\beta*
4801 If the paragraph is completely parsed, OBJECTS-DATA will be
4803 ((PARAGRAPH t BOLD-OBJECT ENTITY-OBJECT)
4804 (BOLD-OBJECT t ENTITY-OBJECT))
4806 whereas in a partially parsed paragraph, it could be
4808 ((PARAGRAPH nil ENTITY-OBJECT))
4810 This cache is used in `org-element-context'.")
4812 (defvar org-element--cache-sync-requests nil
4813 "List of pending synchronization requests.
4815 A request is a vector with the following pattern:
4817 \[NEXT BEG END OFFSET PARENT PHASE]
4819 Processing a synchronization request consists of three phases:
4821 0. Delete modified elements,
4822 1. Fill missing area in cache,
4823 2. Shift positions and re-parent elements after the changes.
4825 During phase 0, NEXT is the key of the first element to be
4826 removed, BEG and END is buffer position delimiting the
4827 modifications. Elements starting between them (inclusive) are
4828 removed. So are elements whose parent is removed. PARENT, when
4829 non-nil, is the parent of the first element to be removed.
4831 During phase 1, NEXT is the key of the next known element in
4832 cache and BEG its beginning position. Parse buffer between that
4833 element and the one before it in order to determine the parent of
4834 the next element. Set PARENT to the element containing NEXT.
4836 During phase 2, NEXT is the key of the next element to shift in
4837 the parse tree. All elements starting from this one have their
4838 properties relatives to buffer positions shifted by integer
4839 OFFSET and, if they belong to element PARENT, are adopted by it.
4841 PHASE specifies the phase number, as an integer.")
4843 (defvar org-element--cache-sync-timer nil
4844 "Timer used for cache synchronization.")
4846 (defvar org-element--cache-sync-keys nil
4847 "Hash table used to store keys during synchronization.
4848 See `org-element--cache-key' for more information.")
4850 (defsubst org-element--cache-key (element)
4851 "Return a unique key for ELEMENT in cache tree.
4853 Keys are used to keep a total order among elements in the cache.
4854 Comparison is done with `org-element--cache-key-less-p'.
4856 When no synchronization is taking place, a key is simply the
4857 beginning position of the element, or that position plus one in
4858 the case of an first item (respectively row) in
4859 a list (respectively a table).
4861 During a synchronization, the key is the one the element had when
4862 the cache was synchronized for the last time. Elements added to
4863 cache during the synchronization get a new key generated with
4864 `org-element--cache-generate-key'.
4866 Such keys are stored in `org-element--cache-sync-keys'. The hash
4867 table is cleared once the synchronization is complete."
4868 (or (gethash element org-element--cache-sync-keys)
4869 (let* ((begin (org-element-property :begin element))
4870 ;; Increase beginning position of items (respectively
4871 ;; table rows) by one, so the first item can get
4872 ;; a different key from its parent list (respectively
4873 ;; table).
4874 (key (if (memq (org-element-type element) '(item table-row))
4875 (1+ begin)
4876 begin)))
4877 (if org-element--cache-sync-requests
4878 (puthash element key org-element--cache-sync-keys)
4879 key))))
4881 (defun org-element--cache-generate-key (lower upper)
4882 "Generate a key between LOWER and UPPER.
4884 LOWER and UPPER are integers or lists, possibly empty.
4886 If LOWER and UPPER are equals, return LOWER. Otherwise, return
4887 a unique key, as an integer or a list of integers, according to
4888 the following rules:
4890 - LOWER and UPPER are compared level-wise until values differ.
4892 - If, at a given level, LOWER and UPPER differ from more than
4893 2, the new key shares all the levels above with LOWER and
4894 gets a new level. Its value is the mean between LOWER and
4895 UPPER:
4897 (1 2) + (1 4) --> (1 3)
4899 - If LOWER has no value to compare with, it is assumed that its
4900 value is `most-negative-fixnum'. E.g.,
4902 (1 1) + (1 1 2)
4904 is equivalent to
4906 (1 1 m) + (1 1 2)
4908 where m is `most-negative-fixnum'. Likewise, if UPPER is
4909 short of levels, the current value is `most-positive-fixnum'.
4911 - If they differ from only one, the new key inherits from
4912 current LOWER level and fork it at the next level. E.g.,
4914 (2 1) + (3 3)
4916 is equivalent to
4918 (2 1) + (2 M)
4920 where M is `most-positive-fixnum'.
4922 - If the key is only one level long, it is returned as an
4923 integer:
4925 (1 2) + (3 2) --> 2
4927 When they are not equals, the function assumes that LOWER is
4928 lesser than UPPER, per `org-element--cache-key-less-p'."
4929 (if (equal lower upper) lower
4930 (let ((lower (if (integerp lower) (list lower) lower))
4931 (upper (if (integerp upper) (list upper) upper))
4932 skip-upper key)
4933 (catch 'exit
4934 (while t
4935 (let ((min (or (car lower) most-negative-fixnum))
4936 (max (cond (skip-upper most-positive-fixnum)
4937 ((car upper))
4938 (t most-positive-fixnum))))
4939 (if (< (1+ min) max)
4940 (let ((mean (+ (ash min -1) (ash max -1) (logand min max 1))))
4941 (throw 'exit (if key (nreverse (cons mean key)) mean)))
4942 (when (and (< min max) (not skip-upper))
4943 ;; When at a given level, LOWER and UPPER differ from
4944 ;; 1, ignore UPPER altogether. Instead create a key
4945 ;; between LOWER and the greatest key with the same
4946 ;; prefix as LOWER so far.
4947 (setq skip-upper t))
4948 (push min key)
4949 (setq lower (cdr lower) upper (cdr upper)))))))))
4951 (defsubst org-element--cache-key-less-p (a b)
4952 "Non-nil if key A is less than key B.
4953 A and B are either integers or lists of integers, as returned by
4954 `org-element--cache-key'."
4955 (if (integerp a) (if (integerp b) (< a b) (<= a (car b)))
4956 (if (integerp b) (< (car a) b)
4957 (catch 'exit
4958 (while (and a b)
4959 (cond ((car-less-than-car a b) (throw 'exit t))
4960 ((car-less-than-car b a) (throw 'exit nil))
4961 (t (setq a (cdr a) b (cdr b)))))
4962 ;; If A is empty, either keys are equal (B is also empty) and
4963 ;; we return nil, or A is lesser than B (B is longer) and we
4964 ;; return a non-nil value.
4966 ;; If A is not empty, B is necessarily empty and A is greater
4967 ;; than B (A is longer). Therefore, return nil.
4968 (and (null a) b)))))
4970 (defun org-element--cache-compare (a b)
4971 "Non-nil when element A is located before element B."
4972 (org-element--cache-key-less-p (org-element--cache-key a)
4973 (org-element--cache-key b)))
4975 (defsubst org-element--cache-root ()
4976 "Return root value in cache.
4977 This function assumes `org-element--cache' is a valid AVL tree."
4978 (avl-tree--node-left (avl-tree--dummyroot org-element--cache)))
4981 ;;;; Tools
4983 (defsubst org-element--cache-active-p ()
4984 "Non-nil when cache is active in current buffer."
4985 (and org-element-use-cache
4986 org-element--cache
4987 (derived-mode-p 'org-mode)))
4989 (defun org-element--cache-find (pos &optional side)
4990 "Find element in cache starting at POS or before.
4992 POS refers to a buffer position.
4994 When optional argument SIDE is non-nil, the function checks for
4995 elements starting at or past POS instead. If SIDE is `both', the
4996 function returns a cons cell where car is the first element
4997 starting at or before POS and cdr the first element starting
4998 after POS.
5000 The function can only find elements in the synchronized part of
5001 the cache."
5002 (let ((limit (and org-element--cache-sync-requests
5003 (aref (car org-element--cache-sync-requests) 0)))
5004 (node (org-element--cache-root))
5005 lower upper)
5006 (while node
5007 (let* ((element (avl-tree--node-data node))
5008 (begin (org-element-property :begin element)))
5009 (cond
5010 ((and limit
5011 (not (org-element--cache-key-less-p
5012 (org-element--cache-key element) limit)))
5013 (setq node (avl-tree--node-left node)))
5014 ((> begin pos)
5015 (setq upper element
5016 node (avl-tree--node-left node)))
5017 ((< begin pos)
5018 (setq lower element
5019 node (avl-tree--node-right node)))
5020 ;; We found an element in cache starting at POS. If `side'
5021 ;; is `both' we also want the next one in order to generate
5022 ;; a key in-between.
5024 ;; If the element is the first row or item in a table or
5025 ;; a plain list, we always return the table or the plain
5026 ;; list.
5028 ;; In any other case, we return the element found.
5029 ((eq side 'both)
5030 (setq lower element)
5031 (setq node (avl-tree--node-right node)))
5032 ((and (memq (org-element-type element) '(item table-row))
5033 (let ((parent (org-element-property :parent element)))
5034 (and (= (org-element-property :begin element)
5035 (org-element-property :contents-begin parent))
5036 (setq node nil
5037 lower parent
5038 upper parent)))))
5040 (setq node nil
5041 lower element
5042 upper element)))))
5043 (pcase side
5044 (`both (cons lower upper))
5045 (`nil lower)
5046 (_ upper))))
5048 (defun org-element--cache-put (element &optional data)
5049 "Store ELEMENT in current buffer's cache, if allowed.
5050 When optional argument DATA is non-nil, assume is it object data
5051 relative to ELEMENT and store it in the objects cache."
5052 (cond ((not (org-element--cache-active-p)) nil)
5053 ((not data)
5054 (when org-element--cache-sync-requests
5055 ;; During synchronization, first build an appropriate key
5056 ;; for the new element so `avl-tree-enter' can insert it at
5057 ;; the right spot in the cache.
5058 (let ((keys (org-element--cache-find
5059 (org-element-property :begin element) 'both)))
5060 (puthash element
5061 (org-element--cache-generate-key
5062 (and (car keys) (org-element--cache-key (car keys)))
5063 (cond ((cdr keys) (org-element--cache-key (cdr keys)))
5064 (org-element--cache-sync-requests
5065 (aref (car org-element--cache-sync-requests) 0))))
5066 org-element--cache-sync-keys)))
5067 (avl-tree-enter org-element--cache element))
5068 ;; Headlines are not stored in cache, so objects in titles are
5069 ;; not stored either.
5070 ((eq (org-element-type element) 'headline) nil)
5071 (t (puthash element data org-element--cache-objects))))
5073 (defsubst org-element--cache-remove (element)
5074 "Remove ELEMENT from cache.
5075 Assume ELEMENT belongs to cache and that a cache is active."
5076 (avl-tree-delete org-element--cache element)
5077 (remhash element org-element--cache-objects))
5080 ;;;; Synchronization
5082 (defsubst org-element--cache-set-timer (buffer)
5083 "Set idle timer for cache synchronization in BUFFER."
5084 (when org-element--cache-sync-timer
5085 (cancel-timer org-element--cache-sync-timer))
5086 (setq org-element--cache-sync-timer
5087 (run-with-idle-timer
5088 (let ((idle (current-idle-time)))
5089 (if idle (time-add idle org-element-cache-sync-break)
5090 org-element-cache-sync-idle-time))
5092 #'org-element--cache-sync
5093 buffer)))
5095 (defsubst org-element--cache-interrupt-p (time-limit)
5096 "Non-nil when synchronization process should be interrupted.
5097 TIME-LIMIT is a time value or nil."
5098 (and time-limit
5099 (or (input-pending-p)
5100 (time-less-p time-limit (current-time)))))
5102 (defsubst org-element--cache-shift-positions (element offset &optional props)
5103 "Shift ELEMENT properties relative to buffer positions by OFFSET.
5105 Properties containing buffer positions are `:begin', `:end',
5106 `:contents-begin', `:contents-end' and `:structure'. When
5107 optional argument PROPS is a list of keywords, only shift
5108 properties provided in that list.
5110 Properties are modified by side-effect."
5111 (let ((properties (nth 1 element)))
5112 ;; Shift `:structure' property for the first plain list only: it
5113 ;; is the only one that really matters and it prevents from
5114 ;; shifting it more than once.
5115 (when (and (or (not props) (memq :structure props))
5116 (eq (org-element-type element) 'plain-list)
5117 (not (eq (org-element-type (plist-get properties :parent))
5118 'item)))
5119 (dolist (item (plist-get properties :structure))
5120 (cl-incf (car item) offset)
5121 (cl-incf (nth 6 item) offset)))
5122 (dolist (key '(:begin :contents-begin :contents-end :end :post-affiliated))
5123 (let ((value (and (or (not props) (memq key props))
5124 (plist-get properties key))))
5125 (and value (plist-put properties key (+ offset value)))))))
5127 (defun org-element--cache-sync (buffer &optional threshold future-change)
5128 "Synchronize cache with recent modification in BUFFER.
5130 When optional argument THRESHOLD is non-nil, do the
5131 synchronization for all elements starting before or at threshold,
5132 then exit. Otherwise, synchronize cache for as long as
5133 `org-element-cache-sync-duration' or until Emacs leaves idle
5134 state.
5136 FUTURE-CHANGE, when non-nil, is a buffer position where changes
5137 not registered yet in the cache are going to happen. It is used
5138 in `org-element--cache-submit-request', where cache is partially
5139 updated before current modification are actually submitted."
5140 (when (buffer-live-p buffer)
5141 (with-current-buffer buffer
5142 (let ((inhibit-quit t) request next)
5143 (when org-element--cache-sync-timer
5144 (cancel-timer org-element--cache-sync-timer))
5145 (catch 'interrupt
5146 (while org-element--cache-sync-requests
5147 (setq request (car org-element--cache-sync-requests)
5148 next (nth 1 org-element--cache-sync-requests))
5149 (org-element--cache-process-request
5150 request
5151 (and next (aref next 0))
5152 threshold
5153 (and (not threshold)
5154 (time-add (current-time)
5155 org-element-cache-sync-duration))
5156 future-change)
5157 ;; Request processed. Merge current and next offsets and
5158 ;; transfer ending position.
5159 (when next
5160 (cl-incf (aref next 3) (aref request 3))
5161 (aset next 2 (aref request 2)))
5162 (setq org-element--cache-sync-requests
5163 (cdr org-element--cache-sync-requests))))
5164 ;; If more requests are awaiting, set idle timer accordingly.
5165 ;; Otherwise, reset keys.
5166 (if org-element--cache-sync-requests
5167 (org-element--cache-set-timer buffer)
5168 (clrhash org-element--cache-sync-keys))))))
5170 (defun org-element--cache-process-request
5171 (request next threshold time-limit future-change)
5172 "Process synchronization REQUEST for all entries before NEXT.
5174 REQUEST is a vector, built by `org-element--cache-submit-request'.
5176 NEXT is a cache key, as returned by `org-element--cache-key'.
5178 When non-nil, THRESHOLD is a buffer position. Synchronization
5179 stops as soon as a shifted element begins after it.
5181 When non-nil, TIME-LIMIT is a time value. Synchronization stops
5182 after this time or when Emacs exits idle state.
5184 When non-nil, FUTURE-CHANGE is a buffer position where changes
5185 not registered yet in the cache are going to happen. See
5186 `org-element--cache-submit-request' for more information.
5188 Throw `interrupt' if the process stops before completing the
5189 request."
5190 (catch 'quit
5191 (when (= (aref request 5) 0)
5192 ;; Phase 0.
5194 ;; Delete all elements starting after BEG, but not after buffer
5195 ;; position END or past element with key NEXT. Also delete
5196 ;; elements contained within a previously removed element
5197 ;; (stored in `last-container').
5199 ;; At each iteration, we start again at tree root since
5200 ;; a deletion modifies structure of the balanced tree.
5201 (catch 'end-phase
5202 (while t
5203 (when (org-element--cache-interrupt-p time-limit)
5204 (throw 'interrupt nil))
5205 ;; Find first element in cache with key BEG or after it.
5206 (let ((beg (aref request 0))
5207 (end (aref request 2))
5208 (node (org-element--cache-root))
5209 data data-key last-container)
5210 (while node
5211 (let* ((element (avl-tree--node-data node))
5212 (key (org-element--cache-key element)))
5213 (cond
5214 ((org-element--cache-key-less-p key beg)
5215 (setq node (avl-tree--node-right node)))
5216 ((org-element--cache-key-less-p beg key)
5217 (setq data element
5218 data-key key
5219 node (avl-tree--node-left node)))
5220 (t (setq data element
5221 data-key key
5222 node nil)))))
5223 (if data
5224 (let ((pos (org-element-property :begin data)))
5225 (if (if (or (not next)
5226 (org-element--cache-key-less-p data-key next))
5227 (<= pos end)
5228 (and last-container
5229 (let ((up data))
5230 (while (and up (not (eq up last-container)))
5231 (setq up (org-element-property :parent up)))
5232 up)))
5233 (progn (when (and (not last-container)
5234 (> (org-element-property :end data)
5235 end))
5236 (setq last-container data))
5237 (org-element--cache-remove data))
5238 (aset request 0 data-key)
5239 (aset request 1 pos)
5240 (aset request 5 1)
5241 (throw 'end-phase nil)))
5242 ;; No element starting after modifications left in
5243 ;; cache: further processing is futile.
5244 (throw 'quit t))))))
5245 (when (= (aref request 5) 1)
5246 ;; Phase 1.
5248 ;; Phase 0 left a hole in the cache. Some elements after it
5249 ;; could have parents within. For example, in the following
5250 ;; buffer:
5252 ;; - item
5255 ;; Paragraph1
5257 ;; Paragraph2
5259 ;; if we remove a blank line between "item" and "Paragraph1",
5260 ;; everything down to "Paragraph2" is removed from cache. But
5261 ;; the paragraph now belongs to the list, and its `:parent'
5262 ;; property no longer is accurate.
5264 ;; Therefore we need to parse again elements in the hole, or at
5265 ;; least in its last section, so that we can re-parent
5266 ;; subsequent elements, during phase 2.
5268 ;; Note that we only need to get the parent from the first
5269 ;; element in cache after the hole.
5271 ;; When next key is lesser or equal to the current one, delegate
5272 ;; phase 1 processing to next request in order to preserve key
5273 ;; order among requests.
5274 (let ((key (aref request 0)))
5275 (when (and next (not (org-element--cache-key-less-p key next)))
5276 (let ((next-request (nth 1 org-element--cache-sync-requests)))
5277 (aset next-request 0 key)
5278 (aset next-request 1 (aref request 1))
5279 (aset next-request 5 1))
5280 (throw 'quit t)))
5281 ;; Next element will start at its beginning position plus
5282 ;; offset, since it hasn't been shifted yet. Therefore, LIMIT
5283 ;; contains the real beginning position of the first element to
5284 ;; shift and re-parent.
5285 (let ((limit (+ (aref request 1) (aref request 3))))
5286 (cond ((and threshold (> limit threshold)) (throw 'interrupt nil))
5287 ((and future-change (>= limit future-change))
5288 ;; Changes are going to happen around this element and
5289 ;; they will trigger another phase 1 request. Skip the
5290 ;; current one.
5291 (aset request 5 2))
5293 (let ((parent (org-element--parse-to limit t time-limit)))
5294 (aset request 4 parent)
5295 (aset request 5 2))))))
5296 ;; Phase 2.
5298 ;; Shift all elements starting from key START, but before NEXT, by
5299 ;; OFFSET, and re-parent them when appropriate.
5301 ;; Elements are modified by side-effect so the tree structure
5302 ;; remains intact.
5304 ;; Once THRESHOLD, if any, is reached, or once there is an input
5305 ;; pending, exit. Before leaving, the current synchronization
5306 ;; request is updated.
5307 (let ((start (aref request 0))
5308 (offset (aref request 3))
5309 (parent (aref request 4))
5310 (node (org-element--cache-root))
5311 (stack (list nil))
5312 (leftp t)
5313 exit-flag)
5314 ;; No re-parenting nor shifting planned: request is over.
5315 (when (and (not parent) (zerop offset)) (throw 'quit t))
5316 (while node
5317 (let* ((data (avl-tree--node-data node))
5318 (key (org-element--cache-key data)))
5319 (if (and leftp (avl-tree--node-left node)
5320 (not (org-element--cache-key-less-p key start)))
5321 (progn (push node stack)
5322 (setq node (avl-tree--node-left node)))
5323 (unless (org-element--cache-key-less-p key start)
5324 ;; We reached NEXT. Request is complete.
5325 (when (equal key next) (throw 'quit t))
5326 ;; Handle interruption request. Update current request.
5327 (when (or exit-flag (org-element--cache-interrupt-p time-limit))
5328 (aset request 0 key)
5329 (aset request 4 parent)
5330 (throw 'interrupt nil))
5331 ;; Shift element.
5332 (unless (zerop offset)
5333 (org-element--cache-shift-positions data offset)
5334 ;; Shift associated objects data, if any.
5335 (dolist (object-data (gethash data org-element--cache-objects))
5336 (dolist (object (cddr object-data))
5337 (org-element--cache-shift-positions object offset))))
5338 (let ((begin (org-element-property :begin data)))
5339 ;; Update PARENT and re-parent DATA, only when
5340 ;; necessary. Propagate new structures for lists.
5341 (while (and parent
5342 (<= (org-element-property :end parent) begin))
5343 (setq parent (org-element-property :parent parent)))
5344 (cond ((and (not parent) (zerop offset)) (throw 'quit nil))
5345 ((and parent
5346 (let ((p (org-element-property :parent data)))
5347 (or (not p)
5348 (< (org-element-property :begin p)
5349 (org-element-property :begin parent)))))
5350 (org-element-put-property data :parent parent)
5351 (let ((s (org-element-property :structure parent)))
5352 (when (and s (org-element-property :structure data))
5353 (org-element-put-property data :structure s)))))
5354 ;; Cache is up-to-date past THRESHOLD. Request
5355 ;; interruption.
5356 (when (and threshold (> begin threshold)) (setq exit-flag t))))
5357 (setq node (if (setq leftp (avl-tree--node-right node))
5358 (avl-tree--node-right node)
5359 (pop stack))))))
5360 ;; We reached end of tree: synchronization complete.
5361 t)))
5363 (defun org-element--parse-to (pos &optional syncp time-limit)
5364 "Parse elements in current section, down to POS.
5366 Start parsing from the closest between the last known element in
5367 cache or headline above. Return the smallest element containing
5368 POS.
5370 When optional argument SYNCP is non-nil, return the parent of the
5371 element containing POS instead. In that case, it is also
5372 possible to provide TIME-LIMIT, which is a time value specifying
5373 when the parsing should stop. The function throws `interrupt' if
5374 the process stopped before finding the expected result."
5375 (catch 'exit
5376 (org-with-wide-buffer
5377 (goto-char pos)
5378 (let* ((cached (and (org-element--cache-active-p)
5379 (org-element--cache-find pos nil)))
5380 (begin (org-element-property :begin cached))
5381 element next mode)
5382 (cond
5383 ;; Nothing in cache before point: start parsing from first
5384 ;; element following headline above, or first element in
5385 ;; buffer.
5386 ((not cached)
5387 (when (org-with-limited-levels (outline-previous-heading))
5388 (setq mode 'planning)
5389 (forward-line))
5390 (skip-chars-forward " \r\t\n")
5391 (beginning-of-line))
5392 ;; Cache returned exact match: return it.
5393 ((= pos begin)
5394 (throw 'exit (if syncp (org-element-property :parent cached) cached)))
5395 ;; There's a headline between cached value and POS: cached
5396 ;; value is invalid. Start parsing from first element
5397 ;; following the headline.
5398 ((re-search-backward
5399 (org-with-limited-levels org-outline-regexp-bol) begin t)
5400 (forward-line)
5401 (skip-chars-forward " \r\t\n")
5402 (beginning-of-line)
5403 (setq mode 'planning))
5404 ;; Check if CACHED or any of its ancestors contain point.
5406 ;; If there is such an element, we inspect it in order to know
5407 ;; if we return it or if we need to parse its contents.
5408 ;; Otherwise, we just start parsing from current location,
5409 ;; which is right after the top-most element containing
5410 ;; CACHED.
5412 ;; As a special case, if POS is at the end of the buffer, we
5413 ;; want to return the innermost element ending there.
5415 ;; Also, if we find an ancestor and discover that we need to
5416 ;; parse its contents, make sure we don't start from
5417 ;; `:contents-begin', as we would otherwise go past CACHED
5418 ;; again. Instead, in that situation, we will resume parsing
5419 ;; from NEXT, which is located after CACHED or its higher
5420 ;; ancestor not containing point.
5422 (let ((up cached)
5423 (pos (if (= (point-max) pos) (1- pos) pos)))
5424 (goto-char (or (org-element-property :contents-begin cached) begin))
5425 (while (let ((end (org-element-property :end up)))
5426 (and (<= end pos)
5427 (goto-char end)
5428 (setq up (org-element-property :parent up)))))
5429 (cond ((not up))
5430 ((eobp) (setq element up))
5431 (t (setq element up next (point)))))))
5432 ;; Parse successively each element until we reach POS.
5433 (let ((end (or (org-element-property :end element)
5434 (save-excursion
5435 (org-with-limited-levels (outline-next-heading))
5436 (point))))
5437 (parent element))
5438 (while t
5439 (when syncp
5440 (cond ((= (point) pos) (throw 'exit parent))
5441 ((org-element--cache-interrupt-p time-limit)
5442 (throw 'interrupt nil))))
5443 (unless element
5444 (setq element (org-element--current-element
5445 end 'element mode
5446 (org-element-property :structure parent)))
5447 (org-element-put-property element :parent parent)
5448 (org-element--cache-put element))
5449 (let ((elem-end (org-element-property :end element))
5450 (type (org-element-type element)))
5451 (cond
5452 ;; Skip any element ending before point. Also skip
5453 ;; element ending at point (unless it is also the end of
5454 ;; buffer) since we're sure that another element begins
5455 ;; after it.
5456 ((and (<= elem-end pos) (/= (point-max) elem-end))
5457 (goto-char elem-end)
5458 (setq mode (org-element--next-mode type nil)))
5459 ;; A non-greater element contains point: return it.
5460 ((not (memq type org-element-greater-elements))
5461 (throw 'exit element))
5462 ;; Otherwise, we have to decide if ELEMENT really
5463 ;; contains POS. In that case we start parsing from
5464 ;; contents' beginning.
5466 ;; If POS is at contents' beginning but it is also at
5467 ;; the beginning of the first item in a list or a table.
5468 ;; In that case, we need to create an anchor for that
5469 ;; list or table, so return it.
5471 ;; Also, if POS is at the end of the buffer, no element
5472 ;; can start after it, but more than one may end there.
5473 ;; Arbitrarily, we choose to return the innermost of
5474 ;; such elements.
5475 ((let ((cbeg (org-element-property :contents-begin element))
5476 (cend (org-element-property :contents-end element)))
5477 (when (or syncp
5478 (and cbeg cend
5479 (or (< cbeg pos)
5480 (and (= cbeg pos)
5481 (not (memq type '(plain-list table)))))
5482 (or (> cend pos)
5483 (and (= cend pos) (= (point-max) pos)))))
5484 (goto-char (or next cbeg))
5485 (setq next nil
5486 mode (org-element--next-mode type t)
5487 parent element
5488 end cend))))
5489 ;; Otherwise, return ELEMENT as it is the smallest
5490 ;; element containing POS.
5491 (t (throw 'exit element))))
5492 (setq element nil)))))))
5495 ;;;; Staging Buffer Changes
5497 (defconst org-element--cache-sensitive-re
5498 (concat
5499 org-outline-regexp-bol "\\|"
5500 "\\\\end{[A-Za-z0-9*]+}[ \t]*$" "\\|"
5501 "^[ \t]*\\(?:"
5502 "#\\+\\(?:BEGIN[:_]\\|END\\(?:_\\|:?[ \t]*$\\)\\)" "\\|"
5503 "\\\\begin{[A-Za-z0-9*]+}" "\\|"
5504 ":\\(?:\\w\\|[-_]\\)+:[ \t]*$"
5505 "\\)")
5506 "Regexp matching a sensitive line, structure wise.
5507 A sensitive line is a headline, inlinetask, block, drawer, or
5508 latex-environment boundary. When such a line is modified,
5509 structure changes in the document may propagate in the whole
5510 section, possibly making cache invalid.")
5512 (defvar org-element--cache-change-warning nil
5513 "Non-nil when a sensitive line is about to be changed.
5514 It is a symbol among nil, t and `headline'.")
5516 (defun org-element--cache-before-change (beg end)
5517 "Request extension of area going to be modified if needed.
5518 BEG and END are the beginning and end of the range of changed
5519 text. See `before-change-functions' for more information."
5520 (when (org-element--cache-active-p)
5521 (org-with-wide-buffer
5522 (goto-char beg)
5523 (beginning-of-line)
5524 (let ((bottom (save-excursion (goto-char end) (line-end-position))))
5525 (setq org-element--cache-change-warning
5526 (save-match-data
5527 (if (and (org-with-limited-levels (org-at-heading-p))
5528 (= (line-end-position) bottom))
5529 'headline
5530 (let ((case-fold-search t))
5531 (re-search-forward
5532 org-element--cache-sensitive-re bottom t)))))))))
5534 (defun org-element--cache-after-change (beg end pre)
5535 "Update buffer modifications for current buffer.
5536 BEG and END are the beginning and end of the range of changed
5537 text, and the length in bytes of the pre-change text replaced by
5538 that range. See `after-change-functions' for more information."
5539 (when (org-element--cache-active-p)
5540 (org-with-wide-buffer
5541 (goto-char beg)
5542 (beginning-of-line)
5543 (save-match-data
5544 (let ((top (point))
5545 (bottom (save-excursion (goto-char end) (line-end-position))))
5546 ;; Determine if modified area needs to be extended, according
5547 ;; to both previous and current state. We make a special
5548 ;; case for headline editing: if a headline is modified but
5549 ;; not removed, do not extend.
5550 (when (pcase org-element--cache-change-warning
5551 (`t t)
5552 (`headline
5553 (not (and (org-with-limited-levels (org-at-heading-p))
5554 (= (line-end-position) bottom))))
5556 (let ((case-fold-search t))
5557 (re-search-forward
5558 org-element--cache-sensitive-re bottom t))))
5559 ;; Effectively extend modified area.
5560 (org-with-limited-levels
5561 (setq top (progn (goto-char top)
5562 (when (outline-previous-heading) (forward-line))
5563 (point)))
5564 (setq bottom (progn (goto-char bottom)
5565 (if (outline-next-heading) (1- (point))
5566 (point))))))
5567 ;; Store synchronization request.
5568 (let ((offset (- end beg pre)))
5569 (org-element--cache-submit-request top (- bottom offset) offset)))))
5570 ;; Activate a timer to process the request during idle time.
5571 (org-element--cache-set-timer (current-buffer))))
5573 (defun org-element--cache-for-removal (beg end offset)
5574 "Return first element to remove from cache.
5576 BEG and END are buffer positions delimiting buffer modifications.
5577 OFFSET is the size of the changes.
5579 Returned element is usually the first element in cache containing
5580 any position between BEG and END. As an exception, greater
5581 elements around the changes that are robust to contents
5582 modifications are preserved and updated according to the
5583 changes."
5584 (let* ((elements (org-element--cache-find (1- beg) 'both))
5585 (before (car elements))
5586 (after (cdr elements)))
5587 (if (not before) after
5588 (let ((up before)
5589 (robust-flag t))
5590 (while up
5591 (if (let ((type (org-element-type up)))
5592 (and (or (memq type '(center-block dynamic-block quote-block
5593 special-block))
5594 ;; Drawers named "PROPERTIES" are probably
5595 ;; a properties drawer being edited. Force
5596 ;; parsing to check if editing is over.
5597 (and (eq type 'drawer)
5598 (not (string=
5599 (org-element-property :drawer-name up)
5600 "PROPERTIES"))))
5601 (let ((cbeg (org-element-property :contents-begin up)))
5602 (and cbeg
5603 (<= cbeg beg)
5604 (> (org-element-property :contents-end up) end)))))
5605 ;; UP is a robust greater element containing changes.
5606 ;; We only need to extend its ending boundaries.
5607 (org-element--cache-shift-positions
5608 up offset '(:contents-end :end))
5609 (setq before up)
5610 (when robust-flag (setq robust-flag nil)))
5611 (setq up (org-element-property :parent up)))
5612 ;; We're at top level element containing ELEMENT: if it's
5613 ;; altered by buffer modifications, it is first element in
5614 ;; cache to be removed. Otherwise, that first element is the
5615 ;; following one.
5617 ;; As a special case, do not remove BEFORE if it is a robust
5618 ;; container for current changes.
5619 (if (or (< (org-element-property :end before) beg) robust-flag) after
5620 before)))))
5622 (defun org-element--cache-submit-request (beg end offset)
5623 "Submit a new cache synchronization request for current buffer.
5624 BEG and END are buffer positions delimiting the minimal area
5625 where cache data should be removed. OFFSET is the size of the
5626 change, as an integer."
5627 (let ((next (car org-element--cache-sync-requests))
5628 delete-to delete-from)
5629 (if (and next
5630 (zerop (aref next 5))
5631 (> (setq delete-to (+ (aref next 2) (aref next 3))) end)
5632 (<= (setq delete-from (aref next 1)) end))
5633 ;; Current changes can be merged with first sync request: we
5634 ;; can save a partial cache synchronization.
5635 (progn
5636 (cl-incf (aref next 3) offset)
5637 ;; If last change happened within area to be removed, extend
5638 ;; boundaries of robust parents, if any. Otherwise, find
5639 ;; first element to remove and update request accordingly.
5640 (if (> beg delete-from)
5641 (let ((up (aref next 4)))
5642 (while up
5643 (org-element--cache-shift-positions
5644 up offset '(:contents-end :end))
5645 (setq up (org-element-property :parent up))))
5646 (let ((first (org-element--cache-for-removal beg delete-to offset)))
5647 (when first
5648 (aset next 0 (org-element--cache-key first))
5649 (aset next 1 (org-element-property :begin first))
5650 (aset next 4 (org-element-property :parent first))))))
5651 ;; Ensure cache is correct up to END. Also make sure that NEXT,
5652 ;; if any, is no longer a 0-phase request, thus ensuring that
5653 ;; phases are properly ordered. We need to provide OFFSET as
5654 ;; optional parameter since current modifications are not known
5655 ;; yet to the otherwise correct part of the cache (i.e, before
5656 ;; the first request).
5657 (when next (org-element--cache-sync (current-buffer) end beg))
5658 (let ((first (org-element--cache-for-removal beg end offset)))
5659 (if first
5660 (push (let ((beg (org-element-property :begin first))
5661 (key (org-element--cache-key first)))
5662 (cond
5663 ;; When changes happen before the first known
5664 ;; element, re-parent and shift the rest of the
5665 ;; cache.
5666 ((> beg end) (vector key beg nil offset nil 1))
5667 ;; Otherwise, we find the first non robust
5668 ;; element containing END. All elements between
5669 ;; FIRST and this one are to be removed.
5670 ((let ((first-end (org-element-property :end first)))
5671 (and (> first-end end)
5672 (vector key beg first-end offset first 0))))
5674 (let* ((element (org-element--cache-find end))
5675 (end (org-element-property :end element))
5676 (up element))
5677 (while (and (setq up (org-element-property :parent up))
5678 (>= (org-element-property :begin up) beg))
5679 (setq end (org-element-property :end up)
5680 element up))
5681 (vector key beg end offset element 0)))))
5682 org-element--cache-sync-requests)
5683 ;; No element to remove. No need to re-parent either.
5684 ;; Simply shift additional elements, if any, by OFFSET.
5685 (when org-element--cache-sync-requests
5686 (cl-incf (aref (car org-element--cache-sync-requests) 3)
5687 offset)))))))
5690 ;;;; Public Functions
5692 ;;;###autoload
5693 (defun org-element-cache-reset (&optional all)
5694 "Reset cache in current buffer.
5695 When optional argument ALL is non-nil, reset cache in all Org
5696 buffers."
5697 (interactive "P")
5698 (dolist (buffer (if all (buffer-list) (list (current-buffer))))
5699 (with-current-buffer buffer
5700 (when (and org-element-use-cache (derived-mode-p 'org-mode))
5701 (setq-local org-element--cache
5702 (avl-tree-create #'org-element--cache-compare))
5703 (setq-local org-element--cache-objects (make-hash-table :test #'eq))
5704 (setq-local org-element--cache-sync-keys
5705 (make-hash-table :weakness 'key :test #'eq))
5706 (setq-local org-element--cache-change-warning nil)
5707 (setq-local org-element--cache-sync-requests nil)
5708 (setq-local org-element--cache-sync-timer nil)
5709 (add-hook 'before-change-functions
5710 #'org-element--cache-before-change nil t)
5711 (add-hook 'after-change-functions
5712 #'org-element--cache-after-change nil t)))))
5714 ;;;###autoload
5715 (defun org-element-cache-refresh (pos)
5716 "Refresh cache at position POS."
5717 (when (org-element--cache-active-p)
5718 (org-element--cache-sync (current-buffer) pos)
5719 (org-element--cache-submit-request pos pos 0)
5720 (org-element--cache-set-timer (current-buffer))))
5724 ;;; The Toolbox
5726 ;; The first move is to implement a way to obtain the smallest element
5727 ;; containing point. This is the job of `org-element-at-point'. It
5728 ;; basically jumps back to the beginning of section containing point
5729 ;; and proceed, one element after the other, with
5730 ;; `org-element--current-element' until the container is found. Note:
5731 ;; When using `org-element-at-point', secondary values are never
5732 ;; parsed since the function focuses on elements, not on objects.
5734 ;; At a deeper level, `org-element-context' lists all elements and
5735 ;; objects containing point.
5737 ;; `org-element-nested-p' and `org-element-swap-A-B' may be used
5738 ;; internally by navigation and manipulation tools.
5741 ;;;###autoload
5742 (defun org-element-at-point ()
5743 "Determine closest element around point.
5745 Return value is a list like (TYPE PROPS) where TYPE is the type
5746 of the element and PROPS a plist of properties associated to the
5747 element.
5749 Possible types are defined in `org-element-all-elements'.
5750 Properties depend on element or object type, but always include
5751 `:begin', `:end', `:parent' and `:post-blank' properties.
5753 As a special case, if point is at the very beginning of the first
5754 item in a list or sub-list, returned element will be that list
5755 instead of the item. Likewise, if point is at the beginning of
5756 the first row of a table, returned element will be the table
5757 instead of the first row.
5759 When point is at the end of the buffer, return the innermost
5760 element ending there."
5761 (org-with-wide-buffer
5762 (let ((origin (point)))
5763 (end-of-line)
5764 (skip-chars-backward " \r\t\n")
5765 (cond
5766 ;; Within blank lines at the beginning of buffer, return nil.
5767 ((bobp) nil)
5768 ;; Within blank lines right after a headline, return that
5769 ;; headline.
5770 ((org-with-limited-levels (org-at-heading-p))
5771 (beginning-of-line)
5772 (org-element-headline-parser (point-max) t))
5773 ;; Otherwise parse until we find element containing ORIGIN.
5775 (when (org-element--cache-active-p)
5776 (if (not org-element--cache) (org-element-cache-reset)
5777 (org-element--cache-sync (current-buffer) origin)))
5778 (org-element--parse-to origin))))))
5780 ;;;###autoload
5781 (defun org-element-context (&optional element)
5782 "Return smallest element or object around point.
5784 Return value is a list like (TYPE PROPS) where TYPE is the type
5785 of the element or object and PROPS a plist of properties
5786 associated to it.
5788 Possible types are defined in `org-element-all-elements' and
5789 `org-element-all-objects'. Properties depend on element or
5790 object type, but always include `:begin', `:end', `:parent' and
5791 `:post-blank'.
5793 As a special case, if point is right after an object and not at
5794 the beginning of any other object, return that object.
5796 Optional argument ELEMENT, when non-nil, is the closest element
5797 containing point, as returned by `org-element-at-point'.
5798 Providing it allows for quicker computation."
5799 (catch 'objects-forbidden
5800 (org-with-wide-buffer
5801 (let* ((pos (point))
5802 (element (or element (org-element-at-point)))
5803 (type (org-element-type element))
5804 (post (org-element-property :post-affiliated element)))
5805 ;; If point is inside an element containing objects or
5806 ;; a secondary string, narrow buffer to the container and
5807 ;; proceed with parsing. Otherwise, return ELEMENT.
5808 (cond
5809 ;; At a parsed affiliated keyword, check if we're inside main
5810 ;; or dual value.
5811 ((and post (< pos post))
5812 (beginning-of-line)
5813 (let ((case-fold-search t)) (looking-at org-element--affiliated-re))
5814 (cond
5815 ((not (member-ignore-case (match-string 1)
5816 org-element-parsed-keywords))
5817 (throw 'objects-forbidden element))
5818 ((< (match-end 0) pos)
5819 (narrow-to-region (match-end 0) (line-end-position)))
5820 ((and (match-beginning 2)
5821 (>= pos (match-beginning 2))
5822 (< pos (match-end 2)))
5823 (narrow-to-region (match-beginning 2) (match-end 2)))
5824 (t (throw 'objects-forbidden element)))
5825 ;; Also change type to retrieve correct restrictions.
5826 (setq type 'keyword))
5827 ;; At an item, objects can only be located within tag, if any.
5828 ((eq type 'item)
5829 (let ((tag (org-element-property :tag element)))
5830 (if (or (not tag) (/= (line-beginning-position) post))
5831 (throw 'objects-forbidden element)
5832 (beginning-of-line)
5833 (search-forward tag (line-end-position))
5834 (goto-char (match-beginning 0))
5835 (if (and (>= pos (point)) (< pos (match-end 0)))
5836 (narrow-to-region (point) (match-end 0))
5837 (throw 'objects-forbidden element)))))
5838 ;; At an headline or inlinetask, objects are in title.
5839 ((memq type '(headline inlinetask))
5840 (let ((case-fold-search nil))
5841 (goto-char (org-element-property :begin element))
5842 (looking-at org-complex-heading-regexp)
5843 (let ((end (match-end 4)))
5844 (if (not end) (throw 'objects-forbidden element)
5845 (goto-char (match-beginning 4))
5846 (when (looking-at org-comment-string)
5847 (goto-char (match-end 0)))
5848 (if (>= (point) end) (throw 'objects-forbidden element)
5849 (narrow-to-region (point) end))))))
5850 ;; At a paragraph, a table-row or a verse block, objects are
5851 ;; located within their contents.
5852 ((memq type '(paragraph table-row verse-block))
5853 (let ((cbeg (org-element-property :contents-begin element))
5854 (cend (org-element-property :contents-end element)))
5855 ;; CBEG is nil for table rules.
5856 (if (and cbeg cend (>= pos cbeg)
5857 (or (< pos cend) (and (= pos cend) (eobp))))
5858 (narrow-to-region cbeg cend)
5859 (throw 'objects-forbidden element))))
5860 ;; At a planning line, if point is at a timestamp, return it,
5861 ;; otherwise, return element.
5862 ((eq type 'planning)
5863 (dolist (p '(:closed :deadline :scheduled))
5864 (let ((timestamp (org-element-property p element)))
5865 (when (and timestamp
5866 (<= (org-element-property :begin timestamp) pos)
5867 (> (org-element-property :end timestamp) pos))
5868 (throw 'objects-forbidden timestamp))))
5869 ;; All other locations cannot contain objects: bail out.
5870 (throw 'objects-forbidden element))
5871 (t (throw 'objects-forbidden element)))
5872 (goto-char (point-min))
5873 (let ((restriction (org-element-restriction type))
5874 (parent element)
5875 (cache (cond ((not (org-element--cache-active-p)) nil)
5876 (org-element--cache-objects
5877 (gethash element org-element--cache-objects))
5878 (t (org-element-cache-reset) nil)))
5879 next object-data last)
5880 (prog1
5881 (catch 'exit
5882 (while t
5883 ;; When entering PARENT for the first time, get list
5884 ;; of objects within known so far. Store it in
5885 ;; OBJECT-DATA.
5886 (unless next
5887 (let ((data (assq parent cache)))
5888 (if data (setq object-data data)
5889 (push (setq object-data (list parent nil)) cache))))
5890 ;; Find NEXT object for analysis.
5891 (catch 'found
5892 ;; If NEXT is non-nil, we already exhausted the
5893 ;; cache so we can parse buffer to find the object
5894 ;; after it.
5895 (if next (setq next (org-element--object-lex restriction))
5896 ;; Otherwise, check if cache can help us.
5897 (let ((objects (cddr object-data))
5898 (completep (nth 1 object-data)))
5899 (cond
5900 ((and (not objects) completep) (throw 'exit parent))
5901 ((not objects)
5902 (setq next (org-element--object-lex restriction)))
5904 (let ((cache-limit
5905 (org-element-property :end (car objects))))
5906 (if (>= cache-limit pos)
5907 ;; Cache contains the information needed.
5908 (dolist (object objects (throw 'exit parent))
5909 (when (<= (org-element-property :begin object)
5910 pos)
5911 (if (>= (org-element-property :end object)
5912 pos)
5913 (throw 'found (setq next object))
5914 (throw 'exit parent))))
5915 (goto-char cache-limit)
5916 (setq next
5917 (org-element--object-lex restriction))))))))
5918 ;; If we have a new object to analyze, store it in
5919 ;; cache. Otherwise record that there is nothing
5920 ;; more to parse in this element at this depth.
5921 (if next
5922 (progn (org-element-put-property next :parent parent)
5923 (push next (cddr object-data)))
5924 (setcar (cdr object-data) t)))
5925 ;; Process NEXT, if any, in order to know if we need
5926 ;; to skip it, return it or move into it.
5927 (if (or (not next) (> (org-element-property :begin next) pos))
5928 (throw 'exit (or last parent))
5929 (let ((end (org-element-property :end next))
5930 (cbeg (org-element-property :contents-begin next))
5931 (cend (org-element-property :contents-end next)))
5932 (cond
5933 ;; Skip objects ending before point. Also skip
5934 ;; objects ending at point unless it is also the
5935 ;; end of buffer, since we want to return the
5936 ;; innermost object.
5937 ((and (<= end pos) (/= (point-max) end))
5938 (goto-char end)
5939 ;; For convenience, when object ends at POS,
5940 ;; without any space, store it in LAST, as we
5941 ;; will return it if no object starts here.
5942 (when (and (= end pos)
5943 (not (memq (char-before) '(?\s ?\t))))
5944 (setq last next)))
5945 ;; If POS is within a container object, move
5946 ;; into that object.
5947 ((and cbeg cend
5948 (>= pos cbeg)
5949 (or (< pos cend)
5950 ;; At contents' end, if there is no
5951 ;; space before point, also move into
5952 ;; object, for consistency with
5953 ;; convenience feature above.
5954 (and (= pos cend)
5955 (or (= (point-max) pos)
5956 (not (memq (char-before pos)
5957 '(?\s ?\t)))))))
5958 (goto-char cbeg)
5959 (narrow-to-region (point) cend)
5960 (setq parent next
5961 restriction (org-element-restriction next)
5962 next nil
5963 object-data nil))
5964 ;; Otherwise, return NEXT.
5965 (t (throw 'exit next)))))))
5966 ;; Store results in cache, if applicable.
5967 (org-element--cache-put element cache)))))))
5969 (defun org-element-lineage (blob &optional types with-self)
5970 "List all ancestors of a given element or object.
5972 BLOB is an object or element.
5974 When optional argument TYPES is a list of symbols, return the
5975 first element or object in the lineage whose type belongs to that
5976 list.
5978 When optional argument WITH-SELF is non-nil, lineage includes
5979 BLOB itself as the first element, and TYPES, if provided, also
5980 apply to it.
5982 When BLOB is obtained through `org-element-context' or
5983 `org-element-at-point', only ancestors from its section can be
5984 found. There is no such limitation when BLOB belongs to a full
5985 parse tree."
5986 (let ((up (if with-self blob (org-element-property :parent blob)))
5987 ancestors)
5988 (while (and up (not (memq (org-element-type up) types)))
5989 (unless types (push up ancestors))
5990 (setq up (org-element-property :parent up)))
5991 (if types up (nreverse ancestors))))
5993 (defun org-element-nested-p (elem-A elem-B)
5994 "Non-nil when elements ELEM-A and ELEM-B are nested."
5995 (let ((beg-A (org-element-property :begin elem-A))
5996 (beg-B (org-element-property :begin elem-B))
5997 (end-A (org-element-property :end elem-A))
5998 (end-B (org-element-property :end elem-B)))
5999 (or (and (>= beg-A beg-B) (<= end-A end-B))
6000 (and (>= beg-B beg-A) (<= end-B end-A)))))
6002 (defun org-element-swap-A-B (elem-A elem-B)
6003 "Swap elements ELEM-A and ELEM-B.
6004 Assume ELEM-B is after ELEM-A in the buffer. Leave point at the
6005 end of ELEM-A."
6006 (goto-char (org-element-property :begin elem-A))
6007 ;; There are two special cases when an element doesn't start at bol:
6008 ;; the first paragraph in an item or in a footnote definition.
6009 (let ((specialp (not (bolp))))
6010 ;; Only a paragraph without any affiliated keyword can be moved at
6011 ;; ELEM-A position in such a situation. Note that the case of
6012 ;; a footnote definition is impossible: it cannot contain two
6013 ;; paragraphs in a row because it cannot contain a blank line.
6014 (if (and specialp
6015 (or (not (eq (org-element-type elem-B) 'paragraph))
6016 (/= (org-element-property :begin elem-B)
6017 (org-element-property :contents-begin elem-B))))
6018 (error "Cannot swap elements"))
6019 ;; In a special situation, ELEM-A will have no indentation. We'll
6020 ;; give it ELEM-B's (which will in, in turn, have no indentation).
6021 (let* ((ind-B (when specialp
6022 (goto-char (org-element-property :begin elem-B))
6023 (org-get-indentation)))
6024 (beg-A (org-element-property :begin elem-A))
6025 (end-A (save-excursion
6026 (goto-char (org-element-property :end elem-A))
6027 (skip-chars-backward " \r\t\n")
6028 (point-at-eol)))
6029 (beg-B (org-element-property :begin elem-B))
6030 (end-B (save-excursion
6031 (goto-char (org-element-property :end elem-B))
6032 (skip-chars-backward " \r\t\n")
6033 (point-at-eol)))
6034 ;; Store inner overlays responsible for visibility status.
6035 ;; We also need to store their boundaries as they will be
6036 ;; removed from buffer.
6037 (overlays
6038 (cons
6039 (delq nil
6040 (mapcar (lambda (o)
6041 (and (>= (overlay-start o) beg-A)
6042 (<= (overlay-end o) end-A)
6043 (list o (overlay-start o) (overlay-end o))))
6044 (overlays-in beg-A end-A)))
6045 (delq nil
6046 (mapcar (lambda (o)
6047 (and (>= (overlay-start o) beg-B)
6048 (<= (overlay-end o) end-B)
6049 (list o (overlay-start o) (overlay-end o))))
6050 (overlays-in beg-B end-B)))))
6051 ;; Get contents.
6052 (body-A (buffer-substring beg-A end-A))
6053 (body-B (delete-and-extract-region beg-B end-B)))
6054 (goto-char beg-B)
6055 (when specialp
6056 (setq body-B (replace-regexp-in-string "\\`[ \t]*" "" body-B))
6057 (indent-to-column ind-B))
6058 (insert body-A)
6059 ;; Restore ex ELEM-A overlays.
6060 (let ((offset (- beg-B beg-A)))
6061 (dolist (o (car overlays))
6062 (move-overlay (car o) (+ (nth 1 o) offset) (+ (nth 2 o) offset)))
6063 (goto-char beg-A)
6064 (delete-region beg-A end-A)
6065 (insert body-B)
6066 ;; Restore ex ELEM-B overlays.
6067 (dolist (o (cdr overlays))
6068 (move-overlay (car o) (- (nth 1 o) offset) (- (nth 2 o) offset))))
6069 (goto-char (org-element-property :end elem-B)))))
6072 (provide 'org-element)
6074 ;; Local variables:
6075 ;; generated-autoload-file: "org-loaddefs.el"
6076 ;; End:
6078 ;;; org-element.el ends here