1 ;;; org-element.el --- Parser And Applications for Org syntax
3 ;; Copyright (C) 2012-2013 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', `node-property', `quote-section' `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', `quote-section',
52 ;; `src-block', `table', `table-row' and `verse-block'. Among them,
53 ;; `paragraph' and `verse-block' types can contain Org objects and
56 ;; Objects are related to document's contents. Some of them are
57 ;; recursive. Associated types are of the following: `bold', `code',
58 ;; `entity', `export-snippet', `footnote-reference',
59 ;; `inline-babel-call', `inline-src-block', `italic',
60 ;; `latex-fragment', `line-break', `link', `macro', `radio-target',
61 ;; `statistics-cookie', `strike-through', `subscript', `superscript',
62 ;; `table-cell', `target', `timestamp', `underline' and `verbatim'.
64 ;; Some elements also have special properties whose value can hold
65 ;; objects themselves (i.e. an item tag or a headline name). Such
66 ;; values are called "secondary strings". Any object belongs to
67 ;; either an element or a secondary string.
69 ;; Notwithstanding affiliated keywords, each greater element, element
70 ;; and object has a fixed set of properties attached to it. Among
71 ;; them, four are shared by all types: `:begin' and `:end', which
72 ;; refer to the beginning and ending buffer positions of the
73 ;; considered element or object, `:post-blank', which holds the number
74 ;; of blank lines, or white spaces, at its end and `:parent' which
75 ;; refers to the element or object containing it. Greater elements,
76 ;; elements and objects containing objects will also have
77 ;; `:contents-begin' and `:contents-end' properties to delimit
78 ;; contents. Eventually, greater elements and elements accepting
79 ;; affiliated keywords will have a `:post-affiliated' property,
80 ;; referring to the buffer position after all such keywords.
82 ;; At the lowest level, a `:parent' property is also attached to any
83 ;; string, as a text property.
85 ;; Lisp-wise, an element or an object can be represented as a list.
86 ;; It follows the pattern (TYPE PROPERTIES CONTENTS), where:
87 ;; TYPE is a symbol describing the Org element or object.
88 ;; PROPERTIES is the property list attached to it. See docstring of
89 ;; appropriate parsing function to get an exhaustive
91 ;; CONTENTS is a list of elements, objects or raw strings contained
92 ;; in the current element or object, when applicable.
94 ;; An Org buffer is a nested list of such elements and objects, whose
95 ;; type is `org-data' and properties is nil.
97 ;; The first part of this file defines Org syntax, while the second
98 ;; one provide accessors and setters functions.
100 ;; The next part implements a parser and an interpreter for each
101 ;; element and object type in Org syntax.
103 ;; The following part creates a fully recursive buffer parser. It
104 ;; also provides a tool to map a function to elements or objects
105 ;; matching some criteria in the parse tree. Functions of interest
106 ;; are `org-element-parse-buffer', `org-element-map' and, to a lesser
107 ;; extent, `org-element-parse-secondary-string'.
109 ;; The penultimate part is the cradle of an interpreter for the
110 ;; obtained parse tree: `org-element-interpret-data'.
112 ;; The library ends by furnishing `org-element-at-point' function, and
113 ;; a way to give information about document structure around point
114 ;; with `org-element-context'. A simple cache mechanism is also
115 ;; provided for these functions.
120 (eval-when-compile (require 'cl
))
126 ;;; Definitions And Rules
128 ;; Define elements, greater elements and specify recursive objects,
129 ;; along with the affiliated keywords recognized. Also set up
130 ;; restrictions on recursive objects combinations.
132 ;; These variables really act as a control center for the parsing
135 (defconst org-element-paragraph-separate
137 ;; Headlines, inlinetasks.
138 org-outline-regexp
"\\|"
139 ;; Footnote definitions.
140 "\\[\\(?:[0-9]+\\|fn:[-_[:word:]]+\\)\\]" "\\|"
146 ;; Tables (any type).
147 "\\(?:|\\|\\+-[-+]\\)" "\\|"
148 ;; Blocks (any type), Babel calls and keywords. Note: this
149 ;; is only an indication and need some thorough check.
150 "#\\(?:[+ ]\\|$\\)" "\\|"
151 ;; Drawers (any type) and fixed-width areas. This is also
152 ;; only an indication.
155 "-\\{5,\\}[ \t]*$" "\\|"
156 ;; LaTeX environments.
157 "\\\\begin{\\([A-Za-z0-9]+\\*?\\)}" "\\|"
158 ;; Planning and Clock lines.
159 (regexp-opt (list org-scheduled-string
165 (let ((term (case org-plain-list-ordered-item-terminator
166 (?\
) ")") (?.
"\\.") (otherwise "[.)]")))
167 (alpha (and org-list-allow-alphabetical
"\\|[A-Za-z]")))
168 (concat "\\(?:[-+*]\\|\\(?:[0-9]+" alpha
"\\)" term
"\\)"
169 "\\(?:[ \t]\\|$\\)"))
171 "Regexp to separate paragraphs in an Org buffer.
172 In the case of lines starting with \"#\" and \":\", this regexp
173 is not sufficient to know if point is at a paragraph ending. See
174 `org-element-paragraph-parser' for more information.")
176 (defconst org-element-all-elements
177 '(babel-call center-block clock comment comment-block diary-sexp drawer
178 dynamic-block example-block export-block fixed-width
179 footnote-definition headline horizontal-rule inlinetask item
180 keyword latex-environment node-property paragraph plain-list
181 planning property-drawer quote-block quote-section section
182 special-block src-block table table-row verse-block
)
183 "Complete list of element types.")
185 (defconst org-element-greater-elements
186 '(center-block drawer dynamic-block footnote-definition headline inlinetask
187 item plain-list property-drawer quote-block section
189 "List of recursive element types aka Greater Elements.")
191 (defconst org-element-all-successors
192 '(export-snippet footnote-reference inline-babel-call inline-src-block
193 latex-or-entity line-break link macro plain-link radio-target
194 statistics-cookie sub
/superscript table-cell target
195 text-markup timestamp
)
196 "Complete list of successors.")
198 (defconst org-element-object-successor-alist
199 '((subscript . sub
/superscript
) (superscript . sub
/superscript
)
200 (bold . text-markup
) (code . text-markup
) (italic . text-markup
)
201 (strike-through . text-markup
) (underline . text-markup
)
202 (verbatim . text-markup
) (entity . latex-or-entity
)
203 (latex-fragment . latex-or-entity
))
204 "Alist of translations between object type and successor name.
205 Sharing the same successor comes handy when, for example, the
206 regexp matching one object can also match the other object.")
208 (defconst org-element-all-objects
209 '(bold code entity export-snippet footnote-reference inline-babel-call
210 inline-src-block italic line-break latex-fragment link macro
211 radio-target statistics-cookie strike-through subscript superscript
212 table-cell target timestamp underline verbatim
)
213 "Complete list of object types.")
215 (defconst org-element-recursive-objects
216 '(bold italic link subscript radio-target strike-through superscript
217 table-cell underline
)
218 "List of recursive object types.")
220 (defvar org-element-block-name-alist
221 '(("CENTER" . org-element-center-block-parser
)
222 ("COMMENT" . org-element-comment-block-parser
)
223 ("EXAMPLE" . org-element-example-block-parser
)
224 ("QUOTE" . org-element-quote-block-parser
)
225 ("SRC" . org-element-src-block-parser
)
226 ("VERSE" . org-element-verse-block-parser
))
227 "Alist between block names and the associated parsing function.
228 Names must be uppercase. Any block whose name has no association
229 is parsed with `org-element-special-block-parser'.")
231 (defconst org-element-link-type-is-file
232 '("file" "file+emacs" "file+sys" "docview")
233 "List of link types equivalent to \"file\".
234 Only these types can accept search options and an explicit
235 application to open them.")
237 (defconst org-element-affiliated-keywords
238 '("CAPTION" "DATA" "HEADER" "HEADERS" "LABEL" "NAME" "PLOT" "RESNAME" "RESULT"
239 "RESULTS" "SOURCE" "SRCNAME" "TBLNAME")
240 "List of affiliated keywords as strings.
241 By default, all keywords setting attributes (i.e. \"ATTR_LATEX\")
242 are affiliated keywords and need not to be in this list.")
244 (defconst org-element-keyword-translation-alist
245 '(("DATA" .
"NAME") ("LABEL" .
"NAME") ("RESNAME" .
"NAME")
246 ("SOURCE" .
"NAME") ("SRCNAME" .
"NAME") ("TBLNAME" .
"NAME")
247 ("RESULT" .
"RESULTS") ("HEADERS" .
"HEADER"))
248 "Alist of usual translations for keywords.
249 The key is the old name and the value the new one. The property
250 holding their value will be named after the translated name.")
252 (defconst org-element-multiple-keywords
'("CAPTION" "HEADER")
253 "List of affiliated keywords that can occur more than once in an element.
255 Their value will be consed into a list of strings, which will be
256 returned as the value of the property.
258 This list is checked after translations have been applied. See
259 `org-element-keyword-translation-alist'.
261 By default, all keywords setting attributes (i.e. \"ATTR_LATEX\")
262 allow multiple occurrences and need not to be in this list.")
264 (defconst org-element-parsed-keywords
'("CAPTION")
265 "List of affiliated keywords whose value can be parsed.
267 Their value will be stored as a secondary string: a list of
270 This list is checked after translations have been applied. See
271 `org-element-keyword-translation-alist'.")
273 (defconst org-element-dual-keywords
'("CAPTION" "RESULTS")
274 "List of affiliated keywords which can have a secondary value.
276 In Org syntax, they can be written with optional square brackets
277 before the colons. For example, RESULTS keyword can be
278 associated to a hash value with the following:
280 #+RESULTS[hash-string]: some-source
282 This list is checked after translations have been applied. See
283 `org-element-keyword-translation-alist'.")
285 (defconst org-element-document-properties
'("AUTHOR" "DATE" "TITLE")
286 "List of properties associated to the whole document.
287 Any keyword in this list will have its value parsed and stored as
288 a secondary string.")
290 (defconst org-element--affiliated-re
291 (format "[ \t]*#\\+\\(?:%s\\):\\(?: \\|$\\)"
293 ;; Dual affiliated keywords.
294 (format "\\(?1:%s\\)\\(?:\\[\\(.*\\)\\]\\)?"
295 (regexp-opt org-element-dual-keywords
))
297 ;; Regular affiliated keywords.
298 (format "\\(?1:%s\\)"
302 (member keyword org-element-dual-keywords
))
303 org-element-affiliated-keywords
)))
305 ;; Export attributes.
306 "\\(?1:ATTR_[-_A-Za-z0-9]+\\)"))
307 "Regexp matching any affiliated keyword.
309 Keyword name is put in match group 1. Moreover, if keyword
310 belongs to `org-element-dual-keywords', put the dual value in
313 Don't modify it, set `org-element-affiliated-keywords' instead.")
315 (defconst org-element-object-restrictions
317 (remq 'plain-link
(remq 'table-cell org-element-all-successors
)))
318 (standard-set-no-line-break (remq 'line-break standard-set
)))
319 `((bold ,@standard-set
)
320 (footnote-reference ,@standard-set
)
321 (headline ,@standard-set-no-line-break
)
322 (inlinetask ,@standard-set-no-line-break
)
323 (italic ,@standard-set
)
324 (item ,@standard-set-no-line-break
)
325 (keyword ,@standard-set
)
326 ;; Ignore all links excepted plain links in a link description.
327 ;; Also ignore radio-targets and line breaks.
328 (link export-snippet inline-babel-call inline-src-block latex-or-entity
329 macro plain-link statistics-cookie sub
/superscript text-markup
)
330 (paragraph ,@standard-set
)
331 ;; Remove any variable object from radio target as it would
332 ;; prevent it from being properly recognized.
333 (radio-target latex-or-entity sub
/superscript
)
334 (strike-through ,@standard-set
)
335 (subscript ,@standard-set
)
336 (superscript ,@standard-set
)
337 ;; Ignore inline babel call and inline src block as formulas are
338 ;; possible. Also ignore line breaks and statistics cookies.
339 (table-cell export-snippet footnote-reference latex-or-entity link macro
340 radio-target sub
/superscript target text-markup timestamp
)
341 (table-row table-cell
)
342 (underline ,@standard-set
)
343 (verse-block ,@standard-set
)))
344 "Alist of objects restrictions.
346 CAR is an element or object type containing objects and CDR is
347 a list of successors that will be called within an element or
350 For example, in a `radio-target' object, one can only find
351 entities, latex-fragments, subscript and superscript.
353 This alist also applies to secondary string. For example, an
354 `headline' type element doesn't directly contain objects, but
355 still has an entry since one of its properties (`:title') does.")
357 (defconst org-element-secondary-value-alist
358 '((headline .
:title
)
359 (inlinetask .
:title
)
361 (footnote-reference .
:inline-definition
))
362 "Alist between element types and location of secondary value.")
364 (defconst org-element-object-variables
'(org-link-abbrev-alist-local)
365 "List of buffer-local variables used when parsing objects.
366 These variables are copied to the temporary buffer created by
367 `org-export-secondary-string'.")
371 ;;; Accessors and Setters
373 ;; Provide four accessors: `org-element-type', `org-element-property'
374 ;; `org-element-contents' and `org-element-restriction'.
376 ;; Setter functions allow to modify elements by side effect. There is
377 ;; `org-element-put-property', `org-element-set-contents'. These
378 ;; low-level functions are useful to build a parse tree.
380 ;; `org-element-adopt-element', `org-element-set-element',
381 ;; `org-element-extract-element' and `org-element-insert-before' are
382 ;; high-level functions useful to modify a parse tree.
384 ;; `org-element-secondary-p' is a predicate used to know if a given
385 ;; object belongs to a secondary string.
387 (defsubst org-element-type
(element)
388 "Return type of ELEMENT.
390 The function returns the type of the element or object provided.
391 It can also return the following special value:
392 `plain-text' for a string
393 `org-data' for a complete document
394 nil in any other case."
396 ((not (consp element
)) (and (stringp element
) 'plain-text
))
397 ((symbolp (car element
)) (car element
))))
399 (defsubst org-element-property
(property element
)
400 "Extract the value from the PROPERTY of an ELEMENT."
401 (if (stringp element
) (get-text-property 0 property element
)
402 (plist-get (nth 1 element
) property
)))
404 (defsubst org-element-contents
(element)
405 "Extract contents from an ELEMENT."
406 (cond ((not (consp element
)) nil
)
407 ((symbolp (car element
)) (nthcdr 2 element
))
410 (defsubst org-element-restriction
(element)
411 "Return restriction associated to ELEMENT.
412 ELEMENT can be an element, an object or a symbol representing an
413 element or object type."
414 (cdr (assq (if (symbolp element
) element
(org-element-type element
))
415 org-element-object-restrictions
)))
417 (defsubst org-element-put-property
(element property value
)
418 "In ELEMENT set PROPERTY to VALUE.
419 Return modified element."
420 (if (stringp element
) (org-add-props element nil property value
)
421 (setcar (cdr element
) (plist-put (nth 1 element
) property value
))
424 (defsubst org-element-set-contents
(element &rest contents
)
425 "Set ELEMENT contents to CONTENTS.
426 Return modified element."
427 (cond ((not element
) (list contents
))
428 ((not (symbolp (car element
))) contents
)
429 ((cdr element
) (setcdr (cdr element
) contents
))
430 (t (nconc element contents
))))
432 (defun org-element-secondary-p (object)
433 "Non-nil when OBJECT belongs to a secondary string.
434 Return value is the property name, as a keyword, or nil."
435 (let* ((parent (org-element-property :parent object
))
436 (property (cdr (assq (org-element-type parent
)
437 org-element-secondary-value-alist
))))
439 (memq object
(org-element-property property parent
))
442 (defsubst org-element-adopt-elements
(parent &rest children
)
443 "Append elements to the contents of another element.
445 PARENT is an element or object. CHILDREN can be elements,
446 objects, or a strings.
448 The function takes care of setting `:parent' property for CHILD.
449 Return parent element."
450 ;; Link every child to PARENT. If PARENT is nil, it is a secondary
451 ;; string: parent is the list itself.
452 (mapc (lambda (child)
453 (org-element-put-property child
:parent
(or parent children
)))
455 ;; Add CHILDREN at the end of PARENT contents.
457 (apply 'org-element-set-contents
459 (nconc (org-element-contents parent
) children
)))
460 ;; Return modified PARENT element.
461 (or parent children
))
463 (defun org-element-extract-element (element)
464 "Extract ELEMENT from parse tree.
465 Remove element from the parse tree by side-effect, and return it
466 with its `:parent' property stripped out."
467 (let ((parent (org-element-property :parent element
))
468 (secondary (org-element-secondary-p element
)))
470 (org-element-put-property
472 (delq element
(org-element-property secondary parent
)))
473 (apply #'org-element-set-contents
475 (delq element
(org-element-contents parent
))))
476 ;; Return ELEMENT with its :parent removed.
477 (org-element-put-property element
:parent nil
)))
479 (defun org-element-insert-before (element location
)
480 "Insert ELEMENT before LOCATION in parse tree.
481 LOCATION is an element, object or string within the parse tree.
482 Parse tree is modified by side effect."
483 (let* ((parent (org-element-property :parent location
))
484 (property (org-element-secondary-p location
))
485 (siblings (if property
(org-element-property property parent
)
486 (org-element-contents parent
)))
487 ;; Special case: LOCATION is the first element of an
488 ;; independent secondary string (e.g. :title property). Add
490 (specialp (and (not property
)
492 (eq (car parent
) location
))))
493 ;; Install ELEMENT at the appropriate POSITION within SIBLINGS.
495 ((or (null siblings
) (eq (car siblings
) location
))
496 (push element siblings
))
497 ((null location
) (nconc siblings
(list element
)))
498 (t (let ((previous (cadr (memq location
(reverse siblings
)))))
500 (error "No location found to insert element")
501 (let ((next (memq previous siblings
)))
502 (setcdr next
(cons element
(cdr next
))))))))
503 ;; Store SIBLINGS at appropriate place in parse tree.
505 (specialp (setcdr parent
(copy-sequence parent
)) (setcar parent element
))
506 (property (org-element-put-property parent property siblings
))
507 (t (apply #'org-element-set-contents parent siblings
)))
508 ;; Set appropriate :parent property.
509 (org-element-put-property element
:parent parent
)))
511 (defun org-element-set-element (old new
)
512 "Replace element or object OLD with element or object NEW.
513 The function takes care of setting `:parent' property for NEW."
514 ;; Ensure OLD and NEW have the same parent.
515 (org-element-put-property new
:parent
(org-element-property :parent old
))
516 (if (or (memq (org-element-type old
) '(plain-text nil
))
517 (memq (org-element-type new
) '(plain-text nil
)))
518 ;; We cannot replace OLD with NEW since one of them is not an
519 ;; object or element. We take the long path.
520 (progn (org-element-insert-before new old
)
521 (org-element-extract-element old
))
522 ;; Since OLD is going to be changed into NEW by side-effect, first
523 ;; make sure that every element or object within NEW has OLD as
525 (dolist (blob (org-element-contents new
))
526 (org-element-put-property blob
:parent old
))
527 ;; Transfer contents.
528 (apply #'org-element-set-contents old
(org-element-contents new
))
529 ;; Overwrite OLD's properties with NEW's.
530 (setcar (cdr old
) (nth 1 new
))
532 (setcar old
(car new
))))
538 ;; For each greater element type, we define a parser and an
541 ;; A parser returns the element or object as the list described above.
542 ;; Most of them accepts no argument. Though, exceptions exist. Hence
543 ;; every element containing a secondary string (see
544 ;; `org-element-secondary-value-alist') will accept an optional
545 ;; argument to toggle parsing of that secondary string. Moreover,
546 ;; `item' parser requires current list's structure as its first
549 ;; An interpreter accepts two arguments: the list representation of
550 ;; the element or object, and its contents. The latter may be nil,
551 ;; depending on the element or object considered. It returns the
552 ;; appropriate Org syntax, as a string.
554 ;; Parsing functions must follow the naming convention:
555 ;; org-element-TYPE-parser, where TYPE is greater element's type, as
556 ;; defined in `org-element-greater-elements'.
558 ;; Similarly, interpreting functions must follow the naming
559 ;; convention: org-element-TYPE-interpreter.
561 ;; With the exception of `headline' and `item' types, greater elements
562 ;; cannot contain other greater elements of their own type.
564 ;; Beside implementing a parser and an interpreter, adding a new
565 ;; greater element requires to tweak `org-element--current-element'.
566 ;; Moreover, the newly defined type must be added to both
567 ;; `org-element-all-elements' and `org-element-greater-elements'.
572 (defun org-element-center-block-parser (limit affiliated
)
573 "Parse a center block.
575 LIMIT bounds the search. AFFILIATED is a list of which CAR is
576 the buffer position at the beginning of the first affiliated
577 keyword and CDR is a plist of affiliated keywords along with
580 Return a list whose CAR is `center-block' and CDR is a plist
581 containing `:begin', `:end', `:contents-begin', `:contents-end',
582 `:post-blank' and `:post-affiliated' keywords.
584 Assume point is at the beginning of the block."
585 (let ((case-fold-search t
))
586 (if (not (save-excursion
587 (re-search-forward "^[ \t]*#\\+END_CENTER[ \t]*$" limit t
)))
588 ;; Incomplete block: parse it as a paragraph.
589 (org-element-paragraph-parser limit affiliated
)
590 (let ((block-end-line (match-beginning 0)))
591 (let* ((begin (car affiliated
))
592 (post-affiliated (point))
593 ;; Empty blocks have no contents.
594 (contents-begin (progn (forward-line)
595 (and (< (point) block-end-line
)
597 (contents-end (and contents-begin block-end-line
))
598 (pos-before-blank (progn (goto-char block-end-line
)
602 (skip-chars-forward " \r\t\n" limit
)
603 (if (eobp) (point) (line-beginning-position)))))
608 :contents-begin contents-begin
609 :contents-end contents-end
610 :post-blank
(count-lines pos-before-blank end
)
611 :post-affiliated post-affiliated
)
612 (cdr affiliated
))))))))
614 (defun org-element-center-block-interpreter (center-block contents
)
615 "Interpret CENTER-BLOCK element as Org syntax.
616 CONTENTS is the contents of the element."
617 (format "#+BEGIN_CENTER\n%s#+END_CENTER" contents
))
622 (defun org-element-drawer-parser (limit affiliated
)
625 LIMIT bounds the search. AFFILIATED is a list of which CAR is
626 the buffer position at the beginning of the first affiliated
627 keyword and CDR is a plist of affiliated keywords along with
630 Return a list whose CAR is `drawer' and CDR is a plist containing
631 `:drawer-name', `:begin', `:end', `:contents-begin',
632 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
634 Assume point is at beginning of drawer."
635 (let ((case-fold-search t
))
636 (if (not (save-excursion (re-search-forward "^[ \t]*:END:[ \t]*$" limit t
)))
637 ;; Incomplete drawer: parse it as a paragraph.
638 (org-element-paragraph-parser limit affiliated
)
640 (let* ((drawer-end-line (match-beginning 0))
641 (name (progn (looking-at org-drawer-regexp
)
642 (org-match-string-no-properties 1)))
643 (begin (car affiliated
))
644 (post-affiliated (point))
645 ;; Empty drawers have no contents.
646 (contents-begin (progn (forward-line)
647 (and (< (point) drawer-end-line
)
649 (contents-end (and contents-begin drawer-end-line
))
650 (pos-before-blank (progn (goto-char drawer-end-line
)
653 (end (progn (skip-chars-forward " \r\t\n" limit
)
654 (if (eobp) (point) (line-beginning-position)))))
660 :contents-begin contents-begin
661 :contents-end contents-end
662 :post-blank
(count-lines pos-before-blank end
)
663 :post-affiliated post-affiliated
)
664 (cdr affiliated
))))))))
666 (defun org-element-drawer-interpreter (drawer contents
)
667 "Interpret DRAWER element as Org syntax.
668 CONTENTS is the contents of the element."
669 (format ":%s:\n%s:END:"
670 (org-element-property :drawer-name drawer
)
676 (defun org-element-dynamic-block-parser (limit affiliated
)
677 "Parse a dynamic block.
679 LIMIT bounds the search. AFFILIATED is a list of which CAR is
680 the buffer position at the beginning of the first affiliated
681 keyword and CDR is a plist of affiliated keywords along with
684 Return a list whose CAR is `dynamic-block' and CDR is a plist
685 containing `:block-name', `:begin', `:end', `:contents-begin',
686 `:contents-end', `:arguments', `:post-blank' and
687 `:post-affiliated' keywords.
689 Assume point is at beginning of dynamic block."
690 (let ((case-fold-search t
))
691 (if (not (save-excursion
692 (re-search-forward "^[ \t]*#\\+END:?[ \t]*$" limit t
)))
693 ;; Incomplete block: parse it as a paragraph.
694 (org-element-paragraph-parser limit affiliated
)
695 (let ((block-end-line (match-beginning 0)))
697 (let* ((name (progn (looking-at org-dblock-start-re
)
698 (org-match-string-no-properties 1)))
699 (arguments (org-match-string-no-properties 3))
700 (begin (car affiliated
))
701 (post-affiliated (point))
702 ;; Empty blocks have no contents.
703 (contents-begin (progn (forward-line)
704 (and (< (point) block-end-line
)
706 (contents-end (and contents-begin block-end-line
))
707 (pos-before-blank (progn (goto-char block-end-line
)
710 (end (progn (skip-chars-forward " \r\t\n" limit
)
711 (if (eobp) (point) (line-beginning-position)))))
718 :contents-begin contents-begin
719 :contents-end contents-end
720 :post-blank
(count-lines pos-before-blank end
)
721 :post-affiliated post-affiliated
)
722 (cdr affiliated
)))))))))
724 (defun org-element-dynamic-block-interpreter (dynamic-block contents
)
725 "Interpret DYNAMIC-BLOCK element as Org syntax.
726 CONTENTS is the contents of the element."
727 (format "#+BEGIN: %s%s\n%s#+END:"
728 (org-element-property :block-name dynamic-block
)
729 (let ((args (org-element-property :arguments dynamic-block
)))
730 (and args
(concat " " args
)))
734 ;;;; Footnote Definition
736 (defun org-element-footnote-definition-parser (limit affiliated
)
737 "Parse a footnote definition.
739 LIMIT bounds the search. AFFILIATED is a list of which CAR is
740 the buffer position at the beginning of the first affiliated
741 keyword and CDR is a plist of affiliated keywords along with
744 Return a list whose CAR is `footnote-definition' and CDR is
745 a plist containing `:label', `:begin' `:end', `:contents-begin',
746 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
748 Assume point is at the beginning of the footnote definition."
750 (let* ((label (progn (looking-at org-footnote-definition-re
)
751 (org-match-string-no-properties 1)))
752 (begin (car affiliated
))
753 (post-affiliated (point))
754 (ending (save-excursion
758 (concat org-outline-regexp-bol
"\\|"
759 org-footnote-definition-re
"\\|"
760 "^\\([ \t]*\n\\)\\{2,\\}") limit
'move
))
763 (contents-begin (progn
765 (skip-chars-forward " \r\t\n" ending
)
766 (cond ((= (point) ending
) nil
)
767 ((= (line-beginning-position) begin
) (point))
768 (t (line-beginning-position)))))
769 (contents-end (and contents-begin ending
))
770 (end (progn (goto-char ending
)
771 (skip-chars-forward " \r\t\n" limit
)
772 (if (eobp) (point) (line-beginning-position)))))
773 (list 'footnote-definition
778 :contents-begin contents-begin
779 :contents-end contents-end
780 :post-blank
(count-lines ending end
)
781 :post-affiliated post-affiliated
)
782 (cdr affiliated
))))))
784 (defun org-element-footnote-definition-interpreter (footnote-definition contents
)
785 "Interpret FOOTNOTE-DEFINITION element as Org syntax.
786 CONTENTS is the contents of the footnote-definition."
787 (concat (format "[%s]" (org-element-property :label footnote-definition
))
794 (defun org-element-headline-parser (limit &optional raw-secondary-p
)
797 Return a list whose CAR is `headline' and CDR is a plist
798 containing `:raw-value', `:title', `:alt-title', `:begin',
799 `:end', `:pre-blank', `:contents-begin' and `:contents-end',
800 `:level', `:priority', `:tags', `:todo-keyword',`:todo-type',
801 `:scheduled', `:deadline', `:closed', `:quotedp', `:archivedp',
802 `:commentedp' and `:footnote-section-p' keywords.
804 The plist also contains any property set in the property drawer,
805 with its name in upper cases and colons added at the
806 beginning (i.e. `:CUSTOM_ID').
808 When RAW-SECONDARY-P is non-nil, headline's title will not be
809 parsed as a secondary string, but as a plain string instead.
811 Assume point is at beginning of the headline."
813 (let* ((components (org-heading-components))
814 (level (nth 1 components
))
815 (todo (nth 2 components
))
817 (and todo
(if (member todo org-done-keywords
) 'done
'todo
)))
818 (tags (let ((raw-tags (nth 5 components
)))
819 (and raw-tags
(org-split-string raw-tags
":"))))
820 (raw-value (or (nth 4 components
) ""))
822 (let ((case-fold-search nil
))
823 (string-match (format "^%s\\( \\|$\\)" org-quote-string
)
826 (let ((case-fold-search nil
))
827 (string-match (format "^%s\\( \\|$\\)" org-comment-string
)
829 (archivedp (member org-archive-tag tags
))
830 (footnote-section-p (and org-footnote-section
831 (string= org-footnote-section raw-value
)))
832 ;; Upcase property names. It avoids confusion between
833 ;; properties obtained through property drawer and default
834 ;; properties from the parser (e.g. `:end' and :END:)
841 (intern (concat ":" (upcase (car p
))))
843 (org-entry-properties nil
'standard
))
846 ;; Read time properties on the line below the headline.
848 (when (progn (forward-line)
849 (looking-at org-planning-or-clock-line-re
))
850 (let ((end (line-end-position)) plist
)
851 (while (re-search-forward
852 org-keyword-time-not-clock-regexp end t
)
853 (goto-char (match-end 1))
854 (skip-chars-forward " \t")
855 (let ((keyword (match-string 1))
856 (time (org-element-timestamp-parser)))
857 (cond ((equal keyword org-scheduled-string
)
858 (setq plist
(plist-put plist
:scheduled time
)))
859 ((equal keyword org-deadline-string
)
860 (setq plist
(plist-put plist
:deadline time
)))
861 (t (setq plist
(plist-put plist
:closed time
))))))
864 (end (save-excursion (goto-char (org-end-of-subtree t t
))))
865 (pos-after-head (progn (forward-line) (point)))
866 (contents-begin (save-excursion
867 (skip-chars-forward " \r\t\n" end
)
868 (and (/= (point) end
) (line-beginning-position))))
869 (contents-end (and contents-begin
870 (progn (goto-char end
)
871 (skip-chars-backward " \r\t\n")
874 ;; Clean RAW-VALUE from any quote or comment string.
875 (when (or quotedp commentedp
)
876 (let ((case-fold-search nil
))
878 (replace-regexp-in-string
880 (regexp-opt (list org-quote-string org-comment-string
))
884 ;; Clean TAGS from archive tag, if any.
885 (when archivedp
(setq tags
(delete org-archive-tag tags
)))
889 (list :raw-value raw-value
893 (if (not contents-begin
) 0
894 (count-lines pos-after-head contents-begin
))
895 :contents-begin contents-begin
896 :contents-end contents-end
898 :priority
(nth 3 components
)
902 :post-blank
(count-lines
903 (if (not contents-end
) pos-after-head
904 (goto-char contents-end
)
908 :footnote-section-p footnote-section-p
910 :commentedp commentedp
914 (let ((alt-title (org-element-property :ALT_TITLE headline
)))
916 (org-element-put-property
918 (if raw-secondary-p alt-title
919 (org-element-parse-secondary-string
920 alt-title
(org-element-restriction 'headline
) headline
)))))
921 (org-element-put-property
923 (if raw-secondary-p raw-value
924 (org-element-parse-secondary-string
925 raw-value
(org-element-restriction 'headline
) headline
)))))))
927 (defun org-element-headline-interpreter (headline contents
)
928 "Interpret HEADLINE element as Org syntax.
929 CONTENTS is the contents of the element."
930 (let* ((level (org-element-property :level headline
))
931 (todo (org-element-property :todo-keyword headline
))
932 (priority (org-element-property :priority headline
))
933 (title (org-element-interpret-data
934 (org-element-property :title headline
)))
935 (tags (let ((tag-list (if (org-element-property :archivedp headline
)
936 (cons org-archive-tag
937 (org-element-property :tags headline
))
938 (org-element-property :tags headline
))))
940 (format ":%s:" (mapconcat 'identity tag-list
":")))))
941 (commentedp (org-element-property :commentedp headline
))
942 (quotedp (org-element-property :quotedp headline
))
943 (pre-blank (or (org-element-property :pre-blank headline
) 0))
944 (heading (concat (make-string (org-reduced-level level
) ?
*)
945 (and todo
(concat " " todo
))
946 (and quotedp
(concat " " org-quote-string
))
947 (and commentedp
(concat " " org-comment-string
))
949 (format " [#%s]" (char-to-string priority
)))
950 (cond ((and org-footnote-section
951 (org-element-property
952 :footnote-section-p headline
))
953 (concat " " org-footnote-section
))
954 (title (concat " " title
))))))
959 ((zerop org-tags-column
) (format " %s" tags
))
960 ((< org-tags-column
0)
963 (max (- (+ org-tags-column
(length heading
) (length tags
))) 1)
968 (make-string (max (- org-tags-column
(length heading
)) 1) ?
)
970 (make-string (1+ pre-blank
) 10)
976 (defun org-element-inlinetask-parser (limit &optional raw-secondary-p
)
977 "Parse an inline task.
979 Return a list whose CAR is `inlinetask' and CDR is a plist
980 containing `:title', `:begin', `:end', `:contents-begin' and
981 `:contents-end', `:level', `:priority', `:raw-value', `:tags',
982 `:todo-keyword', `:todo-type', `:scheduled', `:deadline',
983 `:closed' and `:post-blank' keywords.
985 The plist also contains any property set in the property drawer,
986 with its name in upper cases and colons added at the
987 beginning (i.e. `:CUSTOM_ID').
989 When optional argument RAW-SECONDARY-P is non-nil, inline-task's
990 title will not be parsed as a secondary string, but as a plain
993 Assume point is at beginning of the inline task."
995 (let* ((begin (point))
996 (components (org-heading-components))
997 (todo (nth 2 components
))
999 (if (member todo org-done-keywords
) 'done
'todo
)))
1000 (tags (let ((raw-tags (nth 5 components
)))
1001 (and raw-tags
(org-split-string raw-tags
":"))))
1002 (raw-value (or (nth 4 components
) ""))
1003 ;; Upcase property names. It avoids confusion between
1004 ;; properties obtained through property drawer and default
1005 ;; properties from the parser (e.g. `:end' and :END:)
1012 (intern (concat ":" (upcase (car p
))))
1014 (org-entry-properties nil
'standard
))
1017 ;; Read time properties on the line below the inlinetask
1020 (when (progn (forward-line)
1021 (looking-at org-planning-or-clock-line-re
))
1022 (let ((end (line-end-position)) plist
)
1023 (while (re-search-forward
1024 org-keyword-time-not-clock-regexp end t
)
1025 (goto-char (match-end 1))
1026 (skip-chars-forward " \t")
1027 (let ((keyword (match-string 1))
1028 (time (org-element-timestamp-parser)))
1029 (cond ((equal keyword org-scheduled-string
)
1030 (setq plist
(plist-put plist
:scheduled time
)))
1031 ((equal keyword org-deadline-string
)
1032 (setq plist
(plist-put plist
:deadline time
)))
1033 (t (setq plist
(plist-put plist
:closed time
))))))
1035 (task-end (save-excursion
1037 (and (re-search-forward "^\\*+ END" limit t
)
1038 (match-beginning 0))))
1039 (contents-begin (progn (forward-line)
1040 (and task-end
(< (point) task-end
) (point))))
1041 (contents-end (and contents-begin task-end
))
1042 (before-blank (if (not task-end
) (point)
1043 (goto-char task-end
)
1046 (end (progn (skip-chars-forward " \r\t\n" limit
)
1047 (if (eobp) (point) (line-beginning-position))))
1051 (list :raw-value raw-value
1054 :contents-begin contents-begin
1055 :contents-end contents-end
1056 :level
(nth 1 components
)
1057 :priority
(nth 3 components
)
1060 :todo-type todo-type
1061 :post-blank
(count-lines before-blank end
))
1064 (org-element-put-property
1066 (if raw-secondary-p raw-value
1067 (org-element-parse-secondary-string
1069 (org-element-restriction 'inlinetask
)
1072 (defun org-element-inlinetask-interpreter (inlinetask contents
)
1073 "Interpret INLINETASK element as Org syntax.
1074 CONTENTS is the contents of inlinetask."
1075 (let* ((level (org-element-property :level inlinetask
))
1076 (todo (org-element-property :todo-keyword inlinetask
))
1077 (priority (org-element-property :priority inlinetask
))
1078 (title (org-element-interpret-data
1079 (org-element-property :title inlinetask
)))
1080 (tags (let ((tag-list (org-element-property :tags inlinetask
)))
1082 (format ":%s:" (mapconcat 'identity tag-list
":")))))
1083 (task (concat (make-string level ?
*)
1084 (and todo
(concat " " todo
))
1086 (format " [#%s]" (char-to-string priority
)))
1087 (and title
(concat " " title
)))))
1092 ((zerop org-tags-column
) (format " %s" tags
))
1093 ((< org-tags-column
0)
1096 (max (- (+ org-tags-column
(length task
) (length tags
))) 1)
1101 (make-string (max (- org-tags-column
(length task
)) 1) ?
)
1103 ;; Prefer degenerate inlinetasks when there are no
1108 (make-string level ?
*) " END")))))
1113 (defun org-element-item-parser (limit struct
&optional raw-secondary-p
)
1116 STRUCT is the structure of the plain list.
1118 Return a list whose CAR is `item' and CDR is a plist containing
1119 `:bullet', `:begin', `:end', `:contents-begin', `:contents-end',
1120 `:checkbox', `:counter', `:tag', `:structure' and `:post-blank'
1123 When optional argument RAW-SECONDARY-P is non-nil, item's tag, if
1124 any, will not be parsed as a secondary string, but as a plain
1127 Assume point is at the beginning of the item."
1130 (looking-at org-list-full-item-re
)
1131 (let* ((begin (point))
1132 (bullet (org-match-string-no-properties 1))
1133 (checkbox (let ((box (org-match-string-no-properties 3)))
1134 (cond ((equal "[ ]" box
) 'off
)
1135 ((equal "[X]" box
) 'on
)
1136 ((equal "[-]" box
) 'trans
))))
1137 (counter (let ((c (org-match-string-no-properties 2)))
1141 ((string-match "[A-Za-z]" c
)
1142 (- (string-to-char (upcase (match-string 0 c
)))
1144 ((string-match "[0-9]+" c
)
1145 (string-to-number (match-string 0 c
)))))))
1146 (end (progn (goto-char (nth 6 (assq (point) struct
)))
1147 (unless (bolp) (forward-line))
1151 ;; Ignore tags in un-ordered lists: they are just
1152 ;; a part of item's body.
1153 (if (and (match-beginning 4)
1154 (save-match-data (string-match "[.)]" bullet
)))
1157 (skip-chars-forward " \r\t\n" limit
)
1158 ;; If first line isn't empty, contents really start
1159 ;; at the text after item's meta-data.
1160 (if (= (point-at-bol) begin
) (point) (point-at-bol))))
1161 (contents-end (progn (goto-char end
)
1162 (skip-chars-backward " \r\t\n")
1167 (list :bullet bullet
1170 ;; CONTENTS-BEGIN and CONTENTS-END may be
1171 ;; mixed up in the case of an empty item
1172 ;; separated from the next by a blank line.
1173 ;; Thus ensure the former is always the
1175 :contents-begin
(min contents-begin contents-end
)
1176 :contents-end
(max contents-begin contents-end
)
1180 :post-blank
(count-lines contents-end end
)))))
1181 (org-element-put-property
1183 (let ((raw-tag (org-list-get-tag begin struct
)))
1185 (if raw-secondary-p raw-tag
1186 (org-element-parse-secondary-string
1187 raw-tag
(org-element-restriction 'item
) item
))))))))
1189 (defun org-element-item-interpreter (item contents
)
1190 "Interpret ITEM element as Org syntax.
1191 CONTENTS is the contents of the element."
1192 (let* ((bullet (let ((bullet (org-element-property :bullet item
)))
1193 (org-list-bullet-string
1194 (cond ((not (string-match "[0-9a-zA-Z]" bullet
)) "- ")
1195 ((eq org-plain-list-ordered-item-terminator ?\
)) "1)")
1197 (checkbox (org-element-property :checkbox item
))
1198 (counter (org-element-property :counter item
))
1199 (tag (let ((tag (org-element-property :tag item
)))
1200 (and tag
(org-element-interpret-data tag
))))
1201 ;; Compute indentation.
1202 (ind (make-string (length bullet
) 32))
1203 (item-starts-with-par-p
1204 (eq (org-element-type (car (org-element-contents item
)))
1209 (and counter
(format "[@%d] " counter
))
1214 (and tag
(format "%s :: " tag
))
1216 (let ((contents (replace-regexp-in-string
1217 "\\(^\\)[ \t]*\\S-" ind contents nil nil
1)))
1218 (if item-starts-with-par-p
(org-trim contents
)
1219 (concat "\n" contents
)))))))
1224 (defun org-element--list-struct (limit)
1225 ;; Return structure of list at point. Internal function. See
1226 ;; `org-list-struct' for details.
1227 (let ((case-fold-search t
)
1229 (item-re (org-item-re))
1230 (inlinetask-re (and (featurep 'org-inlinetask
) "^\\*+ "))
1236 ;; At limit: end all items.
1239 (let ((end (progn (skip-chars-backward " \r\t\n")
1242 (dolist (item items
(sort (nconc items struct
)
1243 'car-less-than-car
))
1244 (setcar (nthcdr 6 item
) end
)))))
1245 ;; At list end: end all items.
1246 ((looking-at org-list-end-re
)
1247 (throw 'exit
(dolist (item items
(sort (nconc items struct
)
1248 'car-less-than-car
))
1249 (setcar (nthcdr 6 item
) (point)))))
1250 ;; At a new item: end previous sibling.
1251 ((looking-at item-re
)
1252 (let ((ind (save-excursion (skip-chars-forward " \t")
1254 (setq top-ind
(min top-ind ind
))
1255 (while (and items
(<= ind
(nth 1 (car items
))))
1256 (let ((item (pop items
)))
1257 (setcar (nthcdr 6 item
) (point))
1258 (push item struct
)))
1259 (push (progn (looking-at org-list-full-item-re
)
1260 (let ((bullet (match-string-no-properties 1)))
1264 (match-string-no-properties 2) ; counter
1265 (match-string-no-properties 3) ; checkbox
1267 (and (save-match-data
1268 (string-match "[-+*]" bullet
))
1269 (match-string-no-properties 4))
1270 ;; Ending position, unknown so far.
1274 ;; Skip empty lines.
1275 ((looking-at "^[ \t]*$") (forward-line))
1276 ;; Skip inline tasks and blank lines along the way.
1277 ((and inlinetask-re
(looking-at inlinetask-re
))
1279 (let ((origin (point)))
1280 (when (re-search-forward inlinetask-re limit t
)
1281 (if (looking-at "^\\*+ END[ \t]*$") (forward-line)
1282 (goto-char origin
)))))
1283 ;; At some text line. Check if it ends any previous item.
1285 (let ((ind (progn (skip-chars-forward " \t") (current-column))))
1286 (when (<= ind top-ind
)
1287 (skip-chars-backward " \r\t\n")
1289 (while (<= ind
(nth 1 (car items
)))
1290 (let ((item (pop items
)))
1291 (setcar (nthcdr 6 item
) (line-beginning-position))
1294 (throw 'exit
(sort struct
'car-less-than-car
))))))
1295 ;; Skip blocks (any type) and drawers contents.
1297 ((and (looking-at "#\\+BEGIN\\(:\\|_\\S-+\\)")
1299 (format "^[ \t]*#\\+END%s[ \t]*$"
1300 (org-match-string-no-properties 1))
1302 ((and (looking-at org-drawer-regexp
)
1303 (re-search-forward "^[ \t]*:END:[ \t]*$" limit t
))))
1304 (forward-line))))))))
1306 (defun org-element-plain-list-parser (limit affiliated structure
)
1307 "Parse a plain list.
1309 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1310 the buffer position at the beginning of the first affiliated
1311 keyword and CDR is a plist of affiliated keywords along with
1312 their value. STRUCTURE is the structure of the plain list being
1315 Return a list whose CAR is `plain-list' and CDR is a plist
1316 containing `:type', `:begin', `:end', `:contents-begin' and
1317 `:contents-end', `:structure', `:post-blank' and
1318 `:post-affiliated' keywords.
1320 Assume point is at the beginning of the list."
1322 (let* ((struct (or structure
(org-element--list-struct limit
)))
1323 (type (cond ((org-looking-at-p "[ \t]*[A-Za-z0-9]") 'ordered
)
1324 ((nth 5 (assq (point) struct
)) 'descriptive
)
1326 (contents-begin (point))
1327 (begin (car affiliated
))
1328 (contents-end (let* ((item (assq contents-begin struct
))
1331 (while (and (setq item
(assq pos struct
))
1332 (= (nth 1 item
) ind
))
1333 (setq pos
(nth 6 item
)))
1335 (end (progn (goto-char contents-end
)
1336 (skip-chars-forward " \r\t\n" limit
)
1337 (if (= (point) limit
) limit
(line-beginning-position)))))
1344 :contents-begin contents-begin
1345 :contents-end contents-end
1347 :post-blank
(count-lines contents-end end
)
1348 :post-affiliated contents-begin
)
1349 (cdr affiliated
))))))
1351 (defun org-element-plain-list-interpreter (plain-list contents
)
1352 "Interpret PLAIN-LIST element as Org syntax.
1353 CONTENTS is the contents of the element."
1356 (goto-char (point-min))
1361 ;;;; Property Drawer
1363 (defun org-element-property-drawer-parser (limit affiliated
)
1364 "Parse a property drawer.
1366 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1367 the buffer position at the beginning of the first affiliated
1368 keyword and CDR is a plist of affiliated keywords along with
1371 Return a list whose CAR is `property-drawer' and CDR is a plist
1372 containing `:begin', `:end', `:contents-begin', `:contents-end',
1373 `:post-blank' and `:post-affiliated' keywords.
1375 Assume point is at the beginning of the property drawer."
1377 (let ((case-fold-search t
))
1378 (if (not (save-excursion
1379 (re-search-forward "^[ \t]*:END:[ \t]*$" limit t
)))
1380 ;; Incomplete drawer: parse it as a paragraph.
1381 (org-element-paragraph-parser limit affiliated
)
1383 (let* ((drawer-end-line (match-beginning 0))
1384 (begin (car affiliated
))
1385 (post-affiliated (point))
1386 (contents-begin (progn (forward-line)
1387 (and (< (point) drawer-end-line
)
1389 (contents-end (and contents-begin drawer-end-line
))
1390 (pos-before-blank (progn (goto-char drawer-end-line
)
1393 (end (progn (skip-chars-forward " \r\t\n" limit
)
1394 (if (eobp) (point) (line-beginning-position)))))
1395 (list 'property-drawer
1399 :contents-begin contents-begin
1400 :contents-end contents-end
1401 :post-blank
(count-lines pos-before-blank end
)
1402 :post-affiliated post-affiliated
)
1403 (cdr affiliated
)))))))))
1405 (defun org-element-property-drawer-interpreter (property-drawer contents
)
1406 "Interpret PROPERTY-DRAWER element as Org syntax.
1407 CONTENTS is the properties within the drawer."
1408 (format ":PROPERTIES:\n%s:END:" contents
))
1413 (defun org-element-quote-block-parser (limit affiliated
)
1414 "Parse a quote block.
1416 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1417 the buffer position at the beginning of the first affiliated
1418 keyword and CDR is a plist of affiliated keywords along with
1421 Return a list whose CAR is `quote-block' and CDR is a plist
1422 containing `:begin', `:end', `:contents-begin', `:contents-end',
1423 `:post-blank' and `:post-affiliated' keywords.
1425 Assume point is at the beginning of the block."
1426 (let ((case-fold-search t
))
1427 (if (not (save-excursion
1428 (re-search-forward "^[ \t]*#\\+END_QUOTE[ \t]*$" limit t
)))
1429 ;; Incomplete block: parse it as a paragraph.
1430 (org-element-paragraph-parser limit affiliated
)
1431 (let ((block-end-line (match-beginning 0)))
1433 (let* ((begin (car affiliated
))
1434 (post-affiliated (point))
1435 ;; Empty blocks have no contents.
1436 (contents-begin (progn (forward-line)
1437 (and (< (point) block-end-line
)
1439 (contents-end (and contents-begin block-end-line
))
1440 (pos-before-blank (progn (goto-char block-end-line
)
1443 (end (progn (skip-chars-forward " \r\t\n" limit
)
1444 (if (eobp) (point) (line-beginning-position)))))
1449 :contents-begin contents-begin
1450 :contents-end contents-end
1451 :post-blank
(count-lines pos-before-blank end
)
1452 :post-affiliated post-affiliated
)
1453 (cdr affiliated
)))))))))
1455 (defun org-element-quote-block-interpreter (quote-block contents
)
1456 "Interpret QUOTE-BLOCK element as Org syntax.
1457 CONTENTS is the contents of the element."
1458 (format "#+BEGIN_QUOTE\n%s#+END_QUOTE" contents
))
1463 (defun org-element-section-parser (limit)
1466 LIMIT bounds the search.
1468 Return a list whose CAR is `section' and CDR is a plist
1469 containing `:begin', `:end', `:contents-begin', `contents-end'
1470 and `:post-blank' keywords."
1472 ;; Beginning of section is the beginning of the first non-blank
1473 ;; line after previous headline.
1474 (let ((begin (point))
1475 (end (progn (org-with-limited-levels (outline-next-heading))
1477 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
1483 :contents-begin begin
1484 :contents-end pos-before-blank
1485 :post-blank
(count-lines pos-before-blank end
))))))
1487 (defun org-element-section-interpreter (section contents
)
1488 "Interpret SECTION element as Org syntax.
1489 CONTENTS is the contents of the element."
1495 (defun org-element-special-block-parser (limit affiliated
)
1496 "Parse a special block.
1498 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1499 the buffer position at the beginning of the first affiliated
1500 keyword and CDR is a plist of affiliated keywords along with
1503 Return a list whose CAR is `special-block' and CDR is a plist
1504 containing `:type', `:begin', `:end', `:contents-begin',
1505 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
1507 Assume point is at the beginning of the block."
1508 (let* ((case-fold-search t
)
1509 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
1510 (upcase (match-string-no-properties 1)))))
1511 (if (not (save-excursion
1513 (format "^[ \t]*#\\+END_%s[ \t]*$" (regexp-quote type
))
1515 ;; Incomplete block: parse it as a paragraph.
1516 (org-element-paragraph-parser limit affiliated
)
1517 (let ((block-end-line (match-beginning 0)))
1519 (let* ((begin (car affiliated
))
1520 (post-affiliated (point))
1521 ;; Empty blocks have no contents.
1522 (contents-begin (progn (forward-line)
1523 (and (< (point) block-end-line
)
1525 (contents-end (and contents-begin block-end-line
))
1526 (pos-before-blank (progn (goto-char block-end-line
)
1529 (end (progn (skip-chars-forward " \r\t\n" limit
)
1530 (if (eobp) (point) (line-beginning-position)))))
1531 (list 'special-block
1536 :contents-begin contents-begin
1537 :contents-end contents-end
1538 :post-blank
(count-lines pos-before-blank end
)
1539 :post-affiliated post-affiliated
)
1540 (cdr affiliated
)))))))))
1542 (defun org-element-special-block-interpreter (special-block contents
)
1543 "Interpret SPECIAL-BLOCK element as Org syntax.
1544 CONTENTS is the contents of the element."
1545 (let ((block-type (org-element-property :type special-block
)))
1546 (format "#+BEGIN_%s\n%s#+END_%s" block-type contents block-type
)))
1552 ;; For each element, a parser and an interpreter are also defined.
1553 ;; Both follow the same naming convention used for greater elements.
1555 ;; Also, as for greater elements, adding a new element type is done
1556 ;; through the following steps: implement a parser and an interpreter,
1557 ;; tweak `org-element--current-element' so that it recognizes the new
1558 ;; type and add that new type to `org-element-all-elements'.
1560 ;; As a special case, when the newly defined type is a block type,
1561 ;; `org-element-block-name-alist' has to be modified accordingly.
1566 (defun org-element-babel-call-parser (limit affiliated
)
1567 "Parse a babel call.
1569 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1570 the buffer position at the beginning of the first affiliated
1571 keyword and CDR is a plist of affiliated keywords along with
1574 Return a list whose CAR is `babel-call' and CDR is a plist
1575 containing `:begin', `:end', `:value', `:post-blank' and
1576 `:post-affiliated' as keywords."
1578 (let ((begin (car affiliated
))
1579 (post-affiliated (point))
1580 (value (progn (let ((case-fold-search t
))
1581 (re-search-forward "call:[ \t]*" nil t
))
1582 (buffer-substring-no-properties (point)
1583 (line-end-position))))
1584 (pos-before-blank (progn (forward-line) (point)))
1585 (end (progn (skip-chars-forward " \r\t\n" limit
)
1586 (if (eobp) (point) (line-beginning-position)))))
1592 :post-blank
(count-lines pos-before-blank end
)
1593 :post-affiliated post-affiliated
)
1594 (cdr affiliated
))))))
1596 (defun org-element-babel-call-interpreter (babel-call contents
)
1597 "Interpret BABEL-CALL element as Org syntax.
1599 (concat "#+CALL: " (org-element-property :value babel-call
)))
1604 (defun org-element-clock-parser (limit)
1607 LIMIT bounds the search.
1609 Return a list whose CAR is `clock' and CDR is a plist containing
1610 `:status', `:value', `:time', `:begin', `:end' and `:post-blank'
1613 (let* ((case-fold-search nil
)
1615 (value (progn (search-forward org-clock-string
(line-end-position) t
)
1616 (skip-chars-forward " \t")
1617 (org-element-timestamp-parser)))
1618 (duration (and (search-forward " => " (line-end-position) t
)
1619 (progn (skip-chars-forward " \t")
1620 (looking-at "\\(\\S-+\\)[ \t]*$"))
1621 (org-match-string-no-properties 1)))
1622 (status (if duration
'closed
'running
))
1623 (post-blank (let ((before-blank (progn (forward-line) (point))))
1624 (skip-chars-forward " \r\t\n" limit
)
1625 (skip-chars-backward " \t")
1626 (unless (bolp) (end-of-line))
1627 (count-lines before-blank
(point))))
1630 (list :status status
1635 :post-blank post-blank
)))))
1637 (defun org-element-clock-interpreter (clock contents
)
1638 "Interpret CLOCK element as Org syntax.
1640 (concat org-clock-string
" "
1641 (org-element-timestamp-interpreter
1642 (org-element-property :value clock
) nil
)
1643 (let ((duration (org-element-property :duration clock
)))
1648 (org-split-string duration
":")))))))
1653 (defun org-element-comment-parser (limit affiliated
)
1656 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1657 the buffer position at the beginning of the first affiliated
1658 keyword and CDR is a plist of affiliated keywords along with
1661 Return a list whose CAR is `comment' and CDR is a plist
1662 containing `:begin', `:end', `:value', `:post-blank',
1663 `:post-affiliated' keywords.
1665 Assume point is at comment beginning."
1667 (let* ((begin (car affiliated
))
1668 (post-affiliated (point))
1669 (value (prog2 (looking-at "[ \t]*# ?")
1670 (buffer-substring-no-properties
1671 (match-end 0) (line-end-position))
1674 ;; Get comments ending.
1676 (while (and (< (point) limit
) (looking-at "[ \t]*#\\( \\|$\\)"))
1677 ;; Accumulate lines without leading hash and first
1682 (buffer-substring-no-properties
1683 (match-end 0) (line-end-position))))
1686 (end (progn (goto-char com-end
)
1687 (skip-chars-forward " \r\t\n" limit
)
1688 (if (eobp) (point) (line-beginning-position)))))
1694 :post-blank
(count-lines com-end end
)
1695 :post-affiliated post-affiliated
)
1696 (cdr affiliated
))))))
1698 (defun org-element-comment-interpreter (comment contents
)
1699 "Interpret COMMENT element as Org syntax.
1701 (replace-regexp-in-string "^" "# " (org-element-property :value comment
)))
1706 (defun org-element-comment-block-parser (limit affiliated
)
1707 "Parse an export block.
1709 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1710 the buffer position at the beginning of the first affiliated
1711 keyword and CDR is a plist of affiliated keywords along with
1714 Return a list whose CAR is `comment-block' and CDR is a plist
1715 containing `:begin', `:end', `:value', `:post-blank' and
1716 `:post-affiliated' keywords.
1718 Assume point is at comment block beginning."
1719 (let ((case-fold-search t
))
1720 (if (not (save-excursion
1721 (re-search-forward "^[ \t]*#\\+END_COMMENT[ \t]*$" limit t
)))
1722 ;; Incomplete block: parse it as a paragraph.
1723 (org-element-paragraph-parser limit affiliated
)
1724 (let ((contents-end (match-beginning 0)))
1726 (let* ((begin (car affiliated
))
1727 (post-affiliated (point))
1728 (contents-begin (progn (forward-line) (point)))
1729 (pos-before-blank (progn (goto-char contents-end
)
1732 (end (progn (skip-chars-forward " \r\t\n" limit
)
1733 (if (eobp) (point) (line-beginning-position))))
1734 (value (buffer-substring-no-properties
1735 contents-begin contents-end
)))
1736 (list 'comment-block
1741 :post-blank
(count-lines pos-before-blank end
)
1742 :post-affiliated post-affiliated
)
1743 (cdr affiliated
)))))))))
1745 (defun org-element-comment-block-interpreter (comment-block contents
)
1746 "Interpret COMMENT-BLOCK element as Org syntax.
1748 (format "#+BEGIN_COMMENT\n%s#+END_COMMENT"
1749 (org-remove-indentation (org-element-property :value comment-block
))))
1754 (defun org-element-diary-sexp-parser (limit affiliated
)
1755 "Parse a diary sexp.
1757 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1758 the buffer position at the beginning of the first affiliated
1759 keyword and CDR is a plist of affiliated keywords along with
1762 Return a list whose CAR is `diary-sexp' and CDR is a plist
1763 containing `:begin', `:end', `:value', `:post-blank' and
1764 `:post-affiliated' keywords."
1766 (let ((begin (car affiliated
))
1767 (post-affiliated (point))
1768 (value (progn (looking-at "\\(%%(.*\\)[ \t]*$")
1769 (org-match-string-no-properties 1)))
1770 (pos-before-blank (progn (forward-line) (point)))
1771 (end (progn (skip-chars-forward " \r\t\n" limit
)
1772 (if (eobp) (point) (line-beginning-position)))))
1778 :post-blank
(count-lines pos-before-blank end
)
1779 :post-affiliated post-affiliated
)
1780 (cdr affiliated
))))))
1782 (defun org-element-diary-sexp-interpreter (diary-sexp contents
)
1783 "Interpret DIARY-SEXP as Org syntax.
1785 (org-element-property :value diary-sexp
))
1790 (defun org-element-example-block-parser (limit affiliated
)
1791 "Parse an example block.
1793 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1794 the buffer position at the beginning of the first affiliated
1795 keyword and CDR is a plist of affiliated keywords along with
1798 Return a list whose CAR is `example-block' and CDR is a plist
1799 containing `:begin', `:end', `:number-lines', `:preserve-indent',
1800 `:retain-labels', `:use-labels', `:label-fmt', `:switches',
1801 `:value', `:post-blank' and `:post-affiliated' keywords."
1802 (let ((case-fold-search t
))
1803 (if (not (save-excursion
1804 (re-search-forward "^[ \t]*#\\+END_EXAMPLE[ \t]*$" limit t
)))
1805 ;; Incomplete block: parse it as a paragraph.
1806 (org-element-paragraph-parser limit affiliated
)
1807 (let ((contents-end (match-beginning 0)))
1811 (looking-at "^[ \t]*#\\+BEGIN_EXAMPLE\\(?: +\\(.*\\)\\)?")
1812 (org-match-string-no-properties 1)))
1813 ;; Switches analysis
1815 (cond ((not switches
) nil
)
1816 ((string-match "-n\\>" switches
) 'new
)
1817 ((string-match "+n\\>" switches
) 'continued
)))
1819 (and switches
(string-match "-i\\>" switches
)))
1820 ;; Should labels be retained in (or stripped from) example
1824 (not (string-match "-r\\>" switches
))
1825 (and number-lines
(string-match "-k\\>" switches
))))
1826 ;; What should code-references use - labels or
1831 (not (string-match "-k\\>" switches
)))))
1834 (string-match "-l +\"\\([^\"\n]+\\)\"" switches
)
1835 (match-string 1 switches
)))
1836 ;; Standard block parsing.
1837 (begin (car affiliated
))
1838 (post-affiliated (point))
1839 (block-ind (progn (skip-chars-forward " \t") (current-column)))
1840 (contents-begin (progn (forward-line) (point)))
1841 (value (org-element-remove-indentation
1842 (org-unescape-code-in-string
1843 (buffer-substring-no-properties
1844 contents-begin contents-end
))
1846 (pos-before-blank (progn (goto-char contents-end
)
1849 (end (progn (skip-chars-forward " \r\t\n" limit
)
1850 (if (eobp) (point) (line-beginning-position)))))
1851 (list 'example-block
1857 :number-lines number-lines
1858 :preserve-indent preserve-indent
1859 :retain-labels retain-labels
1860 :use-labels use-labels
1861 :label-fmt label-fmt
1862 :post-blank
(count-lines pos-before-blank end
)
1863 :post-affiliated post-affiliated
)
1864 (cdr affiliated
)))))))))
1866 (defun org-element-example-block-interpreter (example-block contents
)
1867 "Interpret EXAMPLE-BLOCK element as Org syntax.
1869 (let ((switches (org-element-property :switches example-block
))
1870 (value (org-element-property :value example-block
)))
1871 (concat "#+BEGIN_EXAMPLE" (and switches
(concat " " switches
)) "\n"
1872 (org-escape-code-in-string
1873 (if (or org-src-preserve-indentation
1874 (org-element-property :preserve-indent example-block
))
1876 (org-element-remove-indentation value
)))
1882 (defun org-element-export-block-parser (limit affiliated
)
1883 "Parse an export block.
1885 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1886 the buffer position at the beginning of the first affiliated
1887 keyword and CDR is a plist of affiliated keywords along with
1890 Return a list whose CAR is `export-block' and CDR is a plist
1891 containing `:begin', `:end', `:type', `:value', `:post-blank' and
1892 `:post-affiliated' keywords.
1894 Assume point is at export-block beginning."
1895 (let* ((case-fold-search t
)
1896 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
1897 (upcase (org-match-string-no-properties 1)))))
1898 (if (not (save-excursion
1900 (format "^[ \t]*#\\+END_%s[ \t]*$" type
) limit t
)))
1901 ;; Incomplete block: parse it as a paragraph.
1902 (org-element-paragraph-parser limit affiliated
)
1903 (let ((contents-end (match-beginning 0)))
1905 (let* ((begin (car affiliated
))
1906 (post-affiliated (point))
1907 (contents-begin (progn (forward-line) (point)))
1908 (pos-before-blank (progn (goto-char contents-end
)
1911 (end (progn (skip-chars-forward " \r\t\n" limit
)
1912 (if (eobp) (point) (line-beginning-position))))
1913 (value (buffer-substring-no-properties contents-begin
1921 :post-blank
(count-lines pos-before-blank end
)
1922 :post-affiliated post-affiliated
)
1923 (cdr affiliated
)))))))))
1925 (defun org-element-export-block-interpreter (export-block contents
)
1926 "Interpret EXPORT-BLOCK element as Org syntax.
1928 (let ((type (org-element-property :type export-block
)))
1929 (concat (format "#+BEGIN_%s\n" type
)
1930 (org-element-property :value export-block
)
1931 (format "#+END_%s" type
))))
1936 (defun org-element-fixed-width-parser (limit affiliated
)
1937 "Parse a fixed-width section.
1939 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1940 the buffer position at the beginning of the first affiliated
1941 keyword and CDR is a plist of affiliated keywords along with
1944 Return a list whose CAR is `fixed-width' and CDR is a plist
1945 containing `:begin', `:end', `:value', `:post-blank' and
1946 `:post-affiliated' keywords.
1948 Assume point is at the beginning of the fixed-width area."
1950 (let* ((begin (car affiliated
))
1951 (post-affiliated (point))
1955 (while (and (< (point) limit
)
1956 (looking-at "[ \t]*:\\( \\|$\\)"))
1957 ;; Accumulate text without starting colons.
1960 (buffer-substring-no-properties
1961 (match-end 0) (point-at-eol))
1965 (end (progn (skip-chars-forward " \r\t\n" limit
)
1966 (if (eobp) (point) (line-beginning-position)))))
1972 :post-blank
(count-lines end-area end
)
1973 :post-affiliated post-affiliated
)
1974 (cdr affiliated
))))))
1976 (defun org-element-fixed-width-interpreter (fixed-width contents
)
1977 "Interpret FIXED-WIDTH element as Org syntax.
1979 (let ((value (org-element-property :value fixed-width
)))
1981 (replace-regexp-in-string
1983 (if (string-match "\n\\'" value
) (substring value
0 -
1) value
)))))
1986 ;;;; Horizontal Rule
1988 (defun org-element-horizontal-rule-parser (limit affiliated
)
1989 "Parse an horizontal rule.
1991 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1992 the buffer position at the beginning of the first affiliated
1993 keyword and CDR is a plist of affiliated keywords along with
1996 Return a list whose CAR is `horizontal-rule' and CDR is a plist
1997 containing `:begin', `:end', `:post-blank' and `:post-affiliated'
2000 (let ((begin (car affiliated
))
2001 (post-affiliated (point))
2002 (post-hr (progn (forward-line) (point)))
2003 (end (progn (skip-chars-forward " \r\t\n" limit
)
2004 (if (eobp) (point) (line-beginning-position)))))
2005 (list 'horizontal-rule
2009 :post-blank
(count-lines post-hr end
)
2010 :post-affiliated post-affiliated
)
2011 (cdr affiliated
))))))
2013 (defun org-element-horizontal-rule-interpreter (horizontal-rule contents
)
2014 "Interpret HORIZONTAL-RULE element as Org syntax.
2021 (defun org-element-keyword-parser (limit affiliated
)
2022 "Parse a keyword at point.
2024 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2025 the buffer position at the beginning of the first affiliated
2026 keyword and CDR is a plist of affiliated keywords along with
2029 Return a list whose CAR is `keyword' and CDR is a plist
2030 containing `:key', `:value', `:begin', `:end', `:post-blank' and
2031 `:post-affiliated' keywords."
2033 ;; An orphaned affiliated keyword is considered as a regular
2034 ;; keyword. In this case AFFILIATED is nil, so we take care of
2035 ;; this corner case.
2036 (let ((begin (or (car affiliated
) (point)))
2037 (post-affiliated (point))
2038 (key (progn (looking-at "[ \t]*#\\+\\(\\S-+*\\):")
2039 (upcase (org-match-string-no-properties 1))))
2040 (value (org-trim (buffer-substring-no-properties
2041 (match-end 0) (point-at-eol))))
2042 (pos-before-blank (progn (forward-line) (point)))
2043 (end (progn (skip-chars-forward " \r\t\n" limit
)
2044 (if (eobp) (point) (line-beginning-position)))))
2051 :post-blank
(count-lines pos-before-blank end
)
2052 :post-affiliated post-affiliated
)
2053 (cdr affiliated
))))))
2055 (defun org-element-keyword-interpreter (keyword contents
)
2056 "Interpret KEYWORD element as Org syntax.
2059 (org-element-property :key keyword
)
2060 (org-element-property :value keyword
)))
2063 ;;;; Latex Environment
2065 (defun org-element-latex-environment-parser (limit affiliated
)
2066 "Parse a LaTeX environment.
2068 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2069 the buffer position at the beginning of the first affiliated
2070 keyword and CDR is a plist of affiliated keywords along with
2073 Return a list whose CAR is `latex-environment' and CDR is a plist
2074 containing `:begin', `:end', `:value', `:post-blank' and
2075 `:post-affiliated' keywords.
2077 Assume point is at the beginning of the latex environment."
2079 (let ((case-fold-search t
)
2080 (code-begin (point)))
2081 (looking-at "[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}")
2082 (if (not (re-search-forward (format "^[ \t]*\\\\end{%s}[ \t]*$"
2083 (regexp-quote (match-string 1)))
2085 ;; Incomplete latex environment: parse it as a paragraph.
2086 (org-element-paragraph-parser limit affiliated
)
2087 (let* ((code-end (progn (forward-line) (point)))
2088 (begin (car affiliated
))
2089 (value (buffer-substring-no-properties code-begin code-end
))
2090 (end (progn (skip-chars-forward " \r\t\n" limit
)
2091 (if (eobp) (point) (line-beginning-position)))))
2092 (list 'latex-environment
2097 :post-blank
(count-lines code-end end
)
2098 :post-affiliated code-begin
)
2099 (cdr affiliated
))))))))
2101 (defun org-element-latex-environment-interpreter (latex-environment contents
)
2102 "Interpret LATEX-ENVIRONMENT element as Org syntax.
2104 (org-element-property :value latex-environment
))
2109 (defun org-element-node-property-parser (limit)
2110 "Parse a node-property at point.
2112 LIMIT bounds the search.
2114 Return a list whose CAR is `node-property' and CDR is a plist
2115 containing `:key', `:value', `:begin', `:end' and `:post-blank'
2118 (looking-at org-property-re
)
2119 (let ((case-fold-search t
)
2121 (key (org-match-string-no-properties 2))
2122 (value (org-match-string-no-properties 3))
2123 (pos-before-blank (progn (forward-line) (point)))
2124 (end (progn (skip-chars-forward " \r\t\n" limit
)
2125 (if (eobp) (point) (point-at-bol)))))
2126 (list 'node-property
2131 :post-blank
(count-lines pos-before-blank end
))))))
2133 (defun org-element-node-property-interpreter (node-property contents
)
2134 "Interpret NODE-PROPERTY element as Org syntax.
2136 (format org-property-format
2137 (format ":%s:" (org-element-property :key node-property
))
2138 (org-element-property :value node-property
)))
2143 (defun org-element-paragraph-parser (limit affiliated
)
2146 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2147 the buffer position at the beginning of the first affiliated
2148 keyword and CDR is a plist of affiliated keywords along with
2151 Return a list whose CAR is `paragraph' and CDR is a plist
2152 containing `:begin', `:end', `:contents-begin' and
2153 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
2155 Assume point is at the beginning of the paragraph."
2157 (let* ((begin (car affiliated
))
2158 (contents-begin (point))
2160 (let ((case-fold-search t
))
2162 (if (not (re-search-forward
2163 org-element-paragraph-separate limit
'm
))
2165 ;; A matching `org-element-paragraph-separate' is not
2166 ;; necessarily the end of the paragraph. In
2167 ;; particular, lines starting with # or : as a first
2168 ;; non-space character are ambiguous. We have check
2169 ;; if they are valid Org syntax (i.e. not an
2170 ;; incomplete keyword).
2174 ;; There's no ambiguity for other symbols or
2175 ;; empty lines: stop here.
2176 (looking-at "[ \t]*\\(?:[^:#]\\|$\\)")
2177 ;; Stop at valid fixed-width areas.
2178 (looking-at "[ \t]*:\\(?: \\|$\\)")
2180 (and (looking-at org-drawer-regexp
)
2183 "^[ \t]*:END:[ \t]*$" limit t
)))
2184 ;; Stop at valid comments.
2185 (looking-at "[ \t]*#\\(?: \\|$\\)")
2186 ;; Stop at valid dynamic blocks.
2187 (and (looking-at org-dblock-start-re
)
2190 "^[ \t]*#\\+END:?[ \t]*$" limit t
)))
2191 ;; Stop at valid blocks.
2192 (and (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
2195 (format "^[ \t]*#\\+END_%s[ \t]*$"
2197 (org-match-string-no-properties 1)))
2199 ;; Stop at valid latex environments.
2201 "[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}")
2204 (format "^[ \t]*\\\\end{%s}[ \t]*$"
2206 (org-match-string-no-properties 1)))
2208 ;; Stop at valid keywords.
2209 (looking-at "[ \t]*#\\+\\S-+:")
2210 ;; Skip everything else.
2214 (re-search-forward org-element-paragraph-separate
2216 (beginning-of-line)))
2217 (if (= (point) limit
) limit
2218 (goto-char (line-beginning-position)))))
2219 (contents-end (progn (skip-chars-backward " \r\t\n" contents-begin
)
2222 (end (progn (skip-chars-forward " \r\t\n" limit
)
2223 (if (eobp) (point) (line-beginning-position)))))
2228 :contents-begin contents-begin
2229 :contents-end contents-end
2230 :post-blank
(count-lines before-blank end
)
2231 :post-affiliated contents-begin
)
2232 (cdr affiliated
))))))
2234 (defun org-element-paragraph-interpreter (paragraph contents
)
2235 "Interpret PARAGRAPH element as Org syntax.
2236 CONTENTS is the contents of the element."
2242 (defun org-element-planning-parser (limit)
2245 LIMIT bounds the search.
2247 Return a list whose CAR is `planning' and CDR is a plist
2248 containing `:closed', `:deadline', `:scheduled', `:begin', `:end'
2249 and `:post-blank' keywords."
2251 (let* ((case-fold-search nil
)
2253 (post-blank (let ((before-blank (progn (forward-line) (point))))
2254 (skip-chars-forward " \r\t\n" limit
)
2255 (skip-chars-backward " \t")
2256 (unless (bolp) (end-of-line))
2257 (count-lines before-blank
(point))))
2259 closed deadline scheduled
)
2261 (while (re-search-forward org-keyword-time-not-clock-regexp end t
)
2262 (goto-char (match-end 1))
2263 (skip-chars-forward " \t" end
)
2264 (let ((keyword (match-string 1))
2265 (time (org-element-timestamp-parser)))
2266 (cond ((equal keyword org-closed-string
) (setq closed time
))
2267 ((equal keyword org-deadline-string
) (setq deadline time
))
2268 (t (setq scheduled time
)))))
2270 (list :closed closed
2272 :scheduled scheduled
2275 :post-blank post-blank
)))))
2277 (defun org-element-planning-interpreter (planning contents
)
2278 "Interpret PLANNING element as Org syntax.
2283 (list (let ((deadline (org-element-property :deadline planning
)))
2285 (concat org-deadline-string
" "
2286 (org-element-timestamp-interpreter deadline nil
))))
2287 (let ((scheduled (org-element-property :scheduled planning
)))
2289 (concat org-scheduled-string
" "
2290 (org-element-timestamp-interpreter scheduled nil
))))
2291 (let ((closed (org-element-property :closed planning
)))
2293 (concat org-closed-string
" "
2294 (org-element-timestamp-interpreter closed nil
))))))
2300 (defun org-element-quote-section-parser (limit)
2301 "Parse a quote section.
2303 LIMIT bounds the search.
2305 Return a list whose CAR is `quote-section' and CDR is a plist
2306 containing `:begin', `:end', `:value' and `:post-blank' keywords.
2308 Assume point is at beginning of the section."
2310 (let* ((begin (point))
2311 (end (progn (org-with-limited-levels (outline-next-heading))
2313 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
2316 (value (buffer-substring-no-properties begin pos-before-blank
)))
2317 (list 'quote-section
2321 :post-blank
(count-lines pos-before-blank end
))))))
2323 (defun org-element-quote-section-interpreter (quote-section contents
)
2324 "Interpret QUOTE-SECTION element as Org syntax.
2326 (org-element-property :value quote-section
))
2331 (defun org-element-src-block-parser (limit affiliated
)
2334 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2335 the buffer position at the beginning of the first affiliated
2336 keyword and CDR is a plist of affiliated keywords along with
2339 Return a list whose CAR is `src-block' and CDR is a plist
2340 containing `:language', `:switches', `:parameters', `:begin',
2341 `:end', `:number-lines', `:retain-labels', `:use-labels',
2342 `:label-fmt', `:preserve-indent', `:value', `:post-blank' and
2343 `:post-affiliated' keywords.
2345 Assume point is at the beginning of the block."
2346 (let ((case-fold-search t
))
2347 (if (not (save-excursion (re-search-forward "^[ \t]*#\\+END_SRC[ \t]*$"
2349 ;; Incomplete block: parse it as a paragraph.
2350 (org-element-paragraph-parser limit affiliated
)
2351 (let ((contents-end (match-beginning 0)))
2353 (let* ((begin (car affiliated
))
2354 (post-affiliated (point))
2355 ;; Get language as a string.
2359 (concat "^[ \t]*#\\+BEGIN_SRC"
2360 "\\(?: +\\(\\S-+\\)\\)?"
2361 "\\(\\(?: +\\(?:-l \".*?\"\\|[-+][A-Za-z]\\)\\)+\\)?"
2363 (org-match-string-no-properties 1)))
2365 (switches (org-match-string-no-properties 2))
2367 (parameters (org-match-string-no-properties 3))
2368 ;; Switches analysis
2370 (cond ((not switches
) nil
)
2371 ((string-match "-n\\>" switches
) 'new
)
2372 ((string-match "+n\\>" switches
) 'continued
)))
2373 (preserve-indent (and switches
2374 (string-match "-i\\>" switches
)))
2377 (string-match "-l +\"\\([^\"\n]+\\)\"" switches
)
2378 (match-string 1 switches
)))
2379 ;; Should labels be retained in (or stripped from)
2383 (not (string-match "-r\\>" switches
))
2384 (and number-lines
(string-match "-k\\>" switches
))))
2385 ;; What should code-references use - labels or
2390 (not (string-match "-k\\>" switches
)))))
2392 (block-ind (progn (skip-chars-forward " \t") (current-column)))
2394 (value (org-element-remove-indentation
2395 (org-unescape-code-in-string
2396 (buffer-substring-no-properties
2397 (progn (forward-line) (point)) contents-end
))
2399 (pos-before-blank (progn (goto-char contents-end
)
2402 ;; Get position after ending blank lines.
2403 (end (progn (skip-chars-forward " \r\t\n" limit
)
2404 (if (eobp) (point) (line-beginning-position)))))
2407 (list :language language
2408 :switches
(and (org-string-nw-p switches
)
2409 (org-trim switches
))
2410 :parameters
(and (org-string-nw-p parameters
)
2411 (org-trim parameters
))
2414 :number-lines number-lines
2415 :preserve-indent preserve-indent
2416 :retain-labels retain-labels
2417 :use-labels use-labels
2418 :label-fmt label-fmt
2420 :post-blank
(count-lines pos-before-blank end
)
2421 :post-affiliated post-affiliated
)
2422 (cdr affiliated
)))))))))
2424 (defun org-element-src-block-interpreter (src-block contents
)
2425 "Interpret SRC-BLOCK element as Org syntax.
2427 (let ((lang (org-element-property :language src-block
))
2428 (switches (org-element-property :switches src-block
))
2429 (params (org-element-property :parameters src-block
))
2431 (let ((val (org-element-property :value src-block
)))
2433 ((or org-src-preserve-indentation
2434 (org-element-property :preserve-indent src-block
))
2436 ((zerop org-edit-src-content-indentation
) val
)
2438 (let ((ind (make-string org-edit-src-content-indentation ?\s
)))
2439 (replace-regexp-in-string
2440 "\\(^\\)[ \t]*\\S-" ind val nil nil
1)))))))
2441 (concat (format "#+BEGIN_SRC%s\n"
2442 (concat (and lang
(concat " " lang
))
2443 (and switches
(concat " " switches
))
2444 (and params
(concat " " params
))))
2445 (org-escape-code-in-string value
)
2451 (defun org-element-table-parser (limit affiliated
)
2452 "Parse a table at point.
2454 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2455 the buffer position at the beginning of the first affiliated
2456 keyword and CDR is a plist of affiliated keywords along with
2459 Return a list whose CAR is `table' and CDR is a plist containing
2460 `:begin', `:end', `:tblfm', `:type', `:contents-begin',
2461 `:contents-end', `:value', `:post-blank' and `:post-affiliated'
2464 Assume point is at the beginning of the table."
2466 (let* ((case-fold-search t
)
2467 (table-begin (point))
2468 (type (if (org-at-table.el-p
) 'table.el
'org
))
2469 (begin (car affiliated
))
2471 (if (re-search-forward org-table-any-border-regexp limit
'm
)
2472 (goto-char (match-beginning 0))
2475 (while (looking-at "[ \t]*#\\+TBLFM: +\\(.*\\)[ \t]*$")
2476 (push (org-match-string-no-properties 1) acc
)
2479 (pos-before-blank (point))
2480 (end (progn (skip-chars-forward " \r\t\n" limit
)
2481 (if (eobp) (point) (line-beginning-position)))))
2488 ;; Only `org' tables have contents. `table.el' tables
2489 ;; use a `:value' property to store raw table as
2491 :contents-begin
(and (eq type
'org
) table-begin
)
2492 :contents-end
(and (eq type
'org
) table-end
)
2493 :value
(and (eq type
'table.el
)
2494 (buffer-substring-no-properties
2495 table-begin table-end
))
2496 :post-blank
(count-lines pos-before-blank end
)
2497 :post-affiliated table-begin
)
2498 (cdr affiliated
))))))
2500 (defun org-element-table-interpreter (table contents
)
2501 "Interpret TABLE element as Org syntax.
2503 (if (eq (org-element-property :type table
) 'table.el
)
2504 (org-remove-indentation (org-element-property :value table
))
2505 (concat (with-temp-buffer (insert contents
)
2508 (mapconcat (lambda (fm) (concat "#+TBLFM: " fm
))
2509 (reverse (org-element-property :tblfm table
))
2515 (defun org-element-table-row-parser (limit)
2516 "Parse table row at point.
2518 LIMIT bounds the search.
2520 Return a list whose CAR is `table-row' and CDR is a plist
2521 containing `:begin', `:end', `:contents-begin', `:contents-end',
2522 `:type' and `:post-blank' keywords."
2524 (let* ((type (if (looking-at "^[ \t]*|-") 'rule
'standard
))
2526 ;; A table rule has no contents. In that case, ensure
2527 ;; CONTENTS-BEGIN matches CONTENTS-END.
2528 (contents-begin (and (eq type
'standard
)
2529 (search-forward "|")
2531 (contents-end (and (eq type
'standard
)
2534 (skip-chars-backward " \t")
2536 (end (progn (forward-line) (point))))
2541 :contents-begin contents-begin
2542 :contents-end contents-end
2545 (defun org-element-table-row-interpreter (table-row contents
)
2546 "Interpret TABLE-ROW element as Org syntax.
2547 CONTENTS is the contents of the table row."
2548 (if (eq (org-element-property :type table-row
) 'rule
) "|-"
2549 (concat "| " contents
)))
2554 (defun org-element-verse-block-parser (limit affiliated
)
2555 "Parse a verse block.
2557 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2558 the buffer position at the beginning of the first affiliated
2559 keyword and CDR is a plist of affiliated keywords along with
2562 Return a list whose CAR is `verse-block' and CDR is a plist
2563 containing `:begin', `:end', `:contents-begin', `:contents-end',
2564 `:post-blank' and `:post-affiliated' keywords.
2566 Assume point is at beginning of the block."
2567 (let ((case-fold-search t
))
2568 (if (not (save-excursion
2569 (re-search-forward "^[ \t]*#\\+END_VERSE[ \t]*$" limit t
)))
2570 ;; Incomplete block: parse it as a paragraph.
2571 (org-element-paragraph-parser limit affiliated
)
2572 (let ((contents-end (match-beginning 0)))
2574 (let* ((begin (car affiliated
))
2575 (post-affiliated (point))
2576 (contents-begin (progn (forward-line) (point)))
2577 (pos-before-blank (progn (goto-char contents-end
)
2580 (end (progn (skip-chars-forward " \r\t\n" limit
)
2581 (if (eobp) (point) (line-beginning-position)))))
2586 :contents-begin contents-begin
2587 :contents-end contents-end
2588 :post-blank
(count-lines pos-before-blank end
)
2589 :post-affiliated post-affiliated
)
2590 (cdr affiliated
)))))))))
2592 (defun org-element-verse-block-interpreter (verse-block contents
)
2593 "Interpret VERSE-BLOCK element as Org syntax.
2594 CONTENTS is verse block contents."
2595 (format "#+BEGIN_VERSE\n%s#+END_VERSE" contents
))
2601 ;; Unlike to elements, interstices can be found between objects.
2602 ;; That's why, along with the parser, successor functions are provided
2603 ;; for each object. Some objects share the same successor (i.e. `code'
2604 ;; and `verbatim' objects).
2606 ;; A successor must accept a single argument bounding the search. It
2607 ;; will return either a cons cell whose CAR is the object's type, as
2608 ;; a symbol, and CDR the position of its next occurrence, or nil.
2610 ;; Successors follow the naming convention:
2611 ;; org-element-NAME-successor, where NAME is the name of the
2612 ;; successor, as defined in `org-element-all-successors'.
2614 ;; Some object types (i.e. `italic') are recursive. Restrictions on
2615 ;; object types they can contain will be specified in
2616 ;; `org-element-object-restrictions'.
2618 ;; Adding a new type of object is simple. Implement a successor,
2619 ;; a parser, and an interpreter for it, all following the naming
2620 ;; convention. Register type in `org-element-all-objects' and
2621 ;; successor in `org-element-all-successors'. Maybe tweak
2622 ;; restrictions about it, and that's it.
2627 (defun org-element-bold-parser ()
2628 "Parse bold object at point.
2630 Return a list whose CAR is `bold' and CDR is a plist with
2631 `:begin', `:end', `:contents-begin' and `:contents-end' and
2632 `:post-blank' keywords.
2634 Assume point is at the first star marker."
2636 (unless (bolp) (backward-char 1))
2637 (looking-at org-emph-re
)
2638 (let ((begin (match-beginning 2))
2639 (contents-begin (match-beginning 4))
2640 (contents-end (match-end 4))
2641 (post-blank (progn (goto-char (match-end 2))
2642 (skip-chars-forward " \t")))
2647 :contents-begin contents-begin
2648 :contents-end contents-end
2649 :post-blank post-blank
)))))
2651 (defun org-element-bold-interpreter (bold contents
)
2652 "Interpret BOLD object as Org syntax.
2653 CONTENTS is the contents of the object."
2654 (format "*%s*" contents
))
2656 (defun org-element-text-markup-successor ()
2657 "Search for the next text-markup object.
2659 Return value is a cons cell whose CAR is a symbol among `bold',
2660 `italic', `underline', `strike-through', `code' and `verbatim'
2661 and CDR is beginning position."
2663 (unless (bolp) (backward-char))
2664 (when (re-search-forward org-emph-re nil t
)
2665 (let ((marker (match-string 3)))
2667 ((equal marker
"*") 'bold
)
2668 ((equal marker
"/") 'italic
)
2669 ((equal marker
"_") 'underline
)
2670 ((equal marker
"+") 'strike-through
)
2671 ((equal marker
"~") 'code
)
2672 ((equal marker
"=") 'verbatim
)
2673 (t (error "Unknown marker at %d" (match-beginning 3))))
2674 (match-beginning 2))))))
2679 (defun org-element-code-parser ()
2680 "Parse code object at point.
2682 Return a list whose CAR is `code' and CDR is a plist with
2683 `:value', `:begin', `:end' and `:post-blank' keywords.
2685 Assume point is at the first tilde marker."
2687 (unless (bolp) (backward-char 1))
2688 (looking-at org-emph-re
)
2689 (let ((begin (match-beginning 2))
2690 (value (org-match-string-no-properties 4))
2691 (post-blank (progn (goto-char (match-end 2))
2692 (skip-chars-forward " \t")))
2698 :post-blank post-blank
)))))
2700 (defun org-element-code-interpreter (code contents
)
2701 "Interpret CODE object as Org syntax.
2703 (format "~%s~" (org-element-property :value code
)))
2708 (defun org-element-entity-parser ()
2709 "Parse entity at point.
2711 Return a list whose CAR is `entity' and CDR a plist with
2712 `:begin', `:end', `:latex', `:latex-math-p', `:html', `:latin1',
2713 `:utf-8', `:ascii', `:use-brackets-p' and `:post-blank' as
2716 Assume point is at the beginning of the entity."
2718 (looking-at "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)")
2719 (let* ((value (org-entity-get (match-string 1)))
2720 (begin (match-beginning 0))
2721 (bracketsp (string= (match-string 2) "{}"))
2722 (post-blank (progn (goto-char (match-end 1))
2723 (when bracketsp
(forward-char 2))
2724 (skip-chars-forward " \t")))
2727 (list :name
(car value
)
2728 :latex
(nth 1 value
)
2729 :latex-math-p
(nth 2 value
)
2731 :ascii
(nth 4 value
)
2732 :latin1
(nth 5 value
)
2733 :utf-8
(nth 6 value
)
2736 :use-brackets-p bracketsp
2737 :post-blank post-blank
)))))
2739 (defun org-element-entity-interpreter (entity contents
)
2740 "Interpret ENTITY object as Org syntax.
2743 (org-element-property :name entity
)
2744 (when (org-element-property :use-brackets-p entity
) "{}")))
2746 (defun org-element-latex-or-entity-successor ()
2747 "Search for the next latex-fragment or entity object.
2749 Return value is a cons cell whose CAR is `entity' or
2750 `latex-fragment' and CDR is beginning position."
2752 (unless (bolp) (backward-char))
2753 (let ((matchers (cdr org-latex-regexps
))
2754 ;; ENTITY-RE matches both LaTeX commands and Org entities.
2756 "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))
2757 (when (re-search-forward
2758 (concat (mapconcat #'cadr matchers
"\\|") "\\|" entity-re
) nil t
)
2759 (goto-char (match-beginning 0))
2760 (if (looking-at entity-re
)
2761 ;; Determine if it's a real entity or a LaTeX command.
2762 (cons (if (org-entity-get (match-string 1)) 'entity
'latex-fragment
)
2763 (match-beginning 0))
2764 ;; No entity nor command: point is at a LaTeX fragment.
2765 ;; Determine its type to get the correct beginning position.
2766 (cons 'latex-fragment
2768 (dolist (e matchers
)
2769 (when (looking-at (nth 1 e
))
2770 (throw 'return
(match-beginning (nth 2 e
)))))
2776 (defun org-element-export-snippet-parser ()
2777 "Parse export snippet at point.
2779 Return a list whose CAR is `export-snippet' and CDR a plist with
2780 `:begin', `:end', `:back-end', `:value' and `:post-blank' as
2783 Assume point is at the beginning of the snippet."
2785 (re-search-forward "@@\\([-A-Za-z0-9]+\\):" nil t
)
2786 (let* ((begin (match-beginning 0))
2787 (back-end (org-match-string-no-properties 1))
2788 (value (buffer-substring-no-properties
2790 (progn (re-search-forward "@@" nil t
) (match-beginning 0))))
2791 (post-blank (skip-chars-forward " \t"))
2793 (list 'export-snippet
2794 (list :back-end back-end
2798 :post-blank post-blank
)))))
2800 (defun org-element-export-snippet-interpreter (export-snippet contents
)
2801 "Interpret EXPORT-SNIPPET object as Org syntax.
2804 (org-element-property :back-end export-snippet
)
2805 (org-element-property :value export-snippet
)))
2807 (defun org-element-export-snippet-successor ()
2808 "Search for the next export-snippet object.
2810 Return value is a cons cell whose CAR is `export-snippet' and CDR
2811 its beginning position."
2814 (when (and (re-search-forward "@@[-A-Za-z0-9]+:" nil t
)
2815 (setq beg
(match-beginning 0))
2816 (search-forward "@@" nil t
))
2817 (cons 'export-snippet beg
)))))
2820 ;;;; Footnote Reference
2822 (defun org-element-footnote-reference-parser ()
2823 "Parse footnote reference at point.
2825 Return a list whose CAR is `footnote-reference' and CDR a plist
2826 with `:label', `:type', `:inline-definition', `:begin', `:end'
2827 and `:post-blank' as keywords."
2829 (looking-at org-footnote-re
)
2830 (let* ((begin (point))
2831 (label (or (org-match-string-no-properties 2)
2832 (org-match-string-no-properties 3)
2833 (and (match-string 1)
2834 (concat "fn:" (org-match-string-no-properties 1)))))
2835 (type (if (or (not label
) (match-string 1)) 'inline
'standard
))
2836 (inner-begin (match-end 0))
2840 (while (and (> count
0) (re-search-forward "[][]" nil t
))
2841 (if (equal (match-string 0) "[") (incf count
) (decf count
)))
2843 (post-blank (progn (goto-char (1+ inner-end
))
2844 (skip-chars-forward " \t")))
2847 (list 'footnote-reference
2852 :post-blank post-blank
))))
2853 (org-element-put-property
2854 footnote-reference
:inline-definition
2855 (and (eq type
'inline
)
2856 (org-element-parse-secondary-string
2857 (buffer-substring inner-begin inner-end
)
2858 (org-element-restriction 'footnote-reference
)
2859 footnote-reference
))))))
2861 (defun org-element-footnote-reference-interpreter (footnote-reference contents
)
2862 "Interpret FOOTNOTE-REFERENCE object as Org syntax.
2864 (let ((label (or (org-element-property :label footnote-reference
) "fn:"))
2867 (org-element-property :inline-definition footnote-reference
)))
2868 (if (not inline-def
) ""
2869 (concat ":" (org-element-interpret-data inline-def
))))))
2870 (format "[%s]" (concat label def
))))
2872 (defun org-element-footnote-reference-successor ()
2873 "Search for the next footnote-reference object.
2875 Return value is a cons cell whose CAR is `footnote-reference' and
2876 CDR is beginning position."
2879 (while (re-search-forward org-footnote-re nil t
)
2881 (let ((beg (match-beginning 0))
2884 (while (re-search-forward "[][]" nil t
)
2885 (if (equal (match-string 0) "[") (incf count
) (decf count
))
2887 (throw 'exit
(cons 'footnote-reference beg
))))))))))
2890 ;;;; Inline Babel Call
2892 (defun org-element-inline-babel-call-parser ()
2893 "Parse inline babel call at point.
2895 Return a list whose CAR is `inline-babel-call' and CDR a plist
2896 with `:begin', `:end', `:value' and `:post-blank' as keywords.
2898 Assume point is at the beginning of the babel call."
2900 (unless (bolp) (backward-char))
2901 (let ((case-fold-search t
))
2902 (looking-at org-babel-inline-lob-one-liner-regexp
))
2903 (let ((begin (match-end 1))
2904 (value (buffer-substring-no-properties (match-end 1) (match-end 0)))
2905 (post-blank (progn (goto-char (match-end 0))
2906 (skip-chars-forward " \t")))
2908 (list 'inline-babel-call
2912 :post-blank post-blank
)))))
2914 (defun org-element-inline-babel-call-interpreter (inline-babel-call contents
)
2915 "Interpret INLINE-BABEL-CALL object as Org syntax.
2917 (org-element-property :value inline-babel-call
))
2919 (defun org-element-inline-babel-call-successor ()
2920 "Search for the next inline-babel-call object.
2922 Return value is a cons cell whose CAR is `inline-babel-call' and
2923 CDR is beginning position."
2925 (when (re-search-forward org-babel-inline-lob-one-liner-regexp nil t
)
2926 (cons 'inline-babel-call
(match-end 1)))))
2929 ;;;; Inline Src Block
2931 (defun org-element-inline-src-block-parser ()
2932 "Parse inline source block at point.
2934 Return a list whose CAR is `inline-src-block' and CDR a plist
2935 with `:begin', `:end', `:language', `:value', `:parameters' and
2936 `:post-blank' as keywords.
2938 Assume point is at the beginning of the inline src block."
2940 (unless (bolp) (backward-char))
2941 (looking-at org-babel-inline-src-block-regexp
)
2942 (let ((begin (match-beginning 1))
2943 (language (org-match-string-no-properties 2))
2944 (parameters (org-match-string-no-properties 4))
2945 (value (org-match-string-no-properties 5))
2946 (post-blank (progn (goto-char (match-end 0))
2947 (skip-chars-forward " \t")))
2949 (list 'inline-src-block
2950 (list :language language
2952 :parameters parameters
2955 :post-blank post-blank
)))))
2957 (defun org-element-inline-src-block-interpreter (inline-src-block contents
)
2958 "Interpret INLINE-SRC-BLOCK object as Org syntax.
2960 (let ((language (org-element-property :language inline-src-block
))
2961 (arguments (org-element-property :parameters inline-src-block
))
2962 (body (org-element-property :value inline-src-block
)))
2963 (format "src_%s%s{%s}"
2965 (if arguments
(format "[%s]" arguments
) "")
2968 (defun org-element-inline-src-block-successor ()
2969 "Search for the next inline-babel-call element.
2971 Return value is a cons cell whose CAR is `inline-babel-call' and
2972 CDR is beginning position."
2974 (unless (bolp) (backward-char))
2975 (when (re-search-forward org-babel-inline-src-block-regexp nil t
)
2976 (cons 'inline-src-block
(match-beginning 1)))))
2980 (defun org-element-italic-parser ()
2981 "Parse italic object at point.
2983 Return a list whose CAR is `italic' and CDR is a plist with
2984 `:begin', `:end', `:contents-begin' and `:contents-end' and
2985 `:post-blank' keywords.
2987 Assume point is at the first slash marker."
2989 (unless (bolp) (backward-char 1))
2990 (looking-at org-emph-re
)
2991 (let ((begin (match-beginning 2))
2992 (contents-begin (match-beginning 4))
2993 (contents-end (match-end 4))
2994 (post-blank (progn (goto-char (match-end 2))
2995 (skip-chars-forward " \t")))
3000 :contents-begin contents-begin
3001 :contents-end contents-end
3002 :post-blank post-blank
)))))
3004 (defun org-element-italic-interpreter (italic contents
)
3005 "Interpret ITALIC object as Org syntax.
3006 CONTENTS is the contents of the object."
3007 (format "/%s/" contents
))
3012 (defun org-element-latex-fragment-parser ()
3013 "Parse LaTeX fragment at point.
3015 Return a list whose CAR is `latex-fragment' and CDR a plist with
3016 `:value', `:begin', `:end', and `:post-blank' as keywords.
3018 Assume point is at the beginning of the LaTeX fragment."
3020 (let* ((begin (point))
3023 (dolist (e (cdr org-latex-regexps
))
3024 (let ((latex-regexp (nth 1 e
)))
3025 (when (or (looking-at latex-regexp
)
3029 (looking-at latex-regexp
))))
3030 (throw 'exit
(nth 2 e
)))))
3031 ;; None found: it's a macro.
3032 (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")
3034 (value (org-match-string-no-properties substring-match
))
3035 (post-blank (progn (goto-char (match-end substring-match
))
3036 (skip-chars-forward " \t")))
3038 (list 'latex-fragment
3042 :post-blank post-blank
)))))
3044 (defun org-element-latex-fragment-interpreter (latex-fragment contents
)
3045 "Interpret LATEX-FRAGMENT object as Org syntax.
3047 (org-element-property :value latex-fragment
))
3051 (defun org-element-line-break-parser ()
3052 "Parse line break at point.
3054 Return a list whose CAR is `line-break', and CDR a plist with
3055 `:begin', `:end' and `:post-blank' keywords.
3057 Assume point is at the beginning of the line break."
3059 (list :begin
(point)
3060 :end
(progn (forward-line) (point))
3063 (defun org-element-line-break-interpreter (line-break contents
)
3064 "Interpret LINE-BREAK object as Org syntax.
3068 (defun org-element-line-break-successor ()
3069 "Search for the next line-break object.
3071 Return value is a cons cell whose CAR is `line-break' and CDR is
3072 beginning position."
3074 (let ((beg (and (re-search-forward "[^\\\\]\\(\\\\\\\\\\)[ \t]*$" nil t
)
3075 (goto-char (match-beginning 1)))))
3076 ;; A line break can only happen on a non-empty line.
3077 (when (and beg
(re-search-backward "\\S-" (point-at-bol) t
))
3078 (cons 'line-break beg
)))))
3083 (defun org-element-link-parser ()
3084 "Parse link at point.
3086 Return a list whose CAR is `link' and CDR a plist with `:type',
3087 `:path', `:raw-link', `:application', `:search-option', `:begin',
3088 `:end', `:contents-begin', `:contents-end' and `:post-blank' as
3091 Assume point is at the beginning of the link."
3093 (let ((begin (point))
3094 end contents-begin contents-end link-end post-blank path type
3095 raw-link link search-option application
)
3097 ;; Type 1: Text targeted from a radio target.
3098 ((and org-target-link-regexp
(looking-at org-target-link-regexp
))
3100 link-end
(match-end 0)
3101 path
(org-match-string-no-properties 0)))
3102 ;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]]
3103 ((looking-at org-bracket-link-regexp
)
3104 (setq contents-begin
(match-beginning 3)
3105 contents-end
(match-end 3)
3106 link-end
(match-end 0)
3107 ;; RAW-LINK is the original link. Expand any
3108 ;; abbreviation in it.
3109 raw-link
(org-translate-link
3110 (org-link-expand-abbrev
3111 (org-match-string-no-properties 1))))
3112 ;; Determine TYPE of link and set PATH accordingly.
3115 ((or (file-name-absolute-p raw-link
)
3116 (string-match "^\\.\\.?/" raw-link
))
3117 (setq type
"file" path raw-link
))
3118 ;; Explicit type (http, irc, bbdb...). See `org-link-types'.
3119 ((string-match org-link-re-with-space3 raw-link
)
3120 (setq type
(match-string 1 raw-link
) path
(match-string 2 raw-link
)))
3121 ;; Id type: PATH is the id.
3122 ((string-match "^id:\\([-a-f0-9]+\\)" raw-link
)
3123 (setq type
"id" path
(match-string 1 raw-link
)))
3124 ;; Code-ref type: PATH is the name of the reference.
3125 ((string-match "^(\\(.*\\))$" raw-link
)
3126 (setq type
"coderef" path
(match-string 1 raw-link
)))
3127 ;; Custom-id type: PATH is the name of the custom id.
3128 ((= (aref raw-link
0) ?
#)
3129 (setq type
"custom-id" path
(substring raw-link
1)))
3130 ;; Fuzzy type: Internal link either matches a target, an
3131 ;; headline name or nothing. PATH is the target or
3133 (t (setq type
"fuzzy" path raw-link
))))
3134 ;; Type 3: Plain link, i.e. http://orgmode.org
3135 ((looking-at org-plain-link-re
)
3136 (setq raw-link
(org-match-string-no-properties 0)
3137 type
(org-match-string-no-properties 1)
3138 link-end
(match-end 0)
3139 path
(org-match-string-no-properties 2)))
3140 ;; Type 4: Angular link, i.e. <http://orgmode.org>
3141 ((looking-at org-angle-link-re
)
3142 (setq raw-link
(buffer-substring-no-properties
3143 (match-beginning 1) (match-end 2))
3144 type
(org-match-string-no-properties 1)
3145 link-end
(match-end 0)
3146 path
(org-match-string-no-properties 2))))
3147 ;; In any case, deduce end point after trailing white space from
3148 ;; LINK-END variable.
3149 (setq post-blank
(progn (goto-char link-end
) (skip-chars-forward " \t"))
3151 ;; Extract search option and opening application out of
3152 ;; "file"-type links.
3153 (when (member type org-element-link-type-is-file
)
3155 (cond ((string-match "^file\\+\\(.*\\)$" type
)
3156 (setq application
(match-string 1 type
)))
3157 ((not (string-match "^file" type
))
3158 (setq application type
)))
3159 ;; Extract search option from PATH.
3160 (when (string-match "::\\(.*\\)$" path
)
3161 (setq search-option
(match-string 1 path
)
3162 path
(replace-match "" nil nil path
)))
3163 ;; Make sure TYPE always reports "file".
3168 :raw-link
(or raw-link path
)
3169 :application application
3170 :search-option search-option
3173 :contents-begin contents-begin
3174 :contents-end contents-end
3175 :post-blank post-blank
)))))
3177 (defun org-element-link-interpreter (link contents
)
3178 "Interpret LINK object as Org syntax.
3179 CONTENTS is the contents of the object, or nil."
3180 (let ((type (org-element-property :type link
))
3181 (raw-link (org-element-property :raw-link link
)))
3182 (if (string= type
"radio") raw-link
3185 (if contents
(format "[%s]" contents
) "")))))
3187 (defun org-element-link-successor ()
3188 "Search for the next link object.
3190 Return value is a cons cell whose CAR is `link' and CDR is
3191 beginning position."
3194 (if (not org-target-link-regexp
) org-any-link-re
3195 (concat org-any-link-re
"\\|" org-target-link-regexp
))))
3196 (when (re-search-forward link-regexp nil t
)
3197 (cons 'link
(match-beginning 0))))))
3199 (defun org-element-plain-link-successor ()
3200 "Search for the next plain link object.
3202 Return value is a cons cell whose CAR is `link' and CDR is
3203 beginning position."
3204 (and (save-excursion (re-search-forward org-plain-link-re nil t
))
3205 (cons 'link
(match-beginning 0))))
3210 (defun org-element-macro-parser ()
3211 "Parse macro at point.
3213 Return a list whose CAR is `macro' and CDR a plist with `:key',
3214 `:args', `:begin', `:end', `:value' and `:post-blank' as
3217 Assume point is at the macro."
3219 (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
3220 (let ((begin (point))
3221 (key (downcase (org-match-string-no-properties 1)))
3222 (value (org-match-string-no-properties 0))
3223 (post-blank (progn (goto-char (match-end 0))
3224 (skip-chars-forward " \t")))
3226 (args (let ((args (org-match-string-no-properties 3)))
3228 ;; Do not use `org-split-string' since empty
3229 ;; strings are meaningful here.
3231 (replace-regexp-in-string
3232 "\\(\\\\*\\)\\(,\\)"
3234 (let ((len (length (match-string 1 str
))))
3235 (concat (make-string (/ len
2) ?
\\)
3236 (if (zerop (mod len
2)) "\000" ","))))
3245 :post-blank post-blank
)))))
3247 (defun org-element-macro-interpreter (macro contents
)
3248 "Interpret MACRO object as Org syntax.
3250 (org-element-property :value macro
))
3252 (defun org-element-macro-successor ()
3253 "Search for the next macro object.
3255 Return value is cons cell whose CAR is `macro' and CDR is
3256 beginning position."
3258 (when (re-search-forward
3259 "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
3261 (cons 'macro
(match-beginning 0)))))
3266 (defun org-element-radio-target-parser ()
3267 "Parse radio target at point.
3269 Return a list whose CAR is `radio-target' and CDR a plist with
3270 `:begin', `:end', `:contents-begin', `:contents-end', `:value'
3271 and `:post-blank' as keywords.
3273 Assume point is at the radio target."
3275 (looking-at org-radio-target-regexp
)
3276 (let ((begin (point))
3277 (contents-begin (match-beginning 1))
3278 (contents-end (match-end 1))
3279 (value (org-match-string-no-properties 1))
3280 (post-blank (progn (goto-char (match-end 0))
3281 (skip-chars-forward " \t")))
3286 :contents-begin contents-begin
3287 :contents-end contents-end
3288 :post-blank post-blank
3291 (defun org-element-radio-target-interpreter (target contents
)
3292 "Interpret TARGET object as Org syntax.
3293 CONTENTS is the contents of the object."
3294 (concat "<<<" contents
">>>"))
3296 (defun org-element-radio-target-successor ()
3297 "Search for the next radio-target object.
3299 Return value is a cons cell whose CAR is `radio-target' and CDR
3300 is beginning position."
3302 (when (re-search-forward org-radio-target-regexp nil t
)
3303 (cons 'radio-target
(match-beginning 0)))))
3306 ;;;; Statistics Cookie
3308 (defun org-element-statistics-cookie-parser ()
3309 "Parse statistics cookie at point.
3311 Return a list whose CAR is `statistics-cookie', and CDR a plist
3312 with `:begin', `:end', `:value' and `:post-blank' keywords.
3314 Assume point is at the beginning of the statistics-cookie."
3316 (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
3317 (let* ((begin (point))
3318 (value (buffer-substring-no-properties
3319 (match-beginning 0) (match-end 0)))
3320 (post-blank (progn (goto-char (match-end 0))
3321 (skip-chars-forward " \t")))
3323 (list 'statistics-cookie
3327 :post-blank post-blank
)))))
3329 (defun org-element-statistics-cookie-interpreter (statistics-cookie contents
)
3330 "Interpret STATISTICS-COOKIE object as Org syntax.
3332 (org-element-property :value statistics-cookie
))
3334 (defun org-element-statistics-cookie-successor ()
3335 "Search for the next statistics cookie object.
3337 Return value is a cons cell whose CAR is `statistics-cookie' and
3338 CDR is beginning position."
3340 (when (re-search-forward "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]" nil t
)
3341 (cons 'statistics-cookie
(match-beginning 0)))))
3346 (defun org-element-strike-through-parser ()
3347 "Parse strike-through object at point.
3349 Return a list whose CAR is `strike-through' and CDR is a plist
3350 with `:begin', `:end', `:contents-begin' and `:contents-end' and
3351 `:post-blank' keywords.
3353 Assume point is at the first plus sign marker."
3355 (unless (bolp) (backward-char 1))
3356 (looking-at org-emph-re
)
3357 (let ((begin (match-beginning 2))
3358 (contents-begin (match-beginning 4))
3359 (contents-end (match-end 4))
3360 (post-blank (progn (goto-char (match-end 2))
3361 (skip-chars-forward " \t")))
3363 (list 'strike-through
3366 :contents-begin contents-begin
3367 :contents-end contents-end
3368 :post-blank post-blank
)))))
3370 (defun org-element-strike-through-interpreter (strike-through contents
)
3371 "Interpret STRIKE-THROUGH object as Org syntax.
3372 CONTENTS is the contents of the object."
3373 (format "+%s+" contents
))
3378 (defun org-element-subscript-parser ()
3379 "Parse subscript at point.
3381 Return a list whose CAR is `subscript' and CDR a plist with
3382 `:begin', `:end', `:contents-begin', `:contents-end',
3383 `:use-brackets-p' and `:post-blank' as keywords.
3385 Assume point is at the underscore."
3387 (unless (bolp) (backward-char))
3388 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp
)
3390 (not (looking-at org-match-substring-regexp
))))
3391 (begin (match-beginning 2))
3392 (contents-begin (or (match-beginning 5)
3393 (match-beginning 3)))
3394 (contents-end (or (match-end 5) (match-end 3)))
3395 (post-blank (progn (goto-char (match-end 0))
3396 (skip-chars-forward " \t")))
3401 :use-brackets-p bracketsp
3402 :contents-begin contents-begin
3403 :contents-end contents-end
3404 :post-blank post-blank
)))))
3406 (defun org-element-subscript-interpreter (subscript contents
)
3407 "Interpret SUBSCRIPT object as Org syntax.
3408 CONTENTS is the contents of the object."
3410 (if (org-element-property :use-brackets-p subscript
) "_{%s}" "_%s")
3413 (defun org-element-sub/superscript-successor
()
3414 "Search for the next sub/superscript object.
3416 Return value is a cons cell whose CAR is either `subscript' or
3417 `superscript' and CDR is beginning position."
3419 (unless (bolp) (backward-char))
3420 (when (re-search-forward org-match-substring-regexp nil t
)
3421 (cons (if (string= (match-string 2) "_") 'subscript
'superscript
)
3422 (match-beginning 2)))))
3427 (defun org-element-superscript-parser ()
3428 "Parse superscript at point.
3430 Return a list whose CAR is `superscript' and CDR a plist with
3431 `:begin', `:end', `:contents-begin', `:contents-end',
3432 `:use-brackets-p' and `:post-blank' as keywords.
3434 Assume point is at the caret."
3436 (unless (bolp) (backward-char))
3437 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp
) t
3438 (not (looking-at org-match-substring-regexp
))))
3439 (begin (match-beginning 2))
3440 (contents-begin (or (match-beginning 5)
3441 (match-beginning 3)))
3442 (contents-end (or (match-end 5) (match-end 3)))
3443 (post-blank (progn (goto-char (match-end 0))
3444 (skip-chars-forward " \t")))
3449 :use-brackets-p bracketsp
3450 :contents-begin contents-begin
3451 :contents-end contents-end
3452 :post-blank post-blank
)))))
3454 (defun org-element-superscript-interpreter (superscript contents
)
3455 "Interpret SUPERSCRIPT object as Org syntax.
3456 CONTENTS is the contents of the object."
3458 (if (org-element-property :use-brackets-p superscript
) "^{%s}" "^%s")
3464 (defun org-element-table-cell-parser ()
3465 "Parse table cell at point.
3467 Return a list whose CAR is `table-cell' and CDR is a plist
3468 containing `:begin', `:end', `:contents-begin', `:contents-end'
3469 and `:post-blank' keywords."
3470 (looking-at "[ \t]*\\(.*?\\)[ \t]*|")
3471 (let* ((begin (match-beginning 0))
3473 (contents-begin (match-beginning 1))
3474 (contents-end (match-end 1)))
3478 :contents-begin contents-begin
3479 :contents-end contents-end
3482 (defun org-element-table-cell-interpreter (table-cell contents
)
3483 "Interpret TABLE-CELL element as Org syntax.
3484 CONTENTS is the contents of the cell, or nil."
3485 (concat " " contents
" |"))
3487 (defun org-element-table-cell-successor ()
3488 "Search for the next table-cell object.
3490 Return value is a cons cell whose CAR is `table-cell' and CDR is
3491 beginning position."
3492 (when (looking-at "[ \t]*.*?[ \t]*|") (cons 'table-cell
(point))))
3497 (defun org-element-target-parser ()
3498 "Parse target at point.
3500 Return a list whose CAR is `target' and CDR a plist with
3501 `:begin', `:end', `:value' and `:post-blank' as keywords.
3503 Assume point is at the target."
3505 (looking-at org-target-regexp
)
3506 (let ((begin (point))
3507 (value (org-match-string-no-properties 1))
3508 (post-blank (progn (goto-char (match-end 0))
3509 (skip-chars-forward " \t")))
3515 :post-blank post-blank
)))))
3517 (defun org-element-target-interpreter (target contents
)
3518 "Interpret TARGET object as Org syntax.
3520 (format "<<%s>>" (org-element-property :value target
)))
3522 (defun org-element-target-successor ()
3523 "Search for the next target object.
3525 Return value is a cons cell whose CAR is `target' and CDR is
3526 beginning position."
3528 (when (re-search-forward org-target-regexp nil t
)
3529 (cons 'target
(match-beginning 0)))))
3534 (defun org-element-timestamp-parser ()
3535 "Parse time stamp at point.
3537 Return a list whose CAR is `timestamp', and CDR a plist with
3538 `:type', `:raw-value', `:year-start', `:month-start',
3539 `:day-start', `:hour-start', `:minute-start', `:year-end',
3540 `:month-end', `:day-end', `:hour-end', `:minute-end',
3541 `:repeater-type', `:repeater-value', `:repeater-unit',
3542 `:warning-type', `:warning-value', `:warning-unit', `:begin',
3543 `:end', `:value' and `:post-blank' keywords.
3545 Assume point is at the beginning of the timestamp."
3547 (let* ((begin (point))
3548 (activep (eq (char-after) ?
<))
3551 (looking-at "\\([<[]\\(%%\\)?.*?\\)[]>]\\(?:--\\([<[].*?[]>]\\)\\)?")
3552 (match-string-no-properties 0)))
3553 (date-start (match-string-no-properties 1))
3554 (date-end (match-string 3))
3555 (diaryp (match-beginning 2))
3556 (post-blank (progn (goto-char (match-end 0))
3557 (skip-chars-forward " \t")))
3562 "[012]?[0-9]:[0-5][0-9]\\(-\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)"
3564 (cons (string-to-number (match-string 2 date-start
))
3565 (string-to-number (match-string 3 date-start
)))))
3566 (type (cond (diaryp 'diary
)
3567 ((and activep
(or date-end time-range
)) 'active-range
)
3569 ((or date-end time-range
) 'inactive-range
)
3573 (string-match "\\([.+]?\\+\\)\\([0-9]+\\)\\([hdwmy]\\)"
3577 (let ((type (match-string 1 raw-value
)))
3578 (cond ((equal "++" type
) 'catch-up
)
3579 ((equal ".+" type
) 'restart
)
3581 :repeater-value
(string-to-number (match-string 2 raw-value
))
3583 (case (string-to-char (match-string 3 raw-value
))
3584 (?h
'hour
) (?d
'day
) (?w
'week
) (?m
'month
) (t 'year
)))))
3587 (string-match "\\(-\\)?-\\([0-9]+\\)\\([hdwmy]\\)" raw-value
)
3589 :warning-type
(if (match-string 1 raw-value
) 'first
'all
)
3590 :warning-value
(string-to-number (match-string 2 raw-value
))
3592 (case (string-to-char (match-string 3 raw-value
))
3593 (?h
'hour
) (?d
'day
) (?w
'week
) (?m
'month
) (t 'year
)))))
3594 year-start month-start day-start hour-start minute-start year-end
3595 month-end day-end hour-end minute-end
)
3596 ;; Parse date-start.
3598 (let ((date (org-parse-time-string date-start t
)))
3599 (setq year-start
(nth 5 date
)
3600 month-start
(nth 4 date
)
3601 day-start
(nth 3 date
)
3602 hour-start
(nth 2 date
)
3603 minute-start
(nth 1 date
))))
3604 ;; Compute date-end. It can be provided directly in time-stamp,
3605 ;; or extracted from time range. Otherwise, it defaults to the
3606 ;; same values as date-start.
3608 (let ((date (and date-end
(org-parse-time-string date-end t
))))
3609 (setq year-end
(or (nth 5 date
) year-start
)
3610 month-end
(or (nth 4 date
) month-start
)
3611 day-end
(or (nth 3 date
) day-start
)
3612 hour-end
(or (nth 2 date
) (car time-range
) hour-start
)
3613 minute-end
(or (nth 1 date
) (cdr time-range
) minute-start
))))
3615 (nconc (list :type type
3616 :raw-value raw-value
3617 :year-start year-start
3618 :month-start month-start
3619 :day-start day-start
3620 :hour-start hour-start
3621 :minute-start minute-start
3623 :month-end month-end
3626 :minute-end minute-end
3629 :post-blank post-blank
)
3633 (defun org-element-timestamp-interpreter (timestamp contents
)
3634 "Interpret TIMESTAMP object as Org syntax.
3636 (let* ((repeat-string
3638 (case (org-element-property :repeater-type timestamp
)
3639 (cumulate "+") (catch-up "++") (restart ".+"))
3640 (let ((val (org-element-property :repeater-value timestamp
)))
3641 (and val
(number-to-string val
)))
3642 (case (org-element-property :repeater-unit timestamp
)
3643 (hour "h") (day "d") (week "w") (month "m") (year "y"))))
3646 (case (org-element-property :warning-type timestamp
)
3649 (let ((val (org-element-property :warning-value timestamp
)))
3650 (and val
(number-to-string val
)))
3651 (case (org-element-property :warning-unit timestamp
)
3652 (hour "h") (day "d") (week "w") (month "m") (year "y"))))
3654 ;; Build an Org timestamp string from TIME. ACTIVEP is
3655 ;; non-nil when time stamp is active. If WITH-TIME-P is
3656 ;; non-nil, add a time part. HOUR-END and MINUTE-END
3657 ;; specify a time range in the timestamp. REPEAT-STRING is
3658 ;; the repeater string, if any.
3659 (lambda (time activep
&optional with-time-p hour-end minute-end
)
3660 (let ((ts (format-time-string
3661 (funcall (if with-time-p
'cdr
'car
)
3662 org-time-stamp-formats
)
3664 (when (and hour-end minute-end
)
3665 (string-match "[012]?[0-9]:[0-5][0-9]" ts
)
3668 (format "\\&-%02d:%02d" hour-end minute-end
)
3670 (unless activep
(setq ts
(format "[%s]" (substring ts
1 -
1))))
3671 (dolist (s (list repeat-string warning-string
))
3672 (when (org-string-nw-p s
)
3673 (setq ts
(concat (substring ts
0 -
1)
3676 (substring ts -
1)))))
3679 (type (org-element-property :type timestamp
)))
3682 (let* ((minute-start (org-element-property :minute-start timestamp
))
3683 (minute-end (org-element-property :minute-end timestamp
))
3684 (hour-start (org-element-property :hour-start timestamp
))
3685 (hour-end (org-element-property :hour-end timestamp
))
3686 (time-range-p (and hour-start hour-end minute-start minute-end
3687 (or (/= hour-start hour-end
)
3688 (/= minute-start minute-end
)))))
3694 (org-element-property :day-start timestamp
)
3695 (org-element-property :month-start timestamp
)
3696 (org-element-property :year-start timestamp
))
3698 (and hour-start minute-start
)
3699 (and time-range-p hour-end
)
3700 (and time-range-p minute-end
))))
3701 ((active-range inactive-range
)
3702 (let ((minute-start (org-element-property :minute-start timestamp
))
3703 (minute-end (org-element-property :minute-end timestamp
))
3704 (hour-start (org-element-property :hour-start timestamp
))
3705 (hour-end (org-element-property :hour-end timestamp
)))
3708 build-ts-string
(encode-time
3712 (org-element-property :day-start timestamp
)
3713 (org-element-property :month-start timestamp
)
3714 (org-element-property :year-start timestamp
))
3715 (eq type
'active-range
)
3716 (and hour-start minute-start
))
3718 (funcall build-ts-string
3722 (org-element-property :day-end timestamp
)
3723 (org-element-property :month-end timestamp
)
3724 (org-element-property :year-end timestamp
))
3725 (eq type
'active-range
)
3726 (and hour-end minute-end
))))))))
3728 (defun org-element-timestamp-successor ()
3729 "Search for the next timestamp object.
3731 Return value is a cons cell whose CAR is `timestamp' and CDR is
3732 beginning position."
3734 (when (re-search-forward
3735 (concat org-ts-regexp-both
3737 "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
3739 "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
3741 (cons 'timestamp
(match-beginning 0)))))
3746 (defun org-element-underline-parser ()
3747 "Parse underline object at point.
3749 Return a list whose CAR is `underline' and CDR is a plist with
3750 `:begin', `:end', `:contents-begin' and `:contents-end' and
3751 `:post-blank' keywords.
3753 Assume point is at the first underscore marker."
3755 (unless (bolp) (backward-char 1))
3756 (looking-at org-emph-re
)
3757 (let ((begin (match-beginning 2))
3758 (contents-begin (match-beginning 4))
3759 (contents-end (match-end 4))
3760 (post-blank (progn (goto-char (match-end 2))
3761 (skip-chars-forward " \t")))
3766 :contents-begin contents-begin
3767 :contents-end contents-end
3768 :post-blank post-blank
)))))
3770 (defun org-element-underline-interpreter (underline contents
)
3771 "Interpret UNDERLINE object as Org syntax.
3772 CONTENTS is the contents of the object."
3773 (format "_%s_" contents
))
3778 (defun org-element-verbatim-parser ()
3779 "Parse verbatim object at point.
3781 Return a list whose CAR is `verbatim' and CDR is a plist with
3782 `:value', `:begin', `:end' and `:post-blank' keywords.
3784 Assume point is at the first equal sign marker."
3786 (unless (bolp) (backward-char 1))
3787 (looking-at org-emph-re
)
3788 (let ((begin (match-beginning 2))
3789 (value (org-match-string-no-properties 4))
3790 (post-blank (progn (goto-char (match-end 2))
3791 (skip-chars-forward " \t")))
3797 :post-blank post-blank
)))))
3799 (defun org-element-verbatim-interpreter (verbatim contents
)
3800 "Interpret VERBATIM object as Org syntax.
3802 (format "=%s=" (org-element-property :value verbatim
)))
3806 ;;; Parsing Element Starting At Point
3808 ;; `org-element--current-element' is the core function of this section.
3809 ;; It returns the Lisp representation of the element starting at
3812 ;; `org-element--current-element' makes use of special modes. They
3813 ;; are activated for fixed element chaining (i.e. `plain-list' >
3814 ;; `item') or fixed conditional element chaining (i.e. `headline' >
3815 ;; `section'). Special modes are: `first-section', `item',
3816 ;; `node-property', `quote-section', `section' and `table-row'.
3818 (defun org-element--current-element
3819 (limit &optional granularity special structure
)
3820 "Parse the element starting at point.
3822 Return value is a list like (TYPE PROPS) where TYPE is the type
3823 of the element and PROPS a plist of properties associated to the
3826 Possible types are defined in `org-element-all-elements'.
3828 LIMIT bounds the search.
3830 Optional argument GRANULARITY determines the depth of the
3831 recursion. Allowed values are `headline', `greater-element',
3832 `element', `object' or nil. When it is broader than `object' (or
3833 nil), secondary values will not be parsed, since they only
3836 Optional argument SPECIAL, when non-nil, can be either
3837 `first-section', `item', `node-property', `quote-section',
3838 `section', and `table-row'.
3840 If STRUCTURE isn't provided but SPECIAL is set to `item', it will
3843 This function assumes point is always at the beginning of the
3844 element it has to parse."
3846 (let ((case-fold-search t
)
3847 ;; Determine if parsing depth allows for secondary strings
3848 ;; parsing. It only applies to elements referenced in
3849 ;; `org-element-secondary-value-alist'.
3850 (raw-secondary-p (and granularity
(not (eq granularity
'object
)))))
3854 (org-element-item-parser limit structure raw-secondary-p
))
3856 ((eq special
'table-row
) (org-element-table-row-parser limit
))
3858 ((eq special
'node-property
) (org-element-node-property-parser limit
))
3860 ((org-with-limited-levels (org-at-heading-p))
3861 (org-element-headline-parser limit raw-secondary-p
))
3862 ;; Sections (must be checked after headline).
3863 ((eq special
'section
) (org-element-section-parser limit
))
3864 ((eq special
'quote-section
) (org-element-quote-section-parser limit
))
3865 ((eq special
'first-section
)
3866 (org-element-section-parser
3867 (or (save-excursion (org-with-limited-levels (outline-next-heading)))
3869 ;; When not at bol, point is at the beginning of an item or
3870 ;; a footnote definition: next item is always a paragraph.
3871 ((not (bolp)) (org-element-paragraph-parser limit
(list (point))))
3872 ;; Planning and Clock.
3873 ((looking-at org-planning-or-clock-line-re
)
3874 (if (equal (match-string 1) org-clock-string
)
3875 (org-element-clock-parser limit
)
3876 (org-element-planning-parser limit
)))
3879 (org-element-inlinetask-parser limit raw-secondary-p
))
3880 ;; From there, elements can have affiliated keywords.
3881 (t (let ((affiliated (org-element--collect-affiliated-keywords limit
)))
3883 ;; Jumping over affiliated keywords put point off-limits.
3884 ;; Parse them as regular keywords.
3885 ((and (cdr affiliated
) (>= (point) limit
))
3886 (goto-char (car affiliated
))
3887 (org-element-keyword-parser limit nil
))
3888 ;; LaTeX Environment.
3890 "[ \t]*\\\\begin{[A-Za-z0-9*]+}\\(\\[.*?\\]\\|{.*?}\\)*[ \t]*$")
3891 (org-element-latex-environment-parser limit affiliated
))
3892 ;; Drawer and Property Drawer.
3893 ((looking-at org-drawer-regexp
)
3894 (if (equal (match-string 1) "PROPERTIES")
3895 (org-element-property-drawer-parser limit affiliated
)
3896 (org-element-drawer-parser limit affiliated
)))
3898 ((looking-at "[ \t]*:\\( \\|$\\)")
3899 (org-element-fixed-width-parser limit affiliated
))
3900 ;; Inline Comments, Blocks, Babel Calls, Dynamic Blocks and
3902 ((looking-at "[ \t]*#")
3903 (goto-char (match-end 0))
3904 (cond ((looking-at "\\(?: \\|$\\)")
3906 (org-element-comment-parser limit affiliated
))
3907 ((looking-at "\\+BEGIN_\\(\\S-+\\)")
3909 (let ((parser (assoc (upcase (match-string 1))
3910 org-element-block-name-alist
)))
3911 (if parser
(funcall (cdr parser
) limit affiliated
)
3912 (org-element-special-block-parser limit affiliated
))))
3913 ((looking-at "\\+CALL:")
3915 (org-element-babel-call-parser limit affiliated
))
3916 ((looking-at "\\+BEGIN:? ")
3918 (org-element-dynamic-block-parser limit affiliated
))
3919 ((looking-at "\\+\\S-+:")
3921 (org-element-keyword-parser limit affiliated
))
3924 (org-element-paragraph-parser limit affiliated
))))
3925 ;; Footnote Definition.
3926 ((looking-at org-footnote-definition-re
)
3927 (org-element-footnote-definition-parser limit affiliated
))
3929 ((looking-at "[ \t]*-\\{5,\\}[ \t]*$")
3930 (org-element-horizontal-rule-parser limit affiliated
))
3933 (org-element-diary-sexp-parser limit affiliated
))
3935 ((org-at-table-p t
) (org-element-table-parser limit affiliated
))
3937 ((looking-at (org-item-re))
3938 (org-element-plain-list-parser
3940 (or structure
(org-element--list-struct limit
))))
3941 ;; Default element: Paragraph.
3942 (t (org-element-paragraph-parser limit affiliated
)))))))))
3945 ;; Most elements can have affiliated keywords. When looking for an
3946 ;; element beginning, we want to move before them, as they belong to
3947 ;; that element, and, in the meantime, collect information they give
3948 ;; into appropriate properties. Hence the following function.
3950 (defun org-element--collect-affiliated-keywords (limit)
3951 "Collect affiliated keywords from point down to LIMIT.
3953 Return a list whose CAR is the position at the first of them and
3954 CDR a plist of keywords and values and move point to the
3955 beginning of the first line after them.
3957 As a special case, if element doesn't start at the beginning of
3958 the line (i.e. a paragraph starting an item), CAR is current
3959 position of point and CDR is nil."
3960 (if (not (bolp)) (list (point))
3961 (let ((case-fold-search t
)
3963 ;; RESTRICT is the list of objects allowed in parsed
3965 (restrict (org-element-restriction 'keyword
))
3967 (while (and (< (point) limit
) (looking-at org-element--affiliated-re
))
3968 (let* ((raw-kwd (upcase (match-string 1)))
3969 ;; Apply translation to RAW-KWD. From there, KWD is
3970 ;; the official keyword.
3971 (kwd (or (cdr (assoc raw-kwd
3972 org-element-keyword-translation-alist
))
3974 ;; Find main value for any keyword.
3978 (buffer-substring-no-properties
3979 (match-end 0) (point-at-eol)))))
3980 ;; PARSEDP is non-nil when keyword should have its
3982 (parsedp (member kwd org-element-parsed-keywords
))
3983 ;; If KWD is a dual keyword, find its secondary
3984 ;; value. Maybe parse it.
3985 (dualp (member kwd org-element-dual-keywords
))
3988 (let ((sec (org-match-string-no-properties 2)))
3989 (if (or (not sec
) (not parsedp
)) sec
3990 (org-element-parse-secondary-string sec restrict
)))))
3991 ;; Attribute a property name to KWD.
3992 (kwd-sym (and kwd
(intern (concat ":" (downcase kwd
))))))
3993 ;; Now set final shape for VALUE.
3995 (setq value
(org-element-parse-secondary-string value restrict
)))
3997 (setq value
(and (or value dual-value
) (cons value dual-value
))))
3998 (when (or (member kwd org-element-multiple-keywords
)
3999 ;; Attributes can always appear on multiple lines.
4000 (string-match "^ATTR_" kwd
))
4001 (setq value
(cons value
(plist-get output kwd-sym
))))
4002 ;; Eventually store the new value in OUTPUT.
4003 (setq output
(plist-put output kwd-sym value
))
4004 ;; Move to next keyword.
4006 ;; If affiliated keywords are orphaned: move back to first one.
4007 ;; They will be parsed as a paragraph.
4008 (when (looking-at "[ \t]*$") (goto-char origin
) (setq output nil
))
4010 (cons origin output
))))
4016 ;; The two major functions here are `org-element-parse-buffer', which
4017 ;; parses Org syntax inside the current buffer, taking into account
4018 ;; region, narrowing, or even visibility if specified, and
4019 ;; `org-element-parse-secondary-string', which parses objects within
4022 ;; The (almost) almighty `org-element-map' allows to apply a function
4023 ;; on elements or objects matching some type, and accumulate the
4024 ;; resulting values. In an export situation, it also skips unneeded
4025 ;; parts of the parse tree.
4027 (defun org-element-parse-buffer (&optional granularity visible-only
)
4028 "Recursively parse the buffer and return structure.
4029 If narrowing is in effect, only parse the visible part of the
4032 Optional argument GRANULARITY determines the depth of the
4033 recursion. It can be set to the following symbols:
4035 `headline' Only parse headlines.
4036 `greater-element' Don't recurse into greater elements excepted
4037 headlines and sections. Thus, elements
4038 parsed are the top-level ones.
4039 `element' Parse everything but objects and plain text.
4040 `object' Parse the complete buffer (default).
4042 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
4045 An element or an objects is represented as a list with the
4046 pattern (TYPE PROPERTIES CONTENTS), where :
4048 TYPE is a symbol describing the element or object. See
4049 `org-element-all-elements' and `org-element-all-objects' for an
4050 exhaustive list of such symbols. One can retrieve it with
4051 `org-element-type' function.
4053 PROPERTIES is the list of attributes attached to the element or
4054 object, as a plist. Although most of them are specific to the
4055 element or object type, all types share `:begin', `:end',
4056 `:post-blank' and `:parent' properties, which respectively
4057 refer to buffer position where the element or object starts,
4058 ends, the number of white spaces or blank lines after it, and
4059 the element or object containing it. Properties values can be
4060 obtained by using `org-element-property' function.
4062 CONTENTS is a list of elements, objects or raw strings
4063 contained in the current element or object, when applicable.
4064 One can access them with `org-element-contents' function.
4066 The Org buffer has `org-data' as type and nil as properties.
4067 `org-element-map' function can be used to find specific elements
4068 or objects within the parse tree.
4070 This function assumes that current major mode is `org-mode'."
4072 (goto-char (point-min))
4073 (org-skip-whitespace)
4074 (org-element--parse-elements
4075 (point-at-bol) (point-max)
4076 ;; Start in `first-section' mode so text before the first
4077 ;; headline belongs to a section.
4078 'first-section nil granularity visible-only
(list 'org-data nil
))))
4080 (defun org-element-parse-secondary-string (string restriction
&optional parent
)
4081 "Recursively parse objects in STRING and return structure.
4083 RESTRICTION is a symbol limiting the object types that will be
4086 Optional argument PARENT, when non-nil, is the element or object
4087 containing the secondary string. It is used to set correctly
4088 `:parent' property within the string."
4089 ;; Copy buffer-local variables listed in
4090 ;; `org-element-object-variables' into temporary buffer. This is
4091 ;; required since object parsing is dependent on these variables.
4092 (let ((pairs (delq nil
(mapcar (lambda (var)
4094 (cons var
(symbol-value var
))))
4095 org-element-object-variables
))))
4097 (mapc (lambda (pair) (org-set-local (car pair
) (cdr pair
))) pairs
)
4099 (let ((secondary (org-element--parse-objects
4100 (point-min) (point-max) nil restriction
)))
4102 (mapc (lambda (obj) (org-element-put-property obj
:parent parent
))
4106 (defun org-element-map
4107 (data types fun
&optional info first-match no-recursion with-affiliated
)
4108 "Map a function on selected elements or objects.
4110 DATA is a parse tree, an element, an object, a string, or a list
4111 of such constructs. TYPES is a symbol or list of symbols of
4112 elements or objects types (see `org-element-all-elements' and
4113 `org-element-all-objects' for a complete list of types). FUN is
4114 the function called on the matching element or object. It has to
4115 accept one argument: the element or object itself.
4117 When optional argument INFO is non-nil, it should be a plist
4118 holding export options. In that case, parts of the parse tree
4119 not exportable according to that property list will be skipped.
4121 When optional argument FIRST-MATCH is non-nil, stop at the first
4122 match for which FUN doesn't return nil, and return that value.
4124 Optional argument NO-RECURSION is a symbol or a list of symbols
4125 representing elements or objects types. `org-element-map' won't
4126 enter any recursive element or object whose type belongs to that
4127 list. Though, FUN can still be applied on them.
4129 When optional argument WITH-AFFILIATED is non-nil, FUN will also
4130 apply to matching objects within parsed affiliated keywords (see
4131 `org-element-parsed-keywords').
4133 Nil values returned from FUN do not appear in the results.
4139 Assuming TREE is a variable containing an Org buffer parse tree,
4140 the following example will return a flat list of all `src-block'
4141 and `example-block' elements in it:
4143 \(org-element-map tree '(example-block src-block) 'identity)
4145 The following snippet will find the first headline with a level
4146 of 1 and a \"phone\" tag, and will return its beginning position:
4148 \(org-element-map tree 'headline
4150 \(and (= (org-element-property :level hl) 1)
4151 \(member \"phone\" (org-element-property :tags hl))
4152 \(org-element-property :begin hl)))
4155 The next example will return a flat list of all `plain-list' type
4156 elements in TREE that are not a sub-list themselves:
4158 \(org-element-map tree 'plain-list 'identity nil nil 'plain-list)
4160 Eventually, this example will return a flat list of all `bold'
4161 type objects containing a `latex-snippet' type object, even
4162 looking into captions:
4164 \(org-element-map tree 'bold
4166 \(and (org-element-map b 'latex-snippet 'identity nil t) b))
4168 ;; Ensure TYPES and NO-RECURSION are a list, even of one element.
4169 (unless (listp types
) (setq types
(list types
)))
4170 (unless (listp no-recursion
) (setq no-recursion
(list no-recursion
)))
4171 ;; Recursion depth is determined by --CATEGORY.
4174 (let ((category 'greater-elements
))
4175 (mapc (lambda (type)
4176 (cond ((or (memq type org-element-all-objects
)
4177 (eq type
'plain-text
))
4178 ;; If one object is found, the function
4179 ;; has to recurse into every object.
4180 (throw 'found
'objects
))
4181 ((not (memq type org-element-greater-elements
))
4182 ;; If one regular element is found, the
4183 ;; function has to recurse, at least,
4184 ;; into every element it encounters.
4185 (and (not (eq category
'elements
))
4186 (setq category
'elements
)))))
4189 ;; Compute properties for affiliated keywords if necessary.
4191 (and with-affiliated
4192 (mapcar (lambda (kwd)
4193 (cons kwd
(intern (concat ":" (downcase kwd
)))))
4194 org-element-affiliated-keywords
)))
4200 ;; Recursively walk DATA. INFO, if non-nil, is a plist
4201 ;; holding contextual information.
4202 (let ((--type (org-element-type --data
)))
4205 ;; Ignored element in an export context.
4206 ((and info
(memq --data
(plist-get info
:ignore-list
))))
4207 ;; List of elements or objects.
4208 ((not --type
) (mapc --walk-tree --data
))
4209 ;; Unconditionally enter parse trees.
4210 ((eq --type
'org-data
)
4211 (mapc --walk-tree
(org-element-contents --data
)))
4213 ;; Check if TYPE is matching among TYPES. If so,
4214 ;; apply FUN to --DATA and accumulate return value
4215 ;; into --ACC (or exit if FIRST-MATCH is non-nil).
4216 (when (memq --type types
)
4217 (let ((result (funcall fun --data
)))
4218 (cond ((not result
))
4219 (first-match (throw '--map-first-match result
))
4220 (t (push result --acc
)))))
4221 ;; If --DATA has a secondary string that can contain
4222 ;; objects with their type among TYPES, look into it.
4223 (when (and (eq --category
'objects
) (not (stringp --data
)))
4225 (assq --type org-element-secondary-value-alist
)))
4227 (funcall --walk-tree
4228 (org-element-property (cdr sec-prop
) --data
)))))
4229 ;; If --DATA has any affiliated keywords and
4230 ;; WITH-AFFILIATED is non-nil, look for objects in
4232 (when (and with-affiliated
4233 (eq --category
'objects
)
4234 (memq --type org-element-all-elements
))
4235 (mapc (lambda (kwd-pair)
4236 (let ((kwd (car kwd-pair
))
4237 (value (org-element-property
4238 (cdr kwd-pair
) --data
)))
4239 ;; Pay attention to the type of value.
4240 ;; Preserve order for multiple keywords.
4243 ((and (member kwd org-element-multiple-keywords
)
4244 (member kwd org-element-dual-keywords
))
4245 (mapc (lambda (line)
4246 (funcall --walk-tree
(cdr line
))
4247 (funcall --walk-tree
(car line
)))
4249 ((member kwd org-element-multiple-keywords
)
4250 (mapc (lambda (line) (funcall --walk-tree line
))
4252 ((member kwd org-element-dual-keywords
)
4253 (funcall --walk-tree
(cdr value
))
4254 (funcall --walk-tree
(car value
)))
4255 (t (funcall --walk-tree value
)))))
4256 --affiliated-alist
))
4257 ;; Determine if a recursion into --DATA is possible.
4259 ;; --TYPE is explicitly removed from recursion.
4260 ((memq --type no-recursion
))
4261 ;; --DATA has no contents.
4262 ((not (org-element-contents --data
)))
4263 ;; Looking for greater elements but --DATA is simply
4264 ;; an element or an object.
4265 ((and (eq --category
'greater-elements
)
4266 (not (memq --type org-element-greater-elements
))))
4267 ;; Looking for elements but --DATA is an object.
4268 ((and (eq --category
'elements
)
4269 (memq --type org-element-all-objects
)))
4270 ;; In any other case, map contents.
4271 (t (mapc --walk-tree
(org-element-contents --data
)))))))))))
4272 (catch '--map-first-match
4273 (funcall --walk-tree data
)
4274 ;; Return value in a proper order.
4276 (put 'org-element-map
'lisp-indent-function
2)
4278 ;; The following functions are internal parts of the parser.
4280 ;; The first one, `org-element--parse-elements' acts at the element's
4283 ;; The second one, `org-element--parse-objects' applies on all objects
4284 ;; of a paragraph or a secondary string. It uses
4285 ;; `org-element--get-next-object-candidates' to optimize the search of
4286 ;; the next object in the buffer.
4288 ;; More precisely, that function looks for every allowed object type
4289 ;; first. Then, it discards failed searches, keeps further matches,
4290 ;; and searches again types matched behind point, for subsequent
4291 ;; calls. Thus, searching for a given type fails only once, and every
4292 ;; object is searched only once at top level (but sometimes more for
4295 (defun org-element--parse-elements
4296 (beg end special structure granularity visible-only acc
)
4297 "Parse elements between BEG and END positions.
4299 SPECIAL prioritize some elements over the others. It can be set
4300 to `first-section', `quote-section', `section' `item' or
4303 When value is `item', STRUCTURE will be used as the current list
4306 GRANULARITY determines the depth of the recursion. See
4307 `org-element-parse-buffer' for more information.
4309 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
4312 Elements are accumulated into ACC."
4315 ;; Visible only: skip invisible parts at the beginning of the
4317 (when (and visible-only
(org-invisible-p2))
4318 (goto-char (min (1+ (org-find-visible)) end
)))
4319 ;; When parsing only headlines, skip any text before first one.
4320 (when (and (eq granularity
'headline
) (not (org-at-heading-p)))
4321 (org-with-limited-levels (outline-next-heading)))
4323 (while (< (point) end
)
4324 ;; Find current element's type and parse it accordingly to
4326 (let* ((element (org-element--current-element
4327 end granularity special structure
))
4328 (type (org-element-type element
))
4329 (cbeg (org-element-property :contents-begin element
)))
4330 (goto-char (org-element-property :end element
))
4331 ;; Visible only: skip invisible parts between siblings.
4332 (when (and visible-only
(org-invisible-p2))
4333 (goto-char (min (1+ (org-find-visible)) end
)))
4334 ;; Fill ELEMENT contents by side-effect.
4336 ;; If element has no contents, don't modify it.
4338 ;; Greater element: parse it between `contents-begin' and
4339 ;; `contents-end'. Make sure GRANULARITY allows the
4340 ;; recursion, or ELEMENT is a headline, in which case going
4341 ;; inside is mandatory, in order to get sub-level headings.
4342 ((and (memq type org-element-greater-elements
)
4343 (or (memq granularity
'(element object nil
))
4344 (and (eq granularity
'greater-element
)
4346 (eq type
'headline
)))
4347 (org-element--parse-elements
4348 cbeg
(org-element-property :contents-end element
)
4349 ;; Possibly switch to a special mode.
4352 (if (org-element-property :quotedp element
) 'quote-section
4355 (property-drawer 'node-property
)
4357 (and (memq type
'(item plain-list
))
4358 (org-element-property :structure element
))
4359 granularity visible-only element
))
4360 ;; ELEMENT has contents. Parse objects inside, if
4361 ;; GRANULARITY allows it.
4362 ((memq granularity
'(object nil
))
4363 (org-element--parse-objects
4364 cbeg
(org-element-property :contents-end element
) element
4365 (org-element-restriction type
))))
4366 (org-element-adopt-elements acc element
)))
4370 (defun org-element--parse-objects (beg end acc restriction
)
4371 "Parse objects between BEG and END and return recursive structure.
4373 Objects are accumulated in ACC.
4375 RESTRICTION is a list of object successors which are allowed in
4376 the current object."
4377 (let ((candidates 'initial
))
4380 (narrow-to-region beg end
)
4381 (goto-char (point-min))
4382 (while (and (not (eobp))
4384 (org-element--get-next-object-candidates
4385 restriction candidates
)))
4387 (let ((pos (apply 'min
(mapcar 'cdr candidates
))))
4390 (funcall (intern (format "org-element-%s-parser"
4391 (car (rassq pos candidates
)))))))))
4392 ;; 1. Text before any object. Untabify it.
4393 (let ((obj-beg (org-element-property :begin next-object
)))
4394 (unless (= (point) obj-beg
)
4396 (org-element-adopt-elements
4398 (replace-regexp-in-string
4399 "\t" (make-string tab-width ?
)
4400 (buffer-substring-no-properties (point) obj-beg
))))))
4402 (let ((obj-end (org-element-property :end next-object
))
4403 (cont-beg (org-element-property :contents-begin next-object
)))
4404 ;; Fill contents of NEXT-OBJECT by side-effect, if it has
4405 ;; a recursive type.
4407 (memq (car next-object
) org-element-recursive-objects
))
4408 (org-element--parse-objects
4409 cont-beg
(org-element-property :contents-end next-object
)
4410 next-object
(org-element-restriction next-object
)))
4411 (setq acc
(org-element-adopt-elements acc next-object
))
4412 (goto-char obj-end
))))
4413 ;; 3. Text after last object. Untabify it.
4416 (org-element-adopt-elements
4418 (replace-regexp-in-string
4419 "\t" (make-string tab-width ?
)
4420 (buffer-substring-no-properties (point) end
)))))
4424 (defun org-element--get-next-object-candidates (restriction objects
)
4425 "Return an alist of candidates for the next object.
4427 RESTRICTION is a list of object types, as symbols. Only
4428 candidates with such types are looked after.
4430 OBJECTS is the previous candidates alist. If it is set to
4431 `initial', no search has been done before, and all symbols in
4432 RESTRICTION should be looked after.
4434 Return value is an alist whose CAR is the object type and CDR its
4435 beginning position."
4438 (if (eq objects
'initial
)
4439 ;; When searching for the first time, look for every successor
4440 ;; allowed in RESTRICTION.
4443 (funcall (intern (format "org-element-%s-successor" res
))))
4445 ;; Focus on objects returned during last search. Keep those
4446 ;; still after point. Search again objects before it.
4449 (if (>= (cdr obj
) (point)) obj
4450 (let* ((type (car obj
))
4451 (succ (or (cdr (assq type org-element-object-successor-alist
))
4454 (funcall (intern (format "org-element-%s-successor" succ
)))))))
4459 ;;; Towards A Bijective Process
4461 ;; The parse tree obtained with `org-element-parse-buffer' is really
4462 ;; a snapshot of the corresponding Org buffer. Therefore, it can be
4463 ;; interpreted and expanded into a string with canonical Org syntax.
4464 ;; Hence `org-element-interpret-data'.
4466 ;; The function relies internally on
4467 ;; `org-element--interpret-affiliated-keywords'.
4470 (defun org-element-interpret-data (data &optional pseudo-objects
)
4471 "Interpret DATA as Org syntax.
4473 DATA is a parse tree, an element, an object or a secondary string
4476 Optional argument PSEUDO-OBJECTS is a list of symbols defining
4477 new types that should be treated as objects. An unknown type not
4478 belonging to this list is seen as a pseudo-element instead. Both
4479 pseudo-objects and pseudo-elements are transparent entities, i.e.
4480 only their contents are interpreted.
4482 Return Org syntax as a string."
4483 (org-element--interpret-data-1 data nil pseudo-objects
))
4485 (defun org-element--interpret-data-1 (data parent pseudo-objects
)
4486 "Interpret DATA as Org syntax.
4488 DATA is a parse tree, an element, an object or a secondary string
4489 to interpret. PARENT is used for recursive calls. It contains
4490 the element or object containing data, or nil. PSEUDO-OBJECTS
4491 are list of symbols defining new element or object types.
4492 Unknown types that don't belong to this list are treated as
4493 pseudo-elements instead.
4495 Return Org syntax as a string."
4496 (let* ((type (org-element-type data
))
4497 ;; Find interpreter for current object or element. If it
4498 ;; doesn't exist (e.g. this is a pseudo object or element),
4499 ;; return contents, if any.
4501 (let ((fun (intern (format "org-element-%s-interpreter" type
))))
4502 (if (fboundp fun
) fun
(lambda (data contents
) contents
))))
4505 ;; Secondary string.
4509 (org-element--interpret-data-1 obj parent pseudo-objects
))
4511 ;; Full Org document.
4512 ((eq type
'org-data
)
4515 (org-element--interpret-data-1 obj parent pseudo-objects
))
4516 (org-element-contents data
) ""))
4517 ;; Plain text: remove `:parent' text property from output.
4518 ((stringp data
) (org-no-properties data
))
4519 ;; Element or object without contents.
4520 ((not (org-element-contents data
)) (funcall interpret data nil
))
4521 ;; Element or object with contents.
4523 (funcall interpret data
4524 ;; Recursively interpret contents.
4527 (org-element--interpret-data-1 obj data pseudo-objects
))
4528 (org-element-contents
4529 (if (not (memq type
'(paragraph verse-block
)))
4531 ;; Fix indentation of elements containing
4532 ;; objects. We ignore `table-row' elements
4533 ;; as they are one line long anyway.
4534 (org-element-normalize-contents
4536 ;; When normalizing first paragraph of an
4537 ;; item or a footnote-definition, ignore
4538 ;; first line's indentation.
4539 (and (eq type
'paragraph
)
4540 (equal data
(car (org-element-contents parent
)))
4541 (memq (org-element-type parent
)
4542 '(footnote-definition item
))))))
4544 (if (memq type
'(org-data plain-text nil
)) results
4545 ;; Build white spaces. If no `:post-blank' property is
4546 ;; specified, assume its value is 0.
4547 (let ((post-blank (or (org-element-property :post-blank data
) 0)))
4548 (if (or (memq type org-element-all-objects
)
4549 (memq type pseudo-objects
))
4550 (concat results
(make-string post-blank ?\s
))
4552 (org-element--interpret-affiliated-keywords data
)
4553 (org-element-normalize-string results
)
4554 (make-string post-blank ?
\n)))))))
4556 (defun org-element--interpret-affiliated-keywords (element)
4557 "Return ELEMENT's affiliated keywords as Org syntax.
4558 If there is no affiliated keyword, return the empty string."
4559 (let ((keyword-to-org
4563 (when (member key org-element-dual-keywords
)
4564 (setq dual
(cdr value
) value
(car value
)))
4567 (format "[%s]" (org-element-interpret-data dual
)))
4569 (if (member key org-element-parsed-keywords
)
4570 (org-element-interpret-data value
)
4575 (let ((value (org-element-property prop element
))
4576 (keyword (upcase (substring (symbol-name prop
) 1))))
4578 (if (or (member keyword org-element-multiple-keywords
)
4579 ;; All attribute keywords can have multiple lines.
4580 (string-match "^ATTR_" keyword
))
4581 (mapconcat (lambda (line) (funcall keyword-to-org keyword line
))
4584 (funcall keyword-to-org keyword value
)))))
4585 ;; List all ELEMENT's properties matching an attribute line or an
4586 ;; affiliated keyword, but ignore translated keywords since they
4587 ;; cannot belong to the property list.
4588 (loop for prop in
(nth 1 element
) by
'cddr
4589 when
(let ((keyword (upcase (substring (symbol-name prop
) 1))))
4590 (or (string-match "^ATTR_" keyword
)
4592 (member keyword org-element-affiliated-keywords
)
4594 org-element-keyword-translation-alist
)))))
4598 ;; Because interpretation of the parse tree must return the same
4599 ;; number of blank lines between elements and the same number of white
4600 ;; space after objects, some special care must be given to white
4603 ;; The first function, `org-element-normalize-string', ensures any
4604 ;; string different from the empty string will end with a single
4605 ;; newline character.
4607 ;; The second function, `org-element-normalize-contents', removes
4608 ;; global indentation from the contents of the current element.
4610 (defun org-element-normalize-string (s)
4611 "Ensure string S ends with a single newline character.
4613 If S isn't a string return it unchanged. If S is the empty
4614 string, return it. Otherwise, return a new string with a single
4615 newline character at its end."
4617 ((not (stringp s
)) s
)
4619 (t (and (string-match "\\(\n[ \t]*\\)*\\'" s
)
4620 (replace-match "\n" nil nil s
)))))
4622 (defun org-element-normalize-contents (element &optional ignore-first
)
4623 "Normalize plain text in ELEMENT's contents.
4625 ELEMENT must only contain plain text and objects.
4627 If optional argument IGNORE-FIRST is non-nil, ignore first line's
4628 indentation to compute maximal common indentation.
4630 Return the normalized element that is element with global
4631 indentation removed from its contents. The function assumes that
4632 indentation is not done with TAB characters."
4633 (let* (ind-list ; for byte-compiler
4634 collect-inds
; for byte-compiler
4637 ;; Return list of indentations within BLOB. This is done by
4638 ;; walking recursively BLOB and updating IND-LIST along the
4639 ;; way. FIRST-FLAG is non-nil when the first string hasn't
4640 ;; been seen yet. It is required as this string is the only
4641 ;; one whose indentation doesn't happen after a newline
4643 (lambda (blob first-flag
)
4646 (when (and first-flag
(stringp object
))
4647 (setq first-flag nil
)
4648 (string-match "\\`\\( *\\)" object
)
4649 (let ((len (length (match-string 1 object
))))
4650 ;; An indentation of zero means no string will be
4651 ;; modified. Quit the process.
4652 (if (zerop len
) (throw 'zero
(setq ind-list nil
))
4653 (push len ind-list
))))
4657 ;; Avoid matching blank or empty lines.
4658 (while (and (string-match "\n\\( *\\)\\(.\\)" object start
)
4659 (not (equal (match-string 2 object
) " ")))
4660 (setq start
(match-end 0))
4661 (push (length (match-string 1 object
)) ind-list
))))
4662 ((memq (org-element-type object
) org-element-recursive-objects
)
4663 (funcall collect-inds object first-flag
))))
4664 (org-element-contents blob
))))))
4665 ;; Collect indentation list in ELEMENT. Possibly remove first
4666 ;; value if IGNORE-FIRST is non-nil.
4667 (catch 'zero
(funcall collect-inds element
(not ignore-first
)))
4668 (if (not ind-list
) element
4669 ;; Build ELEMENT back, replacing each string with the same
4670 ;; string minus common indentation.
4671 (let* (build ; For byte compiler.
4674 (lambda (blob mci first-flag
)
4675 ;; Return BLOB with all its strings indentation
4676 ;; shortened from MCI white spaces. FIRST-FLAG is
4677 ;; non-nil when the first string hasn't been seen
4682 (when (and first-flag
(stringp object
))
4683 (setq first-flag nil
)
4685 (replace-regexp-in-string
4686 (format "\\` \\{%d\\}" mci
) "" object
)))
4689 (replace-regexp-in-string
4690 (format "\n \\{%d\\}" mci
) "\n" object
))
4691 ((memq (org-element-type object
)
4692 org-element-recursive-objects
)
4693 (funcall build object mci first-flag
))
4695 (org-element-contents blob
)))
4697 (funcall build element
(apply 'min ind-list
) (not ignore-first
))))))
4703 ;; The first move is to implement a way to obtain the smallest element
4704 ;; containing point. This is the job of `org-element-at-point'. It
4705 ;; basically jumps back to the beginning of section containing point
4706 ;; and proceed, one element after the other, with
4707 ;; `org-element--current-element' until the container is found. Note:
4708 ;; When using `org-element-at-point', secondary values are never
4709 ;; parsed since the function focuses on elements, not on objects.
4711 ;; At a deeper level, `org-element-context' lists all elements and
4712 ;; objects containing point.
4714 ;; `org-element-nested-p' and `org-element-swap-A-B' may be used
4715 ;; internally by navigation and manipulation tools.
4719 (defun org-element-at-point ()
4720 "Determine closest element around point.
4722 Return value is a list like (TYPE PROPS) where TYPE is the type
4723 of the element and PROPS a plist of properties associated to the
4726 Possible types are defined in `org-element-all-elements'.
4727 Properties depend on element or object type, but always include
4728 `:begin', `:end', `:parent' and `:post-blank' properties.
4730 As a special case, if point is at the very beginning of the first
4731 item in a list or sub-list, returned element will be that list
4732 instead of the item. Likewise, if point is at the beginning of
4733 the first row of a table, returned element will be the table
4734 instead of the first row.
4736 When point is at the end of the buffer, return the innermost
4737 element ending there."
4739 (org-with-wide-buffer
4740 (let ((origin (point)) element next
)
4742 (skip-chars-backward " \r\t\n")
4744 ;; Within blank lines at the beginning of buffer, return nil.
4745 ((bobp) (throw 'exit nil
))
4746 ;; Within blank lines right after a headline, return that
4748 ((org-with-limited-levels (org-at-heading-p))
4750 (throw 'exit
(org-element-headline-parser (point-max) t
))))
4751 ;; Otherwise use cache in order to approximate current element.
4753 (let* ((cached (org-element-cache-get origin
))
4754 (begin (org-element-property :begin cached
)))
4756 ;; Nothing in cache before point: start parsing from first
4757 ;; element following headline above, or first element in
4760 (org-with-limited-levels (outline-previous-heading)
4761 (when (org-at-heading-p) (forward-line)))
4762 (skip-chars-forward " \r\t\n")
4763 (beginning-of-line))
4764 ;; Cache returned exact match: return it.
4765 ((= origin begin
) (throw 'exit cached
))
4766 ;; There's a headline between cached value and ORIGIN:
4767 ;; cached value is invalid. Start parsing from first
4768 ;; element following the headline.
4769 ((re-search-backward
4770 (org-with-limited-levels org-outline-regexp-bol
) begin t
)
4772 (skip-chars-forward " \r\t\n")
4773 (beginning-of-line))
4774 ;; Check if CACHED or any of its ancestors contain point.
4776 ;; If there is such an element, we inspect it in order to
4777 ;; know if we return it or if we need to parse its contents.
4778 ;; Otherwise, we just start parsing from current location,
4779 ;; which is right after the top-most element containing
4782 ;; As a special case, if ORIGIN is at the end of the buffer,
4783 ;; we want to return the innermost element ending there.
4785 ;; Also, if we find an ancestor and discover that we need to
4786 ;; parse its contents, make sure we don't start from
4787 ;; `:contents-begin', as we would otherwise go past CACHED
4788 ;; again. Instead, in that situation, we will resume
4789 ;; parsing from NEXT, which is located after CACHED or its
4790 ;; higher ancestor not containing point.
4793 (goto-char (or (org-element-property :contents-begin cached
)
4797 (<= (org-element-property :end up
) origin
))
4798 (goto-char (org-element-property :end up
))
4799 (setq up
(org-element-property :parent up
)))
4801 ((eobp) (setq element up
))
4802 (t (setq element up next
(point))))))))
4803 ;; Parse successively each element until we reach ORIGIN.
4804 (let ((end (or (org-element-property
4805 :contents-end
(org-element-property :parent element
))
4807 (org-with-limited-levels (outline-next-heading))
4809 parent special-flag
)
4812 (let ((e (org-element--current-element
4813 end
'element special-flag
4814 (org-element-property :structure parent
))))
4815 (org-element-put-property e
:parent parent
)
4816 (setq element
(org-element-cache-put e
))))
4817 (let ((elem-end (org-element-property :end element
))
4818 (type (org-element-type element
)))
4820 ;; Special case: ORIGIN is at the end of the buffer and
4821 ;; CACHED ends here. No element can start after it, but
4822 ;; more than one may end there. Arbitrarily, we choose
4823 ;; to return the innermost of such elements.
4824 ((and (= (point-max) origin
) (= origin elem-end
))
4825 (let ((cend (org-element-property :contents-end element
)))
4826 (if (or (not (memq type org-element-greater-elements
))
4829 (throw 'exit element
)
4831 (or next
(org-element-property :contents-begin element
)))
4832 (setq special-flag
(case type
4834 (property-drawer 'node-property
)
4838 ;; Skip any element ending before point. Also skip
4839 ;; element ending at point since we're sure that another
4840 ;; element begins after it.
4841 ((<= elem-end origin
) (goto-char elem-end
))
4842 ;; A non-greater element contains point: return it.
4843 ((not (memq type org-element-greater-elements
))
4844 (throw 'exit element
))
4845 ;; Otherwise, we have to decide if ELEMENT really
4846 ;; contains ORIGIN. In that case we start parsing from
4847 ;; contents' beginning. Otherwise we return UP as it is
4848 ;; the smallest element containing ORIGIN.
4850 ;; There is a special cases to consider, though. If
4851 ;; ORIGIN is at contents' beginning but it is also at
4852 ;; the beginning of the first item in a list or a table.
4853 ;; In that case, we need to create an anchor for that
4854 ;; list or table, so return it.
4856 (let ((cbeg (org-element-property :contents-begin element
))
4857 (cend (org-element-property :contents-end element
)))
4858 (if (or (not cbeg
) (not cend
) (> cbeg origin
) (<= cend origin
)
4859 (and (= cbeg origin
) (memq type
'(plain-list table
))))
4860 (throw 'exit element
)
4861 (goto-char (or next cbeg
))
4862 (setq special-flag
(case type
4864 (property-drawer 'node-property
)
4868 ;; Continue parsing buffer contents from new position.
4869 (setq element nil next nil
)))))))
4872 (defun org-element-context (&optional element
)
4873 "Return closest element or object around point.
4875 Return value is a list like (TYPE PROPS) where TYPE is the type
4876 of the element or object and PROPS a plist of properties
4879 Possible types are defined in `org-element-all-elements' and
4880 `org-element-all-objects'. Properties depend on element or
4881 object type, but always include `:begin', `:end', `:parent' and
4884 Optional argument ELEMENT, when non-nil, is the closest element
4885 containing point, as returned by `org-element-at-point'.
4886 Providing it allows for quicker computation."
4887 (catch 'objects-forbidden
4888 (org-with-wide-buffer
4889 (let* ((origin (point))
4890 (element (or element
(org-element-at-point)))
4891 (type (org-element-type element
)))
4892 ;; If point is inside an element containing objects or
4893 ;; a secondary string, narrow buffer to the container and
4894 ;; proceed with parsing. Otherwise, return ELEMENT.
4896 ;; At a parsed affiliated keyword, check if we're inside main
4898 ((let ((post (org-element-property :post-affiliated element
)))
4899 (and post
(< origin post
)))
4901 (let ((case-fold-search t
)) (looking-at org-element--affiliated-re
))
4903 ((not (member-ignore-case (match-string 1)
4904 org-element-parsed-keywords
))
4905 (throw 'objects-forbidden element
))
4906 ((< (match-end 0) origin
)
4907 (narrow-to-region (match-end 0) (line-end-position)))
4908 ((and (match-beginning 2)
4909 (>= origin
(match-beginning 2))
4910 (< origin
(match-end 2)))
4911 (narrow-to-region (match-beginning 2) (match-end 2)))
4912 (t (throw 'objects-forbidden element
)))
4913 ;; Also change type to retrieve correct restrictions.
4914 (setq type
'keyword
))
4915 ;; At an item, objects can only be located within tag, if any.
4917 (let ((tag (org-element-property :tag element
)))
4918 (if (not tag
) (throw 'objects-forbidden element
)
4920 (search-forward tag
(line-end-position))
4921 (goto-char (match-beginning 0))
4922 (if (and (>= origin
(point)) (< origin
(match-end 0)))
4923 (narrow-to-region (point) (match-end 0))
4924 (throw 'objects-forbidden element
)))))
4925 ;; At an headline or inlinetask, objects are in title.
4926 ((memq type
'(headline inlinetask
))
4927 (goto-char (org-element-property :begin element
))
4928 (skip-chars-forward "* ")
4929 (if (and (>= origin
(point)) (< origin
(line-end-position)))
4930 (narrow-to-region (point) (line-end-position))
4931 (throw 'objects-forbidden element
)))
4932 ;; At a paragraph, a table-row or a verse block, objects are
4933 ;; located within their contents.
4934 ((memq type
'(paragraph table-row verse-block
))
4935 (let ((cbeg (org-element-property :contents-begin element
))
4936 (cend (org-element-property :contents-end element
)))
4937 ;; CBEG is nil for table rules.
4938 (if (and cbeg cend
(>= origin cbeg
) (< origin cend
))
4939 (narrow-to-region cbeg cend
)
4940 (throw 'objects-forbidden element
))))
4941 ;; At a parsed keyword, objects are located within value.
4943 (if (not (member (org-element-property :key element
)
4944 org-element-document-properties
))
4945 (throw 'objects-forbidden element
)
4947 (search-forward ":")
4948 (if (and (>= origin
(point)) (< origin
(line-end-position)))
4949 (narrow-to-region (point) (line-end-position))
4950 (throw 'objects-forbidden element
))))
4951 ;; All other locations cannot contain objects: bail out.
4952 (t (throw 'objects-forbidden element
)))
4953 (goto-char (point-min))
4954 (let* ((restriction (org-element-restriction type
))
4956 (candidates 'initial
)
4957 (cache (org-element-cache-get element
))
4958 objects-data next update-cache-flag
)
4962 ;; Get list of next object candidates in CANDIDATES.
4963 ;; When entering for the first time PARENT, grab it
4964 ;; from cache, if available, or compute it. Then,
4965 ;; for each subsequent iteration in PARENT, always
4966 ;; compute it since we're beyond cache anyway.
4968 (let ((data (assq (point) cache
)))
4969 (if data
(setq candidates
(nth 1 (setq objects-data data
)))
4970 (push (setq objects-data
(list (point) 'initial
))
4972 (when (or next
(eq 'initial candidates
))
4974 (org-element--get-next-object-candidates
4975 restriction candidates
))
4976 (setcar (cdr objects-data
) candidates
))
4977 ;; Compare ORIGIN with next object starting position,
4980 ;; If ORIGIN is lesser or if there is no object
4981 ;; following, look for a previous object that might
4982 ;; contain it in cache. If there is no cache, we
4983 ;; didn't miss any object so simply return PARENT.
4985 ;; If ORIGIN is greater or equal, parse next
4986 ;; candidate for further processing.
4989 (rassq (apply #'min
(mapcar #'cdr candidates
))
4991 (if (or (not closest
) (> (cdr closest
) origin
))
4993 (dolist (obj (cddr objects-data
) (throw 'exit parent
))
4994 (when (<= (org-element-property :begin obj
) origin
)
4995 (if (<= (org-element-property :end obj
) origin
)
4996 ;; Object ends before ORIGIN and we
4997 ;; know next one in cache starts
4998 ;; after it: bail out.
4999 (throw 'exit parent
)
5000 (throw 'found
(setq next obj
))))))
5001 (goto-char (cdr closest
))
5003 (funcall (intern (format "org-element-%s-parser"
5005 (push next
(cddr objects-data
))))
5006 ;; Process NEXT to know if we need to skip it, return
5007 ;; it or move into it.
5008 (let ((cbeg (org-element-property :contents-begin next
))
5009 (cend (org-element-property :contents-end next
))
5010 (obj-end (org-element-property :end next
)))
5012 ;; ORIGIN is after NEXT, so skip it.
5013 ((<= obj-end origin
) (goto-char obj-end
))
5014 ;; ORIGIN is within a non-recursive next or
5015 ;; at an object boundaries: Return that object.
5016 ((or (not cbeg
) (< origin cbeg
) (>= origin cend
))
5018 (org-element-put-property next
:parent parent
)))
5019 ;; Otherwise, move into NEXT and reset flags as we
5022 (narrow-to-region (point) cend
)
5023 (org-element-put-property next
:parent parent
)
5025 restriction
(org-element-restriction next
)
5028 candidates
'initial
))))))
5029 ;; Store results in cache, if applicable.
5030 (org-element-cache-put cache element
)))))))
5032 (defun org-element-nested-p (elem-A elem-B
)
5033 "Non-nil when elements ELEM-A and ELEM-B are nested."
5034 (let ((beg-A (org-element-property :begin elem-A
))
5035 (beg-B (org-element-property :begin elem-B
))
5036 (end-A (org-element-property :end elem-A
))
5037 (end-B (org-element-property :end elem-B
)))
5038 (or (and (>= beg-A beg-B
) (<= end-A end-B
))
5039 (and (>= beg-B beg-A
) (<= end-B end-A
)))))
5041 (defun org-element-swap-A-B (elem-A elem-B
)
5042 "Swap elements ELEM-A and ELEM-B.
5043 Assume ELEM-B is after ELEM-A in the buffer. Leave point at the
5045 (goto-char (org-element-property :begin elem-A
))
5046 ;; There are two special cases when an element doesn't start at bol:
5047 ;; the first paragraph in an item or in a footnote definition.
5048 (let ((specialp (not (bolp))))
5049 ;; Only a paragraph without any affiliated keyword can be moved at
5050 ;; ELEM-A position in such a situation. Note that the case of
5051 ;; a footnote definition is impossible: it cannot contain two
5052 ;; paragraphs in a row because it cannot contain a blank line.
5054 (or (not (eq (org-element-type elem-B
) 'paragraph
))
5055 (/= (org-element-property :begin elem-B
)
5056 (org-element-property :contents-begin elem-B
))))
5057 (error "Cannot swap elements"))
5058 ;; In a special situation, ELEM-A will have no indentation. We'll
5059 ;; give it ELEM-B's (which will in, in turn, have no indentation).
5060 (let* ((ind-B (when specialp
5061 (goto-char (org-element-property :begin elem-B
))
5062 (org-get-indentation)))
5063 (beg-A (org-element-property :begin elem-A
))
5064 (end-A (save-excursion
5065 (goto-char (org-element-property :end elem-A
))
5066 (skip-chars-backward " \r\t\n")
5068 (beg-B (org-element-property :begin elem-B
))
5069 (end-B (save-excursion
5070 (goto-char (org-element-property :end elem-B
))
5071 (skip-chars-backward " \r\t\n")
5073 ;; Store overlays responsible for visibility status. We
5074 ;; also need to store their boundaries as they will be
5075 ;; removed from buffer.
5078 (mapcar (lambda (ov) (list ov
(overlay-start ov
) (overlay-end ov
)))
5079 (overlays-in beg-A end-A
))
5080 (mapcar (lambda (ov) (list ov
(overlay-start ov
) (overlay-end ov
)))
5081 (overlays-in beg-B end-B
))))
5083 (body-A (buffer-substring beg-A end-A
))
5084 (body-B (delete-and-extract-region beg-B end-B
)))
5087 (setq body-B
(replace-regexp-in-string "\\`[ \t]*" "" body-B
))
5088 (org-indent-to-column ind-B
))
5090 ;; Restore ex ELEM-A overlays.
5091 (let ((offset (- beg-B beg-A
)))
5094 (car ov
) (+ (nth 1 ov
) offset
) (+ (nth 2 ov
) offset
)))
5097 (delete-region beg-A end-A
)
5099 ;; Restore ex ELEM-B overlays.
5102 (car ov
) (- (nth 1 ov
) offset
) (- (nth 2 ov
) offset
)))
5104 (goto-char (org-element-property :end elem-B
)))))
5106 (defun org-element-remove-indentation (s &optional n
)
5107 "Remove maximum common indentation in string S and return it.
5108 When optional argument N is a positive integer, remove exactly
5109 that much characters from indentation, if possible, or return
5110 S as-is otherwise. Unlike to `org-remove-indentation', this
5111 function doesn't call `untabify' on S."
5115 (goto-char (point-min))
5116 ;; Find maximum common indentation, if not specified.
5118 (let ((min-ind (point-max)))
5120 (while (re-search-forward "^[ \t]*\\S-" nil t
)
5121 (let ((ind (1- (current-column))))
5122 (if (zerop ind
) (throw 'exit s
)
5123 (setq min-ind
(min min-ind ind
))))))
5126 ;; Remove exactly N indentation, but give up if not possible.
5128 (let ((ind (progn (skip-chars-forward " \t") (current-column))))
5129 (cond ((eolp) (delete-region (line-beginning-position) (point)))
5130 ((< ind n
) (throw 'exit s
))
5131 (t (org-indent-line-to (- ind n
))))
5139 ;; Both functions `org-element-at-point' and `org-element-context'
5140 ;; benefit from a simple caching mechanism.
5142 ;; Three public functions are provided: `org-element-cache-put',
5143 ;; `org-element-cache-get' and `org-element-cache-reset'.
5145 ;; Cache is enabled by default, but can be disabled globally with
5146 ;; `org-element-use-cache'. `org-element-cache-sync-idle-time' and
5147 ;; `org-element-cache-merge-changes-threshold' can be tweaked to
5148 ;; control caching behaviour.
5151 (defvar org-element-use-cache t
5152 "Non nil when Org parser should cache its results.
5153 This is mostly for debugging purpose.")
5155 (defvar org-element-cache-merge-changes-threshold
200
5156 "Number of characters triggering cache syncing.
5158 The cache mechanism only stores one buffer modification at any
5159 given time. When another change happens, it replaces it with
5160 a change containing both the stored modification and the current
5161 one. This is a trade-off, as merging them prevents another
5162 syncing, but every element between them is then lost.
5164 This variable determines the maximum size, in characters, we
5165 accept to lose in order to avoid syncing the cache.")
5167 (defvar org-element-cache-sync-idle-time
0.5
5168 "Number of seconds of idle time wait before syncing buffer cache.
5169 Syncing also happens when current modification is too distant
5170 from the stored one (for more information, see
5171 `org-element-cache-merge-changes-threshold').")
5176 (defvar org-element--cache nil
5177 "AVL tree used to cache elements.
5178 Each node of the tree contains an element. Comparison is done
5179 with `org-element--cache-compare'. This cache is used in
5180 `org-element-at-point'.")
5182 (defvar org-element--cache-objects nil
5183 "Hash table used as to cache objects.
5184 Key is an element, as returned by `org-element-at-point', and
5185 value is an alist where each association is:
5187 \(POS CANDIDATES . OBJECTS)
5189 where POS is a buffer position, CANDIDATES is the last know list
5190 of successors (see `org-element--get-next-object-candidates') in
5191 container starting at POS and OBJECTS is a list of objects known
5192 to live within that container, from farthest to closest.
5194 In the following example, \\alpha, bold object and \\beta start
5195 at, respectively, positions 1, 7 and 8,
5199 If the paragraph is completely parsed, OBJECTS-DATA will be
5201 \((1 nil BOLD-OBJECT ENTITY-OBJECT)
5202 \(8 nil ENTITY-OBJECT))
5204 whereas in a partially parsed paragraph, it could be
5206 \((1 ((entity . 1) (bold . 7)) ENTITY-OBJECT))
5208 This cache is used in `org-element-context'.")
5210 (defun org-element--cache-compare (a b
)
5211 "Non-nil when element A is located before element B."
5212 (let ((beg-a (org-element-property :begin a
))
5213 (beg-b (org-element-property :begin b
)))
5215 ;; Items and plain lists on the one hand, table rows and
5216 ;; tables on the other hand can start at the same position.
5217 ;; In this case, the parent element is always before its child
5219 (and (= beg-a beg-b
)
5220 (memq (org-element-type a
) '(plain-list table
))
5221 (memq (org-element-type b
) '(item table-row
))))))
5224 ;;;; Staging Buffer Changes
5226 (defvar org-element--cache-status nil
5227 "Contains data about cache validity for current buffer.
5229 Value is a vector of seven elements,
5231 [ACTIVEP BEGIN END OFFSET TIMER PREVIOUS-STATE]
5233 ACTIVEP is a boolean non-nil when changes described in the other
5234 slots are valid for current buffer.
5236 BEGIN and END are the beginning and ending position of the area
5237 for which cache cannot be trusted.
5239 OFFSET it an integer specifying the number to add to position of
5240 elements after that area.
5242 TIMER is a timer used to apply these changes to cache when Emacs
5245 PREVIOUS-STATE is a symbol referring to the state of the buffer
5246 before a change happens. It is used to know if sensitive
5247 areas (block boundaries, headlines) were modified. It can be set
5248 to nil, `headline' or `other'.")
5250 (defconst org-element--cache-opening-line
5251 (concat "^[ \t]*\\(?:"
5252 "#\\+BEGIN[:_]" "\\|"
5253 "\\\\begin{[A-Za-z0-9]+\\*?}" "\\|"
5256 "Regexp matching an element opening line.
5257 When such a line is modified, modifications may propagate after
5258 modified area. In that situation, every element between that
5259 area and next section is removed from cache.")
5261 (defconst org-element--cache-closing-line
5262 (concat "^[ \t]*\\(?:"
5263 "#\\+END\\(?:_\\|:?[ \t]*$\\)" "\\|"
5264 "\\\\end{[A-Za-z0-9]+\\*?}[ \t]*$" "\\|"
5267 "Regexp matching an element closing line.
5268 When such a line is modified, modifications may propagate before
5269 modified area. In that situation, every element between that
5270 area and previous section is removed from cache.")
5272 (defsubst org-element--cache-pending-changes-p
()
5273 "Non-nil when changes are not integrated in cache yet."
5274 (and org-element--cache-status
5275 (aref org-element--cache-status
0)))
5277 (defsubst org-element--cache-push-change
(beg end offset
)
5278 "Push change to current buffer staging area.
5279 BEG and END and the beginning and ending position of the
5280 modification area. OFFSET is the size of the change, as an
5282 (aset org-element--cache-status
1 beg
)
5283 (aset org-element--cache-status
2 end
)
5284 (aset org-element--cache-status
3 offset
)
5285 (let ((timer (aref org-element--cache-status
4)))
5286 (if timer
(timer-activate-when-idle timer t
)
5287 (aset org-element--cache-status
4
5288 (run-with-idle-timer org-element-cache-sync-idle-time
5290 #'org-element--cache-sync
5291 (current-buffer)))))
5292 (aset org-element--cache-status
0 t
))
5294 (defsubst org-element--cache-cancel-changes
()
5295 "Remove any cache change set for current buffer."
5296 (let ((timer (aref org-element--cache-status
4)))
5297 (and timer
(cancel-timer timer
)))
5298 (aset org-element--cache-status
0 nil
))
5300 (defun org-element--cache-before-change (beg end
)
5301 "Request extension of area going to be modified if needed.
5302 BEG and END are the beginning and end of the range of changed
5303 text. See `before-change-functions' for more information."
5304 (let ((inhibit-quit t
))
5305 (org-with-wide-buffer
5309 (bottom (save-excursion (goto-char end
) (line-end-position)))
5311 ;; A sensitive line is a headline or a block (or drawer,
5312 ;; or latex-environment) boundary. Inserting one can
5313 ;; modify buffer drastically both above and below that
5314 ;; line, possibly making cache invalid. Therefore, we
5315 ;; need to pay special attention to changes happening to
5318 "\\(" (org-with-limited-levels org-outline-regexp-bol
) "\\)" "\\|"
5319 org-element--cache-closing-line
"\\|"
5320 org-element--cache-opening-line
)))
5322 (aset org-element--cache-status
5
5323 (cond ((not (re-search-forward sensitive-re bottom t
)) nil
)
5324 ((and (match-beginning 1)
5325 (progn (goto-char bottom
)
5326 (or (not (re-search-backward sensitive-re
5328 (match-beginning 1))))
5332 (defun org-element--cache-record-change (beg end pre
)
5333 "Update buffer modifications for current buffer.
5335 BEG and END are the beginning and end of the range of changed
5336 text, and the length in bytes of the pre-change text replaced by
5337 that range. See `after-change-functions' for more information.
5339 If there are already pending changes, try to merge them into
5340 a bigger change record. If that's not possible, the function
5341 will first synchronize cache with previous change and store the
5343 (let ((inhibit-quit t
))
5344 (when (and org-element-use-cache org-element--cache
)
5345 (org-with-wide-buffer
5349 (bottom (save-excursion (goto-char end
) (line-end-position))))
5350 (org-with-limited-levels
5352 ;; Determine if modified area needs to be extended,
5353 ;; according to both previous and current state. We make
5354 ;; a special case for headline editing: if a headline is
5355 ;; modified but not removed, do not extend.
5356 (when (let ((previous-state (aref org-element--cache-status
5))
5358 (concat "\\(" org-outline-regexp-bol
"\\)" "\\|"
5359 org-element--cache-closing-line
"\\|"
5360 org-element--cache-opening-line
)))
5361 (cond ((eq previous-state
'other
))
5362 ((not (re-search-forward sensitive-re bottom t
))
5363 (eq previous-state
'headline
))
5364 ((match-beginning 1)
5365 (or (not (eq previous-state
'headline
))
5366 (and (progn (goto-char bottom
)
5368 sensitive-re
(match-end 1) t
))
5369 (not (match-beginning 1)))))
5371 ;; Effectively extend modified area.
5372 (setq top
(progn (goto-char top
)
5373 (outline-previous-heading)
5374 ;; Headline above is inclusive.
5376 (setq bottom
(progn (goto-char bottom
)
5377 (outline-next-heading)
5378 ;; Headline below is exclusive.
5379 (if (eobp) (point) (1- (point))))))))
5381 (let ((offset (- end beg pre
)))
5382 (if (not (org-element--cache-pending-changes-p))
5383 ;; No pending changes. Store the new ones.
5384 (org-element--cache-push-change top
(- bottom offset
) offset
)
5385 (let* ((current-start (aref org-element--cache-status
1))
5386 (current-end (+ (aref org-element--cache-status
2)
5387 (aref org-element--cache-status
3)))
5388 (gap (max (- beg current-end
) (- current-start end
))))
5389 (if (> gap org-element-cache-merge-changes-threshold
)
5390 ;; If we cannot merge two change sets (i.e. they
5391 ;; modify distinct buffer parts) first apply current
5392 ;; change set and store new one. This way, there is
5393 ;; never more than one pending change set, which
5394 ;; avoids handling costly merges.
5395 (progn (org-element--cache-sync (current-buffer))
5396 (org-element--cache-push-change
5397 top
(- bottom offset
) offset
))
5398 ;; Change sets can be merged. We can expand the area
5399 ;; that requires an update, and postpone the sync.
5400 (timer-activate-when-idle (aref org-element--cache-status
4) t
)
5401 (aset org-element--cache-status
0 t
)
5402 (aset org-element--cache-status
1 (min top current-start
))
5403 (aset org-element--cache-status
2
5404 (- (max current-end bottom
) offset
))
5405 (incf (aref org-element--cache-status
3) offset
))))))))))
5408 ;;;; Synchronization
5410 (defsubst org-element--cache-shift-positions
(element offset
&optional props
)
5411 "Shift ELEMENT properties relative to buffer positions by OFFSET.
5413 Properties containing buffer positions are `:begin', `:end',
5414 `:contents-begin', `:contents-end' and `:structure'. When
5415 optional argument PROPS is a list of keywords, only shift
5416 properties provided in that list.
5418 Properties are modified by side-effect. Return ELEMENT."
5419 (let ((properties (nth 1 element
)))
5420 ;; Shift :structure property for the first plain list only: it is
5421 ;; the only one that really matters and it prevents from shifting
5422 ;; it more than once.
5423 (when (and (or (not props
) (memq :structure props
))
5424 (eq (org-element-type element
) 'plain-list
)
5425 (not (eq (org-element-type (plist-get properties
:parent
))
5427 (dolist (item (plist-get properties
:structure
))
5428 (incf (car item
) offset
)
5429 (incf (nth 6 item
) offset
)))
5430 (dolist (key '(:begin
:contents-begin
:contents-end
:end
:post-affiliated
))
5431 (let ((value (and (or (not props
) (memq key props
))
5432 (plist-get properties key
))))
5433 (and value
(plist-put properties key
(+ offset value
))))))
5436 (defun org-element--cache-sync (buffer)
5437 "Synchronize cache with recent modification in BUFFER.
5438 Elements ending before modification area are kept in cache.
5439 Elements starting after modification area have their position
5440 shifted by the size of the modification. Every other element is
5441 removed from the cache."
5442 (when (buffer-live-p buffer
)
5443 (with-current-buffer buffer
5444 (when (org-element--cache-pending-changes-p)
5446 (let ((inhibit-quit t
)
5447 (offset (aref org-element--cache-status
3))
5448 ;; END is the beginning position of the first element
5449 ;; in cache that isn't removed but needs to be
5450 ;; shifted. It will be updated during phase 1.
5451 (end (aref org-element--cache-status
2)))
5454 ;; Delete, in ascending order, all elements starting after
5455 ;; BEG, but before END.
5457 ;; BEG is the position of the first element in cache to
5458 ;; remove. It takes into consideration partially modified
5459 ;; elements (starting before changes but ending after
5460 ;; them). Though, it preserves greater elements that are
5461 ;; not affected when changes alter only their contents.
5463 ;; END is updated when necessary to include elements
5464 ;; starting after modifications but included in an element
5465 ;; altered by modifications.
5467 ;; At each iteration, we start again at tree root since
5468 ;; a deletion modifies structure of the balanced tree.
5470 (let* ((beg (aref org-element--cache-status
1))
5471 (element (org-element-cache-get (1- beg
) t
)))
5472 (if (not element
) beg
5475 (while (setq up
(org-element-property :parent up
))
5477 (memq (org-element-type up
)
5479 drawer dynamic-block inlinetask
5480 property-drawer quote-block
5482 (<= (org-element-property :contents-begin up
)
5484 (> (org-element-property :contents-end up
)
5486 ;; UP is a greater element that is
5487 ;; wrapped around the changes. We
5488 ;; only need to extend its ending
5489 ;; boundaries and those of all its
5494 (org-element--cache-shift-positions
5495 up offset
'(:contents-end
:end
))
5496 (setq up
(org-element-property
5498 (org-element-property
5501 ;; We're at top level element containing
5502 ;; ELEMENT: if it's altered by buffer
5503 ;; modifications, it is first element in
5504 ;; cache to be removed. Otherwise, that
5505 ;; first element is the following one.
5506 (if (< (org-element-property :end element
) beg
)
5507 (org-element-property :end element
)
5508 (org-element-property :begin element
))))))))
5509 (while (let ((node (avl-tree--root org-element--cache
)) data
)
5510 ;; DATA will contain the closest element from
5511 ;; BEG, always after it.
5513 (let* ((element (avl-tree--node-data node
))
5514 (pos (org-element-property :begin element
)))
5517 (setq node
(avl-tree--node-right node
)))
5519 (setq data
(avl-tree--node-data node
)
5520 node
(avl-tree--node-left node
)))
5522 (setq data
(avl-tree--node-data node
)
5525 ;; No DATA is found so there's no element left
5526 ;; after BEG. Bail out.
5527 ((not data
) (throw 'escape t
))
5528 ;; Element starts after END, it is the first
5529 ;; one that needn't be removed from cache.
5530 ;; Move to second phase.
5531 ((> (org-element-property :begin data
) end
) nil
)
5532 ;; Remove element. Extend END so that all
5533 ;; elements it may contain are also removed.
5536 (max (1- (org-element-property :end data
)) end
))
5537 (avl-tree-delete org-element--cache data nil t
))))))
5540 ;; Shift all elements starting after END by OFFSET (for an
5541 ;; offset different from 0).
5543 ;; Increasing all beginning positions by OFFSET doesn't
5544 ;; alter tree structure, so elements are modified by
5547 ;; We change all elements in decreasing order and make
5548 ;; sure to quit at the first element in cache starting
5550 (unless (zerop offset
)
5554 (if (<= (org-element-property :begin data
) end
)
5557 (org-element--cache-shift-positions data offset
)
5558 ;; Shift associated objects data, if any.
5559 (dolist (object-data
5560 (gethash data org-element--cache-objects
))
5561 (incf (car object-data
) offset
)
5562 (dolist (successor (nth 1 object-data
))
5563 (incf (cdr successor
) offset
))
5564 (dolist (object (cddr object-data
))
5565 (org-element--cache-shift-positions
5567 org-element--cache
'reverse
)))))
5568 ;; Eventually signal cache as up-to-date.
5569 (org-element--cache-cancel-changes)))))
5572 ;;;; Public Functions
5574 (defun org-element-cache-get (key &optional ignore-changes
)
5575 "Return cached data relative to KEY.
5577 KEY is either a number or an Org element, as returned by
5578 `org-element-at-point'. If KEY is a number, return closest
5579 cached data before or at position KEY. Otherwise, return cached
5580 objects contained in element KEY.
5582 In any case, return nil if no data is found, or if caching is not
5585 If changes are pending in current buffer, first synchronize the
5586 cache, unless optional argument IGNORE-CHANGES is non-nil."
5587 (when (and org-element-use-cache org-element--cache
)
5588 ;; If there are pending changes, first sync them.
5589 (when (and (not ignore-changes
) (org-element--cache-pending-changes-p))
5590 (org-element--cache-sync (current-buffer)))
5591 (if (not (wholenump key
)) (gethash key org-element--cache-objects
)
5592 (let ((node (avl-tree--root org-element--cache
)) last
)
5595 (let* ((element (avl-tree--node-data node
))
5596 (beg (org-element-property :begin element
)))
5599 (setq node
(avl-tree--node-left node
)))
5601 (if (memq (org-element-type element
) '(item table-row
))
5602 (setq last
(avl-tree--node-data node
)
5603 node
(avl-tree--node-left node
))
5604 (throw 'found
(avl-tree--node-data node
))))
5606 (setq last
(avl-tree--node-data node
)
5607 node
(avl-tree--node-right node
))))))
5610 (defun org-element-cache-put (data &optional element
)
5611 "Store DATA in current buffer's cache, if allowed.
5612 If optional argument ELEMENT is non-nil, store DATA as objects
5613 relative to it. Otherwise, store DATA as an element. Nothing
5614 will be stored if `org-element-use-cache' is nil. Return DATA."
5615 (if (not (and org-element-use-cache
(derived-mode-p 'org-mode
))) data
5616 (unless (and org-element--cache org-element--cache-objects
)
5617 (org-element-cache-reset))
5618 (if element
(puthash element data org-element--cache-objects
)
5619 (avl-tree-enter org-element--cache data
))))
5622 (defun org-element-cache-reset (&optional all
)
5623 "Reset cache in current buffer.
5624 When optional argument ALL is non-nil, reset cache in all Org
5625 buffers. This function will do nothing if
5626 `org-element-use-cache' is nil."
5628 (when org-element-use-cache
5629 (dolist (buffer (if all
(buffer-list) (list (current-buffer))))
5630 (with-current-buffer buffer
5631 (when (derived-mode-p 'org-mode
)
5632 (if (org-bound-and-true-p org-element--cache
)
5633 (avl-tree-clear org-element--cache
)
5634 (org-set-local 'org-element--cache
5635 (avl-tree-create #'org-element--cache-compare
)))
5636 (if org-element--cache-objects
(clrhash org-element--cache-objects
)
5638 'org-element--cache-objects
5639 (make-hash-table :size
1009 :weakness
'key
:test
#'eq
)))
5640 (org-set-local 'org-element--cache-status
(make-vector 6 nil
))
5641 (add-hook 'before-change-functions
5642 'org-element--cache-before-change nil t
)
5643 (add-hook 'after-change-functions
5644 'org-element--cache-record-change nil t
))))))
5648 (provide 'org-element
)
5651 ;; generated-autoload-file: "org-loaddefs.el"
5654 ;;; org-element.el ends here