1 ;;; org-element.el --- Parser And Applications for Org syntax
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
119 (eval-when-compile (require 'cl
))
125 ;;; Definitions And Rules
127 ;; Define elements, greater elements and specify recursive objects,
128 ;; along with the affiliated keywords recognized. Also set up
129 ;; restrictions on recursive objects combinations.
131 ;; `org-element-update-syntax' builds proper syntax regexps according
134 (defvar org-element-paragraph-separate nil
135 "Regexp to separate paragraphs in an Org buffer.
136 In the case of lines starting with \"#\" and \":\", this regexp
137 is not sufficient to know if point is at a paragraph ending. See
138 `org-element-paragraph-parser' for more information.")
140 (defvar org-element--object-regexp nil
141 "Regexp possibly matching the beginning of an object.
142 This regexp allows false positives. Dedicated parser (e.g.,
143 `org-export-bold-parser') will take care of further filtering.
144 Radio links are not matched by this regexp, as they are treated
145 specially in `org-element--object-lex'.")
147 (defun org-element--set-regexps ()
148 "Build variable syntax regexps."
149 (setq org-element-paragraph-separate
151 ;; Headlines, inlinetasks.
152 org-outline-regexp
"\\|"
153 ;; Footnote definitions.
154 "\\[\\(?:[0-9]+\\|fn:[-_[:word:]]+\\)\\]" "\\|"
160 ;; Tables (any type).
162 "\\+\\(?:-+\\+\\)+[ \t]*$" "\\|"
163 ;; Comments, keyword-like or block-like constructs.
164 ;; Blocks and keywords with dual values need to be
166 "#\\(?: \\|$\\|\\+\\(?:"
168 "\\S-+\\(?:\\[.*\\]\\)?:[ \t]*\\)\\)"
170 ;; Drawers (any type) and fixed-width areas. Drawers
171 ;; need to be double-checked.
172 ":\\(?: \\|$\\|[-_[:word:]]+:[ \t]*$\\)" "\\|"
174 "-\\{5,\\}[ \t]*$" "\\|"
175 ;; LaTeX environments.
176 "\\\\begin{\\([A-Za-z0-9*]+\\)}" "\\|"
178 (regexp-quote org-clock-string
) "\\|"
180 (let ((term (case org-plain-list-ordered-item-terminator
181 (?\
) ")") (?.
"\\.") (otherwise "[.)]")))
182 (alpha (and org-list-allow-alphabetical
"\\|[A-Za-z]")))
183 (concat "\\(?:[-+*]\\|\\(?:[0-9]+" alpha
"\\)" term
"\\)"
184 "\\(?:[ \t]\\|$\\)"))
186 org-element--object-regexp
187 (mapconcat #'identity
188 (let ((link-types (regexp-opt org-link-types
)))
191 "\\(?:[_^][-{(*+.,[:alnum:]]\\)"
192 ;; Bold, code, italic, strike-through, underline
196 (nth 2 org-emphasis-regexp-components
)))
198 (concat "\\<" link-types
":")
199 ;; Objects starting with "[": regular link,
200 ;; footnote reference, statistics cookie,
201 ;; timestamp (inactive).
202 "\\[\\(?:fn:\\|\\(?:[0-9]\\|\\(?:%\\|/[0-9]*\\)\\]\\)\\|\\[\\)"
203 ;; Objects starting with "@": export snippets.
205 ;; Objects starting with "{": macro.
207 ;; Objects starting with "<" : timestamp
208 ;; (active, diary), target, radio target and
210 (concat "<\\(?:%%\\|<\\|[0-9]\\|" link-types
"\\)")
211 ;; Objects starting with "$": latex fragment.
213 ;; Objects starting with "\": line break,
214 ;; entity, latex fragment.
215 "\\\\\\(?:[a-zA-Z[(]\\|\\\\[ \t]*$\\|_ +\\)"
216 ;; Objects starting with raw text: inline Babel
217 ;; source block, inline Babel call.
218 "\\(?:call\\|src\\)_"))
221 (org-element--set-regexps)
224 (defun org-element-update-syntax ()
225 "Update parser internals."
227 (org-element--set-regexps)
228 (org-element-cache-reset 'all
))
230 (defconst org-element-all-elements
231 '(babel-call center-block clock comment comment-block diary-sexp drawer
232 dynamic-block example-block export-block fixed-width
233 footnote-definition headline horizontal-rule inlinetask item
234 keyword latex-environment node-property paragraph plain-list
235 planning property-drawer quote-block section
236 special-block src-block table table-row verse-block
)
237 "Complete list of element types.")
239 (defconst org-element-greater-elements
240 '(center-block drawer dynamic-block footnote-definition headline inlinetask
241 item plain-list property-drawer quote-block section
243 "List of recursive element types aka Greater Elements.")
245 (defconst org-element-all-objects
246 '(bold code entity export-snippet footnote-reference inline-babel-call
247 inline-src-block italic line-break latex-fragment link macro
248 radio-target statistics-cookie strike-through subscript superscript
249 table-cell target timestamp underline verbatim
)
250 "Complete list of object types.")
252 (defconst org-element-recursive-objects
253 '(bold footnote-reference italic link subscript radio-target strike-through
254 superscript table-cell underline
)
255 "List of recursive object types.")
257 (defconst org-element-object-containers
258 (append org-element-recursive-objects
'(paragraph table-row verse-block
))
259 "List of object or element types that can directly contain objects.")
261 (defvar org-element-block-name-alist
262 '(("CENTER" . org-element-center-block-parser
)
263 ("COMMENT" . org-element-comment-block-parser
)
264 ("EXAMPLE" . org-element-example-block-parser
)
265 ("QUOTE" . org-element-quote-block-parser
)
266 ("SRC" . org-element-src-block-parser
)
267 ("VERSE" . org-element-verse-block-parser
))
268 "Alist between block names and the associated parsing function.
269 Names must be uppercase. Any block whose name has no association
270 is parsed with `org-element-special-block-parser'.")
272 (defconst org-element-affiliated-keywords
273 '("CAPTION" "DATA" "HEADER" "HEADERS" "LABEL" "NAME" "PLOT" "RESNAME" "RESULT"
274 "RESULTS" "SOURCE" "SRCNAME" "TBLNAME")
275 "List of affiliated keywords as strings.
276 By default, all keywords setting attributes (e.g., \"ATTR_LATEX\")
277 are affiliated keywords and need not to be in this list.")
279 (defconst org-element-keyword-translation-alist
280 '(("DATA" .
"NAME") ("LABEL" .
"NAME") ("RESNAME" .
"NAME")
281 ("SOURCE" .
"NAME") ("SRCNAME" .
"NAME") ("TBLNAME" .
"NAME")
282 ("RESULT" .
"RESULTS") ("HEADERS" .
"HEADER"))
283 "Alist of usual translations for keywords.
284 The key is the old name and the value the new one. The property
285 holding their value will be named after the translated name.")
287 (defconst org-element-multiple-keywords
'("CAPTION" "HEADER")
288 "List of affiliated keywords that can occur more than once in an element.
290 Their value will be consed into a list of strings, which will be
291 returned as the value of the property.
293 This list is checked after translations have been applied. See
294 `org-element-keyword-translation-alist'.
296 By default, all keywords setting attributes (e.g., \"ATTR_LATEX\")
297 allow multiple occurrences and need not to be in this list.")
299 (defconst org-element-parsed-keywords
'("CAPTION")
300 "List of affiliated keywords whose value can be parsed.
302 Their value will be stored as a secondary string: a list of
305 This list is checked after translations have been applied. See
306 `org-element-keyword-translation-alist'.")
308 (defconst org-element--parsed-properties-alist
309 (mapcar (lambda (k) (cons k
(intern (concat ":" (downcase k
)))))
310 org-element-parsed-keywords
)
311 "Alist of parsed keywords and associated properties.
312 This is generated from `org-element-parsed-keywords', which
315 (defconst org-element-dual-keywords
'("CAPTION" "RESULTS")
316 "List of affiliated keywords which can have a secondary value.
318 In Org syntax, they can be written with optional square brackets
319 before the colons. For example, RESULTS keyword can be
320 associated to a hash value with the following:
322 #+RESULTS[hash-string]: some-source
324 This list is checked after translations have been applied. See
325 `org-element-keyword-translation-alist'.")
327 (defconst org-element--affiliated-re
328 (format "[ \t]*#\\+\\(?:%s\\):[ \t]*"
330 ;; Dual affiliated keywords.
331 (format "\\(?1:%s\\)\\(?:\\[\\(.*\\)\\]\\)?"
332 (regexp-opt org-element-dual-keywords
))
334 ;; Regular affiliated keywords.
335 (format "\\(?1:%s\\)"
338 (lambda (k) (member k org-element-dual-keywords
))
339 org-element-affiliated-keywords
)))
341 ;; Export attributes.
342 "\\(?1:ATTR_[-_A-Za-z0-9]+\\)"))
343 "Regexp matching any affiliated keyword.
345 Keyword name is put in match group 1. Moreover, if keyword
346 belongs to `org-element-dual-keywords', put the dual value in
349 Don't modify it, set `org-element-affiliated-keywords' instead.")
351 (defconst org-element-object-restrictions
352 (let* ((standard-set (remq 'table-cell org-element-all-objects
))
353 (standard-set-no-line-break (remq 'line-break standard-set
)))
354 `((bold ,@standard-set
)
355 (footnote-reference ,@standard-set
)
356 (headline ,@standard-set-no-line-break
)
357 (inlinetask ,@standard-set-no-line-break
)
358 (italic ,@standard-set
)
359 (item ,@standard-set-no-line-break
)
360 (keyword ,@(remq 'footnote-reference standard-set
))
361 ;; Ignore all links excepted plain links in a link description.
362 ;; Also ignore radio-targets and line breaks.
363 (link bold code entity export-snippet inline-babel-call inline-src-block
364 italic latex-fragment macro plain-link statistics-cookie
365 strike-through subscript superscript underline verbatim
)
366 (paragraph ,@standard-set
)
367 ;; Remove any variable object from radio target as it would
368 ;; prevent it from being properly recognized.
369 (radio-target bold code entity italic latex-fragment strike-through
370 subscript superscript underline superscript
)
371 (strike-through ,@standard-set
)
372 (subscript ,@standard-set
)
373 (superscript ,@standard-set
)
374 ;; Ignore inline babel call and inline src block as formulas are
375 ;; possible. Also ignore line breaks and statistics cookies.
376 (table-cell bold code entity export-snippet footnote-reference italic
377 latex-fragment link macro radio-target strike-through
378 subscript superscript target timestamp underline verbatim
)
379 (table-row table-cell
)
380 (underline ,@standard-set
)
381 (verse-block ,@standard-set
)))
382 "Alist of objects restrictions.
384 key is an element or object type containing objects and value is
385 a list of types that can be contained within an element or object
388 For example, in a `radio-target' object, one can only find
389 entities, latex-fragments, subscript, superscript and text
392 This alist also applies to secondary string. For example, an
393 `headline' type element doesn't directly contain objects, but
394 still has an entry since one of its properties (`:title') does.")
396 (defconst org-element-secondary-value-alist
400 "Alist between element types and locations of secondary values.")
402 (defconst org-element--pair-square-table
403 (let ((table (make-syntax-table)))
404 (modify-syntax-entry ?\
[ "(]" table
)
405 (modify-syntax-entry ?\
] ")[" table
)
406 (dolist (char '(?\
{ ?\
} ?\
( ?\
) ?\
< ?\
>) table
)
407 (modify-syntax-entry char
" " table
)))
408 "Table used internally to pair only square brackets.
409 Other brackets are treated as spaces.")
413 ;;; Accessors and Setters
415 ;; Provide four accessors: `org-element-type', `org-element-property'
416 ;; `org-element-contents' and `org-element-restriction'.
418 ;; Setter functions allow to modify elements by side effect. There is
419 ;; `org-element-put-property', `org-element-set-contents'. These
420 ;; low-level functions are useful to build a parse tree.
422 ;; `org-element-adopt-element', `org-element-set-element',
423 ;; `org-element-extract-element' and `org-element-insert-before' are
424 ;; high-level functions useful to modify a parse tree.
426 ;; `org-element-secondary-p' is a predicate used to know if a given
427 ;; object belongs to a secondary string. `org-element-copy' returns
428 ;; an element or object, stripping its parent property in the process.
430 (defsubst org-element-type
(element)
431 "Return type of ELEMENT.
433 The function returns the type of the element or object provided.
434 It can also return the following special value:
435 `plain-text' for a string
436 `org-data' for a complete document
437 nil in any other case."
439 ((not (consp element
)) (and (stringp element
) 'plain-text
))
440 ((symbolp (car element
)) (car element
))))
442 (defsubst org-element-property
(property element
)
443 "Extract the value from the PROPERTY of an ELEMENT."
444 (if (stringp element
) (get-text-property 0 property element
)
445 (plist-get (nth 1 element
) property
)))
447 (defsubst org-element-contents
(element)
448 "Extract contents from an ELEMENT."
449 (cond ((not (consp element
)) nil
)
450 ((symbolp (car element
)) (nthcdr 2 element
))
453 (defsubst org-element-restriction
(element)
454 "Return restriction associated to ELEMENT.
455 ELEMENT can be an element, an object or a symbol representing an
456 element or object type."
457 (cdr (assq (if (symbolp element
) element
(org-element-type element
))
458 org-element-object-restrictions
)))
460 (defsubst org-element-put-property
(element property value
)
461 "In ELEMENT set PROPERTY to VALUE.
462 Return modified element."
463 (if (stringp element
) (org-add-props element nil property value
)
464 (setcar (cdr element
) (plist-put (nth 1 element
) property value
))
467 (defsubst org-element-set-contents
(element &rest contents
)
468 "Set ELEMENT contents to CONTENTS."
469 (cond ((not element
) (list contents
))
470 ((not (symbolp (car element
))) contents
)
471 ((cdr element
) (setcdr (cdr element
) contents
))
472 (t (nconc element contents
))))
474 (defun org-element-secondary-p (object)
475 "Non-nil when OBJECT directly belongs to a secondary string.
476 Return value is the property name, as a keyword, or nil."
477 (let* ((parent (org-element-property :parent object
))
478 (properties (cdr (assq (org-element-type parent
)
479 org-element-secondary-value-alist
))))
481 (dolist (p properties
)
482 (and (memq object
(org-element-property p parent
))
485 (defsubst org-element-adopt-elements
(parent &rest children
)
486 "Append elements to the contents of another element.
488 PARENT is an element or object. CHILDREN can be elements,
489 objects, or a strings.
491 The function takes care of setting `:parent' property for CHILD.
492 Return parent element."
493 (if (not children
) parent
494 ;; Link every child to PARENT. If PARENT is nil, it is a secondary
495 ;; string: parent is the list itself.
496 (dolist (child children
)
497 (org-element-put-property child
:parent
(or parent children
)))
498 ;; Add CHILDREN at the end of PARENT contents.
500 (apply #'org-element-set-contents
502 (nconc (org-element-contents parent
) children
)))
503 ;; Return modified PARENT element.
504 (or parent children
)))
506 (defun org-element-extract-element (element)
507 "Extract ELEMENT from parse tree.
508 Remove element from the parse tree by side-effect, and return it
509 with its `:parent' property stripped out."
510 (let ((parent (org-element-property :parent element
))
511 (secondary (org-element-secondary-p element
)))
513 (org-element-put-property
515 (delq element
(org-element-property secondary parent
)))
516 (apply #'org-element-set-contents
518 (delq element
(org-element-contents parent
))))
519 ;; Return ELEMENT with its :parent removed.
520 (org-element-put-property element
:parent nil
)))
522 (defun org-element-insert-before (element location
)
523 "Insert ELEMENT before LOCATION in parse tree.
524 LOCATION is an element, object or string within the parse tree.
525 Parse tree is modified by side effect."
526 (let* ((parent (org-element-property :parent location
))
527 (property (org-element-secondary-p location
))
528 (siblings (if property
(org-element-property property parent
)
529 (org-element-contents parent
)))
530 ;; Special case: LOCATION is the first element of an
531 ;; independent secondary string (e.g. :title property). Add
533 (specialp (and (not property
)
535 (eq (car parent
) location
))))
536 ;; Install ELEMENT at the appropriate POSITION within SIBLINGS.
538 ((or (null siblings
) (eq (car siblings
) location
))
539 (push element siblings
))
540 ((null location
) (nconc siblings
(list element
)))
541 (t (let ((previous (cadr (memq location
(reverse siblings
)))))
543 (error "No location found to insert element")
544 (let ((next (memq previous siblings
)))
545 (setcdr next
(cons element
(cdr next
))))))))
546 ;; Store SIBLINGS at appropriate place in parse tree.
548 (specialp (setcdr parent
(copy-sequence parent
)) (setcar parent element
))
549 (property (org-element-put-property parent property siblings
))
550 (t (apply #'org-element-set-contents parent siblings
)))
551 ;; Set appropriate :parent property.
552 (org-element-put-property element
:parent parent
)))
554 (defun org-element-set-element (old new
)
555 "Replace element or object OLD with element or object NEW.
556 The function takes care of setting `:parent' property for NEW."
557 ;; Ensure OLD and NEW have the same parent.
558 (org-element-put-property new
:parent
(org-element-property :parent old
))
559 (if (or (memq (org-element-type old
) '(plain-text nil
))
560 (memq (org-element-type new
) '(plain-text nil
)))
561 ;; We cannot replace OLD with NEW since one of them is not an
562 ;; object or element. We take the long path.
563 (progn (org-element-insert-before new old
)
564 (org-element-extract-element old
))
565 ;; Since OLD is going to be changed into NEW by side-effect, first
566 ;; make sure that every element or object within NEW has OLD as
568 (dolist (blob (org-element-contents new
))
569 (org-element-put-property blob
:parent old
))
570 ;; Transfer contents.
571 (apply #'org-element-set-contents old
(org-element-contents new
))
572 ;; Overwrite OLD's properties with NEW's.
573 (setcar (cdr old
) (nth 1 new
))
575 (setcar old
(car new
))))
577 (defun org-element-create (type &optional props
&rest children
)
578 "Create a new element of type TYPE.
579 Optional argument PROPS, when non-nil, is a plist defining the
580 properties of the element. CHILDREN can be elements, objects or
582 (apply #'org-element-adopt-elements
(list type props
) children
))
584 (defun org-element-copy (datum)
585 "Return a copy of DATUM.
586 DATUM is an element, object, string or nil. `:parent' property
587 is cleared and contents are removed in the process."
589 (let ((type (org-element-type datum
)))
591 (org-data (list 'org-data nil
))
592 (plain-text (substring-no-properties datum
))
593 ((nil) (copy-sequence datum
))
595 (list type
(plist-put (copy-sequence (nth 1 datum
)) :parent nil
)))))))
601 ;; For each greater element type, we define a parser and an
604 ;; A parser returns the element or object as the list described above.
605 ;; Most of them accepts no argument. Though, exceptions exist. Hence
606 ;; every element containing a secondary string (see
607 ;; `org-element-secondary-value-alist') will accept an optional
608 ;; argument to toggle parsing of these secondary strings. Moreover,
609 ;; `item' parser requires current list's structure as its first
612 ;; An interpreter accepts two arguments: the list representation of
613 ;; the element or object, and its contents. The latter may be nil,
614 ;; depending on the element or object considered. It returns the
615 ;; appropriate Org syntax, as a string.
617 ;; Parsing functions must follow the naming convention:
618 ;; org-element-TYPE-parser, where TYPE is greater element's type, as
619 ;; defined in `org-element-greater-elements'.
621 ;; Similarly, interpreting functions must follow the naming
622 ;; convention: org-element-TYPE-interpreter.
624 ;; With the exception of `headline' and `item' types, greater elements
625 ;; cannot contain other greater elements of their own type.
627 ;; Beside implementing a parser and an interpreter, adding a new
628 ;; greater element requires to tweak `org-element--current-element'.
629 ;; Moreover, the newly defined type must be added to both
630 ;; `org-element-all-elements' and `org-element-greater-elements'.
635 (defun org-element-center-block-parser (limit affiliated
)
636 "Parse a center block.
638 LIMIT bounds the search. AFFILIATED is a list of which CAR is
639 the buffer position at the beginning of the first affiliated
640 keyword and CDR is a plist of affiliated keywords along with
643 Return a list whose CAR is `center-block' and CDR is a plist
644 containing `:begin', `:end', `:contents-begin', `:contents-end',
645 `:post-blank' and `:post-affiliated' keywords.
647 Assume point is at the beginning of the block."
648 (let ((case-fold-search t
))
649 (if (not (save-excursion
650 (re-search-forward "^[ \t]*#\\+END_CENTER[ \t]*$" limit t
)))
651 ;; Incomplete block: parse it as a paragraph.
652 (org-element-paragraph-parser limit affiliated
)
653 (let ((block-end-line (match-beginning 0)))
654 (let* ((begin (car affiliated
))
655 (post-affiliated (point))
656 ;; Empty blocks have no contents.
657 (contents-begin (progn (forward-line)
658 (and (< (point) block-end-line
)
660 (contents-end (and contents-begin block-end-line
))
661 (pos-before-blank (progn (goto-char block-end-line
)
665 (skip-chars-forward " \r\t\n" limit
)
666 (if (eobp) (point) (line-beginning-position)))))
671 :contents-begin contents-begin
672 :contents-end contents-end
673 :post-blank
(count-lines pos-before-blank end
)
674 :post-affiliated post-affiliated
)
675 (cdr affiliated
))))))))
677 (defun org-element-center-block-interpreter (center-block contents
)
678 "Interpret CENTER-BLOCK element as Org syntax.
679 CONTENTS is the contents of the element."
680 (format "#+BEGIN_CENTER\n%s#+END_CENTER" contents
))
685 (defun org-element-drawer-parser (limit affiliated
)
688 LIMIT bounds the search. AFFILIATED is a list of which CAR is
689 the buffer position at the beginning of the first affiliated
690 keyword and CDR is a plist of affiliated keywords along with
693 Return a list whose CAR is `drawer' and CDR is a plist containing
694 `:drawer-name', `:begin', `:end', `:contents-begin',
695 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
697 Assume point is at beginning of drawer."
698 (let ((case-fold-search t
))
699 (if (not (save-excursion (re-search-forward "^[ \t]*:END:[ \t]*$" limit t
)))
700 ;; Incomplete drawer: parse it as a paragraph.
701 (org-element-paragraph-parser limit affiliated
)
703 (let* ((drawer-end-line (match-beginning 0))
704 (name (progn (looking-at org-drawer-regexp
)
705 (org-match-string-no-properties 1)))
706 (begin (car affiliated
))
707 (post-affiliated (point))
708 ;; Empty drawers have no contents.
709 (contents-begin (progn (forward-line)
710 (and (< (point) drawer-end-line
)
712 (contents-end (and contents-begin drawer-end-line
))
713 (pos-before-blank (progn (goto-char drawer-end-line
)
716 (end (progn (skip-chars-forward " \r\t\n" limit
)
717 (if (eobp) (point) (line-beginning-position)))))
723 :contents-begin contents-begin
724 :contents-end contents-end
725 :post-blank
(count-lines pos-before-blank end
)
726 :post-affiliated post-affiliated
)
727 (cdr affiliated
))))))))
729 (defun org-element-drawer-interpreter (drawer contents
)
730 "Interpret DRAWER element as Org syntax.
731 CONTENTS is the contents of the element."
732 (format ":%s:\n%s:END:"
733 (org-element-property :drawer-name drawer
)
739 (defun org-element-dynamic-block-parser (limit affiliated
)
740 "Parse a dynamic block.
742 LIMIT bounds the search. AFFILIATED is a list of which CAR is
743 the buffer position at the beginning of the first affiliated
744 keyword and CDR is a plist of affiliated keywords along with
747 Return a list whose CAR is `dynamic-block' and CDR is a plist
748 containing `:block-name', `:begin', `:end', `:contents-begin',
749 `:contents-end', `:arguments', `:post-blank' and
750 `:post-affiliated' keywords.
752 Assume point is at beginning of dynamic block."
753 (let ((case-fold-search t
))
754 (if (not (save-excursion
755 (re-search-forward "^[ \t]*#\\+END:?[ \t]*$" limit t
)))
756 ;; Incomplete block: parse it as a paragraph.
757 (org-element-paragraph-parser limit affiliated
)
758 (let ((block-end-line (match-beginning 0)))
760 (let* ((name (progn (looking-at org-dblock-start-re
)
761 (org-match-string-no-properties 1)))
762 (arguments (org-match-string-no-properties 3))
763 (begin (car affiliated
))
764 (post-affiliated (point))
765 ;; Empty blocks have no contents.
766 (contents-begin (progn (forward-line)
767 (and (< (point) block-end-line
)
769 (contents-end (and contents-begin block-end-line
))
770 (pos-before-blank (progn (goto-char block-end-line
)
773 (end (progn (skip-chars-forward " \r\t\n" limit
)
774 (if (eobp) (point) (line-beginning-position)))))
781 :contents-begin contents-begin
782 :contents-end contents-end
783 :post-blank
(count-lines pos-before-blank end
)
784 :post-affiliated post-affiliated
)
785 (cdr affiliated
)))))))))
787 (defun org-element-dynamic-block-interpreter (dynamic-block contents
)
788 "Interpret DYNAMIC-BLOCK element as Org syntax.
789 CONTENTS is the contents of the element."
790 (format "#+BEGIN: %s%s\n%s#+END:"
791 (org-element-property :block-name dynamic-block
)
792 (let ((args (org-element-property :arguments dynamic-block
)))
793 (and args
(concat " " args
)))
797 ;;;; Footnote Definition
799 (defun org-element-footnote-definition-parser (limit affiliated
)
800 "Parse a footnote definition.
802 LIMIT bounds the search. AFFILIATED is a list of which CAR is
803 the buffer position at the beginning of the first affiliated
804 keyword and CDR is a plist of affiliated keywords along with
807 Return a list whose CAR is `footnote-definition' and CDR is
808 a plist containing `:label', `:begin' `:end', `:contents-begin',
809 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
811 Assume point is at the beginning of the footnote definition."
813 (let* ((label (progn (looking-at org-footnote-definition-re
)
814 (org-match-string-no-properties 1)))
815 (begin (car affiliated
))
816 (post-affiliated (point))
817 (ending (save-excursion
821 (concat org-outline-regexp-bol
"\\|"
822 org-footnote-definition-re
"\\|"
823 "^\\([ \t]*\n\\)\\{2,\\}") limit
'move
))
826 (contents-begin (progn
828 (skip-chars-forward " \r\t\n" ending
)
829 (cond ((= (point) ending
) nil
)
830 ((= (line-beginning-position) begin
) (point))
831 (t (line-beginning-position)))))
832 (contents-end (and contents-begin ending
))
833 (end (progn (goto-char ending
)
834 (skip-chars-forward " \r\t\n" limit
)
835 (if (eobp) (point) (line-beginning-position)))))
836 (list 'footnote-definition
841 :contents-begin contents-begin
842 :contents-end contents-end
843 :post-blank
(count-lines ending end
)
844 :post-affiliated post-affiliated
)
845 (cdr affiliated
))))))
847 (defun org-element-footnote-definition-interpreter (footnote-definition contents
)
848 "Interpret FOOTNOTE-DEFINITION element as Org syntax.
849 CONTENTS is the contents of the footnote-definition."
850 (concat (format "[%s]" (org-element-property :label footnote-definition
))
857 (defun org-element--get-node-properties ()
858 "Return node properties associated to headline at point.
859 Upcase property names. It avoids confusion between properties
860 obtained through property drawer and default properties from the
861 parser (e.g. `:end' and :END:). Return value is a plist."
864 (when (org-looking-at-p org-planning-line-re
) (forward-line))
865 (when (looking-at org-property-drawer-re
)
867 (let ((end (match-end 0)) properties
)
868 (while (< (line-end-position) end
)
869 (looking-at org-property-re
)
870 (push (org-match-string-no-properties 3) properties
)
871 (push (intern (concat ":" (upcase (match-string 2)))) properties
)
875 (defun org-element--get-time-properties ()
876 "Return time properties associated to headline at point.
877 Return value is a plist."
879 (when (progn (forward-line) (looking-at org-planning-line-re
))
880 (let ((end (line-end-position)) plist
)
881 (while (re-search-forward org-keyword-time-not-clock-regexp end t
)
882 (goto-char (match-end 1))
883 (skip-chars-forward " \t")
884 (let ((keyword (match-string 1))
885 (time (org-element-timestamp-parser)))
886 (cond ((equal keyword org-scheduled-string
)
887 (setq plist
(plist-put plist
:scheduled time
)))
888 ((equal keyword org-deadline-string
)
889 (setq plist
(plist-put plist
:deadline time
)))
890 (t (setq plist
(plist-put plist
:closed time
))))))
893 (defun org-element-headline-parser (limit &optional raw-secondary-p
)
896 Return a list whose CAR is `headline' and CDR is a plist
897 containing `:raw-value', `:title', `:begin', `:end',
898 `:pre-blank', `:contents-begin' and `:contents-end', `:level',
899 `:priority', `:tags', `:todo-keyword',`:todo-type', `:scheduled',
900 `:deadline', `:closed', `:archivedp', `:commentedp'
901 `:footnote-section-p', `:post-blank' and `:post-affiliated'
904 The plist also contains any property set in the property drawer,
905 with its name in upper cases and colons added at the
906 beginning (e.g., `:CUSTOM_ID').
908 LIMIT is a buffer position bounding the search.
910 When RAW-SECONDARY-P is non-nil, headline's title will not be
911 parsed as a secondary string, but as a plain string instead.
913 Assume point is at beginning of the headline."
915 (let* ((begin (point))
916 (level (prog1 (org-reduced-level (skip-chars-forward "*"))
917 (skip-chars-forward " \t")))
918 (todo (and org-todo-regexp
919 (let (case-fold-search) (looking-at org-todo-regexp
))
920 (progn (goto-char (match-end 0))
921 (skip-chars-forward " \t")
924 (and todo
(if (member todo org-done-keywords
) 'done
'todo
)))
925 (priority (and (looking-at "\\[#.\\][ \t]*")
926 (progn (goto-char (match-end 0))
927 (aref (match-string 0) 2))))
929 (and (let (case-fold-search) (looking-at org-comment-string
))
930 (goto-char (match-end 0))))
931 (title-start (point))
932 (tags (when (re-search-forward
933 (org-re "[ \t]+\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
936 (goto-char (match-beginning 0))
937 (org-split-string (match-string 1) ":")))
940 (buffer-substring-no-properties title-start title-end
)))
941 (archivedp (member org-archive-tag tags
))
942 (footnote-section-p (and org-footnote-section
943 (string= org-footnote-section raw-value
)))
944 (standard-props (org-element--get-node-properties))
945 (time-props (org-element--get-time-properties))
946 (end (min (save-excursion (org-end-of-subtree t t
)) limit
))
947 (pos-after-head (progn (forward-line) (point)))
948 (contents-begin (save-excursion
949 (skip-chars-forward " \r\t\n" end
)
950 (and (/= (point) end
) (line-beginning-position))))
951 (contents-end (and contents-begin
952 (progn (goto-char end
)
953 (skip-chars-backward " \r\t\n")
959 (list :raw-value raw-value
963 (if (not contents-begin
) 0
964 (count-lines pos-after-head contents-begin
))
965 :contents-begin contents-begin
966 :contents-end contents-end
972 :post-blank
(count-lines
973 (or contents-end pos-after-head
)
975 :footnote-section-p footnote-section-p
977 :commentedp commentedp
978 :post-affiliated begin
)
981 (org-element-put-property
983 (if raw-secondary-p raw-value
984 (let ((title (org-element--parse-objects
985 (progn (goto-char title-start
)
986 (skip-chars-forward " \t")
988 (progn (goto-char title-end
)
989 (skip-chars-backward " \t")
992 (org-element-restriction 'headline
))))
993 (dolist (datum title title
)
994 (org-element-put-property datum
:parent headline
)))))))))
996 (defun org-element-headline-interpreter (headline contents
)
997 "Interpret HEADLINE element as Org syntax.
998 CONTENTS is the contents of the element."
999 (let* ((level (org-element-property :level headline
))
1000 (todo (org-element-property :todo-keyword headline
))
1001 (priority (org-element-property :priority headline
))
1002 (title (org-element-interpret-data
1003 (org-element-property :title headline
)))
1004 (tags (let ((tag-list (org-element-property :tags headline
)))
1006 (format ":%s:" (mapconcat #'identity tag-list
":")))))
1007 (commentedp (org-element-property :commentedp headline
))
1008 (pre-blank (or (org-element-property :pre-blank headline
) 0))
1010 (concat (make-string (if org-odd-levels-only
(1- (* level
2)) level
)
1012 (and todo
(concat " " todo
))
1013 (and commentedp
(concat " " org-comment-string
))
1014 (and priority
(format " [#%c]" priority
))
1016 (if (and org-footnote-section
1017 (org-element-property :footnote-section-p headline
))
1018 org-footnote-section
1025 ((zerop org-tags-column
) (format " %s" tags
))
1026 ((< org-tags-column
0)
1029 (max (- (+ org-tags-column
(length heading
) (length tags
))) 1)
1034 (make-string (max (- org-tags-column
(length heading
)) 1) ?\s
)
1036 (make-string (1+ pre-blank
) ?
\n)
1042 (defun org-element-inlinetask-parser (limit &optional raw-secondary-p
)
1043 "Parse an inline task.
1045 Return a list whose CAR is `inlinetask' and CDR is a plist
1046 containing `:title', `:begin', `:end', `:contents-begin' and
1047 `:contents-end', `:level', `:priority', `:raw-value', `:tags',
1048 `:todo-keyword', `:todo-type', `:scheduled', `:deadline',
1049 `:closed', `:post-blank' and `:post-affiliated' keywords.
1051 The plist also contains any property set in the property drawer,
1052 with its name in upper cases and colons added at the
1053 beginning (e.g., `:CUSTOM_ID').
1055 When optional argument RAW-SECONDARY-P is non-nil, inline-task's
1056 title will not be parsed as a secondary string, but as a plain
1059 Assume point is at beginning of the inline task."
1061 (let* ((begin (point))
1062 (level (prog1 (org-reduced-level (skip-chars-forward "*"))
1063 (skip-chars-forward " \t")))
1064 (todo (and org-todo-regexp
1065 (let (case-fold-search) (looking-at org-todo-regexp
))
1066 (progn (goto-char (match-end 0))
1067 (skip-chars-forward " \t")
1069 (todo-type (and todo
1070 (if (member todo org-done-keywords
) 'done
'todo
)))
1071 (priority (and (looking-at "\\[#.\\][ \t]*")
1072 (progn (goto-char (match-end 0))
1073 (aref (match-string 0) 2))))
1074 (title-start (point))
1075 (tags (when (re-search-forward
1076 (org-re "[ \t]+\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
1079 (goto-char (match-beginning 0))
1080 (org-split-string (match-string 1) ":")))
1082 (raw-value (org-trim
1083 (buffer-substring-no-properties title-start title-end
)))
1084 (task-end (save-excursion
1086 (and (re-search-forward org-outline-regexp-bol limit t
)
1087 (org-looking-at-p "END[ \t]*$")
1088 (line-beginning-position))))
1089 (standard-props (and task-end
(org-element--get-node-properties)))
1090 (time-props (and task-end
(org-element--get-time-properties)))
1091 (contents-begin (progn (forward-line)
1092 (and task-end
(< (point) task-end
) (point))))
1093 (contents-end (and contents-begin task-end
))
1094 (before-blank (if (not task-end
) (point)
1095 (goto-char task-end
)
1098 (end (progn (skip-chars-forward " \r\t\n" limit
)
1099 (if (eobp) (point) (line-beginning-position))))
1103 (list :raw-value raw-value
1106 :contents-begin contents-begin
1107 :contents-end contents-end
1112 :todo-type todo-type
1113 :post-blank
(count-lines before-blank end
)
1114 :post-affiliated begin
)
1117 (org-element-put-property
1119 (if raw-secondary-p raw-value
1120 (let ((title (org-element--parse-objects
1121 (progn (goto-char title-start
)
1122 (skip-chars-forward " \t")
1124 (progn (goto-char title-end
)
1125 (skip-chars-backward " \t")
1128 (org-element-restriction 'inlinetask
))))
1129 (dolist (datum title title
)
1130 (org-element-put-property datum
:parent inlinetask
))))))))
1132 (defun org-element-inlinetask-interpreter (inlinetask contents
)
1133 "Interpret INLINETASK element as Org syntax.
1134 CONTENTS is the contents of inlinetask."
1135 (let* ((level (org-element-property :level inlinetask
))
1136 (todo (org-element-property :todo-keyword inlinetask
))
1137 (priority (org-element-property :priority inlinetask
))
1138 (title (org-element-interpret-data
1139 (org-element-property :title inlinetask
)))
1140 (tags (let ((tag-list (org-element-property :tags inlinetask
)))
1142 (format ":%s:" (mapconcat 'identity tag-list
":")))))
1143 (task (concat (make-string level ?
*)
1144 (and todo
(concat " " todo
))
1145 (and priority
(format " [#%c]" priority
))
1146 (and title
(concat " " title
)))))
1151 ((zerop org-tags-column
) (format " %s" tags
))
1152 ((< org-tags-column
0)
1155 (max (- (+ org-tags-column
(length task
) (length tags
))) 1)
1160 (make-string (max (- org-tags-column
(length task
)) 1) ?
)
1162 ;; Prefer degenerate inlinetasks when there are no
1167 (make-string level ?
*) " END")))))
1172 (defun org-element-item-parser (limit struct
&optional raw-secondary-p
)
1175 STRUCT is the structure of the plain list.
1177 Return a list whose CAR is `item' and CDR is a plist containing
1178 `:bullet', `:begin', `:end', `:contents-begin', `:contents-end',
1179 `:checkbox', `:counter', `:tag', `:structure', `:post-blank' and
1180 `:post-affiliated' keywords.
1182 When optional argument RAW-SECONDARY-P is non-nil, item's tag, if
1183 any, will not be parsed as a secondary string, but as a plain
1186 Assume point is at the beginning of the item."
1189 (looking-at org-list-full-item-re
)
1190 (let* ((begin (point))
1191 (bullet (org-match-string-no-properties 1))
1192 (checkbox (let ((box (match-string 3)))
1193 (cond ((equal "[ ]" box
) 'off
)
1194 ((equal "[X]" box
) 'on
)
1195 ((equal "[-]" box
) 'trans
))))
1196 (counter (let ((c (match-string 2)))
1200 ((string-match "[A-Za-z]" c
)
1201 (- (string-to-char (upcase (match-string 0 c
)))
1203 ((string-match "[0-9]+" c
)
1204 (string-to-number (match-string 0 c
)))))))
1205 (end (progn (goto-char (nth 6 (assq (point) struct
)))
1206 (if (bolp) (point) (line-beginning-position 2))))
1209 ;; Ignore tags in un-ordered lists: they are just
1210 ;; a part of item's body.
1211 (if (and (match-beginning 4)
1212 (save-match-data (string-match "[.)]" bullet
)))
1215 (skip-chars-forward " \r\t\n" end
)
1216 (cond ((= (point) end
) nil
)
1217 ;; If first line isn't empty, contents really
1218 ;; start at the text after item's meta-data.
1219 ((= (line-beginning-position) begin
) (point))
1220 (t (line-beginning-position)))))
1221 (contents-end (and contents-begin
1222 (progn (goto-char end
)
1223 (skip-chars-backward " \r\t\n")
1224 (line-beginning-position 2))))
1227 (list :bullet bullet
1230 :contents-begin contents-begin
1231 :contents-end contents-end
1235 :post-blank
(count-lines (or contents-end begin
) end
)
1236 :post-affiliated begin
))))
1237 (org-element-put-property
1239 (let ((raw (org-list-get-tag begin struct
)))
1241 (if raw-secondary-p raw
1242 (let ((tag (org-element--parse-objects
1243 (match-beginning 4) (match-end 4) nil
1244 (org-element-restriction 'item
))))
1245 (dolist (datum tag tag
)
1246 (org-element-put-property datum
:parent item
))))))))))
1248 (defun org-element-item-interpreter (item contents
)
1249 "Interpret ITEM element as Org syntax.
1250 CONTENTS is the contents of the element."
1251 (let* ((bullet (let ((bullet (org-element-property :bullet item
)))
1252 (org-list-bullet-string
1253 (cond ((not (string-match "[0-9a-zA-Z]" bullet
)) "- ")
1254 ((eq org-plain-list-ordered-item-terminator ?\
)) "1)")
1256 (checkbox (org-element-property :checkbox item
))
1257 (counter (org-element-property :counter item
))
1258 (tag (let ((tag (org-element-property :tag item
)))
1259 (and tag
(org-element-interpret-data tag
))))
1260 ;; Compute indentation.
1261 (ind (make-string (length bullet
) 32))
1262 (item-starts-with-par-p
1263 (eq (org-element-type (car (org-element-contents item
)))
1268 (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 (plain-list 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 (property-drawer 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 (quote-block 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 (limit)
1511 LIMIT bounds the search.
1513 Return a list whose CAR is `section' and CDR is a plist
1514 containing `:begin', `:end', `:contents-begin', `contents-end',
1515 `:post-blank' and `:post-affiliated' keywords."
1517 ;; Beginning of section is the beginning of the first non-blank
1518 ;; line after previous headline.
1519 (let ((begin (point))
1520 (end (progn (org-with-limited-levels (outline-next-heading))
1522 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
1528 :contents-begin begin
1529 :contents-end pos-before-blank
1530 :post-blank
(count-lines pos-before-blank end
)
1531 :post-affiliated begin
)))))
1533 (defun org-element-section-interpreter (section contents
)
1534 "Interpret SECTION element as Org syntax.
1535 CONTENTS is the contents of the element."
1541 (defun org-element-special-block-parser (limit affiliated
)
1542 "Parse a special block.
1544 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1545 the buffer position at the beginning of the first affiliated
1546 keyword and CDR is a plist of affiliated keywords along with
1549 Return a list whose CAR is `special-block' and CDR is a plist
1550 containing `:type', `:begin', `:end', `:contents-begin',
1551 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
1553 Assume point is at the beginning of the block."
1554 (let* ((case-fold-search t
)
1555 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
1556 (match-string-no-properties 1))))
1557 (if (not (save-excursion
1559 (format "^[ \t]*#\\+END_%s[ \t]*$" (regexp-quote type
))
1561 ;; Incomplete block: parse it as a paragraph.
1562 (org-element-paragraph-parser limit affiliated
)
1563 (let ((block-end-line (match-beginning 0)))
1565 (let* ((begin (car affiliated
))
1566 (post-affiliated (point))
1567 ;; Empty blocks have no contents.
1568 (contents-begin (progn (forward-line)
1569 (and (< (point) block-end-line
)
1571 (contents-end (and contents-begin block-end-line
))
1572 (pos-before-blank (progn (goto-char block-end-line
)
1575 (end (progn (skip-chars-forward " \r\t\n" limit
)
1576 (if (eobp) (point) (line-beginning-position)))))
1577 (list 'special-block
1582 :contents-begin contents-begin
1583 :contents-end contents-end
1584 :post-blank
(count-lines pos-before-blank end
)
1585 :post-affiliated post-affiliated
)
1586 (cdr affiliated
)))))))))
1588 (defun org-element-special-block-interpreter (special-block contents
)
1589 "Interpret SPECIAL-BLOCK element as Org syntax.
1590 CONTENTS is the contents of the element."
1591 (let ((block-type (org-element-property :type special-block
)))
1592 (format "#+BEGIN_%s\n%s#+END_%s" block-type contents block-type
)))
1598 ;; For each element, a parser and an interpreter are also defined.
1599 ;; Both follow the same naming convention used for greater elements.
1601 ;; Also, as for greater elements, adding a new element type is done
1602 ;; through the following steps: implement a parser and an interpreter,
1603 ;; tweak `org-element--current-element' so that it recognizes the new
1604 ;; type and add that new type to `org-element-all-elements'.
1606 ;; As a special case, when the newly defined type is a block type,
1607 ;; `org-element-block-name-alist' has to be modified accordingly.
1612 (defun org-element-babel-call-parser (limit affiliated
)
1613 "Parse a babel call.
1615 LIMIT bounds the search. AFFILIATED is a list of which car is
1616 the buffer position at the beginning of the first affiliated
1617 keyword and cdr is a plist of affiliated keywords along with
1620 Return a list whose car is `babel-call' and cdr is a plist
1621 containing `:call', `:inside-header', `:arguments',
1622 `:end-header', `:begin', `:end', `:value', `:post-blank' and
1623 `:post-affiliated' as keywords."
1625 (let* ((begin (car affiliated
))
1626 (post-affiliated (point))
1627 (value (progn (search-forward ":" nil t
)
1629 (buffer-substring-no-properties
1630 (point) (line-end-position)))))
1631 (pos-before-blank (progn (forward-line) (point)))
1632 (end (progn (skip-chars-forward " \r\t\n" limit
)
1633 (if (eobp) (point) (line-beginning-position))))
1636 "\\([^()\n]+?\\)\\(?:\\[\\(.*?\\)\\]\\)?(\\(.*\\))[ \t]*\\(.*\\)"
1640 (list :call
(and valid-value
(match-string 1 value
))
1641 :inside-header
(and valid-value
1642 (org-string-nw-p (match-string 2 value
)))
1643 :arguments
(and valid-value
1644 (org-string-nw-p (match-string 3 value
)))
1645 :end-header
(and valid-value
1646 (org-string-nw-p (match-string 4 value
)))
1650 :post-blank
(count-lines pos-before-blank end
)
1651 :post-affiliated post-affiliated
)
1652 (cdr affiliated
))))))
1654 (defun org-element-babel-call-interpreter (babel-call contents
)
1655 "Interpret BABEL-CALL element as Org syntax.
1658 (org-element-property :call babel-call
)
1659 (let ((h (org-element-property :inside-header babel-call
)))
1660 (and h
(format "[%s]" h
)))
1661 (concat "(" (org-element-property :arguments babel-call
) ")")
1662 (let ((h (org-element-property :end-header babel-call
)))
1663 (and h
(concat " " h
)))))
1668 (defun org-element-clock-parser (limit)
1671 LIMIT bounds the search.
1673 Return a list whose CAR is `clock' and CDR is a plist containing
1674 `:status', `:value', `:time', `:begin', `:end', `:post-blank' and
1675 `:post-affiliated' as keywords."
1677 (let* ((case-fold-search nil
)
1679 (value (progn (search-forward org-clock-string
(line-end-position) t
)
1680 (skip-chars-forward " \t")
1681 (org-element-timestamp-parser)))
1682 (duration (and (search-forward " => " (line-end-position) t
)
1683 (progn (skip-chars-forward " \t")
1684 (looking-at "\\(\\S-+\\)[ \t]*$"))
1685 (org-match-string-no-properties 1)))
1686 (status (if duration
'closed
'running
))
1687 (post-blank (let ((before-blank (progn (forward-line) (point))))
1688 (skip-chars-forward " \r\t\n" limit
)
1689 (skip-chars-backward " \t")
1690 (unless (bolp) (end-of-line))
1691 (count-lines before-blank
(point))))
1694 (list :status status
1699 :post-blank post-blank
1700 :post-affiliated begin
)))))
1702 (defun org-element-clock-interpreter (clock contents
)
1703 "Interpret CLOCK element as Org syntax.
1705 (concat org-clock-string
" "
1706 (org-element-timestamp-interpreter
1707 (org-element-property :value clock
) nil
)
1708 (let ((duration (org-element-property :duration clock
)))
1713 (org-split-string duration
":")))))))
1718 (defun org-element-comment-parser (limit affiliated
)
1721 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1722 the buffer position at the beginning of the first affiliated
1723 keyword and CDR is a plist of affiliated keywords along with
1726 Return a list whose CAR is `comment' and CDR is a plist
1727 containing `:begin', `:end', `:value', `:post-blank',
1728 `:post-affiliated' keywords.
1730 Assume point is at comment beginning."
1732 (let* ((begin (car affiliated
))
1733 (post-affiliated (point))
1734 (value (prog2 (looking-at "[ \t]*# ?")
1735 (buffer-substring-no-properties
1736 (match-end 0) (line-end-position))
1739 ;; Get comments ending.
1741 (while (and (< (point) limit
) (looking-at "[ \t]*#\\( \\|$\\)"))
1742 ;; Accumulate lines without leading hash and first
1747 (buffer-substring-no-properties
1748 (match-end 0) (line-end-position))))
1751 (end (progn (goto-char com-end
)
1752 (skip-chars-forward " \r\t\n" limit
)
1753 (if (eobp) (point) (line-beginning-position)))))
1759 :post-blank
(count-lines com-end end
)
1760 :post-affiliated post-affiliated
)
1761 (cdr affiliated
))))))
1763 (defun org-element-comment-interpreter (comment contents
)
1764 "Interpret COMMENT element as Org syntax.
1766 (replace-regexp-in-string "^" "# " (org-element-property :value comment
)))
1771 (defun org-element-comment-block-parser (limit affiliated
)
1772 "Parse an export block.
1774 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1775 the buffer position at the beginning of the first affiliated
1776 keyword and CDR is a plist of affiliated keywords along with
1779 Return a list whose CAR is `comment-block' and CDR is a plist
1780 containing `:begin', `:end', `:value', `:post-blank' and
1781 `:post-affiliated' keywords.
1783 Assume point is at comment block beginning."
1784 (let ((case-fold-search t
))
1785 (if (not (save-excursion
1786 (re-search-forward "^[ \t]*#\\+END_COMMENT[ \t]*$" limit t
)))
1787 ;; Incomplete block: parse it as a paragraph.
1788 (org-element-paragraph-parser limit affiliated
)
1789 (let ((contents-end (match-beginning 0)))
1791 (let* ((begin (car affiliated
))
1792 (post-affiliated (point))
1793 (contents-begin (progn (forward-line) (point)))
1794 (pos-before-blank (progn (goto-char contents-end
)
1797 (end (progn (skip-chars-forward " \r\t\n" limit
)
1798 (if (eobp) (point) (line-beginning-position))))
1799 (value (buffer-substring-no-properties
1800 contents-begin contents-end
)))
1801 (list 'comment-block
1806 :post-blank
(count-lines pos-before-blank end
)
1807 :post-affiliated post-affiliated
)
1808 (cdr affiliated
)))))))))
1810 (defun org-element-comment-block-interpreter (comment-block contents
)
1811 "Interpret COMMENT-BLOCK element as Org syntax.
1813 (format "#+BEGIN_COMMENT\n%s#+END_COMMENT"
1814 (org-element-normalize-string
1815 (org-remove-indentation
1816 (org-element-property :value comment-block
)))))
1821 (defun org-element-diary-sexp-parser (limit affiliated
)
1822 "Parse a diary sexp.
1824 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1825 the buffer position at the beginning of the first affiliated
1826 keyword and CDR is a plist of affiliated keywords along with
1829 Return a list whose CAR is `diary-sexp' and CDR is a plist
1830 containing `:begin', `:end', `:value', `:post-blank' and
1831 `:post-affiliated' keywords."
1833 (let ((begin (car affiliated
))
1834 (post-affiliated (point))
1835 (value (progn (looking-at "\\(%%(.*\\)[ \t]*$")
1836 (org-match-string-no-properties 1)))
1837 (pos-before-blank (progn (forward-line) (point)))
1838 (end (progn (skip-chars-forward " \r\t\n" limit
)
1839 (if (eobp) (point) (line-beginning-position)))))
1845 :post-blank
(count-lines pos-before-blank end
)
1846 :post-affiliated post-affiliated
)
1847 (cdr affiliated
))))))
1849 (defun org-element-diary-sexp-interpreter (diary-sexp contents
)
1850 "Interpret DIARY-SEXP as Org syntax.
1852 (org-element-property :value diary-sexp
))
1857 (defun org-element-example-block-parser (limit affiliated
)
1858 "Parse an example block.
1860 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1861 the buffer position at the beginning of the first affiliated
1862 keyword and CDR is a plist of affiliated keywords along with
1865 Return a list whose CAR is `example-block' and CDR is a plist
1866 containing `:begin', `:end', `:number-lines', `:preserve-indent',
1867 `:retain-labels', `:use-labels', `:label-fmt', `:switches',
1868 `:value', `:post-blank' and `:post-affiliated' keywords."
1869 (let ((case-fold-search t
))
1870 (if (not (save-excursion
1871 (re-search-forward "^[ \t]*#\\+END_EXAMPLE[ \t]*$" limit t
)))
1872 ;; Incomplete block: parse it as a paragraph.
1873 (org-element-paragraph-parser limit affiliated
)
1874 (let ((contents-end (match-beginning 0)))
1878 (looking-at "^[ \t]*#\\+BEGIN_EXAMPLE\\(?: +\\(.*\\)\\)?")
1879 (org-match-string-no-properties 1)))
1880 ;; Switches analysis
1882 (cond ((not switches
) nil
)
1883 ((string-match "-n\\>" switches
) 'new
)
1884 ((string-match "+n\\>" switches
) 'continued
)))
1886 (and switches
(string-match "-i\\>" switches
)))
1887 ;; Should labels be retained in (or stripped from) example
1891 (not (string-match "-r\\>" switches
))
1892 (and number-lines
(string-match "-k\\>" switches
))))
1893 ;; What should code-references use - labels or
1898 (not (string-match "-k\\>" switches
)))))
1901 (string-match "-l +\"\\([^\"\n]+\\)\"" switches
)
1902 (match-string 1 switches
)))
1903 ;; Standard block parsing.
1904 (begin (car affiliated
))
1905 (post-affiliated (point))
1906 (block-ind (progn (skip-chars-forward " \t") (current-column)))
1907 (contents-begin (progn (forward-line) (point)))
1908 (value (org-element-remove-indentation
1909 (org-unescape-code-in-string
1910 (buffer-substring-no-properties
1911 contents-begin contents-end
))
1913 (pos-before-blank (progn (goto-char contents-end
)
1916 (end (progn (skip-chars-forward " \r\t\n" limit
)
1917 (if (eobp) (point) (line-beginning-position)))))
1918 (list 'example-block
1924 :number-lines number-lines
1925 :preserve-indent preserve-indent
1926 :retain-labels retain-labels
1927 :use-labels use-labels
1928 :label-fmt label-fmt
1929 :post-blank
(count-lines pos-before-blank end
)
1930 :post-affiliated post-affiliated
)
1931 (cdr affiliated
)))))))))
1933 (defun org-element-example-block-interpreter (example-block contents
)
1934 "Interpret EXAMPLE-BLOCK element as Org syntax.
1936 (let ((switches (org-element-property :switches example-block
))
1937 (value (org-element-property :value example-block
)))
1938 (concat "#+BEGIN_EXAMPLE" (and switches
(concat " " switches
)) "\n"
1939 (org-element-normalize-string
1940 (org-escape-code-in-string
1941 (if (or org-src-preserve-indentation
1942 (org-element-property :preserve-indent example-block
))
1944 (org-element-remove-indentation value
))))
1950 (defun org-element-export-block-parser (limit affiliated
)
1951 "Parse an export block.
1953 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1954 the buffer position at the beginning of the first affiliated
1955 keyword and CDR is a plist of affiliated keywords along with
1958 Return a list whose CAR is `export-block' and CDR is a plist
1959 containing `:begin', `:end', `:type', `:value', `:post-blank' and
1960 `:post-affiliated' keywords.
1962 Assume point is at export-block beginning."
1963 (let* ((case-fold-search t
)
1964 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
1965 (upcase (org-match-string-no-properties 1)))))
1966 (if (not (save-excursion
1968 (format "^[ \t]*#\\+END_%s[ \t]*$" type
) limit t
)))
1969 ;; Incomplete block: parse it as a paragraph.
1970 (org-element-paragraph-parser limit affiliated
)
1971 (let ((contents-end (match-beginning 0)))
1973 (let* ((begin (car affiliated
))
1974 (post-affiliated (point))
1975 (contents-begin (progn (forward-line) (point)))
1976 (pos-before-blank (progn (goto-char contents-end
)
1979 (end (progn (skip-chars-forward " \r\t\n" limit
)
1980 (if (eobp) (point) (line-beginning-position))))
1981 (value (buffer-substring-no-properties contents-begin
1989 :post-blank
(count-lines pos-before-blank end
)
1990 :post-affiliated post-affiliated
)
1991 (cdr affiliated
)))))))))
1993 (defun org-element-export-block-interpreter (export-block contents
)
1994 "Interpret EXPORT-BLOCK element as Org syntax.
1996 (let ((type (org-element-property :type export-block
)))
1997 (concat (format "#+BEGIN_%s\n" type
)
1998 (org-element-property :value export-block
)
1999 (format "#+END_%s" type
))))
2004 (defun org-element-fixed-width-parser (limit affiliated
)
2005 "Parse a fixed-width section.
2007 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2008 the buffer position at the beginning of the first affiliated
2009 keyword and CDR is a plist of affiliated keywords along with
2012 Return a list whose CAR is `fixed-width' and CDR is a plist
2013 containing `:begin', `:end', `:value', `:post-blank' and
2014 `:post-affiliated' keywords.
2016 Assume point is at the beginning of the fixed-width area."
2018 (let* ((begin (car affiliated
))
2019 (post-affiliated (point))
2023 (while (and (< (point) limit
)
2024 (looking-at "[ \t]*:\\( \\|$\\)"))
2025 ;; Accumulate text without starting colons.
2028 (buffer-substring-no-properties
2029 (match-end 0) (point-at-eol))
2033 (end (progn (skip-chars-forward " \r\t\n" limit
)
2034 (if (eobp) (point) (line-beginning-position)))))
2040 :post-blank
(count-lines end-area end
)
2041 :post-affiliated post-affiliated
)
2042 (cdr affiliated
))))))
2044 (defun org-element-fixed-width-interpreter (fixed-width contents
)
2045 "Interpret FIXED-WIDTH element as Org syntax.
2047 (let ((value (org-element-property :value fixed-width
)))
2049 (replace-regexp-in-string
2051 (if (string-match "\n\\'" value
) (substring value
0 -
1) value
)))))
2054 ;;;; Horizontal Rule
2056 (defun org-element-horizontal-rule-parser (limit affiliated
)
2057 "Parse an horizontal rule.
2059 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2060 the buffer position at the beginning of the first affiliated
2061 keyword and CDR is a plist of affiliated keywords along with
2064 Return a list whose CAR is `horizontal-rule' and CDR is a plist
2065 containing `:begin', `:end', `:post-blank' and `:post-affiliated'
2068 (let ((begin (car affiliated
))
2069 (post-affiliated (point))
2070 (post-hr (progn (forward-line) (point)))
2071 (end (progn (skip-chars-forward " \r\t\n" limit
)
2072 (if (eobp) (point) (line-beginning-position)))))
2073 (list 'horizontal-rule
2077 :post-blank
(count-lines post-hr end
)
2078 :post-affiliated post-affiliated
)
2079 (cdr affiliated
))))))
2081 (defun org-element-horizontal-rule-interpreter (horizontal-rule contents
)
2082 "Interpret HORIZONTAL-RULE element as Org syntax.
2089 (defun org-element-keyword-parser (limit affiliated
)
2090 "Parse a keyword at point.
2092 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2093 the buffer position at the beginning of the first affiliated
2094 keyword and CDR is a plist of affiliated keywords along with
2097 Return a list whose CAR is `keyword' and CDR is a plist
2098 containing `:key', `:value', `:begin', `:end', `:post-blank' and
2099 `:post-affiliated' keywords."
2101 ;; An orphaned affiliated keyword is considered as a regular
2102 ;; keyword. In this case AFFILIATED is nil, so we take care of
2103 ;; this corner case.
2104 (let ((begin (or (car affiliated
) (point)))
2105 (post-affiliated (point))
2106 (key (progn (looking-at "[ \t]*#\\+\\(\\S-+*\\):")
2107 (upcase (org-match-string-no-properties 1))))
2108 (value (org-trim (buffer-substring-no-properties
2109 (match-end 0) (point-at-eol))))
2110 (pos-before-blank (progn (forward-line) (point)))
2111 (end (progn (skip-chars-forward " \r\t\n" limit
)
2112 (if (eobp) (point) (line-beginning-position)))))
2119 :post-blank
(count-lines pos-before-blank end
)
2120 :post-affiliated post-affiliated
)
2121 (cdr affiliated
))))))
2123 (defun org-element-keyword-interpreter (keyword contents
)
2124 "Interpret KEYWORD element as Org syntax.
2127 (org-element-property :key keyword
)
2128 (org-element-property :value keyword
)))
2131 ;;;; Latex Environment
2133 (defconst org-element--latex-begin-environment
2134 "^[ \t]*\\\\begin{\\([A-Za-z0-9*]+\\)}"
2135 "Regexp matching the beginning of a LaTeX environment.
2136 The environment is captured by the first group.
2138 See also `org-element--latex-end-environment'.")
2140 (defconst org-element--latex-end-environment
2141 "\\\\end{%s}[ \t]*$"
2142 "Format string matching the ending of a LaTeX environment.
2143 See also `org-element--latex-begin-environment'.")
2145 (defun org-element-latex-environment-parser (limit affiliated
)
2146 "Parse a LaTeX environment.
2148 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2149 the buffer position at the beginning of the first affiliated
2150 keyword and CDR is a plist of affiliated keywords along with
2153 Return a list whose CAR is `latex-environment' and CDR is a plist
2154 containing `:begin', `:end', `:value', `:post-blank' and
2155 `:post-affiliated' keywords.
2157 Assume point is at the beginning of the latex environment."
2159 (let ((case-fold-search t
)
2160 (code-begin (point)))
2161 (looking-at org-element--latex-begin-environment
)
2162 (if (not (re-search-forward (format org-element--latex-end-environment
2163 (regexp-quote (match-string 1)))
2165 ;; Incomplete latex environment: parse it as a paragraph.
2166 (org-element-paragraph-parser limit affiliated
)
2167 (let* ((code-end (progn (forward-line) (point)))
2168 (begin (car affiliated
))
2169 (value (buffer-substring-no-properties code-begin code-end
))
2170 (end (progn (skip-chars-forward " \r\t\n" limit
)
2171 (if (eobp) (point) (line-beginning-position)))))
2172 (list 'latex-environment
2177 :post-blank
(count-lines code-end end
)
2178 :post-affiliated code-begin
)
2179 (cdr affiliated
))))))))
2181 (defun org-element-latex-environment-interpreter (latex-environment contents
)
2182 "Interpret LATEX-ENVIRONMENT element as Org syntax.
2184 (org-element-property :value latex-environment
))
2189 (defun org-element-node-property-parser (limit)
2190 "Parse a node-property at point.
2192 LIMIT bounds the search.
2194 Return a list whose CAR is `node-property' and CDR is a plist
2195 containing `:key', `:value', `:begin', `:end', `:post-blank' and
2196 `:post-affiliated' keywords."
2197 (looking-at org-property-re
)
2198 (let ((case-fold-search t
)
2200 (key (org-match-string-no-properties 2))
2201 (value (org-match-string-no-properties 3))
2202 (end (save-excursion
2204 (if (re-search-forward org-property-re limit t
)
2205 (line-beginning-position)
2207 (list 'node-property
2213 :post-affiliated begin
))))
2215 (defun org-element-node-property-interpreter (node-property contents
)
2216 "Interpret NODE-PROPERTY element as Org syntax.
2218 (format org-property-format
2219 (format ":%s:" (org-element-property :key node-property
))
2220 (or (org-element-property :value node-property
) "")))
2225 (defun org-element-paragraph-parser (limit affiliated
)
2228 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2229 the buffer position at the beginning of the first affiliated
2230 keyword and CDR is a plist of affiliated keywords along with
2233 Return a list whose CAR is `paragraph' and CDR is a plist
2234 containing `:begin', `:end', `:contents-begin' and
2235 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
2237 Assume point is at the beginning of the paragraph."
2239 (let* ((begin (car affiliated
))
2240 (contents-begin (point))
2242 (let ((case-fold-search t
))
2244 ;; A matching `org-element-paragraph-separate' is not
2245 ;; necessarily the end of the paragraph. In particular,
2246 ;; drawers, blocks or LaTeX environments opening lines
2247 ;; must be closed. Moreover keywords with a secondary
2248 ;; value must belong to "dual keywords".
2251 ((not (and (re-search-forward
2252 org-element-paragraph-separate limit
'move
)
2253 (progn (beginning-of-line) t
))))
2254 ((looking-at org-drawer-regexp
)
2256 (re-search-forward "^[ \t]*:END:[ \t]*$" limit t
)))
2257 ((looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
2260 (format "^[ \t]*#\\+END_%s[ \t]*$"
2261 (regexp-quote (match-string 1)))
2263 ((looking-at org-element--latex-begin-environment
)
2266 (format org-element--latex-end-environment
2267 (regexp-quote (match-string 1)))
2269 ((looking-at "[ \t]*#\\+\\(\\S-+\\)\\[.*\\]:")
2270 (member-ignore-case (match-string 1)
2271 org-element-dual-keywords
))
2272 ;; Everything else is unambiguous.
2275 (if (= (point) limit
) limit
2276 (goto-char (line-beginning-position)))))
2277 (contents-end (save-excursion
2278 (skip-chars-backward " \r\t\n" contents-begin
)
2279 (line-beginning-position 2)))
2280 (end (progn (skip-chars-forward " \r\t\n" limit
)
2281 (if (eobp) (point) (line-beginning-position)))))
2286 :contents-begin contents-begin
2287 :contents-end contents-end
2288 :post-blank
(count-lines before-blank end
)
2289 :post-affiliated contents-begin
)
2290 (cdr affiliated
))))))
2292 (defun org-element-paragraph-interpreter (paragraph contents
)
2293 "Interpret PARAGRAPH element as Org syntax.
2294 CONTENTS is the contents of the element."
2300 (defun org-element-planning-parser (limit)
2303 LIMIT bounds the search.
2305 Return a list whose CAR is `planning' and CDR is a plist
2306 containing `:closed', `:deadline', `:scheduled', `:begin',
2307 `:end', `:post-blank' and `:post-affiliated' keywords."
2309 (let* ((case-fold-search nil
)
2311 (post-blank (let ((before-blank (progn (forward-line) (point))))
2312 (skip-chars-forward " \r\t\n" limit
)
2313 (skip-chars-backward " \t")
2314 (unless (bolp) (end-of-line))
2315 (count-lines before-blank
(point))))
2317 closed deadline scheduled
)
2319 (while (re-search-forward org-keyword-time-not-clock-regexp end t
)
2320 (goto-char (match-end 1))
2321 (skip-chars-forward " \t" end
)
2322 (let ((keyword (match-string 1))
2323 (time (org-element-timestamp-parser)))
2324 (cond ((equal keyword org-closed-string
) (setq closed time
))
2325 ((equal keyword org-deadline-string
) (setq deadline time
))
2326 (t (setq scheduled time
)))))
2328 (list :closed closed
2330 :scheduled scheduled
2333 :post-blank post-blank
2334 :post-affiliated begin
)))))
2336 (defun org-element-planning-interpreter (planning contents
)
2337 "Interpret PLANNING element as Org syntax.
2342 (list (let ((deadline (org-element-property :deadline planning
)))
2344 (concat org-deadline-string
" "
2345 (org-element-timestamp-interpreter deadline nil
))))
2346 (let ((scheduled (org-element-property :scheduled planning
)))
2348 (concat org-scheduled-string
" "
2349 (org-element-timestamp-interpreter scheduled nil
))))
2350 (let ((closed (org-element-property :closed planning
)))
2352 (concat org-closed-string
" "
2353 (org-element-timestamp-interpreter closed nil
))))))
2359 (defun org-element-src-block-parser (limit affiliated
)
2362 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2363 the buffer position at the beginning of the first affiliated
2364 keyword and CDR is a plist of affiliated keywords along with
2367 Return a list whose CAR is `src-block' and CDR is a plist
2368 containing `:language', `:switches', `:parameters', `:begin',
2369 `:end', `:number-lines', `:retain-labels', `:use-labels',
2370 `:label-fmt', `:preserve-indent', `:value', `:post-blank' and
2371 `:post-affiliated' keywords.
2373 Assume point is at the beginning of the block."
2374 (let ((case-fold-search t
))
2375 (if (not (save-excursion (re-search-forward "^[ \t]*#\\+END_SRC[ \t]*$"
2377 ;; Incomplete block: parse it as a paragraph.
2378 (org-element-paragraph-parser limit affiliated
)
2379 (let ((contents-end (match-beginning 0)))
2381 (let* ((begin (car affiliated
))
2382 (post-affiliated (point))
2383 ;; Get language as a string.
2387 (concat "^[ \t]*#\\+BEGIN_SRC"
2388 "\\(?: +\\(\\S-+\\)\\)?"
2389 "\\(\\(?: +\\(?:-l \".*?\"\\|[-+][A-Za-z]\\)\\)+\\)?"
2391 (org-match-string-no-properties 1)))
2393 (switches (org-match-string-no-properties 2))
2395 (parameters (org-match-string-no-properties 3))
2396 ;; Switches analysis
2398 (cond ((not switches
) nil
)
2399 ((string-match "-n\\>" switches
) 'new
)
2400 ((string-match "+n\\>" switches
) 'continued
)))
2401 (preserve-indent (and switches
2402 (string-match "-i\\>" switches
)))
2405 (string-match "-l +\"\\([^\"\n]+\\)\"" switches
)
2406 (match-string 1 switches
)))
2407 ;; Should labels be retained in (or stripped from)
2411 (not (string-match "-r\\>" switches
))
2412 (and number-lines
(string-match "-k\\>" switches
))))
2413 ;; What should code-references use - labels or
2418 (not (string-match "-k\\>" switches
)))))
2420 (block-ind (progn (skip-chars-forward " \t") (current-column)))
2422 (value (org-element-remove-indentation
2423 (org-unescape-code-in-string
2424 (buffer-substring-no-properties
2425 (progn (forward-line) (point)) contents-end
))
2427 (pos-before-blank (progn (goto-char contents-end
)
2430 ;; Get position after ending blank lines.
2431 (end (progn (skip-chars-forward " \r\t\n" limit
)
2432 (if (eobp) (point) (line-beginning-position)))))
2435 (list :language language
2436 :switches
(and (org-string-nw-p switches
)
2437 (org-trim switches
))
2438 :parameters
(and (org-string-nw-p parameters
)
2439 (org-trim parameters
))
2442 :number-lines number-lines
2443 :preserve-indent preserve-indent
2444 :retain-labels retain-labels
2445 :use-labels use-labels
2446 :label-fmt label-fmt
2448 :post-blank
(count-lines pos-before-blank end
)
2449 :post-affiliated post-affiliated
)
2450 (cdr affiliated
)))))))))
2452 (defun org-element-src-block-interpreter (src-block contents
)
2453 "Interpret SRC-BLOCK element as Org syntax.
2455 (let ((lang (org-element-property :language src-block
))
2456 (switches (org-element-property :switches src-block
))
2457 (params (org-element-property :parameters src-block
))
2459 (let ((val (org-element-property :value src-block
)))
2461 ((or org-src-preserve-indentation
2462 (org-element-property :preserve-indent src-block
))
2464 ((zerop org-edit-src-content-indentation
) val
)
2466 (let ((ind (make-string org-edit-src-content-indentation ?\s
)))
2467 (replace-regexp-in-string
2468 "\\(^\\)[ \t]*\\S-" ind val nil nil
1)))))))
2469 (concat (format "#+BEGIN_SRC%s\n"
2470 (concat (and lang
(concat " " lang
))
2471 (and switches
(concat " " switches
))
2472 (and params
(concat " " params
))))
2473 (org-element-normalize-string (org-escape-code-in-string value
))
2479 (defun org-element-table-parser (limit affiliated
)
2480 "Parse a table at point.
2482 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2483 the buffer position at the beginning of the first affiliated
2484 keyword and CDR is a plist of affiliated keywords along with
2487 Return a list whose CAR is `table' and CDR is a plist containing
2488 `:begin', `:end', `:tblfm', `:type', `:contents-begin',
2489 `:contents-end', `:value', `:post-blank' and `:post-affiliated'
2492 Assume point is at the beginning of the table."
2494 (let* ((case-fold-search t
)
2495 (table-begin (point))
2496 (type (if (looking-at "[ \t]*|") 'org
'table.el
))
2497 (end-re (format "^[ \t]*\\($\\|[^| \t%s]\\)"
2498 (if (eq type
'org
) "" "+")))
2499 (begin (car affiliated
))
2501 (if (re-search-forward end-re limit
'move
)
2502 (goto-char (match-beginning 0))
2505 (while (looking-at "[ \t]*#\\+TBLFM: +\\(.*\\)[ \t]*$")
2506 (push (org-match-string-no-properties 1) acc
)
2509 (pos-before-blank (point))
2510 (end (progn (skip-chars-forward " \r\t\n" limit
)
2511 (if (eobp) (point) (line-beginning-position)))))
2518 ;; Only `org' tables have contents. `table.el' tables
2519 ;; use a `:value' property to store raw table as
2521 :contents-begin
(and (eq type
'org
) table-begin
)
2522 :contents-end
(and (eq type
'org
) table-end
)
2523 :value
(and (eq type
'table.el
)
2524 (buffer-substring-no-properties
2525 table-begin table-end
))
2526 :post-blank
(count-lines pos-before-blank end
)
2527 :post-affiliated table-begin
)
2528 (cdr affiliated
))))))
2530 (defun org-element-table-interpreter (table contents
)
2531 "Interpret TABLE element as Org syntax.
2532 CONTENTS is a string, if table's type is `org', or nil."
2533 (if (eq (org-element-property :type table
) 'table.el
)
2534 (org-remove-indentation (org-element-property :value table
))
2535 (concat (with-temp-buffer (insert contents
)
2538 (mapconcat (lambda (fm) (concat "#+TBLFM: " fm
))
2539 (reverse (org-element-property :tblfm table
))
2545 (defun org-element-table-row-parser (limit)
2546 "Parse table row at point.
2548 LIMIT bounds the search.
2550 Return a list whose CAR is `table-row' and CDR is a plist
2551 containing `:begin', `:end', `:contents-begin', `:contents-end',
2552 `:type', `:post-blank' and `:post-affiliated' keywords."
2554 (let* ((type (if (looking-at "^[ \t]*|-") 'rule
'standard
))
2556 ;; A table rule has no contents. In that case, ensure
2557 ;; CONTENTS-BEGIN matches CONTENTS-END.
2558 (contents-begin (and (eq type
'standard
)
2559 (search-forward "|")
2561 (contents-end (and (eq type
'standard
)
2564 (skip-chars-backward " \t")
2566 (end (line-beginning-position 2)))
2571 :contents-begin contents-begin
2572 :contents-end contents-end
2574 :post-affiliated begin
)))))
2576 (defun org-element-table-row-interpreter (table-row contents
)
2577 "Interpret TABLE-ROW element as Org syntax.
2578 CONTENTS is the contents of the table row."
2579 (if (eq (org-element-property :type table-row
) 'rule
) "|-"
2580 (concat "| " contents
)))
2585 (defun org-element-verse-block-parser (limit affiliated
)
2586 "Parse a verse block.
2588 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2589 the buffer position at the beginning of the first affiliated
2590 keyword and CDR is a plist of affiliated keywords along with
2593 Return a list whose CAR is `verse-block' and CDR is a plist
2594 containing `:begin', `:end', `:contents-begin', `:contents-end',
2595 `:post-blank' and `:post-affiliated' keywords.
2597 Assume point is at beginning of the block."
2598 (let ((case-fold-search t
))
2599 (if (not (save-excursion
2600 (re-search-forward "^[ \t]*#\\+END_VERSE[ \t]*$" limit t
)))
2601 ;; Incomplete block: parse it as a paragraph.
2602 (org-element-paragraph-parser limit affiliated
)
2603 (let ((contents-end (match-beginning 0)))
2605 (let* ((begin (car affiliated
))
2606 (post-affiliated (point))
2607 (contents-begin (progn (forward-line) (point)))
2608 (pos-before-blank (progn (goto-char contents-end
)
2611 (end (progn (skip-chars-forward " \r\t\n" limit
)
2612 (if (eobp) (point) (line-beginning-position)))))
2617 :contents-begin contents-begin
2618 :contents-end contents-end
2619 :post-blank
(count-lines pos-before-blank end
)
2620 :post-affiliated post-affiliated
)
2621 (cdr affiliated
)))))))))
2623 (defun org-element-verse-block-interpreter (verse-block contents
)
2624 "Interpret VERSE-BLOCK element as Org syntax.
2625 CONTENTS is verse block contents."
2626 (format "#+BEGIN_VERSE\n%s#+END_VERSE" contents
))
2632 ;; Unlike to elements, raw text can be found between objects. Hence,
2633 ;; `org-element--object-lex' is provided to find the next object in
2636 ;; Some object types (e.g., `italic') are recursive. Restrictions on
2637 ;; object types they can contain will be specified in
2638 ;; `org-element-object-restrictions'.
2640 ;; Creating a new type of object requires to alter
2641 ;; `org-element--object-regexp' and `org-element--object-lex', add the
2642 ;; new type in `org-element-all-objects', and possibly add
2643 ;; restrictions in `org-element-object-restrictions'.
2647 (defun org-element-bold-parser ()
2648 "Parse bold object at point, if any.
2650 When at a bold object, return a list whose car is `bold' and cdr
2651 is a plist with `:begin', `:end', `:contents-begin' and
2652 `:contents-end' and `:post-blank' keywords. Otherwise, return
2655 Assume point is at the first star marker."
2657 (unless (bolp) (backward-char 1))
2658 (when (looking-at org-emph-re
)
2659 (let ((begin (match-beginning 2))
2660 (contents-begin (match-beginning 4))
2661 (contents-end (match-end 4))
2662 (post-blank (progn (goto-char (match-end 2))
2663 (skip-chars-forward " \t")))
2668 :contents-begin contents-begin
2669 :contents-end contents-end
2670 :post-blank post-blank
))))))
2672 (defun org-element-bold-interpreter (bold contents
)
2673 "Interpret BOLD object as Org syntax.
2674 CONTENTS is the contents of the object."
2675 (format "*%s*" contents
))
2680 (defun org-element-code-parser ()
2681 "Parse code object at point, if any.
2683 When at a code object, return a list whose car is `code' and cdr
2684 is a plist with `:value', `:begin', `:end' and `:post-blank'
2685 keywords. Otherwise, return nil.
2687 Assume point is at the first tilde marker."
2689 (unless (bolp) (backward-char 1))
2690 (when (looking-at org-emph-re
)
2691 (let ((begin (match-beginning 2))
2692 (value (org-match-string-no-properties 4))
2693 (post-blank (progn (goto-char (match-end 2))
2694 (skip-chars-forward " \t")))
2700 :post-blank post-blank
))))))
2702 (defun org-element-code-interpreter (code contents
)
2703 "Interpret CODE object as Org syntax.
2705 (format "~%s~" (org-element-property :value code
)))
2710 (defun org-element-entity-parser ()
2711 "Parse entity at point, if any.
2713 When at an entity, return a list whose car is `entity' and cdr
2714 a plist with `:begin', `:end', `:latex', `:latex-math-p',
2715 `:html', `:latin1', `:utf-8', `:ascii', `:use-brackets-p' and
2716 `:post-blank' as keywords. Otherwise, return nil.
2718 Assume point is at the beginning of the entity."
2720 (when (looking-at "\\\\\\(?:\\(?1:_ +\\)\\|\\(?1:there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\(?2:$\\|{}\\|[^[:alpha:]]\\)\\)")
2722 (let* ((value (or (org-entity-get (match-string 1))
2723 (throw 'no-object nil
)))
2724 (begin (match-beginning 0))
2725 (bracketsp (string= (match-string 2) "{}"))
2726 (post-blank (progn (goto-char (match-end 1))
2727 (when bracketsp
(forward-char 2))
2728 (skip-chars-forward " \t")))
2731 (list :name
(car value
)
2732 :latex
(nth 1 value
)
2733 :latex-math-p
(nth 2 value
)
2735 :ascii
(nth 4 value
)
2736 :latin1
(nth 5 value
)
2737 :utf-8
(nth 6 value
)
2740 :use-brackets-p bracketsp
2741 :post-blank post-blank
)))))))
2743 (defun org-element-entity-interpreter (entity contents
)
2744 "Interpret ENTITY object as Org syntax.
2747 (org-element-property :name entity
)
2748 (when (org-element-property :use-brackets-p entity
) "{}")))
2753 (defun org-element-export-snippet-parser ()
2754 "Parse export snippet at point.
2756 When at an export snippet, return a list whose car is
2757 `export-snippet' and cdr a plist with `:begin', `:end',
2758 `:back-end', `:value' and `:post-blank' as keywords. Otherwise,
2761 Assume point is at the beginning of the snippet."
2764 (when (and (looking-at "@@\\([-A-Za-z0-9]+\\):")
2766 (save-match-data (goto-char (match-end 0))
2767 (re-search-forward "@@" nil t
)
2768 (match-beginning 0))))
2769 (let* ((begin (match-beginning 0))
2770 (back-end (org-match-string-no-properties 1))
2771 (value (buffer-substring-no-properties
2772 (match-end 0) contents-end
))
2773 (post-blank (skip-chars-forward " \t"))
2775 (list 'export-snippet
2776 (list :back-end back-end
2780 :post-blank post-blank
)))))))
2782 (defun org-element-export-snippet-interpreter (export-snippet contents
)
2783 "Interpret EXPORT-SNIPPET object as Org syntax.
2786 (org-element-property :back-end export-snippet
)
2787 (org-element-property :value export-snippet
)))
2790 ;;;; Footnote Reference
2792 (defun org-element-footnote-reference-parser ()
2793 "Parse footnote reference at point, if any.
2795 When at a footnote reference, return a list whose car is
2796 `footnote-reference' and cdr a plist with `:label', `:type',
2797 `:begin', `:end', `:content-begin', `:contents-end' and
2798 `:post-blank' as keywords. Otherwise, return nil."
2799 (when (looking-at org-footnote-re
)
2800 (let ((closing (with-syntax-table org-element--pair-square-table
2801 (ignore-errors (scan-lists (point) 1 0)))))
2804 (let* ((begin (point))
2806 (or (org-match-string-no-properties 2)
2807 (org-match-string-no-properties 3)
2808 (and (match-string 1)
2809 (concat "fn:" (org-match-string-no-properties 1)))))
2810 (type (if (or (not label
) (match-string 1)) 'inline
'standard
))
2811 (inner-begin (match-end 0))
2812 (inner-end (1- closing
))
2813 (post-blank (progn (goto-char closing
)
2814 (skip-chars-forward " \t")))
2816 (list 'footnote-reference
2821 :contents-begin
(and (eq type
'inline
) inner-begin
)
2822 :contents-end
(and (eq type
'inline
) inner-end
)
2823 :post-blank post-blank
))))))))
2825 (defun org-element-footnote-reference-interpreter (footnote-reference contents
)
2826 "Interpret FOOTNOTE-REFERENCE object as Org syntax.
2827 CONTENTS is its definition, when inline, or nil."
2829 (concat (or (org-element-property :label footnote-reference
) "fn:")
2830 (and contents
(concat ":" contents
)))))
2833 ;;;; Inline Babel Call
2835 (defun org-element-inline-babel-call-parser ()
2836 "Parse inline babel call at point, if any.
2838 When at an inline babel call, return a list whose car is
2839 `inline-babel-call' and cdr a plist with `:call',
2840 `:inside-header', `:arguments', `:end-header', `:begin', `:end',
2841 `:value' and `:post-blank' as keywords. Otherwise, return nil.
2843 Assume point is at the beginning of the babel call."
2845 (unless (bolp) (backward-char))
2846 (when (let ((case-fold-search t
))
2847 (looking-at org-babel-inline-lob-one-liner-regexp
))
2848 (let ((begin (match-end 1))
2849 (call (org-match-string-no-properties 2))
2850 (inside-header (org-string-nw-p (org-match-string-no-properties 4)))
2851 (arguments (org-string-nw-p (org-match-string-no-properties 6)))
2852 (end-header (org-string-nw-p (org-match-string-no-properties 8)))
2853 (value (buffer-substring-no-properties (match-end 1) (match-end 0)))
2854 (post-blank (progn (goto-char (match-end 0))
2855 (skip-chars-forward " \t")))
2857 (list 'inline-babel-call
2859 :inside-header inside-header
2860 :arguments arguments
2861 :end-header end-header
2865 :post-blank post-blank
))))))
2867 (defun org-element-inline-babel-call-interpreter (inline-babel-call contents
)
2868 "Interpret INLINE-BABEL-CALL object as Org syntax.
2871 (org-element-property :call inline-babel-call
)
2872 (let ((h (org-element-property :inside-header inline-babel-call
)))
2873 (and h
(format "[%s]" h
)))
2874 "(" (org-element-property :arguments inline-babel-call
) ")"
2875 (let ((h (org-element-property :end-header inline-babel-call
)))
2876 (and h
(format "[%s]" h
)))))
2879 ;;;; Inline Src Block
2881 (defun org-element-inline-src-block-parser ()
2882 "Parse inline source block at point, if any.
2884 When at an inline source block, return a list whose car is
2885 `inline-src-block' and cdr a plist with `:begin', `:end',
2886 `:language', `:value', `:parameters' and `:post-blank' as
2887 keywords. Otherwise, return nil.
2889 Assume point is at the beginning of the inline src block."
2891 (unless (bolp) (backward-char))
2892 (when (looking-at org-babel-inline-src-block-regexp
)
2893 (let ((begin (match-beginning 1))
2894 (language (org-match-string-no-properties 2))
2895 (parameters (org-match-string-no-properties 4))
2896 (value (org-match-string-no-properties 5))
2897 (post-blank (progn (goto-char (match-end 0))
2898 (skip-chars-forward " \t")))
2900 (list 'inline-src-block
2901 (list :language language
2903 :parameters parameters
2906 :post-blank post-blank
))))))
2908 (defun org-element-inline-src-block-interpreter (inline-src-block contents
)
2909 "Interpret INLINE-SRC-BLOCK object as Org syntax.
2911 (let ((language (org-element-property :language inline-src-block
))
2912 (arguments (org-element-property :parameters inline-src-block
))
2913 (body (org-element-property :value inline-src-block
)))
2914 (format "src_%s%s{%s}"
2916 (if arguments
(format "[%s]" arguments
) "")
2921 (defun org-element-italic-parser ()
2922 "Parse italic object at point, if any.
2924 When at an italic object, return a list whose car is `italic' and
2925 cdr is a plist with `:begin', `:end', `:contents-begin' and
2926 `:contents-end' and `:post-blank' keywords. Otherwise, return
2929 Assume point is at the first slash marker."
2931 (unless (bolp) (backward-char 1))
2932 (when (looking-at org-emph-re
)
2933 (let ((begin (match-beginning 2))
2934 (contents-begin (match-beginning 4))
2935 (contents-end (match-end 4))
2936 (post-blank (progn (goto-char (match-end 2))
2937 (skip-chars-forward " \t")))
2942 :contents-begin contents-begin
2943 :contents-end contents-end
2944 :post-blank post-blank
))))))
2946 (defun org-element-italic-interpreter (italic contents
)
2947 "Interpret ITALIC object as Org syntax.
2948 CONTENTS is the contents of the object."
2949 (format "/%s/" contents
))
2954 (defun org-element-latex-fragment-parser ()
2955 "Parse LaTeX fragment at point, if any.
2957 When at a LaTeX fragment, return a list whose car is
2958 `latex-fragment' and cdr a plist with `:value', `:begin', `:end',
2959 and `:post-blank' as keywords. Otherwise, return nil.
2961 Assume point is at the beginning of the LaTeX fragment."
2964 (let* ((begin (point))
2966 (if (eq (char-after) ?$
)
2967 (if (eq (char-after (1+ (point))) ?$
)
2968 (search-forward "$$" nil t
2)
2969 (and (not (eq (char-before) ?$
))
2970 (search-forward "$" nil t
2)
2971 (not (memq (char-before (match-beginning 0))
2972 '(?\s ?
\t ?
\n ?
, ?.
)))
2973 (looking-at "\\(\\s.\\|\\s-\\|\\s(\\|\\s)\\|\\s\"\\|$\\)")
2975 (case (char-after (1+ (point)))
2976 (?\
( (search-forward "\\)" nil t
))
2977 (?\
[ (search-forward "\\]" nil t
))
2980 (and (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")
2982 (post-blank (if (not after-fragment
) (throw 'no-object nil
)
2983 (goto-char after-fragment
)
2984 (skip-chars-forward " \t")))
2986 (list 'latex-fragment
2987 (list :value
(buffer-substring-no-properties begin after-fragment
)
2990 :post-blank post-blank
))))))
2992 (defun org-element-latex-fragment-interpreter (latex-fragment contents
)
2993 "Interpret LATEX-FRAGMENT object as Org syntax.
2995 (org-element-property :value latex-fragment
))
2999 (defun org-element-line-break-parser ()
3000 "Parse line break at point, if any.
3002 When at a line break, return a list whose car is `line-break',
3003 and cdr a plist with `:begin', `:end' and `:post-blank' keywords.
3004 Otherwise, return nil.
3006 Assume point is at the beginning of the line break."
3007 (when (and (org-looking-at-p "\\\\\\\\[ \t]*$")
3008 (not (eq (char-before) ?
\\)))
3010 (list :begin
(point)
3011 :end
(line-beginning-position 2)
3014 (defun org-element-line-break-interpreter (line-break contents
)
3015 "Interpret LINE-BREAK object as Org syntax.
3022 (defun org-element-link-parser ()
3023 "Parse link at point, if any.
3025 When at a link, return a list whose car is `link' and cdr a plist
3026 with `:type', `:path', `:raw-link', `:application',
3027 `:search-option', `:begin', `:end', `:contents-begin',
3028 `:contents-end' and `:post-blank' as keywords. Otherwise, return
3031 Assume point is at the beginning of the link."
3033 (let ((begin (point))
3034 end contents-begin contents-end link-end post-blank path type
3035 raw-link link search-option application
)
3037 ;; Type 1: Text targeted from a radio target.
3038 ((and org-target-link-regexp
3039 (save-excursion (or (bolp) (backward-char))
3040 (looking-at org-target-link-regexp
)))
3042 link-end
(match-end 1)
3043 path
(org-match-string-no-properties 1)
3044 contents-begin
(match-beginning 1)
3045 contents-end
(match-end 1)))
3046 ;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]]
3047 ((looking-at org-bracket-link-regexp
)
3048 (setq contents-begin
(match-beginning 3))
3049 (setq contents-end
(match-end 3))
3050 (setq link-end
(match-end 0))
3051 ;; RAW-LINK is the original link. Expand any
3052 ;; abbreviation in it.
3054 ;; Also treat any newline character and associated
3055 ;; indentation as a single space character. This is not
3056 ;; compatible with RFC 3986, which requires to ignore
3057 ;; them altogether. However, doing so would require
3058 ;; users to encode spaces on the fly when writing links
3059 ;; (e.g., insert [[shell:ls%20*.org]] instead of
3060 ;; [[shell:ls *.org]], which defeats Org's focus on
3062 (setq raw-link
(org-link-expand-abbrev
3063 (replace-regexp-in-string
3064 "[ \t]*\n[ \t]*" " "
3065 (org-match-string-no-properties 1))))
3066 ;; Determine TYPE of link and set PATH accordingly. According
3067 ;; to RFC 3986, remove whitespaces from URI in external links.
3068 ;; In internal ones, treat indentation as a single space.
3071 ((or (file-name-absolute-p raw-link
)
3072 (string-match "\\`\\.\\.?/" raw-link
))
3074 (setq path raw-link
))
3075 ;; Explicit type (http, irc, bbdb...). See `org-link-types'.
3076 ((string-match org-link-types-re raw-link
)
3077 (setq type
(match-string 1 raw-link
))
3078 (setq path
(substring raw-link
(match-end 0))))
3079 ;; Id type: PATH is the id.
3080 ((string-match "\\`id:\\([-a-f0-9]+\\)\\'" raw-link
)
3081 (setq type
"id" path
(match-string 1 raw-link
)))
3082 ;; Code-ref type: PATH is the name of the reference.
3083 ((and (org-string-match-p "\\`(" raw-link
)
3084 (org-string-match-p ")\\'" raw-link
))
3085 (setq type
"coderef")
3086 (setq path
(substring raw-link
1 -
1)))
3087 ;; Custom-id type: PATH is the name of the custom id.
3088 ((= (string-to-char raw-link
) ?
#)
3089 (setq type
"custom-id")
3090 (setq path
(substring raw-link
1)))
3091 ;; Fuzzy type: Internal link either matches a target, an
3092 ;; headline name or nothing. PATH is the target or
3096 (setq path raw-link
))))
3097 ;; Type 3: Plain link, e.g., http://orgmode.org
3098 ((looking-at org-plain-link-re
)
3099 (setq raw-link
(org-match-string-no-properties 0)
3100 type
(org-match-string-no-properties 1)
3101 link-end
(match-end 0)
3102 path
(org-match-string-no-properties 2)))
3103 ;; Type 4: Angular link, e.g., <http://orgmode.org>. Unlike to
3104 ;; bracket links, follow RFC 3986 and remove any extra
3105 ;; whitespace in URI.
3106 ((looking-at org-angle-link-re
)
3107 (setq type
(org-match-string-no-properties 1))
3108 (setq link-end
(match-end 0))
3110 (buffer-substring-no-properties
3111 (match-beginning 1) (match-end 2)))
3112 (setq path
(replace-regexp-in-string
3113 "[ \t]*\n[ \t]*" "" (org-match-string-no-properties 2))))
3114 (t (throw 'no-object nil
)))
3115 ;; In any case, deduce end point after trailing white space from
3116 ;; LINK-END variable.
3119 (progn (goto-char link-end
) (skip-chars-forward " \t")))
3121 ;; Special "file" type link processing. Extract opening
3122 ;; application and search option, if any. Also normalize URI.
3123 (when (string-match "\\`file\\(?:\\+\\(.+\\)\\)?\\'" type
)
3124 (setq application
(match-string 1 type
) type
"file")
3125 (when (string-match "::\\(.*\\)\\'" path
)
3126 (setq search-option
(match-string 1 path
))
3127 (setq path
(replace-match "" nil nil path
)))
3128 (setq path
(replace-regexp-in-string "\\`/+" "/" path
)))
3129 ;; Translate link, if `org-link-translation-function' is set.
3130 (let ((trans (and (functionp org-link-translation-function
)
3131 (funcall org-link-translation-function type path
))))
3133 (setq type
(car trans
))
3134 (setq path
(cdr trans
))))
3138 :raw-link
(or raw-link path
)
3139 :application application
3140 :search-option search-option
3143 :contents-begin contents-begin
3144 :contents-end contents-end
3145 :post-blank post-blank
)))))
3147 (defun org-element-link-interpreter (link contents
)
3148 "Interpret LINK object as Org syntax.
3149 CONTENTS is the contents of the object, or nil."
3150 (let ((type (org-element-property :type link
))
3151 (path (org-element-property :path link
)))
3152 (if (string= type
"radio") path
3154 (cond ((string= type
"coderef") (format "(%s)" path
))
3155 ((string= type
"custom-id") (concat "#" path
))
3156 ((string= type
"file")
3157 (let ((app (org-element-property :application link
))
3158 (opt (org-element-property :search-option link
)))
3159 (concat type
(and app
(concat "+" app
)) ":"
3161 (and opt
(concat "::" opt
)))))
3162 ((string= type
"fuzzy") path
)
3163 (t (concat type
":" path
)))
3164 (if contents
(format "[%s]" contents
) "")))))
3169 (defun org-element-macro-parser ()
3170 "Parse macro at point, if any.
3172 When at a macro, return a list whose car is `macro' and cdr
3173 a plist with `:key', `:args', `:begin', `:end', `:value' and
3174 `:post-blank' as keywords. Otherwise, return nil.
3176 Assume point is at the macro."
3178 (when (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
3179 (let ((begin (point))
3180 (key (downcase (org-match-string-no-properties 1)))
3181 (value (org-match-string-no-properties 0))
3182 (post-blank (progn (goto-char (match-end 0))
3183 (skip-chars-forward " \t")))
3185 (args (let ((args (org-match-string-no-properties 3)))
3186 (and args
(org-macro-extract-arguments args
)))))
3193 :post-blank post-blank
))))))
3195 (defun org-element-macro-interpreter (macro contents
)
3196 "Interpret MACRO object as Org syntax.
3198 (org-element-property :value macro
))
3203 (defun org-element-radio-target-parser ()
3204 "Parse radio target at point, if any.
3206 When at a radio target, return a list whose car is `radio-target'
3207 and cdr a plist with `:begin', `:end', `:contents-begin',
3208 `:contents-end', `:value' and `:post-blank' as keywords.
3209 Otherwise, return nil.
3211 Assume point is at the radio target."
3213 (when (looking-at org-radio-target-regexp
)
3214 (let ((begin (point))
3215 (contents-begin (match-beginning 1))
3216 (contents-end (match-end 1))
3217 (value (org-match-string-no-properties 1))
3218 (post-blank (progn (goto-char (match-end 0))
3219 (skip-chars-forward " \t")))
3224 :contents-begin contents-begin
3225 :contents-end contents-end
3226 :post-blank post-blank
3229 (defun org-element-radio-target-interpreter (target contents
)
3230 "Interpret TARGET object as Org syntax.
3231 CONTENTS is the contents of the object."
3232 (concat "<<<" contents
">>>"))
3235 ;;;; Statistics Cookie
3237 (defun org-element-statistics-cookie-parser ()
3238 "Parse statistics cookie at point, if any.
3240 When at a statistics cookie, return a list whose car is
3241 `statistics-cookie', and cdr a plist with `:begin', `:end',
3242 `:value' and `:post-blank' keywords. Otherwise, return nil.
3244 Assume point is at the beginning of the statistics-cookie."
3246 (when (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
3247 (let* ((begin (point))
3248 (value (buffer-substring-no-properties
3249 (match-beginning 0) (match-end 0)))
3250 (post-blank (progn (goto-char (match-end 0))
3251 (skip-chars-forward " \t")))
3253 (list 'statistics-cookie
3257 :post-blank post-blank
))))))
3259 (defun org-element-statistics-cookie-interpreter (statistics-cookie contents
)
3260 "Interpret STATISTICS-COOKIE object as Org syntax.
3262 (org-element-property :value statistics-cookie
))
3267 (defun org-element-strike-through-parser ()
3268 "Parse strike-through object at point, if any.
3270 When at a strike-through object, return a list whose car is
3271 `strike-through' and cdr is a plist with `:begin', `:end',
3272 `:contents-begin' and `:contents-end' and `:post-blank' keywords.
3273 Otherwise, return nil.
3275 Assume point is at the first plus sign marker."
3277 (unless (bolp) (backward-char 1))
3278 (when (looking-at org-emph-re
)
3279 (let ((begin (match-beginning 2))
3280 (contents-begin (match-beginning 4))
3281 (contents-end (match-end 4))
3282 (post-blank (progn (goto-char (match-end 2))
3283 (skip-chars-forward " \t")))
3285 (list 'strike-through
3288 :contents-begin contents-begin
3289 :contents-end contents-end
3290 :post-blank post-blank
))))))
3292 (defun org-element-strike-through-interpreter (strike-through contents
)
3293 "Interpret STRIKE-THROUGH object as Org syntax.
3294 CONTENTS is the contents of the object."
3295 (format "+%s+" contents
))
3300 (defun org-element-subscript-parser ()
3301 "Parse subscript at point, if any.
3303 When at a subscript object, return a list whose car is
3304 `subscript' and cdr a plist with `:begin', `:end',
3305 `:contents-begin', `:contents-end', `:use-brackets-p' and
3306 `:post-blank' as keywords. Otherwise, return nil.
3308 Assume point is at the underscore."
3310 (unless (bolp) (backward-char))
3311 (when (looking-at org-match-substring-regexp
)
3312 (let ((bracketsp (match-beginning 4))
3313 (begin (match-beginning 2))
3314 (contents-begin (or (match-beginning 4)
3315 (match-beginning 3)))
3316 (contents-end (or (match-end 4) (match-end 3)))
3317 (post-blank (progn (goto-char (match-end 0))
3318 (skip-chars-forward " \t")))
3323 :use-brackets-p bracketsp
3324 :contents-begin contents-begin
3325 :contents-end contents-end
3326 :post-blank post-blank
))))))
3328 (defun org-element-subscript-interpreter (subscript contents
)
3329 "Interpret SUBSCRIPT object as Org syntax.
3330 CONTENTS is the contents of the object."
3332 (if (org-element-property :use-brackets-p subscript
) "_{%s}" "_%s")
3338 (defun org-element-superscript-parser ()
3339 "Parse superscript at point, if any.
3341 When at a superscript object, return a list whose car is
3342 `superscript' and cdr a plist with `:begin', `:end',
3343 `:contents-begin', `:contents-end', `:use-brackets-p' and
3344 `:post-blank' as keywords. Otherwise, return nil.
3346 Assume point is at the caret."
3348 (unless (bolp) (backward-char))
3349 (when (looking-at org-match-substring-regexp
)
3350 (let ((bracketsp (match-beginning 4))
3351 (begin (match-beginning 2))
3352 (contents-begin (or (match-beginning 4)
3353 (match-beginning 3)))
3354 (contents-end (or (match-end 4) (match-end 3)))
3355 (post-blank (progn (goto-char (match-end 0))
3356 (skip-chars-forward " \t")))
3361 :use-brackets-p bracketsp
3362 :contents-begin contents-begin
3363 :contents-end contents-end
3364 :post-blank post-blank
))))))
3366 (defun org-element-superscript-interpreter (superscript contents
)
3367 "Interpret SUPERSCRIPT object as Org syntax.
3368 CONTENTS is the contents of the object."
3370 (if (org-element-property :use-brackets-p superscript
) "^{%s}" "^%s")
3376 (defun org-element-table-cell-parser ()
3377 "Parse table cell at point.
3378 Return a list whose car is `table-cell' and cdr is a plist
3379 containing `:begin', `:end', `:contents-begin', `:contents-end'
3380 and `:post-blank' keywords."
3381 (looking-at "[ \t]*\\(.*?\\)[ \t]*\\(?:|\\|$\\)")
3382 (let* ((begin (match-beginning 0))
3384 (contents-begin (match-beginning 1))
3385 (contents-end (match-end 1)))
3389 :contents-begin contents-begin
3390 :contents-end contents-end
3393 (defun org-element-table-cell-interpreter (table-cell contents
)
3394 "Interpret TABLE-CELL element as Org syntax.
3395 CONTENTS is the contents of the cell, or nil."
3396 (concat " " contents
" |"))
3401 (defun org-element-target-parser ()
3402 "Parse target at point, if any.
3404 When at a target, return a list whose car is `target' and cdr
3405 a plist with `:begin', `:end', `:value' and `:post-blank' as
3406 keywords. Otherwise, return nil.
3408 Assume point is at the target."
3410 (when (looking-at org-target-regexp
)
3411 (let ((begin (point))
3412 (value (org-match-string-no-properties 1))
3413 (post-blank (progn (goto-char (match-end 0))
3414 (skip-chars-forward " \t")))
3420 :post-blank post-blank
))))))
3422 (defun org-element-target-interpreter (target contents
)
3423 "Interpret TARGET object as Org syntax.
3425 (format "<<%s>>" (org-element-property :value target
)))
3430 (defconst org-element--timestamp-regexp
3431 (concat org-ts-regexp-both
3433 "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
3435 "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
3436 "Regexp matching any timestamp type object.")
3438 (defun org-element-timestamp-parser ()
3439 "Parse time stamp at point, if any.
3441 When at a time stamp, return a list whose car is `timestamp', and
3442 cdr a plist with `:type', `:raw-value', `:year-start',
3443 `:month-start', `:day-start', `:hour-start', `:minute-start',
3444 `:year-end', `:month-end', `:day-end', `:hour-end',
3445 `:minute-end', `:repeater-type', `:repeater-value',
3446 `:repeater-unit', `:warning-type', `:warning-value',
3447 `:warning-unit', `:begin', `:end' and `:post-blank' keywords.
3448 Otherwise, return nil.
3450 Assume point is at the beginning of the timestamp."
3451 (when (org-looking-at-p org-element--timestamp-regexp
)
3453 (let* ((begin (point))
3454 (activep (eq (char-after) ?
<))
3457 (looking-at "\\([<[]\\(%%\\)?.*?\\)[]>]\\(?:--\\([<[].*?[]>]\\)\\)?")
3458 (match-string-no-properties 0)))
3459 (date-start (match-string-no-properties 1))
3460 (date-end (match-string 3))
3461 (diaryp (match-beginning 2))
3462 (post-blank (progn (goto-char (match-end 0))
3463 (skip-chars-forward " \t")))
3468 "[012]?[0-9]:[0-5][0-9]\\(-\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)"
3470 (cons (string-to-number (match-string 2 date-start
))
3471 (string-to-number (match-string 3 date-start
)))))
3472 (type (cond (diaryp 'diary
)
3473 ((and activep
(or date-end time-range
)) 'active-range
)
3475 ((or date-end time-range
) 'inactive-range
)
3479 (string-match "\\([.+]?\\+\\)\\([0-9]+\\)\\([hdwmy]\\)"
3483 (let ((type (match-string 1 raw-value
)))
3484 (cond ((equal "++" type
) 'catch-up
)
3485 ((equal ".+" type
) 'restart
)
3487 :repeater-value
(string-to-number (match-string 2 raw-value
))
3489 (case (string-to-char (match-string 3 raw-value
))
3490 (?h
'hour
) (?d
'day
) (?w
'week
) (?m
'month
) (t 'year
)))))
3493 (string-match "\\(-\\)?-\\([0-9]+\\)\\([hdwmy]\\)" raw-value
)
3495 :warning-type
(if (match-string 1 raw-value
) 'first
'all
)
3496 :warning-value
(string-to-number (match-string 2 raw-value
))
3498 (case (string-to-char (match-string 3 raw-value
))
3499 (?h
'hour
) (?d
'day
) (?w
'week
) (?m
'month
) (t 'year
)))))
3500 year-start month-start day-start hour-start minute-start year-end
3501 month-end day-end hour-end minute-end
)
3502 ;; Parse date-start.
3504 (let ((date (org-parse-time-string date-start t
)))
3505 (setq year-start
(nth 5 date
)
3506 month-start
(nth 4 date
)
3507 day-start
(nth 3 date
)
3508 hour-start
(nth 2 date
)
3509 minute-start
(nth 1 date
))))
3510 ;; Compute date-end. It can be provided directly in time-stamp,
3511 ;; or extracted from time range. Otherwise, it defaults to the
3512 ;; same values as date-start.
3514 (let ((date (and date-end
(org-parse-time-string date-end t
))))
3515 (setq year-end
(or (nth 5 date
) year-start
)
3516 month-end
(or (nth 4 date
) month-start
)
3517 day-end
(or (nth 3 date
) day-start
)
3518 hour-end
(or (nth 2 date
) (car time-range
) hour-start
)
3519 minute-end
(or (nth 1 date
) (cdr time-range
) minute-start
))))
3521 (nconc (list :type type
3522 :raw-value raw-value
3523 :year-start year-start
3524 :month-start month-start
3525 :day-start day-start
3526 :hour-start hour-start
3527 :minute-start minute-start
3529 :month-end month-end
3532 :minute-end minute-end
3535 :post-blank post-blank
)
3539 (defun org-element-timestamp-interpreter (timestamp contents
)
3540 "Interpret TIMESTAMP object as Org syntax.
3542 (let* ((repeat-string
3544 (case (org-element-property :repeater-type timestamp
)
3545 (cumulate "+") (catch-up "++") (restart ".+"))
3546 (let ((val (org-element-property :repeater-value timestamp
)))
3547 (and val
(number-to-string val
)))
3548 (case (org-element-property :repeater-unit timestamp
)
3549 (hour "h") (day "d") (week "w") (month "m") (year "y"))))
3552 (case (org-element-property :warning-type timestamp
)
3555 (let ((val (org-element-property :warning-value timestamp
)))
3556 (and val
(number-to-string val
)))
3557 (case (org-element-property :warning-unit timestamp
)
3558 (hour "h") (day "d") (week "w") (month "m") (year "y"))))
3560 ;; Build an Org timestamp string from TIME. ACTIVEP is
3561 ;; non-nil when time stamp is active. If WITH-TIME-P is
3562 ;; non-nil, add a time part. HOUR-END and MINUTE-END
3563 ;; specify a time range in the timestamp. REPEAT-STRING is
3564 ;; the repeater string, if any.
3565 (lambda (time activep
&optional with-time-p hour-end minute-end
)
3566 (let ((ts (format-time-string
3567 (funcall (if with-time-p
'cdr
'car
)
3568 org-time-stamp-formats
)
3570 (when (and hour-end minute-end
)
3571 (string-match "[012]?[0-9]:[0-5][0-9]" ts
)
3574 (format "\\&-%02d:%02d" hour-end minute-end
)
3576 (unless activep
(setq ts
(format "[%s]" (substring ts
1 -
1))))
3577 (dolist (s (list repeat-string warning-string
))
3578 (when (org-string-nw-p s
)
3579 (setq ts
(concat (substring ts
0 -
1)
3582 (substring ts -
1)))))
3585 (type (org-element-property :type timestamp
)))
3588 (let* ((minute-start (org-element-property :minute-start timestamp
))
3589 (minute-end (org-element-property :minute-end timestamp
))
3590 (hour-start (org-element-property :hour-start timestamp
))
3591 (hour-end (org-element-property :hour-end timestamp
))
3592 (time-range-p (and hour-start hour-end minute-start minute-end
3593 (or (/= hour-start hour-end
)
3594 (/= minute-start minute-end
)))))
3600 (org-element-property :day-start timestamp
)
3601 (org-element-property :month-start timestamp
)
3602 (org-element-property :year-start timestamp
))
3604 (and hour-start minute-start
)
3605 (and time-range-p hour-end
)
3606 (and time-range-p minute-end
))))
3607 ((active-range inactive-range
)
3608 (let ((minute-start (org-element-property :minute-start timestamp
))
3609 (minute-end (org-element-property :minute-end timestamp
))
3610 (hour-start (org-element-property :hour-start timestamp
))
3611 (hour-end (org-element-property :hour-end timestamp
)))
3614 build-ts-string
(encode-time
3618 (org-element-property :day-start timestamp
)
3619 (org-element-property :month-start timestamp
)
3620 (org-element-property :year-start timestamp
))
3621 (eq type
'active-range
)
3622 (and hour-start minute-start
))
3624 (funcall build-ts-string
3628 (org-element-property :day-end timestamp
)
3629 (org-element-property :month-end timestamp
)
3630 (org-element-property :year-end timestamp
))
3631 (eq type
'active-range
)
3632 (and hour-end minute-end
)))))
3633 (otherwise (org-element-property :raw-value timestamp
)))))
3638 (defun org-element-underline-parser ()
3639 "Parse underline object at point, if any.
3641 When at an underline object, return a list whose car is
3642 `underline' and cdr is a plist with `:begin', `:end',
3643 `:contents-begin' and `:contents-end' and `:post-blank' keywords.
3644 Otherwise, return nil.
3646 Assume point is at the first underscore marker."
3648 (unless (bolp) (backward-char 1))
3649 (when (looking-at org-emph-re
)
3650 (let ((begin (match-beginning 2))
3651 (contents-begin (match-beginning 4))
3652 (contents-end (match-end 4))
3653 (post-blank (progn (goto-char (match-end 2))
3654 (skip-chars-forward " \t")))
3659 :contents-begin contents-begin
3660 :contents-end contents-end
3661 :post-blank post-blank
))))))
3663 (defun org-element-underline-interpreter (underline contents
)
3664 "Interpret UNDERLINE object as Org syntax.
3665 CONTENTS is the contents of the object."
3666 (format "_%s_" contents
))
3671 (defun org-element-verbatim-parser ()
3672 "Parse verbatim object at point, if any.
3674 When at a verbatim object, return a list whose car is `verbatim'
3675 and cdr is a plist with `:value', `:begin', `:end' and
3676 `:post-blank' keywords. Otherwise, return nil.
3678 Assume point is at the first equal sign marker."
3680 (unless (bolp) (backward-char 1))
3681 (when (looking-at org-emph-re
)
3682 (let ((begin (match-beginning 2))
3683 (value (org-match-string-no-properties 4))
3684 (post-blank (progn (goto-char (match-end 2))
3685 (skip-chars-forward " \t")))
3691 :post-blank post-blank
))))))
3693 (defun org-element-verbatim-interpreter (verbatim contents
)
3694 "Interpret VERBATIM object as Org syntax.
3696 (format "=%s=" (org-element-property :value verbatim
)))
3700 ;;; Parsing Element Starting At Point
3702 ;; `org-element--current-element' is the core function of this section.
3703 ;; It returns the Lisp representation of the element starting at
3706 ;; `org-element--current-element' makes use of special modes. They
3707 ;; are activated for fixed element chaining (e.g., `plain-list' >
3708 ;; `item') or fixed conditional element chaining (e.g., `headline' >
3709 ;; `section'). Special modes are: `first-section', `item',
3710 ;; `node-property', `section' and `table-row'.
3712 (defun org-element--current-element (limit &optional granularity mode structure
)
3713 "Parse the element starting at point.
3715 Return value is a list like (TYPE PROPS) where TYPE is the type
3716 of the element and PROPS a plist of properties associated to the
3719 Possible types are defined in `org-element-all-elements'.
3721 LIMIT bounds the search.
3723 Optional argument GRANULARITY determines the depth of the
3724 recursion. Allowed values are `headline', `greater-element',
3725 `element', `object' or nil. When it is broader than `object' (or
3726 nil), secondary values will not be parsed, since they only
3729 Optional argument MODE, when non-nil, can be either
3730 `first-section', `section', `planning', `item', `node-property'
3733 If STRUCTURE isn't provided but MODE is set to `item', it will be
3736 This function assumes point is always at the beginning of the
3737 element it has to parse."
3739 (let ((case-fold-search t
)
3740 ;; Determine if parsing depth allows for secondary strings
3741 ;; parsing. It only applies to elements referenced in
3742 ;; `org-element-secondary-value-alist'.
3743 (raw-secondary-p (and granularity
(not (eq granularity
'object
)))))
3747 (org-element-item-parser limit structure raw-secondary-p
))
3749 ((eq mode
'table-row
) (org-element-table-row-parser limit
))
3751 ((eq mode
'node-property
) (org-element-node-property-parser limit
))
3753 ((org-with-limited-levels (org-at-heading-p))
3754 (org-element-headline-parser limit raw-secondary-p
))
3755 ;; Sections (must be checked after headline).
3756 ((eq mode
'section
) (org-element-section-parser limit
))
3757 ((eq mode
'first-section
)
3758 (org-element-section-parser
3759 (or (save-excursion (org-with-limited-levels (outline-next-heading)))
3762 ((and (eq mode
'planning
) (looking-at org-planning-line-re
))
3763 (org-element-planning-parser limit
))
3765 ((and (memq mode
'(planning property-drawer
))
3766 (looking-at org-property-drawer-re
))
3767 (org-element-property-drawer-parser limit
))
3768 ;; When not at bol, point is at the beginning of an item or
3769 ;; a footnote definition: next item is always a paragraph.
3770 ((not (bolp)) (org-element-paragraph-parser limit
(list (point))))
3772 ((looking-at org-clock-line-re
) (org-element-clock-parser limit
))
3775 (org-element-inlinetask-parser limit raw-secondary-p
))
3776 ;; From there, elements can have affiliated keywords.
3777 (t (let ((affiliated (org-element--collect-affiliated-keywords limit
)))
3779 ;; Jumping over affiliated keywords put point off-limits.
3780 ;; Parse them as regular keywords.
3781 ((and (cdr affiliated
) (>= (point) limit
))
3782 (goto-char (car affiliated
))
3783 (org-element-keyword-parser limit nil
))
3784 ;; LaTeX Environment.
3785 ((looking-at org-element--latex-begin-environment
)
3786 (org-element-latex-environment-parser limit affiliated
))
3787 ;; Drawer and Property Drawer.
3788 ((looking-at org-drawer-regexp
)
3789 (org-element-drawer-parser limit affiliated
))
3791 ((looking-at "[ \t]*:\\( \\|$\\)")
3792 (org-element-fixed-width-parser limit affiliated
))
3793 ;; Inline Comments, Blocks, Babel Calls, Dynamic Blocks and
3795 ((looking-at "[ \t]*#")
3796 (goto-char (match-end 0))
3797 (cond ((looking-at "\\(?: \\|$\\)")
3799 (org-element-comment-parser limit affiliated
))
3800 ((looking-at "\\+BEGIN_\\(\\S-+\\)")
3802 (let ((parser (assoc (upcase (match-string 1))
3803 org-element-block-name-alist
)))
3804 (if parser
(funcall (cdr parser
) limit affiliated
)
3805 (org-element-special-block-parser limit affiliated
))))
3806 ((looking-at "\\+CALL:")
3808 (org-element-babel-call-parser limit affiliated
))
3809 ((looking-at "\\+BEGIN:? ")
3811 (org-element-dynamic-block-parser limit affiliated
))
3812 ((looking-at "\\+\\S-+:")
3814 (org-element-keyword-parser limit affiliated
))
3817 (org-element-paragraph-parser limit affiliated
))))
3818 ;; Footnote Definition.
3819 ((looking-at org-footnote-definition-re
)
3820 (org-element-footnote-definition-parser limit affiliated
))
3822 ((looking-at "[ \t]*-\\{5,\\}[ \t]*$")
3823 (org-element-horizontal-rule-parser limit affiliated
))
3826 (org-element-diary-sexp-parser limit affiliated
))
3828 ((looking-at "[ \t]*\\(|\\|\\+\\(-+\\+\\)+[ \t]*$\\)")
3829 (org-element-table-parser limit affiliated
))
3831 ((looking-at (org-item-re))
3832 (org-element-plain-list-parser
3834 (or structure
(org-element--list-struct limit
))))
3835 ;; Default element: Paragraph.
3836 (t (org-element-paragraph-parser limit affiliated
)))))))))
3839 ;; Most elements can have affiliated keywords. When looking for an
3840 ;; element beginning, we want to move before them, as they belong to
3841 ;; that element, and, in the meantime, collect information they give
3842 ;; into appropriate properties. Hence the following function.
3844 (defun org-element--collect-affiliated-keywords (limit)
3845 "Collect affiliated keywords from point down to LIMIT.
3847 Return a list whose CAR is the position at the first of them and
3848 CDR a plist of keywords and values and move point to the
3849 beginning of the first line after them.
3851 As a special case, if element doesn't start at the beginning of
3852 the line (e.g., a paragraph starting an item), CAR is current
3853 position of point and CDR is nil."
3854 (if (not (bolp)) (list (point))
3855 (let ((case-fold-search t
)
3857 ;; RESTRICT is the list of objects allowed in parsed
3859 (restrict (org-element-restriction 'keyword
))
3861 (while (and (< (point) limit
) (looking-at org-element--affiliated-re
))
3862 (let* ((raw-kwd (upcase (match-string 1)))
3863 ;; Apply translation to RAW-KWD. From there, KWD is
3864 ;; the official keyword.
3865 (kwd (or (cdr (assoc raw-kwd
3866 org-element-keyword-translation-alist
))
3868 ;; Find main value for any keyword.
3872 (buffer-substring-no-properties
3873 (match-end 0) (line-end-position)))))
3874 ;; PARSEDP is non-nil when keyword should have its
3876 (parsedp (member kwd org-element-parsed-keywords
))
3877 ;; If KWD is a dual keyword, find its secondary
3878 ;; value. Maybe parse it.
3879 (dualp (member kwd org-element-dual-keywords
))
3882 (let ((sec (org-match-string-no-properties 2)))
3883 (if (or (not sec
) (not parsedp
)) sec
3885 (org-element--parse-objects
3886 (match-beginning 2) (match-end 2) nil restrict
))))))
3887 ;; Attribute a property name to KWD.
3888 (kwd-sym (and kwd
(intern (concat ":" (downcase kwd
))))))
3889 ;; Now set final shape for VALUE.
3892 (org-element--parse-objects
3894 (progn (end-of-line) (skip-chars-backward " \t") (point))
3897 (setq value
(and (or value dual-value
) (cons value dual-value
))))
3898 (when (or (member kwd org-element-multiple-keywords
)
3899 ;; Attributes can always appear on multiple lines.
3900 (string-match "^ATTR_" kwd
))
3901 (setq value
(cons value
(plist-get output kwd-sym
))))
3902 ;; Eventually store the new value in OUTPUT.
3903 (setq output
(plist-put output kwd-sym value
))
3904 ;; Move to next keyword.
3906 ;; If affiliated keywords are orphaned: move back to first one.
3907 ;; They will be parsed as a paragraph.
3908 (when (looking-at "[ \t]*$") (goto-char origin
) (setq output nil
))
3910 (cons origin output
))))
3916 ;; The two major functions here are `org-element-parse-buffer', which
3917 ;; parses Org syntax inside the current buffer, taking into account
3918 ;; region, narrowing, or even visibility if specified, and
3919 ;; `org-element-parse-secondary-string', which parses objects within
3922 ;; The (almost) almighty `org-element-map' allows to apply a function
3923 ;; on elements or objects matching some type, and accumulate the
3924 ;; resulting values. In an export situation, it also skips unneeded
3925 ;; parts of the parse tree.
3927 (defun org-element-parse-buffer (&optional granularity visible-only
)
3928 "Recursively parse the buffer and return structure.
3929 If narrowing is in effect, only parse the visible part of the
3932 Optional argument GRANULARITY determines the depth of the
3933 recursion. It can be set to the following symbols:
3935 `headline' Only parse headlines.
3936 `greater-element' Don't recurse into greater elements excepted
3937 headlines and sections. Thus, elements
3938 parsed are the top-level ones.
3939 `element' Parse everything but objects and plain text.
3940 `object' Parse the complete buffer (default).
3942 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3945 An element or an objects is represented as a list with the
3946 pattern (TYPE PROPERTIES CONTENTS), where :
3948 TYPE is a symbol describing the element or object. See
3949 `org-element-all-elements' and `org-element-all-objects' for an
3950 exhaustive list of such symbols. One can retrieve it with
3951 `org-element-type' function.
3953 PROPERTIES is the list of attributes attached to the element or
3954 object, as a plist. Although most of them are specific to the
3955 element or object type, all types share `:begin', `:end',
3956 `:post-blank' and `:parent' properties, which respectively
3957 refer to buffer position where the element or object starts,
3958 ends, the number of white spaces or blank lines after it, and
3959 the element or object containing it. Properties values can be
3960 obtained by using `org-element-property' function.
3962 CONTENTS is a list of elements, objects or raw strings
3963 contained in the current element or object, when applicable.
3964 One can access them with `org-element-contents' function.
3966 The Org buffer has `org-data' as type and nil as properties.
3967 `org-element-map' function can be used to find specific elements
3968 or objects within the parse tree.
3970 This function assumes that current major mode is `org-mode'."
3972 (goto-char (point-min))
3973 (org-skip-whitespace)
3974 (org-element--parse-elements
3975 (point-at-bol) (point-max)
3976 ;; Start in `first-section' mode so text before the first
3977 ;; headline belongs to a section.
3978 'first-section nil granularity visible-only
(list 'org-data nil
))))
3980 (defun org-element-parse-secondary-string (string restriction
&optional parent
)
3981 "Recursively parse objects in STRING and return structure.
3983 RESTRICTION is a symbol limiting the object types that will be
3986 Optional argument PARENT, when non-nil, is the element or object
3987 containing the secondary string. It is used to set correctly
3988 `:parent' property within the string.
3990 If STRING is the empty string or nil, return nil."
3993 ((equal string
"") nil
)
3994 (t (let ((local-variables (buffer-local-variables)))
3996 (dolist (v local-variables
)
3998 (if (symbolp v
) (makunbound v
)
3999 (org-set-local (car v
) (cdr v
)))))
4001 (restore-buffer-modified-p nil
)
4002 (let ((data (org-element--parse-objects
4003 (point-min) (point-max) nil restriction
)))
4005 (dolist (o data
) (org-element-put-property o
:parent parent
)))
4008 (defun org-element-map
4009 (data types fun
&optional info first-match no-recursion with-affiliated
)
4010 "Map a function on selected elements or objects.
4012 DATA is a parse tree, an element, an object, a string, or a list
4013 of such constructs. TYPES is a symbol or list of symbols of
4014 elements or objects types (see `org-element-all-elements' and
4015 `org-element-all-objects' for a complete list of types). FUN is
4016 the function called on the matching element or object. It has to
4017 accept one argument: the element or object itself.
4019 When optional argument INFO is non-nil, it should be a plist
4020 holding export options. In that case, parts of the parse tree
4021 not exportable according to that property list will be skipped.
4023 When optional argument FIRST-MATCH is non-nil, stop at the first
4024 match for which FUN doesn't return nil, and return that value.
4026 Optional argument NO-RECURSION is a symbol or a list of symbols
4027 representing elements or objects types. `org-element-map' won't
4028 enter any recursive element or object whose type belongs to that
4029 list. Though, FUN can still be applied on them.
4031 When optional argument WITH-AFFILIATED is non-nil, FUN will also
4032 apply to matching objects within parsed affiliated keywords (see
4033 `org-element-parsed-keywords').
4035 Nil values returned from FUN do not appear in the results.
4041 Assuming TREE is a variable containing an Org buffer parse tree,
4042 the following example will return a flat list of all `src-block'
4043 and `example-block' elements in it:
4045 \(org-element-map tree '(example-block src-block) #'identity)
4047 The following snippet will find the first headline with a level
4048 of 1 and a \"phone\" tag, and will return its beginning position:
4050 \(org-element-map tree 'headline
4052 \(and (= (org-element-property :level hl) 1)
4053 \(member \"phone\" (org-element-property :tags hl))
4054 \(org-element-property :begin hl)))
4057 The next example will return a flat list of all `plain-list' type
4058 elements in TREE that are not a sub-list themselves:
4060 \(org-element-map tree 'plain-list #'identity nil nil 'plain-list)
4062 Eventually, this example will return a flat list of all `bold'
4063 type objects containing a `latex-snippet' type object, even
4064 looking into captions:
4066 \(org-element-map tree 'bold
4068 \(and (org-element-map b 'latex-snippet #'identity nil t) b))
4070 ;; Ensure TYPES and NO-RECURSION are a list, even of one element.
4071 (let* ((types (if (listp types
) types
(list types
)))
4072 (no-recursion (if (listp no-recursion
) no-recursion
4073 (list no-recursion
)))
4074 ;; Recursion depth is determined by --CATEGORY.
4077 (let ((category 'greater-elements
)
4078 (all-objects (cons 'plain-text org-element-all-objects
)))
4079 (dolist (type types category
)
4080 (cond ((memq type all-objects
)
4081 ;; If one object is found, the function has to
4082 ;; recurse into every object.
4083 (throw 'found
'objects
))
4084 ((not (memq type org-element-greater-elements
))
4085 ;; If one regular element is found, the
4086 ;; function has to recurse, at least, into
4087 ;; every element it encounters.
4088 (and (not (eq category
'elements
))
4089 (setq category
'elements
))))))))
4094 ;; Recursively walk DATA. INFO, if non-nil, is a plist
4095 ;; holding contextual information.
4096 (let ((--type (org-element-type --data
)))
4099 ;; Ignored element in an export context.
4100 ((and info
(memq --data
(plist-get info
:ignore-list
))))
4101 ;; List of elements or objects.
4102 ((not --type
) (mapc --walk-tree --data
))
4103 ;; Unconditionally enter parse trees.
4104 ((eq --type
'org-data
)
4105 (mapc --walk-tree
(org-element-contents --data
)))
4107 ;; Check if TYPE is matching among TYPES. If so,
4108 ;; apply FUN to --DATA and accumulate return value
4109 ;; into --ACC (or exit if FIRST-MATCH is non-nil).
4110 (when (memq --type types
)
4111 (let ((result (funcall fun --data
)))
4112 (cond ((not result
))
4113 (first-match (throw '--map-first-match result
))
4114 (t (push result --acc
)))))
4115 ;; If --DATA has a secondary string that can contain
4116 ;; objects with their type among TYPES, look into it.
4117 (when (and (eq --category
'objects
) (not (stringp --data
)))
4118 (dolist (p (cdr (assq --type
4119 org-element-secondary-value-alist
)))
4120 (funcall --walk-tree
(org-element-property p --data
))))
4121 ;; If --DATA has any parsed affiliated keywords and
4122 ;; WITH-AFFILIATED is non-nil, look for objects in
4124 (when (and with-affiliated
4125 (eq --category
'objects
)
4126 (memq --type org-element-all-elements
))
4127 (dolist (kwd-pair org-element--parsed-properties-alist
)
4128 (let ((kwd (car kwd-pair
))
4129 (value (org-element-property (cdr kwd-pair
) --data
)))
4130 ;; Pay attention to the type of parsed keyword.
4131 ;; In particular, preserve order for multiple
4135 ((member kwd org-element-dual-keywords
)
4136 (if (member kwd org-element-multiple-keywords
)
4137 (dolist (line (reverse value
))
4138 (funcall --walk-tree
(cdr line
))
4139 (funcall --walk-tree
(car line
)))
4140 (funcall --walk-tree
(cdr value
))
4141 (funcall --walk-tree
(car value
))))
4142 ((member kwd org-element-multiple-keywords
)
4143 (mapc --walk-tree
(reverse value
)))
4144 (t (funcall --walk-tree value
))))))
4145 ;; Determine if a recursion into --DATA is possible.
4147 ;; --TYPE is explicitly removed from recursion.
4148 ((memq --type no-recursion
))
4149 ;; --DATA has no contents.
4150 ((not (org-element-contents --data
)))
4151 ;; Looking for greater elements but --DATA is simply
4152 ;; an element or an object.
4153 ((and (eq --category
'greater-elements
)
4154 (not (memq --type org-element-greater-elements
))))
4155 ;; Looking for elements but --DATA is an object.
4156 ((and (eq --category
'elements
)
4157 (memq --type org-element-all-objects
)))
4158 ;; In any other case, map contents.
4159 (t (mapc --walk-tree
(org-element-contents --data
))))))))))
4160 (catch '--map-first-match
4161 (funcall --walk-tree data
)
4162 ;; Return value in a proper order.
4164 (put 'org-element-map
'lisp-indent-function
2)
4166 ;; The following functions are internal parts of the parser.
4168 ;; The first one, `org-element--parse-elements' acts at the element's
4171 ;; The second one, `org-element--parse-objects' applies on all objects
4172 ;; of a paragraph or a secondary string. It calls
4173 ;; `org-element--object-lex' to find the next object in the current
4176 (defsubst org-element--next-mode
(type parentp
)
4177 "Return next special mode according to TYPE, or nil.
4178 TYPE is a symbol representing the type of an element or object
4179 containing next element if PARENTP is non-nil, or before it
4180 otherwise. Modes can be either `first-section', `item',
4181 `node-property', `planning', `property-drawer', `section',
4182 `table-row' or nil."
4187 (property-drawer 'node-property
)
4192 (node-property 'node-property
)
4193 (planning 'property-drawer
)
4194 (table-row 'table-row
))))
4196 (defun org-element--parse-elements
4197 (beg end mode structure granularity visible-only acc
)
4198 "Parse elements between BEG and END positions.
4200 MODE prioritizes some elements over the others. It can be set to
4201 `first-section', `section', `planning', `item', `node-property'
4204 When value is `item', STRUCTURE will be used as the current list
4207 GRANULARITY determines the depth of the recursion. See
4208 `org-element-parse-buffer' for more information.
4210 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
4213 Elements are accumulated into ACC."
4216 ;; Visible only: skip invisible parts at the beginning of the
4218 (when (and visible-only
(org-invisible-p2))
4219 (goto-char (min (1+ (org-find-visible)) end
)))
4220 ;; When parsing only headlines, skip any text before first one.
4221 (when (and (eq granularity
'headline
) (not (org-at-heading-p)))
4222 (org-with-limited-levels (outline-next-heading)))
4224 (while (< (point) end
)
4225 ;; Find current element's type and parse it accordingly to
4227 (let* ((element (org-element--current-element
4228 end granularity mode structure
))
4229 (type (org-element-type element
))
4230 (cbeg (org-element-property :contents-begin element
)))
4231 (goto-char (org-element-property :end element
))
4232 ;; Visible only: skip invisible parts between siblings.
4233 (when (and visible-only
(org-invisible-p2))
4234 (goto-char (min (1+ (org-find-visible)) end
)))
4235 ;; Fill ELEMENT contents by side-effect.
4237 ;; If element has no contents, don't modify it.
4239 ;; Greater element: parse it between `contents-begin' and
4240 ;; `contents-end'. Make sure GRANULARITY allows the
4241 ;; recursion, or ELEMENT is a headline, in which case going
4242 ;; inside is mandatory, in order to get sub-level headings.
4243 ((and (memq type org-element-greater-elements
)
4244 (or (memq granularity
'(element object nil
))
4245 (and (eq granularity
'greater-element
)
4247 (eq type
'headline
)))
4248 (org-element--parse-elements
4249 cbeg
(org-element-property :contents-end element
)
4250 ;; Possibly switch to a special mode.
4251 (org-element--next-mode type t
)
4252 (and (memq type
'(item plain-list
))
4253 (org-element-property :structure element
))
4254 granularity visible-only element
))
4255 ;; ELEMENT has contents. Parse objects inside, if
4256 ;; GRANULARITY allows it.
4257 ((memq granularity
'(object nil
))
4258 (org-element--parse-objects
4259 cbeg
(org-element-property :contents-end element
) element
4260 (org-element-restriction type
))))
4261 (org-element-adopt-elements acc element
)
4263 (setq mode
(org-element--next-mode type nil
))))
4267 (defun org-element--object-lex (restriction)
4268 "Return next object in current buffer or nil.
4269 RESTRICTION is a list of object types, as symbols, that should be
4270 looked after. This function assumes that the buffer is narrowed
4271 to an appropriate container (e.g., a paragraph)."
4272 (if (memq 'table-cell restriction
) (org-element-table-cell-parser)
4274 (let ((limit (and org-target-link-regexp
4276 (or (bolp) (backward-char))
4277 (re-search-forward org-target-link-regexp nil t
))
4278 (match-beginning 1)))
4280 (while (and (not found
)
4281 (re-search-forward org-element--object-regexp limit t
))
4282 (goto-char (match-beginning 0))
4283 (let ((result (match-string 0)))
4286 ((eq (compare-strings result nil nil
"call_" nil nil t
) t
)
4287 (and (memq 'inline-babel-call restriction
)
4288 (org-element-inline-babel-call-parser)))
4289 ((eq (compare-strings result nil nil
"src_" nil nil t
) t
)
4290 (and (memq 'inline-src-block restriction
)
4291 (org-element-inline-src-block-parser)))
4294 (?^
(and (memq 'superscript restriction
)
4295 (org-element-superscript-parser)))
4296 (?_
(or (and (memq 'subscript restriction
)
4297 (org-element-subscript-parser))
4298 (and (memq 'underline restriction
)
4299 (org-element-underline-parser))))
4300 (?
* (and (memq 'bold restriction
)
4301 (org-element-bold-parser)))
4302 (?
/ (and (memq 'italic restriction
)
4303 (org-element-italic-parser)))
4304 (?~
(and (memq 'code restriction
)
4305 (org-element-code-parser)))
4306 (?
= (and (memq 'verbatim restriction
)
4307 (org-element-verbatim-parser)))
4308 (?
+ (and (memq 'strike-through restriction
)
4309 (org-element-strike-through-parser)))
4310 (?
@ (and (memq 'export-snippet restriction
)
4311 (org-element-export-snippet-parser)))
4312 (?
{ (and (memq 'macro restriction
)
4313 (org-element-macro-parser)))
4314 (?$
(and (memq 'latex-fragment restriction
)
4315 (org-element-latex-fragment-parser)))
4317 (if (eq (aref result
1) ?
<)
4318 (or (and (memq 'radio-target restriction
)
4319 (org-element-radio-target-parser))
4320 (and (memq 'target restriction
)
4321 (org-element-target-parser)))
4322 (or (and (memq 'timestamp restriction
)
4323 (org-element-timestamp-parser))
4324 (and (memq 'link restriction
)
4325 (org-element-link-parser)))))
4327 (if (eq (aref result
1) ?
\\)
4328 (and (memq 'line-break restriction
)
4329 (org-element-line-break-parser))
4330 (or (and (memq 'entity restriction
)
4331 (org-element-entity-parser))
4332 (and (memq 'latex-fragment restriction
)
4333 (org-element-latex-fragment-parser)))))
4335 (if (eq (aref result
1) ?\
[)
4336 (and (memq 'link restriction
)
4337 (org-element-link-parser))
4338 (or (and (memq 'footnote-reference restriction
)
4339 (org-element-footnote-reference-parser))
4340 (and (memq 'timestamp restriction
)
4341 (org-element-timestamp-parser))
4342 (and (memq 'statistics-cookie restriction
)
4343 (org-element-statistics-cookie-parser)))))
4344 ;; This is probably a plain link.
4345 (otherwise (and (or (memq 'link restriction
)
4346 (memq 'plain-link restriction
))
4347 (org-element-link-parser)))))))
4348 (or (eobp) (forward-char))))
4351 ((and limit
(memq 'link restriction
))
4352 (goto-char limit
) (org-element-link-parser)))))))
4354 (defun org-element--parse-objects (beg end acc restriction
)
4355 "Parse objects between BEG and END and return recursive structure.
4357 Objects are accumulated in ACC.
4359 RESTRICTION is a list of object successors which are allowed in
4360 the current object."
4363 (narrow-to-region beg end
)
4364 (goto-char (point-min))
4366 (while (and (not (eobp))
4367 (setq next-object
(org-element--object-lex restriction
)))
4368 ;; 1. Text before any object. Untabify it.
4369 (let ((obj-beg (org-element-property :begin next-object
)))
4370 (unless (= (point) obj-beg
)
4372 (org-element-adopt-elements
4374 (replace-regexp-in-string
4375 "\t" (make-string tab-width ?
)
4376 (buffer-substring-no-properties (point) obj-beg
))))))
4378 (let ((obj-end (org-element-property :end next-object
))
4379 (cont-beg (org-element-property :contents-begin next-object
)))
4380 ;; Fill contents of NEXT-OBJECT by side-effect, if it has
4381 ;; a recursive type.
4383 (memq (car next-object
) org-element-recursive-objects
))
4384 (org-element--parse-objects
4385 cont-beg
(org-element-property :contents-end next-object
)
4386 next-object
(org-element-restriction next-object
)))
4387 (setq acc
(org-element-adopt-elements acc next-object
))
4388 (goto-char obj-end
))))
4389 ;; 3. Text after last object. Untabify it.
4392 (org-element-adopt-elements
4394 (replace-regexp-in-string
4395 "\t" (make-string tab-width ?
)
4396 (buffer-substring-no-properties (point) end
)))))
4402 ;;; Towards A Bijective Process
4404 ;; The parse tree obtained with `org-element-parse-buffer' is really
4405 ;; a snapshot of the corresponding Org buffer. Therefore, it can be
4406 ;; interpreted and expanded into a string with canonical Org syntax.
4407 ;; Hence `org-element-interpret-data'.
4409 ;; The function relies internally on
4410 ;; `org-element--interpret-affiliated-keywords'.
4413 (defun org-element-interpret-data (data)
4414 "Interpret DATA as Org syntax.
4415 DATA is a parse tree, an element, an object or a secondary string
4416 to interpret. Return Org syntax as a string."
4417 (org-element--interpret-data-1 data nil
))
4419 (defun org-element--interpret-data-1 (data parent
)
4420 "Interpret DATA as Org syntax.
4422 DATA is a parse tree, an element, an object or a secondary string
4423 to interpret. PARENT is used for recursive calls. It contains
4424 the element or object containing data, or nil.
4426 Return Org syntax as a string."
4427 (let* ((type (org-element-type data
))
4428 ;; Find interpreter for current object or element. If it
4429 ;; doesn't exist (e.g. this is a pseudo object or element),
4430 ;; return contents, if any.
4432 (let ((fun (intern (format "org-element-%s-interpreter" type
))))
4433 (if (fboundp fun
) fun
(lambda (data contents
) contents
))))
4436 ;; Secondary string.
4439 (lambda (obj) (org-element--interpret-data-1 obj parent
)) data
""))
4440 ;; Full Org document.
4441 ((eq type
'org-data
)
4442 (mapconcat (lambda (obj) (org-element--interpret-data-1 obj parent
))
4443 (org-element-contents data
) ""))
4444 ;; Plain text: return it.
4445 ((stringp data
) data
)
4446 ;; Element or object without contents.
4447 ((not (org-element-contents data
)) (funcall interpret data nil
))
4448 ;; Element or object with contents.
4450 (funcall interpret data
4451 ;; Recursively interpret contents.
4453 (lambda (obj) (org-element--interpret-data-1 obj data
))
4454 (org-element-contents
4455 (if (not (memq type
'(paragraph verse-block
)))
4457 ;; Fix indentation of elements containing
4458 ;; objects. We ignore `table-row' elements
4459 ;; as they are one line long anyway.
4460 (org-element-normalize-contents
4462 ;; When normalizing first paragraph of an
4463 ;; item or a footnote-definition, ignore
4464 ;; first line's indentation.
4465 (and (eq type
'paragraph
)
4466 (equal data
(car (org-element-contents parent
)))
4467 (memq (org-element-type parent
)
4468 '(footnote-definition item
))))))
4470 (if (memq type
'(org-data plain-text nil
)) results
4471 ;; Build white spaces. If no `:post-blank' property is
4472 ;; specified, assume its value is 0.
4473 (let ((post-blank (or (org-element-property :post-blank data
) 0)))
4474 (if (or (memq type org-element-all-objects
)
4476 (let ((type (org-element-type parent
)))
4478 (memq type org-element-object-containers
)))))
4479 (concat results
(make-string post-blank ?\s
))
4481 (org-element--interpret-affiliated-keywords data
)
4482 (org-element-normalize-string results
)
4483 (make-string post-blank ?
\n)))))))
4485 (defun org-element--interpret-affiliated-keywords (element)
4486 "Return ELEMENT's affiliated keywords as Org syntax.
4487 If there is no affiliated keyword, return the empty string."
4488 (let ((keyword-to-org
4492 (when (member key org-element-dual-keywords
)
4493 (setq dual
(cdr value
) value
(car value
)))
4496 (format "[%s]" (org-element-interpret-data dual
)))
4498 (if (member key org-element-parsed-keywords
)
4499 (org-element-interpret-data value
)
4504 (let ((value (org-element-property prop element
))
4505 (keyword (upcase (substring (symbol-name prop
) 1))))
4507 (if (or (member keyword org-element-multiple-keywords
)
4508 ;; All attribute keywords can have multiple lines.
4509 (string-match "^ATTR_" keyword
))
4510 (mapconcat (lambda (line) (funcall keyword-to-org keyword line
))
4513 (funcall keyword-to-org keyword value
)))))
4514 ;; List all ELEMENT's properties matching an attribute line or an
4515 ;; affiliated keyword, but ignore translated keywords since they
4516 ;; cannot belong to the property list.
4517 (loop for prop in
(nth 1 element
) by
'cddr
4518 when
(let ((keyword (upcase (substring (symbol-name prop
) 1))))
4519 (or (string-match "^ATTR_" keyword
)
4521 (member keyword org-element-affiliated-keywords
)
4523 org-element-keyword-translation-alist
)))))
4527 ;; Because interpretation of the parse tree must return the same
4528 ;; number of blank lines between elements and the same number of white
4529 ;; space after objects, some special care must be given to white
4532 ;; The first function, `org-element-normalize-string', ensures any
4533 ;; string different from the empty string will end with a single
4534 ;; newline character.
4536 ;; The second function, `org-element-normalize-contents', removes
4537 ;; global indentation from the contents of the current element.
4539 (defun org-element-normalize-string (s)
4540 "Ensure string S ends with a single newline character.
4542 If S isn't a string return it unchanged. If S is the empty
4543 string, return it. Otherwise, return a new string with a single
4544 newline character at its end."
4546 ((not (stringp s
)) s
)
4548 (t (and (string-match "\\(\n[ \t]*\\)*\\'" s
)
4549 (replace-match "\n" nil nil s
)))))
4551 (defun org-element-normalize-contents (element &optional ignore-first
)
4552 "Normalize plain text in ELEMENT's contents.
4554 ELEMENT must only contain plain text and objects.
4556 If optional argument IGNORE-FIRST is non-nil, ignore first line's
4557 indentation to compute maximal common indentation.
4559 Return the normalized element that is element with global
4560 indentation removed from its contents. The function assumes that
4561 indentation is not done with TAB characters."
4562 (let* ((min-ind most-positive-fixnum
)
4563 find-min-ind
; For byte-compiler.
4565 ;; Return minimal common indentation within BLOB. This is
4566 ;; done by walking recursively BLOB and updating MIN-IND
4567 ;; along the way. FIRST-FLAG is non-nil when the next
4568 ;; object is expected to be a string that doesn't start with
4569 ;; a newline character. It happens for strings at the
4570 ;; beginnings of the contents or right after a line break.
4571 (lambda (blob first-flag
)
4572 (dolist (object (org-element-contents blob
))
4574 (setq first-flag nil
)
4575 ;; Objects cannot start with spaces: in this case,
4576 ;; indentation is 0.
4577 (if (not (stringp object
)) (throw 'zero
(setq min-ind
0))
4578 (string-match "\\` *" object
)
4579 (let ((len (match-end 0)))
4580 ;; An indentation of zero means no string will be
4581 ;; modified. Quit the process.
4582 (if (zerop len
) (throw 'zero
(setq min-ind
0))
4583 (setq min-ind
(min len min-ind
))))))
4586 (dolist (line (cdr (org-split-string object
" *\n")))
4587 (unless (string= line
"")
4588 (setq min-ind
(min (org-get-indentation line
) min-ind
)))))
4589 ((eq (org-element-type object
) 'line-break
) (setq first-flag t
))
4590 ((memq (org-element-type object
) org-element-recursive-objects
)
4591 (funcall find-min-ind object first-flag
)))))))
4592 ;; Find minimal indentation in ELEMENT.
4593 (catch 'zero
(funcall find-min-ind element
(not ignore-first
)))
4594 (if (or (zerop min-ind
) (= min-ind most-positive-fixnum
)) element
4595 ;; Build ELEMENT back, replacing each string with the same
4596 ;; string minus common indentation.
4597 (let* (build ; For byte compiler.
4599 (lambda (blob first-flag
)
4600 ;; Return BLOB with all its strings indentation
4601 ;; shortened from MIN-IND white spaces. FIRST-FLAG is
4602 ;; non-nil when the next object is expected to be
4603 ;; a string that doesn't start with a newline
4609 (setq first-flag nil
)
4610 (when (stringp object
)
4612 (replace-regexp-in-string
4613 (format "\\` \\{%d\\}" min-ind
)
4617 (replace-regexp-in-string
4618 (format "\n \\{%d\\}" min-ind
) "\n" object
))
4619 ((memq (org-element-type object
)
4620 org-element-recursive-objects
)
4621 (funcall build object first-flag
))
4622 ((eq (org-element-type object
) 'line-break
)
4626 (org-element-contents blob
)))
4628 (funcall build element
(not ignore-first
))))))
4634 ;; Implement a caching mechanism for `org-element-at-point' and
4635 ;; `org-element-context', which see.
4637 ;; A single public function is provided: `org-element-cache-reset'.
4639 ;; Cache is enabled by default, but can be disabled globally with
4640 ;; `org-element-use-cache'. `org-element-cache-sync-idle-time',
4641 ;; org-element-cache-sync-duration' and `org-element-cache-sync-break'
4642 ;; can be tweaked to control caching behaviour.
4644 ;; Internally, parsed elements are stored in an AVL tree,
4645 ;; `org-element--cache'. This tree is updated lazily: whenever
4646 ;; a change happens to the buffer, a synchronization request is
4647 ;; registered in `org-element--cache-sync-requests' (see
4648 ;; `org-element--cache-submit-request'). During idle time, requests
4649 ;; are processed by `org-element--cache-sync'. Synchronization also
4650 ;; happens when an element is required from the cache. In this case,
4651 ;; the process stops as soon as the needed element is up-to-date.
4653 ;; A synchronization request can only apply on a synchronized part of
4654 ;; the cache. Therefore, the cache is updated at least to the
4655 ;; location where the new request applies. Thus, requests are ordered
4656 ;; from left to right and all elements starting before the first
4657 ;; request are correct. This property is used by functions like
4658 ;; `org-element--cache-find' to retrieve elements in the part of the
4659 ;; cache that can be trusted.
4661 ;; A request applies to every element, starting from its original
4662 ;; location (or key, see below). When a request is processed, it
4663 ;; moves forward and may collide the next one. In this case, both
4664 ;; requests are merged into a new one that starts from that element.
4665 ;; As a consequence, the whole synchronization complexity does not
4666 ;; depend on the number of pending requests, but on the number of
4667 ;; elements the very first request will be applied on.
4669 ;; Elements cannot be accessed through their beginning position, which
4670 ;; may or may not be up-to-date. Instead, each element in the tree is
4671 ;; associated to a key, obtained with `org-element--cache-key'. This
4672 ;; mechanism is robust enough to preserve total order among elements
4673 ;; even when the tree is only partially synchronized.
4675 ;; Objects contained in an element are stored in a hash table,
4676 ;; `org-element--cache-objects'.
4679 (defvar org-element-use-cache t
4680 "Non nil when Org parser should cache its results.
4681 This is mostly for debugging purpose.")
4683 (defvar org-element-cache-sync-idle-time
0.6
4684 "Length, in seconds, of idle time before syncing cache.")
4686 (defvar org-element-cache-sync-duration
(seconds-to-time 0.04)
4687 "Maximum duration, as a time value, for a cache synchronization.
4688 If the synchronization is not over after this delay, the process
4689 pauses and resumes after `org-element-cache-sync-break'
4692 (defvar org-element-cache-sync-break
(seconds-to-time 0.3)
4693 "Duration, as a time value, of the pause between synchronizations.
4694 See `org-element-cache-sync-duration' for more information.")
4699 (defvar org-element--cache nil
4700 "AVL tree used to cache elements.
4701 Each node of the tree contains an element. Comparison is done
4702 with `org-element--cache-compare'. This cache is used in
4703 `org-element-at-point'.")
4705 (defvar org-element--cache-objects nil
4706 "Hash table used as to cache objects.
4707 Key is an element, as returned by `org-element-at-point', and
4708 value is an alist where each association is:
4710 \(PARENT COMPLETEP . OBJECTS)
4712 where PARENT is an element or object, COMPLETEP is a boolean,
4713 non-nil when all direct children of parent are already cached and
4714 OBJECTS is a list of such children, as objects, from farthest to
4717 In the following example, \\alpha, bold object and \\beta are
4718 contained within a paragraph
4722 If the paragraph is completely parsed, OBJECTS-DATA will be
4724 \((PARAGRAPH t BOLD-OBJECT ENTITY-OBJECT)
4725 \(BOLD-OBJECT t ENTITY-OBJECT))
4727 whereas in a partially parsed paragraph, it could be
4729 \((PARAGRAPH nil ENTITY-OBJECT))
4731 This cache is used in `org-element-context'.")
4733 (defvar org-element--cache-sync-requests nil
4734 "List of pending synchronization requests.
4736 A request is a vector with the following pattern:
4738 \[NEXT BEG END OFFSET PARENT PHASE]
4740 Processing a synchronization request consists of three phases:
4742 0. Delete modified elements,
4743 1. Fill missing area in cache,
4744 2. Shift positions and re-parent elements after the changes.
4746 During phase 0, NEXT is the key of the first element to be
4747 removed, BEG and END is buffer position delimiting the
4748 modifications. Elements starting between them (inclusive) are
4749 removed. So are elements whose parent is removed. PARENT, when
4750 non-nil, is the parent of the first element to be removed.
4752 During phase 1, NEXT is the key of the next known element in
4753 cache and BEG its beginning position. Parse buffer between that
4754 element and the one before it in order to determine the parent of
4755 the next element. Set PARENT to the element containing NEXT.
4757 During phase 2, NEXT is the key of the next element to shift in
4758 the parse tree. All elements starting from this one have their
4759 properties relatives to buffer positions shifted by integer
4760 OFFSET and, if they belong to element PARENT, are adopted by it.
4762 PHASE specifies the phase number, as an integer.")
4764 (defvar org-element--cache-sync-timer nil
4765 "Timer used for cache synchronization.")
4767 (defvar org-element--cache-sync-keys nil
4768 "Hash table used to store keys during synchronization.
4769 See `org-element--cache-key' for more information.")
4771 (defsubst org-element--cache-key
(element)
4772 "Return a unique key for ELEMENT in cache tree.
4774 Keys are used to keep a total order among elements in the cache.
4775 Comparison is done with `org-element--cache-key-less-p'.
4777 When no synchronization is taking place, a key is simply the
4778 beginning position of the element, or that position plus one in
4779 the case of an first item (respectively row) in
4780 a list (respectively a table).
4782 During a synchronization, the key is the one the element had when
4783 the cache was synchronized for the last time. Elements added to
4784 cache during the synchronization get a new key generated with
4785 `org-element--cache-generate-key'.
4787 Such keys are stored in `org-element--cache-sync-keys'. The hash
4788 table is cleared once the synchronization is complete."
4789 (or (gethash element org-element--cache-sync-keys
)
4790 (let* ((begin (org-element-property :begin element
))
4791 ;; Increase beginning position of items (respectively
4792 ;; table rows) by one, so the first item can get
4793 ;; a different key from its parent list (respectively
4795 (key (if (memq (org-element-type element
) '(item table-row
))
4798 (if org-element--cache-sync-requests
4799 (puthash element key org-element--cache-sync-keys
)
4802 (defun org-element--cache-generate-key (lower upper
)
4803 "Generate a key between LOWER and UPPER.
4805 LOWER and UPPER are integers or lists, possibly empty.
4807 If LOWER and UPPER are equals, return LOWER. Otherwise, return
4808 a unique key, as an integer or a list of integers, according to
4809 the following rules:
4811 - LOWER and UPPER are compared level-wise until values differ.
4813 - If, at a given level, LOWER and UPPER differ from more than
4814 2, the new key shares all the levels above with LOWER and
4815 gets a new level. Its value is the mean between LOWER and
4818 \(1 2) + (1 4) --> (1 3)
4820 - If LOWER has no value to compare with, it is assumed that its
4821 value is `most-negative-fixnum'. E.g.,
4829 where m is `most-negative-fixnum'. Likewise, if UPPER is
4830 short of levels, the current value is `most-positive-fixnum'.
4832 - If they differ from only one, the new key inherits from
4833 current LOWER level and fork it at the next level. E.g.,
4841 where M is `most-positive-fixnum'.
4843 - If the key is only one level long, it is returned as an
4846 \(1 2) + (3 2) --> 2
4848 When they are not equals, the function assumes that LOWER is
4849 lesser than UPPER, per `org-element--cache-key-less-p'."
4850 (if (equal lower upper
) lower
4851 (let ((lower (if (integerp lower
) (list lower
) lower
))
4852 (upper (if (integerp upper
) (list upper
) upper
))
4856 (let ((min (or (car lower
) most-negative-fixnum
))
4857 (max (cond (skip-upper most-positive-fixnum
)
4859 (t most-positive-fixnum
))))
4860 (if (< (1+ min
) max
)
4861 (let ((mean (+ (ash min -
1) (ash max -
1) (logand min max
1))))
4862 (throw 'exit
(if key
(nreverse (cons mean key
)) mean
)))
4863 (when (and (< min max
) (not skip-upper
))
4864 ;; When at a given level, LOWER and UPPER differ from
4865 ;; 1, ignore UPPER altogether. Instead create a key
4866 ;; between LOWER and the greatest key with the same
4867 ;; prefix as LOWER so far.
4868 (setq skip-upper t
))
4870 (setq lower
(cdr lower
) upper
(cdr upper
)))))))))
4872 (defsubst org-element--cache-key-less-p
(a b
)
4873 "Non-nil if key A is less than key B.
4874 A and B are either integers or lists of integers, as returned by
4875 `org-element--cache-key'."
4876 (if (integerp a
) (if (integerp b
) (< a b
) (<= a
(car b
)))
4877 (if (integerp b
) (< (car a
) b
)
4880 (cond ((car-less-than-car a b
) (throw 'exit t
))
4881 ((car-less-than-car b a
) (throw 'exit nil
))
4882 (t (setq a
(cdr a
) b
(cdr b
)))))
4883 ;; If A is empty, either keys are equal (B is also empty) and
4884 ;; we return nil, or A is lesser than B (B is longer) and we
4885 ;; return a non-nil value.
4887 ;; If A is not empty, B is necessarily empty and A is greater
4888 ;; than B (A is longer). Therefore, return nil.
4889 (and (null a
) b
)))))
4891 (defun org-element--cache-compare (a b
)
4892 "Non-nil when element A is located before element B."
4893 (org-element--cache-key-less-p (org-element--cache-key a
)
4894 (org-element--cache-key b
)))
4896 (defsubst org-element--cache-root
()
4897 "Return root value in cache.
4898 This function assumes `org-element--cache' is a valid AVL tree."
4899 (avl-tree--node-left (avl-tree--dummyroot org-element--cache
)))
4904 (defsubst org-element--cache-active-p
()
4905 "Non-nil when cache is active in current buffer."
4906 (and org-element-use-cache
4908 (or (derived-mode-p 'org-mode
) orgstruct-mode
)))
4910 (defun org-element--cache-find (pos &optional side
)
4911 "Find element in cache starting at POS or before.
4913 POS refers to a buffer position.
4915 When optional argument SIDE is non-nil, the function checks for
4916 elements starting at or past POS instead. If SIDE is `both', the
4917 function returns a cons cell where car is the first element
4918 starting at or before POS and cdr the first element starting
4921 The function can only find elements in the synchronized part of
4923 (let ((limit (and org-element--cache-sync-requests
4924 (aref (car org-element--cache-sync-requests
) 0)))
4925 (node (org-element--cache-root))
4928 (let* ((element (avl-tree--node-data node
))
4929 (begin (org-element-property :begin element
)))
4932 (not (org-element--cache-key-less-p
4933 (org-element--cache-key element
) limit
)))
4934 (setq node
(avl-tree--node-left node
)))
4937 node
(avl-tree--node-left node
)))
4940 node
(avl-tree--node-right node
)))
4941 ;; We found an element in cache starting at POS. If `side'
4942 ;; is `both' we also want the next one in order to generate
4943 ;; a key in-between.
4945 ;; If the element is the first row or item in a table or
4946 ;; a plain list, we always return the table or the plain
4949 ;; In any other case, we return the element found.
4951 (setq lower element
)
4952 (setq node
(avl-tree--node-right node
)))
4953 ((and (memq (org-element-type element
) '(item table-row
))
4954 (let ((parent (org-element-property :parent element
)))
4955 (and (= (org-element-property :begin element
)
4956 (org-element-property :contents-begin parent
))
4965 (both (cons lower upper
))
4967 (otherwise upper
))))
4969 (defun org-element--cache-put (element &optional data
)
4970 "Store ELEMENT in current buffer's cache, if allowed.
4971 When optional argument DATA is non-nil, assume is it object data
4972 relative to ELEMENT and store it in the objects cache."
4973 (cond ((not (org-element--cache-active-p)) nil
)
4975 (when org-element--cache-sync-requests
4976 ;; During synchronization, first build an appropriate key
4977 ;; for the new element so `avl-tree-enter' can insert it at
4978 ;; the right spot in the cache.
4979 (let ((keys (org-element--cache-find
4980 (org-element-property :begin element
) 'both
)))
4982 (org-element--cache-generate-key
4983 (and (car keys
) (org-element--cache-key (car keys
)))
4984 (cond ((cdr keys
) (org-element--cache-key (cdr keys
)))
4985 (org-element--cache-sync-requests
4986 (aref (car org-element--cache-sync-requests
) 0))))
4987 org-element--cache-sync-keys
)))
4988 (avl-tree-enter org-element--cache element
))
4989 ;; Headlines are not stored in cache, so objects in titles are
4990 ;; not stored either.
4991 ((eq (org-element-type element
) 'headline
) nil
)
4992 (t (puthash element data org-element--cache-objects
))))
4994 (defsubst org-element--cache-remove
(element)
4995 "Remove ELEMENT from cache.
4996 Assume ELEMENT belongs to cache and that a cache is active."
4997 (avl-tree-delete org-element--cache element
)
4998 (remhash element org-element--cache-objects
))
5001 ;;;; Synchronization
5003 (defsubst org-element--cache-set-timer
(buffer)
5004 "Set idle timer for cache synchronization in BUFFER."
5005 (when org-element--cache-sync-timer
5006 (cancel-timer org-element--cache-sync-timer
))
5007 (setq org-element--cache-sync-timer
5008 (run-with-idle-timer
5009 (let ((idle (current-idle-time)))
5010 (if idle
(time-add idle org-element-cache-sync-break
)
5011 org-element-cache-sync-idle-time
))
5013 #'org-element--cache-sync
5016 (defsubst org-element--cache-interrupt-p
(time-limit)
5017 "Non-nil when synchronization process should be interrupted.
5018 TIME-LIMIT is a time value or nil."
5020 (or (input-pending-p)
5021 (time-less-p time-limit
(current-time)))))
5023 (defsubst org-element--cache-shift-positions
(element offset
&optional props
)
5024 "Shift ELEMENT properties relative to buffer positions by OFFSET.
5026 Properties containing buffer positions are `:begin', `:end',
5027 `:contents-begin', `:contents-end' and `:structure'. When
5028 optional argument PROPS is a list of keywords, only shift
5029 properties provided in that list.
5031 Properties are modified by side-effect."
5032 (let ((properties (nth 1 element
)))
5033 ;; Shift `:structure' property for the first plain list only: it
5034 ;; is the only one that really matters and it prevents from
5035 ;; shifting it more than once.
5036 (when (and (or (not props
) (memq :structure props
))
5037 (eq (org-element-type element
) 'plain-list
)
5038 (not (eq (org-element-type (plist-get properties
:parent
))
5040 (dolist (item (plist-get properties
:structure
))
5041 (incf (car item
) offset
)
5042 (incf (nth 6 item
) offset
)))
5043 (dolist (key '(:begin
:contents-begin
:contents-end
:end
:post-affiliated
))
5044 (let ((value (and (or (not props
) (memq key props
))
5045 (plist-get properties key
))))
5046 (and value
(plist-put properties key
(+ offset value
)))))))
5048 (defun org-element--cache-sync (buffer &optional threshold future-change
)
5049 "Synchronize cache with recent modification in BUFFER.
5051 When optional argument THRESHOLD is non-nil, do the
5052 synchronization for all elements starting before or at threshold,
5053 then exit. Otherwise, synchronize cache for as long as
5054 `org-element-cache-sync-duration' or until Emacs leaves idle
5057 FUTURE-CHANGE, when non-nil, is a buffer position where changes
5058 not registered yet in the cache are going to happen. It is used
5059 in `org-element--cache-submit-request', where cache is partially
5060 updated before current modification are actually submitted."
5061 (when (buffer-live-p buffer
)
5062 (with-current-buffer buffer
5063 (let ((inhibit-quit t
) request next
)
5064 (when org-element--cache-sync-timer
5065 (cancel-timer org-element--cache-sync-timer
))
5067 (while org-element--cache-sync-requests
5068 (setq request
(car org-element--cache-sync-requests
)
5069 next
(nth 1 org-element--cache-sync-requests
))
5070 (org-element--cache-process-request
5072 (and next
(aref next
0))
5074 (and (not threshold
)
5075 (time-add (current-time)
5076 org-element-cache-sync-duration
))
5078 ;; Request processed. Merge current and next offsets and
5079 ;; transfer ending position.
5081 (incf (aref next
3) (aref request
3))
5082 (aset next
2 (aref request
2)))
5083 (setq org-element--cache-sync-requests
5084 (cdr org-element--cache-sync-requests
))))
5085 ;; If more requests are awaiting, set idle timer accordingly.
5086 ;; Otherwise, reset keys.
5087 (if org-element--cache-sync-requests
5088 (org-element--cache-set-timer buffer
)
5089 (clrhash org-element--cache-sync-keys
))))))
5091 (defun org-element--cache-process-request
5092 (request next threshold time-limit future-change
)
5093 "Process synchronization REQUEST for all entries before NEXT.
5095 REQUEST is a vector, built by `org-element--cache-submit-request'.
5097 NEXT is a cache key, as returned by `org-element--cache-key'.
5099 When non-nil, THRESHOLD is a buffer position. Synchronization
5100 stops as soon as a shifted element begins after it.
5102 When non-nil, TIME-LIMIT is a time value. Synchronization stops
5103 after this time or when Emacs exits idle state.
5105 When non-nil, FUTURE-CHANGE is a buffer position where changes
5106 not registered yet in the cache are going to happen. See
5107 `org-element--cache-submit-request' for more information.
5109 Throw `interrupt' if the process stops before completing the
5112 (when (= (aref request
5) 0)
5115 ;; Delete all elements starting after BEG, but not after buffer
5116 ;; position END or past element with key NEXT. Also delete
5117 ;; elements contained within a previously removed element
5118 ;; (stored in `last-container').
5120 ;; At each iteration, we start again at tree root since
5121 ;; a deletion modifies structure of the balanced tree.
5124 (when (org-element--cache-interrupt-p time-limit
)
5125 (throw 'interrupt nil
))
5126 ;; Find first element in cache with key BEG or after it.
5127 (let ((beg (aref request
0))
5128 (end (aref request
2))
5129 (node (org-element--cache-root))
5130 data data-key last-container
)
5132 (let* ((element (avl-tree--node-data node
))
5133 (key (org-element--cache-key element
)))
5135 ((org-element--cache-key-less-p key beg
)
5136 (setq node
(avl-tree--node-right node
)))
5137 ((org-element--cache-key-less-p beg key
)
5140 node
(avl-tree--node-left node
)))
5141 (t (setq data element
5145 (let ((pos (org-element-property :begin data
)))
5146 (if (if (or (not next
)
5147 (org-element--cache-key-less-p data-key next
))
5151 (while (and up
(not (eq up last-container
)))
5152 (setq up
(org-element-property :parent up
)))
5154 (progn (when (and (not last-container
)
5155 (> (org-element-property :end data
)
5157 (setq last-container data
))
5158 (org-element--cache-remove data
))
5159 (aset request
0 data-key
)
5160 (aset request
1 pos
)
5162 (throw 'end-phase nil
)))
5163 ;; No element starting after modifications left in
5164 ;; cache: further processing is futile.
5165 (throw 'quit t
))))))
5166 (when (= (aref request
5) 1)
5169 ;; Phase 0 left a hole in the cache. Some elements after it
5170 ;; could have parents within. For example, in the following
5180 ;; if we remove a blank line between "item" and "Paragraph1",
5181 ;; everything down to "Paragraph2" is removed from cache. But
5182 ;; the paragraph now belongs to the list, and its `:parent'
5183 ;; property no longer is accurate.
5185 ;; Therefore we need to parse again elements in the hole, or at
5186 ;; least in its last section, so that we can re-parent
5187 ;; subsequent elements, during phase 2.
5189 ;; Note that we only need to get the parent from the first
5190 ;; element in cache after the hole.
5192 ;; When next key is lesser or equal to the current one, delegate
5193 ;; phase 1 processing to next request in order to preserve key
5194 ;; order among requests.
5195 (let ((key (aref request
0)))
5196 (when (and next
(not (org-element--cache-key-less-p key next
)))
5197 (let ((next-request (nth 1 org-element--cache-sync-requests
)))
5198 (aset next-request
0 key
)
5199 (aset next-request
1 (aref request
1))
5200 (aset next-request
5 1))
5202 ;; Next element will start at its beginning position plus
5203 ;; offset, since it hasn't been shifted yet. Therefore, LIMIT
5204 ;; contains the real beginning position of the first element to
5205 ;; shift and re-parent.
5206 (let ((limit (+ (aref request
1) (aref request
3))))
5207 (cond ((and threshold
(> limit threshold
)) (throw 'interrupt nil
))
5208 ((and future-change
(>= limit future-change
))
5209 ;; Changes are going to happen around this element and
5210 ;; they will trigger another phase 1 request. Skip the
5214 (let ((parent (org-element--parse-to limit t time-limit
)))
5215 (aset request
4 parent
)
5216 (aset request
5 2))))))
5219 ;; Shift all elements starting from key START, but before NEXT, by
5220 ;; OFFSET, and re-parent them when appropriate.
5222 ;; Elements are modified by side-effect so the tree structure
5225 ;; Once THRESHOLD, if any, is reached, or once there is an input
5226 ;; pending, exit. Before leaving, the current synchronization
5227 ;; request is updated.
5228 (let ((start (aref request
0))
5229 (offset (aref request
3))
5230 (parent (aref request
4))
5231 (node (org-element--cache-root))
5235 ;; No re-parenting nor shifting planned: request is over.
5236 (when (and (not parent
) (zerop offset
)) (throw 'quit t
))
5238 (let* ((data (avl-tree--node-data node
))
5239 (key (org-element--cache-key data
)))
5240 (if (and leftp
(avl-tree--node-left node
)
5241 (not (org-element--cache-key-less-p key start
)))
5242 (progn (push node stack
)
5243 (setq node
(avl-tree--node-left node
)))
5244 (unless (org-element--cache-key-less-p key start
)
5245 ;; We reached NEXT. Request is complete.
5246 (when (equal key next
) (throw 'quit t
))
5247 ;; Handle interruption request. Update current request.
5248 (when (or exit-flag
(org-element--cache-interrupt-p time-limit
))
5249 (aset request
0 key
)
5250 (aset request
4 parent
)
5251 (throw 'interrupt nil
))
5253 (unless (zerop offset
)
5254 (org-element--cache-shift-positions data offset
)
5255 ;; Shift associated objects data, if any.
5256 (dolist (object-data (gethash data org-element--cache-objects
))
5257 (dolist (object (cddr object-data
))
5258 (org-element--cache-shift-positions object offset
))))
5259 (let ((begin (org-element-property :begin data
)))
5260 ;; Update PARENT and re-parent DATA, only when
5261 ;; necessary. Propagate new structures for lists.
5263 (<= (org-element-property :end parent
) begin
))
5264 (setq parent
(org-element-property :parent parent
)))
5265 (cond ((and (not parent
) (zerop offset
)) (throw 'quit nil
))
5267 (let ((p (org-element-property :parent data
)))
5269 (< (org-element-property :begin p
)
5270 (org-element-property :begin parent
)))))
5271 (org-element-put-property data
:parent parent
)
5272 (let ((s (org-element-property :structure parent
)))
5273 (when (and s
(org-element-property :structure data
))
5274 (org-element-put-property data
:structure s
)))))
5275 ;; Cache is up-to-date past THRESHOLD. Request
5277 (when (and threshold
(> begin threshold
)) (setq exit-flag t
))))
5278 (setq node
(if (setq leftp
(avl-tree--node-right node
))
5279 (avl-tree--node-right node
)
5281 ;; We reached end of tree: synchronization complete.
5284 (defun org-element--parse-to (pos &optional syncp time-limit
)
5285 "Parse elements in current section, down to POS.
5287 Start parsing from the closest between the last known element in
5288 cache or headline above. Return the smallest element containing
5291 When optional argument SYNCP is non-nil, return the parent of the
5292 element containing POS instead. In that case, it is also
5293 possible to provide TIME-LIMIT, which is a time value specifying
5294 when the parsing should stop. The function throws `interrupt' if
5295 the process stopped before finding the expected result."
5297 (org-with-wide-buffer
5299 (let* ((cached (and (org-element--cache-active-p)
5300 (org-element--cache-find pos nil
)))
5301 (begin (org-element-property :begin cached
))
5304 ;; Nothing in cache before point: start parsing from first
5305 ;; element following headline above, or first element in
5308 (when (org-with-limited-levels (outline-previous-heading))
5309 (setq mode
'planning
)
5311 (skip-chars-forward " \r\t\n")
5312 (beginning-of-line))
5313 ;; Cache returned exact match: return it.
5315 (throw 'exit
(if syncp
(org-element-property :parent cached
) cached
)))
5316 ;; There's a headline between cached value and POS: cached
5317 ;; value is invalid. Start parsing from first element
5318 ;; following the headline.
5319 ((re-search-backward
5320 (org-with-limited-levels org-outline-regexp-bol
) begin t
)
5322 (skip-chars-forward " \r\t\n")
5324 (setq mode
'planning
))
5325 ;; Check if CACHED or any of its ancestors contain point.
5327 ;; If there is such an element, we inspect it in order to know
5328 ;; if we return it or if we need to parse its contents.
5329 ;; Otherwise, we just start parsing from current location,
5330 ;; which is right after the top-most element containing
5333 ;; As a special case, if POS is at the end of the buffer, we
5334 ;; want to return the innermost element ending there.
5336 ;; Also, if we find an ancestor and discover that we need to
5337 ;; parse its contents, make sure we don't start from
5338 ;; `:contents-begin', as we would otherwise go past CACHED
5339 ;; again. Instead, in that situation, we will resume parsing
5340 ;; from NEXT, which is located after CACHED or its higher
5341 ;; ancestor not containing point.
5344 (pos (if (= (point-max) pos
) (1- pos
) pos
)))
5345 (goto-char (or (org-element-property :contents-begin cached
) begin
))
5346 (while (let ((end (org-element-property :end up
)))
5349 (setq up
(org-element-property :parent up
)))))
5351 ((eobp) (setq element up
))
5352 (t (setq element up next
(point)))))))
5353 ;; Parse successively each element until we reach POS.
5354 (let ((end (or (org-element-property :end element
)
5356 (org-with-limited-levels (outline-next-heading))
5361 (cond ((= (point) pos
) (throw 'exit parent
))
5362 ((org-element--cache-interrupt-p time-limit
)
5363 (throw 'interrupt nil
))))
5365 (setq element
(org-element--current-element
5367 (org-element-property :structure parent
)))
5368 (org-element-put-property element
:parent parent
)
5369 (org-element--cache-put element
))
5370 (let ((elem-end (org-element-property :end element
))
5371 (type (org-element-type element
)))
5373 ;; Skip any element ending before point. Also skip
5374 ;; element ending at point (unless it is also the end of
5375 ;; buffer) since we're sure that another element begins
5377 ((and (<= elem-end pos
) (/= (point-max) elem-end
))
5378 (goto-char elem-end
)
5379 (setq mode
(org-element--next-mode type nil
)))
5380 ;; A non-greater element contains point: return it.
5381 ((not (memq type org-element-greater-elements
))
5382 (throw 'exit element
))
5383 ;; Otherwise, we have to decide if ELEMENT really
5384 ;; contains POS. In that case we start parsing from
5385 ;; contents' beginning.
5387 ;; If POS is at contents' beginning but it is also at
5388 ;; the beginning of the first item in a list or a table.
5389 ;; In that case, we need to create an anchor for that
5390 ;; list or table, so return it.
5392 ;; Also, if POS is at the end of the buffer, no element
5393 ;; can start after it, but more than one may end there.
5394 ;; Arbitrarily, we choose to return the innermost of
5396 ((let ((cbeg (org-element-property :contents-begin element
))
5397 (cend (org-element-property :contents-end element
)))
5402 (not (memq type
'(plain-list table
)))))
5404 (and (= cend pos
) (= (point-max) pos
)))))
5405 (goto-char (or next cbeg
))
5407 mode
(org-element--next-mode type t
)
5410 ;; Otherwise, return ELEMENT as it is the smallest
5411 ;; element containing POS.
5412 (t (throw 'exit element
))))
5413 (setq element nil
)))))))
5416 ;;;; Staging Buffer Changes
5418 (defconst org-element--cache-sensitive-re
5420 org-outline-regexp-bol
"\\|"
5421 "\\\\end{[A-Za-z0-9*]+}[ \t]*$" "\\|"
5423 "#\\+\\(?:BEGIN[:_]\\|END\\(?:_\\|:?[ \t]*$\\)\\)" "\\|"
5424 "\\\\begin{[A-Za-z0-9*]+}" "\\|"
5425 ":\\(?:\\w\\|[-_]\\)+:[ \t]*$"
5427 "Regexp matching a sensitive line, structure wise.
5428 A sensitive line is a headline, inlinetask, block, drawer, or
5429 latex-environment boundary. When such a line is modified,
5430 structure changes in the document may propagate in the whole
5431 section, possibly making cache invalid.")
5433 (defvar org-element--cache-change-warning nil
5434 "Non-nil when a sensitive line is about to be changed.
5435 It is a symbol among nil, t and `headline'.")
5437 (defun org-element--cache-before-change (beg end
)
5438 "Request extension of area going to be modified if needed.
5439 BEG and END are the beginning and end of the range of changed
5440 text. See `before-change-functions' for more information."
5441 (when (org-element--cache-active-p)
5442 (org-with-wide-buffer
5445 (let ((bottom (save-excursion (goto-char end
) (line-end-position))))
5446 (setq org-element--cache-change-warning
5448 (if (and (org-with-limited-levels (org-at-heading-p))
5449 (= (line-end-position) bottom
))
5451 (let ((case-fold-search t
))
5453 org-element--cache-sensitive-re bottom t
)))))))))
5455 (defun org-element--cache-after-change (beg end pre
)
5456 "Update buffer modifications for current buffer.
5457 BEG and END are the beginning and end of the range of changed
5458 text, and the length in bytes of the pre-change text replaced by
5459 that range. See `after-change-functions' for more information."
5460 (when (org-element--cache-active-p)
5461 (org-with-wide-buffer
5466 (bottom (save-excursion (goto-char end
) (line-end-position))))
5467 ;; Determine if modified area needs to be extended, according
5468 ;; to both previous and current state. We make a special
5469 ;; case for headline editing: if a headline is modified but
5470 ;; not removed, do not extend.
5471 (when (case org-element--cache-change-warning
5474 (not (and (org-with-limited-levels (org-at-heading-p))
5475 (= (line-end-position) bottom
))))
5477 (let ((case-fold-search t
))
5479 org-element--cache-sensitive-re bottom t
))))
5480 ;; Effectively extend modified area.
5481 (org-with-limited-levels
5482 (setq top
(progn (goto-char top
)
5483 (when (outline-previous-heading) (forward-line))
5485 (setq bottom
(progn (goto-char bottom
)
5486 (if (outline-next-heading) (1- (point))
5488 ;; Store synchronization request.
5489 (let ((offset (- end beg pre
)))
5490 (org-element--cache-submit-request top
(- bottom offset
) offset
)))))
5491 ;; Activate a timer to process the request during idle time.
5492 (org-element--cache-set-timer (current-buffer))))
5494 (defun org-element--cache-for-removal (beg end offset
)
5495 "Return first element to remove from cache.
5497 BEG and END are buffer positions delimiting buffer modifications.
5498 OFFSET is the size of the changes.
5500 Returned element is usually the first element in cache containing
5501 any position between BEG and END. As an exception, greater
5502 elements around the changes that are robust to contents
5503 modifications are preserved and updated according to the
5505 (let* ((elements (org-element--cache-find (1- beg
) 'both
))
5506 (before (car elements
))
5507 (after (cdr elements
)))
5508 (if (not before
) after
5512 (if (let ((type (org-element-type up
)))
5513 (and (or (memq type
'(center-block dynamic-block quote-block
5515 ;; Drawers named "PROPERTIES" are probably
5516 ;; a properties drawer being edited. Force
5517 ;; parsing to check if editing is over.
5518 (and (eq type
'drawer
)
5520 (org-element-property :drawer-name up
)
5522 (let ((cbeg (org-element-property :contents-begin up
)))
5525 (> (org-element-property :contents-end up
) end
)))))
5526 ;; UP is a robust greater element containing changes.
5527 ;; We only need to extend its ending boundaries.
5528 (org-element--cache-shift-positions
5529 up offset
'(:contents-end
:end
))
5531 (when robust-flag
(setq robust-flag nil
)))
5532 (setq up
(org-element-property :parent up
)))
5533 ;; We're at top level element containing ELEMENT: if it's
5534 ;; altered by buffer modifications, it is first element in
5535 ;; cache to be removed. Otherwise, that first element is the
5538 ;; As a special case, do not remove BEFORE if it is a robust
5539 ;; container for current changes.
5540 (if (or (< (org-element-property :end before
) beg
) robust-flag
) after
5543 (defun org-element--cache-submit-request (beg end offset
)
5544 "Submit a new cache synchronization request for current buffer.
5545 BEG and END are buffer positions delimiting the minimal area
5546 where cache data should be removed. OFFSET is the size of the
5547 change, as an integer."
5548 (let ((next (car org-element--cache-sync-requests
))
5549 delete-to delete-from
)
5551 (zerop (aref next
5))
5552 (> (setq delete-to
(+ (aref next
2) (aref next
3))) end
)
5553 (<= (setq delete-from
(aref next
1)) end
))
5554 ;; Current changes can be merged with first sync request: we
5555 ;; can save a partial cache synchronization.
5557 (incf (aref next
3) offset
)
5558 ;; If last change happened within area to be removed, extend
5559 ;; boundaries of robust parents, if any. Otherwise, find
5560 ;; first element to remove and update request accordingly.
5561 (if (> beg delete-from
)
5562 (let ((up (aref next
4)))
5564 (org-element--cache-shift-positions
5565 up offset
'(:contents-end
:end
))
5566 (setq up
(org-element-property :parent up
))))
5567 (let ((first (org-element--cache-for-removal beg delete-to offset
)))
5569 (aset next
0 (org-element--cache-key first
))
5570 (aset next
1 (org-element-property :begin first
))
5571 (aset next
4 (org-element-property :parent first
))))))
5572 ;; Ensure cache is correct up to END. Also make sure that NEXT,
5573 ;; if any, is no longer a 0-phase request, thus ensuring that
5574 ;; phases are properly ordered. We need to provide OFFSET as
5575 ;; optional parameter since current modifications are not known
5576 ;; yet to the otherwise correct part of the cache (i.e, before
5577 ;; the first request).
5578 (when next
(org-element--cache-sync (current-buffer) end beg
))
5579 (let ((first (org-element--cache-for-removal beg end offset
)))
5581 (push (let ((beg (org-element-property :begin first
))
5582 (key (org-element--cache-key first
)))
5584 ;; When changes happen before the first known
5585 ;; element, re-parent and shift the rest of the
5587 ((> beg end
) (vector key beg nil offset nil
1))
5588 ;; Otherwise, we find the first non robust
5589 ;; element containing END. All elements between
5590 ;; FIRST and this one are to be removed.
5591 ((let ((first-end (org-element-property :end first
)))
5592 (and (> first-end end
)
5593 (vector key beg first-end offset first
0))))
5595 (let* ((element (org-element--cache-find end
))
5596 (end (org-element-property :end element
))
5598 (while (and (setq up
(org-element-property :parent up
))
5599 (>= (org-element-property :begin up
) beg
))
5600 (setq end
(org-element-property :end up
)
5602 (vector key beg end offset element
0)))))
5603 org-element--cache-sync-requests
)
5604 ;; No element to remove. No need to re-parent either.
5605 ;; Simply shift additional elements, if any, by OFFSET.
5606 (when org-element--cache-sync-requests
5607 (incf (aref (car org-element--cache-sync-requests
) 3) offset
)))))))
5610 ;;;; Public Functions
5613 (defun org-element-cache-reset (&optional all
)
5614 "Reset cache in current buffer.
5615 When optional argument ALL is non-nil, reset cache in all Org
5618 (dolist (buffer (if all
(buffer-list) (list (current-buffer))))
5619 (with-current-buffer buffer
5620 (when (and org-element-use-cache
5621 (or (derived-mode-p 'org-mode
) orgstruct-mode
))
5622 (org-set-local 'org-element--cache
5623 (avl-tree-create #'org-element--cache-compare
))
5624 (org-set-local 'org-element--cache-objects
(make-hash-table :test
#'eq
))
5625 (org-set-local 'org-element--cache-sync-keys
5626 (make-hash-table :weakness
'key
:test
#'eq
))
5627 (org-set-local 'org-element--cache-change-warning nil
)
5628 (org-set-local 'org-element--cache-sync-requests nil
)
5629 (org-set-local 'org-element--cache-sync-timer nil
)
5630 (add-hook 'before-change-functions
5631 #'org-element--cache-before-change nil t
)
5632 (add-hook 'after-change-functions
5633 #'org-element--cache-after-change nil t
)))))
5636 (defun org-element-cache-refresh (pos)
5637 "Refresh cache at position POS."
5638 (when (org-element--cache-active-p)
5639 (org-element--cache-sync (current-buffer) pos
)
5640 (org-element--cache-submit-request pos pos
0)
5641 (org-element--cache-set-timer (current-buffer))))
5647 ;; The first move is to implement a way to obtain the smallest element
5648 ;; containing point. This is the job of `org-element-at-point'. It
5649 ;; basically jumps back to the beginning of section containing point
5650 ;; and proceed, one element after the other, with
5651 ;; `org-element--current-element' until the container is found. Note:
5652 ;; When using `org-element-at-point', secondary values are never
5653 ;; parsed since the function focuses on elements, not on objects.
5655 ;; At a deeper level, `org-element-context' lists all elements and
5656 ;; objects containing point.
5658 ;; `org-element-nested-p' and `org-element-swap-A-B' may be used
5659 ;; internally by navigation and manipulation tools.
5663 (defun org-element-at-point ()
5664 "Determine closest element around point.
5666 Return value is a list like (TYPE PROPS) where TYPE is the type
5667 of the element and PROPS a plist of properties associated to the
5670 Possible types are defined in `org-element-all-elements'.
5671 Properties depend on element or object type, but always include
5672 `:begin', `:end', `:parent' and `:post-blank' properties.
5674 As a special case, if point is at the very beginning of the first
5675 item in a list or sub-list, returned element will be that list
5676 instead of the item. Likewise, if point is at the beginning of
5677 the first row of a table, returned element will be the table
5678 instead of the first row.
5680 When point is at the end of the buffer, return the innermost
5681 element ending there."
5682 (org-with-wide-buffer
5683 (let ((origin (point)))
5685 (skip-chars-backward " \r\t\n")
5687 ;; Within blank lines at the beginning of buffer, return nil.
5689 ;; Within blank lines right after a headline, return that
5691 ((org-with-limited-levels (org-at-heading-p))
5693 (org-element-headline-parser (point-max) t
))
5694 ;; Otherwise parse until we find element containing ORIGIN.
5696 (when (org-element--cache-active-p)
5697 (if (not org-element--cache
) (org-element-cache-reset)
5698 (org-element--cache-sync (current-buffer) origin
)))
5699 (org-element--parse-to origin
))))))
5702 (defun org-element-context (&optional element
)
5703 "Return smallest element or object around point.
5705 Return value is a list like (TYPE PROPS) where TYPE is the type
5706 of the element or object and PROPS a plist of properties
5709 Possible types are defined in `org-element-all-elements' and
5710 `org-element-all-objects'. Properties depend on element or
5711 object type, but always include `:begin', `:end', `:parent' and
5714 As a special case, if point is right after an object and not at
5715 the beginning of any other object, return that object.
5717 Optional argument ELEMENT, when non-nil, is the closest element
5718 containing point, as returned by `org-element-at-point'.
5719 Providing it allows for quicker computation."
5720 (catch 'objects-forbidden
5721 (org-with-wide-buffer
5722 (let* ((pos (point))
5723 (element (or element
(org-element-at-point)))
5724 (type (org-element-type element
)))
5725 ;; If point is inside an element containing objects or
5726 ;; a secondary string, narrow buffer to the container and
5727 ;; proceed with parsing. Otherwise, return ELEMENT.
5729 ;; At a parsed affiliated keyword, check if we're inside main
5731 ((let ((post (org-element-property :post-affiliated element
)))
5732 (and post
(< pos post
)))
5734 (let ((case-fold-search t
)) (looking-at org-element--affiliated-re
))
5736 ((not (member-ignore-case (match-string 1)
5737 org-element-parsed-keywords
))
5738 (throw 'objects-forbidden element
))
5739 ((< (match-end 0) pos
)
5740 (narrow-to-region (match-end 0) (line-end-position)))
5741 ((and (match-beginning 2)
5742 (>= pos
(match-beginning 2))
5743 (< pos
(match-end 2)))
5744 (narrow-to-region (match-beginning 2) (match-end 2)))
5745 (t (throw 'objects-forbidden element
)))
5746 ;; Also change type to retrieve correct restrictions.
5747 (setq type
'keyword
))
5748 ;; At an item, objects can only be located within tag, if any.
5750 (let ((tag (org-element-property :tag element
)))
5751 (if (not tag
) (throw 'objects-forbidden element
)
5753 (search-forward tag
(line-end-position))
5754 (goto-char (match-beginning 0))
5755 (if (and (>= pos
(point)) (< pos
(match-end 0)))
5756 (narrow-to-region (point) (match-end 0))
5757 (throw 'objects-forbidden element
)))))
5758 ;; At an headline or inlinetask, objects are in title.
5759 ((memq type
'(headline inlinetask
))
5760 (goto-char (org-element-property :begin element
))
5761 (skip-chars-forward "*")
5762 (if (and (> pos
(point)) (< pos
(line-end-position)))
5763 (narrow-to-region (point) (line-end-position))
5764 (throw 'objects-forbidden element
)))
5765 ;; At a paragraph, a table-row or a verse block, objects are
5766 ;; located within their contents.
5767 ((memq type
'(paragraph table-row verse-block
))
5768 (let ((cbeg (org-element-property :contents-begin element
))
5769 (cend (org-element-property :contents-end element
)))
5770 ;; CBEG is nil for table rules.
5771 (if (and cbeg cend
(>= pos cbeg
)
5772 (or (< pos cend
) (and (= pos cend
) (eobp))))
5773 (narrow-to-region cbeg cend
)
5774 (throw 'objects-forbidden element
))))
5775 ;; At a planning line, if point is at a timestamp, return it,
5776 ;; otherwise, return element.
5777 ((eq type
'planning
)
5778 (dolist (p '(:closed
:deadline
:scheduled
))
5779 (let ((timestamp (org-element-property p element
)))
5780 (when (and timestamp
5781 (<= (org-element-property :begin timestamp
) pos
)
5782 (> (org-element-property :end timestamp
) pos
))
5783 (throw 'objects-forbidden timestamp
))))
5784 ;; All other locations cannot contain objects: bail out.
5785 (throw 'objects-forbidden element
))
5786 (t (throw 'objects-forbidden element
)))
5787 (goto-char (point-min))
5788 (let ((restriction (org-element-restriction type
))
5790 (cache (cond ((not (org-element--cache-active-p)) nil
)
5791 (org-element--cache-objects
5792 (gethash element org-element--cache-objects
))
5793 (t (org-element-cache-reset) nil
)))
5794 next object-data last
)
5798 ;; When entering PARENT for the first time, get list
5799 ;; of objects within known so far. Store it in
5802 (let ((data (assq parent cache
)))
5803 (if data
(setq object-data data
)
5804 (push (setq object-data
(list parent nil
)) cache
))))
5805 ;; Find NEXT object for analysis.
5807 ;; If NEXT is non-nil, we already exhausted the
5808 ;; cache so we can parse buffer to find the object
5810 (if next
(setq next
(org-element--object-lex restriction
))
5811 ;; Otherwise, check if cache can help us.
5812 (let ((objects (cddr object-data
))
5813 (completep (nth 1 object-data
)))
5815 ((and (not objects
) completep
) (throw 'exit parent
))
5817 (setq next
(org-element--object-lex restriction
)))
5820 (org-element-property :end
(car objects
))))
5821 (if (>= cache-limit pos
)
5822 ;; Cache contains the information needed.
5823 (dolist (object objects
(throw 'exit parent
))
5824 (when (<= (org-element-property :begin object
)
5826 (if (>= (org-element-property :end object
)
5828 (throw 'found
(setq next object
))
5829 (throw 'exit parent
))))
5830 (goto-char cache-limit
)
5832 (org-element--object-lex restriction
))))))))
5833 ;; If we have a new object to analyze, store it in
5834 ;; cache. Otherwise record that there is nothing
5835 ;; more to parse in this element at this depth.
5837 (progn (org-element-put-property next
:parent parent
)
5838 (push next
(cddr object-data
)))
5839 (setcar (cdr object-data
) t
)))
5840 ;; Process NEXT, if any, in order to know if we need
5841 ;; to skip it, return it or move into it.
5842 (if (or (not next
) (> (org-element-property :begin next
) pos
))
5843 (throw 'exit
(or last parent
))
5844 (let ((end (org-element-property :end next
))
5845 (cbeg (org-element-property :contents-begin next
))
5846 (cend (org-element-property :contents-end next
)))
5848 ;; Skip objects ending before point. Also skip
5849 ;; objects ending at point unless it is also the
5850 ;; end of buffer, since we want to return the
5851 ;; innermost object.
5852 ((and (<= end pos
) (/= (point-max) end
))
5854 ;; For convenience, when object ends at POS,
5855 ;; without any space, store it in LAST, as we
5856 ;; will return it if no object starts here.
5857 (when (and (= end pos
)
5858 (not (memq (char-before) '(?\s ?
\t))))
5860 ;; If POS is within a container object, move
5861 ;; into that object.
5865 ;; At contents' end, if there is no
5866 ;; space before point, also move into
5867 ;; object, for consistency with
5868 ;; convenience feature above.
5870 (or (= (point-max) pos
)
5871 (not (memq (char-before pos
)
5874 (narrow-to-region (point) cend
)
5876 restriction
(org-element-restriction next
)
5879 ;; Otherwise, return NEXT.
5880 (t (throw 'exit next
)))))))
5881 ;; Store results in cache, if applicable.
5882 (org-element--cache-put element cache
)))))))
5884 (defun org-element-lineage (blob &optional types with-self
)
5885 "List all ancestors of a given element or object.
5887 BLOB is an object or element.
5889 When optional argument TYPES is a list of symbols, return the
5890 first element or object in the lineage whose type belongs to that
5893 When optional argument WITH-SELF is non-nil, lineage includes
5894 BLOB itself as the first element, and TYPES, if provided, also
5897 When BLOB is obtained through `org-element-context' or
5898 `org-element-at-point', only ancestors from its section can be
5899 found. There is no such limitation when BLOB belongs to a full
5901 (let ((up (if with-self blob
(org-element-property :parent blob
)))
5903 (while (and up
(not (memq (org-element-type up
) types
)))
5904 (unless types
(push up ancestors
))
5905 (setq up
(org-element-property :parent up
)))
5906 (if types up
(nreverse ancestors
))))
5908 (defun org-element-nested-p (elem-A elem-B
)
5909 "Non-nil when elements ELEM-A and ELEM-B are nested."
5910 (let ((beg-A (org-element-property :begin elem-A
))
5911 (beg-B (org-element-property :begin elem-B
))
5912 (end-A (org-element-property :end elem-A
))
5913 (end-B (org-element-property :end elem-B
)))
5914 (or (and (>= beg-A beg-B
) (<= end-A end-B
))
5915 (and (>= beg-B beg-A
) (<= end-B end-A
)))))
5917 (defun org-element-swap-A-B (elem-A elem-B
)
5918 "Swap elements ELEM-A and ELEM-B.
5919 Assume ELEM-B is after ELEM-A in the buffer. Leave point at the
5921 (goto-char (org-element-property :begin elem-A
))
5922 ;; There are two special cases when an element doesn't start at bol:
5923 ;; the first paragraph in an item or in a footnote definition.
5924 (let ((specialp (not (bolp))))
5925 ;; Only a paragraph without any affiliated keyword can be moved at
5926 ;; ELEM-A position in such a situation. Note that the case of
5927 ;; a footnote definition is impossible: it cannot contain two
5928 ;; paragraphs in a row because it cannot contain a blank line.
5930 (or (not (eq (org-element-type elem-B
) 'paragraph
))
5931 (/= (org-element-property :begin elem-B
)
5932 (org-element-property :contents-begin elem-B
))))
5933 (error "Cannot swap elements"))
5934 ;; In a special situation, ELEM-A will have no indentation. We'll
5935 ;; give it ELEM-B's (which will in, in turn, have no indentation).
5936 (let* ((ind-B (when specialp
5937 (goto-char (org-element-property :begin elem-B
))
5938 (org-get-indentation)))
5939 (beg-A (org-element-property :begin elem-A
))
5940 (end-A (save-excursion
5941 (goto-char (org-element-property :end elem-A
))
5942 (skip-chars-backward " \r\t\n")
5944 (beg-B (org-element-property :begin elem-B
))
5945 (end-B (save-excursion
5946 (goto-char (org-element-property :end elem-B
))
5947 (skip-chars-backward " \r\t\n")
5949 ;; Store inner overlays responsible for visibility status.
5950 ;; We also need to store their boundaries as they will be
5951 ;; removed from buffer.
5956 (and (>= (overlay-start o
) beg-A
)
5957 (<= (overlay-end o
) end-A
)
5958 (list o
(overlay-start o
) (overlay-end o
))))
5959 (overlays-in beg-A end-A
)))
5962 (and (>= (overlay-start o
) beg-B
)
5963 (<= (overlay-end o
) end-B
)
5964 (list o
(overlay-start o
) (overlay-end o
))))
5965 (overlays-in beg-B end-B
)))))
5967 (body-A (buffer-substring beg-A end-A
))
5968 (body-B (delete-and-extract-region beg-B end-B
)))
5971 (setq body-B
(replace-regexp-in-string "\\`[ \t]*" "" body-B
))
5972 (org-indent-to-column ind-B
))
5974 ;; Restore ex ELEM-A overlays.
5975 (let ((offset (- beg-B beg-A
)))
5976 (dolist (o (car overlays
))
5977 (move-overlay (car o
) (+ (nth 1 o
) offset
) (+ (nth 2 o
) offset
)))
5979 (delete-region beg-A end-A
)
5981 ;; Restore ex ELEM-B overlays.
5982 (dolist (o (cdr overlays
))
5983 (move-overlay (car o
) (- (nth 1 o
) offset
) (- (nth 2 o
) offset
))))
5984 (goto-char (org-element-property :end elem-B
)))))
5986 (defun org-element-remove-indentation (s &optional n
)
5987 "Remove maximum common indentation in string S and return it.
5988 When optional argument N is a positive integer, remove exactly
5989 that much characters from indentation, if possible, or return
5990 S as-is otherwise. Unlike to `org-remove-indentation', this
5991 function doesn't call `untabify' on S."
5995 (goto-char (point-min))
5996 ;; Find maximum common indentation, if not specified.
5998 (let ((min-ind (point-max)))
6000 (while (re-search-forward "^[ \t]*\\S-" nil t
)
6001 (let ((ind (1- (current-column))))
6002 (if (zerop ind
) (throw 'exit s
)
6003 (setq min-ind
(min min-ind ind
))))))
6006 ;; Remove exactly N indentation, but give up if not possible.
6008 (let ((ind (progn (skip-chars-forward " \t") (current-column))))
6009 (cond ((eolp) (delete-region (line-beginning-position) (point)))
6010 ((< ind n
) (throw 'exit s
))
6011 (t (org-indent-line-to (- ind n
))))
6017 (provide 'org-element
)
6020 ;; generated-autoload-file: "org-loaddefs.el"
6023 ;;; org-element.el ends here