1 ;;; org-element.el --- Parser for Org Syntax -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2012-2016 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
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 "\\[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 (pcase org-plain-list-ordered-item-terminator
181 (?\
) ")") (?.
"\\.") (_ "[.)]")))
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).
205 "[0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}" "\\|"
206 "[0-9]*\\(?:%\\|/[0-9]*\\)\\]"
208 ;; Objects starting with "@": export snippets.
210 ;; Objects starting with "{": macro.
212 ;; Objects starting with "<" : timestamp
213 ;; (active, diary), target, radio target and
215 (concat "<\\(?:%%\\|<\\|[0-9]\\|" link-types
"\\)")
216 ;; Objects starting with "$": latex fragment.
218 ;; Objects starting with "\": line break,
219 ;; entity, latex fragment.
220 "\\\\\\(?:[a-zA-Z[(]\\|\\\\[ \t]*$\\|_ +\\)"
221 ;; Objects starting with raw text: inline Babel
222 ;; source block, inline Babel call.
223 "\\(?:call\\|src\\)_"))
226 (org-element--set-regexps)
229 (defun org-element-update-syntax ()
230 "Update parser internals."
232 (org-element--set-regexps)
233 (org-element-cache-reset 'all
))
235 (defconst org-element-all-elements
236 '(babel-call center-block clock comment comment-block diary-sexp drawer
237 dynamic-block example-block export-block fixed-width
238 footnote-definition headline horizontal-rule inlinetask item
239 keyword latex-environment node-property paragraph plain-list
240 planning property-drawer quote-block section
241 special-block src-block table table-row verse-block
)
242 "Complete list of element types.")
244 (defconst org-element-greater-elements
245 '(center-block drawer dynamic-block footnote-definition headline inlinetask
246 item plain-list property-drawer quote-block section
248 "List of recursive element types aka Greater Elements.")
250 (defconst org-element-all-objects
251 '(bold code entity export-snippet footnote-reference inline-babel-call
252 inline-src-block italic line-break latex-fragment link macro
253 radio-target statistics-cookie strike-through subscript superscript
254 table-cell target timestamp underline verbatim
)
255 "Complete list of object types.")
257 (defconst org-element-recursive-objects
258 '(bold footnote-reference italic link subscript radio-target strike-through
259 superscript table-cell underline
)
260 "List of recursive object types.")
262 (defconst org-element-object-containers
263 (append org-element-recursive-objects
'(paragraph table-row verse-block
))
264 "List of object or element types that can directly contain objects.")
266 (defconst org-element-affiliated-keywords
267 '("CAPTION" "DATA" "HEADER" "HEADERS" "LABEL" "NAME" "PLOT" "RESNAME" "RESULT"
268 "RESULTS" "SOURCE" "SRCNAME" "TBLNAME")
269 "List of affiliated keywords as strings.
270 By default, all keywords setting attributes (e.g., \"ATTR_LATEX\")
271 are affiliated keywords and need not to be in this list.")
273 (defconst org-element-keyword-translation-alist
274 '(("DATA" .
"NAME") ("LABEL" .
"NAME") ("RESNAME" .
"NAME")
275 ("SOURCE" .
"NAME") ("SRCNAME" .
"NAME") ("TBLNAME" .
"NAME")
276 ("RESULT" .
"RESULTS") ("HEADERS" .
"HEADER"))
277 "Alist of usual translations for keywords.
278 The key is the old name and the value the new one. The property
279 holding their value will be named after the translated name.")
281 (defconst org-element-multiple-keywords
'("CAPTION" "HEADER")
282 "List of affiliated keywords that can occur more than once in an element.
284 Their value will be consed into a list of strings, which will be
285 returned as the value of the property.
287 This list is checked after translations have been applied. See
288 `org-element-keyword-translation-alist'.
290 By default, all keywords setting attributes (e.g., \"ATTR_LATEX\")
291 allow multiple occurrences and need not to be in this list.")
293 (defconst org-element-parsed-keywords
'("CAPTION")
294 "List of affiliated keywords whose value can be parsed.
296 Their value will be stored as a secondary string: a list of
299 This list is checked after translations have been applied. See
300 `org-element-keyword-translation-alist'.")
302 (defconst org-element--parsed-properties-alist
303 (mapcar (lambda (k) (cons k
(intern (concat ":" (downcase k
)))))
304 org-element-parsed-keywords
)
305 "Alist of parsed keywords and associated properties.
306 This is generated from `org-element-parsed-keywords', which
309 (defconst org-element-dual-keywords
'("CAPTION" "RESULTS")
310 "List of affiliated keywords which can have a secondary value.
312 In Org syntax, they can be written with optional square brackets
313 before the colons. For example, RESULTS keyword can be
314 associated to a hash value with the following:
316 #+RESULTS[hash-string]: some-source
318 This list is checked after translations have been applied. See
319 `org-element-keyword-translation-alist'.")
321 (defconst org-element--affiliated-re
322 (format "[ \t]*#\\+\\(?:%s\\):[ \t]*"
324 ;; Dual affiliated keywords.
325 (format "\\(?1:%s\\)\\(?:\\[\\(.*\\)\\]\\)?"
326 (regexp-opt org-element-dual-keywords
))
328 ;; Regular affiliated keywords.
329 (format "\\(?1:%s\\)"
332 (lambda (k) (member k org-element-dual-keywords
))
333 org-element-affiliated-keywords
)))
335 ;; Export attributes.
336 "\\(?1:ATTR_[-_A-Za-z0-9]+\\)"))
337 "Regexp matching any affiliated keyword.
339 Keyword name is put in match group 1. Moreover, if keyword
340 belongs to `org-element-dual-keywords', put the dual value in
343 Don't modify it, set `org-element-affiliated-keywords' instead.")
345 (defconst org-element-object-restrictions
346 (let* ((standard-set (remq 'table-cell org-element-all-objects
))
347 (standard-set-no-line-break (remq 'line-break standard-set
)))
348 `((bold ,@standard-set
)
349 (footnote-reference ,@standard-set
)
350 (headline ,@standard-set-no-line-break
)
351 (inlinetask ,@standard-set-no-line-break
)
352 (italic ,@standard-set
)
353 (item ,@standard-set-no-line-break
)
354 (keyword ,@(remq 'footnote-reference standard-set
))
355 ;; Ignore all links excepted plain links in a link description.
356 ;; Also ignore radio-targets and line breaks.
357 (link bold code entity export-snippet inline-babel-call inline-src-block
358 italic latex-fragment macro plain-link statistics-cookie
359 strike-through subscript superscript underline verbatim
)
360 (paragraph ,@standard-set
)
361 ;; Remove any variable object from radio target as it would
362 ;; prevent it from being properly recognized.
363 (radio-target bold code entity italic latex-fragment strike-through
364 subscript superscript underline superscript
)
365 (strike-through ,@standard-set
)
366 (subscript ,@standard-set
)
367 (superscript ,@standard-set
)
368 ;; Ignore inline babel call and inline src block as formulas are
369 ;; possible. Also ignore line breaks and statistics cookies.
370 (table-cell bold code entity export-snippet footnote-reference italic
371 latex-fragment link macro radio-target strike-through
372 subscript superscript target timestamp underline verbatim
)
373 (table-row table-cell
)
374 (underline ,@standard-set
)
375 (verse-block ,@standard-set
)))
376 "Alist of objects restrictions.
378 key is an element or object type containing objects and value is
379 a list of types that can be contained within an element or object
382 For example, in a `radio-target' object, one can only find
383 entities, latex-fragments, subscript, superscript and text
386 This alist also applies to secondary string. For example, an
387 `headline' type element doesn't directly contain objects, but
388 still has an entry since one of its properties (`:title') does.")
390 (defconst org-element-secondary-value-alist
394 "Alist between element types and locations of secondary values.")
396 (defconst org-element--pair-square-table
397 (let ((table (make-syntax-table)))
398 (modify-syntax-entry ?\
[ "(]" table
)
399 (modify-syntax-entry ?\
] ")[" table
)
400 (dolist (char '(?\
{ ?\
} ?\
( ?\
) ?\
< ?\
>) table
)
401 (modify-syntax-entry char
" " table
)))
402 "Table used internally to pair only square brackets.
403 Other brackets are treated as spaces.")
407 ;;; Accessors and Setters
409 ;; Provide four accessors: `org-element-type', `org-element-property'
410 ;; `org-element-contents' and `org-element-restriction'.
412 ;; Setter functions allow to modify elements by side effect. There is
413 ;; `org-element-put-property', `org-element-set-contents'. These
414 ;; low-level functions are useful to build a parse tree.
416 ;; `org-element-adopt-element', `org-element-set-element',
417 ;; `org-element-extract-element' and `org-element-insert-before' are
418 ;; high-level functions useful to modify a parse tree.
420 ;; `org-element-secondary-p' is a predicate used to know if a given
421 ;; object belongs to a secondary string. `org-element-copy' returns
422 ;; an element or object, stripping its parent property in the process.
424 (defsubst org-element-type
(element)
425 "Return type of ELEMENT.
427 The function returns the type of the element or object provided.
428 It can also return the following special value:
429 `plain-text' for a string
430 `org-data' for a complete document
431 nil in any other case."
433 ((not (consp element
)) (and (stringp element
) 'plain-text
))
434 ((symbolp (car element
)) (car element
))))
436 (defsubst org-element-property
(property element
)
437 "Extract the value from the PROPERTY of an ELEMENT."
438 (if (stringp element
) (get-text-property 0 property element
)
439 (plist-get (nth 1 element
) property
)))
441 (defsubst org-element-contents
(element)
442 "Extract contents from an ELEMENT."
443 (cond ((not (consp element
)) nil
)
444 ((symbolp (car element
)) (nthcdr 2 element
))
447 (defsubst org-element-restriction
(element)
448 "Return restriction associated to ELEMENT.
449 ELEMENT can be an element, an object or a symbol representing an
450 element or object type."
451 (cdr (assq (if (symbolp element
) element
(org-element-type element
))
452 org-element-object-restrictions
)))
454 (defsubst org-element-put-property
(element property value
)
455 "In ELEMENT set PROPERTY to VALUE.
456 Return modified element."
457 (if (stringp element
) (org-add-props element nil property value
)
458 (setcar (cdr element
) (plist-put (nth 1 element
) property value
))
461 (defsubst org-element-set-contents
(element &rest contents
)
462 "Set ELEMENT contents to CONTENTS."
463 (cond ((not element
) (list contents
))
464 ((not (symbolp (car element
))) contents
)
465 ((cdr element
) (setcdr (cdr element
) contents
))
466 (t (nconc element contents
))))
468 (defun org-element-secondary-p (object)
469 "Non-nil when OBJECT directly belongs to a secondary string.
470 Return value is the property name, as a keyword, or nil."
471 (let* ((parent (org-element-property :parent object
))
472 (properties (cdr (assq (org-element-type parent
)
473 org-element-secondary-value-alist
))))
475 (dolist (p properties
)
476 (and (memq object
(org-element-property p parent
))
479 (defsubst org-element-adopt-elements
(parent &rest children
)
480 "Append elements to the contents of another element.
482 PARENT is an element or object. CHILDREN can be elements,
483 objects, or a strings.
485 The function takes care of setting `:parent' property for CHILD.
486 Return parent element."
487 (if (not children
) parent
488 ;; Link every child to PARENT. If PARENT is nil, it is a secondary
489 ;; string: parent is the list itself.
490 (dolist (child children
)
491 (org-element-put-property child
:parent
(or parent children
)))
492 ;; Add CHILDREN at the end of PARENT contents.
494 (apply #'org-element-set-contents
496 (nconc (org-element-contents parent
) children
)))
497 ;; Return modified PARENT element.
498 (or parent children
)))
500 (defun org-element-extract-element (element)
501 "Extract ELEMENT from parse tree.
502 Remove element from the parse tree by side-effect, and return it
503 with its `:parent' property stripped out."
504 (let ((parent (org-element-property :parent element
))
505 (secondary (org-element-secondary-p element
)))
507 (org-element-put-property
509 (delq element
(org-element-property secondary parent
)))
510 (apply #'org-element-set-contents
512 (delq element
(org-element-contents parent
))))
513 ;; Return ELEMENT with its :parent removed.
514 (org-element-put-property element
:parent nil
)))
516 (defun org-element-insert-before (element location
)
517 "Insert ELEMENT before LOCATION in parse tree.
518 LOCATION is an element, object or string within the parse tree.
519 Parse tree is modified by side effect."
520 (let* ((parent (org-element-property :parent location
))
521 (property (org-element-secondary-p location
))
522 (siblings (if property
(org-element-property property parent
)
523 (org-element-contents parent
)))
524 ;; Special case: LOCATION is the first element of an
525 ;; independent secondary string (e.g. :title property). Add
527 (specialp (and (not property
)
529 (eq (car parent
) location
))))
530 ;; Install ELEMENT at the appropriate POSITION within SIBLINGS.
532 ((or (null siblings
) (eq (car siblings
) location
))
533 (push element siblings
))
534 ((null location
) (nconc siblings
(list element
)))
535 (t (let ((previous (cadr (memq location
(reverse siblings
)))))
537 (error "No location found to insert element")
538 (let ((next (memq previous siblings
)))
539 (setcdr next
(cons element
(cdr next
))))))))
540 ;; Store SIBLINGS at appropriate place in parse tree.
542 (specialp (setcdr parent
(copy-sequence parent
)) (setcar parent element
))
543 (property (org-element-put-property parent property siblings
))
544 (t (apply #'org-element-set-contents parent siblings
)))
545 ;; Set appropriate :parent property.
546 (org-element-put-property element
:parent parent
)))
548 (defun org-element-set-element (old new
)
549 "Replace element or object OLD with element or object NEW.
550 The function takes care of setting `:parent' property for NEW."
551 ;; Ensure OLD and NEW have the same parent.
552 (org-element-put-property new
:parent
(org-element-property :parent old
))
553 (if (or (memq (org-element-type old
) '(plain-text nil
))
554 (memq (org-element-type new
) '(plain-text nil
)))
555 ;; We cannot replace OLD with NEW since one of them is not an
556 ;; object or element. We take the long path.
557 (progn (org-element-insert-before new old
)
558 (org-element-extract-element old
))
559 ;; Since OLD is going to be changed into NEW by side-effect, first
560 ;; make sure that every element or object within NEW has OLD as
562 (dolist (blob (org-element-contents new
))
563 (org-element-put-property blob
:parent old
))
564 ;; Transfer contents.
565 (apply #'org-element-set-contents old
(org-element-contents new
))
566 ;; Overwrite OLD's properties with NEW's.
567 (setcar (cdr old
) (nth 1 new
))
569 (setcar old
(car new
))))
571 (defun org-element-create (type &optional props
&rest children
)
572 "Create a new element of type TYPE.
573 Optional argument PROPS, when non-nil, is a plist defining the
574 properties of the element. CHILDREN can be elements, objects or
576 (apply #'org-element-adopt-elements
(list type props
) children
))
578 (defun org-element-copy (datum)
579 "Return a copy of DATUM.
580 DATUM is an element, object, string or nil. `:parent' property
581 is cleared and contents are removed in the process."
583 (let ((type (org-element-type datum
)))
585 (`org-data
(list 'org-data nil
))
586 (`plain-text
(substring-no-properties datum
))
587 (`nil
(copy-sequence datum
))
589 (list type
(plist-put (copy-sequence (nth 1 datum
)) :parent nil
)))))))
595 ;; For each greater element type, we define a parser and an
598 ;; A parser returns the element or object as the list described above.
599 ;; Most of them accepts no argument. Though, exceptions exist. Hence
600 ;; every element containing a secondary string (see
601 ;; `org-element-secondary-value-alist') will accept an optional
602 ;; argument to toggle parsing of these secondary strings. Moreover,
603 ;; `item' parser requires current list's structure as its first
606 ;; An interpreter accepts two arguments: the list representation of
607 ;; the element or object, and its contents. The latter may be nil,
608 ;; depending on the element or object considered. It returns the
609 ;; appropriate Org syntax, as a string.
611 ;; Parsing functions must follow the naming convention:
612 ;; org-element-TYPE-parser, where TYPE is greater element's type, as
613 ;; defined in `org-element-greater-elements'.
615 ;; Similarly, interpreting functions must follow the naming
616 ;; convention: org-element-TYPE-interpreter.
618 ;; With the exception of `headline' and `item' types, greater elements
619 ;; cannot contain other greater elements of their own type.
621 ;; Beside implementing a parser and an interpreter, adding a new
622 ;; greater element requires to tweak `org-element--current-element'.
623 ;; Moreover, the newly defined type must be added to both
624 ;; `org-element-all-elements' and `org-element-greater-elements'.
629 (defun org-element-center-block-parser (limit affiliated
)
630 "Parse a center block.
632 LIMIT bounds the search. AFFILIATED is a list of which CAR is
633 the buffer position at the beginning of the first affiliated
634 keyword and CDR is a plist of affiliated keywords along with
637 Return a list whose CAR is `center-block' and CDR is a plist
638 containing `:begin', `:end', `:contents-begin', `:contents-end',
639 `:post-blank' and `:post-affiliated' keywords.
641 Assume point is at the beginning of the block."
642 (let ((case-fold-search t
))
643 (if (not (save-excursion
644 (re-search-forward "^[ \t]*#\\+END_CENTER[ \t]*$" limit t
)))
645 ;; Incomplete block: parse it as a paragraph.
646 (org-element-paragraph-parser limit affiliated
)
647 (let ((block-end-line (match-beginning 0)))
648 (let* ((begin (car affiliated
))
649 (post-affiliated (point))
650 ;; Empty blocks have no contents.
651 (contents-begin (progn (forward-line)
652 (and (< (point) block-end-line
)
654 (contents-end (and contents-begin block-end-line
))
655 (pos-before-blank (progn (goto-char block-end-line
)
659 (skip-chars-forward " \r\t\n" limit
)
660 (if (eobp) (point) (line-beginning-position)))))
665 :contents-begin contents-begin
666 :contents-end contents-end
667 :post-blank
(count-lines pos-before-blank end
)
668 :post-affiliated post-affiliated
)
669 (cdr affiliated
))))))))
671 (defun org-element-center-block-interpreter (_ contents
)
672 "Interpret a center-block element as Org syntax.
673 CONTENTS is the contents of the element."
674 (format "#+BEGIN_CENTER\n%s#+END_CENTER" contents
))
679 (defun org-element-drawer-parser (limit affiliated
)
682 LIMIT bounds the search. AFFILIATED is a list of which CAR is
683 the buffer position at the beginning of the first affiliated
684 keyword and CDR is a plist of affiliated keywords along with
687 Return a list whose CAR is `drawer' and CDR is a plist containing
688 `:drawer-name', `:begin', `:end', `:contents-begin',
689 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
691 Assume point is at beginning of drawer."
692 (let ((case-fold-search t
))
693 (if (not (save-excursion (re-search-forward "^[ \t]*:END:[ \t]*$" limit t
)))
694 ;; Incomplete drawer: parse it as a paragraph.
695 (org-element-paragraph-parser limit affiliated
)
697 (let* ((drawer-end-line (match-beginning 0))
698 (name (progn (looking-at org-drawer-regexp
)
699 (org-match-string-no-properties 1)))
700 (begin (car affiliated
))
701 (post-affiliated (point))
702 ;; Empty drawers have no contents.
703 (contents-begin (progn (forward-line)
704 (and (< (point) drawer-end-line
)
706 (contents-end (and contents-begin drawer-end-line
))
707 (pos-before-blank (progn (goto-char drawer-end-line
)
710 (end (progn (skip-chars-forward " \r\t\n" limit
)
711 (if (eobp) (point) (line-beginning-position)))))
717 :contents-begin contents-begin
718 :contents-end contents-end
719 :post-blank
(count-lines pos-before-blank end
)
720 :post-affiliated post-affiliated
)
721 (cdr affiliated
))))))))
723 (defun org-element-drawer-interpreter (drawer contents
)
724 "Interpret DRAWER element as Org syntax.
725 CONTENTS is the contents of the element."
726 (format ":%s:\n%s:END:"
727 (org-element-property :drawer-name drawer
)
733 (defun org-element-dynamic-block-parser (limit affiliated
)
734 "Parse a dynamic block.
736 LIMIT bounds the search. AFFILIATED is a list of which CAR is
737 the buffer position at the beginning of the first affiliated
738 keyword and CDR is a plist of affiliated keywords along with
741 Return a list whose CAR is `dynamic-block' and CDR is a plist
742 containing `:block-name', `:begin', `:end', `:contents-begin',
743 `:contents-end', `:arguments', `:post-blank' and
744 `:post-affiliated' keywords.
746 Assume point is at beginning of dynamic block."
747 (let ((case-fold-search t
))
748 (if (not (save-excursion
749 (re-search-forward "^[ \t]*#\\+END:?[ \t]*$" limit t
)))
750 ;; Incomplete block: parse it as a paragraph.
751 (org-element-paragraph-parser limit affiliated
)
752 (let ((block-end-line (match-beginning 0)))
754 (let* ((name (progn (looking-at org-dblock-start-re
)
755 (org-match-string-no-properties 1)))
756 (arguments (org-match-string-no-properties 3))
757 (begin (car affiliated
))
758 (post-affiliated (point))
759 ;; Empty blocks have no contents.
760 (contents-begin (progn (forward-line)
761 (and (< (point) block-end-line
)
763 (contents-end (and contents-begin block-end-line
))
764 (pos-before-blank (progn (goto-char block-end-line
)
767 (end (progn (skip-chars-forward " \r\t\n" limit
)
768 (if (eobp) (point) (line-beginning-position)))))
775 :contents-begin contents-begin
776 :contents-end contents-end
777 :post-blank
(count-lines pos-before-blank end
)
778 :post-affiliated post-affiliated
)
779 (cdr affiliated
)))))))))
781 (defun org-element-dynamic-block-interpreter (dynamic-block contents
)
782 "Interpret DYNAMIC-BLOCK element as Org syntax.
783 CONTENTS is the contents of the element."
784 (format "#+BEGIN: %s%s\n%s#+END:"
785 (org-element-property :block-name dynamic-block
)
786 (let ((args (org-element-property :arguments dynamic-block
)))
787 (and args
(concat " " args
)))
791 ;;;; Footnote Definition
793 (defun org-element-footnote-definition-parser (limit affiliated
)
794 "Parse a footnote definition.
796 LIMIT bounds the search. AFFILIATED is a list of which CAR is
797 the buffer position at the beginning of the first affiliated
798 keyword and CDR is a plist of affiliated keywords along with
801 Return a list whose CAR is `footnote-definition' and CDR is
802 a plist containing `:label', `:begin' `:end', `:contents-begin',
803 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
805 Assume point is at the beginning of the footnote definition."
807 (let* ((label (progn (looking-at org-footnote-definition-re
)
808 (org-match-string-no-properties 1)))
809 (begin (car affiliated
))
810 (post-affiliated (point))
811 (ending (save-excursion
815 (concat org-outline-regexp-bol
"\\|"
816 org-footnote-definition-re
"\\|"
817 "^\\([ \t]*\n\\)\\{2,\\}") limit
'move
))
820 (contents-begin (progn
822 (skip-chars-forward " \r\t\n" ending
)
823 (cond ((= (point) ending
) nil
)
824 ((= (line-beginning-position) begin
) (point))
825 (t (line-beginning-position)))))
826 (contents-end (and contents-begin ending
))
827 (end (progn (goto-char ending
)
828 (skip-chars-forward " \r\t\n" limit
)
829 (if (eobp) (point) (line-beginning-position)))))
830 (list 'footnote-definition
835 :contents-begin contents-begin
836 :contents-end contents-end
837 :post-blank
(count-lines ending end
)
838 :post-affiliated post-affiliated
)
839 (cdr affiliated
))))))
841 (defun org-element-footnote-definition-interpreter (footnote-definition contents
)
842 "Interpret FOOTNOTE-DEFINITION element as Org syntax.
843 CONTENTS is the contents of the footnote-definition."
844 (concat (format "[fn:%s]" (org-element-property :label footnote-definition
))
851 (defun org-element--get-node-properties ()
852 "Return node properties associated to headline at point.
853 Upcase property names. It avoids confusion between properties
854 obtained through property drawer and default properties from the
855 parser (e.g. `:end' and :END:). Return value is a plist."
858 (when (org-looking-at-p org-planning-line-re
) (forward-line))
859 (when (looking-at org-property-drawer-re
)
861 (let ((end (match-end 0)) properties
)
862 (while (< (line-end-position) end
)
863 (looking-at org-property-re
)
864 (push (org-match-string-no-properties 3) properties
)
865 (push (intern (concat ":" (upcase (match-string 2)))) properties
)
869 (defun org-element--get-time-properties ()
870 "Return time properties associated to headline at point.
871 Return value is a plist."
873 (when (progn (forward-line) (looking-at org-planning-line-re
))
874 (let ((end (line-end-position)) plist
)
875 (while (re-search-forward org-keyword-time-not-clock-regexp end t
)
876 (goto-char (match-end 1))
877 (skip-chars-forward " \t")
878 (let ((keyword (match-string 1))
879 (time (org-element-timestamp-parser)))
880 (cond ((equal keyword org-scheduled-string
)
881 (setq plist
(plist-put plist
:scheduled time
)))
882 ((equal keyword org-deadline-string
)
883 (setq plist
(plist-put plist
:deadline time
)))
884 (t (setq plist
(plist-put plist
:closed time
))))))
887 (defun org-element-headline-parser (limit &optional raw-secondary-p
)
890 Return a list whose CAR is `headline' and CDR is a plist
891 containing `:raw-value', `:title', `:begin', `:end',
892 `:pre-blank', `:contents-begin' and `:contents-end', `:level',
893 `:priority', `:tags', `:todo-keyword',`:todo-type', `:scheduled',
894 `:deadline', `:closed', `:archivedp', `:commentedp'
895 `:footnote-section-p', `:post-blank' and `:post-affiliated'
898 The plist also contains any property set in the property drawer,
899 with its name in upper cases and colons added at the
900 beginning (e.g., `:CUSTOM_ID').
902 LIMIT is a buffer position bounding the search.
904 When RAW-SECONDARY-P is non-nil, headline's title will not be
905 parsed as a secondary string, but as a plain string instead.
907 Assume point is at beginning of the headline."
909 (let* ((begin (point))
910 (level (prog1 (org-reduced-level (skip-chars-forward "*"))
911 (skip-chars-forward " \t")))
912 (todo (and org-todo-regexp
913 (let (case-fold-search) (looking-at org-todo-regexp
))
914 (progn (goto-char (match-end 0))
915 (skip-chars-forward " \t")
918 (and todo
(if (member todo org-done-keywords
) 'done
'todo
)))
919 (priority (and (looking-at "\\[#.\\][ \t]*")
920 (progn (goto-char (match-end 0))
921 (aref (match-string 0) 2))))
923 (and (let (case-fold-search) (looking-at org-comment-string
))
924 (goto-char (match-end 0))))
925 (title-start (point))
926 (tags (when (re-search-forward
927 (org-re "[ \t]+\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
930 (goto-char (match-beginning 0))
931 (org-split-string (match-string 1) ":")))
934 (buffer-substring-no-properties title-start title-end
)))
935 (archivedp (member org-archive-tag tags
))
936 (footnote-section-p (and org-footnote-section
937 (string= org-footnote-section raw-value
)))
938 (standard-props (org-element--get-node-properties))
939 (time-props (org-element--get-time-properties))
940 (end (min (save-excursion (org-end-of-subtree t t
)) limit
))
941 (pos-after-head (progn (forward-line) (point)))
942 (contents-begin (save-excursion
943 (skip-chars-forward " \r\t\n" end
)
944 (and (/= (point) end
) (line-beginning-position))))
945 (contents-end (and contents-begin
946 (progn (goto-char end
)
947 (skip-chars-backward " \r\t\n")
953 (list :raw-value raw-value
957 (if (not contents-begin
) 0
958 (count-lines pos-after-head contents-begin
))
959 :contents-begin contents-begin
960 :contents-end contents-end
966 :post-blank
(count-lines
967 (or contents-end pos-after-head
)
969 :footnote-section-p footnote-section-p
971 :commentedp commentedp
972 :post-affiliated begin
)
975 (org-element-put-property
977 (if raw-secondary-p raw-value
978 (let ((title (org-element--parse-objects
979 (progn (goto-char title-start
)
980 (skip-chars-forward " \t")
982 (progn (goto-char title-end
)
983 (skip-chars-backward " \t")
986 (org-element-restriction 'headline
))))
987 (dolist (datum title title
)
988 (org-element-put-property datum
:parent headline
)))))))))
990 (defun org-element-headline-interpreter (headline contents
)
991 "Interpret HEADLINE element as Org syntax.
992 CONTENTS is the contents of the element."
993 (let* ((level (org-element-property :level headline
))
994 (todo (org-element-property :todo-keyword headline
))
995 (priority (org-element-property :priority headline
))
996 (title (org-element-interpret-data
997 (org-element-property :title headline
)))
998 (tags (let ((tag-list (org-element-property :tags headline
)))
1000 (format ":%s:" (mapconcat #'identity tag-list
":")))))
1001 (commentedp (org-element-property :commentedp headline
))
1002 (pre-blank (or (org-element-property :pre-blank headline
) 0))
1004 (concat (make-string (if org-odd-levels-only
(1- (* level
2)) level
)
1006 (and todo
(concat " " todo
))
1007 (and commentedp
(concat " " org-comment-string
))
1008 (and priority
(format " [#%c]" priority
))
1010 (if (and org-footnote-section
1011 (org-element-property :footnote-section-p headline
))
1012 org-footnote-section
1019 ((zerop org-tags-column
) (format " %s" tags
))
1020 ((< org-tags-column
0)
1023 (max (- (+ org-tags-column
(length heading
) (length tags
))) 1)
1028 (make-string (max (- org-tags-column
(length heading
)) 1) ?\s
)
1030 (make-string (1+ pre-blank
) ?
\n)
1036 (defun org-element-inlinetask-parser (limit &optional raw-secondary-p
)
1037 "Parse an inline task.
1039 Return a list whose CAR is `inlinetask' and CDR is a plist
1040 containing `:title', `:begin', `:end', `:contents-begin' and
1041 `:contents-end', `:level', `:priority', `:raw-value', `:tags',
1042 `:todo-keyword', `:todo-type', `:scheduled', `:deadline',
1043 `:closed', `:post-blank' and `:post-affiliated' keywords.
1045 The plist also contains any property set in the property drawer,
1046 with its name in upper cases and colons added at the
1047 beginning (e.g., `:CUSTOM_ID').
1049 When optional argument RAW-SECONDARY-P is non-nil, inline-task's
1050 title will not be parsed as a secondary string, but as a plain
1053 Assume point is at beginning of the inline task."
1055 (let* ((begin (point))
1056 (level (prog1 (org-reduced-level (skip-chars-forward "*"))
1057 (skip-chars-forward " \t")))
1058 (todo (and org-todo-regexp
1059 (let (case-fold-search) (looking-at org-todo-regexp
))
1060 (progn (goto-char (match-end 0))
1061 (skip-chars-forward " \t")
1063 (todo-type (and todo
1064 (if (member todo org-done-keywords
) 'done
'todo
)))
1065 (priority (and (looking-at "\\[#.\\][ \t]*")
1066 (progn (goto-char (match-end 0))
1067 (aref (match-string 0) 2))))
1068 (title-start (point))
1069 (tags (when (re-search-forward
1070 (org-re "[ \t]+\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
1073 (goto-char (match-beginning 0))
1074 (org-split-string (match-string 1) ":")))
1076 (raw-value (org-trim
1077 (buffer-substring-no-properties title-start title-end
)))
1078 (task-end (save-excursion
1080 (and (re-search-forward org-outline-regexp-bol limit t
)
1081 (org-looking-at-p "END[ \t]*$")
1082 (line-beginning-position))))
1083 (standard-props (and task-end
(org-element--get-node-properties)))
1084 (time-props (and task-end
(org-element--get-time-properties)))
1085 (contents-begin (progn (forward-line)
1086 (and task-end
(< (point) task-end
) (point))))
1087 (contents-end (and contents-begin task-end
))
1088 (before-blank (if (not task-end
) (point)
1089 (goto-char task-end
)
1092 (end (progn (skip-chars-forward " \r\t\n" limit
)
1093 (if (eobp) (point) (line-beginning-position))))
1097 (list :raw-value raw-value
1100 :contents-begin contents-begin
1101 :contents-end contents-end
1106 :todo-type todo-type
1107 :post-blank
(count-lines before-blank end
)
1108 :post-affiliated begin
)
1111 (org-element-put-property
1113 (if raw-secondary-p raw-value
1114 (let ((title (org-element--parse-objects
1115 (progn (goto-char title-start
)
1116 (skip-chars-forward " \t")
1118 (progn (goto-char title-end
)
1119 (skip-chars-backward " \t")
1122 (org-element-restriction 'inlinetask
))))
1123 (dolist (datum title title
)
1124 (org-element-put-property datum
:parent inlinetask
))))))))
1126 (defun org-element-inlinetask-interpreter (inlinetask contents
)
1127 "Interpret INLINETASK element as Org syntax.
1128 CONTENTS is the contents of inlinetask."
1129 (let* ((level (org-element-property :level inlinetask
))
1130 (todo (org-element-property :todo-keyword inlinetask
))
1131 (priority (org-element-property :priority inlinetask
))
1132 (title (org-element-interpret-data
1133 (org-element-property :title inlinetask
)))
1134 (tags (let ((tag-list (org-element-property :tags inlinetask
)))
1136 (format ":%s:" (mapconcat 'identity tag-list
":")))))
1137 (task (concat (make-string level ?
*)
1138 (and todo
(concat " " todo
))
1139 (and priority
(format " [#%c]" priority
))
1140 (and title
(concat " " title
)))))
1145 ((zerop org-tags-column
) (format " %s" tags
))
1146 ((< org-tags-column
0)
1149 (max (- (+ org-tags-column
(length task
) (length tags
))) 1)
1154 (make-string (max (- org-tags-column
(length task
)) 1) ?
)
1156 ;; Prefer degenerate inlinetasks when there are no
1161 (make-string level ?
*) " END")))))
1166 (defun org-element-item-parser (_ struct
&optional raw-secondary-p
)
1169 STRUCT is the structure of the plain list.
1171 Return a list whose CAR is `item' and CDR is a plist containing
1172 `:bullet', `:begin', `:end', `:contents-begin', `:contents-end',
1173 `:checkbox', `:counter', `:tag', `:structure', `:post-blank' and
1174 `:post-affiliated' keywords.
1176 When optional argument RAW-SECONDARY-P is non-nil, item's tag, if
1177 any, will not be parsed as a secondary string, but as a plain
1180 Assume point is at the beginning of the item."
1183 (looking-at org-list-full-item-re
)
1184 (let* ((begin (point))
1185 (bullet (org-match-string-no-properties 1))
1186 (checkbox (let ((box (match-string 3)))
1187 (cond ((equal "[ ]" box
) 'off
)
1188 ((equal "[X]" box
) 'on
)
1189 ((equal "[-]" box
) 'trans
))))
1190 (counter (let ((c (match-string 2)))
1194 ((string-match "[A-Za-z]" c
)
1195 (- (string-to-char (upcase (match-string 0 c
)))
1197 ((string-match "[0-9]+" c
)
1198 (string-to-number (match-string 0 c
)))))))
1199 (end (progn (goto-char (nth 6 (assq (point) struct
)))
1200 (if (bolp) (point) (line-beginning-position 2))))
1203 ;; Ignore tags in un-ordered lists: they are just
1204 ;; a part of item's body.
1205 (if (and (match-beginning 4)
1206 (save-match-data (string-match "[.)]" bullet
)))
1209 (skip-chars-forward " \r\t\n" end
)
1210 (cond ((= (point) end
) nil
)
1211 ;; If first line isn't empty, contents really
1212 ;; start at the text after item's meta-data.
1213 ((= (line-beginning-position) begin
) (point))
1214 (t (line-beginning-position)))))
1215 (contents-end (and contents-begin
1216 (progn (goto-char end
)
1217 (skip-chars-backward " \r\t\n")
1218 (line-beginning-position 2))))
1221 (list :bullet bullet
1224 :contents-begin contents-begin
1225 :contents-end contents-end
1229 :post-blank
(count-lines (or contents-end begin
) end
)
1230 :post-affiliated begin
))))
1231 (org-element-put-property
1233 (let ((raw (org-list-get-tag begin struct
)))
1235 (if raw-secondary-p raw
1236 (let ((tag (org-element--parse-objects
1237 (match-beginning 4) (match-end 4) nil
1238 (org-element-restriction 'item
))))
1239 (dolist (datum tag tag
)
1240 (org-element-put-property datum
:parent item
))))))))))
1242 (defun org-element-item-interpreter (item contents
)
1243 "Interpret ITEM element as Org syntax.
1244 CONTENTS is the contents of the element."
1245 (let* ((bullet (let ((bullet (org-element-property :bullet item
)))
1246 (org-list-bullet-string
1247 (cond ((not (string-match "[0-9a-zA-Z]" bullet
)) "- ")
1248 ((eq org-plain-list-ordered-item-terminator ?\
)) "1)")
1250 (checkbox (org-element-property :checkbox item
))
1251 (counter (org-element-property :counter item
))
1252 (tag (let ((tag (org-element-property :tag item
)))
1253 (and tag
(org-element-interpret-data tag
))))
1254 ;; Compute indentation.
1255 (ind (make-string (length bullet
) 32))
1256 (item-starts-with-par-p
1257 (eq (org-element-type (car (org-element-contents item
)))
1262 (and counter
(format "[@%d] " counter
))
1268 (and tag
(format "%s :: " tag
))
1270 (let ((contents (replace-regexp-in-string
1271 "\\(^\\)[ \t]*\\S-" ind contents nil nil
1)))
1272 (if item-starts-with-par-p
(org-trim contents
)
1273 (concat "\n" contents
)))))))
1278 (defun org-element--list-struct (limit)
1279 ;; Return structure of list at point. Internal function. See
1280 ;; `org-list-struct' for details.
1281 (let ((case-fold-search t
)
1283 (item-re (org-item-re))
1284 (inlinetask-re (and (featurep 'org-inlinetask
) "^\\*+ "))
1290 ;; At limit: end all items.
1293 (let ((end (progn (skip-chars-backward " \r\t\n")
1296 (dolist (item items
(sort (nconc items struct
)
1297 'car-less-than-car
))
1298 (setcar (nthcdr 6 item
) end
)))))
1299 ;; At list end: end all items.
1300 ((looking-at org-list-end-re
)
1301 (throw 'exit
(dolist (item items
(sort (nconc items struct
)
1302 'car-less-than-car
))
1303 (setcar (nthcdr 6 item
) (point)))))
1304 ;; At a new item: end previous sibling.
1305 ((looking-at item-re
)
1306 (let ((ind (save-excursion (skip-chars-forward " \t")
1308 (setq top-ind
(min top-ind ind
))
1309 (while (and items
(<= ind
(nth 1 (car items
))))
1310 (let ((item (pop items
)))
1311 (setcar (nthcdr 6 item
) (point))
1312 (push item struct
)))
1313 (push (progn (looking-at org-list-full-item-re
)
1314 (let ((bullet (match-string-no-properties 1)))
1318 (match-string-no-properties 2) ; counter
1319 (match-string-no-properties 3) ; checkbox
1321 (and (save-match-data
1322 (string-match "[-+*]" bullet
))
1323 (match-string-no-properties 4))
1324 ;; Ending position, unknown so far.
1328 ;; Skip empty lines.
1329 ((looking-at "^[ \t]*$") (forward-line))
1330 ;; Skip inline tasks and blank lines along the way.
1331 ((and inlinetask-re
(looking-at inlinetask-re
))
1333 (let ((origin (point)))
1334 (when (re-search-forward inlinetask-re limit t
)
1335 (if (org-looking-at-p "END[ \t]*$") (forward-line)
1336 (goto-char origin
)))))
1337 ;; At some text line. Check if it ends any previous item.
1339 (let ((ind (save-excursion (skip-chars-forward " \t")
1341 (when (<= ind top-ind
)
1342 (skip-chars-backward " \r\t\n")
1344 (while (<= ind
(nth 1 (car items
)))
1345 (let ((item (pop items
)))
1346 (setcar (nthcdr 6 item
) (line-beginning-position))
1349 (throw 'exit
(sort struct
#'car-less-than-car
))))))
1350 ;; Skip blocks (any type) and drawers contents.
1352 ((and (looking-at "[ \t]*#\\+BEGIN\\(:\\|_\\S-+\\)")
1354 (format "^[ \t]*#\\+END%s[ \t]*$" (match-string 1))
1356 ((and (looking-at org-drawer-regexp
)
1357 (re-search-forward "^[ \t]*:END:[ \t]*$" limit t
))))
1358 (forward-line))))))))
1360 (defun org-element-plain-list-parser (limit affiliated structure
)
1361 "Parse a plain list.
1363 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1364 the buffer position at the beginning of the first affiliated
1365 keyword and CDR is a plist of affiliated keywords along with
1366 their value. STRUCTURE is the structure of the plain list being
1369 Return a list whose CAR is `plain-list' and CDR is a plist
1370 containing `:type', `:begin', `:end', `:contents-begin' and
1371 `:contents-end', `:structure', `:post-blank' and
1372 `:post-affiliated' keywords.
1374 Assume point is at the beginning of the list."
1376 (let* ((struct (or structure
(org-element--list-struct limit
)))
1377 (type (cond ((org-looking-at-p "[ \t]*[A-Za-z0-9]") 'ordered
)
1378 ((nth 5 (assq (point) struct
)) 'descriptive
)
1380 (contents-begin (point))
1381 (begin (car affiliated
))
1382 (contents-end (let* ((item (assq contents-begin struct
))
1385 (while (and (setq item
(assq pos struct
))
1386 (= (nth 1 item
) ind
))
1387 (setq pos
(nth 6 item
)))
1389 (end (progn (goto-char contents-end
)
1390 (skip-chars-forward " \r\t\n" limit
)
1391 (if (= (point) limit
) limit
(line-beginning-position)))))
1398 :contents-begin contents-begin
1399 :contents-end contents-end
1401 :post-blank
(count-lines contents-end end
)
1402 :post-affiliated contents-begin
)
1403 (cdr affiliated
))))))
1405 (defun org-element-plain-list-interpreter (_ contents
)
1406 "Interpret plain-list element as Org syntax.
1407 CONTENTS is the contents of the element."
1410 (goto-char (point-min))
1415 ;;;; Property Drawer
1417 (defun org-element-property-drawer-parser (limit)
1418 "Parse a property drawer.
1420 LIMIT bounds the search.
1422 Return a list whose car is `property-drawer' and cdr is a plist
1423 containing `:begin', `:end', `:contents-begin', `:contents-end',
1424 `:post-blank' and `:post-affiliated' keywords.
1426 Assume point is at the beginning of the property drawer."
1428 (let ((case-fold-search t
)
1430 (contents-begin (line-beginning-position 2)))
1431 (re-search-forward "^[ \t]*:END:[ \t]*$" limit t
)
1432 (let ((contents-end (and (> (match-beginning 0) contents-begin
)
1433 (match-beginning 0)))
1434 (before-blank (progn (forward-line) (point)))
1435 (end (progn (skip-chars-forward " \r\t\n" limit
)
1436 (if (eobp) (point) (line-beginning-position)))))
1437 (list 'property-drawer
1440 :contents-begin
(and contents-end contents-begin
)
1441 :contents-end contents-end
1442 :post-blank
(count-lines before-blank end
)
1443 :post-affiliated begin
))))))
1445 (defun org-element-property-drawer-interpreter (_ contents
)
1446 "Interpret property-drawer element as Org syntax.
1447 CONTENTS is the properties within the drawer."
1448 (format ":PROPERTIES:\n%s:END:" contents
))
1453 (defun org-element-quote-block-parser (limit affiliated
)
1454 "Parse a quote block.
1456 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1457 the buffer position at the beginning of the first affiliated
1458 keyword and CDR is a plist of affiliated keywords along with
1461 Return a list whose CAR is `quote-block' and CDR is a plist
1462 containing `:begin', `:end', `:contents-begin', `:contents-end',
1463 `:post-blank' and `:post-affiliated' keywords.
1465 Assume point is at the beginning of the block."
1466 (let ((case-fold-search t
))
1467 (if (not (save-excursion
1468 (re-search-forward "^[ \t]*#\\+END_QUOTE[ \t]*$" limit t
)))
1469 ;; Incomplete block: parse it as a paragraph.
1470 (org-element-paragraph-parser limit affiliated
)
1471 (let ((block-end-line (match-beginning 0)))
1473 (let* ((begin (car affiliated
))
1474 (post-affiliated (point))
1475 ;; Empty blocks have no contents.
1476 (contents-begin (progn (forward-line)
1477 (and (< (point) block-end-line
)
1479 (contents-end (and contents-begin block-end-line
))
1480 (pos-before-blank (progn (goto-char block-end-line
)
1483 (end (progn (skip-chars-forward " \r\t\n" limit
)
1484 (if (eobp) (point) (line-beginning-position)))))
1489 :contents-begin contents-begin
1490 :contents-end contents-end
1491 :post-blank
(count-lines pos-before-blank end
)
1492 :post-affiliated post-affiliated
)
1493 (cdr affiliated
)))))))))
1495 (defun org-element-quote-block-interpreter (_ contents
)
1496 "Interpret quote-block element as Org syntax.
1497 CONTENTS is the contents of the element."
1498 (format "#+BEGIN_QUOTE\n%s#+END_QUOTE" contents
))
1503 (defun org-element-section-parser (_)
1506 Return a list whose CAR is `section' and CDR is a plist
1507 containing `:begin', `:end', `:contents-begin', `contents-end',
1508 `:post-blank' and `:post-affiliated' keywords."
1510 ;; Beginning of section is the beginning of the first non-blank
1511 ;; line after previous headline.
1512 (let ((begin (point))
1513 (end (progn (org-with-limited-levels (outline-next-heading))
1515 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
1516 (line-beginning-position 2))))
1520 :contents-begin begin
1521 :contents-end pos-before-blank
1522 :post-blank
(count-lines pos-before-blank end
)
1523 :post-affiliated begin
)))))
1525 (defun org-element-section-interpreter (_ contents
)
1526 "Interpret section element as Org syntax.
1527 CONTENTS is the contents of the element."
1533 (defun org-element-special-block-parser (limit affiliated
)
1534 "Parse a special block.
1536 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1537 the buffer position at the beginning of the first affiliated
1538 keyword and CDR is a plist of affiliated keywords along with
1541 Return a list whose CAR is `special-block' and CDR is a plist
1542 containing `:type', `:begin', `:end', `:contents-begin',
1543 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
1545 Assume point is at the beginning of the block."
1546 (let* ((case-fold-search t
)
1547 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
1548 (match-string-no-properties 1))))
1549 (if (not (save-excursion
1551 (format "^[ \t]*#\\+END_%s[ \t]*$" (regexp-quote type
))
1553 ;; Incomplete block: parse it as a paragraph.
1554 (org-element-paragraph-parser limit affiliated
)
1555 (let ((block-end-line (match-beginning 0)))
1557 (let* ((begin (car affiliated
))
1558 (post-affiliated (point))
1559 ;; Empty blocks have no contents.
1560 (contents-begin (progn (forward-line)
1561 (and (< (point) block-end-line
)
1563 (contents-end (and contents-begin block-end-line
))
1564 (pos-before-blank (progn (goto-char block-end-line
)
1567 (end (progn (skip-chars-forward " \r\t\n" limit
)
1568 (if (eobp) (point) (line-beginning-position)))))
1569 (list 'special-block
1574 :contents-begin contents-begin
1575 :contents-end contents-end
1576 :post-blank
(count-lines pos-before-blank end
)
1577 :post-affiliated post-affiliated
)
1578 (cdr affiliated
)))))))))
1580 (defun org-element-special-block-interpreter (special-block contents
)
1581 "Interpret SPECIAL-BLOCK element as Org syntax.
1582 CONTENTS is the contents of the element."
1583 (let ((block-type (org-element-property :type special-block
)))
1584 (format "#+BEGIN_%s\n%s#+END_%s" block-type contents block-type
)))
1590 ;; For each element, a parser and an interpreter are also defined.
1591 ;; Both follow the same naming convention used for greater elements.
1593 ;; Also, as for greater elements, adding a new element type is done
1594 ;; through the following steps: implement a parser and an interpreter,
1595 ;; tweak `org-element--current-element' so that it recognizes the new
1596 ;; type and add that new type to `org-element-all-elements'.
1601 (defun org-element-babel-call-parser (limit affiliated
)
1602 "Parse a babel call.
1604 LIMIT bounds the search. AFFILIATED is a list of which car is
1605 the buffer position at the beginning of the first affiliated
1606 keyword and cdr is a plist of affiliated keywords along with
1609 Return a list whose car is `babel-call' and cdr is a plist
1610 containing `:call', `:inside-header', `:arguments',
1611 `:end-header', `:begin', `:end', `:value', `:post-blank' and
1612 `:post-affiliated' as keywords."
1614 (let* ((begin (car affiliated
))
1615 (post-affiliated (point))
1616 (value (progn (search-forward ":" nil t
)
1618 (buffer-substring-no-properties
1619 (point) (line-end-position)))))
1620 (pos-before-blank (progn (forward-line) (point)))
1621 (end (progn (skip-chars-forward " \r\t\n" limit
)
1622 (if (eobp) (point) (line-beginning-position))))
1625 "\\([^()\n]+?\\)\\(?:\\[\\(.*?\\)\\]\\)?(\\(.*\\))[ \t]*\\(.*\\)"
1629 (list :call
(and valid-value
(match-string 1 value
))
1630 :inside-header
(and valid-value
1631 (org-string-nw-p (match-string 2 value
)))
1632 :arguments
(and valid-value
1633 (org-string-nw-p (match-string 3 value
)))
1634 :end-header
(and valid-value
1635 (org-string-nw-p (match-string 4 value
)))
1639 :post-blank
(count-lines pos-before-blank end
)
1640 :post-affiliated post-affiliated
)
1641 (cdr affiliated
))))))
1643 (defun org-element-babel-call-interpreter (babel-call _
)
1644 "Interpret BABEL-CALL element as Org syntax."
1646 (org-element-property :call babel-call
)
1647 (let ((h (org-element-property :inside-header babel-call
)))
1648 (and h
(format "[%s]" h
)))
1649 (concat "(" (org-element-property :arguments babel-call
) ")")
1650 (let ((h (org-element-property :end-header babel-call
)))
1651 (and h
(concat " " h
)))))
1656 (defun org-element-clock-parser (limit)
1659 LIMIT bounds the search.
1661 Return a list whose CAR is `clock' and CDR is a plist containing
1662 `:status', `:value', `:time', `:begin', `:end', `:post-blank' and
1663 `:post-affiliated' as keywords."
1665 (let* ((case-fold-search nil
)
1667 (value (progn (search-forward org-clock-string
(line-end-position) t
)
1668 (skip-chars-forward " \t")
1669 (org-element-timestamp-parser)))
1670 (duration (and (search-forward " => " (line-end-position) t
)
1671 (progn (skip-chars-forward " \t")
1672 (looking-at "\\(\\S-+\\)[ \t]*$"))
1673 (org-match-string-no-properties 1)))
1674 (status (if duration
'closed
'running
))
1675 (post-blank (let ((before-blank (progn (forward-line) (point))))
1676 (skip-chars-forward " \r\t\n" limit
)
1677 (skip-chars-backward " \t")
1678 (unless (bolp) (end-of-line))
1679 (count-lines before-blank
(point))))
1682 (list :status status
1687 :post-blank post-blank
1688 :post-affiliated begin
)))))
1690 (defun org-element-clock-interpreter (clock _
)
1691 "Interpret CLOCK element as Org syntax."
1692 (concat org-clock-string
" "
1693 (org-element-timestamp-interpreter
1694 (org-element-property :value clock
) nil
)
1695 (let ((duration (org-element-property :duration clock
)))
1700 (org-split-string duration
":")))))))
1705 (defun org-element-comment-parser (limit affiliated
)
1708 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1709 the buffer position at the beginning of the first affiliated
1710 keyword and CDR is a plist of affiliated keywords along with
1713 Return a list whose CAR is `comment' and CDR is a plist
1714 containing `:begin', `:end', `:value', `:post-blank',
1715 `:post-affiliated' keywords.
1717 Assume point is at comment beginning."
1719 (let* ((begin (car affiliated
))
1720 (post-affiliated (point))
1721 (value (prog2 (looking-at "[ \t]*# ?")
1722 (buffer-substring-no-properties
1723 (match-end 0) (line-end-position))
1726 ;; Get comments ending.
1728 (while (and (< (point) limit
) (looking-at "[ \t]*#\\( \\|$\\)"))
1729 ;; Accumulate lines without leading hash and first
1734 (buffer-substring-no-properties
1735 (match-end 0) (line-end-position))))
1738 (end (progn (goto-char com-end
)
1739 (skip-chars-forward " \r\t\n" limit
)
1740 (if (eobp) (point) (line-beginning-position)))))
1746 :post-blank
(count-lines com-end end
)
1747 :post-affiliated post-affiliated
)
1748 (cdr affiliated
))))))
1750 (defun org-element-comment-interpreter (comment _
)
1751 "Interpret COMMENT element as Org syntax.
1753 (replace-regexp-in-string "^" "# " (org-element-property :value comment
)))
1758 (defun org-element-comment-block-parser (limit affiliated
)
1759 "Parse an export block.
1761 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1762 the buffer position at the beginning of the first affiliated
1763 keyword and CDR is a plist of affiliated keywords along with
1766 Return a list whose CAR is `comment-block' and CDR is a plist
1767 containing `:begin', `:end', `:value', `:post-blank' and
1768 `:post-affiliated' keywords.
1770 Assume point is at comment block beginning."
1771 (let ((case-fold-search t
))
1772 (if (not (save-excursion
1773 (re-search-forward "^[ \t]*#\\+END_COMMENT[ \t]*$" limit t
)))
1774 ;; Incomplete block: parse it as a paragraph.
1775 (org-element-paragraph-parser limit affiliated
)
1776 (let ((contents-end (match-beginning 0)))
1778 (let* ((begin (car affiliated
))
1779 (post-affiliated (point))
1780 (contents-begin (progn (forward-line) (point)))
1781 (pos-before-blank (progn (goto-char contents-end
)
1784 (end (progn (skip-chars-forward " \r\t\n" limit
)
1785 (if (eobp) (point) (line-beginning-position))))
1786 (value (buffer-substring-no-properties
1787 contents-begin contents-end
)))
1788 (list 'comment-block
1793 :post-blank
(count-lines pos-before-blank end
)
1794 :post-affiliated post-affiliated
)
1795 (cdr affiliated
)))))))))
1797 (defun org-element-comment-block-interpreter (comment-block _
)
1798 "Interpret COMMENT-BLOCK element as Org syntax."
1799 (format "#+BEGIN_COMMENT\n%s#+END_COMMENT"
1800 (org-element-normalize-string
1801 (org-remove-indentation
1802 (org-element-property :value comment-block
)))))
1807 (defun org-element-diary-sexp-parser (limit affiliated
)
1808 "Parse a diary sexp.
1810 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1811 the buffer position at the beginning of the first affiliated
1812 keyword and CDR is a plist of affiliated keywords along with
1815 Return a list whose CAR is `diary-sexp' and CDR is a plist
1816 containing `:begin', `:end', `:value', `:post-blank' and
1817 `:post-affiliated' keywords."
1819 (let ((begin (car affiliated
))
1820 (post-affiliated (point))
1821 (value (progn (looking-at "\\(%%(.*\\)[ \t]*$")
1822 (org-match-string-no-properties 1)))
1823 (pos-before-blank (progn (forward-line) (point)))
1824 (end (progn (skip-chars-forward " \r\t\n" limit
)
1825 (if (eobp) (point) (line-beginning-position)))))
1831 :post-blank
(count-lines pos-before-blank end
)
1832 :post-affiliated post-affiliated
)
1833 (cdr affiliated
))))))
1835 (defun org-element-diary-sexp-interpreter (diary-sexp _
)
1836 "Interpret DIARY-SEXP as Org syntax."
1837 (org-element-property :value diary-sexp
))
1842 (defun org-element-example-block-parser (limit affiliated
)
1843 "Parse an example block.
1845 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1846 the buffer position at the beginning of the first affiliated
1847 keyword and CDR is a plist of affiliated keywords along with
1850 Return a list whose CAR is `example-block' and CDR is a plist
1851 containing `:begin', `:end', `:number-lines', `:preserve-indent',
1852 `:retain-labels', `:use-labels', `:label-fmt', `:switches',
1853 `:value', `:post-blank' and `:post-affiliated' keywords."
1854 (let ((case-fold-search t
))
1855 (if (not (save-excursion
1856 (re-search-forward "^[ \t]*#\\+END_EXAMPLE[ \t]*$" limit t
)))
1857 ;; Incomplete block: parse it as a paragraph.
1858 (org-element-paragraph-parser limit affiliated
)
1859 (let ((contents-end (match-beginning 0)))
1863 (looking-at "^[ \t]*#\\+BEGIN_EXAMPLE\\(?: +\\(.*\\)\\)?")
1864 (org-match-string-no-properties 1)))
1865 ;; Switches analysis
1867 (cond ((not switches
) nil
)
1868 ((string-match "-n\\>" switches
) 'new
)
1869 ((string-match "+n\\>" switches
) 'continued
)))
1871 (and switches
(string-match "-i\\>" switches
)))
1872 ;; Should labels be retained in (or stripped from) example
1876 (not (string-match "-r\\>" switches
))
1877 (and number-lines
(string-match "-k\\>" switches
))))
1878 ;; What should code-references use - labels or
1883 (not (string-match "-k\\>" switches
)))))
1886 (string-match "-l +\"\\([^\"\n]+\\)\"" switches
)
1887 (match-string 1 switches
)))
1888 ;; Standard block parsing.
1889 (begin (car affiliated
))
1890 (post-affiliated (point))
1891 (block-ind (progn (skip-chars-forward " \t") (current-column)))
1892 (contents-begin (progn (forward-line) (point)))
1893 (value (org-remove-indentation
1894 (org-unescape-code-in-string
1895 (buffer-substring-no-properties
1896 contents-begin contents-end
))
1898 (pos-before-blank (progn (goto-char contents-end
)
1901 (end (progn (skip-chars-forward " \r\t\n" limit
)
1902 (if (eobp) (point) (line-beginning-position)))))
1903 (list 'example-block
1909 :number-lines number-lines
1910 :preserve-indent preserve-indent
1911 :retain-labels retain-labels
1912 :use-labels use-labels
1913 :label-fmt label-fmt
1914 :post-blank
(count-lines pos-before-blank end
)
1915 :post-affiliated post-affiliated
)
1916 (cdr affiliated
)))))))))
1918 (defun org-element-example-block-interpreter (example-block _
)
1919 "Interpret EXAMPLE-BLOCK element as Org syntax."
1920 (let ((switches (org-element-property :switches example-block
))
1921 (value (org-element-property :value example-block
)))
1922 (concat "#+BEGIN_EXAMPLE" (and switches
(concat " " switches
)) "\n"
1923 (org-element-normalize-string
1924 (org-escape-code-in-string
1925 (if (or org-src-preserve-indentation
1926 (org-element-property :preserve-indent example-block
))
1928 (org-remove-indentation value
))))
1934 (defun org-element-export-block-parser (limit affiliated
)
1935 "Parse an export block.
1937 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1938 the buffer position at the beginning of the first affiliated
1939 keyword and CDR is a plist of affiliated keywords along with
1942 Return a list whose CAR is `export-block' and CDR is a plist
1943 containing `:begin', `:end', `:type', `:value', `:post-blank' and
1944 `:post-affiliated' keywords.
1946 Assume point is at export-block beginning."
1947 (let* ((case-fold-search t
))
1948 (if (not (save-excursion
1949 (re-search-forward "^[ \t]*#\\+END_EXPORT[ \t]*$" limit t
)))
1950 ;; Incomplete block: parse it as a paragraph.
1951 (org-element-paragraph-parser limit affiliated
)
1953 (let* ((contents-end (match-beginning 0))
1957 "[ \t]*#\\+BEGIN_EXPORT\\(?:[ \t]+\\(\\S-+\\)\\)?[ \t]*$")
1958 (match-string-no-properties 1)))
1959 (begin (car affiliated
))
1960 (post-affiliated (point))
1961 (contents-begin (progn (forward-line) (point)))
1962 (pos-before-blank (progn (goto-char contents-end
)
1965 (end (progn (skip-chars-forward " \r\t\n" limit
)
1966 (if (eobp) (point) (line-beginning-position))))
1967 (value (buffer-substring-no-properties contents-begin
1971 (list :type
(and backend
(upcase backend
))
1975 :post-blank
(count-lines pos-before-blank end
)
1976 :post-affiliated post-affiliated
)
1977 (cdr affiliated
))))))))
1979 (defun org-element-export-block-interpreter (export-block _
)
1980 "Interpret EXPORT-BLOCK element as Org syntax."
1981 (format "#+BEGIN_EXPORT %s\n%s#+END_EXPORT"
1982 (org-element-property :type export-block
)
1983 (org-element-property :value export-block
)))
1988 (defun org-element-fixed-width-parser (limit affiliated
)
1989 "Parse a fixed-width section.
1991 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1992 the buffer position at the beginning of the first affiliated
1993 keyword and CDR is a plist of affiliated keywords along with
1996 Return a list whose CAR is `fixed-width' and CDR is a plist
1997 containing `:begin', `:end', `:value', `:post-blank' and
1998 `:post-affiliated' keywords.
2000 Assume point is at the beginning of the fixed-width area."
2002 (let* ((begin (car affiliated
))
2003 (post-affiliated (point))
2007 (while (and (< (point) limit
)
2008 (looking-at "[ \t]*:\\( \\|$\\)"))
2009 ;; Accumulate text without starting colons.
2012 (buffer-substring-no-properties
2013 (match-end 0) (point-at-eol))
2017 (end (progn (skip-chars-forward " \r\t\n" limit
)
2018 (if (eobp) (point) (line-beginning-position)))))
2024 :post-blank
(count-lines end-area end
)
2025 :post-affiliated post-affiliated
)
2026 (cdr affiliated
))))))
2028 (defun org-element-fixed-width-interpreter (fixed-width _
)
2029 "Interpret FIXED-WIDTH element as Org syntax."
2030 (let ((value (org-element-property :value fixed-width
)))
2032 (replace-regexp-in-string
2034 (if (string-match "\n\\'" value
) (substring value
0 -
1) value
)))))
2037 ;;;; Horizontal Rule
2039 (defun org-element-horizontal-rule-parser (limit affiliated
)
2040 "Parse an horizontal rule.
2042 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2043 the buffer position at the beginning of the first affiliated
2044 keyword and CDR is a plist of affiliated keywords along with
2047 Return a list whose CAR is `horizontal-rule' and CDR is a plist
2048 containing `:begin', `:end', `:post-blank' and `:post-affiliated'
2051 (let ((begin (car affiliated
))
2052 (post-affiliated (point))
2053 (post-hr (progn (forward-line) (point)))
2054 (end (progn (skip-chars-forward " \r\t\n" limit
)
2055 (if (eobp) (point) (line-beginning-position)))))
2056 (list 'horizontal-rule
2060 :post-blank
(count-lines post-hr end
)
2061 :post-affiliated post-affiliated
)
2062 (cdr affiliated
))))))
2064 (defun org-element-horizontal-rule-interpreter (&rest _
)
2065 "Interpret HORIZONTAL-RULE element as Org syntax."
2071 (defun org-element-keyword-parser (limit affiliated
)
2072 "Parse a keyword at point.
2074 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2075 the buffer position at the beginning of the first affiliated
2076 keyword and CDR is a plist of affiliated keywords along with
2079 Return a list whose CAR is `keyword' and CDR is a plist
2080 containing `:key', `:value', `:begin', `:end', `:post-blank' and
2081 `:post-affiliated' keywords."
2083 ;; An orphaned affiliated keyword is considered as a regular
2084 ;; keyword. In this case AFFILIATED is nil, so we take care of
2085 ;; this corner case.
2086 (let ((begin (or (car affiliated
) (point)))
2087 (post-affiliated (point))
2088 (key (progn (looking-at "[ \t]*#\\+\\(\\S-+*\\):")
2089 (upcase (org-match-string-no-properties 1))))
2090 (value (org-trim (buffer-substring-no-properties
2091 (match-end 0) (point-at-eol))))
2092 (pos-before-blank (progn (forward-line) (point)))
2093 (end (progn (skip-chars-forward " \r\t\n" limit
)
2094 (if (eobp) (point) (line-beginning-position)))))
2101 :post-blank
(count-lines pos-before-blank end
)
2102 :post-affiliated post-affiliated
)
2103 (cdr affiliated
))))))
2105 (defun org-element-keyword-interpreter (keyword _
)
2106 "Interpret KEYWORD element as Org syntax."
2108 (org-element-property :key keyword
)
2109 (org-element-property :value keyword
)))
2112 ;;;; Latex Environment
2114 (defconst org-element--latex-begin-environment
2115 "^[ \t]*\\\\begin{\\([A-Za-z0-9*]+\\)}"
2116 "Regexp matching the beginning of a LaTeX environment.
2117 The environment is captured by the first group.
2119 See also `org-element--latex-end-environment'.")
2121 (defconst org-element--latex-end-environment
2122 "\\\\end{%s}[ \t]*$"
2123 "Format string matching the ending of a LaTeX environment.
2124 See also `org-element--latex-begin-environment'.")
2126 (defun org-element-latex-environment-parser (limit affiliated
)
2127 "Parse a LaTeX environment.
2129 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2130 the buffer position at the beginning of the first affiliated
2131 keyword and CDR is a plist of affiliated keywords along with
2134 Return a list whose CAR is `latex-environment' and CDR is a plist
2135 containing `:begin', `:end', `:value', `:post-blank' and
2136 `:post-affiliated' keywords.
2138 Assume point is at the beginning of the latex environment."
2140 (let ((case-fold-search t
)
2141 (code-begin (point)))
2142 (looking-at org-element--latex-begin-environment
)
2143 (if (not (re-search-forward (format org-element--latex-end-environment
2144 (regexp-quote (match-string 1)))
2146 ;; Incomplete latex environment: parse it as a paragraph.
2147 (org-element-paragraph-parser limit affiliated
)
2148 (let* ((code-end (progn (forward-line) (point)))
2149 (begin (car affiliated
))
2150 (value (buffer-substring-no-properties code-begin code-end
))
2151 (end (progn (skip-chars-forward " \r\t\n" limit
)
2152 (if (eobp) (point) (line-beginning-position)))))
2153 (list 'latex-environment
2158 :post-blank
(count-lines code-end end
)
2159 :post-affiliated code-begin
)
2160 (cdr affiliated
))))))))
2162 (defun org-element-latex-environment-interpreter (latex-environment _
)
2163 "Interpret LATEX-ENVIRONMENT element as Org syntax."
2164 (org-element-property :value latex-environment
))
2169 (defun org-element-node-property-parser (limit)
2170 "Parse a node-property at point.
2172 LIMIT bounds the search.
2174 Return a list whose CAR is `node-property' and CDR is a plist
2175 containing `:key', `:value', `:begin', `:end', `:post-blank' and
2176 `:post-affiliated' keywords."
2177 (looking-at org-property-re
)
2178 (let ((case-fold-search t
)
2180 (key (org-match-string-no-properties 2))
2181 (value (org-match-string-no-properties 3))
2182 (end (save-excursion
2184 (if (re-search-forward org-property-re limit t
)
2185 (line-beginning-position)
2187 (list 'node-property
2193 :post-affiliated begin
))))
2195 (defun org-element-node-property-interpreter (node-property _
)
2196 "Interpret NODE-PROPERTY element as Org syntax."
2197 (format org-property-format
2198 (format ":%s:" (org-element-property :key node-property
))
2199 (or (org-element-property :value node-property
) "")))
2204 (defun org-element-paragraph-parser (limit affiliated
)
2207 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2208 the buffer position at the beginning of the first affiliated
2209 keyword and CDR is a plist of affiliated keywords along with
2212 Return a list whose CAR is `paragraph' and CDR is a plist
2213 containing `:begin', `:end', `:contents-begin' and
2214 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
2216 Assume point is at the beginning of the paragraph."
2218 (let* ((begin (car affiliated
))
2219 (contents-begin (point))
2221 (let ((case-fold-search t
))
2223 ;; A matching `org-element-paragraph-separate' is not
2224 ;; necessarily the end of the paragraph. In particular,
2225 ;; drawers, blocks or LaTeX environments opening lines
2226 ;; must be closed. Moreover keywords with a secondary
2227 ;; value must belong to "dual keywords".
2230 ((not (and (re-search-forward
2231 org-element-paragraph-separate limit
'move
)
2232 (progn (beginning-of-line) t
))))
2233 ((looking-at org-drawer-regexp
)
2235 (re-search-forward "^[ \t]*:END:[ \t]*$" limit t
)))
2236 ((looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
2239 (format "^[ \t]*#\\+END_%s[ \t]*$"
2240 (regexp-quote (match-string 1)))
2242 ((looking-at org-element--latex-begin-environment
)
2245 (format org-element--latex-end-environment
2246 (regexp-quote (match-string 1)))
2248 ((looking-at "[ \t]*#\\+\\(\\S-+\\)\\[.*\\]:")
2249 (member-ignore-case (match-string 1)
2250 org-element-dual-keywords
))
2251 ;; Everything else is unambiguous.
2254 (if (= (point) limit
) limit
2255 (goto-char (line-beginning-position)))))
2256 (contents-end (save-excursion
2257 (skip-chars-backward " \r\t\n" contents-begin
)
2258 (line-beginning-position 2)))
2259 (end (progn (skip-chars-forward " \r\t\n" limit
)
2260 (if (eobp) (point) (line-beginning-position)))))
2265 :contents-begin contents-begin
2266 :contents-end contents-end
2267 :post-blank
(count-lines before-blank end
)
2268 :post-affiliated contents-begin
)
2269 (cdr affiliated
))))))
2271 (defun org-element-paragraph-interpreter (_ contents
)
2272 "Interpret paragraph element as Org syntax.
2273 CONTENTS is the contents of the element."
2279 (defun org-element-planning-parser (limit)
2282 LIMIT bounds the search.
2284 Return a list whose CAR is `planning' and CDR is a plist
2285 containing `:closed', `:deadline', `:scheduled', `:begin',
2286 `:end', `:post-blank' and `:post-affiliated' keywords."
2288 (let* ((case-fold-search nil
)
2290 (post-blank (let ((before-blank (progn (forward-line) (point))))
2291 (skip-chars-forward " \r\t\n" limit
)
2292 (skip-chars-backward " \t")
2293 (unless (bolp) (end-of-line))
2294 (count-lines before-blank
(point))))
2296 closed deadline scheduled
)
2298 (while (re-search-forward org-keyword-time-not-clock-regexp end t
)
2299 (goto-char (match-end 1))
2300 (skip-chars-forward " \t" end
)
2301 (let ((keyword (match-string 1))
2302 (time (org-element-timestamp-parser)))
2303 (cond ((equal keyword org-closed-string
) (setq closed time
))
2304 ((equal keyword org-deadline-string
) (setq deadline time
))
2305 (t (setq scheduled time
)))))
2307 (list :closed closed
2309 :scheduled scheduled
2312 :post-blank post-blank
2313 :post-affiliated begin
)))))
2315 (defun org-element-planning-interpreter (planning _
)
2316 "Interpret PLANNING element as Org syntax."
2320 (list (let ((deadline (org-element-property :deadline planning
)))
2322 (concat org-deadline-string
" "
2323 (org-element-timestamp-interpreter deadline nil
))))
2324 (let ((scheduled (org-element-property :scheduled planning
)))
2326 (concat org-scheduled-string
" "
2327 (org-element-timestamp-interpreter scheduled nil
))))
2328 (let ((closed (org-element-property :closed planning
)))
2330 (concat org-closed-string
" "
2331 (org-element-timestamp-interpreter closed nil
))))))
2337 (defun org-element-src-block-parser (limit affiliated
)
2340 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2341 the buffer position at the beginning of the first affiliated
2342 keyword and CDR is a plist of affiliated keywords along with
2345 Return a list whose CAR is `src-block' and CDR is a plist
2346 containing `:language', `:switches', `:parameters', `:begin',
2347 `:end', `:number-lines', `:retain-labels', `:use-labels',
2348 `:label-fmt', `:preserve-indent', `:value', `:post-blank' and
2349 `:post-affiliated' keywords.
2351 Assume point is at the beginning of the block."
2352 (let ((case-fold-search t
))
2353 (if (not (save-excursion (re-search-forward "^[ \t]*#\\+END_SRC[ \t]*$"
2355 ;; Incomplete block: parse it as a paragraph.
2356 (org-element-paragraph-parser limit affiliated
)
2357 (let ((contents-end (match-beginning 0)))
2359 (let* ((begin (car affiliated
))
2360 (post-affiliated (point))
2361 ;; Get language as a string.
2365 (concat "^[ \t]*#\\+BEGIN_SRC"
2366 "\\(?: +\\(\\S-+\\)\\)?"
2367 "\\(\\(?: +\\(?:-l \".*?\"\\|[-+][A-Za-z]\\)\\)+\\)?"
2369 (org-match-string-no-properties 1)))
2371 (switches (org-match-string-no-properties 2))
2373 (parameters (org-match-string-no-properties 3))
2374 ;; Switches analysis
2376 (cond ((not switches
) nil
)
2377 ((string-match "-n\\>" switches
) 'new
)
2378 ((string-match "+n\\>" switches
) 'continued
)))
2379 (preserve-indent (and switches
2380 (string-match "-i\\>" switches
)))
2383 (string-match "-l +\"\\([^\"\n]+\\)\"" switches
)
2384 (match-string 1 switches
)))
2385 ;; Should labels be retained in (or stripped from)
2389 (not (string-match "-r\\>" switches
))
2390 (and number-lines
(string-match "-k\\>" switches
))))
2391 ;; What should code-references use - labels or
2396 (not (string-match "-k\\>" switches
)))))
2398 (block-ind (progn (skip-chars-forward " \t") (current-column)))
2400 (value (org-remove-indentation
2401 (org-unescape-code-in-string
2402 (buffer-substring-no-properties
2403 (progn (forward-line) (point)) contents-end
))
2405 (pos-before-blank (progn (goto-char contents-end
)
2408 ;; Get position after ending blank lines.
2409 (end (progn (skip-chars-forward " \r\t\n" limit
)
2410 (if (eobp) (point) (line-beginning-position)))))
2413 (list :language language
2414 :switches
(and (org-string-nw-p switches
)
2415 (org-trim switches
))
2416 :parameters
(and (org-string-nw-p parameters
)
2417 (org-trim parameters
))
2420 :number-lines number-lines
2421 :preserve-indent preserve-indent
2422 :retain-labels retain-labels
2423 :use-labels use-labels
2424 :label-fmt label-fmt
2426 :post-blank
(count-lines pos-before-blank end
)
2427 :post-affiliated post-affiliated
)
2428 (cdr affiliated
)))))))))
2430 (defun org-element-src-block-interpreter (src-block _
)
2431 "Interpret SRC-BLOCK element as Org syntax."
2432 (let ((lang (org-element-property :language src-block
))
2433 (switches (org-element-property :switches src-block
))
2434 (params (org-element-property :parameters src-block
))
2436 (let ((val (org-element-property :value src-block
)))
2438 ((or org-src-preserve-indentation
2439 (org-element-property :preserve-indent src-block
))
2441 ((zerop org-edit-src-content-indentation
) val
)
2443 (let ((ind (make-string org-edit-src-content-indentation ?\s
)))
2444 (replace-regexp-in-string
2445 "\\(^\\)[ \t]*\\S-" ind val nil nil
1)))))))
2446 (concat (format "#+BEGIN_SRC%s\n"
2447 (concat (and lang
(concat " " lang
))
2448 (and switches
(concat " " switches
))
2449 (and params
(concat " " params
))))
2450 (org-element-normalize-string (org-escape-code-in-string value
))
2456 (defun org-element-table-parser (limit affiliated
)
2457 "Parse a table at point.
2459 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2460 the buffer position at the beginning of the first affiliated
2461 keyword and CDR is a plist of affiliated keywords along with
2464 Return a list whose CAR is `table' and CDR is a plist containing
2465 `:begin', `:end', `:tblfm', `:type', `:contents-begin',
2466 `:contents-end', `:value', `:post-blank' and `:post-affiliated'
2469 Assume point is at the beginning of the table."
2471 (let* ((case-fold-search t
)
2472 (table-begin (point))
2473 (type (if (looking-at "[ \t]*|") 'org
'table.el
))
2474 (end-re (format "^[ \t]*\\($\\|[^| \t%s]\\)"
2475 (if (eq type
'org
) "" "+")))
2476 (begin (car affiliated
))
2478 (if (re-search-forward end-re limit
'move
)
2479 (goto-char (match-beginning 0))
2482 (while (looking-at "[ \t]*#\\+TBLFM: +\\(.*\\)[ \t]*$")
2483 (push (org-match-string-no-properties 1) acc
)
2486 (pos-before-blank (point))
2487 (end (progn (skip-chars-forward " \r\t\n" limit
)
2488 (if (eobp) (point) (line-beginning-position)))))
2495 ;; Only `org' tables have contents. `table.el' tables
2496 ;; use a `:value' property to store raw table as
2498 :contents-begin
(and (eq type
'org
) table-begin
)
2499 :contents-end
(and (eq type
'org
) table-end
)
2500 :value
(and (eq type
'table.el
)
2501 (buffer-substring-no-properties
2502 table-begin table-end
))
2503 :post-blank
(count-lines pos-before-blank end
)
2504 :post-affiliated table-begin
)
2505 (cdr affiliated
))))))
2507 (defun org-element-table-interpreter (table contents
)
2508 "Interpret TABLE element as Org syntax.
2509 CONTENTS is a string, if table's type is `org', or nil."
2510 (if (eq (org-element-property :type table
) 'table.el
)
2511 (org-remove-indentation (org-element-property :value table
))
2512 (concat (with-temp-buffer (insert contents
)
2515 (mapconcat (lambda (fm) (concat "#+TBLFM: " fm
))
2516 (reverse (org-element-property :tblfm table
))
2522 (defun org-element-table-row-parser (_)
2523 "Parse table row at point.
2525 Return a list whose CAR is `table-row' and CDR is a plist
2526 containing `:begin', `:end', `:contents-begin', `:contents-end',
2527 `:type', `:post-blank' and `:post-affiliated' keywords."
2529 (let* ((type (if (looking-at "^[ \t]*|-") 'rule
'standard
))
2531 ;; A table rule has no contents. In that case, ensure
2532 ;; CONTENTS-BEGIN matches CONTENTS-END.
2533 (contents-begin (and (eq type
'standard
) (search-forward "|")))
2534 (contents-end (and (eq type
'standard
)
2537 (skip-chars-backward " \t")
2539 (end (line-beginning-position 2)))
2544 :contents-begin contents-begin
2545 :contents-end contents-end
2547 :post-affiliated begin
)))))
2549 (defun org-element-table-row-interpreter (table-row contents
)
2550 "Interpret TABLE-ROW element as Org syntax.
2551 CONTENTS is the contents of the table row."
2552 (if (eq (org-element-property :type table-row
) 'rule
) "|-"
2553 (concat "| " contents
)))
2558 (defun org-element-verse-block-parser (limit affiliated
)
2559 "Parse a verse block.
2561 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2562 the buffer position at the beginning of the first affiliated
2563 keyword and CDR is a plist of affiliated keywords along with
2566 Return a list whose CAR is `verse-block' and CDR is a plist
2567 containing `:begin', `:end', `:contents-begin', `:contents-end',
2568 `:post-blank' and `:post-affiliated' keywords.
2570 Assume point is at beginning of the block."
2571 (let ((case-fold-search t
))
2572 (if (not (save-excursion
2573 (re-search-forward "^[ \t]*#\\+END_VERSE[ \t]*$" limit t
)))
2574 ;; Incomplete block: parse it as a paragraph.
2575 (org-element-paragraph-parser limit affiliated
)
2576 (let ((contents-end (match-beginning 0)))
2578 (let* ((begin (car affiliated
))
2579 (post-affiliated (point))
2580 (contents-begin (progn (forward-line) (point)))
2581 (pos-before-blank (progn (goto-char contents-end
)
2584 (end (progn (skip-chars-forward " \r\t\n" limit
)
2585 (if (eobp) (point) (line-beginning-position)))))
2590 :contents-begin contents-begin
2591 :contents-end contents-end
2592 :post-blank
(count-lines pos-before-blank end
)
2593 :post-affiliated post-affiliated
)
2594 (cdr affiliated
)))))))))
2596 (defun org-element-verse-block-interpreter (_ contents
)
2597 "Interpret verse-block element as Org syntax.
2598 CONTENTS is verse block contents."
2599 (format "#+BEGIN_VERSE\n%s#+END_VERSE" contents
))
2605 ;; Unlike to elements, raw text can be found between objects. Hence,
2606 ;; `org-element--object-lex' is provided to find the next object in
2609 ;; Some object types (e.g., `italic') are recursive. Restrictions on
2610 ;; object types they can contain will be specified in
2611 ;; `org-element-object-restrictions'.
2613 ;; Creating a new type of object requires to alter
2614 ;; `org-element--object-regexp' and `org-element--object-lex', add the
2615 ;; new type in `org-element-all-objects', and possibly add
2616 ;; restrictions in `org-element-object-restrictions'.
2620 (defun org-element-bold-parser ()
2621 "Parse bold object at point, if any.
2623 When at a bold object, return a list whose car is `bold' and cdr
2624 is a plist with `:begin', `:end', `:contents-begin' and
2625 `:contents-end' and `:post-blank' keywords. Otherwise, return
2628 Assume point is at the first star marker."
2630 (unless (bolp) (backward-char 1))
2631 (when (looking-at org-emph-re
)
2632 (let ((begin (match-beginning 2))
2633 (contents-begin (match-beginning 4))
2634 (contents-end (match-end 4))
2635 (post-blank (progn (goto-char (match-end 2))
2636 (skip-chars-forward " \t")))
2641 :contents-begin contents-begin
2642 :contents-end contents-end
2643 :post-blank post-blank
))))))
2645 (defun org-element-bold-interpreter (_ contents
)
2646 "Interpret bold object as Org syntax.
2647 CONTENTS is the contents of the object."
2648 (format "*%s*" contents
))
2653 (defun org-element-code-parser ()
2654 "Parse code object at point, if any.
2656 When at a code object, return a list whose car is `code' and cdr
2657 is a plist with `:value', `:begin', `:end' and `:post-blank'
2658 keywords. Otherwise, return nil.
2660 Assume point is at the first tilde marker."
2662 (unless (bolp) (backward-char 1))
2663 (when (looking-at org-emph-re
)
2664 (let ((begin (match-beginning 2))
2665 (value (org-match-string-no-properties 4))
2666 (post-blank (progn (goto-char (match-end 2))
2667 (skip-chars-forward " \t")))
2673 :post-blank post-blank
))))))
2675 (defun org-element-code-interpreter (code _
)
2676 "Interpret CODE object as Org syntax."
2677 (format "~%s~" (org-element-property :value code
)))
2682 (defun org-element-entity-parser ()
2683 "Parse entity at point, if any.
2685 When at an entity, return a list whose car is `entity' and cdr
2686 a plist with `:begin', `:end', `:latex', `:latex-math-p',
2687 `:html', `:latin1', `:utf-8', `:ascii', `:use-brackets-p' and
2688 `:post-blank' as keywords. Otherwise, return nil.
2690 Assume point is at the beginning of the entity."
2692 (when (looking-at "\\\\\\(?:\\(?1:_ +\\)\\|\\(?1:there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\(?2:$\\|{}\\|[^[:alpha:]]\\)\\)")
2694 (let* ((value (or (org-entity-get (match-string 1))
2695 (throw 'no-object nil
)))
2696 (begin (match-beginning 0))
2697 (bracketsp (string= (match-string 2) "{}"))
2698 (post-blank (progn (goto-char (match-end 1))
2699 (when bracketsp
(forward-char 2))
2700 (skip-chars-forward " \t")))
2703 (list :name
(car value
)
2704 :latex
(nth 1 value
)
2705 :latex-math-p
(nth 2 value
)
2707 :ascii
(nth 4 value
)
2708 :latin1
(nth 5 value
)
2709 :utf-8
(nth 6 value
)
2712 :use-brackets-p bracketsp
2713 :post-blank post-blank
)))))))
2715 (defun org-element-entity-interpreter (entity _
)
2716 "Interpret ENTITY object as Org syntax."
2718 (org-element-property :name entity
)
2719 (when (org-element-property :use-brackets-p entity
) "{}")))
2724 (defun org-element-export-snippet-parser ()
2725 "Parse export snippet at point.
2727 When at an export snippet, return a list whose car is
2728 `export-snippet' and cdr a plist with `:begin', `:end',
2729 `:back-end', `:value' and `:post-blank' as keywords. Otherwise,
2732 Assume point is at the beginning of the snippet."
2735 (when (and (looking-at "@@\\([-A-Za-z0-9]+\\):")
2737 (save-match-data (goto-char (match-end 0))
2738 (re-search-forward "@@" nil t
)
2739 (match-beginning 0))))
2740 (let* ((begin (match-beginning 0))
2741 (back-end (org-match-string-no-properties 1))
2742 (value (buffer-substring-no-properties
2743 (match-end 0) contents-end
))
2744 (post-blank (skip-chars-forward " \t"))
2746 (list 'export-snippet
2747 (list :back-end back-end
2751 :post-blank post-blank
)))))))
2753 (defun org-element-export-snippet-interpreter (export-snippet _
)
2754 "Interpret EXPORT-SNIPPET object as Org syntax."
2756 (org-element-property :back-end export-snippet
)
2757 (org-element-property :value export-snippet
)))
2760 ;;;; Footnote Reference
2762 (defun org-element-footnote-reference-parser ()
2763 "Parse footnote reference at point, if any.
2765 When at a footnote reference, return a list whose car is
2766 `footnote-reference' and cdr a plist with `:label', `:type',
2767 `:begin', `:end', `:content-begin', `:contents-end' and
2768 `:post-blank' as keywords. Otherwise, return nil."
2769 (when (looking-at org-footnote-re
)
2770 (let ((closing (with-syntax-table org-element--pair-square-table
2771 (ignore-errors (scan-lists (point) 1 0)))))
2774 (let* ((begin (point))
2775 (label (match-string-no-properties 1))
2776 (inner-begin (match-end 0))
2777 (inner-end (1- closing
))
2778 (type (if (match-end 2) 'inline
'standard
))
2779 (post-blank (progn (goto-char closing
)
2780 (skip-chars-forward " \t")))
2782 (list 'footnote-reference
2787 :contents-begin
(and (eq type
'inline
) inner-begin
)
2788 :contents-end
(and (eq type
'inline
) inner-end
)
2789 :post-blank post-blank
))))))))
2791 (defun org-element-footnote-reference-interpreter (footnote-reference contents
)
2792 "Interpret FOOTNOTE-REFERENCE object as Org syntax.
2793 CONTENTS is its definition, when inline, or nil."
2795 (or (org-element-property :label footnote-reference
) "")
2796 (if contents
(concat ":" contents
) "")))
2799 ;;;; Inline Babel Call
2801 (defun org-element-inline-babel-call-parser ()
2802 "Parse inline babel call at point, if any.
2804 When at an inline babel call, return a list whose car is
2805 `inline-babel-call' and cdr a plist with `:call',
2806 `:inside-header', `:arguments', `:end-header', `:begin', `:end',
2807 `:value' and `:post-blank' as keywords. Otherwise, return nil.
2809 Assume point is at the beginning of the babel call."
2811 (unless (bolp) (backward-char))
2812 (when (let ((case-fold-search t
))
2813 (looking-at org-babel-inline-lob-one-liner-regexp
))
2814 (let ((begin (match-end 1))
2815 (call (org-match-string-no-properties 2))
2816 (inside-header (org-string-nw-p (org-match-string-no-properties 4)))
2817 (arguments (org-string-nw-p (org-match-string-no-properties 6)))
2818 (end-header (org-string-nw-p (org-match-string-no-properties 8)))
2819 (value (buffer-substring-no-properties (match-end 1) (match-end 0)))
2820 (post-blank (progn (goto-char (match-end 0))
2821 (skip-chars-forward " \t")))
2823 (list 'inline-babel-call
2825 :inside-header inside-header
2826 :arguments arguments
2827 :end-header end-header
2831 :post-blank post-blank
))))))
2833 (defun org-element-inline-babel-call-interpreter (inline-babel-call _
)
2834 "Interpret INLINE-BABEL-CALL object as Org syntax."
2836 (org-element-property :call inline-babel-call
)
2837 (let ((h (org-element-property :inside-header inline-babel-call
)))
2838 (and h
(format "[%s]" h
)))
2839 "(" (org-element-property :arguments inline-babel-call
) ")"
2840 (let ((h (org-element-property :end-header inline-babel-call
)))
2841 (and h
(format "[%s]" h
)))))
2844 ;;;; Inline Src Block
2846 (defun org-element-inline-src-block-parser ()
2847 "Parse inline source block at point, if any.
2849 When at an inline source block, return a list whose car is
2850 `inline-src-block' and cdr a plist with `:begin', `:end',
2851 `:language', `:value', `:parameters' and `:post-blank' as
2852 keywords. Otherwise, return nil.
2854 Assume point is at the beginning of the inline src block."
2856 (unless (bolp) (backward-char))
2857 (when (looking-at org-babel-inline-src-block-regexp
)
2858 (let ((begin (match-beginning 1))
2859 (language (org-match-string-no-properties 2))
2860 (parameters (org-match-string-no-properties 4))
2861 (value (org-match-string-no-properties 5))
2862 (post-blank (progn (goto-char (match-end 0))
2863 (skip-chars-forward " \t")))
2865 (list 'inline-src-block
2866 (list :language language
2868 :parameters parameters
2871 :post-blank post-blank
))))))
2873 (defun org-element-inline-src-block-interpreter (inline-src-block _
)
2874 "Interpret INLINE-SRC-BLOCK object as Org syntax."
2875 (let ((language (org-element-property :language inline-src-block
))
2876 (arguments (org-element-property :parameters inline-src-block
))
2877 (body (org-element-property :value inline-src-block
)))
2878 (format "src_%s%s{%s}"
2880 (if arguments
(format "[%s]" arguments
) "")
2885 (defun org-element-italic-parser ()
2886 "Parse italic object at point, if any.
2888 When at an italic object, return a list whose car is `italic' and
2889 cdr is a plist with `:begin', `:end', `:contents-begin' and
2890 `:contents-end' and `:post-blank' keywords. Otherwise, return
2893 Assume point is at the first slash marker."
2895 (unless (bolp) (backward-char 1))
2896 (when (looking-at org-emph-re
)
2897 (let ((begin (match-beginning 2))
2898 (contents-begin (match-beginning 4))
2899 (contents-end (match-end 4))
2900 (post-blank (progn (goto-char (match-end 2))
2901 (skip-chars-forward " \t")))
2906 :contents-begin contents-begin
2907 :contents-end contents-end
2908 :post-blank post-blank
))))))
2910 (defun org-element-italic-interpreter (_ contents
)
2911 "Interpret italic object as Org syntax.
2912 CONTENTS is the contents of the object."
2913 (format "/%s/" contents
))
2918 (defun org-element-latex-fragment-parser ()
2919 "Parse LaTeX fragment at point, if any.
2921 When at a LaTeX fragment, return a list whose car is
2922 `latex-fragment' and cdr a plist with `:value', `:begin', `:end',
2923 and `:post-blank' as keywords. Otherwise, return nil.
2925 Assume point is at the beginning of the LaTeX fragment."
2928 (let* ((begin (point))
2930 (if (eq (char-after) ?$
)
2931 (if (eq (char-after (1+ (point))) ?$
)
2932 (search-forward "$$" nil t
2)
2933 (and (not (eq (char-before) ?$
))
2934 (search-forward "$" nil t
2)
2935 (not (memq (char-before (match-beginning 0))
2936 '(?\s ?
\t ?
\n ?
, ?.
)))
2937 (looking-at "\\(\\s.\\|\\s-\\|\\s(\\|\\s)\\|\\s\"\\|$\\)")
2939 (pcase (char-after (1+ (point)))
2940 (?\
( (search-forward "\\)" nil t
))
2941 (?\
[ (search-forward "\\]" nil t
))
2944 (and (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\
2945 \\|\\({[^{}\n]*}\\)\\)*")
2947 (post-blank (if (not after-fragment
) (throw 'no-object nil
)
2948 (goto-char after-fragment
)
2949 (skip-chars-forward " \t")))
2951 (list 'latex-fragment
2952 (list :value
(buffer-substring-no-properties begin after-fragment
)
2955 :post-blank post-blank
))))))
2957 (defun org-element-latex-fragment-interpreter (latex-fragment _
)
2958 "Interpret LATEX-FRAGMENT object as Org syntax."
2959 (org-element-property :value latex-fragment
))
2963 (defun org-element-line-break-parser ()
2964 "Parse line break at point, if any.
2966 When at a line break, return a list whose car is `line-break',
2967 and cdr a plist with `:begin', `:end' and `:post-blank' keywords.
2968 Otherwise, return nil.
2970 Assume point is at the beginning of the line break."
2971 (when (and (org-looking-at-p "\\\\\\\\[ \t]*$")
2972 (not (eq (char-before) ?
\\)))
2974 (list :begin
(point)
2975 :end
(line-beginning-position 2)
2978 (defun org-element-line-break-interpreter (&rest _
)
2979 "Interpret LINE-BREAK object as Org syntax."
2985 (defun org-element-link-parser ()
2986 "Parse link at point, if any.
2988 When at a link, return a list whose car is `link' and cdr a plist
2989 with `:type', `:path', `:raw-link', `:application',
2990 `:search-option', `:begin', `:end', `:contents-begin',
2991 `:contents-end' and `:post-blank' as keywords. Otherwise, return
2994 Assume point is at the beginning of the link."
2996 (let ((begin (point))
2997 end contents-begin contents-end link-end post-blank path type
2998 raw-link search-option application
)
3000 ;; Type 1: Text targeted from a radio target.
3001 ((and org-target-link-regexp
3002 (save-excursion (or (bolp) (backward-char))
3003 (looking-at org-target-link-regexp
)))
3005 link-end
(match-end 1)
3006 path
(org-match-string-no-properties 1)
3007 contents-begin
(match-beginning 1)
3008 contents-end
(match-end 1)))
3009 ;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]]
3010 ((looking-at org-bracket-link-regexp
)
3011 (setq contents-begin
(match-beginning 3))
3012 (setq contents-end
(match-end 3))
3013 (setq link-end
(match-end 0))
3014 ;; RAW-LINK is the original link. Expand any
3015 ;; abbreviation in it.
3017 ;; Also treat any newline character and associated
3018 ;; indentation as a single space character. This is not
3019 ;; compatible with RFC 3986, which requires to ignore
3020 ;; them altogether. However, doing so would require
3021 ;; users to encode spaces on the fly when writing links
3022 ;; (e.g., insert [[shell:ls%20*.org]] instead of
3023 ;; [[shell:ls *.org]], which defeats Org's focus on
3025 (setq raw-link
(org-link-expand-abbrev
3026 (replace-regexp-in-string
3027 "[ \t]*\n[ \t]*" " "
3028 (org-match-string-no-properties 1))))
3029 ;; Determine TYPE of link and set PATH accordingly. According
3030 ;; to RFC 3986, remove whitespaces from URI in external links.
3031 ;; In internal ones, treat indentation as a single space.
3034 ((or (file-name-absolute-p raw-link
)
3035 (string-match "\\`\\.\\.?/" raw-link
))
3037 (setq path raw-link
))
3038 ;; Explicit type (http, irc, bbdb...). See `org-link-types'.
3039 ((string-match org-link-types-re raw-link
)
3040 (setq type
(match-string 1 raw-link
))
3041 (setq path
(substring raw-link
(match-end 0))))
3042 ;; Id type: PATH is the id.
3043 ((string-match "\\`id:\\([-a-f0-9]+\\)\\'" raw-link
)
3044 (setq type
"id" path
(match-string 1 raw-link
)))
3045 ;; Code-ref type: PATH is the name of the reference.
3046 ((and (org-string-match-p "\\`(" raw-link
)
3047 (org-string-match-p ")\\'" raw-link
))
3048 (setq type
"coderef")
3049 (setq path
(substring raw-link
1 -
1)))
3050 ;; Custom-id type: PATH is the name of the custom id.
3051 ((= (string-to-char raw-link
) ?
#)
3052 (setq type
"custom-id")
3053 (setq path
(substring raw-link
1)))
3054 ;; Fuzzy type: Internal link either matches a target, an
3055 ;; headline name or nothing. PATH is the target or
3059 (setq path raw-link
))))
3060 ;; Type 3: Plain link, e.g., http://orgmode.org
3061 ((looking-at org-plain-link-re
)
3062 (setq raw-link
(org-match-string-no-properties 0)
3063 type
(org-match-string-no-properties 1)
3064 link-end
(match-end 0)
3065 path
(org-match-string-no-properties 2)))
3066 ;; Type 4: Angular link, e.g., <http://orgmode.org>. Unlike to
3067 ;; bracket links, follow RFC 3986 and remove any extra
3068 ;; whitespace in URI.
3069 ((looking-at org-angle-link-re
)
3070 (setq type
(org-match-string-no-properties 1))
3071 (setq link-end
(match-end 0))
3073 (buffer-substring-no-properties
3074 (match-beginning 1) (match-end 2)))
3075 (setq path
(replace-regexp-in-string
3076 "[ \t]*\n[ \t]*" "" (org-match-string-no-properties 2))))
3077 (t (throw 'no-object nil
)))
3078 ;; In any case, deduce end point after trailing white space from
3079 ;; LINK-END variable.
3082 (progn (goto-char link-end
) (skip-chars-forward " \t")))
3084 ;; Special "file" type link processing. Extract opening
3085 ;; application and search option, if any. Also normalize URI.
3086 (when (string-match "\\`file\\(?:\\+\\(.+\\)\\)?\\'" type
)
3087 (setq application
(match-string 1 type
) type
"file")
3088 (when (string-match "::\\(.*\\)\\'" path
)
3089 (setq search-option
(match-string 1 path
))
3090 (setq path
(replace-match "" nil nil path
)))
3091 (setq path
(replace-regexp-in-string "\\`///+" "/" path
)))
3092 ;; Translate link, if `org-link-translation-function' is set.
3093 (let ((trans (and (functionp org-link-translation-function
)
3094 (funcall org-link-translation-function type path
))))
3096 (setq type
(car trans
))
3097 (setq path
(cdr trans
))))
3101 :raw-link
(or raw-link path
)
3102 :application application
3103 :search-option search-option
3106 :contents-begin contents-begin
3107 :contents-end contents-end
3108 :post-blank post-blank
)))))
3110 (defun org-element-link-interpreter (link contents
)
3111 "Interpret LINK object as Org syntax.
3112 CONTENTS is the contents of the object, or nil."
3113 (let ((type (org-element-property :type link
))
3114 (path (org-element-property :path link
)))
3115 (if (string= type
"radio") path
3117 (cond ((string= type
"coderef") (format "(%s)" path
))
3118 ((string= type
"custom-id") (concat "#" path
))
3119 ((string= type
"file")
3120 (let ((app (org-element-property :application link
))
3121 (opt (org-element-property :search-option link
)))
3122 (concat type
(and app
(concat "+" app
)) ":"
3124 (and opt
(concat "::" opt
)))))
3125 ((string= type
"fuzzy") path
)
3126 (t (concat type
":" path
)))
3127 (if contents
(format "[%s]" contents
) "")))))
3132 (defun org-element-macro-parser ()
3133 "Parse macro at point, if any.
3135 When at a macro, return a list whose car is `macro' and cdr
3136 a plist with `:key', `:args', `:begin', `:end', `:value' and
3137 `:post-blank' as keywords. Otherwise, return nil.
3139 Assume point is at the macro."
3141 (when (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
3142 (let ((begin (point))
3143 (key (downcase (org-match-string-no-properties 1)))
3144 (value (org-match-string-no-properties 0))
3145 (post-blank (progn (goto-char (match-end 0))
3146 (skip-chars-forward " \t")))
3148 (args (let ((args (org-match-string-no-properties 3)))
3149 (and args
(org-macro-extract-arguments args
)))))
3156 :post-blank post-blank
))))))
3158 (defun org-element-macro-interpreter (macro _
)
3159 "Interpret MACRO object as Org syntax."
3160 (org-element-property :value macro
))
3165 (defun org-element-radio-target-parser ()
3166 "Parse radio target at point, if any.
3168 When at a radio target, return a list whose car is `radio-target'
3169 and cdr a plist with `:begin', `:end', `:contents-begin',
3170 `:contents-end', `:value' and `:post-blank' as keywords.
3171 Otherwise, return nil.
3173 Assume point is at the radio target."
3175 (when (looking-at org-radio-target-regexp
)
3176 (let ((begin (point))
3177 (contents-begin (match-beginning 1))
3178 (contents-end (match-end 1))
3179 (value (org-match-string-no-properties 1))
3180 (post-blank (progn (goto-char (match-end 0))
3181 (skip-chars-forward " \t")))
3186 :contents-begin contents-begin
3187 :contents-end contents-end
3188 :post-blank post-blank
3191 (defun org-element-radio-target-interpreter (_ contents
)
3192 "Interpret target object as Org syntax.
3193 CONTENTS is the contents of the object."
3194 (concat "<<<" contents
">>>"))
3197 ;;;; Statistics Cookie
3199 (defun org-element-statistics-cookie-parser ()
3200 "Parse statistics cookie at point, if any.
3202 When at a statistics cookie, return a list whose car is
3203 `statistics-cookie', and cdr a plist with `:begin', `:end',
3204 `:value' and `:post-blank' keywords. Otherwise, return nil.
3206 Assume point is at the beginning of the statistics-cookie."
3208 (when (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
3209 (let* ((begin (point))
3210 (value (buffer-substring-no-properties
3211 (match-beginning 0) (match-end 0)))
3212 (post-blank (progn (goto-char (match-end 0))
3213 (skip-chars-forward " \t")))
3215 (list 'statistics-cookie
3219 :post-blank post-blank
))))))
3221 (defun org-element-statistics-cookie-interpreter (statistics-cookie _
)
3222 "Interpret STATISTICS-COOKIE object as Org syntax."
3223 (org-element-property :value statistics-cookie
))
3228 (defun org-element-strike-through-parser ()
3229 "Parse strike-through object at point, if any.
3231 When at a strike-through object, return a list whose car is
3232 `strike-through' and cdr is a plist with `:begin', `:end',
3233 `:contents-begin' and `:contents-end' and `:post-blank' keywords.
3234 Otherwise, return nil.
3236 Assume point is at the first plus sign marker."
3238 (unless (bolp) (backward-char 1))
3239 (when (looking-at org-emph-re
)
3240 (let ((begin (match-beginning 2))
3241 (contents-begin (match-beginning 4))
3242 (contents-end (match-end 4))
3243 (post-blank (progn (goto-char (match-end 2))
3244 (skip-chars-forward " \t")))
3246 (list 'strike-through
3249 :contents-begin contents-begin
3250 :contents-end contents-end
3251 :post-blank post-blank
))))))
3253 (defun org-element-strike-through-interpreter (_ contents
)
3254 "Interpret strike-through object as Org syntax.
3255 CONTENTS is the contents of the object."
3256 (format "+%s+" contents
))
3261 (defun org-element-subscript-parser ()
3262 "Parse subscript at point, if any.
3264 When at a subscript object, return a list whose car is
3265 `subscript' and cdr a plist with `:begin', `:end',
3266 `:contents-begin', `:contents-end', `:use-brackets-p' and
3267 `:post-blank' as keywords. Otherwise, return nil.
3269 Assume point is at the underscore."
3271 (unless (bolp) (backward-char))
3272 (when (looking-at org-match-substring-regexp
)
3273 (let ((bracketsp (match-beginning 4))
3274 (begin (match-beginning 2))
3275 (contents-begin (or (match-beginning 4)
3276 (match-beginning 3)))
3277 (contents-end (or (match-end 4) (match-end 3)))
3278 (post-blank (progn (goto-char (match-end 0))
3279 (skip-chars-forward " \t")))
3284 :use-brackets-p bracketsp
3285 :contents-begin contents-begin
3286 :contents-end contents-end
3287 :post-blank post-blank
))))))
3289 (defun org-element-subscript-interpreter (subscript contents
)
3290 "Interpret SUBSCRIPT object as Org syntax.
3291 CONTENTS is the contents of the object."
3293 (if (org-element-property :use-brackets-p subscript
) "_{%s}" "_%s")
3299 (defun org-element-superscript-parser ()
3300 "Parse superscript at point, if any.
3302 When at a superscript object, return a list whose car is
3303 `superscript' and cdr a plist with `:begin', `:end',
3304 `:contents-begin', `:contents-end', `:use-brackets-p' and
3305 `:post-blank' as keywords. Otherwise, return nil.
3307 Assume point is at the caret."
3309 (unless (bolp) (backward-char))
3310 (when (looking-at org-match-substring-regexp
)
3311 (let ((bracketsp (match-beginning 4))
3312 (begin (match-beginning 2))
3313 (contents-begin (or (match-beginning 4)
3314 (match-beginning 3)))
3315 (contents-end (or (match-end 4) (match-end 3)))
3316 (post-blank (progn (goto-char (match-end 0))
3317 (skip-chars-forward " \t")))
3322 :use-brackets-p bracketsp
3323 :contents-begin contents-begin
3324 :contents-end contents-end
3325 :post-blank post-blank
))))))
3327 (defun org-element-superscript-interpreter (superscript contents
)
3328 "Interpret SUPERSCRIPT object as Org syntax.
3329 CONTENTS is the contents of the object."
3331 (if (org-element-property :use-brackets-p superscript
) "^{%s}" "^%s")
3337 (defun org-element-table-cell-parser ()
3338 "Parse table cell at point.
3339 Return a list whose car is `table-cell' and cdr is a plist
3340 containing `:begin', `:end', `:contents-begin', `:contents-end'
3341 and `:post-blank' keywords."
3342 (looking-at "[ \t]*\\(.*?\\)[ \t]*\\(?:|\\|$\\)")
3343 (let* ((begin (match-beginning 0))
3345 (contents-begin (match-beginning 1))
3346 (contents-end (match-end 1)))
3350 :contents-begin contents-begin
3351 :contents-end contents-end
3354 (defun org-element-table-cell-interpreter (_ contents
)
3355 "Interpret table-cell element as Org syntax.
3356 CONTENTS is the contents of the cell, or nil."
3357 (concat " " contents
" |"))
3362 (defun org-element-target-parser ()
3363 "Parse target at point, if any.
3365 When at a target, return a list whose car is `target' and cdr
3366 a plist with `:begin', `:end', `:value' and `:post-blank' as
3367 keywords. Otherwise, return nil.
3369 Assume point is at the target."
3371 (when (looking-at org-target-regexp
)
3372 (let ((begin (point))
3373 (value (org-match-string-no-properties 1))
3374 (post-blank (progn (goto-char (match-end 0))
3375 (skip-chars-forward " \t")))
3381 :post-blank post-blank
))))))
3383 (defun org-element-target-interpreter (target _
)
3384 "Interpret TARGET object as Org syntax."
3385 (format "<<%s>>" (org-element-property :value target
)))
3390 (defconst org-element--timestamp-regexp
3391 (concat org-ts-regexp-both
3393 "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
3395 "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
3396 "Regexp matching any timestamp type object.")
3398 (defun org-element-timestamp-parser ()
3399 "Parse time stamp at point, if any.
3401 When at a time stamp, return a list whose car is `timestamp', and
3402 cdr a plist with `:type', `:raw-value', `:year-start',
3403 `:month-start', `:day-start', `:hour-start', `:minute-start',
3404 `:year-end', `:month-end', `:day-end', `:hour-end',
3405 `:minute-end', `:repeater-type', `:repeater-value',
3406 `:repeater-unit', `:warning-type', `:warning-value',
3407 `:warning-unit', `:begin', `:end' and `:post-blank' keywords.
3408 Otherwise, return nil.
3410 Assume point is at the beginning of the timestamp."
3411 (when (org-looking-at-p org-element--timestamp-regexp
)
3413 (let* ((begin (point))
3414 (activep (eq (char-after) ?
<))
3417 (looking-at "\\([<[]\\(%%\\)?.*?\\)[]>]\\(?:--\\([<[].*?[]>]\\)\\)?")
3418 (match-string-no-properties 0)))
3419 (date-start (match-string-no-properties 1))
3420 (date-end (match-string 3))
3421 (diaryp (match-beginning 2))
3422 (post-blank (progn (goto-char (match-end 0))
3423 (skip-chars-forward " \t")))
3428 "[012]?[0-9]:[0-5][0-9]\\(-\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)"
3430 (cons (string-to-number (match-string 2 date-start
))
3431 (string-to-number (match-string 3 date-start
)))))
3432 (type (cond (diaryp 'diary
)
3433 ((and activep
(or date-end time-range
)) 'active-range
)
3435 ((or date-end time-range
) 'inactive-range
)
3439 (string-match "\\([.+]?\\+\\)\\([0-9]+\\)\\([hdwmy]\\)"
3443 (let ((type (match-string 1 raw-value
)))
3444 (cond ((equal "++" type
) 'catch-up
)
3445 ((equal ".+" type
) 'restart
)
3447 :repeater-value
(string-to-number (match-string 2 raw-value
))
3449 (pcase (string-to-char (match-string 3 raw-value
))
3450 (?h
'hour
) (?d
'day
) (?w
'week
) (?m
'month
) (_ 'year
)))))
3453 (string-match "\\(-\\)?-\\([0-9]+\\)\\([hdwmy]\\)" raw-value
)
3455 :warning-type
(if (match-string 1 raw-value
) 'first
'all
)
3456 :warning-value
(string-to-number (match-string 2 raw-value
))
3458 (pcase (string-to-char (match-string 3 raw-value
))
3459 (?h
'hour
) (?d
'day
) (?w
'week
) (?m
'month
) (_ 'year
)))))
3460 year-start month-start day-start hour-start minute-start year-end
3461 month-end day-end hour-end minute-end
)
3462 ;; Parse date-start.
3464 (let ((date (org-parse-time-string date-start t
)))
3465 (setq year-start
(nth 5 date
)
3466 month-start
(nth 4 date
)
3467 day-start
(nth 3 date
)
3468 hour-start
(nth 2 date
)
3469 minute-start
(nth 1 date
))))
3470 ;; Compute date-end. It can be provided directly in time-stamp,
3471 ;; or extracted from time range. Otherwise, it defaults to the
3472 ;; same values as date-start.
3474 (let ((date (and date-end
(org-parse-time-string date-end t
))))
3475 (setq year-end
(or (nth 5 date
) year-start
)
3476 month-end
(or (nth 4 date
) month-start
)
3477 day-end
(or (nth 3 date
) day-start
)
3478 hour-end
(or (nth 2 date
) (car time-range
) hour-start
)
3479 minute-end
(or (nth 1 date
) (cdr time-range
) minute-start
))))
3481 (nconc (list :type type
3482 :raw-value raw-value
3483 :year-start year-start
3484 :month-start month-start
3485 :day-start day-start
3486 :hour-start hour-start
3487 :minute-start minute-start
3489 :month-end month-end
3492 :minute-end minute-end
3495 :post-blank post-blank
)
3499 (defun org-element-timestamp-interpreter (timestamp _
)
3500 "Interpret TIMESTAMP object as Org syntax."
3501 (let* ((repeat-string
3503 (pcase (org-element-property :repeater-type timestamp
)
3504 (`cumulate
"+") (`catch-up
"++") (`restart
".+"))
3505 (let ((val (org-element-property :repeater-value timestamp
)))
3506 (and val
(number-to-string val
)))
3507 (pcase (org-element-property :repeater-unit timestamp
)
3508 (`hour
"h") (`day
"d") (`week
"w") (`month
"m") (`year
"y"))))
3511 (pcase (org-element-property :warning-type timestamp
)
3512 (`first
"--") (`all
"-"))
3513 (let ((val (org-element-property :warning-value timestamp
)))
3514 (and val
(number-to-string val
)))
3515 (pcase (org-element-property :warning-unit timestamp
)
3516 (`hour
"h") (`day
"d") (`week
"w") (`month
"m") (`year
"y"))))
3518 ;; Build an Org timestamp string from TIME. ACTIVEP is
3519 ;; non-nil when time stamp is active. If WITH-TIME-P is
3520 ;; non-nil, add a time part. HOUR-END and MINUTE-END
3521 ;; specify a time range in the timestamp. REPEAT-STRING is
3522 ;; the repeater string, if any.
3523 (lambda (time activep
&optional with-time-p hour-end minute-end
)
3524 (let ((ts (format-time-string
3525 (funcall (if with-time-p
#'cdr
#'car
)
3526 org-time-stamp-formats
)
3528 (when (and hour-end minute-end
)
3529 (string-match "[012]?[0-9]:[0-5][0-9]" ts
)
3532 (format "\\&-%02d:%02d" hour-end minute-end
)
3534 (unless activep
(setq ts
(format "[%s]" (substring ts
1 -
1))))
3535 (dolist (s (list repeat-string warning-string
))
3536 (when (org-string-nw-p s
)
3537 (setq ts
(concat (substring ts
0 -
1)
3540 (substring ts -
1)))))
3543 (type (org-element-property :type timestamp
)))
3545 ((or `active
`inactive
)
3546 (let* ((minute-start (org-element-property :minute-start timestamp
))
3547 (minute-end (org-element-property :minute-end timestamp
))
3548 (hour-start (org-element-property :hour-start timestamp
))
3549 (hour-end (org-element-property :hour-end timestamp
))
3550 (time-range-p (and hour-start hour-end minute-start minute-end
3551 (or (/= hour-start hour-end
)
3552 (/= minute-start minute-end
)))))
3558 (org-element-property :day-start timestamp
)
3559 (org-element-property :month-start timestamp
)
3560 (org-element-property :year-start timestamp
))
3562 (and hour-start minute-start
)
3563 (and time-range-p hour-end
)
3564 (and time-range-p minute-end
))))
3565 ((or `active-range
`inactive-range
)
3566 (let ((minute-start (org-element-property :minute-start timestamp
))
3567 (minute-end (org-element-property :minute-end timestamp
))
3568 (hour-start (org-element-property :hour-start timestamp
))
3569 (hour-end (org-element-property :hour-end timestamp
)))
3572 build-ts-string
(encode-time
3576 (org-element-property :day-start timestamp
)
3577 (org-element-property :month-start timestamp
)
3578 (org-element-property :year-start timestamp
))
3579 (eq type
'active-range
)
3580 (and hour-start minute-start
))
3582 (funcall build-ts-string
3586 (org-element-property :day-end timestamp
)
3587 (org-element-property :month-end timestamp
)
3588 (org-element-property :year-end timestamp
))
3589 (eq type
'active-range
)
3590 (and hour-end minute-end
)))))
3591 (_ (org-element-property :raw-value timestamp
)))))
3596 (defun org-element-underline-parser ()
3597 "Parse underline object at point, if any.
3599 When at an underline object, return a list whose car is
3600 `underline' and cdr is a plist with `:begin', `:end',
3601 `:contents-begin' and `:contents-end' and `:post-blank' keywords.
3602 Otherwise, return nil.
3604 Assume point is at the first underscore marker."
3606 (unless (bolp) (backward-char 1))
3607 (when (looking-at org-emph-re
)
3608 (let ((begin (match-beginning 2))
3609 (contents-begin (match-beginning 4))
3610 (contents-end (match-end 4))
3611 (post-blank (progn (goto-char (match-end 2))
3612 (skip-chars-forward " \t")))
3617 :contents-begin contents-begin
3618 :contents-end contents-end
3619 :post-blank post-blank
))))))
3621 (defun org-element-underline-interpreter (_ contents
)
3622 "Interpret underline object as Org syntax.
3623 CONTENTS is the contents of the object."
3624 (format "_%s_" contents
))
3629 (defun org-element-verbatim-parser ()
3630 "Parse verbatim object at point, if any.
3632 When at a verbatim object, return a list whose car is `verbatim'
3633 and cdr is a plist with `:value', `:begin', `:end' and
3634 `:post-blank' keywords. Otherwise, return nil.
3636 Assume point is at the first equal sign marker."
3638 (unless (bolp) (backward-char 1))
3639 (when (looking-at org-emph-re
)
3640 (let ((begin (match-beginning 2))
3641 (value (org-match-string-no-properties 4))
3642 (post-blank (progn (goto-char (match-end 2))
3643 (skip-chars-forward " \t")))
3649 :post-blank post-blank
))))))
3651 (defun org-element-verbatim-interpreter (verbatim _
)
3652 "Interpret VERBATIM object as Org syntax."
3653 (format "=%s=" (org-element-property :value verbatim
)))
3657 ;;; Parsing Element Starting At Point
3659 ;; `org-element--current-element' is the core function of this section.
3660 ;; It returns the Lisp representation of the element starting at
3663 ;; `org-element--current-element' makes use of special modes. They
3664 ;; are activated for fixed element chaining (e.g., `plain-list' >
3665 ;; `item') or fixed conditional element chaining (e.g., `headline' >
3666 ;; `section'). Special modes are: `first-section', `item',
3667 ;; `node-property', `section' and `table-row'.
3669 (defun org-element--current-element (limit &optional granularity mode structure
)
3670 "Parse the element starting at point.
3672 Return value is a list like (TYPE PROPS) where TYPE is the type
3673 of the element and PROPS a plist of properties associated to the
3676 Possible types are defined in `org-element-all-elements'.
3678 LIMIT bounds the search.
3680 Optional argument GRANULARITY determines the depth of the
3681 recursion. Allowed values are `headline', `greater-element',
3682 `element', `object' or nil. When it is broader than `object' (or
3683 nil), secondary values will not be parsed, since they only
3686 Optional argument MODE, when non-nil, can be either
3687 `first-section', `section', `planning', `item', `node-property'
3690 If STRUCTURE isn't provided but MODE is set to `item', it will be
3693 This function assumes point is always at the beginning of the
3694 element it has to parse."
3696 (let ((case-fold-search t
)
3697 ;; Determine if parsing depth allows for secondary strings
3698 ;; parsing. It only applies to elements referenced in
3699 ;; `org-element-secondary-value-alist'.
3700 (raw-secondary-p (and granularity
(not (eq granularity
'object
)))))
3704 (org-element-item-parser limit structure raw-secondary-p
))
3706 ((eq mode
'table-row
) (org-element-table-row-parser limit
))
3708 ((eq mode
'node-property
) (org-element-node-property-parser limit
))
3710 ((org-with-limited-levels (org-at-heading-p))
3711 (org-element-headline-parser limit raw-secondary-p
))
3712 ;; Sections (must be checked after headline).
3713 ((eq mode
'section
) (org-element-section-parser limit
))
3714 ((eq mode
'first-section
)
3715 (org-element-section-parser
3716 (or (save-excursion (org-with-limited-levels (outline-next-heading)))
3719 ((and (eq mode
'planning
) (looking-at org-planning-line-re
))
3720 (org-element-planning-parser limit
))
3722 ((and (memq mode
'(planning property-drawer
))
3723 (looking-at org-property-drawer-re
))
3724 (org-element-property-drawer-parser limit
))
3725 ;; When not at bol, point is at the beginning of an item or
3726 ;; a footnote definition: next item is always a paragraph.
3727 ((not (bolp)) (org-element-paragraph-parser limit
(list (point))))
3729 ((looking-at org-clock-line-re
) (org-element-clock-parser limit
))
3732 (org-element-inlinetask-parser limit raw-secondary-p
))
3733 ;; From there, elements can have affiliated keywords.
3734 (t (let ((affiliated (org-element--collect-affiliated-keywords limit
)))
3736 ;; Jumping over affiliated keywords put point off-limits.
3737 ;; Parse them as regular keywords.
3738 ((and (cdr affiliated
) (>= (point) limit
))
3739 (goto-char (car affiliated
))
3740 (org-element-keyword-parser limit nil
))
3741 ;; LaTeX Environment.
3742 ((looking-at org-element--latex-begin-environment
)
3743 (org-element-latex-environment-parser limit affiliated
))
3744 ;; Drawer and Property Drawer.
3745 ((looking-at org-drawer-regexp
)
3746 (org-element-drawer-parser limit affiliated
))
3748 ((looking-at "[ \t]*:\\( \\|$\\)")
3749 (org-element-fixed-width-parser limit affiliated
))
3750 ;; Inline Comments, Blocks, Babel Calls, Dynamic Blocks and
3752 ((looking-at "[ \t]*#")
3753 (goto-char (match-end 0))
3755 ((looking-at "\\(?: \\|$\\)")
3757 (org-element-comment-parser limit affiliated
))
3758 ((looking-at "\\+BEGIN_\\(\\S-+\\)")
3760 (funcall (pcase (upcase (match-string 1))
3761 ("CENTER" #'org-element-center-block-parser
)
3762 ("COMMENT" #'org-element-comment-block-parser
)
3763 ("EXAMPLE" #'org-element-example-block-parser
)
3764 ("EXPORT" #'org-element-export-block-parser
)
3765 ("QUOTE" #'org-element-quote-block-parser
)
3766 ("SRC" #'org-element-src-block-parser
)
3767 ("VERSE" #'org-element-verse-block-parser
)
3768 (_ #'org-element-special-block-parser
))
3771 ((looking-at "\\+CALL:")
3773 (org-element-babel-call-parser limit affiliated
))
3774 ((looking-at "\\+BEGIN:? ")
3776 (org-element-dynamic-block-parser limit affiliated
))
3777 ((looking-at "\\+\\S-+:")
3779 (org-element-keyword-parser limit affiliated
))
3782 (org-element-paragraph-parser limit affiliated
))))
3783 ;; Footnote Definition.
3784 ((looking-at org-footnote-definition-re
)
3785 (org-element-footnote-definition-parser limit affiliated
))
3787 ((looking-at "[ \t]*-\\{5,\\}[ \t]*$")
3788 (org-element-horizontal-rule-parser limit affiliated
))
3791 (org-element-diary-sexp-parser limit affiliated
))
3793 ((looking-at "[ \t]*\\(|\\|\\+\\(-+\\+\\)+[ \t]*$\\)")
3794 (org-element-table-parser limit affiliated
))
3796 ((looking-at (org-item-re))
3797 (org-element-plain-list-parser
3799 (or structure
(org-element--list-struct limit
))))
3800 ;; Default element: Paragraph.
3801 (t (org-element-paragraph-parser limit affiliated
)))))))))
3804 ;; Most elements can have affiliated keywords. When looking for an
3805 ;; element beginning, we want to move before them, as they belong to
3806 ;; that element, and, in the meantime, collect information they give
3807 ;; into appropriate properties. Hence the following function.
3809 (defun org-element--collect-affiliated-keywords (limit)
3810 "Collect affiliated keywords from point down to LIMIT.
3812 Return a list whose CAR is the position at the first of them and
3813 CDR a plist of keywords and values and move point to the
3814 beginning of the first line after them.
3816 As a special case, if element doesn't start at the beginning of
3817 the line (e.g., a paragraph starting an item), CAR is current
3818 position of point and CDR is nil."
3819 (if (not (bolp)) (list (point))
3820 (let ((case-fold-search t
)
3822 ;; RESTRICT is the list of objects allowed in parsed
3824 (restrict (org-element-restriction 'keyword
))
3826 (while (and (< (point) limit
) (looking-at org-element--affiliated-re
))
3827 (let* ((raw-kwd (upcase (match-string 1)))
3828 ;; Apply translation to RAW-KWD. From there, KWD is
3829 ;; the official keyword.
3830 (kwd (or (cdr (assoc raw-kwd
3831 org-element-keyword-translation-alist
))
3833 ;; Find main value for any keyword.
3837 (buffer-substring-no-properties
3838 (match-end 0) (line-end-position)))))
3839 ;; PARSEDP is non-nil when keyword should have its
3841 (parsedp (member kwd org-element-parsed-keywords
))
3842 ;; If KWD is a dual keyword, find its secondary
3843 ;; value. Maybe parse it.
3844 (dualp (member kwd org-element-dual-keywords
))
3847 (let ((sec (org-match-string-no-properties 2)))
3848 (if (or (not sec
) (not parsedp
)) sec
3850 (org-element--parse-objects
3851 (match-beginning 2) (match-end 2) nil restrict
))))))
3852 ;; Attribute a property name to KWD.
3853 (kwd-sym (and kwd
(intern (concat ":" (downcase kwd
))))))
3854 ;; Now set final shape for VALUE.
3857 (org-element--parse-objects
3859 (progn (end-of-line) (skip-chars-backward " \t") (point))
3862 (setq value
(and (or value dual-value
) (cons value dual-value
))))
3863 (when (or (member kwd org-element-multiple-keywords
)
3864 ;; Attributes can always appear on multiple lines.
3865 (string-match "^ATTR_" kwd
))
3866 (setq value
(cons value
(plist-get output kwd-sym
))))
3867 ;; Eventually store the new value in OUTPUT.
3868 (setq output
(plist-put output kwd-sym value
))
3869 ;; Move to next keyword.
3871 ;; If affiliated keywords are orphaned: move back to first one.
3872 ;; They will be parsed as a paragraph.
3873 (when (looking-at "[ \t]*$") (goto-char origin
) (setq output nil
))
3875 (cons origin output
))))
3881 ;; The two major functions here are `org-element-parse-buffer', which
3882 ;; parses Org syntax inside the current buffer, taking into account
3883 ;; region, narrowing, or even visibility if specified, and
3884 ;; `org-element-parse-secondary-string', which parses objects within
3887 ;; The (almost) almighty `org-element-map' allows to apply a function
3888 ;; on elements or objects matching some type, and accumulate the
3889 ;; resulting values. In an export situation, it also skips unneeded
3890 ;; parts of the parse tree.
3892 (defun org-element-parse-buffer (&optional granularity visible-only
)
3893 "Recursively parse the buffer and return structure.
3894 If narrowing is in effect, only parse the visible part of the
3897 Optional argument GRANULARITY determines the depth of the
3898 recursion. It can be set to the following symbols:
3900 `headline' Only parse headlines.
3901 `greater-element' Don't recurse into greater elements excepted
3902 headlines and sections. Thus, elements
3903 parsed are the top-level ones.
3904 `element' Parse everything but objects and plain text.
3905 `object' Parse the complete buffer (default).
3907 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3910 An element or an objects is represented as a list with the
3911 pattern (TYPE PROPERTIES CONTENTS), where :
3913 TYPE is a symbol describing the element or object. See
3914 `org-element-all-elements' and `org-element-all-objects' for an
3915 exhaustive list of such symbols. One can retrieve it with
3916 `org-element-type' function.
3918 PROPERTIES is the list of attributes attached to the element or
3919 object, as a plist. Although most of them are specific to the
3920 element or object type, all types share `:begin', `:end',
3921 `:post-blank' and `:parent' properties, which respectively
3922 refer to buffer position where the element or object starts,
3923 ends, the number of white spaces or blank lines after it, and
3924 the element or object containing it. Properties values can be
3925 obtained by using `org-element-property' function.
3927 CONTENTS is a list of elements, objects or raw strings
3928 contained in the current element or object, when applicable.
3929 One can access them with `org-element-contents' function.
3931 The Org buffer has `org-data' as type and nil as properties.
3932 `org-element-map' function can be used to find specific elements
3933 or objects within the parse tree.
3935 This function assumes that current major mode is `org-mode'."
3937 (goto-char (point-min))
3938 (org-skip-whitespace)
3939 (org-element--parse-elements
3940 (point-at-bol) (point-max)
3941 ;; Start in `first-section' mode so text before the first
3942 ;; headline belongs to a section.
3943 'first-section nil granularity visible-only
(list 'org-data nil
))))
3945 (defun org-element-parse-secondary-string (string restriction
&optional parent
)
3946 "Recursively parse objects in STRING and return structure.
3948 RESTRICTION is a symbol limiting the object types that will be
3951 Optional argument PARENT, when non-nil, is the element or object
3952 containing the secondary string. It is used to set correctly
3953 `:parent' property within the string.
3955 If STRING is the empty string or nil, return nil."
3958 ((equal string
"") nil
)
3959 (t (let ((local-variables (buffer-local-variables)))
3961 (dolist (v local-variables
)
3963 (if (symbolp v
) (makunbound v
)
3964 (set (make-local-variable (car v
)) (cdr v
)))))
3966 (restore-buffer-modified-p nil
)
3967 (let ((data (org-element--parse-objects
3968 (point-min) (point-max) nil restriction
)))
3970 (dolist (o data
) (org-element-put-property o
:parent parent
)))
3973 (defun org-element-map
3974 (data types fun
&optional info first-match no-recursion with-affiliated
)
3975 "Map a function on selected elements or objects.
3977 DATA is a parse tree, an element, an object, a string, or a list
3978 of such constructs. TYPES is a symbol or list of symbols of
3979 elements or objects types (see `org-element-all-elements' and
3980 `org-element-all-objects' for a complete list of types). FUN is
3981 the function called on the matching element or object. It has to
3982 accept one argument: the element or object itself.
3984 When optional argument INFO is non-nil, it should be a plist
3985 holding export options. In that case, parts of the parse tree
3986 not exportable according to that property list will be skipped.
3988 When optional argument FIRST-MATCH is non-nil, stop at the first
3989 match for which FUN doesn't return nil, and return that value.
3991 Optional argument NO-RECURSION is a symbol or a list of symbols
3992 representing elements or objects types. `org-element-map' won't
3993 enter any recursive element or object whose type belongs to that
3994 list. Though, FUN can still be applied on them.
3996 When optional argument WITH-AFFILIATED is non-nil, FUN will also
3997 apply to matching objects within parsed affiliated keywords (see
3998 `org-element-parsed-keywords').
4000 Nil values returned from FUN do not appear in the results.
4006 Assuming TREE is a variable containing an Org buffer parse tree,
4007 the following example will return a flat list of all `src-block'
4008 and `example-block' elements in it:
4010 (org-element-map tree \\='(example-block src-block) #\\='identity)
4012 The following snippet will find the first headline with a level
4013 of 1 and a \"phone\" tag, and will return its beginning position:
4015 (org-element-map tree \\='headline
4017 (and (= (org-element-property :level hl) 1)
4018 (member \"phone\" (org-element-property :tags hl))
4019 (org-element-property :begin hl)))
4022 The next example will return a flat list of all `plain-list' type
4023 elements in TREE that are not a sub-list themselves:
4025 (org-element-map tree \\='plain-list #\\='identity nil nil \\='plain-list)
4027 Eventually, this example will return a flat list of all `bold'
4028 type objects containing a `latex-snippet' type object, even
4029 looking into captions:
4031 (org-element-map tree \\='bold
4033 (and (org-element-map b \\='latex-snippet #\\='identity nil t) b))
4035 ;; Ensure TYPES and NO-RECURSION are a list, even of one element.
4036 (let* ((types (if (listp types
) types
(list types
)))
4037 (no-recursion (if (listp no-recursion
) no-recursion
4038 (list no-recursion
)))
4039 ;; Recursion depth is determined by --CATEGORY.
4042 (let ((category 'greater-elements
)
4043 (all-objects (cons 'plain-text org-element-all-objects
)))
4044 (dolist (type types category
)
4045 (cond ((memq type all-objects
)
4046 ;; If one object is found, the function has
4047 ;; to recurse into every object.
4048 (throw :--found
'objects
))
4049 ((not (memq type org-element-greater-elements
))
4050 ;; If one regular element is found, the
4051 ;; function has to recurse, at least, into
4052 ;; every element it encounters.
4053 (and (not (eq category
'elements
))
4054 (setq category
'elements
))))))))
4056 (letrec ((--walk-tree
4058 ;; Recursively walk DATA. INFO, if non-nil, is a plist
4059 ;; holding contextual information.
4060 (let ((--type (org-element-type --data
)))
4063 ;; Ignored element in an export context.
4064 ((and info
(memq --data
(plist-get info
:ignore-list
))))
4065 ;; List of elements or objects.
4066 ((not --type
) (mapc --walk-tree --data
))
4067 ;; Unconditionally enter parse trees.
4068 ((eq --type
'org-data
)
4069 (mapc --walk-tree
(org-element-contents --data
)))
4071 ;; Check if TYPE is matching among TYPES. If so,
4072 ;; apply FUN to --DATA and accumulate return value
4073 ;; into --ACC (or exit if FIRST-MATCH is non-nil).
4074 (when (memq --type types
)
4075 (let ((result (funcall fun --data
)))
4076 (cond ((not result
))
4077 (first-match (throw :--map-first-match result
))
4078 (t (push result --acc
)))))
4079 ;; If --DATA has a secondary string that can contain
4080 ;; objects with their type among TYPES, look inside.
4081 (when (and (eq --category
'objects
) (not (stringp --data
)))
4082 (dolist (p (cdr (assq --type
4083 org-element-secondary-value-alist
)))
4084 (funcall --walk-tree
(org-element-property p --data
))))
4085 ;; If --DATA has any parsed affiliated keywords and
4086 ;; WITH-AFFILIATED is non-nil, look for objects in
4088 (when (and with-affiliated
4089 (eq --category
'objects
)
4090 (memq --type org-element-all-elements
))
4091 (dolist (kwd-pair org-element--parsed-properties-alist
)
4092 (let ((kwd (car kwd-pair
))
4093 (value (org-element-property (cdr kwd-pair
) --data
)))
4094 ;; Pay attention to the type of parsed
4095 ;; keyword. In particular, preserve order for
4096 ;; multiple keywords.
4099 ((member kwd org-element-dual-keywords
)
4100 (if (member kwd org-element-multiple-keywords
)
4101 (dolist (line (reverse value
))
4102 (funcall --walk-tree
(cdr line
))
4103 (funcall --walk-tree
(car line
)))
4104 (funcall --walk-tree
(cdr value
))
4105 (funcall --walk-tree
(car value
))))
4106 ((member kwd org-element-multiple-keywords
)
4107 (mapc --walk-tree
(reverse value
)))
4108 (t (funcall --walk-tree value
))))))
4109 ;; Determine if a recursion into --DATA is possible.
4111 ;; --TYPE is explicitly removed from recursion.
4112 ((memq --type no-recursion
))
4113 ;; --DATA has no contents.
4114 ((not (org-element-contents --data
)))
4115 ;; Looking for greater elements but --DATA is
4116 ;; simply an element or an object.
4117 ((and (eq --category
'greater-elements
)
4118 (not (memq --type org-element-greater-elements
))))
4119 ;; Looking for elements but --DATA is an object.
4120 ((and (eq --category
'elements
)
4121 (memq --type org-element-all-objects
)))
4122 ;; In any other case, map contents.
4123 (t (mapc --walk-tree
(org-element-contents --data
))))))))))
4124 (catch :--map-first-match
4125 (funcall --walk-tree data
)
4126 ;; Return value in a proper order.
4127 (nreverse --acc
)))))
4128 (put 'org-element-map
'lisp-indent-function
2)
4130 ;; The following functions are internal parts of the parser.
4132 ;; The first one, `org-element--parse-elements' acts at the element's
4135 ;; The second one, `org-element--parse-objects' applies on all objects
4136 ;; of a paragraph or a secondary string. It calls
4137 ;; `org-element--object-lex' to find the next object in the current
4140 (defsubst org-element--next-mode
(type parentp
)
4141 "Return next special mode according to TYPE, or nil.
4142 TYPE is a symbol representing the type of an element or object
4143 containing next element if PARENTP is non-nil, or before it
4144 otherwise. Modes can be either `first-section', `item',
4145 `node-property', `planning', `property-drawer', `section',
4146 `table-row' or nil."
4149 (`headline
'section
)
4150 (`inlinetask
'planning
)
4152 (`property-drawer
'node-property
)
4153 (`section
'planning
)
4154 (`table
'table-row
))
4157 (`node-property
'node-property
)
4158 (`planning
'property-drawer
)
4159 (`table-row
'table-row
))))
4161 (defun org-element--parse-elements
4162 (beg end mode structure granularity visible-only acc
)
4163 "Parse elements between BEG and END positions.
4165 MODE prioritizes some elements over the others. It can be set to
4166 `first-section', `section', `planning', `item', `node-property'
4169 When value is `item', STRUCTURE will be used as the current list
4172 GRANULARITY determines the depth of the recursion. See
4173 `org-element-parse-buffer' for more information.
4175 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
4178 Elements are accumulated into ACC."
4181 ;; Visible only: skip invisible parts at the beginning of the
4183 (when (and visible-only
(org-invisible-p2))
4184 (goto-char (min (1+ (org-find-visible)) end
)))
4185 ;; When parsing only headlines, skip any text before first one.
4186 (when (and (eq granularity
'headline
) (not (org-at-heading-p)))
4187 (org-with-limited-levels (outline-next-heading)))
4189 (while (< (point) end
)
4190 ;; Find current element's type and parse it accordingly to
4192 (let* ((element (org-element--current-element
4193 end granularity mode structure
))
4194 (type (org-element-type element
))
4195 (cbeg (org-element-property :contents-begin element
)))
4196 (goto-char (org-element-property :end element
))
4197 ;; Visible only: skip invisible parts between siblings.
4198 (when (and visible-only
(org-invisible-p2))
4199 (goto-char (min (1+ (org-find-visible)) end
)))
4200 ;; Fill ELEMENT contents by side-effect.
4202 ;; If element has no contents, don't modify it.
4204 ;; Greater element: parse it between `contents-begin' and
4205 ;; `contents-end'. Make sure GRANULARITY allows the
4206 ;; recursion, or ELEMENT is a headline, in which case going
4207 ;; inside is mandatory, in order to get sub-level headings.
4208 ((and (memq type org-element-greater-elements
)
4209 (or (memq granularity
'(element object nil
))
4210 (and (eq granularity
'greater-element
)
4212 (eq type
'headline
)))
4213 (org-element--parse-elements
4214 cbeg
(org-element-property :contents-end element
)
4215 ;; Possibly switch to a special mode.
4216 (org-element--next-mode type t
)
4217 (and (memq type
'(item plain-list
))
4218 (org-element-property :structure element
))
4219 granularity visible-only element
))
4220 ;; ELEMENT has contents. Parse objects inside, if
4221 ;; GRANULARITY allows it.
4222 ((memq granularity
'(object nil
))
4223 (org-element--parse-objects
4224 cbeg
(org-element-property :contents-end element
) element
4225 (org-element-restriction type
))))
4226 (org-element-adopt-elements acc element
)
4228 (setq mode
(org-element--next-mode type nil
))))
4232 (defun org-element--object-lex (restriction)
4233 "Return next object in current buffer or nil.
4234 RESTRICTION is a list of object types, as symbols, that should be
4235 looked after. This function assumes that the buffer is narrowed
4236 to an appropriate container (e.g., a paragraph)."
4237 (if (memq 'table-cell restriction
) (org-element-table-cell-parser)
4239 (let ((limit (and org-target-link-regexp
4241 (or (bolp) (backward-char))
4242 (re-search-forward org-target-link-regexp nil t
))
4243 (match-beginning 1)))
4245 (while (and (not found
)
4246 (re-search-forward org-element--object-regexp limit t
))
4247 (goto-char (match-beginning 0))
4248 (let ((result (match-string 0)))
4251 ((eq (compare-strings result nil nil
"call_" nil nil t
) t
)
4252 (and (memq 'inline-babel-call restriction
)
4253 (org-element-inline-babel-call-parser)))
4254 ((eq (compare-strings result nil nil
"src_" nil nil t
) t
)
4255 (and (memq 'inline-src-block restriction
)
4256 (org-element-inline-src-block-parser)))
4259 (?^
(and (memq 'superscript restriction
)
4260 (org-element-superscript-parser)))
4261 (?_
(or (and (memq 'subscript restriction
)
4262 (org-element-subscript-parser))
4263 (and (memq 'underline restriction
)
4264 (org-element-underline-parser))))
4265 (?
* (and (memq 'bold restriction
)
4266 (org-element-bold-parser)))
4267 (?
/ (and (memq 'italic restriction
)
4268 (org-element-italic-parser)))
4269 (?~
(and (memq 'code restriction
)
4270 (org-element-code-parser)))
4271 (?
= (and (memq 'verbatim restriction
)
4272 (org-element-verbatim-parser)))
4273 (?
+ (and (memq 'strike-through restriction
)
4274 (org-element-strike-through-parser)))
4275 (?
@ (and (memq 'export-snippet restriction
)
4276 (org-element-export-snippet-parser)))
4277 (?
{ (and (memq 'macro restriction
)
4278 (org-element-macro-parser)))
4279 (?$
(and (memq 'latex-fragment restriction
)
4280 (org-element-latex-fragment-parser)))
4282 (if (eq (aref result
1) ?
<)
4283 (or (and (memq 'radio-target restriction
)
4284 (org-element-radio-target-parser))
4285 (and (memq 'target restriction
)
4286 (org-element-target-parser)))
4287 (or (and (memq 'timestamp restriction
)
4288 (org-element-timestamp-parser))
4289 (and (memq 'link restriction
)
4290 (org-element-link-parser)))))
4292 (if (eq (aref result
1) ?
\\)
4293 (and (memq 'line-break restriction
)
4294 (org-element-line-break-parser))
4295 (or (and (memq 'entity restriction
)
4296 (org-element-entity-parser))
4297 (and (memq 'latex-fragment restriction
)
4298 (org-element-latex-fragment-parser)))))
4300 (if (eq (aref result
1) ?\
[)
4301 (and (memq 'link restriction
)
4302 (org-element-link-parser))
4303 (or (and (memq 'footnote-reference restriction
)
4304 (org-element-footnote-reference-parser))
4305 (and (memq 'timestamp restriction
)
4306 (org-element-timestamp-parser))
4307 (and (memq 'statistics-cookie restriction
)
4308 (org-element-statistics-cookie-parser)))))
4309 ;; This is probably a plain link.
4310 (_ (and (or (memq 'link restriction
)
4311 (memq 'plain-link restriction
))
4312 (org-element-link-parser)))))))
4313 (or (eobp) (forward-char))))
4316 ((and limit
(memq 'link restriction
))
4317 (goto-char limit
) (org-element-link-parser)))))))
4319 (defun org-element--parse-objects (beg end acc restriction
)
4320 "Parse objects between BEG and END and return recursive structure.
4322 Objects are accumulated in ACC.
4324 RESTRICTION is a list of object successors which are allowed in
4325 the current object."
4328 (narrow-to-region beg end
)
4329 (goto-char (point-min))
4331 (while (and (not (eobp))
4332 (setq next-object
(org-element--object-lex restriction
)))
4333 ;; 1. Text before any object. Untabify it.
4334 (let ((obj-beg (org-element-property :begin next-object
)))
4335 (unless (= (point) obj-beg
)
4337 (org-element-adopt-elements
4339 (replace-regexp-in-string
4340 "\t" (make-string tab-width ?\s
)
4341 (buffer-substring-no-properties (point) obj-beg
))))))
4343 (let ((obj-end (org-element-property :end next-object
))
4344 (cont-beg (org-element-property :contents-begin next-object
)))
4345 ;; Fill contents of NEXT-OBJECT by side-effect, if it has
4346 ;; a recursive type.
4348 (memq (car next-object
) org-element-recursive-objects
))
4349 (org-element--parse-objects
4350 cont-beg
(org-element-property :contents-end next-object
)
4351 next-object
(org-element-restriction next-object
)))
4352 (setq acc
(org-element-adopt-elements acc next-object
))
4353 (goto-char obj-end
))))
4354 ;; 3. Text after last object. Untabify it.
4357 (org-element-adopt-elements
4359 (replace-regexp-in-string
4360 "\t" (make-string tab-width ?\s
)
4361 (buffer-substring-no-properties (point) end
)))))
4367 ;;; Towards A Bijective Process
4369 ;; The parse tree obtained with `org-element-parse-buffer' is really
4370 ;; a snapshot of the corresponding Org buffer. Therefore, it can be
4371 ;; interpreted and expanded into a string with canonical Org syntax.
4372 ;; Hence `org-element-interpret-data'.
4374 ;; The function relies internally on
4375 ;; `org-element--interpret-affiliated-keywords'.
4378 (defun org-element-interpret-data (data)
4379 "Interpret DATA as Org syntax.
4380 DATA is a parse tree, an element, an object or a secondary string
4381 to interpret. Return Org syntax as a string."
4383 (lambda (--data parent
)
4384 (let* ((type (org-element-type --data
))
4385 ;; Find interpreter for current object or
4386 ;; element. If it doesn't exist (e.g. this is
4387 ;; a pseudo object or element), return contents,
4391 (format "org-element-%s-interpreter" type
))))
4392 (if (fboundp fun
) fun
(lambda (_ contents
) contents
))))
4395 ;; Secondary string.
4397 (mapconcat (lambda (obj) (funcall fun obj parent
))
4399 ;; Full Org document.
4400 ((eq type
'org-data
)
4401 (mapconcat (lambda (obj) (funcall fun obj parent
))
4402 (org-element-contents --data
) ""))
4403 ;; Plain text: return it.
4404 ((stringp --data
) --data
)
4405 ;; Element or object without contents.
4406 ((not (org-element-contents --data
))
4407 (funcall interpret --data nil
))
4408 ;; Element or object with contents.
4413 ;; Recursively interpret contents.
4415 (lambda (obj) (funcall fun obj --data
))
4416 (org-element-contents
4417 (if (not (memq type
'(paragraph verse-block
)))
4419 ;; Fix indentation of elements containing
4420 ;; objects. We ignore `table-row'
4421 ;; elements as they are one line long
4423 (org-element-normalize-contents
4425 ;; When normalizing first paragraph of
4426 ;; an item or a footnote-definition,
4427 ;; ignore first line's indentation.
4428 (and (eq type
'paragraph
)
4430 (car (org-element-contents parent
)))
4431 (memq (org-element-type parent
)
4432 '(footnote-definition item
))))))
4434 (if (memq type
'(org-data plain-text nil
)) results
4435 ;; Build white spaces. If no `:post-blank' property
4436 ;; is specified, assume its value is 0.
4437 (let ((blank (or (org-element-property :post-blank --data
) 0)))
4438 (if (or (memq type org-element-all-objects
)
4440 (let ((type (org-element-type parent
)))
4442 (memq type org-element-object-containers
)))))
4443 (concat results
(make-string blank ?\s
))
4445 (org-element--interpret-affiliated-keywords --data
)
4446 (org-element-normalize-string results
)
4447 (make-string blank ?
\n)))))))))
4448 (funcall fun data nil
)))
4450 (defun org-element--interpret-affiliated-keywords (element)
4451 "Return ELEMENT's affiliated keywords as Org syntax.
4452 If there is no affiliated keyword, return the empty string."
4453 (let ((keyword-to-org
4457 (when (member key org-element-dual-keywords
)
4458 (setq dual
(cdr value
) value
(car value
)))
4461 (format "[%s]" (org-element-interpret-data dual
)))
4463 (if (member key org-element-parsed-keywords
)
4464 (org-element-interpret-data value
)
4469 (let ((value (org-element-property prop element
))
4470 (keyword (upcase (substring (symbol-name prop
) 1))))
4472 (if (or (member keyword org-element-multiple-keywords
)
4473 ;; All attribute keywords can have multiple lines.
4474 (string-match "^ATTR_" keyword
))
4475 (mapconcat (lambda (line) (funcall keyword-to-org keyword line
))
4478 (funcall keyword-to-org keyword value
)))))
4479 ;; List all ELEMENT's properties matching an attribute line or an
4480 ;; affiliated keyword, but ignore translated keywords since they
4481 ;; cannot belong to the property list.
4482 (loop for prop in
(nth 1 element
) by
'cddr
4483 when
(let ((keyword (upcase (substring (symbol-name prop
) 1))))
4484 (or (string-match "^ATTR_" keyword
)
4486 (member keyword org-element-affiliated-keywords
)
4488 org-element-keyword-translation-alist
)))))
4492 ;; Because interpretation of the parse tree must return the same
4493 ;; number of blank lines between elements and the same number of white
4494 ;; space after objects, some special care must be given to white
4497 ;; The first function, `org-element-normalize-string', ensures any
4498 ;; string different from the empty string will end with a single
4499 ;; newline character.
4501 ;; The second function, `org-element-normalize-contents', removes
4502 ;; global indentation from the contents of the current element.
4504 (defun org-element-normalize-string (s)
4505 "Ensure string S ends with a single newline character.
4507 If S isn't a string return it unchanged. If S is the empty
4508 string, return it. Otherwise, return a new string with a single
4509 newline character at its end."
4511 ((not (stringp s
)) s
)
4513 (t (and (string-match "\\(\n[ \t]*\\)*\\'" s
)
4514 (replace-match "\n" nil nil s
)))))
4516 (defun org-element-normalize-contents (element &optional ignore-first
)
4517 "Normalize plain text in ELEMENT's contents.
4519 ELEMENT must only contain plain text and objects.
4521 If optional argument IGNORE-FIRST is non-nil, ignore first line's
4522 indentation to compute maximal common indentation.
4524 Return the normalized element that is element with global
4525 indentation removed from its contents. The function assumes that
4526 indentation is not done with TAB characters."
4527 (letrec ((find-min-ind
4528 ;; Return minimal common indentation within BLOB. This is
4529 ;; done by walking recursively BLOB and updating MIN-IND
4530 ;; along the way. FIRST-FLAG is non-nil when the next
4531 ;; object is expected to be a string that doesn't start
4532 ;; with a newline character. It happens for strings at
4533 ;; the beginnings of the contents or right after a line
4535 (lambda (blob first-flag min-ind
)
4537 (dolist (object (org-element-contents blob
) min-ind
)
4539 (setq first-flag nil
)
4540 ;; Objects cannot start with spaces: in this case,
4541 ;; indentation is 0.
4542 (if (not (stringp object
)) (throw 'zero
0)
4543 (string-match "\\` *" object
)
4544 (let ((len (match-end 0)))
4545 ;; An indentation of zero means no string will
4546 ;; be modified. Quit the process.
4547 (if (zerop len
) (throw 'zero
0)
4548 (setq min-ind
(min len min-ind
))))))
4551 (dolist (line (cdr (org-split-string object
" *\n")))
4552 (unless (string= line
"")
4554 (min (org-get-indentation line
) min-ind
)))))
4555 ((eq (org-element-type object
) 'line-break
)
4556 (setq first-flag t
))
4557 ((memq (org-element-type object
)
4558 org-element-recursive-objects
)
4560 (funcall find-min-ind
4561 object first-flag min-ind
))))))))
4562 (min-ind (funcall find-min-ind
4563 element
(not ignore-first
) most-positive-fixnum
)))
4564 (if (or (zerop min-ind
) (= min-ind most-positive-fixnum
)) element
4565 ;; Build ELEMENT back, replacing each string with the same
4566 ;; string minus common indentation.
4568 (lambda (datum first-flag
)
4569 ;; Return DATUM with all its strings indentation
4570 ;; shortened from MIN-IND white spaces.
4571 ;; FIRST-FLAG is non-nil when the next object is
4572 ;; expected to be a string that doesn't start with
4573 ;; a newline character.
4578 (setq first-flag nil
)
4579 (when (stringp object
)
4581 (replace-regexp-in-string
4582 (format "\\` \\{%d\\}" min-ind
)
4586 (replace-regexp-in-string
4587 (format "\n \\{%d\\}" min-ind
) "\n" object
))
4588 ((memq (org-element-type object
)
4589 org-element-recursive-objects
)
4590 (funcall build object first-flag
))
4591 ((eq (org-element-type object
) 'line-break
)
4595 (org-element-contents datum
)))
4597 (funcall build element
(not ignore-first
))))))
4603 ;; Implement a caching mechanism for `org-element-at-point' and
4604 ;; `org-element-context', which see.
4606 ;; A single public function is provided: `org-element-cache-reset'.
4608 ;; Cache is enabled by default, but can be disabled globally with
4609 ;; `org-element-use-cache'. `org-element-cache-sync-idle-time',
4610 ;; org-element-cache-sync-duration' and `org-element-cache-sync-break'
4611 ;; can be tweaked to control caching behaviour.
4613 ;; Internally, parsed elements are stored in an AVL tree,
4614 ;; `org-element--cache'. This tree is updated lazily: whenever
4615 ;; a change happens to the buffer, a synchronization request is
4616 ;; registered in `org-element--cache-sync-requests' (see
4617 ;; `org-element--cache-submit-request'). During idle time, requests
4618 ;; are processed by `org-element--cache-sync'. Synchronization also
4619 ;; happens when an element is required from the cache. In this case,
4620 ;; the process stops as soon as the needed element is up-to-date.
4622 ;; A synchronization request can only apply on a synchronized part of
4623 ;; the cache. Therefore, the cache is updated at least to the
4624 ;; location where the new request applies. Thus, requests are ordered
4625 ;; from left to right and all elements starting before the first
4626 ;; request are correct. This property is used by functions like
4627 ;; `org-element--cache-find' to retrieve elements in the part of the
4628 ;; cache that can be trusted.
4630 ;; A request applies to every element, starting from its original
4631 ;; location (or key, see below). When a request is processed, it
4632 ;; moves forward and may collide the next one. In this case, both
4633 ;; requests are merged into a new one that starts from that element.
4634 ;; As a consequence, the whole synchronization complexity does not
4635 ;; depend on the number of pending requests, but on the number of
4636 ;; elements the very first request will be applied on.
4638 ;; Elements cannot be accessed through their beginning position, which
4639 ;; may or may not be up-to-date. Instead, each element in the tree is
4640 ;; associated to a key, obtained with `org-element--cache-key'. This
4641 ;; mechanism is robust enough to preserve total order among elements
4642 ;; even when the tree is only partially synchronized.
4644 ;; Objects contained in an element are stored in a hash table,
4645 ;; `org-element--cache-objects'.
4648 (defvar org-element-use-cache t
4649 "Non nil when Org parser should cache its results.
4650 This is mostly for debugging purpose.")
4652 (defvar org-element-cache-sync-idle-time
0.6
4653 "Length, in seconds, of idle time before syncing cache.")
4655 (defvar org-element-cache-sync-duration
(seconds-to-time 0.04)
4656 "Maximum duration, as a time value, for a cache synchronization.
4657 If the synchronization is not over after this delay, the process
4658 pauses and resumes after `org-element-cache-sync-break'
4661 (defvar org-element-cache-sync-break
(seconds-to-time 0.3)
4662 "Duration, as a time value, of the pause between synchronizations.
4663 See `org-element-cache-sync-duration' for more information.")
4668 (defvar org-element--cache nil
4669 "AVL tree used to cache elements.
4670 Each node of the tree contains an element. Comparison is done
4671 with `org-element--cache-compare'. This cache is used in
4672 `org-element-at-point'.")
4674 (defvar org-element--cache-objects nil
4675 "Hash table used as to cache objects.
4676 Key is an element, as returned by `org-element-at-point', and
4677 value is an alist where each association is:
4679 \(PARENT COMPLETEP . OBJECTS)
4681 where PARENT is an element or object, COMPLETEP is a boolean,
4682 non-nil when all direct children of parent are already cached and
4683 OBJECTS is a list of such children, as objects, from farthest to
4686 In the following example, \\alpha, bold object and \\beta are
4687 contained within a paragraph
4691 If the paragraph is completely parsed, OBJECTS-DATA will be
4693 \((PARAGRAPH t BOLD-OBJECT ENTITY-OBJECT)
4694 \(BOLD-OBJECT t ENTITY-OBJECT))
4696 whereas in a partially parsed paragraph, it could be
4698 \((PARAGRAPH nil ENTITY-OBJECT))
4700 This cache is used in `org-element-context'.")
4702 (defvar org-element--cache-sync-requests nil
4703 "List of pending synchronization requests.
4705 A request is a vector with the following pattern:
4707 \[NEXT BEG END OFFSET PARENT PHASE]
4709 Processing a synchronization request consists of three phases:
4711 0. Delete modified elements,
4712 1. Fill missing area in cache,
4713 2. Shift positions and re-parent elements after the changes.
4715 During phase 0, NEXT is the key of the first element to be
4716 removed, BEG and END is buffer position delimiting the
4717 modifications. Elements starting between them (inclusive) are
4718 removed. So are elements whose parent is removed. PARENT, when
4719 non-nil, is the parent of the first element to be removed.
4721 During phase 1, NEXT is the key of the next known element in
4722 cache and BEG its beginning position. Parse buffer between that
4723 element and the one before it in order to determine the parent of
4724 the next element. Set PARENT to the element containing NEXT.
4726 During phase 2, NEXT is the key of the next element to shift in
4727 the parse tree. All elements starting from this one have their
4728 properties relatives to buffer positions shifted by integer
4729 OFFSET and, if they belong to element PARENT, are adopted by it.
4731 PHASE specifies the phase number, as an integer.")
4733 (defvar org-element--cache-sync-timer nil
4734 "Timer used for cache synchronization.")
4736 (defvar org-element--cache-sync-keys nil
4737 "Hash table used to store keys during synchronization.
4738 See `org-element--cache-key' for more information.")
4740 (defsubst org-element--cache-key
(element)
4741 "Return a unique key for ELEMENT in cache tree.
4743 Keys are used to keep a total order among elements in the cache.
4744 Comparison is done with `org-element--cache-key-less-p'.
4746 When no synchronization is taking place, a key is simply the
4747 beginning position of the element, or that position plus one in
4748 the case of an first item (respectively row) in
4749 a list (respectively a table).
4751 During a synchronization, the key is the one the element had when
4752 the cache was synchronized for the last time. Elements added to
4753 cache during the synchronization get a new key generated with
4754 `org-element--cache-generate-key'.
4756 Such keys are stored in `org-element--cache-sync-keys'. The hash
4757 table is cleared once the synchronization is complete."
4758 (or (gethash element org-element--cache-sync-keys
)
4759 (let* ((begin (org-element-property :begin element
))
4760 ;; Increase beginning position of items (respectively
4761 ;; table rows) by one, so the first item can get
4762 ;; a different key from its parent list (respectively
4764 (key (if (memq (org-element-type element
) '(item table-row
))
4767 (if org-element--cache-sync-requests
4768 (puthash element key org-element--cache-sync-keys
)
4771 (defun org-element--cache-generate-key (lower upper
)
4772 "Generate a key between LOWER and UPPER.
4774 LOWER and UPPER are integers or lists, possibly empty.
4776 If LOWER and UPPER are equals, return LOWER. Otherwise, return
4777 a unique key, as an integer or a list of integers, according to
4778 the following rules:
4780 - LOWER and UPPER are compared level-wise until values differ.
4782 - If, at a given level, LOWER and UPPER differ from more than
4783 2, the new key shares all the levels above with LOWER and
4784 gets a new level. Its value is the mean between LOWER and
4787 \(1 2) + (1 4) --> (1 3)
4789 - If LOWER has no value to compare with, it is assumed that its
4790 value is `most-negative-fixnum'. E.g.,
4798 where m is `most-negative-fixnum'. Likewise, if UPPER is
4799 short of levels, the current value is `most-positive-fixnum'.
4801 - If they differ from only one, the new key inherits from
4802 current LOWER level and fork it at the next level. E.g.,
4810 where M is `most-positive-fixnum'.
4812 - If the key is only one level long, it is returned as an
4815 \(1 2) + (3 2) --> 2
4817 When they are not equals, the function assumes that LOWER is
4818 lesser than UPPER, per `org-element--cache-key-less-p'."
4819 (if (equal lower upper
) lower
4820 (let ((lower (if (integerp lower
) (list lower
) lower
))
4821 (upper (if (integerp upper
) (list upper
) upper
))
4825 (let ((min (or (car lower
) most-negative-fixnum
))
4826 (max (cond (skip-upper most-positive-fixnum
)
4828 (t most-positive-fixnum
))))
4829 (if (< (1+ min
) max
)
4830 (let ((mean (+ (ash min -
1) (ash max -
1) (logand min max
1))))
4831 (throw 'exit
(if key
(nreverse (cons mean key
)) mean
)))
4832 (when (and (< min max
) (not skip-upper
))
4833 ;; When at a given level, LOWER and UPPER differ from
4834 ;; 1, ignore UPPER altogether. Instead create a key
4835 ;; between LOWER and the greatest key with the same
4836 ;; prefix as LOWER so far.
4837 (setq skip-upper t
))
4839 (setq lower
(cdr lower
) upper
(cdr upper
)))))))))
4841 (defsubst org-element--cache-key-less-p
(a b
)
4842 "Non-nil if key A is less than key B.
4843 A and B are either integers or lists of integers, as returned by
4844 `org-element--cache-key'."
4845 (if (integerp a
) (if (integerp b
) (< a b
) (<= a
(car b
)))
4846 (if (integerp b
) (< (car a
) b
)
4849 (cond ((car-less-than-car a b
) (throw 'exit t
))
4850 ((car-less-than-car b a
) (throw 'exit nil
))
4851 (t (setq a
(cdr a
) b
(cdr b
)))))
4852 ;; If A is empty, either keys are equal (B is also empty) and
4853 ;; we return nil, or A is lesser than B (B is longer) and we
4854 ;; return a non-nil value.
4856 ;; If A is not empty, B is necessarily empty and A is greater
4857 ;; than B (A is longer). Therefore, return nil.
4858 (and (null a
) b
)))))
4860 (defun org-element--cache-compare (a b
)
4861 "Non-nil when element A is located before element B."
4862 (org-element--cache-key-less-p (org-element--cache-key a
)
4863 (org-element--cache-key b
)))
4865 (defsubst org-element--cache-root
()
4866 "Return root value in cache.
4867 This function assumes `org-element--cache' is a valid AVL tree."
4868 (avl-tree--node-left (avl-tree--dummyroot org-element--cache
)))
4873 (defsubst org-element--cache-active-p
()
4874 "Non-nil when cache is active in current buffer."
4875 (and org-element-use-cache
4877 (or (derived-mode-p 'org-mode
) orgstruct-mode
)))
4879 (defun org-element--cache-find (pos &optional side
)
4880 "Find element in cache starting at POS or before.
4882 POS refers to a buffer position.
4884 When optional argument SIDE is non-nil, the function checks for
4885 elements starting at or past POS instead. If SIDE is `both', the
4886 function returns a cons cell where car is the first element
4887 starting at or before POS and cdr the first element starting
4890 The function can only find elements in the synchronized part of
4892 (let ((limit (and org-element--cache-sync-requests
4893 (aref (car org-element--cache-sync-requests
) 0)))
4894 (node (org-element--cache-root))
4897 (let* ((element (avl-tree--node-data node
))
4898 (begin (org-element-property :begin element
)))
4901 (not (org-element--cache-key-less-p
4902 (org-element--cache-key element
) limit
)))
4903 (setq node
(avl-tree--node-left node
)))
4906 node
(avl-tree--node-left node
)))
4909 node
(avl-tree--node-right node
)))
4910 ;; We found an element in cache starting at POS. If `side'
4911 ;; is `both' we also want the next one in order to generate
4912 ;; a key in-between.
4914 ;; If the element is the first row or item in a table or
4915 ;; a plain list, we always return the table or the plain
4918 ;; In any other case, we return the element found.
4920 (setq lower element
)
4921 (setq node
(avl-tree--node-right node
)))
4922 ((and (memq (org-element-type element
) '(item table-row
))
4923 (let ((parent (org-element-property :parent element
)))
4924 (and (= (org-element-property :begin element
)
4925 (org-element-property :contents-begin parent
))
4934 (`both
(cons lower upper
))
4938 (defun org-element--cache-put (element &optional data
)
4939 "Store ELEMENT in current buffer's cache, if allowed.
4940 When optional argument DATA is non-nil, assume is it object data
4941 relative to ELEMENT and store it in the objects cache."
4942 (cond ((not (org-element--cache-active-p)) nil
)
4944 (when org-element--cache-sync-requests
4945 ;; During synchronization, first build an appropriate key
4946 ;; for the new element so `avl-tree-enter' can insert it at
4947 ;; the right spot in the cache.
4948 (let ((keys (org-element--cache-find
4949 (org-element-property :begin element
) 'both
)))
4951 (org-element--cache-generate-key
4952 (and (car keys
) (org-element--cache-key (car keys
)))
4953 (cond ((cdr keys
) (org-element--cache-key (cdr keys
)))
4954 (org-element--cache-sync-requests
4955 (aref (car org-element--cache-sync-requests
) 0))))
4956 org-element--cache-sync-keys
)))
4957 (avl-tree-enter org-element--cache element
))
4958 ;; Headlines are not stored in cache, so objects in titles are
4959 ;; not stored either.
4960 ((eq (org-element-type element
) 'headline
) nil
)
4961 (t (puthash element data org-element--cache-objects
))))
4963 (defsubst org-element--cache-remove
(element)
4964 "Remove ELEMENT from cache.
4965 Assume ELEMENT belongs to cache and that a cache is active."
4966 (avl-tree-delete org-element--cache element
)
4967 (remhash element org-element--cache-objects
))
4970 ;;;; Synchronization
4972 (defsubst org-element--cache-set-timer
(buffer)
4973 "Set idle timer for cache synchronization in BUFFER."
4974 (when org-element--cache-sync-timer
4975 (cancel-timer org-element--cache-sync-timer
))
4976 (setq org-element--cache-sync-timer
4977 (run-with-idle-timer
4978 (let ((idle (current-idle-time)))
4979 (if idle
(time-add idle org-element-cache-sync-break
)
4980 org-element-cache-sync-idle-time
))
4982 #'org-element--cache-sync
4985 (defsubst org-element--cache-interrupt-p
(time-limit)
4986 "Non-nil when synchronization process should be interrupted.
4987 TIME-LIMIT is a time value or nil."
4989 (or (input-pending-p)
4990 (time-less-p time-limit
(current-time)))))
4992 (defsubst org-element--cache-shift-positions
(element offset
&optional props
)
4993 "Shift ELEMENT properties relative to buffer positions by OFFSET.
4995 Properties containing buffer positions are `:begin', `:end',
4996 `:contents-begin', `:contents-end' and `:structure'. When
4997 optional argument PROPS is a list of keywords, only shift
4998 properties provided in that list.
5000 Properties are modified by side-effect."
5001 (let ((properties (nth 1 element
)))
5002 ;; Shift `:structure' property for the first plain list only: it
5003 ;; is the only one that really matters and it prevents from
5004 ;; shifting it more than once.
5005 (when (and (or (not props
) (memq :structure props
))
5006 (eq (org-element-type element
) 'plain-list
)
5007 (not (eq (org-element-type (plist-get properties
:parent
))
5009 (dolist (item (plist-get properties
:structure
))
5010 (incf (car item
) offset
)
5011 (incf (nth 6 item
) offset
)))
5012 (dolist (key '(:begin
:contents-begin
:contents-end
:end
:post-affiliated
))
5013 (let ((value (and (or (not props
) (memq key props
))
5014 (plist-get properties key
))))
5015 (and value
(plist-put properties key
(+ offset value
)))))))
5017 (defun org-element--cache-sync (buffer &optional threshold future-change
)
5018 "Synchronize cache with recent modification in BUFFER.
5020 When optional argument THRESHOLD is non-nil, do the
5021 synchronization for all elements starting before or at threshold,
5022 then exit. Otherwise, synchronize cache for as long as
5023 `org-element-cache-sync-duration' or until Emacs leaves idle
5026 FUTURE-CHANGE, when non-nil, is a buffer position where changes
5027 not registered yet in the cache are going to happen. It is used
5028 in `org-element--cache-submit-request', where cache is partially
5029 updated before current modification are actually submitted."
5030 (when (buffer-live-p buffer
)
5031 (with-current-buffer buffer
5032 (let ((inhibit-quit t
) request next
)
5033 (when org-element--cache-sync-timer
5034 (cancel-timer org-element--cache-sync-timer
))
5036 (while org-element--cache-sync-requests
5037 (setq request
(car org-element--cache-sync-requests
)
5038 next
(nth 1 org-element--cache-sync-requests
))
5039 (org-element--cache-process-request
5041 (and next
(aref next
0))
5043 (and (not threshold
)
5044 (time-add (current-time)
5045 org-element-cache-sync-duration
))
5047 ;; Request processed. Merge current and next offsets and
5048 ;; transfer ending position.
5050 (incf (aref next
3) (aref request
3))
5051 (aset next
2 (aref request
2)))
5052 (setq org-element--cache-sync-requests
5053 (cdr org-element--cache-sync-requests
))))
5054 ;; If more requests are awaiting, set idle timer accordingly.
5055 ;; Otherwise, reset keys.
5056 (if org-element--cache-sync-requests
5057 (org-element--cache-set-timer buffer
)
5058 (clrhash org-element--cache-sync-keys
))))))
5060 (defun org-element--cache-process-request
5061 (request next threshold time-limit future-change
)
5062 "Process synchronization REQUEST for all entries before NEXT.
5064 REQUEST is a vector, built by `org-element--cache-submit-request'.
5066 NEXT is a cache key, as returned by `org-element--cache-key'.
5068 When non-nil, THRESHOLD is a buffer position. Synchronization
5069 stops as soon as a shifted element begins after it.
5071 When non-nil, TIME-LIMIT is a time value. Synchronization stops
5072 after this time or when Emacs exits idle state.
5074 When non-nil, FUTURE-CHANGE is a buffer position where changes
5075 not registered yet in the cache are going to happen. See
5076 `org-element--cache-submit-request' for more information.
5078 Throw `interrupt' if the process stops before completing the
5081 (when (= (aref request
5) 0)
5084 ;; Delete all elements starting after BEG, but not after buffer
5085 ;; position END or past element with key NEXT. Also delete
5086 ;; elements contained within a previously removed element
5087 ;; (stored in `last-container').
5089 ;; At each iteration, we start again at tree root since
5090 ;; a deletion modifies structure of the balanced tree.
5093 (when (org-element--cache-interrupt-p time-limit
)
5094 (throw 'interrupt nil
))
5095 ;; Find first element in cache with key BEG or after it.
5096 (let ((beg (aref request
0))
5097 (end (aref request
2))
5098 (node (org-element--cache-root))
5099 data data-key last-container
)
5101 (let* ((element (avl-tree--node-data node
))
5102 (key (org-element--cache-key element
)))
5104 ((org-element--cache-key-less-p key beg
)
5105 (setq node
(avl-tree--node-right node
)))
5106 ((org-element--cache-key-less-p beg key
)
5109 node
(avl-tree--node-left node
)))
5110 (t (setq data element
5114 (let ((pos (org-element-property :begin data
)))
5115 (if (if (or (not next
)
5116 (org-element--cache-key-less-p data-key next
))
5120 (while (and up
(not (eq up last-container
)))
5121 (setq up
(org-element-property :parent up
)))
5123 (progn (when (and (not last-container
)
5124 (> (org-element-property :end data
)
5126 (setq last-container data
))
5127 (org-element--cache-remove data
))
5128 (aset request
0 data-key
)
5129 (aset request
1 pos
)
5131 (throw 'end-phase nil
)))
5132 ;; No element starting after modifications left in
5133 ;; cache: further processing is futile.
5134 (throw 'quit t
))))))
5135 (when (= (aref request
5) 1)
5138 ;; Phase 0 left a hole in the cache. Some elements after it
5139 ;; could have parents within. For example, in the following
5149 ;; if we remove a blank line between "item" and "Paragraph1",
5150 ;; everything down to "Paragraph2" is removed from cache. But
5151 ;; the paragraph now belongs to the list, and its `:parent'
5152 ;; property no longer is accurate.
5154 ;; Therefore we need to parse again elements in the hole, or at
5155 ;; least in its last section, so that we can re-parent
5156 ;; subsequent elements, during phase 2.
5158 ;; Note that we only need to get the parent from the first
5159 ;; element in cache after the hole.
5161 ;; When next key is lesser or equal to the current one, delegate
5162 ;; phase 1 processing to next request in order to preserve key
5163 ;; order among requests.
5164 (let ((key (aref request
0)))
5165 (when (and next
(not (org-element--cache-key-less-p key next
)))
5166 (let ((next-request (nth 1 org-element--cache-sync-requests
)))
5167 (aset next-request
0 key
)
5168 (aset next-request
1 (aref request
1))
5169 (aset next-request
5 1))
5171 ;; Next element will start at its beginning position plus
5172 ;; offset, since it hasn't been shifted yet. Therefore, LIMIT
5173 ;; contains the real beginning position of the first element to
5174 ;; shift and re-parent.
5175 (let ((limit (+ (aref request
1) (aref request
3))))
5176 (cond ((and threshold
(> limit threshold
)) (throw 'interrupt nil
))
5177 ((and future-change
(>= limit future-change
))
5178 ;; Changes are going to happen around this element and
5179 ;; they will trigger another phase 1 request. Skip the
5183 (let ((parent (org-element--parse-to limit t time-limit
)))
5184 (aset request
4 parent
)
5185 (aset request
5 2))))))
5188 ;; Shift all elements starting from key START, but before NEXT, by
5189 ;; OFFSET, and re-parent them when appropriate.
5191 ;; Elements are modified by side-effect so the tree structure
5194 ;; Once THRESHOLD, if any, is reached, or once there is an input
5195 ;; pending, exit. Before leaving, the current synchronization
5196 ;; request is updated.
5197 (let ((start (aref request
0))
5198 (offset (aref request
3))
5199 (parent (aref request
4))
5200 (node (org-element--cache-root))
5204 ;; No re-parenting nor shifting planned: request is over.
5205 (when (and (not parent
) (zerop offset
)) (throw 'quit t
))
5207 (let* ((data (avl-tree--node-data node
))
5208 (key (org-element--cache-key data
)))
5209 (if (and leftp
(avl-tree--node-left node
)
5210 (not (org-element--cache-key-less-p key start
)))
5211 (progn (push node stack
)
5212 (setq node
(avl-tree--node-left node
)))
5213 (unless (org-element--cache-key-less-p key start
)
5214 ;; We reached NEXT. Request is complete.
5215 (when (equal key next
) (throw 'quit t
))
5216 ;; Handle interruption request. Update current request.
5217 (when (or exit-flag
(org-element--cache-interrupt-p time-limit
))
5218 (aset request
0 key
)
5219 (aset request
4 parent
)
5220 (throw 'interrupt nil
))
5222 (unless (zerop offset
)
5223 (org-element--cache-shift-positions data offset
)
5224 ;; Shift associated objects data, if any.
5225 (dolist (object-data (gethash data org-element--cache-objects
))
5226 (dolist (object (cddr object-data
))
5227 (org-element--cache-shift-positions object offset
))))
5228 (let ((begin (org-element-property :begin data
)))
5229 ;; Update PARENT and re-parent DATA, only when
5230 ;; necessary. Propagate new structures for lists.
5232 (<= (org-element-property :end parent
) begin
))
5233 (setq parent
(org-element-property :parent parent
)))
5234 (cond ((and (not parent
) (zerop offset
)) (throw 'quit nil
))
5236 (let ((p (org-element-property :parent data
)))
5238 (< (org-element-property :begin p
)
5239 (org-element-property :begin parent
)))))
5240 (org-element-put-property data
:parent parent
)
5241 (let ((s (org-element-property :structure parent
)))
5242 (when (and s
(org-element-property :structure data
))
5243 (org-element-put-property data
:structure s
)))))
5244 ;; Cache is up-to-date past THRESHOLD. Request
5246 (when (and threshold
(> begin threshold
)) (setq exit-flag t
))))
5247 (setq node
(if (setq leftp
(avl-tree--node-right node
))
5248 (avl-tree--node-right node
)
5250 ;; We reached end of tree: synchronization complete.
5253 (defun org-element--parse-to (pos &optional syncp time-limit
)
5254 "Parse elements in current section, down to POS.
5256 Start parsing from the closest between the last known element in
5257 cache or headline above. Return the smallest element containing
5260 When optional argument SYNCP is non-nil, return the parent of the
5261 element containing POS instead. In that case, it is also
5262 possible to provide TIME-LIMIT, which is a time value specifying
5263 when the parsing should stop. The function throws `interrupt' if
5264 the process stopped before finding the expected result."
5266 (org-with-wide-buffer
5268 (let* ((cached (and (org-element--cache-active-p)
5269 (org-element--cache-find pos nil
)))
5270 (begin (org-element-property :begin cached
))
5273 ;; Nothing in cache before point: start parsing from first
5274 ;; element following headline above, or first element in
5277 (when (org-with-limited-levels (outline-previous-heading))
5278 (setq mode
'planning
)
5280 (skip-chars-forward " \r\t\n")
5281 (beginning-of-line))
5282 ;; Cache returned exact match: return it.
5284 (throw 'exit
(if syncp
(org-element-property :parent cached
) cached
)))
5285 ;; There's a headline between cached value and POS: cached
5286 ;; value is invalid. Start parsing from first element
5287 ;; following the headline.
5288 ((re-search-backward
5289 (org-with-limited-levels org-outline-regexp-bol
) begin t
)
5291 (skip-chars-forward " \r\t\n")
5293 (setq mode
'planning
))
5294 ;; Check if CACHED or any of its ancestors contain point.
5296 ;; If there is such an element, we inspect it in order to know
5297 ;; if we return it or if we need to parse its contents.
5298 ;; Otherwise, we just start parsing from current location,
5299 ;; which is right after the top-most element containing
5302 ;; As a special case, if POS is at the end of the buffer, we
5303 ;; want to return the innermost element ending there.
5305 ;; Also, if we find an ancestor and discover that we need to
5306 ;; parse its contents, make sure we don't start from
5307 ;; `:contents-begin', as we would otherwise go past CACHED
5308 ;; again. Instead, in that situation, we will resume parsing
5309 ;; from NEXT, which is located after CACHED or its higher
5310 ;; ancestor not containing point.
5313 (pos (if (= (point-max) pos
) (1- pos
) pos
)))
5314 (goto-char (or (org-element-property :contents-begin cached
) begin
))
5315 (while (let ((end (org-element-property :end up
)))
5318 (setq up
(org-element-property :parent up
)))))
5320 ((eobp) (setq element up
))
5321 (t (setq element up next
(point)))))))
5322 ;; Parse successively each element until we reach POS.
5323 (let ((end (or (org-element-property :end element
)
5325 (org-with-limited-levels (outline-next-heading))
5330 (cond ((= (point) pos
) (throw 'exit parent
))
5331 ((org-element--cache-interrupt-p time-limit
)
5332 (throw 'interrupt nil
))))
5334 (setq element
(org-element--current-element
5336 (org-element-property :structure parent
)))
5337 (org-element-put-property element
:parent parent
)
5338 (org-element--cache-put element
))
5339 (let ((elem-end (org-element-property :end element
))
5340 (type (org-element-type element
)))
5342 ;; Skip any element ending before point. Also skip
5343 ;; element ending at point (unless it is also the end of
5344 ;; buffer) since we're sure that another element begins
5346 ((and (<= elem-end pos
) (/= (point-max) elem-end
))
5347 (goto-char elem-end
)
5348 (setq mode
(org-element--next-mode type nil
)))
5349 ;; A non-greater element contains point: return it.
5350 ((not (memq type org-element-greater-elements
))
5351 (throw 'exit element
))
5352 ;; Otherwise, we have to decide if ELEMENT really
5353 ;; contains POS. In that case we start parsing from
5354 ;; contents' beginning.
5356 ;; If POS is at contents' beginning but it is also at
5357 ;; the beginning of the first item in a list or a table.
5358 ;; In that case, we need to create an anchor for that
5359 ;; list or table, so return it.
5361 ;; Also, if POS is at the end of the buffer, no element
5362 ;; can start after it, but more than one may end there.
5363 ;; Arbitrarily, we choose to return the innermost of
5365 ((let ((cbeg (org-element-property :contents-begin element
))
5366 (cend (org-element-property :contents-end element
)))
5371 (not (memq type
'(plain-list table
)))))
5373 (and (= cend pos
) (= (point-max) pos
)))))
5374 (goto-char (or next cbeg
))
5376 mode
(org-element--next-mode type t
)
5379 ;; Otherwise, return ELEMENT as it is the smallest
5380 ;; element containing POS.
5381 (t (throw 'exit element
))))
5382 (setq element nil
)))))))
5385 ;;;; Staging Buffer Changes
5387 (defconst org-element--cache-sensitive-re
5389 org-outline-regexp-bol
"\\|"
5390 "\\\\end{[A-Za-z0-9*]+}[ \t]*$" "\\|"
5392 "#\\+\\(?:BEGIN[:_]\\|END\\(?:_\\|:?[ \t]*$\\)\\)" "\\|"
5393 "\\\\begin{[A-Za-z0-9*]+}" "\\|"
5394 ":\\(?:\\w\\|[-_]\\)+:[ \t]*$"
5396 "Regexp matching a sensitive line, structure wise.
5397 A sensitive line is a headline, inlinetask, block, drawer, or
5398 latex-environment boundary. When such a line is modified,
5399 structure changes in the document may propagate in the whole
5400 section, possibly making cache invalid.")
5402 (defvar org-element--cache-change-warning nil
5403 "Non-nil when a sensitive line is about to be changed.
5404 It is a symbol among nil, t and `headline'.")
5406 (defun org-element--cache-before-change (beg end
)
5407 "Request extension of area going to be modified if needed.
5408 BEG and END are the beginning and end of the range of changed
5409 text. See `before-change-functions' for more information."
5410 (when (org-element--cache-active-p)
5411 (org-with-wide-buffer
5414 (let ((bottom (save-excursion (goto-char end
) (line-end-position))))
5415 (setq org-element--cache-change-warning
5417 (if (and (org-with-limited-levels (org-at-heading-p))
5418 (= (line-end-position) bottom
))
5420 (let ((case-fold-search t
))
5422 org-element--cache-sensitive-re bottom t
)))))))))
5424 (defun org-element--cache-after-change (beg end pre
)
5425 "Update buffer modifications for current buffer.
5426 BEG and END are the beginning and end of the range of changed
5427 text, and the length in bytes of the pre-change text replaced by
5428 that range. See `after-change-functions' for more information."
5429 (when (org-element--cache-active-p)
5430 (org-with-wide-buffer
5435 (bottom (save-excursion (goto-char end
) (line-end-position))))
5436 ;; Determine if modified area needs to be extended, according
5437 ;; to both previous and current state. We make a special
5438 ;; case for headline editing: if a headline is modified but
5439 ;; not removed, do not extend.
5440 (when (pcase org-element--cache-change-warning
5443 (not (and (org-with-limited-levels (org-at-heading-p))
5444 (= (line-end-position) bottom
))))
5446 (let ((case-fold-search t
))
5448 org-element--cache-sensitive-re bottom t
))))
5449 ;; Effectively extend modified area.
5450 (org-with-limited-levels
5451 (setq top
(progn (goto-char top
)
5452 (when (outline-previous-heading) (forward-line))
5454 (setq bottom
(progn (goto-char bottom
)
5455 (if (outline-next-heading) (1- (point))
5457 ;; Store synchronization request.
5458 (let ((offset (- end beg pre
)))
5459 (org-element--cache-submit-request top
(- bottom offset
) offset
)))))
5460 ;; Activate a timer to process the request during idle time.
5461 (org-element--cache-set-timer (current-buffer))))
5463 (defun org-element--cache-for-removal (beg end offset
)
5464 "Return first element to remove from cache.
5466 BEG and END are buffer positions delimiting buffer modifications.
5467 OFFSET is the size of the changes.
5469 Returned element is usually the first element in cache containing
5470 any position between BEG and END. As an exception, greater
5471 elements around the changes that are robust to contents
5472 modifications are preserved and updated according to the
5474 (let* ((elements (org-element--cache-find (1- beg
) 'both
))
5475 (before (car elements
))
5476 (after (cdr elements
)))
5477 (if (not before
) after
5481 (if (let ((type (org-element-type up
)))
5482 (and (or (memq type
'(center-block dynamic-block quote-block
5484 ;; Drawers named "PROPERTIES" are probably
5485 ;; a properties drawer being edited. Force
5486 ;; parsing to check if editing is over.
5487 (and (eq type
'drawer
)
5489 (org-element-property :drawer-name up
)
5491 (let ((cbeg (org-element-property :contents-begin up
)))
5494 (> (org-element-property :contents-end up
) end
)))))
5495 ;; UP is a robust greater element containing changes.
5496 ;; We only need to extend its ending boundaries.
5497 (org-element--cache-shift-positions
5498 up offset
'(:contents-end
:end
))
5500 (when robust-flag
(setq robust-flag nil
)))
5501 (setq up
(org-element-property :parent up
)))
5502 ;; We're at top level element containing ELEMENT: if it's
5503 ;; altered by buffer modifications, it is first element in
5504 ;; cache to be removed. Otherwise, that first element is the
5507 ;; As a special case, do not remove BEFORE if it is a robust
5508 ;; container for current changes.
5509 (if (or (< (org-element-property :end before
) beg
) robust-flag
) after
5512 (defun org-element--cache-submit-request (beg end offset
)
5513 "Submit a new cache synchronization request for current buffer.
5514 BEG and END are buffer positions delimiting the minimal area
5515 where cache data should be removed. OFFSET is the size of the
5516 change, as an integer."
5517 (let ((next (car org-element--cache-sync-requests
))
5518 delete-to delete-from
)
5520 (zerop (aref next
5))
5521 (> (setq delete-to
(+ (aref next
2) (aref next
3))) end
)
5522 (<= (setq delete-from
(aref next
1)) end
))
5523 ;; Current changes can be merged with first sync request: we
5524 ;; can save a partial cache synchronization.
5526 (incf (aref next
3) offset
)
5527 ;; If last change happened within area to be removed, extend
5528 ;; boundaries of robust parents, if any. Otherwise, find
5529 ;; first element to remove and update request accordingly.
5530 (if (> beg delete-from
)
5531 (let ((up (aref next
4)))
5533 (org-element--cache-shift-positions
5534 up offset
'(:contents-end
:end
))
5535 (setq up
(org-element-property :parent up
))))
5536 (let ((first (org-element--cache-for-removal beg delete-to offset
)))
5538 (aset next
0 (org-element--cache-key first
))
5539 (aset next
1 (org-element-property :begin first
))
5540 (aset next
4 (org-element-property :parent first
))))))
5541 ;; Ensure cache is correct up to END. Also make sure that NEXT,
5542 ;; if any, is no longer a 0-phase request, thus ensuring that
5543 ;; phases are properly ordered. We need to provide OFFSET as
5544 ;; optional parameter since current modifications are not known
5545 ;; yet to the otherwise correct part of the cache (i.e, before
5546 ;; the first request).
5547 (when next
(org-element--cache-sync (current-buffer) end beg
))
5548 (let ((first (org-element--cache-for-removal beg end offset
)))
5550 (push (let ((beg (org-element-property :begin first
))
5551 (key (org-element--cache-key first
)))
5553 ;; When changes happen before the first known
5554 ;; element, re-parent and shift the rest of the
5556 ((> beg end
) (vector key beg nil offset nil
1))
5557 ;; Otherwise, we find the first non robust
5558 ;; element containing END. All elements between
5559 ;; FIRST and this one are to be removed.
5560 ((let ((first-end (org-element-property :end first
)))
5561 (and (> first-end end
)
5562 (vector key beg first-end offset first
0))))
5564 (let* ((element (org-element--cache-find end
))
5565 (end (org-element-property :end element
))
5567 (while (and (setq up
(org-element-property :parent up
))
5568 (>= (org-element-property :begin up
) beg
))
5569 (setq end
(org-element-property :end up
)
5571 (vector key beg end offset element
0)))))
5572 org-element--cache-sync-requests
)
5573 ;; No element to remove. No need to re-parent either.
5574 ;; Simply shift additional elements, if any, by OFFSET.
5575 (when org-element--cache-sync-requests
5576 (incf (aref (car org-element--cache-sync-requests
) 3) offset
)))))))
5579 ;;;; Public Functions
5582 (defun org-element-cache-reset (&optional all
)
5583 "Reset cache in current buffer.
5584 When optional argument ALL is non-nil, reset cache in all Org
5587 (dolist (buffer (if all
(buffer-list) (list (current-buffer))))
5588 (with-current-buffer buffer
5589 (when (and org-element-use-cache
5590 (or (derived-mode-p 'org-mode
) orgstruct-mode
))
5591 (setq-local org-element--cache
5592 (avl-tree-create #'org-element--cache-compare
))
5593 (setq-local org-element--cache-objects
(make-hash-table :test
#'eq
))
5594 (setq-local org-element--cache-sync-keys
5595 (make-hash-table :weakness
'key
:test
#'eq
))
5596 (setq-local org-element--cache-change-warning nil
)
5597 (setq-local org-element--cache-sync-requests nil
)
5598 (setq-local org-element--cache-sync-timer nil
)
5599 (add-hook 'before-change-functions
5600 #'org-element--cache-before-change nil t
)
5601 (add-hook 'after-change-functions
5602 #'org-element--cache-after-change nil t
)))))
5605 (defun org-element-cache-refresh (pos)
5606 "Refresh cache at position POS."
5607 (when (org-element--cache-active-p)
5608 (org-element--cache-sync (current-buffer) pos
)
5609 (org-element--cache-submit-request pos pos
0)
5610 (org-element--cache-set-timer (current-buffer))))
5616 ;; The first move is to implement a way to obtain the smallest element
5617 ;; containing point. This is the job of `org-element-at-point'. It
5618 ;; basically jumps back to the beginning of section containing point
5619 ;; and proceed, one element after the other, with
5620 ;; `org-element--current-element' until the container is found. Note:
5621 ;; When using `org-element-at-point', secondary values are never
5622 ;; parsed since the function focuses on elements, not on objects.
5624 ;; At a deeper level, `org-element-context' lists all elements and
5625 ;; objects containing point.
5627 ;; `org-element-nested-p' and `org-element-swap-A-B' may be used
5628 ;; internally by navigation and manipulation tools.
5632 (defun org-element-at-point ()
5633 "Determine closest element around point.
5635 Return value is a list like (TYPE PROPS) where TYPE is the type
5636 of the element and PROPS a plist of properties associated to the
5639 Possible types are defined in `org-element-all-elements'.
5640 Properties depend on element or object type, but always include
5641 `:begin', `:end', `:parent' and `:post-blank' properties.
5643 As a special case, if point is at the very beginning of the first
5644 item in a list or sub-list, returned element will be that list
5645 instead of the item. Likewise, if point is at the beginning of
5646 the first row of a table, returned element will be the table
5647 instead of the first row.
5649 When point is at the end of the buffer, return the innermost
5650 element ending there."
5651 (org-with-wide-buffer
5652 (let ((origin (point)))
5654 (skip-chars-backward " \r\t\n")
5656 ;; Within blank lines at the beginning of buffer, return nil.
5658 ;; Within blank lines right after a headline, return that
5660 ((org-with-limited-levels (org-at-heading-p))
5662 (org-element-headline-parser (point-max) t
))
5663 ;; Otherwise parse until we find element containing ORIGIN.
5665 (when (org-element--cache-active-p)
5666 (if (not org-element--cache
) (org-element-cache-reset)
5667 (org-element--cache-sync (current-buffer) origin
)))
5668 (org-element--parse-to origin
))))))
5671 (defun org-element-context (&optional element
)
5672 "Return smallest element or object around point.
5674 Return value is a list like (TYPE PROPS) where TYPE is the type
5675 of the element or object and PROPS a plist of properties
5678 Possible types are defined in `org-element-all-elements' and
5679 `org-element-all-objects'. Properties depend on element or
5680 object type, but always include `:begin', `:end', `:parent' and
5683 As a special case, if point is right after an object and not at
5684 the beginning of any other object, return that object.
5686 Optional argument ELEMENT, when non-nil, is the closest element
5687 containing point, as returned by `org-element-at-point'.
5688 Providing it allows for quicker computation."
5689 (catch 'objects-forbidden
5690 (org-with-wide-buffer
5691 (let* ((pos (point))
5692 (element (or element
(org-element-at-point)))
5693 (type (org-element-type element
)))
5694 ;; If point is inside an element containing objects or
5695 ;; a secondary string, narrow buffer to the container and
5696 ;; proceed with parsing. Otherwise, return ELEMENT.
5698 ;; At a parsed affiliated keyword, check if we're inside main
5700 ((let ((post (org-element-property :post-affiliated element
)))
5701 (and post
(< pos post
)))
5703 (let ((case-fold-search t
)) (looking-at org-element--affiliated-re
))
5705 ((not (member-ignore-case (match-string 1)
5706 org-element-parsed-keywords
))
5707 (throw 'objects-forbidden element
))
5708 ((< (match-end 0) pos
)
5709 (narrow-to-region (match-end 0) (line-end-position)))
5710 ((and (match-beginning 2)
5711 (>= pos
(match-beginning 2))
5712 (< pos
(match-end 2)))
5713 (narrow-to-region (match-beginning 2) (match-end 2)))
5714 (t (throw 'objects-forbidden element
)))
5715 ;; Also change type to retrieve correct restrictions.
5716 (setq type
'keyword
))
5717 ;; At an item, objects can only be located within tag, if any.
5719 (let ((tag (org-element-property :tag element
)))
5720 (if (not tag
) (throw 'objects-forbidden element
)
5722 (search-forward tag
(line-end-position))
5723 (goto-char (match-beginning 0))
5724 (if (and (>= pos
(point)) (< pos
(match-end 0)))
5725 (narrow-to-region (point) (match-end 0))
5726 (throw 'objects-forbidden element
)))))
5727 ;; At an headline or inlinetask, objects are in title.
5728 ((memq type
'(headline inlinetask
))
5729 (goto-char (org-element-property :begin element
))
5730 (skip-chars-forward "*")
5731 (if (and (> pos
(point)) (< pos
(line-end-position)))
5732 (narrow-to-region (point) (line-end-position))
5733 (throw 'objects-forbidden element
)))
5734 ;; At a paragraph, a table-row or a verse block, objects are
5735 ;; located within their contents.
5736 ((memq type
'(paragraph table-row verse-block
))
5737 (let ((cbeg (org-element-property :contents-begin element
))
5738 (cend (org-element-property :contents-end element
)))
5739 ;; CBEG is nil for table rules.
5740 (if (and cbeg cend
(>= pos cbeg
)
5741 (or (< pos cend
) (and (= pos cend
) (eobp))))
5742 (narrow-to-region cbeg cend
)
5743 (throw 'objects-forbidden element
))))
5744 ;; At a planning line, if point is at a timestamp, return it,
5745 ;; otherwise, return element.
5746 ((eq type
'planning
)
5747 (dolist (p '(:closed
:deadline
:scheduled
))
5748 (let ((timestamp (org-element-property p element
)))
5749 (when (and timestamp
5750 (<= (org-element-property :begin timestamp
) pos
)
5751 (> (org-element-property :end timestamp
) pos
))
5752 (throw 'objects-forbidden timestamp
))))
5753 ;; All other locations cannot contain objects: bail out.
5754 (throw 'objects-forbidden element
))
5755 (t (throw 'objects-forbidden element
)))
5756 (goto-char (point-min))
5757 (let ((restriction (org-element-restriction type
))
5759 (cache (cond ((not (org-element--cache-active-p)) nil
)
5760 (org-element--cache-objects
5761 (gethash element org-element--cache-objects
))
5762 (t (org-element-cache-reset) nil
)))
5763 next object-data last
)
5767 ;; When entering PARENT for the first time, get list
5768 ;; of objects within known so far. Store it in
5771 (let ((data (assq parent cache
)))
5772 (if data
(setq object-data data
)
5773 (push (setq object-data
(list parent nil
)) cache
))))
5774 ;; Find NEXT object for analysis.
5776 ;; If NEXT is non-nil, we already exhausted the
5777 ;; cache so we can parse buffer to find the object
5779 (if next
(setq next
(org-element--object-lex restriction
))
5780 ;; Otherwise, check if cache can help us.
5781 (let ((objects (cddr object-data
))
5782 (completep (nth 1 object-data
)))
5784 ((and (not objects
) completep
) (throw 'exit parent
))
5786 (setq next
(org-element--object-lex restriction
)))
5789 (org-element-property :end
(car objects
))))
5790 (if (>= cache-limit pos
)
5791 ;; Cache contains the information needed.
5792 (dolist (object objects
(throw 'exit parent
))
5793 (when (<= (org-element-property :begin object
)
5795 (if (>= (org-element-property :end object
)
5797 (throw 'found
(setq next object
))
5798 (throw 'exit parent
))))
5799 (goto-char cache-limit
)
5801 (org-element--object-lex restriction
))))))))
5802 ;; If we have a new object to analyze, store it in
5803 ;; cache. Otherwise record that there is nothing
5804 ;; more to parse in this element at this depth.
5806 (progn (org-element-put-property next
:parent parent
)
5807 (push next
(cddr object-data
)))
5808 (setcar (cdr object-data
) t
)))
5809 ;; Process NEXT, if any, in order to know if we need
5810 ;; to skip it, return it or move into it.
5811 (if (or (not next
) (> (org-element-property :begin next
) pos
))
5812 (throw 'exit
(or last parent
))
5813 (let ((end (org-element-property :end next
))
5814 (cbeg (org-element-property :contents-begin next
))
5815 (cend (org-element-property :contents-end next
)))
5817 ;; Skip objects ending before point. Also skip
5818 ;; objects ending at point unless it is also the
5819 ;; end of buffer, since we want to return the
5820 ;; innermost object.
5821 ((and (<= end pos
) (/= (point-max) end
))
5823 ;; For convenience, when object ends at POS,
5824 ;; without any space, store it in LAST, as we
5825 ;; will return it if no object starts here.
5826 (when (and (= end pos
)
5827 (not (memq (char-before) '(?\s ?
\t))))
5829 ;; If POS is within a container object, move
5830 ;; into that object.
5834 ;; At contents' end, if there is no
5835 ;; space before point, also move into
5836 ;; object, for consistency with
5837 ;; convenience feature above.
5839 (or (= (point-max) pos
)
5840 (not (memq (char-before pos
)
5843 (narrow-to-region (point) cend
)
5845 restriction
(org-element-restriction next
)
5848 ;; Otherwise, return NEXT.
5849 (t (throw 'exit next
)))))))
5850 ;; Store results in cache, if applicable.
5851 (org-element--cache-put element cache
)))))))
5853 (defun org-element-lineage (blob &optional types with-self
)
5854 "List all ancestors of a given element or object.
5856 BLOB is an object or element.
5858 When optional argument TYPES is a list of symbols, return the
5859 first element or object in the lineage whose type belongs to that
5862 When optional argument WITH-SELF is non-nil, lineage includes
5863 BLOB itself as the first element, and TYPES, if provided, also
5866 When BLOB is obtained through `org-element-context' or
5867 `org-element-at-point', only ancestors from its section can be
5868 found. There is no such limitation when BLOB belongs to a full
5870 (let ((up (if with-self blob
(org-element-property :parent blob
)))
5872 (while (and up
(not (memq (org-element-type up
) types
)))
5873 (unless types
(push up ancestors
))
5874 (setq up
(org-element-property :parent up
)))
5875 (if types up
(nreverse ancestors
))))
5877 (defun org-element-nested-p (elem-A elem-B
)
5878 "Non-nil when elements ELEM-A and ELEM-B are nested."
5879 (let ((beg-A (org-element-property :begin elem-A
))
5880 (beg-B (org-element-property :begin elem-B
))
5881 (end-A (org-element-property :end elem-A
))
5882 (end-B (org-element-property :end elem-B
)))
5883 (or (and (>= beg-A beg-B
) (<= end-A end-B
))
5884 (and (>= beg-B beg-A
) (<= end-B end-A
)))))
5886 (defun org-element-swap-A-B (elem-A elem-B
)
5887 "Swap elements ELEM-A and ELEM-B.
5888 Assume ELEM-B is after ELEM-A in the buffer. Leave point at the
5890 (goto-char (org-element-property :begin elem-A
))
5891 ;; There are two special cases when an element doesn't start at bol:
5892 ;; the first paragraph in an item or in a footnote definition.
5893 (let ((specialp (not (bolp))))
5894 ;; Only a paragraph without any affiliated keyword can be moved at
5895 ;; ELEM-A position in such a situation. Note that the case of
5896 ;; a footnote definition is impossible: it cannot contain two
5897 ;; paragraphs in a row because it cannot contain a blank line.
5899 (or (not (eq (org-element-type elem-B
) 'paragraph
))
5900 (/= (org-element-property :begin elem-B
)
5901 (org-element-property :contents-begin elem-B
))))
5902 (error "Cannot swap elements"))
5903 ;; In a special situation, ELEM-A will have no indentation. We'll
5904 ;; give it ELEM-B's (which will in, in turn, have no indentation).
5905 (let* ((ind-B (when specialp
5906 (goto-char (org-element-property :begin elem-B
))
5907 (org-get-indentation)))
5908 (beg-A (org-element-property :begin elem-A
))
5909 (end-A (save-excursion
5910 (goto-char (org-element-property :end elem-A
))
5911 (skip-chars-backward " \r\t\n")
5913 (beg-B (org-element-property :begin elem-B
))
5914 (end-B (save-excursion
5915 (goto-char (org-element-property :end elem-B
))
5916 (skip-chars-backward " \r\t\n")
5918 ;; Store inner overlays responsible for visibility status.
5919 ;; We also need to store their boundaries as they will be
5920 ;; removed from buffer.
5925 (and (>= (overlay-start o
) beg-A
)
5926 (<= (overlay-end o
) end-A
)
5927 (list o
(overlay-start o
) (overlay-end o
))))
5928 (overlays-in beg-A end-A
)))
5931 (and (>= (overlay-start o
) beg-B
)
5932 (<= (overlay-end o
) end-B
)
5933 (list o
(overlay-start o
) (overlay-end o
))))
5934 (overlays-in beg-B end-B
)))))
5936 (body-A (buffer-substring beg-A end-A
))
5937 (body-B (delete-and-extract-region beg-B end-B
)))
5940 (setq body-B
(replace-regexp-in-string "\\`[ \t]*" "" body-B
))
5941 (org-indent-to-column ind-B
))
5943 ;; Restore ex ELEM-A overlays.
5944 (let ((offset (- beg-B beg-A
)))
5945 (dolist (o (car overlays
))
5946 (move-overlay (car o
) (+ (nth 1 o
) offset
) (+ (nth 2 o
) offset
)))
5948 (delete-region beg-A end-A
)
5950 ;; Restore ex ELEM-B overlays.
5951 (dolist (o (cdr overlays
))
5952 (move-overlay (car o
) (- (nth 1 o
) offset
) (- (nth 2 o
) offset
))))
5953 (goto-char (org-element-property :end elem-B
)))))
5955 ;; For backward-compatibility with Org <= 8.3
5956 (define-obsolete-function-alias
5957 'org-element-remove-indentation
'org-remove-indentation
"25.1")
5960 (provide 'org-element
)
5963 ;; generated-autoload-file: "org-loaddefs.el"
5966 ;;; org-element.el ends here