1 ;;; org-element.el --- Parser And Applications for Org syntax
3 ;; Copyright (C) 2012-2014 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 (defvar org-element-block-name-alist
253 '(("CENTER" . org-element-center-block-parser
)
254 ("COMMENT" . org-element-comment-block-parser
)
255 ("EXAMPLE" . org-element-example-block-parser
)
256 ("QUOTE" . org-element-quote-block-parser
)
257 ("SRC" . org-element-src-block-parser
)
258 ("VERSE" . org-element-verse-block-parser
))
259 "Alist between block names and the associated parsing function.
260 Names must be uppercase. Any block whose name has no association
261 is parsed with `org-element-special-block-parser'.")
263 (defconst org-element-link-type-is-file
264 '("file" "file+emacs" "file+sys" "docview")
265 "List of link types equivalent to \"file\".
266 Only these types can accept search options and an explicit
267 application to open them.")
269 (defconst org-element-affiliated-keywords
270 '("CAPTION" "DATA" "HEADER" "HEADERS" "LABEL" "NAME" "PLOT" "RESNAME" "RESULT"
271 "RESULTS" "SOURCE" "SRCNAME" "TBLNAME")
272 "List of affiliated keywords as strings.
273 By default, all keywords setting attributes (e.g., \"ATTR_LATEX\")
274 are affiliated keywords and need not to be in this list.")
276 (defconst org-element-keyword-translation-alist
277 '(("DATA" .
"NAME") ("LABEL" .
"NAME") ("RESNAME" .
"NAME")
278 ("SOURCE" .
"NAME") ("SRCNAME" .
"NAME") ("TBLNAME" .
"NAME")
279 ("RESULT" .
"RESULTS") ("HEADERS" .
"HEADER"))
280 "Alist of usual translations for keywords.
281 The key is the old name and the value the new one. The property
282 holding their value will be named after the translated name.")
284 (defconst org-element-multiple-keywords
'("CAPTION" "HEADER")
285 "List of affiliated keywords that can occur more than once in an element.
287 Their value will be consed into a list of strings, which will be
288 returned as the value of the property.
290 This list is checked after translations have been applied. See
291 `org-element-keyword-translation-alist'.
293 By default, all keywords setting attributes (e.g., \"ATTR_LATEX\")
294 allow multiple occurrences and need not to be in this list.")
296 (defconst org-element-parsed-keywords
'("CAPTION")
297 "List of affiliated keywords whose value can be parsed.
299 Their value will be stored as a secondary string: a list of
302 This list is checked after translations have been applied. See
303 `org-element-keyword-translation-alist'.")
305 (defconst org-element--parsed-properties-alist
306 (mapcar (lambda (k) (cons k
(intern (concat ":" (downcase k
)))))
307 org-element-parsed-keywords
)
308 "Alist of parsed keywords and associated properties.
309 This is generated from `org-element-parsed-keywords', which
312 (defconst org-element-dual-keywords
'("CAPTION" "RESULTS")
313 "List of affiliated keywords which can have a secondary value.
315 In Org syntax, they can be written with optional square brackets
316 before the colons. For example, RESULTS keyword can be
317 associated to a hash value with the following:
319 #+RESULTS[hash-string]: some-source
321 This list is checked after translations have been applied. See
322 `org-element-keyword-translation-alist'.")
324 (defconst org-element-document-properties
'("AUTHOR" "DATE" "TITLE")
325 "List of properties associated to the whole document.
326 Any keyword in this list will have its value parsed and stored as
327 a secondary string.")
329 (defconst org-element--affiliated-re
330 (format "[ \t]*#\\+\\(?:%s\\):\\(?: \\|$\\)"
332 ;; Dual affiliated keywords.
333 (format "\\(?1:%s\\)\\(?:\\[\\(.*\\)\\]\\)?"
334 (regexp-opt org-element-dual-keywords
))
336 ;; Regular affiliated keywords.
337 (format "\\(?1:%s\\)"
341 (member keyword org-element-dual-keywords
))
342 org-element-affiliated-keywords
)))
344 ;; Export attributes.
345 "\\(?1:ATTR_[-_A-Za-z0-9]+\\)"))
346 "Regexp matching any affiliated keyword.
348 Keyword name is put in match group 1. Moreover, if keyword
349 belongs to `org-element-dual-keywords', put the dual value in
352 Don't modify it, set `org-element-affiliated-keywords' instead.")
354 (defconst org-element-object-restrictions
355 (let* ((standard-set (remq 'table-cell org-element-all-objects
))
356 (standard-set-no-line-break (remq 'line-break standard-set
)))
357 `((bold ,@standard-set
)
358 (footnote-reference ,@standard-set
)
359 (headline ,@standard-set-no-line-break
)
360 (inlinetask ,@standard-set-no-line-break
)
361 (italic ,@standard-set
)
362 (item ,@standard-set-no-line-break
)
363 (keyword ,@standard-set
)
364 ;; Ignore all links excepted plain links in a link description.
365 ;; Also ignore radio-targets and line breaks.
366 (link bold code entity export-snippet inline-babel-call inline-src-block
367 italic latex-fragment macro plain-link statistics-cookie
368 strike-through subscript superscript underline verbatim
)
369 (paragraph ,@standard-set
)
370 ;; Remove any variable object from radio target as it would
371 ;; prevent it from being properly recognized.
372 (radio-target bold code entity italic latex-fragment strike-through
373 subscript superscript underline superscript
)
374 (strike-through ,@standard-set
)
375 (subscript ,@standard-set
)
376 (superscript ,@standard-set
)
377 ;; Ignore inline babel call and inline src block as formulas are
378 ;; possible. Also ignore line breaks and statistics cookies.
379 (table-cell bold code entity export-snippet footnote-reference italic
380 latex-fragment link macro radio-target strike-through
381 subscript superscript target timestamp underline verbatim
)
382 (table-row table-cell
)
383 (underline ,@standard-set
)
384 (verse-block ,@standard-set
)))
385 "Alist of objects restrictions.
387 key is an element or object type containing objects and value is
388 a list of types that can be contained within an element or object
391 For example, in a `radio-target' object, one can only find
392 entities, latex-fragments, subscript, superscript and text
395 This alist also applies to secondary string. For example, an
396 `headline' type element doesn't directly contain objects, but
397 still has an entry since one of its properties (`:title') does.")
399 (defconst org-element-secondary-value-alist
400 '((headline .
:title
)
401 (inlinetask .
:title
)
403 "Alist between element types and location of secondary value.")
407 ;;; Accessors and Setters
409 ;; Provide four accessors: `org-element-type', `org-element-property'
410 ;; `org-element-contents' and `org-element-restriction'.
412 ;; Setter functions allow to modify elements by side effect. There is
413 ;; `org-element-put-property', `org-element-set-contents'. These
414 ;; low-level functions are useful to build a parse tree.
416 ;; `org-element-adopt-element', `org-element-set-element',
417 ;; `org-element-extract-element' and `org-element-insert-before' are
418 ;; high-level functions useful to modify a parse tree.
420 ;; `org-element-secondary-p' is a predicate used to know if a given
421 ;; object belongs to a secondary string.
423 (defsubst org-element-type
(element)
424 "Return type of ELEMENT.
426 The function returns the type of the element or object provided.
427 It can also return the following special value:
428 `plain-text' for a string
429 `org-data' for a complete document
430 nil in any other case."
432 ((not (consp element
)) (and (stringp element
) 'plain-text
))
433 ((symbolp (car element
)) (car element
))))
435 (defsubst org-element-property
(property element
)
436 "Extract the value from the PROPERTY of an ELEMENT."
437 (if (stringp element
) (get-text-property 0 property element
)
438 (plist-get (nth 1 element
) property
)))
440 (defsubst org-element-contents
(element)
441 "Extract contents from an ELEMENT."
442 (cond ((not (consp element
)) nil
)
443 ((symbolp (car element
)) (nthcdr 2 element
))
446 (defsubst org-element-restriction
(element)
447 "Return restriction associated to ELEMENT.
448 ELEMENT can be an element, an object or a symbol representing an
449 element or object type."
450 (cdr (assq (if (symbolp element
) element
(org-element-type element
))
451 org-element-object-restrictions
)))
453 (defsubst org-element-put-property
(element property value
)
454 "In ELEMENT set PROPERTY to VALUE.
455 Return modified element."
456 (if (stringp element
) (org-add-props element nil property value
)
457 (setcar (cdr element
) (plist-put (nth 1 element
) property value
))
460 (defsubst org-element-set-contents
(element &rest contents
)
461 "Set ELEMENT contents to CONTENTS.
462 Return modified element."
463 (cond ((not element
) (list contents
))
464 ((not (symbolp (car element
))) contents
)
465 ((cdr element
) (setcdr (cdr element
) contents
))
466 (t (nconc element contents
))))
468 (defun org-element-secondary-p (object)
469 "Non-nil when OBJECT belongs to a secondary string.
470 Return value is the property name, as a keyword, or nil."
471 (let* ((parent (org-element-property :parent object
))
472 (property (cdr (assq (org-element-type parent
)
473 org-element-secondary-value-alist
))))
475 (memq object
(org-element-property property parent
))
478 (defsubst org-element-adopt-elements
(parent &rest children
)
479 "Append elements to the contents of another element.
481 PARENT is an element or object. CHILDREN can be elements,
482 objects, or a strings.
484 The function takes care of setting `:parent' property for CHILD.
485 Return parent element."
486 ;; Link every child to PARENT. If PARENT is nil, it is a secondary
487 ;; string: parent is the list itself.
488 (mapc (lambda (child)
489 (org-element-put-property child
:parent
(or parent children
)))
491 ;; Add CHILDREN at the end of PARENT contents.
493 (apply 'org-element-set-contents
495 (nconc (org-element-contents parent
) children
)))
496 ;; Return modified PARENT element.
497 (or parent children
))
499 (defun org-element-extract-element (element)
500 "Extract ELEMENT from parse tree.
501 Remove element from the parse tree by side-effect, and return it
502 with its `:parent' property stripped out."
503 (let ((parent (org-element-property :parent element
))
504 (secondary (org-element-secondary-p element
)))
506 (org-element-put-property
508 (delq element
(org-element-property secondary parent
)))
509 (apply #'org-element-set-contents
511 (delq element
(org-element-contents parent
))))
512 ;; Return ELEMENT with its :parent removed.
513 (org-element-put-property element
:parent nil
)))
515 (defun org-element-insert-before (element location
)
516 "Insert ELEMENT before LOCATION in parse tree.
517 LOCATION is an element, object or string within the parse tree.
518 Parse tree is modified by side effect."
519 (let* ((parent (org-element-property :parent location
))
520 (property (org-element-secondary-p location
))
521 (siblings (if property
(org-element-property property parent
)
522 (org-element-contents parent
)))
523 ;; Special case: LOCATION is the first element of an
524 ;; independent secondary string (e.g. :title property). Add
526 (specialp (and (not property
)
528 (eq (car parent
) location
))))
529 ;; Install ELEMENT at the appropriate POSITION within SIBLINGS.
531 ((or (null siblings
) (eq (car siblings
) location
))
532 (push element siblings
))
533 ((null location
) (nconc siblings
(list element
)))
534 (t (let ((previous (cadr (memq location
(reverse siblings
)))))
536 (error "No location found to insert element")
537 (let ((next (memq previous siblings
)))
538 (setcdr next
(cons element
(cdr next
))))))))
539 ;; Store SIBLINGS at appropriate place in parse tree.
541 (specialp (setcdr parent
(copy-sequence parent
)) (setcar parent element
))
542 (property (org-element-put-property parent property siblings
))
543 (t (apply #'org-element-set-contents parent siblings
)))
544 ;; Set appropriate :parent property.
545 (org-element-put-property element
:parent parent
)))
547 (defun org-element-set-element (old new
)
548 "Replace element or object OLD with element or object NEW.
549 The function takes care of setting `:parent' property for NEW."
550 ;; Ensure OLD and NEW have the same parent.
551 (org-element-put-property new
:parent
(org-element-property :parent old
))
552 (if (or (memq (org-element-type old
) '(plain-text nil
))
553 (memq (org-element-type new
) '(plain-text nil
)))
554 ;; We cannot replace OLD with NEW since one of them is not an
555 ;; object or element. We take the long path.
556 (progn (org-element-insert-before new old
)
557 (org-element-extract-element old
))
558 ;; Since OLD is going to be changed into NEW by side-effect, first
559 ;; make sure that every element or object within NEW has OLD as
561 (dolist (blob (org-element-contents new
))
562 (org-element-put-property blob
:parent old
))
563 ;; Transfer contents.
564 (apply #'org-element-set-contents old
(org-element-contents new
))
565 ;; Overwrite OLD's properties with NEW's.
566 (setcar (cdr old
) (nth 1 new
))
568 (setcar old
(car new
))))
574 ;; For each greater element type, we define a parser and an
577 ;; A parser returns the element or object as the list described above.
578 ;; Most of them accepts no argument. Though, exceptions exist. Hence
579 ;; every element containing a secondary string (see
580 ;; `org-element-secondary-value-alist') will accept an optional
581 ;; argument to toggle parsing of that secondary string. Moreover,
582 ;; `item' parser requires current list's structure as its first
585 ;; An interpreter accepts two arguments: the list representation of
586 ;; the element or object, and its contents. The latter may be nil,
587 ;; depending on the element or object considered. It returns the
588 ;; appropriate Org syntax, as a string.
590 ;; Parsing functions must follow the naming convention:
591 ;; org-element-TYPE-parser, where TYPE is greater element's type, as
592 ;; defined in `org-element-greater-elements'.
594 ;; Similarly, interpreting functions must follow the naming
595 ;; convention: org-element-TYPE-interpreter.
597 ;; With the exception of `headline' and `item' types, greater elements
598 ;; cannot contain other greater elements of their own type.
600 ;; Beside implementing a parser and an interpreter, adding a new
601 ;; greater element requires to tweak `org-element--current-element'.
602 ;; Moreover, the newly defined type must be added to both
603 ;; `org-element-all-elements' and `org-element-greater-elements'.
608 (defun org-element-center-block-parser (limit affiliated
)
609 "Parse a center block.
611 LIMIT bounds the search. AFFILIATED is a list of which CAR is
612 the buffer position at the beginning of the first affiliated
613 keyword and CDR is a plist of affiliated keywords along with
616 Return a list whose CAR is `center-block' and CDR is a plist
617 containing `:begin', `:end', `:contents-begin', `:contents-end',
618 `:post-blank' and `:post-affiliated' keywords.
620 Assume point is at the beginning of the block."
621 (let ((case-fold-search t
))
622 (if (not (save-excursion
623 (re-search-forward "^[ \t]*#\\+END_CENTER[ \t]*$" limit t
)))
624 ;; Incomplete block: parse it as a paragraph.
625 (org-element-paragraph-parser limit affiliated
)
626 (let ((block-end-line (match-beginning 0)))
627 (let* ((begin (car affiliated
))
628 (post-affiliated (point))
629 ;; Empty blocks have no contents.
630 (contents-begin (progn (forward-line)
631 (and (< (point) block-end-line
)
633 (contents-end (and contents-begin block-end-line
))
634 (pos-before-blank (progn (goto-char block-end-line
)
638 (skip-chars-forward " \r\t\n" limit
)
639 (if (eobp) (point) (line-beginning-position)))))
644 :contents-begin contents-begin
645 :contents-end contents-end
646 :post-blank
(count-lines pos-before-blank end
)
647 :post-affiliated post-affiliated
)
648 (cdr affiliated
))))))))
650 (defun org-element-center-block-interpreter (center-block contents
)
651 "Interpret CENTER-BLOCK element as Org syntax.
652 CONTENTS is the contents of the element."
653 (format "#+BEGIN_CENTER\n%s#+END_CENTER" contents
))
658 (defun org-element-drawer-parser (limit affiliated
)
661 LIMIT bounds the search. AFFILIATED is a list of which CAR is
662 the buffer position at the beginning of the first affiliated
663 keyword and CDR is a plist of affiliated keywords along with
666 Return a list whose CAR is `drawer' and CDR is a plist containing
667 `:drawer-name', `:begin', `:end', `:contents-begin',
668 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
670 Assume point is at beginning of drawer."
671 (let ((case-fold-search t
))
672 (if (not (save-excursion (re-search-forward "^[ \t]*:END:[ \t]*$" limit t
)))
673 ;; Incomplete drawer: parse it as a paragraph.
674 (org-element-paragraph-parser limit affiliated
)
676 (let* ((drawer-end-line (match-beginning 0))
677 (name (progn (looking-at org-drawer-regexp
)
678 (org-match-string-no-properties 1)))
679 (begin (car affiliated
))
680 (post-affiliated (point))
681 ;; Empty drawers have no contents.
682 (contents-begin (progn (forward-line)
683 (and (< (point) drawer-end-line
)
685 (contents-end (and contents-begin drawer-end-line
))
686 (pos-before-blank (progn (goto-char drawer-end-line
)
689 (end (progn (skip-chars-forward " \r\t\n" limit
)
690 (if (eobp) (point) (line-beginning-position)))))
696 :contents-begin contents-begin
697 :contents-end contents-end
698 :post-blank
(count-lines pos-before-blank end
)
699 :post-affiliated post-affiliated
)
700 (cdr affiliated
))))))))
702 (defun org-element-drawer-interpreter (drawer contents
)
703 "Interpret DRAWER element as Org syntax.
704 CONTENTS is the contents of the element."
705 (format ":%s:\n%s:END:"
706 (org-element-property :drawer-name drawer
)
712 (defun org-element-dynamic-block-parser (limit affiliated
)
713 "Parse a dynamic block.
715 LIMIT bounds the search. AFFILIATED is a list of which CAR is
716 the buffer position at the beginning of the first affiliated
717 keyword and CDR is a plist of affiliated keywords along with
720 Return a list whose CAR is `dynamic-block' and CDR is a plist
721 containing `:block-name', `:begin', `:end', `:contents-begin',
722 `:contents-end', `:arguments', `:post-blank' and
723 `:post-affiliated' keywords.
725 Assume point is at beginning of dynamic block."
726 (let ((case-fold-search t
))
727 (if (not (save-excursion
728 (re-search-forward "^[ \t]*#\\+END:?[ \t]*$" limit t
)))
729 ;; Incomplete block: parse it as a paragraph.
730 (org-element-paragraph-parser limit affiliated
)
731 (let ((block-end-line (match-beginning 0)))
733 (let* ((name (progn (looking-at org-dblock-start-re
)
734 (org-match-string-no-properties 1)))
735 (arguments (org-match-string-no-properties 3))
736 (begin (car affiliated
))
737 (post-affiliated (point))
738 ;; Empty blocks have no contents.
739 (contents-begin (progn (forward-line)
740 (and (< (point) block-end-line
)
742 (contents-end (and contents-begin block-end-line
))
743 (pos-before-blank (progn (goto-char block-end-line
)
746 (end (progn (skip-chars-forward " \r\t\n" limit
)
747 (if (eobp) (point) (line-beginning-position)))))
754 :contents-begin contents-begin
755 :contents-end contents-end
756 :post-blank
(count-lines pos-before-blank end
)
757 :post-affiliated post-affiliated
)
758 (cdr affiliated
)))))))))
760 (defun org-element-dynamic-block-interpreter (dynamic-block contents
)
761 "Interpret DYNAMIC-BLOCK element as Org syntax.
762 CONTENTS is the contents of the element."
763 (format "#+BEGIN: %s%s\n%s#+END:"
764 (org-element-property :block-name dynamic-block
)
765 (let ((args (org-element-property :arguments dynamic-block
)))
766 (and args
(concat " " args
)))
770 ;;;; Footnote Definition
772 (defun org-element-footnote-definition-parser (limit affiliated
)
773 "Parse a footnote definition.
775 LIMIT bounds the search. AFFILIATED is a list of which CAR is
776 the buffer position at the beginning of the first affiliated
777 keyword and CDR is a plist of affiliated keywords along with
780 Return a list whose CAR is `footnote-definition' and CDR is
781 a plist containing `:label', `:begin' `:end', `:contents-begin',
782 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
784 Assume point is at the beginning of the footnote definition."
786 (let* ((label (progn (looking-at org-footnote-definition-re
)
787 (org-match-string-no-properties 1)))
788 (begin (car affiliated
))
789 (post-affiliated (point))
790 (ending (save-excursion
794 (concat org-outline-regexp-bol
"\\|"
795 org-footnote-definition-re
"\\|"
796 "^\\([ \t]*\n\\)\\{2,\\}") limit
'move
))
799 (contents-begin (progn
801 (skip-chars-forward " \r\t\n" ending
)
802 (cond ((= (point) ending
) nil
)
803 ((= (line-beginning-position) begin
) (point))
804 (t (line-beginning-position)))))
805 (contents-end (and contents-begin ending
))
806 (end (progn (goto-char ending
)
807 (skip-chars-forward " \r\t\n" limit
)
808 (if (eobp) (point) (line-beginning-position)))))
809 (list 'footnote-definition
814 :contents-begin contents-begin
815 :contents-end contents-end
816 :post-blank
(count-lines ending end
)
817 :post-affiliated post-affiliated
)
818 (cdr affiliated
))))))
820 (defun org-element-footnote-definition-interpreter (footnote-definition contents
)
821 "Interpret FOOTNOTE-DEFINITION element as Org syntax.
822 CONTENTS is the contents of the footnote-definition."
823 (concat (format "[%s]" (org-element-property :label footnote-definition
))
830 (defun org-element--get-node-properties ()
831 "Return node properties associated to headline at point.
832 Upcase property names. It avoids confusion between properties
833 obtained through property drawer and default properties from the
834 parser (e.g. `:end' and :END:). Return value is a plist."
837 (when (org-looking-at-p org-planning-line-re
) (forward-line))
838 (when (looking-at org-property-drawer-re
)
840 (let ((end (match-end 0)) properties
)
841 (while (< (line-end-position) end
)
842 (looking-at org-property-re
)
843 (push (org-match-string-no-properties 3) properties
)
844 (push (intern (concat ":" (upcase (match-string 2)))) properties
)
848 (defun org-element--get-time-properties ()
849 "Return time properties associated to headline at point.
850 Return value is a plist."
852 (when (progn (forward-line) (looking-at org-planning-line-re
))
853 (let ((end (line-end-position)) plist
)
854 (while (re-search-forward org-keyword-time-not-clock-regexp end t
)
855 (goto-char (match-end 1))
856 (skip-chars-forward " \t")
857 (let ((keyword (match-string 1))
858 (time (org-element-timestamp-parser)))
859 (cond ((equal keyword org-scheduled-string
)
860 (setq plist
(plist-put plist
:scheduled time
)))
861 ((equal keyword org-deadline-string
)
862 (setq plist
(plist-put plist
:deadline time
)))
863 (t (setq plist
(plist-put plist
:closed time
))))))
866 (defun org-element-headline-parser (limit &optional raw-secondary-p
)
869 Return a list whose CAR is `headline' and CDR is a plist
870 containing `:raw-value', `:title', `:alt-title', `:begin',
871 `:end', `:pre-blank', `:contents-begin' and `:contents-end',
872 `:level', `:priority', `:tags', `:todo-keyword',`:todo-type',
873 `:scheduled', `:deadline', `:closed', `:archivedp', `:commentedp'
874 `:footnote-section-p', `:post-blank' and `:post-affiliated'
877 The plist also contains any property set in the property drawer,
878 with its name in upper cases and colons added at the
879 beginning (e.g., `:CUSTOM_ID').
881 LIMIT is a buffer position bounding the search.
883 When RAW-SECONDARY-P is non-nil, headline's title will not be
884 parsed as a secondary string, but as a plain string instead.
886 Assume point is at beginning of the headline."
888 (let* ((begin (point))
889 (level (prog1 (org-reduced-level (skip-chars-forward "*"))
890 (skip-chars-forward " \t")))
891 (todo (and org-todo-regexp
892 (let (case-fold-search) (looking-at org-todo-regexp
))
893 (progn (goto-char (match-end 0))
894 (skip-chars-forward " \t")
897 (and todo
(if (member todo org-done-keywords
) 'done
'todo
)))
898 (priority (and (looking-at "\\[#.\\][ \t]*")
899 (progn (goto-char (match-end 0))
900 (aref (match-string 0) 2))))
902 (and (let (case-fold-search) (looking-at org-comment-string
))
903 (goto-char (match-end 0))))
904 (title-start (point))
905 (tags (when (re-search-forward
906 (org-re "[ \t]+\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
909 (goto-char (match-beginning 0))
910 (org-split-string (match-string 1) ":")))
913 (buffer-substring-no-properties title-start title-end
)))
914 (archivedp (member org-archive-tag tags
))
915 (footnote-section-p (and org-footnote-section
916 (string= org-footnote-section raw-value
)))
917 (standard-props (org-element--get-node-properties))
918 (time-props (org-element--get-time-properties))
919 (end (min (save-excursion (org-end-of-subtree t t
)) limit
))
920 (pos-after-head (progn (forward-line) (point)))
921 (contents-begin (save-excursion
922 (skip-chars-forward " \r\t\n" end
)
923 (and (/= (point) end
) (line-beginning-position))))
924 (contents-end (and contents-begin
925 (progn (goto-char end
)
926 (skip-chars-backward " \r\t\n")
929 ;; Clean TAGS from archive tag, if any.
930 (when archivedp
(setq tags
(delete org-archive-tag tags
)))
934 (list :raw-value raw-value
938 (if (not contents-begin
) 0
939 (count-lines pos-after-head contents-begin
))
940 :contents-begin contents-begin
941 :contents-end contents-end
947 :post-blank
(count-lines
948 (or contents-end pos-after-head
)
950 :footnote-section-p footnote-section-p
952 :commentedp commentedp
953 :post-affiliated begin
)
956 (let ((alt-title (org-element-property :ALT_TITLE headline
)))
958 (org-element-put-property
960 (if raw-secondary-p alt-title
961 (org-element-parse-secondary-string
962 alt-title
(org-element-restriction 'headline
) headline
)))))
963 (org-element-put-property
965 (if raw-secondary-p raw-value
966 (let ((title (org-element--parse-objects
967 (progn (goto-char title-start
)
968 (skip-chars-forward " \t")
970 (progn (goto-char title-end
)
971 (skip-chars-backward " \t")
974 (org-element-restriction 'headline
))))
975 (dolist (datum title title
)
976 (org-element-put-property datum
:parent headline
)))))))))
978 (defun org-element-headline-interpreter (headline contents
)
979 "Interpret HEADLINE element as Org syntax.
980 CONTENTS is the contents of the element."
981 (let* ((level (org-element-property :level headline
))
982 (todo (org-element-property :todo-keyword headline
))
983 (priority (org-element-property :priority headline
))
984 (title (org-element-interpret-data
985 (org-element-property :title headline
)))
986 (tags (let ((tag-list (if (org-element-property :archivedp headline
)
987 (cons org-archive-tag
988 (org-element-property :tags headline
))
989 (org-element-property :tags headline
))))
991 (format ":%s:" (mapconcat #'identity tag-list
":")))))
992 (commentedp (org-element-property :commentedp headline
))
993 (pre-blank (or (org-element-property :pre-blank headline
) 0))
995 (concat (make-string (if org-odd-levels-only
(1- (* level
2)) level
)
997 (and todo
(concat " " todo
))
998 (and commentedp
(concat " " org-comment-string
))
999 (and priority
(format " [#%c]" priority
))
1001 (if (and org-footnote-section
1002 (org-element-property :footnote-section-p headline
))
1003 org-footnote-section
1010 ((zerop org-tags-column
) (format " %s" tags
))
1011 ((< org-tags-column
0)
1014 (max (- (+ org-tags-column
(length heading
) (length tags
))) 1)
1019 (make-string (max (- org-tags-column
(length heading
)) 1) ?\s
)
1021 (make-string (1+ pre-blank
) ?
\n)
1027 (defun org-element-inlinetask-parser (limit &optional raw-secondary-p
)
1028 "Parse an inline task.
1030 Return a list whose CAR is `inlinetask' and CDR is a plist
1031 containing `:title', `:begin', `:end', `:contents-begin' and
1032 `:contents-end', `:level', `:priority', `:raw-value', `:tags',
1033 `:todo-keyword', `:todo-type', `:scheduled', `:deadline',
1034 `:closed', `:post-blank' and `:post-affiliated' keywords.
1036 The plist also contains any property set in the property drawer,
1037 with its name in upper cases and colons added at the
1038 beginning (e.g., `:CUSTOM_ID').
1040 When optional argument RAW-SECONDARY-P is non-nil, inline-task's
1041 title will not be parsed as a secondary string, but as a plain
1044 Assume point is at beginning of the inline task."
1046 (let* ((begin (point))
1047 (level (prog1 (org-reduced-level (skip-chars-forward "*"))
1048 (skip-chars-forward " \t")))
1049 (todo (and org-todo-regexp
1050 (let (case-fold-search) (looking-at org-todo-regexp
))
1051 (progn (goto-char (match-end 0))
1052 (skip-chars-forward " \t")
1054 (todo-type (and todo
1055 (if (member todo org-done-keywords
) 'done
'todo
)))
1056 (priority (and (looking-at "\\[#.\\][ \t]*")
1057 (progn (goto-char (match-end 0))
1058 (aref (match-string 0) 2))))
1059 (title-start (point))
1060 (tags (when (re-search-forward
1061 (org-re "[ \t]+\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
1064 (goto-char (match-beginning 0))
1065 (org-split-string (match-string 1) ":")))
1067 (raw-value (org-trim
1068 (buffer-substring-no-properties title-start title-end
)))
1069 (task-end (save-excursion
1071 (and (re-search-forward org-outline-regexp-bol limit t
)
1072 (org-looking-at-p "END[ \t]*$")
1073 (line-beginning-position))))
1074 (standard-props (and task-end
(org-element--get-node-properties)))
1075 (time-props (and task-end
(org-element--get-time-properties)))
1076 (contents-begin (progn (forward-line)
1077 (and task-end
(< (point) task-end
) (point))))
1078 (contents-end (and contents-begin task-end
))
1079 (before-blank (if (not task-end
) (point)
1080 (goto-char task-end
)
1083 (end (progn (skip-chars-forward " \r\t\n" limit
)
1084 (if (eobp) (point) (line-beginning-position))))
1088 (list :raw-value raw-value
1091 :contents-begin contents-begin
1092 :contents-end contents-end
1097 :todo-type todo-type
1098 :post-blank
(count-lines before-blank end
)
1099 :post-affiliated begin
)
1102 (org-element-put-property
1104 (if raw-secondary-p raw-value
1105 (let ((title (org-element--parse-objects
1106 (progn (goto-char title-start
)
1107 (skip-chars-forward " \t")
1109 (progn (goto-char title-end
)
1110 (skip-chars-backward " \t")
1113 (org-element-restriction 'inlinetask
))))
1114 (dolist (datum title title
)
1115 (org-element-put-property datum
:parent inlinetask
))))))))
1117 (defun org-element-inlinetask-interpreter (inlinetask contents
)
1118 "Interpret INLINETASK element as Org syntax.
1119 CONTENTS is the contents of inlinetask."
1120 (let* ((level (org-element-property :level inlinetask
))
1121 (todo (org-element-property :todo-keyword inlinetask
))
1122 (priority (org-element-property :priority inlinetask
))
1123 (title (org-element-interpret-data
1124 (org-element-property :title inlinetask
)))
1125 (tags (let ((tag-list (org-element-property :tags inlinetask
)))
1127 (format ":%s:" (mapconcat 'identity tag-list
":")))))
1128 (task (concat (make-string level ?
*)
1129 (and todo
(concat " " todo
))
1130 (and priority
(format " [#%c]" priority
))
1131 (and title
(concat " " title
)))))
1136 ((zerop org-tags-column
) (format " %s" tags
))
1137 ((< org-tags-column
0)
1140 (max (- (+ org-tags-column
(length task
) (length tags
))) 1)
1145 (make-string (max (- org-tags-column
(length task
)) 1) ?
)
1147 ;; Prefer degenerate inlinetasks when there are no
1152 (make-string level ?
*) " END")))))
1157 (defun org-element-item-parser (limit struct
&optional raw-secondary-p
)
1160 STRUCT is the structure of the plain list.
1162 Return a list whose CAR is `item' and CDR is a plist containing
1163 `:bullet', `:begin', `:end', `:contents-begin', `:contents-end',
1164 `:checkbox', `:counter', `:tag', `:structure', `:post-blank' and
1165 `:post-affiliated' keywords.
1167 When optional argument RAW-SECONDARY-P is non-nil, item's tag, if
1168 any, will not be parsed as a secondary string, but as a plain
1171 Assume point is at the beginning of the item."
1174 (looking-at org-list-full-item-re
)
1175 (let* ((begin (point))
1176 (bullet (org-match-string-no-properties 1))
1177 (checkbox (let ((box (org-match-string-no-properties 3)))
1178 (cond ((equal "[ ]" box
) 'off
)
1179 ((equal "[X]" box
) 'on
)
1180 ((equal "[-]" box
) 'trans
))))
1181 (counter (let ((c (org-match-string-no-properties 2)))
1185 ((string-match "[A-Za-z]" c
)
1186 (- (string-to-char (upcase (match-string 0 c
)))
1188 ((string-match "[0-9]+" c
)
1189 (string-to-number (match-string 0 c
)))))))
1190 (end (progn (goto-char (nth 6 (assq (point) struct
)))
1191 (unless (bolp) (forward-line))
1195 ;; Ignore tags in un-ordered lists: they are just
1196 ;; a part of item's body.
1197 (if (and (match-beginning 4)
1198 (save-match-data (string-match "[.)]" bullet
)))
1201 (skip-chars-forward " \r\t\n" limit
)
1202 ;; If first line isn't empty, contents really start
1203 ;; at the text after item's meta-data.
1204 (if (= (point-at-bol) begin
) (point) (point-at-bol))))
1205 (contents-end (progn (goto-char end
)
1206 (skip-chars-backward " \r\t\n")
1211 (list :bullet bullet
1214 ;; CONTENTS-BEGIN and CONTENTS-END may be
1215 ;; mixed up in the case of an empty item
1216 ;; separated from the next by a blank line.
1217 ;; Thus ensure the former is always the
1219 :contents-begin
(min contents-begin contents-end
)
1220 :contents-end
(max contents-begin contents-end
)
1224 :post-blank
(count-lines contents-end end
)
1225 :post-affiliated begin
))))
1226 (org-element-put-property
1228 (let ((raw (org-list-get-tag begin struct
)))
1230 (if raw-secondary-p raw
1231 (let ((tag (org-element--parse-objects
1232 (match-beginning 4) (match-end 4) nil
1233 (org-element-restriction 'item
))))
1234 (dolist (datum tag tag
)
1235 (org-element-put-property datum
:parent item
))))))))))
1237 (defun org-element-item-interpreter (item contents
)
1238 "Interpret ITEM element as Org syntax.
1239 CONTENTS is the contents of the element."
1240 (let* ((bullet (let ((bullet (org-element-property :bullet item
)))
1241 (org-list-bullet-string
1242 (cond ((not (string-match "[0-9a-zA-Z]" bullet
)) "- ")
1243 ((eq org-plain-list-ordered-item-terminator ?\
)) "1)")
1245 (checkbox (org-element-property :checkbox item
))
1246 (counter (org-element-property :counter item
))
1247 (tag (let ((tag (org-element-property :tag item
)))
1248 (and tag
(org-element-interpret-data tag
))))
1249 ;; Compute indentation.
1250 (ind (make-string (length bullet
) 32))
1251 (item-starts-with-par-p
1252 (eq (org-element-type (car (org-element-contents item
)))
1257 (and counter
(format "[@%d] " counter
))
1262 (and tag
(format "%s :: " tag
))
1264 (let ((contents (replace-regexp-in-string
1265 "\\(^\\)[ \t]*\\S-" ind contents nil nil
1)))
1266 (if item-starts-with-par-p
(org-trim contents
)
1267 (concat "\n" contents
)))))))
1272 (defun org-element--list-struct (limit)
1273 ;; Return structure of list at point. Internal function. See
1274 ;; `org-list-struct' for details.
1275 (let ((case-fold-search t
)
1277 (item-re (org-item-re))
1278 (inlinetask-re (and (featurep 'org-inlinetask
) "^\\*+ "))
1284 ;; At limit: end all items.
1287 (let ((end (progn (skip-chars-backward " \r\t\n")
1290 (dolist (item items
(sort (nconc items struct
)
1291 'car-less-than-car
))
1292 (setcar (nthcdr 6 item
) end
)))))
1293 ;; At list end: end all items.
1294 ((looking-at org-list-end-re
)
1295 (throw 'exit
(dolist (item items
(sort (nconc items struct
)
1296 'car-less-than-car
))
1297 (setcar (nthcdr 6 item
) (point)))))
1298 ;; At a new item: end previous sibling.
1299 ((looking-at item-re
)
1300 (let ((ind (save-excursion (skip-chars-forward " \t")
1302 (setq top-ind
(min top-ind ind
))
1303 (while (and items
(<= ind
(nth 1 (car items
))))
1304 (let ((item (pop items
)))
1305 (setcar (nthcdr 6 item
) (point))
1306 (push item struct
)))
1307 (push (progn (looking-at org-list-full-item-re
)
1308 (let ((bullet (match-string-no-properties 1)))
1312 (match-string-no-properties 2) ; counter
1313 (match-string-no-properties 3) ; checkbox
1315 (and (save-match-data
1316 (string-match "[-+*]" bullet
))
1317 (match-string-no-properties 4))
1318 ;; Ending position, unknown so far.
1322 ;; Skip empty lines.
1323 ((looking-at "^[ \t]*$") (forward-line))
1324 ;; Skip inline tasks and blank lines along the way.
1325 ((and inlinetask-re
(looking-at inlinetask-re
))
1327 (let ((origin (point)))
1328 (when (re-search-forward inlinetask-re limit t
)
1329 (if (org-looking-at-p "END[ \t]*$") (forward-line)
1330 (goto-char origin
)))))
1331 ;; At some text line. Check if it ends any previous item.
1333 (let ((ind (progn (skip-chars-forward " \t") (current-column))))
1334 (when (<= ind top-ind
)
1335 (skip-chars-backward " \r\t\n")
1337 (while (<= ind
(nth 1 (car items
)))
1338 (let ((item (pop items
)))
1339 (setcar (nthcdr 6 item
) (line-beginning-position))
1342 (throw 'exit
(sort struct
'car-less-than-car
))))))
1343 ;; Skip blocks (any type) and drawers contents.
1345 ((and (looking-at "#\\+BEGIN\\(:\\|_\\S-+\\)")
1347 (format "^[ \t]*#\\+END%s[ \t]*$" (match-string 1))
1349 ((and (looking-at org-drawer-regexp
)
1350 (re-search-forward "^[ \t]*:END:[ \t]*$" limit t
))))
1351 (forward-line))))))))
1353 (defun org-element-plain-list-parser (limit affiliated structure
)
1354 "Parse a plain list.
1356 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1357 the buffer position at the beginning of the first affiliated
1358 keyword and CDR is a plist of affiliated keywords along with
1359 their value. STRUCTURE is the structure of the plain list being
1362 Return a list whose CAR is `plain-list' and CDR is a plist
1363 containing `:type', `:begin', `:end', `:contents-begin' and
1364 `:contents-end', `:structure', `:post-blank' and
1365 `:post-affiliated' keywords.
1367 Assume point is at the beginning of the list."
1369 (let* ((struct (or structure
(org-element--list-struct limit
)))
1370 (type (cond ((org-looking-at-p "[ \t]*[A-Za-z0-9]") 'ordered
)
1371 ((nth 5 (assq (point) struct
)) 'descriptive
)
1373 (contents-begin (point))
1374 (begin (car affiliated
))
1375 (contents-end (let* ((item (assq contents-begin struct
))
1378 (while (and (setq item
(assq pos struct
))
1379 (= (nth 1 item
) ind
))
1380 (setq pos
(nth 6 item
)))
1382 (end (progn (goto-char contents-end
)
1383 (skip-chars-forward " \r\t\n" limit
)
1384 (if (= (point) limit
) limit
(line-beginning-position)))))
1391 :contents-begin contents-begin
1392 :contents-end contents-end
1394 :post-blank
(count-lines contents-end end
)
1395 :post-affiliated contents-begin
)
1396 (cdr affiliated
))))))
1398 (defun org-element-plain-list-interpreter (plain-list contents
)
1399 "Interpret PLAIN-LIST element as Org syntax.
1400 CONTENTS is the contents of the element."
1403 (goto-char (point-min))
1408 ;;;; Property Drawer
1410 (defun org-element-property-drawer-parser (limit)
1411 "Parse a property drawer.
1413 LIMIT bounds the search.
1415 Return a list whose car is `property-drawer' and cdr is a plist
1416 containing `:begin', `:end', `:contents-begin', `:contents-end',
1417 `:post-blank' and `:post-affiliated' keywords.
1419 Assume point is at the beginning of the property drawer."
1421 (let ((case-fold-search t
)
1423 (contents-begin (line-beginning-position 2)))
1424 (re-search-forward "^[ \t]*:END:[ \t]*$" limit t
)
1425 (let ((contents-end (and (> (match-beginning 0) contents-begin
)
1426 (match-beginning 0)))
1427 (before-blank (progn (forward-line) (point)))
1428 (end (progn (skip-chars-forward " \r\t\n" limit
)
1429 (if (eobp) (point) (line-beginning-position)))))
1430 (list 'property-drawer
1433 :contents-begin
(and contents-end contents-begin
)
1434 :contents-end contents-end
1435 :post-blank
(count-lines before-blank end
)
1436 :post-affiliated begin
))))))
1438 (defun org-element-property-drawer-interpreter (property-drawer contents
)
1439 "Interpret PROPERTY-DRAWER element as Org syntax.
1440 CONTENTS is the properties within the drawer."
1441 (format ":PROPERTIES:\n%s:END:" contents
))
1446 (defun org-element-quote-block-parser (limit affiliated
)
1447 "Parse a quote block.
1449 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1450 the buffer position at the beginning of the first affiliated
1451 keyword and CDR is a plist of affiliated keywords along with
1454 Return a list whose CAR is `quote-block' and CDR is a plist
1455 containing `:begin', `:end', `:contents-begin', `:contents-end',
1456 `:post-blank' and `:post-affiliated' keywords.
1458 Assume point is at the beginning of the block."
1459 (let ((case-fold-search t
))
1460 (if (not (save-excursion
1461 (re-search-forward "^[ \t]*#\\+END_QUOTE[ \t]*$" limit t
)))
1462 ;; Incomplete block: parse it as a paragraph.
1463 (org-element-paragraph-parser limit affiliated
)
1464 (let ((block-end-line (match-beginning 0)))
1466 (let* ((begin (car affiliated
))
1467 (post-affiliated (point))
1468 ;; Empty blocks have no contents.
1469 (contents-begin (progn (forward-line)
1470 (and (< (point) block-end-line
)
1472 (contents-end (and contents-begin block-end-line
))
1473 (pos-before-blank (progn (goto-char block-end-line
)
1476 (end (progn (skip-chars-forward " \r\t\n" limit
)
1477 (if (eobp) (point) (line-beginning-position)))))
1482 :contents-begin contents-begin
1483 :contents-end contents-end
1484 :post-blank
(count-lines pos-before-blank end
)
1485 :post-affiliated post-affiliated
)
1486 (cdr affiliated
)))))))))
1488 (defun org-element-quote-block-interpreter (quote-block contents
)
1489 "Interpret QUOTE-BLOCK element as Org syntax.
1490 CONTENTS is the contents of the element."
1491 (format "#+BEGIN_QUOTE\n%s#+END_QUOTE" contents
))
1496 (defun org-element-section-parser (limit)
1499 LIMIT bounds the search.
1501 Return a list whose CAR is `section' and CDR is a plist
1502 containing `:begin', `:end', `:contents-begin', `contents-end',
1503 `:post-blank' and `:post-affiliated' keywords."
1505 ;; Beginning of section is the beginning of the first non-blank
1506 ;; line after previous headline.
1507 (let ((begin (point))
1508 (end (progn (org-with-limited-levels (outline-next-heading))
1510 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
1516 :contents-begin begin
1517 :contents-end pos-before-blank
1518 :post-blank
(count-lines pos-before-blank end
)
1519 :post-affiliated begin
)))))
1521 (defun org-element-section-interpreter (section contents
)
1522 "Interpret SECTION element as Org syntax.
1523 CONTENTS is the contents of the element."
1529 (defun org-element-special-block-parser (limit affiliated
)
1530 "Parse a special block.
1532 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1533 the buffer position at the beginning of the first affiliated
1534 keyword and CDR is a plist of affiliated keywords along with
1537 Return a list whose CAR is `special-block' and CDR is a plist
1538 containing `:type', `:begin', `:end', `:contents-begin',
1539 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
1541 Assume point is at the beginning of the block."
1542 (let* ((case-fold-search t
)
1543 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
1544 (match-string-no-properties 1))))
1545 (if (not (save-excursion
1547 (format "^[ \t]*#\\+END_%s[ \t]*$" (regexp-quote type
))
1549 ;; Incomplete block: parse it as a paragraph.
1550 (org-element-paragraph-parser limit affiliated
)
1551 (let ((block-end-line (match-beginning 0)))
1553 (let* ((begin (car affiliated
))
1554 (post-affiliated (point))
1555 ;; Empty blocks have no contents.
1556 (contents-begin (progn (forward-line)
1557 (and (< (point) block-end-line
)
1559 (contents-end (and contents-begin block-end-line
))
1560 (pos-before-blank (progn (goto-char block-end-line
)
1563 (end (progn (skip-chars-forward " \r\t\n" limit
)
1564 (if (eobp) (point) (line-beginning-position)))))
1565 (list 'special-block
1570 :contents-begin contents-begin
1571 :contents-end contents-end
1572 :post-blank
(count-lines pos-before-blank end
)
1573 :post-affiliated post-affiliated
)
1574 (cdr affiliated
)))))))))
1576 (defun org-element-special-block-interpreter (special-block contents
)
1577 "Interpret SPECIAL-BLOCK element as Org syntax.
1578 CONTENTS is the contents of the element."
1579 (let ((block-type (org-element-property :type special-block
)))
1580 (format "#+BEGIN_%s\n%s#+END_%s" block-type contents block-type
)))
1586 ;; For each element, a parser and an interpreter are also defined.
1587 ;; Both follow the same naming convention used for greater elements.
1589 ;; Also, as for greater elements, adding a new element type is done
1590 ;; through the following steps: implement a parser and an interpreter,
1591 ;; tweak `org-element--current-element' so that it recognizes the new
1592 ;; type and add that new type to `org-element-all-elements'.
1594 ;; As a special case, when the newly defined type is a block type,
1595 ;; `org-element-block-name-alist' has to be modified accordingly.
1600 (defun org-element-babel-call-parser (limit affiliated
)
1601 "Parse a babel call.
1603 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1604 the buffer position at the beginning of the first affiliated
1605 keyword and CDR is a plist of affiliated keywords along with
1608 Return a list whose CAR is `babel-call' and CDR is a plist
1609 containing `:begin', `:end', `:value', `:post-blank' and
1610 `:post-affiliated' as keywords."
1612 (let ((begin (car affiliated
))
1613 (post-affiliated (point))
1614 (value (progn (let ((case-fold-search t
))
1615 (re-search-forward "call:[ \t]*" nil t
))
1616 (buffer-substring-no-properties (point)
1617 (line-end-position))))
1618 (pos-before-blank (progn (forward-line) (point)))
1619 (end (progn (skip-chars-forward " \r\t\n" limit
)
1620 (if (eobp) (point) (line-beginning-position)))))
1626 :post-blank
(count-lines pos-before-blank end
)
1627 :post-affiliated post-affiliated
)
1628 (cdr affiliated
))))))
1630 (defun org-element-babel-call-interpreter (babel-call contents
)
1631 "Interpret BABEL-CALL element as Org syntax.
1633 (concat "#+CALL: " (org-element-property :value babel-call
)))
1638 (defun org-element-clock-parser (limit)
1641 LIMIT bounds the search.
1643 Return a list whose CAR is `clock' and CDR is a plist containing
1644 `:status', `:value', `:time', `:begin', `:end', `:post-blank' and
1645 `:post-affiliated' as keywords."
1647 (let* ((case-fold-search nil
)
1649 (value (progn (search-forward org-clock-string
(line-end-position) t
)
1650 (skip-chars-forward " \t")
1651 (org-element-timestamp-parser)))
1652 (duration (and (search-forward " => " (line-end-position) t
)
1653 (progn (skip-chars-forward " \t")
1654 (looking-at "\\(\\S-+\\)[ \t]*$"))
1655 (org-match-string-no-properties 1)))
1656 (status (if duration
'closed
'running
))
1657 (post-blank (let ((before-blank (progn (forward-line) (point))))
1658 (skip-chars-forward " \r\t\n" limit
)
1659 (skip-chars-backward " \t")
1660 (unless (bolp) (end-of-line))
1661 (count-lines before-blank
(point))))
1664 (list :status status
1669 :post-blank post-blank
1670 :post-affiliated begin
)))))
1672 (defun org-element-clock-interpreter (clock contents
)
1673 "Interpret CLOCK element as Org syntax.
1675 (concat org-clock-string
" "
1676 (org-element-timestamp-interpreter
1677 (org-element-property :value clock
) nil
)
1678 (let ((duration (org-element-property :duration clock
)))
1683 (org-split-string duration
":")))))))
1688 (defun org-element-comment-parser (limit affiliated
)
1691 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1692 the buffer position at the beginning of the first affiliated
1693 keyword and CDR is a plist of affiliated keywords along with
1696 Return a list whose CAR is `comment' and CDR is a plist
1697 containing `:begin', `:end', `:value', `:post-blank',
1698 `:post-affiliated' keywords.
1700 Assume point is at comment beginning."
1702 (let* ((begin (car affiliated
))
1703 (post-affiliated (point))
1704 (value (prog2 (looking-at "[ \t]*# ?")
1705 (buffer-substring-no-properties
1706 (match-end 0) (line-end-position))
1709 ;; Get comments ending.
1711 (while (and (< (point) limit
) (looking-at "[ \t]*#\\( \\|$\\)"))
1712 ;; Accumulate lines without leading hash and first
1717 (buffer-substring-no-properties
1718 (match-end 0) (line-end-position))))
1721 (end (progn (goto-char com-end
)
1722 (skip-chars-forward " \r\t\n" limit
)
1723 (if (eobp) (point) (line-beginning-position)))))
1729 :post-blank
(count-lines com-end end
)
1730 :post-affiliated post-affiliated
)
1731 (cdr affiliated
))))))
1733 (defun org-element-comment-interpreter (comment contents
)
1734 "Interpret COMMENT element as Org syntax.
1736 (replace-regexp-in-string "^" "# " (org-element-property :value comment
)))
1741 (defun org-element-comment-block-parser (limit affiliated
)
1742 "Parse an export block.
1744 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1745 the buffer position at the beginning of the first affiliated
1746 keyword and CDR is a plist of affiliated keywords along with
1749 Return a list whose CAR is `comment-block' and CDR is a plist
1750 containing `:begin', `:end', `:value', `:post-blank' and
1751 `:post-affiliated' keywords.
1753 Assume point is at comment block beginning."
1754 (let ((case-fold-search t
))
1755 (if (not (save-excursion
1756 (re-search-forward "^[ \t]*#\\+END_COMMENT[ \t]*$" limit t
)))
1757 ;; Incomplete block: parse it as a paragraph.
1758 (org-element-paragraph-parser limit affiliated
)
1759 (let ((contents-end (match-beginning 0)))
1761 (let* ((begin (car affiliated
))
1762 (post-affiliated (point))
1763 (contents-begin (progn (forward-line) (point)))
1764 (pos-before-blank (progn (goto-char contents-end
)
1767 (end (progn (skip-chars-forward " \r\t\n" limit
)
1768 (if (eobp) (point) (line-beginning-position))))
1769 (value (buffer-substring-no-properties
1770 contents-begin contents-end
)))
1771 (list 'comment-block
1776 :post-blank
(count-lines pos-before-blank end
)
1777 :post-affiliated post-affiliated
)
1778 (cdr affiliated
)))))))))
1780 (defun org-element-comment-block-interpreter (comment-block contents
)
1781 "Interpret COMMENT-BLOCK element as Org syntax.
1783 (format "#+BEGIN_COMMENT\n%s#+END_COMMENT"
1784 (org-element-normalize-string
1785 (org-remove-indentation
1786 (org-element-property :value comment-block
)))))
1791 (defun org-element-diary-sexp-parser (limit affiliated
)
1792 "Parse a diary sexp.
1794 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1795 the buffer position at the beginning of the first affiliated
1796 keyword and CDR is a plist of affiliated keywords along with
1799 Return a list whose CAR is `diary-sexp' and CDR is a plist
1800 containing `:begin', `:end', `:value', `:post-blank' and
1801 `:post-affiliated' keywords."
1803 (let ((begin (car affiliated
))
1804 (post-affiliated (point))
1805 (value (progn (looking-at "\\(%%(.*\\)[ \t]*$")
1806 (org-match-string-no-properties 1)))
1807 (pos-before-blank (progn (forward-line) (point)))
1808 (end (progn (skip-chars-forward " \r\t\n" limit
)
1809 (if (eobp) (point) (line-beginning-position)))))
1815 :post-blank
(count-lines pos-before-blank end
)
1816 :post-affiliated post-affiliated
)
1817 (cdr affiliated
))))))
1819 (defun org-element-diary-sexp-interpreter (diary-sexp contents
)
1820 "Interpret DIARY-SEXP as Org syntax.
1822 (org-element-property :value diary-sexp
))
1827 (defun org-element-example-block-parser (limit affiliated
)
1828 "Parse an example block.
1830 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1831 the buffer position at the beginning of the first affiliated
1832 keyword and CDR is a plist of affiliated keywords along with
1835 Return a list whose CAR is `example-block' and CDR is a plist
1836 containing `:begin', `:end', `:number-lines', `:preserve-indent',
1837 `:retain-labels', `:use-labels', `:label-fmt', `:switches',
1838 `:value', `:post-blank' and `:post-affiliated' keywords."
1839 (let ((case-fold-search t
))
1840 (if (not (save-excursion
1841 (re-search-forward "^[ \t]*#\\+END_EXAMPLE[ \t]*$" limit t
)))
1842 ;; Incomplete block: parse it as a paragraph.
1843 (org-element-paragraph-parser limit affiliated
)
1844 (let ((contents-end (match-beginning 0)))
1848 (looking-at "^[ \t]*#\\+BEGIN_EXAMPLE\\(?: +\\(.*\\)\\)?")
1849 (org-match-string-no-properties 1)))
1850 ;; Switches analysis
1852 (cond ((not switches
) nil
)
1853 ((string-match "-n\\>" switches
) 'new
)
1854 ((string-match "+n\\>" switches
) 'continued
)))
1856 (and switches
(string-match "-i\\>" switches
)))
1857 ;; Should labels be retained in (or stripped from) example
1861 (not (string-match "-r\\>" switches
))
1862 (and number-lines
(string-match "-k\\>" switches
))))
1863 ;; What should code-references use - labels or
1868 (not (string-match "-k\\>" switches
)))))
1871 (string-match "-l +\"\\([^\"\n]+\\)\"" switches
)
1872 (match-string 1 switches
)))
1873 ;; Standard block parsing.
1874 (begin (car affiliated
))
1875 (post-affiliated (point))
1876 (block-ind (progn (skip-chars-forward " \t") (current-column)))
1877 (contents-begin (progn (forward-line) (point)))
1878 (value (org-element-remove-indentation
1879 (org-unescape-code-in-string
1880 (buffer-substring-no-properties
1881 contents-begin contents-end
))
1883 (pos-before-blank (progn (goto-char contents-end
)
1886 (end (progn (skip-chars-forward " \r\t\n" limit
)
1887 (if (eobp) (point) (line-beginning-position)))))
1888 (list 'example-block
1894 :number-lines number-lines
1895 :preserve-indent preserve-indent
1896 :retain-labels retain-labels
1897 :use-labels use-labels
1898 :label-fmt label-fmt
1899 :post-blank
(count-lines pos-before-blank end
)
1900 :post-affiliated post-affiliated
)
1901 (cdr affiliated
)))))))))
1903 (defun org-element-example-block-interpreter (example-block contents
)
1904 "Interpret EXAMPLE-BLOCK element as Org syntax.
1906 (let ((switches (org-element-property :switches example-block
))
1907 (value (org-element-property :value example-block
)))
1908 (concat "#+BEGIN_EXAMPLE" (and switches
(concat " " switches
)) "\n"
1909 (org-element-normalize-string
1910 (org-escape-code-in-string
1911 (if (or org-src-preserve-indentation
1912 (org-element-property :preserve-indent example-block
))
1914 (org-element-remove-indentation value
))))
1920 (defun org-element-export-block-parser (limit affiliated
)
1921 "Parse an export block.
1923 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1924 the buffer position at the beginning of the first affiliated
1925 keyword and CDR is a plist of affiliated keywords along with
1928 Return a list whose CAR is `export-block' and CDR is a plist
1929 containing `:begin', `:end', `:type', `:value', `:post-blank' and
1930 `:post-affiliated' keywords.
1932 Assume point is at export-block beginning."
1933 (let* ((case-fold-search t
)
1934 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
1935 (upcase (org-match-string-no-properties 1)))))
1936 (if (not (save-excursion
1938 (format "^[ \t]*#\\+END_%s[ \t]*$" type
) limit t
)))
1939 ;; Incomplete block: parse it as a paragraph.
1940 (org-element-paragraph-parser limit affiliated
)
1941 (let ((contents-end (match-beginning 0)))
1943 (let* ((begin (car affiliated
))
1944 (post-affiliated (point))
1945 (contents-begin (progn (forward-line) (point)))
1946 (pos-before-blank (progn (goto-char contents-end
)
1949 (end (progn (skip-chars-forward " \r\t\n" limit
)
1950 (if (eobp) (point) (line-beginning-position))))
1951 (value (buffer-substring-no-properties contents-begin
1959 :post-blank
(count-lines pos-before-blank end
)
1960 :post-affiliated post-affiliated
)
1961 (cdr affiliated
)))))))))
1963 (defun org-element-export-block-interpreter (export-block contents
)
1964 "Interpret EXPORT-BLOCK element as Org syntax.
1966 (let ((type (org-element-property :type export-block
)))
1967 (concat (format "#+BEGIN_%s\n" type
)
1968 (org-element-property :value export-block
)
1969 (format "#+END_%s" type
))))
1974 (defun org-element-fixed-width-parser (limit affiliated
)
1975 "Parse a fixed-width section.
1977 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1978 the buffer position at the beginning of the first affiliated
1979 keyword and CDR is a plist of affiliated keywords along with
1982 Return a list whose CAR is `fixed-width' and CDR is a plist
1983 containing `:begin', `:end', `:value', `:post-blank' and
1984 `:post-affiliated' keywords.
1986 Assume point is at the beginning of the fixed-width area."
1988 (let* ((begin (car affiliated
))
1989 (post-affiliated (point))
1993 (while (and (< (point) limit
)
1994 (looking-at "[ \t]*:\\( \\|$\\)"))
1995 ;; Accumulate text without starting colons.
1998 (buffer-substring-no-properties
1999 (match-end 0) (point-at-eol))
2003 (end (progn (skip-chars-forward " \r\t\n" limit
)
2004 (if (eobp) (point) (line-beginning-position)))))
2010 :post-blank
(count-lines end-area end
)
2011 :post-affiliated post-affiliated
)
2012 (cdr affiliated
))))))
2014 (defun org-element-fixed-width-interpreter (fixed-width contents
)
2015 "Interpret FIXED-WIDTH element as Org syntax.
2017 (let ((value (org-element-property :value fixed-width
)))
2019 (replace-regexp-in-string
2021 (if (string-match "\n\\'" value
) (substring value
0 -
1) value
)))))
2024 ;;;; Horizontal Rule
2026 (defun org-element-horizontal-rule-parser (limit affiliated
)
2027 "Parse an horizontal rule.
2029 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2030 the buffer position at the beginning of the first affiliated
2031 keyword and CDR is a plist of affiliated keywords along with
2034 Return a list whose CAR is `horizontal-rule' and CDR is a plist
2035 containing `:begin', `:end', `:post-blank' and `:post-affiliated'
2038 (let ((begin (car affiliated
))
2039 (post-affiliated (point))
2040 (post-hr (progn (forward-line) (point)))
2041 (end (progn (skip-chars-forward " \r\t\n" limit
)
2042 (if (eobp) (point) (line-beginning-position)))))
2043 (list 'horizontal-rule
2047 :post-blank
(count-lines post-hr end
)
2048 :post-affiliated post-affiliated
)
2049 (cdr affiliated
))))))
2051 (defun org-element-horizontal-rule-interpreter (horizontal-rule contents
)
2052 "Interpret HORIZONTAL-RULE element as Org syntax.
2059 (defun org-element-keyword-parser (limit affiliated
)
2060 "Parse a keyword at point.
2062 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2063 the buffer position at the beginning of the first affiliated
2064 keyword and CDR is a plist of affiliated keywords along with
2067 Return a list whose CAR is `keyword' and CDR is a plist
2068 containing `:key', `:value', `:begin', `:end', `:post-blank' and
2069 `:post-affiliated' keywords."
2071 ;; An orphaned affiliated keyword is considered as a regular
2072 ;; keyword. In this case AFFILIATED is nil, so we take care of
2073 ;; this corner case.
2074 (let ((begin (or (car affiliated
) (point)))
2075 (post-affiliated (point))
2076 (key (progn (looking-at "[ \t]*#\\+\\(\\S-+*\\):")
2077 (upcase (org-match-string-no-properties 1))))
2078 (value (org-trim (buffer-substring-no-properties
2079 (match-end 0) (point-at-eol))))
2080 (pos-before-blank (progn (forward-line) (point)))
2081 (end (progn (skip-chars-forward " \r\t\n" limit
)
2082 (if (eobp) (point) (line-beginning-position)))))
2089 :post-blank
(count-lines pos-before-blank end
)
2090 :post-affiliated post-affiliated
)
2091 (cdr affiliated
))))))
2093 (defun org-element-keyword-interpreter (keyword contents
)
2094 "Interpret KEYWORD element as Org syntax.
2097 (org-element-property :key keyword
)
2098 (org-element-property :value keyword
)))
2101 ;;;; Latex Environment
2103 (defconst org-element--latex-begin-environment
2104 "^[ \t]*\\\\begin{\\([A-Za-z0-9*]+\\)}"
2105 "Regexp matching the beginning of a LaTeX environment.
2106 The environment is captured by the first group.
2108 See also `org-element--latex-end-environment'.")
2110 (defconst org-element--latex-end-environment
2111 "\\\\end{%s}[ \t]*$"
2112 "Format string matching the ending of a LaTeX environment.
2113 See also `org-element--latex-begin-environment'.")
2115 (defun org-element-latex-environment-parser (limit affiliated
)
2116 "Parse a LaTeX environment.
2118 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2119 the buffer position at the beginning of the first affiliated
2120 keyword and CDR is a plist of affiliated keywords along with
2123 Return a list whose CAR is `latex-environment' and CDR is a plist
2124 containing `:begin', `:end', `:value', `:post-blank' and
2125 `:post-affiliated' keywords.
2127 Assume point is at the beginning of the latex environment."
2129 (let ((case-fold-search t
)
2130 (code-begin (point)))
2131 (looking-at org-element--latex-begin-environment
)
2132 (if (not (re-search-forward (format org-element--latex-end-environment
2133 (regexp-quote (match-string 1)))
2135 ;; Incomplete latex environment: parse it as a paragraph.
2136 (org-element-paragraph-parser limit affiliated
)
2137 (let* ((code-end (progn (forward-line) (point)))
2138 (begin (car affiliated
))
2139 (value (buffer-substring-no-properties code-begin code-end
))
2140 (end (progn (skip-chars-forward " \r\t\n" limit
)
2141 (if (eobp) (point) (line-beginning-position)))))
2142 (list 'latex-environment
2147 :post-blank
(count-lines code-end end
)
2148 :post-affiliated code-begin
)
2149 (cdr affiliated
))))))))
2151 (defun org-element-latex-environment-interpreter (latex-environment contents
)
2152 "Interpret LATEX-ENVIRONMENT element as Org syntax.
2154 (org-element-property :value latex-environment
))
2159 (defun org-element-node-property-parser (limit)
2160 "Parse a node-property at point.
2162 LIMIT bounds the search.
2164 Return a list whose CAR is `node-property' and CDR is a plist
2165 containing `:key', `:value', `:begin', `:end', `:post-blank' and
2166 `:post-affiliated' keywords."
2167 (looking-at org-property-re
)
2168 (let ((case-fold-search t
)
2170 (key (org-match-string-no-properties 2))
2171 (value (org-match-string-no-properties 3))
2172 (end (save-excursion
2174 (if (re-search-forward org-property-re limit t
)
2175 (line-beginning-position)
2177 (list 'node-property
2183 :post-affiliated begin
))))
2185 (defun org-element-node-property-interpreter (node-property contents
)
2186 "Interpret NODE-PROPERTY element as Org syntax.
2188 (format org-property-format
2189 (format ":%s:" (org-element-property :key node-property
))
2190 (or (org-element-property :value node-property
) "")))
2195 (defun org-element-paragraph-parser (limit affiliated
)
2198 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2199 the buffer position at the beginning of the first affiliated
2200 keyword and CDR is a plist of affiliated keywords along with
2203 Return a list whose CAR is `paragraph' and CDR is a plist
2204 containing `:begin', `:end', `:contents-begin' and
2205 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
2207 Assume point is at the beginning of the paragraph."
2209 (let* ((begin (car affiliated
))
2210 (contents-begin (point))
2212 (let ((case-fold-search t
))
2214 (if (not (re-search-forward
2215 org-element-paragraph-separate limit
'm
))
2217 ;; A matching `org-element-paragraph-separate' is not
2218 ;; necessarily the end of the paragraph. In
2219 ;; particular, lines starting with # or : as a first
2220 ;; non-space character are ambiguous. We have to
2221 ;; check if they are valid Org syntax (e.g., not an
2222 ;; incomplete keyword).
2226 ;; There's no ambiguity for other symbols or
2227 ;; empty lines: stop here.
2228 (looking-at "[ \t]*\\(?:[^:#]\\|$\\)")
2229 ;; Stop at valid fixed-width areas.
2230 (looking-at "[ \t]*:\\(?: \\|$\\)")
2232 (and (looking-at org-drawer-regexp
)
2235 "^[ \t]*:END:[ \t]*$" limit t
)))
2236 ;; Stop at valid comments.
2237 (looking-at "[ \t]*#\\(?: \\|$\\)")
2238 ;; Stop at valid dynamic blocks.
2239 (and (looking-at org-dblock-start-re
)
2242 "^[ \t]*#\\+END:?[ \t]*$" limit t
)))
2243 ;; Stop at valid blocks.
2244 (and (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
2247 (format "^[ \t]*#\\+END_%s[ \t]*$"
2249 (org-match-string-no-properties 1)))
2251 ;; Stop at valid latex environments.
2252 (and (looking-at org-element--latex-begin-environment
)
2255 (format org-element--latex-end-environment
2257 (org-match-string-no-properties 1)))
2259 ;; Stop at valid keywords.
2260 (looking-at "[ \t]*#\\+\\S-+:")
2261 ;; Skip everything else.
2265 (re-search-forward org-element-paragraph-separate
2267 (beginning-of-line)))
2268 (if (= (point) limit
) limit
2269 (goto-char (line-beginning-position)))))
2270 (contents-end (progn (skip-chars-backward " \r\t\n" contents-begin
)
2273 (end (progn (skip-chars-forward " \r\t\n" limit
)
2274 (if (eobp) (point) (line-beginning-position)))))
2279 :contents-begin contents-begin
2280 :contents-end contents-end
2281 :post-blank
(count-lines before-blank end
)
2282 :post-affiliated contents-begin
)
2283 (cdr affiliated
))))))
2285 (defun org-element-paragraph-interpreter (paragraph contents
)
2286 "Interpret PARAGRAPH element as Org syntax.
2287 CONTENTS is the contents of the element."
2293 (defun org-element-planning-parser (limit)
2296 LIMIT bounds the search.
2298 Return a list whose CAR is `planning' and CDR is a plist
2299 containing `:closed', `:deadline', `:scheduled', `:begin',
2300 `:end', `:post-blank' and `:post-affiliated' keywords."
2302 (let* ((case-fold-search nil
)
2304 (post-blank (let ((before-blank (progn (forward-line) (point))))
2305 (skip-chars-forward " \r\t\n" limit
)
2306 (skip-chars-backward " \t")
2307 (unless (bolp) (end-of-line))
2308 (count-lines before-blank
(point))))
2310 closed deadline scheduled
)
2312 (while (re-search-forward org-keyword-time-not-clock-regexp end t
)
2313 (goto-char (match-end 1))
2314 (skip-chars-forward " \t" end
)
2315 (let ((keyword (match-string 1))
2316 (time (org-element-timestamp-parser)))
2317 (cond ((equal keyword org-closed-string
) (setq closed time
))
2318 ((equal keyword org-deadline-string
) (setq deadline time
))
2319 (t (setq scheduled time
)))))
2321 (list :closed closed
2323 :scheduled scheduled
2326 :post-blank post-blank
2327 :post-affiliated begin
)))))
2329 (defun org-element-planning-interpreter (planning contents
)
2330 "Interpret PLANNING element as Org syntax.
2335 (list (let ((deadline (org-element-property :deadline planning
)))
2337 (concat org-deadline-string
" "
2338 (org-element-timestamp-interpreter deadline nil
))))
2339 (let ((scheduled (org-element-property :scheduled planning
)))
2341 (concat org-scheduled-string
" "
2342 (org-element-timestamp-interpreter scheduled nil
))))
2343 (let ((closed (org-element-property :closed planning
)))
2345 (concat org-closed-string
" "
2346 (org-element-timestamp-interpreter closed nil
))))))
2352 (defun org-element-src-block-parser (limit affiliated
)
2355 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2356 the buffer position at the beginning of the first affiliated
2357 keyword and CDR is a plist of affiliated keywords along with
2360 Return a list whose CAR is `src-block' and CDR is a plist
2361 containing `:language', `:switches', `:parameters', `:begin',
2362 `:end', `:number-lines', `:retain-labels', `:use-labels',
2363 `:label-fmt', `:preserve-indent', `:value', `:post-blank' and
2364 `:post-affiliated' keywords.
2366 Assume point is at the beginning of the block."
2367 (let ((case-fold-search t
))
2368 (if (not (save-excursion (re-search-forward "^[ \t]*#\\+END_SRC[ \t]*$"
2370 ;; Incomplete block: parse it as a paragraph.
2371 (org-element-paragraph-parser limit affiliated
)
2372 (let ((contents-end (match-beginning 0)))
2374 (let* ((begin (car affiliated
))
2375 (post-affiliated (point))
2376 ;; Get language as a string.
2380 (concat "^[ \t]*#\\+BEGIN_SRC"
2381 "\\(?: +\\(\\S-+\\)\\)?"
2382 "\\(\\(?: +\\(?:-l \".*?\"\\|[-+][A-Za-z]\\)\\)+\\)?"
2384 (org-match-string-no-properties 1)))
2386 (switches (org-match-string-no-properties 2))
2388 (parameters (org-match-string-no-properties 3))
2389 ;; Switches analysis
2391 (cond ((not switches
) nil
)
2392 ((string-match "-n\\>" switches
) 'new
)
2393 ((string-match "+n\\>" switches
) 'continued
)))
2394 (preserve-indent (and switches
2395 (string-match "-i\\>" switches
)))
2398 (string-match "-l +\"\\([^\"\n]+\\)\"" switches
)
2399 (match-string 1 switches
)))
2400 ;; Should labels be retained in (or stripped from)
2404 (not (string-match "-r\\>" switches
))
2405 (and number-lines
(string-match "-k\\>" switches
))))
2406 ;; What should code-references use - labels or
2411 (not (string-match "-k\\>" switches
)))))
2413 (block-ind (progn (skip-chars-forward " \t") (current-column)))
2415 (value (org-element-remove-indentation
2416 (org-unescape-code-in-string
2417 (buffer-substring-no-properties
2418 (progn (forward-line) (point)) contents-end
))
2420 (pos-before-blank (progn (goto-char contents-end
)
2423 ;; Get position after ending blank lines.
2424 (end (progn (skip-chars-forward " \r\t\n" limit
)
2425 (if (eobp) (point) (line-beginning-position)))))
2428 (list :language language
2429 :switches
(and (org-string-nw-p switches
)
2430 (org-trim switches
))
2431 :parameters
(and (org-string-nw-p parameters
)
2432 (org-trim parameters
))
2435 :number-lines number-lines
2436 :preserve-indent preserve-indent
2437 :retain-labels retain-labels
2438 :use-labels use-labels
2439 :label-fmt label-fmt
2441 :post-blank
(count-lines pos-before-blank end
)
2442 :post-affiliated post-affiliated
)
2443 (cdr affiliated
)))))))))
2445 (defun org-element-src-block-interpreter (src-block contents
)
2446 "Interpret SRC-BLOCK element as Org syntax.
2448 (let ((lang (org-element-property :language src-block
))
2449 (switches (org-element-property :switches src-block
))
2450 (params (org-element-property :parameters src-block
))
2452 (let ((val (org-element-property :value src-block
)))
2454 ((or org-src-preserve-indentation
2455 (org-element-property :preserve-indent src-block
))
2457 ((zerop org-edit-src-content-indentation
) val
)
2459 (let ((ind (make-string org-edit-src-content-indentation ?\s
)))
2460 (replace-regexp-in-string
2461 "\\(^\\)[ \t]*\\S-" ind val nil nil
1)))))))
2462 (concat (format "#+BEGIN_SRC%s\n"
2463 (concat (and lang
(concat " " lang
))
2464 (and switches
(concat " " switches
))
2465 (and params
(concat " " params
))))
2466 (org-element-normalize-string (org-escape-code-in-string value
))
2472 (defun org-element-table-parser (limit affiliated
)
2473 "Parse a table at point.
2475 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2476 the buffer position at the beginning of the first affiliated
2477 keyword and CDR is a plist of affiliated keywords along with
2480 Return a list whose CAR is `table' and CDR is a plist containing
2481 `:begin', `:end', `:tblfm', `:type', `:contents-begin',
2482 `:contents-end', `:value', `:post-blank' and `:post-affiliated'
2485 Assume point is at the beginning of the table."
2487 (let* ((case-fold-search t
)
2488 (table-begin (point))
2489 (type (if (org-at-table.el-p
) 'table.el
'org
))
2490 (begin (car affiliated
))
2492 (if (re-search-forward org-table-any-border-regexp limit
'm
)
2493 (goto-char (match-beginning 0))
2496 (while (looking-at "[ \t]*#\\+TBLFM: +\\(.*\\)[ \t]*$")
2497 (push (org-match-string-no-properties 1) acc
)
2500 (pos-before-blank (point))
2501 (end (progn (skip-chars-forward " \r\t\n" limit
)
2502 (if (eobp) (point) (line-beginning-position)))))
2509 ;; Only `org' tables have contents. `table.el' tables
2510 ;; use a `:value' property to store raw table as
2512 :contents-begin
(and (eq type
'org
) table-begin
)
2513 :contents-end
(and (eq type
'org
) table-end
)
2514 :value
(and (eq type
'table.el
)
2515 (buffer-substring-no-properties
2516 table-begin table-end
))
2517 :post-blank
(count-lines pos-before-blank end
)
2518 :post-affiliated table-begin
)
2519 (cdr affiliated
))))))
2521 (defun org-element-table-interpreter (table contents
)
2522 "Interpret TABLE element as Org syntax.
2523 CONTENTS is a string, if table's type is `org', or nil."
2524 (if (eq (org-element-property :type table
) 'table.el
)
2525 (org-remove-indentation (org-element-property :value table
))
2526 (concat (with-temp-buffer (insert contents
)
2529 (mapconcat (lambda (fm) (concat "#+TBLFM: " fm
))
2530 (reverse (org-element-property :tblfm table
))
2536 (defun org-element-table-row-parser (limit)
2537 "Parse table row at point.
2539 LIMIT bounds the search.
2541 Return a list whose CAR is `table-row' and CDR is a plist
2542 containing `:begin', `:end', `:contents-begin', `:contents-end',
2543 `:type', `:post-blank' and `:post-affiliated' keywords."
2545 (let* ((type (if (looking-at "^[ \t]*|-") 'rule
'standard
))
2547 ;; A table rule has no contents. In that case, ensure
2548 ;; CONTENTS-BEGIN matches CONTENTS-END.
2549 (contents-begin (and (eq type
'standard
)
2550 (search-forward "|")
2552 (contents-end (and (eq type
'standard
)
2555 (skip-chars-backward " \t")
2557 (end (progn (forward-line) (point))))
2562 :contents-begin contents-begin
2563 :contents-end contents-end
2565 :post-affiliated begin
)))))
2567 (defun org-element-table-row-interpreter (table-row contents
)
2568 "Interpret TABLE-ROW element as Org syntax.
2569 CONTENTS is the contents of the table row."
2570 (if (eq (org-element-property :type table-row
) 'rule
) "|-"
2571 (concat "| " contents
)))
2576 (defun org-element-verse-block-parser (limit affiliated
)
2577 "Parse a verse block.
2579 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2580 the buffer position at the beginning of the first affiliated
2581 keyword and CDR is a plist of affiliated keywords along with
2584 Return a list whose CAR is `verse-block' and CDR is a plist
2585 containing `:begin', `:end', `:contents-begin', `:contents-end',
2586 `:post-blank' and `:post-affiliated' keywords.
2588 Assume point is at beginning of the block."
2589 (let ((case-fold-search t
))
2590 (if (not (save-excursion
2591 (re-search-forward "^[ \t]*#\\+END_VERSE[ \t]*$" limit t
)))
2592 ;; Incomplete block: parse it as a paragraph.
2593 (org-element-paragraph-parser limit affiliated
)
2594 (let ((contents-end (match-beginning 0)))
2596 (let* ((begin (car affiliated
))
2597 (post-affiliated (point))
2598 (contents-begin (progn (forward-line) (point)))
2599 (pos-before-blank (progn (goto-char contents-end
)
2602 (end (progn (skip-chars-forward " \r\t\n" limit
)
2603 (if (eobp) (point) (line-beginning-position)))))
2608 :contents-begin contents-begin
2609 :contents-end contents-end
2610 :post-blank
(count-lines pos-before-blank end
)
2611 :post-affiliated post-affiliated
)
2612 (cdr affiliated
)))))))))
2614 (defun org-element-verse-block-interpreter (verse-block contents
)
2615 "Interpret VERSE-BLOCK element as Org syntax.
2616 CONTENTS is verse block contents."
2617 (format "#+BEGIN_VERSE\n%s#+END_VERSE" contents
))
2623 ;; Unlike to elements, raw text can be found between objects. Hence,
2624 ;; `org-element--object-lex' is provided to find the next object in
2627 ;; Some object types (e.g., `italic') are recursive. Restrictions on
2628 ;; object types they can contain will be specified in
2629 ;; `org-element-object-restrictions'.
2631 ;; Creating a new type of object requires to alter
2632 ;; `org-element--object-regexp' and `org-element--object-lex', add the
2633 ;; new type in `org-element-all-objects', and possibly add
2634 ;; restrictions in `org-element-object-restrictions'.
2638 (defun org-element-bold-parser ()
2639 "Parse bold object at point, if any.
2641 When at a bold object, return a list whose car is `bold' and cdr
2642 is a plist with `:begin', `:end', `:contents-begin' and
2643 `:contents-end' and `:post-blank' keywords. Otherwise, return
2646 Assume point is at the first star marker."
2648 (unless (bolp) (backward-char 1))
2649 (when (looking-at org-emph-re
)
2650 (let ((begin (match-beginning 2))
2651 (contents-begin (match-beginning 4))
2652 (contents-end (match-end 4))
2653 (post-blank (progn (goto-char (match-end 2))
2654 (skip-chars-forward " \t")))
2659 :contents-begin contents-begin
2660 :contents-end contents-end
2661 :post-blank post-blank
))))))
2663 (defun org-element-bold-interpreter (bold contents
)
2664 "Interpret BOLD object as Org syntax.
2665 CONTENTS is the contents of the object."
2666 (format "*%s*" contents
))
2671 (defun org-element-code-parser ()
2672 "Parse code object at point, if any.
2674 When at a code object, return a list whose car is `code' and cdr
2675 is a plist with `:value', `:begin', `:end' and `:post-blank'
2676 keywords. Otherwise, return nil.
2678 Assume point is at the first tilde marker."
2680 (unless (bolp) (backward-char 1))
2681 (when (looking-at org-emph-re
)
2682 (let ((begin (match-beginning 2))
2683 (value (org-match-string-no-properties 4))
2684 (post-blank (progn (goto-char (match-end 2))
2685 (skip-chars-forward " \t")))
2691 :post-blank post-blank
))))))
2693 (defun org-element-code-interpreter (code contents
)
2694 "Interpret CODE object as Org syntax.
2696 (format "~%s~" (org-element-property :value code
)))
2701 (defun org-element-entity-parser ()
2702 "Parse entity at point, if any.
2704 When at an entity, return a list whose car is `entity' and cdr
2705 a plist with `:begin', `:end', `:latex', `:latex-math-p',
2706 `:html', `:latin1', `:utf-8', `:ascii', `:use-brackets-p' and
2707 `:post-blank' as keywords. Otherwise, return nil.
2709 Assume point is at the beginning of the entity."
2711 (when (looking-at "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)")
2713 (let* ((value (or (org-entity-get (match-string 1))
2714 (throw 'no-object nil
)))
2715 (begin (match-beginning 0))
2716 (bracketsp (string= (match-string 2) "{}"))
2717 (post-blank (progn (goto-char (match-end 1))
2718 (when bracketsp
(forward-char 2))
2719 (skip-chars-forward " \t")))
2722 (list :name
(car value
)
2723 :latex
(nth 1 value
)
2724 :latex-math-p
(nth 2 value
)
2726 :ascii
(nth 4 value
)
2727 :latin1
(nth 5 value
)
2728 :utf-8
(nth 6 value
)
2731 :use-brackets-p bracketsp
2732 :post-blank post-blank
)))))))
2734 (defun org-element-entity-interpreter (entity contents
)
2735 "Interpret ENTITY object as Org syntax.
2738 (org-element-property :name entity
)
2739 (when (org-element-property :use-brackets-p entity
) "{}")))
2744 (defun org-element-export-snippet-parser ()
2745 "Parse export snippet at point.
2747 When at an export snippet, return a list whose car is
2748 `export-snippet' and cdr a plist with `:begin', `:end',
2749 `:back-end', `:value' and `:post-blank' as keywords. Otherwise,
2752 Assume point is at the beginning of the snippet."
2755 (when (and (looking-at "@@\\([-A-Za-z0-9]+\\):")
2757 (save-match-data (goto-char (match-end 0))
2758 (re-search-forward "@@" nil t
)
2759 (match-beginning 0))))
2760 (let* ((begin (match-beginning 0))
2761 (back-end (org-match-string-no-properties 1))
2762 (value (buffer-substring-no-properties
2763 (match-end 0) contents-end
))
2764 (post-blank (skip-chars-forward " \t"))
2766 (list 'export-snippet
2767 (list :back-end back-end
2771 :post-blank post-blank
)))))))
2773 (defun org-element-export-snippet-interpreter (export-snippet contents
)
2774 "Interpret EXPORT-SNIPPET object as Org syntax.
2777 (org-element-property :back-end export-snippet
)
2778 (org-element-property :value export-snippet
)))
2781 ;;;; Footnote Reference
2783 (defun org-element-footnote-reference-parser ()
2784 "Parse footnote reference at point, if any.
2786 When at a footnote reference, return a list whose car is
2787 `footnote-reference' and cdr a plist with `:label', `:type',
2788 `:begin', `:end', `:content-begin', `:contents-end' and
2789 `:post-blank' as keywords. Otherwise, return nil."
2791 (when (looking-at org-footnote-re
)
2793 (let* ((begin (point))
2795 (or (org-match-string-no-properties 2)
2796 (org-match-string-no-properties 3)
2797 (and (match-string 1)
2798 (concat "fn:" (org-match-string-no-properties 1)))))
2799 (type (if (or (not label
) (match-string 1)) 'inline
'standard
))
2800 (inner-begin (match-end 0))
2804 (while (and (> count
0) (re-search-forward "[][]" nil t
))
2805 (if (equal (match-string 0) "[") (incf count
) (decf count
)))
2806 (unless (zerop count
) (throw 'no-object nil
))
2808 (post-blank (progn (goto-char (1+ inner-end
))
2809 (skip-chars-forward " \t")))
2811 (list 'footnote-reference
2816 :contents-begin
(and (eq type
'inline
) inner-begin
)
2817 :contents-end
(and (eq type
'inline
) inner-end
)
2818 :post-blank post-blank
)))))))
2820 (defun org-element-footnote-reference-interpreter (footnote-reference contents
)
2821 "Interpret FOOTNOTE-REFERENCE object as Org syntax.
2822 CONTENTS is its definition, when inline, or nil."
2824 (concat (or (org-element-property :label footnote-reference
) "fn:")
2825 (and contents
(concat ":" contents
)))))
2828 ;;;; Inline Babel Call
2830 (defun org-element-inline-babel-call-parser ()
2831 "Parse inline babel call at point, if any.
2833 When at an inline babel call, return a list whose car is
2834 `inline-babel-call' and cdr a plist with `:begin', `:end',
2835 `:value' and `:post-blank' as keywords. Otherwise, return nil.
2837 Assume point is at the beginning of the babel call."
2839 (unless (bolp) (backward-char))
2840 (when (let ((case-fold-search t
))
2841 (looking-at org-babel-inline-lob-one-liner-regexp
))
2842 (let ((begin (match-end 1))
2843 (value (buffer-substring-no-properties (match-end 1) (match-end 0)))
2844 (post-blank (progn (goto-char (match-end 0))
2845 (skip-chars-forward " \t")))
2847 (list 'inline-babel-call
2851 :post-blank post-blank
))))))
2853 (defun org-element-inline-babel-call-interpreter (inline-babel-call contents
)
2854 "Interpret INLINE-BABEL-CALL object as Org syntax.
2856 (org-element-property :value inline-babel-call
))
2859 ;;;; Inline Src Block
2861 (defun org-element-inline-src-block-parser ()
2862 "Parse inline source block at point, if any.
2864 When at an inline source block, return a list whose car is
2865 `inline-src-block' and cdr a plist with `:begin', `:end',
2866 `:language', `:value', `:parameters' and `:post-blank' as
2867 keywords. Otherwise, return nil.
2869 Assume point is at the beginning of the inline src block."
2871 (unless (bolp) (backward-char))
2872 (when (looking-at org-babel-inline-src-block-regexp
)
2873 (let ((begin (match-beginning 1))
2874 (language (org-match-string-no-properties 2))
2875 (parameters (org-match-string-no-properties 4))
2876 (value (org-match-string-no-properties 5))
2877 (post-blank (progn (goto-char (match-end 0))
2878 (skip-chars-forward " \t")))
2880 (list 'inline-src-block
2881 (list :language language
2883 :parameters parameters
2886 :post-blank post-blank
))))))
2888 (defun org-element-inline-src-block-interpreter (inline-src-block contents
)
2889 "Interpret INLINE-SRC-BLOCK object as Org syntax.
2891 (let ((language (org-element-property :language inline-src-block
))
2892 (arguments (org-element-property :parameters inline-src-block
))
2893 (body (org-element-property :value inline-src-block
)))
2894 (format "src_%s%s{%s}"
2896 (if arguments
(format "[%s]" arguments
) "")
2901 (defun org-element-italic-parser ()
2902 "Parse italic object at point, if any.
2904 When at an italic object, return a list whose car is `italic' and
2905 cdr is a plist with `:begin', `:end', `:contents-begin' and
2906 `:contents-end' and `:post-blank' keywords. Otherwise, return
2909 Assume point is at the first slash marker."
2911 (unless (bolp) (backward-char 1))
2912 (when (looking-at org-emph-re
)
2913 (let ((begin (match-beginning 2))
2914 (contents-begin (match-beginning 4))
2915 (contents-end (match-end 4))
2916 (post-blank (progn (goto-char (match-end 2))
2917 (skip-chars-forward " \t")))
2922 :contents-begin contents-begin
2923 :contents-end contents-end
2924 :post-blank post-blank
))))))
2926 (defun org-element-italic-interpreter (italic contents
)
2927 "Interpret ITALIC object as Org syntax.
2928 CONTENTS is the contents of the object."
2929 (format "/%s/" contents
))
2934 (defun org-element-latex-fragment-parser ()
2935 "Parse LaTeX fragment at point, if any.
2937 When at a LaTeX fragment, return a list whose car is
2938 `latex-fragment' and cdr a plist with `:value', `:begin', `:end',
2939 and `:post-blank' as keywords. Otherwise, return nil.
2941 Assume point is at the beginning of the LaTeX fragment."
2944 (let* ((begin (point))
2946 (if (eq (char-after) ?$
)
2947 (if (eq (char-after (1+ (point))) ?$
)
2948 (search-forward "$$" nil t
2)
2949 (and (not (eq (char-before) ?$
))
2950 (search-forward "$" nil t
2)
2951 (not (memq (char-before (match-beginning 0))
2952 '(?\s ?
\t ?
\n ?
, ?.
)))
2953 (looking-at "\\([- \t.,?;:'\"]\\|$\\)")
2955 (case (char-after (1+ (point)))
2956 (?\
( (search-forward "\\)" nil t
))
2957 (?\
[ (search-forward "\\]" nil t
))
2960 (and (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")
2962 (post-blank (if (not after-fragment
) (throw 'no-object nil
)
2963 (goto-char after-fragment
)
2964 (skip-chars-forward " \t")))
2966 (list 'latex-fragment
2967 (list :value
(buffer-substring-no-properties begin after-fragment
)
2970 :post-blank post-blank
))))))
2972 (defun org-element-latex-fragment-interpreter (latex-fragment contents
)
2973 "Interpret LATEX-FRAGMENT object as Org syntax.
2975 (org-element-property :value latex-fragment
))
2979 (defun org-element-line-break-parser ()
2980 "Parse line break at point, if any.
2982 When at a line break, return a list whose car is `line-break',
2983 and cdr a plist with `:begin', `:end' and `:post-blank' keywords.
2984 Otherwise, return nil.
2986 Assume point is at the beginning of the line break."
2987 (when (and (org-looking-at-p "\\\\\\\\[ \t]*$")
2988 (not (eq (char-before) ?
\\)))
2990 (list :begin
(point)
2991 :end
(progn (forward-line) (point))
2994 (defun org-element-line-break-interpreter (line-break contents
)
2995 "Interpret LINE-BREAK object as Org syntax.
3002 (defun org-element-link-parser ()
3003 "Parse link at point, if any.
3005 When at a link, return a list whose car is `link' and cdr a plist
3006 with `:type', `:path', `:raw-link', `:application',
3007 `:search-option', `:begin', `:end', `:contents-begin',
3008 `:contents-end' and `:post-blank' as keywords. Otherwise, return
3011 Assume point is at the beginning of the link."
3013 (let ((begin (point))
3014 end contents-begin contents-end link-end post-blank path type
3015 raw-link link search-option application
)
3017 ;; Type 1: Text targeted from a radio target.
3018 ((and org-target-link-regexp
3019 (save-excursion (or (bolp) (backward-char))
3020 (looking-at org-target-link-regexp
)))
3022 link-end
(match-end 1)
3023 path
(org-match-string-no-properties 1)
3024 contents-begin
(match-beginning 1)
3025 contents-end
(match-end 1)))
3026 ;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]]
3027 ((looking-at org-bracket-link-regexp
)
3028 (setq contents-begin
(match-beginning 3)
3029 contents-end
(match-end 3)
3030 link-end
(match-end 0)
3031 ;; RAW-LINK is the original link. Expand any
3032 ;; abbreviation in it.
3033 raw-link
(org-translate-link
3034 (org-link-expand-abbrev
3035 (org-match-string-no-properties 1))))
3036 ;; Determine TYPE of link and set PATH accordingly.
3039 ((or (file-name-absolute-p raw-link
)
3040 (string-match "\\`\\.\\.?/" raw-link
))
3041 (setq type
"file" path raw-link
))
3042 ;; Explicit type (http, irc, bbdb...). See `org-link-types'.
3043 ((string-match org-link-types-re raw-link
)
3044 (setq type
(match-string 1 raw-link
)
3045 ;; According to RFC 3986, extra whitespace should be
3046 ;; ignored when a URI is extracted.
3047 path
(replace-regexp-in-string
3048 "[ \t]*\n[ \t]*" "" (substring raw-link
(match-end 0)))))
3049 ;; Id type: PATH is the id.
3050 ((string-match "\\`id:\\([-a-f0-9]+\\)" raw-link
)
3051 (setq type
"id" path
(match-string 1 raw-link
)))
3052 ;; Code-ref type: PATH is the name of the reference.
3053 ((string-match "\\`(\\(.*\\))\\'" raw-link
)
3054 (setq type
"coderef" path
(match-string 1 raw-link
)))
3055 ;; Custom-id type: PATH is the name of the custom id.
3056 ((= (aref raw-link
0) ?
#)
3057 (setq type
"custom-id" path
(substring raw-link
1)))
3058 ;; Fuzzy type: Internal link either matches a target, an
3059 ;; headline name or nothing. PATH is the target or
3061 (t (setq type
"fuzzy" path raw-link
))))
3062 ;; Type 3: Plain link, e.g., http://orgmode.org
3063 ((looking-at org-plain-link-re
)
3064 (setq raw-link
(org-match-string-no-properties 0)
3065 type
(org-match-string-no-properties 1)
3066 link-end
(match-end 0)
3067 path
(org-match-string-no-properties 2)))
3068 ;; Type 4: Angular link, e.g., <http://orgmode.org>
3069 ((looking-at org-angle-link-re
)
3070 (setq raw-link
(buffer-substring-no-properties
3071 (match-beginning 1) (match-end 2))
3072 type
(org-match-string-no-properties 1)
3073 link-end
(match-end 0)
3074 path
(org-match-string-no-properties 2)))
3075 (t (throw 'no-object nil
)))
3076 ;; In any case, deduce end point after trailing white space from
3077 ;; LINK-END variable.
3079 (setq post-blank
(progn (goto-char link-end
) (skip-chars-forward " \t"))
3081 ;; Special "file" type link processing.
3082 (when (member type org-element-link-type-is-file
)
3083 ;; Extract opening application and search option.
3084 (cond ((string-match "^file\\+\\(.*\\)$" type
)
3085 (setq application
(match-string 1 type
)))
3086 ((not (string-match "^file" type
))
3087 (setq application type
)))
3088 (when (string-match "::\\(.*\\)\\'" path
)
3089 (setq search-option
(match-string 1 path
)
3090 path
(replace-match "" nil nil path
)))
3092 (when (and (file-name-absolute-p path
)
3093 (not (org-string-match-p "\\`[/~]/" path
)))
3094 (setq path
(concat "//" path
)))
3095 ;; Make sure TYPE always reports "file".
3100 :raw-link
(or raw-link path
)
3101 :application application
3102 :search-option search-option
3105 :contents-begin contents-begin
3106 :contents-end contents-end
3107 :post-blank post-blank
))))))
3109 (defun org-element-link-interpreter (link contents
)
3110 "Interpret LINK object as Org syntax.
3111 CONTENTS is the contents of the object, or nil."
3112 (let ((type (org-element-property :type link
))
3113 (raw-link (org-element-property :raw-link link
)))
3114 (if (string= type
"radio") raw-link
3117 (if contents
(format "[%s]" contents
) "")))))
3122 (defun org-element-macro-parser ()
3123 "Parse macro at point, if any.
3125 When at a macro, return a list whose car is `macro' and cdr
3126 a plist with `:key', `:args', `:begin', `:end', `:value' and
3127 `:post-blank' as keywords. Otherwise, return nil.
3129 Assume point is at the macro."
3131 (when (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
3132 (let ((begin (point))
3133 (key (downcase (org-match-string-no-properties 1)))
3134 (value (org-match-string-no-properties 0))
3135 (post-blank (progn (goto-char (match-end 0))
3136 (skip-chars-forward " \t")))
3138 (args (let ((args (org-match-string-no-properties 3)))
3139 (and args
(org-macro-extract-arguments args
)))))
3146 :post-blank post-blank
))))))
3148 (defun org-element-macro-interpreter (macro contents
)
3149 "Interpret MACRO object as Org syntax.
3151 (org-element-property :value macro
))
3156 (defun org-element-radio-target-parser ()
3157 "Parse radio target at point, if any.
3159 When at a radio target, return a list whose car is `radio-target'
3160 and cdr a plist with `:begin', `:end', `:contents-begin',
3161 `:contents-end', `:value' and `:post-blank' as keywords.
3162 Otherwise, return nil.
3164 Assume point is at the radio target."
3166 (when (looking-at org-radio-target-regexp
)
3167 (let ((begin (point))
3168 (contents-begin (match-beginning 1))
3169 (contents-end (match-end 1))
3170 (value (org-match-string-no-properties 1))
3171 (post-blank (progn (goto-char (match-end 0))
3172 (skip-chars-forward " \t")))
3177 :contents-begin contents-begin
3178 :contents-end contents-end
3179 :post-blank post-blank
3182 (defun org-element-radio-target-interpreter (target contents
)
3183 "Interpret TARGET object as Org syntax.
3184 CONTENTS is the contents of the object."
3185 (concat "<<<" contents
">>>"))
3188 ;;;; Statistics Cookie
3190 (defun org-element-statistics-cookie-parser ()
3191 "Parse statistics cookie at point, if any.
3193 When at a statistics cookie, return a list whose car is
3194 `statistics-cookie', and cdr a plist with `:begin', `:end',
3195 `:value' and `:post-blank' keywords. Otherwise, return nil.
3197 Assume point is at the beginning of the statistics-cookie."
3199 (when (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
3200 (let* ((begin (point))
3201 (value (buffer-substring-no-properties
3202 (match-beginning 0) (match-end 0)))
3203 (post-blank (progn (goto-char (match-end 0))
3204 (skip-chars-forward " \t")))
3206 (list 'statistics-cookie
3210 :post-blank post-blank
))))))
3212 (defun org-element-statistics-cookie-interpreter (statistics-cookie contents
)
3213 "Interpret STATISTICS-COOKIE object as Org syntax.
3215 (org-element-property :value statistics-cookie
))
3220 (defun org-element-strike-through-parser ()
3221 "Parse strike-through object at point, if any.
3223 When at a strike-through object, return a list whose car is
3224 `strike-through' and cdr is a plist with `:begin', `:end',
3225 `:contents-begin' and `:contents-end' and `:post-blank' keywords.
3226 Otherwise, return nil.
3228 Assume point is at the first plus sign marker."
3230 (unless (bolp) (backward-char 1))
3231 (when (looking-at org-emph-re
)
3232 (let ((begin (match-beginning 2))
3233 (contents-begin (match-beginning 4))
3234 (contents-end (match-end 4))
3235 (post-blank (progn (goto-char (match-end 2))
3236 (skip-chars-forward " \t")))
3238 (list 'strike-through
3241 :contents-begin contents-begin
3242 :contents-end contents-end
3243 :post-blank post-blank
))))))
3245 (defun org-element-strike-through-interpreter (strike-through contents
)
3246 "Interpret STRIKE-THROUGH object as Org syntax.
3247 CONTENTS is the contents of the object."
3248 (format "+%s+" contents
))
3253 (defun org-element-subscript-parser ()
3254 "Parse subscript at point, if any.
3256 When at a subscript object, return a list whose car is
3257 `subscript' and cdr a plist with `:begin', `:end',
3258 `:contents-begin', `:contents-end', `:use-brackets-p' and
3259 `:post-blank' as keywords. Otherwise, return nil.
3261 Assume point is at the underscore."
3263 (unless (bolp) (backward-char))
3264 (when (looking-at org-match-substring-regexp
)
3265 (let ((bracketsp (match-beginning 4))
3266 (begin (match-beginning 2))
3267 (contents-begin (or (match-beginning 4)
3268 (match-beginning 3)))
3269 (contents-end (or (match-end 4) (match-end 3)))
3270 (post-blank (progn (goto-char (match-end 0))
3271 (skip-chars-forward " \t")))
3276 :use-brackets-p bracketsp
3277 :contents-begin contents-begin
3278 :contents-end contents-end
3279 :post-blank post-blank
))))))
3281 (defun org-element-subscript-interpreter (subscript contents
)
3282 "Interpret SUBSCRIPT object as Org syntax.
3283 CONTENTS is the contents of the object."
3285 (if (org-element-property :use-brackets-p subscript
) "_{%s}" "_%s")
3291 (defun org-element-superscript-parser ()
3292 "Parse superscript at point, if any.
3294 When at a superscript object, return a list whose car is
3295 `superscript' and cdr a plist with `:begin', `:end',
3296 `:contents-begin', `:contents-end', `:use-brackets-p' and
3297 `:post-blank' as keywords. Otherwise, return nil.
3299 Assume point is at the caret."
3301 (unless (bolp) (backward-char))
3302 (when (looking-at org-match-substring-regexp
)
3303 (let ((bracketsp (match-beginning 4))
3304 (begin (match-beginning 2))
3305 (contents-begin (or (match-beginning 4)
3306 (match-beginning 3)))
3307 (contents-end (or (match-end 4) (match-end 3)))
3308 (post-blank (progn (goto-char (match-end 0))
3309 (skip-chars-forward " \t")))
3314 :use-brackets-p bracketsp
3315 :contents-begin contents-begin
3316 :contents-end contents-end
3317 :post-blank post-blank
))))))
3319 (defun org-element-superscript-interpreter (superscript contents
)
3320 "Interpret SUPERSCRIPT object as Org syntax.
3321 CONTENTS is the contents of the object."
3323 (if (org-element-property :use-brackets-p superscript
) "^{%s}" "^%s")
3329 (defun org-element-table-cell-parser ()
3330 "Parse table cell at point.
3331 Return a list whose car is `table-cell' and cdr is a plist
3332 containing `:begin', `:end', `:contents-begin', `:contents-end'
3333 and `:post-blank' keywords."
3334 (looking-at "[ \t]*\\(.*?\\)[ \t]*\\(?:|\\|$\\)")
3335 (let* ((begin (match-beginning 0))
3337 (contents-begin (match-beginning 1))
3338 (contents-end (match-end 1)))
3342 :contents-begin contents-begin
3343 :contents-end contents-end
3346 (defun org-element-table-cell-interpreter (table-cell contents
)
3347 "Interpret TABLE-CELL element as Org syntax.
3348 CONTENTS is the contents of the cell, or nil."
3349 (concat " " contents
" |"))
3354 (defun org-element-target-parser ()
3355 "Parse target at point, if any.
3357 When at a target, return a list whose car is `target' and cdr
3358 a plist with `:begin', `:end', `:value' and `:post-blank' as
3359 keywords. Otherwise, return nil.
3361 Assume point is at the target."
3363 (when (looking-at org-target-regexp
)
3364 (let ((begin (point))
3365 (value (org-match-string-no-properties 1))
3366 (post-blank (progn (goto-char (match-end 0))
3367 (skip-chars-forward " \t")))
3373 :post-blank post-blank
))))))
3375 (defun org-element-target-interpreter (target contents
)
3376 "Interpret TARGET object as Org syntax.
3378 (format "<<%s>>" (org-element-property :value target
)))
3383 (defconst org-element--timestamp-regexp
3384 (concat org-ts-regexp-both
3386 "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
3388 "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
3389 "Regexp matching any timestamp type object.")
3391 (defun org-element-timestamp-parser ()
3392 "Parse time stamp at point, if any.
3394 When at a time stamp, return a list whose car is `timestamp', and
3395 cdr a plist with `:type', `:raw-value', `:year-start',
3396 `:month-start', `:day-start', `:hour-start', `:minute-start',
3397 `:year-end', `:month-end', `:day-end', `:hour-end',
3398 `:minute-end', `:repeater-type', `:repeater-value',
3399 `:repeater-unit', `:warning-type', `:warning-value',
3400 `:warning-unit', `:begin', `:end' and `:post-blank' keywords.
3401 Otherwise, return nil.
3403 Assume point is at the beginning of the timestamp."
3404 (when (org-looking-at-p org-element--timestamp-regexp
)
3406 (let* ((begin (point))
3407 (activep (eq (char-after) ?
<))
3410 (looking-at "\\([<[]\\(%%\\)?.*?\\)[]>]\\(?:--\\([<[].*?[]>]\\)\\)?")
3411 (match-string-no-properties 0)))
3412 (date-start (match-string-no-properties 1))
3413 (date-end (match-string 3))
3414 (diaryp (match-beginning 2))
3415 (post-blank (progn (goto-char (match-end 0))
3416 (skip-chars-forward " \t")))
3421 "[012]?[0-9]:[0-5][0-9]\\(-\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)"
3423 (cons (string-to-number (match-string 2 date-start
))
3424 (string-to-number (match-string 3 date-start
)))))
3425 (type (cond (diaryp 'diary
)
3426 ((and activep
(or date-end time-range
)) 'active-range
)
3428 ((or date-end time-range
) 'inactive-range
)
3432 (string-match "\\([.+]?\\+\\)\\([0-9]+\\)\\([hdwmy]\\)"
3436 (let ((type (match-string 1 raw-value
)))
3437 (cond ((equal "++" type
) 'catch-up
)
3438 ((equal ".+" type
) 'restart
)
3440 :repeater-value
(string-to-number (match-string 2 raw-value
))
3442 (case (string-to-char (match-string 3 raw-value
))
3443 (?h
'hour
) (?d
'day
) (?w
'week
) (?m
'month
) (t 'year
)))))
3446 (string-match "\\(-\\)?-\\([0-9]+\\)\\([hdwmy]\\)" raw-value
)
3448 :warning-type
(if (match-string 1 raw-value
) 'first
'all
)
3449 :warning-value
(string-to-number (match-string 2 raw-value
))
3451 (case (string-to-char (match-string 3 raw-value
))
3452 (?h
'hour
) (?d
'day
) (?w
'week
) (?m
'month
) (t 'year
)))))
3453 year-start month-start day-start hour-start minute-start year-end
3454 month-end day-end hour-end minute-end
)
3455 ;; Parse date-start.
3457 (let ((date (org-parse-time-string date-start t
)))
3458 (setq year-start
(nth 5 date
)
3459 month-start
(nth 4 date
)
3460 day-start
(nth 3 date
)
3461 hour-start
(nth 2 date
)
3462 minute-start
(nth 1 date
))))
3463 ;; Compute date-end. It can be provided directly in time-stamp,
3464 ;; or extracted from time range. Otherwise, it defaults to the
3465 ;; same values as date-start.
3467 (let ((date (and date-end
(org-parse-time-string date-end t
))))
3468 (setq year-end
(or (nth 5 date
) year-start
)
3469 month-end
(or (nth 4 date
) month-start
)
3470 day-end
(or (nth 3 date
) day-start
)
3471 hour-end
(or (nth 2 date
) (car time-range
) hour-start
)
3472 minute-end
(or (nth 1 date
) (cdr time-range
) minute-start
))))
3474 (nconc (list :type type
3475 :raw-value raw-value
3476 :year-start year-start
3477 :month-start month-start
3478 :day-start day-start
3479 :hour-start hour-start
3480 :minute-start minute-start
3482 :month-end month-end
3485 :minute-end minute-end
3488 :post-blank post-blank
)
3492 (defun org-element-timestamp-interpreter (timestamp contents
)
3493 "Interpret TIMESTAMP object as Org syntax.
3495 (let* ((repeat-string
3497 (case (org-element-property :repeater-type timestamp
)
3498 (cumulate "+") (catch-up "++") (restart ".+"))
3499 (let ((val (org-element-property :repeater-value timestamp
)))
3500 (and val
(number-to-string val
)))
3501 (case (org-element-property :repeater-unit timestamp
)
3502 (hour "h") (day "d") (week "w") (month "m") (year "y"))))
3505 (case (org-element-property :warning-type timestamp
)
3508 (let ((val (org-element-property :warning-value timestamp
)))
3509 (and val
(number-to-string val
)))
3510 (case (org-element-property :warning-unit timestamp
)
3511 (hour "h") (day "d") (week "w") (month "m") (year "y"))))
3513 ;; Build an Org timestamp string from TIME. ACTIVEP is
3514 ;; non-nil when time stamp is active. If WITH-TIME-P is
3515 ;; non-nil, add a time part. HOUR-END and MINUTE-END
3516 ;; specify a time range in the timestamp. REPEAT-STRING is
3517 ;; the repeater string, if any.
3518 (lambda (time activep
&optional with-time-p hour-end minute-end
)
3519 (let ((ts (format-time-string
3520 (funcall (if with-time-p
'cdr
'car
)
3521 org-time-stamp-formats
)
3523 (when (and hour-end minute-end
)
3524 (string-match "[012]?[0-9]:[0-5][0-9]" ts
)
3527 (format "\\&-%02d:%02d" hour-end minute-end
)
3529 (unless activep
(setq ts
(format "[%s]" (substring ts
1 -
1))))
3530 (dolist (s (list repeat-string warning-string
))
3531 (when (org-string-nw-p s
)
3532 (setq ts
(concat (substring ts
0 -
1)
3535 (substring ts -
1)))))
3538 (type (org-element-property :type timestamp
)))
3541 (let* ((minute-start (org-element-property :minute-start timestamp
))
3542 (minute-end (org-element-property :minute-end timestamp
))
3543 (hour-start (org-element-property :hour-start timestamp
))
3544 (hour-end (org-element-property :hour-end timestamp
))
3545 (time-range-p (and hour-start hour-end minute-start minute-end
3546 (or (/= hour-start hour-end
)
3547 (/= minute-start minute-end
)))))
3553 (org-element-property :day-start timestamp
)
3554 (org-element-property :month-start timestamp
)
3555 (org-element-property :year-start timestamp
))
3557 (and hour-start minute-start
)
3558 (and time-range-p hour-end
)
3559 (and time-range-p minute-end
))))
3560 ((active-range inactive-range
)
3561 (let ((minute-start (org-element-property :minute-start timestamp
))
3562 (minute-end (org-element-property :minute-end timestamp
))
3563 (hour-start (org-element-property :hour-start timestamp
))
3564 (hour-end (org-element-property :hour-end timestamp
)))
3567 build-ts-string
(encode-time
3571 (org-element-property :day-start timestamp
)
3572 (org-element-property :month-start timestamp
)
3573 (org-element-property :year-start timestamp
))
3574 (eq type
'active-range
)
3575 (and hour-start minute-start
))
3577 (funcall build-ts-string
3581 (org-element-property :day-end timestamp
)
3582 (org-element-property :month-end timestamp
)
3583 (org-element-property :year-end timestamp
))
3584 (eq type
'active-range
)
3585 (and hour-end minute-end
)))))
3586 (otherwise (org-element-property :raw-value timestamp
)))))
3591 (defun org-element-underline-parser ()
3592 "Parse underline object at point, if any.
3594 When at an underline object, return a list whose car is
3595 `underline' and cdr is a plist with `:begin', `:end',
3596 `:contents-begin' and `:contents-end' and `:post-blank' keywords.
3597 Otherwise, return nil.
3599 Assume point is at the first underscore marker."
3601 (unless (bolp) (backward-char 1))
3602 (when (looking-at org-emph-re
)
3603 (let ((begin (match-beginning 2))
3604 (contents-begin (match-beginning 4))
3605 (contents-end (match-end 4))
3606 (post-blank (progn (goto-char (match-end 2))
3607 (skip-chars-forward " \t")))
3612 :contents-begin contents-begin
3613 :contents-end contents-end
3614 :post-blank post-blank
))))))
3616 (defun org-element-underline-interpreter (underline contents
)
3617 "Interpret UNDERLINE object as Org syntax.
3618 CONTENTS is the contents of the object."
3619 (format "_%s_" contents
))
3624 (defun org-element-verbatim-parser ()
3625 "Parse verbatim object at point, if any.
3627 When at a verbatim object, return a list whose car is `verbatim'
3628 and cdr is a plist with `:value', `:begin', `:end' and
3629 `:post-blank' keywords. Otherwise, return nil.
3631 Assume point is at the first equal sign marker."
3633 (unless (bolp) (backward-char 1))
3634 (when (looking-at org-emph-re
)
3635 (let ((begin (match-beginning 2))
3636 (value (org-match-string-no-properties 4))
3637 (post-blank (progn (goto-char (match-end 2))
3638 (skip-chars-forward " \t")))
3644 :post-blank post-blank
))))))
3646 (defun org-element-verbatim-interpreter (verbatim contents
)
3647 "Interpret VERBATIM object as Org syntax.
3649 (format "=%s=" (org-element-property :value verbatim
)))
3653 ;;; Parsing Element Starting At Point
3655 ;; `org-element--current-element' is the core function of this section.
3656 ;; It returns the Lisp representation of the element starting at
3659 ;; `org-element--current-element' makes use of special modes. They
3660 ;; are activated for fixed element chaining (e.g., `plain-list' >
3661 ;; `item') or fixed conditional element chaining (e.g., `headline' >
3662 ;; `section'). Special modes are: `first-section', `item',
3663 ;; `node-property', `section' and `table-row'.
3665 (defun org-element--current-element (limit &optional granularity mode structure
)
3666 "Parse the element starting at point.
3668 Return value is a list like (TYPE PROPS) where TYPE is the type
3669 of the element and PROPS a plist of properties associated to the
3672 Possible types are defined in `org-element-all-elements'.
3674 LIMIT bounds the search.
3676 Optional argument GRANULARITY determines the depth of the
3677 recursion. Allowed values are `headline', `greater-element',
3678 `element', `object' or nil. When it is broader than `object' (or
3679 nil), secondary values will not be parsed, since they only
3682 Optional argument MODE, when non-nil, can be either
3683 `first-section', `section', `planning', `item', `node-property'
3686 If STRUCTURE isn't provided but MODE is set to `item', it will be
3689 This function assumes point is always at the beginning of the
3690 element it has to parse."
3692 (let ((case-fold-search t
)
3693 ;; Determine if parsing depth allows for secondary strings
3694 ;; parsing. It only applies to elements referenced in
3695 ;; `org-element-secondary-value-alist'.
3696 (raw-secondary-p (and granularity
(not (eq granularity
'object
)))))
3700 (org-element-item-parser limit structure raw-secondary-p
))
3702 ((eq mode
'table-row
) (org-element-table-row-parser limit
))
3704 ((eq mode
'node-property
) (org-element-node-property-parser limit
))
3706 ((org-with-limited-levels (org-at-heading-p))
3707 (org-element-headline-parser limit raw-secondary-p
))
3708 ;; Sections (must be checked after headline).
3709 ((eq mode
'section
) (org-element-section-parser limit
))
3710 ((eq mode
'first-section
)
3711 (org-element-section-parser
3712 (or (save-excursion (org-with-limited-levels (outline-next-heading)))
3715 ((and (eq mode
'planning
) (looking-at org-planning-line-re
))
3716 (org-element-planning-parser limit
))
3718 ((and (memq mode
'(planning property-drawer
))
3719 (looking-at org-property-drawer-re
))
3720 (org-element-property-drawer-parser limit
))
3721 ;; When not at bol, point is at the beginning of an item or
3722 ;; a footnote definition: next item is always a paragraph.
3723 ((not (bolp)) (org-element-paragraph-parser limit
(list (point))))
3725 ((looking-at org-clock-line-re
) (org-element-clock-parser limit
))
3728 (org-element-inlinetask-parser limit raw-secondary-p
))
3729 ;; From there, elements can have affiliated keywords.
3730 (t (let ((affiliated (org-element--collect-affiliated-keywords limit
)))
3732 ;; Jumping over affiliated keywords put point off-limits.
3733 ;; Parse them as regular keywords.
3734 ((and (cdr affiliated
) (>= (point) limit
))
3735 (goto-char (car affiliated
))
3736 (org-element-keyword-parser limit nil
))
3737 ;; LaTeX Environment.
3738 ((looking-at org-element--latex-begin-environment
)
3739 (org-element-latex-environment-parser limit affiliated
))
3740 ;; Drawer and Property Drawer.
3741 ((looking-at org-drawer-regexp
)
3742 (org-element-drawer-parser limit affiliated
))
3744 ((looking-at "[ \t]*:\\( \\|$\\)")
3745 (org-element-fixed-width-parser limit affiliated
))
3746 ;; Inline Comments, Blocks, Babel Calls, Dynamic Blocks and
3748 ((looking-at "[ \t]*#")
3749 (goto-char (match-end 0))
3750 (cond ((looking-at "\\(?: \\|$\\)")
3752 (org-element-comment-parser limit affiliated
))
3753 ((looking-at "\\+BEGIN_\\(\\S-+\\)")
3755 (let ((parser (assoc (upcase (match-string 1))
3756 org-element-block-name-alist
)))
3757 (if parser
(funcall (cdr parser
) limit affiliated
)
3758 (org-element-special-block-parser limit affiliated
))))
3759 ((looking-at "\\+CALL:")
3761 (org-element-babel-call-parser limit affiliated
))
3762 ((looking-at "\\+BEGIN:? ")
3764 (org-element-dynamic-block-parser limit affiliated
))
3765 ((looking-at "\\+\\S-+:")
3767 (org-element-keyword-parser limit affiliated
))
3770 (org-element-paragraph-parser limit affiliated
))))
3771 ;; Footnote Definition.
3772 ((looking-at org-footnote-definition-re
)
3773 (org-element-footnote-definition-parser limit affiliated
))
3775 ((looking-at "[ \t]*-\\{5,\\}[ \t]*$")
3776 (org-element-horizontal-rule-parser limit affiliated
))
3779 (org-element-diary-sexp-parser limit affiliated
))
3781 ((org-at-table-p t
) (org-element-table-parser limit affiliated
))
3783 ((looking-at (org-item-re))
3784 (org-element-plain-list-parser
3786 (or structure
(org-element--list-struct limit
))))
3787 ;; Default element: Paragraph.
3788 (t (org-element-paragraph-parser limit affiliated
)))))))))
3791 ;; Most elements can have affiliated keywords. When looking for an
3792 ;; element beginning, we want to move before them, as they belong to
3793 ;; that element, and, in the meantime, collect information they give
3794 ;; into appropriate properties. Hence the following function.
3796 (defun org-element--collect-affiliated-keywords (limit)
3797 "Collect affiliated keywords from point down to LIMIT.
3799 Return a list whose CAR is the position at the first of them and
3800 CDR a plist of keywords and values and move point to the
3801 beginning of the first line after them.
3803 As a special case, if element doesn't start at the beginning of
3804 the line (e.g., a paragraph starting an item), CAR is current
3805 position of point and CDR is nil."
3806 (if (not (bolp)) (list (point))
3807 (let ((case-fold-search t
)
3809 ;; RESTRICT is the list of objects allowed in parsed
3811 (restrict (org-element-restriction 'keyword
))
3813 (while (and (< (point) limit
) (looking-at org-element--affiliated-re
))
3814 (let* ((raw-kwd (upcase (match-string 1)))
3815 ;; Apply translation to RAW-KWD. From there, KWD is
3816 ;; the official keyword.
3817 (kwd (or (cdr (assoc raw-kwd
3818 org-element-keyword-translation-alist
))
3820 ;; Find main value for any keyword.
3824 (buffer-substring-no-properties
3825 (match-end 0) (line-end-position)))))
3826 ;; PARSEDP is non-nil when keyword should have its
3828 (parsedp (member kwd org-element-parsed-keywords
))
3829 ;; If KWD is a dual keyword, find its secondary
3830 ;; value. Maybe parse it.
3831 (dualp (member kwd org-element-dual-keywords
))
3834 (let ((sec (org-match-string-no-properties 2)))
3835 (if (or (not sec
) (not parsedp
)) sec
3836 (org-element--parse-objects
3837 (match-beginning 2) (match-end 2) nil restrict
)))))
3838 ;; Attribute a property name to KWD.
3839 (kwd-sym (and kwd
(intern (concat ":" (downcase kwd
))))))
3840 ;; Now set final shape for VALUE.
3843 (org-element--parse-objects
3845 (progn (end-of-line) (skip-chars-backward " \t") (point))
3848 (setq value
(and (or value dual-value
) (cons value dual-value
))))
3849 (when (or (member kwd org-element-multiple-keywords
)
3850 ;; Attributes can always appear on multiple lines.
3851 (string-match "^ATTR_" kwd
))
3852 (setq value
(cons value
(plist-get output kwd-sym
))))
3853 ;; Eventually store the new value in OUTPUT.
3854 (setq output
(plist-put output kwd-sym value
))
3855 ;; Move to next keyword.
3857 ;; If affiliated keywords are orphaned: move back to first one.
3858 ;; They will be parsed as a paragraph.
3859 (when (looking-at "[ \t]*$") (goto-char origin
) (setq output nil
))
3861 (cons origin output
))))
3867 ;; The two major functions here are `org-element-parse-buffer', which
3868 ;; parses Org syntax inside the current buffer, taking into account
3869 ;; region, narrowing, or even visibility if specified, and
3870 ;; `org-element-parse-secondary-string', which parses objects within
3873 ;; The (almost) almighty `org-element-map' allows to apply a function
3874 ;; on elements or objects matching some type, and accumulate the
3875 ;; resulting values. In an export situation, it also skips unneeded
3876 ;; parts of the parse tree.
3878 (defun org-element-parse-buffer (&optional granularity visible-only
)
3879 "Recursively parse the buffer and return structure.
3880 If narrowing is in effect, only parse the visible part of the
3883 Optional argument GRANULARITY determines the depth of the
3884 recursion. It can be set to the following symbols:
3886 `headline' Only parse headlines.
3887 `greater-element' Don't recurse into greater elements excepted
3888 headlines and sections. Thus, elements
3889 parsed are the top-level ones.
3890 `element' Parse everything but objects and plain text.
3891 `object' Parse the complete buffer (default).
3893 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3896 An element or an objects is represented as a list with the
3897 pattern (TYPE PROPERTIES CONTENTS), where :
3899 TYPE is a symbol describing the element or object. See
3900 `org-element-all-elements' and `org-element-all-objects' for an
3901 exhaustive list of such symbols. One can retrieve it with
3902 `org-element-type' function.
3904 PROPERTIES is the list of attributes attached to the element or
3905 object, as a plist. Although most of them are specific to the
3906 element or object type, all types share `:begin', `:end',
3907 `:post-blank' and `:parent' properties, which respectively
3908 refer to buffer position where the element or object starts,
3909 ends, the number of white spaces or blank lines after it, and
3910 the element or object containing it. Properties values can be
3911 obtained by using `org-element-property' function.
3913 CONTENTS is a list of elements, objects or raw strings
3914 contained in the current element or object, when applicable.
3915 One can access them with `org-element-contents' function.
3917 The Org buffer has `org-data' as type and nil as properties.
3918 `org-element-map' function can be used to find specific elements
3919 or objects within the parse tree.
3921 This function assumes that current major mode is `org-mode'."
3923 (goto-char (point-min))
3924 (org-skip-whitespace)
3925 (org-element--parse-elements
3926 (point-at-bol) (point-max)
3927 ;; Start in `first-section' mode so text before the first
3928 ;; headline belongs to a section.
3929 'first-section nil granularity visible-only
(list 'org-data nil
))))
3931 (defun org-element-parse-secondary-string (string restriction
&optional parent
)
3932 "Recursively parse objects in STRING and return structure.
3934 RESTRICTION is a symbol limiting the object types that will be
3937 Optional argument PARENT, when non-nil, is the element or object
3938 containing the secondary string. It is used to set correctly
3939 `:parent' property within the string."
3940 (let ((local-variables (buffer-local-variables)))
3942 (dolist (v local-variables
)
3944 (if (symbolp v
) (makunbound v
)
3945 (org-set-local (car v
) (cdr v
)))))
3947 (restore-buffer-modified-p nil
)
3948 (let ((secondary (org-element--parse-objects
3949 (point-min) (point-max) nil restriction
)))
3951 (dolist (o secondary
) (org-element-put-property o
:parent parent
)))
3954 (defun org-element-map
3955 (data types fun
&optional info first-match no-recursion with-affiliated
)
3956 "Map a function on selected elements or objects.
3958 DATA is a parse tree, an element, an object, a string, or a list
3959 of such constructs. TYPES is a symbol or list of symbols of
3960 elements or objects types (see `org-element-all-elements' and
3961 `org-element-all-objects' for a complete list of types). FUN is
3962 the function called on the matching element or object. It has to
3963 accept one argument: the element or object itself.
3965 When optional argument INFO is non-nil, it should be a plist
3966 holding export options. In that case, parts of the parse tree
3967 not exportable according to that property list will be skipped.
3969 When optional argument FIRST-MATCH is non-nil, stop at the first
3970 match for which FUN doesn't return nil, and return that value.
3972 Optional argument NO-RECURSION is a symbol or a list of symbols
3973 representing elements or objects types. `org-element-map' won't
3974 enter any recursive element or object whose type belongs to that
3975 list. Though, FUN can still be applied on them.
3977 When optional argument WITH-AFFILIATED is non-nil, FUN will also
3978 apply to matching objects within parsed affiliated keywords (see
3979 `org-element-parsed-keywords').
3981 Nil values returned from FUN do not appear in the results.
3987 Assuming TREE is a variable containing an Org buffer parse tree,
3988 the following example will return a flat list of all `src-block'
3989 and `example-block' elements in it:
3991 \(org-element-map tree '(example-block src-block) #'identity)
3993 The following snippet will find the first headline with a level
3994 of 1 and a \"phone\" tag, and will return its beginning position:
3996 \(org-element-map tree 'headline
3998 \(and (= (org-element-property :level hl) 1)
3999 \(member \"phone\" (org-element-property :tags hl))
4000 \(org-element-property :begin hl)))
4003 The next example will return a flat list of all `plain-list' type
4004 elements in TREE that are not a sub-list themselves:
4006 \(org-element-map tree 'plain-list #'identity nil nil 'plain-list)
4008 Eventually, this example will return a flat list of all `bold'
4009 type objects containing a `latex-snippet' type object, even
4010 looking into captions:
4012 \(org-element-map tree 'bold
4014 \(and (org-element-map b 'latex-snippet #'identity nil t) b))
4016 ;; Ensure TYPES and NO-RECURSION are a list, even of one element.
4017 (let* ((types (if (listp types
) types
(list types
)))
4018 (no-recursion (if (listp no-recursion
) no-recursion
4019 (list no-recursion
)))
4020 ;; Recursion depth is determined by --CATEGORY.
4023 (let ((category 'greater-elements
)
4024 (all-objects (cons 'plain-text org-element-all-objects
)))
4025 (dolist (type types category
)
4026 (cond ((memq type all-objects
)
4027 ;; If one object is found, the function has to
4028 ;; recurse into every object.
4029 (throw 'found
'objects
))
4030 ((not (memq type org-element-greater-elements
))
4031 ;; If one regular element is found, the
4032 ;; function has to recurse, at least, into
4033 ;; every element it encounters.
4034 (and (not (eq category
'elements
))
4035 (setq category
'elements
))))))))
4040 ;; Recursively walk DATA. INFO, if non-nil, is a plist
4041 ;; holding contextual information.
4042 (let ((--type (org-element-type --data
)))
4045 ;; Ignored element in an export context.
4046 ((and info
(memq --data
(plist-get info
:ignore-list
))))
4047 ;; List of elements or objects.
4048 ((not --type
) (mapc --walk-tree --data
))
4049 ;; Unconditionally enter parse trees.
4050 ((eq --type
'org-data
)
4051 (mapc --walk-tree
(org-element-contents --data
)))
4053 ;; Check if TYPE is matching among TYPES. If so,
4054 ;; apply FUN to --DATA and accumulate return value
4055 ;; into --ACC (or exit if FIRST-MATCH is non-nil).
4056 (when (memq --type types
)
4057 (let ((result (funcall fun --data
)))
4058 (cond ((not result
))
4059 (first-match (throw '--map-first-match result
))
4060 (t (push result --acc
)))))
4061 ;; If --DATA has a secondary string that can contain
4062 ;; objects with their type among TYPES, look into it.
4063 (when (and (eq --category
'objects
) (not (stringp --data
)))
4065 (assq --type org-element-secondary-value-alist
)))
4067 (funcall --walk-tree
4068 (org-element-property (cdr sec-prop
) --data
)))))
4069 ;; If --DATA has any parsed affiliated keywords and
4070 ;; WITH-AFFILIATED is non-nil, look for objects in
4072 (when (and with-affiliated
4073 (eq --category
'objects
)
4074 (memq --type org-element-all-elements
))
4075 (dolist (kwd-pair org-element--parsed-properties-alist
)
4076 (let ((kwd (car kwd-pair
))
4077 (value (org-element-property (cdr kwd-pair
) --data
)))
4078 ;; Pay attention to the type of parsed keyword.
4079 ;; In particular, preserve order for multiple
4083 ((member kwd org-element-dual-keywords
)
4084 (if (member kwd org-element-multiple-keywords
)
4085 (dolist (line (reverse value
))
4086 (funcall --walk-tree
(cdr line
))
4087 (funcall --walk-tree
(car line
)))
4088 (funcall --walk-tree
(cdr value
))
4089 (funcall --walk-tree
(car value
))))
4090 ((member kwd org-element-multiple-keywords
)
4091 (mapc --walk-tree
(reverse value
)))
4092 (t (funcall --walk-tree value
))))))
4093 ;; Determine if a recursion into --DATA is possible.
4095 ;; --TYPE is explicitly removed from recursion.
4096 ((memq --type no-recursion
))
4097 ;; --DATA has no contents.
4098 ((not (org-element-contents --data
)))
4099 ;; Looking for greater elements but --DATA is simply
4100 ;; an element or an object.
4101 ((and (eq --category
'greater-elements
)
4102 (not (memq --type org-element-greater-elements
))))
4103 ;; Looking for elements but --DATA is an object.
4104 ((and (eq --category
'elements
)
4105 (memq --type org-element-all-objects
)))
4106 ;; In any other case, map contents.
4107 (t (mapc --walk-tree
(org-element-contents --data
))))))))))
4108 (catch '--map-first-match
4109 (funcall --walk-tree data
)
4110 ;; Return value in a proper order.
4112 (put 'org-element-map
'lisp-indent-function
2)
4114 ;; The following functions are internal parts of the parser.
4116 ;; The first one, `org-element--parse-elements' acts at the element's
4119 ;; The second one, `org-element--parse-objects' applies on all objects
4120 ;; of a paragraph or a secondary string. It calls
4121 ;; `org-element--object-lex' to find the next object in the current
4124 (defsubst org-element--next-mode
(type parentp
)
4125 "Return next special mode according to TYPE, or nil.
4126 TYPE is a symbol representing the type of an element or object
4127 containing next element if PARENTP is non-nil, or before it
4128 otherwise. Modes can be either `first-section', `item',
4129 `node-property', `planning', `property-drawer', `section',
4130 `table-row' or nil."
4135 (property-drawer 'node-property
)
4140 (node-property 'node-property
)
4141 (planning 'property-drawer
)
4142 (table-row 'table-row
))))
4144 (defun org-element--parse-elements
4145 (beg end mode structure granularity visible-only acc
)
4146 "Parse elements between BEG and END positions.
4148 MODE prioritizes some elements over the others. It can be set to
4149 `first-section', `section', `planning', `item', `node-property'
4152 When value is `item', STRUCTURE will be used as the current list
4155 GRANULARITY determines the depth of the recursion. See
4156 `org-element-parse-buffer' for more information.
4158 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
4161 Elements are accumulated into ACC."
4164 ;; Visible only: skip invisible parts at the beginning of the
4166 (when (and visible-only
(org-invisible-p2))
4167 (goto-char (min (1+ (org-find-visible)) end
)))
4168 ;; When parsing only headlines, skip any text before first one.
4169 (when (and (eq granularity
'headline
) (not (org-at-heading-p)))
4170 (org-with-limited-levels (outline-next-heading)))
4172 (while (< (point) end
)
4173 ;; Find current element's type and parse it accordingly to
4175 (let* ((element (org-element--current-element
4176 end granularity mode structure
))
4177 (type (org-element-type element
))
4178 (cbeg (org-element-property :contents-begin element
)))
4179 (goto-char (org-element-property :end element
))
4180 ;; Visible only: skip invisible parts between siblings.
4181 (when (and visible-only
(org-invisible-p2))
4182 (goto-char (min (1+ (org-find-visible)) end
)))
4183 ;; Fill ELEMENT contents by side-effect.
4185 ;; If element has no contents, don't modify it.
4187 ;; Greater element: parse it between `contents-begin' and
4188 ;; `contents-end'. Make sure GRANULARITY allows the
4189 ;; recursion, or ELEMENT is a headline, in which case going
4190 ;; inside is mandatory, in order to get sub-level headings.
4191 ((and (memq type org-element-greater-elements
)
4192 (or (memq granularity
'(element object nil
))
4193 (and (eq granularity
'greater-element
)
4195 (eq type
'headline
)))
4196 (org-element--parse-elements
4197 cbeg
(org-element-property :contents-end element
)
4198 ;; Possibly switch to a special mode.
4199 (org-element--next-mode type t
)
4200 (and (memq type
'(item plain-list
))
4201 (org-element-property :structure element
))
4202 granularity visible-only element
))
4203 ;; ELEMENT has contents. Parse objects inside, if
4204 ;; GRANULARITY allows it.
4205 ((memq granularity
'(object nil
))
4206 (org-element--parse-objects
4207 cbeg
(org-element-property :contents-end element
) element
4208 (org-element-restriction type
))))
4209 (org-element-adopt-elements acc element
)
4211 (setq mode
(org-element--next-mode type nil
))))
4215 (defun org-element--object-lex (restriction)
4216 "Return next object in current buffer or nil.
4217 RESTRICTION is a list of object types, as symbols, that should be
4218 looked after. This function assumes that the buffer is narrowed
4219 to an appropriate container (e.g., a paragraph)."
4220 (if (memq 'table-cell restriction
) (org-element-table-cell-parser)
4222 (let ((limit (and org-target-link-regexp
4224 (or (bolp) (backward-char))
4225 (re-search-forward org-target-link-regexp nil t
))
4226 (match-beginning 1)))
4228 (while (and (not found
)
4229 (re-search-forward org-element--object-regexp limit t
))
4230 (goto-char (match-beginning 0))
4231 (let ((result (match-string 0)))
4234 ((eq (compare-strings result nil nil
"call_" nil nil t
) t
)
4235 (and (memq 'inline-babel-call restriction
)
4236 (org-element-inline-babel-call-parser)))
4237 ((eq (compare-strings result nil nil
"src_" nil nil t
) t
)
4238 (and (memq 'inline-src-block restriction
)
4239 (org-element-inline-src-block-parser)))
4242 (?^
(and (memq 'superscript restriction
)
4243 (org-element-superscript-parser)))
4244 (?_
(or (and (memq 'subscript restriction
)
4245 (org-element-subscript-parser))
4246 (and (memq 'underline restriction
)
4247 (org-element-underline-parser))))
4248 (?
* (and (memq 'bold restriction
)
4249 (org-element-bold-parser)))
4250 (?
/ (and (memq 'italic restriction
)
4251 (org-element-italic-parser)))
4252 (?~
(and (memq 'code restriction
)
4253 (org-element-code-parser)))
4254 (?
= (and (memq 'verbatim restriction
)
4255 (org-element-verbatim-parser)))
4256 (?
+ (and (memq 'strike-through restriction
)
4257 (org-element-strike-through-parser)))
4258 (?
@ (and (memq 'export-snippet restriction
)
4259 (org-element-export-snippet-parser)))
4260 (?
{ (and (memq 'macro restriction
)
4261 (org-element-macro-parser)))
4262 (?$
(and (memq 'latex-fragment restriction
)
4263 (org-element-latex-fragment-parser)))
4265 (if (eq (aref result
1) ?
<)
4266 (or (and (memq 'radio-target restriction
)
4267 (org-element-radio-target-parser))
4268 (and (memq 'target restriction
)
4269 (org-element-target-parser)))
4270 (or (and (memq 'timestamp restriction
)
4271 (org-element-timestamp-parser))
4272 (and (memq 'link restriction
)
4273 (org-element-link-parser)))))
4275 (if (eq (aref result
1) ?
\\)
4276 (and (memq 'line-break restriction
)
4277 (org-element-line-break-parser))
4278 (or (and (memq 'entity restriction
)
4279 (org-element-entity-parser))
4280 (and (memq 'latex-fragment restriction
)
4281 (org-element-latex-fragment-parser)))))
4283 (if (eq (aref result
1) ?\
[)
4284 (and (memq 'link restriction
)
4285 (org-element-link-parser))
4286 (or (and (memq 'footnote-reference restriction
)
4287 (org-element-footnote-reference-parser))
4288 (and (memq 'timestamp restriction
)
4289 (org-element-timestamp-parser))
4290 (and (memq 'statistics-cookie restriction
)
4291 (org-element-statistics-cookie-parser)))))
4292 ;; This is probably a plain link.
4293 (otherwise (and (or (memq 'link restriction
)
4294 (memq 'plain-link restriction
))
4295 (org-element-link-parser)))))))
4296 (or (eobp) (forward-char))))
4299 ((and limit
(memq 'link restriction
))
4300 (goto-char limit
) (org-element-link-parser)))))))
4302 (defun org-element--parse-objects (beg end acc restriction
)
4303 "Parse objects between BEG and END and return recursive structure.
4305 Objects are accumulated in ACC.
4307 RESTRICTION is a list of object successors which are allowed in
4308 the current object."
4311 (narrow-to-region beg end
)
4312 (goto-char (point-min))
4314 (while (and (not (eobp))
4315 (setq next-object
(org-element--object-lex restriction
)))
4316 ;; 1. Text before any object. Untabify it.
4317 (let ((obj-beg (org-element-property :begin next-object
)))
4318 (unless (= (point) obj-beg
)
4320 (org-element-adopt-elements
4322 (replace-regexp-in-string
4323 "\t" (make-string tab-width ?
)
4324 (buffer-substring-no-properties (point) obj-beg
))))))
4326 (let ((obj-end (org-element-property :end next-object
))
4327 (cont-beg (org-element-property :contents-begin next-object
)))
4328 ;; Fill contents of NEXT-OBJECT by side-effect, if it has
4329 ;; a recursive type.
4331 (memq (car next-object
) org-element-recursive-objects
))
4332 (org-element--parse-objects
4333 cont-beg
(org-element-property :contents-end next-object
)
4334 next-object
(org-element-restriction next-object
)))
4335 (setq acc
(org-element-adopt-elements acc next-object
))
4336 (goto-char obj-end
))))
4337 ;; 3. Text after last object. Untabify it.
4340 (org-element-adopt-elements
4342 (replace-regexp-in-string
4343 "\t" (make-string tab-width ?
)
4344 (buffer-substring-no-properties (point) end
)))))
4350 ;;; Towards A Bijective Process
4352 ;; The parse tree obtained with `org-element-parse-buffer' is really
4353 ;; a snapshot of the corresponding Org buffer. Therefore, it can be
4354 ;; interpreted and expanded into a string with canonical Org syntax.
4355 ;; Hence `org-element-interpret-data'.
4357 ;; The function relies internally on
4358 ;; `org-element--interpret-affiliated-keywords'.
4361 (defun org-element-interpret-data (data &optional pseudo-objects
)
4362 "Interpret DATA as Org syntax.
4364 DATA is a parse tree, an element, an object or a secondary string
4367 Optional argument PSEUDO-OBJECTS is a list of symbols defining
4368 new types that should be treated as objects. An unknown type not
4369 belonging to this list is seen as a pseudo-element instead. Both
4370 pseudo-objects and pseudo-elements are transparent entities, i.e.
4371 only their contents are interpreted.
4373 Return Org syntax as a string."
4374 (org-element--interpret-data-1 data nil pseudo-objects
))
4376 (defun org-element--interpret-data-1 (data parent pseudo-objects
)
4377 "Interpret DATA as Org syntax.
4379 DATA is a parse tree, an element, an object or a secondary string
4380 to interpret. PARENT is used for recursive calls. It contains
4381 the element or object containing data, or nil. PSEUDO-OBJECTS
4382 are list of symbols defining new element or object types.
4383 Unknown types that don't belong to this list are treated as
4384 pseudo-elements instead.
4386 Return Org syntax as a string."
4387 (let* ((type (org-element-type data
))
4388 ;; Find interpreter for current object or element. If it
4389 ;; doesn't exist (e.g. this is a pseudo object or element),
4390 ;; return contents, if any.
4392 (let ((fun (intern (format "org-element-%s-interpreter" type
))))
4393 (if (fboundp fun
) fun
(lambda (data contents
) contents
))))
4396 ;; Secondary string.
4400 (org-element--interpret-data-1 obj parent pseudo-objects
))
4402 ;; Full Org document.
4403 ((eq type
'org-data
)
4406 (org-element--interpret-data-1 obj parent pseudo-objects
))
4407 (org-element-contents data
) ""))
4408 ;; Plain text: return it.
4409 ((stringp data
) data
)
4410 ;; Element or object without contents.
4411 ((not (org-element-contents data
)) (funcall interpret data nil
))
4412 ;; Element or object with contents.
4414 (funcall interpret data
4415 ;; Recursively interpret contents.
4418 (org-element--interpret-data-1 obj data pseudo-objects
))
4419 (org-element-contents
4420 (if (not (memq type
'(paragraph verse-block
)))
4422 ;; Fix indentation of elements containing
4423 ;; objects. We ignore `table-row' elements
4424 ;; as they are one line long anyway.
4425 (org-element-normalize-contents
4427 ;; When normalizing first paragraph of an
4428 ;; item or a footnote-definition, ignore
4429 ;; first line's indentation.
4430 (and (eq type
'paragraph
)
4431 (equal data
(car (org-element-contents parent
)))
4432 (memq (org-element-type parent
)
4433 '(footnote-definition item
))))))
4435 (if (memq type
'(org-data plain-text nil
)) results
4436 ;; Build white spaces. If no `:post-blank' property is
4437 ;; specified, assume its value is 0.
4438 (let ((post-blank (or (org-element-property :post-blank data
) 0)))
4439 (if (or (memq type org-element-all-objects
)
4440 (memq type pseudo-objects
))
4441 (concat results
(make-string post-blank ?\s
))
4443 (org-element--interpret-affiliated-keywords data
)
4444 (org-element-normalize-string results
)
4445 (make-string post-blank ?
\n)))))))
4447 (defun org-element--interpret-affiliated-keywords (element)
4448 "Return ELEMENT's affiliated keywords as Org syntax.
4449 If there is no affiliated keyword, return the empty string."
4450 (let ((keyword-to-org
4454 (when (member key org-element-dual-keywords
)
4455 (setq dual
(cdr value
) value
(car value
)))
4458 (format "[%s]" (org-element-interpret-data dual
)))
4460 (if (member key org-element-parsed-keywords
)
4461 (org-element-interpret-data value
)
4466 (let ((value (org-element-property prop element
))
4467 (keyword (upcase (substring (symbol-name prop
) 1))))
4469 (if (or (member keyword org-element-multiple-keywords
)
4470 ;; All attribute keywords can have multiple lines.
4471 (string-match "^ATTR_" keyword
))
4472 (mapconcat (lambda (line) (funcall keyword-to-org keyword line
))
4475 (funcall keyword-to-org keyword value
)))))
4476 ;; List all ELEMENT's properties matching an attribute line or an
4477 ;; affiliated keyword, but ignore translated keywords since they
4478 ;; cannot belong to the property list.
4479 (loop for prop in
(nth 1 element
) by
'cddr
4480 when
(let ((keyword (upcase (substring (symbol-name prop
) 1))))
4481 (or (string-match "^ATTR_" keyword
)
4483 (member keyword org-element-affiliated-keywords
)
4485 org-element-keyword-translation-alist
)))))
4489 ;; Because interpretation of the parse tree must return the same
4490 ;; number of blank lines between elements and the same number of white
4491 ;; space after objects, some special care must be given to white
4494 ;; The first function, `org-element-normalize-string', ensures any
4495 ;; string different from the empty string will end with a single
4496 ;; newline character.
4498 ;; The second function, `org-element-normalize-contents', removes
4499 ;; global indentation from the contents of the current element.
4501 (defun org-element-normalize-string (s)
4502 "Ensure string S ends with a single newline character.
4504 If S isn't a string return it unchanged. If S is the empty
4505 string, return it. Otherwise, return a new string with a single
4506 newline character at its end."
4508 ((not (stringp s
)) s
)
4510 (t (and (string-match "\\(\n[ \t]*\\)*\\'" s
)
4511 (replace-match "\n" nil nil s
)))))
4513 (defun org-element-normalize-contents (element &optional ignore-first
)
4514 "Normalize plain text in ELEMENT's contents.
4516 ELEMENT must only contain plain text and objects.
4518 If optional argument IGNORE-FIRST is non-nil, ignore first line's
4519 indentation to compute maximal common indentation.
4521 Return the normalized element that is element with global
4522 indentation removed from its contents. The function assumes that
4523 indentation is not done with TAB characters."
4524 (let* ((min-ind most-positive-fixnum
)
4525 find-min-ind
; For byte-compiler.
4527 ;; Return minimal common indentation within BLOB. This is
4528 ;; done by walking recursively BLOB and updating MIN-IND
4529 ;; along the way. FIRST-FLAG is non-nil when the first
4530 ;; string hasn't been seen yet. It is required as this
4531 ;; string is the only one whose indentation doesn't happen
4532 ;; after a newline character.
4533 (lambda (blob first-flag
)
4534 (dolist (object (org-element-contents blob
))
4535 (when (and first-flag
(stringp object
))
4536 (setq first-flag nil
)
4537 (string-match "\\` *" object
)
4538 (let ((len (match-end 0)))
4539 ;; An indentation of zero means no string will be
4540 ;; modified. Quit the process.
4541 (if (zerop len
) (throw 'zero
(setq min-ind
0))
4542 (setq min-ind
(min len min-ind
)))))
4545 (dolist (line (cdr (org-split-string object
" *\n")))
4546 (unless (string= line
"")
4547 (setq min-ind
(min (org-get-indentation line
) min-ind
)))))
4548 ((memq (org-element-type object
) org-element-recursive-objects
)
4549 (funcall find-min-ind object first-flag
)))))))
4550 ;; Find minimal indentation in ELEMENT.
4551 (catch 'zero
(funcall find-min-ind element
(not ignore-first
)))
4552 (if (or (zerop min-ind
) (= min-ind most-positive-fixnum
)) element
4553 ;; Build ELEMENT back, replacing each string with the same
4554 ;; string minus common indentation.
4555 (let* (build ; For byte compiler.
4558 (lambda (blob first-flag
)
4559 ;; Return BLOB with all its strings indentation
4560 ;; shortened from MIN-IND white spaces. FIRST-FLAG
4561 ;; is non-nil when the first string hasn't been seen
4566 (when (and first-flag
(stringp object
))
4567 (setq first-flag nil
)
4569 (replace-regexp-in-string
4570 (format "\\` \\{%d\\}" min-ind
)
4574 (replace-regexp-in-string
4575 (format "\n \\{%d\\}" min-ind
) "\n" object
))
4576 ((memq (org-element-type object
)
4577 org-element-recursive-objects
)
4578 (funcall build object first-flag
))
4580 (org-element-contents blob
)))
4582 (funcall build element
(not ignore-first
))))))
4588 ;; Implement a caching mechanism for `org-element-at-point' and
4589 ;; `org-element-context', which see.
4591 ;; A single public function is provided: `org-element-cache-reset'.
4593 ;; Cache is enabled by default, but can be disabled globally with
4594 ;; `org-element-use-cache'. `org-element-cache-sync-idle-time',
4595 ;; org-element-cache-sync-duration' and `org-element-cache-sync-break'
4596 ;; can be tweaked to control caching behaviour.
4598 ;; Internally, parsed elements are stored in an AVL tree,
4599 ;; `org-element--cache'. This tree is updated lazily: whenever
4600 ;; a change happens to the buffer, a synchronization request is
4601 ;; registered in `org-element--cache-sync-requests' (see
4602 ;; `org-element--cache-submit-request'). During idle time, requests
4603 ;; are processed by `org-element--cache-sync'. Synchronization also
4604 ;; happens when an element is required from the cache. In this case,
4605 ;; the process stops as soon as the needed element is up-to-date.
4607 ;; A synchronization request can only apply on a synchronized part of
4608 ;; the cache. Therefore, the cache is updated at least to the
4609 ;; location where the new request applies. Thus, requests are ordered
4610 ;; from left to right and all elements starting before the first
4611 ;; request are correct. This property is used by functions like
4612 ;; `org-element--cache-find' to retrieve elements in the part of the
4613 ;; cache that can be trusted.
4615 ;; A request applies to every element, starting from its original
4616 ;; location (or key, see below). When a request is processed, it
4617 ;; moves forward and may collide the next one. In this case, both
4618 ;; requests are merged into a new one that starts from that element.
4619 ;; As a consequence, the whole synchronization complexity does not
4620 ;; depend on the number of pending requests, but on the number of
4621 ;; elements the very first request will be applied on.
4623 ;; Elements cannot be accessed through their beginning position, which
4624 ;; may or may not be up-to-date. Instead, each element in the tree is
4625 ;; associated to a key, obtained with `org-element--cache-key'. This
4626 ;; mechanism is robust enough to preserve total order among elements
4627 ;; even when the tree is only partially synchronized.
4629 ;; Objects contained in an element are stored in a hash table,
4630 ;; `org-element--cache-objects'.
4633 (defvar org-element-use-cache t
4634 "Non nil when Org parser should cache its results.
4635 This is mostly for debugging purpose.")
4637 (defvar org-element-cache-sync-idle-time
0.6
4638 "Length, in seconds, of idle time before syncing cache.")
4640 (defvar org-element-cache-sync-duration
(seconds-to-time 0.04)
4641 "Maximum duration, as a time value, for a cache synchronization.
4642 If the synchronization is not over after this delay, the process
4643 pauses and resumes after `org-element-cache-sync-break'
4646 (defvar org-element-cache-sync-break
(seconds-to-time 0.3)
4647 "Duration, as a time value, of the pause between synchronizations.
4648 See `org-element-cache-sync-duration' for more information.")
4653 (defvar org-element--cache nil
4654 "AVL tree used to cache elements.
4655 Each node of the tree contains an element. Comparison is done
4656 with `org-element--cache-compare'. This cache is used in
4657 `org-element-at-point'.")
4659 (defvar org-element--cache-objects nil
4660 "Hash table used as to cache objects.
4661 Key is an element, as returned by `org-element-at-point', and
4662 value is an alist where each association is:
4664 \(PARENT COMPLETEP . OBJECTS)
4666 where PARENT is an element or object, COMPLETEP is a boolean,
4667 non-nil when all direct children of parent are already cached and
4668 OBJECTS is a list of such children, as objects, from farthest to
4671 In the following example, \\alpha, bold object and \\beta are
4672 contained within a paragraph
4676 If the paragraph is completely parsed, OBJECTS-DATA will be
4678 \((PARAGRAPH t BOLD-OBJECT ENTITY-OBJECT)
4679 \(BOLD-OBJECT t ENTITY-OBJECT))
4681 whereas in a partially parsed paragraph, it could be
4683 \((PARAGRAPH nil ENTITY-OBJECT))
4685 This cache is used in `org-element-context'.")
4687 (defvar org-element--cache-sync-requests nil
4688 "List of pending synchronization requests.
4690 A request is a vector with the following pattern:
4692 \[NEXT BEG END OFFSET OUTREACH PARENT PHASE]
4694 Processing a synchronization request consists of three phases:
4696 0. Delete modified elements,
4697 1. Fill missing area in cache,
4698 2. Shift positions and re-parent elements after the changes.
4700 During phase 0, NEXT is the key of the first element to be
4701 removed, BEG and END is buffer position delimiting the
4702 modifications. Elements starting between them (inclusive) are
4703 removed and so are those contained within OUTREACH. PARENT, when
4704 non-nil, is the parent of the first element to be removed.
4706 During phase 1, NEXT is the key of the next known element in
4707 cache and BEG its beginning position. Parse buffer between that
4708 element and the one before it in order to determine the parent of
4709 the next element. Set PARENT to the element containing NEXT.
4711 During phase 2, NEXT is the key of the next element to shift in
4712 the parse tree. All elements starting from this one have their
4713 properties relatives to buffer positions shifted by integer
4714 OFFSET and, if they belong to element PARENT, are adopted by it.
4716 PHASE specifies the phase number, as an integer.")
4718 (defvar org-element--cache-sync-timer nil
4719 "Timer used for cache synchronization.")
4721 (defvar org-element--cache-sync-keys nil
4722 "Hash table used to store keys during synchronization.
4723 See `org-element--cache-key' for more information.")
4725 (defsubst org-element--cache-key
(element)
4726 "Return a unique key for ELEMENT in cache tree.
4728 Keys are used to keep a total order among elements in the cache.
4729 Comparison is done with `org-element--cache-key-less-p'.
4731 When no synchronization is taking place, a key is simply the
4732 beginning position of the element, or that position plus one in
4733 the case of an first item (respectively row) in
4734 a list (respectively a table).
4736 During a synchronization, the key is the one the element had when
4737 the cache was synchronized for the last time. Elements added to
4738 cache during the synchronization get a new key generated with
4739 `org-element--cache-generate-key'.
4741 Such keys are stored in `org-element--cache-sync-keys'. The hash
4742 table is cleared once the synchronization is complete."
4743 (or (gethash element org-element--cache-sync-keys
)
4744 (let* ((begin (org-element-property :begin element
))
4745 ;; Increase beginning position of items (respectively
4746 ;; table rows) by one, so the first item can get
4747 ;; a different key from its parent list (respectively
4749 (key (if (memq (org-element-type element
) '(item table-row
))
4752 (if org-element--cache-sync-requests
4753 (puthash element key org-element--cache-sync-keys
)
4756 (defun org-element--cache-generate-key (lower upper
)
4757 "Generate a key between LOWER and UPPER.
4759 LOWER and UPPER are integers or lists, possibly empty.
4761 If LOWER and UPPER are equals, return LOWER. Otherwise, return
4762 a unique key, as an integer or a list of integers, according to
4763 the following rules:
4765 - LOWER and UPPER are compared level-wise until values differ.
4767 - If, at a given level, LOWER and UPPER differ from more than
4768 2, the new key shares all the levels above with LOWER and
4769 gets a new level. Its value is the mean between LOWER and
4772 \(1 2) + (1 4) --> (1 3)
4774 - If LOWER has no value to compare with, it is assumed that its
4775 value is `most-negative-fixnum'. E.g.,
4783 where m is `most-negative-fixnum'. Likewise, if UPPER is
4784 short of levels, the current value is `most-positive-fixnum'.
4786 - If they differ from only one, the new key inherits from
4787 current LOWER level and fork it at the next level. E.g.,
4795 where M is `most-positive-fixnum'.
4797 - If the key is only one level long, it is returned as an
4800 \(1 2) + (3 2) --> 2
4802 When they are not equals, the function assumes that LOWER is
4803 lesser than UPPER, per `org-element--cache-key-less-p'."
4804 (if (equal lower upper
) lower
4805 (let ((lower (if (integerp lower
) (list lower
) lower
))
4806 (upper (if (integerp upper
) (list upper
) upper
))
4810 (let ((min (or (car lower
) most-negative-fixnum
))
4811 (max (cond (skip-upper most-positive-fixnum
)
4813 (t most-positive-fixnum
))))
4814 (if (< (1+ min
) max
)
4815 (let ((mean (+ (ash min -
1) (ash max -
1) (logand min max
1))))
4816 (throw 'exit
(if key
(nreverse (cons mean key
)) mean
)))
4817 (when (and (< min max
) (not skip-upper
))
4818 ;; When at a given level, LOWER and UPPER differ from
4819 ;; 1, ignore UPPER altogether. Instead create a key
4820 ;; between LOWER and the greatest key with the same
4821 ;; prefix as LOWER so far.
4822 (setq skip-upper t
))
4824 (setq lower
(cdr lower
) upper
(cdr upper
)))))))))
4826 (defsubst org-element--cache-key-less-p
(a b
)
4827 "Non-nil if key A is less than key B.
4828 A and B are either integers or lists of integers, as returned by
4829 `org-element--cache-key'."
4830 (if (integerp a
) (if (integerp b
) (< a b
) (<= a
(car b
)))
4831 (if (integerp b
) (< (car a
) b
)
4834 (cond ((car-less-than-car a b
) (throw 'exit t
))
4835 ((car-less-than-car b a
) (throw 'exit nil
))
4836 (t (setq a
(cdr a
) b
(cdr b
)))))
4837 ;; If A is empty, either keys are equal (B is also empty) and
4838 ;; we return nil, or A is lesser than B (B is longer) and we
4839 ;; return a non-nil value.
4841 ;; If A is not empty, B is necessarily empty and A is greater
4842 ;; than B (A is longer). Therefore, return nil.
4843 (and (null a
) b
)))))
4845 (defun org-element--cache-compare (a b
)
4846 "Non-nil when element A is located before element B."
4847 (org-element--cache-key-less-p (org-element--cache-key a
)
4848 (org-element--cache-key b
)))
4850 (defsubst org-element--cache-root
()
4851 "Return root value in cache.
4852 This function assumes `org-element--cache' is a valid AVL tree."
4853 (avl-tree--node-left (avl-tree--dummyroot org-element--cache
)))
4858 (defsubst org-element--cache-active-p
()
4859 "Non-nil when cache is active in current buffer."
4860 (and org-element-use-cache
4861 (or (derived-mode-p 'org-mode
) orgstruct-mode
)))
4863 (defun org-element--cache-find (pos &optional side
)
4864 "Find element in cache starting at POS or before.
4866 POS refers to a buffer position.
4868 When optional argument SIDE is non-nil, the function checks for
4869 elements starting at or past POS instead. If SIDE is `both', the
4870 function returns a cons cell where car is the first element
4871 starting at or before POS and cdr the first element starting
4874 The function can only find elements in the synchronized part of
4876 (let ((limit (and org-element--cache-sync-requests
4877 (aref (car org-element--cache-sync-requests
) 0)))
4878 (node (org-element--cache-root))
4881 (let* ((element (avl-tree--node-data node
))
4882 (begin (org-element-property :begin element
)))
4885 (not (org-element--cache-key-less-p
4886 (org-element--cache-key element
) limit
)))
4887 (setq node
(avl-tree--node-left node
)))
4890 node
(avl-tree--node-left node
)))
4893 node
(avl-tree--node-right node
)))
4894 ;; We found an element in cache starting at POS. If `side'
4895 ;; is `both' we also want the next one in order to generate
4896 ;; a key in-between.
4898 ;; If the element is the first row or item in a table or
4899 ;; a plain list, we always return the table or the plain
4902 ;; In any other case, we return the element found.
4904 (setq lower element
)
4905 (setq node
(avl-tree--node-right node
)))
4906 ((and (memq (org-element-type element
) '(item table-row
))
4907 (let ((parent (org-element-property :parent element
)))
4908 (and (= (org-element-property :begin element
)
4909 (org-element-property :contents-begin parent
))
4918 (both (cons lower upper
))
4920 (otherwise upper
))))
4922 (defun org-element--cache-put (element &optional data
)
4923 "Store ELEMENT in current buffer's cache, if allowed.
4924 When optional argument DATA is non-nil, assume is it object data
4925 relative to ELEMENT and store it in the objects cache."
4926 (cond ((not (org-element--cache-active-p)) nil
)
4928 (when org-element--cache-sync-requests
4929 ;; During synchronization, first build an appropriate key
4930 ;; for the new element so `avl-tree-enter' can insert it at
4931 ;; the right spot in the cache.
4932 (let ((keys (org-element--cache-find
4933 (org-element-property :begin element
) 'both
)))
4935 (org-element--cache-generate-key
4936 (and (car keys
) (org-element--cache-key (car keys
)))
4937 (cond ((cdr keys
) (org-element--cache-key (cdr keys
)))
4938 (org-element--cache-sync-requests
4939 (aref (car org-element--cache-sync-requests
) 0))))
4940 org-element--cache-sync-keys
)))
4941 (avl-tree-enter org-element--cache element
))
4942 ;; Headlines are not stored in cache, so objects in titles are
4943 ;; not stored either.
4944 ((eq (org-element-type element
) 'headline
) nil
)
4945 (t (puthash element data org-element--cache-objects
))))
4947 (defsubst org-element--cache-remove
(element)
4948 "Remove ELEMENT from cache.
4949 Assume ELEMENT belongs to cache and that a cache is active."
4950 (avl-tree-delete org-element--cache element
)
4951 (remhash element org-element--cache-objects
))
4954 ;;;; Synchronization
4956 (defsubst org-element--cache-set-timer
(buffer)
4957 "Set idle timer for cache synchronization in BUFFER."
4958 (when org-element--cache-sync-timer
4959 (cancel-timer org-element--cache-sync-timer
))
4960 (setq org-element--cache-sync-timer
4961 (run-with-idle-timer
4962 (let ((idle (current-idle-time)))
4963 (if idle
(time-add idle org-element-cache-sync-break
)
4964 org-element-cache-sync-idle-time
))
4966 #'org-element--cache-sync
4969 (defsubst org-element--cache-interrupt-p
(time-limit)
4970 "Non-nil when synchronization process should be interrupted.
4971 TIME-LIMIT is a time value or nil."
4973 (or (input-pending-p)
4974 (time-less-p time-limit
(current-time)))))
4976 (defsubst org-element--cache-shift-positions
(element offset
&optional props
)
4977 "Shift ELEMENT properties relative to buffer positions by OFFSET.
4979 Properties containing buffer positions are `:begin', `:end',
4980 `:contents-begin', `:contents-end' and `:structure'. When
4981 optional argument PROPS is a list of keywords, only shift
4982 properties provided in that list.
4984 Properties are modified by side-effect."
4985 (let ((properties (nth 1 element
)))
4986 ;; Shift `:structure' property for the first plain list only: it
4987 ;; is the only one that really matters and it prevents from
4988 ;; shifting it more than once.
4989 (when (and (or (not props
) (memq :structure props
))
4990 (eq (org-element-type element
) 'plain-list
)
4991 (not (eq (org-element-type (plist-get properties
:parent
))
4993 (dolist (item (plist-get properties
:structure
))
4994 (incf (car item
) offset
)
4995 (incf (nth 6 item
) offset
)))
4996 (dolist (key '(:begin
:contents-begin
:contents-end
:end
:post-affiliated
))
4997 (let ((value (and (or (not props
) (memq key props
))
4998 (plist-get properties key
))))
4999 (and value
(plist-put properties key
(+ offset value
)))))))
5001 (defun org-element--cache-sync (buffer &optional threshold future-change
)
5002 "Synchronize cache with recent modification in BUFFER.
5004 When optional argument THRESHOLD is non-nil, do the
5005 synchronization for all elements starting before or at threshold,
5006 then exit. Otherwise, synchronize cache for as long as
5007 `org-element-cache-sync-duration' or until Emacs leaves idle
5010 FUTURE-CHANGE, when non-nil, is a buffer position where changes
5011 not registered yet in the cache are going to happen. It is used
5012 in `org-element--cache-submit-request', where cache is partially
5013 updated before current modification are actually submitted."
5014 (when (buffer-live-p buffer
)
5015 (with-current-buffer buffer
5016 (let ((inhibit-quit t
) request next
)
5017 (when org-element--cache-sync-timer
5018 (cancel-timer org-element--cache-sync-timer
))
5020 (while org-element--cache-sync-requests
5021 (setq request
(car org-element--cache-sync-requests
)
5022 next
(nth 1 org-element--cache-sync-requests
))
5023 (org-element--cache-process-request
5025 (and next
(aref next
0))
5027 (and (not threshold
)
5028 (time-add (current-time)
5029 org-element-cache-sync-duration
))
5031 ;; Request processed. Merge current and next offsets and
5032 ;; transfer ending position.
5034 (incf (aref next
3) (aref request
3))
5035 (aset next
2 (aref request
2)))
5036 (setq org-element--cache-sync-requests
5037 (cdr org-element--cache-sync-requests
))))
5038 ;; If more requests are awaiting, set idle timer accordingly.
5039 ;; Otherwise, reset keys.
5040 (if org-element--cache-sync-requests
5041 (org-element--cache-set-timer buffer
)
5042 (clrhash org-element--cache-sync-keys
))))))
5044 (defun org-element--cache-process-request
5045 (request next threshold time-limit future-change
)
5046 "Process synchronization REQUEST for all entries before NEXT.
5048 REQUEST is a vector, built by `org-element--cache-submit-request'.
5050 NEXT is a cache key, as returned by `org-element--cache-key'.
5052 When non-nil, THRESHOLD is a buffer position. Synchronization
5053 stops as soon as a shifted element begins after it.
5055 When non-nil, TIME-LIMIT is a time value. Synchronization stops
5056 after this time or when Emacs exits idle state.
5058 When non-nil, FUTURE-CHANGE is a buffer position where changes
5059 not registered yet in the cache are going to happen. See
5060 `org-element--cache-submit-request' for more information.
5062 Throw `interrupt' if the process stops before completing the
5065 (when (= (aref request
6) 0)
5068 ;; Delete all elements starting after BEG, but not after buffer
5069 ;; position END or past element with key NEXT.
5071 ;; At each iteration, we start again at tree root since
5072 ;; a deletion modifies structure of the balanced tree.
5074 (let ((beg (aref request
0))
5075 (end (aref request
2))
5076 (outreach (aref request
4)))
5078 (when (org-element--cache-interrupt-p time-limit
)
5079 (throw 'interrupt nil
))
5080 ;; Find first element in cache with key BEG or after it.
5081 (let ((node (org-element--cache-root)) data data-key
)
5083 (let* ((element (avl-tree--node-data node
))
5084 (key (org-element--cache-key element
)))
5086 ((org-element--cache-key-less-p key beg
)
5087 (setq node
(avl-tree--node-right node
)))
5088 ((org-element--cache-key-less-p beg key
)
5091 node
(avl-tree--node-left node
)))
5092 (t (setq data element
5096 (let ((pos (org-element-property :begin data
)))
5097 (if (if (or (not next
)
5098 (org-element--cache-key-less-p data-key next
))
5101 (while (and up
(not (eq up outreach
)))
5102 (setq up
(org-element-property :parent up
)))
5104 (org-element--cache-remove data
)
5105 (aset request
0 data-key
)
5106 (aset request
1 pos
)
5108 (throw 'end-phase nil
)))
5109 ;; No element starting after modifications left in
5110 ;; cache: further processing is futile.
5111 (throw 'quit t
)))))))
5112 (when (= (aref request
6) 1)
5115 ;; Phase 0 left a hole in the cache. Some elements after it
5116 ;; could have parents within. For example, in the following
5126 ;; if we remove a blank line between "item" and "Paragraph1",
5127 ;; everything down to "Paragraph2" is removed from cache. But
5128 ;; the paragraph now belongs to the list, and its `:parent'
5129 ;; property no longer is accurate.
5131 ;; Therefore we need to parse again elements in the hole, or at
5132 ;; least in its last section, so that we can re-parent
5133 ;; subsequent elements, during phase 2.
5135 ;; Note that we only need to get the parent from the first
5136 ;; element in cache after the hole.
5138 ;; When next key is lesser or equal to the current one, delegate
5139 ;; phase 1 processing to next request in order to preserve key
5140 ;; order among requests.
5141 (let ((key (aref request
0)))
5142 (when (and next
(not (org-element--cache-key-less-p key next
)))
5143 (let ((next-request (nth 1 org-element--cache-sync-requests
)))
5144 (aset next-request
0 key
)
5145 (aset next-request
1 (aref request
1))
5146 (aset next-request
6 1))
5148 ;; Next element will start at its beginning position plus
5149 ;; offset, since it hasn't been shifted yet. Therefore, LIMIT
5150 ;; contains the real beginning position of the first element to
5151 ;; shift and re-parent.
5152 (let ((limit (+ (aref request
1) (aref request
3))))
5153 (cond ((and threshold
(> limit threshold
)) (throw 'interrupt nil
))
5154 ((and future-change
(>= limit future-change
))
5155 ;; Changes are going to happen around this element and
5156 ;; they will trigger another phase 1 request. Skip the
5160 (let ((parent (org-element--parse-to limit t time-limit
)))
5161 (aset request
5 parent
)
5162 (aset request
6 2))))))
5165 ;; Shift all elements starting from key START, but before NEXT, by
5166 ;; OFFSET, and re-parent them when appropriate.
5168 ;; Elements are modified by side-effect so the tree structure
5171 ;; Once THRESHOLD, if any, is reached, or once there is an input
5172 ;; pending, exit. Before leaving, the current synchronization
5173 ;; request is updated.
5174 (let ((start (aref request
0))
5175 (offset (aref request
3))
5176 (parent (aref request
5))
5177 (node (org-element--cache-root))
5181 ;; No re-parenting nor shifting planned: request is over.
5182 (when (and (not parent
) (zerop offset
)) (throw 'quit t
))
5184 (let* ((data (avl-tree--node-data node
))
5185 (key (org-element--cache-key data
)))
5186 (if (and leftp
(avl-tree--node-left node
)
5187 (not (org-element--cache-key-less-p key start
)))
5188 (progn (push node stack
)
5189 (setq node
(avl-tree--node-left node
)))
5190 (unless (org-element--cache-key-less-p key start
)
5191 ;; We reached NEXT. Request is complete.
5192 (when (equal key next
) (throw 'quit t
))
5193 ;; Handle interruption request. Update current request.
5194 (when (or exit-flag
(org-element--cache-interrupt-p time-limit
))
5195 (aset request
0 key
)
5196 (aset request
5 parent
)
5197 (throw 'interrupt nil
))
5199 (unless (zerop offset
)
5200 (org-element--cache-shift-positions data offset
)
5201 ;; Shift associated objects data, if any.
5202 (dolist (object-data (gethash data org-element--cache-objects
))
5203 (dolist (object (cddr object-data
))
5204 (org-element--cache-shift-positions object offset
))))
5205 (let ((begin (org-element-property :begin data
)))
5206 ;; Update PARENT and re-parent DATA, only when
5207 ;; necessary. Propagate new structures for lists.
5209 (<= (org-element-property :end parent
) begin
))
5210 (setq parent
(org-element-property :parent parent
)))
5211 (cond ((and (not parent
) (zerop offset
)) (throw 'quit nil
))
5213 (let ((p (org-element-property :parent data
)))
5215 (< (org-element-property :begin p
)
5216 (org-element-property :begin parent
)))))
5217 (org-element-put-property data
:parent parent
)
5218 (let ((s (org-element-property :structure parent
)))
5219 (when (and s
(org-element-property :structure data
))
5220 (org-element-put-property data
:structure s
)))))
5221 ;; Cache is up-to-date past THRESHOLD. Request
5223 (when (and threshold
(> begin threshold
)) (setq exit-flag t
))))
5224 (setq node
(if (setq leftp
(avl-tree--node-right node
))
5225 (avl-tree--node-right node
)
5227 ;; We reached end of tree: synchronization complete.
5230 (defun org-element--parse-to (pos &optional syncp time-limit
)
5231 "Parse elements in current section, down to POS.
5233 Start parsing from the closest between the last known element in
5234 cache or headline above. Return the smallest element containing
5237 When optional argument SYNCP is non-nil, return the parent of the
5238 element containing POS instead. In that case, it is also
5239 possible to provide TIME-LIMIT, which is a time value specifying
5240 when the parsing should stop. The function throws `interrupt' if
5241 the process stopped before finding the expected result."
5243 (org-with-wide-buffer
5245 (let* ((cached (and (org-element--cache-active-p)
5246 (org-element--cache-find pos nil
)))
5247 (begin (org-element-property :begin cached
))
5250 ;; Nothing in cache before point: start parsing from first
5251 ;; element following headline above, or first element in
5254 (when (org-with-limited-levels (outline-previous-heading))
5255 (setq mode
'planning
)
5257 (skip-chars-forward " \r\t\n")
5258 (beginning-of-line))
5259 ;; Cache returned exact match: return it.
5261 (throw 'exit
(if syncp
(org-element-property :parent cached
) cached
)))
5262 ;; There's a headline between cached value and POS: cached
5263 ;; value is invalid. Start parsing from first element
5264 ;; following the headline.
5265 ((re-search-backward
5266 (org-with-limited-levels org-outline-regexp-bol
) begin t
)
5268 (skip-chars-forward " \r\t\n")
5270 (setq mode
'planning
))
5271 ;; Check if CACHED or any of its ancestors contain point.
5273 ;; If there is such an element, we inspect it in order to know
5274 ;; if we return it or if we need to parse its contents.
5275 ;; Otherwise, we just start parsing from current location,
5276 ;; which is right after the top-most element containing
5279 ;; As a special case, if POS is at the end of the buffer, we
5280 ;; want to return the innermost element ending there.
5282 ;; Also, if we find an ancestor and discover that we need to
5283 ;; parse its contents, make sure we don't start from
5284 ;; `:contents-begin', as we would otherwise go past CACHED
5285 ;; again. Instead, in that situation, we will resume parsing
5286 ;; from NEXT, which is located after CACHED or its higher
5287 ;; ancestor not containing point.
5290 (pos (if (= (point-max) pos
) (1- pos
) pos
)))
5291 (goto-char (or (org-element-property :contents-begin cached
) begin
))
5292 (while (let ((end (org-element-property :end up
)))
5295 (setq up
(org-element-property :parent up
)))))
5297 ((eobp) (setq element up
))
5298 (t (setq element up next
(point)))))))
5299 ;; Parse successively each element until we reach POS.
5300 (let ((end (or (org-element-property :end element
)
5302 (org-with-limited-levels (outline-next-heading))
5307 (cond ((= (point) pos
) (throw 'exit parent
))
5308 ((org-element--cache-interrupt-p time-limit
)
5309 (throw 'interrupt nil
))))
5311 (setq element
(org-element--current-element
5313 (org-element-property :structure parent
)))
5314 (org-element-put-property element
:parent parent
)
5315 (org-element--cache-put element
))
5316 (let ((elem-end (org-element-property :end element
))
5317 (type (org-element-type element
)))
5319 ;; Skip any element ending before point. Also skip
5320 ;; element ending at point (unless it is also the end of
5321 ;; buffer) since we're sure that another element begins
5323 ((and (<= elem-end pos
) (/= (point-max) elem-end
))
5324 (goto-char elem-end
)
5325 (setq mode
(org-element--next-mode type nil
)))
5326 ;; A non-greater element contains point: return it.
5327 ((not (memq type org-element-greater-elements
))
5328 (throw 'exit element
))
5329 ;; Otherwise, we have to decide if ELEMENT really
5330 ;; contains POS. In that case we start parsing from
5331 ;; contents' beginning.
5333 ;; If POS is at contents' beginning but it is also at
5334 ;; the beginning of the first item in a list or a table.
5335 ;; In that case, we need to create an anchor for that
5336 ;; list or table, so return it.
5338 ;; Also, if POS is at the end of the buffer, no element
5339 ;; can start after it, but more than one may end there.
5340 ;; Arbitrarily, we choose to return the innermost of
5342 ((let ((cbeg (org-element-property :contents-begin element
))
5343 (cend (org-element-property :contents-end element
)))
5348 (not (memq type
'(plain-list table
)))))
5350 (and (= cend pos
) (= (point-max) pos
)))))
5351 (goto-char (or next cbeg
))
5353 mode
(org-element--next-mode type t
)
5356 ;; Otherwise, return ELEMENT as it is the smallest
5357 ;; element containing POS.
5358 (t (throw 'exit element
))))
5359 (setq element nil
)))))))
5362 ;;;; Staging Buffer Changes
5364 (defconst org-element--cache-sensitive-re
5366 org-outline-regexp-bol
"\\|"
5369 "#\\+\\(?:BEGIN[:_]\\|END\\(?:_\\|:?[ \t]*$\\)\\)" "\\|"
5370 ;; LaTeX environments.
5371 "\\\\\\(?:begin{[A-Za-z0-9*]+}\\|end{[A-Za-z0-9*]+}[ \t]*$\\)" "\\|"
5373 ":\\(?:\\w\\|[-_]\\)+:[ \t]*$"
5375 "Regexp matching a sensitive line, structure wise.
5376 A sensitive line is a headline, inlinetask, block, drawer, or
5377 latex-environment boundary. When such a line is modified,
5378 structure changes in the document may propagate in the whole
5379 section, possibly making cache invalid.")
5381 (defvar org-element--cache-change-warning nil
5382 "Non-nil when a sensitive line is about to be changed.
5383 It is a symbol among nil, t and `headline'.")
5385 (defun org-element--cache-before-change (beg end
)
5386 "Request extension of area going to be modified if needed.
5387 BEG and END are the beginning and end of the range of changed
5388 text. See `before-change-functions' for more information."
5389 (when (org-element--cache-active-p)
5390 (org-with-wide-buffer
5393 (let ((bottom (save-excursion (goto-char end
) (line-end-position))))
5394 (setq org-element--cache-change-warning
5396 (if (and (org-with-limited-levels (org-at-heading-p))
5397 (= (line-end-position) bottom
))
5399 (let ((case-fold-search t
))
5401 org-element--cache-sensitive-re bottom t
)))))))))
5403 (defun org-element--cache-after-change (beg end pre
)
5404 "Update buffer modifications for current buffer.
5405 BEG and END are the beginning and end of the range of changed
5406 text, and the length in bytes of the pre-change text replaced by
5407 that range. See `after-change-functions' for more information."
5408 (when (org-element--cache-active-p)
5409 (org-with-wide-buffer
5414 (bottom (save-excursion (goto-char end
) (line-end-position))))
5415 ;; Determine if modified area needs to be extended, according
5416 ;; to both previous and current state. We make a special
5417 ;; case for headline editing: if a headline is modified but
5418 ;; not removed, do not extend.
5419 (when (case org-element--cache-change-warning
5422 (not (and (org-with-limited-levels (org-at-heading-p))
5423 (= (line-end-position) bottom
))))
5425 (let ((case-fold-search t
))
5427 org-element--cache-sensitive-re bottom t
))))
5428 ;; Effectively extend modified area.
5429 (org-with-limited-levels
5430 (setq top
(progn (goto-char top
)
5431 (when (outline-previous-heading) (forward-line))
5433 (setq bottom
(progn (goto-char bottom
)
5434 (if (outline-next-heading) (1- (point))
5436 ;; Store synchronization request.
5437 (let ((offset (- end beg pre
)))
5438 (org-element--cache-submit-request top
(- bottom offset
) offset
)))))
5439 ;; Activate a timer to process the request during idle time.
5440 (org-element--cache-set-timer (current-buffer))))
5442 (defun org-element--cache-for-removal (beg end offset
)
5443 "Return first element to remove from cache.
5445 BEG and END are buffer positions delimiting buffer modifications.
5446 OFFSET is the size of the changes.
5448 Returned element is usually the first element in cache containing
5449 any position between BEG and END. As an exception, greater
5450 elements around the changes that are robust to contents
5451 modifications are preserved and updated according to the
5453 (let* ((elements (org-element--cache-find (1- beg
) 'both
))
5454 (before (car elements
))
5455 (after (cdr elements
)))
5456 (if (not before
) after
5460 (if (and (memq (org-element-type up
)
5461 '(center-block drawer dynamic-block
5462 quote-block special-block
))
5463 (let ((cbeg (org-element-property :contents-begin up
)))
5466 (> (org-element-property :contents-end up
) end
))))
5467 ;; UP is a robust greater element containing changes.
5468 ;; We only need to extend its ending boundaries.
5469 (org-element--cache-shift-positions
5470 up offset
'(:contents-end
:end
))
5472 (when robust-flag
(setq robust-flag nil
)))
5473 (setq up
(org-element-property :parent up
)))
5474 ;; We're at top level element containing ELEMENT: if it's
5475 ;; altered by buffer modifications, it is first element in
5476 ;; cache to be removed. Otherwise, that first element is the
5479 ;; As a special case, do not remove BEFORE if it is a robust
5480 ;; container for current changes.
5481 (if (or (< (org-element-property :end before
) beg
) robust-flag
) after
5484 (defun org-element--cache-submit-request (beg end offset
)
5485 "Submit a new cache synchronization request for current buffer.
5486 BEG and END are buffer positions delimiting the minimal area
5487 where cache data should be removed. OFFSET is the size of the
5488 change, as an integer."
5489 (let ((next (car org-element--cache-sync-requests
))
5490 delete-to delete-from
)
5492 (zerop (aref next
6))
5493 (> (setq delete-to
(+ (aref next
2) (aref next
3))) end
)
5494 (<= (setq delete-from
(aref next
1)) end
))
5495 ;; Current changes can be merged with first sync request: we
5496 ;; can save a partial cache synchronization.
5498 (incf (aref next
3) offset
)
5499 ;; If last change happened within area to be removed, extend
5500 ;; boundaries of robust parents, if any. Otherwise, find
5501 ;; first element to remove and update request accordingly.
5502 (if (> beg delete-from
)
5503 (let ((up (aref next
5)))
5505 (org-element--cache-shift-positions
5506 up offset
'(:contents-end
:end
))
5507 (setq up
(org-element-property :parent up
))))
5508 (let ((first (org-element--cache-for-removal beg delete-to offset
)))
5510 (aset next
0 (org-element--cache-key first
))
5511 (aset next
1 (org-element-property :begin first
))
5512 (aset next
5 (org-element-property :parent first
))))))
5513 ;; Ensure cache is correct up to END. Also make sure that NEXT,
5514 ;; if any, is no longer a 0-phase request, thus ensuring that
5515 ;; phases are properly ordered. We need to provide OFFSET as
5516 ;; optional parameter since current modifications are not known
5517 ;; yet to the otherwise correct part of the cache (i.e, before
5518 ;; the first request).
5519 (when next
(org-element--cache-sync (current-buffer) end beg
))
5520 (let ((first (org-element--cache-for-removal beg end offset
)))
5522 (push (let ((beg (org-element-property :begin first
))
5523 (key (org-element--cache-key first
)))
5525 ;; When changes happen before the first known
5526 ;; element, re-parent and shift the rest of the
5528 ((> beg end
) (vector key beg nil offset nil nil
1))
5529 ;; Otherwise, we find the first non robust
5530 ;; element containing END. All elements between
5531 ;; FIRST and this one are to be removed.
5533 ;; Among them, some could be located outside the
5534 ;; synchronized part of the cache, in which case
5535 ;; comparing buffer positions to find them is
5536 ;; useless. Instead, we store the element
5537 ;; containing them in the request itself. All
5538 ;; its children will be removed.
5539 ((let ((first-end (org-element-property :end first
)))
5540 (and (> first-end end
)
5541 (vector key beg first-end offset first
5542 (org-element-property :parent first
) 0))))
5544 (let* ((element (org-element--cache-find end
))
5545 (end (org-element-property :end element
))
5547 (while (and (setq up
(org-element-property :parent up
))
5548 (>= (org-element-property :begin up
) beg
))
5549 (setq end
(org-element-property :end up
)
5551 (vector key beg end offset element
5552 (org-element-property :parent first
) 0)))))
5553 org-element--cache-sync-requests
)
5554 ;; No element to remove. No need to re-parent either.
5555 ;; Simply shift additional elements, if any, by OFFSET.
5556 (when org-element--cache-sync-requests
5557 (incf (aref (car org-element--cache-sync-requests
) 3) offset
)))))))
5560 ;;;; Public Functions
5563 (defun org-element-cache-reset (&optional all
)
5564 "Reset cache in current buffer.
5565 When optional argument ALL is non-nil, reset cache in all Org
5568 (dolist (buffer (if all
(buffer-list) (list (current-buffer))))
5569 (with-current-buffer buffer
5570 (when (org-element--cache-active-p)
5571 (org-set-local 'org-element--cache
5572 (avl-tree-create #'org-element--cache-compare
))
5573 (org-set-local 'org-element--cache-objects
(make-hash-table :test
#'eq
))
5574 (org-set-local 'org-element--cache-sync-keys
5575 (make-hash-table :weakness
'key
:test
#'eq
))
5576 (org-set-local 'org-element--cache-change-warning nil
)
5577 (org-set-local 'org-element--cache-sync-requests nil
)
5578 (org-set-local 'org-element--cache-sync-timer nil
)
5579 (add-hook 'before-change-functions
5580 #'org-element--cache-before-change nil t
)
5581 (add-hook 'after-change-functions
5582 #'org-element--cache-after-change nil t
)))))
5585 (defun org-element-cache-refresh (pos)
5586 "Refresh cache at position POS."
5587 (when (org-element--cache-active-p)
5588 (org-element--cache-sync (current-buffer) pos
)
5589 (org-element--cache-submit-request pos pos
0)
5590 (org-element--cache-set-timer (current-buffer))))
5596 ;; The first move is to implement a way to obtain the smallest element
5597 ;; containing point. This is the job of `org-element-at-point'. It
5598 ;; basically jumps back to the beginning of section containing point
5599 ;; and proceed, one element after the other, with
5600 ;; `org-element--current-element' until the container is found. Note:
5601 ;; When using `org-element-at-point', secondary values are never
5602 ;; parsed since the function focuses on elements, not on objects.
5604 ;; At a deeper level, `org-element-context' lists all elements and
5605 ;; objects containing point.
5607 ;; `org-element-nested-p' and `org-element-swap-A-B' may be used
5608 ;; internally by navigation and manipulation tools.
5612 (defun org-element-at-point ()
5613 "Determine closest element around point.
5615 Return value is a list like (TYPE PROPS) where TYPE is the type
5616 of the element and PROPS a plist of properties associated to the
5619 Possible types are defined in `org-element-all-elements'.
5620 Properties depend on element or object type, but always include
5621 `:begin', `:end', `:parent' and `:post-blank' properties.
5623 As a special case, if point is at the very beginning of the first
5624 item in a list or sub-list, returned element will be that list
5625 instead of the item. Likewise, if point is at the beginning of
5626 the first row of a table, returned element will be the table
5627 instead of the first row.
5629 When point is at the end of the buffer, return the innermost
5630 element ending there."
5631 (org-with-wide-buffer
5632 (let ((origin (point)))
5634 (skip-chars-backward " \r\t\n")
5636 ;; Within blank lines at the beginning of buffer, return nil.
5638 ;; Within blank lines right after a headline, return that
5640 ((org-with-limited-levels (org-at-heading-p))
5642 (org-element-headline-parser (point-max) t
))
5643 ;; Otherwise parse until we find element containing ORIGIN.
5645 (when (org-element--cache-active-p)
5646 (if (not org-element--cache
) (org-element-cache-reset)
5647 (org-element--cache-sync (current-buffer) origin
)))
5648 (org-element--parse-to origin
))))))
5651 (defun org-element-context (&optional element
)
5652 "Return smallest element or object around point.
5654 Return value is a list like (TYPE PROPS) where TYPE is the type
5655 of the element or object and PROPS a plist of properties
5658 Possible types are defined in `org-element-all-elements' and
5659 `org-element-all-objects'. Properties depend on element or
5660 object type, but always include `:begin', `:end', `:parent' and
5663 As a special case, if point is right after an object and not at
5664 the beginning of any other object, return that object.
5666 Optional argument ELEMENT, when non-nil, is the closest element
5667 containing point, as returned by `org-element-at-point'.
5668 Providing it allows for quicker computation."
5669 (catch 'objects-forbidden
5670 (org-with-wide-buffer
5671 (let* ((pos (point))
5672 (element (or element
(org-element-at-point)))
5673 (type (org-element-type element
)))
5674 ;; If point is inside an element containing objects or
5675 ;; a secondary string, narrow buffer to the container and
5676 ;; proceed with parsing. Otherwise, return ELEMENT.
5678 ;; At a parsed affiliated keyword, check if we're inside main
5680 ((let ((post (org-element-property :post-affiliated element
)))
5681 (and post
(< pos post
)))
5683 (let ((case-fold-search t
)) (looking-at org-element--affiliated-re
))
5685 ((not (member-ignore-case (match-string 1)
5686 org-element-parsed-keywords
))
5687 (throw 'objects-forbidden element
))
5688 ((< (match-end 0) pos
)
5689 (narrow-to-region (match-end 0) (line-end-position)))
5690 ((and (match-beginning 2)
5691 (>= pos
(match-beginning 2))
5692 (< pos
(match-end 2)))
5693 (narrow-to-region (match-beginning 2) (match-end 2)))
5694 (t (throw 'objects-forbidden element
)))
5695 ;; Also change type to retrieve correct restrictions.
5696 (setq type
'keyword
))
5697 ;; At an item, objects can only be located within tag, if any.
5699 (let ((tag (org-element-property :tag element
)))
5700 (if (not tag
) (throw 'objects-forbidden element
)
5702 (search-forward tag
(line-end-position))
5703 (goto-char (match-beginning 0))
5704 (if (and (>= pos
(point)) (< pos
(match-end 0)))
5705 (narrow-to-region (point) (match-end 0))
5706 (throw 'objects-forbidden element
)))))
5707 ;; At an headline or inlinetask, objects are in title.
5708 ((memq type
'(headline inlinetask
))
5709 (goto-char (org-element-property :begin element
))
5710 (skip-chars-forward "*")
5711 (if (and (> pos
(point)) (< pos
(line-end-position)))
5712 (narrow-to-region (point) (line-end-position))
5713 (throw 'objects-forbidden element
)))
5714 ;; At a paragraph, a table-row or a verse block, objects are
5715 ;; located within their contents.
5716 ((memq type
'(paragraph table-row verse-block
))
5717 (let ((cbeg (org-element-property :contents-begin element
))
5718 (cend (org-element-property :contents-end element
)))
5719 ;; CBEG is nil for table rules.
5720 (if (and cbeg cend
(>= pos cbeg
)
5721 (or (< pos cend
) (and (= pos cend
) (eobp))))
5722 (narrow-to-region cbeg cend
)
5723 (throw 'objects-forbidden element
))))
5724 ;; At a parsed keyword, objects are located within value.
5726 (if (not (member (org-element-property :key element
)
5727 org-element-document-properties
))
5728 (throw 'objects-forbidden element
)
5729 (goto-char (org-element-property :begin element
))
5730 (search-forward ":")
5731 (if (and (>= pos
(point)) (< pos
(line-end-position)))
5732 (narrow-to-region (point) (line-end-position))
5733 (throw 'objects-forbidden element
))))
5734 ;; At a planning line, if point is at a timestamp, return it,
5735 ;; otherwise, return element.
5736 ((eq type
'planning
)
5737 (dolist (p '(:closed
:deadline
:scheduled
))
5738 (let ((timestamp (org-element-property p element
)))
5739 (when (and timestamp
5740 (<= (org-element-property :begin timestamp
) pos
)
5741 (> (org-element-property :end timestamp
) pos
))
5742 (throw 'objects-forbidden timestamp
))))
5743 ;; All other locations cannot contain objects: bail out.
5744 (throw 'objects-forbidden element
))
5745 (t (throw 'objects-forbidden element
)))
5746 (goto-char (point-min))
5747 (let ((restriction (org-element-restriction type
))
5749 (cache (cond ((not (org-element--cache-active-p)) nil
)
5750 (org-element--cache-objects
5751 (gethash element org-element--cache-objects
))
5752 (t (org-element-cache-reset) nil
)))
5753 next object-data last
)
5757 ;; When entering PARENT for the first time, get list
5758 ;; of objects within known so far. Store it in
5761 (let ((data (assq parent cache
)))
5762 (if data
(setq object-data data
)
5763 (push (setq object-data
(list parent nil
)) cache
))))
5764 ;; Find NEXT object for analysis.
5766 ;; If NEXT is non-nil, we already exhausted the
5767 ;; cache so we can parse buffer to find the object
5769 (if next
(setq next
(org-element--object-lex restriction
))
5770 ;; Otherwise, check if cache can help us.
5771 (let ((objects (cddr object-data
))
5772 (completep (nth 1 object-data
)))
5774 ((and (not objects
) completep
) (throw 'exit parent
))
5776 (setq next
(org-element--object-lex restriction
)))
5779 (org-element-property :end
(car objects
))))
5780 (if (>= cache-limit pos
)
5781 ;; Cache contains the information needed.
5782 (dolist (object objects
(throw 'exit parent
))
5783 (when (<= (org-element-property :begin object
)
5785 (if (>= (org-element-property :end object
)
5787 (throw 'found
(setq next object
))
5788 (throw 'exit parent
))))
5789 (goto-char cache-limit
)
5791 (org-element--object-lex restriction
))))))))
5792 ;; If we have a new object to analyze, store it in
5793 ;; cache. Otherwise record that there is nothing
5794 ;; more to parse in this element at this depth.
5796 (progn (org-element-put-property next
:parent parent
)
5797 (push next
(cddr object-data
)))
5798 (setcar (cdr object-data
) t
)))
5799 ;; Process NEXT, if any, in order to know if we need
5800 ;; to skip it, return it or move into it.
5801 (if (or (not next
) (> (org-element-property :begin next
) pos
))
5802 (throw 'exit
(or last parent
))
5803 (let ((end (org-element-property :end next
))
5804 (cbeg (org-element-property :contents-begin next
))
5805 (cend (org-element-property :contents-end next
)))
5807 ;; Skip objects ending before point. Also skip
5808 ;; objects ending at point unless it is also the
5809 ;; end of buffer, since we want to return the
5810 ;; innermost object.
5811 ((and (<= end pos
) (/= (point-max) end
))
5813 ;; For convenience, when object ends at POS,
5814 ;; without any space, store it in LAST, as we
5815 ;; will return it if no object starts here.
5816 (when (and (= end pos
)
5817 (not (memq (char-before) '(?\s ?
\t))))
5819 ;; If POS is within a container object, move
5820 ;; into that object.
5824 ;; At contents' end, if there is no
5825 ;; space before point, also move into
5826 ;; object, for consistency with
5827 ;; convenience feature above.
5829 (or (= (point-max) pos
)
5830 (not (memq (char-before pos
)
5833 (narrow-to-region (point) cend
)
5835 restriction
(org-element-restriction next
)
5838 ;; Otherwise, return NEXT.
5839 (t (throw 'exit next
)))))))
5840 ;; Store results in cache, if applicable.
5841 (org-element--cache-put element cache
)))))))
5843 (defun org-element-lineage (blob &optional types with-self
)
5844 "List all ancestors of a given element or object.
5846 BLOB is an object or element.
5848 When optional argument TYPES is a list of symbols, return the
5849 first element or object in the lineage whose type belongs to that
5852 When optional argument WITH-SELF is non-nil, lineage includes
5853 BLOB itself as the first element, and TYPES, if provided, also
5856 When BLOB is obtained through `org-element-context' or
5857 `org-element-at-point', only ancestors from its section can be
5858 found. There is no such limitation when BLOB belongs to a full
5860 (let ((up (if with-self blob
(org-element-property :parent blob
)))
5862 (while (and up
(not (memq (org-element-type up
) types
)))
5863 (unless types
(push up ancestors
))
5864 (setq up
(org-element-property :parent up
)))
5865 (if types up
(nreverse ancestors
))))
5867 (defun org-element-nested-p (elem-A elem-B
)
5868 "Non-nil when elements ELEM-A and ELEM-B are nested."
5869 (let ((beg-A (org-element-property :begin elem-A
))
5870 (beg-B (org-element-property :begin elem-B
))
5871 (end-A (org-element-property :end elem-A
))
5872 (end-B (org-element-property :end elem-B
)))
5873 (or (and (>= beg-A beg-B
) (<= end-A end-B
))
5874 (and (>= beg-B beg-A
) (<= end-B end-A
)))))
5876 (defun org-element-swap-A-B (elem-A elem-B
)
5877 "Swap elements ELEM-A and ELEM-B.
5878 Assume ELEM-B is after ELEM-A in the buffer. Leave point at the
5880 (goto-char (org-element-property :begin elem-A
))
5881 ;; There are two special cases when an element doesn't start at bol:
5882 ;; the first paragraph in an item or in a footnote definition.
5883 (let ((specialp (not (bolp))))
5884 ;; Only a paragraph without any affiliated keyword can be moved at
5885 ;; ELEM-A position in such a situation. Note that the case of
5886 ;; a footnote definition is impossible: it cannot contain two
5887 ;; paragraphs in a row because it cannot contain a blank line.
5889 (or (not (eq (org-element-type elem-B
) 'paragraph
))
5890 (/= (org-element-property :begin elem-B
)
5891 (org-element-property :contents-begin elem-B
))))
5892 (error "Cannot swap elements"))
5893 ;; In a special situation, ELEM-A will have no indentation. We'll
5894 ;; give it ELEM-B's (which will in, in turn, have no indentation).
5895 (let* ((ind-B (when specialp
5896 (goto-char (org-element-property :begin elem-B
))
5897 (org-get-indentation)))
5898 (beg-A (org-element-property :begin elem-A
))
5899 (end-A (save-excursion
5900 (goto-char (org-element-property :end elem-A
))
5901 (skip-chars-backward " \r\t\n")
5903 (beg-B (org-element-property :begin elem-B
))
5904 (end-B (save-excursion
5905 (goto-char (org-element-property :end elem-B
))
5906 (skip-chars-backward " \r\t\n")
5908 ;; Store overlays responsible for visibility status. We
5909 ;; also need to store their boundaries as they will be
5910 ;; removed from buffer.
5913 (mapcar (lambda (ov) (list ov
(overlay-start ov
) (overlay-end ov
)))
5914 (overlays-in beg-A end-A
))
5915 (mapcar (lambda (ov) (list ov
(overlay-start ov
) (overlay-end ov
)))
5916 (overlays-in beg-B end-B
))))
5918 (body-A (buffer-substring beg-A end-A
))
5919 (body-B (delete-and-extract-region beg-B end-B
)))
5922 (setq body-B
(replace-regexp-in-string "\\`[ \t]*" "" body-B
))
5923 (org-indent-to-column ind-B
))
5925 ;; Restore ex ELEM-A overlays.
5926 (let ((offset (- beg-B beg-A
)))
5929 (car ov
) (+ (nth 1 ov
) offset
) (+ (nth 2 ov
) offset
)))
5932 (delete-region beg-A end-A
)
5934 ;; Restore ex ELEM-B overlays.
5937 (car ov
) (- (nth 1 ov
) offset
) (- (nth 2 ov
) offset
)))
5939 (goto-char (org-element-property :end elem-B
)))))
5941 (defun org-element-remove-indentation (s &optional n
)
5942 "Remove maximum common indentation in string S and return it.
5943 When optional argument N is a positive integer, remove exactly
5944 that much characters from indentation, if possible, or return
5945 S as-is otherwise. Unlike to `org-remove-indentation', this
5946 function doesn't call `untabify' on S."
5950 (goto-char (point-min))
5951 ;; Find maximum common indentation, if not specified.
5953 (let ((min-ind (point-max)))
5955 (while (re-search-forward "^[ \t]*\\S-" nil t
)
5956 (let ((ind (1- (current-column))))
5957 (if (zerop ind
) (throw 'exit s
)
5958 (setq min-ind
(min min-ind ind
))))))
5961 ;; Remove exactly N indentation, but give up if not possible.
5963 (let ((ind (progn (skip-chars-forward " \t") (current-column))))
5964 (cond ((eolp) (delete-region (line-beginning-position) (point)))
5965 ((< ind n
) (throw 'exit s
))
5966 (t (org-indent-line-to (- ind n
))))
5972 (provide 'org-element
)
5975 ;; generated-autoload-file: "org-loaddefs.el"
5978 ;;; org-element.el ends here