1 ;;; org-element.el --- Parser And Applications for Org syntax
3 ;; Copyright (C) 2012-2015 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; Org syntax can be divided into three categories: "Greater
26 ;; elements", "Elements" and "Objects".
28 ;; Elements are related to the structure of the document. Indeed, all
29 ;; elements are a cover for the document: each position within belongs
30 ;; to at least one element.
32 ;; An element always starts and ends at the beginning of a line. With
33 ;; a few exceptions (`clock', `headline', `inlinetask', `item',
34 ;; `planning', `property-drawer', `node-property', `section' and
35 ;; `table-row' types), it can also accept a fixed set of keywords as
36 ;; attributes. Those are called "affiliated keywords" to distinguish
37 ;; them from other keywords, which are full-fledged elements. Almost
38 ;; all affiliated keywords are referenced in
39 ;; `org-element-affiliated-keywords'; the others are export attributes
40 ;; and start with "ATTR_" prefix.
42 ;; Element containing other elements (and only elements) are called
43 ;; greater elements. Concerned types are: `center-block', `drawer',
44 ;; `dynamic-block', `footnote-definition', `headline', `inlinetask',
45 ;; `item', `plain-list', `property-drawer', `quote-block', `section'
46 ;; and `special-block'.
48 ;; Other element types are: `babel-call', `clock', `comment',
49 ;; `comment-block', `diary-sexp', `example-block', `export-block',
50 ;; `fixed-width', `horizontal-rule', `keyword', `latex-environment',
51 ;; `node-property', `paragraph', `planning', `src-block', `table',
52 ;; `table-row' and `verse-block'. Among them, `paragraph' and
53 ;; `verse-block' types can contain Org objects and plain text.
55 ;; Objects are related to document's contents. Some of them are
56 ;; recursive. Associated types are of the following: `bold', `code',
57 ;; `entity', `export-snippet', `footnote-reference',
58 ;; `inline-babel-call', `inline-src-block', `italic',
59 ;; `latex-fragment', `line-break', `link', `macro', `radio-target',
60 ;; `statistics-cookie', `strike-through', `subscript', `superscript',
61 ;; `table-cell', `target', `timestamp', `underline' and `verbatim'.
63 ;; Some elements also have special properties whose value can hold
64 ;; objects themselves (e.g. an item tag or a headline name). Such
65 ;; values are called "secondary strings". Any object belongs to
66 ;; either an element or a secondary string.
68 ;; Notwithstanding affiliated keywords, each greater element, element
69 ;; and object has a fixed set of properties attached to it. Among
70 ;; them, four are shared by all types: `:begin' and `:end', which
71 ;; refer to the beginning and ending buffer positions of the
72 ;; considered element or object, `:post-blank', which holds the number
73 ;; of blank lines, or white spaces, at its end and `:parent' which
74 ;; refers to the element or object containing it. Greater elements,
75 ;; elements and objects containing objects will also have
76 ;; `:contents-begin' and `:contents-end' properties to delimit
77 ;; contents. Eventually, All elements have a `:post-affiliated'
78 ;; property referring to the buffer position after all affiliated
79 ;; keywords, if any, or to their beginning position otherwise.
81 ;; At the lowest level, a `:parent' property is also attached to any
82 ;; string, as a text property.
84 ;; Lisp-wise, an element or an object can be represented as a list.
85 ;; It follows the pattern (TYPE PROPERTIES CONTENTS), where:
86 ;; TYPE is a symbol describing the Org element or object.
87 ;; PROPERTIES is the property list attached to it. See docstring of
88 ;; appropriate parsing function to get an exhaustive
90 ;; CONTENTS is a list of elements, objects or raw strings contained
91 ;; in the current element or object, when applicable.
93 ;; An Org buffer is a nested list of such elements and objects, whose
94 ;; type is `org-data' and properties is nil.
96 ;; The first part of this file defines Org syntax, while the second
97 ;; one provide accessors and setters functions.
99 ;; The next part implements a parser and an interpreter for each
100 ;; element and object type in Org syntax.
102 ;; The following part creates a fully recursive buffer parser. It
103 ;; also provides a tool to map a function to elements or objects
104 ;; matching some criteria in the parse tree. Functions of interest
105 ;; are `org-element-parse-buffer', `org-element-map' and, to a lesser
106 ;; extent, `org-element-parse-secondary-string'.
108 ;; The penultimate part is the cradle of an interpreter for the
109 ;; obtained parse tree: `org-element-interpret-data'.
111 ;; The library ends by furnishing `org-element-at-point' function, and
112 ;; a way to give information about document structure around point
113 ;; with `org-element-context'. A cache mechanism is also provided for
119 (eval-when-compile (require 'cl
))
125 ;;; Definitions And Rules
127 ;; Define elements, greater elements and specify recursive objects,
128 ;; along with the affiliated keywords recognized. Also set up
129 ;; restrictions on recursive objects combinations.
131 ;; `org-element-update-syntax' builds proper syntax regexps according
134 (defvar org-element-paragraph-separate nil
135 "Regexp to separate paragraphs in an Org buffer.
136 In the case of lines starting with \"#\" and \":\", this regexp
137 is not sufficient to know if point is at a paragraph ending. See
138 `org-element-paragraph-parser' for more information.")
140 (defvar org-element--object-regexp nil
141 "Regexp possibly matching the beginning of an object.
142 This regexp allows false positives. Dedicated parser (e.g.,
143 `org-export-bold-parser') will take care of further filtering.
144 Radio links are not matched by this regexp, as they are treated
145 specially in `org-element--object-lex'.")
147 (defun org-element--set-regexps ()
148 "Build variable syntax regexps."
149 (setq org-element-paragraph-separate
151 ;; Headlines, inlinetasks.
152 org-outline-regexp
"\\|"
153 ;; Footnote definitions.
154 "\\[\\(?:[0-9]+\\|fn:[-_[:word:]]+\\)\\]" "\\|"
160 ;; Tables (any type).
161 "\\(?:|\\|\\+-[-+]\\)" "\\|"
162 ;; Blocks (any type), Babel calls and keywords. This
163 ;; is only an indication and need some thorough check.
164 "#\\(?:[+ ]\\|$\\)" "\\|"
165 ;; Drawers (any type) and fixed-width areas. This is
166 ;; also only an indication.
169 "-\\{5,\\}[ \t]*$" "\\|"
170 ;; LaTeX environments.
171 "\\\\begin{\\([A-Za-z0-9]+\\*?\\)}" "\\|"
173 (regexp-quote org-clock-string
) "\\|"
175 (let ((term (case org-plain-list-ordered-item-terminator
176 (?\
) ")") (?.
"\\.") (otherwise "[.)]")))
177 (alpha (and org-list-allow-alphabetical
"\\|[A-Za-z]")))
178 (concat "\\(?:[-+*]\\|\\(?:[0-9]+" alpha
"\\)" term
"\\)"
179 "\\(?:[ \t]\\|$\\)"))
181 org-element--object-regexp
182 (mapconcat #'identity
183 (let ((link-types (regexp-opt org-link-types
)))
186 "\\(?:[_^][-{(*+.,[:alnum:]]\\)"
187 ;; Bold, code, italic, strike-through, underline
191 (nth 2 org-emphasis-regexp-components
)))
193 (concat "\\<" link-types
":")
194 ;; Objects starting with "[": regular link,
195 ;; footnote reference, statistics cookie,
196 ;; timestamp (inactive).
197 "\\[\\(?:fn:\\|\\(?:[0-9]\\|\\(?:%\\|/[0-9]*\\)\\]\\)\\|\\[\\)"
198 ;; Objects starting with "@": export snippets.
200 ;; Objects starting with "{": macro.
202 ;; Objects starting with "<" : timestamp
203 ;; (active, diary), target, radio target and
205 (concat "<\\(?:%%\\|<\\|[0-9]\\|" link-types
"\\)")
206 ;; Objects starting with "$": latex fragment.
208 ;; Objects starting with "\": line break,
209 ;; entity, latex fragment.
210 "\\\\\\(?:[a-zA-Z[(]\\|\\\\[ \t]*$\\)"
211 ;; Objects starting with raw text: inline Babel
212 ;; source block, inline Babel call.
213 "\\(?:call\\|src\\)_"))
216 (org-element--set-regexps)
219 (defun org-element-update-syntax ()
220 "Update parser internals."
222 (org-element--set-regexps)
223 (org-element-cache-reset 'all
))
225 (defconst org-element-all-elements
226 '(babel-call center-block clock comment comment-block diary-sexp drawer
227 dynamic-block example-block export-block fixed-width
228 footnote-definition headline horizontal-rule inlinetask item
229 keyword latex-environment node-property paragraph plain-list
230 planning property-drawer quote-block section
231 special-block src-block table table-row verse-block
)
232 "Complete list of element types.")
234 (defconst org-element-greater-elements
235 '(center-block drawer dynamic-block footnote-definition headline inlinetask
236 item plain-list property-drawer quote-block section
238 "List of recursive element types aka Greater Elements.")
240 (defconst org-element-all-objects
241 '(bold code entity export-snippet footnote-reference inline-babel-call
242 inline-src-block italic line-break latex-fragment link macro
243 radio-target statistics-cookie strike-through subscript superscript
244 table-cell target timestamp underline verbatim
)
245 "Complete list of object types.")
247 (defconst org-element-recursive-objects
248 '(bold footnote-reference italic link subscript radio-target strike-through
249 superscript table-cell underline
)
250 "List of recursive object types.")
252 (defconst org-element-object-containers
253 (append org-element-recursive-objects
'(paragraph table-row verse-block
))
254 "List of object or element types that can directly contain objects.")
256 (defvar org-element-block-name-alist
257 '(("CENTER" . org-element-center-block-parser
)
258 ("COMMENT" . org-element-comment-block-parser
)
259 ("EXAMPLE" . org-element-example-block-parser
)
260 ("QUOTE" . org-element-quote-block-parser
)
261 ("SRC" . org-element-src-block-parser
)
262 ("VERSE" . org-element-verse-block-parser
))
263 "Alist between block names and the associated parsing function.
264 Names must be uppercase. Any block whose name has no association
265 is parsed with `org-element-special-block-parser'.")
267 (defconst org-element-affiliated-keywords
268 '("CAPTION" "DATA" "HEADER" "HEADERS" "LABEL" "NAME" "PLOT" "RESNAME" "RESULT"
269 "RESULTS" "SOURCE" "SRCNAME" "TBLNAME")
270 "List of affiliated keywords as strings.
271 By default, all keywords setting attributes (e.g., \"ATTR_LATEX\")
272 are affiliated keywords and need not to be in this list.")
274 (defconst org-element-keyword-translation-alist
275 '(("DATA" .
"NAME") ("LABEL" .
"NAME") ("RESNAME" .
"NAME")
276 ("SOURCE" .
"NAME") ("SRCNAME" .
"NAME") ("TBLNAME" .
"NAME")
277 ("RESULT" .
"RESULTS") ("HEADERS" .
"HEADER"))
278 "Alist of usual translations for keywords.
279 The key is the old name and the value the new one. The property
280 holding their value will be named after the translated name.")
282 (defconst org-element-multiple-keywords
'("CAPTION" "HEADER")
283 "List of affiliated keywords that can occur more than once in an element.
285 Their value will be consed into a list of strings, which will be
286 returned as the value of the property.
288 This list is checked after translations have been applied. See
289 `org-element-keyword-translation-alist'.
291 By default, all keywords setting attributes (e.g., \"ATTR_LATEX\")
292 allow multiple occurrences and need not to be in this list.")
294 (defconst org-element-parsed-keywords
'("CAPTION")
295 "List of affiliated keywords whose value can be parsed.
297 Their value will be stored as a secondary string: a list of
300 This list is checked after translations have been applied. See
301 `org-element-keyword-translation-alist'.")
303 (defconst org-element--parsed-properties-alist
304 (mapcar (lambda (k) (cons k
(intern (concat ":" (downcase k
)))))
305 org-element-parsed-keywords
)
306 "Alist of parsed keywords and associated properties.
307 This is generated from `org-element-parsed-keywords', which
310 (defconst org-element-dual-keywords
'("CAPTION" "RESULTS")
311 "List of affiliated keywords which can have a secondary value.
313 In Org syntax, they can be written with optional square brackets
314 before the colons. For example, RESULTS keyword can be
315 associated to a hash value with the following:
317 #+RESULTS[hash-string]: some-source
319 This list is checked after translations have been applied. See
320 `org-element-keyword-translation-alist'.")
322 (defconst org-element-document-properties
'("AUTHOR" "DATE" "TITLE")
323 "List of properties associated to the whole document.
324 Any keyword in this list will have its value parsed and stored as
325 a secondary string.")
327 (defconst org-element--affiliated-re
328 (format "[ \t]*#\\+\\(?:%s\\):\\(?: \\|$\\)"
330 ;; Dual affiliated keywords.
331 (format "\\(?1:%s\\)\\(?:\\[\\(.*\\)\\]\\)?"
332 (regexp-opt org-element-dual-keywords
))
334 ;; Regular affiliated keywords.
335 (format "\\(?1:%s\\)"
339 (member keyword org-element-dual-keywords
))
340 org-element-affiliated-keywords
)))
342 ;; Export attributes.
343 "\\(?1:ATTR_[-_A-Za-z0-9]+\\)"))
344 "Regexp matching any affiliated keyword.
346 Keyword name is put in match group 1. Moreover, if keyword
347 belongs to `org-element-dual-keywords', put the dual value in
350 Don't modify it, set `org-element-affiliated-keywords' instead.")
352 (defconst org-element-object-restrictions
353 (let* ((standard-set (remq 'table-cell org-element-all-objects
))
354 (standard-set-no-line-break (remq 'line-break standard-set
)))
355 `((bold ,@standard-set
)
356 (footnote-reference ,@standard-set
)
357 (headline ,@standard-set-no-line-break
)
358 (inlinetask ,@standard-set-no-line-break
)
359 (italic ,@standard-set
)
360 (item ,@standard-set-no-line-break
)
361 (keyword ,@(remq 'footnote-reference standard-set
))
362 ;; Ignore all links excepted plain links in a link description.
363 ;; Also ignore radio-targets and line breaks.
364 (link bold code entity export-snippet inline-babel-call inline-src-block
365 italic latex-fragment macro plain-link statistics-cookie
366 strike-through subscript superscript underline verbatim
)
367 (paragraph ,@standard-set
)
368 ;; Remove any variable object from radio target as it would
369 ;; prevent it from being properly recognized.
370 (radio-target bold code entity italic latex-fragment strike-through
371 subscript superscript underline superscript
)
372 (strike-through ,@standard-set
)
373 (subscript ,@standard-set
)
374 (superscript ,@standard-set
)
375 ;; Ignore inline babel call and inline src block as formulas are
376 ;; possible. Also ignore line breaks and statistics cookies.
377 (table-cell bold code entity export-snippet footnote-reference italic
378 latex-fragment link macro radio-target strike-through
379 subscript superscript target timestamp underline verbatim
)
380 (table-row table-cell
)
381 (underline ,@standard-set
)
382 (verse-block ,@standard-set
)))
383 "Alist of objects restrictions.
385 key is an element or object type containing objects and value is
386 a list of types that can be contained within an element or object
389 For example, in a `radio-target' object, one can only find
390 entities, latex-fragments, subscript, superscript and text
393 This alist also applies to secondary string. For example, an
394 `headline' type element doesn't directly contain objects, but
395 still has an entry since one of its properties (`:title') does.")
397 (defconst org-element-secondary-value-alist
398 '((headline .
:title
)
399 (inlinetask .
:title
)
401 "Alist between element types and location of secondary value.")
403 (defconst org-element--pair-square-table
404 (let ((table (make-syntax-table)))
405 (modify-syntax-entry ?\
[ "(]" table
)
406 (modify-syntax-entry ?\
] ")[" table
)
407 (dolist (char '(?\
{ ?\
} ?\
( ?\
) ?\
< ?\
>) table
)
408 (modify-syntax-entry char
" " table
)))
409 "Table used internally to pair only square brackets.
410 Other brackets are treated as spaces.")
414 ;;; Accessors and Setters
416 ;; Provide four accessors: `org-element-type', `org-element-property'
417 ;; `org-element-contents' and `org-element-restriction'.
419 ;; Setter functions allow to modify elements by side effect. There is
420 ;; `org-element-put-property', `org-element-set-contents'. These
421 ;; low-level functions are useful to build a parse tree.
423 ;; `org-element-adopt-element', `org-element-set-element',
424 ;; `org-element-extract-element' and `org-element-insert-before' are
425 ;; high-level functions useful to modify a parse tree.
427 ;; `org-element-secondary-p' is a predicate used to know if a given
428 ;; object belongs to a secondary string. `org-element-copy' returns
429 ;; an element or object, stripping its parent property in the process.
431 (defsubst org-element-type
(element)
432 "Return type of ELEMENT.
434 The function returns the type of the element or object provided.
435 It can also return the following special value:
436 `plain-text' for a string
437 `org-data' for a complete document
438 nil in any other case."
440 ((not (consp element
)) (and (stringp element
) 'plain-text
))
441 ((symbolp (car element
)) (car element
))))
443 (defsubst org-element-property
(property element
)
444 "Extract the value from the PROPERTY of an ELEMENT."
445 (if (stringp element
) (get-text-property 0 property element
)
446 (plist-get (nth 1 element
) property
)))
448 (defsubst org-element-contents
(element)
449 "Extract contents from an ELEMENT."
450 (cond ((not (consp element
)) nil
)
451 ((symbolp (car element
)) (nthcdr 2 element
))
454 (defsubst org-element-restriction
(element)
455 "Return restriction associated to ELEMENT.
456 ELEMENT can be an element, an object or a symbol representing an
457 element or object type."
458 (cdr (assq (if (symbolp element
) element
(org-element-type element
))
459 org-element-object-restrictions
)))
461 (defsubst org-element-put-property
(element property value
)
462 "In ELEMENT set PROPERTY to VALUE.
463 Return modified element."
464 (if (stringp element
) (org-add-props element nil property value
)
465 (setcar (cdr element
) (plist-put (nth 1 element
) property value
))
468 (defsubst org-element-set-contents
(element &rest contents
)
469 "Set ELEMENT contents to CONTENTS."
470 (cond ((not element
) (list contents
))
471 ((not (symbolp (car element
))) contents
)
472 ((cdr element
) (setcdr (cdr element
) contents
))
473 (t (nconc element contents
))))
475 (defun org-element-secondary-p (object)
476 "Non-nil when OBJECT belongs to a secondary string.
477 Return value is the property name, as a keyword, or nil."
478 (let* ((parent (org-element-property :parent object
))
479 (property (cdr (assq (org-element-type parent
)
480 org-element-secondary-value-alist
))))
482 (memq object
(org-element-property property parent
))
485 (defsubst org-element-adopt-elements
(parent &rest children
)
486 "Append elements to the contents of another element.
488 PARENT is an element or object. CHILDREN can be elements,
489 objects, or a strings.
491 The function takes care of setting `:parent' property for CHILD.
492 Return parent element."
493 ;; Link every child to PARENT. If PARENT is nil, it is a secondary
494 ;; string: parent is the list itself.
495 (mapc (lambda (child)
496 (org-element-put-property child
:parent
(or parent children
)))
498 ;; Add CHILDREN at the end of PARENT contents.
500 (apply 'org-element-set-contents
502 (nconc (org-element-contents parent
) children
)))
503 ;; Return modified PARENT element.
504 (or parent children
))
506 (defun org-element-extract-element (element)
507 "Extract ELEMENT from parse tree.
508 Remove element from the parse tree by side-effect, and return it
509 with its `:parent' property stripped out."
510 (let ((parent (org-element-property :parent element
))
511 (secondary (org-element-secondary-p element
)))
513 (org-element-put-property
515 (delq element
(org-element-property secondary parent
)))
516 (apply #'org-element-set-contents
518 (delq element
(org-element-contents parent
))))
519 ;; Return ELEMENT with its :parent removed.
520 (org-element-put-property element
:parent nil
)))
522 (defun org-element-insert-before (element location
)
523 "Insert ELEMENT before LOCATION in parse tree.
524 LOCATION is an element, object or string within the parse tree.
525 Parse tree is modified by side effect."
526 (let* ((parent (org-element-property :parent location
))
527 (property (org-element-secondary-p location
))
528 (siblings (if property
(org-element-property property parent
)
529 (org-element-contents parent
)))
530 ;; Special case: LOCATION is the first element of an
531 ;; independent secondary string (e.g. :title property). Add
533 (specialp (and (not property
)
535 (eq (car parent
) location
))))
536 ;; Install ELEMENT at the appropriate POSITION within SIBLINGS.
538 ((or (null siblings
) (eq (car siblings
) location
))
539 (push element siblings
))
540 ((null location
) (nconc siblings
(list element
)))
541 (t (let ((previous (cadr (memq location
(reverse siblings
)))))
543 (error "No location found to insert element")
544 (let ((next (memq previous siblings
)))
545 (setcdr next
(cons element
(cdr next
))))))))
546 ;; Store SIBLINGS at appropriate place in parse tree.
548 (specialp (setcdr parent
(copy-sequence parent
)) (setcar parent element
))
549 (property (org-element-put-property parent property siblings
))
550 (t (apply #'org-element-set-contents parent siblings
)))
551 ;; Set appropriate :parent property.
552 (org-element-put-property element
:parent parent
)))
554 (defun org-element-set-element (old new
)
555 "Replace element or object OLD with element or object NEW.
556 The function takes care of setting `:parent' property for NEW."
557 ;; Ensure OLD and NEW have the same parent.
558 (org-element-put-property new
:parent
(org-element-property :parent old
))
559 (if (or (memq (org-element-type old
) '(plain-text nil
))
560 (memq (org-element-type new
) '(plain-text nil
)))
561 ;; We cannot replace OLD with NEW since one of them is not an
562 ;; object or element. We take the long path.
563 (progn (org-element-insert-before new old
)
564 (org-element-extract-element old
))
565 ;; Since OLD is going to be changed into NEW by side-effect, first
566 ;; make sure that every element or object within NEW has OLD as
568 (dolist (blob (org-element-contents new
))
569 (org-element-put-property blob
:parent old
))
570 ;; Transfer contents.
571 (apply #'org-element-set-contents old
(org-element-contents new
))
572 ;; Overwrite OLD's properties with NEW's.
573 (setcar (cdr old
) (nth 1 new
))
575 (setcar old
(car new
))))
577 (defun org-element-copy (datum)
578 "Return a copy of DATUM.
579 DATUM is an element, object, string or nil. `:parent' property
580 is cleared and contents are removed in the process."
582 (let ((type (org-element-type datum
)))
584 (org-data (list 'org-data nil
))
585 (plain-text (substring-no-properties datum
))
586 ((nil) (copy-sequence datum
))
588 (list type
(plist-put (copy-sequence (nth 1 datum
)) :parent nil
)))))))
594 ;; For each greater element type, we define a parser and an
597 ;; A parser returns the element or object as the list described above.
598 ;; Most of them accepts no argument. Though, exceptions exist. Hence
599 ;; every element containing a secondary string (see
600 ;; `org-element-secondary-value-alist') will accept an optional
601 ;; argument to toggle parsing of that secondary string. Moreover,
602 ;; `item' parser requires current list's structure as its first
605 ;; An interpreter accepts two arguments: the list representation of
606 ;; the element or object, and its contents. The latter may be nil,
607 ;; depending on the element or object considered. It returns the
608 ;; appropriate Org syntax, as a string.
610 ;; Parsing functions must follow the naming convention:
611 ;; org-element-TYPE-parser, where TYPE is greater element's type, as
612 ;; defined in `org-element-greater-elements'.
614 ;; Similarly, interpreting functions must follow the naming
615 ;; convention: org-element-TYPE-interpreter.
617 ;; With the exception of `headline' and `item' types, greater elements
618 ;; cannot contain other greater elements of their own type.
620 ;; Beside implementing a parser and an interpreter, adding a new
621 ;; greater element requires to tweak `org-element--current-element'.
622 ;; Moreover, the newly defined type must be added to both
623 ;; `org-element-all-elements' and `org-element-greater-elements'.
628 (defun org-element-center-block-parser (limit affiliated
)
629 "Parse a center block.
631 LIMIT bounds the search. AFFILIATED is a list of which CAR is
632 the buffer position at the beginning of the first affiliated
633 keyword and CDR is a plist of affiliated keywords along with
636 Return a list whose CAR is `center-block' and CDR is a plist
637 containing `:begin', `:end', `:contents-begin', `:contents-end',
638 `:post-blank' and `:post-affiliated' keywords.
640 Assume point is at the beginning of the block."
641 (let ((case-fold-search t
))
642 (if (not (save-excursion
643 (re-search-forward "^[ \t]*#\\+END_CENTER[ \t]*$" limit t
)))
644 ;; Incomplete block: parse it as a paragraph.
645 (org-element-paragraph-parser limit affiliated
)
646 (let ((block-end-line (match-beginning 0)))
647 (let* ((begin (car affiliated
))
648 (post-affiliated (point))
649 ;; Empty blocks have no contents.
650 (contents-begin (progn (forward-line)
651 (and (< (point) block-end-line
)
653 (contents-end (and contents-begin block-end-line
))
654 (pos-before-blank (progn (goto-char block-end-line
)
658 (skip-chars-forward " \r\t\n" limit
)
659 (if (eobp) (point) (line-beginning-position)))))
664 :contents-begin contents-begin
665 :contents-end contents-end
666 :post-blank
(count-lines pos-before-blank end
)
667 :post-affiliated post-affiliated
)
668 (cdr affiliated
))))))))
670 (defun org-element-center-block-interpreter (center-block contents
)
671 "Interpret CENTER-BLOCK element as Org syntax.
672 CONTENTS is the contents of the element."
673 (format "#+BEGIN_CENTER\n%s#+END_CENTER" contents
))
678 (defun org-element-drawer-parser (limit affiliated
)
681 LIMIT bounds the search. AFFILIATED is a list of which CAR is
682 the buffer position at the beginning of the first affiliated
683 keyword and CDR is a plist of affiliated keywords along with
686 Return a list whose CAR is `drawer' and CDR is a plist containing
687 `:drawer-name', `:begin', `:end', `:contents-begin',
688 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
690 Assume point is at beginning of drawer."
691 (let ((case-fold-search t
))
692 (if (not (save-excursion (re-search-forward "^[ \t]*:END:[ \t]*$" limit t
)))
693 ;; Incomplete drawer: parse it as a paragraph.
694 (org-element-paragraph-parser limit affiliated
)
696 (let* ((drawer-end-line (match-beginning 0))
697 (name (progn (looking-at org-drawer-regexp
)
698 (org-match-string-no-properties 1)))
699 (begin (car affiliated
))
700 (post-affiliated (point))
701 ;; Empty drawers have no contents.
702 (contents-begin (progn (forward-line)
703 (and (< (point) drawer-end-line
)
705 (contents-end (and contents-begin drawer-end-line
))
706 (pos-before-blank (progn (goto-char drawer-end-line
)
709 (end (progn (skip-chars-forward " \r\t\n" limit
)
710 (if (eobp) (point) (line-beginning-position)))))
716 :contents-begin contents-begin
717 :contents-end contents-end
718 :post-blank
(count-lines pos-before-blank end
)
719 :post-affiliated post-affiliated
)
720 (cdr affiliated
))))))))
722 (defun org-element-drawer-interpreter (drawer contents
)
723 "Interpret DRAWER element as Org syntax.
724 CONTENTS is the contents of the element."
725 (format ":%s:\n%s:END:"
726 (org-element-property :drawer-name drawer
)
732 (defun org-element-dynamic-block-parser (limit affiliated
)
733 "Parse a dynamic block.
735 LIMIT bounds the search. AFFILIATED is a list of which CAR is
736 the buffer position at the beginning of the first affiliated
737 keyword and CDR is a plist of affiliated keywords along with
740 Return a list whose CAR is `dynamic-block' and CDR is a plist
741 containing `:block-name', `:begin', `:end', `:contents-begin',
742 `:contents-end', `:arguments', `:post-blank' and
743 `:post-affiliated' keywords.
745 Assume point is at beginning of dynamic block."
746 (let ((case-fold-search t
))
747 (if (not (save-excursion
748 (re-search-forward "^[ \t]*#\\+END:?[ \t]*$" limit t
)))
749 ;; Incomplete block: parse it as a paragraph.
750 (org-element-paragraph-parser limit affiliated
)
751 (let ((block-end-line (match-beginning 0)))
753 (let* ((name (progn (looking-at org-dblock-start-re
)
754 (org-match-string-no-properties 1)))
755 (arguments (org-match-string-no-properties 3))
756 (begin (car affiliated
))
757 (post-affiliated (point))
758 ;; Empty blocks have no contents.
759 (contents-begin (progn (forward-line)
760 (and (< (point) block-end-line
)
762 (contents-end (and contents-begin block-end-line
))
763 (pos-before-blank (progn (goto-char block-end-line
)
766 (end (progn (skip-chars-forward " \r\t\n" limit
)
767 (if (eobp) (point) (line-beginning-position)))))
774 :contents-begin contents-begin
775 :contents-end contents-end
776 :post-blank
(count-lines pos-before-blank end
)
777 :post-affiliated post-affiliated
)
778 (cdr affiliated
)))))))))
780 (defun org-element-dynamic-block-interpreter (dynamic-block contents
)
781 "Interpret DYNAMIC-BLOCK element as Org syntax.
782 CONTENTS is the contents of the element."
783 (format "#+BEGIN: %s%s\n%s#+END:"
784 (org-element-property :block-name dynamic-block
)
785 (let ((args (org-element-property :arguments dynamic-block
)))
786 (and args
(concat " " args
)))
790 ;;;; Footnote Definition
792 (defun org-element-footnote-definition-parser (limit affiliated
)
793 "Parse a footnote definition.
795 LIMIT bounds the search. AFFILIATED is a list of which CAR is
796 the buffer position at the beginning of the first affiliated
797 keyword and CDR is a plist of affiliated keywords along with
800 Return a list whose CAR is `footnote-definition' and CDR is
801 a plist containing `:label', `:begin' `:end', `:contents-begin',
802 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
804 Assume point is at the beginning of the footnote definition."
806 (let* ((label (progn (looking-at org-footnote-definition-re
)
807 (org-match-string-no-properties 1)))
808 (begin (car affiliated
))
809 (post-affiliated (point))
810 (ending (save-excursion
814 (concat org-outline-regexp-bol
"\\|"
815 org-footnote-definition-re
"\\|"
816 "^\\([ \t]*\n\\)\\{2,\\}") limit
'move
))
819 (contents-begin (progn
821 (skip-chars-forward " \r\t\n" ending
)
822 (cond ((= (point) ending
) nil
)
823 ((= (line-beginning-position) begin
) (point))
824 (t (line-beginning-position)))))
825 (contents-end (and contents-begin ending
))
826 (end (progn (goto-char ending
)
827 (skip-chars-forward " \r\t\n" limit
)
828 (if (eobp) (point) (line-beginning-position)))))
829 (list 'footnote-definition
834 :contents-begin contents-begin
835 :contents-end contents-end
836 :post-blank
(count-lines ending end
)
837 :post-affiliated post-affiliated
)
838 (cdr affiliated
))))))
840 (defun org-element-footnote-definition-interpreter (footnote-definition contents
)
841 "Interpret FOOTNOTE-DEFINITION element as Org syntax.
842 CONTENTS is the contents of the footnote-definition."
843 (concat (format "[%s]" (org-element-property :label footnote-definition
))
850 (defun org-element--get-node-properties ()
851 "Return node properties associated to headline at point.
852 Upcase property names. It avoids confusion between properties
853 obtained through property drawer and default properties from the
854 parser (e.g. `:end' and :END:). Return value is a plist."
857 (when (org-looking-at-p org-planning-line-re
) (forward-line))
858 (when (looking-at org-property-drawer-re
)
860 (let ((end (match-end 0)) properties
)
861 (while (< (line-end-position) end
)
862 (looking-at org-property-re
)
863 (push (org-match-string-no-properties 3) properties
)
864 (push (intern (concat ":" (upcase (match-string 2)))) properties
)
868 (defun org-element--get-time-properties ()
869 "Return time properties associated to headline at point.
870 Return value is a plist."
872 (when (progn (forward-line) (looking-at org-planning-line-re
))
873 (let ((end (line-end-position)) plist
)
874 (while (re-search-forward org-keyword-time-not-clock-regexp end t
)
875 (goto-char (match-end 1))
876 (skip-chars-forward " \t")
877 (let ((keyword (match-string 1))
878 (time (org-element-timestamp-parser)))
879 (cond ((equal keyword org-scheduled-string
)
880 (setq plist
(plist-put plist
:scheduled time
)))
881 ((equal keyword org-deadline-string
)
882 (setq plist
(plist-put plist
:deadline time
)))
883 (t (setq plist
(plist-put plist
:closed time
))))))
886 (defun org-element-headline-parser (limit &optional raw-secondary-p
)
889 Return a list whose CAR is `headline' and CDR is a plist
890 containing `:raw-value', `:title', `:begin', `:end',
891 `:pre-blank', `:contents-begin' and `:contents-end', `:level',
892 `:priority', `:tags', `:todo-keyword',`:todo-type', `:scheduled',
893 `:deadline', `:closed', `:archivedp', `:commentedp'
894 `:footnote-section-p', `:post-blank' and `:post-affiliated'
897 The plist also contains any property set in the property drawer,
898 with its name in upper cases and colons added at the
899 beginning (e.g., `:CUSTOM_ID').
901 LIMIT is a buffer position bounding the search.
903 When RAW-SECONDARY-P is non-nil, headline's title will not be
904 parsed as a secondary string, but as a plain string instead.
906 Assume point is at beginning of the headline."
908 (let* ((begin (point))
909 (level (prog1 (org-reduced-level (skip-chars-forward "*"))
910 (skip-chars-forward " \t")))
911 (todo (and org-todo-regexp
912 (let (case-fold-search) (looking-at org-todo-regexp
))
913 (progn (goto-char (match-end 0))
914 (skip-chars-forward " \t")
917 (and todo
(if (member todo org-done-keywords
) 'done
'todo
)))
918 (priority (and (looking-at "\\[#.\\][ \t]*")
919 (progn (goto-char (match-end 0))
920 (aref (match-string 0) 2))))
922 (and (let (case-fold-search) (looking-at org-comment-string
))
923 (goto-char (match-end 0))))
924 (title-start (point))
925 (tags (when (re-search-forward
926 (org-re "[ \t]+\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
929 (goto-char (match-beginning 0))
930 (org-split-string (match-string 1) ":")))
933 (buffer-substring-no-properties title-start title-end
)))
934 (archivedp (member org-archive-tag tags
))
935 (footnote-section-p (and org-footnote-section
936 (string= org-footnote-section raw-value
)))
937 (standard-props (org-element--get-node-properties))
938 (time-props (org-element--get-time-properties))
939 (end (min (save-excursion (org-end-of-subtree t t
)) limit
))
940 (pos-after-head (progn (forward-line) (point)))
941 (contents-begin (save-excursion
942 (skip-chars-forward " \r\t\n" end
)
943 (and (/= (point) end
) (line-beginning-position))))
944 (contents-end (and contents-begin
945 (progn (goto-char end
)
946 (skip-chars-backward " \r\t\n")
949 ;; Clean TAGS from archive tag, if any.
950 (when archivedp
(setq tags
(delete org-archive-tag tags
)))
954 (list :raw-value raw-value
958 (if (not contents-begin
) 0
959 (count-lines pos-after-head contents-begin
))
960 :contents-begin contents-begin
961 :contents-end contents-end
967 :post-blank
(count-lines
968 (or contents-end pos-after-head
)
970 :footnote-section-p footnote-section-p
972 :commentedp commentedp
973 :post-affiliated begin
)
976 (org-element-put-property
978 (if raw-secondary-p raw-value
979 (let ((title (org-element--parse-objects
980 (progn (goto-char title-start
)
981 (skip-chars-forward " \t")
983 (progn (goto-char title-end
)
984 (skip-chars-backward " \t")
987 (org-element-restriction 'headline
))))
988 (dolist (datum title title
)
989 (org-element-put-property datum
:parent headline
)))))))))
991 (defun org-element-headline-interpreter (headline contents
)
992 "Interpret HEADLINE element as Org syntax.
993 CONTENTS is the contents of the element."
994 (let* ((level (org-element-property :level headline
))
995 (todo (org-element-property :todo-keyword headline
))
996 (priority (org-element-property :priority headline
))
997 (title (org-element-interpret-data
998 (org-element-property :title headline
)))
999 (tags (let ((tag-list (if (org-element-property :archivedp headline
)
1000 (cons org-archive-tag
1001 (org-element-property :tags headline
))
1002 (org-element-property :tags headline
))))
1004 (format ":%s:" (mapconcat #'identity tag-list
":")))))
1005 (commentedp (org-element-property :commentedp headline
))
1006 (pre-blank (or (org-element-property :pre-blank headline
) 0))
1008 (concat (make-string (if org-odd-levels-only
(1- (* level
2)) level
)
1010 (and todo
(concat " " todo
))
1011 (and commentedp
(concat " " org-comment-string
))
1012 (and priority
(format " [#%c]" priority
))
1014 (if (and org-footnote-section
1015 (org-element-property :footnote-section-p headline
))
1016 org-footnote-section
1023 ((zerop org-tags-column
) (format " %s" tags
))
1024 ((< org-tags-column
0)
1027 (max (- (+ org-tags-column
(length heading
) (length tags
))) 1)
1032 (make-string (max (- org-tags-column
(length heading
)) 1) ?\s
)
1034 (make-string (1+ pre-blank
) ?
\n)
1040 (defun org-element-inlinetask-parser (limit &optional raw-secondary-p
)
1041 "Parse an inline task.
1043 Return a list whose CAR is `inlinetask' and CDR is a plist
1044 containing `:title', `:begin', `:end', `:contents-begin' and
1045 `:contents-end', `:level', `:priority', `:raw-value', `:tags',
1046 `:todo-keyword', `:todo-type', `:scheduled', `:deadline',
1047 `:closed', `:post-blank' and `:post-affiliated' keywords.
1049 The plist also contains any property set in the property drawer,
1050 with its name in upper cases and colons added at the
1051 beginning (e.g., `:CUSTOM_ID').
1053 When optional argument RAW-SECONDARY-P is non-nil, inline-task's
1054 title will not be parsed as a secondary string, but as a plain
1057 Assume point is at beginning of the inline task."
1059 (let* ((begin (point))
1060 (level (prog1 (org-reduced-level (skip-chars-forward "*"))
1061 (skip-chars-forward " \t")))
1062 (todo (and org-todo-regexp
1063 (let (case-fold-search) (looking-at org-todo-regexp
))
1064 (progn (goto-char (match-end 0))
1065 (skip-chars-forward " \t")
1067 (todo-type (and todo
1068 (if (member todo org-done-keywords
) 'done
'todo
)))
1069 (priority (and (looking-at "\\[#.\\][ \t]*")
1070 (progn (goto-char (match-end 0))
1071 (aref (match-string 0) 2))))
1072 (title-start (point))
1073 (tags (when (re-search-forward
1074 (org-re "[ \t]+\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
1077 (goto-char (match-beginning 0))
1078 (org-split-string (match-string 1) ":")))
1080 (raw-value (org-trim
1081 (buffer-substring-no-properties title-start title-end
)))
1082 (task-end (save-excursion
1084 (and (re-search-forward org-outline-regexp-bol limit t
)
1085 (org-looking-at-p "END[ \t]*$")
1086 (line-beginning-position))))
1087 (standard-props (and task-end
(org-element--get-node-properties)))
1088 (time-props (and task-end
(org-element--get-time-properties)))
1089 (contents-begin (progn (forward-line)
1090 (and task-end
(< (point) task-end
) (point))))
1091 (contents-end (and contents-begin task-end
))
1092 (before-blank (if (not task-end
) (point)
1093 (goto-char task-end
)
1096 (end (progn (skip-chars-forward " \r\t\n" limit
)
1097 (if (eobp) (point) (line-beginning-position))))
1101 (list :raw-value raw-value
1104 :contents-begin contents-begin
1105 :contents-end contents-end
1110 :todo-type todo-type
1111 :post-blank
(count-lines before-blank end
)
1112 :post-affiliated begin
)
1115 (org-element-put-property
1117 (if raw-secondary-p raw-value
1118 (let ((title (org-element--parse-objects
1119 (progn (goto-char title-start
)
1120 (skip-chars-forward " \t")
1122 (progn (goto-char title-end
)
1123 (skip-chars-backward " \t")
1126 (org-element-restriction 'inlinetask
))))
1127 (dolist (datum title title
)
1128 (org-element-put-property datum
:parent inlinetask
))))))))
1130 (defun org-element-inlinetask-interpreter (inlinetask contents
)
1131 "Interpret INLINETASK element as Org syntax.
1132 CONTENTS is the contents of inlinetask."
1133 (let* ((level (org-element-property :level inlinetask
))
1134 (todo (org-element-property :todo-keyword inlinetask
))
1135 (priority (org-element-property :priority inlinetask
))
1136 (title (org-element-interpret-data
1137 (org-element-property :title inlinetask
)))
1138 (tags (let ((tag-list (org-element-property :tags inlinetask
)))
1140 (format ":%s:" (mapconcat 'identity tag-list
":")))))
1141 (task (concat (make-string level ?
*)
1142 (and todo
(concat " " todo
))
1143 (and priority
(format " [#%c]" priority
))
1144 (and title
(concat " " title
)))))
1149 ((zerop org-tags-column
) (format " %s" tags
))
1150 ((< org-tags-column
0)
1153 (max (- (+ org-tags-column
(length task
) (length tags
))) 1)
1158 (make-string (max (- org-tags-column
(length task
)) 1) ?
)
1160 ;; Prefer degenerate inlinetasks when there are no
1165 (make-string level ?
*) " END")))))
1170 (defun org-element-item-parser (limit struct
&optional raw-secondary-p
)
1173 STRUCT is the structure of the plain list.
1175 Return a list whose CAR is `item' and CDR is a plist containing
1176 `:bullet', `:begin', `:end', `:contents-begin', `:contents-end',
1177 `:checkbox', `:counter', `:tag', `:structure', `:post-blank' and
1178 `:post-affiliated' keywords.
1180 When optional argument RAW-SECONDARY-P is non-nil, item's tag, if
1181 any, will not be parsed as a secondary string, but as a plain
1184 Assume point is at the beginning of the item."
1187 (looking-at org-list-full-item-re
)
1188 (let* ((begin (point))
1189 (bullet (org-match-string-no-properties 1))
1190 (checkbox (let ((box (org-match-string-no-properties 3)))
1191 (cond ((equal "[ ]" box
) 'off
)
1192 ((equal "[X]" box
) 'on
)
1193 ((equal "[-]" box
) 'trans
))))
1194 (counter (let ((c (org-match-string-no-properties 2)))
1198 ((string-match "[A-Za-z]" c
)
1199 (- (string-to-char (upcase (match-string 0 c
)))
1201 ((string-match "[0-9]+" c
)
1202 (string-to-number (match-string 0 c
)))))))
1203 (end (progn (goto-char (nth 6 (assq (point) struct
)))
1204 (unless (bolp) (forward-line))
1208 ;; Ignore tags in un-ordered lists: they are just
1209 ;; a part of item's body.
1210 (if (and (match-beginning 4)
1211 (save-match-data (string-match "[.)]" bullet
)))
1214 (skip-chars-forward " \r\t\n" limit
)
1215 ;; If first line isn't empty, contents really start
1216 ;; at the text after item's meta-data.
1217 (if (= (point-at-bol) begin
) (point) (point-at-bol))))
1218 (contents-end (progn (goto-char end
)
1219 (skip-chars-backward " \r\t\n")
1224 (list :bullet bullet
1227 ;; CONTENTS-BEGIN and CONTENTS-END may be
1228 ;; mixed up in the case of an empty item
1229 ;; separated from the next by a blank line.
1230 ;; Thus ensure the former is always the
1232 :contents-begin
(min contents-begin contents-end
)
1233 :contents-end
(max contents-begin contents-end
)
1237 :post-blank
(count-lines contents-end end
)
1238 :post-affiliated begin
))))
1239 (org-element-put-property
1241 (let ((raw (org-list-get-tag begin struct
)))
1243 (if raw-secondary-p raw
1244 (let ((tag (org-element--parse-objects
1245 (match-beginning 4) (match-end 4) nil
1246 (org-element-restriction 'item
))))
1247 (dolist (datum tag tag
)
1248 (org-element-put-property datum
:parent item
))))))))))
1250 (defun org-element-item-interpreter (item contents
)
1251 "Interpret ITEM element as Org syntax.
1252 CONTENTS is the contents of the element."
1253 (let* ((bullet (let ((bullet (org-element-property :bullet item
)))
1254 (org-list-bullet-string
1255 (cond ((not (string-match "[0-9a-zA-Z]" bullet
)) "- ")
1256 ((eq org-plain-list-ordered-item-terminator ?\
)) "1)")
1258 (checkbox (org-element-property :checkbox item
))
1259 (counter (org-element-property :counter item
))
1260 (tag (let ((tag (org-element-property :tag item
)))
1261 (and tag
(org-element-interpret-data tag
))))
1262 ;; Compute indentation.
1263 (ind (make-string (length bullet
) 32))
1264 (item-starts-with-par-p
1265 (eq (org-element-type (car (org-element-contents item
)))
1270 (and counter
(format "[@%d] " counter
))
1275 (and tag
(format "%s :: " tag
))
1277 (let ((contents (replace-regexp-in-string
1278 "\\(^\\)[ \t]*\\S-" ind contents nil nil
1)))
1279 (if item-starts-with-par-p
(org-trim contents
)
1280 (concat "\n" contents
)))))))
1285 (defun org-element--list-struct (limit)
1286 ;; Return structure of list at point. Internal function. See
1287 ;; `org-list-struct' for details.
1288 (let ((case-fold-search t
)
1290 (item-re (org-item-re))
1291 (inlinetask-re (and (featurep 'org-inlinetask
) "^\\*+ "))
1297 ;; At limit: end all items.
1300 (let ((end (progn (skip-chars-backward " \r\t\n")
1303 (dolist (item items
(sort (nconc items struct
)
1304 'car-less-than-car
))
1305 (setcar (nthcdr 6 item
) end
)))))
1306 ;; At list end: end all items.
1307 ((looking-at org-list-end-re
)
1308 (throw 'exit
(dolist (item items
(sort (nconc items struct
)
1309 'car-less-than-car
))
1310 (setcar (nthcdr 6 item
) (point)))))
1311 ;; At a new item: end previous sibling.
1312 ((looking-at item-re
)
1313 (let ((ind (save-excursion (skip-chars-forward " \t")
1315 (setq top-ind
(min top-ind ind
))
1316 (while (and items
(<= ind
(nth 1 (car items
))))
1317 (let ((item (pop items
)))
1318 (setcar (nthcdr 6 item
) (point))
1319 (push item struct
)))
1320 (push (progn (looking-at org-list-full-item-re
)
1321 (let ((bullet (match-string-no-properties 1)))
1325 (match-string-no-properties 2) ; counter
1326 (match-string-no-properties 3) ; checkbox
1328 (and (save-match-data
1329 (string-match "[-+*]" bullet
))
1330 (match-string-no-properties 4))
1331 ;; Ending position, unknown so far.
1335 ;; Skip empty lines.
1336 ((looking-at "^[ \t]*$") (forward-line))
1337 ;; Skip inline tasks and blank lines along the way.
1338 ((and inlinetask-re
(looking-at inlinetask-re
))
1340 (let ((origin (point)))
1341 (when (re-search-forward inlinetask-re limit t
)
1342 (if (org-looking-at-p "END[ \t]*$") (forward-line)
1343 (goto-char origin
)))))
1344 ;; At some text line. Check if it ends any previous item.
1346 (let ((ind (progn (skip-chars-forward " \t") (current-column))))
1347 (when (<= ind top-ind
)
1348 (skip-chars-backward " \r\t\n")
1350 (while (<= ind
(nth 1 (car items
)))
1351 (let ((item (pop items
)))
1352 (setcar (nthcdr 6 item
) (line-beginning-position))
1355 (throw 'exit
(sort struct
'car-less-than-car
))))))
1356 ;; Skip blocks (any type) and drawers contents.
1358 ((and (looking-at "#\\+BEGIN\\(:\\|_\\S-+\\)")
1360 (format "^[ \t]*#\\+END%s[ \t]*$" (match-string 1))
1362 ((and (looking-at org-drawer-regexp
)
1363 (re-search-forward "^[ \t]*:END:[ \t]*$" limit t
))))
1364 (forward-line))))))))
1366 (defun org-element-plain-list-parser (limit affiliated structure
)
1367 "Parse a plain list.
1369 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1370 the buffer position at the beginning of the first affiliated
1371 keyword and CDR is a plist of affiliated keywords along with
1372 their value. STRUCTURE is the structure of the plain list being
1375 Return a list whose CAR is `plain-list' and CDR is a plist
1376 containing `:type', `:begin', `:end', `:contents-begin' and
1377 `:contents-end', `:structure', `:post-blank' and
1378 `:post-affiliated' keywords.
1380 Assume point is at the beginning of the list."
1382 (let* ((struct (or structure
(org-element--list-struct limit
)))
1383 (type (cond ((org-looking-at-p "[ \t]*[A-Za-z0-9]") 'ordered
)
1384 ((nth 5 (assq (point) struct
)) 'descriptive
)
1386 (contents-begin (point))
1387 (begin (car affiliated
))
1388 (contents-end (let* ((item (assq contents-begin struct
))
1391 (while (and (setq item
(assq pos struct
))
1392 (= (nth 1 item
) ind
))
1393 (setq pos
(nth 6 item
)))
1395 (end (progn (goto-char contents-end
)
1396 (skip-chars-forward " \r\t\n" limit
)
1397 (if (= (point) limit
) limit
(line-beginning-position)))))
1404 :contents-begin contents-begin
1405 :contents-end contents-end
1407 :post-blank
(count-lines contents-end end
)
1408 :post-affiliated contents-begin
)
1409 (cdr affiliated
))))))
1411 (defun org-element-plain-list-interpreter (plain-list contents
)
1412 "Interpret PLAIN-LIST element as Org syntax.
1413 CONTENTS is the contents of the element."
1416 (goto-char (point-min))
1421 ;;;; Property Drawer
1423 (defun org-element-property-drawer-parser (limit)
1424 "Parse a property drawer.
1426 LIMIT bounds the search.
1428 Return a list whose car is `property-drawer' and cdr is a plist
1429 containing `:begin', `:end', `:contents-begin', `:contents-end',
1430 `:post-blank' and `:post-affiliated' keywords.
1432 Assume point is at the beginning of the property drawer."
1434 (let ((case-fold-search t
)
1436 (contents-begin (line-beginning-position 2)))
1437 (re-search-forward "^[ \t]*:END:[ \t]*$" limit t
)
1438 (let ((contents-end (and (> (match-beginning 0) contents-begin
)
1439 (match-beginning 0)))
1440 (before-blank (progn (forward-line) (point)))
1441 (end (progn (skip-chars-forward " \r\t\n" limit
)
1442 (if (eobp) (point) (line-beginning-position)))))
1443 (list 'property-drawer
1446 :contents-begin
(and contents-end contents-begin
)
1447 :contents-end contents-end
1448 :post-blank
(count-lines before-blank end
)
1449 :post-affiliated begin
))))))
1451 (defun org-element-property-drawer-interpreter (property-drawer contents
)
1452 "Interpret PROPERTY-DRAWER element as Org syntax.
1453 CONTENTS is the properties within the drawer."
1454 (format ":PROPERTIES:\n%s:END:" contents
))
1459 (defun org-element-quote-block-parser (limit affiliated
)
1460 "Parse a quote block.
1462 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1463 the buffer position at the beginning of the first affiliated
1464 keyword and CDR is a plist of affiliated keywords along with
1467 Return a list whose CAR is `quote-block' and CDR is a plist
1468 containing `:begin', `:end', `:contents-begin', `:contents-end',
1469 `:post-blank' and `:post-affiliated' keywords.
1471 Assume point is at the beginning of the block."
1472 (let ((case-fold-search t
))
1473 (if (not (save-excursion
1474 (re-search-forward "^[ \t]*#\\+END_QUOTE[ \t]*$" limit t
)))
1475 ;; Incomplete block: parse it as a paragraph.
1476 (org-element-paragraph-parser limit affiliated
)
1477 (let ((block-end-line (match-beginning 0)))
1479 (let* ((begin (car affiliated
))
1480 (post-affiliated (point))
1481 ;; Empty blocks have no contents.
1482 (contents-begin (progn (forward-line)
1483 (and (< (point) block-end-line
)
1485 (contents-end (and contents-begin block-end-line
))
1486 (pos-before-blank (progn (goto-char block-end-line
)
1489 (end (progn (skip-chars-forward " \r\t\n" limit
)
1490 (if (eobp) (point) (line-beginning-position)))))
1495 :contents-begin contents-begin
1496 :contents-end contents-end
1497 :post-blank
(count-lines pos-before-blank end
)
1498 :post-affiliated post-affiliated
)
1499 (cdr affiliated
)))))))))
1501 (defun org-element-quote-block-interpreter (quote-block contents
)
1502 "Interpret QUOTE-BLOCK element as Org syntax.
1503 CONTENTS is the contents of the element."
1504 (format "#+BEGIN_QUOTE\n%s#+END_QUOTE" contents
))
1509 (defun org-element-section-parser (limit)
1512 LIMIT bounds the search.
1514 Return a list whose CAR is `section' and CDR is a plist
1515 containing `:begin', `:end', `:contents-begin', `contents-end',
1516 `:post-blank' and `:post-affiliated' keywords."
1518 ;; Beginning of section is the beginning of the first non-blank
1519 ;; line after previous headline.
1520 (let ((begin (point))
1521 (end (progn (org-with-limited-levels (outline-next-heading))
1523 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
1529 :contents-begin begin
1530 :contents-end pos-before-blank
1531 :post-blank
(count-lines pos-before-blank end
)
1532 :post-affiliated begin
)))))
1534 (defun org-element-section-interpreter (section contents
)
1535 "Interpret SECTION element as Org syntax.
1536 CONTENTS is the contents of the element."
1542 (defun org-element-special-block-parser (limit affiliated
)
1543 "Parse a special block.
1545 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1546 the buffer position at the beginning of the first affiliated
1547 keyword and CDR is a plist of affiliated keywords along with
1550 Return a list whose CAR is `special-block' and CDR is a plist
1551 containing `:type', `:begin', `:end', `:contents-begin',
1552 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
1554 Assume point is at the beginning of the block."
1555 (let* ((case-fold-search t
)
1556 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
1557 (match-string-no-properties 1))))
1558 (if (not (save-excursion
1560 (format "^[ \t]*#\\+END_%s[ \t]*$" (regexp-quote type
))
1562 ;; Incomplete block: parse it as a paragraph.
1563 (org-element-paragraph-parser limit affiliated
)
1564 (let ((block-end-line (match-beginning 0)))
1566 (let* ((begin (car affiliated
))
1567 (post-affiliated (point))
1568 ;; Empty blocks have no contents.
1569 (contents-begin (progn (forward-line)
1570 (and (< (point) block-end-line
)
1572 (contents-end (and contents-begin block-end-line
))
1573 (pos-before-blank (progn (goto-char block-end-line
)
1576 (end (progn (skip-chars-forward " \r\t\n" limit
)
1577 (if (eobp) (point) (line-beginning-position)))))
1578 (list 'special-block
1583 :contents-begin contents-begin
1584 :contents-end contents-end
1585 :post-blank
(count-lines pos-before-blank end
)
1586 :post-affiliated post-affiliated
)
1587 (cdr affiliated
)))))))))
1589 (defun org-element-special-block-interpreter (special-block contents
)
1590 "Interpret SPECIAL-BLOCK element as Org syntax.
1591 CONTENTS is the contents of the element."
1592 (let ((block-type (org-element-property :type special-block
)))
1593 (format "#+BEGIN_%s\n%s#+END_%s" block-type contents block-type
)))
1599 ;; For each element, a parser and an interpreter are also defined.
1600 ;; Both follow the same naming convention used for greater elements.
1602 ;; Also, as for greater elements, adding a new element type is done
1603 ;; through the following steps: implement a parser and an interpreter,
1604 ;; tweak `org-element--current-element' so that it recognizes the new
1605 ;; type and add that new type to `org-element-all-elements'.
1607 ;; As a special case, when the newly defined type is a block type,
1608 ;; `org-element-block-name-alist' has to be modified accordingly.
1613 (defun org-element-babel-call-parser (limit affiliated
)
1614 "Parse a babel call.
1616 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1617 the buffer position at the beginning of the first affiliated
1618 keyword and CDR is a plist of affiliated keywords along with
1621 Return a list whose CAR is `babel-call' and CDR is a plist
1622 containing `:begin', `:end', `:value', `:post-blank' and
1623 `:post-affiliated' as keywords."
1625 (let ((begin (car affiliated
))
1626 (post-affiliated (point))
1627 (value (progn (let ((case-fold-search t
))
1628 (re-search-forward "call:[ \t]*" nil t
))
1629 (buffer-substring-no-properties (point)
1630 (line-end-position))))
1631 (pos-before-blank (progn (forward-line) (point)))
1632 (end (progn (skip-chars-forward " \r\t\n" limit
)
1633 (if (eobp) (point) (line-beginning-position)))))
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 contents
)
1644 "Interpret BABEL-CALL element as Org syntax.
1646 (concat "#+CALL: " (org-element-property :value babel-call
)))
1651 (defun org-element-clock-parser (limit)
1654 LIMIT bounds the search.
1656 Return a list whose CAR is `clock' and CDR is a plist containing
1657 `:status', `:value', `:time', `:begin', `:end', `:post-blank' and
1658 `:post-affiliated' as keywords."
1660 (let* ((case-fold-search nil
)
1662 (value (progn (search-forward org-clock-string
(line-end-position) t
)
1663 (skip-chars-forward " \t")
1664 (org-element-timestamp-parser)))
1665 (duration (and (search-forward " => " (line-end-position) t
)
1666 (progn (skip-chars-forward " \t")
1667 (looking-at "\\(\\S-+\\)[ \t]*$"))
1668 (org-match-string-no-properties 1)))
1669 (status (if duration
'closed
'running
))
1670 (post-blank (let ((before-blank (progn (forward-line) (point))))
1671 (skip-chars-forward " \r\t\n" limit
)
1672 (skip-chars-backward " \t")
1673 (unless (bolp) (end-of-line))
1674 (count-lines before-blank
(point))))
1677 (list :status status
1682 :post-blank post-blank
1683 :post-affiliated begin
)))))
1685 (defun org-element-clock-interpreter (clock contents
)
1686 "Interpret CLOCK element as Org syntax.
1688 (concat org-clock-string
" "
1689 (org-element-timestamp-interpreter
1690 (org-element-property :value clock
) nil
)
1691 (let ((duration (org-element-property :duration clock
)))
1696 (org-split-string duration
":")))))))
1701 (defun org-element-comment-parser (limit affiliated
)
1704 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1705 the buffer position at the beginning of the first affiliated
1706 keyword and CDR is a plist of affiliated keywords along with
1709 Return a list whose CAR is `comment' and CDR is a plist
1710 containing `:begin', `:end', `:value', `:post-blank',
1711 `:post-affiliated' keywords.
1713 Assume point is at comment beginning."
1715 (let* ((begin (car affiliated
))
1716 (post-affiliated (point))
1717 (value (prog2 (looking-at "[ \t]*# ?")
1718 (buffer-substring-no-properties
1719 (match-end 0) (line-end-position))
1722 ;; Get comments ending.
1724 (while (and (< (point) limit
) (looking-at "[ \t]*#\\( \\|$\\)"))
1725 ;; Accumulate lines without leading hash and first
1730 (buffer-substring-no-properties
1731 (match-end 0) (line-end-position))))
1734 (end (progn (goto-char com-end
)
1735 (skip-chars-forward " \r\t\n" limit
)
1736 (if (eobp) (point) (line-beginning-position)))))
1742 :post-blank
(count-lines com-end end
)
1743 :post-affiliated post-affiliated
)
1744 (cdr affiliated
))))))
1746 (defun org-element-comment-interpreter (comment contents
)
1747 "Interpret COMMENT element as Org syntax.
1749 (replace-regexp-in-string "^" "# " (org-element-property :value comment
)))
1754 (defun org-element-comment-block-parser (limit affiliated
)
1755 "Parse an export block.
1757 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1758 the buffer position at the beginning of the first affiliated
1759 keyword and CDR is a plist of affiliated keywords along with
1762 Return a list whose CAR is `comment-block' and CDR is a plist
1763 containing `:begin', `:end', `:value', `:post-blank' and
1764 `:post-affiliated' keywords.
1766 Assume point is at comment block beginning."
1767 (let ((case-fold-search t
))
1768 (if (not (save-excursion
1769 (re-search-forward "^[ \t]*#\\+END_COMMENT[ \t]*$" limit t
)))
1770 ;; Incomplete block: parse it as a paragraph.
1771 (org-element-paragraph-parser limit affiliated
)
1772 (let ((contents-end (match-beginning 0)))
1774 (let* ((begin (car affiliated
))
1775 (post-affiliated (point))
1776 (contents-begin (progn (forward-line) (point)))
1777 (pos-before-blank (progn (goto-char contents-end
)
1780 (end (progn (skip-chars-forward " \r\t\n" limit
)
1781 (if (eobp) (point) (line-beginning-position))))
1782 (value (buffer-substring-no-properties
1783 contents-begin contents-end
)))
1784 (list 'comment-block
1789 :post-blank
(count-lines pos-before-blank end
)
1790 :post-affiliated post-affiliated
)
1791 (cdr affiliated
)))))))))
1793 (defun org-element-comment-block-interpreter (comment-block contents
)
1794 "Interpret COMMENT-BLOCK element as Org syntax.
1796 (format "#+BEGIN_COMMENT\n%s#+END_COMMENT"
1797 (org-element-normalize-string
1798 (org-remove-indentation
1799 (org-element-property :value comment-block
)))))
1804 (defun org-element-diary-sexp-parser (limit affiliated
)
1805 "Parse a diary sexp.
1807 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1808 the buffer position at the beginning of the first affiliated
1809 keyword and CDR is a plist of affiliated keywords along with
1812 Return a list whose CAR is `diary-sexp' and CDR is a plist
1813 containing `:begin', `:end', `:value', `:post-blank' and
1814 `:post-affiliated' keywords."
1816 (let ((begin (car affiliated
))
1817 (post-affiliated (point))
1818 (value (progn (looking-at "\\(%%(.*\\)[ \t]*$")
1819 (org-match-string-no-properties 1)))
1820 (pos-before-blank (progn (forward-line) (point)))
1821 (end (progn (skip-chars-forward " \r\t\n" limit
)
1822 (if (eobp) (point) (line-beginning-position)))))
1828 :post-blank
(count-lines pos-before-blank end
)
1829 :post-affiliated post-affiliated
)
1830 (cdr affiliated
))))))
1832 (defun org-element-diary-sexp-interpreter (diary-sexp contents
)
1833 "Interpret DIARY-SEXP as Org syntax.
1835 (org-element-property :value diary-sexp
))
1840 (defun org-element-example-block-parser (limit affiliated
)
1841 "Parse an example block.
1843 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1844 the buffer position at the beginning of the first affiliated
1845 keyword and CDR is a plist of affiliated keywords along with
1848 Return a list whose CAR is `example-block' and CDR is a plist
1849 containing `:begin', `:end', `:number-lines', `:preserve-indent',
1850 `:retain-labels', `:use-labels', `:label-fmt', `:switches',
1851 `:value', `:post-blank' and `:post-affiliated' keywords."
1852 (let ((case-fold-search t
))
1853 (if (not (save-excursion
1854 (re-search-forward "^[ \t]*#\\+END_EXAMPLE[ \t]*$" limit t
)))
1855 ;; Incomplete block: parse it as a paragraph.
1856 (org-element-paragraph-parser limit affiliated
)
1857 (let ((contents-end (match-beginning 0)))
1861 (looking-at "^[ \t]*#\\+BEGIN_EXAMPLE\\(?: +\\(.*\\)\\)?")
1862 (org-match-string-no-properties 1)))
1863 ;; Switches analysis
1865 (cond ((not switches
) nil
)
1866 ((string-match "-n\\>" switches
) 'new
)
1867 ((string-match "+n\\>" switches
) 'continued
)))
1869 (and switches
(string-match "-i\\>" switches
)))
1870 ;; Should labels be retained in (or stripped from) example
1874 (not (string-match "-r\\>" switches
))
1875 (and number-lines
(string-match "-k\\>" switches
))))
1876 ;; What should code-references use - labels or
1881 (not (string-match "-k\\>" switches
)))))
1884 (string-match "-l +\"\\([^\"\n]+\\)\"" switches
)
1885 (match-string 1 switches
)))
1886 ;; Standard block parsing.
1887 (begin (car affiliated
))
1888 (post-affiliated (point))
1889 (block-ind (progn (skip-chars-forward " \t") (current-column)))
1890 (contents-begin (progn (forward-line) (point)))
1891 (value (org-element-remove-indentation
1892 (org-unescape-code-in-string
1893 (buffer-substring-no-properties
1894 contents-begin contents-end
))
1896 (pos-before-blank (progn (goto-char contents-end
)
1899 (end (progn (skip-chars-forward " \r\t\n" limit
)
1900 (if (eobp) (point) (line-beginning-position)))))
1901 (list 'example-block
1907 :number-lines number-lines
1908 :preserve-indent preserve-indent
1909 :retain-labels retain-labels
1910 :use-labels use-labels
1911 :label-fmt label-fmt
1912 :post-blank
(count-lines pos-before-blank end
)
1913 :post-affiliated post-affiliated
)
1914 (cdr affiliated
)))))))))
1916 (defun org-element-example-block-interpreter (example-block contents
)
1917 "Interpret EXAMPLE-BLOCK element as Org syntax.
1919 (let ((switches (org-element-property :switches example-block
))
1920 (value (org-element-property :value example-block
)))
1921 (concat "#+BEGIN_EXAMPLE" (and switches
(concat " " switches
)) "\n"
1922 (org-element-normalize-string
1923 (org-escape-code-in-string
1924 (if (or org-src-preserve-indentation
1925 (org-element-property :preserve-indent example-block
))
1927 (org-element-remove-indentation value
))))
1933 (defun org-element-export-block-parser (limit affiliated
)
1934 "Parse an export block.
1936 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1937 the buffer position at the beginning of the first affiliated
1938 keyword and CDR is a plist of affiliated keywords along with
1941 Return a list whose CAR is `export-block' and CDR is a plist
1942 containing `:begin', `:end', `:type', `:value', `:post-blank' and
1943 `:post-affiliated' keywords.
1945 Assume point is at export-block beginning."
1946 (let* ((case-fold-search t
)
1947 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
1948 (upcase (org-match-string-no-properties 1)))))
1949 (if (not (save-excursion
1951 (format "^[ \t]*#\\+END_%s[ \t]*$" type
) limit t
)))
1952 ;; Incomplete block: parse it as a paragraph.
1953 (org-element-paragraph-parser limit affiliated
)
1954 (let ((contents-end (match-beginning 0)))
1956 (let* ((begin (car affiliated
))
1957 (post-affiliated (point))
1958 (contents-begin (progn (forward-line) (point)))
1959 (pos-before-blank (progn (goto-char contents-end
)
1962 (end (progn (skip-chars-forward " \r\t\n" limit
)
1963 (if (eobp) (point) (line-beginning-position))))
1964 (value (buffer-substring-no-properties contents-begin
1972 :post-blank
(count-lines pos-before-blank end
)
1973 :post-affiliated post-affiliated
)
1974 (cdr affiliated
)))))))))
1976 (defun org-element-export-block-interpreter (export-block contents
)
1977 "Interpret EXPORT-BLOCK element as Org syntax.
1979 (let ((type (org-element-property :type export-block
)))
1980 (concat (format "#+BEGIN_%s\n" type
)
1981 (org-element-property :value export-block
)
1982 (format "#+END_%s" type
))))
1987 (defun org-element-fixed-width-parser (limit affiliated
)
1988 "Parse a fixed-width section.
1990 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1991 the buffer position at the beginning of the first affiliated
1992 keyword and CDR is a plist of affiliated keywords along with
1995 Return a list whose CAR is `fixed-width' and CDR is a plist
1996 containing `:begin', `:end', `:value', `:post-blank' and
1997 `:post-affiliated' keywords.
1999 Assume point is at the beginning of the fixed-width area."
2001 (let* ((begin (car affiliated
))
2002 (post-affiliated (point))
2006 (while (and (< (point) limit
)
2007 (looking-at "[ \t]*:\\( \\|$\\)"))
2008 ;; Accumulate text without starting colons.
2011 (buffer-substring-no-properties
2012 (match-end 0) (point-at-eol))
2016 (end (progn (skip-chars-forward " \r\t\n" limit
)
2017 (if (eobp) (point) (line-beginning-position)))))
2023 :post-blank
(count-lines end-area end
)
2024 :post-affiliated post-affiliated
)
2025 (cdr affiliated
))))))
2027 (defun org-element-fixed-width-interpreter (fixed-width contents
)
2028 "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 (horizontal-rule contents
)
2065 "Interpret HORIZONTAL-RULE element as Org syntax.
2072 (defun org-element-keyword-parser (limit affiliated
)
2073 "Parse a keyword at point.
2075 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2076 the buffer position at the beginning of the first affiliated
2077 keyword and CDR is a plist of affiliated keywords along with
2080 Return a list whose CAR is `keyword' and CDR is a plist
2081 containing `:key', `:value', `:begin', `:end', `:post-blank' and
2082 `:post-affiliated' keywords."
2084 ;; An orphaned affiliated keyword is considered as a regular
2085 ;; keyword. In this case AFFILIATED is nil, so we take care of
2086 ;; this corner case.
2087 (let ((begin (or (car affiliated
) (point)))
2088 (post-affiliated (point))
2089 (key (progn (looking-at "[ \t]*#\\+\\(\\S-+*\\):")
2090 (upcase (org-match-string-no-properties 1))))
2091 (value (org-trim (buffer-substring-no-properties
2092 (match-end 0) (point-at-eol))))
2093 (pos-before-blank (progn (forward-line) (point)))
2094 (end (progn (skip-chars-forward " \r\t\n" limit
)
2095 (if (eobp) (point) (line-beginning-position)))))
2102 :post-blank
(count-lines pos-before-blank end
)
2103 :post-affiliated post-affiliated
)
2104 (cdr affiliated
))))))
2106 (defun org-element-keyword-interpreter (keyword contents
)
2107 "Interpret KEYWORD element as Org syntax.
2110 (org-element-property :key keyword
)
2111 (org-element-property :value keyword
)))
2114 ;;;; Latex Environment
2116 (defconst org-element--latex-begin-environment
2117 "^[ \t]*\\\\begin{\\([A-Za-z0-9*]+\\)}"
2118 "Regexp matching the beginning of a LaTeX environment.
2119 The environment is captured by the first group.
2121 See also `org-element--latex-end-environment'.")
2123 (defconst org-element--latex-end-environment
2124 "\\\\end{%s}[ \t]*$"
2125 "Format string matching the ending of a LaTeX environment.
2126 See also `org-element--latex-begin-environment'.")
2128 (defun org-element-latex-environment-parser (limit affiliated
)
2129 "Parse a LaTeX environment.
2131 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2132 the buffer position at the beginning of the first affiliated
2133 keyword and CDR is a plist of affiliated keywords along with
2136 Return a list whose CAR is `latex-environment' and CDR is a plist
2137 containing `:begin', `:end', `:value', `:post-blank' and
2138 `:post-affiliated' keywords.
2140 Assume point is at the beginning of the latex environment."
2142 (let ((case-fold-search t
)
2143 (code-begin (point)))
2144 (looking-at org-element--latex-begin-environment
)
2145 (if (not (re-search-forward (format org-element--latex-end-environment
2146 (regexp-quote (match-string 1)))
2148 ;; Incomplete latex environment: parse it as a paragraph.
2149 (org-element-paragraph-parser limit affiliated
)
2150 (let* ((code-end (progn (forward-line) (point)))
2151 (begin (car affiliated
))
2152 (value (buffer-substring-no-properties code-begin code-end
))
2153 (end (progn (skip-chars-forward " \r\t\n" limit
)
2154 (if (eobp) (point) (line-beginning-position)))))
2155 (list 'latex-environment
2160 :post-blank
(count-lines code-end end
)
2161 :post-affiliated code-begin
)
2162 (cdr affiliated
))))))))
2164 (defun org-element-latex-environment-interpreter (latex-environment contents
)
2165 "Interpret LATEX-ENVIRONMENT element as Org syntax.
2167 (org-element-property :value latex-environment
))
2172 (defun org-element-node-property-parser (limit)
2173 "Parse a node-property at point.
2175 LIMIT bounds the search.
2177 Return a list whose CAR is `node-property' and CDR is a plist
2178 containing `:key', `:value', `:begin', `:end', `:post-blank' and
2179 `:post-affiliated' keywords."
2180 (looking-at org-property-re
)
2181 (let ((case-fold-search t
)
2183 (key (org-match-string-no-properties 2))
2184 (value (org-match-string-no-properties 3))
2185 (end (save-excursion
2187 (if (re-search-forward org-property-re limit t
)
2188 (line-beginning-position)
2190 (list 'node-property
2196 :post-affiliated begin
))))
2198 (defun org-element-node-property-interpreter (node-property contents
)
2199 "Interpret NODE-PROPERTY element as Org syntax.
2201 (format org-property-format
2202 (format ":%s:" (org-element-property :key node-property
))
2203 (or (org-element-property :value node-property
) "")))
2208 (defun org-element-paragraph-parser (limit affiliated
)
2211 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2212 the buffer position at the beginning of the first affiliated
2213 keyword and CDR is a plist of affiliated keywords along with
2216 Return a list whose CAR is `paragraph' and CDR is a plist
2217 containing `:begin', `:end', `:contents-begin' and
2218 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
2220 Assume point is at the beginning of the paragraph."
2222 (let* ((begin (car affiliated
))
2223 (contents-begin (point))
2225 (let ((case-fold-search t
))
2227 (if (not (re-search-forward
2228 org-element-paragraph-separate limit
'm
))
2230 ;; A matching `org-element-paragraph-separate' is not
2231 ;; necessarily the end of the paragraph. In
2232 ;; particular, lines starting with # or : as a first
2233 ;; non-space character are ambiguous. We have to
2234 ;; check if they are valid Org syntax (e.g., not an
2235 ;; incomplete keyword).
2239 ;; There's no ambiguity for other symbols or
2240 ;; empty lines: stop here.
2241 (looking-at "[ \t]*\\(?:[^:#]\\|$\\)")
2242 ;; Stop at valid fixed-width areas.
2243 (looking-at "[ \t]*:\\(?: \\|$\\)")
2245 (and (looking-at org-drawer-regexp
)
2248 "^[ \t]*:END:[ \t]*$" limit t
)))
2249 ;; Stop at valid comments.
2250 (looking-at "[ \t]*#\\(?: \\|$\\)")
2251 ;; Stop at valid dynamic blocks.
2252 (and (looking-at org-dblock-start-re
)
2255 "^[ \t]*#\\+END:?[ \t]*$" limit t
)))
2256 ;; Stop at valid blocks.
2257 (and (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
2260 (format "^[ \t]*#\\+END_%s[ \t]*$"
2262 (org-match-string-no-properties 1)))
2264 ;; Stop at valid latex environments.
2265 (and (looking-at org-element--latex-begin-environment
)
2268 (format org-element--latex-end-environment
2270 (org-match-string-no-properties 1)))
2272 ;; Stop at valid keywords.
2273 (looking-at "[ \t]*#\\+\\S-+:")
2274 ;; Skip everything else.
2278 (re-search-forward org-element-paragraph-separate
2280 (beginning-of-line)))
2281 (if (= (point) limit
) limit
2282 (goto-char (line-beginning-position)))))
2283 (contents-end (progn (skip-chars-backward " \r\t\n" contents-begin
)
2286 (end (progn (skip-chars-forward " \r\t\n" limit
)
2287 (if (eobp) (point) (line-beginning-position)))))
2292 :contents-begin contents-begin
2293 :contents-end contents-end
2294 :post-blank
(count-lines before-blank end
)
2295 :post-affiliated contents-begin
)
2296 (cdr affiliated
))))))
2298 (defun org-element-paragraph-interpreter (paragraph contents
)
2299 "Interpret PARAGRAPH element as Org syntax.
2300 CONTENTS is the contents of the element."
2306 (defun org-element-planning-parser (limit)
2309 LIMIT bounds the search.
2311 Return a list whose CAR is `planning' and CDR is a plist
2312 containing `:closed', `:deadline', `:scheduled', `:begin',
2313 `:end', `:post-blank' and `:post-affiliated' keywords."
2315 (let* ((case-fold-search nil
)
2317 (post-blank (let ((before-blank (progn (forward-line) (point))))
2318 (skip-chars-forward " \r\t\n" limit
)
2319 (skip-chars-backward " \t")
2320 (unless (bolp) (end-of-line))
2321 (count-lines before-blank
(point))))
2323 closed deadline scheduled
)
2325 (while (re-search-forward org-keyword-time-not-clock-regexp end t
)
2326 (goto-char (match-end 1))
2327 (skip-chars-forward " \t" end
)
2328 (let ((keyword (match-string 1))
2329 (time (org-element-timestamp-parser)))
2330 (cond ((equal keyword org-closed-string
) (setq closed time
))
2331 ((equal keyword org-deadline-string
) (setq deadline time
))
2332 (t (setq scheduled time
)))))
2334 (list :closed closed
2336 :scheduled scheduled
2339 :post-blank post-blank
2340 :post-affiliated begin
)))))
2342 (defun org-element-planning-interpreter (planning contents
)
2343 "Interpret PLANNING element as Org syntax.
2348 (list (let ((deadline (org-element-property :deadline planning
)))
2350 (concat org-deadline-string
" "
2351 (org-element-timestamp-interpreter deadline nil
))))
2352 (let ((scheduled (org-element-property :scheduled planning
)))
2354 (concat org-scheduled-string
" "
2355 (org-element-timestamp-interpreter scheduled nil
))))
2356 (let ((closed (org-element-property :closed planning
)))
2358 (concat org-closed-string
" "
2359 (org-element-timestamp-interpreter closed nil
))))))
2365 (defun org-element-src-block-parser (limit affiliated
)
2368 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2369 the buffer position at the beginning of the first affiliated
2370 keyword and CDR is a plist of affiliated keywords along with
2373 Return a list whose CAR is `src-block' and CDR is a plist
2374 containing `:language', `:switches', `:parameters', `:begin',
2375 `:end', `:number-lines', `:retain-labels', `:use-labels',
2376 `:label-fmt', `:preserve-indent', `:value', `:post-blank' and
2377 `:post-affiliated' keywords.
2379 Assume point is at the beginning of the block."
2380 (let ((case-fold-search t
))
2381 (if (not (save-excursion (re-search-forward "^[ \t]*#\\+END_SRC[ \t]*$"
2383 ;; Incomplete block: parse it as a paragraph.
2384 (org-element-paragraph-parser limit affiliated
)
2385 (let ((contents-end (match-beginning 0)))
2387 (let* ((begin (car affiliated
))
2388 (post-affiliated (point))
2389 ;; Get language as a string.
2393 (concat "^[ \t]*#\\+BEGIN_SRC"
2394 "\\(?: +\\(\\S-+\\)\\)?"
2395 "\\(\\(?: +\\(?:-l \".*?\"\\|[-+][A-Za-z]\\)\\)+\\)?"
2397 (org-match-string-no-properties 1)))
2399 (switches (org-match-string-no-properties 2))
2401 (parameters (org-match-string-no-properties 3))
2402 ;; Switches analysis
2404 (cond ((not switches
) nil
)
2405 ((string-match "-n\\>" switches
) 'new
)
2406 ((string-match "+n\\>" switches
) 'continued
)))
2407 (preserve-indent (and switches
2408 (string-match "-i\\>" switches
)))
2411 (string-match "-l +\"\\([^\"\n]+\\)\"" switches
)
2412 (match-string 1 switches
)))
2413 ;; Should labels be retained in (or stripped from)
2417 (not (string-match "-r\\>" switches
))
2418 (and number-lines
(string-match "-k\\>" switches
))))
2419 ;; What should code-references use - labels or
2424 (not (string-match "-k\\>" switches
)))))
2426 (block-ind (progn (skip-chars-forward " \t") (current-column)))
2428 (value (org-element-remove-indentation
2429 (org-unescape-code-in-string
2430 (buffer-substring-no-properties
2431 (progn (forward-line) (point)) contents-end
))
2433 (pos-before-blank (progn (goto-char contents-end
)
2436 ;; Get position after ending blank lines.
2437 (end (progn (skip-chars-forward " \r\t\n" limit
)
2438 (if (eobp) (point) (line-beginning-position)))))
2441 (list :language language
2442 :switches
(and (org-string-nw-p switches
)
2443 (org-trim switches
))
2444 :parameters
(and (org-string-nw-p parameters
)
2445 (org-trim parameters
))
2448 :number-lines number-lines
2449 :preserve-indent preserve-indent
2450 :retain-labels retain-labels
2451 :use-labels use-labels
2452 :label-fmt label-fmt
2454 :post-blank
(count-lines pos-before-blank end
)
2455 :post-affiliated post-affiliated
)
2456 (cdr affiliated
)))))))))
2458 (defun org-element-src-block-interpreter (src-block contents
)
2459 "Interpret SRC-BLOCK element as Org syntax.
2461 (let ((lang (org-element-property :language src-block
))
2462 (switches (org-element-property :switches src-block
))
2463 (params (org-element-property :parameters src-block
))
2465 (let ((val (org-element-property :value src-block
)))
2467 ((or org-src-preserve-indentation
2468 (org-element-property :preserve-indent src-block
))
2470 ((zerop org-edit-src-content-indentation
) val
)
2472 (let ((ind (make-string org-edit-src-content-indentation ?\s
)))
2473 (replace-regexp-in-string
2474 "\\(^\\)[ \t]*\\S-" ind val nil nil
1)))))))
2475 (concat (format "#+BEGIN_SRC%s\n"
2476 (concat (and lang
(concat " " lang
))
2477 (and switches
(concat " " switches
))
2478 (and params
(concat " " params
))))
2479 (org-element-normalize-string (org-escape-code-in-string value
))
2485 (defun org-element-table-parser (limit affiliated
)
2486 "Parse a table at point.
2488 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2489 the buffer position at the beginning of the first affiliated
2490 keyword and CDR is a plist of affiliated keywords along with
2493 Return a list whose CAR is `table' and CDR is a plist containing
2494 `:begin', `:end', `:tblfm', `:type', `:contents-begin',
2495 `:contents-end', `:value', `:post-blank' and `:post-affiliated'
2498 Assume point is at the beginning of the table."
2500 (let* ((case-fold-search t
)
2501 (table-begin (point))
2502 (type (if (org-at-table.el-p
) 'table.el
'org
))
2503 (begin (car affiliated
))
2505 (if (re-search-forward org-table-any-border-regexp limit
'm
)
2506 (goto-char (match-beginning 0))
2509 (while (looking-at "[ \t]*#\\+TBLFM: +\\(.*\\)[ \t]*$")
2510 (push (org-match-string-no-properties 1) acc
)
2513 (pos-before-blank (point))
2514 (end (progn (skip-chars-forward " \r\t\n" limit
)
2515 (if (eobp) (point) (line-beginning-position)))))
2522 ;; Only `org' tables have contents. `table.el' tables
2523 ;; use a `:value' property to store raw table as
2525 :contents-begin
(and (eq type
'org
) table-begin
)
2526 :contents-end
(and (eq type
'org
) table-end
)
2527 :value
(and (eq type
'table.el
)
2528 (buffer-substring-no-properties
2529 table-begin table-end
))
2530 :post-blank
(count-lines pos-before-blank end
)
2531 :post-affiliated table-begin
)
2532 (cdr affiliated
))))))
2534 (defun org-element-table-interpreter (table contents
)
2535 "Interpret TABLE element as Org syntax.
2536 CONTENTS is a string, if table's type is `org', or nil."
2537 (if (eq (org-element-property :type table
) 'table.el
)
2538 (org-remove-indentation (org-element-property :value table
))
2539 (concat (with-temp-buffer (insert contents
)
2542 (mapconcat (lambda (fm) (concat "#+TBLFM: " fm
))
2543 (reverse (org-element-property :tblfm table
))
2549 (defun org-element-table-row-parser (limit)
2550 "Parse table row at point.
2552 LIMIT bounds the search.
2554 Return a list whose CAR is `table-row' and CDR is a plist
2555 containing `:begin', `:end', `:contents-begin', `:contents-end',
2556 `:type', `:post-blank' and `:post-affiliated' keywords."
2558 (let* ((type (if (looking-at "^[ \t]*|-") 'rule
'standard
))
2560 ;; A table rule has no contents. In that case, ensure
2561 ;; CONTENTS-BEGIN matches CONTENTS-END.
2562 (contents-begin (and (eq type
'standard
)
2563 (search-forward "|")
2565 (contents-end (and (eq type
'standard
)
2568 (skip-chars-backward " \t")
2570 (end (progn (forward-line) (point))))
2575 :contents-begin contents-begin
2576 :contents-end contents-end
2578 :post-affiliated begin
)))))
2580 (defun org-element-table-row-interpreter (table-row contents
)
2581 "Interpret TABLE-ROW element as Org syntax.
2582 CONTENTS is the contents of the table row."
2583 (if (eq (org-element-property :type table-row
) 'rule
) "|-"
2584 (concat "| " contents
)))
2589 (defun org-element-verse-block-parser (limit affiliated
)
2590 "Parse a verse block.
2592 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2593 the buffer position at the beginning of the first affiliated
2594 keyword and CDR is a plist of affiliated keywords along with
2597 Return a list whose CAR is `verse-block' and CDR is a plist
2598 containing `:begin', `:end', `:contents-begin', `:contents-end',
2599 `:post-blank' and `:post-affiliated' keywords.
2601 Assume point is at beginning of the block."
2602 (let ((case-fold-search t
))
2603 (if (not (save-excursion
2604 (re-search-forward "^[ \t]*#\\+END_VERSE[ \t]*$" limit t
)))
2605 ;; Incomplete block: parse it as a paragraph.
2606 (org-element-paragraph-parser limit affiliated
)
2607 (let ((contents-end (match-beginning 0)))
2609 (let* ((begin (car affiliated
))
2610 (post-affiliated (point))
2611 (contents-begin (progn (forward-line) (point)))
2612 (pos-before-blank (progn (goto-char contents-end
)
2615 (end (progn (skip-chars-forward " \r\t\n" limit
)
2616 (if (eobp) (point) (line-beginning-position)))))
2621 :contents-begin contents-begin
2622 :contents-end contents-end
2623 :post-blank
(count-lines pos-before-blank end
)
2624 :post-affiliated post-affiliated
)
2625 (cdr affiliated
)))))))))
2627 (defun org-element-verse-block-interpreter (verse-block contents
)
2628 "Interpret VERSE-BLOCK element as Org syntax.
2629 CONTENTS is verse block contents."
2630 (format "#+BEGIN_VERSE\n%s#+END_VERSE" contents
))
2636 ;; Unlike to elements, raw text can be found between objects. Hence,
2637 ;; `org-element--object-lex' is provided to find the next object in
2640 ;; Some object types (e.g., `italic') are recursive. Restrictions on
2641 ;; object types they can contain will be specified in
2642 ;; `org-element-object-restrictions'.
2644 ;; Creating a new type of object requires to alter
2645 ;; `org-element--object-regexp' and `org-element--object-lex', add the
2646 ;; new type in `org-element-all-objects', and possibly add
2647 ;; restrictions in `org-element-object-restrictions'.
2651 (defun org-element-bold-parser ()
2652 "Parse bold object at point, if any.
2654 When at a bold object, return a list whose car is `bold' and cdr
2655 is a plist with `:begin', `:end', `:contents-begin' and
2656 `:contents-end' and `:post-blank' keywords. Otherwise, return
2659 Assume point is at the first star marker."
2661 (unless (bolp) (backward-char 1))
2662 (when (looking-at org-emph-re
)
2663 (let ((begin (match-beginning 2))
2664 (contents-begin (match-beginning 4))
2665 (contents-end (match-end 4))
2666 (post-blank (progn (goto-char (match-end 2))
2667 (skip-chars-forward " \t")))
2672 :contents-begin contents-begin
2673 :contents-end contents-end
2674 :post-blank post-blank
))))))
2676 (defun org-element-bold-interpreter (bold contents
)
2677 "Interpret BOLD object as Org syntax.
2678 CONTENTS is the contents of the object."
2679 (format "*%s*" contents
))
2684 (defun org-element-code-parser ()
2685 "Parse code object at point, if any.
2687 When at a code object, return a list whose car is `code' and cdr
2688 is a plist with `:value', `:begin', `:end' and `:post-blank'
2689 keywords. Otherwise, return nil.
2691 Assume point is at the first tilde marker."
2693 (unless (bolp) (backward-char 1))
2694 (when (looking-at org-emph-re
)
2695 (let ((begin (match-beginning 2))
2696 (value (org-match-string-no-properties 4))
2697 (post-blank (progn (goto-char (match-end 2))
2698 (skip-chars-forward " \t")))
2704 :post-blank post-blank
))))))
2706 (defun org-element-code-interpreter (code contents
)
2707 "Interpret CODE object as Org syntax.
2709 (format "~%s~" (org-element-property :value code
)))
2714 (defun org-element-entity-parser ()
2715 "Parse entity at point, if any.
2717 When at an entity, return a list whose car is `entity' and cdr
2718 a plist with `:begin', `:end', `:latex', `:latex-math-p',
2719 `:html', `:latin1', `:utf-8', `:ascii', `:use-brackets-p' and
2720 `:post-blank' as keywords. Otherwise, return nil.
2722 Assume point is at the beginning of the entity."
2724 (when (looking-at "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)")
2726 (let* ((value (or (org-entity-get (match-string 1))
2727 (throw 'no-object nil
)))
2728 (begin (match-beginning 0))
2729 (bracketsp (string= (match-string 2) "{}"))
2730 (post-blank (progn (goto-char (match-end 1))
2731 (when bracketsp
(forward-char 2))
2732 (skip-chars-forward " \t")))
2735 (list :name
(car value
)
2736 :latex
(nth 1 value
)
2737 :latex-math-p
(nth 2 value
)
2739 :ascii
(nth 4 value
)
2740 :latin1
(nth 5 value
)
2741 :utf-8
(nth 6 value
)
2744 :use-brackets-p bracketsp
2745 :post-blank post-blank
)))))))
2747 (defun org-element-entity-interpreter (entity contents
)
2748 "Interpret ENTITY object as Org syntax.
2751 (org-element-property :name entity
)
2752 (when (org-element-property :use-brackets-p entity
) "{}")))
2757 (defun org-element-export-snippet-parser ()
2758 "Parse export snippet at point.
2760 When at an export snippet, return a list whose car is
2761 `export-snippet' and cdr a plist with `:begin', `:end',
2762 `:back-end', `:value' and `:post-blank' as keywords. Otherwise,
2765 Assume point is at the beginning of the snippet."
2768 (when (and (looking-at "@@\\([-A-Za-z0-9]+\\):")
2770 (save-match-data (goto-char (match-end 0))
2771 (re-search-forward "@@" nil t
)
2772 (match-beginning 0))))
2773 (let* ((begin (match-beginning 0))
2774 (back-end (org-match-string-no-properties 1))
2775 (value (buffer-substring-no-properties
2776 (match-end 0) contents-end
))
2777 (post-blank (skip-chars-forward " \t"))
2779 (list 'export-snippet
2780 (list :back-end back-end
2784 :post-blank post-blank
)))))))
2786 (defun org-element-export-snippet-interpreter (export-snippet contents
)
2787 "Interpret EXPORT-SNIPPET object as Org syntax.
2790 (org-element-property :back-end export-snippet
)
2791 (org-element-property :value export-snippet
)))
2794 ;;;; Footnote Reference
2796 (defun org-element-footnote-reference-parser ()
2797 "Parse footnote reference at point, if any.
2799 When at a footnote reference, return a list whose car is
2800 `footnote-reference' and cdr a plist with `:label', `:type',
2801 `:begin', `:end', `:content-begin', `:contents-end' and
2802 `:post-blank' as keywords. Otherwise, return nil."
2803 (when (looking-at org-footnote-re
)
2804 (let ((closing (with-syntax-table org-element--pair-square-table
2805 (ignore-errors (scan-lists (point) 1 0)))))
2808 (let* ((begin (point))
2810 (or (org-match-string-no-properties 2)
2811 (org-match-string-no-properties 3)
2812 (and (match-string 1)
2813 (concat "fn:" (org-match-string-no-properties 1)))))
2814 (type (if (or (not label
) (match-string 1)) 'inline
'standard
))
2815 (inner-begin (match-end 0))
2816 (inner-end (1- closing
))
2817 (post-blank (progn (goto-char closing
)
2818 (skip-chars-forward " \t")))
2820 (list 'footnote-reference
2825 :contents-begin
(and (eq type
'inline
) inner-begin
)
2826 :contents-end
(and (eq type
'inline
) inner-end
)
2827 :post-blank post-blank
))))))))
2829 (defun org-element-footnote-reference-interpreter (footnote-reference contents
)
2830 "Interpret FOOTNOTE-REFERENCE object as Org syntax.
2831 CONTENTS is its definition, when inline, or nil."
2833 (concat (or (org-element-property :label footnote-reference
) "fn:")
2834 (and contents
(concat ":" contents
)))))
2837 ;;;; Inline Babel Call
2839 (defun org-element-inline-babel-call-parser ()
2840 "Parse inline babel call at point, if any.
2842 When at an inline babel call, return a list whose car is
2843 `inline-babel-call' and cdr a plist with `:begin', `:end',
2844 `:value' and `:post-blank' as keywords. Otherwise, return nil.
2846 Assume point is at the beginning of the babel call."
2848 (unless (bolp) (backward-char))
2849 (when (let ((case-fold-search t
))
2850 (looking-at org-babel-inline-lob-one-liner-regexp
))
2851 (let ((begin (match-end 1))
2852 (value (buffer-substring-no-properties (match-end 1) (match-end 0)))
2853 (post-blank (progn (goto-char (match-end 0))
2854 (skip-chars-forward " \t")))
2856 (list 'inline-babel-call
2860 :post-blank post-blank
))))))
2862 (defun org-element-inline-babel-call-interpreter (inline-babel-call contents
)
2863 "Interpret INLINE-BABEL-CALL object as Org syntax.
2865 (org-element-property :value inline-babel-call
))
2868 ;;;; Inline Src Block
2870 (defun org-element-inline-src-block-parser ()
2871 "Parse inline source block at point, if any.
2873 When at an inline source block, return a list whose car is
2874 `inline-src-block' and cdr a plist with `:begin', `:end',
2875 `:language', `:value', `:parameters' and `:post-blank' as
2876 keywords. Otherwise, return nil.
2878 Assume point is at the beginning of the inline src block."
2880 (unless (bolp) (backward-char))
2881 (when (looking-at org-babel-inline-src-block-regexp
)
2882 (let ((begin (match-beginning 1))
2883 (language (org-match-string-no-properties 2))
2884 (parameters (org-match-string-no-properties 4))
2885 (value (org-match-string-no-properties 5))
2886 (post-blank (progn (goto-char (match-end 0))
2887 (skip-chars-forward " \t")))
2889 (list 'inline-src-block
2890 (list :language language
2892 :parameters parameters
2895 :post-blank post-blank
))))))
2897 (defun org-element-inline-src-block-interpreter (inline-src-block contents
)
2898 "Interpret INLINE-SRC-BLOCK object as Org syntax.
2900 (let ((language (org-element-property :language inline-src-block
))
2901 (arguments (org-element-property :parameters inline-src-block
))
2902 (body (org-element-property :value inline-src-block
)))
2903 (format "src_%s%s{%s}"
2905 (if arguments
(format "[%s]" arguments
) "")
2910 (defun org-element-italic-parser ()
2911 "Parse italic object at point, if any.
2913 When at an italic object, return a list whose car is `italic' and
2914 cdr is a plist with `:begin', `:end', `:contents-begin' and
2915 `:contents-end' and `:post-blank' keywords. Otherwise, return
2918 Assume point is at the first slash marker."
2920 (unless (bolp) (backward-char 1))
2921 (when (looking-at org-emph-re
)
2922 (let ((begin (match-beginning 2))
2923 (contents-begin (match-beginning 4))
2924 (contents-end (match-end 4))
2925 (post-blank (progn (goto-char (match-end 2))
2926 (skip-chars-forward " \t")))
2931 :contents-begin contents-begin
2932 :contents-end contents-end
2933 :post-blank post-blank
))))))
2935 (defun org-element-italic-interpreter (italic contents
)
2936 "Interpret ITALIC object as Org syntax.
2937 CONTENTS is the contents of the object."
2938 (format "/%s/" contents
))
2943 (defun org-element-latex-fragment-parser ()
2944 "Parse LaTeX fragment at point, if any.
2946 When at a LaTeX fragment, return a list whose car is
2947 `latex-fragment' and cdr a plist with `:value', `:begin', `:end',
2948 and `:post-blank' as keywords. Otherwise, return nil.
2950 Assume point is at the beginning of the LaTeX fragment."
2953 (let* ((begin (point))
2955 (if (eq (char-after) ?$
)
2956 (if (eq (char-after (1+ (point))) ?$
)
2957 (search-forward "$$" nil t
2)
2958 (and (not (eq (char-before) ?$
))
2959 (search-forward "$" nil t
2)
2960 (not (memq (char-before (match-beginning 0))
2961 '(?\s ?
\t ?
\n ?
, ?.
)))
2962 (looking-at "\\([- \t.,?;:'\"]\\|$\\)")
2964 (case (char-after (1+ (point)))
2965 (?\
( (search-forward "\\)" nil t
))
2966 (?\
[ (search-forward "\\]" nil t
))
2969 (and (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")
2971 (post-blank (if (not after-fragment
) (throw 'no-object nil
)
2972 (goto-char after-fragment
)
2973 (skip-chars-forward " \t")))
2975 (list 'latex-fragment
2976 (list :value
(buffer-substring-no-properties begin after-fragment
)
2979 :post-blank post-blank
))))))
2981 (defun org-element-latex-fragment-interpreter (latex-fragment contents
)
2982 "Interpret LATEX-FRAGMENT object as Org syntax.
2984 (org-element-property :value latex-fragment
))
2988 (defun org-element-line-break-parser ()
2989 "Parse line break at point, if any.
2991 When at a line break, return a list whose car is `line-break',
2992 and cdr a plist with `:begin', `:end' and `:post-blank' keywords.
2993 Otherwise, return nil.
2995 Assume point is at the beginning of the line break."
2996 (when (and (org-looking-at-p "\\\\\\\\[ \t]*$")
2997 (not (eq (char-before) ?
\\)))
2999 (list :begin
(point)
3000 :end
(progn (forward-line) (point))
3003 (defun org-element-line-break-interpreter (line-break contents
)
3004 "Interpret LINE-BREAK object as Org syntax.
3011 (defun org-element-link-parser ()
3012 "Parse link at point, if any.
3014 When at a link, return a list whose car is `link' and cdr a plist
3015 with `:type', `:path', `:raw-link', `:application',
3016 `:search-option', `:begin', `:end', `:contents-begin',
3017 `:contents-end' and `:post-blank' as keywords. Otherwise, return
3020 Assume point is at the beginning of the link."
3022 (let ((begin (point))
3023 end contents-begin contents-end link-end post-blank path type
3024 raw-link link search-option application
)
3026 ;; Type 1: Text targeted from a radio target.
3027 ((and org-target-link-regexp
3028 (save-excursion (or (bolp) (backward-char))
3029 (looking-at org-target-link-regexp
)))
3031 link-end
(match-end 1)
3032 path
(org-match-string-no-properties 1)
3033 contents-begin
(match-beginning 1)
3034 contents-end
(match-end 1)))
3035 ;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]]
3036 ((looking-at org-bracket-link-regexp
)
3037 (setq contents-begin
(match-beginning 3)
3038 contents-end
(match-end 3)
3039 link-end
(match-end 0)
3040 ;; RAW-LINK is the original link. Expand any
3041 ;; abbreviation in it.
3042 raw-link
(org-translate-link
3043 (org-link-expand-abbrev
3044 (org-match-string-no-properties 1))))
3045 ;; Determine TYPE of link and set PATH accordingly.
3048 ((or (file-name-absolute-p raw-link
)
3049 (string-match "\\`\\.\\.?/" raw-link
))
3050 (setq type
"file" path raw-link
))
3051 ;; Explicit type (http, irc, bbdb...). See `org-link-types'.
3052 ((string-match org-link-types-re raw-link
)
3053 (setq type
(match-string 1 raw-link
)
3054 ;; According to RFC 3986, extra whitespace should be
3055 ;; ignored when a URI is extracted.
3056 path
(replace-regexp-in-string
3057 "[ \t]*\n[ \t]*" "" (substring raw-link
(match-end 0)))))
3058 ;; Id type: PATH is the id.
3059 ((string-match "\\`id:\\([-a-f0-9]+\\)" raw-link
)
3060 (setq type
"id" path
(match-string 1 raw-link
)))
3061 ;; Code-ref type: PATH is the name of the reference.
3062 ((string-match "\\`(\\(.*\\))\\'" raw-link
)
3063 (setq type
"coderef" path
(match-string 1 raw-link
)))
3064 ;; Custom-id type: PATH is the name of the custom id.
3065 ((= (aref raw-link
0) ?
#)
3066 (setq type
"custom-id" path
(substring raw-link
1)))
3067 ;; Fuzzy type: Internal link either matches a target, an
3068 ;; headline name or nothing. PATH is the target or
3070 (t (setq type
"fuzzy" path raw-link
))))
3071 ;; Type 3: Plain link, e.g., http://orgmode.org
3072 ((looking-at org-plain-link-re
)
3073 (setq raw-link
(org-match-string-no-properties 0)
3074 type
(org-match-string-no-properties 1)
3075 link-end
(match-end 0)
3076 path
(org-match-string-no-properties 2)))
3077 ;; Type 4: Angular link, e.g., <http://orgmode.org>
3078 ((looking-at org-angle-link-re
)
3079 (setq raw-link
(buffer-substring-no-properties
3080 (match-beginning 1) (match-end 2))
3081 type
(org-match-string-no-properties 1)
3082 link-end
(match-end 0)
3083 path
(org-match-string-no-properties 2)))
3084 (t (throw 'no-object nil
)))
3085 ;; In any case, deduce end point after trailing white space from
3086 ;; LINK-END variable.
3088 (setq post-blank
(progn (goto-char link-end
) (skip-chars-forward " \t"))
3090 ;; Special "file" type link processing. Extract opening
3091 ;; application and search option, if any. Also normalize URI.
3092 (when (string-match "\\`file\\(?:\\+\\(.+\\)\\)?\\'" type
)
3093 (setq application
(match-string 1 type
) type
"file")
3094 (when (string-match "::\\(.*\\)\\'" path
)
3095 (setq search-option
(match-string 1 path
)
3096 path
(replace-match "" nil nil path
)))
3097 (when (and (file-name-absolute-p path
)
3098 (not (org-string-match-p "\\`[/~]/" path
)))
3099 (setq path
(concat "//" path
))))
3103 :raw-link
(or raw-link path
)
3104 :application application
3105 :search-option search-option
3108 :contents-begin contents-begin
3109 :contents-end contents-end
3110 :post-blank post-blank
))))))
3112 (defun org-element-link-interpreter (link contents
)
3113 "Interpret LINK object as Org syntax.
3114 CONTENTS is the contents of the object, or nil."
3115 (let ((type (org-element-property :type link
))
3116 (raw-link (org-element-property :raw-link link
)))
3117 (if (string= type
"radio") raw-link
3120 (if contents
(format "[%s]" contents
) "")))))
3125 (defun org-element-macro-parser ()
3126 "Parse macro at point, if any.
3128 When at a macro, return a list whose car is `macro' and cdr
3129 a plist with `:key', `:args', `:begin', `:end', `:value' and
3130 `:post-blank' as keywords. Otherwise, return nil.
3132 Assume point is at the macro."
3134 (when (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
3135 (let ((begin (point))
3136 (key (downcase (org-match-string-no-properties 1)))
3137 (value (org-match-string-no-properties 0))
3138 (post-blank (progn (goto-char (match-end 0))
3139 (skip-chars-forward " \t")))
3141 (args (let ((args (org-match-string-no-properties 3)))
3142 (and args
(org-macro-extract-arguments args
)))))
3149 :post-blank post-blank
))))))
3151 (defun org-element-macro-interpreter (macro contents
)
3152 "Interpret MACRO object as Org syntax.
3154 (org-element-property :value macro
))
3159 (defun org-element-radio-target-parser ()
3160 "Parse radio target at point, if any.
3162 When at a radio target, return a list whose car is `radio-target'
3163 and cdr a plist with `:begin', `:end', `:contents-begin',
3164 `:contents-end', `:value' and `:post-blank' as keywords.
3165 Otherwise, return nil.
3167 Assume point is at the radio target."
3169 (when (looking-at org-radio-target-regexp
)
3170 (let ((begin (point))
3171 (contents-begin (match-beginning 1))
3172 (contents-end (match-end 1))
3173 (value (org-match-string-no-properties 1))
3174 (post-blank (progn (goto-char (match-end 0))
3175 (skip-chars-forward " \t")))
3180 :contents-begin contents-begin
3181 :contents-end contents-end
3182 :post-blank post-blank
3185 (defun org-element-radio-target-interpreter (target contents
)
3186 "Interpret TARGET object as Org syntax.
3187 CONTENTS is the contents of the object."
3188 (concat "<<<" contents
">>>"))
3191 ;;;; Statistics Cookie
3193 (defun org-element-statistics-cookie-parser ()
3194 "Parse statistics cookie at point, if any.
3196 When at a statistics cookie, return a list whose car is
3197 `statistics-cookie', and cdr a plist with `:begin', `:end',
3198 `:value' and `:post-blank' keywords. Otherwise, return nil.
3200 Assume point is at the beginning of the statistics-cookie."
3202 (when (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
3203 (let* ((begin (point))
3204 (value (buffer-substring-no-properties
3205 (match-beginning 0) (match-end 0)))
3206 (post-blank (progn (goto-char (match-end 0))
3207 (skip-chars-forward " \t")))
3209 (list 'statistics-cookie
3213 :post-blank post-blank
))))))
3215 (defun org-element-statistics-cookie-interpreter (statistics-cookie contents
)
3216 "Interpret STATISTICS-COOKIE object as Org syntax.
3218 (org-element-property :value statistics-cookie
))
3223 (defun org-element-strike-through-parser ()
3224 "Parse strike-through object at point, if any.
3226 When at a strike-through object, return a list whose car is
3227 `strike-through' and cdr is a plist with `:begin', `:end',
3228 `:contents-begin' and `:contents-end' and `:post-blank' keywords.
3229 Otherwise, return nil.
3231 Assume point is at the first plus sign marker."
3233 (unless (bolp) (backward-char 1))
3234 (when (looking-at org-emph-re
)
3235 (let ((begin (match-beginning 2))
3236 (contents-begin (match-beginning 4))
3237 (contents-end (match-end 4))
3238 (post-blank (progn (goto-char (match-end 2))
3239 (skip-chars-forward " \t")))
3241 (list 'strike-through
3244 :contents-begin contents-begin
3245 :contents-end contents-end
3246 :post-blank post-blank
))))))
3248 (defun org-element-strike-through-interpreter (strike-through contents
)
3249 "Interpret STRIKE-THROUGH object as Org syntax.
3250 CONTENTS is the contents of the object."
3251 (format "+%s+" contents
))
3256 (defun org-element-subscript-parser ()
3257 "Parse subscript at point, if any.
3259 When at a subscript object, return a list whose car is
3260 `subscript' and cdr a plist with `:begin', `:end',
3261 `:contents-begin', `:contents-end', `:use-brackets-p' and
3262 `:post-blank' as keywords. Otherwise, return nil.
3264 Assume point is at the underscore."
3266 (unless (bolp) (backward-char))
3267 (when (looking-at org-match-substring-regexp
)
3268 (let ((bracketsp (match-beginning 4))
3269 (begin (match-beginning 2))
3270 (contents-begin (or (match-beginning 4)
3271 (match-beginning 3)))
3272 (contents-end (or (match-end 4) (match-end 3)))
3273 (post-blank (progn (goto-char (match-end 0))
3274 (skip-chars-forward " \t")))
3279 :use-brackets-p bracketsp
3280 :contents-begin contents-begin
3281 :contents-end contents-end
3282 :post-blank post-blank
))))))
3284 (defun org-element-subscript-interpreter (subscript contents
)
3285 "Interpret SUBSCRIPT object as Org syntax.
3286 CONTENTS is the contents of the object."
3288 (if (org-element-property :use-brackets-p subscript
) "_{%s}" "_%s")
3294 (defun org-element-superscript-parser ()
3295 "Parse superscript at point, if any.
3297 When at a superscript object, return a list whose car is
3298 `superscript' and cdr a plist with `:begin', `:end',
3299 `:contents-begin', `:contents-end', `:use-brackets-p' and
3300 `:post-blank' as keywords. Otherwise, return nil.
3302 Assume point is at the caret."
3304 (unless (bolp) (backward-char))
3305 (when (looking-at org-match-substring-regexp
)
3306 (let ((bracketsp (match-beginning 4))
3307 (begin (match-beginning 2))
3308 (contents-begin (or (match-beginning 4)
3309 (match-beginning 3)))
3310 (contents-end (or (match-end 4) (match-end 3)))
3311 (post-blank (progn (goto-char (match-end 0))
3312 (skip-chars-forward " \t")))
3317 :use-brackets-p bracketsp
3318 :contents-begin contents-begin
3319 :contents-end contents-end
3320 :post-blank post-blank
))))))
3322 (defun org-element-superscript-interpreter (superscript contents
)
3323 "Interpret SUPERSCRIPT object as Org syntax.
3324 CONTENTS is the contents of the object."
3326 (if (org-element-property :use-brackets-p superscript
) "^{%s}" "^%s")
3332 (defun org-element-table-cell-parser ()
3333 "Parse table cell at point.
3334 Return a list whose car is `table-cell' and cdr is a plist
3335 containing `:begin', `:end', `:contents-begin', `:contents-end'
3336 and `:post-blank' keywords."
3337 (looking-at "[ \t]*\\(.*?\\)[ \t]*\\(?:|\\|$\\)")
3338 (let* ((begin (match-beginning 0))
3340 (contents-begin (match-beginning 1))
3341 (contents-end (match-end 1)))
3345 :contents-begin contents-begin
3346 :contents-end contents-end
3349 (defun org-element-table-cell-interpreter (table-cell contents
)
3350 "Interpret TABLE-CELL element as Org syntax.
3351 CONTENTS is the contents of the cell, or nil."
3352 (concat " " contents
" |"))
3357 (defun org-element-target-parser ()
3358 "Parse target at point, if any.
3360 When at a target, return a list whose car is `target' and cdr
3361 a plist with `:begin', `:end', `:value' and `:post-blank' as
3362 keywords. Otherwise, return nil.
3364 Assume point is at the target."
3366 (when (looking-at org-target-regexp
)
3367 (let ((begin (point))
3368 (value (org-match-string-no-properties 1))
3369 (post-blank (progn (goto-char (match-end 0))
3370 (skip-chars-forward " \t")))
3376 :post-blank post-blank
))))))
3378 (defun org-element-target-interpreter (target contents
)
3379 "Interpret TARGET object as Org syntax.
3381 (format "<<%s>>" (org-element-property :value target
)))
3386 (defconst org-element--timestamp-regexp
3387 (concat org-ts-regexp-both
3389 "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
3391 "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
3392 "Regexp matching any timestamp type object.")
3394 (defun org-element-timestamp-parser ()
3395 "Parse time stamp at point, if any.
3397 When at a time stamp, return a list whose car is `timestamp', and
3398 cdr a plist with `:type', `:raw-value', `:year-start',
3399 `:month-start', `:day-start', `:hour-start', `:minute-start',
3400 `:year-end', `:month-end', `:day-end', `:hour-end',
3401 `:minute-end', `:repeater-type', `:repeater-value',
3402 `:repeater-unit', `:warning-type', `:warning-value',
3403 `:warning-unit', `:begin', `:end' and `:post-blank' keywords.
3404 Otherwise, return nil.
3406 Assume point is at the beginning of the timestamp."
3407 (when (org-looking-at-p org-element--timestamp-regexp
)
3409 (let* ((begin (point))
3410 (activep (eq (char-after) ?
<))
3413 (looking-at "\\([<[]\\(%%\\)?.*?\\)[]>]\\(?:--\\([<[].*?[]>]\\)\\)?")
3414 (match-string-no-properties 0)))
3415 (date-start (match-string-no-properties 1))
3416 (date-end (match-string 3))
3417 (diaryp (match-beginning 2))
3418 (post-blank (progn (goto-char (match-end 0))
3419 (skip-chars-forward " \t")))
3424 "[012]?[0-9]:[0-5][0-9]\\(-\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)"
3426 (cons (string-to-number (match-string 2 date-start
))
3427 (string-to-number (match-string 3 date-start
)))))
3428 (type (cond (diaryp 'diary
)
3429 ((and activep
(or date-end time-range
)) 'active-range
)
3431 ((or date-end time-range
) 'inactive-range
)
3435 (string-match "\\([.+]?\\+\\)\\([0-9]+\\)\\([hdwmy]\\)"
3439 (let ((type (match-string 1 raw-value
)))
3440 (cond ((equal "++" type
) 'catch-up
)
3441 ((equal ".+" type
) 'restart
)
3443 :repeater-value
(string-to-number (match-string 2 raw-value
))
3445 (case (string-to-char (match-string 3 raw-value
))
3446 (?h
'hour
) (?d
'day
) (?w
'week
) (?m
'month
) (t 'year
)))))
3449 (string-match "\\(-\\)?-\\([0-9]+\\)\\([hdwmy]\\)" raw-value
)
3451 :warning-type
(if (match-string 1 raw-value
) 'first
'all
)
3452 :warning-value
(string-to-number (match-string 2 raw-value
))
3454 (case (string-to-char (match-string 3 raw-value
))
3455 (?h
'hour
) (?d
'day
) (?w
'week
) (?m
'month
) (t 'year
)))))
3456 year-start month-start day-start hour-start minute-start year-end
3457 month-end day-end hour-end minute-end
)
3458 ;; Parse date-start.
3460 (let ((date (org-parse-time-string date-start t
)))
3461 (setq year-start
(nth 5 date
)
3462 month-start
(nth 4 date
)
3463 day-start
(nth 3 date
)
3464 hour-start
(nth 2 date
)
3465 minute-start
(nth 1 date
))))
3466 ;; Compute date-end. It can be provided directly in time-stamp,
3467 ;; or extracted from time range. Otherwise, it defaults to the
3468 ;; same values as date-start.
3470 (let ((date (and date-end
(org-parse-time-string date-end t
))))
3471 (setq year-end
(or (nth 5 date
) year-start
)
3472 month-end
(or (nth 4 date
) month-start
)
3473 day-end
(or (nth 3 date
) day-start
)
3474 hour-end
(or (nth 2 date
) (car time-range
) hour-start
)
3475 minute-end
(or (nth 1 date
) (cdr time-range
) minute-start
))))
3477 (nconc (list :type type
3478 :raw-value raw-value
3479 :year-start year-start
3480 :month-start month-start
3481 :day-start day-start
3482 :hour-start hour-start
3483 :minute-start minute-start
3485 :month-end month-end
3488 :minute-end minute-end
3491 :post-blank post-blank
)
3495 (defun org-element-timestamp-interpreter (timestamp contents
)
3496 "Interpret TIMESTAMP object as Org syntax.
3498 (let* ((repeat-string
3500 (case (org-element-property :repeater-type timestamp
)
3501 (cumulate "+") (catch-up "++") (restart ".+"))
3502 (let ((val (org-element-property :repeater-value timestamp
)))
3503 (and val
(number-to-string val
)))
3504 (case (org-element-property :repeater-unit timestamp
)
3505 (hour "h") (day "d") (week "w") (month "m") (year "y"))))
3508 (case (org-element-property :warning-type timestamp
)
3511 (let ((val (org-element-property :warning-value timestamp
)))
3512 (and val
(number-to-string val
)))
3513 (case (org-element-property :warning-unit timestamp
)
3514 (hour "h") (day "d") (week "w") (month "m") (year "y"))))
3516 ;; Build an Org timestamp string from TIME. ACTIVEP is
3517 ;; non-nil when time stamp is active. If WITH-TIME-P is
3518 ;; non-nil, add a time part. HOUR-END and MINUTE-END
3519 ;; specify a time range in the timestamp. REPEAT-STRING is
3520 ;; the repeater string, if any.
3521 (lambda (time activep
&optional with-time-p hour-end minute-end
)
3522 (let ((ts (format-time-string
3523 (funcall (if with-time-p
'cdr
'car
)
3524 org-time-stamp-formats
)
3526 (when (and hour-end minute-end
)
3527 (string-match "[012]?[0-9]:[0-5][0-9]" ts
)
3530 (format "\\&-%02d:%02d" hour-end minute-end
)
3532 (unless activep
(setq ts
(format "[%s]" (substring ts
1 -
1))))
3533 (dolist (s (list repeat-string warning-string
))
3534 (when (org-string-nw-p s
)
3535 (setq ts
(concat (substring ts
0 -
1)
3538 (substring ts -
1)))))
3541 (type (org-element-property :type timestamp
)))
3544 (let* ((minute-start (org-element-property :minute-start timestamp
))
3545 (minute-end (org-element-property :minute-end timestamp
))
3546 (hour-start (org-element-property :hour-start timestamp
))
3547 (hour-end (org-element-property :hour-end timestamp
))
3548 (time-range-p (and hour-start hour-end minute-start minute-end
3549 (or (/= hour-start hour-end
)
3550 (/= minute-start minute-end
)))))
3556 (org-element-property :day-start timestamp
)
3557 (org-element-property :month-start timestamp
)
3558 (org-element-property :year-start timestamp
))
3560 (and hour-start minute-start
)
3561 (and time-range-p hour-end
)
3562 (and time-range-p minute-end
))))
3563 ((active-range inactive-range
)
3564 (let ((minute-start (org-element-property :minute-start timestamp
))
3565 (minute-end (org-element-property :minute-end timestamp
))
3566 (hour-start (org-element-property :hour-start timestamp
))
3567 (hour-end (org-element-property :hour-end timestamp
)))
3570 build-ts-string
(encode-time
3574 (org-element-property :day-start timestamp
)
3575 (org-element-property :month-start timestamp
)
3576 (org-element-property :year-start timestamp
))
3577 (eq type
'active-range
)
3578 (and hour-start minute-start
))
3580 (funcall build-ts-string
3584 (org-element-property :day-end timestamp
)
3585 (org-element-property :month-end timestamp
)
3586 (org-element-property :year-end timestamp
))
3587 (eq type
'active-range
)
3588 (and hour-end minute-end
)))))
3589 (otherwise (org-element-property :raw-value timestamp
)))))
3594 (defun org-element-underline-parser ()
3595 "Parse underline object at point, if any.
3597 When at an underline object, return a list whose car is
3598 `underline' and cdr is a plist with `:begin', `:end',
3599 `:contents-begin' and `:contents-end' and `:post-blank' keywords.
3600 Otherwise, return nil.
3602 Assume point is at the first underscore marker."
3604 (unless (bolp) (backward-char 1))
3605 (when (looking-at org-emph-re
)
3606 (let ((begin (match-beginning 2))
3607 (contents-begin (match-beginning 4))
3608 (contents-end (match-end 4))
3609 (post-blank (progn (goto-char (match-end 2))
3610 (skip-chars-forward " \t")))
3615 :contents-begin contents-begin
3616 :contents-end contents-end
3617 :post-blank post-blank
))))))
3619 (defun org-element-underline-interpreter (underline contents
)
3620 "Interpret UNDERLINE object as Org syntax.
3621 CONTENTS is the contents of the object."
3622 (format "_%s_" contents
))
3627 (defun org-element-verbatim-parser ()
3628 "Parse verbatim object at point, if any.
3630 When at a verbatim object, return a list whose car is `verbatim'
3631 and cdr is a plist with `:value', `:begin', `:end' and
3632 `:post-blank' keywords. Otherwise, return nil.
3634 Assume point is at the first equal sign marker."
3636 (unless (bolp) (backward-char 1))
3637 (when (looking-at org-emph-re
)
3638 (let ((begin (match-beginning 2))
3639 (value (org-match-string-no-properties 4))
3640 (post-blank (progn (goto-char (match-end 2))
3641 (skip-chars-forward " \t")))
3647 :post-blank post-blank
))))))
3649 (defun org-element-verbatim-interpreter (verbatim contents
)
3650 "Interpret VERBATIM object as Org syntax.
3652 (format "=%s=" (org-element-property :value verbatim
)))
3656 ;;; Parsing Element Starting At Point
3658 ;; `org-element--current-element' is the core function of this section.
3659 ;; It returns the Lisp representation of the element starting at
3662 ;; `org-element--current-element' makes use of special modes. They
3663 ;; are activated for fixed element chaining (e.g., `plain-list' >
3664 ;; `item') or fixed conditional element chaining (e.g., `headline' >
3665 ;; `section'). Special modes are: `first-section', `item',
3666 ;; `node-property', `section' and `table-row'.
3668 (defun org-element--current-element (limit &optional granularity mode structure
)
3669 "Parse the element starting at point.
3671 Return value is a list like (TYPE PROPS) where TYPE is the type
3672 of the element and PROPS a plist of properties associated to the
3675 Possible types are defined in `org-element-all-elements'.
3677 LIMIT bounds the search.
3679 Optional argument GRANULARITY determines the depth of the
3680 recursion. Allowed values are `headline', `greater-element',
3681 `element', `object' or nil. When it is broader than `object' (or
3682 nil), secondary values will not be parsed, since they only
3685 Optional argument MODE, when non-nil, can be either
3686 `first-section', `section', `planning', `item', `node-property'
3689 If STRUCTURE isn't provided but MODE is set to `item', it will be
3692 This function assumes point is always at the beginning of the
3693 element it has to parse."
3695 (let ((case-fold-search t
)
3696 ;; Determine if parsing depth allows for secondary strings
3697 ;; parsing. It only applies to elements referenced in
3698 ;; `org-element-secondary-value-alist'.
3699 (raw-secondary-p (and granularity
(not (eq granularity
'object
)))))
3703 (org-element-item-parser limit structure raw-secondary-p
))
3705 ((eq mode
'table-row
) (org-element-table-row-parser limit
))
3707 ((eq mode
'node-property
) (org-element-node-property-parser limit
))
3709 ((org-with-limited-levels (org-at-heading-p))
3710 (org-element-headline-parser limit raw-secondary-p
))
3711 ;; Sections (must be checked after headline).
3712 ((eq mode
'section
) (org-element-section-parser limit
))
3713 ((eq mode
'first-section
)
3714 (org-element-section-parser
3715 (or (save-excursion (org-with-limited-levels (outline-next-heading)))
3718 ((and (eq mode
'planning
) (looking-at org-planning-line-re
))
3719 (org-element-planning-parser limit
))
3721 ((and (memq mode
'(planning property-drawer
))
3722 (looking-at org-property-drawer-re
))
3723 (org-element-property-drawer-parser limit
))
3724 ;; When not at bol, point is at the beginning of an item or
3725 ;; a footnote definition: next item is always a paragraph.
3726 ((not (bolp)) (org-element-paragraph-parser limit
(list (point))))
3728 ((looking-at org-clock-line-re
) (org-element-clock-parser limit
))
3731 (org-element-inlinetask-parser limit raw-secondary-p
))
3732 ;; From there, elements can have affiliated keywords.
3733 (t (let ((affiliated (org-element--collect-affiliated-keywords limit
)))
3735 ;; Jumping over affiliated keywords put point off-limits.
3736 ;; Parse them as regular keywords.
3737 ((and (cdr affiliated
) (>= (point) limit
))
3738 (goto-char (car affiliated
))
3739 (org-element-keyword-parser limit nil
))
3740 ;; LaTeX Environment.
3741 ((looking-at org-element--latex-begin-environment
)
3742 (org-element-latex-environment-parser limit affiliated
))
3743 ;; Drawer and Property Drawer.
3744 ((looking-at org-drawer-regexp
)
3745 (org-element-drawer-parser limit affiliated
))
3747 ((looking-at "[ \t]*:\\( \\|$\\)")
3748 (org-element-fixed-width-parser limit affiliated
))
3749 ;; Inline Comments, Blocks, Babel Calls, Dynamic Blocks and
3751 ((looking-at "[ \t]*#")
3752 (goto-char (match-end 0))
3753 (cond ((looking-at "\\(?: \\|$\\)")
3755 (org-element-comment-parser limit affiliated
))
3756 ((looking-at "\\+BEGIN_\\(\\S-+\\)")
3758 (let ((parser (assoc (upcase (match-string 1))
3759 org-element-block-name-alist
)))
3760 (if parser
(funcall (cdr parser
) limit affiliated
)
3761 (org-element-special-block-parser limit affiliated
))))
3762 ((looking-at "\\+CALL:")
3764 (org-element-babel-call-parser limit affiliated
))
3765 ((looking-at "\\+BEGIN:? ")
3767 (org-element-dynamic-block-parser limit affiliated
))
3768 ((looking-at "\\+\\S-+:")
3770 (org-element-keyword-parser limit affiliated
))
3773 (org-element-paragraph-parser limit affiliated
))))
3774 ;; Footnote Definition.
3775 ((looking-at org-footnote-definition-re
)
3776 (org-element-footnote-definition-parser limit affiliated
))
3778 ((looking-at "[ \t]*-\\{5,\\}[ \t]*$")
3779 (org-element-horizontal-rule-parser limit affiliated
))
3782 (org-element-diary-sexp-parser limit affiliated
))
3784 ((org-at-table-p t
) (org-element-table-parser limit affiliated
))
3786 ((looking-at (org-item-re))
3787 (org-element-plain-list-parser
3789 (or structure
(org-element--list-struct limit
))))
3790 ;; Default element: Paragraph.
3791 (t (org-element-paragraph-parser limit affiliated
)))))))))
3794 ;; Most elements can have affiliated keywords. When looking for an
3795 ;; element beginning, we want to move before them, as they belong to
3796 ;; that element, and, in the meantime, collect information they give
3797 ;; into appropriate properties. Hence the following function.
3799 (defun org-element--collect-affiliated-keywords (limit)
3800 "Collect affiliated keywords from point down to LIMIT.
3802 Return a list whose CAR is the position at the first of them and
3803 CDR a plist of keywords and values and move point to the
3804 beginning of the first line after them.
3806 As a special case, if element doesn't start at the beginning of
3807 the line (e.g., a paragraph starting an item), CAR is current
3808 position of point and CDR is nil."
3809 (if (not (bolp)) (list (point))
3810 (let ((case-fold-search t
)
3812 ;; RESTRICT is the list of objects allowed in parsed
3814 (restrict (org-element-restriction 'keyword
))
3816 (while (and (< (point) limit
) (looking-at org-element--affiliated-re
))
3817 (let* ((raw-kwd (upcase (match-string 1)))
3818 ;; Apply translation to RAW-KWD. From there, KWD is
3819 ;; the official keyword.
3820 (kwd (or (cdr (assoc raw-kwd
3821 org-element-keyword-translation-alist
))
3823 ;; Find main value for any keyword.
3827 (buffer-substring-no-properties
3828 (match-end 0) (line-end-position)))))
3829 ;; PARSEDP is non-nil when keyword should have its
3831 (parsedp (member kwd org-element-parsed-keywords
))
3832 ;; If KWD is a dual keyword, find its secondary
3833 ;; value. Maybe parse it.
3834 (dualp (member kwd org-element-dual-keywords
))
3837 (let ((sec (org-match-string-no-properties 2)))
3838 (if (or (not sec
) (not parsedp
)) sec
3839 (org-element--parse-objects
3840 (match-beginning 2) (match-end 2) nil restrict
)))))
3841 ;; Attribute a property name to KWD.
3842 (kwd-sym (and kwd
(intern (concat ":" (downcase kwd
))))))
3843 ;; Now set final shape for VALUE.
3846 (org-element--parse-objects
3848 (progn (end-of-line) (skip-chars-backward " \t") (point))
3851 (setq value
(and (or value dual-value
) (cons value dual-value
))))
3852 (when (or (member kwd org-element-multiple-keywords
)
3853 ;; Attributes can always appear on multiple lines.
3854 (string-match "^ATTR_" kwd
))
3855 (setq value
(cons value
(plist-get output kwd-sym
))))
3856 ;; Eventually store the new value in OUTPUT.
3857 (setq output
(plist-put output kwd-sym value
))
3858 ;; Move to next keyword.
3860 ;; If affiliated keywords are orphaned: move back to first one.
3861 ;; They will be parsed as a paragraph.
3862 (when (looking-at "[ \t]*$") (goto-char origin
) (setq output nil
))
3864 (cons origin output
))))
3870 ;; The two major functions here are `org-element-parse-buffer', which
3871 ;; parses Org syntax inside the current buffer, taking into account
3872 ;; region, narrowing, or even visibility if specified, and
3873 ;; `org-element-parse-secondary-string', which parses objects within
3876 ;; The (almost) almighty `org-element-map' allows to apply a function
3877 ;; on elements or objects matching some type, and accumulate the
3878 ;; resulting values. In an export situation, it also skips unneeded
3879 ;; parts of the parse tree.
3881 (defun org-element-parse-buffer (&optional granularity visible-only
)
3882 "Recursively parse the buffer and return structure.
3883 If narrowing is in effect, only parse the visible part of the
3886 Optional argument GRANULARITY determines the depth of the
3887 recursion. It can be set to the following symbols:
3889 `headline' Only parse headlines.
3890 `greater-element' Don't recurse into greater elements excepted
3891 headlines and sections. Thus, elements
3892 parsed are the top-level ones.
3893 `element' Parse everything but objects and plain text.
3894 `object' Parse the complete buffer (default).
3896 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3899 An element or an objects is represented as a list with the
3900 pattern (TYPE PROPERTIES CONTENTS), where :
3902 TYPE is a symbol describing the element or object. See
3903 `org-element-all-elements' and `org-element-all-objects' for an
3904 exhaustive list of such symbols. One can retrieve it with
3905 `org-element-type' function.
3907 PROPERTIES is the list of attributes attached to the element or
3908 object, as a plist. Although most of them are specific to the
3909 element or object type, all types share `:begin', `:end',
3910 `:post-blank' and `:parent' properties, which respectively
3911 refer to buffer position where the element or object starts,
3912 ends, the number of white spaces or blank lines after it, and
3913 the element or object containing it. Properties values can be
3914 obtained by using `org-element-property' function.
3916 CONTENTS is a list of elements, objects or raw strings
3917 contained in the current element or object, when applicable.
3918 One can access them with `org-element-contents' function.
3920 The Org buffer has `org-data' as type and nil as properties.
3921 `org-element-map' function can be used to find specific elements
3922 or objects within the parse tree.
3924 This function assumes that current major mode is `org-mode'."
3926 (goto-char (point-min))
3927 (org-skip-whitespace)
3928 (org-element--parse-elements
3929 (point-at-bol) (point-max)
3930 ;; Start in `first-section' mode so text before the first
3931 ;; headline belongs to a section.
3932 'first-section nil granularity visible-only
(list 'org-data nil
))))
3934 (defun org-element-parse-secondary-string (string restriction
&optional parent
)
3935 "Recursively parse objects in STRING and return structure.
3937 RESTRICTION is a symbol limiting the object types that will be
3940 Optional argument PARENT, when non-nil, is the element or object
3941 containing the secondary string. It is used to set correctly
3942 `:parent' property within the string."
3943 (let ((local-variables (buffer-local-variables)))
3945 (dolist (v local-variables
)
3947 (if (symbolp v
) (makunbound v
)
3948 (org-set-local (car v
) (cdr v
)))))
3950 (restore-buffer-modified-p nil
)
3951 (let ((secondary (org-element--parse-objects
3952 (point-min) (point-max) nil restriction
)))
3954 (dolist (o secondary
) (org-element-put-property o
:parent parent
)))
3957 (defun org-element-map
3958 (data types fun
&optional info first-match no-recursion with-affiliated
)
3959 "Map a function on selected elements or objects.
3961 DATA is a parse tree, an element, an object, a string, or a list
3962 of such constructs. TYPES is a symbol or list of symbols of
3963 elements or objects types (see `org-element-all-elements' and
3964 `org-element-all-objects' for a complete list of types). FUN is
3965 the function called on the matching element or object. It has to
3966 accept one argument: the element or object itself.
3968 When optional argument INFO is non-nil, it should be a plist
3969 holding export options. In that case, parts of the parse tree
3970 not exportable according to that property list will be skipped.
3972 When optional argument FIRST-MATCH is non-nil, stop at the first
3973 match for which FUN doesn't return nil, and return that value.
3975 Optional argument NO-RECURSION is a symbol or a list of symbols
3976 representing elements or objects types. `org-element-map' won't
3977 enter any recursive element or object whose type belongs to that
3978 list. Though, FUN can still be applied on them.
3980 When optional argument WITH-AFFILIATED is non-nil, FUN will also
3981 apply to matching objects within parsed affiliated keywords (see
3982 `org-element-parsed-keywords').
3984 Nil values returned from FUN do not appear in the results.
3990 Assuming TREE is a variable containing an Org buffer parse tree,
3991 the following example will return a flat list of all `src-block'
3992 and `example-block' elements in it:
3994 \(org-element-map tree '(example-block src-block) #'identity)
3996 The following snippet will find the first headline with a level
3997 of 1 and a \"phone\" tag, and will return its beginning position:
3999 \(org-element-map tree 'headline
4001 \(and (= (org-element-property :level hl) 1)
4002 \(member \"phone\" (org-element-property :tags hl))
4003 \(org-element-property :begin hl)))
4006 The next example will return a flat list of all `plain-list' type
4007 elements in TREE that are not a sub-list themselves:
4009 \(org-element-map tree 'plain-list #'identity nil nil 'plain-list)
4011 Eventually, this example will return a flat list of all `bold'
4012 type objects containing a `latex-snippet' type object, even
4013 looking into captions:
4015 \(org-element-map tree 'bold
4017 \(and (org-element-map b 'latex-snippet #'identity nil t) b))
4019 ;; Ensure TYPES and NO-RECURSION are a list, even of one element.
4020 (let* ((types (if (listp types
) types
(list types
)))
4021 (no-recursion (if (listp no-recursion
) no-recursion
4022 (list no-recursion
)))
4023 ;; Recursion depth is determined by --CATEGORY.
4026 (let ((category 'greater-elements
)
4027 (all-objects (cons 'plain-text org-element-all-objects
)))
4028 (dolist (type types category
)
4029 (cond ((memq type all-objects
)
4030 ;; If one object is found, the function has to
4031 ;; recurse into every object.
4032 (throw 'found
'objects
))
4033 ((not (memq type org-element-greater-elements
))
4034 ;; If one regular element is found, the
4035 ;; function has to recurse, at least, into
4036 ;; every element it encounters.
4037 (and (not (eq category
'elements
))
4038 (setq category
'elements
))))))))
4043 ;; Recursively walk DATA. INFO, if non-nil, is a plist
4044 ;; holding contextual information.
4045 (let ((--type (org-element-type --data
)))
4048 ;; Ignored element in an export context.
4049 ((and info
(memq --data
(plist-get info
:ignore-list
))))
4050 ;; List of elements or objects.
4051 ((not --type
) (mapc --walk-tree --data
))
4052 ;; Unconditionally enter parse trees.
4053 ((eq --type
'org-data
)
4054 (mapc --walk-tree
(org-element-contents --data
)))
4056 ;; Check if TYPE is matching among TYPES. If so,
4057 ;; apply FUN to --DATA and accumulate return value
4058 ;; into --ACC (or exit if FIRST-MATCH is non-nil).
4059 (when (memq --type types
)
4060 (let ((result (funcall fun --data
)))
4061 (cond ((not result
))
4062 (first-match (throw '--map-first-match result
))
4063 (t (push result --acc
)))))
4064 ;; If --DATA has a secondary string that can contain
4065 ;; objects with their type among TYPES, look into it.
4066 (when (and (eq --category
'objects
) (not (stringp --data
)))
4068 (assq --type org-element-secondary-value-alist
)))
4070 (funcall --walk-tree
4071 (org-element-property (cdr sec-prop
) --data
)))))
4072 ;; If --DATA has any parsed affiliated keywords and
4073 ;; WITH-AFFILIATED is non-nil, look for objects in
4075 (when (and with-affiliated
4076 (eq --category
'objects
)
4077 (memq --type org-element-all-elements
))
4078 (dolist (kwd-pair org-element--parsed-properties-alist
)
4079 (let ((kwd (car kwd-pair
))
4080 (value (org-element-property (cdr kwd-pair
) --data
)))
4081 ;; Pay attention to the type of parsed keyword.
4082 ;; In particular, preserve order for multiple
4086 ((member kwd org-element-dual-keywords
)
4087 (if (member kwd org-element-multiple-keywords
)
4088 (dolist (line (reverse value
))
4089 (funcall --walk-tree
(cdr line
))
4090 (funcall --walk-tree
(car line
)))
4091 (funcall --walk-tree
(cdr value
))
4092 (funcall --walk-tree
(car value
))))
4093 ((member kwd org-element-multiple-keywords
)
4094 (mapc --walk-tree
(reverse value
)))
4095 (t (funcall --walk-tree value
))))))
4096 ;; Determine if a recursion into --DATA is possible.
4098 ;; --TYPE is explicitly removed from recursion.
4099 ((memq --type no-recursion
))
4100 ;; --DATA has no contents.
4101 ((not (org-element-contents --data
)))
4102 ;; Looking for greater elements but --DATA is simply
4103 ;; an element or an object.
4104 ((and (eq --category
'greater-elements
)
4105 (not (memq --type org-element-greater-elements
))))
4106 ;; Looking for elements but --DATA is an object.
4107 ((and (eq --category
'elements
)
4108 (memq --type org-element-all-objects
)))
4109 ;; In any other case, map contents.
4110 (t (mapc --walk-tree
(org-element-contents --data
))))))))))
4111 (catch '--map-first-match
4112 (funcall --walk-tree data
)
4113 ;; Return value in a proper order.
4115 (put 'org-element-map
'lisp-indent-function
2)
4117 ;; The following functions are internal parts of the parser.
4119 ;; The first one, `org-element--parse-elements' acts at the element's
4122 ;; The second one, `org-element--parse-objects' applies on all objects
4123 ;; of a paragraph or a secondary string. It calls
4124 ;; `org-element--object-lex' to find the next object in the current
4127 (defsubst org-element--next-mode
(type parentp
)
4128 "Return next special mode according to TYPE, or nil.
4129 TYPE is a symbol representing the type of an element or object
4130 containing next element if PARENTP is non-nil, or before it
4131 otherwise. Modes can be either `first-section', `item',
4132 `node-property', `planning', `property-drawer', `section',
4133 `table-row' or nil."
4138 (property-drawer 'node-property
)
4143 (node-property 'node-property
)
4144 (planning 'property-drawer
)
4145 (table-row 'table-row
))))
4147 (defun org-element--parse-elements
4148 (beg end mode structure granularity visible-only acc
)
4149 "Parse elements between BEG and END positions.
4151 MODE prioritizes some elements over the others. It can be set to
4152 `first-section', `section', `planning', `item', `node-property'
4155 When value is `item', STRUCTURE will be used as the current list
4158 GRANULARITY determines the depth of the recursion. See
4159 `org-element-parse-buffer' for more information.
4161 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
4164 Elements are accumulated into ACC."
4167 ;; Visible only: skip invisible parts at the beginning of the
4169 (when (and visible-only
(org-invisible-p2))
4170 (goto-char (min (1+ (org-find-visible)) end
)))
4171 ;; When parsing only headlines, skip any text before first one.
4172 (when (and (eq granularity
'headline
) (not (org-at-heading-p)))
4173 (org-with-limited-levels (outline-next-heading)))
4175 (while (< (point) end
)
4176 ;; Find current element's type and parse it accordingly to
4178 (let* ((element (org-element--current-element
4179 end granularity mode structure
))
4180 (type (org-element-type element
))
4181 (cbeg (org-element-property :contents-begin element
)))
4182 (goto-char (org-element-property :end element
))
4183 ;; Visible only: skip invisible parts between siblings.
4184 (when (and visible-only
(org-invisible-p2))
4185 (goto-char (min (1+ (org-find-visible)) end
)))
4186 ;; Fill ELEMENT contents by side-effect.
4188 ;; If element has no contents, don't modify it.
4190 ;; Greater element: parse it between `contents-begin' and
4191 ;; `contents-end'. Make sure GRANULARITY allows the
4192 ;; recursion, or ELEMENT is a headline, in which case going
4193 ;; inside is mandatory, in order to get sub-level headings.
4194 ((and (memq type org-element-greater-elements
)
4195 (or (memq granularity
'(element object nil
))
4196 (and (eq granularity
'greater-element
)
4198 (eq type
'headline
)))
4199 (org-element--parse-elements
4200 cbeg
(org-element-property :contents-end element
)
4201 ;; Possibly switch to a special mode.
4202 (org-element--next-mode type t
)
4203 (and (memq type
'(item plain-list
))
4204 (org-element-property :structure element
))
4205 granularity visible-only element
))
4206 ;; ELEMENT has contents. Parse objects inside, if
4207 ;; GRANULARITY allows it.
4208 ((memq granularity
'(object nil
))
4209 (org-element--parse-objects
4210 cbeg
(org-element-property :contents-end element
) element
4211 (org-element-restriction type
))))
4212 (org-element-adopt-elements acc element
)
4214 (setq mode
(org-element--next-mode type nil
))))
4218 (defun org-element--object-lex (restriction)
4219 "Return next object in current buffer or nil.
4220 RESTRICTION is a list of object types, as symbols, that should be
4221 looked after. This function assumes that the buffer is narrowed
4222 to an appropriate container (e.g., a paragraph)."
4223 (if (memq 'table-cell restriction
) (org-element-table-cell-parser)
4225 (let ((limit (and org-target-link-regexp
4227 (or (bolp) (backward-char))
4228 (re-search-forward org-target-link-regexp nil t
))
4229 (match-beginning 1)))
4231 (while (and (not found
)
4232 (re-search-forward org-element--object-regexp limit t
))
4233 (goto-char (match-beginning 0))
4234 (let ((result (match-string 0)))
4237 ((eq (compare-strings result nil nil
"call_" nil nil t
) t
)
4238 (and (memq 'inline-babel-call restriction
)
4239 (org-element-inline-babel-call-parser)))
4240 ((eq (compare-strings result nil nil
"src_" nil nil t
) t
)
4241 (and (memq 'inline-src-block restriction
)
4242 (org-element-inline-src-block-parser)))
4245 (?^
(and (memq 'superscript restriction
)
4246 (org-element-superscript-parser)))
4247 (?_
(or (and (memq 'subscript restriction
)
4248 (org-element-subscript-parser))
4249 (and (memq 'underline restriction
)
4250 (org-element-underline-parser))))
4251 (?
* (and (memq 'bold restriction
)
4252 (org-element-bold-parser)))
4253 (?
/ (and (memq 'italic restriction
)
4254 (org-element-italic-parser)))
4255 (?~
(and (memq 'code restriction
)
4256 (org-element-code-parser)))
4257 (?
= (and (memq 'verbatim restriction
)
4258 (org-element-verbatim-parser)))
4259 (?
+ (and (memq 'strike-through restriction
)
4260 (org-element-strike-through-parser)))
4261 (?
@ (and (memq 'export-snippet restriction
)
4262 (org-element-export-snippet-parser)))
4263 (?
{ (and (memq 'macro restriction
)
4264 (org-element-macro-parser)))
4265 (?$
(and (memq 'latex-fragment restriction
)
4266 (org-element-latex-fragment-parser)))
4268 (if (eq (aref result
1) ?
<)
4269 (or (and (memq 'radio-target restriction
)
4270 (org-element-radio-target-parser))
4271 (and (memq 'target restriction
)
4272 (org-element-target-parser)))
4273 (or (and (memq 'timestamp restriction
)
4274 (org-element-timestamp-parser))
4275 (and (memq 'link restriction
)
4276 (org-element-link-parser)))))
4278 (if (eq (aref result
1) ?
\\)
4279 (and (memq 'line-break restriction
)
4280 (org-element-line-break-parser))
4281 (or (and (memq 'entity restriction
)
4282 (org-element-entity-parser))
4283 (and (memq 'latex-fragment restriction
)
4284 (org-element-latex-fragment-parser)))))
4286 (if (eq (aref result
1) ?\
[)
4287 (and (memq 'link restriction
)
4288 (org-element-link-parser))
4289 (or (and (memq 'footnote-reference restriction
)
4290 (org-element-footnote-reference-parser))
4291 (and (memq 'timestamp restriction
)
4292 (org-element-timestamp-parser))
4293 (and (memq 'statistics-cookie restriction
)
4294 (org-element-statistics-cookie-parser)))))
4295 ;; This is probably a plain link.
4296 (otherwise (and (or (memq 'link restriction
)
4297 (memq 'plain-link restriction
))
4298 (org-element-link-parser)))))))
4299 (or (eobp) (forward-char))))
4302 ((and limit
(memq 'link restriction
))
4303 (goto-char limit
) (org-element-link-parser)))))))
4305 (defun org-element--parse-objects (beg end acc restriction
)
4306 "Parse objects between BEG and END and return recursive structure.
4308 Objects are accumulated in ACC.
4310 RESTRICTION is a list of object successors which are allowed in
4311 the current object."
4314 (narrow-to-region beg end
)
4315 (goto-char (point-min))
4317 (while (and (not (eobp))
4318 (setq next-object
(org-element--object-lex restriction
)))
4319 ;; 1. Text before any object. Untabify it.
4320 (let ((obj-beg (org-element-property :begin next-object
)))
4321 (unless (= (point) obj-beg
)
4323 (org-element-adopt-elements
4325 (replace-regexp-in-string
4326 "\t" (make-string tab-width ?
)
4327 (buffer-substring-no-properties (point) obj-beg
))))))
4329 (let ((obj-end (org-element-property :end next-object
))
4330 (cont-beg (org-element-property :contents-begin next-object
)))
4331 ;; Fill contents of NEXT-OBJECT by side-effect, if it has
4332 ;; a recursive type.
4334 (memq (car next-object
) org-element-recursive-objects
))
4335 (org-element--parse-objects
4336 cont-beg
(org-element-property :contents-end next-object
)
4337 next-object
(org-element-restriction next-object
)))
4338 (setq acc
(org-element-adopt-elements acc next-object
))
4339 (goto-char obj-end
))))
4340 ;; 3. Text after last object. Untabify it.
4343 (org-element-adopt-elements
4345 (replace-regexp-in-string
4346 "\t" (make-string tab-width ?
)
4347 (buffer-substring-no-properties (point) end
)))))
4353 ;;; Towards A Bijective Process
4355 ;; The parse tree obtained with `org-element-parse-buffer' is really
4356 ;; a snapshot of the corresponding Org buffer. Therefore, it can be
4357 ;; interpreted and expanded into a string with canonical Org syntax.
4358 ;; Hence `org-element-interpret-data'.
4360 ;; The function relies internally on
4361 ;; `org-element--interpret-affiliated-keywords'.
4364 (defun org-element-interpret-data (data)
4365 "Interpret DATA as Org syntax.
4366 DATA is a parse tree, an element, an object or a secondary string
4367 to interpret. Return Org syntax as a string."
4368 (org-element--interpret-data-1 data nil
))
4370 (defun org-element--interpret-data-1 (data parent
)
4371 "Interpret DATA as Org syntax.
4373 DATA is a parse tree, an element, an object or a secondary string
4374 to interpret. PARENT is used for recursive calls. It contains
4375 the element or object containing data, or nil.
4377 Return Org syntax as a string."
4378 (let* ((type (org-element-type data
))
4379 ;; Find interpreter for current object or element. If it
4380 ;; doesn't exist (e.g. this is a pseudo object or element),
4381 ;; return contents, if any.
4383 (let ((fun (intern (format "org-element-%s-interpreter" type
))))
4384 (if (fboundp fun
) fun
(lambda (data contents
) contents
))))
4387 ;; Secondary string.
4390 (lambda (obj) (org-element--interpret-data-1 obj parent
)) data
""))
4391 ;; Full Org document.
4392 ((eq type
'org-data
)
4393 (mapconcat (lambda (obj) (org-element--interpret-data-1 obj parent
))
4394 (org-element-contents data
) ""))
4395 ;; Plain text: return it.
4396 ((stringp data
) data
)
4397 ;; Element or object without contents.
4398 ((not (org-element-contents data
)) (funcall interpret data nil
))
4399 ;; Element or object with contents.
4401 (funcall interpret data
4402 ;; Recursively interpret contents.
4404 (lambda (obj) (org-element--interpret-data-1 obj data
))
4405 (org-element-contents
4406 (if (not (memq type
'(paragraph verse-block
)))
4408 ;; Fix indentation of elements containing
4409 ;; objects. We ignore `table-row' elements
4410 ;; as they are one line long anyway.
4411 (org-element-normalize-contents
4413 ;; When normalizing first paragraph of an
4414 ;; item or a footnote-definition, ignore
4415 ;; first line's indentation.
4416 (and (eq type
'paragraph
)
4417 (equal data
(car (org-element-contents parent
)))
4418 (memq (org-element-type parent
)
4419 '(footnote-definition item
))))))
4421 (if (memq type
'(org-data plain-text nil
)) results
4422 ;; Build white spaces. If no `:post-blank' property is
4423 ;; specified, assume its value is 0.
4424 (let ((post-blank (or (org-element-property :post-blank data
) 0)))
4425 (if (or (memq type org-element-all-objects
)
4427 (let ((type (org-element-type parent
)))
4429 (memq type org-element-object-containers
)))))
4430 (concat results
(make-string post-blank ?\s
))
4432 (org-element--interpret-affiliated-keywords data
)
4433 (org-element-normalize-string results
)
4434 (make-string post-blank ?
\n)))))))
4436 (defun org-element--interpret-affiliated-keywords (element)
4437 "Return ELEMENT's affiliated keywords as Org syntax.
4438 If there is no affiliated keyword, return the empty string."
4439 (let ((keyword-to-org
4443 (when (member key org-element-dual-keywords
)
4444 (setq dual
(cdr value
) value
(car value
)))
4447 (format "[%s]" (org-element-interpret-data dual
)))
4449 (if (member key org-element-parsed-keywords
)
4450 (org-element-interpret-data value
)
4455 (let ((value (org-element-property prop element
))
4456 (keyword (upcase (substring (symbol-name prop
) 1))))
4458 (if (or (member keyword org-element-multiple-keywords
)
4459 ;; All attribute keywords can have multiple lines.
4460 (string-match "^ATTR_" keyword
))
4461 (mapconcat (lambda (line) (funcall keyword-to-org keyword line
))
4464 (funcall keyword-to-org keyword value
)))))
4465 ;; List all ELEMENT's properties matching an attribute line or an
4466 ;; affiliated keyword, but ignore translated keywords since they
4467 ;; cannot belong to the property list.
4468 (loop for prop in
(nth 1 element
) by
'cddr
4469 when
(let ((keyword (upcase (substring (symbol-name prop
) 1))))
4470 (or (string-match "^ATTR_" keyword
)
4472 (member keyword org-element-affiliated-keywords
)
4474 org-element-keyword-translation-alist
)))))
4478 ;; Because interpretation of the parse tree must return the same
4479 ;; number of blank lines between elements and the same number of white
4480 ;; space after objects, some special care must be given to white
4483 ;; The first function, `org-element-normalize-string', ensures any
4484 ;; string different from the empty string will end with a single
4485 ;; newline character.
4487 ;; The second function, `org-element-normalize-contents', removes
4488 ;; global indentation from the contents of the current element.
4490 (defun org-element-normalize-string (s)
4491 "Ensure string S ends with a single newline character.
4493 If S isn't a string return it unchanged. If S is the empty
4494 string, return it. Otherwise, return a new string with a single
4495 newline character at its end."
4497 ((not (stringp s
)) s
)
4499 (t (and (string-match "\\(\n[ \t]*\\)*\\'" s
)
4500 (replace-match "\n" nil nil s
)))))
4502 (defun org-element-normalize-contents (element &optional ignore-first
)
4503 "Normalize plain text in ELEMENT's contents.
4505 ELEMENT must only contain plain text and objects.
4507 If optional argument IGNORE-FIRST is non-nil, ignore first line's
4508 indentation to compute maximal common indentation.
4510 Return the normalized element that is element with global
4511 indentation removed from its contents. The function assumes that
4512 indentation is not done with TAB characters."
4513 (let* ((min-ind most-positive-fixnum
)
4514 find-min-ind
; For byte-compiler.
4516 ;; Return minimal common indentation within BLOB. This is
4517 ;; done by walking recursively BLOB and updating MIN-IND
4518 ;; along the way. FIRST-FLAG is non-nil when the first
4519 ;; string hasn't been seen yet. It is required as this
4520 ;; string is the only one whose indentation doesn't happen
4521 ;; after a newline character.
4522 (lambda (blob first-flag
)
4523 (dolist (object (org-element-contents blob
))
4524 (when (and first-flag
(stringp object
))
4525 (setq first-flag nil
)
4526 (string-match "\\` *" object
)
4527 (let ((len (match-end 0)))
4528 ;; An indentation of zero means no string will be
4529 ;; modified. Quit the process.
4530 (if (zerop len
) (throw 'zero
(setq min-ind
0))
4531 (setq min-ind
(min len min-ind
)))))
4534 (dolist (line (cdr (org-split-string object
" *\n")))
4535 (unless (string= line
"")
4536 (setq min-ind
(min (org-get-indentation line
) min-ind
)))))
4537 ((memq (org-element-type object
) org-element-recursive-objects
)
4538 (funcall find-min-ind object first-flag
)))))))
4539 ;; Find minimal indentation in ELEMENT.
4540 (catch 'zero
(funcall find-min-ind element
(not ignore-first
)))
4541 (if (or (zerop min-ind
) (= min-ind most-positive-fixnum
)) element
4542 ;; Build ELEMENT back, replacing each string with the same
4543 ;; string minus common indentation.
4544 (let* (build ; For byte compiler.
4547 (lambda (blob first-flag
)
4548 ;; Return BLOB with all its strings indentation
4549 ;; shortened from MIN-IND white spaces. FIRST-FLAG
4550 ;; is non-nil when the first string hasn't been seen
4555 (when (and first-flag
(stringp object
))
4556 (setq first-flag nil
)
4558 (replace-regexp-in-string
4559 (format "\\` \\{%d\\}" min-ind
)
4563 (replace-regexp-in-string
4564 (format "\n \\{%d\\}" min-ind
) "\n" object
))
4565 ((memq (org-element-type object
)
4566 org-element-recursive-objects
)
4567 (funcall build object first-flag
))
4569 (org-element-contents blob
)))
4571 (funcall build element
(not ignore-first
))))))
4577 ;; Implement a caching mechanism for `org-element-at-point' and
4578 ;; `org-element-context', which see.
4580 ;; A single public function is provided: `org-element-cache-reset'.
4582 ;; Cache is enabled by default, but can be disabled globally with
4583 ;; `org-element-use-cache'. `org-element-cache-sync-idle-time',
4584 ;; org-element-cache-sync-duration' and `org-element-cache-sync-break'
4585 ;; can be tweaked to control caching behaviour.
4587 ;; Internally, parsed elements are stored in an AVL tree,
4588 ;; `org-element--cache'. This tree is updated lazily: whenever
4589 ;; a change happens to the buffer, a synchronization request is
4590 ;; registered in `org-element--cache-sync-requests' (see
4591 ;; `org-element--cache-submit-request'). During idle time, requests
4592 ;; are processed by `org-element--cache-sync'. Synchronization also
4593 ;; happens when an element is required from the cache. In this case,
4594 ;; the process stops as soon as the needed element is up-to-date.
4596 ;; A synchronization request can only apply on a synchronized part of
4597 ;; the cache. Therefore, the cache is updated at least to the
4598 ;; location where the new request applies. Thus, requests are ordered
4599 ;; from left to right and all elements starting before the first
4600 ;; request are correct. This property is used by functions like
4601 ;; `org-element--cache-find' to retrieve elements in the part of the
4602 ;; cache that can be trusted.
4604 ;; A request applies to every element, starting from its original
4605 ;; location (or key, see below). When a request is processed, it
4606 ;; moves forward and may collide the next one. In this case, both
4607 ;; requests are merged into a new one that starts from that element.
4608 ;; As a consequence, the whole synchronization complexity does not
4609 ;; depend on the number of pending requests, but on the number of
4610 ;; elements the very first request will be applied on.
4612 ;; Elements cannot be accessed through their beginning position, which
4613 ;; may or may not be up-to-date. Instead, each element in the tree is
4614 ;; associated to a key, obtained with `org-element--cache-key'. This
4615 ;; mechanism is robust enough to preserve total order among elements
4616 ;; even when the tree is only partially synchronized.
4618 ;; Objects contained in an element are stored in a hash table,
4619 ;; `org-element--cache-objects'.
4622 (defvar org-element-use-cache t
4623 "Non nil when Org parser should cache its results.
4624 This is mostly for debugging purpose.")
4626 (defvar org-element-cache-sync-idle-time
0.6
4627 "Length, in seconds, of idle time before syncing cache.")
4629 (defvar org-element-cache-sync-duration
(seconds-to-time 0.04)
4630 "Maximum duration, as a time value, for a cache synchronization.
4631 If the synchronization is not over after this delay, the process
4632 pauses and resumes after `org-element-cache-sync-break'
4635 (defvar org-element-cache-sync-break
(seconds-to-time 0.3)
4636 "Duration, as a time value, of the pause between synchronizations.
4637 See `org-element-cache-sync-duration' for more information.")
4642 (defvar org-element--cache nil
4643 "AVL tree used to cache elements.
4644 Each node of the tree contains an element. Comparison is done
4645 with `org-element--cache-compare'. This cache is used in
4646 `org-element-at-point'.")
4648 (defvar org-element--cache-objects nil
4649 "Hash table used as to cache objects.
4650 Key is an element, as returned by `org-element-at-point', and
4651 value is an alist where each association is:
4653 \(PARENT COMPLETEP . OBJECTS)
4655 where PARENT is an element or object, COMPLETEP is a boolean,
4656 non-nil when all direct children of parent are already cached and
4657 OBJECTS is a list of such children, as objects, from farthest to
4660 In the following example, \\alpha, bold object and \\beta are
4661 contained within a paragraph
4665 If the paragraph is completely parsed, OBJECTS-DATA will be
4667 \((PARAGRAPH t BOLD-OBJECT ENTITY-OBJECT)
4668 \(BOLD-OBJECT t ENTITY-OBJECT))
4670 whereas in a partially parsed paragraph, it could be
4672 \((PARAGRAPH nil ENTITY-OBJECT))
4674 This cache is used in `org-element-context'.")
4676 (defvar org-element--cache-sync-requests nil
4677 "List of pending synchronization requests.
4679 A request is a vector with the following pattern:
4681 \[NEXT BEG END OFFSET OUTREACH PARENT PHASE]
4683 Processing a synchronization request consists of three phases:
4685 0. Delete modified elements,
4686 1. Fill missing area in cache,
4687 2. Shift positions and re-parent elements after the changes.
4689 During phase 0, NEXT is the key of the first element to be
4690 removed, BEG and END is buffer position delimiting the
4691 modifications. Elements starting between them (inclusive) are
4692 removed and so are those contained within OUTREACH. PARENT, when
4693 non-nil, is the parent of the first element to be removed.
4695 During phase 1, NEXT is the key of the next known element in
4696 cache and BEG its beginning position. Parse buffer between that
4697 element and the one before it in order to determine the parent of
4698 the next element. Set PARENT to the element containing NEXT.
4700 During phase 2, NEXT is the key of the next element to shift in
4701 the parse tree. All elements starting from this one have their
4702 properties relatives to buffer positions shifted by integer
4703 OFFSET and, if they belong to element PARENT, are adopted by it.
4705 PHASE specifies the phase number, as an integer.")
4707 (defvar org-element--cache-sync-timer nil
4708 "Timer used for cache synchronization.")
4710 (defvar org-element--cache-sync-keys nil
4711 "Hash table used to store keys during synchronization.
4712 See `org-element--cache-key' for more information.")
4714 (defsubst org-element--cache-key
(element)
4715 "Return a unique key for ELEMENT in cache tree.
4717 Keys are used to keep a total order among elements in the cache.
4718 Comparison is done with `org-element--cache-key-less-p'.
4720 When no synchronization is taking place, a key is simply the
4721 beginning position of the element, or that position plus one in
4722 the case of an first item (respectively row) in
4723 a list (respectively a table).
4725 During a synchronization, the key is the one the element had when
4726 the cache was synchronized for the last time. Elements added to
4727 cache during the synchronization get a new key generated with
4728 `org-element--cache-generate-key'.
4730 Such keys are stored in `org-element--cache-sync-keys'. The hash
4731 table is cleared once the synchronization is complete."
4732 (or (gethash element org-element--cache-sync-keys
)
4733 (let* ((begin (org-element-property :begin element
))
4734 ;; Increase beginning position of items (respectively
4735 ;; table rows) by one, so the first item can get
4736 ;; a different key from its parent list (respectively
4738 (key (if (memq (org-element-type element
) '(item table-row
))
4741 (if org-element--cache-sync-requests
4742 (puthash element key org-element--cache-sync-keys
)
4745 (defun org-element--cache-generate-key (lower upper
)
4746 "Generate a key between LOWER and UPPER.
4748 LOWER and UPPER are integers or lists, possibly empty.
4750 If LOWER and UPPER are equals, return LOWER. Otherwise, return
4751 a unique key, as an integer or a list of integers, according to
4752 the following rules:
4754 - LOWER and UPPER are compared level-wise until values differ.
4756 - If, at a given level, LOWER and UPPER differ from more than
4757 2, the new key shares all the levels above with LOWER and
4758 gets a new level. Its value is the mean between LOWER and
4761 \(1 2) + (1 4) --> (1 3)
4763 - If LOWER has no value to compare with, it is assumed that its
4764 value is `most-negative-fixnum'. E.g.,
4772 where m is `most-negative-fixnum'. Likewise, if UPPER is
4773 short of levels, the current value is `most-positive-fixnum'.
4775 - If they differ from only one, the new key inherits from
4776 current LOWER level and fork it at the next level. E.g.,
4784 where M is `most-positive-fixnum'.
4786 - If the key is only one level long, it is returned as an
4789 \(1 2) + (3 2) --> 2
4791 When they are not equals, the function assumes that LOWER is
4792 lesser than UPPER, per `org-element--cache-key-less-p'."
4793 (if (equal lower upper
) lower
4794 (let ((lower (if (integerp lower
) (list lower
) lower
))
4795 (upper (if (integerp upper
) (list upper
) upper
))
4799 (let ((min (or (car lower
) most-negative-fixnum
))
4800 (max (cond (skip-upper most-positive-fixnum
)
4802 (t most-positive-fixnum
))))
4803 (if (< (1+ min
) max
)
4804 (let ((mean (+ (ash min -
1) (ash max -
1) (logand min max
1))))
4805 (throw 'exit
(if key
(nreverse (cons mean key
)) mean
)))
4806 (when (and (< min max
) (not skip-upper
))
4807 ;; When at a given level, LOWER and UPPER differ from
4808 ;; 1, ignore UPPER altogether. Instead create a key
4809 ;; between LOWER and the greatest key with the same
4810 ;; prefix as LOWER so far.
4811 (setq skip-upper t
))
4813 (setq lower
(cdr lower
) upper
(cdr upper
)))))))))
4815 (defsubst org-element--cache-key-less-p
(a b
)
4816 "Non-nil if key A is less than key B.
4817 A and B are either integers or lists of integers, as returned by
4818 `org-element--cache-key'."
4819 (if (integerp a
) (if (integerp b
) (< a b
) (<= a
(car b
)))
4820 (if (integerp b
) (< (car a
) b
)
4823 (cond ((car-less-than-car a b
) (throw 'exit t
))
4824 ((car-less-than-car b a
) (throw 'exit nil
))
4825 (t (setq a
(cdr a
) b
(cdr b
)))))
4826 ;; If A is empty, either keys are equal (B is also empty) and
4827 ;; we return nil, or A is lesser than B (B is longer) and we
4828 ;; return a non-nil value.
4830 ;; If A is not empty, B is necessarily empty and A is greater
4831 ;; than B (A is longer). Therefore, return nil.
4832 (and (null a
) b
)))))
4834 (defun org-element--cache-compare (a b
)
4835 "Non-nil when element A is located before element B."
4836 (org-element--cache-key-less-p (org-element--cache-key a
)
4837 (org-element--cache-key b
)))
4839 (defsubst org-element--cache-root
()
4840 "Return root value in cache.
4841 This function assumes `org-element--cache' is a valid AVL tree."
4842 (avl-tree--node-left (avl-tree--dummyroot org-element--cache
)))
4847 (defsubst org-element--cache-active-p
()
4848 "Non-nil when cache is active in current buffer."
4849 (and org-element-use-cache
4850 (or (derived-mode-p 'org-mode
) orgstruct-mode
)))
4852 (defun org-element--cache-find (pos &optional side
)
4853 "Find element in cache starting at POS or before.
4855 POS refers to a buffer position.
4857 When optional argument SIDE is non-nil, the function checks for
4858 elements starting at or past POS instead. If SIDE is `both', the
4859 function returns a cons cell where car is the first element
4860 starting at or before POS and cdr the first element starting
4863 The function can only find elements in the synchronized part of
4865 (let ((limit (and org-element--cache-sync-requests
4866 (aref (car org-element--cache-sync-requests
) 0)))
4867 (node (org-element--cache-root))
4870 (let* ((element (avl-tree--node-data node
))
4871 (begin (org-element-property :begin element
)))
4874 (not (org-element--cache-key-less-p
4875 (org-element--cache-key element
) limit
)))
4876 (setq node
(avl-tree--node-left node
)))
4879 node
(avl-tree--node-left node
)))
4882 node
(avl-tree--node-right node
)))
4883 ;; We found an element in cache starting at POS. If `side'
4884 ;; is `both' we also want the next one in order to generate
4885 ;; a key in-between.
4887 ;; If the element is the first row or item in a table or
4888 ;; a plain list, we always return the table or the plain
4891 ;; In any other case, we return the element found.
4893 (setq lower element
)
4894 (setq node
(avl-tree--node-right node
)))
4895 ((and (memq (org-element-type element
) '(item table-row
))
4896 (let ((parent (org-element-property :parent element
)))
4897 (and (= (org-element-property :begin element
)
4898 (org-element-property :contents-begin parent
))
4907 (both (cons lower upper
))
4909 (otherwise upper
))))
4911 (defun org-element--cache-put (element &optional data
)
4912 "Store ELEMENT in current buffer's cache, if allowed.
4913 When optional argument DATA is non-nil, assume is it object data
4914 relative to ELEMENT and store it in the objects cache."
4915 (cond ((not (org-element--cache-active-p)) nil
)
4917 (when org-element--cache-sync-requests
4918 ;; During synchronization, first build an appropriate key
4919 ;; for the new element so `avl-tree-enter' can insert it at
4920 ;; the right spot in the cache.
4921 (let ((keys (org-element--cache-find
4922 (org-element-property :begin element
) 'both
)))
4924 (org-element--cache-generate-key
4925 (and (car keys
) (org-element--cache-key (car keys
)))
4926 (cond ((cdr keys
) (org-element--cache-key (cdr keys
)))
4927 (org-element--cache-sync-requests
4928 (aref (car org-element--cache-sync-requests
) 0))))
4929 org-element--cache-sync-keys
)))
4930 (avl-tree-enter org-element--cache element
))
4931 ;; Headlines are not stored in cache, so objects in titles are
4932 ;; not stored either.
4933 ((eq (org-element-type element
) 'headline
) nil
)
4934 (t (puthash element data org-element--cache-objects
))))
4936 (defsubst org-element--cache-remove
(element)
4937 "Remove ELEMENT from cache.
4938 Assume ELEMENT belongs to cache and that a cache is active."
4939 (avl-tree-delete org-element--cache element
)
4940 (remhash element org-element--cache-objects
))
4943 ;;;; Synchronization
4945 (defsubst org-element--cache-set-timer
(buffer)
4946 "Set idle timer for cache synchronization in BUFFER."
4947 (when org-element--cache-sync-timer
4948 (cancel-timer org-element--cache-sync-timer
))
4949 (setq org-element--cache-sync-timer
4950 (run-with-idle-timer
4951 (let ((idle (current-idle-time)))
4952 (if idle
(time-add idle org-element-cache-sync-break
)
4953 org-element-cache-sync-idle-time
))
4955 #'org-element--cache-sync
4958 (defsubst org-element--cache-interrupt-p
(time-limit)
4959 "Non-nil when synchronization process should be interrupted.
4960 TIME-LIMIT is a time value or nil."
4962 (or (input-pending-p)
4963 (time-less-p time-limit
(current-time)))))
4965 (defsubst org-element--cache-shift-positions
(element offset
&optional props
)
4966 "Shift ELEMENT properties relative to buffer positions by OFFSET.
4968 Properties containing buffer positions are `:begin', `:end',
4969 `:contents-begin', `:contents-end' and `:structure'. When
4970 optional argument PROPS is a list of keywords, only shift
4971 properties provided in that list.
4973 Properties are modified by side-effect."
4974 (let ((properties (nth 1 element
)))
4975 ;; Shift `:structure' property for the first plain list only: it
4976 ;; is the only one that really matters and it prevents from
4977 ;; shifting it more than once.
4978 (when (and (or (not props
) (memq :structure props
))
4979 (eq (org-element-type element
) 'plain-list
)
4980 (not (eq (org-element-type (plist-get properties
:parent
))
4982 (dolist (item (plist-get properties
:structure
))
4983 (incf (car item
) offset
)
4984 (incf (nth 6 item
) offset
)))
4985 (dolist (key '(:begin
:contents-begin
:contents-end
:end
:post-affiliated
))
4986 (let ((value (and (or (not props
) (memq key props
))
4987 (plist-get properties key
))))
4988 (and value
(plist-put properties key
(+ offset value
)))))))
4990 (defun org-element--cache-sync (buffer &optional threshold future-change
)
4991 "Synchronize cache with recent modification in BUFFER.
4993 When optional argument THRESHOLD is non-nil, do the
4994 synchronization for all elements starting before or at threshold,
4995 then exit. Otherwise, synchronize cache for as long as
4996 `org-element-cache-sync-duration' or until Emacs leaves idle
4999 FUTURE-CHANGE, when non-nil, is a buffer position where changes
5000 not registered yet in the cache are going to happen. It is used
5001 in `org-element--cache-submit-request', where cache is partially
5002 updated before current modification are actually submitted."
5003 (when (buffer-live-p buffer
)
5004 (with-current-buffer buffer
5005 (let ((inhibit-quit t
) request next
)
5006 (when org-element--cache-sync-timer
5007 (cancel-timer org-element--cache-sync-timer
))
5009 (while org-element--cache-sync-requests
5010 (setq request
(car org-element--cache-sync-requests
)
5011 next
(nth 1 org-element--cache-sync-requests
))
5012 (org-element--cache-process-request
5014 (and next
(aref next
0))
5016 (and (not threshold
)
5017 (time-add (current-time)
5018 org-element-cache-sync-duration
))
5020 ;; Request processed. Merge current and next offsets and
5021 ;; transfer ending position.
5023 (incf (aref next
3) (aref request
3))
5024 (aset next
2 (aref request
2)))
5025 (setq org-element--cache-sync-requests
5026 (cdr org-element--cache-sync-requests
))))
5027 ;; If more requests are awaiting, set idle timer accordingly.
5028 ;; Otherwise, reset keys.
5029 (if org-element--cache-sync-requests
5030 (org-element--cache-set-timer buffer
)
5031 (clrhash org-element--cache-sync-keys
))))))
5033 (defun org-element--cache-process-request
5034 (request next threshold time-limit future-change
)
5035 "Process synchronization REQUEST for all entries before NEXT.
5037 REQUEST is a vector, built by `org-element--cache-submit-request'.
5039 NEXT is a cache key, as returned by `org-element--cache-key'.
5041 When non-nil, THRESHOLD is a buffer position. Synchronization
5042 stops as soon as a shifted element begins after it.
5044 When non-nil, TIME-LIMIT is a time value. Synchronization stops
5045 after this time or when Emacs exits idle state.
5047 When non-nil, FUTURE-CHANGE is a buffer position where changes
5048 not registered yet in the cache are going to happen. See
5049 `org-element--cache-submit-request' for more information.
5051 Throw `interrupt' if the process stops before completing the
5054 (when (= (aref request
6) 0)
5057 ;; Delete all elements starting after BEG, but not after buffer
5058 ;; position END or past element with key NEXT.
5060 ;; At each iteration, we start again at tree root since
5061 ;; a deletion modifies structure of the balanced tree.
5063 (let ((beg (aref request
0))
5064 (end (aref request
2))
5065 (outreach (aref request
4)))
5067 (when (org-element--cache-interrupt-p time-limit
)
5068 (throw 'interrupt nil
))
5069 ;; Find first element in cache with key BEG or after it.
5070 (let ((node (org-element--cache-root)) data data-key
)
5072 (let* ((element (avl-tree--node-data node
))
5073 (key (org-element--cache-key element
)))
5075 ((org-element--cache-key-less-p key beg
)
5076 (setq node
(avl-tree--node-right node
)))
5077 ((org-element--cache-key-less-p beg key
)
5080 node
(avl-tree--node-left node
)))
5081 (t (setq data element
5085 (let ((pos (org-element-property :begin data
)))
5086 (if (if (or (not next
)
5087 (org-element--cache-key-less-p data-key next
))
5090 (while (and up
(not (eq up outreach
)))
5091 (setq up
(org-element-property :parent up
)))
5093 (org-element--cache-remove data
)
5094 (aset request
0 data-key
)
5095 (aset request
1 pos
)
5097 (throw 'end-phase nil
)))
5098 ;; No element starting after modifications left in
5099 ;; cache: further processing is futile.
5100 (throw 'quit t
)))))))
5101 (when (= (aref request
6) 1)
5104 ;; Phase 0 left a hole in the cache. Some elements after it
5105 ;; could have parents within. For example, in the following
5115 ;; if we remove a blank line between "item" and "Paragraph1",
5116 ;; everything down to "Paragraph2" is removed from cache. But
5117 ;; the paragraph now belongs to the list, and its `:parent'
5118 ;; property no longer is accurate.
5120 ;; Therefore we need to parse again elements in the hole, or at
5121 ;; least in its last section, so that we can re-parent
5122 ;; subsequent elements, during phase 2.
5124 ;; Note that we only need to get the parent from the first
5125 ;; element in cache after the hole.
5127 ;; When next key is lesser or equal to the current one, delegate
5128 ;; phase 1 processing to next request in order to preserve key
5129 ;; order among requests.
5130 (let ((key (aref request
0)))
5131 (when (and next
(not (org-element--cache-key-less-p key next
)))
5132 (let ((next-request (nth 1 org-element--cache-sync-requests
)))
5133 (aset next-request
0 key
)
5134 (aset next-request
1 (aref request
1))
5135 (aset next-request
6 1))
5137 ;; Next element will start at its beginning position plus
5138 ;; offset, since it hasn't been shifted yet. Therefore, LIMIT
5139 ;; contains the real beginning position of the first element to
5140 ;; shift and re-parent.
5141 (let ((limit (+ (aref request
1) (aref request
3))))
5142 (cond ((and threshold
(> limit threshold
)) (throw 'interrupt nil
))
5143 ((and future-change
(>= limit future-change
))
5144 ;; Changes are going to happen around this element and
5145 ;; they will trigger another phase 1 request. Skip the
5149 (let ((parent (org-element--parse-to limit t time-limit
)))
5150 (aset request
5 parent
)
5151 (aset request
6 2))))))
5154 ;; Shift all elements starting from key START, but before NEXT, by
5155 ;; OFFSET, and re-parent them when appropriate.
5157 ;; Elements are modified by side-effect so the tree structure
5160 ;; Once THRESHOLD, if any, is reached, or once there is an input
5161 ;; pending, exit. Before leaving, the current synchronization
5162 ;; request is updated.
5163 (let ((start (aref request
0))
5164 (offset (aref request
3))
5165 (parent (aref request
5))
5166 (node (org-element--cache-root))
5170 ;; No re-parenting nor shifting planned: request is over.
5171 (when (and (not parent
) (zerop offset
)) (throw 'quit t
))
5173 (let* ((data (avl-tree--node-data node
))
5174 (key (org-element--cache-key data
)))
5175 (if (and leftp
(avl-tree--node-left node
)
5176 (not (org-element--cache-key-less-p key start
)))
5177 (progn (push node stack
)
5178 (setq node
(avl-tree--node-left node
)))
5179 (unless (org-element--cache-key-less-p key start
)
5180 ;; We reached NEXT. Request is complete.
5181 (when (equal key next
) (throw 'quit t
))
5182 ;; Handle interruption request. Update current request.
5183 (when (or exit-flag
(org-element--cache-interrupt-p time-limit
))
5184 (aset request
0 key
)
5185 (aset request
5 parent
)
5186 (throw 'interrupt nil
))
5188 (unless (zerop offset
)
5189 (org-element--cache-shift-positions data offset
)
5190 ;; Shift associated objects data, if any.
5191 (dolist (object-data (gethash data org-element--cache-objects
))
5192 (dolist (object (cddr object-data
))
5193 (org-element--cache-shift-positions object offset
))))
5194 (let ((begin (org-element-property :begin data
)))
5195 ;; Update PARENT and re-parent DATA, only when
5196 ;; necessary. Propagate new structures for lists.
5198 (<= (org-element-property :end parent
) begin
))
5199 (setq parent
(org-element-property :parent parent
)))
5200 (cond ((and (not parent
) (zerop offset
)) (throw 'quit nil
))
5202 (let ((p (org-element-property :parent data
)))
5204 (< (org-element-property :begin p
)
5205 (org-element-property :begin parent
)))))
5206 (org-element-put-property data
:parent parent
)
5207 (let ((s (org-element-property :structure parent
)))
5208 (when (and s
(org-element-property :structure data
))
5209 (org-element-put-property data
:structure s
)))))
5210 ;; Cache is up-to-date past THRESHOLD. Request
5212 (when (and threshold
(> begin threshold
)) (setq exit-flag t
))))
5213 (setq node
(if (setq leftp
(avl-tree--node-right node
))
5214 (avl-tree--node-right node
)
5216 ;; We reached end of tree: synchronization complete.
5219 (defun org-element--parse-to (pos &optional syncp time-limit
)
5220 "Parse elements in current section, down to POS.
5222 Start parsing from the closest between the last known element in
5223 cache or headline above. Return the smallest element containing
5226 When optional argument SYNCP is non-nil, return the parent of the
5227 element containing POS instead. In that case, it is also
5228 possible to provide TIME-LIMIT, which is a time value specifying
5229 when the parsing should stop. The function throws `interrupt' if
5230 the process stopped before finding the expected result."
5232 (org-with-wide-buffer
5234 (let* ((cached (and (org-element--cache-active-p)
5235 (org-element--cache-find pos nil
)))
5236 (begin (org-element-property :begin cached
))
5239 ;; Nothing in cache before point: start parsing from first
5240 ;; element following headline above, or first element in
5243 (when (org-with-limited-levels (outline-previous-heading))
5244 (setq mode
'planning
)
5246 (skip-chars-forward " \r\t\n")
5247 (beginning-of-line))
5248 ;; Cache returned exact match: return it.
5250 (throw 'exit
(if syncp
(org-element-property :parent cached
) cached
)))
5251 ;; There's a headline between cached value and POS: cached
5252 ;; value is invalid. Start parsing from first element
5253 ;; following the headline.
5254 ((re-search-backward
5255 (org-with-limited-levels org-outline-regexp-bol
) begin t
)
5257 (skip-chars-forward " \r\t\n")
5259 (setq mode
'planning
))
5260 ;; Check if CACHED or any of its ancestors contain point.
5262 ;; If there is such an element, we inspect it in order to know
5263 ;; if we return it or if we need to parse its contents.
5264 ;; Otherwise, we just start parsing from current location,
5265 ;; which is right after the top-most element containing
5268 ;; As a special case, if POS is at the end of the buffer, we
5269 ;; want to return the innermost element ending there.
5271 ;; Also, if we find an ancestor and discover that we need to
5272 ;; parse its contents, make sure we don't start from
5273 ;; `:contents-begin', as we would otherwise go past CACHED
5274 ;; again. Instead, in that situation, we will resume parsing
5275 ;; from NEXT, which is located after CACHED or its higher
5276 ;; ancestor not containing point.
5279 (pos (if (= (point-max) pos
) (1- pos
) pos
)))
5280 (goto-char (or (org-element-property :contents-begin cached
) begin
))
5281 (while (let ((end (org-element-property :end up
)))
5284 (setq up
(org-element-property :parent up
)))))
5286 ((eobp) (setq element up
))
5287 (t (setq element up next
(point)))))))
5288 ;; Parse successively each element until we reach POS.
5289 (let ((end (or (org-element-property :end element
)
5291 (org-with-limited-levels (outline-next-heading))
5296 (cond ((= (point) pos
) (throw 'exit parent
))
5297 ((org-element--cache-interrupt-p time-limit
)
5298 (throw 'interrupt nil
))))
5300 (setq element
(org-element--current-element
5302 (org-element-property :structure parent
)))
5303 (org-element-put-property element
:parent parent
)
5304 (org-element--cache-put element
))
5305 (let ((elem-end (org-element-property :end element
))
5306 (type (org-element-type element
)))
5308 ;; Skip any element ending before point. Also skip
5309 ;; element ending at point (unless it is also the end of
5310 ;; buffer) since we're sure that another element begins
5312 ((and (<= elem-end pos
) (/= (point-max) elem-end
))
5313 (goto-char elem-end
)
5314 (setq mode
(org-element--next-mode type nil
)))
5315 ;; A non-greater element contains point: return it.
5316 ((not (memq type org-element-greater-elements
))
5317 (throw 'exit element
))
5318 ;; Otherwise, we have to decide if ELEMENT really
5319 ;; contains POS. In that case we start parsing from
5320 ;; contents' beginning.
5322 ;; If POS is at contents' beginning but it is also at
5323 ;; the beginning of the first item in a list or a table.
5324 ;; In that case, we need to create an anchor for that
5325 ;; list or table, so return it.
5327 ;; Also, if POS is at the end of the buffer, no element
5328 ;; can start after it, but more than one may end there.
5329 ;; Arbitrarily, we choose to return the innermost of
5331 ((let ((cbeg (org-element-property :contents-begin element
))
5332 (cend (org-element-property :contents-end element
)))
5337 (not (memq type
'(plain-list table
)))))
5339 (and (= cend pos
) (= (point-max) pos
)))))
5340 (goto-char (or next cbeg
))
5342 mode
(org-element--next-mode type t
)
5345 ;; Otherwise, return ELEMENT as it is the smallest
5346 ;; element containing POS.
5347 (t (throw 'exit element
))))
5348 (setq element nil
)))))))
5351 ;;;; Staging Buffer Changes
5353 (defconst org-element--cache-sensitive-re
5355 org-outline-regexp-bol
"\\|"
5358 "#\\+\\(?:BEGIN[:_]\\|END\\(?:_\\|:?[ \t]*$\\)\\)" "\\|"
5359 ;; LaTeX environments.
5360 "\\\\\\(?:begin{[A-Za-z0-9*]+}\\|end{[A-Za-z0-9*]+}[ \t]*$\\)" "\\|"
5362 ":\\(?:\\w\\|[-_]\\)+:[ \t]*$"
5364 "Regexp matching a sensitive line, structure wise.
5365 A sensitive line is a headline, inlinetask, block, drawer, or
5366 latex-environment boundary. When such a line is modified,
5367 structure changes in the document may propagate in the whole
5368 section, possibly making cache invalid.")
5370 (defvar org-element--cache-change-warning nil
5371 "Non-nil when a sensitive line is about to be changed.
5372 It is a symbol among nil, t and `headline'.")
5374 (defun org-element--cache-before-change (beg end
)
5375 "Request extension of area going to be modified if needed.
5376 BEG and END are the beginning and end of the range of changed
5377 text. See `before-change-functions' for more information."
5378 (when (org-element--cache-active-p)
5379 (org-with-wide-buffer
5382 (let ((bottom (save-excursion (goto-char end
) (line-end-position))))
5383 (setq org-element--cache-change-warning
5385 (if (and (org-with-limited-levels (org-at-heading-p))
5386 (= (line-end-position) bottom
))
5388 (let ((case-fold-search t
))
5390 org-element--cache-sensitive-re bottom t
)))))))))
5392 (defun org-element--cache-after-change (beg end pre
)
5393 "Update buffer modifications for current buffer.
5394 BEG and END are the beginning and end of the range of changed
5395 text, and the length in bytes of the pre-change text replaced by
5396 that range. See `after-change-functions' for more information."
5397 (when (org-element--cache-active-p)
5398 (org-with-wide-buffer
5403 (bottom (save-excursion (goto-char end
) (line-end-position))))
5404 ;; Determine if modified area needs to be extended, according
5405 ;; to both previous and current state. We make a special
5406 ;; case for headline editing: if a headline is modified but
5407 ;; not removed, do not extend.
5408 (when (case org-element--cache-change-warning
5411 (not (and (org-with-limited-levels (org-at-heading-p))
5412 (= (line-end-position) bottom
))))
5414 (let ((case-fold-search t
))
5416 org-element--cache-sensitive-re bottom t
))))
5417 ;; Effectively extend modified area.
5418 (org-with-limited-levels
5419 (setq top
(progn (goto-char top
)
5420 (when (outline-previous-heading) (forward-line))
5422 (setq bottom
(progn (goto-char bottom
)
5423 (if (outline-next-heading) (1- (point))
5425 ;; Store synchronization request.
5426 (let ((offset (- end beg pre
)))
5427 (org-element--cache-submit-request top
(- bottom offset
) offset
)))))
5428 ;; Activate a timer to process the request during idle time.
5429 (org-element--cache-set-timer (current-buffer))))
5431 (defun org-element--cache-for-removal (beg end offset
)
5432 "Return first element to remove from cache.
5434 BEG and END are buffer positions delimiting buffer modifications.
5435 OFFSET is the size of the changes.
5437 Returned element is usually the first element in cache containing
5438 any position between BEG and END. As an exception, greater
5439 elements around the changes that are robust to contents
5440 modifications are preserved and updated according to the
5442 (let* ((elements (org-element--cache-find (1- beg
) 'both
))
5443 (before (car elements
))
5444 (after (cdr elements
)))
5445 (if (not before
) after
5449 (if (and (memq (org-element-type up
)
5450 '(center-block drawer dynamic-block
5451 quote-block special-block
))
5452 (let ((cbeg (org-element-property :contents-begin up
)))
5455 (> (org-element-property :contents-end up
) end
))))
5456 ;; UP is a robust greater element containing changes.
5457 ;; We only need to extend its ending boundaries.
5458 (org-element--cache-shift-positions
5459 up offset
'(:contents-end
:end
))
5461 (when robust-flag
(setq robust-flag nil
)))
5462 (setq up
(org-element-property :parent up
)))
5463 ;; We're at top level element containing ELEMENT: if it's
5464 ;; altered by buffer modifications, it is first element in
5465 ;; cache to be removed. Otherwise, that first element is the
5468 ;; As a special case, do not remove BEFORE if it is a robust
5469 ;; container for current changes.
5470 (if (or (< (org-element-property :end before
) beg
) robust-flag
) after
5473 (defun org-element--cache-submit-request (beg end offset
)
5474 "Submit a new cache synchronization request for current buffer.
5475 BEG and END are buffer positions delimiting the minimal area
5476 where cache data should be removed. OFFSET is the size of the
5477 change, as an integer."
5478 (let ((next (car org-element--cache-sync-requests
))
5479 delete-to delete-from
)
5481 (zerop (aref next
6))
5482 (> (setq delete-to
(+ (aref next
2) (aref next
3))) end
)
5483 (<= (setq delete-from
(aref next
1)) end
))
5484 ;; Current changes can be merged with first sync request: we
5485 ;; can save a partial cache synchronization.
5487 (incf (aref next
3) offset
)
5488 ;; If last change happened within area to be removed, extend
5489 ;; boundaries of robust parents, if any. Otherwise, find
5490 ;; first element to remove and update request accordingly.
5491 (if (> beg delete-from
)
5492 (let ((up (aref next
5)))
5494 (org-element--cache-shift-positions
5495 up offset
'(:contents-end
:end
))
5496 (setq up
(org-element-property :parent up
))))
5497 (let ((first (org-element--cache-for-removal beg delete-to offset
)))
5499 (aset next
0 (org-element--cache-key first
))
5500 (aset next
1 (org-element-property :begin first
))
5501 (aset next
5 (org-element-property :parent first
))))))
5502 ;; Ensure cache is correct up to END. Also make sure that NEXT,
5503 ;; if any, is no longer a 0-phase request, thus ensuring that
5504 ;; phases are properly ordered. We need to provide OFFSET as
5505 ;; optional parameter since current modifications are not known
5506 ;; yet to the otherwise correct part of the cache (i.e, before
5507 ;; the first request).
5508 (when next
(org-element--cache-sync (current-buffer) end beg
))
5509 (let ((first (org-element--cache-for-removal beg end offset
)))
5511 (push (let ((beg (org-element-property :begin first
))
5512 (key (org-element--cache-key first
)))
5514 ;; When changes happen before the first known
5515 ;; element, re-parent and shift the rest of the
5517 ((> beg end
) (vector key beg nil offset nil nil
1))
5518 ;; Otherwise, we find the first non robust
5519 ;; element containing END. All elements between
5520 ;; FIRST and this one are to be removed.
5522 ;; Among them, some could be located outside the
5523 ;; synchronized part of the cache, in which case
5524 ;; comparing buffer positions to find them is
5525 ;; useless. Instead, we store the element
5526 ;; containing them in the request itself. All
5527 ;; its children will be removed.
5528 ((let ((first-end (org-element-property :end first
)))
5529 (and (> first-end end
)
5530 (vector key beg first-end offset first
5531 (org-element-property :parent first
) 0))))
5533 (let* ((element (org-element--cache-find end
))
5534 (end (org-element-property :end element
))
5536 (while (and (setq up
(org-element-property :parent up
))
5537 (>= (org-element-property :begin up
) beg
))
5538 (setq end
(org-element-property :end up
)
5540 (vector key beg end offset element
5541 (org-element-property :parent first
) 0)))))
5542 org-element--cache-sync-requests
)
5543 ;; No element to remove. No need to re-parent either.
5544 ;; Simply shift additional elements, if any, by OFFSET.
5545 (when org-element--cache-sync-requests
5546 (incf (aref (car org-element--cache-sync-requests
) 3) offset
)))))))
5549 ;;;; Public Functions
5552 (defun org-element-cache-reset (&optional all
)
5553 "Reset cache in current buffer.
5554 When optional argument ALL is non-nil, reset cache in all Org
5557 (dolist (buffer (if all
(buffer-list) (list (current-buffer))))
5558 (with-current-buffer buffer
5559 (when (org-element--cache-active-p)
5560 (org-set-local 'org-element--cache
5561 (avl-tree-create #'org-element--cache-compare
))
5562 (org-set-local 'org-element--cache-objects
(make-hash-table :test
#'eq
))
5563 (org-set-local 'org-element--cache-sync-keys
5564 (make-hash-table :weakness
'key
:test
#'eq
))
5565 (org-set-local 'org-element--cache-change-warning nil
)
5566 (org-set-local 'org-element--cache-sync-requests nil
)
5567 (org-set-local 'org-element--cache-sync-timer nil
)
5568 (add-hook 'before-change-functions
5569 #'org-element--cache-before-change nil t
)
5570 (add-hook 'after-change-functions
5571 #'org-element--cache-after-change nil t
)))))
5574 (defun org-element-cache-refresh (pos)
5575 "Refresh cache at position POS."
5576 (when (org-element--cache-active-p)
5577 (org-element--cache-sync (current-buffer) pos
)
5578 (org-element--cache-submit-request pos pos
0)
5579 (org-element--cache-set-timer (current-buffer))))
5585 ;; The first move is to implement a way to obtain the smallest element
5586 ;; containing point. This is the job of `org-element-at-point'. It
5587 ;; basically jumps back to the beginning of section containing point
5588 ;; and proceed, one element after the other, with
5589 ;; `org-element--current-element' until the container is found. Note:
5590 ;; When using `org-element-at-point', secondary values are never
5591 ;; parsed since the function focuses on elements, not on objects.
5593 ;; At a deeper level, `org-element-context' lists all elements and
5594 ;; objects containing point.
5596 ;; `org-element-nested-p' and `org-element-swap-A-B' may be used
5597 ;; internally by navigation and manipulation tools.
5601 (defun org-element-at-point ()
5602 "Determine closest element around point.
5604 Return value is a list like (TYPE PROPS) where TYPE is the type
5605 of the element and PROPS a plist of properties associated to the
5608 Possible types are defined in `org-element-all-elements'.
5609 Properties depend on element or object type, but always include
5610 `:begin', `:end', `:parent' and `:post-blank' properties.
5612 As a special case, if point is at the very beginning of the first
5613 item in a list or sub-list, returned element will be that list
5614 instead of the item. Likewise, if point is at the beginning of
5615 the first row of a table, returned element will be the table
5616 instead of the first row.
5618 When point is at the end of the buffer, return the innermost
5619 element ending there."
5620 (org-with-wide-buffer
5621 (let ((origin (point)))
5623 (skip-chars-backward " \r\t\n")
5625 ;; Within blank lines at the beginning of buffer, return nil.
5627 ;; Within blank lines right after a headline, return that
5629 ((org-with-limited-levels (org-at-heading-p))
5631 (org-element-headline-parser (point-max) t
))
5632 ;; Otherwise parse until we find element containing ORIGIN.
5634 (when (org-element--cache-active-p)
5635 (if (not org-element--cache
) (org-element-cache-reset)
5636 (org-element--cache-sync (current-buffer) origin
)))
5637 (org-element--parse-to origin
))))))
5640 (defun org-element-context (&optional element
)
5641 "Return smallest element or object around point.
5643 Return value is a list like (TYPE PROPS) where TYPE is the type
5644 of the element or object and PROPS a plist of properties
5647 Possible types are defined in `org-element-all-elements' and
5648 `org-element-all-objects'. Properties depend on element or
5649 object type, but always include `:begin', `:end', `:parent' and
5652 As a special case, if point is right after an object and not at
5653 the beginning of any other object, return that object.
5655 Optional argument ELEMENT, when non-nil, is the closest element
5656 containing point, as returned by `org-element-at-point'.
5657 Providing it allows for quicker computation."
5658 (catch 'objects-forbidden
5659 (org-with-wide-buffer
5660 (let* ((pos (point))
5661 (element (or element
(org-element-at-point)))
5662 (type (org-element-type element
)))
5663 ;; If point is inside an element containing objects or
5664 ;; a secondary string, narrow buffer to the container and
5665 ;; proceed with parsing. Otherwise, return ELEMENT.
5667 ;; At a parsed affiliated keyword, check if we're inside main
5669 ((let ((post (org-element-property :post-affiliated element
)))
5670 (and post
(< pos post
)))
5672 (let ((case-fold-search t
)) (looking-at org-element--affiliated-re
))
5674 ((not (member-ignore-case (match-string 1)
5675 org-element-parsed-keywords
))
5676 (throw 'objects-forbidden element
))
5677 ((< (match-end 0) pos
)
5678 (narrow-to-region (match-end 0) (line-end-position)))
5679 ((and (match-beginning 2)
5680 (>= pos
(match-beginning 2))
5681 (< pos
(match-end 2)))
5682 (narrow-to-region (match-beginning 2) (match-end 2)))
5683 (t (throw 'objects-forbidden element
)))
5684 ;; Also change type to retrieve correct restrictions.
5685 (setq type
'keyword
))
5686 ;; At an item, objects can only be located within tag, if any.
5688 (let ((tag (org-element-property :tag element
)))
5689 (if (not tag
) (throw 'objects-forbidden element
)
5691 (search-forward tag
(line-end-position))
5692 (goto-char (match-beginning 0))
5693 (if (and (>= pos
(point)) (< pos
(match-end 0)))
5694 (narrow-to-region (point) (match-end 0))
5695 (throw 'objects-forbidden element
)))))
5696 ;; At an headline or inlinetask, objects are in title.
5697 ((memq type
'(headline inlinetask
))
5698 (goto-char (org-element-property :begin element
))
5699 (skip-chars-forward "*")
5700 (if (and (> pos
(point)) (< pos
(line-end-position)))
5701 (narrow-to-region (point) (line-end-position))
5702 (throw 'objects-forbidden element
)))
5703 ;; At a paragraph, a table-row or a verse block, objects are
5704 ;; located within their contents.
5705 ((memq type
'(paragraph table-row verse-block
))
5706 (let ((cbeg (org-element-property :contents-begin element
))
5707 (cend (org-element-property :contents-end element
)))
5708 ;; CBEG is nil for table rules.
5709 (if (and cbeg cend
(>= pos cbeg
)
5710 (or (< pos cend
) (and (= pos cend
) (eobp))))
5711 (narrow-to-region cbeg cend
)
5712 (throw 'objects-forbidden element
))))
5713 ;; At a parsed keyword, objects are located within value.
5715 (if (not (member (org-element-property :key element
)
5716 org-element-document-properties
))
5717 (throw 'objects-forbidden element
)
5718 (goto-char (org-element-property :begin element
))
5719 (search-forward ":")
5720 (if (and (>= pos
(point)) (< pos
(line-end-position)))
5721 (narrow-to-region (point) (line-end-position))
5722 (throw 'objects-forbidden element
))))
5723 ;; At a planning line, if point is at a timestamp, return it,
5724 ;; otherwise, return element.
5725 ((eq type
'planning
)
5726 (dolist (p '(:closed
:deadline
:scheduled
))
5727 (let ((timestamp (org-element-property p element
)))
5728 (when (and timestamp
5729 (<= (org-element-property :begin timestamp
) pos
)
5730 (> (org-element-property :end timestamp
) pos
))
5731 (throw 'objects-forbidden timestamp
))))
5732 ;; All other locations cannot contain objects: bail out.
5733 (throw 'objects-forbidden element
))
5734 (t (throw 'objects-forbidden element
)))
5735 (goto-char (point-min))
5736 (let ((restriction (org-element-restriction type
))
5738 (cache (cond ((not (org-element--cache-active-p)) nil
)
5739 (org-element--cache-objects
5740 (gethash element org-element--cache-objects
))
5741 (t (org-element-cache-reset) nil
)))
5742 next object-data last
)
5746 ;; When entering PARENT for the first time, get list
5747 ;; of objects within known so far. Store it in
5750 (let ((data (assq parent cache
)))
5751 (if data
(setq object-data data
)
5752 (push (setq object-data
(list parent nil
)) cache
))))
5753 ;; Find NEXT object for analysis.
5755 ;; If NEXT is non-nil, we already exhausted the
5756 ;; cache so we can parse buffer to find the object
5758 (if next
(setq next
(org-element--object-lex restriction
))
5759 ;; Otherwise, check if cache can help us.
5760 (let ((objects (cddr object-data
))
5761 (completep (nth 1 object-data
)))
5763 ((and (not objects
) completep
) (throw 'exit parent
))
5765 (setq next
(org-element--object-lex restriction
)))
5768 (org-element-property :end
(car objects
))))
5769 (if (>= cache-limit pos
)
5770 ;; Cache contains the information needed.
5771 (dolist (object objects
(throw 'exit parent
))
5772 (when (<= (org-element-property :begin object
)
5774 (if (>= (org-element-property :end object
)
5776 (throw 'found
(setq next object
))
5777 (throw 'exit parent
))))
5778 (goto-char cache-limit
)
5780 (org-element--object-lex restriction
))))))))
5781 ;; If we have a new object to analyze, store it in
5782 ;; cache. Otherwise record that there is nothing
5783 ;; more to parse in this element at this depth.
5785 (progn (org-element-put-property next
:parent parent
)
5786 (push next
(cddr object-data
)))
5787 (setcar (cdr object-data
) t
)))
5788 ;; Process NEXT, if any, in order to know if we need
5789 ;; to skip it, return it or move into it.
5790 (if (or (not next
) (> (org-element-property :begin next
) pos
))
5791 (throw 'exit
(or last parent
))
5792 (let ((end (org-element-property :end next
))
5793 (cbeg (org-element-property :contents-begin next
))
5794 (cend (org-element-property :contents-end next
)))
5796 ;; Skip objects ending before point. Also skip
5797 ;; objects ending at point unless it is also the
5798 ;; end of buffer, since we want to return the
5799 ;; innermost object.
5800 ((and (<= end pos
) (/= (point-max) end
))
5802 ;; For convenience, when object ends at POS,
5803 ;; without any space, store it in LAST, as we
5804 ;; will return it if no object starts here.
5805 (when (and (= end pos
)
5806 (not (memq (char-before) '(?\s ?
\t))))
5808 ;; If POS is within a container object, move
5809 ;; into that object.
5813 ;; At contents' end, if there is no
5814 ;; space before point, also move into
5815 ;; object, for consistency with
5816 ;; convenience feature above.
5818 (or (= (point-max) pos
)
5819 (not (memq (char-before pos
)
5822 (narrow-to-region (point) cend
)
5824 restriction
(org-element-restriction next
)
5827 ;; Otherwise, return NEXT.
5828 (t (throw 'exit next
)))))))
5829 ;; Store results in cache, if applicable.
5830 (org-element--cache-put element cache
)))))))
5832 (defun org-element-lineage (blob &optional types with-self
)
5833 "List all ancestors of a given element or object.
5835 BLOB is an object or element.
5837 When optional argument TYPES is a list of symbols, return the
5838 first element or object in the lineage whose type belongs to that
5841 When optional argument WITH-SELF is non-nil, lineage includes
5842 BLOB itself as the first element, and TYPES, if provided, also
5845 When BLOB is obtained through `org-element-context' or
5846 `org-element-at-point', only ancestors from its section can be
5847 found. There is no such limitation when BLOB belongs to a full
5849 (let ((up (if with-self blob
(org-element-property :parent blob
)))
5851 (while (and up
(not (memq (org-element-type up
) types
)))
5852 (unless types
(push up ancestors
))
5853 (setq up
(org-element-property :parent up
)))
5854 (if types up
(nreverse ancestors
))))
5856 (defun org-element-nested-p (elem-A elem-B
)
5857 "Non-nil when elements ELEM-A and ELEM-B are nested."
5858 (let ((beg-A (org-element-property :begin elem-A
))
5859 (beg-B (org-element-property :begin elem-B
))
5860 (end-A (org-element-property :end elem-A
))
5861 (end-B (org-element-property :end elem-B
)))
5862 (or (and (>= beg-A beg-B
) (<= end-A end-B
))
5863 (and (>= beg-B beg-A
) (<= end-B end-A
)))))
5865 (defun org-element-swap-A-B (elem-A elem-B
)
5866 "Swap elements ELEM-A and ELEM-B.
5867 Assume ELEM-B is after ELEM-A in the buffer. Leave point at the
5869 (goto-char (org-element-property :begin elem-A
))
5870 ;; There are two special cases when an element doesn't start at bol:
5871 ;; the first paragraph in an item or in a footnote definition.
5872 (let ((specialp (not (bolp))))
5873 ;; Only a paragraph without any affiliated keyword can be moved at
5874 ;; ELEM-A position in such a situation. Note that the case of
5875 ;; a footnote definition is impossible: it cannot contain two
5876 ;; paragraphs in a row because it cannot contain a blank line.
5878 (or (not (eq (org-element-type elem-B
) 'paragraph
))
5879 (/= (org-element-property :begin elem-B
)
5880 (org-element-property :contents-begin elem-B
))))
5881 (error "Cannot swap elements"))
5882 ;; In a special situation, ELEM-A will have no indentation. We'll
5883 ;; give it ELEM-B's (which will in, in turn, have no indentation).
5884 (let* ((ind-B (when specialp
5885 (goto-char (org-element-property :begin elem-B
))
5886 (org-get-indentation)))
5887 (beg-A (org-element-property :begin elem-A
))
5888 (end-A (save-excursion
5889 (goto-char (org-element-property :end elem-A
))
5890 (skip-chars-backward " \r\t\n")
5892 (beg-B (org-element-property :begin elem-B
))
5893 (end-B (save-excursion
5894 (goto-char (org-element-property :end elem-B
))
5895 (skip-chars-backward " \r\t\n")
5897 ;; Store inner overlays responsible for visibility status.
5898 ;; We also need to store their boundaries as they will be
5899 ;; removed from buffer.
5904 (and (>= (overlay-start o
) beg-A
)
5905 (<= (overlay-end o
) end-A
)
5906 (list o
(overlay-start o
) (overlay-end o
))))
5907 (overlays-in beg-A end-A
)))
5910 (and (>= (overlay-start o
) beg-B
)
5911 (<= (overlay-end o
) end-B
)
5912 (list o
(overlay-start o
) (overlay-end o
))))
5913 (overlays-in beg-B end-B
)))))
5915 (body-A (buffer-substring beg-A end-A
))
5916 (body-B (delete-and-extract-region beg-B end-B
)))
5919 (setq body-B
(replace-regexp-in-string "\\`[ \t]*" "" body-B
))
5920 (org-indent-to-column ind-B
))
5922 ;; Restore ex ELEM-A overlays.
5923 (let ((offset (- beg-B beg-A
)))
5924 (dolist (o (car overlays
))
5925 (move-overlay (car o
) (+ (nth 1 o
) offset
) (+ (nth 2 o
) offset
)))
5927 (delete-region beg-A end-A
)
5929 ;; Restore ex ELEM-B overlays.
5930 (dolist (o (cdr overlays
))
5931 (move-overlay (car o
) (- (nth 1 o
) offset
) (- (nth 2 o
) offset
))))
5932 (goto-char (org-element-property :end elem-B
)))))
5934 (defun org-element-remove-indentation (s &optional n
)
5935 "Remove maximum common indentation in string S and return it.
5936 When optional argument N is a positive integer, remove exactly
5937 that much characters from indentation, if possible, or return
5938 S as-is otherwise. Unlike to `org-remove-indentation', this
5939 function doesn't call `untabify' on S."
5943 (goto-char (point-min))
5944 ;; Find maximum common indentation, if not specified.
5946 (let ((min-ind (point-max)))
5948 (while (re-search-forward "^[ \t]*\\S-" nil t
)
5949 (let ((ind (1- (current-column))))
5950 (if (zerop ind
) (throw 'exit s
)
5951 (setq min-ind
(min min-ind ind
))))))
5954 ;; Remove exactly N indentation, but give up if not possible.
5956 (let ((ind (progn (skip-chars-forward " \t") (current-column))))
5957 (cond ((eolp) (delete-region (line-beginning-position) (point)))
5958 ((< ind n
) (throw 'exit s
))
5959 (t (org-indent-line-to (- ind n
))))
5965 (provide 'org-element
)
5968 ;; generated-autoload-file: "org-loaddefs.el"
5971 ;;; org-element.el ends here