org-element: Fix a code comment
[org-mode.git] / lisp / org-element.el
blobe69c3d0b771efac06ade9906d0461f7b2b6916d5
1 ;;; org-element.el --- Parser And Applications for Org syntax
3 ;; Copyright (C) 2012-2014 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
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', `section' and `table-row' types), it
35 ;; can also accept a fixed set of keywords as attributes. Those are
36 ;; called "affiliated keywords" to distinguish them from other
37 ;; keywords, which are full-fledged elements. Almost all affiliated
38 ;; keywords are referenced in `org-element-affiliated-keywords'; the
39 ;; others are export attributes and start with "ATTR_" prefix.
41 ;; Element containing other elements (and only elements) are called
42 ;; greater elements. Concerned types are: `center-block', `drawer',
43 ;; `dynamic-block', `footnote-definition', `headline', `inlinetask',
44 ;; `item', `plain-list', `property-drawer', `quote-block', `section'
45 ;; and `special-block'.
47 ;; Other element types are: `babel-call', `clock', `comment',
48 ;; `comment-block', `diary-sexp', `example-block', `export-block',
49 ;; `fixed-width', `horizontal-rule', `keyword', `latex-environment',
50 ;; `node-property', `paragraph', `planning', `src-block', `table',
51 ;; `table-row' and `verse-block'. Among them, `paragraph' and
52 ;; `verse-block' types can contain Org objects and plain text.
54 ;; Objects are related to document's contents. Some of them are
55 ;; recursive. Associated types are of the following: `bold', `code',
56 ;; `entity', `export-snippet', `footnote-reference',
57 ;; `inline-babel-call', `inline-src-block', `italic',
58 ;; `latex-fragment', `line-break', `link', `macro', `radio-target',
59 ;; `statistics-cookie', `strike-through', `subscript', `superscript',
60 ;; `table-cell', `target', `timestamp', `underline' and `verbatim'.
62 ;; Some elements also have special properties whose value can hold
63 ;; objects themselves (i.e. an item tag or a headline name). Such
64 ;; values are called "secondary strings". Any object belongs to
65 ;; either an element or a secondary string.
67 ;; Notwithstanding affiliated keywords, each greater element, element
68 ;; and object has a fixed set of properties attached to it. Among
69 ;; them, four are shared by all types: `:begin' and `:end', which
70 ;; refer to the beginning and ending buffer positions of the
71 ;; considered element or object, `:post-blank', which holds the number
72 ;; of blank lines, or white spaces, at its end and `:parent' which
73 ;; refers to the element or object containing it. Greater elements,
74 ;; elements and objects containing objects will also have
75 ;; `:contents-begin' and `:contents-end' properties to delimit
76 ;; contents. Eventually, greater elements and elements accepting
77 ;; affiliated keywords will have a `:post-affiliated' property,
78 ;; referring to the buffer position after all such keywords.
80 ;; At the lowest level, a `:parent' property is also attached to any
81 ;; string, as a text property.
83 ;; Lisp-wise, an element or an object can be represented as a list.
84 ;; It follows the pattern (TYPE PROPERTIES CONTENTS), where:
85 ;; TYPE is a symbol describing the Org element or object.
86 ;; PROPERTIES is the property list attached to it. See docstring of
87 ;; appropriate parsing function to get an exhaustive
88 ;; list.
89 ;; CONTENTS is a list of elements, objects or raw strings contained
90 ;; in the current element or object, when applicable.
92 ;; An Org buffer is a nested list of such elements and objects, whose
93 ;; type is `org-data' and properties is nil.
95 ;; The first part of this file defines Org syntax, while the second
96 ;; one provide accessors and setters functions.
98 ;; The next part implements a parser and an interpreter for each
99 ;; element and object type in Org syntax.
101 ;; The following part creates a fully recursive buffer parser. It
102 ;; also provides a tool to map a function to elements or objects
103 ;; matching some criteria in the parse tree. Functions of interest
104 ;; are `org-element-parse-buffer', `org-element-map' and, to a lesser
105 ;; extent, `org-element-parse-secondary-string'.
107 ;; The penultimate part is the cradle of an interpreter for the
108 ;; obtained parse tree: `org-element-interpret-data'.
110 ;; The library ends by furnishing `org-element-at-point' function, and
111 ;; a way to give information about document structure around point
112 ;; with `org-element-context'. A simple cache mechanism is also
113 ;; provided for these functions.
116 ;;; Code:
118 (eval-when-compile (require 'cl))
119 (require 'org)
120 (require 'avl-tree)
124 ;;; Definitions And Rules
126 ;; Define elements, greater elements and specify recursive objects,
127 ;; along with the affiliated keywords recognized. Also set up
128 ;; restrictions on recursive objects combinations.
130 ;; These variables really act as a control center for the parsing
131 ;; process.
133 (defconst org-element-paragraph-separate
134 (concat "^\\(?:"
135 ;; Headlines, inlinetasks.
136 org-outline-regexp "\\|"
137 ;; Footnote definitions.
138 "\\[\\(?:[0-9]+\\|fn:[-_[:word:]]+\\)\\]" "\\|"
139 ;; Diary sexps.
140 "%%(" "\\|"
141 "[ \t]*\\(?:"
142 ;; Empty lines.
143 "$" "\\|"
144 ;; Tables (any type).
145 "\\(?:|\\|\\+-[-+]\\)" "\\|"
146 ;; Blocks (any type), Babel calls and keywords. Note: this
147 ;; is only an indication and need some thorough check.
148 "#\\(?:[+ ]\\|$\\)" "\\|"
149 ;; Drawers (any type) and fixed-width areas. This is also
150 ;; only an indication.
151 ":" "\\|"
152 ;; Horizontal rules.
153 "-\\{5,\\}[ \t]*$" "\\|"
154 ;; LaTeX environments.
155 "\\\\begin{\\([A-Za-z0-9]+\\*?\\)}" "\\|"
156 ;; Planning and Clock lines.
157 (regexp-opt (list org-scheduled-string
158 org-deadline-string
159 org-closed-string
160 org-clock-string))
161 "\\|"
162 ;; Lists.
163 (let ((term (case org-plain-list-ordered-item-terminator
164 (?\) ")") (?. "\\.") (otherwise "[.)]")))
165 (alpha (and org-list-allow-alphabetical "\\|[A-Za-z]")))
166 (concat "\\(?:[-+*]\\|\\(?:[0-9]+" alpha "\\)" term "\\)"
167 "\\(?:[ \t]\\|$\\)"))
168 "\\)\\)")
169 "Regexp to separate paragraphs in an Org buffer.
170 In the case of lines starting with \"#\" and \":\", this regexp
171 is not sufficient to know if point is at a paragraph ending. See
172 `org-element-paragraph-parser' for more information.")
174 (defconst org-element-all-elements
175 '(babel-call center-block clock comment comment-block diary-sexp drawer
176 dynamic-block example-block export-block fixed-width
177 footnote-definition headline horizontal-rule inlinetask item
178 keyword latex-environment node-property paragraph plain-list
179 planning property-drawer quote-block section
180 special-block src-block table table-row verse-block)
181 "Complete list of element types.")
183 (defconst org-element-greater-elements
184 '(center-block drawer dynamic-block footnote-definition headline inlinetask
185 item plain-list property-drawer quote-block section
186 special-block table)
187 "List of recursive element types aka Greater Elements.")
189 (defconst org-element-all-successors
190 '(export-snippet footnote-reference inline-babel-call inline-src-block
191 latex-or-entity line-break link macro plain-link radio-target
192 statistics-cookie sub/superscript table-cell target
193 text-markup timestamp)
194 "Complete list of successors.")
196 (defconst org-element-object-successor-alist
197 '((subscript . sub/superscript) (superscript . sub/superscript)
198 (bold . text-markup) (code . text-markup) (italic . text-markup)
199 (strike-through . text-markup) (underline . text-markup)
200 (verbatim . text-markup) (entity . latex-or-entity)
201 (latex-fragment . latex-or-entity))
202 "Alist of translations between object type and successor name.
203 Sharing the same successor comes handy when, for example, the
204 regexp matching one object can also match the other object.")
206 (defconst org-element-all-objects
207 '(bold code entity export-snippet footnote-reference inline-babel-call
208 inline-src-block italic line-break latex-fragment link macro
209 radio-target statistics-cookie strike-through subscript superscript
210 table-cell target timestamp underline verbatim)
211 "Complete list of object types.")
213 (defconst org-element-recursive-objects
214 '(bold italic link subscript radio-target strike-through superscript
215 table-cell underline)
216 "List of recursive object types.")
218 (defvar org-element-block-name-alist
219 '(("CENTER" . org-element-center-block-parser)
220 ("COMMENT" . org-element-comment-block-parser)
221 ("EXAMPLE" . org-element-example-block-parser)
222 ("QUOTE" . org-element-quote-block-parser)
223 ("SRC" . org-element-src-block-parser)
224 ("VERSE" . org-element-verse-block-parser))
225 "Alist between block names and the associated parsing function.
226 Names must be uppercase. Any block whose name has no association
227 is parsed with `org-element-special-block-parser'.")
229 (defconst org-element-link-type-is-file
230 '("file" "file+emacs" "file+sys" "docview")
231 "List of link types equivalent to \"file\".
232 Only these types can accept search options and an explicit
233 application to open them.")
235 (defconst org-element-affiliated-keywords
236 '("CAPTION" "DATA" "HEADER" "HEADERS" "LABEL" "NAME" "PLOT" "RESNAME" "RESULT"
237 "RESULTS" "SOURCE" "SRCNAME" "TBLNAME")
238 "List of affiliated keywords as strings.
239 By default, all keywords setting attributes (i.e. \"ATTR_LATEX\")
240 are affiliated keywords and need not to be in this list.")
242 (defconst org-element-keyword-translation-alist
243 '(("DATA" . "NAME") ("LABEL" . "NAME") ("RESNAME" . "NAME")
244 ("SOURCE" . "NAME") ("SRCNAME" . "NAME") ("TBLNAME" . "NAME")
245 ("RESULT" . "RESULTS") ("HEADERS" . "HEADER"))
246 "Alist of usual translations for keywords.
247 The key is the old name and the value the new one. The property
248 holding their value will be named after the translated name.")
250 (defconst org-element-multiple-keywords '("CAPTION" "HEADER")
251 "List of affiliated keywords that can occur more than once in an element.
253 Their value will be consed into a list of strings, which will be
254 returned as the value of the property.
256 This list is checked after translations have been applied. See
257 `org-element-keyword-translation-alist'.
259 By default, all keywords setting attributes (i.e. \"ATTR_LATEX\")
260 allow multiple occurrences and need not to be in this list.")
262 (defconst org-element-parsed-keywords '("CAPTION")
263 "List of affiliated keywords whose value can be parsed.
265 Their value will be stored as a secondary string: a list of
266 strings and objects.
268 This list is checked after translations have been applied. See
269 `org-element-keyword-translation-alist'.")
271 (defconst org-element-dual-keywords '("CAPTION" "RESULTS")
272 "List of affiliated keywords which can have a secondary value.
274 In Org syntax, they can be written with optional square brackets
275 before the colons. For example, RESULTS keyword can be
276 associated to a hash value with the following:
278 #+RESULTS[hash-string]: some-source
280 This list is checked after translations have been applied. See
281 `org-element-keyword-translation-alist'.")
283 (defconst org-element-document-properties '("AUTHOR" "DATE" "TITLE")
284 "List of properties associated to the whole document.
285 Any keyword in this list will have its value parsed and stored as
286 a secondary string.")
288 (defconst org-element--affiliated-re
289 (format "[ \t]*#\\+\\(?:%s\\):\\(?: \\|$\\)"
290 (concat
291 ;; Dual affiliated keywords.
292 (format "\\(?1:%s\\)\\(?:\\[\\(.*\\)\\]\\)?"
293 (regexp-opt org-element-dual-keywords))
294 "\\|"
295 ;; Regular affiliated keywords.
296 (format "\\(?1:%s\\)"
297 (regexp-opt
298 (org-remove-if
299 #'(lambda (keyword)
300 (member keyword org-element-dual-keywords))
301 org-element-affiliated-keywords)))
302 "\\|"
303 ;; Export attributes.
304 "\\(?1:ATTR_[-_A-Za-z0-9]+\\)"))
305 "Regexp matching any affiliated keyword.
307 Keyword name is put in match group 1. Moreover, if keyword
308 belongs to `org-element-dual-keywords', put the dual value in
309 match group 2.
311 Don't modify it, set `org-element-affiliated-keywords' instead.")
313 (defconst org-element-object-restrictions
314 (let* ((standard-set
315 (remq 'plain-link (remq 'table-cell org-element-all-successors)))
316 (standard-set-no-line-break (remq 'line-break standard-set)))
317 `((bold ,@standard-set)
318 (footnote-reference ,@standard-set)
319 (headline ,@standard-set-no-line-break)
320 (inlinetask ,@standard-set-no-line-break)
321 (italic ,@standard-set)
322 (item ,@standard-set-no-line-break)
323 (keyword ,@standard-set)
324 ;; Ignore all links excepted plain links in a link description.
325 ;; Also ignore radio-targets and line breaks.
326 (link export-snippet inline-babel-call inline-src-block latex-or-entity
327 macro plain-link statistics-cookie sub/superscript text-markup)
328 (paragraph ,@standard-set)
329 ;; Remove any variable object from radio target as it would
330 ;; prevent it from being properly recognized.
331 (radio-target latex-or-entity sub/superscript)
332 (strike-through ,@standard-set)
333 (subscript ,@standard-set)
334 (superscript ,@standard-set)
335 ;; Ignore inline babel call and inline src block as formulas are
336 ;; possible. Also ignore line breaks and statistics cookies.
337 (table-cell export-snippet footnote-reference latex-or-entity link macro
338 radio-target sub/superscript target text-markup timestamp)
339 (table-row table-cell)
340 (underline ,@standard-set)
341 (verse-block ,@standard-set)))
342 "Alist of objects restrictions.
344 CAR is an element or object type containing objects and CDR is
345 a list of successors that will be called within an element or
346 object of such type.
348 For example, in a `radio-target' object, one can only find
349 entities, latex-fragments, subscript and superscript.
351 This alist also applies to secondary string. For example, an
352 `headline' type element doesn't directly contain objects, but
353 still has an entry since one of its properties (`:title') does.")
355 (defconst org-element-secondary-value-alist
356 '((headline . :title)
357 (inlinetask . :title)
358 (item . :tag)
359 (footnote-reference . :inline-definition))
360 "Alist between element types and location of secondary value.")
362 (defconst org-element-object-variables '(org-link-abbrev-alist-local)
363 "List of buffer-local variables used when parsing objects.
364 These variables are copied to the temporary buffer created by
365 `org-export-secondary-string'.")
369 ;;; Accessors and Setters
371 ;; Provide four accessors: `org-element-type', `org-element-property'
372 ;; `org-element-contents' and `org-element-restriction'.
374 ;; Setter functions allow to modify elements by side effect. There is
375 ;; `org-element-put-property', `org-element-set-contents'. These
376 ;; low-level functions are useful to build a parse tree.
378 ;; `org-element-adopt-element', `org-element-set-element',
379 ;; `org-element-extract-element' and `org-element-insert-before' are
380 ;; high-level functions useful to modify a parse tree.
382 ;; `org-element-secondary-p' is a predicate used to know if a given
383 ;; object belongs to a secondary string.
385 (defsubst org-element-type (element)
386 "Return type of ELEMENT.
388 The function returns the type of the element or object provided.
389 It can also return the following special value:
390 `plain-text' for a string
391 `org-data' for a complete document
392 nil in any other case."
393 (cond
394 ((not (consp element)) (and (stringp element) 'plain-text))
395 ((symbolp (car element)) (car element))))
397 (defsubst org-element-property (property element)
398 "Extract the value from the PROPERTY of an ELEMENT."
399 (if (stringp element) (get-text-property 0 property element)
400 (plist-get (nth 1 element) property)))
402 (defsubst org-element-contents (element)
403 "Extract contents from an ELEMENT."
404 (cond ((not (consp element)) nil)
405 ((symbolp (car element)) (nthcdr 2 element))
406 (t element)))
408 (defsubst org-element-restriction (element)
409 "Return restriction associated to ELEMENT.
410 ELEMENT can be an element, an object or a symbol representing an
411 element or object type."
412 (cdr (assq (if (symbolp element) element (org-element-type element))
413 org-element-object-restrictions)))
415 (defsubst org-element-put-property (element property value)
416 "In ELEMENT set PROPERTY to VALUE.
417 Return modified element."
418 (if (stringp element) (org-add-props element nil property value)
419 (setcar (cdr element) (plist-put (nth 1 element) property value))
420 element))
422 (defsubst org-element-set-contents (element &rest contents)
423 "Set ELEMENT contents to CONTENTS.
424 Return modified element."
425 (cond ((not element) (list contents))
426 ((not (symbolp (car element))) contents)
427 ((cdr element) (setcdr (cdr element) contents))
428 (t (nconc element contents))))
430 (defun org-element-secondary-p (object)
431 "Non-nil when OBJECT belongs to a secondary string.
432 Return value is the property name, as a keyword, or nil."
433 (let* ((parent (org-element-property :parent object))
434 (property (cdr (assq (org-element-type parent)
435 org-element-secondary-value-alist))))
436 (and property
437 (memq object (org-element-property property parent))
438 property)))
440 (defsubst org-element-adopt-elements (parent &rest children)
441 "Append elements to the contents of another element.
443 PARENT is an element or object. CHILDREN can be elements,
444 objects, or a strings.
446 The function takes care of setting `:parent' property for CHILD.
447 Return parent element."
448 ;; Link every child to PARENT. If PARENT is nil, it is a secondary
449 ;; string: parent is the list itself.
450 (mapc (lambda (child)
451 (org-element-put-property child :parent (or parent children)))
452 children)
453 ;; Add CHILDREN at the end of PARENT contents.
454 (when parent
455 (apply 'org-element-set-contents
456 parent
457 (nconc (org-element-contents parent) children)))
458 ;; Return modified PARENT element.
459 (or parent children))
461 (defun org-element-extract-element (element)
462 "Extract ELEMENT from parse tree.
463 Remove element from the parse tree by side-effect, and return it
464 with its `:parent' property stripped out."
465 (let ((parent (org-element-property :parent element))
466 (secondary (org-element-secondary-p element)))
467 (if secondary
468 (org-element-put-property
469 parent secondary
470 (delq element (org-element-property secondary parent)))
471 (apply #'org-element-set-contents
472 parent
473 (delq element (org-element-contents parent))))
474 ;; Return ELEMENT with its :parent removed.
475 (org-element-put-property element :parent nil)))
477 (defun org-element-insert-before (element location)
478 "Insert ELEMENT before LOCATION in parse tree.
479 LOCATION is an element, object or string within the parse tree.
480 Parse tree is modified by side effect."
481 (let* ((parent (org-element-property :parent location))
482 (property (org-element-secondary-p location))
483 (siblings (if property (org-element-property property parent)
484 (org-element-contents parent)))
485 ;; Special case: LOCATION is the first element of an
486 ;; independent secondary string (e.g. :title property). Add
487 ;; ELEMENT in-place.
488 (specialp (and (not property)
489 (eq siblings parent)
490 (eq (car parent) location))))
491 ;; Install ELEMENT at the appropriate POSITION within SIBLINGS.
492 (cond (specialp)
493 ((or (null siblings) (eq (car siblings) location))
494 (push element siblings))
495 ((null location) (nconc siblings (list element)))
496 (t (let ((previous (cadr (memq location (reverse siblings)))))
497 (if (not previous)
498 (error "No location found to insert element")
499 (let ((next (memq previous siblings)))
500 (setcdr next (cons element (cdr next))))))))
501 ;; Store SIBLINGS at appropriate place in parse tree.
502 (cond
503 (specialp (setcdr parent (copy-sequence parent)) (setcar parent element))
504 (property (org-element-put-property parent property siblings))
505 (t (apply #'org-element-set-contents parent siblings)))
506 ;; Set appropriate :parent property.
507 (org-element-put-property element :parent parent)))
509 (defun org-element-set-element (old new)
510 "Replace element or object OLD with element or object NEW.
511 The function takes care of setting `:parent' property for NEW."
512 ;; Ensure OLD and NEW have the same parent.
513 (org-element-put-property new :parent (org-element-property :parent old))
514 (if (or (memq (org-element-type old) '(plain-text nil))
515 (memq (org-element-type new) '(plain-text nil)))
516 ;; We cannot replace OLD with NEW since one of them is not an
517 ;; object or element. We take the long path.
518 (progn (org-element-insert-before new old)
519 (org-element-extract-element old))
520 ;; Since OLD is going to be changed into NEW by side-effect, first
521 ;; make sure that every element or object within NEW has OLD as
522 ;; parent.
523 (dolist (blob (org-element-contents new))
524 (org-element-put-property blob :parent old))
525 ;; Transfer contents.
526 (apply #'org-element-set-contents old (org-element-contents new))
527 ;; Overwrite OLD's properties with NEW's.
528 (setcar (cdr old) (nth 1 new))
529 ;; Transfer type.
530 (setcar old (car new))))
534 ;;; Greater elements
536 ;; For each greater element type, we define a parser and an
537 ;; interpreter.
539 ;; A parser returns the element or object as the list described above.
540 ;; Most of them accepts no argument. Though, exceptions exist. Hence
541 ;; every element containing a secondary string (see
542 ;; `org-element-secondary-value-alist') will accept an optional
543 ;; argument to toggle parsing of that secondary string. Moreover,
544 ;; `item' parser requires current list's structure as its first
545 ;; element.
547 ;; An interpreter accepts two arguments: the list representation of
548 ;; the element or object, and its contents. The latter may be nil,
549 ;; depending on the element or object considered. It returns the
550 ;; appropriate Org syntax, as a string.
552 ;; Parsing functions must follow the naming convention:
553 ;; org-element-TYPE-parser, where TYPE is greater element's type, as
554 ;; defined in `org-element-greater-elements'.
556 ;; Similarly, interpreting functions must follow the naming
557 ;; convention: org-element-TYPE-interpreter.
559 ;; With the exception of `headline' and `item' types, greater elements
560 ;; cannot contain other greater elements of their own type.
562 ;; Beside implementing a parser and an interpreter, adding a new
563 ;; greater element requires to tweak `org-element--current-element'.
564 ;; Moreover, the newly defined type must be added to both
565 ;; `org-element-all-elements' and `org-element-greater-elements'.
568 ;;;; Center Block
570 (defun org-element-center-block-parser (limit affiliated)
571 "Parse a center block.
573 LIMIT bounds the search. AFFILIATED is a list of which CAR is
574 the buffer position at the beginning of the first affiliated
575 keyword and CDR is a plist of affiliated keywords along with
576 their value.
578 Return a list whose CAR is `center-block' and CDR is a plist
579 containing `:begin', `:end', `:contents-begin', `:contents-end',
580 `:post-blank' and `:post-affiliated' keywords.
582 Assume point is at the beginning of the block."
583 (let ((case-fold-search t))
584 (if (not (save-excursion
585 (re-search-forward "^[ \t]*#\\+END_CENTER[ \t]*$" limit t)))
586 ;; Incomplete block: parse it as a paragraph.
587 (org-element-paragraph-parser limit affiliated)
588 (let ((block-end-line (match-beginning 0)))
589 (let* ((begin (car affiliated))
590 (post-affiliated (point))
591 ;; Empty blocks have no contents.
592 (contents-begin (progn (forward-line)
593 (and (< (point) block-end-line)
594 (point))))
595 (contents-end (and contents-begin block-end-line))
596 (pos-before-blank (progn (goto-char block-end-line)
597 (forward-line)
598 (point)))
599 (end (save-excursion
600 (skip-chars-forward " \r\t\n" limit)
601 (if (eobp) (point) (line-beginning-position)))))
602 (list 'center-block
603 (nconc
604 (list :begin begin
605 :end end
606 :contents-begin contents-begin
607 :contents-end contents-end
608 :post-blank (count-lines pos-before-blank end)
609 :post-affiliated post-affiliated)
610 (cdr affiliated))))))))
612 (defun org-element-center-block-interpreter (center-block contents)
613 "Interpret CENTER-BLOCK element as Org syntax.
614 CONTENTS is the contents of the element."
615 (format "#+BEGIN_CENTER\n%s#+END_CENTER" contents))
618 ;;;; Drawer
620 (defun org-element-drawer-parser (limit affiliated)
621 "Parse a drawer.
623 LIMIT bounds the search. AFFILIATED is a list of which CAR is
624 the buffer position at the beginning of the first affiliated
625 keyword and CDR is a plist of affiliated keywords along with
626 their value.
628 Return a list whose CAR is `drawer' and CDR is a plist containing
629 `:drawer-name', `:begin', `:end', `:contents-begin',
630 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
632 Assume point is at beginning of drawer."
633 (let ((case-fold-search t))
634 (if (not (save-excursion (re-search-forward "^[ \t]*:END:[ \t]*$" limit t)))
635 ;; Incomplete drawer: parse it as a paragraph.
636 (org-element-paragraph-parser limit affiliated)
637 (save-excursion
638 (let* ((drawer-end-line (match-beginning 0))
639 (name (progn (looking-at org-drawer-regexp)
640 (org-match-string-no-properties 1)))
641 (begin (car affiliated))
642 (post-affiliated (point))
643 ;; Empty drawers have no contents.
644 (contents-begin (progn (forward-line)
645 (and (< (point) drawer-end-line)
646 (point))))
647 (contents-end (and contents-begin drawer-end-line))
648 (pos-before-blank (progn (goto-char drawer-end-line)
649 (forward-line)
650 (point)))
651 (end (progn (skip-chars-forward " \r\t\n" limit)
652 (if (eobp) (point) (line-beginning-position)))))
653 (list 'drawer
654 (nconc
655 (list :begin begin
656 :end end
657 :drawer-name name
658 :contents-begin contents-begin
659 :contents-end contents-end
660 :post-blank (count-lines pos-before-blank end)
661 :post-affiliated post-affiliated)
662 (cdr affiliated))))))))
664 (defun org-element-drawer-interpreter (drawer contents)
665 "Interpret DRAWER element as Org syntax.
666 CONTENTS is the contents of the element."
667 (format ":%s:\n%s:END:"
668 (org-element-property :drawer-name drawer)
669 contents))
672 ;;;; Dynamic Block
674 (defun org-element-dynamic-block-parser (limit affiliated)
675 "Parse a dynamic block.
677 LIMIT bounds the search. AFFILIATED is a list of which CAR is
678 the buffer position at the beginning of the first affiliated
679 keyword and CDR is a plist of affiliated keywords along with
680 their value.
682 Return a list whose CAR is `dynamic-block' and CDR is a plist
683 containing `:block-name', `:begin', `:end', `:contents-begin',
684 `:contents-end', `:arguments', `:post-blank' and
685 `:post-affiliated' keywords.
687 Assume point is at beginning of dynamic block."
688 (let ((case-fold-search t))
689 (if (not (save-excursion
690 (re-search-forward "^[ \t]*#\\+END:?[ \t]*$" limit t)))
691 ;; Incomplete block: parse it as a paragraph.
692 (org-element-paragraph-parser limit affiliated)
693 (let ((block-end-line (match-beginning 0)))
694 (save-excursion
695 (let* ((name (progn (looking-at org-dblock-start-re)
696 (org-match-string-no-properties 1)))
697 (arguments (org-match-string-no-properties 3))
698 (begin (car affiliated))
699 (post-affiliated (point))
700 ;; Empty blocks have no contents.
701 (contents-begin (progn (forward-line)
702 (and (< (point) block-end-line)
703 (point))))
704 (contents-end (and contents-begin block-end-line))
705 (pos-before-blank (progn (goto-char block-end-line)
706 (forward-line)
707 (point)))
708 (end (progn (skip-chars-forward " \r\t\n" limit)
709 (if (eobp) (point) (line-beginning-position)))))
710 (list 'dynamic-block
711 (nconc
712 (list :begin begin
713 :end end
714 :block-name name
715 :arguments arguments
716 :contents-begin contents-begin
717 :contents-end contents-end
718 :post-blank (count-lines pos-before-blank end)
719 :post-affiliated post-affiliated)
720 (cdr affiliated)))))))))
722 (defun org-element-dynamic-block-interpreter (dynamic-block contents)
723 "Interpret DYNAMIC-BLOCK element as Org syntax.
724 CONTENTS is the contents of the element."
725 (format "#+BEGIN: %s%s\n%s#+END:"
726 (org-element-property :block-name dynamic-block)
727 (let ((args (org-element-property :arguments dynamic-block)))
728 (and args (concat " " args)))
729 contents))
732 ;;;; Footnote Definition
734 (defun org-element-footnote-definition-parser (limit affiliated)
735 "Parse a footnote definition.
737 LIMIT bounds the search. AFFILIATED is a list of which CAR is
738 the buffer position at the beginning of the first affiliated
739 keyword and CDR is a plist of affiliated keywords along with
740 their value.
742 Return a list whose CAR is `footnote-definition' and CDR is
743 a plist containing `:label', `:begin' `:end', `:contents-begin',
744 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
746 Assume point is at the beginning of the footnote definition."
747 (save-excursion
748 (let* ((label (progn (looking-at org-footnote-definition-re)
749 (org-match-string-no-properties 1)))
750 (begin (car affiliated))
751 (post-affiliated (point))
752 (ending (save-excursion
753 (if (progn
754 (end-of-line)
755 (re-search-forward
756 (concat org-outline-regexp-bol "\\|"
757 org-footnote-definition-re "\\|"
758 "^\\([ \t]*\n\\)\\{2,\\}") limit 'move))
759 (match-beginning 0)
760 (point))))
761 (contents-begin (progn
762 (search-forward "]")
763 (skip-chars-forward " \r\t\n" ending)
764 (cond ((= (point) ending) nil)
765 ((= (line-beginning-position) begin) (point))
766 (t (line-beginning-position)))))
767 (contents-end (and contents-begin ending))
768 (end (progn (goto-char ending)
769 (skip-chars-forward " \r\t\n" limit)
770 (if (eobp) (point) (line-beginning-position)))))
771 (list 'footnote-definition
772 (nconc
773 (list :label label
774 :begin begin
775 :end end
776 :contents-begin contents-begin
777 :contents-end contents-end
778 :post-blank (count-lines ending end)
779 :post-affiliated post-affiliated)
780 (cdr affiliated))))))
782 (defun org-element-footnote-definition-interpreter (footnote-definition contents)
783 "Interpret FOOTNOTE-DEFINITION element as Org syntax.
784 CONTENTS is the contents of the footnote-definition."
785 (concat (format "[%s]" (org-element-property :label footnote-definition))
787 contents))
790 ;;;; Headline
792 (defun org-element-headline-parser (limit &optional raw-secondary-p)
793 "Parse a headline.
795 Return a list whose CAR is `headline' and CDR is a plist
796 containing `:raw-value', `:title', `:alt-title', `:begin',
797 `:end', `:pre-blank', `:contents-begin' and `:contents-end',
798 `:level', `:priority', `:tags', `:todo-keyword',`:todo-type',
799 `:scheduled', `:deadline', `:closed', `:archivedp', `:commentedp'
800 and `:footnote-section-p' keywords.
802 The plist also contains any property set in the property drawer,
803 with its name in upper cases and colons added at the
804 beginning (i.e. `:CUSTOM_ID').
806 When RAW-SECONDARY-P is non-nil, headline's title will not be
807 parsed as a secondary string, but as a plain string instead.
809 Assume point is at beginning of the headline."
810 (save-excursion
811 (let* ((components (org-heading-components))
812 (level (nth 1 components))
813 (todo (nth 2 components))
814 (todo-type
815 (and todo (if (member todo org-done-keywords) 'done 'todo)))
816 (tags (let ((raw-tags (nth 5 components)))
817 (and raw-tags (org-split-string raw-tags ":"))))
818 (raw-value (or (nth 4 components) ""))
819 (commentedp
820 (let ((case-fold-search nil))
821 (string-match (format "^%s\\( \\|$\\)" org-comment-string)
822 raw-value)))
823 (archivedp (member org-archive-tag tags))
824 (footnote-section-p (and org-footnote-section
825 (string= org-footnote-section raw-value)))
826 ;; Upcase property names. It avoids confusion between
827 ;; properties obtained through property drawer and default
828 ;; properties from the parser (e.g. `:end' and :END:)
829 (standard-props
830 (let (plist)
831 (mapc
832 (lambda (p)
833 (setq plist
834 (plist-put plist
835 (intern (concat ":" (upcase (car p))))
836 (cdr p))))
837 (org-entry-properties nil 'standard))
838 plist))
839 (time-props
840 ;; Read time properties on the line below the headline.
841 (save-excursion
842 (when (progn (forward-line)
843 (looking-at org-planning-or-clock-line-re))
844 (let ((end (line-end-position)) plist)
845 (while (re-search-forward
846 org-keyword-time-not-clock-regexp end t)
847 (goto-char (match-end 1))
848 (skip-chars-forward " \t")
849 (let ((keyword (match-string 1))
850 (time (org-element-timestamp-parser)))
851 (cond ((equal keyword org-scheduled-string)
852 (setq plist (plist-put plist :scheduled time)))
853 ((equal keyword org-deadline-string)
854 (setq plist (plist-put plist :deadline time)))
855 (t (setq plist (plist-put plist :closed time))))))
856 plist))))
857 (begin (point))
858 (end (save-excursion (goto-char (org-end-of-subtree t t))))
859 (pos-after-head (progn (forward-line) (point)))
860 (contents-begin (save-excursion
861 (skip-chars-forward " \r\t\n" end)
862 (and (/= (point) end) (line-beginning-position))))
863 (contents-end (and contents-begin
864 (progn (goto-char end)
865 (skip-chars-backward " \r\t\n")
866 (forward-line)
867 (point)))))
868 ;; Clean RAW-VALUE from any comment string.
869 (when commentedp
870 (let ((case-fold-search nil))
871 (setq raw-value
872 (replace-regexp-in-string
873 (concat (regexp-quote org-comment-string) "\\(?: \\|$\\)")
875 raw-value))))
876 ;; Clean TAGS from archive tag, if any.
877 (when archivedp (setq tags (delete org-archive-tag tags)))
878 (let ((headline
879 (list 'headline
880 (nconc
881 (list :raw-value raw-value
882 :begin begin
883 :end end
884 :pre-blank
885 (if (not contents-begin) 0
886 (count-lines pos-after-head contents-begin))
887 :contents-begin contents-begin
888 :contents-end contents-end
889 :level level
890 :priority (nth 3 components)
891 :tags tags
892 :todo-keyword todo
893 :todo-type todo-type
894 :post-blank (count-lines
895 (if (not contents-end) pos-after-head
896 (goto-char contents-end)
897 (forward-line)
898 (point))
899 end)
900 :footnote-section-p footnote-section-p
901 :archivedp archivedp
902 :commentedp commentedp)
903 time-props
904 standard-props))))
905 (let ((alt-title (org-element-property :ALT_TITLE headline)))
906 (when alt-title
907 (org-element-put-property
908 headline :alt-title
909 (if raw-secondary-p alt-title
910 (org-element-parse-secondary-string
911 alt-title (org-element-restriction 'headline) headline)))))
912 (org-element-put-property
913 headline :title
914 (if raw-secondary-p raw-value
915 (org-element-parse-secondary-string
916 raw-value (org-element-restriction 'headline) headline)))))))
918 (defun org-element-headline-interpreter (headline contents)
919 "Interpret HEADLINE element as Org syntax.
920 CONTENTS is the contents of the element."
921 (let* ((level (org-element-property :level headline))
922 (todo (org-element-property :todo-keyword headline))
923 (priority (org-element-property :priority headline))
924 (title (org-element-interpret-data
925 (org-element-property :title headline)))
926 (tags (let ((tag-list (if (org-element-property :archivedp headline)
927 (cons org-archive-tag
928 (org-element-property :tags headline))
929 (org-element-property :tags headline))))
930 (and tag-list
931 (format ":%s:" (mapconcat 'identity tag-list ":")))))
932 (commentedp (org-element-property :commentedp headline))
933 (pre-blank (or (org-element-property :pre-blank headline) 0))
934 (heading (concat (make-string (org-reduced-level level) ?*)
935 (and todo (concat " " todo))
936 (and commentedp (concat " " org-comment-string))
937 (and priority
938 (format " [#%s]" (char-to-string priority)))
939 (cond ((and org-footnote-section
940 (org-element-property
941 :footnote-section-p headline))
942 (concat " " org-footnote-section))
943 (title (concat " " title))))))
944 (concat heading
945 ;; Align tags.
946 (when tags
947 (cond
948 ((zerop org-tags-column) (format " %s" tags))
949 ((< org-tags-column 0)
950 (concat
951 (make-string
952 (max (- (+ org-tags-column (length heading) (length tags))) 1)
954 tags))
956 (concat
957 (make-string (max (- org-tags-column (length heading)) 1) ? )
958 tags))))
959 (make-string (1+ pre-blank) 10)
960 contents)))
963 ;;;; Inlinetask
965 (defun org-element-inlinetask-parser (limit &optional raw-secondary-p)
966 "Parse an inline task.
968 Return a list whose CAR is `inlinetask' and CDR is a plist
969 containing `:title', `:begin', `:end', `:contents-begin' and
970 `:contents-end', `:level', `:priority', `:raw-value', `:tags',
971 `:todo-keyword', `:todo-type', `:scheduled', `:deadline',
972 `:closed' and `:post-blank' keywords.
974 The plist also contains any property set in the property drawer,
975 with its name in upper cases and colons added at the
976 beginning (i.e. `:CUSTOM_ID').
978 When optional argument RAW-SECONDARY-P is non-nil, inline-task's
979 title will not be parsed as a secondary string, but as a plain
980 string instead.
982 Assume point is at beginning of the inline task."
983 (save-excursion
984 (let* ((begin (point))
985 (components (org-heading-components))
986 (todo (nth 2 components))
987 (todo-type (and todo
988 (if (member todo org-done-keywords) 'done 'todo)))
989 (tags (let ((raw-tags (nth 5 components)))
990 (and raw-tags (org-split-string raw-tags ":"))))
991 (raw-value (or (nth 4 components) ""))
992 ;; Upcase property names. It avoids confusion between
993 ;; properties obtained through property drawer and default
994 ;; properties from the parser (e.g. `:end' and :END:)
995 (standard-props
996 (let (plist)
997 (mapc
998 (lambda (p)
999 (setq plist
1000 (plist-put plist
1001 (intern (concat ":" (upcase (car p))))
1002 (cdr p))))
1003 (org-entry-properties nil 'standard))
1004 plist))
1005 (time-props
1006 ;; Read time properties on the line below the inlinetask
1007 ;; opening string.
1008 (save-excursion
1009 (when (progn (forward-line)
1010 (looking-at org-planning-or-clock-line-re))
1011 (let ((end (line-end-position)) plist)
1012 (while (re-search-forward
1013 org-keyword-time-not-clock-regexp end t)
1014 (goto-char (match-end 1))
1015 (skip-chars-forward " \t")
1016 (let ((keyword (match-string 1))
1017 (time (org-element-timestamp-parser)))
1018 (cond ((equal keyword org-scheduled-string)
1019 (setq plist (plist-put plist :scheduled time)))
1020 ((equal keyword org-deadline-string)
1021 (setq plist (plist-put plist :deadline time)))
1022 (t (setq plist (plist-put plist :closed time))))))
1023 plist))))
1024 (task-end (save-excursion
1025 (end-of-line)
1026 (and (re-search-forward "^\\*+ END" limit t)
1027 (match-beginning 0))))
1028 (contents-begin (progn (forward-line)
1029 (and task-end (< (point) task-end) (point))))
1030 (contents-end (and contents-begin task-end))
1031 (before-blank (if (not task-end) (point)
1032 (goto-char task-end)
1033 (forward-line)
1034 (point)))
1035 (end (progn (skip-chars-forward " \r\t\n" limit)
1036 (if (eobp) (point) (line-beginning-position))))
1037 (inlinetask
1038 (list 'inlinetask
1039 (nconc
1040 (list :raw-value raw-value
1041 :begin begin
1042 :end end
1043 :contents-begin contents-begin
1044 :contents-end contents-end
1045 :level (nth 1 components)
1046 :priority (nth 3 components)
1047 :tags tags
1048 :todo-keyword todo
1049 :todo-type todo-type
1050 :post-blank (count-lines before-blank end))
1051 time-props
1052 standard-props))))
1053 (org-element-put-property
1054 inlinetask :title
1055 (if raw-secondary-p raw-value
1056 (org-element-parse-secondary-string
1057 raw-value
1058 (org-element-restriction 'inlinetask)
1059 inlinetask))))))
1061 (defun org-element-inlinetask-interpreter (inlinetask contents)
1062 "Interpret INLINETASK element as Org syntax.
1063 CONTENTS is the contents of inlinetask."
1064 (let* ((level (org-element-property :level inlinetask))
1065 (todo (org-element-property :todo-keyword inlinetask))
1066 (priority (org-element-property :priority inlinetask))
1067 (title (org-element-interpret-data
1068 (org-element-property :title inlinetask)))
1069 (tags (let ((tag-list (org-element-property :tags inlinetask)))
1070 (and tag-list
1071 (format ":%s:" (mapconcat 'identity tag-list ":")))))
1072 (task (concat (make-string level ?*)
1073 (and todo (concat " " todo))
1074 (and priority
1075 (format " [#%s]" (char-to-string priority)))
1076 (and title (concat " " title)))))
1077 (concat task
1078 ;; Align tags.
1079 (when tags
1080 (cond
1081 ((zerop org-tags-column) (format " %s" tags))
1082 ((< org-tags-column 0)
1083 (concat
1084 (make-string
1085 (max (- (+ org-tags-column (length task) (length tags))) 1)
1087 tags))
1089 (concat
1090 (make-string (max (- org-tags-column (length task)) 1) ? )
1091 tags))))
1092 ;; Prefer degenerate inlinetasks when there are no
1093 ;; contents.
1094 (when contents
1095 (concat "\n"
1096 contents
1097 (make-string level ?*) " END")))))
1100 ;;;; Item
1102 (defun org-element-item-parser (limit struct &optional raw-secondary-p)
1103 "Parse an item.
1105 STRUCT is the structure of the plain list.
1107 Return a list whose CAR is `item' and CDR is a plist containing
1108 `:bullet', `:begin', `:end', `:contents-begin', `:contents-end',
1109 `:checkbox', `:counter', `:tag', `:structure' and `:post-blank'
1110 keywords.
1112 When optional argument RAW-SECONDARY-P is non-nil, item's tag, if
1113 any, will not be parsed as a secondary string, but as a plain
1114 string instead.
1116 Assume point is at the beginning of the item."
1117 (save-excursion
1118 (beginning-of-line)
1119 (looking-at org-list-full-item-re)
1120 (let* ((begin (point))
1121 (bullet (org-match-string-no-properties 1))
1122 (checkbox (let ((box (org-match-string-no-properties 3)))
1123 (cond ((equal "[ ]" box) 'off)
1124 ((equal "[X]" box) 'on)
1125 ((equal "[-]" box) 'trans))))
1126 (counter (let ((c (org-match-string-no-properties 2)))
1127 (save-match-data
1128 (cond
1129 ((not c) nil)
1130 ((string-match "[A-Za-z]" c)
1131 (- (string-to-char (upcase (match-string 0 c)))
1132 64))
1133 ((string-match "[0-9]+" c)
1134 (string-to-number (match-string 0 c)))))))
1135 (end (progn (goto-char (nth 6 (assq (point) struct)))
1136 (unless (bolp) (forward-line))
1137 (point)))
1138 (contents-begin
1139 (progn (goto-char
1140 ;; Ignore tags in un-ordered lists: they are just
1141 ;; a part of item's body.
1142 (if (and (match-beginning 4)
1143 (save-match-data (string-match "[.)]" bullet)))
1144 (match-beginning 4)
1145 (match-end 0)))
1146 (skip-chars-forward " \r\t\n" limit)
1147 ;; If first line isn't empty, contents really start
1148 ;; at the text after item's meta-data.
1149 (if (= (point-at-bol) begin) (point) (point-at-bol))))
1150 (contents-end (progn (goto-char end)
1151 (skip-chars-backward " \r\t\n")
1152 (forward-line)
1153 (point)))
1154 (item
1155 (list 'item
1156 (list :bullet bullet
1157 :begin begin
1158 :end end
1159 ;; CONTENTS-BEGIN and CONTENTS-END may be
1160 ;; mixed up in the case of an empty item
1161 ;; separated from the next by a blank line.
1162 ;; Thus ensure the former is always the
1163 ;; smallest.
1164 :contents-begin (min contents-begin contents-end)
1165 :contents-end (max contents-begin contents-end)
1166 :checkbox checkbox
1167 :counter counter
1168 :structure struct
1169 :post-blank (count-lines contents-end end)))))
1170 (org-element-put-property
1171 item :tag
1172 (let ((raw-tag (org-list-get-tag begin struct)))
1173 (and raw-tag
1174 (if raw-secondary-p raw-tag
1175 (org-element-parse-secondary-string
1176 raw-tag (org-element-restriction 'item) item))))))))
1178 (defun org-element-item-interpreter (item contents)
1179 "Interpret ITEM element as Org syntax.
1180 CONTENTS is the contents of the element."
1181 (let* ((bullet (let ((bullet (org-element-property :bullet item)))
1182 (org-list-bullet-string
1183 (cond ((not (string-match "[0-9a-zA-Z]" bullet)) "- ")
1184 ((eq org-plain-list-ordered-item-terminator ?\)) "1)")
1185 (t "1.")))))
1186 (checkbox (org-element-property :checkbox item))
1187 (counter (org-element-property :counter item))
1188 (tag (let ((tag (org-element-property :tag item)))
1189 (and tag (org-element-interpret-data tag))))
1190 ;; Compute indentation.
1191 (ind (make-string (length bullet) 32))
1192 (item-starts-with-par-p
1193 (eq (org-element-type (car (org-element-contents item)))
1194 'paragraph)))
1195 ;; Indent contents.
1196 (concat
1197 bullet
1198 (and counter (format "[@%d] " counter))
1199 (case checkbox
1200 (on "[X] ")
1201 (off "[ ] ")
1202 (trans "[-] "))
1203 (and tag (format "%s :: " tag))
1204 (when contents
1205 (let ((contents (replace-regexp-in-string
1206 "\\(^\\)[ \t]*\\S-" ind contents nil nil 1)))
1207 (if item-starts-with-par-p (org-trim contents)
1208 (concat "\n" contents)))))))
1211 ;;;; Plain List
1213 (defun org-element--list-struct (limit)
1214 ;; Return structure of list at point. Internal function. See
1215 ;; `org-list-struct' for details.
1216 (let ((case-fold-search t)
1217 (top-ind limit)
1218 (item-re (org-item-re))
1219 (inlinetask-re (and (featurep 'org-inlinetask) "^\\*+ "))
1220 items struct)
1221 (save-excursion
1222 (catch 'exit
1223 (while t
1224 (cond
1225 ;; At limit: end all items.
1226 ((>= (point) limit)
1227 (throw 'exit
1228 (let ((end (progn (skip-chars-backward " \r\t\n")
1229 (forward-line)
1230 (point))))
1231 (dolist (item items (sort (nconc items struct)
1232 'car-less-than-car))
1233 (setcar (nthcdr 6 item) end)))))
1234 ;; At list end: end all items.
1235 ((looking-at org-list-end-re)
1236 (throw 'exit (dolist (item items (sort (nconc items struct)
1237 'car-less-than-car))
1238 (setcar (nthcdr 6 item) (point)))))
1239 ;; At a new item: end previous sibling.
1240 ((looking-at item-re)
1241 (let ((ind (save-excursion (skip-chars-forward " \t")
1242 (current-column))))
1243 (setq top-ind (min top-ind ind))
1244 (while (and items (<= ind (nth 1 (car items))))
1245 (let ((item (pop items)))
1246 (setcar (nthcdr 6 item) (point))
1247 (push item struct)))
1248 (push (progn (looking-at org-list-full-item-re)
1249 (let ((bullet (match-string-no-properties 1)))
1250 (list (point)
1252 bullet
1253 (match-string-no-properties 2) ; counter
1254 (match-string-no-properties 3) ; checkbox
1255 ;; Description tag.
1256 (and (save-match-data
1257 (string-match "[-+*]" bullet))
1258 (match-string-no-properties 4))
1259 ;; Ending position, unknown so far.
1260 nil)))
1261 items))
1262 (forward-line 1))
1263 ;; Skip empty lines.
1264 ((looking-at "^[ \t]*$") (forward-line))
1265 ;; Skip inline tasks and blank lines along the way.
1266 ((and inlinetask-re (looking-at inlinetask-re))
1267 (forward-line)
1268 (let ((origin (point)))
1269 (when (re-search-forward inlinetask-re limit t)
1270 (if (looking-at "^\\*+ END[ \t]*$") (forward-line)
1271 (goto-char origin)))))
1272 ;; At some text line. Check if it ends any previous item.
1274 (let ((ind (progn (skip-chars-forward " \t") (current-column))))
1275 (when (<= ind top-ind)
1276 (skip-chars-backward " \r\t\n")
1277 (forward-line))
1278 (while (<= ind (nth 1 (car items)))
1279 (let ((item (pop items)))
1280 (setcar (nthcdr 6 item) (line-beginning-position))
1281 (push item struct)
1282 (unless items
1283 (throw 'exit (sort struct 'car-less-than-car))))))
1284 ;; Skip blocks (any type) and drawers contents.
1285 (cond
1286 ((and (looking-at "#\\+BEGIN\\(:\\|_\\S-+\\)")
1287 (re-search-forward
1288 (format "^[ \t]*#\\+END%s[ \t]*$"
1289 (org-match-string-no-properties 1))
1290 limit t)))
1291 ((and (looking-at org-drawer-regexp)
1292 (re-search-forward "^[ \t]*:END:[ \t]*$" limit t))))
1293 (forward-line))))))))
1295 (defun org-element-plain-list-parser (limit affiliated structure)
1296 "Parse a plain list.
1298 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1299 the buffer position at the beginning of the first affiliated
1300 keyword and CDR is a plist of affiliated keywords along with
1301 their value. STRUCTURE is the structure of the plain list being
1302 parsed.
1304 Return a list whose CAR is `plain-list' and CDR is a plist
1305 containing `:type', `:begin', `:end', `:contents-begin' and
1306 `:contents-end', `:structure', `:post-blank' and
1307 `:post-affiliated' keywords.
1309 Assume point is at the beginning of the list."
1310 (save-excursion
1311 (let* ((struct (or structure (org-element--list-struct limit)))
1312 (type (cond ((org-looking-at-p "[ \t]*[A-Za-z0-9]") 'ordered)
1313 ((nth 5 (assq (point) struct)) 'descriptive)
1314 (t 'unordered)))
1315 (contents-begin (point))
1316 (begin (car affiliated))
1317 (contents-end (let* ((item (assq contents-begin struct))
1318 (ind (nth 1 item))
1319 (pos (nth 6 item)))
1320 (while (and (setq item (assq pos struct))
1321 (= (nth 1 item) ind))
1322 (setq pos (nth 6 item)))
1323 pos))
1324 (end (progn (goto-char contents-end)
1325 (skip-chars-forward " \r\t\n" limit)
1326 (if (= (point) limit) limit (line-beginning-position)))))
1327 ;; Return value.
1328 (list 'plain-list
1329 (nconc
1330 (list :type type
1331 :begin begin
1332 :end end
1333 :contents-begin contents-begin
1334 :contents-end contents-end
1335 :structure struct
1336 :post-blank (count-lines contents-end end)
1337 :post-affiliated contents-begin)
1338 (cdr affiliated))))))
1340 (defun org-element-plain-list-interpreter (plain-list contents)
1341 "Interpret PLAIN-LIST element as Org syntax.
1342 CONTENTS is the contents of the element."
1343 (with-temp-buffer
1344 (insert contents)
1345 (goto-char (point-min))
1346 (org-list-repair)
1347 (buffer-string)))
1350 ;;;; Property Drawer
1352 (defun org-element-property-drawer-parser (limit affiliated)
1353 "Parse a property drawer.
1355 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1356 the buffer position at the beginning of the first affiliated
1357 keyword and CDR is a plist of affiliated keywords along with
1358 their value.
1360 Return a list whose CAR is `property-drawer' and CDR is a plist
1361 containing `:begin', `:end', `:contents-begin', `:contents-end',
1362 `:post-blank' and `:post-affiliated' keywords.
1364 Assume point is at the beginning of the property drawer."
1365 (save-excursion
1366 (let ((case-fold-search t))
1367 (if (not (save-excursion
1368 (re-search-forward "^[ \t]*:END:[ \t]*$" limit t)))
1369 ;; Incomplete drawer: parse it as a paragraph.
1370 (org-element-paragraph-parser limit affiliated)
1371 (save-excursion
1372 (let* ((drawer-end-line (match-beginning 0))
1373 (begin (car affiliated))
1374 (post-affiliated (point))
1375 (contents-begin (progn (forward-line)
1376 (and (< (point) drawer-end-line)
1377 (point))))
1378 (contents-end (and contents-begin drawer-end-line))
1379 (pos-before-blank (progn (goto-char drawer-end-line)
1380 (forward-line)
1381 (point)))
1382 (end (progn (skip-chars-forward " \r\t\n" limit)
1383 (if (eobp) (point) (line-beginning-position)))))
1384 (list 'property-drawer
1385 (nconc
1386 (list :begin begin
1387 :end end
1388 :contents-begin contents-begin
1389 :contents-end contents-end
1390 :post-blank (count-lines pos-before-blank end)
1391 :post-affiliated post-affiliated)
1392 (cdr affiliated)))))))))
1394 (defun org-element-property-drawer-interpreter (property-drawer contents)
1395 "Interpret PROPERTY-DRAWER element as Org syntax.
1396 CONTENTS is the properties within the drawer."
1397 (format ":PROPERTIES:\n%s:END:" contents))
1400 ;;;; Quote Block
1402 (defun org-element-quote-block-parser (limit affiliated)
1403 "Parse a quote block.
1405 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1406 the buffer position at the beginning of the first affiliated
1407 keyword and CDR is a plist of affiliated keywords along with
1408 their value.
1410 Return a list whose CAR is `quote-block' and CDR is a plist
1411 containing `:begin', `:end', `:contents-begin', `:contents-end',
1412 `:post-blank' and `:post-affiliated' keywords.
1414 Assume point is at the beginning of the block."
1415 (let ((case-fold-search t))
1416 (if (not (save-excursion
1417 (re-search-forward "^[ \t]*#\\+END_QUOTE[ \t]*$" limit t)))
1418 ;; Incomplete block: parse it as a paragraph.
1419 (org-element-paragraph-parser limit affiliated)
1420 (let ((block-end-line (match-beginning 0)))
1421 (save-excursion
1422 (let* ((begin (car affiliated))
1423 (post-affiliated (point))
1424 ;; Empty blocks have no contents.
1425 (contents-begin (progn (forward-line)
1426 (and (< (point) block-end-line)
1427 (point))))
1428 (contents-end (and contents-begin block-end-line))
1429 (pos-before-blank (progn (goto-char block-end-line)
1430 (forward-line)
1431 (point)))
1432 (end (progn (skip-chars-forward " \r\t\n" limit)
1433 (if (eobp) (point) (line-beginning-position)))))
1434 (list 'quote-block
1435 (nconc
1436 (list :begin begin
1437 :end end
1438 :contents-begin contents-begin
1439 :contents-end contents-end
1440 :post-blank (count-lines pos-before-blank end)
1441 :post-affiliated post-affiliated)
1442 (cdr affiliated)))))))))
1444 (defun org-element-quote-block-interpreter (quote-block contents)
1445 "Interpret QUOTE-BLOCK element as Org syntax.
1446 CONTENTS is the contents of the element."
1447 (format "#+BEGIN_QUOTE\n%s#+END_QUOTE" contents))
1450 ;;;; Section
1452 (defun org-element-section-parser (limit)
1453 "Parse a section.
1455 LIMIT bounds the search.
1457 Return a list whose CAR is `section' and CDR is a plist
1458 containing `:begin', `:end', `:contents-begin', `contents-end'
1459 and `:post-blank' keywords."
1460 (save-excursion
1461 ;; Beginning of section is the beginning of the first non-blank
1462 ;; line after previous headline.
1463 (let ((begin (point))
1464 (end (progn (org-with-limited-levels (outline-next-heading))
1465 (point)))
1466 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
1467 (forward-line)
1468 (point))))
1469 (list 'section
1470 (list :begin begin
1471 :end end
1472 :contents-begin begin
1473 :contents-end pos-before-blank
1474 :post-blank (count-lines pos-before-blank end))))))
1476 (defun org-element-section-interpreter (section contents)
1477 "Interpret SECTION element as Org syntax.
1478 CONTENTS is the contents of the element."
1479 contents)
1482 ;;;; Special Block
1484 (defun org-element-special-block-parser (limit affiliated)
1485 "Parse a special block.
1487 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1488 the buffer position at the beginning of the first affiliated
1489 keyword and CDR is a plist of affiliated keywords along with
1490 their value.
1492 Return a list whose CAR is `special-block' and CDR is a plist
1493 containing `:type', `:begin', `:end', `:contents-begin',
1494 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
1496 Assume point is at the beginning of the block."
1497 (let* ((case-fold-search t)
1498 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
1499 (upcase (match-string-no-properties 1)))))
1500 (if (not (save-excursion
1501 (re-search-forward
1502 (format "^[ \t]*#\\+END_%s[ \t]*$" (regexp-quote type))
1503 limit t)))
1504 ;; Incomplete block: parse it as a paragraph.
1505 (org-element-paragraph-parser limit affiliated)
1506 (let ((block-end-line (match-beginning 0)))
1507 (save-excursion
1508 (let* ((begin (car affiliated))
1509 (post-affiliated (point))
1510 ;; Empty blocks have no contents.
1511 (contents-begin (progn (forward-line)
1512 (and (< (point) block-end-line)
1513 (point))))
1514 (contents-end (and contents-begin block-end-line))
1515 (pos-before-blank (progn (goto-char block-end-line)
1516 (forward-line)
1517 (point)))
1518 (end (progn (skip-chars-forward " \r\t\n" limit)
1519 (if (eobp) (point) (line-beginning-position)))))
1520 (list 'special-block
1521 (nconc
1522 (list :type type
1523 :begin begin
1524 :end end
1525 :contents-begin contents-begin
1526 :contents-end contents-end
1527 :post-blank (count-lines pos-before-blank end)
1528 :post-affiliated post-affiliated)
1529 (cdr affiliated)))))))))
1531 (defun org-element-special-block-interpreter (special-block contents)
1532 "Interpret SPECIAL-BLOCK element as Org syntax.
1533 CONTENTS is the contents of the element."
1534 (let ((block-type (org-element-property :type special-block)))
1535 (format "#+BEGIN_%s\n%s#+END_%s" block-type contents block-type)))
1539 ;;; Elements
1541 ;; For each element, a parser and an interpreter are also defined.
1542 ;; Both follow the same naming convention used for greater elements.
1544 ;; Also, as for greater elements, adding a new element type is done
1545 ;; through the following steps: implement a parser and an interpreter,
1546 ;; tweak `org-element--current-element' so that it recognizes the new
1547 ;; type and add that new type to `org-element-all-elements'.
1549 ;; As a special case, when the newly defined type is a block type,
1550 ;; `org-element-block-name-alist' has to be modified accordingly.
1553 ;;;; Babel Call
1555 (defun org-element-babel-call-parser (limit affiliated)
1556 "Parse a babel call.
1558 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1559 the buffer position at the beginning of the first affiliated
1560 keyword and CDR is a plist of affiliated keywords along with
1561 their value.
1563 Return a list whose CAR is `babel-call' and CDR is a plist
1564 containing `:begin', `:end', `:value', `:post-blank' and
1565 `:post-affiliated' as keywords."
1566 (save-excursion
1567 (let ((begin (car affiliated))
1568 (post-affiliated (point))
1569 (value (progn (let ((case-fold-search t))
1570 (re-search-forward "call:[ \t]*" nil t))
1571 (buffer-substring-no-properties (point)
1572 (line-end-position))))
1573 (pos-before-blank (progn (forward-line) (point)))
1574 (end (progn (skip-chars-forward " \r\t\n" limit)
1575 (if (eobp) (point) (line-beginning-position)))))
1576 (list 'babel-call
1577 (nconc
1578 (list :begin begin
1579 :end end
1580 :value value
1581 :post-blank (count-lines pos-before-blank end)
1582 :post-affiliated post-affiliated)
1583 (cdr affiliated))))))
1585 (defun org-element-babel-call-interpreter (babel-call contents)
1586 "Interpret BABEL-CALL element as Org syntax.
1587 CONTENTS is nil."
1588 (concat "#+CALL: " (org-element-property :value babel-call)))
1591 ;;;; Clock
1593 (defun org-element-clock-parser (limit)
1594 "Parse a clock.
1596 LIMIT bounds the search.
1598 Return a list whose CAR is `clock' and CDR is a plist containing
1599 `:status', `:value', `:time', `:begin', `:end' and `:post-blank'
1600 as keywords."
1601 (save-excursion
1602 (let* ((case-fold-search nil)
1603 (begin (point))
1604 (value (progn (search-forward org-clock-string (line-end-position) t)
1605 (skip-chars-forward " \t")
1606 (org-element-timestamp-parser)))
1607 (duration (and (search-forward " => " (line-end-position) t)
1608 (progn (skip-chars-forward " \t")
1609 (looking-at "\\(\\S-+\\)[ \t]*$"))
1610 (org-match-string-no-properties 1)))
1611 (status (if duration 'closed 'running))
1612 (post-blank (let ((before-blank (progn (forward-line) (point))))
1613 (skip-chars-forward " \r\t\n" limit)
1614 (skip-chars-backward " \t")
1615 (unless (bolp) (end-of-line))
1616 (count-lines before-blank (point))))
1617 (end (point)))
1618 (list 'clock
1619 (list :status status
1620 :value value
1621 :duration duration
1622 :begin begin
1623 :end end
1624 :post-blank post-blank)))))
1626 (defun org-element-clock-interpreter (clock contents)
1627 "Interpret CLOCK element as Org syntax.
1628 CONTENTS is nil."
1629 (concat org-clock-string " "
1630 (org-element-timestamp-interpreter
1631 (org-element-property :value clock) nil)
1632 (let ((duration (org-element-property :duration clock)))
1633 (and duration
1634 (concat " => "
1635 (apply 'format
1636 "%2s:%02s"
1637 (org-split-string duration ":")))))))
1640 ;;;; Comment
1642 (defun org-element-comment-parser (limit affiliated)
1643 "Parse a comment.
1645 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1646 the buffer position at the beginning of the first affiliated
1647 keyword and CDR is a plist of affiliated keywords along with
1648 their value.
1650 Return a list whose CAR is `comment' and CDR is a plist
1651 containing `:begin', `:end', `:value', `:post-blank',
1652 `:post-affiliated' keywords.
1654 Assume point is at comment beginning."
1655 (save-excursion
1656 (let* ((begin (car affiliated))
1657 (post-affiliated (point))
1658 (value (prog2 (looking-at "[ \t]*# ?")
1659 (buffer-substring-no-properties
1660 (match-end 0) (line-end-position))
1661 (forward-line)))
1662 (com-end
1663 ;; Get comments ending.
1664 (progn
1665 (while (and (< (point) limit) (looking-at "[ \t]*#\\( \\|$\\)"))
1666 ;; Accumulate lines without leading hash and first
1667 ;; whitespace.
1668 (setq value
1669 (concat value
1670 "\n"
1671 (buffer-substring-no-properties
1672 (match-end 0) (line-end-position))))
1673 (forward-line))
1674 (point)))
1675 (end (progn (goto-char com-end)
1676 (skip-chars-forward " \r\t\n" limit)
1677 (if (eobp) (point) (line-beginning-position)))))
1678 (list 'comment
1679 (nconc
1680 (list :begin begin
1681 :end end
1682 :value value
1683 :post-blank (count-lines com-end end)
1684 :post-affiliated post-affiliated)
1685 (cdr affiliated))))))
1687 (defun org-element-comment-interpreter (comment contents)
1688 "Interpret COMMENT element as Org syntax.
1689 CONTENTS is nil."
1690 (replace-regexp-in-string "^" "# " (org-element-property :value comment)))
1693 ;;;; Comment Block
1695 (defun org-element-comment-block-parser (limit affiliated)
1696 "Parse an export block.
1698 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1699 the buffer position at the beginning of the first affiliated
1700 keyword and CDR is a plist of affiliated keywords along with
1701 their value.
1703 Return a list whose CAR is `comment-block' and CDR is a plist
1704 containing `:begin', `:end', `:value', `:post-blank' and
1705 `:post-affiliated' keywords.
1707 Assume point is at comment block beginning."
1708 (let ((case-fold-search t))
1709 (if (not (save-excursion
1710 (re-search-forward "^[ \t]*#\\+END_COMMENT[ \t]*$" limit t)))
1711 ;; Incomplete block: parse it as a paragraph.
1712 (org-element-paragraph-parser limit affiliated)
1713 (let ((contents-end (match-beginning 0)))
1714 (save-excursion
1715 (let* ((begin (car affiliated))
1716 (post-affiliated (point))
1717 (contents-begin (progn (forward-line) (point)))
1718 (pos-before-blank (progn (goto-char contents-end)
1719 (forward-line)
1720 (point)))
1721 (end (progn (skip-chars-forward " \r\t\n" limit)
1722 (if (eobp) (point) (line-beginning-position))))
1723 (value (buffer-substring-no-properties
1724 contents-begin contents-end)))
1725 (list 'comment-block
1726 (nconc
1727 (list :begin begin
1728 :end end
1729 :value value
1730 :post-blank (count-lines pos-before-blank end)
1731 :post-affiliated post-affiliated)
1732 (cdr affiliated)))))))))
1734 (defun org-element-comment-block-interpreter (comment-block contents)
1735 "Interpret COMMENT-BLOCK element as Org syntax.
1736 CONTENTS is nil."
1737 (format "#+BEGIN_COMMENT\n%s#+END_COMMENT"
1738 (org-remove-indentation (org-element-property :value comment-block))))
1741 ;;;; Diary Sexp
1743 (defun org-element-diary-sexp-parser (limit affiliated)
1744 "Parse a diary sexp.
1746 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1747 the buffer position at the beginning of the first affiliated
1748 keyword and CDR is a plist of affiliated keywords along with
1749 their value.
1751 Return a list whose CAR is `diary-sexp' and CDR is a plist
1752 containing `:begin', `:end', `:value', `:post-blank' and
1753 `:post-affiliated' keywords."
1754 (save-excursion
1755 (let ((begin (car affiliated))
1756 (post-affiliated (point))
1757 (value (progn (looking-at "\\(%%(.*\\)[ \t]*$")
1758 (org-match-string-no-properties 1)))
1759 (pos-before-blank (progn (forward-line) (point)))
1760 (end (progn (skip-chars-forward " \r\t\n" limit)
1761 (if (eobp) (point) (line-beginning-position)))))
1762 (list 'diary-sexp
1763 (nconc
1764 (list :value value
1765 :begin begin
1766 :end end
1767 :post-blank (count-lines pos-before-blank end)
1768 :post-affiliated post-affiliated)
1769 (cdr affiliated))))))
1771 (defun org-element-diary-sexp-interpreter (diary-sexp contents)
1772 "Interpret DIARY-SEXP as Org syntax.
1773 CONTENTS is nil."
1774 (org-element-property :value diary-sexp))
1777 ;;;; Example Block
1779 (defun org-element-example-block-parser (limit affiliated)
1780 "Parse an example block.
1782 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1783 the buffer position at the beginning of the first affiliated
1784 keyword and CDR is a plist of affiliated keywords along with
1785 their value.
1787 Return a list whose CAR is `example-block' and CDR is a plist
1788 containing `:begin', `:end', `:number-lines', `:preserve-indent',
1789 `:retain-labels', `:use-labels', `:label-fmt', `:switches',
1790 `:value', `:post-blank' and `:post-affiliated' keywords."
1791 (let ((case-fold-search t))
1792 (if (not (save-excursion
1793 (re-search-forward "^[ \t]*#\\+END_EXAMPLE[ \t]*$" limit t)))
1794 ;; Incomplete block: parse it as a paragraph.
1795 (org-element-paragraph-parser limit affiliated)
1796 (let ((contents-end (match-beginning 0)))
1797 (save-excursion
1798 (let* ((switches
1799 (progn
1800 (looking-at "^[ \t]*#\\+BEGIN_EXAMPLE\\(?: +\\(.*\\)\\)?")
1801 (org-match-string-no-properties 1)))
1802 ;; Switches analysis
1803 (number-lines
1804 (cond ((not switches) nil)
1805 ((string-match "-n\\>" switches) 'new)
1806 ((string-match "+n\\>" switches) 'continued)))
1807 (preserve-indent
1808 (and switches (string-match "-i\\>" switches)))
1809 ;; Should labels be retained in (or stripped from) example
1810 ;; blocks?
1811 (retain-labels
1812 (or (not switches)
1813 (not (string-match "-r\\>" switches))
1814 (and number-lines (string-match "-k\\>" switches))))
1815 ;; What should code-references use - labels or
1816 ;; line-numbers?
1817 (use-labels
1818 (or (not switches)
1819 (and retain-labels
1820 (not (string-match "-k\\>" switches)))))
1821 (label-fmt
1822 (and switches
1823 (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
1824 (match-string 1 switches)))
1825 ;; Standard block parsing.
1826 (begin (car affiliated))
1827 (post-affiliated (point))
1828 (block-ind (progn (skip-chars-forward " \t") (current-column)))
1829 (contents-begin (progn (forward-line) (point)))
1830 (value (org-element-remove-indentation
1831 (org-unescape-code-in-string
1832 (buffer-substring-no-properties
1833 contents-begin contents-end))
1834 block-ind))
1835 (pos-before-blank (progn (goto-char contents-end)
1836 (forward-line)
1837 (point)))
1838 (end (progn (skip-chars-forward " \r\t\n" limit)
1839 (if (eobp) (point) (line-beginning-position)))))
1840 (list 'example-block
1841 (nconc
1842 (list :begin begin
1843 :end end
1844 :value value
1845 :switches switches
1846 :number-lines number-lines
1847 :preserve-indent preserve-indent
1848 :retain-labels retain-labels
1849 :use-labels use-labels
1850 :label-fmt label-fmt
1851 :post-blank (count-lines pos-before-blank end)
1852 :post-affiliated post-affiliated)
1853 (cdr affiliated)))))))))
1855 (defun org-element-example-block-interpreter (example-block contents)
1856 "Interpret EXAMPLE-BLOCK element as Org syntax.
1857 CONTENTS is nil."
1858 (let ((switches (org-element-property :switches example-block))
1859 (value (org-element-property :value example-block)))
1860 (concat "#+BEGIN_EXAMPLE" (and switches (concat " " switches)) "\n"
1861 (org-escape-code-in-string
1862 (if (or org-src-preserve-indentation
1863 (org-element-property :preserve-indent example-block))
1864 value
1865 (org-element-remove-indentation value)))
1866 "#+END_EXAMPLE")))
1869 ;;;; Export Block
1871 (defun org-element-export-block-parser (limit affiliated)
1872 "Parse an export block.
1874 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1875 the buffer position at the beginning of the first affiliated
1876 keyword and CDR is a plist of affiliated keywords along with
1877 their value.
1879 Return a list whose CAR is `export-block' and CDR is a plist
1880 containing `:begin', `:end', `:type', `:value', `:post-blank' and
1881 `:post-affiliated' keywords.
1883 Assume point is at export-block beginning."
1884 (let* ((case-fold-search t)
1885 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
1886 (upcase (org-match-string-no-properties 1)))))
1887 (if (not (save-excursion
1888 (re-search-forward
1889 (format "^[ \t]*#\\+END_%s[ \t]*$" type) limit t)))
1890 ;; Incomplete block: parse it as a paragraph.
1891 (org-element-paragraph-parser limit affiliated)
1892 (let ((contents-end (match-beginning 0)))
1893 (save-excursion
1894 (let* ((begin (car affiliated))
1895 (post-affiliated (point))
1896 (contents-begin (progn (forward-line) (point)))
1897 (pos-before-blank (progn (goto-char contents-end)
1898 (forward-line)
1899 (point)))
1900 (end (progn (skip-chars-forward " \r\t\n" limit)
1901 (if (eobp) (point) (line-beginning-position))))
1902 (value (buffer-substring-no-properties contents-begin
1903 contents-end)))
1904 (list 'export-block
1905 (nconc
1906 (list :begin begin
1907 :end end
1908 :type type
1909 :value value
1910 :post-blank (count-lines pos-before-blank end)
1911 :post-affiliated post-affiliated)
1912 (cdr affiliated)))))))))
1914 (defun org-element-export-block-interpreter (export-block contents)
1915 "Interpret EXPORT-BLOCK element as Org syntax.
1916 CONTENTS is nil."
1917 (let ((type (org-element-property :type export-block)))
1918 (concat (format "#+BEGIN_%s\n" type)
1919 (org-element-property :value export-block)
1920 (format "#+END_%s" type))))
1923 ;;;; Fixed-width
1925 (defun org-element-fixed-width-parser (limit affiliated)
1926 "Parse a fixed-width section.
1928 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1929 the buffer position at the beginning of the first affiliated
1930 keyword and CDR is a plist of affiliated keywords along with
1931 their value.
1933 Return a list whose CAR is `fixed-width' and CDR is a plist
1934 containing `:begin', `:end', `:value', `:post-blank' and
1935 `:post-affiliated' keywords.
1937 Assume point is at the beginning of the fixed-width area."
1938 (save-excursion
1939 (let* ((begin (car affiliated))
1940 (post-affiliated (point))
1941 value
1942 (end-area
1943 (progn
1944 (while (and (< (point) limit)
1945 (looking-at "[ \t]*:\\( \\|$\\)"))
1946 ;; Accumulate text without starting colons.
1947 (setq value
1948 (concat value
1949 (buffer-substring-no-properties
1950 (match-end 0) (point-at-eol))
1951 "\n"))
1952 (forward-line))
1953 (point)))
1954 (end (progn (skip-chars-forward " \r\t\n" limit)
1955 (if (eobp) (point) (line-beginning-position)))))
1956 (list 'fixed-width
1957 (nconc
1958 (list :begin begin
1959 :end end
1960 :value value
1961 :post-blank (count-lines end-area end)
1962 :post-affiliated post-affiliated)
1963 (cdr affiliated))))))
1965 (defun org-element-fixed-width-interpreter (fixed-width contents)
1966 "Interpret FIXED-WIDTH element as Org syntax.
1967 CONTENTS is nil."
1968 (let ((value (org-element-property :value fixed-width)))
1969 (and value
1970 (replace-regexp-in-string
1971 "^" ": "
1972 (if (string-match "\n\\'" value) (substring value 0 -1) value)))))
1975 ;;;; Horizontal Rule
1977 (defun org-element-horizontal-rule-parser (limit affiliated)
1978 "Parse an horizontal rule.
1980 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1981 the buffer position at the beginning of the first affiliated
1982 keyword and CDR is a plist of affiliated keywords along with
1983 their value.
1985 Return a list whose CAR is `horizontal-rule' and CDR is a plist
1986 containing `:begin', `:end', `:post-blank' and `:post-affiliated'
1987 keywords."
1988 (save-excursion
1989 (let ((begin (car affiliated))
1990 (post-affiliated (point))
1991 (post-hr (progn (forward-line) (point)))
1992 (end (progn (skip-chars-forward " \r\t\n" limit)
1993 (if (eobp) (point) (line-beginning-position)))))
1994 (list 'horizontal-rule
1995 (nconc
1996 (list :begin begin
1997 :end end
1998 :post-blank (count-lines post-hr end)
1999 :post-affiliated post-affiliated)
2000 (cdr affiliated))))))
2002 (defun org-element-horizontal-rule-interpreter (horizontal-rule contents)
2003 "Interpret HORIZONTAL-RULE element as Org syntax.
2004 CONTENTS is nil."
2005 "-----")
2008 ;;;; Keyword
2010 (defun org-element-keyword-parser (limit affiliated)
2011 "Parse a keyword at point.
2013 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2014 the buffer position at the beginning of the first affiliated
2015 keyword and CDR is a plist of affiliated keywords along with
2016 their value.
2018 Return a list whose CAR is `keyword' and CDR is a plist
2019 containing `:key', `:value', `:begin', `:end', `:post-blank' and
2020 `:post-affiliated' keywords."
2021 (save-excursion
2022 ;; An orphaned affiliated keyword is considered as a regular
2023 ;; keyword. In this case AFFILIATED is nil, so we take care of
2024 ;; this corner case.
2025 (let ((begin (or (car affiliated) (point)))
2026 (post-affiliated (point))
2027 (key (progn (looking-at "[ \t]*#\\+\\(\\S-+*\\):")
2028 (upcase (org-match-string-no-properties 1))))
2029 (value (org-trim (buffer-substring-no-properties
2030 (match-end 0) (point-at-eol))))
2031 (pos-before-blank (progn (forward-line) (point)))
2032 (end (progn (skip-chars-forward " \r\t\n" limit)
2033 (if (eobp) (point) (line-beginning-position)))))
2034 (list 'keyword
2035 (nconc
2036 (list :key key
2037 :value value
2038 :begin begin
2039 :end end
2040 :post-blank (count-lines pos-before-blank end)
2041 :post-affiliated post-affiliated)
2042 (cdr affiliated))))))
2044 (defun org-element-keyword-interpreter (keyword contents)
2045 "Interpret KEYWORD element as Org syntax.
2046 CONTENTS is nil."
2047 (format "#+%s: %s"
2048 (org-element-property :key keyword)
2049 (org-element-property :value keyword)))
2052 ;;;; Latex Environment
2054 (defun org-element-latex-environment-parser (limit affiliated)
2055 "Parse a LaTeX environment.
2057 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2058 the buffer position at the beginning of the first affiliated
2059 keyword and CDR is a plist of affiliated keywords along with
2060 their value.
2062 Return a list whose CAR is `latex-environment' and CDR is a plist
2063 containing `:begin', `:end', `:value', `:post-blank' and
2064 `:post-affiliated' keywords.
2066 Assume point is at the beginning of the latex environment."
2067 (save-excursion
2068 (let ((case-fold-search t)
2069 (code-begin (point)))
2070 (looking-at "[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}")
2071 (if (not (re-search-forward (format "^[ \t]*\\\\end{%s}[ \t]*$"
2072 (regexp-quote (match-string 1)))
2073 limit t))
2074 ;; Incomplete latex environment: parse it as a paragraph.
2075 (org-element-paragraph-parser limit affiliated)
2076 (let* ((code-end (progn (forward-line) (point)))
2077 (begin (car affiliated))
2078 (value (buffer-substring-no-properties code-begin code-end))
2079 (end (progn (skip-chars-forward " \r\t\n" limit)
2080 (if (eobp) (point) (line-beginning-position)))))
2081 (list 'latex-environment
2082 (nconc
2083 (list :begin begin
2084 :end end
2085 :value value
2086 :post-blank (count-lines code-end end)
2087 :post-affiliated code-begin)
2088 (cdr affiliated))))))))
2090 (defun org-element-latex-environment-interpreter (latex-environment contents)
2091 "Interpret LATEX-ENVIRONMENT element as Org syntax.
2092 CONTENTS is nil."
2093 (org-element-property :value latex-environment))
2096 ;;;; Node Property
2098 (defun org-element-node-property-parser (limit)
2099 "Parse a node-property at point.
2101 LIMIT bounds the search.
2103 Return a list whose CAR is `node-property' and CDR is a plist
2104 containing `:key', `:value', `:begin', `:end' and `:post-blank'
2105 keywords."
2106 (save-excursion
2107 (looking-at org-property-re)
2108 (let ((case-fold-search t)
2109 (begin (point))
2110 (key (org-match-string-no-properties 2))
2111 (value (org-match-string-no-properties 3))
2112 (pos-before-blank (progn (forward-line) (point)))
2113 (end (progn (skip-chars-forward " \r\t\n" limit)
2114 (if (eobp) (point) (point-at-bol)))))
2115 (list 'node-property
2116 (list :key key
2117 :value value
2118 :begin begin
2119 :end end
2120 :post-blank (count-lines pos-before-blank end))))))
2122 (defun org-element-node-property-interpreter (node-property contents)
2123 "Interpret NODE-PROPERTY element as Org syntax.
2124 CONTENTS is nil."
2125 (format org-property-format
2126 (format ":%s:" (org-element-property :key node-property))
2127 (org-element-property :value node-property)))
2130 ;;;; Paragraph
2132 (defun org-element-paragraph-parser (limit affiliated)
2133 "Parse a paragraph.
2135 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2136 the buffer position at the beginning of the first affiliated
2137 keyword and CDR is a plist of affiliated keywords along with
2138 their value.
2140 Return a list whose CAR is `paragraph' and CDR is a plist
2141 containing `:begin', `:end', `:contents-begin' and
2142 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
2144 Assume point is at the beginning of the paragraph."
2145 (save-excursion
2146 (let* ((begin (car affiliated))
2147 (contents-begin (point))
2148 (before-blank
2149 (let ((case-fold-search t))
2150 (end-of-line)
2151 (if (not (re-search-forward
2152 org-element-paragraph-separate limit 'm))
2153 limit
2154 ;; A matching `org-element-paragraph-separate' is not
2155 ;; necessarily the end of the paragraph. In
2156 ;; particular, lines starting with # or : as a first
2157 ;; non-space character are ambiguous. We have check
2158 ;; if they are valid Org syntax (i.e. not an
2159 ;; incomplete keyword).
2160 (beginning-of-line)
2161 (while (not
2163 ;; There's no ambiguity for other symbols or
2164 ;; empty lines: stop here.
2165 (looking-at "[ \t]*\\(?:[^:#]\\|$\\)")
2166 ;; Stop at valid fixed-width areas.
2167 (looking-at "[ \t]*:\\(?: \\|$\\)")
2168 ;; Stop at drawers.
2169 (and (looking-at org-drawer-regexp)
2170 (save-excursion
2171 (re-search-forward
2172 "^[ \t]*:END:[ \t]*$" limit t)))
2173 ;; Stop at valid comments.
2174 (looking-at "[ \t]*#\\(?: \\|$\\)")
2175 ;; Stop at valid dynamic blocks.
2176 (and (looking-at org-dblock-start-re)
2177 (save-excursion
2178 (re-search-forward
2179 "^[ \t]*#\\+END:?[ \t]*$" limit t)))
2180 ;; Stop at valid blocks.
2181 (and (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
2182 (save-excursion
2183 (re-search-forward
2184 (format "^[ \t]*#\\+END_%s[ \t]*$"
2185 (regexp-quote
2186 (org-match-string-no-properties 1)))
2187 limit t)))
2188 ;; Stop at valid latex environments.
2189 (and (looking-at
2190 "[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}")
2191 (save-excursion
2192 (re-search-forward
2193 (format "^[ \t]*\\\\end{%s}[ \t]*$"
2194 (regexp-quote
2195 (org-match-string-no-properties 1)))
2196 limit t)))
2197 ;; Stop at valid keywords.
2198 (looking-at "[ \t]*#\\+\\S-+:")
2199 ;; Skip everything else.
2200 (not
2201 (progn
2202 (end-of-line)
2203 (re-search-forward org-element-paragraph-separate
2204 limit 'm)))))
2205 (beginning-of-line)))
2206 (if (= (point) limit) limit
2207 (goto-char (line-beginning-position)))))
2208 (contents-end (progn (skip-chars-backward " \r\t\n" contents-begin)
2209 (forward-line)
2210 (point)))
2211 (end (progn (skip-chars-forward " \r\t\n" limit)
2212 (if (eobp) (point) (line-beginning-position)))))
2213 (list 'paragraph
2214 (nconc
2215 (list :begin begin
2216 :end end
2217 :contents-begin contents-begin
2218 :contents-end contents-end
2219 :post-blank (count-lines before-blank end)
2220 :post-affiliated contents-begin)
2221 (cdr affiliated))))))
2223 (defun org-element-paragraph-interpreter (paragraph contents)
2224 "Interpret PARAGRAPH element as Org syntax.
2225 CONTENTS is the contents of the element."
2226 contents)
2229 ;;;; Planning
2231 (defun org-element-planning-parser (limit)
2232 "Parse a planning.
2234 LIMIT bounds the search.
2236 Return a list whose CAR is `planning' and CDR is a plist
2237 containing `:closed', `:deadline', `:scheduled', `:begin', `:end'
2238 and `:post-blank' keywords."
2239 (save-excursion
2240 (let* ((case-fold-search nil)
2241 (begin (point))
2242 (post-blank (let ((before-blank (progn (forward-line) (point))))
2243 (skip-chars-forward " \r\t\n" limit)
2244 (skip-chars-backward " \t")
2245 (unless (bolp) (end-of-line))
2246 (count-lines before-blank (point))))
2247 (end (point))
2248 closed deadline scheduled)
2249 (goto-char begin)
2250 (while (re-search-forward org-keyword-time-not-clock-regexp end t)
2251 (goto-char (match-end 1))
2252 (skip-chars-forward " \t" end)
2253 (let ((keyword (match-string 1))
2254 (time (org-element-timestamp-parser)))
2255 (cond ((equal keyword org-closed-string) (setq closed time))
2256 ((equal keyword org-deadline-string) (setq deadline time))
2257 (t (setq scheduled time)))))
2258 (list 'planning
2259 (list :closed closed
2260 :deadline deadline
2261 :scheduled scheduled
2262 :begin begin
2263 :end end
2264 :post-blank post-blank)))))
2266 (defun org-element-planning-interpreter (planning contents)
2267 "Interpret PLANNING element as Org syntax.
2268 CONTENTS is nil."
2269 (mapconcat
2270 'identity
2271 (delq nil
2272 (list (let ((deadline (org-element-property :deadline planning)))
2273 (when deadline
2274 (concat org-deadline-string " "
2275 (org-element-timestamp-interpreter deadline nil))))
2276 (let ((scheduled (org-element-property :scheduled planning)))
2277 (when scheduled
2278 (concat org-scheduled-string " "
2279 (org-element-timestamp-interpreter scheduled nil))))
2280 (let ((closed (org-element-property :closed planning)))
2281 (when closed
2282 (concat org-closed-string " "
2283 (org-element-timestamp-interpreter closed nil))))))
2284 " "))
2287 ;;;; Src Block
2289 (defun org-element-src-block-parser (limit affiliated)
2290 "Parse a src block.
2292 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2293 the buffer position at the beginning of the first affiliated
2294 keyword and CDR is a plist of affiliated keywords along with
2295 their value.
2297 Return a list whose CAR is `src-block' and CDR is a plist
2298 containing `:language', `:switches', `:parameters', `:begin',
2299 `:end', `:number-lines', `:retain-labels', `:use-labels',
2300 `:label-fmt', `:preserve-indent', `:value', `:post-blank' and
2301 `:post-affiliated' keywords.
2303 Assume point is at the beginning of the block."
2304 (let ((case-fold-search t))
2305 (if (not (save-excursion (re-search-forward "^[ \t]*#\\+END_SRC[ \t]*$"
2306 limit t)))
2307 ;; Incomplete block: parse it as a paragraph.
2308 (org-element-paragraph-parser limit affiliated)
2309 (let ((contents-end (match-beginning 0)))
2310 (save-excursion
2311 (let* ((begin (car affiliated))
2312 (post-affiliated (point))
2313 ;; Get language as a string.
2314 (language
2315 (progn
2316 (looking-at
2317 (concat "^[ \t]*#\\+BEGIN_SRC"
2318 "\\(?: +\\(\\S-+\\)\\)?"
2319 "\\(\\(?: +\\(?:-l \".*?\"\\|[-+][A-Za-z]\\)\\)+\\)?"
2320 "\\(.*\\)[ \t]*$"))
2321 (org-match-string-no-properties 1)))
2322 ;; Get switches.
2323 (switches (org-match-string-no-properties 2))
2324 ;; Get parameters.
2325 (parameters (org-match-string-no-properties 3))
2326 ;; Switches analysis
2327 (number-lines
2328 (cond ((not switches) nil)
2329 ((string-match "-n\\>" switches) 'new)
2330 ((string-match "+n\\>" switches) 'continued)))
2331 (preserve-indent (and switches
2332 (string-match "-i\\>" switches)))
2333 (label-fmt
2334 (and switches
2335 (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
2336 (match-string 1 switches)))
2337 ;; Should labels be retained in (or stripped from)
2338 ;; src blocks?
2339 (retain-labels
2340 (or (not switches)
2341 (not (string-match "-r\\>" switches))
2342 (and number-lines (string-match "-k\\>" switches))))
2343 ;; What should code-references use - labels or
2344 ;; line-numbers?
2345 (use-labels
2346 (or (not switches)
2347 (and retain-labels
2348 (not (string-match "-k\\>" switches)))))
2349 ;; Indentation.
2350 (block-ind (progn (skip-chars-forward " \t") (current-column)))
2351 ;; Retrieve code.
2352 (value (org-element-remove-indentation
2353 (org-unescape-code-in-string
2354 (buffer-substring-no-properties
2355 (progn (forward-line) (point)) contents-end))
2356 block-ind))
2357 (pos-before-blank (progn (goto-char contents-end)
2358 (forward-line)
2359 (point)))
2360 ;; Get position after ending blank lines.
2361 (end (progn (skip-chars-forward " \r\t\n" limit)
2362 (if (eobp) (point) (line-beginning-position)))))
2363 (list 'src-block
2364 (nconc
2365 (list :language language
2366 :switches (and (org-string-nw-p switches)
2367 (org-trim switches))
2368 :parameters (and (org-string-nw-p parameters)
2369 (org-trim parameters))
2370 :begin begin
2371 :end end
2372 :number-lines number-lines
2373 :preserve-indent preserve-indent
2374 :retain-labels retain-labels
2375 :use-labels use-labels
2376 :label-fmt label-fmt
2377 :value value
2378 :post-blank (count-lines pos-before-blank end)
2379 :post-affiliated post-affiliated)
2380 (cdr affiliated)))))))))
2382 (defun org-element-src-block-interpreter (src-block contents)
2383 "Interpret SRC-BLOCK element as Org syntax.
2384 CONTENTS is nil."
2385 (let ((lang (org-element-property :language src-block))
2386 (switches (org-element-property :switches src-block))
2387 (params (org-element-property :parameters src-block))
2388 (value
2389 (let ((val (org-element-property :value src-block)))
2390 (cond
2391 ((or org-src-preserve-indentation
2392 (org-element-property :preserve-indent src-block))
2393 val)
2394 ((zerop org-edit-src-content-indentation) val)
2396 (let ((ind (make-string org-edit-src-content-indentation ?\s)))
2397 (replace-regexp-in-string
2398 "\\(^\\)[ \t]*\\S-" ind val nil nil 1)))))))
2399 (concat (format "#+BEGIN_SRC%s\n"
2400 (concat (and lang (concat " " lang))
2401 (and switches (concat " " switches))
2402 (and params (concat " " params))))
2403 (org-escape-code-in-string value)
2404 "#+END_SRC")))
2407 ;;;; Table
2409 (defun org-element-table-parser (limit affiliated)
2410 "Parse a table at point.
2412 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2413 the buffer position at the beginning of the first affiliated
2414 keyword and CDR is a plist of affiliated keywords along with
2415 their value.
2417 Return a list whose CAR is `table' and CDR is a plist containing
2418 `:begin', `:end', `:tblfm', `:type', `:contents-begin',
2419 `:contents-end', `:value', `:post-blank' and `:post-affiliated'
2420 keywords.
2422 Assume point is at the beginning of the table."
2423 (save-excursion
2424 (let* ((case-fold-search t)
2425 (table-begin (point))
2426 (type (if (org-at-table.el-p) 'table.el 'org))
2427 (begin (car affiliated))
2428 (table-end
2429 (if (re-search-forward org-table-any-border-regexp limit 'm)
2430 (goto-char (match-beginning 0))
2431 (point)))
2432 (tblfm (let (acc)
2433 (while (looking-at "[ \t]*#\\+TBLFM: +\\(.*\\)[ \t]*$")
2434 (push (org-match-string-no-properties 1) acc)
2435 (forward-line))
2436 acc))
2437 (pos-before-blank (point))
2438 (end (progn (skip-chars-forward " \r\t\n" limit)
2439 (if (eobp) (point) (line-beginning-position)))))
2440 (list 'table
2441 (nconc
2442 (list :begin begin
2443 :end end
2444 :type type
2445 :tblfm tblfm
2446 ;; Only `org' tables have contents. `table.el' tables
2447 ;; use a `:value' property to store raw table as
2448 ;; a string.
2449 :contents-begin (and (eq type 'org) table-begin)
2450 :contents-end (and (eq type 'org) table-end)
2451 :value (and (eq type 'table.el)
2452 (buffer-substring-no-properties
2453 table-begin table-end))
2454 :post-blank (count-lines pos-before-blank end)
2455 :post-affiliated table-begin)
2456 (cdr affiliated))))))
2458 (defun org-element-table-interpreter (table contents)
2459 "Interpret TABLE element as Org syntax.
2460 CONTENTS is nil."
2461 (if (eq (org-element-property :type table) 'table.el)
2462 (org-remove-indentation (org-element-property :value table))
2463 (concat (with-temp-buffer (insert contents)
2464 (org-table-align)
2465 (buffer-string))
2466 (mapconcat (lambda (fm) (concat "#+TBLFM: " fm))
2467 (reverse (org-element-property :tblfm table))
2468 "\n"))))
2471 ;;;; Table Row
2473 (defun org-element-table-row-parser (limit)
2474 "Parse table row at point.
2476 LIMIT bounds the search.
2478 Return a list whose CAR is `table-row' and CDR is a plist
2479 containing `:begin', `:end', `:contents-begin', `:contents-end',
2480 `:type' and `:post-blank' keywords."
2481 (save-excursion
2482 (let* ((type (if (looking-at "^[ \t]*|-") 'rule 'standard))
2483 (begin (point))
2484 ;; A table rule has no contents. In that case, ensure
2485 ;; CONTENTS-BEGIN matches CONTENTS-END.
2486 (contents-begin (and (eq type 'standard)
2487 (search-forward "|")
2488 (point)))
2489 (contents-end (and (eq type 'standard)
2490 (progn
2491 (end-of-line)
2492 (skip-chars-backward " \t")
2493 (point))))
2494 (end (progn (forward-line) (point))))
2495 (list 'table-row
2496 (list :type type
2497 :begin begin
2498 :end end
2499 :contents-begin contents-begin
2500 :contents-end contents-end
2501 :post-blank 0)))))
2503 (defun org-element-table-row-interpreter (table-row contents)
2504 "Interpret TABLE-ROW element as Org syntax.
2505 CONTENTS is the contents of the table row."
2506 (if (eq (org-element-property :type table-row) 'rule) "|-"
2507 (concat "| " contents)))
2510 ;;;; Verse Block
2512 (defun org-element-verse-block-parser (limit affiliated)
2513 "Parse a verse block.
2515 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2516 the buffer position at the beginning of the first affiliated
2517 keyword and CDR is a plist of affiliated keywords along with
2518 their value.
2520 Return a list whose CAR is `verse-block' and CDR is a plist
2521 containing `:begin', `:end', `:contents-begin', `:contents-end',
2522 `:post-blank' and `:post-affiliated' keywords.
2524 Assume point is at beginning of the block."
2525 (let ((case-fold-search t))
2526 (if (not (save-excursion
2527 (re-search-forward "^[ \t]*#\\+END_VERSE[ \t]*$" limit t)))
2528 ;; Incomplete block: parse it as a paragraph.
2529 (org-element-paragraph-parser limit affiliated)
2530 (let ((contents-end (match-beginning 0)))
2531 (save-excursion
2532 (let* ((begin (car affiliated))
2533 (post-affiliated (point))
2534 (contents-begin (progn (forward-line) (point)))
2535 (pos-before-blank (progn (goto-char contents-end)
2536 (forward-line)
2537 (point)))
2538 (end (progn (skip-chars-forward " \r\t\n" limit)
2539 (if (eobp) (point) (line-beginning-position)))))
2540 (list 'verse-block
2541 (nconc
2542 (list :begin begin
2543 :end end
2544 :contents-begin contents-begin
2545 :contents-end contents-end
2546 :post-blank (count-lines pos-before-blank end)
2547 :post-affiliated post-affiliated)
2548 (cdr affiliated)))))))))
2550 (defun org-element-verse-block-interpreter (verse-block contents)
2551 "Interpret VERSE-BLOCK element as Org syntax.
2552 CONTENTS is verse block contents."
2553 (format "#+BEGIN_VERSE\n%s#+END_VERSE" contents))
2557 ;;; Objects
2559 ;; Unlike to elements, interstices can be found between objects.
2560 ;; That's why, along with the parser, successor functions are provided
2561 ;; for each object. Some objects share the same successor (i.e. `code'
2562 ;; and `verbatim' objects).
2564 ;; A successor must accept a single argument bounding the search. It
2565 ;; will return either a cons cell whose CAR is the object's type, as
2566 ;; a symbol, and CDR the position of its next occurrence, or nil.
2568 ;; Successors follow the naming convention:
2569 ;; org-element-NAME-successor, where NAME is the name of the
2570 ;; successor, as defined in `org-element-all-successors'.
2572 ;; Some object types (i.e. `italic') are recursive. Restrictions on
2573 ;; object types they can contain will be specified in
2574 ;; `org-element-object-restrictions'.
2576 ;; Adding a new type of object is simple. Implement a successor,
2577 ;; a parser, and an interpreter for it, all following the naming
2578 ;; convention. Register type in `org-element-all-objects' and
2579 ;; successor in `org-element-all-successors'. Maybe tweak
2580 ;; restrictions about it, and that's it.
2583 ;;;; Bold
2585 (defun org-element-bold-parser ()
2586 "Parse bold object at point.
2588 Return a list whose CAR is `bold' and CDR is a plist with
2589 `:begin', `:end', `:contents-begin' and `:contents-end' and
2590 `:post-blank' keywords.
2592 Assume point is at the first star marker."
2593 (save-excursion
2594 (unless (bolp) (backward-char 1))
2595 (looking-at org-emph-re)
2596 (let ((begin (match-beginning 2))
2597 (contents-begin (match-beginning 4))
2598 (contents-end (match-end 4))
2599 (post-blank (progn (goto-char (match-end 2))
2600 (skip-chars-forward " \t")))
2601 (end (point)))
2602 (list 'bold
2603 (list :begin begin
2604 :end end
2605 :contents-begin contents-begin
2606 :contents-end contents-end
2607 :post-blank post-blank)))))
2609 (defun org-element-bold-interpreter (bold contents)
2610 "Interpret BOLD object as Org syntax.
2611 CONTENTS is the contents of the object."
2612 (format "*%s*" contents))
2614 (defun org-element-text-markup-successor ()
2615 "Search for the next text-markup object.
2617 Return value is a cons cell whose CAR is a symbol among `bold',
2618 `italic', `underline', `strike-through', `code' and `verbatim'
2619 and CDR is beginning position."
2620 (save-excursion
2621 (unless (bolp) (backward-char))
2622 (when (re-search-forward org-emph-re nil t)
2623 (let ((marker (match-string 3)))
2624 (cons (cond
2625 ((equal marker "*") 'bold)
2626 ((equal marker "/") 'italic)
2627 ((equal marker "_") 'underline)
2628 ((equal marker "+") 'strike-through)
2629 ((equal marker "~") 'code)
2630 ((equal marker "=") 'verbatim)
2631 (t (error "Unknown marker at %d" (match-beginning 3))))
2632 (match-beginning 2))))))
2635 ;;;; Code
2637 (defun org-element-code-parser ()
2638 "Parse code object at point.
2640 Return a list whose CAR is `code' and CDR is a plist with
2641 `:value', `:begin', `:end' and `:post-blank' keywords.
2643 Assume point is at the first tilde marker."
2644 (save-excursion
2645 (unless (bolp) (backward-char 1))
2646 (looking-at org-emph-re)
2647 (let ((begin (match-beginning 2))
2648 (value (org-match-string-no-properties 4))
2649 (post-blank (progn (goto-char (match-end 2))
2650 (skip-chars-forward " \t")))
2651 (end (point)))
2652 (list 'code
2653 (list :value value
2654 :begin begin
2655 :end end
2656 :post-blank post-blank)))))
2658 (defun org-element-code-interpreter (code contents)
2659 "Interpret CODE object as Org syntax.
2660 CONTENTS is nil."
2661 (format "~%s~" (org-element-property :value code)))
2664 ;;;; Entity
2666 (defun org-element-entity-parser ()
2667 "Parse entity at point.
2669 Return a list whose CAR is `entity' and CDR a plist with
2670 `:begin', `:end', `:latex', `:latex-math-p', `:html', `:latin1',
2671 `:utf-8', `:ascii', `:use-brackets-p' and `:post-blank' as
2672 keywords.
2674 Assume point is at the beginning of the entity."
2675 (save-excursion
2676 (looking-at "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)")
2677 (let* ((value (org-entity-get (match-string 1)))
2678 (begin (match-beginning 0))
2679 (bracketsp (string= (match-string 2) "{}"))
2680 (post-blank (progn (goto-char (match-end 1))
2681 (when bracketsp (forward-char 2))
2682 (skip-chars-forward " \t")))
2683 (end (point)))
2684 (list 'entity
2685 (list :name (car value)
2686 :latex (nth 1 value)
2687 :latex-math-p (nth 2 value)
2688 :html (nth 3 value)
2689 :ascii (nth 4 value)
2690 :latin1 (nth 5 value)
2691 :utf-8 (nth 6 value)
2692 :begin begin
2693 :end end
2694 :use-brackets-p bracketsp
2695 :post-blank post-blank)))))
2697 (defun org-element-entity-interpreter (entity contents)
2698 "Interpret ENTITY object as Org syntax.
2699 CONTENTS is nil."
2700 (concat "\\"
2701 (org-element-property :name entity)
2702 (when (org-element-property :use-brackets-p entity) "{}")))
2704 (defun org-element-latex-or-entity-successor ()
2705 "Search for the next latex-fragment or entity object.
2707 Return value is a cons cell whose CAR is `entity' or
2708 `latex-fragment' and CDR is beginning position."
2709 (save-excursion
2710 (unless (bolp) (backward-char))
2711 (let ((matchers (cdr org-latex-regexps))
2712 ;; ENTITY-RE matches both LaTeX commands and Org entities.
2713 (entity-re
2714 "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))
2715 (when (re-search-forward
2716 (concat (mapconcat #'cadr matchers "\\|") "\\|" entity-re) nil t)
2717 (goto-char (match-beginning 0))
2718 (if (looking-at entity-re)
2719 ;; Determine if it's a real entity or a LaTeX command.
2720 (cons (if (org-entity-get (match-string 1)) 'entity 'latex-fragment)
2721 (match-beginning 0))
2722 ;; No entity nor command: point is at a LaTeX fragment.
2723 ;; Determine its type to get the correct beginning position.
2724 (cons 'latex-fragment
2725 (catch 'return
2726 (dolist (e matchers)
2727 (when (looking-at (nth 1 e))
2728 (throw 'return (match-beginning (nth 2 e)))))
2729 (point))))))))
2732 ;;;; Export Snippet
2734 (defun org-element-export-snippet-parser ()
2735 "Parse export snippet at point.
2737 Return a list whose CAR is `export-snippet' and CDR a plist with
2738 `:begin', `:end', `:back-end', `:value' and `:post-blank' as
2739 keywords.
2741 Assume point is at the beginning of the snippet."
2742 (save-excursion
2743 (re-search-forward "@@\\([-A-Za-z0-9]+\\):" nil t)
2744 (let* ((begin (match-beginning 0))
2745 (back-end (org-match-string-no-properties 1))
2746 (value (buffer-substring-no-properties
2747 (point)
2748 (progn (re-search-forward "@@" nil t) (match-beginning 0))))
2749 (post-blank (skip-chars-forward " \t"))
2750 (end (point)))
2751 (list 'export-snippet
2752 (list :back-end back-end
2753 :value value
2754 :begin begin
2755 :end end
2756 :post-blank post-blank)))))
2758 (defun org-element-export-snippet-interpreter (export-snippet contents)
2759 "Interpret EXPORT-SNIPPET object as Org syntax.
2760 CONTENTS is nil."
2761 (format "@@%s:%s@@"
2762 (org-element-property :back-end export-snippet)
2763 (org-element-property :value export-snippet)))
2765 (defun org-element-export-snippet-successor ()
2766 "Search for the next export-snippet object.
2768 Return value is a cons cell whose CAR is `export-snippet' and CDR
2769 its beginning position."
2770 (save-excursion
2771 (let (beg)
2772 (when (and (re-search-forward "@@[-A-Za-z0-9]+:" nil t)
2773 (setq beg (match-beginning 0))
2774 (search-forward "@@" nil t))
2775 (cons 'export-snippet beg)))))
2778 ;;;; Footnote Reference
2780 (defun org-element-footnote-reference-parser ()
2781 "Parse footnote reference at point.
2783 Return a list whose CAR is `footnote-reference' and CDR a plist
2784 with `:label', `:type', `:inline-definition', `:begin', `:end'
2785 and `:post-blank' as keywords."
2786 (save-excursion
2787 (looking-at org-footnote-re)
2788 (let* ((begin (point))
2789 (label (or (org-match-string-no-properties 2)
2790 (org-match-string-no-properties 3)
2791 (and (match-string 1)
2792 (concat "fn:" (org-match-string-no-properties 1)))))
2793 (type (if (or (not label) (match-string 1)) 'inline 'standard))
2794 (inner-begin (match-end 0))
2795 (inner-end
2796 (let ((count 1))
2797 (forward-char)
2798 (while (and (> count 0) (re-search-forward "[][]" nil t))
2799 (if (equal (match-string 0) "[") (incf count) (decf count)))
2800 (1- (point))))
2801 (post-blank (progn (goto-char (1+ inner-end))
2802 (skip-chars-forward " \t")))
2803 (end (point))
2804 (footnote-reference
2805 (list 'footnote-reference
2806 (list :label label
2807 :type type
2808 :begin begin
2809 :end end
2810 :post-blank post-blank))))
2811 (org-element-put-property
2812 footnote-reference :inline-definition
2813 (and (eq type 'inline)
2814 (org-element-parse-secondary-string
2815 (buffer-substring inner-begin inner-end)
2816 (org-element-restriction 'footnote-reference)
2817 footnote-reference))))))
2819 (defun org-element-footnote-reference-interpreter (footnote-reference contents)
2820 "Interpret FOOTNOTE-REFERENCE object as Org syntax.
2821 CONTENTS is nil."
2822 (let ((label (or (org-element-property :label footnote-reference) "fn:"))
2823 (def
2824 (let ((inline-def
2825 (org-element-property :inline-definition footnote-reference)))
2826 (if (not inline-def) ""
2827 (concat ":" (org-element-interpret-data inline-def))))))
2828 (format "[%s]" (concat label def))))
2830 (defun org-element-footnote-reference-successor ()
2831 "Search for the next footnote-reference object.
2833 Return value is a cons cell whose CAR is `footnote-reference' and
2834 CDR is beginning position."
2835 (save-excursion
2836 (catch 'exit
2837 (while (re-search-forward org-footnote-re nil t)
2838 (save-excursion
2839 (let ((beg (match-beginning 0))
2840 (count 1))
2841 (backward-char)
2842 (while (re-search-forward "[][]" nil t)
2843 (if (equal (match-string 0) "[") (incf count) (decf count))
2844 (when (zerop count)
2845 (throw 'exit (cons 'footnote-reference beg))))))))))
2848 ;;;; Inline Babel Call
2850 (defun org-element-inline-babel-call-parser ()
2851 "Parse inline babel call at point.
2853 Return a list whose CAR is `inline-babel-call' and CDR a plist
2854 with `:begin', `:end', `:value' and `:post-blank' as keywords.
2856 Assume point is at the beginning of the babel call."
2857 (save-excursion
2858 (unless (bolp) (backward-char))
2859 (let ((case-fold-search t))
2860 (looking-at org-babel-inline-lob-one-liner-regexp))
2861 (let ((begin (match-end 1))
2862 (value (buffer-substring-no-properties (match-end 1) (match-end 0)))
2863 (post-blank (progn (goto-char (match-end 0))
2864 (skip-chars-forward " \t")))
2865 (end (point)))
2866 (list 'inline-babel-call
2867 (list :begin begin
2868 :end end
2869 :value value
2870 :post-blank post-blank)))))
2872 (defun org-element-inline-babel-call-interpreter (inline-babel-call contents)
2873 "Interpret INLINE-BABEL-CALL object as Org syntax.
2874 CONTENTS is nil."
2875 (org-element-property :value inline-babel-call))
2877 (defun org-element-inline-babel-call-successor ()
2878 "Search for the next inline-babel-call object.
2880 Return value is a cons cell whose CAR is `inline-babel-call' and
2881 CDR is beginning position."
2882 (save-excursion
2883 (when (re-search-forward org-babel-inline-lob-one-liner-regexp nil t)
2884 (cons 'inline-babel-call (match-end 1)))))
2887 ;;;; Inline Src Block
2889 (defun org-element-inline-src-block-parser ()
2890 "Parse inline source block at point.
2892 Return a list whose CAR is `inline-src-block' and CDR a plist
2893 with `:begin', `:end', `:language', `:value', `:parameters' and
2894 `:post-blank' as keywords.
2896 Assume point is at the beginning of the inline src block."
2897 (save-excursion
2898 (unless (bolp) (backward-char))
2899 (looking-at org-babel-inline-src-block-regexp)
2900 (let ((begin (match-beginning 1))
2901 (language (org-match-string-no-properties 2))
2902 (parameters (org-match-string-no-properties 4))
2903 (value (org-match-string-no-properties 5))
2904 (post-blank (progn (goto-char (match-end 0))
2905 (skip-chars-forward " \t")))
2906 (end (point)))
2907 (list 'inline-src-block
2908 (list :language language
2909 :value value
2910 :parameters parameters
2911 :begin begin
2912 :end end
2913 :post-blank post-blank)))))
2915 (defun org-element-inline-src-block-interpreter (inline-src-block contents)
2916 "Interpret INLINE-SRC-BLOCK object as Org syntax.
2917 CONTENTS is nil."
2918 (let ((language (org-element-property :language inline-src-block))
2919 (arguments (org-element-property :parameters inline-src-block))
2920 (body (org-element-property :value inline-src-block)))
2921 (format "src_%s%s{%s}"
2922 language
2923 (if arguments (format "[%s]" arguments) "")
2924 body)))
2926 (defun org-element-inline-src-block-successor ()
2927 "Search for the next inline-babel-call element.
2929 Return value is a cons cell whose CAR is `inline-babel-call' and
2930 CDR is beginning position."
2931 (save-excursion
2932 (unless (bolp) (backward-char))
2933 (when (re-search-forward org-babel-inline-src-block-regexp nil t)
2934 (cons 'inline-src-block (match-beginning 1)))))
2936 ;;;; Italic
2938 (defun org-element-italic-parser ()
2939 "Parse italic object at point.
2941 Return a list whose CAR is `italic' and CDR is a plist with
2942 `:begin', `:end', `:contents-begin' and `:contents-end' and
2943 `:post-blank' keywords.
2945 Assume point is at the first slash marker."
2946 (save-excursion
2947 (unless (bolp) (backward-char 1))
2948 (looking-at org-emph-re)
2949 (let ((begin (match-beginning 2))
2950 (contents-begin (match-beginning 4))
2951 (contents-end (match-end 4))
2952 (post-blank (progn (goto-char (match-end 2))
2953 (skip-chars-forward " \t")))
2954 (end (point)))
2955 (list 'italic
2956 (list :begin begin
2957 :end end
2958 :contents-begin contents-begin
2959 :contents-end contents-end
2960 :post-blank post-blank)))))
2962 (defun org-element-italic-interpreter (italic contents)
2963 "Interpret ITALIC object as Org syntax.
2964 CONTENTS is the contents of the object."
2965 (format "/%s/" contents))
2968 ;;;; Latex Fragment
2970 (defun org-element-latex-fragment-parser ()
2971 "Parse LaTeX fragment at point.
2973 Return a list whose CAR is `latex-fragment' and CDR a plist with
2974 `:value', `:begin', `:end', and `:post-blank' as keywords.
2976 Assume point is at the beginning of the LaTeX fragment."
2977 (save-excursion
2978 (let* ((begin (point))
2979 (substring-match
2980 (catch 'exit
2981 (dolist (e (cdr org-latex-regexps))
2982 (let ((latex-regexp (nth 1 e)))
2983 (when (or (looking-at latex-regexp)
2984 (and (not (bobp))
2985 (save-excursion
2986 (backward-char)
2987 (looking-at latex-regexp))))
2988 (throw 'exit (nth 2 e)))))
2989 ;; None found: it's a macro.
2990 (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")
2992 (value (org-match-string-no-properties substring-match))
2993 (post-blank (progn (goto-char (match-end substring-match))
2994 (skip-chars-forward " \t")))
2995 (end (point)))
2996 (list 'latex-fragment
2997 (list :value value
2998 :begin begin
2999 :end end
3000 :post-blank post-blank)))))
3002 (defun org-element-latex-fragment-interpreter (latex-fragment contents)
3003 "Interpret LATEX-FRAGMENT object as Org syntax.
3004 CONTENTS is nil."
3005 (org-element-property :value latex-fragment))
3007 ;;;; Line Break
3009 (defun org-element-line-break-parser ()
3010 "Parse line break at point.
3012 Return a list whose CAR is `line-break', and CDR a plist with
3013 `:begin', `:end' and `:post-blank' keywords.
3015 Assume point is at the beginning of the line break."
3016 (list 'line-break
3017 (list :begin (point)
3018 :end (progn (forward-line) (point))
3019 :post-blank 0)))
3021 (defun org-element-line-break-interpreter (line-break contents)
3022 "Interpret LINE-BREAK object as Org syntax.
3023 CONTENTS is nil."
3024 "\\\\\n")
3026 (defun org-element-line-break-successor ()
3027 "Search for the next line-break object.
3029 Return value is a cons cell whose CAR is `line-break' and CDR is
3030 beginning position."
3031 (save-excursion
3032 (let ((beg (and (re-search-forward "[^\\\\]\\(\\\\\\\\\\)[ \t]*$" nil t)
3033 (goto-char (match-beginning 1)))))
3034 ;; A line break can only happen on a non-empty line.
3035 (when (and beg (re-search-backward "\\S-" (point-at-bol) t))
3036 (cons 'line-break beg)))))
3039 ;;;; Link
3041 (defun org-element-link-parser ()
3042 "Parse link at point.
3044 Return a list whose CAR is `link' and CDR a plist with `:type',
3045 `:path', `:raw-link', `:application', `:search-option', `:begin',
3046 `:end', `:contents-begin', `:contents-end' and `:post-blank' as
3047 keywords.
3049 Assume point is at the beginning of the link."
3050 (save-excursion
3051 (let ((begin (point))
3052 end contents-begin contents-end link-end post-blank path type
3053 raw-link link search-option application)
3054 (cond
3055 ;; Type 1: Text targeted from a radio target.
3056 ((and org-target-link-regexp (looking-at org-target-link-regexp))
3057 (setq type "radio"
3058 link-end (match-end 0)
3059 path (org-match-string-no-properties 0)))
3060 ;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]]
3061 ((looking-at org-bracket-link-regexp)
3062 (setq contents-begin (match-beginning 3)
3063 contents-end (match-end 3)
3064 link-end (match-end 0)
3065 ;; RAW-LINK is the original link. Expand any
3066 ;; abbreviation in it.
3067 raw-link (org-translate-link
3068 (org-link-expand-abbrev
3069 (org-match-string-no-properties 1))))
3070 ;; Determine TYPE of link and set PATH accordingly.
3071 (cond
3072 ;; File type.
3073 ((or (file-name-absolute-p raw-link)
3074 (string-match "^\\.\\.?/" raw-link))
3075 (setq type "file" path raw-link))
3076 ;; Explicit type (http, irc, bbdb...). See `org-link-types'.
3077 ((string-match org-link-re-with-space3 raw-link)
3078 (setq type (match-string 1 raw-link) path (match-string 2 raw-link)))
3079 ;; Id type: PATH is the id.
3080 ((string-match "^id:\\([-a-f0-9]+\\)" raw-link)
3081 (setq type "id" path (match-string 1 raw-link)))
3082 ;; Code-ref type: PATH is the name of the reference.
3083 ((string-match "^(\\(.*\\))$" raw-link)
3084 (setq type "coderef" path (match-string 1 raw-link)))
3085 ;; Custom-id type: PATH is the name of the custom id.
3086 ((= (aref raw-link 0) ?#)
3087 (setq type "custom-id" path (substring raw-link 1)))
3088 ;; Fuzzy type: Internal link either matches a target, an
3089 ;; headline name or nothing. PATH is the target or
3090 ;; headline's name.
3091 (t (setq type "fuzzy" path raw-link))))
3092 ;; Type 3: Plain link, i.e. http://orgmode.org
3093 ((looking-at org-plain-link-re)
3094 (setq raw-link (org-match-string-no-properties 0)
3095 type (org-match-string-no-properties 1)
3096 link-end (match-end 0)
3097 path (org-match-string-no-properties 2)))
3098 ;; Type 4: Angular link, i.e. <http://orgmode.org>
3099 ((looking-at org-angle-link-re)
3100 (setq raw-link (buffer-substring-no-properties
3101 (match-beginning 1) (match-end 2))
3102 type (org-match-string-no-properties 1)
3103 link-end (match-end 0)
3104 path (org-match-string-no-properties 2))))
3105 ;; In any case, deduce end point after trailing white space from
3106 ;; LINK-END variable.
3107 (setq post-blank (progn (goto-char link-end) (skip-chars-forward " \t"))
3108 end (point))
3109 ;; Extract search option and opening application out of
3110 ;; "file"-type links.
3111 (when (member type org-element-link-type-is-file)
3112 ;; Application.
3113 (cond ((string-match "^file\\+\\(.*\\)$" type)
3114 (setq application (match-string 1 type)))
3115 ((not (string-match "^file" type))
3116 (setq application type)))
3117 ;; Extract search option from PATH.
3118 (when (string-match "::\\(.*\\)$" path)
3119 (setq search-option (match-string 1 path)
3120 path (replace-match "" nil nil path)))
3121 ;; Make sure TYPE always reports "file".
3122 (setq type "file"))
3123 (list 'link
3124 (list :type type
3125 :path path
3126 :raw-link (or raw-link path)
3127 :application application
3128 :search-option search-option
3129 :begin begin
3130 :end end
3131 :contents-begin contents-begin
3132 :contents-end contents-end
3133 :post-blank post-blank)))))
3135 (defun org-element-link-interpreter (link contents)
3136 "Interpret LINK object as Org syntax.
3137 CONTENTS is the contents of the object, or nil."
3138 (let ((type (org-element-property :type link))
3139 (raw-link (org-element-property :raw-link link)))
3140 (if (string= type "radio") raw-link
3141 (format "[[%s]%s]"
3142 raw-link
3143 (if contents (format "[%s]" contents) "")))))
3145 (defun org-element-link-successor ()
3146 "Search for the next link object.
3148 Return value is a cons cell whose CAR is `link' and CDR is
3149 beginning position."
3150 (save-excursion
3151 (let ((link-regexp
3152 (if (not org-target-link-regexp) org-any-link-re
3153 (concat org-any-link-re "\\|" org-target-link-regexp))))
3154 (when (re-search-forward link-regexp nil t)
3155 (cons 'link (match-beginning 0))))))
3157 (defun org-element-plain-link-successor ()
3158 "Search for the next plain link object.
3160 Return value is a cons cell whose CAR is `link' and CDR is
3161 beginning position."
3162 (and (save-excursion (re-search-forward org-plain-link-re nil t))
3163 (cons 'link (match-beginning 0))))
3166 ;;;; Macro
3168 (defun org-element-macro-parser ()
3169 "Parse macro at point.
3171 Return a list whose CAR is `macro' and CDR a plist with `:key',
3172 `:args', `:begin', `:end', `:value' and `:post-blank' as
3173 keywords.
3175 Assume point is at the macro."
3176 (save-excursion
3177 (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
3178 (let ((begin (point))
3179 (key (downcase (org-match-string-no-properties 1)))
3180 (value (org-match-string-no-properties 0))
3181 (post-blank (progn (goto-char (match-end 0))
3182 (skip-chars-forward " \t")))
3183 (end (point))
3184 (args (let ((args (org-match-string-no-properties 3)))
3185 (when args
3186 ;; Do not use `org-split-string' since empty
3187 ;; strings are meaningful here.
3188 (split-string
3189 (replace-regexp-in-string
3190 "\\(\\\\*\\)\\(,\\)"
3191 (lambda (str)
3192 (let ((len (length (match-string 1 str))))
3193 (concat (make-string (/ len 2) ?\\)
3194 (if (zerop (mod len 2)) "\000" ","))))
3195 args nil t)
3196 "\000")))))
3197 (list 'macro
3198 (list :key key
3199 :value value
3200 :args args
3201 :begin begin
3202 :end end
3203 :post-blank post-blank)))))
3205 (defun org-element-macro-interpreter (macro contents)
3206 "Interpret MACRO object as Org syntax.
3207 CONTENTS is nil."
3208 (org-element-property :value macro))
3210 (defun org-element-macro-successor ()
3211 "Search for the next macro object.
3213 Return value is cons cell whose CAR is `macro' and CDR is
3214 beginning position."
3215 (save-excursion
3216 (when (re-search-forward
3217 "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
3218 nil t)
3219 (cons 'macro (match-beginning 0)))))
3222 ;;;; Radio-target
3224 (defun org-element-radio-target-parser ()
3225 "Parse radio target at point.
3227 Return a list whose CAR is `radio-target' and CDR a plist with
3228 `:begin', `:end', `:contents-begin', `:contents-end', `:value'
3229 and `:post-blank' as keywords.
3231 Assume point is at the radio target."
3232 (save-excursion
3233 (looking-at org-radio-target-regexp)
3234 (let ((begin (point))
3235 (contents-begin (match-beginning 1))
3236 (contents-end (match-end 1))
3237 (value (org-match-string-no-properties 1))
3238 (post-blank (progn (goto-char (match-end 0))
3239 (skip-chars-forward " \t")))
3240 (end (point)))
3241 (list 'radio-target
3242 (list :begin begin
3243 :end end
3244 :contents-begin contents-begin
3245 :contents-end contents-end
3246 :post-blank post-blank
3247 :value value)))))
3249 (defun org-element-radio-target-interpreter (target contents)
3250 "Interpret TARGET object as Org syntax.
3251 CONTENTS is the contents of the object."
3252 (concat "<<<" contents ">>>"))
3254 (defun org-element-radio-target-successor ()
3255 "Search for the next radio-target object.
3257 Return value is a cons cell whose CAR is `radio-target' and CDR
3258 is beginning position."
3259 (save-excursion
3260 (when (re-search-forward org-radio-target-regexp nil t)
3261 (cons 'radio-target (match-beginning 0)))))
3264 ;;;; Statistics Cookie
3266 (defun org-element-statistics-cookie-parser ()
3267 "Parse statistics cookie at point.
3269 Return a list whose CAR is `statistics-cookie', and CDR a plist
3270 with `:begin', `:end', `:value' and `:post-blank' keywords.
3272 Assume point is at the beginning of the statistics-cookie."
3273 (save-excursion
3274 (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
3275 (let* ((begin (point))
3276 (value (buffer-substring-no-properties
3277 (match-beginning 0) (match-end 0)))
3278 (post-blank (progn (goto-char (match-end 0))
3279 (skip-chars-forward " \t")))
3280 (end (point)))
3281 (list 'statistics-cookie
3282 (list :begin begin
3283 :end end
3284 :value value
3285 :post-blank post-blank)))))
3287 (defun org-element-statistics-cookie-interpreter (statistics-cookie contents)
3288 "Interpret STATISTICS-COOKIE object as Org syntax.
3289 CONTENTS is nil."
3290 (org-element-property :value statistics-cookie))
3292 (defun org-element-statistics-cookie-successor ()
3293 "Search for the next statistics cookie object.
3295 Return value is a cons cell whose CAR is `statistics-cookie' and
3296 CDR is beginning position."
3297 (save-excursion
3298 (when (re-search-forward "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]" nil t)
3299 (cons 'statistics-cookie (match-beginning 0)))))
3302 ;;;; Strike-Through
3304 (defun org-element-strike-through-parser ()
3305 "Parse strike-through object at point.
3307 Return a list whose CAR is `strike-through' and CDR is a plist
3308 with `:begin', `:end', `:contents-begin' and `:contents-end' and
3309 `:post-blank' keywords.
3311 Assume point is at the first plus sign marker."
3312 (save-excursion
3313 (unless (bolp) (backward-char 1))
3314 (looking-at org-emph-re)
3315 (let ((begin (match-beginning 2))
3316 (contents-begin (match-beginning 4))
3317 (contents-end (match-end 4))
3318 (post-blank (progn (goto-char (match-end 2))
3319 (skip-chars-forward " \t")))
3320 (end (point)))
3321 (list 'strike-through
3322 (list :begin begin
3323 :end end
3324 :contents-begin contents-begin
3325 :contents-end contents-end
3326 :post-blank post-blank)))))
3328 (defun org-element-strike-through-interpreter (strike-through contents)
3329 "Interpret STRIKE-THROUGH object as Org syntax.
3330 CONTENTS is the contents of the object."
3331 (format "+%s+" contents))
3334 ;;;; Subscript
3336 (defun org-element-subscript-parser ()
3337 "Parse subscript at point.
3339 Return a list whose CAR is `subscript' and CDR a plist with
3340 `:begin', `:end', `:contents-begin', `:contents-end',
3341 `:use-brackets-p' and `:post-blank' as keywords.
3343 Assume point is at the underscore."
3344 (save-excursion
3345 (unless (bolp) (backward-char))
3346 (looking-at org-match-substring-regexp)
3347 (let ((bracketsp (match-beginning 4))
3348 (begin (match-beginning 2))
3349 (contents-begin (or (match-beginning 4)
3350 (match-beginning 3)))
3351 (contents-end (or (match-end 4) (match-end 3)))
3352 (post-blank (progn (goto-char (match-end 0))
3353 (skip-chars-forward " \t")))
3354 (end (point)))
3355 (list 'subscript
3356 (list :begin begin
3357 :end end
3358 :use-brackets-p bracketsp
3359 :contents-begin contents-begin
3360 :contents-end contents-end
3361 :post-blank post-blank)))))
3363 (defun org-element-subscript-interpreter (subscript contents)
3364 "Interpret SUBSCRIPT object as Org syntax.
3365 CONTENTS is the contents of the object."
3366 (format
3367 (if (org-element-property :use-brackets-p subscript) "_{%s}" "_%s")
3368 contents))
3370 (defun org-element-sub/superscript-successor ()
3371 "Search for the next sub/superscript object.
3373 Return value is a cons cell whose CAR is either `subscript' or
3374 `superscript' and CDR is beginning position."
3375 (save-excursion
3376 (unless (bolp) (backward-char))
3377 (when (re-search-forward org-match-substring-regexp nil t)
3378 (cons (if (string= (match-string 2) "_") 'subscript 'superscript)
3379 (match-beginning 2)))))
3382 ;;;; Superscript
3384 (defun org-element-superscript-parser ()
3385 "Parse superscript at point.
3387 Return a list whose CAR is `superscript' and CDR a plist with
3388 `:begin', `:end', `:contents-begin', `:contents-end',
3389 `:use-brackets-p' and `:post-blank' as keywords.
3391 Assume point is at the caret."
3392 (save-excursion
3393 (unless (bolp) (backward-char))
3394 (looking-at org-match-substring-regexp)
3395 (let ((bracketsp (match-beginning 4))
3396 (begin (match-beginning 2))
3397 (contents-begin (or (match-beginning 4)
3398 (match-beginning 3)))
3399 (contents-end (or (match-end 4) (match-end 3)))
3400 (post-blank (progn (goto-char (match-end 0))
3401 (skip-chars-forward " \t")))
3402 (end (point)))
3403 (list 'superscript
3404 (list :begin begin
3405 :end end
3406 :use-brackets-p bracketsp
3407 :contents-begin contents-begin
3408 :contents-end contents-end
3409 :post-blank post-blank)))))
3411 (defun org-element-superscript-interpreter (superscript contents)
3412 "Interpret SUPERSCRIPT object as Org syntax.
3413 CONTENTS is the contents of the object."
3414 (format
3415 (if (org-element-property :use-brackets-p superscript) "^{%s}" "^%s")
3416 contents))
3419 ;;;; Table Cell
3421 (defun org-element-table-cell-parser ()
3422 "Parse table cell at point.
3424 Return a list whose CAR is `table-cell' and CDR is a plist
3425 containing `:begin', `:end', `:contents-begin', `:contents-end'
3426 and `:post-blank' keywords."
3427 (looking-at "[ \t]*\\(.*?\\)[ \t]*|")
3428 (let* ((begin (match-beginning 0))
3429 (end (match-end 0))
3430 (contents-begin (match-beginning 1))
3431 (contents-end (match-end 1)))
3432 (list 'table-cell
3433 (list :begin begin
3434 :end end
3435 :contents-begin contents-begin
3436 :contents-end contents-end
3437 :post-blank 0))))
3439 (defun org-element-table-cell-interpreter (table-cell contents)
3440 "Interpret TABLE-CELL element as Org syntax.
3441 CONTENTS is the contents of the cell, or nil."
3442 (concat " " contents " |"))
3444 (defun org-element-table-cell-successor ()
3445 "Search for the next table-cell object.
3447 Return value is a cons cell whose CAR is `table-cell' and CDR is
3448 beginning position."
3449 (when (looking-at "[ \t]*.*?[ \t]*|") (cons 'table-cell (point))))
3452 ;;;; Target
3454 (defun org-element-target-parser ()
3455 "Parse target at point.
3457 Return a list whose CAR is `target' and CDR a plist with
3458 `:begin', `:end', `:value' and `:post-blank' as keywords.
3460 Assume point is at the target."
3461 (save-excursion
3462 (looking-at org-target-regexp)
3463 (let ((begin (point))
3464 (value (org-match-string-no-properties 1))
3465 (post-blank (progn (goto-char (match-end 0))
3466 (skip-chars-forward " \t")))
3467 (end (point)))
3468 (list 'target
3469 (list :begin begin
3470 :end end
3471 :value value
3472 :post-blank post-blank)))))
3474 (defun org-element-target-interpreter (target contents)
3475 "Interpret TARGET object as Org syntax.
3476 CONTENTS is nil."
3477 (format "<<%s>>" (org-element-property :value target)))
3479 (defun org-element-target-successor ()
3480 "Search for the next target object.
3482 Return value is a cons cell whose CAR is `target' and CDR is
3483 beginning position."
3484 (save-excursion
3485 (when (re-search-forward org-target-regexp nil t)
3486 (cons 'target (match-beginning 0)))))
3489 ;;;; Timestamp
3491 (defun org-element-timestamp-parser ()
3492 "Parse time stamp at point.
3494 Return a list whose CAR is `timestamp', and CDR a plist with
3495 `:type', `:raw-value', `:year-start', `:month-start',
3496 `:day-start', `:hour-start', `:minute-start', `:year-end',
3497 `:month-end', `:day-end', `:hour-end', `:minute-end',
3498 `:repeater-type', `:repeater-value', `:repeater-unit',
3499 `:warning-type', `:warning-value', `:warning-unit', `:begin',
3500 `:end', `:value' and `:post-blank' keywords.
3502 Assume point is at the beginning of the timestamp."
3503 (save-excursion
3504 (let* ((begin (point))
3505 (activep (eq (char-after) ?<))
3506 (raw-value
3507 (progn
3508 (looking-at "\\([<[]\\(%%\\)?.*?\\)[]>]\\(?:--\\([<[].*?[]>]\\)\\)?")
3509 (match-string-no-properties 0)))
3510 (date-start (match-string-no-properties 1))
3511 (date-end (match-string 3))
3512 (diaryp (match-beginning 2))
3513 (post-blank (progn (goto-char (match-end 0))
3514 (skip-chars-forward " \t")))
3515 (end (point))
3516 (time-range
3517 (and (not diaryp)
3518 (string-match
3519 "[012]?[0-9]:[0-5][0-9]\\(-\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)"
3520 date-start)
3521 (cons (string-to-number (match-string 2 date-start))
3522 (string-to-number (match-string 3 date-start)))))
3523 (type (cond (diaryp 'diary)
3524 ((and activep (or date-end time-range)) 'active-range)
3525 (activep 'active)
3526 ((or date-end time-range) 'inactive-range)
3527 (t 'inactive)))
3528 (repeater-props
3529 (and (not diaryp)
3530 (string-match "\\([.+]?\\+\\)\\([0-9]+\\)\\([hdwmy]\\)"
3531 raw-value)
3532 (list
3533 :repeater-type
3534 (let ((type (match-string 1 raw-value)))
3535 (cond ((equal "++" type) 'catch-up)
3536 ((equal ".+" type) 'restart)
3537 (t 'cumulate)))
3538 :repeater-value (string-to-number (match-string 2 raw-value))
3539 :repeater-unit
3540 (case (string-to-char (match-string 3 raw-value))
3541 (?h 'hour) (?d 'day) (?w 'week) (?m 'month) (t 'year)))))
3542 (warning-props
3543 (and (not diaryp)
3544 (string-match "\\(-\\)?-\\([0-9]+\\)\\([hdwmy]\\)" raw-value)
3545 (list
3546 :warning-type (if (match-string 1 raw-value) 'first 'all)
3547 :warning-value (string-to-number (match-string 2 raw-value))
3548 :warning-unit
3549 (case (string-to-char (match-string 3 raw-value))
3550 (?h 'hour) (?d 'day) (?w 'week) (?m 'month) (t 'year)))))
3551 year-start month-start day-start hour-start minute-start year-end
3552 month-end day-end hour-end minute-end)
3553 ;; Parse date-start.
3554 (unless diaryp
3555 (let ((date (org-parse-time-string date-start t)))
3556 (setq year-start (nth 5 date)
3557 month-start (nth 4 date)
3558 day-start (nth 3 date)
3559 hour-start (nth 2 date)
3560 minute-start (nth 1 date))))
3561 ;; Compute date-end. It can be provided directly in time-stamp,
3562 ;; or extracted from time range. Otherwise, it defaults to the
3563 ;; same values as date-start.
3564 (unless diaryp
3565 (let ((date (and date-end (org-parse-time-string date-end t))))
3566 (setq year-end (or (nth 5 date) year-start)
3567 month-end (or (nth 4 date) month-start)
3568 day-end (or (nth 3 date) day-start)
3569 hour-end (or (nth 2 date) (car time-range) hour-start)
3570 minute-end (or (nth 1 date) (cdr time-range) minute-start))))
3571 (list 'timestamp
3572 (nconc (list :type type
3573 :raw-value raw-value
3574 :year-start year-start
3575 :month-start month-start
3576 :day-start day-start
3577 :hour-start hour-start
3578 :minute-start minute-start
3579 :year-end year-end
3580 :month-end month-end
3581 :day-end day-end
3582 :hour-end hour-end
3583 :minute-end minute-end
3584 :begin begin
3585 :end end
3586 :post-blank post-blank)
3587 repeater-props
3588 warning-props)))))
3590 (defun org-element-timestamp-interpreter (timestamp contents)
3591 "Interpret TIMESTAMP object as Org syntax.
3592 CONTENTS is nil."
3593 (let* ((repeat-string
3594 (concat
3595 (case (org-element-property :repeater-type timestamp)
3596 (cumulate "+") (catch-up "++") (restart ".+"))
3597 (let ((val (org-element-property :repeater-value timestamp)))
3598 (and val (number-to-string val)))
3599 (case (org-element-property :repeater-unit timestamp)
3600 (hour "h") (day "d") (week "w") (month "m") (year "y"))))
3601 (warning-string
3602 (concat
3603 (case (org-element-property :warning-type timestamp)
3604 (first "--")
3605 (all "-"))
3606 (let ((val (org-element-property :warning-value timestamp)))
3607 (and val (number-to-string val)))
3608 (case (org-element-property :warning-unit timestamp)
3609 (hour "h") (day "d") (week "w") (month "m") (year "y"))))
3610 (build-ts-string
3611 ;; Build an Org timestamp string from TIME. ACTIVEP is
3612 ;; non-nil when time stamp is active. If WITH-TIME-P is
3613 ;; non-nil, add a time part. HOUR-END and MINUTE-END
3614 ;; specify a time range in the timestamp. REPEAT-STRING is
3615 ;; the repeater string, if any.
3616 (lambda (time activep &optional with-time-p hour-end minute-end)
3617 (let ((ts (format-time-string
3618 (funcall (if with-time-p 'cdr 'car)
3619 org-time-stamp-formats)
3620 time)))
3621 (when (and hour-end minute-end)
3622 (string-match "[012]?[0-9]:[0-5][0-9]" ts)
3623 (setq ts
3624 (replace-match
3625 (format "\\&-%02d:%02d" hour-end minute-end)
3626 nil nil ts)))
3627 (unless activep (setq ts (format "[%s]" (substring ts 1 -1))))
3628 (dolist (s (list repeat-string warning-string))
3629 (when (org-string-nw-p s)
3630 (setq ts (concat (substring ts 0 -1)
3633 (substring ts -1)))))
3634 ;; Return value.
3635 ts)))
3636 (type (org-element-property :type timestamp)))
3637 (case type
3638 ((active inactive)
3639 (let* ((minute-start (org-element-property :minute-start timestamp))
3640 (minute-end (org-element-property :minute-end timestamp))
3641 (hour-start (org-element-property :hour-start timestamp))
3642 (hour-end (org-element-property :hour-end timestamp))
3643 (time-range-p (and hour-start hour-end minute-start minute-end
3644 (or (/= hour-start hour-end)
3645 (/= minute-start minute-end)))))
3646 (funcall
3647 build-ts-string
3648 (encode-time 0
3649 (or minute-start 0)
3650 (or hour-start 0)
3651 (org-element-property :day-start timestamp)
3652 (org-element-property :month-start timestamp)
3653 (org-element-property :year-start timestamp))
3654 (eq type 'active)
3655 (and hour-start minute-start)
3656 (and time-range-p hour-end)
3657 (and time-range-p minute-end))))
3658 ((active-range inactive-range)
3659 (let ((minute-start (org-element-property :minute-start timestamp))
3660 (minute-end (org-element-property :minute-end timestamp))
3661 (hour-start (org-element-property :hour-start timestamp))
3662 (hour-end (org-element-property :hour-end timestamp)))
3663 (concat
3664 (funcall
3665 build-ts-string (encode-time
3667 (or minute-start 0)
3668 (or hour-start 0)
3669 (org-element-property :day-start timestamp)
3670 (org-element-property :month-start timestamp)
3671 (org-element-property :year-start timestamp))
3672 (eq type 'active-range)
3673 (and hour-start minute-start))
3674 "--"
3675 (funcall build-ts-string
3676 (encode-time 0
3677 (or minute-end 0)
3678 (or hour-end 0)
3679 (org-element-property :day-end timestamp)
3680 (org-element-property :month-end timestamp)
3681 (org-element-property :year-end timestamp))
3682 (eq type 'active-range)
3683 (and hour-end minute-end))))))))
3685 (defun org-element-timestamp-successor ()
3686 "Search for the next timestamp object.
3688 Return value is a cons cell whose CAR is `timestamp' and CDR is
3689 beginning position."
3690 (save-excursion
3691 (when (re-search-forward
3692 (concat org-ts-regexp-both
3693 "\\|"
3694 "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
3695 "\\|"
3696 "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
3697 nil t)
3698 (cons 'timestamp (match-beginning 0)))))
3701 ;;;; Underline
3703 (defun org-element-underline-parser ()
3704 "Parse underline object at point.
3706 Return a list whose CAR is `underline' and CDR is a plist with
3707 `:begin', `:end', `:contents-begin' and `:contents-end' and
3708 `:post-blank' keywords.
3710 Assume point is at the first underscore marker."
3711 (save-excursion
3712 (unless (bolp) (backward-char 1))
3713 (looking-at org-emph-re)
3714 (let ((begin (match-beginning 2))
3715 (contents-begin (match-beginning 4))
3716 (contents-end (match-end 4))
3717 (post-blank (progn (goto-char (match-end 2))
3718 (skip-chars-forward " \t")))
3719 (end (point)))
3720 (list 'underline
3721 (list :begin begin
3722 :end end
3723 :contents-begin contents-begin
3724 :contents-end contents-end
3725 :post-blank post-blank)))))
3727 (defun org-element-underline-interpreter (underline contents)
3728 "Interpret UNDERLINE object as Org syntax.
3729 CONTENTS is the contents of the object."
3730 (format "_%s_" contents))
3733 ;;;; Verbatim
3735 (defun org-element-verbatim-parser ()
3736 "Parse verbatim object at point.
3738 Return a list whose CAR is `verbatim' and CDR is a plist with
3739 `:value', `:begin', `:end' and `:post-blank' keywords.
3741 Assume point is at the first equal sign marker."
3742 (save-excursion
3743 (unless (bolp) (backward-char 1))
3744 (looking-at org-emph-re)
3745 (let ((begin (match-beginning 2))
3746 (value (org-match-string-no-properties 4))
3747 (post-blank (progn (goto-char (match-end 2))
3748 (skip-chars-forward " \t")))
3749 (end (point)))
3750 (list 'verbatim
3751 (list :value value
3752 :begin begin
3753 :end end
3754 :post-blank post-blank)))))
3756 (defun org-element-verbatim-interpreter (verbatim contents)
3757 "Interpret VERBATIM object as Org syntax.
3758 CONTENTS is nil."
3759 (format "=%s=" (org-element-property :value verbatim)))
3763 ;;; Parsing Element Starting At Point
3765 ;; `org-element--current-element' is the core function of this section.
3766 ;; It returns the Lisp representation of the element starting at
3767 ;; point.
3769 ;; `org-element--current-element' makes use of special modes. They
3770 ;; are activated for fixed element chaining (i.e. `plain-list' >
3771 ;; `item') or fixed conditional element chaining (i.e. `headline' >
3772 ;; `section'). Special modes are: `first-section', `item',
3773 ;; `node-property', `section' and `table-row'.
3775 (defun org-element--current-element
3776 (limit &optional granularity special structure)
3777 "Parse the element starting at point.
3779 Return value is a list like (TYPE PROPS) where TYPE is the type
3780 of the element and PROPS a plist of properties associated to the
3781 element.
3783 Possible types are defined in `org-element-all-elements'.
3785 LIMIT bounds the search.
3787 Optional argument GRANULARITY determines the depth of the
3788 recursion. Allowed values are `headline', `greater-element',
3789 `element', `object' or nil. When it is broader than `object' (or
3790 nil), secondary values will not be parsed, since they only
3791 contain objects.
3793 Optional argument SPECIAL, when non-nil, can be either
3794 `first-section', `item', `node-property', `section', and
3795 `table-row'.
3797 If STRUCTURE isn't provided but SPECIAL is set to `item', it will
3798 be computed.
3800 This function assumes point is always at the beginning of the
3801 element it has to parse."
3802 (save-excursion
3803 (let ((case-fold-search t)
3804 ;; Determine if parsing depth allows for secondary strings
3805 ;; parsing. It only applies to elements referenced in
3806 ;; `org-element-secondary-value-alist'.
3807 (raw-secondary-p (and granularity (not (eq granularity 'object)))))
3808 (cond
3809 ;; Item.
3810 ((eq special 'item)
3811 (org-element-item-parser limit structure raw-secondary-p))
3812 ;; Table Row.
3813 ((eq special 'table-row) (org-element-table-row-parser limit))
3814 ;; Node Property.
3815 ((eq special 'node-property) (org-element-node-property-parser limit))
3816 ;; Headline.
3817 ((org-with-limited-levels (org-at-heading-p))
3818 (org-element-headline-parser limit raw-secondary-p))
3819 ;; Sections (must be checked after headline).
3820 ((eq special 'section) (org-element-section-parser limit))
3821 ((eq special 'first-section)
3822 (org-element-section-parser
3823 (or (save-excursion (org-with-limited-levels (outline-next-heading)))
3824 limit)))
3825 ;; When not at bol, point is at the beginning of an item or
3826 ;; a footnote definition: next item is always a paragraph.
3827 ((not (bolp)) (org-element-paragraph-parser limit (list (point))))
3828 ;; Planning and Clock.
3829 ((looking-at org-planning-or-clock-line-re)
3830 (if (equal (match-string 1) org-clock-string)
3831 (org-element-clock-parser limit)
3832 (org-element-planning-parser limit)))
3833 ;; Inlinetask.
3834 ((org-at-heading-p)
3835 (org-element-inlinetask-parser limit raw-secondary-p))
3836 ;; From there, elements can have affiliated keywords.
3837 (t (let ((affiliated (org-element--collect-affiliated-keywords limit)))
3838 (cond
3839 ;; Jumping over affiliated keywords put point off-limits.
3840 ;; Parse them as regular keywords.
3841 ((and (cdr affiliated) (>= (point) limit))
3842 (goto-char (car affiliated))
3843 (org-element-keyword-parser limit nil))
3844 ;; LaTeX Environment.
3845 ((looking-at
3846 "[ \t]*\\\\begin{[A-Za-z0-9*]+}\\(\\[.*?\\]\\|{.*?}\\)*[ \t]*$")
3847 (org-element-latex-environment-parser limit affiliated))
3848 ;; Drawer and Property Drawer.
3849 ((looking-at org-drawer-regexp)
3850 (if (equal (match-string 1) "PROPERTIES")
3851 (org-element-property-drawer-parser limit affiliated)
3852 (org-element-drawer-parser limit affiliated)))
3853 ;; Fixed Width
3854 ((looking-at "[ \t]*:\\( \\|$\\)")
3855 (org-element-fixed-width-parser limit affiliated))
3856 ;; Inline Comments, Blocks, Babel Calls, Dynamic Blocks and
3857 ;; Keywords.
3858 ((looking-at "[ \t]*#")
3859 (goto-char (match-end 0))
3860 (cond ((looking-at "\\(?: \\|$\\)")
3861 (beginning-of-line)
3862 (org-element-comment-parser limit affiliated))
3863 ((looking-at "\\+BEGIN_\\(\\S-+\\)")
3864 (beginning-of-line)
3865 (let ((parser (assoc (upcase (match-string 1))
3866 org-element-block-name-alist)))
3867 (if parser (funcall (cdr parser) limit affiliated)
3868 (org-element-special-block-parser limit affiliated))))
3869 ((looking-at "\\+CALL:")
3870 (beginning-of-line)
3871 (org-element-babel-call-parser limit affiliated))
3872 ((looking-at "\\+BEGIN:? ")
3873 (beginning-of-line)
3874 (org-element-dynamic-block-parser limit affiliated))
3875 ((looking-at "\\+\\S-+:")
3876 (beginning-of-line)
3877 (org-element-keyword-parser limit affiliated))
3879 (beginning-of-line)
3880 (org-element-paragraph-parser limit affiliated))))
3881 ;; Footnote Definition.
3882 ((looking-at org-footnote-definition-re)
3883 (org-element-footnote-definition-parser limit affiliated))
3884 ;; Horizontal Rule.
3885 ((looking-at "[ \t]*-\\{5,\\}[ \t]*$")
3886 (org-element-horizontal-rule-parser limit affiliated))
3887 ;; Diary Sexp.
3888 ((looking-at "%%(")
3889 (org-element-diary-sexp-parser limit affiliated))
3890 ;; Table.
3891 ((org-at-table-p t) (org-element-table-parser limit affiliated))
3892 ;; List.
3893 ((looking-at (org-item-re))
3894 (org-element-plain-list-parser
3895 limit affiliated
3896 (or structure (org-element--list-struct limit))))
3897 ;; Default element: Paragraph.
3898 (t (org-element-paragraph-parser limit affiliated)))))))))
3901 ;; Most elements can have affiliated keywords. When looking for an
3902 ;; element beginning, we want to move before them, as they belong to
3903 ;; that element, and, in the meantime, collect information they give
3904 ;; into appropriate properties. Hence the following function.
3906 (defun org-element--collect-affiliated-keywords (limit)
3907 "Collect affiliated keywords from point down to LIMIT.
3909 Return a list whose CAR is the position at the first of them and
3910 CDR a plist of keywords and values and move point to the
3911 beginning of the first line after them.
3913 As a special case, if element doesn't start at the beginning of
3914 the line (i.e. a paragraph starting an item), CAR is current
3915 position of point and CDR is nil."
3916 (if (not (bolp)) (list (point))
3917 (let ((case-fold-search t)
3918 (origin (point))
3919 ;; RESTRICT is the list of objects allowed in parsed
3920 ;; keywords value.
3921 (restrict (org-element-restriction 'keyword))
3922 output)
3923 (while (and (< (point) limit) (looking-at org-element--affiliated-re))
3924 (let* ((raw-kwd (upcase (match-string 1)))
3925 ;; Apply translation to RAW-KWD. From there, KWD is
3926 ;; the official keyword.
3927 (kwd (or (cdr (assoc raw-kwd
3928 org-element-keyword-translation-alist))
3929 raw-kwd))
3930 ;; Find main value for any keyword.
3931 (value
3932 (save-match-data
3933 (org-trim
3934 (buffer-substring-no-properties
3935 (match-end 0) (point-at-eol)))))
3936 ;; PARSEDP is non-nil when keyword should have its
3937 ;; value parsed.
3938 (parsedp (member kwd org-element-parsed-keywords))
3939 ;; If KWD is a dual keyword, find its secondary
3940 ;; value. Maybe parse it.
3941 (dualp (member kwd org-element-dual-keywords))
3942 (dual-value
3943 (and dualp
3944 (let ((sec (org-match-string-no-properties 2)))
3945 (if (or (not sec) (not parsedp)) sec
3946 (org-element-parse-secondary-string sec restrict)))))
3947 ;; Attribute a property name to KWD.
3948 (kwd-sym (and kwd (intern (concat ":" (downcase kwd))))))
3949 ;; Now set final shape for VALUE.
3950 (when parsedp
3951 (setq value (org-element-parse-secondary-string value restrict)))
3952 (when dualp
3953 (setq value (and (or value dual-value) (cons value dual-value))))
3954 (when (or (member kwd org-element-multiple-keywords)
3955 ;; Attributes can always appear on multiple lines.
3956 (string-match "^ATTR_" kwd))
3957 (setq value (cons value (plist-get output kwd-sym))))
3958 ;; Eventually store the new value in OUTPUT.
3959 (setq output (plist-put output kwd-sym value))
3960 ;; Move to next keyword.
3961 (forward-line)))
3962 ;; If affiliated keywords are orphaned: move back to first one.
3963 ;; They will be parsed as a paragraph.
3964 (when (looking-at "[ \t]*$") (goto-char origin) (setq output nil))
3965 ;; Return value.
3966 (cons origin output))))
3970 ;;; The Org Parser
3972 ;; The two major functions here are `org-element-parse-buffer', which
3973 ;; parses Org syntax inside the current buffer, taking into account
3974 ;; region, narrowing, or even visibility if specified, and
3975 ;; `org-element-parse-secondary-string', which parses objects within
3976 ;; a given string.
3978 ;; The (almost) almighty `org-element-map' allows to apply a function
3979 ;; on elements or objects matching some type, and accumulate the
3980 ;; resulting values. In an export situation, it also skips unneeded
3981 ;; parts of the parse tree.
3983 (defun org-element-parse-buffer (&optional granularity visible-only)
3984 "Recursively parse the buffer and return structure.
3985 If narrowing is in effect, only parse the visible part of the
3986 buffer.
3988 Optional argument GRANULARITY determines the depth of the
3989 recursion. It can be set to the following symbols:
3991 `headline' Only parse headlines.
3992 `greater-element' Don't recurse into greater elements excepted
3993 headlines and sections. Thus, elements
3994 parsed are the top-level ones.
3995 `element' Parse everything but objects and plain text.
3996 `object' Parse the complete buffer (default).
3998 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3999 elements.
4001 An element or an objects is represented as a list with the
4002 pattern (TYPE PROPERTIES CONTENTS), where :
4004 TYPE is a symbol describing the element or object. See
4005 `org-element-all-elements' and `org-element-all-objects' for an
4006 exhaustive list of such symbols. One can retrieve it with
4007 `org-element-type' function.
4009 PROPERTIES is the list of attributes attached to the element or
4010 object, as a plist. Although most of them are specific to the
4011 element or object type, all types share `:begin', `:end',
4012 `:post-blank' and `:parent' properties, which respectively
4013 refer to buffer position where the element or object starts,
4014 ends, the number of white spaces or blank lines after it, and
4015 the element or object containing it. Properties values can be
4016 obtained by using `org-element-property' function.
4018 CONTENTS is a list of elements, objects or raw strings
4019 contained in the current element or object, when applicable.
4020 One can access them with `org-element-contents' function.
4022 The Org buffer has `org-data' as type and nil as properties.
4023 `org-element-map' function can be used to find specific elements
4024 or objects within the parse tree.
4026 This function assumes that current major mode is `org-mode'."
4027 (save-excursion
4028 (goto-char (point-min))
4029 (org-skip-whitespace)
4030 (org-element--parse-elements
4031 (point-at-bol) (point-max)
4032 ;; Start in `first-section' mode so text before the first
4033 ;; headline belongs to a section.
4034 'first-section nil granularity visible-only (list 'org-data nil))))
4036 (defun org-element-parse-secondary-string (string restriction &optional parent)
4037 "Recursively parse objects in STRING and return structure.
4039 RESTRICTION is a symbol limiting the object types that will be
4040 looked after.
4042 Optional argument PARENT, when non-nil, is the element or object
4043 containing the secondary string. It is used to set correctly
4044 `:parent' property within the string."
4045 ;; Copy buffer-local variables listed in
4046 ;; `org-element-object-variables' into temporary buffer. This is
4047 ;; required since object parsing is dependent on these variables.
4048 (let ((pairs (delq nil (mapcar (lambda (var)
4049 (when (boundp var)
4050 (cons var (symbol-value var))))
4051 org-element-object-variables))))
4052 (with-temp-buffer
4053 (mapc (lambda (pair) (org-set-local (car pair) (cdr pair))) pairs)
4054 (insert string)
4055 (let ((secondary (org-element--parse-objects
4056 (point-min) (point-max) nil restriction)))
4057 (when parent
4058 (mapc (lambda (obj) (org-element-put-property obj :parent parent))
4059 secondary))
4060 secondary))))
4062 (defun org-element-map
4063 (data types fun &optional info first-match no-recursion with-affiliated)
4064 "Map a function on selected elements or objects.
4066 DATA is a parse tree, an element, an object, a string, or a list
4067 of such constructs. TYPES is a symbol or list of symbols of
4068 elements or objects types (see `org-element-all-elements' and
4069 `org-element-all-objects' for a complete list of types). FUN is
4070 the function called on the matching element or object. It has to
4071 accept one argument: the element or object itself.
4073 When optional argument INFO is non-nil, it should be a plist
4074 holding export options. In that case, parts of the parse tree
4075 not exportable according to that property list will be skipped.
4077 When optional argument FIRST-MATCH is non-nil, stop at the first
4078 match for which FUN doesn't return nil, and return that value.
4080 Optional argument NO-RECURSION is a symbol or a list of symbols
4081 representing elements or objects types. `org-element-map' won't
4082 enter any recursive element or object whose type belongs to that
4083 list. Though, FUN can still be applied on them.
4085 When optional argument WITH-AFFILIATED is non-nil, FUN will also
4086 apply to matching objects within parsed affiliated keywords (see
4087 `org-element-parsed-keywords').
4089 Nil values returned from FUN do not appear in the results.
4092 Examples:
4093 ---------
4095 Assuming TREE is a variable containing an Org buffer parse tree,
4096 the following example will return a flat list of all `src-block'
4097 and `example-block' elements in it:
4099 \(org-element-map tree '(example-block src-block) 'identity)
4101 The following snippet will find the first headline with a level
4102 of 1 and a \"phone\" tag, and will return its beginning position:
4104 \(org-element-map tree 'headline
4105 \(lambda (hl)
4106 \(and (= (org-element-property :level hl) 1)
4107 \(member \"phone\" (org-element-property :tags hl))
4108 \(org-element-property :begin hl)))
4109 nil t)
4111 The next example will return a flat list of all `plain-list' type
4112 elements in TREE that are not a sub-list themselves:
4114 \(org-element-map tree 'plain-list 'identity nil nil 'plain-list)
4116 Eventually, this example will return a flat list of all `bold'
4117 type objects containing a `latex-snippet' type object, even
4118 looking into captions:
4120 \(org-element-map tree 'bold
4121 \(lambda (b)
4122 \(and (org-element-map b 'latex-snippet 'identity nil t) b))
4123 nil nil nil t)"
4124 ;; Ensure TYPES and NO-RECURSION are a list, even of one element.
4125 (unless (listp types) (setq types (list types)))
4126 (unless (listp no-recursion) (setq no-recursion (list no-recursion)))
4127 ;; Recursion depth is determined by --CATEGORY.
4128 (let* ((--category
4129 (catch 'found
4130 (let ((category 'greater-elements))
4131 (mapc (lambda (type)
4132 (cond ((or (memq type org-element-all-objects)
4133 (eq type 'plain-text))
4134 ;; If one object is found, the function
4135 ;; has to recurse into every object.
4136 (throw 'found 'objects))
4137 ((not (memq type org-element-greater-elements))
4138 ;; If one regular element is found, the
4139 ;; function has to recurse, at least,
4140 ;; into every element it encounters.
4141 (and (not (eq category 'elements))
4142 (setq category 'elements)))))
4143 types)
4144 category)))
4145 ;; Compute properties for affiliated keywords if necessary.
4146 (--affiliated-alist
4147 (and with-affiliated
4148 (mapcar (lambda (kwd)
4149 (cons kwd (intern (concat ":" (downcase kwd)))))
4150 org-element-affiliated-keywords)))
4151 --acc
4152 --walk-tree
4153 (--walk-tree
4154 (function
4155 (lambda (--data)
4156 ;; Recursively walk DATA. INFO, if non-nil, is a plist
4157 ;; holding contextual information.
4158 (let ((--type (org-element-type --data)))
4159 (cond
4160 ((not --data))
4161 ;; Ignored element in an export context.
4162 ((and info (memq --data (plist-get info :ignore-list))))
4163 ;; List of elements or objects.
4164 ((not --type) (mapc --walk-tree --data))
4165 ;; Unconditionally enter parse trees.
4166 ((eq --type 'org-data)
4167 (mapc --walk-tree (org-element-contents --data)))
4169 ;; Check if TYPE is matching among TYPES. If so,
4170 ;; apply FUN to --DATA and accumulate return value
4171 ;; into --ACC (or exit if FIRST-MATCH is non-nil).
4172 (when (memq --type types)
4173 (let ((result (funcall fun --data)))
4174 (cond ((not result))
4175 (first-match (throw '--map-first-match result))
4176 (t (push result --acc)))))
4177 ;; If --DATA has a secondary string that can contain
4178 ;; objects with their type among TYPES, look into it.
4179 (when (and (eq --category 'objects) (not (stringp --data)))
4180 (let ((sec-prop
4181 (assq --type org-element-secondary-value-alist)))
4182 (when sec-prop
4183 (funcall --walk-tree
4184 (org-element-property (cdr sec-prop) --data)))))
4185 ;; If --DATA has any affiliated keywords and
4186 ;; WITH-AFFILIATED is non-nil, look for objects in
4187 ;; them.
4188 (when (and with-affiliated
4189 (eq --category 'objects)
4190 (memq --type org-element-all-elements))
4191 (mapc (lambda (kwd-pair)
4192 (let ((kwd (car kwd-pair))
4193 (value (org-element-property
4194 (cdr kwd-pair) --data)))
4195 ;; Pay attention to the type of value.
4196 ;; Preserve order for multiple keywords.
4197 (cond
4198 ((not value))
4199 ((and (member kwd org-element-multiple-keywords)
4200 (member kwd org-element-dual-keywords))
4201 (mapc (lambda (line)
4202 (funcall --walk-tree (cdr line))
4203 (funcall --walk-tree (car line)))
4204 (reverse value)))
4205 ((member kwd org-element-multiple-keywords)
4206 (mapc (lambda (line) (funcall --walk-tree line))
4207 (reverse value)))
4208 ((member kwd org-element-dual-keywords)
4209 (funcall --walk-tree (cdr value))
4210 (funcall --walk-tree (car value)))
4211 (t (funcall --walk-tree value)))))
4212 --affiliated-alist))
4213 ;; Determine if a recursion into --DATA is possible.
4214 (cond
4215 ;; --TYPE is explicitly removed from recursion.
4216 ((memq --type no-recursion))
4217 ;; --DATA has no contents.
4218 ((not (org-element-contents --data)))
4219 ;; Looking for greater elements but --DATA is simply
4220 ;; an element or an object.
4221 ((and (eq --category 'greater-elements)
4222 (not (memq --type org-element-greater-elements))))
4223 ;; Looking for elements but --DATA is an object.
4224 ((and (eq --category 'elements)
4225 (memq --type org-element-all-objects)))
4226 ;; In any other case, map contents.
4227 (t (mapc --walk-tree (org-element-contents --data)))))))))))
4228 (catch '--map-first-match
4229 (funcall --walk-tree data)
4230 ;; Return value in a proper order.
4231 (nreverse --acc))))
4232 (put 'org-element-map 'lisp-indent-function 2)
4234 ;; The following functions are internal parts of the parser.
4236 ;; The first one, `org-element--parse-elements' acts at the element's
4237 ;; level.
4239 ;; The second one, `org-element--parse-objects' applies on all objects
4240 ;; of a paragraph or a secondary string. It uses
4241 ;; `org-element--get-next-object-candidates' to optimize the search of
4242 ;; the next object in the buffer.
4244 ;; More precisely, that function looks for every allowed object type
4245 ;; first. Then, it discards failed searches, keeps further matches,
4246 ;; and searches again types matched behind point, for subsequent
4247 ;; calls. Thus, searching for a given type fails only once, and every
4248 ;; object is searched only once at top level (but sometimes more for
4249 ;; nested types).
4251 (defun org-element--parse-elements
4252 (beg end special structure granularity visible-only acc)
4253 "Parse elements between BEG and END positions.
4255 SPECIAL prioritize some elements over the others. It can be set
4256 to `first-section', `section' `item' or `table-row'.
4258 When value is `item', STRUCTURE will be used as the current list
4259 structure.
4261 GRANULARITY determines the depth of the recursion. See
4262 `org-element-parse-buffer' for more information.
4264 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
4265 elements.
4267 Elements are accumulated into ACC."
4268 (save-excursion
4269 (goto-char beg)
4270 ;; Visible only: skip invisible parts at the beginning of the
4271 ;; element.
4272 (when (and visible-only (org-invisible-p2))
4273 (goto-char (min (1+ (org-find-visible)) end)))
4274 ;; When parsing only headlines, skip any text before first one.
4275 (when (and (eq granularity 'headline) (not (org-at-heading-p)))
4276 (org-with-limited-levels (outline-next-heading)))
4277 ;; Main loop start.
4278 (while (< (point) end)
4279 ;; Find current element's type and parse it accordingly to
4280 ;; its category.
4281 (let* ((element (org-element--current-element
4282 end granularity special structure))
4283 (type (org-element-type element))
4284 (cbeg (org-element-property :contents-begin element)))
4285 (goto-char (org-element-property :end element))
4286 ;; Visible only: skip invisible parts between siblings.
4287 (when (and visible-only (org-invisible-p2))
4288 (goto-char (min (1+ (org-find-visible)) end)))
4289 ;; Fill ELEMENT contents by side-effect.
4290 (cond
4291 ;; If element has no contents, don't modify it.
4292 ((not cbeg))
4293 ;; Greater element: parse it between `contents-begin' and
4294 ;; `contents-end'. Make sure GRANULARITY allows the
4295 ;; recursion, or ELEMENT is a headline, in which case going
4296 ;; inside is mandatory, in order to get sub-level headings.
4297 ((and (memq type org-element-greater-elements)
4298 (or (memq granularity '(element object nil))
4299 (and (eq granularity 'greater-element)
4300 (eq type 'section))
4301 (eq type 'headline)))
4302 (org-element--parse-elements
4303 cbeg (org-element-property :contents-end element)
4304 ;; Possibly switch to a special mode.
4305 (case type
4306 (headline 'section)
4307 (plain-list 'item)
4308 (property-drawer 'node-property)
4309 (table 'table-row))
4310 (and (memq type '(item plain-list))
4311 (org-element-property :structure element))
4312 granularity visible-only element))
4313 ;; ELEMENT has contents. Parse objects inside, if
4314 ;; GRANULARITY allows it.
4315 ((memq granularity '(object nil))
4316 (org-element--parse-objects
4317 cbeg (org-element-property :contents-end element) element
4318 (org-element-restriction type))))
4319 (org-element-adopt-elements acc element)))
4320 ;; Return result.
4321 acc))
4323 (defun org-element--parse-objects (beg end acc restriction)
4324 "Parse objects between BEG and END and return recursive structure.
4326 Objects are accumulated in ACC.
4328 RESTRICTION is a list of object successors which are allowed in
4329 the current object."
4330 (let ((candidates 'initial))
4331 (save-excursion
4332 (save-restriction
4333 (narrow-to-region beg end)
4334 (goto-char (point-min))
4335 (while (and (not (eobp))
4336 (setq candidates
4337 (org-element--get-next-object-candidates
4338 restriction candidates)))
4339 (let ((next-object
4340 (let ((pos (apply 'min (mapcar 'cdr candidates))))
4341 (save-excursion
4342 (goto-char pos)
4343 (funcall (intern (format "org-element-%s-parser"
4344 (car (rassq pos candidates)))))))))
4345 ;; 1. Text before any object. Untabify it.
4346 (let ((obj-beg (org-element-property :begin next-object)))
4347 (unless (= (point) obj-beg)
4348 (setq acc
4349 (org-element-adopt-elements
4351 (replace-regexp-in-string
4352 "\t" (make-string tab-width ? )
4353 (buffer-substring-no-properties (point) obj-beg))))))
4354 ;; 2. Object...
4355 (let ((obj-end (org-element-property :end next-object))
4356 (cont-beg (org-element-property :contents-begin next-object)))
4357 ;; Fill contents of NEXT-OBJECT by side-effect, if it has
4358 ;; a recursive type.
4359 (when (and cont-beg
4360 (memq (car next-object) org-element-recursive-objects))
4361 (org-element--parse-objects
4362 cont-beg (org-element-property :contents-end next-object)
4363 next-object (org-element-restriction next-object)))
4364 (setq acc (org-element-adopt-elements acc next-object))
4365 (goto-char obj-end))))
4366 ;; 3. Text after last object. Untabify it.
4367 (unless (eobp)
4368 (setq acc
4369 (org-element-adopt-elements
4371 (replace-regexp-in-string
4372 "\t" (make-string tab-width ? )
4373 (buffer-substring-no-properties (point) end)))))
4374 ;; Result.
4375 acc))))
4377 (defun org-element--get-next-object-candidates (restriction objects)
4378 "Return an alist of candidates for the next object.
4380 RESTRICTION is a list of object types, as symbols. Only
4381 candidates with such types are looked after.
4383 OBJECTS is the previous candidates alist. If it is set to
4384 `initial', no search has been done before, and all symbols in
4385 RESTRICTION should be looked after.
4387 Return value is an alist whose CAR is the object type and CDR its
4388 beginning position."
4389 (delq
4391 (if (eq objects 'initial)
4392 ;; When searching for the first time, look for every successor
4393 ;; allowed in RESTRICTION.
4394 (mapcar
4395 (lambda (res)
4396 (funcall (intern (format "org-element-%s-successor" res))))
4397 restriction)
4398 ;; Focus on objects returned during last search. Keep those
4399 ;; still after point. Search again objects before it.
4400 (mapcar
4401 (lambda (obj)
4402 (if (>= (cdr obj) (point)) obj
4403 (let* ((type (car obj))
4404 (succ (or (cdr (assq type org-element-object-successor-alist))
4405 type)))
4406 (and succ
4407 (funcall (intern (format "org-element-%s-successor" succ)))))))
4408 objects))))
4412 ;;; Towards A Bijective Process
4414 ;; The parse tree obtained with `org-element-parse-buffer' is really
4415 ;; a snapshot of the corresponding Org buffer. Therefore, it can be
4416 ;; interpreted and expanded into a string with canonical Org syntax.
4417 ;; Hence `org-element-interpret-data'.
4419 ;; The function relies internally on
4420 ;; `org-element--interpret-affiliated-keywords'.
4422 ;;;###autoload
4423 (defun org-element-interpret-data (data &optional pseudo-objects)
4424 "Interpret DATA as Org syntax.
4426 DATA is a parse tree, an element, an object or a secondary string
4427 to interpret.
4429 Optional argument PSEUDO-OBJECTS is a list of symbols defining
4430 new types that should be treated as objects. An unknown type not
4431 belonging to this list is seen as a pseudo-element instead. Both
4432 pseudo-objects and pseudo-elements are transparent entities, i.e.
4433 only their contents are interpreted.
4435 Return Org syntax as a string."
4436 (org-element--interpret-data-1 data nil pseudo-objects))
4438 (defun org-element--interpret-data-1 (data parent pseudo-objects)
4439 "Interpret DATA as Org syntax.
4441 DATA is a parse tree, an element, an object or a secondary string
4442 to interpret. PARENT is used for recursive calls. It contains
4443 the element or object containing data, or nil. PSEUDO-OBJECTS
4444 are list of symbols defining new element or object types.
4445 Unknown types that don't belong to this list are treated as
4446 pseudo-elements instead.
4448 Return Org syntax as a string."
4449 (let* ((type (org-element-type data))
4450 ;; Find interpreter for current object or element. If it
4451 ;; doesn't exist (e.g. this is a pseudo object or element),
4452 ;; return contents, if any.
4453 (interpret
4454 (let ((fun (intern (format "org-element-%s-interpreter" type))))
4455 (if (fboundp fun) fun (lambda (data contents) contents))))
4456 (results
4457 (cond
4458 ;; Secondary string.
4459 ((not type)
4460 (mapconcat
4461 (lambda (obj)
4462 (org-element--interpret-data-1 obj parent pseudo-objects))
4463 data ""))
4464 ;; Full Org document.
4465 ((eq type 'org-data)
4466 (mapconcat
4467 (lambda (obj)
4468 (org-element--interpret-data-1 obj parent pseudo-objects))
4469 (org-element-contents data) ""))
4470 ;; Plain text: remove `:parent' text property from output.
4471 ((stringp data) (org-no-properties data))
4472 ;; Element or object without contents.
4473 ((not (org-element-contents data)) (funcall interpret data nil))
4474 ;; Element or object with contents.
4476 (funcall interpret data
4477 ;; Recursively interpret contents.
4478 (mapconcat
4479 (lambda (obj)
4480 (org-element--interpret-data-1 obj data pseudo-objects))
4481 (org-element-contents
4482 (if (not (memq type '(paragraph verse-block)))
4483 data
4484 ;; Fix indentation of elements containing
4485 ;; objects. We ignore `table-row' elements
4486 ;; as they are one line long anyway.
4487 (org-element-normalize-contents
4488 data
4489 ;; When normalizing first paragraph of an
4490 ;; item or a footnote-definition, ignore
4491 ;; first line's indentation.
4492 (and (eq type 'paragraph)
4493 (equal data (car (org-element-contents parent)))
4494 (memq (org-element-type parent)
4495 '(footnote-definition item))))))
4496 ""))))))
4497 (if (memq type '(org-data plain-text nil)) results
4498 ;; Build white spaces. If no `:post-blank' property is
4499 ;; specified, assume its value is 0.
4500 (let ((post-blank (or (org-element-property :post-blank data) 0)))
4501 (if (or (memq type org-element-all-objects)
4502 (memq type pseudo-objects))
4503 (concat results (make-string post-blank ?\s))
4504 (concat
4505 (org-element--interpret-affiliated-keywords data)
4506 (org-element-normalize-string results)
4507 (make-string post-blank ?\n)))))))
4509 (defun org-element--interpret-affiliated-keywords (element)
4510 "Return ELEMENT's affiliated keywords as Org syntax.
4511 If there is no affiliated keyword, return the empty string."
4512 (let ((keyword-to-org
4513 (function
4514 (lambda (key value)
4515 (let (dual)
4516 (when (member key org-element-dual-keywords)
4517 (setq dual (cdr value) value (car value)))
4518 (concat "#+" key
4519 (and dual
4520 (format "[%s]" (org-element-interpret-data dual)))
4521 ": "
4522 (if (member key org-element-parsed-keywords)
4523 (org-element-interpret-data value)
4524 value)
4525 "\n"))))))
4526 (mapconcat
4527 (lambda (prop)
4528 (let ((value (org-element-property prop element))
4529 (keyword (upcase (substring (symbol-name prop) 1))))
4530 (when value
4531 (if (or (member keyword org-element-multiple-keywords)
4532 ;; All attribute keywords can have multiple lines.
4533 (string-match "^ATTR_" keyword))
4534 (mapconcat (lambda (line) (funcall keyword-to-org keyword line))
4535 (reverse value)
4537 (funcall keyword-to-org keyword value)))))
4538 ;; List all ELEMENT's properties matching an attribute line or an
4539 ;; affiliated keyword, but ignore translated keywords since they
4540 ;; cannot belong to the property list.
4541 (loop for prop in (nth 1 element) by 'cddr
4542 when (let ((keyword (upcase (substring (symbol-name prop) 1))))
4543 (or (string-match "^ATTR_" keyword)
4544 (and
4545 (member keyword org-element-affiliated-keywords)
4546 (not (assoc keyword
4547 org-element-keyword-translation-alist)))))
4548 collect prop)
4549 "")))
4551 ;; Because interpretation of the parse tree must return the same
4552 ;; number of blank lines between elements and the same number of white
4553 ;; space after objects, some special care must be given to white
4554 ;; spaces.
4556 ;; The first function, `org-element-normalize-string', ensures any
4557 ;; string different from the empty string will end with a single
4558 ;; newline character.
4560 ;; The second function, `org-element-normalize-contents', removes
4561 ;; global indentation from the contents of the current element.
4563 (defun org-element-normalize-string (s)
4564 "Ensure string S ends with a single newline character.
4566 If S isn't a string return it unchanged. If S is the empty
4567 string, return it. Otherwise, return a new string with a single
4568 newline character at its end."
4569 (cond
4570 ((not (stringp s)) s)
4571 ((string= "" s) "")
4572 (t (and (string-match "\\(\n[ \t]*\\)*\\'" s)
4573 (replace-match "\n" nil nil s)))))
4575 (defun org-element-normalize-contents (element &optional ignore-first)
4576 "Normalize plain text in ELEMENT's contents.
4578 ELEMENT must only contain plain text and objects.
4580 If optional argument IGNORE-FIRST is non-nil, ignore first line's
4581 indentation to compute maximal common indentation.
4583 Return the normalized element that is element with global
4584 indentation removed from its contents. The function assumes that
4585 indentation is not done with TAB characters."
4586 (let* (ind-list ; for byte-compiler
4587 collect-inds ; for byte-compiler
4588 (collect-inds
4589 (function
4590 ;; Return list of indentations within BLOB. This is done by
4591 ;; walking recursively BLOB and updating IND-LIST along the
4592 ;; way. FIRST-FLAG is non-nil when the first string hasn't
4593 ;; been seen yet. It is required as this string is the only
4594 ;; one whose indentation doesn't happen after a newline
4595 ;; character.
4596 (lambda (blob first-flag)
4597 (mapc
4598 (lambda (object)
4599 (when (and first-flag (stringp object))
4600 (setq first-flag nil)
4601 (string-match "\\`\\( *\\)" object)
4602 (let ((len (length (match-string 1 object))))
4603 ;; An indentation of zero means no string will be
4604 ;; modified. Quit the process.
4605 (if (zerop len) (throw 'zero (setq ind-list nil))
4606 (push len ind-list))))
4607 (cond
4608 ((stringp object)
4609 (let ((start 0))
4610 ;; Avoid matching blank or empty lines.
4611 (while (and (string-match "\n\\( *\\)\\(.\\)" object start)
4612 (not (equal (match-string 2 object) " ")))
4613 (setq start (match-end 0))
4614 (push (length (match-string 1 object)) ind-list))))
4615 ((memq (org-element-type object) org-element-recursive-objects)
4616 (funcall collect-inds object first-flag))))
4617 (org-element-contents blob))))))
4618 ;; Collect indentation list in ELEMENT. Possibly remove first
4619 ;; value if IGNORE-FIRST is non-nil.
4620 (catch 'zero (funcall collect-inds element (not ignore-first)))
4621 (if (not ind-list) element
4622 ;; Build ELEMENT back, replacing each string with the same
4623 ;; string minus common indentation.
4624 (let* (build ; For byte compiler.
4625 (build
4626 (function
4627 (lambda (blob mci first-flag)
4628 ;; Return BLOB with all its strings indentation
4629 ;; shortened from MCI white spaces. FIRST-FLAG is
4630 ;; non-nil when the first string hasn't been seen
4631 ;; yet.
4632 (setcdr (cdr blob)
4633 (mapcar
4634 (lambda (object)
4635 (when (and first-flag (stringp object))
4636 (setq first-flag nil)
4637 (setq object
4638 (replace-regexp-in-string
4639 (format "\\` \\{%d\\}" mci) "" object)))
4640 (cond
4641 ((stringp object)
4642 (replace-regexp-in-string
4643 (format "\n \\{%d\\}" mci) "\n" object))
4644 ((memq (org-element-type object)
4645 org-element-recursive-objects)
4646 (funcall build object mci first-flag))
4647 (t object)))
4648 (org-element-contents blob)))
4649 blob))))
4650 (funcall build element (apply 'min ind-list) (not ignore-first))))))
4654 ;;; The Toolbox
4656 ;; The first move is to implement a way to obtain the smallest element
4657 ;; containing point. This is the job of `org-element-at-point'. It
4658 ;; basically jumps back to the beginning of section containing point
4659 ;; and proceed, one element after the other, with
4660 ;; `org-element--current-element' until the container is found. Note:
4661 ;; When using `org-element-at-point', secondary values are never
4662 ;; parsed since the function focuses on elements, not on objects.
4664 ;; At a deeper level, `org-element-context' lists all elements and
4665 ;; objects containing point.
4667 ;; `org-element-nested-p' and `org-element-swap-A-B' may be used
4668 ;; internally by navigation and manipulation tools.
4671 ;;;###autoload
4672 (defun org-element-at-point ()
4673 "Determine closest element around point.
4675 Return value is a list like (TYPE PROPS) where TYPE is the type
4676 of the element and PROPS a plist of properties associated to the
4677 element.
4679 Possible types are defined in `org-element-all-elements'.
4680 Properties depend on element or object type, but always include
4681 `:begin', `:end', `:parent' and `:post-blank' properties.
4683 As a special case, if point is at the very beginning of the first
4684 item in a list or sub-list, returned element will be that list
4685 instead of the item. Likewise, if point is at the beginning of
4686 the first row of a table, returned element will be the table
4687 instead of the first row.
4689 When point is at the end of the buffer, return the innermost
4690 element ending there."
4691 (catch 'exit
4692 (org-with-wide-buffer
4693 (let ((origin (point)) element next)
4694 (end-of-line)
4695 (skip-chars-backward " \r\t\n")
4696 (cond
4697 ;; Within blank lines at the beginning of buffer, return nil.
4698 ((bobp) (throw 'exit nil))
4699 ;; Within blank lines right after a headline, return that
4700 ;; headline.
4701 ((org-with-limited-levels (org-at-heading-p))
4702 (beginning-of-line)
4703 (throw 'exit (org-element-headline-parser (point-max) t))))
4704 ;; Otherwise use cache in order to approximate current element.
4705 (goto-char origin)
4706 (let* ((cached (org-element-cache-get origin))
4707 (begin (org-element-property :begin cached)))
4708 (cond
4709 ;; Nothing in cache before point: start parsing from first
4710 ;; element following headline above, or first element in
4711 ;; buffer.
4712 ((not cached)
4713 (org-with-limited-levels (outline-previous-heading)
4714 (when (org-at-heading-p) (forward-line)))
4715 (skip-chars-forward " \r\t\n")
4716 (beginning-of-line))
4717 ;; Cache returned exact match: return it.
4718 ((= origin begin) (throw 'exit cached))
4719 ;; There's a headline between cached value and ORIGIN:
4720 ;; cached value is invalid. Start parsing from first
4721 ;; element following the headline.
4722 ((re-search-backward
4723 (org-with-limited-levels org-outline-regexp-bol) begin t)
4724 (forward-line)
4725 (skip-chars-forward " \r\t\n")
4726 (beginning-of-line))
4727 ;; Check if CACHED or any of its ancestors contain point.
4729 ;; If there is such an element, we inspect it in order to
4730 ;; know if we return it or if we need to parse its contents.
4731 ;; Otherwise, we just start parsing from current location,
4732 ;; which is right after the top-most element containing
4733 ;; CACHED.
4735 ;; As a special case, if ORIGIN is at the end of the buffer,
4736 ;; we want to return the innermost element ending there.
4738 ;; Also, if we find an ancestor and discover that we need to
4739 ;; parse its contents, make sure we don't start from
4740 ;; `:contents-begin', as we would otherwise go past CACHED
4741 ;; again. Instead, in that situation, we will resume
4742 ;; parsing from NEXT, which is located after CACHED or its
4743 ;; higher ancestor not containing point.
4745 (let ((up cached)
4746 (origin (if (= (point-max) origin) (1- origin) origin)))
4747 (goto-char (or (org-element-property :contents-begin cached)
4748 begin))
4749 (while (let ((end (org-element-property :end up)))
4750 (and (<= end origin)
4751 (goto-char end)
4752 (setq up (org-element-property :parent up)))))
4753 (cond ((not up))
4754 ((eobp) (setq element up))
4755 (t (setq element up next (point))))))))
4756 ;; Parse successively each element until we reach ORIGIN.
4757 (let ((end (or (org-element-property :end element)
4758 (save-excursion
4759 (org-with-limited-levels (outline-next-heading))
4760 (point))))
4761 parent special-flag)
4762 (while t
4763 (unless element
4764 (let ((e (org-element--current-element
4765 end 'element special-flag
4766 (org-element-property :structure parent))))
4767 (org-element-put-property e :parent parent)
4768 (setq element (org-element-cache-put e))))
4769 (let ((elem-end (org-element-property :end element))
4770 (type (org-element-type element)))
4771 (cond
4772 ;; Special case: ORIGIN is at the end of the buffer and
4773 ;; CACHED ends here. No element can start after it, but
4774 ;; more than one may end there. Arbitrarily, we choose
4775 ;; to return the innermost of such elements.
4776 ((and (= (point-max) origin) (= origin elem-end))
4777 (let ((cend (org-element-property :contents-end element)))
4778 (if (or (not (memq type org-element-greater-elements))
4779 (not cend)
4780 (< cend origin))
4781 (throw 'exit element)
4782 (goto-char
4783 (or next (org-element-property :contents-begin element)))
4784 (setq special-flag (case type
4785 (plain-list 'item)
4786 (property-drawer 'node-property)
4787 (table 'table-row))
4788 parent element
4789 end cend))))
4790 ;; Skip any element ending before point. Also skip
4791 ;; element ending at point since we're sure that another
4792 ;; element begins after it.
4793 ((<= elem-end origin) (goto-char elem-end))
4794 ;; A non-greater element contains point: return it.
4795 ((not (memq type org-element-greater-elements))
4796 (throw 'exit element))
4797 ;; Otherwise, we have to decide if ELEMENT really
4798 ;; contains ORIGIN. In that case we start parsing from
4799 ;; contents' beginning. Otherwise we return UP as it is
4800 ;; the smallest element containing ORIGIN.
4802 ;; There is a special cases to consider, though. If
4803 ;; ORIGIN is at contents' beginning but it is also at
4804 ;; the beginning of the first item in a list or a table.
4805 ;; In that case, we need to create an anchor for that
4806 ;; list or table, so return it.
4808 (let ((cbeg (org-element-property :contents-begin element))
4809 (cend (org-element-property :contents-end element)))
4810 (if (or (not cbeg) (not cend) (> cbeg origin) (<= cend origin)
4811 (and (= cbeg origin) (memq type '(plain-list table))))
4812 (throw 'exit element)
4813 (goto-char (or next cbeg))
4814 (setq special-flag (case type
4815 (plain-list 'item)
4816 (property-drawer 'node-property)
4817 (table 'table-row))
4818 parent element
4819 end cend))))))
4820 ;; Continue parsing buffer contents from new position.
4821 (setq element nil next nil)))))))
4823 ;;;###autoload
4824 (defun org-element-context (&optional element)
4825 "Return closest element or object around point.
4827 Return value is a list like (TYPE PROPS) where TYPE is the type
4828 of the element or object and PROPS a plist of properties
4829 associated to it.
4831 Possible types are defined in `org-element-all-elements' and
4832 `org-element-all-objects'. Properties depend on element or
4833 object type, but always include `:begin', `:end', `:parent' and
4834 `:post-blank'.
4836 Optional argument ELEMENT, when non-nil, is the closest element
4837 containing point, as returned by `org-element-at-point'.
4838 Providing it allows for quicker computation."
4839 (catch 'objects-forbidden
4840 (org-with-wide-buffer
4841 (let* ((origin (point))
4842 (element (or element (org-element-at-point)))
4843 (type (org-element-type element)))
4844 ;; If point is inside an element containing objects or
4845 ;; a secondary string, narrow buffer to the container and
4846 ;; proceed with parsing. Otherwise, return ELEMENT.
4847 (cond
4848 ;; At a parsed affiliated keyword, check if we're inside main
4849 ;; or dual value.
4850 ((let ((post (org-element-property :post-affiliated element)))
4851 (and post (< origin post)))
4852 (beginning-of-line)
4853 (let ((case-fold-search t)) (looking-at org-element--affiliated-re))
4854 (cond
4855 ((not (member-ignore-case (match-string 1)
4856 org-element-parsed-keywords))
4857 (throw 'objects-forbidden element))
4858 ((< (match-end 0) origin)
4859 (narrow-to-region (match-end 0) (line-end-position)))
4860 ((and (match-beginning 2)
4861 (>= origin (match-beginning 2))
4862 (< origin (match-end 2)))
4863 (narrow-to-region (match-beginning 2) (match-end 2)))
4864 (t (throw 'objects-forbidden element)))
4865 ;; Also change type to retrieve correct restrictions.
4866 (setq type 'keyword))
4867 ;; At an item, objects can only be located within tag, if any.
4868 ((eq type 'item)
4869 (let ((tag (org-element-property :tag element)))
4870 (if (not tag) (throw 'objects-forbidden element)
4871 (beginning-of-line)
4872 (search-forward tag (line-end-position))
4873 (goto-char (match-beginning 0))
4874 (if (and (>= origin (point)) (< origin (match-end 0)))
4875 (narrow-to-region (point) (match-end 0))
4876 (throw 'objects-forbidden element)))))
4877 ;; At an headline or inlinetask, objects are in title.
4878 ((memq type '(headline inlinetask))
4879 (goto-char (org-element-property :begin element))
4880 (skip-chars-forward "* ")
4881 (if (and (>= origin (point)) (< origin (line-end-position)))
4882 (narrow-to-region (point) (line-end-position))
4883 (throw 'objects-forbidden element)))
4884 ;; At a paragraph, a table-row or a verse block, objects are
4885 ;; located within their contents.
4886 ((memq type '(paragraph table-row verse-block))
4887 (let ((cbeg (org-element-property :contents-begin element))
4888 (cend (org-element-property :contents-end element)))
4889 ;; CBEG is nil for table rules.
4890 (if (and cbeg cend (>= origin cbeg) (< origin cend))
4891 (narrow-to-region cbeg cend)
4892 (throw 'objects-forbidden element))))
4893 ;; At a parsed keyword, objects are located within value.
4894 ((eq type 'keyword)
4895 (if (not (member (org-element-property :key element)
4896 org-element-document-properties))
4897 (throw 'objects-forbidden element)
4898 (beginning-of-line)
4899 (search-forward ":")
4900 (if (and (>= origin (point)) (< origin (line-end-position)))
4901 (narrow-to-region (point) (line-end-position))
4902 (throw 'objects-forbidden element))))
4903 ;; All other locations cannot contain objects: bail out.
4904 (t (throw 'objects-forbidden element)))
4905 (goto-char (point-min))
4906 (let* ((restriction (org-element-restriction type))
4907 (parent element)
4908 (candidates 'initial)
4909 (cache (org-element-cache-get element))
4910 objects-data next update-cache-flag)
4911 (prog1
4912 (catch 'exit
4913 (while t
4914 ;; Get list of next object candidates in CANDIDATES.
4915 ;; When entering for the first time PARENT, grab it
4916 ;; from cache, if available, or compute it. Then,
4917 ;; for each subsequent iteration in PARENT, always
4918 ;; compute it since we're beyond cache anyway.
4919 (unless next
4920 (let ((data (assq (point) cache)))
4921 (if data (setq candidates (nth 1 (setq objects-data data)))
4922 (push (setq objects-data (list (point) 'initial))
4923 cache))))
4924 (when (or next (eq 'initial candidates))
4925 (setq candidates
4926 (org-element--get-next-object-candidates
4927 restriction candidates))
4928 (setcar (cdr objects-data) candidates))
4929 ;; Compare ORIGIN with next object starting position,
4930 ;; if any.
4932 ;; If ORIGIN is lesser or if there is no object
4933 ;; following, look for a previous object that might
4934 ;; contain it in cache. If there is no cache, we
4935 ;; didn't miss any object so simply return PARENT.
4937 ;; If ORIGIN is greater or equal, parse next
4938 ;; candidate for further processing.
4939 (let ((closest
4940 (and candidates
4941 (rassq (apply #'min (mapcar #'cdr candidates))
4942 candidates))))
4943 (if (or (not closest) (> (cdr closest) origin))
4944 (catch 'found
4945 (dolist (obj (cddr objects-data) (throw 'exit parent))
4946 (when (<= (org-element-property :begin obj) origin)
4947 (if (<= (org-element-property :end obj) origin)
4948 ;; Object ends before ORIGIN and we
4949 ;; know next one in cache starts
4950 ;; after it: bail out.
4951 (throw 'exit parent)
4952 (throw 'found (setq next obj))))))
4953 (goto-char (cdr closest))
4954 (setq next
4955 (funcall (intern (format "org-element-%s-parser"
4956 (car closest)))))
4957 (push next (cddr objects-data))))
4958 ;; Process NEXT to know if we need to skip it, return
4959 ;; it or move into it.
4960 (let ((cbeg (org-element-property :contents-begin next))
4961 (cend (org-element-property :contents-end next))
4962 (obj-end (org-element-property :end next)))
4963 (cond
4964 ;; ORIGIN is after NEXT, so skip it.
4965 ((<= obj-end origin) (goto-char obj-end))
4966 ;; ORIGIN is within a non-recursive next or
4967 ;; at an object boundaries: Return that object.
4968 ((or (not cbeg) (< origin cbeg) (>= origin cend))
4969 (throw 'exit
4970 (org-element-put-property next :parent parent)))
4971 ;; Otherwise, move into NEXT and reset flags as we
4972 ;; shift parent.
4973 (t (goto-char cbeg)
4974 (narrow-to-region (point) cend)
4975 (org-element-put-property next :parent parent)
4976 (setq parent next
4977 restriction (org-element-restriction next)
4978 next nil
4979 objects-data nil
4980 candidates 'initial))))))
4981 ;; Store results in cache, if applicable.
4982 (org-element-cache-put cache element)))))))
4984 (defun org-element-nested-p (elem-A elem-B)
4985 "Non-nil when elements ELEM-A and ELEM-B are nested."
4986 (let ((beg-A (org-element-property :begin elem-A))
4987 (beg-B (org-element-property :begin elem-B))
4988 (end-A (org-element-property :end elem-A))
4989 (end-B (org-element-property :end elem-B)))
4990 (or (and (>= beg-A beg-B) (<= end-A end-B))
4991 (and (>= beg-B beg-A) (<= end-B end-A)))))
4993 (defun org-element-swap-A-B (elem-A elem-B)
4994 "Swap elements ELEM-A and ELEM-B.
4995 Assume ELEM-B is after ELEM-A in the buffer. Leave point at the
4996 end of ELEM-A."
4997 (goto-char (org-element-property :begin elem-A))
4998 ;; There are two special cases when an element doesn't start at bol:
4999 ;; the first paragraph in an item or in a footnote definition.
5000 (let ((specialp (not (bolp))))
5001 ;; Only a paragraph without any affiliated keyword can be moved at
5002 ;; ELEM-A position in such a situation. Note that the case of
5003 ;; a footnote definition is impossible: it cannot contain two
5004 ;; paragraphs in a row because it cannot contain a blank line.
5005 (if (and specialp
5006 (or (not (eq (org-element-type elem-B) 'paragraph))
5007 (/= (org-element-property :begin elem-B)
5008 (org-element-property :contents-begin elem-B))))
5009 (error "Cannot swap elements"))
5010 ;; In a special situation, ELEM-A will have no indentation. We'll
5011 ;; give it ELEM-B's (which will in, in turn, have no indentation).
5012 (let* ((ind-B (when specialp
5013 (goto-char (org-element-property :begin elem-B))
5014 (org-get-indentation)))
5015 (beg-A (org-element-property :begin elem-A))
5016 (end-A (save-excursion
5017 (goto-char (org-element-property :end elem-A))
5018 (skip-chars-backward " \r\t\n")
5019 (point-at-eol)))
5020 (beg-B (org-element-property :begin elem-B))
5021 (end-B (save-excursion
5022 (goto-char (org-element-property :end elem-B))
5023 (skip-chars-backward " \r\t\n")
5024 (point-at-eol)))
5025 ;; Store overlays responsible for visibility status. We
5026 ;; also need to store their boundaries as they will be
5027 ;; removed from buffer.
5028 (overlays
5029 (cons
5030 (mapcar (lambda (ov) (list ov (overlay-start ov) (overlay-end ov)))
5031 (overlays-in beg-A end-A))
5032 (mapcar (lambda (ov) (list ov (overlay-start ov) (overlay-end ov)))
5033 (overlays-in beg-B end-B))))
5034 ;; Get contents.
5035 (body-A (buffer-substring beg-A end-A))
5036 (body-B (delete-and-extract-region beg-B end-B)))
5037 (goto-char beg-B)
5038 (when specialp
5039 (setq body-B (replace-regexp-in-string "\\`[ \t]*" "" body-B))
5040 (org-indent-to-column ind-B))
5041 (insert body-A)
5042 ;; Restore ex ELEM-A overlays.
5043 (let ((offset (- beg-B beg-A)))
5044 (mapc (lambda (ov)
5045 (move-overlay
5046 (car ov) (+ (nth 1 ov) offset) (+ (nth 2 ov) offset)))
5047 (car overlays))
5048 (goto-char beg-A)
5049 (delete-region beg-A end-A)
5050 (insert body-B)
5051 ;; Restore ex ELEM-B overlays.
5052 (mapc (lambda (ov)
5053 (move-overlay
5054 (car ov) (- (nth 1 ov) offset) (- (nth 2 ov) offset)))
5055 (cdr overlays)))
5056 (goto-char (org-element-property :end elem-B)))))
5058 (defun org-element-remove-indentation (s &optional n)
5059 "Remove maximum common indentation in string S and return it.
5060 When optional argument N is a positive integer, remove exactly
5061 that much characters from indentation, if possible, or return
5062 S as-is otherwise. Unlike to `org-remove-indentation', this
5063 function doesn't call `untabify' on S."
5064 (catch 'exit
5065 (with-temp-buffer
5066 (insert s)
5067 (goto-char (point-min))
5068 ;; Find maximum common indentation, if not specified.
5069 (setq n (or n
5070 (let ((min-ind (point-max)))
5071 (save-excursion
5072 (while (re-search-forward "^[ \t]*\\S-" nil t)
5073 (let ((ind (1- (current-column))))
5074 (if (zerop ind) (throw 'exit s)
5075 (setq min-ind (min min-ind ind))))))
5076 min-ind)))
5077 (if (zerop n) s
5078 ;; Remove exactly N indentation, but give up if not possible.
5079 (while (not (eobp))
5080 (let ((ind (progn (skip-chars-forward " \t") (current-column))))
5081 (cond ((eolp) (delete-region (line-beginning-position) (point)))
5082 ((< ind n) (throw 'exit s))
5083 (t (org-indent-line-to (- ind n))))
5084 (forward-line)))
5085 (buffer-string)))))
5089 ;;; Cache
5091 ;; Both functions `org-element-at-point' and `org-element-context'
5092 ;; benefit from a simple caching mechanism.
5094 ;; Three public functions are provided: `org-element-cache-put',
5095 ;; `org-element-cache-get' and `org-element-cache-reset'.
5097 ;; Cache is enabled by default, but can be disabled globally with
5098 ;; `org-element-use-cache'. `org-element-cache-sync-idle-time' and
5099 ;; `org-element-cache-merge-changes-threshold' can be tweaked to
5100 ;; control caching behaviour.
5103 (defvar org-element-use-cache t
5104 "Non nil when Org parser should cache its results.
5105 This is mostly for debugging purpose.")
5107 (defvar org-element-cache-merge-changes-threshold 200
5108 "Number of characters triggering cache syncing.
5110 The cache mechanism only stores one buffer modification at any
5111 given time. When another change happens, it replaces it with
5112 a change containing both the stored modification and the current
5113 one. This is a trade-off, as merging them prevents another
5114 syncing, but every element between them is then lost.
5116 This variable determines the maximum size, in characters, we
5117 accept to lose in order to avoid syncing the cache.")
5119 (defvar org-element-cache-sync-idle-time 0.5
5120 "Number of seconds of idle time wait before syncing buffer cache.
5121 Syncing also happens when current modification is too distant
5122 from the stored one (for more information, see
5123 `org-element-cache-merge-changes-threshold').")
5126 ;;;; Data Structure
5128 (defvar org-element--cache nil
5129 "AVL tree used to cache elements.
5130 Each node of the tree contains an element. Comparison is done
5131 with `org-element--cache-compare'. This cache is used in
5132 `org-element-at-point'.")
5134 (defvar org-element--cache-objects nil
5135 "Hash table used as to cache objects.
5136 Key is an element, as returned by `org-element-at-point', and
5137 value is an alist where each association is:
5139 \(POS CANDIDATES . OBJECTS)
5141 where POS is a buffer position, CANDIDATES is the last know list
5142 of successors (see `org-element--get-next-object-candidates') in
5143 container starting at POS and OBJECTS is a list of objects known
5144 to live within that container, from farthest to closest.
5146 In the following example, \\alpha, bold object and \\beta start
5147 at, respectively, positions 1, 7 and 8,
5149 \\alpha *\\beta*
5151 If the paragraph is completely parsed, OBJECTS-DATA will be
5153 \((1 nil BOLD-OBJECT ENTITY-OBJECT)
5154 \(8 nil ENTITY-OBJECT))
5156 whereas in a partially parsed paragraph, it could be
5158 \((1 ((entity . 1) (bold . 7)) ENTITY-OBJECT))
5160 This cache is used in `org-element-context'.")
5162 (defun org-element--cache-compare (a b)
5163 "Non-nil when element A is located before element B."
5164 (let ((beg-a (org-element-property :begin a))
5165 (beg-b (org-element-property :begin b)))
5166 (or (< beg-a beg-b)
5167 ;; Items and plain lists on the one hand, table rows and
5168 ;; tables on the other hand can start at the same position.
5169 ;; In this case, the parent element is always before its child
5170 ;; in the buffer.
5171 (and (= beg-a beg-b)
5172 (memq (org-element-type a) '(plain-list table))
5173 (memq (org-element-type b) '(item table-row))))))
5175 (defsubst org-element--cache-root ()
5176 "Return root value in cache.
5177 This function assumes `org-element--cache' is a valid AVL tree."
5178 (avl-tree--node-left (avl-tree--dummyroot org-element--cache)))
5181 ;;;; Staging Buffer Changes
5183 (defvar org-element--cache-status nil
5184 "Contains data about cache validity for current buffer.
5186 Value is a vector of seven elements,
5188 [ACTIVEP BEGIN END OFFSET TIMER PREVIOUS-STATE]
5190 ACTIVEP is a boolean non-nil when changes described in the other
5191 slots are valid for current buffer.
5193 BEGIN and END are the beginning and ending position of the area
5194 for which cache cannot be trusted.
5196 OFFSET it an integer specifying the number to add to position of
5197 elements after that area.
5199 TIMER is a timer used to apply these changes to cache when Emacs
5200 is idle.
5202 PREVIOUS-STATE is a symbol referring to the state of the buffer
5203 before a change happens. It is used to know if sensitive
5204 areas (block boundaries, headlines) were modified. It can be set
5205 to nil, `headline' or `other'.")
5207 (defconst org-element--cache-opening-line
5208 (concat "^[ \t]*\\(?:"
5209 "#\\+BEGIN[:_]" "\\|"
5210 "\\\\begin{[A-Za-z0-9]+\\*?}" "\\|"
5211 ":\\S-+:[ \t]*$"
5212 "\\)")
5213 "Regexp matching an element opening line.
5214 When such a line is modified, modifications may propagate after
5215 modified area. In that situation, every element between that
5216 area and next section is removed from cache.")
5218 (defconst org-element--cache-closing-line
5219 (concat "^[ \t]*\\(?:"
5220 "#\\+END\\(?:_\\|:?[ \t]*$\\)" "\\|"
5221 "\\\\end{[A-Za-z0-9]+\\*?}[ \t]*$" "\\|"
5222 ":END:[ \t]*$"
5223 "\\)")
5224 "Regexp matching an element closing line.
5225 When such a line is modified, modifications may propagate before
5226 modified area. In that situation, every element between that
5227 area and previous section is removed from cache.")
5229 (defsubst org-element--cache-pending-changes-p ()
5230 "Non-nil when changes are not integrated in cache yet."
5231 (and org-element--cache-status
5232 (aref org-element--cache-status 0)))
5234 (defsubst org-element--cache-push-change (beg end offset)
5235 "Push change to current buffer staging area.
5236 BEG and END and the beginning and ending position of the
5237 modification area. OFFSET is the size of the change, as an
5238 integer."
5239 (aset org-element--cache-status 1 beg)
5240 (aset org-element--cache-status 2 end)
5241 (aset org-element--cache-status 3 offset)
5242 (let ((timer (aref org-element--cache-status 4)))
5243 (if timer (timer-activate-when-idle timer t)
5244 (aset org-element--cache-status 4
5245 (run-with-idle-timer org-element-cache-sync-idle-time
5247 #'org-element--cache-sync
5248 (current-buffer)))))
5249 (aset org-element--cache-status 0 t))
5251 (defsubst org-element--cache-cancel-changes ()
5252 "Remove any cache change set for current buffer."
5253 (let ((timer (aref org-element--cache-status 4)))
5254 (and timer (cancel-timer timer)))
5255 (aset org-element--cache-status 0 nil))
5257 (defun org-element--cache-before-change (beg end)
5258 "Request extension of area going to be modified if needed.
5259 BEG and END are the beginning and end of the range of changed
5260 text. See `before-change-functions' for more information."
5261 (let ((inhibit-quit t))
5262 (org-with-wide-buffer
5263 (goto-char beg)
5264 (beginning-of-line)
5265 (let ((top (point))
5266 (bottom (save-excursion (goto-char end) (line-end-position)))
5267 (sensitive-re
5268 ;; A sensitive line is a headline or a block (or drawer,
5269 ;; or latex-environment) boundary. Inserting one can
5270 ;; modify buffer drastically both above and below that
5271 ;; line, possibly making cache invalid. Therefore, we
5272 ;; need to pay special attention to changes happening to
5273 ;; them.
5274 (concat
5275 "\\(" (org-with-limited-levels org-outline-regexp-bol) "\\)" "\\|"
5276 org-element--cache-closing-line "\\|"
5277 org-element--cache-opening-line)))
5278 (save-match-data
5279 (aset org-element--cache-status 5
5280 (cond ((not (re-search-forward sensitive-re bottom t)) nil)
5281 ((and (match-beginning 1)
5282 (progn (goto-char bottom)
5283 (or (not (re-search-backward sensitive-re
5284 (match-end 1) t))
5285 (match-beginning 1))))
5286 'headline)
5287 (t 'other))))))))
5289 (defun org-element--cache-record-change (beg end pre)
5290 "Update buffer modifications for current buffer.
5292 BEG and END are the beginning and end of the range of changed
5293 text, and the length in bytes of the pre-change text replaced by
5294 that range. See `after-change-functions' for more information.
5296 If there are already pending changes, try to merge them into
5297 a bigger change record. If that's not possible, the function
5298 will first synchronize cache with previous change and store the
5299 new one."
5300 (let ((inhibit-quit t))
5301 (when (and org-element-use-cache org-element--cache)
5302 (org-with-wide-buffer
5303 (goto-char beg)
5304 (beginning-of-line)
5305 (let ((top (point))
5306 (bottom (save-excursion (goto-char end) (line-end-position))))
5307 (org-with-limited-levels
5308 (save-match-data
5309 ;; Determine if modified area needs to be extended,
5310 ;; according to both previous and current state. We make
5311 ;; a special case for headline editing: if a headline is
5312 ;; modified but not removed, do not extend.
5313 (when (let ((previous-state (aref org-element--cache-status 5))
5314 (sensitive-re
5315 (concat "\\(" org-outline-regexp-bol "\\)" "\\|"
5316 org-element--cache-closing-line "\\|"
5317 org-element--cache-opening-line)))
5318 (cond ((eq previous-state 'other))
5319 ((not (re-search-forward sensitive-re bottom t))
5320 (eq previous-state 'headline))
5321 ((match-beginning 1)
5322 (or (not (eq previous-state 'headline))
5323 (and (progn (goto-char bottom)
5324 (re-search-backward
5325 sensitive-re (match-end 1) t))
5326 (not (match-beginning 1)))))
5327 (t)))
5328 ;; Effectively extend modified area.
5329 (setq top (progn (goto-char top)
5330 (outline-previous-heading)
5331 ;; Headline above is inclusive.
5332 (point)))
5333 (setq bottom (progn (goto-char bottom)
5334 (outline-next-heading)
5335 ;; Headline below is exclusive.
5336 (if (eobp) (point) (1- (point))))))))
5337 ;; Store changes.
5338 (let ((offset (- end beg pre)))
5339 (if (not (org-element--cache-pending-changes-p))
5340 ;; No pending changes. Store the new ones.
5341 (org-element--cache-push-change top (- bottom offset) offset)
5342 (let* ((current-start (aref org-element--cache-status 1))
5343 (current-end (+ (aref org-element--cache-status 2)
5344 (aref org-element--cache-status 3)))
5345 (gap (max (- beg current-end) (- current-start end))))
5346 (if (> gap org-element-cache-merge-changes-threshold)
5347 ;; If we cannot merge two change sets (i.e. they
5348 ;; modify distinct buffer parts) first apply current
5349 ;; change set and store new one. This way, there is
5350 ;; never more than one pending change set, which
5351 ;; avoids handling costly merges.
5352 (progn (org-element--cache-sync (current-buffer))
5353 (org-element--cache-push-change
5354 top (- bottom offset) offset))
5355 ;; Change sets can be merged. We can expand the area
5356 ;; that requires an update, and postpone the sync.
5357 (timer-activate-when-idle (aref org-element--cache-status 4) t)
5358 (aset org-element--cache-status 0 t)
5359 (aset org-element--cache-status 1 (min top current-start))
5360 (aset org-element--cache-status 2
5361 (- (max current-end bottom) offset))
5362 (incf (aref org-element--cache-status 3) offset))))))))))
5365 ;;;; Synchronization
5367 (defsubst org-element--cache-shift-positions (element offset &optional props)
5368 "Shift ELEMENT properties relative to buffer positions by OFFSET.
5370 Properties containing buffer positions are `:begin', `:end',
5371 `:contents-begin', `:contents-end' and `:structure'. When
5372 optional argument PROPS is a list of keywords, only shift
5373 properties provided in that list.
5375 Properties are modified by side-effect. Return ELEMENT."
5376 (let ((properties (nth 1 element)))
5377 ;; Shift :structure property for the first plain list only: it is
5378 ;; the only one that really matters and it prevents from shifting
5379 ;; it more than once.
5380 (when (and (or (not props) (memq :structure props))
5381 (eq (org-element-type element) 'plain-list)
5382 (not (eq (org-element-type (plist-get properties :parent))
5383 'item)))
5384 (dolist (item (plist-get properties :structure))
5385 (incf (car item) offset)
5386 (incf (nth 6 item) offset)))
5387 (dolist (key '(:begin :contents-begin :contents-end :end :post-affiliated))
5388 (let ((value (and (or (not props) (memq key props))
5389 (plist-get properties key))))
5390 (and value (plist-put properties key (+ offset value))))))
5391 element)
5393 (defun org-element--cache-mapc (__map-function__ &optional reverse)
5394 "Apply FUNCTION to all elements in cache.
5395 FUNCTION is applied to the elements in ascending order, or
5396 descending order if REVERSE is non-nil."
5397 (avl-tree--mapc
5398 #'(lambda (node)
5399 (funcall __map-function__ (avl-tree--node-data node)))
5400 (org-element--cache-root)
5401 (if reverse 1 0)))
5403 (defun org-element--cache-sync (buffer)
5404 "Synchronize cache with recent modification in BUFFER.
5405 Elements ending before modification area are kept in cache.
5406 Elements starting after modification area have their position
5407 shifted by the size of the modification. Every other element is
5408 removed from the cache."
5409 (when (buffer-live-p buffer)
5410 (with-current-buffer buffer
5411 (when (org-element--cache-pending-changes-p)
5412 (catch 'escape
5413 (let ((inhibit-quit t)
5414 (offset (aref org-element--cache-status 3))
5415 ;; END is the beginning position of the first element
5416 ;; in cache that isn't removed but needs to be
5417 ;; shifted. It will be updated during phase 1.
5418 (end (aref org-element--cache-status 2)))
5419 ;; Phase 1.
5421 ;; Delete, in ascending order, all elements starting after
5422 ;; BEG, but before END.
5424 ;; BEG is the position of the first element in cache to
5425 ;; remove. It takes into consideration partially modified
5426 ;; elements (starting before changes but ending after
5427 ;; them). Though, it preserves greater elements that are
5428 ;; not affected when changes alter only their contents.
5430 ;; END is updated when necessary to include elements
5431 ;; starting after modifications but included in an element
5432 ;; altered by modifications.
5434 ;; At each iteration, we start again at tree root since
5435 ;; a deletion modifies structure of the balanced tree.
5436 (let ((beg
5437 (let* ((beg (aref org-element--cache-status 1))
5438 (element (org-element-cache-get (1- beg) t)))
5439 (if (not element) beg
5440 (catch 'exit
5441 (let ((up element))
5442 (while (setq up (org-element-property :parent up))
5443 (if (and
5444 (memq (org-element-type up)
5445 '(center-block
5446 drawer dynamic-block inlinetask
5447 property-drawer quote-block
5448 special-block))
5449 (<= (org-element-property :contents-begin up)
5450 beg)
5451 (> (org-element-property :contents-end up)
5452 end))
5453 ;; UP is a greater element that is
5454 ;; wrapped around the changes. We
5455 ;; only need to extend its ending
5456 ;; boundaries and those of all its
5457 ;; parents.
5458 (throw 'exit
5459 (progn
5460 (while up
5461 (org-element--cache-shift-positions
5462 up offset '(:contents-end :end))
5463 (setq up (org-element-property
5464 :parent up)))
5465 (org-element-property
5466 :begin element))))
5467 (setq element up))
5468 ;; We're at top level element containing
5469 ;; ELEMENT: if it's altered by buffer
5470 ;; modifications, it is first element in
5471 ;; cache to be removed. Otherwise, that
5472 ;; first element is the following one.
5473 (if (< (org-element-property :end element) beg)
5474 (org-element-property :end element)
5475 (org-element-property :begin element))))))))
5476 (while (let ((node (org-element--cache-root)) data)
5477 ;; DATA will contain the closest element from
5478 ;; BEG, always after it.
5479 (while node
5480 (let* ((element (avl-tree--node-data node))
5481 (pos (org-element-property :begin element)))
5482 (cond
5483 ((< pos beg)
5484 (setq node (avl-tree--node-right node)))
5485 ((> pos beg)
5486 (setq data (avl-tree--node-data node)
5487 node (avl-tree--node-left node)))
5489 (setq data (avl-tree--node-data node)
5490 node nil)))))
5491 (cond
5492 ;; No DATA is found so there's no element left
5493 ;; after BEG. Bail out.
5494 ((not data) (throw 'escape t))
5495 ;; Element starts after END, it is the first
5496 ;; one that needn't be removed from cache.
5497 ;; Move to second phase.
5498 ((> (org-element-property :begin data) end) nil)
5499 ;; Remove element. Extend END so that all
5500 ;; elements it may contain are also removed.
5502 (setq end
5503 (max (1- (org-element-property :end data)) end))
5504 (avl-tree-delete org-element--cache data)
5505 t)))))
5506 ;; Phase 2.
5508 ;; Shift all elements starting after END by OFFSET (for an
5509 ;; offset different from 0).
5511 ;; Increasing all beginning positions by OFFSET doesn't
5512 ;; alter tree structure, so elements are modified by
5513 ;; side-effect.
5515 ;; We change all elements in decreasing order and make
5516 ;; sure to quit at the first element in cache starting
5517 ;; before END.
5518 (unless (zerop offset)
5519 (catch 'exit
5520 (org-element--cache-mapc
5521 #'(lambda (data)
5522 (if (<= (org-element-property :begin data) end)
5523 (throw 'exit t)
5524 ;; Shift element.
5525 (org-element--cache-shift-positions data offset)
5526 ;; Shift associated objects data, if any.
5527 (dolist (object-data
5528 (gethash data org-element--cache-objects))
5529 (incf (car object-data) offset)
5530 (dolist (successor (nth 1 object-data))
5531 (incf (cdr successor) offset))
5532 (dolist (object (cddr object-data))
5533 (org-element--cache-shift-positions
5534 object offset)))))
5535 'reverse)))))
5536 ;; Eventually signal cache as up-to-date.
5537 (org-element--cache-cancel-changes)))))
5540 ;;;; Public Functions
5542 (defun org-element-cache-get (key &optional ignore-changes)
5543 "Return cached data relative to KEY.
5545 KEY is either a number or an Org element, as returned by
5546 `org-element-at-point'. If KEY is a number, return closest
5547 cached data before or at position KEY. Otherwise, return cached
5548 objects contained in element KEY.
5550 In any case, return nil if no data is found, or if caching is not
5551 allowed.
5553 If changes are pending in current buffer, first synchronize the
5554 cache, unless optional argument IGNORE-CHANGES is non-nil."
5555 (when (and org-element-use-cache org-element--cache)
5556 ;; If there are pending changes, first sync them.
5557 (when (and (not ignore-changes) (org-element--cache-pending-changes-p))
5558 (org-element--cache-sync (current-buffer)))
5559 (if (not (wholenump key)) (gethash key org-element--cache-objects)
5560 (let ((node (org-element--cache-root)) last)
5561 (catch 'found
5562 (while node
5563 (let* ((element (avl-tree--node-data node))
5564 (beg (org-element-property :begin element)))
5565 (cond
5566 ((< key beg)
5567 (setq node (avl-tree--node-left node)))
5568 ((> key beg)
5569 (setq last (avl-tree--node-data node)
5570 node (avl-tree--node-right node)))
5571 ;; When KEY is at the beginning of a table or list,
5572 ;; make sure to return it instead of the first row or
5573 ;; item.
5574 ((and (memq (org-element-type element) '(item table-row))
5575 (= (org-element-property
5576 :contents-begin (org-element-property :parent element))
5577 beg))
5578 (setq last (avl-tree--node-data node)
5579 node (avl-tree--node-left node)))
5580 (t (throw 'found (avl-tree--node-data node))))))
5581 last)))))
5583 (defun org-element-cache-put (data &optional element)
5584 "Store DATA in current buffer's cache, if allowed.
5585 If optional argument ELEMENT is non-nil, store DATA as objects
5586 relative to it. Otherwise, store DATA as an element. Nothing
5587 will be stored if `org-element-use-cache' is nil. Return DATA."
5588 (if (not (and org-element-use-cache (derived-mode-p 'org-mode))) data
5589 (unless (and org-element--cache org-element--cache-objects)
5590 (org-element-cache-reset))
5591 (if element (puthash element data org-element--cache-objects)
5592 (avl-tree-enter org-element--cache data))))
5594 ;;;###autoload
5595 (defun org-element-cache-reset (&optional all)
5596 "Reset cache in current buffer.
5597 When optional argument ALL is non-nil, reset cache in all Org
5598 buffers. This function will do nothing if
5599 `org-element-use-cache' is nil."
5600 (interactive "P")
5601 (when org-element-use-cache
5602 (dolist (buffer (if all (buffer-list) (list (current-buffer))))
5603 (with-current-buffer buffer
5604 (when (derived-mode-p 'org-mode)
5605 (if (org-bound-and-true-p org-element--cache)
5606 (avl-tree-clear org-element--cache)
5607 (org-set-local 'org-element--cache
5608 (avl-tree-create #'org-element--cache-compare)))
5609 (if org-element--cache-objects (clrhash org-element--cache-objects)
5610 (org-set-local
5611 'org-element--cache-objects
5612 (make-hash-table :size 1009 :weakness 'key :test #'eq)))
5613 (org-set-local 'org-element--cache-status (make-vector 6 nil))
5614 (add-hook 'before-change-functions
5615 'org-element--cache-before-change nil t)
5616 (add-hook 'after-change-functions
5617 'org-element--cache-record-change nil t))))))
5621 (provide 'org-element)
5623 ;; Local variables:
5624 ;; generated-autoload-file: "org-loaddefs.el"
5625 ;; End:
5627 ;;; org-element.el ends here