1 ;;; org-element.el --- Parser for Org Syntax -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2012-2015 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/>.
25 ;; Org syntax can be divided into three categories: "Greater
26 ;; elements", "Elements" and "Objects".
28 ;; Elements are related to the structure of the document. Indeed, all
29 ;; elements are a cover for the document: each position within belongs
30 ;; to at least one element.
32 ;; An element always starts and ends at the beginning of a line. With
33 ;; a few exceptions (`clock', `headline', `inlinetask', `item',
34 ;; `planning', `property-drawer', `node-property', `section' and
35 ;; `table-row' types), it can also accept a fixed set of keywords as
36 ;; attributes. Those are called "affiliated keywords" to distinguish
37 ;; them from other keywords, which are full-fledged elements. Almost
38 ;; all affiliated keywords are referenced in
39 ;; `org-element-affiliated-keywords'; the others are export attributes
40 ;; and start with "ATTR_" prefix.
42 ;; Element containing other elements (and only elements) are called
43 ;; greater elements. Concerned types are: `center-block', `drawer',
44 ;; `dynamic-block', `footnote-definition', `headline', `inlinetask',
45 ;; `item', `plain-list', `property-drawer', `quote-block', `section'
46 ;; and `special-block'.
48 ;; Other element types are: `babel-call', `clock', `comment',
49 ;; `comment-block', `diary-sexp', `example-block', `export-block',
50 ;; `fixed-width', `horizontal-rule', `keyword', `latex-environment',
51 ;; `node-property', `paragraph', `planning', `src-block', `table',
52 ;; `table-row' and `verse-block'. Among them, `paragraph' and
53 ;; `verse-block' types can contain Org objects and plain text.
55 ;; Objects are related to document's contents. Some of them are
56 ;; recursive. Associated types are of the following: `bold', `code',
57 ;; `entity', `export-snippet', `footnote-reference',
58 ;; `inline-babel-call', `inline-src-block', `italic',
59 ;; `latex-fragment', `line-break', `link', `macro', `radio-target',
60 ;; `statistics-cookie', `strike-through', `subscript', `superscript',
61 ;; `table-cell', `target', `timestamp', `underline' and `verbatim'.
63 ;; Some elements also have special properties whose value can hold
64 ;; objects themselves (e.g. an item tag or a headline name). Such
65 ;; values are called "secondary strings". Any object belongs to
66 ;; either an element or a secondary string.
68 ;; Notwithstanding affiliated keywords, each greater element, element
69 ;; and object has a fixed set of properties attached to it. Among
70 ;; them, four are shared by all types: `:begin' and `:end', which
71 ;; refer to the beginning and ending buffer positions of the
72 ;; considered element or object, `:post-blank', which holds the number
73 ;; of blank lines, or white spaces, at its end and `:parent' which
74 ;; refers to the element or object containing it. Greater elements,
75 ;; elements and objects containing objects will also have
76 ;; `:contents-begin' and `:contents-end' properties to delimit
77 ;; contents. Eventually, All elements have a `:post-affiliated'
78 ;; property referring to the buffer position after all affiliated
79 ;; keywords, if any, or to their beginning position otherwise.
81 ;; At the lowest level, a `:parent' property is also attached to any
82 ;; string, as a text property.
84 ;; Lisp-wise, an element or an object can be represented as a list.
85 ;; It follows the pattern (TYPE PROPERTIES CONTENTS), where:
86 ;; TYPE is a symbol describing the Org element or object.
87 ;; PROPERTIES is the property list attached to it. See docstring of
88 ;; appropriate parsing function to get an exhaustive
90 ;; CONTENTS is a list of elements, objects or raw strings contained
91 ;; in the current element or object, when applicable.
93 ;; An Org buffer is a nested list of such elements and objects, whose
94 ;; type is `org-data' and properties is nil.
96 ;; The first part of this file defines Org syntax, while the second
97 ;; one provide accessors and setters functions.
99 ;; The next part implements a parser and an interpreter for each
100 ;; element and object type in Org syntax.
102 ;; The following part creates a fully recursive buffer parser. It
103 ;; also provides a tool to map a function to elements or objects
104 ;; matching some criteria in the parse tree. Functions of interest
105 ;; are `org-element-parse-buffer', `org-element-map' and, to a lesser
106 ;; extent, `org-element-parse-secondary-string'.
108 ;; The penultimate part is the cradle of an interpreter for the
109 ;; obtained parse tree: `org-element-interpret-data'.
111 ;; The library ends by furnishing `org-element-at-point' function, and
112 ;; a way to give information about document structure around point
113 ;; with `org-element-context'. A cache mechanism is also provided for
124 ;;; Definitions And Rules
126 ;; Define elements, greater elements and specify recursive objects,
127 ;; along with the affiliated keywords recognized. Also set up
128 ;; restrictions on recursive objects combinations.
130 ;; `org-element-update-syntax' builds proper syntax regexps according
133 (defvar org-element-paragraph-separate nil
134 "Regexp to separate paragraphs in an Org buffer.
135 In the case of lines starting with \"#\" and \":\", this regexp
136 is not sufficient to know if point is at a paragraph ending. See
137 `org-element-paragraph-parser' for more information.")
139 (defvar org-element--object-regexp nil
140 "Regexp possibly matching the beginning of an object.
141 This regexp allows false positives. Dedicated parser (e.g.,
142 `org-export-bold-parser') will take care of further filtering.
143 Radio links are not matched by this regexp, as they are treated
144 specially in `org-element--object-lex'.")
146 (defun org-element--set-regexps ()
147 "Build variable syntax regexps."
148 (setq org-element-paragraph-separate
150 ;; Headlines, inlinetasks.
151 org-outline-regexp
"\\|"
152 ;; Footnote definitions.
153 "\\[\\(?:[0-9]+\\|fn:[-_[:word:]]+\\)\\]" "\\|"
159 ;; Tables (any type).
161 "\\+\\(?:-+\\+\\)+[ \t]*$" "\\|"
162 ;; Comments, keyword-like or block-like constructs.
163 ;; Blocks and keywords with dual values need to be
165 "#\\(?: \\|$\\|\\+\\(?:"
167 "\\S-+\\(?:\\[.*\\]\\)?:[ \t]*\\)\\)"
169 ;; Drawers (any type) and fixed-width areas. Drawers
170 ;; need to be double-checked.
171 ":\\(?: \\|$\\|[-_[:word:]]+:[ \t]*$\\)" "\\|"
173 "-\\{5,\\}[ \t]*$" "\\|"
174 ;; LaTeX environments.
175 "\\\\begin{\\([A-Za-z0-9*]+\\)}" "\\|"
177 (regexp-quote org-clock-string
) "\\|"
179 (let ((term (pcase org-plain-list-ordered-item-terminator
180 (?\
) ")") (?.
"\\.") (_ "[.)]")))
181 (alpha (and org-list-allow-alphabetical
"\\|[A-Za-z]")))
182 (concat "\\(?:[-+*]\\|\\(?:[0-9]+" alpha
"\\)" term
"\\)"
183 "\\(?:[ \t]\\|$\\)"))
185 org-element--object-regexp
186 (mapconcat #'identity
187 (let ((link-types (regexp-opt org-link-types
)))
190 "\\(?:[_^][-{(*+.,[:alnum:]]\\)"
191 ;; Bold, code, italic, strike-through, underline
195 (nth 2 org-emphasis-regexp-components
)))
197 (concat "\\<" link-types
":")
198 ;; Objects starting with "[": regular link,
199 ;; footnote reference, statistics cookie,
200 ;; timestamp (inactive).
201 "\\[\\(?:fn:\\|\\(?:[0-9]\\|\\(?:%\\|/[0-9]*\\)\\]\\)\\|\\[\\)"
202 ;; Objects starting with "@": export snippets.
204 ;; Objects starting with "{": macro.
206 ;; Objects starting with "<" : timestamp
207 ;; (active, diary), target, radio target and
209 (concat "<\\(?:%%\\|<\\|[0-9]\\|" link-types
"\\)")
210 ;; Objects starting with "$": latex fragment.
212 ;; Objects starting with "\": line break,
213 ;; entity, latex fragment.
214 "\\\\\\(?:[a-zA-Z[(]\\|\\\\[ \t]*$\\|_ +\\)"
215 ;; Objects starting with raw text: inline Babel
216 ;; source block, inline Babel call.
217 "\\(?:call\\|src\\)_"))
220 (org-element--set-regexps)
223 (defun org-element-update-syntax ()
224 "Update parser internals."
226 (org-element--set-regexps)
227 (org-element-cache-reset 'all
))
229 (defconst org-element-all-elements
230 '(babel-call center-block clock comment comment-block diary-sexp drawer
231 dynamic-block example-block export-block fixed-width
232 footnote-definition headline horizontal-rule inlinetask item
233 keyword latex-environment node-property paragraph plain-list
234 planning property-drawer quote-block section
235 special-block src-block table table-row verse-block
)
236 "Complete list of element types.")
238 (defconst org-element-greater-elements
239 '(center-block drawer dynamic-block footnote-definition headline inlinetask
240 item plain-list property-drawer quote-block section
242 "List of recursive element types aka Greater Elements.")
244 (defconst org-element-all-objects
245 '(bold code entity export-snippet footnote-reference inline-babel-call
246 inline-src-block italic line-break latex-fragment link macro
247 radio-target statistics-cookie strike-through subscript superscript
248 table-cell target timestamp underline verbatim
)
249 "Complete list of object types.")
251 (defconst org-element-recursive-objects
252 '(bold footnote-reference italic link subscript radio-target strike-through
253 superscript table-cell underline
)
254 "List of recursive object types.")
256 (defconst org-element-object-containers
257 (append org-element-recursive-objects
'(paragraph table-row verse-block
))
258 "List of object or element types that can directly contain objects.")
260 (defvar org-element-block-name-alist
261 '(("CENTER" . org-element-center-block-parser
)
262 ("COMMENT" . org-element-comment-block-parser
)
263 ("EXAMPLE" . org-element-example-block-parser
)
264 ("QUOTE" . org-element-quote-block-parser
)
265 ("SRC" . org-element-src-block-parser
)
266 ("VERSE" . org-element-verse-block-parser
))
267 "Alist between block names and the associated parsing function.
268 Names must be uppercase. Any block whose name has no association
269 is parsed with `org-element-special-block-parser'.")
271 (defconst org-element-affiliated-keywords
272 '("CAPTION" "DATA" "HEADER" "HEADERS" "LABEL" "NAME" "PLOT" "RESNAME" "RESULT"
273 "RESULTS" "SOURCE" "SRCNAME" "TBLNAME")
274 "List of affiliated keywords as strings.
275 By default, all keywords setting attributes (e.g., \"ATTR_LATEX\")
276 are affiliated keywords and need not to be in this list.")
278 (defconst org-element-keyword-translation-alist
279 '(("DATA" .
"NAME") ("LABEL" .
"NAME") ("RESNAME" .
"NAME")
280 ("SOURCE" .
"NAME") ("SRCNAME" .
"NAME") ("TBLNAME" .
"NAME")
281 ("RESULT" .
"RESULTS") ("HEADERS" .
"HEADER"))
282 "Alist of usual translations for keywords.
283 The key is the old name and the value the new one. The property
284 holding their value will be named after the translated name.")
286 (defconst org-element-multiple-keywords
'("CAPTION" "HEADER")
287 "List of affiliated keywords that can occur more than once in an element.
289 Their value will be consed into a list of strings, which will be
290 returned as the value of the property.
292 This list is checked after translations have been applied. See
293 `org-element-keyword-translation-alist'.
295 By default, all keywords setting attributes (e.g., \"ATTR_LATEX\")
296 allow multiple occurrences and need not to be in this list.")
298 (defconst org-element-parsed-keywords
'("CAPTION")
299 "List of affiliated keywords whose value can be parsed.
301 Their value will be stored as a secondary string: a list of
304 This list is checked after translations have been applied. See
305 `org-element-keyword-translation-alist'.")
307 (defconst org-element--parsed-properties-alist
308 (mapcar (lambda (k) (cons k
(intern (concat ":" (downcase k
)))))
309 org-element-parsed-keywords
)
310 "Alist of parsed keywords and associated properties.
311 This is generated from `org-element-parsed-keywords', which
314 (defconst org-element-dual-keywords
'("CAPTION" "RESULTS")
315 "List of affiliated keywords which can have a secondary value.
317 In Org syntax, they can be written with optional square brackets
318 before the colons. For example, RESULTS keyword can be
319 associated to a hash value with the following:
321 #+RESULTS[hash-string]: some-source
323 This list is checked after translations have been applied. See
324 `org-element-keyword-translation-alist'.")
326 (defconst org-element--affiliated-re
327 (format "[ \t]*#\\+\\(?:%s\\):[ \t]*"
329 ;; Dual affiliated keywords.
330 (format "\\(?1:%s\\)\\(?:\\[\\(.*\\)\\]\\)?"
331 (regexp-opt org-element-dual-keywords
))
333 ;; Regular affiliated keywords.
334 (format "\\(?1:%s\\)"
337 (lambda (k) (member k org-element-dual-keywords
))
338 org-element-affiliated-keywords
)))
340 ;; Export attributes.
341 "\\(?1:ATTR_[-_A-Za-z0-9]+\\)"))
342 "Regexp matching any affiliated keyword.
344 Keyword name is put in match group 1. Moreover, if keyword
345 belongs to `org-element-dual-keywords', put the dual value in
348 Don't modify it, set `org-element-affiliated-keywords' instead.")
350 (defconst org-element-object-restrictions
351 (let* ((standard-set (remq 'table-cell org-element-all-objects
))
352 (standard-set-no-line-break (remq 'line-break standard-set
)))
353 `((bold ,@standard-set
)
354 (footnote-reference ,@standard-set
)
355 (headline ,@standard-set-no-line-break
)
356 (inlinetask ,@standard-set-no-line-break
)
357 (italic ,@standard-set
)
358 (item ,@standard-set-no-line-break
)
359 (keyword ,@(remq 'footnote-reference standard-set
))
360 ;; Ignore all links excepted plain links in a link description.
361 ;; Also ignore radio-targets and line breaks.
362 (link bold code entity export-snippet inline-babel-call inline-src-block
363 italic latex-fragment macro plain-link statistics-cookie
364 strike-through subscript superscript underline verbatim
)
365 (paragraph ,@standard-set
)
366 ;; Remove any variable object from radio target as it would
367 ;; prevent it from being properly recognized.
368 (radio-target bold code entity italic latex-fragment strike-through
369 subscript superscript underline superscript
)
370 (strike-through ,@standard-set
)
371 (subscript ,@standard-set
)
372 (superscript ,@standard-set
)
373 ;; Ignore inline babel call and inline src block as formulas are
374 ;; possible. Also ignore line breaks and statistics cookies.
375 (table-cell bold code entity export-snippet footnote-reference italic
376 latex-fragment link macro radio-target strike-through
377 subscript superscript target timestamp underline verbatim
)
378 (table-row table-cell
)
379 (underline ,@standard-set
)
380 (verse-block ,@standard-set
)))
381 "Alist of objects restrictions.
383 key is an element or object type containing objects and value is
384 a list of types that can be contained within an element or object
387 For example, in a `radio-target' object, one can only find
388 entities, latex-fragments, subscript, superscript and text
391 This alist also applies to secondary string. For example, an
392 `headline' type element doesn't directly contain objects, but
393 still has an entry since one of its properties (`:title') does.")
395 (defconst org-element-secondary-value-alist
399 "Alist between element types and locations of secondary values.")
401 (defconst org-element--pair-square-table
402 (let ((table (make-syntax-table)))
403 (modify-syntax-entry ?\
[ "(]" table
)
404 (modify-syntax-entry ?\
] ")[" table
)
405 (dolist (char '(?\
{ ?\
} ?\
( ?\
) ?\
< ?\
>) table
)
406 (modify-syntax-entry char
" " table
)))
407 "Table used internally to pair only square brackets.
408 Other brackets are treated as spaces.")
412 ;;; Accessors and Setters
414 ;; Provide four accessors: `org-element-type', `org-element-property'
415 ;; `org-element-contents' and `org-element-restriction'.
417 ;; Setter functions allow to modify elements by side effect. There is
418 ;; `org-element-put-property', `org-element-set-contents'. These
419 ;; low-level functions are useful to build a parse tree.
421 ;; `org-element-adopt-element', `org-element-set-element',
422 ;; `org-element-extract-element' and `org-element-insert-before' are
423 ;; high-level functions useful to modify a parse tree.
425 ;; `org-element-secondary-p' is a predicate used to know if a given
426 ;; object belongs to a secondary string. `org-element-copy' returns
427 ;; an element or object, stripping its parent property in the process.
429 (defsubst org-element-type
(element)
430 "Return type of ELEMENT.
432 The function returns the type of the element or object provided.
433 It can also return the following special value:
434 `plain-text' for a string
435 `org-data' for a complete document
436 nil in any other case."
438 ((not (consp element
)) (and (stringp element
) 'plain-text
))
439 ((symbolp (car element
)) (car element
))))
441 (defsubst org-element-property
(property element
)
442 "Extract the value from the PROPERTY of an ELEMENT."
443 (if (stringp element
) (get-text-property 0 property element
)
444 (plist-get (nth 1 element
) property
)))
446 (defsubst org-element-contents
(element)
447 "Extract contents from an ELEMENT."
448 (cond ((not (consp element
)) nil
)
449 ((symbolp (car element
)) (nthcdr 2 element
))
452 (defsubst org-element-restriction
(element)
453 "Return restriction associated to ELEMENT.
454 ELEMENT can be an element, an object or a symbol representing an
455 element or object type."
456 (cdr (assq (if (symbolp element
) element
(org-element-type element
))
457 org-element-object-restrictions
)))
459 (defsubst org-element-put-property
(element property value
)
460 "In ELEMENT set PROPERTY to VALUE.
461 Return modified element."
462 (if (stringp element
) (org-add-props element nil property value
)
463 (setcar (cdr element
) (plist-put (nth 1 element
) property value
))
466 (defsubst org-element-set-contents
(element &rest contents
)
467 "Set ELEMENT contents to CONTENTS."
468 (cond ((not element
) (list contents
))
469 ((not (symbolp (car element
))) contents
)
470 ((cdr element
) (setcdr (cdr element
) contents
))
471 (t (nconc element contents
))))
473 (defun org-element-secondary-p (object)
474 "Non-nil when OBJECT directly belongs to a secondary string.
475 Return value is the property name, as a keyword, or nil."
476 (let* ((parent (org-element-property :parent object
))
477 (properties (cdr (assq (org-element-type parent
)
478 org-element-secondary-value-alist
))))
480 (dolist (p properties
)
481 (and (memq object
(org-element-property p parent
))
484 (defsubst org-element-adopt-elements
(parent &rest children
)
485 "Append elements to the contents of another element.
487 PARENT is an element or object. CHILDREN can be elements,
488 objects, or a strings.
490 The function takes care of setting `:parent' property for CHILD.
491 Return parent element."
492 (if (not children
) parent
493 ;; Link every child to PARENT. If PARENT is nil, it is a secondary
494 ;; string: parent is the list itself.
495 (dolist (child children
)
496 (org-element-put-property child
:parent
(or parent children
)))
497 ;; Add CHILDREN at the end of PARENT contents.
499 (apply #'org-element-set-contents
501 (nconc (org-element-contents parent
) children
)))
502 ;; Return modified PARENT element.
503 (or parent children
)))
505 (defun org-element-extract-element (element)
506 "Extract ELEMENT from parse tree.
507 Remove element from the parse tree by side-effect, and return it
508 with its `:parent' property stripped out."
509 (let ((parent (org-element-property :parent element
))
510 (secondary (org-element-secondary-p element
)))
512 (org-element-put-property
514 (delq element
(org-element-property secondary parent
)))
515 (apply #'org-element-set-contents
517 (delq element
(org-element-contents parent
))))
518 ;; Return ELEMENT with its :parent removed.
519 (org-element-put-property element
:parent nil
)))
521 (defun org-element-insert-before (element location
)
522 "Insert ELEMENT before LOCATION in parse tree.
523 LOCATION is an element, object or string within the parse tree.
524 Parse tree is modified by side effect."
525 (let* ((parent (org-element-property :parent location
))
526 (property (org-element-secondary-p location
))
527 (siblings (if property
(org-element-property property parent
)
528 (org-element-contents parent
)))
529 ;; Special case: LOCATION is the first element of an
530 ;; independent secondary string (e.g. :title property). Add
532 (specialp (and (not property
)
534 (eq (car parent
) location
))))
535 ;; Install ELEMENT at the appropriate POSITION within SIBLINGS.
537 ((or (null siblings
) (eq (car siblings
) location
))
538 (push element siblings
))
539 ((null location
) (nconc siblings
(list element
)))
540 (t (let ((previous (cadr (memq location
(reverse siblings
)))))
542 (error "No location found to insert element")
543 (let ((next (memq previous siblings
)))
544 (setcdr next
(cons element
(cdr next
))))))))
545 ;; Store SIBLINGS at appropriate place in parse tree.
547 (specialp (setcdr parent
(copy-sequence parent
)) (setcar parent element
))
548 (property (org-element-put-property parent property siblings
))
549 (t (apply #'org-element-set-contents parent siblings
)))
550 ;; Set appropriate :parent property.
551 (org-element-put-property element
:parent parent
)))
553 (defun org-element-set-element (old new
)
554 "Replace element or object OLD with element or object NEW.
555 The function takes care of setting `:parent' property for NEW."
556 ;; Ensure OLD and NEW have the same parent.
557 (org-element-put-property new
:parent
(org-element-property :parent old
))
558 (if (or (memq (org-element-type old
) '(plain-text nil
))
559 (memq (org-element-type new
) '(plain-text nil
)))
560 ;; We cannot replace OLD with NEW since one of them is not an
561 ;; object or element. We take the long path.
562 (progn (org-element-insert-before new old
)
563 (org-element-extract-element old
))
564 ;; Since OLD is going to be changed into NEW by side-effect, first
565 ;; make sure that every element or object within NEW has OLD as
567 (dolist (blob (org-element-contents new
))
568 (org-element-put-property blob
:parent old
))
569 ;; Transfer contents.
570 (apply #'org-element-set-contents old
(org-element-contents new
))
571 ;; Overwrite OLD's properties with NEW's.
572 (setcar (cdr old
) (nth 1 new
))
574 (setcar old
(car new
))))
576 (defun org-element-create (type &optional props
&rest children
)
577 "Create a new element of type TYPE.
578 Optional argument PROPS, when non-nil, is a plist defining the
579 properties of the element. CHILDREN can be elements, objects or
581 (apply #'org-element-adopt-elements
(list type props
) children
))
583 (defun org-element-copy (datum)
584 "Return a copy of DATUM.
585 DATUM is an element, object, string or nil. `:parent' property
586 is cleared and contents are removed in the process."
588 (let ((type (org-element-type datum
)))
590 (`org-data
(list 'org-data nil
))
591 (`plain-text
(substring-no-properties datum
))
592 (`nil
(copy-sequence datum
))
594 (list type
(plist-put (copy-sequence (nth 1 datum
)) :parent nil
)))))))
600 ;; For each greater element type, we define a parser and an
603 ;; A parser returns the element or object as the list described above.
604 ;; Most of them accepts no argument. Though, exceptions exist. Hence
605 ;; every element containing a secondary string (see
606 ;; `org-element-secondary-value-alist') will accept an optional
607 ;; argument to toggle parsing of these secondary strings. Moreover,
608 ;; `item' parser requires current list's structure as its first
611 ;; An interpreter accepts two arguments: the list representation of
612 ;; the element or object, and its contents. The latter may be nil,
613 ;; depending on the element or object considered. It returns the
614 ;; appropriate Org syntax, as a string.
616 ;; Parsing functions must follow the naming convention:
617 ;; org-element-TYPE-parser, where TYPE is greater element's type, as
618 ;; defined in `org-element-greater-elements'.
620 ;; Similarly, interpreting functions must follow the naming
621 ;; convention: org-element-TYPE-interpreter.
623 ;; With the exception of `headline' and `item' types, greater elements
624 ;; cannot contain other greater elements of their own type.
626 ;; Beside implementing a parser and an interpreter, adding a new
627 ;; greater element requires to tweak `org-element--current-element'.
628 ;; Moreover, the newly defined type must be added to both
629 ;; `org-element-all-elements' and `org-element-greater-elements'.
634 (defun org-element-center-block-parser (limit affiliated
)
635 "Parse a center block.
637 LIMIT bounds the search. AFFILIATED is a list of which CAR is
638 the buffer position at the beginning of the first affiliated
639 keyword and CDR is a plist of affiliated keywords along with
642 Return a list whose CAR is `center-block' and CDR is a plist
643 containing `:begin', `:end', `:contents-begin', `:contents-end',
644 `:post-blank' and `:post-affiliated' keywords.
646 Assume point is at the beginning of the block."
647 (let ((case-fold-search t
))
648 (if (not (save-excursion
649 (re-search-forward "^[ \t]*#\\+END_CENTER[ \t]*$" limit t
)))
650 ;; Incomplete block: parse it as a paragraph.
651 (org-element-paragraph-parser limit affiliated
)
652 (let ((block-end-line (match-beginning 0)))
653 (let* ((begin (car affiliated
))
654 (post-affiliated (point))
655 ;; Empty blocks have no contents.
656 (contents-begin (progn (forward-line)
657 (and (< (point) block-end-line
)
659 (contents-end (and contents-begin block-end-line
))
660 (pos-before-blank (progn (goto-char block-end-line
)
664 (skip-chars-forward " \r\t\n" limit
)
665 (if (eobp) (point) (line-beginning-position)))))
670 :contents-begin contents-begin
671 :contents-end contents-end
672 :post-blank
(count-lines pos-before-blank end
)
673 :post-affiliated post-affiliated
)
674 (cdr affiliated
))))))))
676 (defun org-element-center-block-interpreter (_ contents
)
677 "Interpret a center-block element as Org syntax.
678 CONTENTS is the contents of the element."
679 (format "#+BEGIN_CENTER\n%s#+END_CENTER" contents
))
684 (defun org-element-drawer-parser (limit affiliated
)
687 LIMIT bounds the search. AFFILIATED is a list of which CAR is
688 the buffer position at the beginning of the first affiliated
689 keyword and CDR is a plist of affiliated keywords along with
692 Return a list whose CAR is `drawer' and CDR is a plist containing
693 `:drawer-name', `:begin', `:end', `:contents-begin',
694 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
696 Assume point is at beginning of drawer."
697 (let ((case-fold-search t
))
698 (if (not (save-excursion (re-search-forward "^[ \t]*:END:[ \t]*$" limit t
)))
699 ;; Incomplete drawer: parse it as a paragraph.
700 (org-element-paragraph-parser limit affiliated
)
702 (let* ((drawer-end-line (match-beginning 0))
703 (name (progn (looking-at org-drawer-regexp
)
704 (org-match-string-no-properties 1)))
705 (begin (car affiliated
))
706 (post-affiliated (point))
707 ;; Empty drawers have no contents.
708 (contents-begin (progn (forward-line)
709 (and (< (point) drawer-end-line
)
711 (contents-end (and contents-begin drawer-end-line
))
712 (pos-before-blank (progn (goto-char drawer-end-line
)
715 (end (progn (skip-chars-forward " \r\t\n" limit
)
716 (if (eobp) (point) (line-beginning-position)))))
722 :contents-begin contents-begin
723 :contents-end contents-end
724 :post-blank
(count-lines pos-before-blank end
)
725 :post-affiliated post-affiliated
)
726 (cdr affiliated
))))))))
728 (defun org-element-drawer-interpreter (drawer contents
)
729 "Interpret DRAWER element as Org syntax.
730 CONTENTS is the contents of the element."
731 (format ":%s:\n%s:END:"
732 (org-element-property :drawer-name drawer
)
738 (defun org-element-dynamic-block-parser (limit affiliated
)
739 "Parse a dynamic block.
741 LIMIT bounds the search. AFFILIATED is a list of which CAR is
742 the buffer position at the beginning of the first affiliated
743 keyword and CDR is a plist of affiliated keywords along with
746 Return a list whose CAR is `dynamic-block' and CDR is a plist
747 containing `:block-name', `:begin', `:end', `:contents-begin',
748 `:contents-end', `:arguments', `:post-blank' and
749 `:post-affiliated' keywords.
751 Assume point is at beginning of dynamic block."
752 (let ((case-fold-search t
))
753 (if (not (save-excursion
754 (re-search-forward "^[ \t]*#\\+END:?[ \t]*$" limit t
)))
755 ;; Incomplete block: parse it as a paragraph.
756 (org-element-paragraph-parser limit affiliated
)
757 (let ((block-end-line (match-beginning 0)))
759 (let* ((name (progn (looking-at org-dblock-start-re
)
760 (org-match-string-no-properties 1)))
761 (arguments (org-match-string-no-properties 3))
762 (begin (car affiliated
))
763 (post-affiliated (point))
764 ;; Empty blocks have no contents.
765 (contents-begin (progn (forward-line)
766 (and (< (point) block-end-line
)
768 (contents-end (and contents-begin block-end-line
))
769 (pos-before-blank (progn (goto-char block-end-line
)
772 (end (progn (skip-chars-forward " \r\t\n" limit
)
773 (if (eobp) (point) (line-beginning-position)))))
780 :contents-begin contents-begin
781 :contents-end contents-end
782 :post-blank
(count-lines pos-before-blank end
)
783 :post-affiliated post-affiliated
)
784 (cdr affiliated
)))))))))
786 (defun org-element-dynamic-block-interpreter (dynamic-block contents
)
787 "Interpret DYNAMIC-BLOCK element as Org syntax.
788 CONTENTS is the contents of the element."
789 (format "#+BEGIN: %s%s\n%s#+END:"
790 (org-element-property :block-name dynamic-block
)
791 (let ((args (org-element-property :arguments dynamic-block
)))
792 (and args
(concat " " args
)))
796 ;;;; Footnote Definition
798 (defun org-element-footnote-definition-parser (limit affiliated
)
799 "Parse a footnote definition.
801 LIMIT bounds the search. AFFILIATED is a list of which CAR is
802 the buffer position at the beginning of the first affiliated
803 keyword and CDR is a plist of affiliated keywords along with
806 Return a list whose CAR is `footnote-definition' and CDR is
807 a plist containing `:label', `:begin' `:end', `:contents-begin',
808 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
810 Assume point is at the beginning of the footnote definition."
812 (let* ((label (progn (looking-at org-footnote-definition-re
)
813 (org-match-string-no-properties 1)))
814 (begin (car affiliated
))
815 (post-affiliated (point))
816 (ending (save-excursion
820 (concat org-outline-regexp-bol
"\\|"
821 org-footnote-definition-re
"\\|"
822 "^\\([ \t]*\n\\)\\{2,\\}") limit
'move
))
825 (contents-begin (progn
827 (skip-chars-forward " \r\t\n" ending
)
828 (cond ((= (point) ending
) nil
)
829 ((= (line-beginning-position) begin
) (point))
830 (t (line-beginning-position)))))
831 (contents-end (and contents-begin ending
))
832 (end (progn (goto-char ending
)
833 (skip-chars-forward " \r\t\n" limit
)
834 (if (eobp) (point) (line-beginning-position)))))
835 (list 'footnote-definition
840 :contents-begin contents-begin
841 :contents-end contents-end
842 :post-blank
(count-lines ending end
)
843 :post-affiliated post-affiliated
)
844 (cdr affiliated
))))))
846 (defun org-element-footnote-definition-interpreter (footnote-definition contents
)
847 "Interpret FOOTNOTE-DEFINITION element as Org syntax.
848 CONTENTS is the contents of the footnote-definition."
849 (concat (format "[%s]" (org-element-property :label footnote-definition
))
856 (defun org-element--get-node-properties ()
857 "Return node properties associated to headline at point.
858 Upcase property names. It avoids confusion between properties
859 obtained through property drawer and default properties from the
860 parser (e.g. `:end' and :END:). Return value is a plist."
863 (when (org-looking-at-p org-planning-line-re
) (forward-line))
864 (when (looking-at org-property-drawer-re
)
866 (let ((end (match-end 0)) properties
)
867 (while (< (line-end-position) end
)
868 (looking-at org-property-re
)
869 (push (org-match-string-no-properties 3) properties
)
870 (push (intern (concat ":" (upcase (match-string 2)))) properties
)
874 (defun org-element--get-time-properties ()
875 "Return time properties associated to headline at point.
876 Return value is a plist."
878 (when (progn (forward-line) (looking-at org-planning-line-re
))
879 (let ((end (line-end-position)) plist
)
880 (while (re-search-forward org-keyword-time-not-clock-regexp end t
)
881 (goto-char (match-end 1))
882 (skip-chars-forward " \t")
883 (let ((keyword (match-string 1))
884 (time (org-element-timestamp-parser)))
885 (cond ((equal keyword org-scheduled-string
)
886 (setq plist
(plist-put plist
:scheduled time
)))
887 ((equal keyword org-deadline-string
)
888 (setq plist
(plist-put plist
:deadline time
)))
889 (t (setq plist
(plist-put plist
:closed time
))))))
892 (defun org-element-headline-parser (limit &optional raw-secondary-p
)
895 Return a list whose CAR is `headline' and CDR is a plist
896 containing `:raw-value', `:title', `:begin', `:end',
897 `:pre-blank', `:contents-begin' and `:contents-end', `:level',
898 `:priority', `:tags', `:todo-keyword',`:todo-type', `:scheduled',
899 `:deadline', `:closed', `:archivedp', `:commentedp'
900 `:footnote-section-p', `:post-blank' and `:post-affiliated'
903 The plist also contains any property set in the property drawer,
904 with its name in upper cases and colons added at the
905 beginning (e.g., `:CUSTOM_ID').
907 LIMIT is a buffer position bounding the search.
909 When RAW-SECONDARY-P is non-nil, headline's title will not be
910 parsed as a secondary string, but as a plain string instead.
912 Assume point is at beginning of the headline."
914 (let* ((begin (point))
915 (level (prog1 (org-reduced-level (skip-chars-forward "*"))
916 (skip-chars-forward " \t")))
917 (todo (and org-todo-regexp
918 (let (case-fold-search) (looking-at org-todo-regexp
))
919 (progn (goto-char (match-end 0))
920 (skip-chars-forward " \t")
923 (and todo
(if (member todo org-done-keywords
) 'done
'todo
)))
924 (priority (and (looking-at "\\[#.\\][ \t]*")
925 (progn (goto-char (match-end 0))
926 (aref (match-string 0) 2))))
928 (and (let (case-fold-search) (looking-at org-comment-string
))
929 (goto-char (match-end 0))))
930 (title-start (point))
931 (tags (when (re-search-forward
932 (org-re "[ \t]+\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
935 (goto-char (match-beginning 0))
936 (org-split-string (match-string 1) ":")))
939 (buffer-substring-no-properties title-start title-end
)))
940 (archivedp (member org-archive-tag tags
))
941 (footnote-section-p (and org-footnote-section
942 (string= org-footnote-section raw-value
)))
943 (standard-props (org-element--get-node-properties))
944 (time-props (org-element--get-time-properties))
945 (end (min (save-excursion (org-end-of-subtree t t
)) limit
))
946 (pos-after-head (progn (forward-line) (point)))
947 (contents-begin (save-excursion
948 (skip-chars-forward " \r\t\n" end
)
949 (and (/= (point) end
) (line-beginning-position))))
950 (contents-end (and contents-begin
951 (progn (goto-char end
)
952 (skip-chars-backward " \r\t\n")
958 (list :raw-value raw-value
962 (if (not contents-begin
) 0
963 (count-lines pos-after-head contents-begin
))
964 :contents-begin contents-begin
965 :contents-end contents-end
971 :post-blank
(count-lines
972 (or contents-end pos-after-head
)
974 :footnote-section-p footnote-section-p
976 :commentedp commentedp
977 :post-affiliated begin
)
980 (org-element-put-property
982 (if raw-secondary-p raw-value
983 (let ((title (org-element--parse-objects
984 (progn (goto-char title-start
)
985 (skip-chars-forward " \t")
987 (progn (goto-char title-end
)
988 (skip-chars-backward " \t")
991 (org-element-restriction 'headline
))))
992 (dolist (datum title title
)
993 (org-element-put-property datum
:parent headline
)))))))))
995 (defun org-element-headline-interpreter (headline contents
)
996 "Interpret HEADLINE element as Org syntax.
997 CONTENTS is the contents of the element."
998 (let* ((level (org-element-property :level headline
))
999 (todo (org-element-property :todo-keyword headline
))
1000 (priority (org-element-property :priority headline
))
1001 (title (org-element-interpret-data
1002 (org-element-property :title headline
)))
1003 (tags (let ((tag-list (org-element-property :tags headline
)))
1005 (format ":%s:" (mapconcat #'identity tag-list
":")))))
1006 (commentedp (org-element-property :commentedp headline
))
1007 (pre-blank (or (org-element-property :pre-blank headline
) 0))
1009 (concat (make-string (if org-odd-levels-only
(1- (* level
2)) level
)
1011 (and todo
(concat " " todo
))
1012 (and commentedp
(concat " " org-comment-string
))
1013 (and priority
(format " [#%c]" priority
))
1015 (if (and org-footnote-section
1016 (org-element-property :footnote-section-p headline
))
1017 org-footnote-section
1024 ((zerop org-tags-column
) (format " %s" tags
))
1025 ((< org-tags-column
0)
1028 (max (- (+ org-tags-column
(length heading
) (length tags
))) 1)
1033 (make-string (max (- org-tags-column
(length heading
)) 1) ?\s
)
1035 (make-string (1+ pre-blank
) ?
\n)
1041 (defun org-element-inlinetask-parser (limit &optional raw-secondary-p
)
1042 "Parse an inline task.
1044 Return a list whose CAR is `inlinetask' and CDR is a plist
1045 containing `:title', `:begin', `:end', `:contents-begin' and
1046 `:contents-end', `:level', `:priority', `:raw-value', `:tags',
1047 `:todo-keyword', `:todo-type', `:scheduled', `:deadline',
1048 `:closed', `:post-blank' and `:post-affiliated' keywords.
1050 The plist also contains any property set in the property drawer,
1051 with its name in upper cases and colons added at the
1052 beginning (e.g., `:CUSTOM_ID').
1054 When optional argument RAW-SECONDARY-P is non-nil, inline-task's
1055 title will not be parsed as a secondary string, but as a plain
1058 Assume point is at beginning of the inline task."
1060 (let* ((begin (point))
1061 (level (prog1 (org-reduced-level (skip-chars-forward "*"))
1062 (skip-chars-forward " \t")))
1063 (todo (and org-todo-regexp
1064 (let (case-fold-search) (looking-at org-todo-regexp
))
1065 (progn (goto-char (match-end 0))
1066 (skip-chars-forward " \t")
1068 (todo-type (and todo
1069 (if (member todo org-done-keywords
) 'done
'todo
)))
1070 (priority (and (looking-at "\\[#.\\][ \t]*")
1071 (progn (goto-char (match-end 0))
1072 (aref (match-string 0) 2))))
1073 (title-start (point))
1074 (tags (when (re-search-forward
1075 (org-re "[ \t]+\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
1078 (goto-char (match-beginning 0))
1079 (org-split-string (match-string 1) ":")))
1081 (raw-value (org-trim
1082 (buffer-substring-no-properties title-start title-end
)))
1083 (task-end (save-excursion
1085 (and (re-search-forward org-outline-regexp-bol limit t
)
1086 (org-looking-at-p "END[ \t]*$")
1087 (line-beginning-position))))
1088 (standard-props (and task-end
(org-element--get-node-properties)))
1089 (time-props (and task-end
(org-element--get-time-properties)))
1090 (contents-begin (progn (forward-line)
1091 (and task-end
(< (point) task-end
) (point))))
1092 (contents-end (and contents-begin task-end
))
1093 (before-blank (if (not task-end
) (point)
1094 (goto-char task-end
)
1097 (end (progn (skip-chars-forward " \r\t\n" limit
)
1098 (if (eobp) (point) (line-beginning-position))))
1102 (list :raw-value raw-value
1105 :contents-begin contents-begin
1106 :contents-end contents-end
1111 :todo-type todo-type
1112 :post-blank
(count-lines before-blank end
)
1113 :post-affiliated begin
)
1116 (org-element-put-property
1118 (if raw-secondary-p raw-value
1119 (let ((title (org-element--parse-objects
1120 (progn (goto-char title-start
)
1121 (skip-chars-forward " \t")
1123 (progn (goto-char title-end
)
1124 (skip-chars-backward " \t")
1127 (org-element-restriction 'inlinetask
))))
1128 (dolist (datum title title
)
1129 (org-element-put-property datum
:parent inlinetask
))))))))
1131 (defun org-element-inlinetask-interpreter (inlinetask contents
)
1132 "Interpret INLINETASK element as Org syntax.
1133 CONTENTS is the contents of inlinetask."
1134 (let* ((level (org-element-property :level inlinetask
))
1135 (todo (org-element-property :todo-keyword inlinetask
))
1136 (priority (org-element-property :priority inlinetask
))
1137 (title (org-element-interpret-data
1138 (org-element-property :title inlinetask
)))
1139 (tags (let ((tag-list (org-element-property :tags inlinetask
)))
1141 (format ":%s:" (mapconcat 'identity tag-list
":")))))
1142 (task (concat (make-string level ?
*)
1143 (and todo
(concat " " todo
))
1144 (and priority
(format " [#%c]" priority
))
1145 (and title
(concat " " title
)))))
1150 ((zerop org-tags-column
) (format " %s" tags
))
1151 ((< org-tags-column
0)
1154 (max (- (+ org-tags-column
(length task
) (length tags
))) 1)
1159 (make-string (max (- org-tags-column
(length task
)) 1) ?
)
1161 ;; Prefer degenerate inlinetasks when there are no
1166 (make-string level ?
*) " END")))))
1171 (defun org-element-item-parser (_ struct
&optional raw-secondary-p
)
1174 STRUCT is the structure of the plain list.
1176 Return a list whose CAR is `item' and CDR is a plist containing
1177 `:bullet', `:begin', `:end', `:contents-begin', `:contents-end',
1178 `:checkbox', `:counter', `:tag', `:structure', `:post-blank' and
1179 `:post-affiliated' keywords.
1181 When optional argument RAW-SECONDARY-P is non-nil, item's tag, if
1182 any, will not be parsed as a secondary string, but as a plain
1185 Assume point is at the beginning of the item."
1188 (looking-at org-list-full-item-re
)
1189 (let* ((begin (point))
1190 (bullet (org-match-string-no-properties 1))
1191 (checkbox (let ((box (match-string 3)))
1192 (cond ((equal "[ ]" box
) 'off
)
1193 ((equal "[X]" box
) 'on
)
1194 ((equal "[-]" box
) 'trans
))))
1195 (counter (let ((c (match-string 2)))
1199 ((string-match "[A-Za-z]" c
)
1200 (- (string-to-char (upcase (match-string 0 c
)))
1202 ((string-match "[0-9]+" c
)
1203 (string-to-number (match-string 0 c
)))))))
1204 (end (progn (goto-char (nth 6 (assq (point) struct
)))
1205 (if (bolp) (point) (line-beginning-position 2))))
1208 ;; Ignore tags in un-ordered lists: they are just
1209 ;; a part of item's body.
1210 (if (and (match-beginning 4)
1211 (save-match-data (string-match "[.)]" bullet
)))
1214 (skip-chars-forward " \r\t\n" end
)
1215 (cond ((= (point) end
) nil
)
1216 ;; If first line isn't empty, contents really
1217 ;; start at the text after item's meta-data.
1218 ((= (line-beginning-position) begin
) (point))
1219 (t (line-beginning-position)))))
1220 (contents-end (and contents-begin
1221 (progn (goto-char end
)
1222 (skip-chars-backward " \r\t\n")
1223 (line-beginning-position 2))))
1226 (list :bullet bullet
1229 :contents-begin contents-begin
1230 :contents-end contents-end
1234 :post-blank
(count-lines (or contents-end begin
) end
)
1235 :post-affiliated begin
))))
1236 (org-element-put-property
1238 (let ((raw (org-list-get-tag begin struct
)))
1240 (if raw-secondary-p raw
1241 (let ((tag (org-element--parse-objects
1242 (match-beginning 4) (match-end 4) nil
1243 (org-element-restriction 'item
))))
1244 (dolist (datum tag tag
)
1245 (org-element-put-property datum
:parent item
))))))))))
1247 (defun org-element-item-interpreter (item contents
)
1248 "Interpret ITEM element as Org syntax.
1249 CONTENTS is the contents of the element."
1250 (let* ((bullet (let ((bullet (org-element-property :bullet item
)))
1251 (org-list-bullet-string
1252 (cond ((not (string-match "[0-9a-zA-Z]" bullet
)) "- ")
1253 ((eq org-plain-list-ordered-item-terminator ?\
)) "1)")
1255 (checkbox (org-element-property :checkbox item
))
1256 (counter (org-element-property :counter item
))
1257 (tag (let ((tag (org-element-property :tag item
)))
1258 (and tag
(org-element-interpret-data tag
))))
1259 ;; Compute indentation.
1260 (ind (make-string (length bullet
) 32))
1261 (item-starts-with-par-p
1262 (eq (org-element-type (car (org-element-contents item
)))
1267 (and counter
(format "[@%d] " counter
))
1273 (and tag
(format "%s :: " tag
))
1275 (let ((contents (replace-regexp-in-string
1276 "\\(^\\)[ \t]*\\S-" ind contents nil nil
1)))
1277 (if item-starts-with-par-p
(org-trim contents
)
1278 (concat "\n" contents
)))))))
1283 (defun org-element--list-struct (limit)
1284 ;; Return structure of list at point. Internal function. See
1285 ;; `org-list-struct' for details.
1286 (let ((case-fold-search t
)
1288 (item-re (org-item-re))
1289 (inlinetask-re (and (featurep 'org-inlinetask
) "^\\*+ "))
1295 ;; At limit: end all items.
1298 (let ((end (progn (skip-chars-backward " \r\t\n")
1301 (dolist (item items
(sort (nconc items struct
)
1302 'car-less-than-car
))
1303 (setcar (nthcdr 6 item
) end
)))))
1304 ;; At list end: end all items.
1305 ((looking-at org-list-end-re
)
1306 (throw 'exit
(dolist (item items
(sort (nconc items struct
)
1307 'car-less-than-car
))
1308 (setcar (nthcdr 6 item
) (point)))))
1309 ;; At a new item: end previous sibling.
1310 ((looking-at item-re
)
1311 (let ((ind (save-excursion (skip-chars-forward " \t")
1313 (setq top-ind
(min top-ind ind
))
1314 (while (and items
(<= ind
(nth 1 (car items
))))
1315 (let ((item (pop items
)))
1316 (setcar (nthcdr 6 item
) (point))
1317 (push item struct
)))
1318 (push (progn (looking-at org-list-full-item-re
)
1319 (let ((bullet (match-string-no-properties 1)))
1323 (match-string-no-properties 2) ; counter
1324 (match-string-no-properties 3) ; checkbox
1326 (and (save-match-data
1327 (string-match "[-+*]" bullet
))
1328 (match-string-no-properties 4))
1329 ;; Ending position, unknown so far.
1333 ;; Skip empty lines.
1334 ((looking-at "^[ \t]*$") (forward-line))
1335 ;; Skip inline tasks and blank lines along the way.
1336 ((and inlinetask-re
(looking-at inlinetask-re
))
1338 (let ((origin (point)))
1339 (when (re-search-forward inlinetask-re limit t
)
1340 (if (org-looking-at-p "END[ \t]*$") (forward-line)
1341 (goto-char origin
)))))
1342 ;; At some text line. Check if it ends any previous item.
1344 (let ((ind (save-excursion (skip-chars-forward " \t")
1346 (when (<= ind top-ind
)
1347 (skip-chars-backward " \r\t\n")
1349 (while (<= ind
(nth 1 (car items
)))
1350 (let ((item (pop items
)))
1351 (setcar (nthcdr 6 item
) (line-beginning-position))
1354 (throw 'exit
(sort struct
#'car-less-than-car
))))))
1355 ;; Skip blocks (any type) and drawers contents.
1357 ((and (looking-at "[ \t]*#\\+BEGIN\\(:\\|_\\S-+\\)")
1359 (format "^[ \t]*#\\+END%s[ \t]*$" (match-string 1))
1361 ((and (looking-at org-drawer-regexp
)
1362 (re-search-forward "^[ \t]*:END:[ \t]*$" limit t
))))
1363 (forward-line))))))))
1365 (defun org-element-plain-list-parser (limit affiliated structure
)
1366 "Parse a plain list.
1368 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1369 the buffer position at the beginning of the first affiliated
1370 keyword and CDR is a plist of affiliated keywords along with
1371 their value. STRUCTURE is the structure of the plain list being
1374 Return a list whose CAR is `plain-list' and CDR is a plist
1375 containing `:type', `:begin', `:end', `:contents-begin' and
1376 `:contents-end', `:structure', `:post-blank' and
1377 `:post-affiliated' keywords.
1379 Assume point is at the beginning of the list."
1381 (let* ((struct (or structure
(org-element--list-struct limit
)))
1382 (type (cond ((org-looking-at-p "[ \t]*[A-Za-z0-9]") 'ordered
)
1383 ((nth 5 (assq (point) struct
)) 'descriptive
)
1385 (contents-begin (point))
1386 (begin (car affiliated
))
1387 (contents-end (let* ((item (assq contents-begin struct
))
1390 (while (and (setq item
(assq pos struct
))
1391 (= (nth 1 item
) ind
))
1392 (setq pos
(nth 6 item
)))
1394 (end (progn (goto-char contents-end
)
1395 (skip-chars-forward " \r\t\n" limit
)
1396 (if (= (point) limit
) limit
(line-beginning-position)))))
1403 :contents-begin contents-begin
1404 :contents-end contents-end
1406 :post-blank
(count-lines contents-end end
)
1407 :post-affiliated contents-begin
)
1408 (cdr affiliated
))))))
1410 (defun org-element-plain-list-interpreter (_ contents
)
1411 "Interpret plain-list element as Org syntax.
1412 CONTENTS is the contents of the element."
1415 (goto-char (point-min))
1420 ;;;; Property Drawer
1422 (defun org-element-property-drawer-parser (limit)
1423 "Parse a property drawer.
1425 LIMIT bounds the search.
1427 Return a list whose car is `property-drawer' and cdr is a plist
1428 containing `:begin', `:end', `:contents-begin', `:contents-end',
1429 `:post-blank' and `:post-affiliated' keywords.
1431 Assume point is at the beginning of the property drawer."
1433 (let ((case-fold-search t
)
1435 (contents-begin (line-beginning-position 2)))
1436 (re-search-forward "^[ \t]*:END:[ \t]*$" limit t
)
1437 (let ((contents-end (and (> (match-beginning 0) contents-begin
)
1438 (match-beginning 0)))
1439 (before-blank (progn (forward-line) (point)))
1440 (end (progn (skip-chars-forward " \r\t\n" limit
)
1441 (if (eobp) (point) (line-beginning-position)))))
1442 (list 'property-drawer
1445 :contents-begin
(and contents-end contents-begin
)
1446 :contents-end contents-end
1447 :post-blank
(count-lines before-blank end
)
1448 :post-affiliated begin
))))))
1450 (defun org-element-property-drawer-interpreter (_ contents
)
1451 "Interpret property-drawer element as Org syntax.
1452 CONTENTS is the properties within the drawer."
1453 (format ":PROPERTIES:\n%s:END:" contents
))
1458 (defun org-element-quote-block-parser (limit affiliated
)
1459 "Parse a quote block.
1461 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1462 the buffer position at the beginning of the first affiliated
1463 keyword and CDR is a plist of affiliated keywords along with
1466 Return a list whose CAR is `quote-block' and CDR is a plist
1467 containing `:begin', `:end', `:contents-begin', `:contents-end',
1468 `:post-blank' and `:post-affiliated' keywords.
1470 Assume point is at the beginning of the block."
1471 (let ((case-fold-search t
))
1472 (if (not (save-excursion
1473 (re-search-forward "^[ \t]*#\\+END_QUOTE[ \t]*$" limit t
)))
1474 ;; Incomplete block: parse it as a paragraph.
1475 (org-element-paragraph-parser limit affiliated
)
1476 (let ((block-end-line (match-beginning 0)))
1478 (let* ((begin (car affiliated
))
1479 (post-affiliated (point))
1480 ;; Empty blocks have no contents.
1481 (contents-begin (progn (forward-line)
1482 (and (< (point) block-end-line
)
1484 (contents-end (and contents-begin block-end-line
))
1485 (pos-before-blank (progn (goto-char block-end-line
)
1488 (end (progn (skip-chars-forward " \r\t\n" limit
)
1489 (if (eobp) (point) (line-beginning-position)))))
1494 :contents-begin contents-begin
1495 :contents-end contents-end
1496 :post-blank
(count-lines pos-before-blank end
)
1497 :post-affiliated post-affiliated
)
1498 (cdr affiliated
)))))))))
1500 (defun org-element-quote-block-interpreter (_ contents
)
1501 "Interpret quote-block element as Org syntax.
1502 CONTENTS is the contents of the element."
1503 (format "#+BEGIN_QUOTE\n%s#+END_QUOTE" contents
))
1508 (defun org-element-section-parser (_)
1511 Return a list whose CAR is `section' and CDR is a plist
1512 containing `:begin', `:end', `:contents-begin', `contents-end',
1513 `:post-blank' and `:post-affiliated' keywords."
1515 ;; Beginning of section is the beginning of the first non-blank
1516 ;; line after previous headline.
1517 (let ((begin (point))
1518 (end (progn (org-with-limited-levels (outline-next-heading))
1520 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
1521 (line-beginning-position 2))))
1525 :contents-begin begin
1526 :contents-end pos-before-blank
1527 :post-blank
(count-lines pos-before-blank end
)
1528 :post-affiliated begin
)))))
1530 (defun org-element-section-interpreter (_ contents
)
1531 "Interpret section element as Org syntax.
1532 CONTENTS is the contents of the element."
1538 (defun org-element-special-block-parser (limit affiliated
)
1539 "Parse a special block.
1541 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1542 the buffer position at the beginning of the first affiliated
1543 keyword and CDR is a plist of affiliated keywords along with
1546 Return a list whose CAR is `special-block' and CDR is a plist
1547 containing `:type', `:begin', `:end', `:contents-begin',
1548 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
1550 Assume point is at the beginning of the block."
1551 (let* ((case-fold-search t
)
1552 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
1553 (match-string-no-properties 1))))
1554 (if (not (save-excursion
1556 (format "^[ \t]*#\\+END_%s[ \t]*$" (regexp-quote type
))
1558 ;; Incomplete block: parse it as a paragraph.
1559 (org-element-paragraph-parser limit affiliated
)
1560 (let ((block-end-line (match-beginning 0)))
1562 (let* ((begin (car affiliated
))
1563 (post-affiliated (point))
1564 ;; Empty blocks have no contents.
1565 (contents-begin (progn (forward-line)
1566 (and (< (point) block-end-line
)
1568 (contents-end (and contents-begin block-end-line
))
1569 (pos-before-blank (progn (goto-char block-end-line
)
1572 (end (progn (skip-chars-forward " \r\t\n" limit
)
1573 (if (eobp) (point) (line-beginning-position)))))
1574 (list 'special-block
1579 :contents-begin contents-begin
1580 :contents-end contents-end
1581 :post-blank
(count-lines pos-before-blank end
)
1582 :post-affiliated post-affiliated
)
1583 (cdr affiliated
)))))))))
1585 (defun org-element-special-block-interpreter (special-block contents
)
1586 "Interpret SPECIAL-BLOCK element as Org syntax.
1587 CONTENTS is the contents of the element."
1588 (let ((block-type (org-element-property :type special-block
)))
1589 (format "#+BEGIN_%s\n%s#+END_%s" block-type contents block-type
)))
1595 ;; For each element, a parser and an interpreter are also defined.
1596 ;; Both follow the same naming convention used for greater elements.
1598 ;; Also, as for greater elements, adding a new element type is done
1599 ;; through the following steps: implement a parser and an interpreter,
1600 ;; tweak `org-element--current-element' so that it recognizes the new
1601 ;; type and add that new type to `org-element-all-elements'.
1603 ;; As a special case, when the newly defined type is a block type,
1604 ;; `org-element-block-name-alist' has to be modified accordingly.
1609 (defun org-element-babel-call-parser (limit affiliated
)
1610 "Parse a babel call.
1612 LIMIT bounds the search. AFFILIATED is a list of which car is
1613 the buffer position at the beginning of the first affiliated
1614 keyword and cdr is a plist of affiliated keywords along with
1617 Return a list whose car is `babel-call' and cdr is a plist
1618 containing `:call', `:inside-header', `:arguments',
1619 `:end-header', `:begin', `:end', `:value', `:post-blank' and
1620 `:post-affiliated' as keywords."
1622 (let* ((begin (car affiliated
))
1623 (post-affiliated (point))
1624 (value (progn (search-forward ":" nil t
)
1626 (buffer-substring-no-properties
1627 (point) (line-end-position)))))
1628 (pos-before-blank (progn (forward-line) (point)))
1629 (end (progn (skip-chars-forward " \r\t\n" limit
)
1630 (if (eobp) (point) (line-beginning-position))))
1633 "\\([^()\n]+?\\)\\(?:\\[\\(.*?\\)\\]\\)?(\\(.*\\))[ \t]*\\(.*\\)"
1637 (list :call
(and valid-value
(match-string 1 value
))
1638 :inside-header
(and valid-value
1639 (org-string-nw-p (match-string 2 value
)))
1640 :arguments
(and valid-value
1641 (org-string-nw-p (match-string 3 value
)))
1642 :end-header
(and valid-value
1643 (org-string-nw-p (match-string 4 value
)))
1647 :post-blank
(count-lines pos-before-blank end
)
1648 :post-affiliated post-affiliated
)
1649 (cdr affiliated
))))))
1651 (defun org-element-babel-call-interpreter (babel-call _
)
1652 "Interpret BABEL-CALL element as Org syntax."
1654 (org-element-property :call babel-call
)
1655 (let ((h (org-element-property :inside-header babel-call
)))
1656 (and h
(format "[%s]" h
)))
1657 (concat "(" (org-element-property :arguments babel-call
) ")")
1658 (let ((h (org-element-property :end-header babel-call
)))
1659 (and h
(concat " " h
)))))
1664 (defun org-element-clock-parser (limit)
1667 LIMIT bounds the search.
1669 Return a list whose CAR is `clock' and CDR is a plist containing
1670 `:status', `:value', `:time', `:begin', `:end', `:post-blank' and
1671 `:post-affiliated' as keywords."
1673 (let* ((case-fold-search nil
)
1675 (value (progn (search-forward org-clock-string
(line-end-position) t
)
1676 (skip-chars-forward " \t")
1677 (org-element-timestamp-parser)))
1678 (duration (and (search-forward " => " (line-end-position) t
)
1679 (progn (skip-chars-forward " \t")
1680 (looking-at "\\(\\S-+\\)[ \t]*$"))
1681 (org-match-string-no-properties 1)))
1682 (status (if duration
'closed
'running
))
1683 (post-blank (let ((before-blank (progn (forward-line) (point))))
1684 (skip-chars-forward " \r\t\n" limit
)
1685 (skip-chars-backward " \t")
1686 (unless (bolp) (end-of-line))
1687 (count-lines before-blank
(point))))
1690 (list :status status
1695 :post-blank post-blank
1696 :post-affiliated begin
)))))
1698 (defun org-element-clock-interpreter (clock _
)
1699 "Interpret CLOCK element as Org syntax."
1700 (concat org-clock-string
" "
1701 (org-element-timestamp-interpreter
1702 (org-element-property :value clock
) nil
)
1703 (let ((duration (org-element-property :duration clock
)))
1708 (org-split-string duration
":")))))))
1713 (defun org-element-comment-parser (limit affiliated
)
1716 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1717 the buffer position at the beginning of the first affiliated
1718 keyword and CDR is a plist of affiliated keywords along with
1721 Return a list whose CAR is `comment' and CDR is a plist
1722 containing `:begin', `:end', `:value', `:post-blank',
1723 `:post-affiliated' keywords.
1725 Assume point is at comment beginning."
1727 (let* ((begin (car affiliated
))
1728 (post-affiliated (point))
1729 (value (prog2 (looking-at "[ \t]*# ?")
1730 (buffer-substring-no-properties
1731 (match-end 0) (line-end-position))
1734 ;; Get comments ending.
1736 (while (and (< (point) limit
) (looking-at "[ \t]*#\\( \\|$\\)"))
1737 ;; Accumulate lines without leading hash and first
1742 (buffer-substring-no-properties
1743 (match-end 0) (line-end-position))))
1746 (end (progn (goto-char com-end
)
1747 (skip-chars-forward " \r\t\n" limit
)
1748 (if (eobp) (point) (line-beginning-position)))))
1754 :post-blank
(count-lines com-end end
)
1755 :post-affiliated post-affiliated
)
1756 (cdr affiliated
))))))
1758 (defun org-element-comment-interpreter (comment _
)
1759 "Interpret COMMENT element as Org syntax.
1761 (replace-regexp-in-string "^" "# " (org-element-property :value comment
)))
1766 (defun org-element-comment-block-parser (limit affiliated
)
1767 "Parse an export block.
1769 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1770 the buffer position at the beginning of the first affiliated
1771 keyword and CDR is a plist of affiliated keywords along with
1774 Return a list whose CAR is `comment-block' and CDR is a plist
1775 containing `:begin', `:end', `:value', `:post-blank' and
1776 `:post-affiliated' keywords.
1778 Assume point is at comment block beginning."
1779 (let ((case-fold-search t
))
1780 (if (not (save-excursion
1781 (re-search-forward "^[ \t]*#\\+END_COMMENT[ \t]*$" limit t
)))
1782 ;; Incomplete block: parse it as a paragraph.
1783 (org-element-paragraph-parser limit affiliated
)
1784 (let ((contents-end (match-beginning 0)))
1786 (let* ((begin (car affiliated
))
1787 (post-affiliated (point))
1788 (contents-begin (progn (forward-line) (point)))
1789 (pos-before-blank (progn (goto-char contents-end
)
1792 (end (progn (skip-chars-forward " \r\t\n" limit
)
1793 (if (eobp) (point) (line-beginning-position))))
1794 (value (buffer-substring-no-properties
1795 contents-begin contents-end
)))
1796 (list 'comment-block
1801 :post-blank
(count-lines pos-before-blank end
)
1802 :post-affiliated post-affiliated
)
1803 (cdr affiliated
)))))))))
1805 (defun org-element-comment-block-interpreter (comment-block _
)
1806 "Interpret COMMENT-BLOCK element as Org syntax."
1807 (format "#+BEGIN_COMMENT\n%s#+END_COMMENT"
1808 (org-element-normalize-string
1809 (org-remove-indentation
1810 (org-element-property :value comment-block
)))))
1815 (defun org-element-diary-sexp-parser (limit affiliated
)
1816 "Parse a diary sexp.
1818 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1819 the buffer position at the beginning of the first affiliated
1820 keyword and CDR is a plist of affiliated keywords along with
1823 Return a list whose CAR is `diary-sexp' and CDR is a plist
1824 containing `:begin', `:end', `:value', `:post-blank' and
1825 `:post-affiliated' keywords."
1827 (let ((begin (car affiliated
))
1828 (post-affiliated (point))
1829 (value (progn (looking-at "\\(%%(.*\\)[ \t]*$")
1830 (org-match-string-no-properties 1)))
1831 (pos-before-blank (progn (forward-line) (point)))
1832 (end (progn (skip-chars-forward " \r\t\n" limit
)
1833 (if (eobp) (point) (line-beginning-position)))))
1839 :post-blank
(count-lines pos-before-blank end
)
1840 :post-affiliated post-affiliated
)
1841 (cdr affiliated
))))))
1843 (defun org-element-diary-sexp-interpreter (diary-sexp _
)
1844 "Interpret DIARY-SEXP as Org syntax."
1845 (org-element-property :value diary-sexp
))
1850 (defun org-element-example-block-parser (limit affiliated
)
1851 "Parse an example block.
1853 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1854 the buffer position at the beginning of the first affiliated
1855 keyword and CDR is a plist of affiliated keywords along with
1858 Return a list whose CAR is `example-block' and CDR is a plist
1859 containing `:begin', `:end', `:number-lines', `:preserve-indent',
1860 `:retain-labels', `:use-labels', `:label-fmt', `:switches',
1861 `:value', `:post-blank' and `:post-affiliated' keywords."
1862 (let ((case-fold-search t
))
1863 (if (not (save-excursion
1864 (re-search-forward "^[ \t]*#\\+END_EXAMPLE[ \t]*$" limit t
)))
1865 ;; Incomplete block: parse it as a paragraph.
1866 (org-element-paragraph-parser limit affiliated
)
1867 (let ((contents-end (match-beginning 0)))
1871 (looking-at "^[ \t]*#\\+BEGIN_EXAMPLE\\(?: +\\(.*\\)\\)?")
1872 (org-match-string-no-properties 1)))
1873 ;; Switches analysis
1875 (cond ((not switches
) nil
)
1876 ((string-match "-n\\>" switches
) 'new
)
1877 ((string-match "+n\\>" switches
) 'continued
)))
1879 (and switches
(string-match "-i\\>" switches
)))
1880 ;; Should labels be retained in (or stripped from) example
1884 (not (string-match "-r\\>" switches
))
1885 (and number-lines
(string-match "-k\\>" switches
))))
1886 ;; What should code-references use - labels or
1891 (not (string-match "-k\\>" switches
)))))
1894 (string-match "-l +\"\\([^\"\n]+\\)\"" switches
)
1895 (match-string 1 switches
)))
1896 ;; Standard block parsing.
1897 (begin (car affiliated
))
1898 (post-affiliated (point))
1899 (block-ind (progn (skip-chars-forward " \t") (current-column)))
1900 (contents-begin (progn (forward-line) (point)))
1901 (value (org-element-remove-indentation
1902 (org-unescape-code-in-string
1903 (buffer-substring-no-properties
1904 contents-begin contents-end
))
1906 (pos-before-blank (progn (goto-char contents-end
)
1909 (end (progn (skip-chars-forward " \r\t\n" limit
)
1910 (if (eobp) (point) (line-beginning-position)))))
1911 (list 'example-block
1917 :number-lines number-lines
1918 :preserve-indent preserve-indent
1919 :retain-labels retain-labels
1920 :use-labels use-labels
1921 :label-fmt label-fmt
1922 :post-blank
(count-lines pos-before-blank end
)
1923 :post-affiliated post-affiliated
)
1924 (cdr affiliated
)))))))))
1926 (defun org-element-example-block-interpreter (example-block _
)
1927 "Interpret EXAMPLE-BLOCK element as Org syntax."
1928 (let ((switches (org-element-property :switches example-block
))
1929 (value (org-element-property :value example-block
)))
1930 (concat "#+BEGIN_EXAMPLE" (and switches
(concat " " switches
)) "\n"
1931 (org-element-normalize-string
1932 (org-escape-code-in-string
1933 (if (or org-src-preserve-indentation
1934 (org-element-property :preserve-indent example-block
))
1936 (org-element-remove-indentation value
))))
1942 (defun org-element-export-block-parser (limit affiliated
)
1943 "Parse an export block.
1945 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1946 the buffer position at the beginning of the first affiliated
1947 keyword and CDR is a plist of affiliated keywords along with
1950 Return a list whose CAR is `export-block' and CDR is a plist
1951 containing `:begin', `:end', `:type', `:value', `:post-blank' and
1952 `:post-affiliated' keywords.
1954 Assume point is at export-block beginning."
1955 (let* ((case-fold-search t
)
1956 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
1957 (upcase (org-match-string-no-properties 1)))))
1958 (if (not (save-excursion
1960 (format "^[ \t]*#\\+END_%s[ \t]*$" type
) limit t
)))
1961 ;; Incomplete block: parse it as a paragraph.
1962 (org-element-paragraph-parser limit affiliated
)
1963 (let ((contents-end (match-beginning 0)))
1965 (let* ((begin (car affiliated
))
1966 (post-affiliated (point))
1967 (contents-begin (progn (forward-line) (point)))
1968 (pos-before-blank (progn (goto-char contents-end
)
1971 (end (progn (skip-chars-forward " \r\t\n" limit
)
1972 (if (eobp) (point) (line-beginning-position))))
1973 (value (buffer-substring-no-properties contents-begin
1981 :post-blank
(count-lines pos-before-blank end
)
1982 :post-affiliated post-affiliated
)
1983 (cdr affiliated
)))))))))
1985 (defun org-element-export-block-interpreter (export-block _
)
1986 "Interpret EXPORT-BLOCK element as Org syntax."
1987 (let ((type (org-element-property :type export-block
)))
1988 (concat (format "#+BEGIN_%s\n" type
)
1989 (org-element-property :value export-block
)
1990 (format "#+END_%s" type
))))
1995 (defun org-element-fixed-width-parser (limit affiliated
)
1996 "Parse a fixed-width section.
1998 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1999 the buffer position at the beginning of the first affiliated
2000 keyword and CDR is a plist of affiliated keywords along with
2003 Return a list whose CAR is `fixed-width' and CDR is a plist
2004 containing `:begin', `:end', `:value', `:post-blank' and
2005 `:post-affiliated' keywords.
2007 Assume point is at the beginning of the fixed-width area."
2009 (let* ((begin (car affiliated
))
2010 (post-affiliated (point))
2014 (while (and (< (point) limit
)
2015 (looking-at "[ \t]*:\\( \\|$\\)"))
2016 ;; Accumulate text without starting colons.
2019 (buffer-substring-no-properties
2020 (match-end 0) (point-at-eol))
2024 (end (progn (skip-chars-forward " \r\t\n" limit
)
2025 (if (eobp) (point) (line-beginning-position)))))
2031 :post-blank
(count-lines end-area end
)
2032 :post-affiliated post-affiliated
)
2033 (cdr affiliated
))))))
2035 (defun org-element-fixed-width-interpreter (fixed-width _
)
2036 "Interpret FIXED-WIDTH element as Org syntax."
2037 (let ((value (org-element-property :value fixed-width
)))
2039 (replace-regexp-in-string
2041 (if (string-match "\n\\'" value
) (substring value
0 -
1) value
)))))
2044 ;;;; Horizontal Rule
2046 (defun org-element-horizontal-rule-parser (limit affiliated
)
2047 "Parse an horizontal rule.
2049 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2050 the buffer position at the beginning of the first affiliated
2051 keyword and CDR is a plist of affiliated keywords along with
2054 Return a list whose CAR is `horizontal-rule' and CDR is a plist
2055 containing `:begin', `:end', `:post-blank' and `:post-affiliated'
2058 (let ((begin (car affiliated
))
2059 (post-affiliated (point))
2060 (post-hr (progn (forward-line) (point)))
2061 (end (progn (skip-chars-forward " \r\t\n" limit
)
2062 (if (eobp) (point) (line-beginning-position)))))
2063 (list 'horizontal-rule
2067 :post-blank
(count-lines post-hr end
)
2068 :post-affiliated post-affiliated
)
2069 (cdr affiliated
))))))
2071 (defun org-element-horizontal-rule-interpreter (&rest _
)
2072 "Interpret HORIZONTAL-RULE element as Org syntax."
2078 (defun org-element-keyword-parser (limit affiliated
)
2079 "Parse a keyword at point.
2081 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2082 the buffer position at the beginning of the first affiliated
2083 keyword and CDR is a plist of affiliated keywords along with
2086 Return a list whose CAR is `keyword' and CDR is a plist
2087 containing `:key', `:value', `:begin', `:end', `:post-blank' and
2088 `:post-affiliated' keywords."
2090 ;; An orphaned affiliated keyword is considered as a regular
2091 ;; keyword. In this case AFFILIATED is nil, so we take care of
2092 ;; this corner case.
2093 (let ((begin (or (car affiliated
) (point)))
2094 (post-affiliated (point))
2095 (key (progn (looking-at "[ \t]*#\\+\\(\\S-+*\\):")
2096 (upcase (org-match-string-no-properties 1))))
2097 (value (org-trim (buffer-substring-no-properties
2098 (match-end 0) (point-at-eol))))
2099 (pos-before-blank (progn (forward-line) (point)))
2100 (end (progn (skip-chars-forward " \r\t\n" limit
)
2101 (if (eobp) (point) (line-beginning-position)))))
2108 :post-blank
(count-lines pos-before-blank end
)
2109 :post-affiliated post-affiliated
)
2110 (cdr affiliated
))))))
2112 (defun org-element-keyword-interpreter (keyword _
)
2113 "Interpret KEYWORD element as Org syntax."
2115 (org-element-property :key keyword
)
2116 (org-element-property :value keyword
)))
2119 ;;;; Latex Environment
2121 (defconst org-element--latex-begin-environment
2122 "^[ \t]*\\\\begin{\\([A-Za-z0-9*]+\\)}"
2123 "Regexp matching the beginning of a LaTeX environment.
2124 The environment is captured by the first group.
2126 See also `org-element--latex-end-environment'.")
2128 (defconst org-element--latex-end-environment
2129 "\\\\end{%s}[ \t]*$"
2130 "Format string matching the ending of a LaTeX environment.
2131 See also `org-element--latex-begin-environment'.")
2133 (defun org-element-latex-environment-parser (limit affiliated
)
2134 "Parse a LaTeX environment.
2136 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2137 the buffer position at the beginning of the first affiliated
2138 keyword and CDR is a plist of affiliated keywords along with
2141 Return a list whose CAR is `latex-environment' and CDR is a plist
2142 containing `:begin', `:end', `:value', `:post-blank' and
2143 `:post-affiliated' keywords.
2145 Assume point is at the beginning of the latex environment."
2147 (let ((case-fold-search t
)
2148 (code-begin (point)))
2149 (looking-at org-element--latex-begin-environment
)
2150 (if (not (re-search-forward (format org-element--latex-end-environment
2151 (regexp-quote (match-string 1)))
2153 ;; Incomplete latex environment: parse it as a paragraph.
2154 (org-element-paragraph-parser limit affiliated
)
2155 (let* ((code-end (progn (forward-line) (point)))
2156 (begin (car affiliated
))
2157 (value (buffer-substring-no-properties code-begin code-end
))
2158 (end (progn (skip-chars-forward " \r\t\n" limit
)
2159 (if (eobp) (point) (line-beginning-position)))))
2160 (list 'latex-environment
2165 :post-blank
(count-lines code-end end
)
2166 :post-affiliated code-begin
)
2167 (cdr affiliated
))))))))
2169 (defun org-element-latex-environment-interpreter (latex-environment _
)
2170 "Interpret LATEX-ENVIRONMENT element as Org syntax."
2171 (org-element-property :value latex-environment
))
2176 (defun org-element-node-property-parser (limit)
2177 "Parse a node-property at point.
2179 LIMIT bounds the search.
2181 Return a list whose CAR is `node-property' and CDR is a plist
2182 containing `:key', `:value', `:begin', `:end', `:post-blank' and
2183 `:post-affiliated' keywords."
2184 (looking-at org-property-re
)
2185 (let ((case-fold-search t
)
2187 (key (org-match-string-no-properties 2))
2188 (value (org-match-string-no-properties 3))
2189 (end (save-excursion
2191 (if (re-search-forward org-property-re limit t
)
2192 (line-beginning-position)
2194 (list 'node-property
2200 :post-affiliated begin
))))
2202 (defun org-element-node-property-interpreter (node-property _
)
2203 "Interpret NODE-PROPERTY element as Org syntax."
2204 (format org-property-format
2205 (format ":%s:" (org-element-property :key node-property
))
2206 (or (org-element-property :value node-property
) "")))
2211 (defun org-element-paragraph-parser (limit affiliated
)
2214 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2215 the buffer position at the beginning of the first affiliated
2216 keyword and CDR is a plist of affiliated keywords along with
2219 Return a list whose CAR is `paragraph' and CDR is a plist
2220 containing `:begin', `:end', `:contents-begin' and
2221 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
2223 Assume point is at the beginning of the paragraph."
2225 (let* ((begin (car affiliated
))
2226 (contents-begin (point))
2228 (let ((case-fold-search t
))
2230 ;; A matching `org-element-paragraph-separate' is not
2231 ;; necessarily the end of the paragraph. In particular,
2232 ;; drawers, blocks or LaTeX environments opening lines
2233 ;; must be closed. Moreover keywords with a secondary
2234 ;; value must belong to "dual keywords".
2237 ((not (and (re-search-forward
2238 org-element-paragraph-separate limit
'move
)
2239 (progn (beginning-of-line) t
))))
2240 ((looking-at org-drawer-regexp
)
2242 (re-search-forward "^[ \t]*:END:[ \t]*$" limit t
)))
2243 ((looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
2246 (format "^[ \t]*#\\+END_%s[ \t]*$"
2247 (regexp-quote (match-string 1)))
2249 ((looking-at org-element--latex-begin-environment
)
2252 (format org-element--latex-end-environment
2253 (regexp-quote (match-string 1)))
2255 ((looking-at "[ \t]*#\\+\\(\\S-+\\)\\[.*\\]:")
2256 (member-ignore-case (match-string 1)
2257 org-element-dual-keywords
))
2258 ;; Everything else is unambiguous.
2261 (if (= (point) limit
) limit
2262 (goto-char (line-beginning-position)))))
2263 (contents-end (save-excursion
2264 (skip-chars-backward " \r\t\n" contents-begin
)
2265 (line-beginning-position 2)))
2266 (end (progn (skip-chars-forward " \r\t\n" limit
)
2267 (if (eobp) (point) (line-beginning-position)))))
2272 :contents-begin contents-begin
2273 :contents-end contents-end
2274 :post-blank
(count-lines before-blank end
)
2275 :post-affiliated contents-begin
)
2276 (cdr affiliated
))))))
2278 (defun org-element-paragraph-interpreter (_ contents
)
2279 "Interpret paragraph element as Org syntax.
2280 CONTENTS is the contents of the element."
2286 (defun org-element-planning-parser (limit)
2289 LIMIT bounds the search.
2291 Return a list whose CAR is `planning' and CDR is a plist
2292 containing `:closed', `:deadline', `:scheduled', `:begin',
2293 `:end', `:post-blank' and `:post-affiliated' keywords."
2295 (let* ((case-fold-search nil
)
2297 (post-blank (let ((before-blank (progn (forward-line) (point))))
2298 (skip-chars-forward " \r\t\n" limit
)
2299 (skip-chars-backward " \t")
2300 (unless (bolp) (end-of-line))
2301 (count-lines before-blank
(point))))
2303 closed deadline scheduled
)
2305 (while (re-search-forward org-keyword-time-not-clock-regexp end t
)
2306 (goto-char (match-end 1))
2307 (skip-chars-forward " \t" end
)
2308 (let ((keyword (match-string 1))
2309 (time (org-element-timestamp-parser)))
2310 (cond ((equal keyword org-closed-string
) (setq closed time
))
2311 ((equal keyword org-deadline-string
) (setq deadline time
))
2312 (t (setq scheduled time
)))))
2314 (list :closed closed
2316 :scheduled scheduled
2319 :post-blank post-blank
2320 :post-affiliated begin
)))))
2322 (defun org-element-planning-interpreter (planning _
)
2323 "Interpret PLANNING element as Org syntax."
2327 (list (let ((deadline (org-element-property :deadline planning
)))
2329 (concat org-deadline-string
" "
2330 (org-element-timestamp-interpreter deadline nil
))))
2331 (let ((scheduled (org-element-property :scheduled planning
)))
2333 (concat org-scheduled-string
" "
2334 (org-element-timestamp-interpreter scheduled nil
))))
2335 (let ((closed (org-element-property :closed planning
)))
2337 (concat org-closed-string
" "
2338 (org-element-timestamp-interpreter closed nil
))))))
2344 (defun org-element-src-block-parser (limit affiliated
)
2347 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2348 the buffer position at the beginning of the first affiliated
2349 keyword and CDR is a plist of affiliated keywords along with
2352 Return a list whose CAR is `src-block' and CDR is a plist
2353 containing `:language', `:switches', `:parameters', `:begin',
2354 `:end', `:number-lines', `:retain-labels', `:use-labels',
2355 `:label-fmt', `:preserve-indent', `:value', `:post-blank' and
2356 `:post-affiliated' keywords.
2358 Assume point is at the beginning of the block."
2359 (let ((case-fold-search t
))
2360 (if (not (save-excursion (re-search-forward "^[ \t]*#\\+END_SRC[ \t]*$"
2362 ;; Incomplete block: parse it as a paragraph.
2363 (org-element-paragraph-parser limit affiliated
)
2364 (let ((contents-end (match-beginning 0)))
2366 (let* ((begin (car affiliated
))
2367 (post-affiliated (point))
2368 ;; Get language as a string.
2372 (concat "^[ \t]*#\\+BEGIN_SRC"
2373 "\\(?: +\\(\\S-+\\)\\)?"
2374 "\\(\\(?: +\\(?:-l \".*?\"\\|[-+][A-Za-z]\\)\\)+\\)?"
2376 (org-match-string-no-properties 1)))
2378 (switches (org-match-string-no-properties 2))
2380 (parameters (org-match-string-no-properties 3))
2381 ;; Switches analysis
2383 (cond ((not switches
) nil
)
2384 ((string-match "-n\\>" switches
) 'new
)
2385 ((string-match "+n\\>" switches
) 'continued
)))
2386 (preserve-indent (and switches
2387 (string-match "-i\\>" switches
)))
2390 (string-match "-l +\"\\([^\"\n]+\\)\"" switches
)
2391 (match-string 1 switches
)))
2392 ;; Should labels be retained in (or stripped from)
2396 (not (string-match "-r\\>" switches
))
2397 (and number-lines
(string-match "-k\\>" switches
))))
2398 ;; What should code-references use - labels or
2403 (not (string-match "-k\\>" switches
)))))
2405 (block-ind (progn (skip-chars-forward " \t") (current-column)))
2407 (value (org-element-remove-indentation
2408 (org-unescape-code-in-string
2409 (buffer-substring-no-properties
2410 (progn (forward-line) (point)) contents-end
))
2412 (pos-before-blank (progn (goto-char contents-end
)
2415 ;; Get position after ending blank lines.
2416 (end (progn (skip-chars-forward " \r\t\n" limit
)
2417 (if (eobp) (point) (line-beginning-position)))))
2420 (list :language language
2421 :switches
(and (org-string-nw-p switches
)
2422 (org-trim switches
))
2423 :parameters
(and (org-string-nw-p parameters
)
2424 (org-trim parameters
))
2427 :number-lines number-lines
2428 :preserve-indent preserve-indent
2429 :retain-labels retain-labels
2430 :use-labels use-labels
2431 :label-fmt label-fmt
2433 :post-blank
(count-lines pos-before-blank end
)
2434 :post-affiliated post-affiliated
)
2435 (cdr affiliated
)))))))))
2437 (defun org-element-src-block-interpreter (src-block _
)
2438 "Interpret SRC-BLOCK element as Org syntax."
2439 (let ((lang (org-element-property :language src-block
))
2440 (switches (org-element-property :switches src-block
))
2441 (params (org-element-property :parameters src-block
))
2443 (let ((val (org-element-property :value src-block
)))
2445 ((or org-src-preserve-indentation
2446 (org-element-property :preserve-indent src-block
))
2448 ((zerop org-edit-src-content-indentation
) val
)
2450 (let ((ind (make-string org-edit-src-content-indentation ?\s
)))
2451 (replace-regexp-in-string
2452 "\\(^\\)[ \t]*\\S-" ind val nil nil
1)))))))
2453 (concat (format "#+BEGIN_SRC%s\n"
2454 (concat (and lang
(concat " " lang
))
2455 (and switches
(concat " " switches
))
2456 (and params
(concat " " params
))))
2457 (org-element-normalize-string (org-escape-code-in-string value
))
2463 (defun org-element-table-parser (limit affiliated
)
2464 "Parse a table at point.
2466 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2467 the buffer position at the beginning of the first affiliated
2468 keyword and CDR is a plist of affiliated keywords along with
2471 Return a list whose CAR is `table' and CDR is a plist containing
2472 `:begin', `:end', `:tblfm', `:type', `:contents-begin',
2473 `:contents-end', `:value', `:post-blank' and `:post-affiliated'
2476 Assume point is at the beginning of the table."
2478 (let* ((case-fold-search t
)
2479 (table-begin (point))
2480 (type (if (looking-at "[ \t]*|") 'org
'table.el
))
2481 (end-re (format "^[ \t]*\\($\\|[^| \t%s]\\)"
2482 (if (eq type
'org
) "" "+")))
2483 (begin (car affiliated
))
2485 (if (re-search-forward end-re limit
'move
)
2486 (goto-char (match-beginning 0))
2489 (while (looking-at "[ \t]*#\\+TBLFM: +\\(.*\\)[ \t]*$")
2490 (push (org-match-string-no-properties 1) acc
)
2493 (pos-before-blank (point))
2494 (end (progn (skip-chars-forward " \r\t\n" limit
)
2495 (if (eobp) (point) (line-beginning-position)))))
2502 ;; Only `org' tables have contents. `table.el' tables
2503 ;; use a `:value' property to store raw table as
2505 :contents-begin
(and (eq type
'org
) table-begin
)
2506 :contents-end
(and (eq type
'org
) table-end
)
2507 :value
(and (eq type
'table.el
)
2508 (buffer-substring-no-properties
2509 table-begin table-end
))
2510 :post-blank
(count-lines pos-before-blank end
)
2511 :post-affiliated table-begin
)
2512 (cdr affiliated
))))))
2514 (defun org-element-table-interpreter (table contents
)
2515 "Interpret TABLE element as Org syntax.
2516 CONTENTS is a string, if table's type is `org', or nil."
2517 (if (eq (org-element-property :type table
) 'table.el
)
2518 (org-remove-indentation (org-element-property :value table
))
2519 (concat (with-temp-buffer (insert contents
)
2522 (mapconcat (lambda (fm) (concat "#+TBLFM: " fm
))
2523 (reverse (org-element-property :tblfm table
))
2529 (defun org-element-table-row-parser (_)
2530 "Parse table row at point.
2532 Return a list whose CAR is `table-row' and CDR is a plist
2533 containing `:begin', `:end', `:contents-begin', `:contents-end',
2534 `:type', `:post-blank' and `:post-affiliated' keywords."
2536 (let* ((type (if (looking-at "^[ \t]*|-") 'rule
'standard
))
2538 ;; A table rule has no contents. In that case, ensure
2539 ;; CONTENTS-BEGIN matches CONTENTS-END.
2540 (contents-begin (and (eq type
'standard
) (search-forward "|")))
2541 (contents-end (and (eq type
'standard
)
2544 (skip-chars-backward " \t")
2546 (end (line-beginning-position 2)))
2551 :contents-begin contents-begin
2552 :contents-end contents-end
2554 :post-affiliated begin
)))))
2556 (defun org-element-table-row-interpreter (table-row contents
)
2557 "Interpret TABLE-ROW element as Org syntax.
2558 CONTENTS is the contents of the table row."
2559 (if (eq (org-element-property :type table-row
) 'rule
) "|-"
2560 (concat "| " contents
)))
2565 (defun org-element-verse-block-parser (limit affiliated
)
2566 "Parse a verse block.
2568 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2569 the buffer position at the beginning of the first affiliated
2570 keyword and CDR is a plist of affiliated keywords along with
2573 Return a list whose CAR is `verse-block' and CDR is a plist
2574 containing `:begin', `:end', `:contents-begin', `:contents-end',
2575 `:post-blank' and `:post-affiliated' keywords.
2577 Assume point is at beginning of the block."
2578 (let ((case-fold-search t
))
2579 (if (not (save-excursion
2580 (re-search-forward "^[ \t]*#\\+END_VERSE[ \t]*$" limit t
)))
2581 ;; Incomplete block: parse it as a paragraph.
2582 (org-element-paragraph-parser limit affiliated
)
2583 (let ((contents-end (match-beginning 0)))
2585 (let* ((begin (car affiliated
))
2586 (post-affiliated (point))
2587 (contents-begin (progn (forward-line) (point)))
2588 (pos-before-blank (progn (goto-char contents-end
)
2591 (end (progn (skip-chars-forward " \r\t\n" limit
)
2592 (if (eobp) (point) (line-beginning-position)))))
2597 :contents-begin contents-begin
2598 :contents-end contents-end
2599 :post-blank
(count-lines pos-before-blank end
)
2600 :post-affiliated post-affiliated
)
2601 (cdr affiliated
)))))))))
2603 (defun org-element-verse-block-interpreter (_ contents
)
2604 "Interpret verse-block element as Org syntax.
2605 CONTENTS is verse block contents."
2606 (format "#+BEGIN_VERSE\n%s#+END_VERSE" contents
))
2612 ;; Unlike to elements, raw text can be found between objects. Hence,
2613 ;; `org-element--object-lex' is provided to find the next object in
2616 ;; Some object types (e.g., `italic') are recursive. Restrictions on
2617 ;; object types they can contain will be specified in
2618 ;; `org-element-object-restrictions'.
2620 ;; Creating a new type of object requires to alter
2621 ;; `org-element--object-regexp' and `org-element--object-lex', add the
2622 ;; new type in `org-element-all-objects', and possibly add
2623 ;; restrictions in `org-element-object-restrictions'.
2627 (defun org-element-bold-parser ()
2628 "Parse bold object at point, if any.
2630 When at a bold object, return a list whose car is `bold' and cdr
2631 is a plist with `:begin', `:end', `:contents-begin' and
2632 `:contents-end' and `:post-blank' keywords. Otherwise, return
2635 Assume point is at the first star marker."
2637 (unless (bolp) (backward-char 1))
2638 (when (looking-at org-emph-re
)
2639 (let ((begin (match-beginning 2))
2640 (contents-begin (match-beginning 4))
2641 (contents-end (match-end 4))
2642 (post-blank (progn (goto-char (match-end 2))
2643 (skip-chars-forward " \t")))
2648 :contents-begin contents-begin
2649 :contents-end contents-end
2650 :post-blank post-blank
))))))
2652 (defun org-element-bold-interpreter (_ contents
)
2653 "Interpret bold object as Org syntax.
2654 CONTENTS is the contents of the object."
2655 (format "*%s*" contents
))
2660 (defun org-element-code-parser ()
2661 "Parse code object at point, if any.
2663 When at a code object, return a list whose car is `code' and cdr
2664 is a plist with `:value', `:begin', `:end' and `:post-blank'
2665 keywords. Otherwise, return nil.
2667 Assume point is at the first tilde marker."
2669 (unless (bolp) (backward-char 1))
2670 (when (looking-at org-emph-re
)
2671 (let ((begin (match-beginning 2))
2672 (value (org-match-string-no-properties 4))
2673 (post-blank (progn (goto-char (match-end 2))
2674 (skip-chars-forward " \t")))
2680 :post-blank post-blank
))))))
2682 (defun org-element-code-interpreter (code _
)
2683 "Interpret CODE object as Org syntax."
2684 (format "~%s~" (org-element-property :value code
)))
2689 (defun org-element-entity-parser ()
2690 "Parse entity at point, if any.
2692 When at an entity, return a list whose car is `entity' and cdr
2693 a plist with `:begin', `:end', `:latex', `:latex-math-p',
2694 `:html', `:latin1', `:utf-8', `:ascii', `:use-brackets-p' and
2695 `:post-blank' as keywords. Otherwise, return nil.
2697 Assume point is at the beginning of the entity."
2699 (when (looking-at "\\\\\\(?:\\(?1:_ +\\)\\|\\(?1:there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\(?2:$\\|{}\\|[^[:alpha:]]\\)\\)")
2701 (let* ((value (or (org-entity-get (match-string 1))
2702 (throw 'no-object nil
)))
2703 (begin (match-beginning 0))
2704 (bracketsp (string= (match-string 2) "{}"))
2705 (post-blank (progn (goto-char (match-end 1))
2706 (when bracketsp
(forward-char 2))
2707 (skip-chars-forward " \t")))
2710 (list :name
(car value
)
2711 :latex
(nth 1 value
)
2712 :latex-math-p
(nth 2 value
)
2714 :ascii
(nth 4 value
)
2715 :latin1
(nth 5 value
)
2716 :utf-8
(nth 6 value
)
2719 :use-brackets-p bracketsp
2720 :post-blank post-blank
)))))))
2722 (defun org-element-entity-interpreter (entity _
)
2723 "Interpret ENTITY object as Org syntax."
2725 (org-element-property :name entity
)
2726 (when (org-element-property :use-brackets-p entity
) "{}")))
2731 (defun org-element-export-snippet-parser ()
2732 "Parse export snippet at point.
2734 When at an export snippet, return a list whose car is
2735 `export-snippet' and cdr a plist with `:begin', `:end',
2736 `:back-end', `:value' and `:post-blank' as keywords. Otherwise,
2739 Assume point is at the beginning of the snippet."
2742 (when (and (looking-at "@@\\([-A-Za-z0-9]+\\):")
2744 (save-match-data (goto-char (match-end 0))
2745 (re-search-forward "@@" nil t
)
2746 (match-beginning 0))))
2747 (let* ((begin (match-beginning 0))
2748 (back-end (org-match-string-no-properties 1))
2749 (value (buffer-substring-no-properties
2750 (match-end 0) contents-end
))
2751 (post-blank (skip-chars-forward " \t"))
2753 (list 'export-snippet
2754 (list :back-end back-end
2758 :post-blank post-blank
)))))))
2760 (defun org-element-export-snippet-interpreter (export-snippet _
)
2761 "Interpret EXPORT-SNIPPET object as Org syntax."
2763 (org-element-property :back-end export-snippet
)
2764 (org-element-property :value export-snippet
)))
2767 ;;;; Footnote Reference
2769 (defun org-element-footnote-reference-parser ()
2770 "Parse footnote reference at point, if any.
2772 When at a footnote reference, return a list whose car is
2773 `footnote-reference' and cdr a plist with `:label', `:type',
2774 `:begin', `:end', `:content-begin', `:contents-end' and
2775 `:post-blank' as keywords. Otherwise, return nil."
2776 (when (looking-at org-footnote-re
)
2777 (let ((closing (with-syntax-table org-element--pair-square-table
2778 (ignore-errors (scan-lists (point) 1 0)))))
2781 (let* ((begin (point))
2783 (or (org-match-string-no-properties 2)
2784 (org-match-string-no-properties 3)
2785 (and (match-string 1)
2786 (concat "fn:" (org-match-string-no-properties 1)))))
2787 (type (if (or (not label
) (match-string 1)) 'inline
'standard
))
2788 (inner-begin (match-end 0))
2789 (inner-end (1- closing
))
2790 (post-blank (progn (goto-char closing
)
2791 (skip-chars-forward " \t")))
2793 (list 'footnote-reference
2798 :contents-begin
(and (eq type
'inline
) inner-begin
)
2799 :contents-end
(and (eq type
'inline
) inner-end
)
2800 :post-blank post-blank
))))))))
2802 (defun org-element-footnote-reference-interpreter (footnote-reference contents
)
2803 "Interpret FOOTNOTE-REFERENCE object as Org syntax.
2804 CONTENTS is its definition, when inline, or nil."
2806 (concat (or (org-element-property :label footnote-reference
) "fn:")
2807 (and contents
(concat ":" contents
)))))
2810 ;;;; Inline Babel Call
2812 (defun org-element-inline-babel-call-parser ()
2813 "Parse inline babel call at point, if any.
2815 When at an inline babel call, return a list whose car is
2816 `inline-babel-call' and cdr a plist with `:call',
2817 `:inside-header', `:arguments', `:end-header', `:begin', `:end',
2818 `:value' and `:post-blank' as keywords. Otherwise, return nil.
2820 Assume point is at the beginning of the babel call."
2822 (unless (bolp) (backward-char))
2823 (when (let ((case-fold-search t
))
2824 (looking-at org-babel-inline-lob-one-liner-regexp
))
2825 (let ((begin (match-end 1))
2826 (call (org-match-string-no-properties 2))
2827 (inside-header (org-string-nw-p (org-match-string-no-properties 4)))
2828 (arguments (org-string-nw-p (org-match-string-no-properties 6)))
2829 (end-header (org-string-nw-p (org-match-string-no-properties 8)))
2830 (value (buffer-substring-no-properties (match-end 1) (match-end 0)))
2831 (post-blank (progn (goto-char (match-end 0))
2832 (skip-chars-forward " \t")))
2834 (list 'inline-babel-call
2836 :inside-header inside-header
2837 :arguments arguments
2838 :end-header end-header
2842 :post-blank post-blank
))))))
2844 (defun org-element-inline-babel-call-interpreter (inline-babel-call _
)
2845 "Interpret INLINE-BABEL-CALL object as Org syntax."
2847 (org-element-property :call inline-babel-call
)
2848 (let ((h (org-element-property :inside-header inline-babel-call
)))
2849 (and h
(format "[%s]" h
)))
2850 "(" (org-element-property :arguments inline-babel-call
) ")"
2851 (let ((h (org-element-property :end-header inline-babel-call
)))
2852 (and h
(format "[%s]" h
)))))
2855 ;;;; Inline Src Block
2857 (defun org-element-inline-src-block-parser ()
2858 "Parse inline source block at point, if any.
2860 When at an inline source block, return a list whose car is
2861 `inline-src-block' and cdr a plist with `:begin', `:end',
2862 `:language', `:value', `:parameters' and `:post-blank' as
2863 keywords. Otherwise, return nil.
2865 Assume point is at the beginning of the inline src block."
2867 (unless (bolp) (backward-char))
2868 (when (looking-at org-babel-inline-src-block-regexp
)
2869 (let ((begin (match-beginning 1))
2870 (language (org-match-string-no-properties 2))
2871 (parameters (org-match-string-no-properties 4))
2872 (value (org-match-string-no-properties 5))
2873 (post-blank (progn (goto-char (match-end 0))
2874 (skip-chars-forward " \t")))
2876 (list 'inline-src-block
2877 (list :language language
2879 :parameters parameters
2882 :post-blank post-blank
))))))
2884 (defun org-element-inline-src-block-interpreter (inline-src-block _
)
2885 "Interpret INLINE-SRC-BLOCK object as Org syntax."
2886 (let ((language (org-element-property :language inline-src-block
))
2887 (arguments (org-element-property :parameters inline-src-block
))
2888 (body (org-element-property :value inline-src-block
)))
2889 (format "src_%s%s{%s}"
2891 (if arguments
(format "[%s]" arguments
) "")
2896 (defun org-element-italic-parser ()
2897 "Parse italic object at point, if any.
2899 When at an italic object, return a list whose car is `italic' and
2900 cdr is a plist with `:begin', `:end', `:contents-begin' and
2901 `:contents-end' and `:post-blank' keywords. Otherwise, return
2904 Assume point is at the first slash marker."
2906 (unless (bolp) (backward-char 1))
2907 (when (looking-at org-emph-re
)
2908 (let ((begin (match-beginning 2))
2909 (contents-begin (match-beginning 4))
2910 (contents-end (match-end 4))
2911 (post-blank (progn (goto-char (match-end 2))
2912 (skip-chars-forward " \t")))
2917 :contents-begin contents-begin
2918 :contents-end contents-end
2919 :post-blank post-blank
))))))
2921 (defun org-element-italic-interpreter (_ contents
)
2922 "Interpret italic object as Org syntax.
2923 CONTENTS is the contents of the object."
2924 (format "/%s/" contents
))
2929 (defun org-element-latex-fragment-parser ()
2930 "Parse LaTeX fragment at point, if any.
2932 When at a LaTeX fragment, return a list whose car is
2933 `latex-fragment' and cdr a plist with `:value', `:begin', `:end',
2934 and `:post-blank' as keywords. Otherwise, return nil.
2936 Assume point is at the beginning of the LaTeX fragment."
2939 (let* ((begin (point))
2941 (if (eq (char-after) ?$
)
2942 (if (eq (char-after (1+ (point))) ?$
)
2943 (search-forward "$$" nil t
2)
2944 (and (not (eq (char-before) ?$
))
2945 (search-forward "$" nil t
2)
2946 (not (memq (char-before (match-beginning 0))
2947 '(?\s ?
\t ?
\n ?
, ?.
)))
2948 (looking-at "\\(\\s.\\|\\s-\\|\\s(\\|\\s)\\|\\s\"\\|$\\)")
2950 (pcase (char-after (1+ (point)))
2951 (?\
( (search-forward "\\)" nil t
))
2952 (?\
[ (search-forward "\\]" nil t
))
2955 (and (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\
2956 \\|\\({[^{}\n]*}\\)\\)*")
2958 (post-blank (if (not after-fragment
) (throw 'no-object nil
)
2959 (goto-char after-fragment
)
2960 (skip-chars-forward " \t")))
2962 (list 'latex-fragment
2963 (list :value
(buffer-substring-no-properties begin after-fragment
)
2966 :post-blank post-blank
))))))
2968 (defun org-element-latex-fragment-interpreter (latex-fragment _
)
2969 "Interpret LATEX-FRAGMENT object as Org syntax."
2970 (org-element-property :value latex-fragment
))
2974 (defun org-element-line-break-parser ()
2975 "Parse line break at point, if any.
2977 When at a line break, return a list whose car is `line-break',
2978 and cdr a plist with `:begin', `:end' and `:post-blank' keywords.
2979 Otherwise, return nil.
2981 Assume point is at the beginning of the line break."
2982 (when (and (org-looking-at-p "\\\\\\\\[ \t]*$")
2983 (not (eq (char-before) ?
\\)))
2985 (list :begin
(point)
2986 :end
(line-beginning-position 2)
2989 (defun org-element-line-break-interpreter (&rest _
)
2990 "Interpret LINE-BREAK object as Org syntax."
2996 (defun org-element-link-parser ()
2997 "Parse link at point, if any.
2999 When at a link, return a list whose car is `link' and cdr a plist
3000 with `:type', `:path', `:raw-link', `:application',
3001 `:search-option', `:begin', `:end', `:contents-begin',
3002 `:contents-end' and `:post-blank' as keywords. Otherwise, return
3005 Assume point is at the beginning of the link."
3007 (let ((begin (point))
3008 end contents-begin contents-end link-end post-blank path type
3009 raw-link search-option application
)
3011 ;; Type 1: Text targeted from a radio target.
3012 ((and org-target-link-regexp
3013 (save-excursion (or (bolp) (backward-char))
3014 (looking-at org-target-link-regexp
)))
3016 link-end
(match-end 1)
3017 path
(org-match-string-no-properties 1)
3018 contents-begin
(match-beginning 1)
3019 contents-end
(match-end 1)))
3020 ;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]]
3021 ((looking-at org-bracket-link-regexp
)
3022 (setq contents-begin
(match-beginning 3))
3023 (setq contents-end
(match-end 3))
3024 (setq link-end
(match-end 0))
3025 ;; RAW-LINK is the original link. Expand any
3026 ;; abbreviation in it.
3028 ;; Also treat any newline character and associated
3029 ;; indentation as a single space character. This is not
3030 ;; compatible with RFC 3986, which requires to ignore
3031 ;; them altogether. However, doing so would require
3032 ;; users to encode spaces on the fly when writing links
3033 ;; (e.g., insert [[shell:ls%20*.org]] instead of
3034 ;; [[shell:ls *.org]], which defeats Org's focus on
3036 (setq raw-link
(org-link-expand-abbrev
3037 (replace-regexp-in-string
3038 "[ \t]*\n[ \t]*" " "
3039 (org-match-string-no-properties 1))))
3040 ;; Determine TYPE of link and set PATH accordingly. According
3041 ;; to RFC 3986, remove whitespaces from URI in external links.
3042 ;; In internal ones, treat indentation as a single space.
3045 ((or (file-name-absolute-p raw-link
)
3046 (string-match "\\`\\.\\.?/" raw-link
))
3048 (setq path raw-link
))
3049 ;; Explicit type (http, irc, bbdb...). See `org-link-types'.
3050 ((string-match org-link-types-re raw-link
)
3051 (setq type
(match-string 1 raw-link
))
3052 (setq path
(substring raw-link
(match-end 0))))
3053 ;; Id type: PATH is the id.
3054 ((string-match "\\`id:\\([-a-f0-9]+\\)\\'" raw-link
)
3055 (setq type
"id" path
(match-string 1 raw-link
)))
3056 ;; Code-ref type: PATH is the name of the reference.
3057 ((and (org-string-match-p "\\`(" raw-link
)
3058 (org-string-match-p ")\\'" raw-link
))
3059 (setq type
"coderef")
3060 (setq path
(substring raw-link
1 -
1)))
3061 ;; Custom-id type: PATH is the name of the custom id.
3062 ((= (string-to-char raw-link
) ?
#)
3063 (setq type
"custom-id")
3064 (setq path
(substring raw-link
1)))
3065 ;; Fuzzy type: Internal link either matches a target, an
3066 ;; headline name or nothing. PATH is the target or
3070 (setq path raw-link
))))
3071 ;; Type 3: Plain link, e.g., http://orgmode.org
3072 ((looking-at org-plain-link-re
)
3073 (setq raw-link
(org-match-string-no-properties 0)
3074 type
(org-match-string-no-properties 1)
3075 link-end
(match-end 0)
3076 path
(org-match-string-no-properties 2)))
3077 ;; Type 4: Angular link, e.g., <http://orgmode.org>. Unlike to
3078 ;; bracket links, follow RFC 3986 and remove any extra
3079 ;; whitespace in URI.
3080 ((looking-at org-angle-link-re
)
3081 (setq type
(org-match-string-no-properties 1))
3082 (setq link-end
(match-end 0))
3084 (buffer-substring-no-properties
3085 (match-beginning 1) (match-end 2)))
3086 (setq path
(replace-regexp-in-string
3087 "[ \t]*\n[ \t]*" "" (org-match-string-no-properties 2))))
3088 (t (throw 'no-object nil
)))
3089 ;; In any case, deduce end point after trailing white space from
3090 ;; LINK-END variable.
3093 (progn (goto-char link-end
) (skip-chars-forward " \t")))
3095 ;; Special "file" type link processing. Extract opening
3096 ;; application and search option, if any. Also normalize URI.
3097 (when (string-match "\\`file\\(?:\\+\\(.+\\)\\)?\\'" type
)
3098 (setq application
(match-string 1 type
) type
"file")
3099 (when (string-match "::\\(.*\\)\\'" path
)
3100 (setq search-option
(match-string 1 path
))
3101 (setq path
(replace-match "" nil nil path
)))
3102 (setq path
(replace-regexp-in-string "\\`/+" "/" path
)))
3103 ;; Translate link, if `org-link-translation-function' is set.
3104 (let ((trans (and (functionp org-link-translation-function
)
3105 (funcall org-link-translation-function type path
))))
3107 (setq type
(car trans
))
3108 (setq path
(cdr trans
))))
3112 :raw-link
(or raw-link path
)
3113 :application application
3114 :search-option search-option
3117 :contents-begin contents-begin
3118 :contents-end contents-end
3119 :post-blank post-blank
)))))
3121 (defun org-element-link-interpreter (link contents
)
3122 "Interpret LINK object as Org syntax.
3123 CONTENTS is the contents of the object, or nil."
3124 (let ((type (org-element-property :type link
))
3125 (path (org-element-property :path link
)))
3126 (if (string= type
"radio") path
3128 (cond ((string= type
"coderef") (format "(%s)" path
))
3129 ((string= type
"custom-id") (concat "#" path
))
3130 ((string= type
"file")
3131 (let ((app (org-element-property :application link
))
3132 (opt (org-element-property :search-option link
)))
3133 (concat type
(and app
(concat "+" app
)) ":"
3135 (and opt
(concat "::" opt
)))))
3136 ((string= type
"fuzzy") path
)
3137 (t (concat type
":" path
)))
3138 (if contents
(format "[%s]" contents
) "")))))
3143 (defun org-element-macro-parser ()
3144 "Parse macro at point, if any.
3146 When at a macro, return a list whose car is `macro' and cdr
3147 a plist with `:key', `:args', `:begin', `:end', `:value' and
3148 `:post-blank' as keywords. Otherwise, return nil.
3150 Assume point is at the macro."
3152 (when (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
3153 (let ((begin (point))
3154 (key (downcase (org-match-string-no-properties 1)))
3155 (value (org-match-string-no-properties 0))
3156 (post-blank (progn (goto-char (match-end 0))
3157 (skip-chars-forward " \t")))
3159 (args (let ((args (org-match-string-no-properties 3)))
3160 (and args
(org-macro-extract-arguments args
)))))
3167 :post-blank post-blank
))))))
3169 (defun org-element-macro-interpreter (macro _
)
3170 "Interpret MACRO object as Org syntax."
3171 (org-element-property :value macro
))
3176 (defun org-element-radio-target-parser ()
3177 "Parse radio target at point, if any.
3179 When at a radio target, return a list whose car is `radio-target'
3180 and cdr a plist with `:begin', `:end', `:contents-begin',
3181 `:contents-end', `:value' and `:post-blank' as keywords.
3182 Otherwise, return nil.
3184 Assume point is at the radio target."
3186 (when (looking-at org-radio-target-regexp
)
3187 (let ((begin (point))
3188 (contents-begin (match-beginning 1))
3189 (contents-end (match-end 1))
3190 (value (org-match-string-no-properties 1))
3191 (post-blank (progn (goto-char (match-end 0))
3192 (skip-chars-forward " \t")))
3197 :contents-begin contents-begin
3198 :contents-end contents-end
3199 :post-blank post-blank
3202 (defun org-element-radio-target-interpreter (_ contents
)
3203 "Interpret target object as Org syntax.
3204 CONTENTS is the contents of the object."
3205 (concat "<<<" contents
">>>"))
3208 ;;;; Statistics Cookie
3210 (defun org-element-statistics-cookie-parser ()
3211 "Parse statistics cookie at point, if any.
3213 When at a statistics cookie, return a list whose car is
3214 `statistics-cookie', and cdr a plist with `:begin', `:end',
3215 `:value' and `:post-blank' keywords. Otherwise, return nil.
3217 Assume point is at the beginning of the statistics-cookie."
3219 (when (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
3220 (let* ((begin (point))
3221 (value (buffer-substring-no-properties
3222 (match-beginning 0) (match-end 0)))
3223 (post-blank (progn (goto-char (match-end 0))
3224 (skip-chars-forward " \t")))
3226 (list 'statistics-cookie
3230 :post-blank post-blank
))))))
3232 (defun org-element-statistics-cookie-interpreter (statistics-cookie _
)
3233 "Interpret STATISTICS-COOKIE object as Org syntax."
3234 (org-element-property :value statistics-cookie
))
3239 (defun org-element-strike-through-parser ()
3240 "Parse strike-through object at point, if any.
3242 When at a strike-through object, return a list whose car is
3243 `strike-through' and cdr is a plist with `:begin', `:end',
3244 `:contents-begin' and `:contents-end' and `:post-blank' keywords.
3245 Otherwise, return nil.
3247 Assume point is at the first plus sign marker."
3249 (unless (bolp) (backward-char 1))
3250 (when (looking-at org-emph-re
)
3251 (let ((begin (match-beginning 2))
3252 (contents-begin (match-beginning 4))
3253 (contents-end (match-end 4))
3254 (post-blank (progn (goto-char (match-end 2))
3255 (skip-chars-forward " \t")))
3257 (list 'strike-through
3260 :contents-begin contents-begin
3261 :contents-end contents-end
3262 :post-blank post-blank
))))))
3264 (defun org-element-strike-through-interpreter (_ contents
)
3265 "Interpret strike-through object as Org syntax.
3266 CONTENTS is the contents of the object."
3267 (format "+%s+" contents
))
3272 (defun org-element-subscript-parser ()
3273 "Parse subscript at point, if any.
3275 When at a subscript object, return a list whose car is
3276 `subscript' and cdr a plist with `:begin', `:end',
3277 `:contents-begin', `:contents-end', `:use-brackets-p' and
3278 `:post-blank' as keywords. Otherwise, return nil.
3280 Assume point is at the underscore."
3282 (unless (bolp) (backward-char))
3283 (when (looking-at org-match-substring-regexp
)
3284 (let ((bracketsp (match-beginning 4))
3285 (begin (match-beginning 2))
3286 (contents-begin (or (match-beginning 4)
3287 (match-beginning 3)))
3288 (contents-end (or (match-end 4) (match-end 3)))
3289 (post-blank (progn (goto-char (match-end 0))
3290 (skip-chars-forward " \t")))
3295 :use-brackets-p bracketsp
3296 :contents-begin contents-begin
3297 :contents-end contents-end
3298 :post-blank post-blank
))))))
3300 (defun org-element-subscript-interpreter (subscript contents
)
3301 "Interpret SUBSCRIPT object as Org syntax.
3302 CONTENTS is the contents of the object."
3304 (if (org-element-property :use-brackets-p subscript
) "_{%s}" "_%s")
3310 (defun org-element-superscript-parser ()
3311 "Parse superscript at point, if any.
3313 When at a superscript object, return a list whose car is
3314 `superscript' and cdr a plist with `:begin', `:end',
3315 `:contents-begin', `:contents-end', `:use-brackets-p' and
3316 `:post-blank' as keywords. Otherwise, return nil.
3318 Assume point is at the caret."
3320 (unless (bolp) (backward-char))
3321 (when (looking-at org-match-substring-regexp
)
3322 (let ((bracketsp (match-beginning 4))
3323 (begin (match-beginning 2))
3324 (contents-begin (or (match-beginning 4)
3325 (match-beginning 3)))
3326 (contents-end (or (match-end 4) (match-end 3)))
3327 (post-blank (progn (goto-char (match-end 0))
3328 (skip-chars-forward " \t")))
3333 :use-brackets-p bracketsp
3334 :contents-begin contents-begin
3335 :contents-end contents-end
3336 :post-blank post-blank
))))))
3338 (defun org-element-superscript-interpreter (superscript contents
)
3339 "Interpret SUPERSCRIPT object as Org syntax.
3340 CONTENTS is the contents of the object."
3342 (if (org-element-property :use-brackets-p superscript
) "^{%s}" "^%s")
3348 (defun org-element-table-cell-parser ()
3349 "Parse table cell at point.
3350 Return a list whose car is `table-cell' and cdr is a plist
3351 containing `:begin', `:end', `:contents-begin', `:contents-end'
3352 and `:post-blank' keywords."
3353 (looking-at "[ \t]*\\(.*?\\)[ \t]*\\(?:|\\|$\\)")
3354 (let* ((begin (match-beginning 0))
3356 (contents-begin (match-beginning 1))
3357 (contents-end (match-end 1)))
3361 :contents-begin contents-begin
3362 :contents-end contents-end
3365 (defun org-element-table-cell-interpreter (_ contents
)
3366 "Interpret table-cell element as Org syntax.
3367 CONTENTS is the contents of the cell, or nil."
3368 (concat " " contents
" |"))
3373 (defun org-element-target-parser ()
3374 "Parse target at point, if any.
3376 When at a target, return a list whose car is `target' and cdr
3377 a plist with `:begin', `:end', `:value' and `:post-blank' as
3378 keywords. Otherwise, return nil.
3380 Assume point is at the target."
3382 (when (looking-at org-target-regexp
)
3383 (let ((begin (point))
3384 (value (org-match-string-no-properties 1))
3385 (post-blank (progn (goto-char (match-end 0))
3386 (skip-chars-forward " \t")))
3392 :post-blank post-blank
))))))
3394 (defun org-element-target-interpreter (target _
)
3395 "Interpret TARGET object as Org syntax."
3396 (format "<<%s>>" (org-element-property :value target
)))
3401 (defconst org-element--timestamp-regexp
3402 (concat org-ts-regexp-both
3404 "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
3406 "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
3407 "Regexp matching any timestamp type object.")
3409 (defun org-element-timestamp-parser ()
3410 "Parse time stamp at point, if any.
3412 When at a time stamp, return a list whose car is `timestamp', and
3413 cdr a plist with `:type', `:raw-value', `:year-start',
3414 `:month-start', `:day-start', `:hour-start', `:minute-start',
3415 `:year-end', `:month-end', `:day-end', `:hour-end',
3416 `:minute-end', `:repeater-type', `:repeater-value',
3417 `:repeater-unit', `:warning-type', `:warning-value',
3418 `:warning-unit', `:begin', `:end' and `:post-blank' keywords.
3419 Otherwise, return nil.
3421 Assume point is at the beginning of the timestamp."
3422 (when (org-looking-at-p org-element--timestamp-regexp
)
3424 (let* ((begin (point))
3425 (activep (eq (char-after) ?
<))
3428 (looking-at "\\([<[]\\(%%\\)?.*?\\)[]>]\\(?:--\\([<[].*?[]>]\\)\\)?")
3429 (match-string-no-properties 0)))
3430 (date-start (match-string-no-properties 1))
3431 (date-end (match-string 3))
3432 (diaryp (match-beginning 2))
3433 (post-blank (progn (goto-char (match-end 0))
3434 (skip-chars-forward " \t")))
3439 "[012]?[0-9]:[0-5][0-9]\\(-\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)"
3441 (cons (string-to-number (match-string 2 date-start
))
3442 (string-to-number (match-string 3 date-start
)))))
3443 (type (cond (diaryp 'diary
)
3444 ((and activep
(or date-end time-range
)) 'active-range
)
3446 ((or date-end time-range
) 'inactive-range
)
3450 (string-match "\\([.+]?\\+\\)\\([0-9]+\\)\\([hdwmy]\\)"
3454 (let ((type (match-string 1 raw-value
)))
3455 (cond ((equal "++" type
) 'catch-up
)
3456 ((equal ".+" type
) 'restart
)
3458 :repeater-value
(string-to-number (match-string 2 raw-value
))
3460 (pcase (string-to-char (match-string 3 raw-value
))
3461 (?h
'hour
) (?d
'day
) (?w
'week
) (?m
'month
) (_ 'year
)))))
3464 (string-match "\\(-\\)?-\\([0-9]+\\)\\([hdwmy]\\)" raw-value
)
3466 :warning-type
(if (match-string 1 raw-value
) 'first
'all
)
3467 :warning-value
(string-to-number (match-string 2 raw-value
))
3469 (pcase (string-to-char (match-string 3 raw-value
))
3470 (?h
'hour
) (?d
'day
) (?w
'week
) (?m
'month
) (_ 'year
)))))
3471 year-start month-start day-start hour-start minute-start year-end
3472 month-end day-end hour-end minute-end
)
3473 ;; Parse date-start.
3475 (let ((date (org-parse-time-string date-start t
)))
3476 (setq year-start
(nth 5 date
)
3477 month-start
(nth 4 date
)
3478 day-start
(nth 3 date
)
3479 hour-start
(nth 2 date
)
3480 minute-start
(nth 1 date
))))
3481 ;; Compute date-end. It can be provided directly in time-stamp,
3482 ;; or extracted from time range. Otherwise, it defaults to the
3483 ;; same values as date-start.
3485 (let ((date (and date-end
(org-parse-time-string date-end t
))))
3486 (setq year-end
(or (nth 5 date
) year-start
)
3487 month-end
(or (nth 4 date
) month-start
)
3488 day-end
(or (nth 3 date
) day-start
)
3489 hour-end
(or (nth 2 date
) (car time-range
) hour-start
)
3490 minute-end
(or (nth 1 date
) (cdr time-range
) minute-start
))))
3492 (nconc (list :type type
3493 :raw-value raw-value
3494 :year-start year-start
3495 :month-start month-start
3496 :day-start day-start
3497 :hour-start hour-start
3498 :minute-start minute-start
3500 :month-end month-end
3503 :minute-end minute-end
3506 :post-blank post-blank
)
3510 (defun org-element-timestamp-interpreter (timestamp _
)
3511 "Interpret TIMESTAMP object as Org syntax."
3512 (let* ((repeat-string
3514 (pcase (org-element-property :repeater-type timestamp
)
3515 (`cumulate
"+") (`catch-up
"++") (`restart
".+"))
3516 (let ((val (org-element-property :repeater-value timestamp
)))
3517 (and val
(number-to-string val
)))
3518 (pcase (org-element-property :repeater-unit timestamp
)
3519 (`hour
"h") (`day
"d") (`week
"w") (`month
"m") (`year
"y"))))
3522 (pcase (org-element-property :warning-type timestamp
)
3523 (`first
"--") (`all
"-"))
3524 (let ((val (org-element-property :warning-value timestamp
)))
3525 (and val
(number-to-string val
)))
3526 (pcase (org-element-property :warning-unit timestamp
)
3527 (`hour
"h") (`day
"d") (`week
"w") (`month
"m") (`year
"y"))))
3529 ;; Build an Org timestamp string from TIME. ACTIVEP is
3530 ;; non-nil when time stamp is active. If WITH-TIME-P is
3531 ;; non-nil, add a time part. HOUR-END and MINUTE-END
3532 ;; specify a time range in the timestamp. REPEAT-STRING is
3533 ;; the repeater string, if any.
3534 (lambda (time activep
&optional with-time-p hour-end minute-end
)
3535 (let ((ts (format-time-string
3536 (funcall (if with-time-p
#'cdr
#'car
)
3537 org-time-stamp-formats
)
3539 (when (and hour-end minute-end
)
3540 (string-match "[012]?[0-9]:[0-5][0-9]" ts
)
3543 (format "\\&-%02d:%02d" hour-end minute-end
)
3545 (unless activep
(setq ts
(format "[%s]" (substring ts
1 -
1))))
3546 (dolist (s (list repeat-string warning-string
))
3547 (when (org-string-nw-p s
)
3548 (setq ts
(concat (substring ts
0 -
1)
3551 (substring ts -
1)))))
3554 (type (org-element-property :type timestamp
)))
3556 ((or `active
`inactive
)
3557 (let* ((minute-start (org-element-property :minute-start timestamp
))
3558 (minute-end (org-element-property :minute-end timestamp
))
3559 (hour-start (org-element-property :hour-start timestamp
))
3560 (hour-end (org-element-property :hour-end timestamp
))
3561 (time-range-p (and hour-start hour-end minute-start minute-end
3562 (or (/= hour-start hour-end
)
3563 (/= minute-start minute-end
)))))
3569 (org-element-property :day-start timestamp
)
3570 (org-element-property :month-start timestamp
)
3571 (org-element-property :year-start timestamp
))
3573 (and hour-start minute-start
)
3574 (and time-range-p hour-end
)
3575 (and time-range-p minute-end
))))
3576 ((or `active-range
`inactive-range
)
3577 (let ((minute-start (org-element-property :minute-start timestamp
))
3578 (minute-end (org-element-property :minute-end timestamp
))
3579 (hour-start (org-element-property :hour-start timestamp
))
3580 (hour-end (org-element-property :hour-end timestamp
)))
3583 build-ts-string
(encode-time
3587 (org-element-property :day-start timestamp
)
3588 (org-element-property :month-start timestamp
)
3589 (org-element-property :year-start timestamp
))
3590 (eq type
'active-range
)
3591 (and hour-start minute-start
))
3593 (funcall build-ts-string
3597 (org-element-property :day-end timestamp
)
3598 (org-element-property :month-end timestamp
)
3599 (org-element-property :year-end timestamp
))
3600 (eq type
'active-range
)
3601 (and hour-end minute-end
)))))
3602 (_ (org-element-property :raw-value timestamp
)))))
3607 (defun org-element-underline-parser ()
3608 "Parse underline object at point, if any.
3610 When at an underline object, return a list whose car is
3611 `underline' and cdr is a plist with `:begin', `:end',
3612 `:contents-begin' and `:contents-end' and `:post-blank' keywords.
3613 Otherwise, return nil.
3615 Assume point is at the first underscore marker."
3617 (unless (bolp) (backward-char 1))
3618 (when (looking-at org-emph-re
)
3619 (let ((begin (match-beginning 2))
3620 (contents-begin (match-beginning 4))
3621 (contents-end (match-end 4))
3622 (post-blank (progn (goto-char (match-end 2))
3623 (skip-chars-forward " \t")))
3628 :contents-begin contents-begin
3629 :contents-end contents-end
3630 :post-blank post-blank
))))))
3632 (defun org-element-underline-interpreter (_ contents
)
3633 "Interpret underline object as Org syntax.
3634 CONTENTS is the contents of the object."
3635 (format "_%s_" contents
))
3640 (defun org-element-verbatim-parser ()
3641 "Parse verbatim object at point, if any.
3643 When at a verbatim object, return a list whose car is `verbatim'
3644 and cdr is a plist with `:value', `:begin', `:end' and
3645 `:post-blank' keywords. Otherwise, return nil.
3647 Assume point is at the first equal sign marker."
3649 (unless (bolp) (backward-char 1))
3650 (when (looking-at org-emph-re
)
3651 (let ((begin (match-beginning 2))
3652 (value (org-match-string-no-properties 4))
3653 (post-blank (progn (goto-char (match-end 2))
3654 (skip-chars-forward " \t")))
3660 :post-blank post-blank
))))))
3662 (defun org-element-verbatim-interpreter (verbatim _
)
3663 "Interpret VERBATIM object as Org syntax."
3664 (format "=%s=" (org-element-property :value verbatim
)))
3668 ;;; Parsing Element Starting At Point
3670 ;; `org-element--current-element' is the core function of this section.
3671 ;; It returns the Lisp representation of the element starting at
3674 ;; `org-element--current-element' makes use of special modes. They
3675 ;; are activated for fixed element chaining (e.g., `plain-list' >
3676 ;; `item') or fixed conditional element chaining (e.g., `headline' >
3677 ;; `section'). Special modes are: `first-section', `item',
3678 ;; `node-property', `section' and `table-row'.
3680 (defun org-element--current-element (limit &optional granularity mode structure
)
3681 "Parse the element starting at point.
3683 Return value is a list like (TYPE PROPS) where TYPE is the type
3684 of the element and PROPS a plist of properties associated to the
3687 Possible types are defined in `org-element-all-elements'.
3689 LIMIT bounds the search.
3691 Optional argument GRANULARITY determines the depth of the
3692 recursion. Allowed values are `headline', `greater-element',
3693 `element', `object' or nil. When it is broader than `object' (or
3694 nil), secondary values will not be parsed, since they only
3697 Optional argument MODE, when non-nil, can be either
3698 `first-section', `section', `planning', `item', `node-property'
3701 If STRUCTURE isn't provided but MODE is set to `item', it will be
3704 This function assumes point is always at the beginning of the
3705 element it has to parse."
3707 (let ((case-fold-search t
)
3708 ;; Determine if parsing depth allows for secondary strings
3709 ;; parsing. It only applies to elements referenced in
3710 ;; `org-element-secondary-value-alist'.
3711 (raw-secondary-p (and granularity
(not (eq granularity
'object
)))))
3715 (org-element-item-parser limit structure raw-secondary-p
))
3717 ((eq mode
'table-row
) (org-element-table-row-parser limit
))
3719 ((eq mode
'node-property
) (org-element-node-property-parser limit
))
3721 ((org-with-limited-levels (org-at-heading-p))
3722 (org-element-headline-parser limit raw-secondary-p
))
3723 ;; Sections (must be checked after headline).
3724 ((eq mode
'section
) (org-element-section-parser limit
))
3725 ((eq mode
'first-section
)
3726 (org-element-section-parser
3727 (or (save-excursion (org-with-limited-levels (outline-next-heading)))
3730 ((and (eq mode
'planning
) (looking-at org-planning-line-re
))
3731 (org-element-planning-parser limit
))
3733 ((and (memq mode
'(planning property-drawer
))
3734 (looking-at org-property-drawer-re
))
3735 (org-element-property-drawer-parser limit
))
3736 ;; When not at bol, point is at the beginning of an item or
3737 ;; a footnote definition: next item is always a paragraph.
3738 ((not (bolp)) (org-element-paragraph-parser limit
(list (point))))
3740 ((looking-at org-clock-line-re
) (org-element-clock-parser limit
))
3743 (org-element-inlinetask-parser limit raw-secondary-p
))
3744 ;; From there, elements can have affiliated keywords.
3745 (t (let ((affiliated (org-element--collect-affiliated-keywords limit
)))
3747 ;; Jumping over affiliated keywords put point off-limits.
3748 ;; Parse them as regular keywords.
3749 ((and (cdr affiliated
) (>= (point) limit
))
3750 (goto-char (car affiliated
))
3751 (org-element-keyword-parser limit nil
))
3752 ;; LaTeX Environment.
3753 ((looking-at org-element--latex-begin-environment
)
3754 (org-element-latex-environment-parser limit affiliated
))
3755 ;; Drawer and Property Drawer.
3756 ((looking-at org-drawer-regexp
)
3757 (org-element-drawer-parser limit affiliated
))
3759 ((looking-at "[ \t]*:\\( \\|$\\)")
3760 (org-element-fixed-width-parser limit affiliated
))
3761 ;; Inline Comments, Blocks, Babel Calls, Dynamic Blocks and
3763 ((looking-at "[ \t]*#")
3764 (goto-char (match-end 0))
3765 (cond ((looking-at "\\(?: \\|$\\)")
3767 (org-element-comment-parser limit affiliated
))
3768 ((looking-at "\\+BEGIN_\\(\\S-+\\)")
3770 (let ((parser (assoc (upcase (match-string 1))
3771 org-element-block-name-alist
)))
3772 (if parser
(funcall (cdr parser
) limit affiliated
)
3773 (org-element-special-block-parser limit affiliated
))))
3774 ((looking-at "\\+CALL:")
3776 (org-element-babel-call-parser limit affiliated
))
3777 ((looking-at "\\+BEGIN:? ")
3779 (org-element-dynamic-block-parser limit affiliated
))
3780 ((looking-at "\\+\\S-+:")
3782 (org-element-keyword-parser limit affiliated
))
3785 (org-element-paragraph-parser limit affiliated
))))
3786 ;; Footnote Definition.
3787 ((looking-at org-footnote-definition-re
)
3788 (org-element-footnote-definition-parser limit affiliated
))
3790 ((looking-at "[ \t]*-\\{5,\\}[ \t]*$")
3791 (org-element-horizontal-rule-parser limit affiliated
))
3794 (org-element-diary-sexp-parser limit affiliated
))
3796 ((looking-at "[ \t]*\\(|\\|\\+\\(-+\\+\\)+[ \t]*$\\)")
3797 (org-element-table-parser limit affiliated
))
3799 ((looking-at (org-item-re))
3800 (org-element-plain-list-parser
3802 (or structure
(org-element--list-struct limit
))))
3803 ;; Default element: Paragraph.
3804 (t (org-element-paragraph-parser limit affiliated
)))))))))
3807 ;; Most elements can have affiliated keywords. When looking for an
3808 ;; element beginning, we want to move before them, as they belong to
3809 ;; that element, and, in the meantime, collect information they give
3810 ;; into appropriate properties. Hence the following function.
3812 (defun org-element--collect-affiliated-keywords (limit)
3813 "Collect affiliated keywords from point down to LIMIT.
3815 Return a list whose CAR is the position at the first of them and
3816 CDR a plist of keywords and values and move point to the
3817 beginning of the first line after them.
3819 As a special case, if element doesn't start at the beginning of
3820 the line (e.g., a paragraph starting an item), CAR is current
3821 position of point and CDR is nil."
3822 (if (not (bolp)) (list (point))
3823 (let ((case-fold-search t
)
3825 ;; RESTRICT is the list of objects allowed in parsed
3827 (restrict (org-element-restriction 'keyword
))
3829 (while (and (< (point) limit
) (looking-at org-element--affiliated-re
))
3830 (let* ((raw-kwd (upcase (match-string 1)))
3831 ;; Apply translation to RAW-KWD. From there, KWD is
3832 ;; the official keyword.
3833 (kwd (or (cdr (assoc raw-kwd
3834 org-element-keyword-translation-alist
))
3836 ;; Find main value for any keyword.
3840 (buffer-substring-no-properties
3841 (match-end 0) (line-end-position)))))
3842 ;; PARSEDP is non-nil when keyword should have its
3844 (parsedp (member kwd org-element-parsed-keywords
))
3845 ;; If KWD is a dual keyword, find its secondary
3846 ;; value. Maybe parse it.
3847 (dualp (member kwd org-element-dual-keywords
))
3850 (let ((sec (org-match-string-no-properties 2)))
3851 (if (or (not sec
) (not parsedp
)) sec
3852 (org-element--parse-objects
3853 (match-beginning 2) (match-end 2) nil restrict
)))))
3854 ;; Attribute a property name to KWD.
3855 (kwd-sym (and kwd
(intern (concat ":" (downcase kwd
))))))
3856 ;; Now set final shape for VALUE.
3859 (org-element--parse-objects
3861 (progn (end-of-line) (skip-chars-backward " \t") (point))
3864 (setq value
(and (or value dual-value
) (cons value dual-value
))))
3865 (when (or (member kwd org-element-multiple-keywords
)
3866 ;; Attributes can always appear on multiple lines.
3867 (string-match "^ATTR_" kwd
))
3868 (setq value
(cons value
(plist-get output kwd-sym
))))
3869 ;; Eventually store the new value in OUTPUT.
3870 (setq output
(plist-put output kwd-sym value
))
3871 ;; Move to next keyword.
3873 ;; If affiliated keywords are orphaned: move back to first one.
3874 ;; They will be parsed as a paragraph.
3875 (when (looking-at "[ \t]*$") (goto-char origin
) (setq output nil
))
3877 (cons origin output
))))
3883 ;; The two major functions here are `org-element-parse-buffer', which
3884 ;; parses Org syntax inside the current buffer, taking into account
3885 ;; region, narrowing, or even visibility if specified, and
3886 ;; `org-element-parse-secondary-string', which parses objects within
3889 ;; The (almost) almighty `org-element-map' allows to apply a function
3890 ;; on elements or objects matching some type, and accumulate the
3891 ;; resulting values. In an export situation, it also skips unneeded
3892 ;; parts of the parse tree.
3894 (defun org-element-parse-buffer (&optional granularity visible-only
)
3895 "Recursively parse the buffer and return structure.
3896 If narrowing is in effect, only parse the visible part of the
3899 Optional argument GRANULARITY determines the depth of the
3900 recursion. It can be set to the following symbols:
3902 `headline' Only parse headlines.
3903 `greater-element' Don't recurse into greater elements excepted
3904 headlines and sections. Thus, elements
3905 parsed are the top-level ones.
3906 `element' Parse everything but objects and plain text.
3907 `object' Parse the complete buffer (default).
3909 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3912 An element or an objects is represented as a list with the
3913 pattern (TYPE PROPERTIES CONTENTS), where :
3915 TYPE is a symbol describing the element or object. See
3916 `org-element-all-elements' and `org-element-all-objects' for an
3917 exhaustive list of such symbols. One can retrieve it with
3918 `org-element-type' function.
3920 PROPERTIES is the list of attributes attached to the element or
3921 object, as a plist. Although most of them are specific to the
3922 element or object type, all types share `:begin', `:end',
3923 `:post-blank' and `:parent' properties, which respectively
3924 refer to buffer position where the element or object starts,
3925 ends, the number of white spaces or blank lines after it, and
3926 the element or object containing it. Properties values can be
3927 obtained by using `org-element-property' function.
3929 CONTENTS is a list of elements, objects or raw strings
3930 contained in the current element or object, when applicable.
3931 One can access them with `org-element-contents' function.
3933 The Org buffer has `org-data' as type and nil as properties.
3934 `org-element-map' function can be used to find specific elements
3935 or objects within the parse tree.
3937 This function assumes that current major mode is `org-mode'."
3939 (goto-char (point-min))
3940 (org-skip-whitespace)
3941 (org-element--parse-elements
3942 (point-at-bol) (point-max)
3943 ;; Start in `first-section' mode so text before the first
3944 ;; headline belongs to a section.
3945 'first-section nil granularity visible-only
(list 'org-data nil
))))
3947 (defun org-element-parse-secondary-string (string restriction
&optional parent
)
3948 "Recursively parse objects in STRING and return structure.
3950 RESTRICTION is a symbol limiting the object types that will be
3953 Optional argument PARENT, when non-nil, is the element or object
3954 containing the secondary string. It is used to set correctly
3955 `:parent' property within the string.
3957 If STRING is the empty string or nil, return nil."
3960 ((equal string
"") nil
)
3961 (t (let ((local-variables (buffer-local-variables)))
3963 (dolist (v local-variables
)
3965 (if (symbolp v
) (makunbound v
)
3966 (org-set-local (car v
) (cdr v
)))))
3968 (restore-buffer-modified-p nil
)
3969 (let ((data (org-element--parse-objects
3970 (point-min) (point-max) nil restriction
)))
3972 (dolist (o data
) (org-element-put-property o
:parent parent
)))
3975 (defun org-element-map
3976 (data types fun
&optional info first-match no-recursion with-affiliated
)
3977 "Map a function on selected elements or objects.
3979 DATA is a parse tree, an element, an object, a string, or a list
3980 of such constructs. TYPES is a symbol or list of symbols of
3981 elements or objects types (see `org-element-all-elements' and
3982 `org-element-all-objects' for a complete list of types). FUN is
3983 the function called on the matching element or object. It has to
3984 accept one argument: the element or object itself.
3986 When optional argument INFO is non-nil, it should be a plist
3987 holding export options. In that case, parts of the parse tree
3988 not exportable according to that property list will be skipped.
3990 When optional argument FIRST-MATCH is non-nil, stop at the first
3991 match for which FUN doesn't return nil, and return that value.
3993 Optional argument NO-RECURSION is a symbol or a list of symbols
3994 representing elements or objects types. `org-element-map' won't
3995 enter any recursive element or object whose type belongs to that
3996 list. Though, FUN can still be applied on them.
3998 When optional argument WITH-AFFILIATED is non-nil, FUN will also
3999 apply to matching objects within parsed affiliated keywords (see
4000 `org-element-parsed-keywords').
4002 Nil values returned from FUN do not appear in the results.
4008 Assuming TREE is a variable containing an Org buffer parse tree,
4009 the following example will return a flat list of all `src-block'
4010 and `example-block' elements in it:
4012 \(org-element-map tree '(example-block src-block) #'identity)
4014 The following snippet will find the first headline with a level
4015 of 1 and a \"phone\" tag, and will return its beginning position:
4017 \(org-element-map tree 'headline
4019 \(and (= (org-element-property :level hl) 1)
4020 \(member \"phone\" (org-element-property :tags hl))
4021 \(org-element-property :begin hl)))
4024 The next example will return a flat list of all `plain-list' type
4025 elements in TREE that are not a sub-list themselves:
4027 \(org-element-map tree 'plain-list #'identity nil nil 'plain-list)
4029 Eventually, this example will return a flat list of all `bold'
4030 type objects containing a `latex-snippet' type object, even
4031 looking into captions:
4033 \(org-element-map tree 'bold
4035 \(and (org-element-map b 'latex-snippet #'identity nil t) b))
4037 ;; Ensure TYPES and NO-RECURSION are a list, even of one element.
4038 (let* ((types (if (listp types
) types
(list types
)))
4039 (no-recursion (if (listp no-recursion
) no-recursion
4040 (list no-recursion
)))
4041 ;; Recursion depth is determined by --CATEGORY.
4044 (let ((category 'greater-elements
)
4045 (all-objects (cons 'plain-text org-element-all-objects
)))
4046 (dolist (type types category
)
4047 (cond ((memq type all-objects
)
4048 ;; If one object is found, the function has
4049 ;; to recurse into every object.
4050 (throw :--found
'objects
))
4051 ((not (memq type org-element-greater-elements
))
4052 ;; If one regular element is found, the
4053 ;; function has to recurse, at least, into
4054 ;; every element it encounters.
4055 (and (not (eq category
'elements
))
4056 (setq category
'elements
))))))))
4058 (letrec ((--walk-tree
4060 ;; Recursively walk DATA. INFO, if non-nil, is a plist
4061 ;; holding contextual information.
4062 (let ((--type (org-element-type --data
)))
4065 ;; Ignored element in an export context.
4066 ((and info
(memq --data
(plist-get info
:ignore-list
))))
4067 ;; List of elements or objects.
4068 ((not --type
) (mapc --walk-tree --data
))
4069 ;; Unconditionally enter parse trees.
4070 ((eq --type
'org-data
)
4071 (mapc --walk-tree
(org-element-contents --data
)))
4073 ;; Check if TYPE is matching among TYPES. If so,
4074 ;; apply FUN to --DATA and accumulate return value
4075 ;; into --ACC (or exit if FIRST-MATCH is non-nil).
4076 (when (memq --type types
)
4077 (let ((result (funcall fun --data
)))
4078 (cond ((not result
))
4079 (first-match (throw :--map-first-match result
))
4080 (t (push result --acc
)))))
4081 ;; If --DATA has a secondary string that can contain
4082 ;; objects with their type among TYPES, look inside.
4083 (when (and (eq --category
'objects
) (not (stringp --data
)))
4084 (dolist (p (cdr (assq --type
4085 org-element-secondary-value-alist
)))
4086 (funcall --walk-tree
(org-element-property p --data
))))
4087 ;; If --DATA has any parsed affiliated keywords and
4088 ;; WITH-AFFILIATED is non-nil, look for objects in
4090 (when (and with-affiliated
4091 (eq --category
'objects
)
4092 (memq --type org-element-all-elements
))
4093 (dolist (kwd-pair org-element--parsed-properties-alist
)
4094 (let ((kwd (car kwd-pair
))
4095 (value (org-element-property (cdr kwd-pair
) --data
)))
4096 ;; Pay attention to the type of parsed
4097 ;; keyword. In particular, preserve order for
4098 ;; multiple keywords.
4101 ((member kwd org-element-dual-keywords
)
4102 (if (member kwd org-element-multiple-keywords
)
4103 (dolist (line (reverse value
))
4104 (funcall --walk-tree
(cdr line
))
4105 (funcall --walk-tree
(car line
)))
4106 (funcall --walk-tree
(cdr value
))
4107 (funcall --walk-tree
(car value
))))
4108 ((member kwd org-element-multiple-keywords
)
4109 (mapc --walk-tree
(reverse value
)))
4110 (t (funcall --walk-tree value
))))))
4111 ;; Determine if a recursion into --DATA is possible.
4113 ;; --TYPE is explicitly removed from recursion.
4114 ((memq --type no-recursion
))
4115 ;; --DATA has no contents.
4116 ((not (org-element-contents --data
)))
4117 ;; Looking for greater elements but --DATA is
4118 ;; simply an element or an object.
4119 ((and (eq --category
'greater-elements
)
4120 (not (memq --type org-element-greater-elements
))))
4121 ;; Looking for elements but --DATA is an object.
4122 ((and (eq --category
'elements
)
4123 (memq --type org-element-all-objects
)))
4124 ;; In any other case, map contents.
4125 (t (mapc --walk-tree
(org-element-contents --data
))))))))))
4126 (catch :--map-first-match
4127 (funcall --walk-tree data
)
4128 ;; Return value in a proper order.
4129 (nreverse --acc
)))))
4130 (put 'org-element-map
'lisp-indent-function
2)
4132 ;; The following functions are internal parts of the parser.
4134 ;; The first one, `org-element--parse-elements' acts at the element's
4137 ;; The second one, `org-element--parse-objects' applies on all objects
4138 ;; of a paragraph or a secondary string. It calls
4139 ;; `org-element--object-lex' to find the next object in the current
4142 (defsubst org-element--next-mode
(type parentp
)
4143 "Return next special mode according to TYPE, or nil.
4144 TYPE is a symbol representing the type of an element or object
4145 containing next element if PARENTP is non-nil, or before it
4146 otherwise. Modes can be either `first-section', `item',
4147 `node-property', `planning', `property-drawer', `section',
4148 `table-row' or nil."
4151 (`headline
'section
)
4153 (`property-drawer
'node-property
)
4154 (`section
'planning
)
4155 (`table
'table-row
))
4158 (`node-property
'node-property
)
4159 (`planning
'property-drawer
)
4160 (`table-row
'table-row
))))
4162 (defun org-element--parse-elements
4163 (beg end mode structure granularity visible-only acc
)
4164 "Parse elements between BEG and END positions.
4166 MODE prioritizes some elements over the others. It can be set to
4167 `first-section', `section', `planning', `item', `node-property'
4170 When value is `item', STRUCTURE will be used as the current list
4173 GRANULARITY determines the depth of the recursion. See
4174 `org-element-parse-buffer' for more information.
4176 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
4179 Elements are accumulated into ACC."
4182 ;; Visible only: skip invisible parts at the beginning of the
4184 (when (and visible-only
(org-invisible-p2))
4185 (goto-char (min (1+ (org-find-visible)) end
)))
4186 ;; When parsing only headlines, skip any text before first one.
4187 (when (and (eq granularity
'headline
) (not (org-at-heading-p)))
4188 (org-with-limited-levels (outline-next-heading)))
4190 (while (< (point) end
)
4191 ;; Find current element's type and parse it accordingly to
4193 (let* ((element (org-element--current-element
4194 end granularity mode structure
))
4195 (type (org-element-type element
))
4196 (cbeg (org-element-property :contents-begin element
)))
4197 (goto-char (org-element-property :end element
))
4198 ;; Visible only: skip invisible parts between siblings.
4199 (when (and visible-only
(org-invisible-p2))
4200 (goto-char (min (1+ (org-find-visible)) end
)))
4201 ;; Fill ELEMENT contents by side-effect.
4203 ;; If element has no contents, don't modify it.
4205 ;; Greater element: parse it between `contents-begin' and
4206 ;; `contents-end'. Make sure GRANULARITY allows the
4207 ;; recursion, or ELEMENT is a headline, in which case going
4208 ;; inside is mandatory, in order to get sub-level headings.
4209 ((and (memq type org-element-greater-elements
)
4210 (or (memq granularity
'(element object nil
))
4211 (and (eq granularity
'greater-element
)
4213 (eq type
'headline
)))
4214 (org-element--parse-elements
4215 cbeg
(org-element-property :contents-end element
)
4216 ;; Possibly switch to a special mode.
4217 (org-element--next-mode type t
)
4218 (and (memq type
'(item plain-list
))
4219 (org-element-property :structure element
))
4220 granularity visible-only element
))
4221 ;; ELEMENT has contents. Parse objects inside, if
4222 ;; GRANULARITY allows it.
4223 ((memq granularity
'(object nil
))
4224 (org-element--parse-objects
4225 cbeg
(org-element-property :contents-end element
) element
4226 (org-element-restriction type
))))
4227 (org-element-adopt-elements acc element
)
4229 (setq mode
(org-element--next-mode type nil
))))
4233 (defun org-element--object-lex (restriction)
4234 "Return next object in current buffer or nil.
4235 RESTRICTION is a list of object types, as symbols, that should be
4236 looked after. This function assumes that the buffer is narrowed
4237 to an appropriate container (e.g., a paragraph)."
4238 (if (memq 'table-cell restriction
) (org-element-table-cell-parser)
4240 (let ((limit (and org-target-link-regexp
4242 (or (bolp) (backward-char))
4243 (re-search-forward org-target-link-regexp nil t
))
4244 (match-beginning 1)))
4246 (while (and (not found
)
4247 (re-search-forward org-element--object-regexp limit t
))
4248 (goto-char (match-beginning 0))
4249 (let ((result (match-string 0)))
4252 ((eq (compare-strings result nil nil
"call_" nil nil t
) t
)
4253 (and (memq 'inline-babel-call restriction
)
4254 (org-element-inline-babel-call-parser)))
4255 ((eq (compare-strings result nil nil
"src_" nil nil t
) t
)
4256 (and (memq 'inline-src-block restriction
)
4257 (org-element-inline-src-block-parser)))
4260 (?^
(and (memq 'superscript restriction
)
4261 (org-element-superscript-parser)))
4262 (?_
(or (and (memq 'subscript restriction
)
4263 (org-element-subscript-parser))
4264 (and (memq 'underline restriction
)
4265 (org-element-underline-parser))))
4266 (?
* (and (memq 'bold restriction
)
4267 (org-element-bold-parser)))
4268 (?
/ (and (memq 'italic restriction
)
4269 (org-element-italic-parser)))
4270 (?~
(and (memq 'code restriction
)
4271 (org-element-code-parser)))
4272 (?
= (and (memq 'verbatim restriction
)
4273 (org-element-verbatim-parser)))
4274 (?
+ (and (memq 'strike-through restriction
)
4275 (org-element-strike-through-parser)))
4276 (?
@ (and (memq 'export-snippet restriction
)
4277 (org-element-export-snippet-parser)))
4278 (?
{ (and (memq 'macro restriction
)
4279 (org-element-macro-parser)))
4280 (?$
(and (memq 'latex-fragment restriction
)
4281 (org-element-latex-fragment-parser)))
4283 (if (eq (aref result
1) ?
<)
4284 (or (and (memq 'radio-target restriction
)
4285 (org-element-radio-target-parser))
4286 (and (memq 'target restriction
)
4287 (org-element-target-parser)))
4288 (or (and (memq 'timestamp restriction
)
4289 (org-element-timestamp-parser))
4290 (and (memq 'link restriction
)
4291 (org-element-link-parser)))))
4293 (if (eq (aref result
1) ?
\\)
4294 (and (memq 'line-break restriction
)
4295 (org-element-line-break-parser))
4296 (or (and (memq 'entity restriction
)
4297 (org-element-entity-parser))
4298 (and (memq 'latex-fragment restriction
)
4299 (org-element-latex-fragment-parser)))))
4301 (if (eq (aref result
1) ?\
[)
4302 (and (memq 'link restriction
)
4303 (org-element-link-parser))
4304 (or (and (memq 'footnote-reference restriction
)
4305 (org-element-footnote-reference-parser))
4306 (and (memq 'timestamp restriction
)
4307 (org-element-timestamp-parser))
4308 (and (memq 'statistics-cookie restriction
)
4309 (org-element-statistics-cookie-parser)))))
4310 ;; This is probably a plain link.
4311 (_ (and (or (memq 'link restriction
)
4312 (memq 'plain-link restriction
))
4313 (org-element-link-parser)))))))
4314 (or (eobp) (forward-char))))
4317 ((and limit
(memq 'link restriction
))
4318 (goto-char limit
) (org-element-link-parser)))))))
4320 (defun org-element--parse-objects (beg end acc restriction
)
4321 "Parse objects between BEG and END and return recursive structure.
4323 Objects are accumulated in ACC.
4325 RESTRICTION is a list of object successors which are allowed in
4326 the current object."
4329 (narrow-to-region beg end
)
4330 (goto-char (point-min))
4332 (while (and (not (eobp))
4333 (setq next-object
(org-element--object-lex restriction
)))
4334 ;; 1. Text before any object. Untabify it.
4335 (let ((obj-beg (org-element-property :begin next-object
)))
4336 (unless (= (point) obj-beg
)
4338 (org-element-adopt-elements
4340 (replace-regexp-in-string
4341 "\t" (make-string tab-width ?\s
)
4342 (buffer-substring-no-properties (point) obj-beg
))))))
4344 (let ((obj-end (org-element-property :end next-object
))
4345 (cont-beg (org-element-property :contents-begin next-object
)))
4346 ;; Fill contents of NEXT-OBJECT by side-effect, if it has
4347 ;; a recursive type.
4349 (memq (car next-object
) org-element-recursive-objects
))
4350 (org-element--parse-objects
4351 cont-beg
(org-element-property :contents-end next-object
)
4352 next-object
(org-element-restriction next-object
)))
4353 (setq acc
(org-element-adopt-elements acc next-object
))
4354 (goto-char obj-end
))))
4355 ;; 3. Text after last object. Untabify it.
4358 (org-element-adopt-elements
4360 (replace-regexp-in-string
4361 "\t" (make-string tab-width ?\s
)
4362 (buffer-substring-no-properties (point) end
)))))
4368 ;;; Towards A Bijective Process
4370 ;; The parse tree obtained with `org-element-parse-buffer' is really
4371 ;; a snapshot of the corresponding Org buffer. Therefore, it can be
4372 ;; interpreted and expanded into a string with canonical Org syntax.
4373 ;; Hence `org-element-interpret-data'.
4375 ;; The function relies internally on
4376 ;; `org-element--interpret-affiliated-keywords'.
4379 (defun org-element-interpret-data (data)
4380 "Interpret DATA as Org syntax.
4381 DATA is a parse tree, an element, an object or a secondary string
4382 to interpret. Return Org syntax as a string."
4384 (lambda (--data parent
)
4385 (let* ((type (org-element-type --data
))
4386 ;; Find interpreter for current object or
4387 ;; element. If it doesn't exist (e.g. this is
4388 ;; a pseudo object or element), return contents,
4392 (format "org-element-%s-interpreter" type
))))
4393 (if (fboundp fun
) fun
(lambda (_ contents
) contents
))))
4396 ;; Secondary string.
4398 (mapconcat (lambda (obj) (funcall fun obj parent
))
4400 ;; Full Org document.
4401 ((eq type
'org-data
)
4402 (mapconcat (lambda (obj) (funcall fun obj parent
))
4403 (org-element-contents --data
) ""))
4404 ;; Plain text: return it.
4405 ((stringp --data
) --data
)
4406 ;; Element or object without contents.
4407 ((not (org-element-contents --data
))
4408 (funcall interpret --data nil
))
4409 ;; Element or object with contents.
4414 ;; Recursively interpret contents.
4416 (lambda (obj) (funcall fun obj --data
))
4417 (org-element-contents
4418 (if (not (memq type
'(paragraph verse-block
)))
4420 ;; Fix indentation of elements containing
4421 ;; objects. We ignore `table-row'
4422 ;; elements as they are one line long
4424 (org-element-normalize-contents
4426 ;; When normalizing first paragraph of
4427 ;; an item or a footnote-definition,
4428 ;; ignore first line's indentation.
4429 (and (eq type
'paragraph
)
4431 (car (org-element-contents parent
)))
4432 (memq (org-element-type parent
)
4433 '(footnote-definition item
))))))
4435 (if (memq type
'(org-data plain-text nil
)) results
4436 ;; Build white spaces. If no `:post-blank' property
4437 ;; is specified, assume its value is 0.
4438 (let ((blank (or (org-element-property :post-blank --data
) 0)))
4439 (if (or (memq type org-element-all-objects
)
4441 (let ((type (org-element-type parent
)))
4443 (memq type org-element-object-containers
)))))
4444 (concat results
(make-string blank ?\s
))
4446 (org-element--interpret-affiliated-keywords --data
)
4447 (org-element-normalize-string results
)
4448 (make-string blank ?
\n)))))))))
4449 (funcall fun data nil
)))
4451 (defun org-element--interpret-affiliated-keywords (element)
4452 "Return ELEMENT's affiliated keywords as Org syntax.
4453 If there is no affiliated keyword, return the empty string."
4454 (let ((keyword-to-org
4458 (when (member key org-element-dual-keywords
)
4459 (setq dual
(cdr value
) value
(car value
)))
4462 (format "[%s]" (org-element-interpret-data dual
)))
4464 (if (member key org-element-parsed-keywords
)
4465 (org-element-interpret-data value
)
4470 (let ((value (org-element-property prop element
))
4471 (keyword (upcase (substring (symbol-name prop
) 1))))
4473 (if (or (member keyword org-element-multiple-keywords
)
4474 ;; All attribute keywords can have multiple lines.
4475 (string-match "^ATTR_" keyword
))
4476 (mapconcat (lambda (line) (funcall keyword-to-org keyword line
))
4479 (funcall keyword-to-org keyword value
)))))
4480 ;; List all ELEMENT's properties matching an attribute line or an
4481 ;; affiliated keyword, but ignore translated keywords since they
4482 ;; cannot belong to the property list.
4483 (loop for prop in
(nth 1 element
) by
'cddr
4484 when
(let ((keyword (upcase (substring (symbol-name prop
) 1))))
4485 (or (string-match "^ATTR_" keyword
)
4487 (member keyword org-element-affiliated-keywords
)
4489 org-element-keyword-translation-alist
)))))
4493 ;; Because interpretation of the parse tree must return the same
4494 ;; number of blank lines between elements and the same number of white
4495 ;; space after objects, some special care must be given to white
4498 ;; The first function, `org-element-normalize-string', ensures any
4499 ;; string different from the empty string will end with a single
4500 ;; newline character.
4502 ;; The second function, `org-element-normalize-contents', removes
4503 ;; global indentation from the contents of the current element.
4505 (defun org-element-normalize-string (s)
4506 "Ensure string S ends with a single newline character.
4508 If S isn't a string return it unchanged. If S is the empty
4509 string, return it. Otherwise, return a new string with a single
4510 newline character at its end."
4512 ((not (stringp s
)) s
)
4514 (t (and (string-match "\\(\n[ \t]*\\)*\\'" s
)
4515 (replace-match "\n" nil nil s
)))))
4517 (defun org-element-normalize-contents (element &optional ignore-first
)
4518 "Normalize plain text in ELEMENT's contents.
4520 ELEMENT must only contain plain text and objects.
4522 If optional argument IGNORE-FIRST is non-nil, ignore first line's
4523 indentation to compute maximal common indentation.
4525 Return the normalized element that is element with global
4526 indentation removed from its contents. The function assumes that
4527 indentation is not done with TAB characters."
4528 (letrec ((find-min-ind
4529 ;; Return minimal common indentation within BLOB. This is
4530 ;; done by walking recursively BLOB and updating MIN-IND
4531 ;; along the way. FIRST-FLAG is non-nil when the next
4532 ;; object is expected to be a string that doesn't start
4533 ;; with a newline character. It happens for strings at
4534 ;; the beginnings of the contents or right after a line
4536 (lambda (blob first-flag min-ind
)
4538 (dolist (object (org-element-contents blob
) min-ind
)
4540 (setq first-flag nil
)
4541 ;; Objects cannot start with spaces: in this case,
4542 ;; indentation is 0.
4543 (if (not (stringp object
)) (throw 'zero
0)
4544 (string-match "\\` *" object
)
4545 (let ((len (match-end 0)))
4546 ;; An indentation of zero means no string will
4547 ;; be modified. Quit the process.
4548 (if (zerop len
) (throw 'zero
0)
4549 (setq min-ind
(min len min-ind
))))))
4552 (dolist (line (cdr (org-split-string object
" *\n")))
4553 (unless (string= line
"")
4555 (min (org-get-indentation line
) min-ind
)))))
4556 ((eq (org-element-type object
) 'line-break
)
4557 (setq first-flag t
))
4558 ((memq (org-element-type object
)
4559 org-element-recursive-objects
)
4561 (funcall find-min-ind
4562 object first-flag min-ind
))))))))
4563 (min-ind (funcall find-min-ind
4564 element
(not ignore-first
) most-positive-fixnum
)))
4565 (if (or (zerop min-ind
) (= min-ind most-positive-fixnum
)) element
4566 ;; Build ELEMENT back, replacing each string with the same
4567 ;; string minus common indentation.
4569 (lambda (datum first-flag
)
4570 ;; Return DATUM with all its strings indentation
4571 ;; shortened from MIN-IND white spaces.
4572 ;; FIRST-FLAG is non-nil when the next object is
4573 ;; expected to be a string that doesn't start with
4574 ;; a newline character.
4579 (setq first-flag nil
)
4580 (when (stringp object
)
4582 (replace-regexp-in-string
4583 (format "\\` \\{%d\\}" min-ind
)
4587 (replace-regexp-in-string
4588 (format "\n \\{%d\\}" min-ind
) "\n" object
))
4589 ((memq (org-element-type object
)
4590 org-element-recursive-objects
)
4591 (funcall build object first-flag
))
4592 ((eq (org-element-type object
) 'line-break
)
4596 (org-element-contents datum
)))
4598 (funcall build element
(not ignore-first
))))))
4604 ;; Implement a caching mechanism for `org-element-at-point' and
4605 ;; `org-element-context', which see.
4607 ;; A single public function is provided: `org-element-cache-reset'.
4609 ;; Cache is enabled by default, but can be disabled globally with
4610 ;; `org-element-use-cache'. `org-element-cache-sync-idle-time',
4611 ;; org-element-cache-sync-duration' and `org-element-cache-sync-break'
4612 ;; can be tweaked to control caching behaviour.
4614 ;; Internally, parsed elements are stored in an AVL tree,
4615 ;; `org-element--cache'. This tree is updated lazily: whenever
4616 ;; a change happens to the buffer, a synchronization request is
4617 ;; registered in `org-element--cache-sync-requests' (see
4618 ;; `org-element--cache-submit-request'). During idle time, requests
4619 ;; are processed by `org-element--cache-sync'. Synchronization also
4620 ;; happens when an element is required from the cache. In this case,
4621 ;; the process stops as soon as the needed element is up-to-date.
4623 ;; A synchronization request can only apply on a synchronized part of
4624 ;; the cache. Therefore, the cache is updated at least to the
4625 ;; location where the new request applies. Thus, requests are ordered
4626 ;; from left to right and all elements starting before the first
4627 ;; request are correct. This property is used by functions like
4628 ;; `org-element--cache-find' to retrieve elements in the part of the
4629 ;; cache that can be trusted.
4631 ;; A request applies to every element, starting from its original
4632 ;; location (or key, see below). When a request is processed, it
4633 ;; moves forward and may collide the next one. In this case, both
4634 ;; requests are merged into a new one that starts from that element.
4635 ;; As a consequence, the whole synchronization complexity does not
4636 ;; depend on the number of pending requests, but on the number of
4637 ;; elements the very first request will be applied on.
4639 ;; Elements cannot be accessed through their beginning position, which
4640 ;; may or may not be up-to-date. Instead, each element in the tree is
4641 ;; associated to a key, obtained with `org-element--cache-key'. This
4642 ;; mechanism is robust enough to preserve total order among elements
4643 ;; even when the tree is only partially synchronized.
4645 ;; Objects contained in an element are stored in a hash table,
4646 ;; `org-element--cache-objects'.
4649 (defvar org-element-use-cache t
4650 "Non nil when Org parser should cache its results.
4651 This is mostly for debugging purpose.")
4653 (defvar org-element-cache-sync-idle-time
0.6
4654 "Length, in seconds, of idle time before syncing cache.")
4656 (defvar org-element-cache-sync-duration
(seconds-to-time 0.04)
4657 "Maximum duration, as a time value, for a cache synchronization.
4658 If the synchronization is not over after this delay, the process
4659 pauses and resumes after `org-element-cache-sync-break'
4662 (defvar org-element-cache-sync-break
(seconds-to-time 0.3)
4663 "Duration, as a time value, of the pause between synchronizations.
4664 See `org-element-cache-sync-duration' for more information.")
4669 (defvar org-element--cache nil
4670 "AVL tree used to cache elements.
4671 Each node of the tree contains an element. Comparison is done
4672 with `org-element--cache-compare'. This cache is used in
4673 `org-element-at-point'.")
4675 (defvar org-element--cache-objects nil
4676 "Hash table used as to cache objects.
4677 Key is an element, as returned by `org-element-at-point', and
4678 value is an alist where each association is:
4680 \(PARENT COMPLETEP . OBJECTS)
4682 where PARENT is an element or object, COMPLETEP is a boolean,
4683 non-nil when all direct children of parent are already cached and
4684 OBJECTS is a list of such children, as objects, from farthest to
4687 In the following example, \\alpha, bold object and \\beta are
4688 contained within a paragraph
4692 If the paragraph is completely parsed, OBJECTS-DATA will be
4694 \((PARAGRAPH t BOLD-OBJECT ENTITY-OBJECT)
4695 \(BOLD-OBJECT t ENTITY-OBJECT))
4697 whereas in a partially parsed paragraph, it could be
4699 \((PARAGRAPH nil ENTITY-OBJECT))
4701 This cache is used in `org-element-context'.")
4703 (defvar org-element--cache-sync-requests nil
4704 "List of pending synchronization requests.
4706 A request is a vector with the following pattern:
4708 \[NEXT BEG END OFFSET PARENT PHASE]
4710 Processing a synchronization request consists of three phases:
4712 0. Delete modified elements,
4713 1. Fill missing area in cache,
4714 2. Shift positions and re-parent elements after the changes.
4716 During phase 0, NEXT is the key of the first element to be
4717 removed, BEG and END is buffer position delimiting the
4718 modifications. Elements starting between them (inclusive) are
4719 removed. So are elements whose parent is removed. PARENT, when
4720 non-nil, is the parent of the first element to be removed.
4722 During phase 1, NEXT is the key of the next known element in
4723 cache and BEG its beginning position. Parse buffer between that
4724 element and the one before it in order to determine the parent of
4725 the next element. Set PARENT to the element containing NEXT.
4727 During phase 2, NEXT is the key of the next element to shift in
4728 the parse tree. All elements starting from this one have their
4729 properties relatives to buffer positions shifted by integer
4730 OFFSET and, if they belong to element PARENT, are adopted by it.
4732 PHASE specifies the phase number, as an integer.")
4734 (defvar org-element--cache-sync-timer nil
4735 "Timer used for cache synchronization.")
4737 (defvar org-element--cache-sync-keys nil
4738 "Hash table used to store keys during synchronization.
4739 See `org-element--cache-key' for more information.")
4741 (defsubst org-element--cache-key
(element)
4742 "Return a unique key for ELEMENT in cache tree.
4744 Keys are used to keep a total order among elements in the cache.
4745 Comparison is done with `org-element--cache-key-less-p'.
4747 When no synchronization is taking place, a key is simply the
4748 beginning position of the element, or that position plus one in
4749 the case of an first item (respectively row) in
4750 a list (respectively a table).
4752 During a synchronization, the key is the one the element had when
4753 the cache was synchronized for the last time. Elements added to
4754 cache during the synchronization get a new key generated with
4755 `org-element--cache-generate-key'.
4757 Such keys are stored in `org-element--cache-sync-keys'. The hash
4758 table is cleared once the synchronization is complete."
4759 (or (gethash element org-element--cache-sync-keys
)
4760 (let* ((begin (org-element-property :begin element
))
4761 ;; Increase beginning position of items (respectively
4762 ;; table rows) by one, so the first item can get
4763 ;; a different key from its parent list (respectively
4765 (key (if (memq (org-element-type element
) '(item table-row
))
4768 (if org-element--cache-sync-requests
4769 (puthash element key org-element--cache-sync-keys
)
4772 (defun org-element--cache-generate-key (lower upper
)
4773 "Generate a key between LOWER and UPPER.
4775 LOWER and UPPER are integers or lists, possibly empty.
4777 If LOWER and UPPER are equals, return LOWER. Otherwise, return
4778 a unique key, as an integer or a list of integers, according to
4779 the following rules:
4781 - LOWER and UPPER are compared level-wise until values differ.
4783 - If, at a given level, LOWER and UPPER differ from more than
4784 2, the new key shares all the levels above with LOWER and
4785 gets a new level. Its value is the mean between LOWER and
4788 \(1 2) + (1 4) --> (1 3)
4790 - If LOWER has no value to compare with, it is assumed that its
4791 value is `most-negative-fixnum'. E.g.,
4799 where m is `most-negative-fixnum'. Likewise, if UPPER is
4800 short of levels, the current value is `most-positive-fixnum'.
4802 - If they differ from only one, the new key inherits from
4803 current LOWER level and fork it at the next level. E.g.,
4811 where M is `most-positive-fixnum'.
4813 - If the key is only one level long, it is returned as an
4816 \(1 2) + (3 2) --> 2
4818 When they are not equals, the function assumes that LOWER is
4819 lesser than UPPER, per `org-element--cache-key-less-p'."
4820 (if (equal lower upper
) lower
4821 (let ((lower (if (integerp lower
) (list lower
) lower
))
4822 (upper (if (integerp upper
) (list upper
) upper
))
4826 (let ((min (or (car lower
) most-negative-fixnum
))
4827 (max (cond (skip-upper most-positive-fixnum
)
4829 (t most-positive-fixnum
))))
4830 (if (< (1+ min
) max
)
4831 (let ((mean (+ (ash min -
1) (ash max -
1) (logand min max
1))))
4832 (throw 'exit
(if key
(nreverse (cons mean key
)) mean
)))
4833 (when (and (< min max
) (not skip-upper
))
4834 ;; When at a given level, LOWER and UPPER differ from
4835 ;; 1, ignore UPPER altogether. Instead create a key
4836 ;; between LOWER and the greatest key with the same
4837 ;; prefix as LOWER so far.
4838 (setq skip-upper t
))
4840 (setq lower
(cdr lower
) upper
(cdr upper
)))))))))
4842 (defsubst org-element--cache-key-less-p
(a b
)
4843 "Non-nil if key A is less than key B.
4844 A and B are either integers or lists of integers, as returned by
4845 `org-element--cache-key'."
4846 (if (integerp a
) (if (integerp b
) (< a b
) (<= a
(car b
)))
4847 (if (integerp b
) (< (car a
) b
)
4850 (cond ((car-less-than-car a b
) (throw 'exit t
))
4851 ((car-less-than-car b a
) (throw 'exit nil
))
4852 (t (setq a
(cdr a
) b
(cdr b
)))))
4853 ;; If A is empty, either keys are equal (B is also empty) and
4854 ;; we return nil, or A is lesser than B (B is longer) and we
4855 ;; return a non-nil value.
4857 ;; If A is not empty, B is necessarily empty and A is greater
4858 ;; than B (A is longer). Therefore, return nil.
4859 (and (null a
) b
)))))
4861 (defun org-element--cache-compare (a b
)
4862 "Non-nil when element A is located before element B."
4863 (org-element--cache-key-less-p (org-element--cache-key a
)
4864 (org-element--cache-key b
)))
4866 (defsubst org-element--cache-root
()
4867 "Return root value in cache.
4868 This function assumes `org-element--cache' is a valid AVL tree."
4869 (avl-tree--node-left (avl-tree--dummyroot org-element--cache
)))
4874 (defsubst org-element--cache-active-p
()
4875 "Non-nil when cache is active in current buffer."
4876 (and org-element-use-cache
4877 (or (derived-mode-p 'org-mode
) orgstruct-mode
)))
4879 (defun org-element--cache-find (pos &optional side
)
4880 "Find element in cache starting at POS or before.
4882 POS refers to a buffer position.
4884 When optional argument SIDE is non-nil, the function checks for
4885 elements starting at or past POS instead. If SIDE is `both', the
4886 function returns a cons cell where car is the first element
4887 starting at or before POS and cdr the first element starting
4890 The function can only find elements in the synchronized part of
4892 (let ((limit (and org-element--cache-sync-requests
4893 (aref (car org-element--cache-sync-requests
) 0)))
4894 (node (org-element--cache-root))
4897 (let* ((element (avl-tree--node-data node
))
4898 (begin (org-element-property :begin element
)))
4901 (not (org-element--cache-key-less-p
4902 (org-element--cache-key element
) limit
)))
4903 (setq node
(avl-tree--node-left node
)))
4906 node
(avl-tree--node-left node
)))
4909 node
(avl-tree--node-right node
)))
4910 ;; We found an element in cache starting at POS. If `side'
4911 ;; is `both' we also want the next one in order to generate
4912 ;; a key in-between.
4914 ;; If the element is the first row or item in a table or
4915 ;; a plain list, we always return the table or the plain
4918 ;; In any other case, we return the element found.
4920 (setq lower element
)
4921 (setq node
(avl-tree--node-right node
)))
4922 ((and (memq (org-element-type element
) '(item table-row
))
4923 (let ((parent (org-element-property :parent element
)))
4924 (and (= (org-element-property :begin element
)
4925 (org-element-property :contents-begin parent
))
4934 (`both
(cons lower upper
))
4938 (defun org-element--cache-put (element &optional data
)
4939 "Store ELEMENT in current buffer's cache, if allowed.
4940 When optional argument DATA is non-nil, assume is it object data
4941 relative to ELEMENT and store it in the objects cache."
4942 (cond ((not (org-element--cache-active-p)) nil
)
4944 (when org-element--cache-sync-requests
4945 ;; During synchronization, first build an appropriate key
4946 ;; for the new element so `avl-tree-enter' can insert it at
4947 ;; the right spot in the cache.
4948 (let ((keys (org-element--cache-find
4949 (org-element-property :begin element
) 'both
)))
4951 (org-element--cache-generate-key
4952 (and (car keys
) (org-element--cache-key (car keys
)))
4953 (cond ((cdr keys
) (org-element--cache-key (cdr keys
)))
4954 (org-element--cache-sync-requests
4955 (aref (car org-element--cache-sync-requests
) 0))))
4956 org-element--cache-sync-keys
)))
4957 (avl-tree-enter org-element--cache element
))
4958 ;; Headlines are not stored in cache, so objects in titles are
4959 ;; not stored either.
4960 ((eq (org-element-type element
) 'headline
) nil
)
4961 (t (puthash element data org-element--cache-objects
))))
4963 (defsubst org-element--cache-remove
(element)
4964 "Remove ELEMENT from cache.
4965 Assume ELEMENT belongs to cache and that a cache is active."
4966 (avl-tree-delete org-element--cache element
)
4967 (remhash element org-element--cache-objects
))
4970 ;;;; Synchronization
4972 (defsubst org-element--cache-set-timer
(buffer)
4973 "Set idle timer for cache synchronization in BUFFER."
4974 (when org-element--cache-sync-timer
4975 (cancel-timer org-element--cache-sync-timer
))
4976 (setq org-element--cache-sync-timer
4977 (run-with-idle-timer
4978 (let ((idle (current-idle-time)))
4979 (if idle
(time-add idle org-element-cache-sync-break
)
4980 org-element-cache-sync-idle-time
))
4982 #'org-element--cache-sync
4985 (defsubst org-element--cache-interrupt-p
(time-limit)
4986 "Non-nil when synchronization process should be interrupted.
4987 TIME-LIMIT is a time value or nil."
4989 (or (input-pending-p)
4990 (time-less-p time-limit
(current-time)))))
4992 (defsubst org-element--cache-shift-positions
(element offset
&optional props
)
4993 "Shift ELEMENT properties relative to buffer positions by OFFSET.
4995 Properties containing buffer positions are `:begin', `:end',
4996 `:contents-begin', `:contents-end' and `:structure'. When
4997 optional argument PROPS is a list of keywords, only shift
4998 properties provided in that list.
5000 Properties are modified by side-effect."
5001 (let ((properties (nth 1 element
)))
5002 ;; Shift `:structure' property for the first plain list only: it
5003 ;; is the only one that really matters and it prevents from
5004 ;; shifting it more than once.
5005 (when (and (or (not props
) (memq :structure props
))
5006 (eq (org-element-type element
) 'plain-list
)
5007 (not (eq (org-element-type (plist-get properties
:parent
))
5009 (dolist (item (plist-get properties
:structure
))
5010 (incf (car item
) offset
)
5011 (incf (nth 6 item
) offset
)))
5012 (dolist (key '(:begin
:contents-begin
:contents-end
:end
:post-affiliated
))
5013 (let ((value (and (or (not props
) (memq key props
))
5014 (plist-get properties key
))))
5015 (and value
(plist-put properties key
(+ offset value
)))))))
5017 (defun org-element--cache-sync (buffer &optional threshold future-change
)
5018 "Synchronize cache with recent modification in BUFFER.
5020 When optional argument THRESHOLD is non-nil, do the
5021 synchronization for all elements starting before or at threshold,
5022 then exit. Otherwise, synchronize cache for as long as
5023 `org-element-cache-sync-duration' or until Emacs leaves idle
5026 FUTURE-CHANGE, when non-nil, is a buffer position where changes
5027 not registered yet in the cache are going to happen. It is used
5028 in `org-element--cache-submit-request', where cache is partially
5029 updated before current modification are actually submitted."
5030 (when (buffer-live-p buffer
)
5031 (with-current-buffer buffer
5032 (let ((inhibit-quit t
) request next
)
5033 (when org-element--cache-sync-timer
5034 (cancel-timer org-element--cache-sync-timer
))
5036 (while org-element--cache-sync-requests
5037 (setq request
(car org-element--cache-sync-requests
)
5038 next
(nth 1 org-element--cache-sync-requests
))
5039 (org-element--cache-process-request
5041 (and next
(aref next
0))
5043 (and (not threshold
)
5044 (time-add (current-time)
5045 org-element-cache-sync-duration
))
5047 ;; Request processed. Merge current and next offsets and
5048 ;; transfer ending position.
5050 (incf (aref next
3) (aref request
3))
5051 (aset next
2 (aref request
2)))
5052 (setq org-element--cache-sync-requests
5053 (cdr org-element--cache-sync-requests
))))
5054 ;; If more requests are awaiting, set idle timer accordingly.
5055 ;; Otherwise, reset keys.
5056 (if org-element--cache-sync-requests
5057 (org-element--cache-set-timer buffer
)
5058 (clrhash org-element--cache-sync-keys
))))))
5060 (defun org-element--cache-process-request
5061 (request next threshold time-limit future-change
)
5062 "Process synchronization REQUEST for all entries before NEXT.
5064 REQUEST is a vector, built by `org-element--cache-submit-request'.
5066 NEXT is a cache key, as returned by `org-element--cache-key'.
5068 When non-nil, THRESHOLD is a buffer position. Synchronization
5069 stops as soon as a shifted element begins after it.
5071 When non-nil, TIME-LIMIT is a time value. Synchronization stops
5072 after this time or when Emacs exits idle state.
5074 When non-nil, FUTURE-CHANGE is a buffer position where changes
5075 not registered yet in the cache are going to happen. See
5076 `org-element--cache-submit-request' for more information.
5078 Throw `interrupt' if the process stops before completing the
5081 (when (= (aref request
5) 0)
5084 ;; Delete all elements starting after BEG, but not after buffer
5085 ;; position END or past element with key NEXT. Also delete
5086 ;; elements contained within a previously removed element
5087 ;; (stored in `last-container').
5089 ;; At each iteration, we start again at tree root since
5090 ;; a deletion modifies structure of the balanced tree.
5093 (when (org-element--cache-interrupt-p time-limit
)
5094 (throw 'interrupt nil
))
5095 ;; Find first element in cache with key BEG or after it.
5096 (let ((beg (aref request
0))
5097 (end (aref request
2))
5098 (node (org-element--cache-root))
5099 data data-key last-container
)
5101 (let* ((element (avl-tree--node-data node
))
5102 (key (org-element--cache-key element
)))
5104 ((org-element--cache-key-less-p key beg
)
5105 (setq node
(avl-tree--node-right node
)))
5106 ((org-element--cache-key-less-p beg key
)
5109 node
(avl-tree--node-left node
)))
5110 (t (setq data element
5114 (let ((pos (org-element-property :begin data
)))
5115 (if (if (or (not next
)
5116 (org-element--cache-key-less-p data-key next
))
5120 (while (and up
(not (eq up last-container
)))
5121 (setq up
(org-element-property :parent up
)))
5123 (progn (when (and (not last-container
)
5124 (> (org-element-property :end data
)
5126 (setq last-container data
))
5127 (org-element--cache-remove data
))
5128 (aset request
0 data-key
)
5129 (aset request
1 pos
)
5131 (throw 'end-phase nil
)))
5132 ;; No element starting after modifications left in
5133 ;; cache: further processing is futile.
5134 (throw 'quit t
))))))
5135 (when (= (aref request
5) 1)
5138 ;; Phase 0 left a hole in the cache. Some elements after it
5139 ;; could have parents within. For example, in the following
5149 ;; if we remove a blank line between "item" and "Paragraph1",
5150 ;; everything down to "Paragraph2" is removed from cache. But
5151 ;; the paragraph now belongs to the list, and its `:parent'
5152 ;; property no longer is accurate.
5154 ;; Therefore we need to parse again elements in the hole, or at
5155 ;; least in its last section, so that we can re-parent
5156 ;; subsequent elements, during phase 2.
5158 ;; Note that we only need to get the parent from the first
5159 ;; element in cache after the hole.
5161 ;; When next key is lesser or equal to the current one, delegate
5162 ;; phase 1 processing to next request in order to preserve key
5163 ;; order among requests.
5164 (let ((key (aref request
0)))
5165 (when (and next
(not (org-element--cache-key-less-p key next
)))
5166 (let ((next-request (nth 1 org-element--cache-sync-requests
)))
5167 (aset next-request
0 key
)
5168 (aset next-request
1 (aref request
1))
5169 (aset next-request
5 1))
5171 ;; Next element will start at its beginning position plus
5172 ;; offset, since it hasn't been shifted yet. Therefore, LIMIT
5173 ;; contains the real beginning position of the first element to
5174 ;; shift and re-parent.
5175 (let ((limit (+ (aref request
1) (aref request
3))))
5176 (cond ((and threshold
(> limit threshold
)) (throw 'interrupt nil
))
5177 ((and future-change
(>= limit future-change
))
5178 ;; Changes are going to happen around this element and
5179 ;; they will trigger another phase 1 request. Skip the
5183 (let ((parent (org-element--parse-to limit t time-limit
)))
5184 (aset request
4 parent
)
5185 (aset request
5 2))))))
5188 ;; Shift all elements starting from key START, but before NEXT, by
5189 ;; OFFSET, and re-parent them when appropriate.
5191 ;; Elements are modified by side-effect so the tree structure
5194 ;; Once THRESHOLD, if any, is reached, or once there is an input
5195 ;; pending, exit. Before leaving, the current synchronization
5196 ;; request is updated.
5197 (let ((start (aref request
0))
5198 (offset (aref request
3))
5199 (parent (aref request
4))
5200 (node (org-element--cache-root))
5204 ;; No re-parenting nor shifting planned: request is over.
5205 (when (and (not parent
) (zerop offset
)) (throw 'quit t
))
5207 (let* ((data (avl-tree--node-data node
))
5208 (key (org-element--cache-key data
)))
5209 (if (and leftp
(avl-tree--node-left node
)
5210 (not (org-element--cache-key-less-p key start
)))
5211 (progn (push node stack
)
5212 (setq node
(avl-tree--node-left node
)))
5213 (unless (org-element--cache-key-less-p key start
)
5214 ;; We reached NEXT. Request is complete.
5215 (when (equal key next
) (throw 'quit t
))
5216 ;; Handle interruption request. Update current request.
5217 (when (or exit-flag
(org-element--cache-interrupt-p time-limit
))
5218 (aset request
0 key
)
5219 (aset request
4 parent
)
5220 (throw 'interrupt nil
))
5222 (unless (zerop offset
)
5223 (org-element--cache-shift-positions data offset
)
5224 ;; Shift associated objects data, if any.
5225 (dolist (object-data (gethash data org-element--cache-objects
))
5226 (dolist (object (cddr object-data
))
5227 (org-element--cache-shift-positions object offset
))))
5228 (let ((begin (org-element-property :begin data
)))
5229 ;; Update PARENT and re-parent DATA, only when
5230 ;; necessary. Propagate new structures for lists.
5232 (<= (org-element-property :end parent
) begin
))
5233 (setq parent
(org-element-property :parent parent
)))
5234 (cond ((and (not parent
) (zerop offset
)) (throw 'quit nil
))
5236 (let ((p (org-element-property :parent data
)))
5238 (< (org-element-property :begin p
)
5239 (org-element-property :begin parent
)))))
5240 (org-element-put-property data
:parent parent
)
5241 (let ((s (org-element-property :structure parent
)))
5242 (when (and s
(org-element-property :structure data
))
5243 (org-element-put-property data
:structure s
)))))
5244 ;; Cache is up-to-date past THRESHOLD. Request
5246 (when (and threshold
(> begin threshold
)) (setq exit-flag t
))))
5247 (setq node
(if (setq leftp
(avl-tree--node-right node
))
5248 (avl-tree--node-right node
)
5250 ;; We reached end of tree: synchronization complete.
5253 (defun org-element--parse-to (pos &optional syncp time-limit
)
5254 "Parse elements in current section, down to POS.
5256 Start parsing from the closest between the last known element in
5257 cache or headline above. Return the smallest element containing
5260 When optional argument SYNCP is non-nil, return the parent of the
5261 element containing POS instead. In that case, it is also
5262 possible to provide TIME-LIMIT, which is a time value specifying
5263 when the parsing should stop. The function throws `interrupt' if
5264 the process stopped before finding the expected result."
5266 (org-with-wide-buffer
5268 (let* ((cached (and (org-element--cache-active-p)
5269 (org-element--cache-find pos nil
)))
5270 (begin (org-element-property :begin cached
))
5273 ;; Nothing in cache before point: start parsing from first
5274 ;; element following headline above, or first element in
5277 (when (org-with-limited-levels (outline-previous-heading))
5278 (setq mode
'planning
)
5280 (skip-chars-forward " \r\t\n")
5281 (beginning-of-line))
5282 ;; Cache returned exact match: return it.
5284 (throw 'exit
(if syncp
(org-element-property :parent cached
) cached
)))
5285 ;; There's a headline between cached value and POS: cached
5286 ;; value is invalid. Start parsing from first element
5287 ;; following the headline.
5288 ((re-search-backward
5289 (org-with-limited-levels org-outline-regexp-bol
) begin t
)
5291 (skip-chars-forward " \r\t\n")
5293 (setq mode
'planning
))
5294 ;; Check if CACHED or any of its ancestors contain point.
5296 ;; If there is such an element, we inspect it in order to know
5297 ;; if we return it or if we need to parse its contents.
5298 ;; Otherwise, we just start parsing from current location,
5299 ;; which is right after the top-most element containing
5302 ;; As a special case, if POS is at the end of the buffer, we
5303 ;; want to return the innermost element ending there.
5305 ;; Also, if we find an ancestor and discover that we need to
5306 ;; parse its contents, make sure we don't start from
5307 ;; `:contents-begin', as we would otherwise go past CACHED
5308 ;; again. Instead, in that situation, we will resume parsing
5309 ;; from NEXT, which is located after CACHED or its higher
5310 ;; ancestor not containing point.
5313 (pos (if (= (point-max) pos
) (1- pos
) pos
)))
5314 (goto-char (or (org-element-property :contents-begin cached
) begin
))
5315 (while (let ((end (org-element-property :end up
)))
5318 (setq up
(org-element-property :parent up
)))))
5320 ((eobp) (setq element up
))
5321 (t (setq element up next
(point)))))))
5322 ;; Parse successively each element until we reach POS.
5323 (let ((end (or (org-element-property :end element
)
5325 (org-with-limited-levels (outline-next-heading))
5330 (cond ((= (point) pos
) (throw 'exit parent
))
5331 ((org-element--cache-interrupt-p time-limit
)
5332 (throw 'interrupt nil
))))
5334 (setq element
(org-element--current-element
5336 (org-element-property :structure parent
)))
5337 (org-element-put-property element
:parent parent
)
5338 (org-element--cache-put element
))
5339 (let ((elem-end (org-element-property :end element
))
5340 (type (org-element-type element
)))
5342 ;; Skip any element ending before point. Also skip
5343 ;; element ending at point (unless it is also the end of
5344 ;; buffer) since we're sure that another element begins
5346 ((and (<= elem-end pos
) (/= (point-max) elem-end
))
5347 (goto-char elem-end
)
5348 (setq mode
(org-element--next-mode type nil
)))
5349 ;; A non-greater element contains point: return it.
5350 ((not (memq type org-element-greater-elements
))
5351 (throw 'exit element
))
5352 ;; Otherwise, we have to decide if ELEMENT really
5353 ;; contains POS. In that case we start parsing from
5354 ;; contents' beginning.
5356 ;; If POS is at contents' beginning but it is also at
5357 ;; the beginning of the first item in a list or a table.
5358 ;; In that case, we need to create an anchor for that
5359 ;; list or table, so return it.
5361 ;; Also, if POS is at the end of the buffer, no element
5362 ;; can start after it, but more than one may end there.
5363 ;; Arbitrarily, we choose to return the innermost of
5365 ((let ((cbeg (org-element-property :contents-begin element
))
5366 (cend (org-element-property :contents-end element
)))
5371 (not (memq type
'(plain-list table
)))))
5373 (and (= cend pos
) (= (point-max) pos
)))))
5374 (goto-char (or next cbeg
))
5376 mode
(org-element--next-mode type t
)
5379 ;; Otherwise, return ELEMENT as it is the smallest
5380 ;; element containing POS.
5381 (t (throw 'exit element
))))
5382 (setq element nil
)))))))
5385 ;;;; Staging Buffer Changes
5387 (defconst org-element--cache-sensitive-re
5389 org-outline-regexp-bol
"\\|"
5390 "\\\\end{[A-Za-z0-9*]+}[ \t]*$" "\\|"
5392 "#\\+\\(?:BEGIN[:_]\\|END\\(?:_\\|:?[ \t]*$\\)\\)" "\\|"
5393 "\\\\begin{[A-Za-z0-9*]+}" "\\|"
5394 ":\\(?:\\w\\|[-_]\\)+:[ \t]*$"
5396 "Regexp matching a sensitive line, structure wise.
5397 A sensitive line is a headline, inlinetask, block, drawer, or
5398 latex-environment boundary. When such a line is modified,
5399 structure changes in the document may propagate in the whole
5400 section, possibly making cache invalid.")
5402 (defvar org-element--cache-change-warning nil
5403 "Non-nil when a sensitive line is about to be changed.
5404 It is a symbol among nil, t and `headline'.")
5406 (defun org-element--cache-before-change (beg end
)
5407 "Request extension of area going to be modified if needed.
5408 BEG and END are the beginning and end of the range of changed
5409 text. See `before-change-functions' for more information."
5410 (when (org-element--cache-active-p)
5411 (org-with-wide-buffer
5414 (let ((bottom (save-excursion (goto-char end
) (line-end-position))))
5415 (setq org-element--cache-change-warning
5417 (if (and (org-with-limited-levels (org-at-heading-p))
5418 (= (line-end-position) bottom
))
5420 (let ((case-fold-search t
))
5422 org-element--cache-sensitive-re bottom t
)))))))))
5424 (defun org-element--cache-after-change (beg end pre
)
5425 "Update buffer modifications for current buffer.
5426 BEG and END are the beginning and end of the range of changed
5427 text, and the length in bytes of the pre-change text replaced by
5428 that range. See `after-change-functions' for more information."
5429 (when (org-element--cache-active-p)
5430 (org-with-wide-buffer
5435 (bottom (save-excursion (goto-char end
) (line-end-position))))
5436 ;; Determine if modified area needs to be extended, according
5437 ;; to both previous and current state. We make a special
5438 ;; case for headline editing: if a headline is modified but
5439 ;; not removed, do not extend.
5440 (when (pcase org-element--cache-change-warning
5443 (not (and (org-with-limited-levels (org-at-heading-p))
5444 (= (line-end-position) bottom
))))
5446 (let ((case-fold-search t
))
5448 org-element--cache-sensitive-re bottom t
))))
5449 ;; Effectively extend modified area.
5450 (org-with-limited-levels
5451 (setq top
(progn (goto-char top
)
5452 (when (outline-previous-heading) (forward-line))
5454 (setq bottom
(progn (goto-char bottom
)
5455 (if (outline-next-heading) (1- (point))
5457 ;; Store synchronization request.
5458 (let ((offset (- end beg pre
)))
5459 (org-element--cache-submit-request top
(- bottom offset
) offset
)))))
5460 ;; Activate a timer to process the request during idle time.
5461 (org-element--cache-set-timer (current-buffer))))
5463 (defun org-element--cache-for-removal (beg end offset
)
5464 "Return first element to remove from cache.
5466 BEG and END are buffer positions delimiting buffer modifications.
5467 OFFSET is the size of the changes.
5469 Returned element is usually the first element in cache containing
5470 any position between BEG and END. As an exception, greater
5471 elements around the changes that are robust to contents
5472 modifications are preserved and updated according to the
5474 (let* ((elements (org-element--cache-find (1- beg
) 'both
))
5475 (before (car elements
))
5476 (after (cdr elements
)))
5477 (if (not before
) after
5481 (if (let ((type (org-element-type up
)))
5482 (and (or (memq type
'(center-block dynamic-block quote-block
5484 ;; Drawers named "PROPERTIES" are probably
5485 ;; a properties drawer being edited. Force
5486 ;; parsing to check if editing is over.
5487 (and (eq type
'drawer
)
5489 (org-element-property :drawer-name up
)
5491 (let ((cbeg (org-element-property :contents-begin up
)))
5494 (> (org-element-property :contents-end up
) end
)))))
5495 ;; UP is a robust greater element containing changes.
5496 ;; We only need to extend its ending boundaries.
5497 (org-element--cache-shift-positions
5498 up offset
'(:contents-end
:end
))
5500 (when robust-flag
(setq robust-flag nil
)))
5501 (setq up
(org-element-property :parent up
)))
5502 ;; We're at top level element containing ELEMENT: if it's
5503 ;; altered by buffer modifications, it is first element in
5504 ;; cache to be removed. Otherwise, that first element is the
5507 ;; As a special case, do not remove BEFORE if it is a robust
5508 ;; container for current changes.
5509 (if (or (< (org-element-property :end before
) beg
) robust-flag
) after
5512 (defun org-element--cache-submit-request (beg end offset
)
5513 "Submit a new cache synchronization request for current buffer.
5514 BEG and END are buffer positions delimiting the minimal area
5515 where cache data should be removed. OFFSET is the size of the
5516 change, as an integer."
5517 (let ((next (car org-element--cache-sync-requests
))
5518 delete-to delete-from
)
5520 (zerop (aref next
5))
5521 (> (setq delete-to
(+ (aref next
2) (aref next
3))) end
)
5522 (<= (setq delete-from
(aref next
1)) end
))
5523 ;; Current changes can be merged with first sync request: we
5524 ;; can save a partial cache synchronization.
5526 (incf (aref next
3) offset
)
5527 ;; If last change happened within area to be removed, extend
5528 ;; boundaries of robust parents, if any. Otherwise, find
5529 ;; first element to remove and update request accordingly.
5530 (if (> beg delete-from
)
5531 (let ((up (aref next
4)))
5533 (org-element--cache-shift-positions
5534 up offset
'(:contents-end
:end
))
5535 (setq up
(org-element-property :parent up
))))
5536 (let ((first (org-element--cache-for-removal beg delete-to offset
)))
5538 (aset next
0 (org-element--cache-key first
))
5539 (aset next
1 (org-element-property :begin first
))
5540 (aset next
4 (org-element-property :parent first
))))))
5541 ;; Ensure cache is correct up to END. Also make sure that NEXT,
5542 ;; if any, is no longer a 0-phase request, thus ensuring that
5543 ;; phases are properly ordered. We need to provide OFFSET as
5544 ;; optional parameter since current modifications are not known
5545 ;; yet to the otherwise correct part of the cache (i.e, before
5546 ;; the first request).
5547 (when next
(org-element--cache-sync (current-buffer) end beg
))
5548 (let ((first (org-element--cache-for-removal beg end offset
)))
5550 (push (let ((beg (org-element-property :begin first
))
5551 (key (org-element--cache-key first
)))
5553 ;; When changes happen before the first known
5554 ;; element, re-parent and shift the rest of the
5556 ((> beg end
) (vector key beg nil offset nil
1))
5557 ;; Otherwise, we find the first non robust
5558 ;; element containing END. All elements between
5559 ;; FIRST and this one are to be removed.
5560 ((let ((first-end (org-element-property :end first
)))
5561 (and (> first-end end
)
5562 (vector key beg first-end offset first
0))))
5564 (let* ((element (org-element--cache-find end
))
5565 (end (org-element-property :end element
))
5567 (while (and (setq up
(org-element-property :parent up
))
5568 (>= (org-element-property :begin up
) beg
))
5569 (setq end
(org-element-property :end up
)
5571 (vector key beg end offset element
0)))))
5572 org-element--cache-sync-requests
)
5573 ;; No element to remove. No need to re-parent either.
5574 ;; Simply shift additional elements, if any, by OFFSET.
5575 (when org-element--cache-sync-requests
5576 (incf (aref (car org-element--cache-sync-requests
) 3) offset
)))))))
5579 ;;;; Public Functions
5582 (defun org-element-cache-reset (&optional all
)
5583 "Reset cache in current buffer.
5584 When optional argument ALL is non-nil, reset cache in all Org
5587 (dolist (buffer (if all
(buffer-list) (list (current-buffer))))
5588 (with-current-buffer buffer
5589 (when (org-element--cache-active-p)
5590 (org-set-local 'org-element--cache
5591 (avl-tree-create #'org-element--cache-compare
))
5592 (org-set-local 'org-element--cache-objects
(make-hash-table :test
#'eq
))
5593 (org-set-local 'org-element--cache-sync-keys
5594 (make-hash-table :weakness
'key
:test
#'eq
))
5595 (org-set-local 'org-element--cache-change-warning nil
)
5596 (org-set-local 'org-element--cache-sync-requests nil
)
5597 (org-set-local 'org-element--cache-sync-timer nil
)
5598 (add-hook 'before-change-functions
5599 #'org-element--cache-before-change nil t
)
5600 (add-hook 'after-change-functions
5601 #'org-element--cache-after-change nil t
)))))
5604 (defun org-element-cache-refresh (pos)
5605 "Refresh cache at position POS."
5606 (when (org-element--cache-active-p)
5607 (org-element--cache-sync (current-buffer) pos
)
5608 (org-element--cache-submit-request pos pos
0)
5609 (org-element--cache-set-timer (current-buffer))))
5615 ;; The first move is to implement a way to obtain the smallest element
5616 ;; containing point. This is the job of `org-element-at-point'. It
5617 ;; basically jumps back to the beginning of section containing point
5618 ;; and proceed, one element after the other, with
5619 ;; `org-element--current-element' until the container is found. Note:
5620 ;; When using `org-element-at-point', secondary values are never
5621 ;; parsed since the function focuses on elements, not on objects.
5623 ;; At a deeper level, `org-element-context' lists all elements and
5624 ;; objects containing point.
5626 ;; `org-element-nested-p' and `org-element-swap-A-B' may be used
5627 ;; internally by navigation and manipulation tools.
5631 (defun org-element-at-point ()
5632 "Determine closest element around point.
5634 Return value is a list like (TYPE PROPS) where TYPE is the type
5635 of the element and PROPS a plist of properties associated to the
5638 Possible types are defined in `org-element-all-elements'.
5639 Properties depend on element or object type, but always include
5640 `:begin', `:end', `:parent' and `:post-blank' properties.
5642 As a special case, if point is at the very beginning of the first
5643 item in a list or sub-list, returned element will be that list
5644 instead of the item. Likewise, if point is at the beginning of
5645 the first row of a table, returned element will be the table
5646 instead of the first row.
5648 When point is at the end of the buffer, return the innermost
5649 element ending there."
5650 (org-with-wide-buffer
5651 (let ((origin (point)))
5653 (skip-chars-backward " \r\t\n")
5655 ;; Within blank lines at the beginning of buffer, return nil.
5657 ;; Within blank lines right after a headline, return that
5659 ((org-with-limited-levels (org-at-heading-p))
5661 (org-element-headline-parser (point-max) t
))
5662 ;; Otherwise parse until we find element containing ORIGIN.
5664 (when (org-element--cache-active-p)
5665 (if (not org-element--cache
) (org-element-cache-reset)
5666 (org-element--cache-sync (current-buffer) origin
)))
5667 (org-element--parse-to origin
))))))
5670 (defun org-element-context (&optional element
)
5671 "Return smallest element or object around point.
5673 Return value is a list like (TYPE PROPS) where TYPE is the type
5674 of the element or object and PROPS a plist of properties
5677 Possible types are defined in `org-element-all-elements' and
5678 `org-element-all-objects'. Properties depend on element or
5679 object type, but always include `:begin', `:end', `:parent' and
5682 As a special case, if point is right after an object and not at
5683 the beginning of any other object, return that object.
5685 Optional argument ELEMENT, when non-nil, is the closest element
5686 containing point, as returned by `org-element-at-point'.
5687 Providing it allows for quicker computation."
5688 (catch 'objects-forbidden
5689 (org-with-wide-buffer
5690 (let* ((pos (point))
5691 (element (or element
(org-element-at-point)))
5692 (type (org-element-type element
)))
5693 ;; If point is inside an element containing objects or
5694 ;; a secondary string, narrow buffer to the container and
5695 ;; proceed with parsing. Otherwise, return ELEMENT.
5697 ;; At a parsed affiliated keyword, check if we're inside main
5699 ((let ((post (org-element-property :post-affiliated element
)))
5700 (and post
(< pos post
)))
5702 (let ((case-fold-search t
)) (looking-at org-element--affiliated-re
))
5704 ((not (member-ignore-case (match-string 1)
5705 org-element-parsed-keywords
))
5706 (throw 'objects-forbidden element
))
5707 ((< (match-end 0) pos
)
5708 (narrow-to-region (match-end 0) (line-end-position)))
5709 ((and (match-beginning 2)
5710 (>= pos
(match-beginning 2))
5711 (< pos
(match-end 2)))
5712 (narrow-to-region (match-beginning 2) (match-end 2)))
5713 (t (throw 'objects-forbidden element
)))
5714 ;; Also change type to retrieve correct restrictions.
5715 (setq type
'keyword
))
5716 ;; At an item, objects can only be located within tag, if any.
5718 (let ((tag (org-element-property :tag element
)))
5719 (if (not tag
) (throw 'objects-forbidden element
)
5721 (search-forward tag
(line-end-position))
5722 (goto-char (match-beginning 0))
5723 (if (and (>= pos
(point)) (< pos
(match-end 0)))
5724 (narrow-to-region (point) (match-end 0))
5725 (throw 'objects-forbidden element
)))))
5726 ;; At an headline or inlinetask, objects are in title.
5727 ((memq type
'(headline inlinetask
))
5728 (goto-char (org-element-property :begin element
))
5729 (skip-chars-forward "*")
5730 (if (and (> pos
(point)) (< pos
(line-end-position)))
5731 (narrow-to-region (point) (line-end-position))
5732 (throw 'objects-forbidden element
)))
5733 ;; At a paragraph, a table-row or a verse block, objects are
5734 ;; located within their contents.
5735 ((memq type
'(paragraph table-row verse-block
))
5736 (let ((cbeg (org-element-property :contents-begin element
))
5737 (cend (org-element-property :contents-end element
)))
5738 ;; CBEG is nil for table rules.
5739 (if (and cbeg cend
(>= pos cbeg
)
5740 (or (< pos cend
) (and (= pos cend
) (eobp))))
5741 (narrow-to-region cbeg cend
)
5742 (throw 'objects-forbidden element
))))
5743 ;; At a planning line, if point is at a timestamp, return it,
5744 ;; otherwise, return element.
5745 ((eq type
'planning
)
5746 (dolist (p '(:closed
:deadline
:scheduled
))
5747 (let ((timestamp (org-element-property p element
)))
5748 (when (and timestamp
5749 (<= (org-element-property :begin timestamp
) pos
)
5750 (> (org-element-property :end timestamp
) pos
))
5751 (throw 'objects-forbidden timestamp
))))
5752 ;; All other locations cannot contain objects: bail out.
5753 (throw 'objects-forbidden element
))
5754 (t (throw 'objects-forbidden element
)))
5755 (goto-char (point-min))
5756 (let ((restriction (org-element-restriction type
))
5758 (cache (cond ((not (org-element--cache-active-p)) nil
)
5759 (org-element--cache-objects
5760 (gethash element org-element--cache-objects
))
5761 (t (org-element-cache-reset) nil
)))
5762 next object-data last
)
5766 ;; When entering PARENT for the first time, get list
5767 ;; of objects within known so far. Store it in
5770 (let ((data (assq parent cache
)))
5771 (if data
(setq object-data data
)
5772 (push (setq object-data
(list parent nil
)) cache
))))
5773 ;; Find NEXT object for analysis.
5775 ;; If NEXT is non-nil, we already exhausted the
5776 ;; cache so we can parse buffer to find the object
5778 (if next
(setq next
(org-element--object-lex restriction
))
5779 ;; Otherwise, check if cache can help us.
5780 (let ((objects (cddr object-data
))
5781 (completep (nth 1 object-data
)))
5783 ((and (not objects
) completep
) (throw 'exit parent
))
5785 (setq next
(org-element--object-lex restriction
)))
5788 (org-element-property :end
(car objects
))))
5789 (if (>= cache-limit pos
)
5790 ;; Cache contains the information needed.
5791 (dolist (object objects
(throw 'exit parent
))
5792 (when (<= (org-element-property :begin object
)
5794 (if (>= (org-element-property :end object
)
5796 (throw 'found
(setq next object
))
5797 (throw 'exit parent
))))
5798 (goto-char cache-limit
)
5800 (org-element--object-lex restriction
))))))))
5801 ;; If we have a new object to analyze, store it in
5802 ;; cache. Otherwise record that there is nothing
5803 ;; more to parse in this element at this depth.
5805 (progn (org-element-put-property next
:parent parent
)
5806 (push next
(cddr object-data
)))
5807 (setcar (cdr object-data
) t
)))
5808 ;; Process NEXT, if any, in order to know if we need
5809 ;; to skip it, return it or move into it.
5810 (if (or (not next
) (> (org-element-property :begin next
) pos
))
5811 (throw 'exit
(or last parent
))
5812 (let ((end (org-element-property :end next
))
5813 (cbeg (org-element-property :contents-begin next
))
5814 (cend (org-element-property :contents-end next
)))
5816 ;; Skip objects ending before point. Also skip
5817 ;; objects ending at point unless it is also the
5818 ;; end of buffer, since we want to return the
5819 ;; innermost object.
5820 ((and (<= end pos
) (/= (point-max) end
))
5822 ;; For convenience, when object ends at POS,
5823 ;; without any space, store it in LAST, as we
5824 ;; will return it if no object starts here.
5825 (when (and (= end pos
)
5826 (not (memq (char-before) '(?\s ?
\t))))
5828 ;; If POS is within a container object, move
5829 ;; into that object.
5833 ;; At contents' end, if there is no
5834 ;; space before point, also move into
5835 ;; object, for consistency with
5836 ;; convenience feature above.
5838 (or (= (point-max) pos
)
5839 (not (memq (char-before pos
)
5842 (narrow-to-region (point) cend
)
5844 restriction
(org-element-restriction next
)
5847 ;; Otherwise, return NEXT.
5848 (t (throw 'exit next
)))))))
5849 ;; Store results in cache, if applicable.
5850 (org-element--cache-put element cache
)))))))
5852 (defun org-element-lineage (blob &optional types with-self
)
5853 "List all ancestors of a given element or object.
5855 BLOB is an object or element.
5857 When optional argument TYPES is a list of symbols, return the
5858 first element or object in the lineage whose type belongs to that
5861 When optional argument WITH-SELF is non-nil, lineage includes
5862 BLOB itself as the first element, and TYPES, if provided, also
5865 When BLOB is obtained through `org-element-context' or
5866 `org-element-at-point', only ancestors from its section can be
5867 found. There is no such limitation when BLOB belongs to a full
5869 (let ((up (if with-self blob
(org-element-property :parent blob
)))
5871 (while (and up
(not (memq (org-element-type up
) types
)))
5872 (unless types
(push up ancestors
))
5873 (setq up
(org-element-property :parent up
)))
5874 (if types up
(nreverse ancestors
))))
5876 (defun org-element-nested-p (elem-A elem-B
)
5877 "Non-nil when elements ELEM-A and ELEM-B are nested."
5878 (let ((beg-A (org-element-property :begin elem-A
))
5879 (beg-B (org-element-property :begin elem-B
))
5880 (end-A (org-element-property :end elem-A
))
5881 (end-B (org-element-property :end elem-B
)))
5882 (or (and (>= beg-A beg-B
) (<= end-A end-B
))
5883 (and (>= beg-B beg-A
) (<= end-B end-A
)))))
5885 (defun org-element-swap-A-B (elem-A elem-B
)
5886 "Swap elements ELEM-A and ELEM-B.
5887 Assume ELEM-B is after ELEM-A in the buffer. Leave point at the
5889 (goto-char (org-element-property :begin elem-A
))
5890 ;; There are two special cases when an element doesn't start at bol:
5891 ;; the first paragraph in an item or in a footnote definition.
5892 (let ((specialp (not (bolp))))
5893 ;; Only a paragraph without any affiliated keyword can be moved at
5894 ;; ELEM-A position in such a situation. Note that the case of
5895 ;; a footnote definition is impossible: it cannot contain two
5896 ;; paragraphs in a row because it cannot contain a blank line.
5898 (or (not (eq (org-element-type elem-B
) 'paragraph
))
5899 (/= (org-element-property :begin elem-B
)
5900 (org-element-property :contents-begin elem-B
))))
5901 (error "Cannot swap elements"))
5902 ;; In a special situation, ELEM-A will have no indentation. We'll
5903 ;; give it ELEM-B's (which will in, in turn, have no indentation).
5904 (let* ((ind-B (when specialp
5905 (goto-char (org-element-property :begin elem-B
))
5906 (org-get-indentation)))
5907 (beg-A (org-element-property :begin elem-A
))
5908 (end-A (save-excursion
5909 (goto-char (org-element-property :end elem-A
))
5910 (skip-chars-backward " \r\t\n")
5912 (beg-B (org-element-property :begin elem-B
))
5913 (end-B (save-excursion
5914 (goto-char (org-element-property :end elem-B
))
5915 (skip-chars-backward " \r\t\n")
5917 ;; Store inner overlays responsible for visibility status.
5918 ;; We also need to store their boundaries as they will be
5919 ;; removed from buffer.
5924 (and (>= (overlay-start o
) beg-A
)
5925 (<= (overlay-end o
) end-A
)
5926 (list o
(overlay-start o
) (overlay-end o
))))
5927 (overlays-in beg-A end-A
)))
5930 (and (>= (overlay-start o
) beg-B
)
5931 (<= (overlay-end o
) end-B
)
5932 (list o
(overlay-start o
) (overlay-end o
))))
5933 (overlays-in beg-B end-B
)))))
5935 (body-A (buffer-substring beg-A end-A
))
5936 (body-B (delete-and-extract-region beg-B end-B
)))
5939 (setq body-B
(replace-regexp-in-string "\\`[ \t]*" "" body-B
))
5940 (org-indent-to-column ind-B
))
5942 ;; Restore ex ELEM-A overlays.
5943 (let ((offset (- beg-B beg-A
)))
5944 (dolist (o (car overlays
))
5945 (move-overlay (car o
) (+ (nth 1 o
) offset
) (+ (nth 2 o
) offset
)))
5947 (delete-region beg-A end-A
)
5949 ;; Restore ex ELEM-B overlays.
5950 (dolist (o (cdr overlays
))
5951 (move-overlay (car o
) (- (nth 1 o
) offset
) (- (nth 2 o
) offset
))))
5952 (goto-char (org-element-property :end elem-B
)))))
5954 (defun org-element-remove-indentation (s &optional n
)
5955 "Remove maximum common indentation in string S and return it.
5956 When optional argument N is a positive integer, remove exactly
5957 that much characters from indentation, if possible, or return
5958 S as-is otherwise. Unlike to `org-remove-indentation', this
5959 function doesn't call `untabify' on S."
5963 (goto-char (point-min))
5964 ;; Find maximum common indentation, if not specified.
5966 (let ((min-ind (point-max)))
5968 (while (re-search-forward "^[ \t]*\\S-" nil t
)
5969 (let ((ind (1- (current-column))))
5970 (if (zerop ind
) (throw 'exit s
)
5971 (setq min-ind
(min min-ind ind
))))))
5974 ;; Remove exactly N indentation, but give up if not possible.
5976 (let ((ind (progn (skip-chars-forward " \t") (current-column))))
5977 (cond ((eolp) (delete-region (line-beginning-position) (point)))
5978 ((< ind n
) (throw 'exit s
))
5979 (t (org-indent-line-to (- ind n
))))
5985 (provide 'org-element
)
5988 ;; generated-autoload-file: "org-loaddefs.el"
5991 ;;; org-element.el ends here