1 ;;; org-element.el --- Parser And Applications for Org syntax
3 ;; Copyright (C) 2012 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; Org syntax can be divided into three categories: "Greater
26 ;; elements", "Elements" and "Objects".
28 ;; Elements are related to the structure of the document. Indeed, all
29 ;; elements are a cover for the document: each position within belongs
30 ;; to at least one element.
32 ;; An element always starts and ends at the beginning of a line. With
33 ;; a few exceptions (`clock', `headline', `inlinetask', `item',
34 ;; `planning', `node-property', `quote-section' `section' and
35 ;; `table-row' types), it can also accept a fixed set of keywords as
36 ;; attributes. Those are called "affiliated keywords" to distinguish
37 ;; them from other keywords, which are full-fledged elements. Almost
38 ;; all affiliated keywords are referenced in
39 ;; `org-element-affiliated-keywords'; the others are export attributes
40 ;; and start with "ATTR_" prefix.
42 ;; Element containing other elements (and only elements) are called
43 ;; greater elements. Concerned types are: `center-block', `drawer',
44 ;; `dynamic-block', `footnote-definition', `headline', `inlinetask',
45 ;; `item', `plain-list', `property-drawer', `quote-block', `section'
46 ;; and `special-block'.
48 ;; Other element types are: `babel-call', `clock', `comment',
49 ;; `comment-block', `example-block', `export-block', `fixed-width',
50 ;; `horizontal-rule', `keyword', `latex-environment', `node-property',
51 ;; `paragraph', `planning', `quote-section', `src-block', `table',
52 ;; `table-row' and `verse-block'. Among them, `paragraph' and
53 ;; `verse-block' types can contain Org objects and plain text.
55 ;; Objects are related to document's contents. Some of them are
56 ;; recursive. Associated types are of the following: `bold', `code',
57 ;; `entity', `export-snippet', `footnote-reference',
58 ;; `inline-babel-call', `inline-src-block', `italic',
59 ;; `latex-fragment', `line-break', `link', `macro', `radio-target',
60 ;; `statistics-cookie', `strike-through', `subscript', `superscript',
61 ;; `table-cell', `target', `timestamp', `underline' and `verbatim'.
63 ;; Some elements also have special properties whose value can hold
64 ;; objects themselves (i.e. an item tag or an headline name). Such
65 ;; values are called "secondary strings". Any object belongs to
66 ;; either an element or a secondary string.
68 ;; Notwithstanding affiliated keywords, each greater element, element
69 ;; and object has a fixed set of properties attached to it. Among
70 ;; them, four are shared by all types: `:begin' and `:end', which
71 ;; refer to the beginning and ending buffer positions of the
72 ;; considered element or object, `:post-blank', which holds the number
73 ;; of blank lines, or white spaces, at its end and `:parent' which
74 ;; refers to the element or object containing it. Greater elements
75 ;; and elements containing objects will also have `:contents-begin'
76 ;; and `:contents-end' properties to delimit contents.
78 ;; Lisp-wise, an element or an object can be represented as a list.
79 ;; It follows the pattern (TYPE PROPERTIES CONTENTS), where:
80 ;; TYPE is a symbol describing the Org element or object.
81 ;; PROPERTIES is the property list attached to it. See docstring of
82 ;; appropriate parsing function to get an exhaustive
84 ;; CONTENTS is a list of elements, objects or raw strings contained
85 ;; in the current element or object, when applicable.
87 ;; An Org buffer is a nested list of such elements and objects, whose
88 ;; type is `org-data' and properties is nil.
90 ;; The first part of this file defines Org syntax, while the second
91 ;; one provide accessors and setters functions.
93 ;; The next part implements a parser and an interpreter for each
94 ;; element and object type in Org syntax.
96 ;; The following part creates a fully recursive buffer parser. It
97 ;; also provides a tool to map a function to elements or objects
98 ;; matching some criteria in the parse tree. Functions of interest
99 ;; are `org-element-parse-buffer', `org-element-map' and, to a lesser
100 ;; extent, `org-element-parse-secondary-string'.
102 ;; The penultimate part is the cradle of an interpreter for the
103 ;; obtained parse tree: `org-element-interpret-data'.
105 ;; The library ends by furnishing `org-element-at-point' function, and
106 ;; a way to give information about document structure around point
107 ;; with `org-element-context'.
112 (eval-when-compile (require 'cl
))
117 ;;; Definitions And Rules
119 ;; Define elements, greater elements and specify recursive objects,
120 ;; along with the affiliated keywords recognized. Also set up
121 ;; restrictions on recursive objects combinations.
123 ;; These variables really act as a control center for the parsing
126 (defconst org-element-paragraph-separate
128 ;; Headlines, inlinetasks.
129 org-outline-regexp
"\\|"
130 ;; Footnote definitions.
131 "\\[\\(?:[0-9]+\\|fn:[-_[:word:]]+\\)\\]" "\\|"
135 ;; Tables (any type).
136 "\\(?:|\\|\\+-[-+]\\)" "\\|"
137 ;; Blocks (any type), Babel calls, drawers (any type),
138 ;; fixed-width areas and keywords. Note: this is only an
139 ;; indication and need some thorough check.
142 "-\\{5,\\}[ \t]*$" "\\|"
143 ;; LaTeX environments.
144 "\\\\begin{\\([A-Za-z0-9]+\\*?\\)}" "\\|"
145 ;; Planning and Clock lines.
146 (regexp-opt (list org-scheduled-string
152 (let ((term (case org-plain-list-ordered-item-terminator
153 (?\
) ")") (?.
"\\.") (otherwise "[.)]")))
154 (alpha (and org-alphabetical-lists
"\\|[A-Za-z]")))
155 (concat "\\(?:[-+*]\\|\\(?:[0-9]+" alpha
"\\)" term
"\\)"
156 "\\(?:[ \t]\\|$\\)"))
158 "Regexp to separate paragraphs in an Org buffer.
159 In the case of lines starting with \"#\" and \":\", this regexp
160 is not sufficient to know if point is at a paragraph ending. See
161 `org-element-paragraph-parser' for more information.")
163 (defconst org-element-all-elements
164 '(babel-call center-block clock comment comment-block drawer dynamic-block
165 example-block export-block fixed-width footnote-definition
166 headline horizontal-rule inlinetask item keyword
167 latex-environment node-property paragraph plain-list planning
168 property-drawer quote-block quote-section section special-block
169 src-block table table-row verse-block
)
170 "Complete list of element types.")
172 (defconst org-element-greater-elements
173 '(center-block drawer dynamic-block footnote-definition headline inlinetask
174 item plain-list property-drawer quote-block section
176 "List of recursive element types aka Greater Elements.")
178 (defconst org-element-all-successors
179 '(export-snippet footnote-reference inline-babel-call inline-src-block
180 latex-or-entity line-break link macro radio-target
181 statistics-cookie sub
/superscript table-cell target
182 text-markup timestamp
)
183 "Complete list of successors.")
185 (defconst org-element-object-successor-alist
186 '((subscript . sub
/superscript
) (superscript . sub
/superscript
)
187 (bold . text-markup
) (code . text-markup
) (italic . text-markup
)
188 (strike-through . text-markup
) (underline . text-markup
)
189 (verbatim . text-markup
) (entity . latex-or-entity
)
190 (latex-fragment . latex-or-entity
))
191 "Alist of translations between object type and successor name.
193 Sharing the same successor comes handy when, for example, the
194 regexp matching one object can also match the other object.")
196 (defconst org-element-all-objects
197 '(bold code entity export-snippet footnote-reference inline-babel-call
198 inline-src-block italic line-break latex-fragment link macro
199 radio-target statistics-cookie strike-through subscript superscript
200 table-cell target timestamp underline verbatim
)
201 "Complete list of object types.")
203 (defconst org-element-recursive-objects
204 '(bold italic link subscript radio-target strike-through superscript
205 table-cell underline
)
206 "List of recursive object types.")
208 (defconst org-element-block-name-alist
209 '(("CENTER" . org-element-center-block-parser
)
210 ("COMMENT" . org-element-comment-block-parser
)
211 ("EXAMPLE" . org-element-example-block-parser
)
212 ("QUOTE" . org-element-quote-block-parser
)
213 ("SRC" . org-element-src-block-parser
)
214 ("VERSE" . org-element-verse-block-parser
))
215 "Alist between block names and the associated parsing function.
216 Names must be uppercase. Any block whose name has no association
217 is parsed with `org-element-special-block-parser'.")
219 (defconst org-element-link-type-is-file
220 '("file" "file+emacs" "file+sys" "docview")
221 "List of link types equivalent to \"file\".
222 Only these types can accept search options and an explicit
223 application to open them.")
225 (defconst org-element-affiliated-keywords
226 '("CAPTION" "DATA" "HEADER" "HEADERS" "LABEL" "NAME" "PLOT" "RESNAME" "RESULT"
227 "RESULTS" "SOURCE" "SRCNAME" "TBLNAME")
228 "List of affiliated keywords as strings.
229 By default, all keywords setting attributes (i.e. \"ATTR_LATEX\")
230 are affiliated keywords and need not to be in this list.")
232 (defconst org-element--affiliated-re
233 (format "[ \t]*#\\+%s:"
234 ;; Regular affiliated keywords.
235 (format "\\(%s\\|ATTR_[-_A-Za-z0-9]+\\)\\(?:\\[\\(.*\\)\\]\\)?"
236 (regexp-opt org-element-affiliated-keywords
)))
237 "Regexp matching any affiliated keyword.
239 Keyword name is put in match group 1. Moreover, if keyword
240 belongs to `org-element-dual-keywords', put the dual value in
243 Don't modify it, set `org-element-affiliated-keywords' instead.")
245 (defconst org-element-keyword-translation-alist
246 '(("DATA" .
"NAME") ("LABEL" .
"NAME") ("RESNAME" .
"NAME")
247 ("SOURCE" .
"NAME") ("SRCNAME" .
"NAME") ("TBLNAME" .
"NAME")
248 ("RESULT" .
"RESULTS") ("HEADERS" .
"HEADER"))
249 "Alist of usual translations for keywords.
250 The key is the old name and the value the new one. The property
251 holding their value will be named after the translated name.")
253 (defconst org-element-multiple-keywords
'("CAPTION" "HEADER")
254 "List of affiliated keywords that can occur more than once in an element.
256 Their value will be consed into a list of strings, which will be
257 returned as the value of the property.
259 This list is checked after translations have been applied. See
260 `org-element-keyword-translation-alist'.
262 By default, all keywords setting attributes (i.e. \"ATTR_LATEX\")
263 allow multiple occurrences and need not to be in this list.")
265 (defconst org-element-parsed-keywords
'("CAPTION")
266 "List of affiliated keywords whose value can be parsed.
268 Their value will be stored as a secondary string: a list of
271 This list is checked after translations have been applied. See
272 `org-element-keyword-translation-alist'.")
274 (defconst org-element-dual-keywords
'("CAPTION" "RESULTS")
275 "List of affiliated keywords which can have a secondary value.
277 In Org syntax, they can be written with optional square brackets
278 before the colons. For example, RESULTS keyword can be
279 associated to a hash value with the following:
281 #+RESULTS[hash-string]: some-source
283 This list is checked after translations have been applied. See
284 `org-element-keyword-translation-alist'.")
286 (defconst org-element-document-properties
'("AUTHOR" "DATE" "TITLE")
287 "List of properties associated to the whole document.
288 Any keyword in this list will have its value parsed and stored as
289 a secondary string.")
291 (defconst org-element-object-restrictions
292 '((bold export-snippet inline-babel-call inline-src-block latex-or-entity link
293 radio-target sub
/superscript target text-markup timestamp
)
294 (footnote-reference export-snippet footnote-reference inline-babel-call
295 inline-src-block latex-or-entity line-break link macro
296 radio-target sub
/superscript target text-markup
298 (headline inline-babel-call inline-src-block latex-or-entity link macro
299 radio-target statistics-cookie sub
/superscript target text-markup
301 (inlinetask inline-babel-call inline-src-block latex-or-entity link macro
302 radio-target sub
/superscript target text-markup timestamp
)
303 (italic export-snippet inline-babel-call inline-src-block latex-or-entity
304 link radio-target sub
/superscript target text-markup timestamp
)
305 (item export-snippet footnote-reference inline-babel-call latex-or-entity
306 link macro radio-target sub
/superscript target text-markup
)
307 (keyword latex-or-entity macro sub
/superscript text-markup
)
308 (link export-snippet inline-babel-call inline-src-block latex-or-entity link
309 sub
/superscript text-markup
)
310 (paragraph export-snippet footnote-reference inline-babel-call
311 inline-src-block latex-or-entity line-break link macro
312 radio-target statistics-cookie sub
/superscript target text-markup
314 (radio-target export-snippet latex-or-entity sub
/superscript
)
315 (strike-through export-snippet inline-babel-call inline-src-block
316 latex-or-entity link radio-target sub
/superscript target
317 text-markup timestamp
)
318 (subscript export-snippet inline-babel-call inline-src-block latex-or-entity
319 sub
/superscript target text-markup
)
320 (superscript export-snippet inline-babel-call inline-src-block
321 latex-or-entity sub
/superscript target text-markup
)
322 (table-cell export-snippet latex-or-entity link macro radio-target
323 sub
/superscript target text-markup timestamp
)
324 (table-row table-cell
)
325 (underline export-snippet inline-babel-call inline-src-block latex-or-entity
326 link radio-target sub
/superscript target text-markup timestamp
)
327 (verse-block footnote-reference inline-babel-call inline-src-block
328 latex-or-entity line-break link macro radio-target
329 sub
/superscript target text-markup timestamp
))
330 "Alist of objects restrictions.
332 CAR is an element or object type containing objects and CDR is
333 a list of successors that will be called within an element or
336 For example, in a `radio-target' object, one can only find
337 entities, export snippets, latex-fragments, subscript and
340 This alist also applies to secondary string. For example, an
341 `headline' type element doesn't directly contain objects, but
342 still has an entry since one of its properties (`:title') does.")
344 (defconst org-element-secondary-value-alist
345 '((headline .
:title
)
346 (inlinetask .
:title
)
348 (footnote-reference .
:inline-definition
))
349 "Alist between element types and location of secondary value.")
353 ;;; Accessors and Setters
355 ;; Provide four accessors: `org-element-type', `org-element-property'
356 ;; `org-element-contents' and `org-element-restriction'.
358 ;; Setter functions allow to modify elements by side effect. There is
359 ;; `org-element-put-property', `org-element-set-contents',
360 ;; `org-element-set-element' and `org-element-adopt-element'. Note
361 ;; that `org-element-set-element' and `org-element-adopt-elements' are
362 ;; higher level functions since also update `:parent' property.
364 (defsubst org-element-type
(element)
365 "Return type of ELEMENT.
367 The function returns the type of the element or object provided.
368 It can also return the following special value:
369 `plain-text' for a string
370 `org-data' for a complete document
371 nil in any other case."
373 ((not (consp element
)) (and (stringp element
) 'plain-text
))
374 ((symbolp (car element
)) (car element
))))
376 (defsubst org-element-property
(property element
)
377 "Extract the value from the PROPERTY of an ELEMENT."
378 (plist-get (nth 1 element
) property
))
380 (defsubst org-element-contents
(element)
381 "Extract contents from an ELEMENT."
382 (and (consp element
) (nthcdr 2 element
)))
384 (defsubst org-element-restriction
(element)
385 "Return restriction associated to ELEMENT.
386 ELEMENT can be an element, an object or a symbol representing an
387 element or object type."
388 (cdr (assq (if (symbolp element
) element
(org-element-type element
))
389 org-element-object-restrictions
)))
391 (defsubst org-element-put-property
(element property value
)
392 "In ELEMENT set PROPERTY to VALUE.
393 Return modified element."
394 (when (consp element
)
395 (setcar (cdr element
) (plist-put (nth 1 element
) property value
)))
398 (defsubst org-element-set-contents
(element &rest contents
)
399 "Set ELEMENT contents to CONTENTS.
400 Return modified element."
401 (cond ((not element
) (list contents
))
402 ((cdr element
) (setcdr (cdr element
) contents
))
403 (t (nconc element contents
))))
405 (defsubst org-element-set-element
(old new
)
406 "Replace element or object OLD with element or object NEW.
407 The function takes care of setting `:parent' property for NEW."
408 ;; Since OLD is going to be changed into NEW by side-effect, first
409 ;; make sure that every element or object within NEW has OLD as
411 (mapc (lambda (blob) (org-element-put-property blob
:parent old
))
412 (org-element-contents new
))
413 ;; Transfer contents.
414 (apply 'org-element-set-contents old
(org-element-contents new
))
415 ;; Ensure NEW has same parent as OLD, then overwrite OLD properties
417 (org-element-put-property new
:parent
(org-element-property :parent old
))
418 (setcar (cdr old
) (nth 1 new
))
420 (setcar old
(car new
)))
422 (defsubst org-element-adopt-elements
(parent &rest children
)
423 "Append elements to the contents of another element.
425 PARENT is an element or object. CHILDREN can be elements,
426 objects, or a strings.
428 The function takes care of setting `:parent' property for CHILD.
429 Return parent element."
430 (if (not parent
) children
431 ;; Link every child to PARENT.
432 (mapc (lambda (child)
433 (unless (stringp child
)
434 (org-element-put-property child
:parent parent
)))
436 ;; Add CHILDREN at the end of PARENT contents.
437 (apply 'org-element-set-contents
439 (nconc (org-element-contents parent
) children
))
440 ;; Return modified PARENT element.
447 ;; For each greater element type, we define a parser and an
450 ;; A parser returns the element or object as the list described above.
451 ;; Most of them accepts no argument. Though, exceptions exist. Hence
452 ;; every element containing a secondary string (see
453 ;; `org-element-secondary-value-alist') will accept an optional
454 ;; argument to toggle parsing of that secondary string. Moreover,
455 ;; `item' parser requires current list's structure as its first
458 ;; An interpreter accepts two arguments: the list representation of
459 ;; the element or object, and its contents. The latter may be nil,
460 ;; depending on the element or object considered. It returns the
461 ;; appropriate Org syntax, as a string.
463 ;; Parsing functions must follow the naming convention:
464 ;; org-element-TYPE-parser, where TYPE is greater element's type, as
465 ;; defined in `org-element-greater-elements'.
467 ;; Similarly, interpreting functions must follow the naming
468 ;; convention: org-element-TYPE-interpreter.
470 ;; With the exception of `headline' and `item' types, greater elements
471 ;; cannot contain other greater elements of their own type.
473 ;; Beside implementing a parser and an interpreter, adding a new
474 ;; greater element requires to tweak `org-element--current-element'.
475 ;; Moreover, the newly defined type must be added to both
476 ;; `org-element-all-elements' and `org-element-greater-elements'.
481 (defun org-element-center-block-parser (limit affiliated
)
482 "Parse a center block.
484 LIMIT bounds the search. AFFILIATED is a list of which CAR is
485 the buffer position at the beginning of the first affiliated
486 keyword and CDR is a plist of affiliated keywords along with
489 Return a list whose CAR is `center-block' and CDR is a plist
490 containing `:begin', `:end', `:hiddenp', `:contents-begin',
491 `:contents-end' and `:post-blank' keywords.
493 Assume point is at the beginning of the block."
494 (let ((case-fold-search t
))
495 (if (not (save-excursion
496 (re-search-forward "^[ \t]*#\\+END_CENTER[ \t]*$" limit t
)))
497 ;; Incomplete block: parse it as a paragraph.
498 (org-element-paragraph-parser limit affiliated
)
499 (let ((block-end-line (match-beginning 0)))
500 (let* ((begin (car affiliated
))
501 ;; Empty blocks have no contents.
502 (contents-begin (progn (forward-line)
503 (and (< (point) block-end-line
)
505 (contents-end (and contents-begin block-end-line
))
506 (hidden (org-invisible-p2))
507 (pos-before-blank (progn (goto-char block-end-line
)
510 (end (save-excursion (skip-chars-forward " \r\t\n" limit
)
511 (if (eobp) (point) (point-at-bol)))))
517 :contents-begin contents-begin
518 :contents-end contents-end
519 :post-blank
(count-lines pos-before-blank end
))
520 (cdr affiliated
))))))))
522 (defun org-element-center-block-interpreter (center-block contents
)
523 "Interpret CENTER-BLOCK element as Org syntax.
524 CONTENTS is the contents of the element."
525 (format "#+BEGIN_CENTER\n%s#+END_CENTER" contents
))
530 (defun org-element-drawer-parser (limit affiliated
)
533 LIMIT bounds the search. AFFILIATED is a list of which CAR is
534 the buffer position at the beginning of the first affiliated
535 keyword and CDR is a plist of affiliated keywords along with
538 Return a list whose CAR is `drawer' and CDR is a plist containing
539 `:drawer-name', `:begin', `:end', `:hiddenp', `:contents-begin',
540 `:contents-end' and `:post-blank' keywords.
542 Assume point is at beginning of drawer."
543 (let ((case-fold-search t
))
544 (if (not (save-excursion (re-search-forward "^[ \t]*:END:[ \t]*$" limit t
)))
545 ;; Incomplete drawer: parse it as a paragraph.
546 (org-element-paragraph-parser limit affiliated
)
548 (let* ((drawer-end-line (match-beginning 0))
549 (name (progn (looking-at org-drawer-regexp
)
550 (org-match-string-no-properties 1)))
551 (begin (car affiliated
))
552 ;; Empty drawers have no contents.
553 (contents-begin (progn (forward-line)
554 (and (< (point) drawer-end-line
)
556 (contents-end (and contents-begin drawer-end-line
))
557 (hidden (org-invisible-p2))
558 (pos-before-blank (progn (goto-char drawer-end-line
)
561 (end (progn (skip-chars-forward " \r\t\n" limit
)
562 (if (eobp) (point) (point-at-bol)))))
569 :contents-begin contents-begin
570 :contents-end contents-end
571 :post-blank
(count-lines pos-before-blank end
))
572 (cdr affiliated
))))))))
574 (defun org-element-drawer-interpreter (drawer contents
)
575 "Interpret DRAWER element as Org syntax.
576 CONTENTS is the contents of the element."
577 (format ":%s:\n%s:END:"
578 (org-element-property :drawer-name drawer
)
584 (defun org-element-dynamic-block-parser (limit affiliated
)
585 "Parse a dynamic block.
587 LIMIT bounds the search. AFFILIATED is a list of which CAR is
588 the buffer position at the beginning of the first affiliated
589 keyword and CDR is a plist of affiliated keywords along with
592 Return a list whose CAR is `dynamic-block' and CDR is a plist
593 containing `:block-name', `:begin', `:end', `:hiddenp',
594 `:contents-begin', `:contents-end', `:arguments' and
595 `:post-blank' keywords.
597 Assume point is at beginning of dynamic block."
598 (let ((case-fold-search t
))
599 (if (not (save-excursion
600 (re-search-forward "^[ \t]*#\\+END:?[ \t]*$" limit t
)))
601 ;; Incomplete block: parse it as a paragraph.
602 (org-element-paragraph-parser limit affiliated
)
603 (let ((block-end-line (match-beginning 0)))
605 (let* ((name (progn (looking-at org-dblock-start-re
)
606 (org-match-string-no-properties 1)))
607 (arguments (org-match-string-no-properties 3))
608 (begin (car affiliated
))
609 ;; Empty blocks have no contents.
610 (contents-begin (progn (forward-line)
611 (and (< (point) block-end-line
)
613 (contents-end (and contents-begin block-end-line
))
614 (hidden (org-invisible-p2))
615 (pos-before-blank (progn (goto-char block-end-line
)
618 (end (progn (skip-chars-forward " \r\t\n" limit
)
619 (if (eobp) (point) (point-at-bol)))))
627 :contents-begin contents-begin
628 :contents-end contents-end
629 :post-blank
(count-lines pos-before-blank end
))
630 (cdr affiliated
)))))))))
632 (defun org-element-dynamic-block-interpreter (dynamic-block contents
)
633 "Interpret DYNAMIC-BLOCK element as Org syntax.
634 CONTENTS is the contents of the element."
635 (format "#+BEGIN: %s%s\n%s#+END:"
636 (org-element-property :block-name dynamic-block
)
637 (let ((args (org-element-property :arguments dynamic-block
)))
638 (and args
(concat " " args
)))
642 ;;;; Footnote Definition
644 (defun org-element-footnote-definition-parser (limit affiliated
)
645 "Parse a footnote definition.
647 LIMIT bounds the search. AFFILIATED is a list of which CAR is
648 the buffer position at the beginning of the first affiliated
649 keyword and CDR is a plist of affiliated keywords along with
652 Return a list whose CAR is `footnote-definition' and CDR is
653 a plist containing `:label', `:begin' `:end', `:contents-begin',
654 `:contents-end' and `:post-blank' keywords.
656 Assume point is at the beginning of the footnote definition."
658 (let* ((label (progn (looking-at org-footnote-definition-re
)
659 (org-match-string-no-properties 1)))
660 (begin (car affiliated
))
661 (ending (save-excursion
665 (concat org-outline-regexp-bol
"\\|"
666 org-footnote-definition-re
"\\|"
667 "^[ \t]*$") limit
'move
))
670 (contents-begin (progn (search-forward "]")
671 (skip-chars-forward " \r\t\n" ending
)
672 (and (/= (point) ending
) (point))))
673 (contents-end (and contents-begin ending
))
674 (end (progn (goto-char ending
)
675 (skip-chars-forward " \r\t\n" limit
)
676 (if (eobp) (point) (point-at-bol)))))
677 (list 'footnote-definition
682 :contents-begin contents-begin
683 :contents-end contents-end
684 :post-blank
(count-lines ending end
))
685 (cdr affiliated
))))))
687 (defun org-element-footnote-definition-interpreter (footnote-definition contents
)
688 "Interpret FOOTNOTE-DEFINITION element as Org syntax.
689 CONTENTS is the contents of the footnote-definition."
690 (concat (format "[%s]" (org-element-property :label footnote-definition
))
697 (defun org-element-headline-parser (limit &optional raw-secondary-p
)
700 Return a list whose CAR is `headline' and CDR is a plist
701 containing `:raw-value', `:title', `:begin', `:end',
702 `:pre-blank', `:hiddenp', `:contents-begin' and `:contents-end',
703 `:level', `:priority', `:tags', `:todo-keyword',`:todo-type',
704 `:scheduled', `:deadline', `:timestamp', `:clock', `:category',
705 `:quotedp', `:archivedp', `:commentedp' and `:footnote-section-p'
708 The plist also contains any property set in the property drawer,
709 with its name in lowercase, the underscores replaced with hyphens
710 and colons at the beginning (i.e. `:custom-id').
712 When RAW-SECONDARY-P is non-nil, headline's title will not be
713 parsed as a secondary string, but as a plain string instead.
715 Assume point is at beginning of the headline."
717 (let* ((components (org-heading-components))
718 (level (nth 1 components
))
719 (todo (nth 2 components
))
721 (and todo
(if (member todo org-done-keywords
) 'done
'todo
)))
722 (tags (let ((raw-tags (nth 5 components
)))
723 (and raw-tags
(org-split-string raw-tags
":"))))
724 (raw-value (or (nth 4 components
) ""))
726 (let ((case-fold-search nil
))
727 (string-match (format "^%s\\( \\|$\\)" org-quote-string
)
730 (let ((case-fold-search nil
))
731 (string-match (format "^%s\\( \\|$\\)" org-comment-string
)
733 (archivedp (member org-archive-tag tags
))
734 (footnote-section-p (and org-footnote-section
735 (string= org-footnote-section raw-value
)))
736 ;; Normalize property names: ":SOME_PROP:" becomes
738 (standard-props (let (plist)
741 (let ((p-name (downcase (car p
))))
742 (while (string-match "_" p-name
)
744 (replace-match "-" nil nil p-name
)))
745 (setq p-name
(intern (concat ":" p-name
)))
747 (plist-put plist p-name
(cdr p
)))))
748 (org-entry-properties nil
'standard
))
750 (time-props (org-entry-properties nil
'special
"CLOCK"))
751 (scheduled (cdr (assoc "SCHEDULED" time-props
)))
752 (deadline (cdr (assoc "DEADLINE" time-props
)))
753 (clock (cdr (assoc "CLOCK" time-props
)))
754 (timestamp (cdr (assoc "TIMESTAMP" time-props
)))
756 (end (save-excursion (goto-char (org-end-of-subtree t t
))))
757 (pos-after-head (progn (forward-line) (point)))
758 (contents-begin (save-excursion
759 (skip-chars-forward " \r\t\n" end
)
760 (and (/= (point) end
) (line-beginning-position))))
761 (hidden (org-invisible-p2))
762 (contents-end (and contents-begin
763 (progn (goto-char end
)
764 (skip-chars-backward " \r\t\n")
767 ;; Clean RAW-VALUE from any quote or comment string.
768 (when (or quotedp commentedp
)
769 (let ((case-fold-search nil
))
771 (replace-regexp-in-string
773 (regexp-opt (list org-quote-string org-comment-string
))
777 ;; Clean TAGS from archive tag, if any.
778 (when archivedp
(setq tags
(delete org-archive-tag tags
)))
782 (list :raw-value raw-value
786 (if (not contents-begin
) 0
787 (count-lines pos-after-head contents-begin
))
789 :contents-begin contents-begin
790 :contents-end contents-end
792 :priority
(nth 3 components
)
800 :post-blank
(count-lines
801 (if (not contents-end
) pos-after-head
802 (goto-char contents-end
)
806 :footnote-section-p footnote-section-p
808 :commentedp commentedp
811 (org-element-put-property
813 (if raw-secondary-p raw-value
814 (org-element-parse-secondary-string
815 raw-value
(org-element-restriction 'headline
) headline
)))))))
817 (defun org-element-headline-interpreter (headline contents
)
818 "Interpret HEADLINE element as Org syntax.
819 CONTENTS is the contents of the element."
820 (let* ((level (org-element-property :level headline
))
821 (todo (org-element-property :todo-keyword headline
))
822 (priority (org-element-property :priority headline
))
823 (title (org-element-interpret-data
824 (org-element-property :title headline
)))
825 (tags (let ((tag-list (if (org-element-property :archivedp headline
)
826 (cons org-archive-tag
827 (org-element-property :tags headline
))
828 (org-element-property :tags headline
))))
830 (format ":%s:" (mapconcat 'identity tag-list
":")))))
831 (commentedp (org-element-property :commentedp headline
))
832 (quotedp (org-element-property :quotedp headline
))
833 (pre-blank (or (org-element-property :pre-blank headline
) 0))
834 (heading (concat (make-string level ?
*)
835 (and todo
(concat " " todo
))
836 (and quotedp
(concat " " org-quote-string
))
837 (and commentedp
(concat " " org-comment-string
))
839 (format " [#%s]" (char-to-string priority
)))
840 (cond ((and org-footnote-section
841 (org-element-property
842 :footnote-section-p headline
))
843 (concat " " org-footnote-section
))
844 (title (concat " " title
))))))
849 ((zerop org-tags-column
) (format " %s" tags
))
850 ((< org-tags-column
0)
853 (max (- (+ org-tags-column
(length heading
) (length tags
))) 1)
858 (make-string (max (- org-tags-column
(length heading
)) 1) ?
)
860 (make-string (1+ pre-blank
) 10)
866 (defun org-element-inlinetask-parser (limit &optional raw-secondary-p
)
867 "Parse an inline task.
869 Return a list whose CAR is `inlinetask' and CDR is a plist
870 containing `:title', `:begin', `:end', `:hiddenp',
871 `:contents-begin' and `:contents-end', `:level', `:priority',
872 `:raw-value', `:tags', `:todo-keyword', `:todo-type',
873 `:scheduled', `:deadline', `:timestamp', `:clock' and
874 `:post-blank' keywords.
876 The plist also contains any property set in the property drawer,
877 with its name in lowercase, the underscores replaced with hyphens
878 and colons at the beginning (i.e. `:custom-id').
880 When optional argument RAW-SECONDARY-P is non-nil, inline-task's
881 title will not be parsed as a secondary string, but as a plain
884 Assume point is at beginning of the inline task."
886 (let* ((keywords (org-element--collect-affiliated-keywords))
887 (begin (car keywords
))
888 (components (org-heading-components))
889 (todo (nth 2 components
))
891 (if (member todo org-done-keywords
) 'done
'todo
)))
892 (tags (let ((raw-tags (nth 5 components
)))
893 (and raw-tags
(org-split-string raw-tags
":"))))
894 (raw-value (or (nth 4 components
) ""))
895 ;; Normalize property names: ":SOME_PROP:" becomes
897 (standard-props (let (plist)
900 (let ((p-name (downcase (car p
))))
901 (while (string-match "_" p-name
)
903 (replace-match "-" nil nil p-name
)))
904 (setq p-name
(intern (concat ":" p-name
)))
906 (plist-put plist p-name
(cdr p
)))))
907 (org-entry-properties nil
'standard
))
909 (time-props (org-entry-properties nil
'special
"CLOCK"))
910 (scheduled (cdr (assoc "SCHEDULED" time-props
)))
911 (deadline (cdr (assoc "DEADLINE" time-props
)))
912 (clock (cdr (assoc "CLOCK" time-props
)))
913 (timestamp (cdr (assoc "TIMESTAMP" time-props
)))
914 (task-end (save-excursion
916 (and (re-search-forward "^\\*+ END" limit t
)
917 (match-beginning 0))))
918 (contents-begin (progn (forward-line)
919 (and task-end
(< (point) task-end
) (point))))
920 (hidden (and contents-begin
(org-invisible-p2)))
921 (contents-end (and contents-begin task-end
))
922 (before-blank (if (not task-end
) (point)
926 (end (progn (skip-chars-forward " \r\t\n" limit
)
927 (if (eobp) (point) (point-at-bol))))
931 (list :raw-value raw-value
935 :contents-begin contents-begin
936 :contents-end contents-end
937 :level
(nth 1 components
)
938 :priority
(nth 3 components
)
946 :post-blank
(count-lines before-blank end
))
949 (org-element-put-property
951 (if raw-secondary-p raw-value
952 (org-element-parse-secondary-string
954 (org-element-restriction 'inlinetask
)
957 (defun org-element-inlinetask-interpreter (inlinetask contents
)
958 "Interpret INLINETASK element as Org syntax.
959 CONTENTS is the contents of inlinetask."
960 (let* ((level (org-element-property :level inlinetask
))
961 (todo (org-element-property :todo-keyword inlinetask
))
962 (priority (org-element-property :priority inlinetask
))
963 (title (org-element-interpret-data
964 (org-element-property :title inlinetask
)))
965 (tags (let ((tag-list (org-element-property :tags inlinetask
)))
967 (format ":%s:" (mapconcat 'identity tag-list
":")))))
968 (task (concat (make-string level ?
*)
969 (and todo
(concat " " todo
))
971 (format " [#%s]" (char-to-string priority
)))
972 (and title
(concat " " title
)))))
977 ((zerop org-tags-column
) (format " %s" tags
))
978 ((< org-tags-column
0)
981 (max (- (+ org-tags-column
(length task
) (length tags
))) 1)
986 (make-string (max (- org-tags-column
(length task
)) 1) ?
)
988 ;; Prefer degenerate inlinetasks when there are no
993 (make-string level ?
*) " END")))))
998 (defun org-element-item-parser (limit struct
&optional raw-secondary-p
)
1001 STRUCT is the structure of the plain list.
1003 Return a list whose CAR is `item' and CDR is a plist containing
1004 `:bullet', `:begin', `:end', `:contents-begin', `:contents-end',
1005 `:checkbox', `:counter', `:tag', `:structure', `:hiddenp' and
1006 `:post-blank' keywords.
1008 When optional argument RAW-SECONDARY-P is non-nil, item's tag, if
1009 any, will not be parsed as a secondary string, but as a plain
1012 Assume point is at the beginning of the item."
1015 (looking-at org-list-full-item-re
)
1016 (let* ((begin (point))
1017 (bullet (org-match-string-no-properties 1))
1018 (checkbox (let ((box (org-match-string-no-properties 3)))
1019 (cond ((equal "[ ]" box
) 'off
)
1020 ((equal "[X]" box
) 'on
)
1021 ((equal "[-]" box
) 'trans
))))
1022 (counter (let ((c (org-match-string-no-properties 2)))
1026 ((string-match "[A-Za-z]" c
)
1027 (- (string-to-char (upcase (match-string 0 c
)))
1029 ((string-match "[0-9]+" c
)
1030 (string-to-number (match-string 0 c
)))))))
1031 (end (save-excursion (goto-char (org-list-get-item-end begin struct
))
1032 (unless (bolp) (forward-line))
1036 ;; Ignore tags in un-ordered lists: they are just
1037 ;; a part of item's body.
1038 (if (and (match-beginning 4)
1039 (save-match-data (string-match "[.)]" bullet
)))
1042 (skip-chars-forward " \r\t\n" limit
)
1043 ;; If first line isn't empty, contents really start
1044 ;; at the text after item's meta-data.
1045 (if (= (point-at-bol) begin
) (point) (point-at-bol))))
1046 (hidden (progn (forward-line)
1047 (and (not (= (point) end
)) (org-invisible-p2))))
1048 (contents-end (progn (goto-char end
)
1049 (skip-chars-backward " \r\t\n")
1054 (list :bullet bullet
1057 ;; CONTENTS-BEGIN and CONTENTS-END may be
1058 ;; mixed up in the case of an empty item
1059 ;; separated from the next by a blank line.
1060 ;; Thus ensure the former is always the
1062 :contents-begin
(min contents-begin contents-end
)
1063 :contents-end
(max contents-begin contents-end
)
1068 :post-blank
(count-lines contents-end end
)))))
1069 (org-element-put-property
1071 (let ((raw-tag (org-list-get-tag begin struct
)))
1073 (if raw-secondary-p raw-tag
1074 (org-element-parse-secondary-string
1075 raw-tag
(org-element-restriction 'item
) item
))))))))
1077 (defun org-element-item-interpreter (item contents
)
1078 "Interpret ITEM element as Org syntax.
1079 CONTENTS is the contents of the element."
1080 (let* ((bullet (org-list-bullet-string (org-element-property :bullet item
)))
1081 (checkbox (org-element-property :checkbox item
))
1082 (counter (org-element-property :counter item
))
1083 (tag (let ((tag (org-element-property :tag item
)))
1084 (and tag
(org-element-interpret-data tag
))))
1085 ;; Compute indentation.
1086 (ind (make-string (length bullet
) 32))
1087 (item-starts-with-par-p
1088 (eq (org-element-type (car (org-element-contents item
)))
1093 (and counter
(format "[@%d] " counter
))
1098 (and tag
(format "%s :: " tag
))
1099 (let ((contents (replace-regexp-in-string
1100 "\\(^\\)[ \t]*\\S-" ind contents nil nil
1)))
1101 (if item-starts-with-par-p
(org-trim contents
)
1102 (concat "\n" contents
))))))
1107 (defun org-element-plain-list-parser (limit affiliated structure
)
1108 "Parse a plain list.
1110 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1111 the buffer position at the beginning of the first affiliated
1112 keyword and CDR is a plist of affiliated keywords along with
1113 their value. STRUCTURE is the structure of the plain list being
1116 Return a list whose CAR is `plain-list' and CDR is a plist
1117 containing `:type', `:begin', `:end', `:contents-begin' and
1118 `:contents-end', `:structure' and `:post-blank' keywords.
1120 Assume point is at the beginning of the list."
1122 (let* ((struct (or structure
(org-list-struct)))
1123 (prevs (org-list-prevs-alist struct
))
1124 (parents (org-list-parents-alist struct
))
1125 (type (org-list-get-list-type (point) struct prevs
))
1126 (contents-begin (point))
1127 (begin (car affiliated
))
1129 (progn (goto-char (org-list-get-list-end (point) struct prevs
))
1130 (unless (bolp) (forward-line))
1132 (end (progn (skip-chars-forward " \r\t\n" limit
)
1133 (if (eobp) (point) (point-at-bol)))))
1140 :contents-begin contents-begin
1141 :contents-end contents-end
1143 :post-blank
(count-lines contents-end end
))
1144 (cdr affiliated
))))))
1146 (defun org-element-plain-list-interpreter (plain-list contents
)
1147 "Interpret PLAIN-LIST element as Org syntax.
1148 CONTENTS is the contents of the element."
1151 (goto-char (point-min))
1156 ;;;; Property Drawer
1158 (defun org-element-property-drawer-parser (limit affiliated
)
1159 "Parse a property drawer.
1161 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1162 the buffer position at the beginning of the first affiliated
1163 keyword and CDR is a plist of affiliated keywords along with
1166 Return a list whose CAR is `property-drawer' and CDR is a plist
1167 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1168 `:contents-end' and `:post-blank' keywords.
1170 Assume point is at the beginning of the property drawer."
1172 (let ((case-fold-search t
))
1173 (if (not (save-excursion
1174 (re-search-forward "^[ \t]*:END:[ \t]*$" limit t
)))
1175 ;; Incomplete drawer: parse it as a paragraph.
1176 (org-element-paragraph-parser limit affiliated
)
1178 (let* ((drawer-end-line (match-beginning 0))
1179 (begin (car affiliated
))
1180 (contents-begin (progn (forward-line)
1181 (and (< (point) drawer-end-line
)
1183 (contents-end (and contents-begin drawer-end-line
))
1184 (hidden (org-invisible-p2))
1185 (pos-before-blank (progn (goto-char drawer-end-line
)
1188 (end (progn (skip-chars-forward " \r\t\n" limit
)
1189 (if (eobp) (point) (point-at-bol)))))
1190 (list 'property-drawer
1195 :contents-begin contents-begin
1196 :contents-end contents-end
1197 :post-blank
(count-lines pos-before-blank end
))
1198 (cdr affiliated
)))))))))
1200 (defun org-element-property-drawer-interpreter (property-drawer contents
)
1201 "Interpret PROPERTY-DRAWER element as Org syntax.
1202 CONTENTS is the properties within the drawer."
1203 (format ":PROPERTIES:\n%s:END:" contents
))
1208 (defun org-element-quote-block-parser (limit affiliated
)
1209 "Parse a quote block.
1211 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1212 the buffer position at the beginning of the first affiliated
1213 keyword and CDR is a plist of affiliated keywords along with
1216 Return a list whose CAR is `quote-block' and CDR is a plist
1217 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1218 `:contents-end' and `:post-blank' keywords.
1220 Assume point is at the beginning of the block."
1221 (let ((case-fold-search t
))
1222 (if (not (save-excursion
1223 (re-search-forward "^[ \t]*#\\+END_QUOTE[ \t]*$" limit t
)))
1224 ;; Incomplete block: parse it as a paragraph.
1225 (org-element-paragraph-parser limit affiliated
)
1226 (let ((block-end-line (match-beginning 0)))
1228 (let* ((begin (car affiliated
))
1229 ;; Empty blocks have no contents.
1230 (contents-begin (progn (forward-line)
1231 (and (< (point) block-end-line
)
1233 (contents-end (and contents-begin block-end-line
))
1234 (hidden (org-invisible-p2))
1235 (pos-before-blank (progn (goto-char block-end-line
)
1238 (end (progn (skip-chars-forward " \r\t\n" limit
)
1239 (if (eobp) (point) (point-at-bol)))))
1245 :contents-begin contents-begin
1246 :contents-end contents-end
1247 :post-blank
(count-lines pos-before-blank end
))
1248 (cdr affiliated
)))))))))
1250 (defun org-element-quote-block-interpreter (quote-block contents
)
1251 "Interpret QUOTE-BLOCK element as Org syntax.
1252 CONTENTS is the contents of the element."
1253 (format "#+BEGIN_QUOTE\n%s#+END_QUOTE" contents
))
1258 (defun org-element-section-parser (limit)
1261 LIMIT bounds the search.
1263 Return a list whose CAR is `section' and CDR is a plist
1264 containing `:begin', `:end', `:contents-begin', `contents-end'
1265 and `:post-blank' keywords."
1267 ;; Beginning of section is the beginning of the first non-blank
1268 ;; line after previous headline.
1269 (let ((begin (point))
1270 (end (progn (org-with-limited-levels (outline-next-heading))
1272 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
1278 :contents-begin begin
1279 :contents-end pos-before-blank
1280 :post-blank
(count-lines pos-before-blank end
))))))
1282 (defun org-element-section-interpreter (section contents
)
1283 "Interpret SECTION element as Org syntax.
1284 CONTENTS is the contents of the element."
1290 (defun org-element-special-block-parser (limit affiliated
)
1291 "Parse a special block.
1293 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1294 the buffer position at the beginning of the first affiliated
1295 keyword and CDR is a plist of affiliated keywords along with
1298 Return a list whose CAR is `special-block' and CDR is a plist
1299 containing `:type', `:begin', `:end', `:hiddenp',
1300 `:contents-begin', `:contents-end' and `:post-blank' keywords.
1302 Assume point is at the beginning of the block."
1303 (let* ((case-fold-search t
)
1304 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(S-+\\)")
1305 (upcase (match-string-no-properties 1)))))
1306 (if (not (save-excursion
1308 (format "^[ \t]*#\\+END_%s[ \t]*$" type
) limit t
)))
1309 ;; Incomplete block: parse it as a paragraph.
1310 (org-element-paragraph-parser limit affiliated
)
1311 (let ((block-end-line (match-beginning 0)))
1313 (let* ((begin (car affiliated
))
1314 ;; Empty blocks have no contents.
1315 (contents-begin (progn (forward-line)
1316 (and (< (point) block-end-line
)
1318 (contents-end (and contents-begin block-end-line
))
1319 (hidden (org-invisible-p2))
1320 (pos-before-blank (progn (goto-char block-end-line
)
1323 (end (progn (org-skip-whitespace)
1324 (if (eobp) (point) (point-at-bol)))))
1325 (list 'special-block
1331 :contents-begin contents-begin
1332 :contents-end contents-end
1333 :post-blank
(count-lines pos-before-blank end
))
1334 (cdr affiliated
)))))))))
1336 (defun org-element-special-block-interpreter (special-block contents
)
1337 "Interpret SPECIAL-BLOCK element as Org syntax.
1338 CONTENTS is the contents of the element."
1339 (let ((block-type (org-element-property :type special-block
)))
1340 (format "#+BEGIN_%s\n%s#+END_%s" block-type contents block-type
)))
1346 ;; For each element, a parser and an interpreter are also defined.
1347 ;; Both follow the same naming convention used for greater elements.
1349 ;; Also, as for greater elements, adding a new element type is done
1350 ;; through the following steps: implement a parser and an interpreter,
1351 ;; tweak `org-element--current-element' so that it recognizes the new
1352 ;; type and add that new type to `org-element-all-elements'.
1354 ;; As a special case, when the newly defined type is a block type,
1355 ;; `org-element-block-name-alist' has to be modified accordingly.
1360 (defun org-element-babel-call-parser (limit affiliated
)
1361 "Parse a babel call.
1363 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1364 the buffer position at the beginning of the first affiliated
1365 keyword and CDR is a plist of affiliated keywords along with
1368 Return a list whose CAR is `babel-call' and CDR is a plist
1369 containing `:begin', `:end', `:info' and `:post-blank' as
1372 (let ((case-fold-search t
)
1373 (info (progn (looking-at org-babel-block-lob-one-liner-regexp
)
1374 (org-babel-lob-get-info)))
1375 (begin (car affiliated
))
1376 (pos-before-blank (progn (forward-line) (point)))
1377 (end (progn (skip-chars-forward " \r\t\n" limit
)
1378 (if (eobp) (point) (point-at-bol)))))
1384 :post-blank
(count-lines pos-before-blank end
))
1385 (cdr affiliated
))))))
1387 (defun org-element-babel-call-interpreter (babel-call contents
)
1388 "Interpret BABEL-CALL element as Org syntax.
1390 (let* ((babel-info (org-element-property :info babel-call
))
1391 (main (car babel-info
))
1392 (post-options (nth 1 babel-info
)))
1394 (if (not (string-match "\\[\\(\\[.*?\\]\\)\\]" main
)) main
1395 ;; Remove redundant square brackets.
1396 (replace-match (match-string 1 main
) nil nil main
))
1397 (and post-options
(format "[%s]" post-options
)))))
1402 (defun org-element-clock-parser (limit)
1405 LIMIT bounds the search.
1407 Return a list whose CAR is `clock' and CDR is a plist containing
1408 `:status', `:value', `:time', `:begin', `:end' and `:post-blank'
1411 (let* ((case-fold-search nil
)
1413 (value (progn (search-forward org-clock-string
(line-end-position) t
)
1414 (org-skip-whitespace)
1415 (looking-at "\\[.*\\]")
1416 (org-match-string-no-properties 0)))
1417 (time (and (progn (goto-char (match-end 0))
1418 (looking-at " +=> +\\(\\S-+\\)[ \t]*$"))
1419 (org-match-string-no-properties 1)))
1420 (status (if time
'closed
'running
))
1421 (post-blank (let ((before-blank (progn (forward-line) (point))))
1422 (skip-chars-forward " \r\t\n" limit
)
1423 (unless (eobp) (beginning-of-line))
1424 (count-lines before-blank
(point))))
1427 (list :status status
1432 :post-blank post-blank
)))))
1434 (defun org-element-clock-interpreter (clock contents
)
1435 "Interpret CLOCK element as Org syntax.
1437 (concat org-clock-string
" "
1438 (org-element-property :value clock
)
1439 (let ((time (org-element-property :time clock
)))
1444 (org-split-string time
":")))))))
1449 (defun org-element-comment-parser (limit affiliated
)
1452 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1453 the buffer position at the beginning of the first affiliated
1454 keyword and CDR is a plist of affiliated keywords along with
1457 Return a list whose CAR is `comment' and CDR is a plist
1458 containing `:begin', `:end', `:value' and `:post-blank'
1461 Assume point is at comment beginning."
1463 (let* ((begin (car affiliated
))
1464 (value (prog2 (looking-at "[ \t]*# ?")
1465 (buffer-substring-no-properties
1466 (match-end 0) (line-end-position))
1469 ;; Get comments ending.
1471 (while (and (< (point) limit
) (looking-at "[ \t]*#\\( \\|$\\)"))
1472 ;; Accumulate lines without leading hash and first
1477 (buffer-substring-no-properties
1478 (match-end 0) (line-end-position))))
1481 (end (progn (goto-char com-end
)
1482 (skip-chars-forward " \r\t\n" limit
)
1483 (if (eobp) (point) (point-at-bol)))))
1489 :post-blank
(count-lines com-end end
))
1490 (cdr affiliated
))))))
1492 (defun org-element-comment-interpreter (comment contents
)
1493 "Interpret COMMENT element as Org syntax.
1495 (replace-regexp-in-string "^" "# " (org-element-property :value comment
)))
1500 (defun org-element-comment-block-parser (limit affiliated
)
1501 "Parse an export block.
1503 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1504 the buffer position at the beginning of the first affiliated
1505 keyword and CDR is a plist of affiliated keywords along with
1508 Return a list whose CAR is `comment-block' and CDR is a plist
1509 containing `:begin', `:end', `:hiddenp', `:value' and
1510 `:post-blank' keywords.
1512 Assume point is at comment block beginning."
1513 (let ((case-fold-search t
))
1514 (if (not (save-excursion
1515 (re-search-forward "^[ \t]*#\\+END_COMMENT[ \t]*$" limit t
)))
1516 ;; Incomplete block: parse it as a paragraph.
1517 (org-element-paragraph-parser limit affiliated
)
1518 (let ((contents-end (match-beginning 0)))
1520 (let* ((begin (car affiliated
))
1521 (contents-begin (progn (forward-line) (point)))
1522 (hidden (org-invisible-p2))
1523 (pos-before-blank (progn (goto-char contents-end
)
1526 (end (progn (skip-chars-forward " \r\t\n" limit
)
1527 (if (eobp) (point) (point-at-bol))))
1528 (value (buffer-substring-no-properties
1529 contents-begin contents-end
)))
1530 (list 'comment-block
1536 :post-blank
(count-lines pos-before-blank end
))
1537 (cdr affiliated
)))))))))
1539 (defun org-element-comment-block-interpreter (comment-block contents
)
1540 "Interpret COMMENT-BLOCK element as Org syntax.
1542 (format "#+BEGIN_COMMENT\n%s#+END_COMMENT"
1543 (org-remove-indentation (org-element-property :value comment-block
))))
1548 (defun org-element-example-block-parser (limit affiliated
)
1549 "Parse an example block.
1551 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1552 the buffer position at the beginning of the first affiliated
1553 keyword and CDR is a plist of affiliated keywords along with
1556 Return a list whose CAR is `example-block' and CDR is a plist
1557 containing `:begin', `:end', `:number-lines', `:preserve-indent',
1558 `:retain-labels', `:use-labels', `:label-fmt', `:hiddenp',
1559 `:switches', `:value' and `:post-blank' keywords."
1560 (let ((case-fold-search t
))
1561 (if (not (save-excursion
1562 (re-search-forward "^[ \t]*#\\+END_EXAMPLE[ \t]*$" limit t
)))
1563 ;; Incomplete block: parse it as a paragraph.
1564 (org-element-paragraph-parser limit affiliated
)
1565 (let ((contents-end (match-beginning 0)))
1568 (progn (looking-at "^[ \t]*#\\+BEGIN_EXAMPLE\\(?: +\\(.*\\)\\)?")
1569 (org-match-string-no-properties 1)))
1570 ;; Switches analysis
1571 (number-lines (cond ((not switches
) nil
)
1572 ((string-match "-n\\>" switches
) 'new
)
1573 ((string-match "+n\\>" switches
) 'continued
)))
1574 (preserve-indent (and switches
(string-match "-i\\>" switches
)))
1575 ;; Should labels be retained in (or stripped from) example
1579 (not (string-match "-r\\>" switches
))
1580 (and number-lines
(string-match "-k\\>" switches
))))
1581 ;; What should code-references use - labels or
1585 (and retain-labels
(not (string-match "-k\\>" switches
)))))
1586 (label-fmt (and switches
1587 (string-match "-l +\"\\([^\"\n]+\\)\"" switches
)
1588 (match-string 1 switches
)))
1589 ;; Standard block parsing.
1590 (begin (car affiliated
))
1591 (contents-begin (progn (forward-line) (point)))
1592 (hidden (org-invisible-p2))
1593 (value (buffer-substring-no-properties contents-begin contents-end
))
1594 (pos-before-blank (progn (goto-char contents-end
)
1597 (end (progn (skip-chars-forward " \r\t\n" limit
)
1598 (if (eobp) (point) (point-at-bol)))))
1599 (list 'example-block
1605 :number-lines number-lines
1606 :preserve-indent preserve-indent
1607 :retain-labels retain-labels
1608 :use-labels use-labels
1609 :label-fmt label-fmt
1611 :post-blank
(count-lines pos-before-blank end
))
1612 (cdr affiliated
)))))))))
1614 (defun org-element-example-block-interpreter (example-block contents
)
1615 "Interpret EXAMPLE-BLOCK element as Org syntax.
1617 (let ((switches (org-element-property :switches example-block
)))
1618 (concat "#+BEGIN_EXAMPLE" (and switches
(concat " " switches
)) "\n"
1619 (org-remove-indentation
1620 (org-element-property :value example-block
))
1626 (defun org-element-export-block-parser (limit affiliated
)
1627 "Parse an export block.
1629 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1630 the buffer position at the beginning of the first affiliated
1631 keyword and CDR is a plist of affiliated keywords along with
1634 Return a list whose CAR is `export-block' and CDR is a plist
1635 containing `:begin', `:end', `:type', `:hiddenp', `:value' and
1636 `:post-blank' keywords.
1638 Assume point is at export-block beginning."
1639 (let* ((case-fold-search t
)
1640 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
1641 (upcase (org-match-string-no-properties 1)))))
1642 (if (not (save-excursion
1644 (format "^[ \t]*#\\+END_%s[ \t]*$" type
) limit t
)))
1645 ;; Incomplete block: parse it as a paragraph.
1646 (org-element-paragraph-parser limit affiliated
)
1647 (let ((contents-end (match-beginning 0)))
1649 (let* ((begin (car affiliated
))
1650 (contents-begin (progn (forward-line) (point)))
1651 (hidden (org-invisible-p2))
1652 (pos-before-blank (progn (goto-char contents-end
)
1655 (end (progn (skip-chars-forward " \r\t\n" limit
)
1656 (if (eobp) (point) (point-at-bol))))
1657 (value (buffer-substring-no-properties contents-begin
1666 :post-blank
(count-lines pos-before-blank end
))
1667 (cdr affiliated
)))))))))
1669 (defun org-element-export-block-interpreter (export-block contents
)
1670 "Interpret EXPORT-BLOCK element as Org syntax.
1672 (let ((type (org-element-property :type export-block
)))
1673 (concat (format "#+BEGIN_%s\n" type
)
1674 (org-element-property :value export-block
)
1675 (format "#+END_%s" type
))))
1680 (defun org-element-fixed-width-parser (limit affiliated
)
1681 "Parse a fixed-width section.
1683 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1684 the buffer position at the beginning of the first affiliated
1685 keyword and CDR is a plist of affiliated keywords along with
1688 Return a list whose CAR is `fixed-width' and CDR is a plist
1689 containing `:begin', `:end', `:value' and `:post-blank' keywords.
1691 Assume point is at the beginning of the fixed-width area."
1693 (let* ((begin (car affiliated
))
1697 (while (and (< (point) limit
)
1698 (looking-at "[ \t]*:\\( \\|$\\)"))
1699 ;; Accumulate text without starting colons.
1702 (buffer-substring-no-properties
1703 (match-end 0) (point-at-eol))
1707 (end (progn (skip-chars-forward " \r\t\n" limit
)
1708 (if (eobp) (point) (point-at-bol)))))
1714 :post-blank
(count-lines end-area end
))
1715 (cdr affiliated
))))))
1717 (defun org-element-fixed-width-interpreter (fixed-width contents
)
1718 "Interpret FIXED-WIDTH element as Org syntax.
1720 (replace-regexp-in-string
1721 "^" ": " (substring (org-element-property :value fixed-width
) 0 -
1)))
1724 ;;;; Horizontal Rule
1726 (defun org-element-horizontal-rule-parser (limit affiliated
)
1727 "Parse an horizontal rule.
1729 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1730 the buffer position at the beginning of the first affiliated
1731 keyword and CDR is a plist of affiliated keywords along with
1734 Return a list whose CAR is `horizontal-rule' and CDR is a plist
1735 containing `:begin', `:end' and `:post-blank' keywords."
1737 (let ((begin (car affiliated
))
1738 (post-hr (progn (forward-line) (point)))
1739 (end (progn (skip-chars-forward " \r\t\n" limit
)
1740 (if (eobp) (point) (point-at-bol)))))
1741 (list 'horizontal-rule
1745 :post-blank
(count-lines post-hr end
))
1746 (cdr affiliated
))))))
1748 (defun org-element-horizontal-rule-interpreter (horizontal-rule contents
)
1749 "Interpret HORIZONTAL-RULE element as Org syntax.
1756 (defun org-element-keyword-parser (limit affiliated
)
1757 "Parse a keyword at point.
1759 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1760 the buffer position at the beginning of the first affiliated
1761 keyword and CDR is a plist of affiliated keywords along with
1764 Return a list whose CAR is `keyword' and CDR is a plist
1765 containing `:key', `:value', `:begin', `:end' and `:post-blank'
1768 (let ((case-fold-search t
)
1769 (begin (car affiliated
))
1770 (key (progn (looking-at "[ \t]*#\\+\\(\\S-+*\\):")
1771 (upcase (org-match-string-no-properties 1))))
1772 (value (org-trim (buffer-substring-no-properties
1773 (match-end 0) (point-at-eol))))
1774 (pos-before-blank (progn (forward-line) (point)))
1775 (end (progn (skip-chars-forward " \r\t\n" limit
)
1776 (if (eobp) (point) (point-at-bol)))))
1783 :post-blank
(count-lines pos-before-blank end
))
1784 (cdr affiliated
))))))
1786 (defun org-element-keyword-interpreter (keyword contents
)
1787 "Interpret KEYWORD element as Org syntax.
1790 (org-element-property :key keyword
)
1791 (org-element-property :value keyword
)))
1794 ;;;; Latex Environment
1796 (defun org-element-latex-environment-parser (limit affiliated
)
1797 "Parse a LaTeX environment.
1799 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1800 the buffer position at the beginning of the first affiliated
1801 keyword and CDR is a plist of affiliated keywords along with
1804 Return a list whose CAR is `latex-environment' and CDR is a plist
1805 containing `:begin', `:end', `:value' and `:post-blank'
1808 Assume point is at the beginning of the latex environment."
1810 (let* ((case-fold-search t
)
1811 (code-begin (point))
1812 (begin (car affiliated
))
1813 (env (progn (looking-at "^[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}")
1814 (regexp-quote (match-string 1))))
1816 (progn (re-search-forward
1817 (format "^[ \t]*\\\\end{%s}[ \t]*$" env
) limit t
)
1820 (value (buffer-substring-no-properties code-begin code-end
))
1821 (end (progn (skip-chars-forward " \r\t\n" limit
)
1822 (if (eobp) (point) (point-at-bol)))))
1823 (list 'latex-environment
1828 :post-blank
(count-lines code-end end
))
1829 (cdr affiliated
))))))
1831 (defun org-element-latex-environment-interpreter (latex-environment contents
)
1832 "Interpret LATEX-ENVIRONMENT element as Org syntax.
1834 (org-element-property :value latex-environment
))
1839 (defun org-element-node-property-parser (limit)
1840 "Parse a node-property at point.
1842 LIMIT bounds the search.
1844 Return a list whose CAR is `node-property' and CDR is a plist
1845 containing `:key', `:value', `:begin', `:end' and `:post-blank'
1848 (let ((case-fold-search t
)
1850 (key (progn (looking-at "[ \t]*:\\(.*?\\):[ \t]+\\(.*?\\)[ \t]*$")
1851 (org-match-string-no-properties 1)))
1852 (value (org-match-string-no-properties 2))
1853 (pos-before-blank (progn (forward-line) (point)))
1854 (end (progn (skip-chars-forward " \r\t\n" limit
)
1855 (if (eobp) (point) (point-at-bol)))))
1856 (list 'node-property
1861 :post-blank
(count-lines pos-before-blank end
))))))
1863 (defun org-element-node-property-interpreter (node-property contents
)
1864 "Interpret NODE-PROPERTY element as Org syntax.
1866 (format org-property-format
1867 (format ":%s:" (org-element-property :key node-property
))
1868 (org-element-property :value node-property
)))
1873 (defun org-element-paragraph-parser (limit affiliated
)
1876 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1877 the buffer position at the beginning of the first affiliated
1878 keyword and CDR is a plist of affiliated keywords along with
1881 Return a list whose CAR is `paragraph' and CDR is a plist
1882 containing `:begin', `:end', `:contents-begin' and
1883 `:contents-end' and `:post-blank' keywords.
1885 Assume point is at the beginning of the paragraph."
1887 (let* ((begin (car affiliated
))
1888 (contents-begin (point))
1890 (let ((case-fold-search t
))
1892 (if (not (re-search-forward
1893 org-element-paragraph-separate limit
'm
))
1895 ;; A matching `org-element-paragraph-separate' is not
1896 ;; necessarily the end of the paragraph. In
1897 ;; particular, lines starting with # or : as a first
1898 ;; non-space character are ambiguous. We have check
1899 ;; if they are valid Org syntax (i.e. not an
1900 ;; incomplete keyword).
1904 ;; There's no ambiguity for other symbols or
1905 ;; empty lines: stop here.
1906 (looking-at "[ \t]*\\(?:[^:#]\\|$\\)")
1907 ;; Stop at valid fixed-width areas.
1908 (looking-at "[ \t]*:\\(?: \\|$\\)")
1910 (and (looking-at org-drawer-regexp
)
1913 "^[ \t]*:END:[ \t]*$" limit t
)))
1914 ;; Stop at valid comments.
1915 (looking-at "[ \t]*#\\(?: \\|$\\)")
1916 ;; Stop at valid dynamic blocks.
1917 (and (looking-at org-dblock-start-re
)
1920 "^[ \t]*#\\+END:?[ \t]*$" limit t
)))
1921 ;; Stop at valid blocks.
1923 "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
1926 (format "^[ \t]*#\\+END_%s[ \t]*$"
1929 ;; Stop at valid latex environments.
1931 "^[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}[ \t]*$")
1934 (format "^[ \t]*\\\\end{%s}[ \t]*$"
1937 ;; Stop at valid keywords.
1938 (looking-at "[ \t]*#\\+\\S-+:")
1939 ;; Skip everything else.
1943 (re-search-forward org-element-paragraph-separate
1945 (beginning-of-line)))
1946 (if (= (point) limit
) limit
1947 (goto-char (line-beginning-position)))))
1948 (contents-end (progn (skip-chars-backward " \r\t\n" contents-begin
)
1951 (end (progn (skip-chars-forward " \r\t\n" limit
)
1952 (if (eobp) (point) (point-at-bol)))))
1957 :contents-begin contents-begin
1958 :contents-end contents-end
1959 :post-blank
(count-lines before-blank end
))
1960 (cdr affiliated
))))))
1962 (defun org-element-paragraph-interpreter (paragraph contents
)
1963 "Interpret PARAGRAPH element as Org syntax.
1964 CONTENTS is the contents of the element."
1970 (defun org-element-planning-parser (limit)
1973 LIMIT bounds the search.
1975 Return a list whose CAR is `planning' and CDR is a plist
1976 containing `:closed', `:deadline', `:scheduled', `:begin', `:end'
1977 and `:post-blank' keywords."
1979 (let* ((case-fold-search nil
)
1981 (post-blank (let ((before-blank (progn (forward-line) (point))))
1982 (skip-chars-forward " \r\t\n" limit
)
1983 (unless (eobp) (beginning-of-line))
1984 (count-lines before-blank
(point))))
1986 closed deadline scheduled
)
1988 (while (re-search-forward org-keyword-time-not-clock-regexp
1989 (line-end-position) t
)
1990 (goto-char (match-end 1))
1991 (org-skip-whitespace)
1992 (let ((time (buffer-substring-no-properties
1993 (1+ (point)) (1- (match-end 0))))
1994 (keyword (match-string 1)))
1995 (cond ((equal keyword org-closed-string
) (setq closed time
))
1996 ((equal keyword org-deadline-string
) (setq deadline time
))
1997 (t (setq scheduled time
)))))
1999 (list :closed closed
2001 :scheduled scheduled
2004 :post-blank post-blank
)))))
2006 (defun org-element-planning-interpreter (planning contents
)
2007 "Interpret PLANNING element as Org syntax.
2012 (list (let ((closed (org-element-property :closed planning
)))
2013 (when closed
(concat org-closed-string
" [" closed
"]")))
2014 (let ((deadline (org-element-property :deadline planning
)))
2015 (when deadline
(concat org-deadline-string
" <" deadline
">")))
2016 (let ((scheduled (org-element-property :scheduled planning
)))
2018 (concat org-scheduled-string
" <" scheduled
">")))))
2024 (defun org-element-quote-section-parser (limit)
2025 "Parse a quote section.
2027 LIMIT bounds the search.
2029 Return a list whose CAR is `quote-section' and CDR is a plist
2030 containing `:begin', `:end', `:value' and `:post-blank' keywords.
2032 Assume point is at beginning of the section."
2034 (let* ((begin (point))
2035 (end (progn (org-with-limited-levels (outline-next-heading))
2037 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
2040 (value (buffer-substring-no-properties begin pos-before-blank
)))
2041 (list 'quote-section
2045 :post-blank
(count-lines pos-before-blank end
))))))
2047 (defun org-element-quote-section-interpreter (quote-section contents
)
2048 "Interpret QUOTE-SECTION element as Org syntax.
2050 (org-element-property :value quote-section
))
2055 (defun org-element-src-block-parser (limit affiliated
)
2058 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2059 the buffer position at the beginning of the first affiliated
2060 keyword and CDR is a plist of affiliated keywords along with
2063 Return a list whose CAR is `src-block' and CDR is a plist
2064 containing `:language', `:switches', `:parameters', `:begin',
2065 `:end', `:hiddenp', `:number-lines', `:retain-labels',
2066 `:use-labels', `:label-fmt', `:preserve-indent', `:value' and
2067 `:post-blank' keywords.
2069 Assume point is at the beginning of the block."
2070 (let ((case-fold-search t
))
2071 (if (not (save-excursion (re-search-forward "^[ \t]*#\\+END_SRC[ \t]*$"
2073 ;; Incomplete block: parse it as a paragraph.
2074 (org-element-paragraph-parser limit affiliated
)
2075 (let ((contents-end (match-beginning 0)))
2077 (let* ((begin (car affiliated
))
2078 ;; Get language as a string.
2082 (concat "^[ \t]*#\\+BEGIN_SRC"
2083 "\\(?: +\\(\\S-+\\)\\)?"
2084 "\\(\\(?: +\\(?:-l \".*?\"\\|[-+][A-Za-z]\\)\\)+\\)?"
2086 (org-match-string-no-properties 1)))
2088 (switches (org-match-string-no-properties 2))
2090 (parameters (org-match-string-no-properties 3))
2091 ;; Switches analysis
2092 (number-lines (cond ((not switches
) nil
)
2093 ((string-match "-n\\>" switches
) 'new
)
2094 ((string-match "+n\\>" switches
) 'continued
)))
2095 (preserve-indent (and switches
(string-match "-i\\>" switches
)))
2096 (label-fmt (and switches
2097 (string-match "-l +\"\\([^\"\n]+\\)\"" switches
)
2098 (match-string 1 switches
)))
2099 ;; Should labels be retained in (or stripped from)
2103 (not (string-match "-r\\>" switches
))
2104 (and number-lines
(string-match "-k\\>" switches
))))
2105 ;; What should code-references use - labels or
2109 (and retain-labels
(not (string-match "-k\\>" switches
)))))
2110 ;; Get visibility status.
2111 (hidden (progn (forward-line) (org-invisible-p2)))
2113 (value (buffer-substring-no-properties (point) contents-end
))
2114 (pos-before-blank (progn (goto-char contents-end
)
2117 ;; Get position after ending blank lines.
2118 (end (progn (skip-chars-forward " \r\t\n" limit
)
2119 (if (eobp) (point) (point-at-bol)))))
2122 (list :language language
2123 :switches
(and (org-string-nw-p switches
)
2124 (org-trim switches
))
2125 :parameters
(and (org-string-nw-p parameters
)
2126 (org-trim parameters
))
2129 :number-lines number-lines
2130 :preserve-indent preserve-indent
2131 :retain-labels retain-labels
2132 :use-labels use-labels
2133 :label-fmt label-fmt
2136 :post-blank
(count-lines pos-before-blank end
))
2137 (cdr affiliated
)))))))))
2139 (defun org-element-src-block-interpreter (src-block contents
)
2140 "Interpret SRC-BLOCK element as Org syntax.
2142 (let ((lang (org-element-property :language src-block
))
2143 (switches (org-element-property :switches src-block
))
2144 (params (org-element-property :parameters src-block
))
2145 (value (let ((val (org-element-property :value src-block
)))
2148 (org-src-preserve-indentation val
)
2149 ((zerop org-edit-src-content-indentation
)
2150 (org-remove-indentation val
))
2152 (let ((ind (make-string
2153 org-edit-src-content-indentation
32)))
2154 (replace-regexp-in-string
2155 "\\(^\\)[ \t]*\\S-" ind
2156 (org-remove-indentation val
) nil nil
1)))))))
2157 (concat (format "#+BEGIN_SRC%s\n"
2158 (concat (and lang
(concat " " lang
))
2159 (and switches
(concat " " switches
))
2160 (and params
(concat " " params
))))
2167 (defun org-element-table-parser (limit affiliated
)
2168 "Parse a table at point.
2170 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2171 the buffer position at the beginning of the first affiliated
2172 keyword and CDR is a plist of affiliated keywords along with
2175 Return a list whose CAR is `table' and CDR is a plist containing
2176 `:begin', `:end', `:tblfm', `:type', `:contents-begin',
2177 `:contents-end', `:value' and `:post-blank' keywords.
2179 Assume point is at the beginning of the table."
2181 (let* ((case-fold-search t
)
2182 (table-begin (point))
2183 (type (if (org-at-table.el-p
) 'table.el
'org
))
2184 (keywords (org-element--collect-affiliated-keywords))
2185 (begin (car affiliated
))
2186 (table-end (goto-char (marker-position (org-table-end t
))))
2188 (while (looking-at "[ \t]*#\\+TBLFM: +\\(.*\\)[ \t]*$")
2189 (push (org-match-string-no-properties 1) acc
)
2192 (pos-before-blank (point))
2193 (end (progn (skip-chars-forward " \r\t\n" limit
)
2194 (if (eobp) (point) (point-at-bol)))))
2201 ;; Only `org' tables have contents. `table.el' tables
2202 ;; use a `:value' property to store raw table as
2204 :contents-begin
(and (eq type
'org
) table-begin
)
2205 :contents-end
(and (eq type
'org
) table-end
)
2206 :value
(and (eq type
'table.el
)
2207 (buffer-substring-no-properties
2208 table-begin table-end
))
2209 :post-blank
(count-lines pos-before-blank end
))
2210 (cdr affiliated
))))))
2212 (defun org-element-table-interpreter (table contents
)
2213 "Interpret TABLE element as Org syntax.
2215 (if (eq (org-element-property :type table
) 'table.el
)
2216 (org-remove-indentation (org-element-property :value table
))
2217 (concat (with-temp-buffer (insert contents
)
2220 (mapconcat (lambda (fm) (concat "#+TBLFM: " fm
))
2221 (reverse (org-element-property :tblfm table
))
2227 (defun org-element-table-row-parser (limit)
2228 "Parse table row at point.
2230 LIMIT bounds the search.
2232 Return a list whose CAR is `table-row' and CDR is a plist
2233 containing `:begin', `:end', `:contents-begin', `:contents-end',
2234 `:type' and `:post-blank' keywords."
2236 (let* ((type (if (looking-at "^[ \t]*|-") 'rule
'standard
))
2238 ;; A table rule has no contents. In that case, ensure
2239 ;; CONTENTS-BEGIN matches CONTENTS-END.
2240 (contents-begin (and (eq type
'standard
)
2241 (search-forward "|")
2243 (contents-end (and (eq type
'standard
)
2246 (skip-chars-backward " \t")
2248 (end (progn (forward-line) (point))))
2253 :contents-begin contents-begin
2254 :contents-end contents-end
2257 (defun org-element-table-row-interpreter (table-row contents
)
2258 "Interpret TABLE-ROW element as Org syntax.
2259 CONTENTS is the contents of the table row."
2260 (if (eq (org-element-property :type table-row
) 'rule
) "|-"
2261 (concat "| " contents
)))
2266 (defun org-element-verse-block-parser (limit affiliated
)
2267 "Parse a verse block.
2269 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2270 the buffer position at the beginning of the first affiliated
2271 keyword and CDR is a plist of affiliated keywords along with
2274 Return a list whose CAR is `verse-block' and CDR is a plist
2275 containing `:begin', `:end', `:contents-begin', `:contents-end',
2276 `:hiddenp' and `:post-blank' keywords.
2278 Assume point is at beginning of the block."
2279 (let ((case-fold-search t
))
2280 (if (not (save-excursion
2281 (re-search-forward "^[ \t]*#\\+END_VERSE[ \t]*$" limit t
)))
2282 ;; Incomplete block: parse it as a paragraph.
2283 (org-element-paragraph-parser limit affiliated
)
2284 (let ((contents-end (match-beginning 0)))
2286 (let* ((begin (car affiliated
))
2287 (hidden (progn (forward-line) (org-invisible-p2)))
2288 (contents-begin (point))
2289 (pos-before-blank (progn (goto-char contents-end
)
2292 (end (progn (skip-chars-forward " \r\t\n" limit
)
2293 (if (eobp) (point) (point-at-bol)))))
2298 :contents-begin contents-begin
2299 :contents-end contents-end
2301 :post-blank
(count-lines pos-before-blank end
))
2302 (cdr affiliated
)))))))))
2304 (defun org-element-verse-block-interpreter (verse-block contents
)
2305 "Interpret VERSE-BLOCK element as Org syntax.
2306 CONTENTS is verse block contents."
2307 (format "#+BEGIN_VERSE\n%s#+END_VERSE" contents
))
2313 ;; Unlike to elements, interstices can be found between objects.
2314 ;; That's why, along with the parser, successor functions are provided
2315 ;; for each object. Some objects share the same successor (i.e. `code'
2316 ;; and `verbatim' objects).
2318 ;; A successor must accept a single argument bounding the search. It
2319 ;; will return either a cons cell whose CAR is the object's type, as
2320 ;; a symbol, and CDR the position of its next occurrence, or nil.
2322 ;; Successors follow the naming convention:
2323 ;; org-element-NAME-successor, where NAME is the name of the
2324 ;; successor, as defined in `org-element-all-successors'.
2326 ;; Some object types (i.e. `italic') are recursive. Restrictions on
2327 ;; object types they can contain will be specified in
2328 ;; `org-element-object-restrictions'.
2330 ;; Adding a new type of object is simple. Implement a successor,
2331 ;; a parser, and an interpreter for it, all following the naming
2332 ;; convention. Register type in `org-element-all-objects' and
2333 ;; successor in `org-element-all-successors'. Maybe tweak
2334 ;; restrictions about it, and that's it.
2339 (defun org-element-bold-parser ()
2340 "Parse bold object at point.
2342 Return a list whose CAR is `bold' and CDR is a plist with
2343 `:begin', `:end', `:contents-begin' and `:contents-end' and
2344 `:post-blank' keywords.
2346 Assume point is at the first star marker."
2348 (unless (bolp) (backward-char 1))
2349 (looking-at org-emph-re
)
2350 (let ((begin (match-beginning 2))
2351 (contents-begin (match-beginning 4))
2352 (contents-end (match-end 4))
2353 (post-blank (progn (goto-char (match-end 2))
2354 (skip-chars-forward " \t")))
2359 :contents-begin contents-begin
2360 :contents-end contents-end
2361 :post-blank post-blank
)))))
2363 (defun org-element-bold-interpreter (bold contents
)
2364 "Interpret BOLD object as Org syntax.
2365 CONTENTS is the contents of the object."
2366 (format "*%s*" contents
))
2368 (defun org-element-text-markup-successor (limit)
2369 "Search for the next text-markup object.
2371 LIMIT bounds the search.
2373 Return value is a cons cell whose CAR is a symbol among `bold',
2374 `italic', `underline', `strike-through', `code' and `verbatim'
2375 and CDR is beginning position."
2377 (unless (bolp) (backward-char))
2378 (when (re-search-forward org-emph-re limit t
)
2379 (let ((marker (match-string 3)))
2381 ((equal marker
"*") 'bold
)
2382 ((equal marker
"/") 'italic
)
2383 ((equal marker
"_") 'underline
)
2384 ((equal marker
"+") 'strike-through
)
2385 ((equal marker
"~") 'code
)
2386 ((equal marker
"=") 'verbatim
)
2387 (t (error "Unknown marker at %d" (match-beginning 3))))
2388 (match-beginning 2))))))
2393 (defun org-element-code-parser ()
2394 "Parse code object at point.
2396 Return a list whose CAR is `code' and CDR is a plist with
2397 `:value', `:begin', `:end' and `:post-blank' keywords.
2399 Assume point is at the first tilde marker."
2401 (unless (bolp) (backward-char 1))
2402 (looking-at org-emph-re
)
2403 (let ((begin (match-beginning 2))
2404 (value (org-match-string-no-properties 4))
2405 (post-blank (progn (goto-char (match-end 2))
2406 (skip-chars-forward " \t")))
2412 :post-blank post-blank
)))))
2414 (defun org-element-code-interpreter (code contents
)
2415 "Interpret CODE object as Org syntax.
2417 (format "~%s~" (org-element-property :value code
)))
2422 (defun org-element-entity-parser ()
2423 "Parse entity at point.
2425 Return a list whose CAR is `entity' and CDR a plist with
2426 `:begin', `:end', `:latex', `:latex-math-p', `:html', `:latin1',
2427 `:utf-8', `:ascii', `:use-brackets-p' and `:post-blank' as
2430 Assume point is at the beginning of the entity."
2432 (looking-at "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)")
2433 (let* ((value (org-entity-get (match-string 1)))
2434 (begin (match-beginning 0))
2435 (bracketsp (string= (match-string 2) "{}"))
2436 (post-blank (progn (goto-char (match-end 1))
2437 (when bracketsp
(forward-char 2))
2438 (skip-chars-forward " \t")))
2441 (list :name
(car value
)
2442 :latex
(nth 1 value
)
2443 :latex-math-p
(nth 2 value
)
2445 :ascii
(nth 4 value
)
2446 :latin1
(nth 5 value
)
2447 :utf-8
(nth 6 value
)
2450 :use-brackets-p bracketsp
2451 :post-blank post-blank
)))))
2453 (defun org-element-entity-interpreter (entity contents
)
2454 "Interpret ENTITY object as Org syntax.
2457 (org-element-property :name entity
)
2458 (when (org-element-property :use-brackets-p entity
) "{}")))
2460 (defun org-element-latex-or-entity-successor (limit)
2461 "Search for the next latex-fragment or entity object.
2463 LIMIT bounds the search.
2465 Return value is a cons cell whose CAR is `entity' or
2466 `latex-fragment' and CDR is beginning position."
2469 (remove "begin" (plist-get org-format-latex-options
:matchers
)))
2470 ;; ENTITY-RE matches both LaTeX commands and Org entities.
2472 "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))
2473 (when (re-search-forward
2474 (concat (mapconcat (lambda (e) (nth 1 (assoc e org-latex-regexps
)))
2478 (goto-char (match-beginning 0))
2479 (if (looking-at entity-re
)
2480 ;; Determine if it's a real entity or a LaTeX command.
2481 (cons (if (org-entity-get (match-string 1)) 'entity
'latex-fragment
)
2482 (match-beginning 0))
2483 ;; No entity nor command: point is at a LaTeX fragment.
2484 ;; Determine its type to get the correct beginning position.
2485 (cons 'latex-fragment
2488 (when (looking-at (nth 1 (assoc e org-latex-regexps
)))
2491 (nth 2 (assoc e org-latex-regexps
))))))
2498 (defun org-element-export-snippet-parser ()
2499 "Parse export snippet at point.
2501 Return a list whose CAR is `export-snippet' and CDR a plist with
2502 `:begin', `:end', `:back-end', `:value' and `:post-blank' as
2505 Assume point is at the beginning of the snippet."
2507 (re-search-forward "@@\\([-A-Za-z0-9]+\\):" nil t
)
2508 (let* ((begin (match-beginning 0))
2509 (back-end (org-match-string-no-properties 1))
2510 (value (buffer-substring-no-properties
2512 (progn (re-search-forward "@@" nil t
) (match-beginning 0))))
2513 (post-blank (skip-chars-forward " \t"))
2515 (list 'export-snippet
2516 (list :back-end back-end
2520 :post-blank post-blank
)))))
2522 (defun org-element-export-snippet-interpreter (export-snippet contents
)
2523 "Interpret EXPORT-SNIPPET object as Org syntax.
2526 (org-element-property :back-end export-snippet
)
2527 (org-element-property :value export-snippet
)))
2529 (defun org-element-export-snippet-successor (limit)
2530 "Search for the next export-snippet object.
2532 LIMIT bounds the search.
2534 Return value is a cons cell whose CAR is `export-snippet' and CDR
2535 its beginning position."
2538 (when (and (re-search-forward "@@[-A-Za-z0-9]+:" limit t
)
2539 (setq beg
(match-beginning 0))
2540 (search-forward "@@" limit t
))
2541 (cons 'export-snippet beg
)))))
2544 ;;;; Footnote Reference
2546 (defun org-element-footnote-reference-parser ()
2547 "Parse footnote reference at point.
2549 Return a list whose CAR is `footnote-reference' and CDR a plist
2550 with `:label', `:type', `:inline-definition', `:begin', `:end'
2551 and `:post-blank' as keywords."
2553 (looking-at org-footnote-re
)
2554 (let* ((begin (point))
2555 (label (or (org-match-string-no-properties 2)
2556 (org-match-string-no-properties 3)
2557 (and (match-string 1)
2558 (concat "fn:" (org-match-string-no-properties 1)))))
2559 (type (if (or (not label
) (match-string 1)) 'inline
'standard
))
2560 (inner-begin (match-end 0))
2564 (while (and (> count
0) (re-search-forward "[][]" nil t
))
2565 (if (equal (match-string 0) "[") (incf count
) (decf count
)))
2567 (post-blank (progn (goto-char (1+ inner-end
))
2568 (skip-chars-forward " \t")))
2571 (list 'footnote-reference
2576 :post-blank post-blank
))))
2577 (org-element-put-property
2578 footnote-reference
:inline-definition
2579 (and (eq type
'inline
)
2580 (org-element-parse-secondary-string
2581 (buffer-substring inner-begin inner-end
)
2582 (org-element-restriction 'footnote-reference
)
2583 footnote-reference
))))))
2585 (defun org-element-footnote-reference-interpreter (footnote-reference contents
)
2586 "Interpret FOOTNOTE-REFERENCE object as Org syntax.
2588 (let ((label (or (org-element-property :label footnote-reference
) "fn:"))
2591 (org-element-property :inline-definition footnote-reference
)))
2592 (if (not inline-def
) ""
2593 (concat ":" (org-element-interpret-data inline-def
))))))
2594 (format "[%s]" (concat label def
))))
2596 (defun org-element-footnote-reference-successor (limit)
2597 "Search for the next footnote-reference object.
2599 LIMIT bounds the search.
2601 Return value is a cons cell whose CAR is `footnote-reference' and
2602 CDR is beginning position."
2605 (while (re-search-forward org-footnote-re limit t
)
2607 (let ((beg (match-beginning 0))
2610 (while (re-search-forward "[][]" limit t
)
2611 (if (equal (match-string 0) "[") (incf count
) (decf count
))
2613 (throw 'exit
(cons 'footnote-reference beg
))))))))))
2616 ;;;; Inline Babel Call
2618 (defun org-element-inline-babel-call-parser ()
2619 "Parse inline babel call at point.
2621 Return a list whose CAR is `inline-babel-call' and CDR a plist
2622 with `:begin', `:end', `:info' and `:post-blank' as keywords.
2624 Assume point is at the beginning of the babel call."
2626 (unless (bolp) (backward-char))
2627 (looking-at org-babel-inline-lob-one-liner-regexp
)
2628 (let ((info (save-match-data (org-babel-lob-get-info)))
2629 (begin (match-end 1))
2630 (post-blank (progn (goto-char (match-end 0))
2631 (skip-chars-forward " \t")))
2633 (list 'inline-babel-call
2637 :post-blank post-blank
)))))
2639 (defun org-element-inline-babel-call-interpreter (inline-babel-call contents
)
2640 "Interpret INLINE-BABEL-CALL object as Org syntax.
2642 (let* ((babel-info (org-element-property :info inline-babel-call
))
2643 (main-source (car babel-info
))
2644 (post-options (nth 1 babel-info
)))
2646 (if (string-match "\\[\\(\\[.*?\\]\\)\\]" main-source
)
2647 ;; Remove redundant square brackets.
2649 (match-string 1 main-source
) nil nil main-source
)
2651 (and post-options
(format "[%s]" post-options
)))))
2653 (defun org-element-inline-babel-call-successor (limit)
2654 "Search for the next inline-babel-call object.
2656 LIMIT bounds the search.
2658 Return value is a cons cell whose CAR is `inline-babel-call' and
2659 CDR is beginning position."
2661 ;; Use a simplified version of
2662 ;; `org-babel-inline-lob-one-liner-regexp'.
2663 (when (re-search-forward
2664 "call_\\([^()\n]+?\\)\\(?:\\[.*?\\]\\)?([^\n]*?)\\(\\[.*?\\]\\)?"
2666 (cons 'inline-babel-call
(match-beginning 0)))))
2669 ;;;; Inline Src Block
2671 (defun org-element-inline-src-block-parser ()
2672 "Parse inline source block at point.
2674 LIMIT bounds the search.
2676 Return a list whose CAR is `inline-src-block' and CDR a plist
2677 with `:begin', `:end', `:language', `:value', `:parameters' and
2678 `:post-blank' as keywords.
2680 Assume point is at the beginning of the inline src block."
2682 (unless (bolp) (backward-char))
2683 (looking-at org-babel-inline-src-block-regexp
)
2684 (let ((begin (match-beginning 1))
2685 (language (org-match-string-no-properties 2))
2686 (parameters (org-match-string-no-properties 4))
2687 (value (org-match-string-no-properties 5))
2688 (post-blank (progn (goto-char (match-end 0))
2689 (skip-chars-forward " \t")))
2691 (list 'inline-src-block
2692 (list :language language
2694 :parameters parameters
2697 :post-blank post-blank
)))))
2699 (defun org-element-inline-src-block-interpreter (inline-src-block contents
)
2700 "Interpret INLINE-SRC-BLOCK object as Org syntax.
2702 (let ((language (org-element-property :language inline-src-block
))
2703 (arguments (org-element-property :parameters inline-src-block
))
2704 (body (org-element-property :value inline-src-block
)))
2705 (format "src_%s%s{%s}"
2707 (if arguments
(format "[%s]" arguments
) "")
2710 (defun org-element-inline-src-block-successor (limit)
2711 "Search for the next inline-babel-call element.
2713 LIMIT bounds the search.
2715 Return value is a cons cell whose CAR is `inline-babel-call' and
2716 CDR is beginning position."
2718 (unless (bolp) (backward-char))
2719 (when (re-search-forward org-babel-inline-src-block-regexp limit t
)
2720 (cons 'inline-src-block
(match-beginning 1)))))
2724 (defun org-element-italic-parser ()
2725 "Parse italic object at point.
2727 Return a list whose CAR is `italic' and CDR is a plist with
2728 `:begin', `:end', `:contents-begin' and `:contents-end' and
2729 `:post-blank' keywords.
2731 Assume point is at the first slash marker."
2733 (unless (bolp) (backward-char 1))
2734 (looking-at org-emph-re
)
2735 (let ((begin (match-beginning 2))
2736 (contents-begin (match-beginning 4))
2737 (contents-end (match-end 4))
2738 (post-blank (progn (goto-char (match-end 2))
2739 (skip-chars-forward " \t")))
2744 :contents-begin contents-begin
2745 :contents-end contents-end
2746 :post-blank post-blank
)))))
2748 (defun org-element-italic-interpreter (italic contents
)
2749 "Interpret ITALIC object as Org syntax.
2750 CONTENTS is the contents of the object."
2751 (format "/%s/" contents
))
2756 (defun org-element-latex-fragment-parser ()
2757 "Parse latex fragment at point.
2759 Return a list whose CAR is `latex-fragment' and CDR a plist with
2760 `:value', `:begin', `:end', and `:post-blank' as keywords.
2762 Assume point is at the beginning of the latex fragment."
2764 (let* ((begin (point))
2768 (let ((latex-regexp (nth 1 (assoc e org-latex-regexps
))))
2769 (when (or (looking-at latex-regexp
)
2773 (looking-at latex-regexp
))))
2774 (throw 'exit
(nth 2 (assoc e org-latex-regexps
))))))
2775 (plist-get org-format-latex-options
:matchers
))
2776 ;; None found: it's a macro.
2777 (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")
2779 (value (match-string-no-properties substring-match
))
2780 (post-blank (progn (goto-char (match-end substring-match
))
2781 (skip-chars-forward " \t")))
2783 (list 'latex-fragment
2787 :post-blank post-blank
)))))
2789 (defun org-element-latex-fragment-interpreter (latex-fragment contents
)
2790 "Interpret LATEX-FRAGMENT object as Org syntax.
2792 (org-element-property :value latex-fragment
))
2796 (defun org-element-line-break-parser ()
2797 "Parse line break at point.
2799 Return a list whose CAR is `line-break', and CDR a plist with
2800 `:begin', `:end' and `:post-blank' keywords.
2802 Assume point is at the beginning of the line break."
2803 (list 'line-break
(list :begin
(point) :end
(point-at-eol) :post-blank
0)))
2805 (defun org-element-line-break-interpreter (line-break contents
)
2806 "Interpret LINE-BREAK object as Org syntax.
2810 (defun org-element-line-break-successor (limit)
2811 "Search for the next line-break object.
2813 LIMIT bounds the search.
2815 Return value is a cons cell whose CAR is `line-break' and CDR is
2816 beginning position."
2818 (let ((beg (and (re-search-forward "[^\\\\]\\(\\\\\\\\\\)[ \t]*$" limit t
)
2819 (goto-char (match-beginning 1)))))
2820 ;; A line break can only happen on a non-empty line.
2821 (when (and beg
(re-search-backward "\\S-" (point-at-bol) t
))
2822 (cons 'line-break beg
)))))
2827 (defun org-element-link-parser ()
2828 "Parse link at point.
2830 Return a list whose CAR is `link' and CDR a plist with `:type',
2831 `:path', `:raw-link', `:application', `:search-option', `:begin',
2832 `:end', `:contents-begin', `:contents-end' and `:post-blank' as
2835 Assume point is at the beginning of the link."
2837 (let ((begin (point))
2838 end contents-begin contents-end link-end post-blank path type
2839 raw-link link search-option application
)
2841 ;; Type 1: Text targeted from a radio target.
2842 ((and org-target-link-regexp
(looking-at org-target-link-regexp
))
2844 link-end
(match-end 0)
2845 path
(org-match-string-no-properties 0)))
2846 ;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]]
2847 ((looking-at org-bracket-link-regexp
)
2848 (setq contents-begin
(match-beginning 3)
2849 contents-end
(match-end 3)
2850 link-end
(match-end 0)
2851 ;; RAW-LINK is the original link.
2852 raw-link
(org-match-string-no-properties 1)
2853 link
(org-translate-link
2854 (org-link-expand-abbrev
2855 (org-link-unescape raw-link
))))
2856 ;; Determine TYPE of link and set PATH accordingly.
2859 ((or (file-name-absolute-p link
) (string-match "^\\.\\.?/" link
))
2860 (setq type
"file" path link
))
2861 ;; Explicit type (http, irc, bbdb...). See `org-link-types'.
2862 ((string-match org-link-re-with-space3 link
)
2863 (setq type
(match-string 1 link
) path
(match-string 2 link
)))
2864 ;; Id type: PATH is the id.
2865 ((string-match "^id:\\([-a-f0-9]+\\)" link
)
2866 (setq type
"id" path
(match-string 1 link
)))
2867 ;; Code-ref type: PATH is the name of the reference.
2868 ((string-match "^(\\(.*\\))$" link
)
2869 (setq type
"coderef" path
(match-string 1 link
)))
2870 ;; Custom-id type: PATH is the name of the custom id.
2871 ((= (aref link
0) ?
#)
2872 (setq type
"custom-id" path
(substring link
1)))
2873 ;; Fuzzy type: Internal link either matches a target, an
2874 ;; headline name or nothing. PATH is the target or
2876 (t (setq type
"fuzzy" path link
))))
2877 ;; Type 3: Plain link, i.e. http://orgmode.org
2878 ((looking-at org-plain-link-re
)
2879 (setq raw-link
(org-match-string-no-properties 0)
2880 type
(org-match-string-no-properties 1)
2881 path
(org-match-string-no-properties 2)
2882 link-end
(match-end 0)))
2883 ;; Type 4: Angular link, i.e. <http://orgmode.org>
2884 ((looking-at org-angle-link-re
)
2885 (setq raw-link
(buffer-substring-no-properties
2886 (match-beginning 1) (match-end 2))
2887 type
(org-match-string-no-properties 1)
2888 path
(org-match-string-no-properties 2)
2889 link-end
(match-end 0))))
2890 ;; In any case, deduce end point after trailing white space from
2891 ;; LINK-END variable.
2892 (setq post-blank
(progn (goto-char link-end
) (skip-chars-forward " \t"))
2894 ;; Extract search option and opening application out of
2895 ;; "file"-type links.
2896 (when (member type org-element-link-type-is-file
)
2898 (cond ((string-match "^file\\+\\(.*\\)$" type
)
2899 (setq application
(match-string 1 type
)))
2900 ((not (string-match "^file" type
))
2901 (setq application type
)))
2902 ;; Extract search option from PATH.
2903 (when (string-match "::\\(.*\\)$" path
)
2904 (setq search-option
(match-string 1 path
)
2905 path
(replace-match "" nil nil path
)))
2906 ;; Make sure TYPE always report "file".
2911 :raw-link
(or raw-link path
)
2912 :application application
2913 :search-option search-option
2916 :contents-begin contents-begin
2917 :contents-end contents-end
2918 :post-blank post-blank
)))))
2920 (defun org-element-link-interpreter (link contents
)
2921 "Interpret LINK object as Org syntax.
2922 CONTENTS is the contents of the object, or nil."
2923 (let ((type (org-element-property :type link
))
2924 (raw-link (org-element-property :raw-link link
)))
2925 (if (string= type
"radio") raw-link
2928 (if contents
(format "[%s]" contents
) "")))))
2930 (defun org-element-link-successor (limit)
2931 "Search for the next link object.
2933 LIMIT bounds the search.
2935 Return value is a cons cell whose CAR is `link' and CDR is
2936 beginning position."
2939 (if (not org-target-link-regexp
) org-any-link-re
2940 (concat org-any-link-re
"\\|" org-target-link-regexp
))))
2941 (when (re-search-forward link-regexp limit t
)
2942 (cons 'link
(match-beginning 0))))))
2947 (defun org-element-macro-parser ()
2948 "Parse macro at point.
2950 Return a list whose CAR is `macro' and CDR a plist with `:key',
2951 `:args', `:begin', `:end', `:value' and `:post-blank' as
2954 Assume point is at the macro."
2956 (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
2957 (let ((begin (point))
2958 (key (downcase (org-match-string-no-properties 1)))
2959 (value (org-match-string-no-properties 0))
2960 (post-blank (progn (goto-char (match-end 0))
2961 (skip-chars-forward " \t")))
2963 (args (let ((args (org-match-string-no-properties 3)) args2
)
2965 (setq args
(org-split-string args
","))
2967 (while (string-match "\\\\\\'" (car args
))
2968 ;; Repair bad splits.
2969 (setcar (cdr args
) (concat (substring (car args
) 0 -
1)
2972 (push (pop args
) args2
))
2973 (mapcar 'org-trim
(nreverse args2
))))))
2980 :post-blank post-blank
)))))
2982 (defun org-element-macro-interpreter (macro contents
)
2983 "Interpret MACRO object as Org syntax.
2985 (org-element-property :value macro
))
2987 (defun org-element-macro-successor (limit)
2988 "Search for the next macro object.
2990 LIMIT bounds the search.
2992 Return value is cons cell whose CAR is `macro' and CDR is
2993 beginning position."
2995 (when (re-search-forward
2996 "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
2998 (cons 'macro
(match-beginning 0)))))
3003 (defun org-element-radio-target-parser ()
3004 "Parse radio target at point.
3006 Return a list whose CAR is `radio-target' and CDR a plist with
3007 `:begin', `:end', `:contents-begin', `:contents-end', `:value'
3008 and `:post-blank' as keywords.
3010 Assume point is at the radio target."
3012 (looking-at org-radio-target-regexp
)
3013 (let ((begin (point))
3014 (contents-begin (match-beginning 1))
3015 (contents-end (match-end 1))
3016 (value (org-match-string-no-properties 1))
3017 (post-blank (progn (goto-char (match-end 0))
3018 (skip-chars-forward " \t")))
3023 :contents-begin contents-begin
3024 :contents-end contents-end
3025 :post-blank post-blank
3028 (defun org-element-radio-target-interpreter (target contents
)
3029 "Interpret TARGET object as Org syntax.
3030 CONTENTS is the contents of the object."
3031 (concat "<<<" contents
">>>"))
3033 (defun org-element-radio-target-successor (limit)
3034 "Search for the next radio-target object.
3036 LIMIT bounds the search.
3038 Return value is a cons cell whose CAR is `radio-target' and CDR
3039 is beginning position."
3041 (when (re-search-forward org-radio-target-regexp limit t
)
3042 (cons 'radio-target
(match-beginning 0)))))
3045 ;;;; Statistics Cookie
3047 (defun org-element-statistics-cookie-parser ()
3048 "Parse statistics cookie at point.
3050 Return a list whose CAR is `statistics-cookie', and CDR a plist
3051 with `:begin', `:end', `:value' and `:post-blank' keywords.
3053 Assume point is at the beginning of the statistics-cookie."
3055 (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
3056 (let* ((begin (point))
3057 (value (buffer-substring-no-properties
3058 (match-beginning 0) (match-end 0)))
3059 (post-blank (progn (goto-char (match-end 0))
3060 (skip-chars-forward " \t")))
3062 (list 'statistics-cookie
3066 :post-blank post-blank
)))))
3068 (defun org-element-statistics-cookie-interpreter (statistics-cookie contents
)
3069 "Interpret STATISTICS-COOKIE object as Org syntax.
3071 (org-element-property :value statistics-cookie
))
3073 (defun org-element-statistics-cookie-successor (limit)
3074 "Search for the next statistics cookie object.
3076 LIMIT bounds the search.
3078 Return value is a cons cell whose CAR is `statistics-cookie' and
3079 CDR is beginning position."
3081 (when (re-search-forward "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]" limit t
)
3082 (cons 'statistics-cookie
(match-beginning 0)))))
3087 (defun org-element-strike-through-parser ()
3088 "Parse strike-through object at point.
3090 Return a list whose CAR is `strike-through' and CDR is a plist
3091 with `:begin', `:end', `:contents-begin' and `:contents-end' and
3092 `:post-blank' keywords.
3094 Assume point is at the first plus sign marker."
3096 (unless (bolp) (backward-char 1))
3097 (looking-at org-emph-re
)
3098 (let ((begin (match-beginning 2))
3099 (contents-begin (match-beginning 4))
3100 (contents-end (match-end 4))
3101 (post-blank (progn (goto-char (match-end 2))
3102 (skip-chars-forward " \t")))
3104 (list 'strike-through
3107 :contents-begin contents-begin
3108 :contents-end contents-end
3109 :post-blank post-blank
)))))
3111 (defun org-element-strike-through-interpreter (strike-through contents
)
3112 "Interpret STRIKE-THROUGH object as Org syntax.
3113 CONTENTS is the contents of the object."
3114 (format "+%s+" contents
))
3119 (defun org-element-subscript-parser ()
3120 "Parse subscript at point.
3122 Return a list whose CAR is `subscript' and CDR a plist with
3123 `:begin', `:end', `:contents-begin', `:contents-end',
3124 `:use-brackets-p' and `:post-blank' as keywords.
3126 Assume point is at the underscore."
3128 (unless (bolp) (backward-char))
3129 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp
)
3131 (not (looking-at org-match-substring-regexp
))))
3132 (begin (match-beginning 2))
3133 (contents-begin (or (match-beginning 5)
3134 (match-beginning 3)))
3135 (contents-end (or (match-end 5) (match-end 3)))
3136 (post-blank (progn (goto-char (match-end 0))
3137 (skip-chars-forward " \t")))
3142 :use-brackets-p bracketsp
3143 :contents-begin contents-begin
3144 :contents-end contents-end
3145 :post-blank post-blank
)))))
3147 (defun org-element-subscript-interpreter (subscript contents
)
3148 "Interpret SUBSCRIPT object as Org syntax.
3149 CONTENTS is the contents of the object."
3151 (if (org-element-property :use-brackets-p subscript
) "_{%s}" "_%s")
3154 (defun org-element-sub/superscript-successor
(limit)
3155 "Search for the next sub/superscript object.
3157 LIMIT bounds the search.
3159 Return value is a cons cell whose CAR is either `subscript' or
3160 `superscript' and CDR is beginning position."
3162 (when (re-search-forward org-match-substring-regexp limit t
)
3163 (cons (if (string= (match-string 2) "_") 'subscript
'superscript
)
3164 (match-beginning 2)))))
3169 (defun org-element-superscript-parser ()
3170 "Parse superscript at point.
3172 Return a list whose CAR is `superscript' and CDR a plist with
3173 `:begin', `:end', `:contents-begin', `:contents-end',
3174 `:use-brackets-p' and `:post-blank' as keywords.
3176 Assume point is at the caret."
3178 (unless (bolp) (backward-char))
3179 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp
) t
3180 (not (looking-at org-match-substring-regexp
))))
3181 (begin (match-beginning 2))
3182 (contents-begin (or (match-beginning 5)
3183 (match-beginning 3)))
3184 (contents-end (or (match-end 5) (match-end 3)))
3185 (post-blank (progn (goto-char (match-end 0))
3186 (skip-chars-forward " \t")))
3191 :use-brackets-p bracketsp
3192 :contents-begin contents-begin
3193 :contents-end contents-end
3194 :post-blank post-blank
)))))
3196 (defun org-element-superscript-interpreter (superscript contents
)
3197 "Interpret SUPERSCRIPT object as Org syntax.
3198 CONTENTS is the contents of the object."
3200 (if (org-element-property :use-brackets-p superscript
) "^{%s}" "^%s")
3206 (defun org-element-table-cell-parser ()
3207 "Parse table cell at point.
3209 Return a list whose CAR is `table-cell' and CDR is a plist
3210 containing `:begin', `:end', `:contents-begin', `:contents-end'
3211 and `:post-blank' keywords."
3212 (looking-at "[ \t]*\\(.*?\\)[ \t]*|")
3213 (let* ((begin (match-beginning 0))
3215 (contents-begin (match-beginning 1))
3216 (contents-end (match-end 1)))
3220 :contents-begin contents-begin
3221 :contents-end contents-end
3224 (defun org-element-table-cell-interpreter (table-cell contents
)
3225 "Interpret TABLE-CELL element as Org syntax.
3226 CONTENTS is the contents of the cell, or nil."
3227 (concat " " contents
" |"))
3229 (defun org-element-table-cell-successor (limit)
3230 "Search for the next table-cell object.
3232 LIMIT bounds the search.
3234 Return value is a cons cell whose CAR is `table-cell' and CDR is
3235 beginning position."
3236 (when (looking-at "[ \t]*.*?[ \t]+|") (cons 'table-cell
(point))))
3241 (defun org-element-target-parser ()
3242 "Parse target at point.
3244 Return a list whose CAR is `target' and CDR a plist with
3245 `:begin', `:end', `:value' and `:post-blank' as keywords.
3247 Assume point is at the target."
3249 (looking-at org-target-regexp
)
3250 (let ((begin (point))
3251 (value (org-match-string-no-properties 1))
3252 (post-blank (progn (goto-char (match-end 0))
3253 (skip-chars-forward " \t")))
3259 :post-blank post-blank
)))))
3261 (defun org-element-target-interpreter (target contents
)
3262 "Interpret TARGET object as Org syntax.
3264 (format "<<%s>>" (org-element-property :value target
)))
3266 (defun org-element-target-successor (limit)
3267 "Search for the next target object.
3269 LIMIT bounds the search.
3271 Return value is a cons cell whose CAR is `target' and CDR is
3272 beginning position."
3274 (when (re-search-forward org-target-regexp limit t
)
3275 (cons 'target
(match-beginning 0)))))
3280 (defun org-element-timestamp-parser ()
3281 "Parse time stamp at point.
3283 Return a list whose CAR is `timestamp', and CDR a plist with
3284 `:type', `:begin', `:end', `:value' and `:post-blank' keywords.
3286 Assume point is at the beginning of the timestamp."
3288 (let* ((begin (point))
3289 (activep (eq (char-after) ?
<))
3292 (looking-at "[<[]\\(\\(%%\\)?.*?\\)[]>]\\(?:--[<[]\\(.*?\\)[]>]\\)?")
3293 (match-string-no-properties 1)))
3294 (range-end (match-string-no-properties 3))
3295 (type (cond ((match-string 2) 'diary
)
3296 ((and activep range-end
) 'active-range
)
3298 (range-end 'inactive-range
)
3300 (post-blank (progn (goto-char (match-end 0))
3301 (skip-chars-forward " \t")))
3306 :range-end range-end
3309 :post-blank post-blank
)))))
3311 (defun org-element-timestamp-interpreter (timestamp contents
)
3312 "Interpret TIMESTAMP object as Org syntax.
3314 (let ((type (org-element-property :type timestamp
) ))
3316 (format (if (memq type
'(inactive inactive-range
)) "[%s]" "<%s>")
3317 (org-element-property :value timestamp
))
3318 (let ((range-end (org-element-property :range-end timestamp
)))
3321 (format (if (eq type
'inactive-range
) "[%s]" "<%s>")
3324 (defun org-element-timestamp-successor (limit)
3325 "Search for the next timestamp object.
3327 LIMIT bounds the search.
3329 Return value is a cons cell whose CAR is `timestamp' and CDR is
3330 beginning position."
3332 (when (re-search-forward
3333 (concat org-ts-regexp-both
3335 "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
3337 "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
3339 (cons 'timestamp
(match-beginning 0)))))
3344 (defun org-element-underline-parser ()
3345 "Parse underline object at point.
3347 Return a list whose CAR is `underline' and CDR is a plist with
3348 `:begin', `:end', `:contents-begin' and `:contents-end' and
3349 `:post-blank' keywords.
3351 Assume point is at the first underscore marker."
3353 (unless (bolp) (backward-char 1))
3354 (looking-at org-emph-re
)
3355 (let ((begin (match-beginning 2))
3356 (contents-begin (match-beginning 4))
3357 (contents-end (match-end 4))
3358 (post-blank (progn (goto-char (match-end 2))
3359 (skip-chars-forward " \t")))
3364 :contents-begin contents-begin
3365 :contents-end contents-end
3366 :post-blank post-blank
)))))
3368 (defun org-element-underline-interpreter (underline contents
)
3369 "Interpret UNDERLINE object as Org syntax.
3370 CONTENTS is the contents of the object."
3371 (format "_%s_" contents
))
3376 (defun org-element-verbatim-parser ()
3377 "Parse verbatim object at point.
3379 Return a list whose CAR is `verbatim' and CDR is a plist with
3380 `:value', `:begin', `:end' and `:post-blank' keywords.
3382 Assume point is at the first equal sign marker."
3384 (unless (bolp) (backward-char 1))
3385 (looking-at org-emph-re
)
3386 (let ((begin (match-beginning 2))
3387 (value (org-match-string-no-properties 4))
3388 (post-blank (progn (goto-char (match-end 2))
3389 (skip-chars-forward " \t")))
3395 :post-blank post-blank
)))))
3397 (defun org-element-verbatim-interpreter (verbatim contents
)
3398 "Interpret VERBATIM object as Org syntax.
3400 (format "=%s=" (org-element-property :value verbatim
)))
3404 ;;; Parsing Element Starting At Point
3406 ;; `org-element--current-element' is the core function of this section.
3407 ;; It returns the Lisp representation of the element starting at
3410 ;; `org-element--current-element' makes use of special modes. They
3411 ;; are activated for fixed element chaining (i.e. `plain-list' >
3412 ;; `item') or fixed conditional element chaining (i.e. `headline' >
3413 ;; `section'). Special modes are: `first-section', `item',
3414 ;; `node-property', `quote-section', `section' and `table-row'.
3416 (defun org-element--current-element
3417 (limit &optional granularity special structure
)
3418 "Parse the element starting at point.
3420 LIMIT bounds the search.
3422 Return value is a list like (TYPE PROPS) where TYPE is the type
3423 of the element and PROPS a plist of properties associated to the
3426 Possible types are defined in `org-element-all-elements'.
3428 Optional argument GRANULARITY determines the depth of the
3429 recursion. Allowed values are `headline', `greater-element',
3430 `element', `object' or nil. When it is broader than `object' (or
3431 nil), secondary values will not be parsed, since they only
3434 Optional argument SPECIAL, when non-nil, can be either
3435 `first-section', `item', `node-property', `quote-section',
3436 `section', and `table-row'.
3438 If STRUCTURE isn't provided but SPECIAL is set to `item', it will
3441 This function assumes point is always at the beginning of the
3442 element it has to parse."
3444 (let ((case-fold-search t
)
3445 ;; Determine if parsing depth allows for secondary strings
3446 ;; parsing. It only applies to elements referenced in
3447 ;; `org-element-secondary-value-alist'.
3448 (raw-secondary-p (and granularity
(not (eq granularity
'object
)))))
3452 (org-element-item-parser limit structure raw-secondary-p
))
3454 ((eq special
'table-row
) (org-element-table-row-parser limit
))
3456 ((eq special
'node-property
) (org-element-node-property-parser limit
))
3458 ((org-with-limited-levels (org-at-heading-p))
3459 (org-element-headline-parser limit raw-secondary-p
))
3460 ;; Sections (must be checked after headline).
3461 ((eq special
'section
) (org-element-section-parser limit
))
3462 ((eq special
'quote-section
) (org-element-quote-section-parser limit
))
3463 ((eq special
'first-section
)
3464 (org-element-section-parser
3465 (or (save-excursion (org-with-limited-levels (outline-next-heading)))
3467 ;; When not at bol, point is at the beginning of an item or
3468 ;; a footnote definition: next item is always a paragraph.
3469 ((not (bolp)) (org-element-paragraph-parser limit
(list (point))))
3470 ;; Planning and Clock.
3471 ((and (looking-at org-planning-or-clock-line-re
))
3472 (if (equal (match-string 1) org-clock-string
)
3473 (org-element-clock-parser limit
)
3474 (org-element-planning-parser limit
)))
3477 (org-element-inlinetask-parser limit raw-secondary-p
))
3478 ;; From there, elements can have affiliated keywords.
3479 (t (let ((affiliated (org-element--collect-affiliated-keywords)))
3481 ;; LaTeX Environment.
3482 ((looking-at "[ \t]*\\\\begin{\\([A-Za-z0-9*]+\\)}[ \t]*$")
3485 (format "^[ \t]*\\\\end{%s}[ \t]*$"
3486 (regexp-quote (match-string 1)))
3488 (org-element-latex-environment-parser limit affiliated
)
3489 (org-element-paragraph-parser limit affiliated
)))
3490 ;; Drawer and Property Drawer.
3491 ((looking-at org-drawer-regexp
)
3492 (let ((name (match-string 1)))
3494 ((not (save-excursion
3495 (re-search-forward "^[ \t]*:END:[ \t]*$" nil t
)))
3496 (org-element-paragraph-parser limit affiliated
))
3497 ((equal "PROPERTIES" name
)
3498 (org-element-property-drawer-parser limit affiliated
))
3499 (t (org-element-drawer-parser limit affiliated
)))))
3501 ((looking-at "[ \t]*:\\( \\|$\\)")
3502 (org-element-fixed-width-parser limit affiliated
))
3503 ;; Inline Comments, Blocks, Babel Calls, Dynamic Blocks and
3505 ((looking-at "[ \t]*#")
3506 (goto-char (match-end 0))
3507 (cond ((looking-at "\\(?: \\|$\\)")
3509 (org-element-comment-parser limit affiliated
))
3510 ((looking-at "\\+BEGIN_\\(\\S-+\\)")
3512 (let ((parser (assoc (upcase (match-string 1))
3513 org-element-block-name-alist
)))
3514 (if parser
(funcall (cdr parser
) limit affiliated
)
3515 (org-element-special-block-parser limit affiliated
))))
3516 ((looking-at "\\+CALL:")
3518 (org-element-babel-call-parser limit affiliated
))
3519 ((looking-at "\\+BEGIN:? ")
3521 (org-element-dynamic-block-parser limit affiliated
))
3522 ((looking-at "\\+\\S-+:")
3524 (org-element-keyword-parser limit affiliated
))
3527 (org-element-paragraph-parser limit affiliated
))))
3528 ;; Footnote Definition.
3529 ((looking-at org-footnote-definition-re
)
3530 (org-element-footnote-definition-parser limit affiliated
))
3532 ((looking-at "[ \t]*-\\{5,\\}[ \t]*$")
3533 (org-element-horizontal-rule-parser limit affiliated
))
3535 ((org-at-table-p t
) (org-element-table-parser limit affiliated
))
3537 ((looking-at (org-item-re))
3538 (org-element-plain-list-parser
3539 limit affiliated
(or structure
(org-list-struct))))
3540 ;; Default element: Paragraph.
3541 (t (org-element-paragraph-parser limit affiliated
)))))))))
3544 ;; Most elements can have affiliated keywords. When looking for an
3545 ;; element beginning, we want to move before them, as they belong to
3546 ;; that element, and, in the meantime, collect information they give
3547 ;; into appropriate properties. Hence the following function.
3549 (defun org-element--collect-affiliated-keywords ()
3550 "Collect affiliated keywords from point.
3552 Return a list whose CAR is the position at the first of them and
3553 CDR a plist of keywords and values and move point to the
3554 beginning of the first line after them.
3556 As a special case, if element doesn't start at the beginning of
3557 the line (i.e. a paragraph starting an item), CAR is current
3558 position of point and CDR is nil."
3559 (if (not (bolp)) (list (point))
3560 (let ((case-fold-search t
)
3562 ;; RESTRICT is the list of objects allowed in parsed
3564 (restrict (org-element-restriction 'keyword
))
3566 (while (and (not (eobp)) (looking-at org-element--affiliated-re
))
3567 (let* ((raw-kwd (upcase (match-string 1)))
3568 ;; Apply translation to RAW-KWD. From there, KWD is
3569 ;; the official keyword.
3570 (kwd (or (cdr (assoc raw-kwd
3571 org-element-keyword-translation-alist
))
3573 ;; Find main value for any keyword.
3577 (buffer-substring-no-properties
3578 (match-end 0) (point-at-eol)))))
3579 ;; PARSEDP is non-nil when keyword should have its
3581 (parsedp (member kwd org-element-parsed-keywords
))
3582 ;; If KWD is a dual keyword, find its secondary
3583 ;; value. Maybe parse it.
3584 (dualp (member kwd org-element-dual-keywords
))
3587 (let ((sec (org-match-string-no-properties 2)))
3588 (if (or (not sec
) (not parsedp
)) sec
3589 (org-element-parse-secondary-string sec restrict
)))))
3590 ;; Attribute a property name to KWD.
3591 (kwd-sym (and kwd
(intern (concat ":" (downcase kwd
))))))
3592 ;; Now set final shape for VALUE.
3594 (setq value
(org-element-parse-secondary-string value restrict
)))
3595 (when dualp
(setq value
(and value
(cons value dual-value
))))
3596 (when (or (member kwd org-element-multiple-keywords
)
3597 ;; Attributes can always appear on multiple lines.
3598 (string-match "^ATTR_" kwd
))
3599 (setq value
(cons value
(plist-get output kwd-sym
))))
3600 ;; Eventually store the new value in OUTPUT.
3601 (setq output
(plist-put output kwd-sym value
))
3602 ;; Move to next keyword.
3604 ;; If affiliated keywords are orphaned: move back to first one.
3605 ;; They will be parsed as a paragraph.
3606 (when (looking-at "[ \t]*$") (goto-char origin
) (setq output nil
))
3608 (cons origin output
))))
3614 ;; The two major functions here are `org-element-parse-buffer', which
3615 ;; parses Org syntax inside the current buffer, taking into account
3616 ;; region, narrowing, or even visibility if specified, and
3617 ;; `org-element-parse-secondary-string', which parses objects within
3620 ;; The (almost) almighty `org-element-map' allows to apply a function
3621 ;; on elements or objects matching some type, and accumulate the
3622 ;; resulting values. In an export situation, it also skips unneeded
3623 ;; parts of the parse tree.
3625 (defun org-element-parse-buffer (&optional granularity visible-only
)
3626 "Recursively parse the buffer and return structure.
3627 If narrowing is in effect, only parse the visible part of the
3630 Optional argument GRANULARITY determines the depth of the
3631 recursion. It can be set to the following symbols:
3633 `headline' Only parse headlines.
3634 `greater-element' Don't recurse into greater elements excepted
3635 headlines and sections. Thus, elements
3636 parsed are the top-level ones.
3637 `element' Parse everything but objects and plain text.
3638 `object' Parse the complete buffer (default).
3640 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3643 Assume buffer is in Org mode."
3645 (goto-char (point-min))
3646 (org-skip-whitespace)
3647 (org-element--parse-elements
3648 (point-at-bol) (point-max)
3649 ;; Start in `first-section' mode so text before the first
3650 ;; headline belongs to a section.
3651 'first-section nil granularity visible-only
(list 'org-data nil
))))
3653 (defun org-element-parse-secondary-string (string restriction
&optional parent
)
3654 "Recursively parse objects in STRING and return structure.
3656 RESTRICTION is a symbol limiting the object types that will be
3659 Optional argument PARENT, when non-nil, is the element or object
3660 containing the secondary string. It is used to set correctly
3661 `:parent' property within the string."
3664 (let ((secondary (org-element--parse-objects
3665 (point-min) (point-max) nil restriction
)))
3666 (mapc (lambda (obj) (org-element-put-property obj
:parent parent
))
3669 (defun org-element-map (data types fun
&optional info first-match no-recursion
)
3670 "Map a function on selected elements or objects.
3672 DATA is the parsed tree, as returned by, i.e,
3673 `org-element-parse-buffer'. TYPES is a symbol or list of symbols
3674 of elements or objects types. FUN is the function called on the
3675 matching element or object. It must accept one arguments: the
3676 element or object itself.
3678 When optional argument INFO is non-nil, it should be a plist
3679 holding export options. In that case, parts of the parse tree
3680 not exportable according to that property list will be skipped.
3682 When optional argument FIRST-MATCH is non-nil, stop at the first
3683 match for which FUN doesn't return nil, and return that value.
3685 Optional argument NO-RECURSION is a symbol or a list of symbols
3686 representing elements or objects types. `org-element-map' won't
3687 enter any recursive element or object whose type belongs to that
3688 list. Though, FUN can still be applied on them.
3690 Nil values returned from FUN do not appear in the results."
3691 ;; Ensure TYPES and NO-RECURSION are a list, even of one element.
3692 (unless (listp types
) (setq types
(list types
)))
3693 (unless (listp no-recursion
) (setq no-recursion
(list no-recursion
)))
3694 ;; Recursion depth is determined by --CATEGORY.
3697 (let ((category 'greater-elements
))
3698 (mapc (lambda (type)
3699 (cond ((or (memq type org-element-all-objects
)
3700 (eq type
'plain-text
))
3701 ;; If one object is found, the function
3702 ;; has to recurse into every object.
3703 (throw 'found
'objects
))
3704 ((not (memq type org-element-greater-elements
))
3705 ;; If one regular element is found, the
3706 ;; function has to recurse, at least,
3707 ;; into every element it encounters.
3708 (and (not (eq category
'elements
))
3709 (setq category
'elements
)))))
3717 ;; Recursively walk DATA. INFO, if non-nil, is a plist
3718 ;; holding contextual information.
3719 (let ((--type (org-element-type --data
)))
3722 ;; Ignored element in an export context.
3723 ((and info
(memq --data
(plist-get info
:ignore-list
))))
3724 ;; Secondary string: only objects can be found there.
3726 (when (eq --category
'objects
) (mapc --walk-tree --data
)))
3727 ;; Unconditionally enter parse trees.
3728 ((eq --type
'org-data
)
3729 (mapc --walk-tree
(org-element-contents --data
)))
3731 ;; Check if TYPE is matching among TYPES. If so,
3732 ;; apply FUN to --DATA and accumulate return value
3733 ;; into --ACC (or exit if FIRST-MATCH is non-nil).
3734 (when (memq --type types
)
3735 (let ((result (funcall fun --data
)))
3736 (cond ((not result
))
3737 (first-match (throw '--map-first-match result
))
3738 (t (push result --acc
)))))
3739 ;; If --DATA has a secondary string that can contain
3740 ;; objects with their type among TYPES, look into it.
3741 (when (eq --category
'objects
)
3743 (assq --type org-element-secondary-value-alist
)))
3745 (funcall --walk-tree
3746 (org-element-property (cdr sec-prop
) --data
)))))
3747 ;; Determine if a recursion into --DATA is possible.
3749 ;; --TYPE is explicitly removed from recursion.
3750 ((memq --type no-recursion
))
3751 ;; --DATA has no contents.
3752 ((not (org-element-contents --data
)))
3753 ;; Looking for greater elements but --DATA is simply
3754 ;; an element or an object.
3755 ((and (eq --category
'greater-elements
)
3756 (not (memq --type org-element-greater-elements
))))
3757 ;; Looking for elements but --DATA is an object.
3758 ((and (eq --category
'elements
)
3759 (memq --type org-element-all-objects
)))
3760 ;; In any other case, map contents.
3761 (t (mapc --walk-tree
(org-element-contents --data
)))))))))))
3762 (catch '--map-first-match
3763 (funcall --walk-tree data
)
3764 ;; Return value in a proper order.
3767 ;; The following functions are internal parts of the parser.
3769 ;; The first one, `org-element--parse-elements' acts at the element's
3772 ;; The second one, `org-element--parse-objects' applies on all objects
3773 ;; of a paragraph or a secondary string. It uses
3774 ;; `org-element--get-next-object-candidates' to optimize the search of
3775 ;; the next object in the buffer.
3777 ;; More precisely, that function looks for every allowed object type
3778 ;; first. Then, it discards failed searches, keeps further matches,
3779 ;; and searches again types matched behind point, for subsequent
3780 ;; calls. Thus, searching for a given type fails only once, and every
3781 ;; object is searched only once at top level (but sometimes more for
3784 (defun org-element--parse-elements
3785 (beg end special structure granularity visible-only acc
)
3786 "Parse elements between BEG and END positions.
3788 SPECIAL prioritize some elements over the others. It can be set
3789 to `first-section', `quote-section', `section' `item' or
3792 When value is `item', STRUCTURE will be used as the current list
3795 GRANULARITY determines the depth of the recursion. See
3796 `org-element-parse-buffer' for more information.
3798 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3801 Elements are accumulated into ACC."
3804 ;; When parsing only headlines, skip any text before first one.
3805 (when (and (eq granularity
'headline
) (not (org-at-heading-p)))
3806 (org-with-limited-levels (outline-next-heading)))
3808 (while (< (point) end
)
3809 ;; Find current element's type and parse it accordingly to
3811 (let* ((element (org-element--current-element
3812 end granularity special structure
))
3813 (type (org-element-type element
))
3814 (cbeg (org-element-property :contents-begin element
)))
3815 (goto-char (org-element-property :end element
))
3816 ;; Fill ELEMENT contents by side-effect.
3818 ;; If VISIBLE-ONLY is true and element is hidden or if it has
3819 ;; no contents, don't modify it.
3820 ((or (and visible-only
(org-element-property :hiddenp element
))
3822 ;; Greater element: parse it between `contents-begin' and
3823 ;; `contents-end'. Make sure GRANULARITY allows the
3824 ;; recursion, or ELEMENT is an headline, in which case going
3825 ;; inside is mandatory, in order to get sub-level headings.
3826 ((and (memq type org-element-greater-elements
)
3827 (or (memq granularity
'(element object nil
))
3828 (and (eq granularity
'greater-element
)
3830 (eq type
'headline
)))
3831 (org-element--parse-elements
3832 cbeg
(org-element-property :contents-end element
)
3833 ;; Possibly switch to a special mode.
3836 (if (org-element-property :quotedp element
) 'quote-section
3839 (property-drawer 'node-property
)
3841 (org-element-property :structure element
)
3842 granularity visible-only element
))
3843 ;; ELEMENT has contents. Parse objects inside, if
3844 ;; GRANULARITY allows it.
3845 ((memq granularity
'(object nil
))
3846 (org-element--parse-objects
3847 cbeg
(org-element-property :contents-end element
) element
3848 (org-element-restriction type
))))
3849 (org-element-adopt-elements acc element
)))
3853 (defun org-element--parse-objects (beg end acc restriction
)
3854 "Parse objects between BEG and END and return recursive structure.
3856 Objects are accumulated in ACC.
3858 RESTRICTION is a list of object types which are allowed in the
3863 (while (and (< (point) end
)
3864 (setq candidates
(org-element--get-next-object-candidates
3865 end restriction candidates
)))
3867 (let ((pos (apply 'min
(mapcar 'cdr candidates
))))
3870 (funcall (intern (format "org-element-%s-parser"
3871 (car (rassq pos candidates
)))))))))
3872 ;; 1. Text before any object. Untabify it.
3873 (let ((obj-beg (org-element-property :begin next-object
)))
3874 (unless (= (point) obj-beg
)
3876 (org-element-adopt-elements
3878 (replace-regexp-in-string
3879 "\t" (make-string tab-width ?
)
3880 (buffer-substring-no-properties (point) obj-beg
))))))
3882 (let ((obj-end (org-element-property :end next-object
))
3883 (cont-beg (org-element-property :contents-begin next-object
)))
3884 ;; Fill contents of NEXT-OBJECT by side-effect, if it has
3885 ;; a recursive type.
3887 (memq (car next-object
) org-element-recursive-objects
))
3891 (org-element-property :contents-end next-object
))
3892 (org-element--parse-objects
3893 (point-min) (point-max) next-object
3894 (org-element-restriction next-object
))))
3895 (setq acc
(org-element-adopt-elements acc next-object
))
3896 (goto-char obj-end
))))
3897 ;; 3. Text after last object. Untabify it.
3898 (unless (= (point) end
)
3900 (org-element-adopt-elements
3902 (replace-regexp-in-string
3903 "\t" (make-string tab-width ?
)
3904 (buffer-substring-no-properties (point) end
)))))
3908 (defun org-element--get-next-object-candidates (limit restriction objects
)
3909 "Return an alist of candidates for the next object.
3911 LIMIT bounds the search, and RESTRICTION narrows candidates to
3914 Return value is an alist whose CAR is position and CDR the object
3917 OBJECTS is the previous candidates alist."
3918 (let (next-candidates types-to-search
)
3919 ;; If no previous result, search every object type in RESTRICTION.
3920 ;; Otherwise, keep potential candidates (old objects located after
3921 ;; point) and ask to search again those which had matched before.
3922 (if (not objects
) (setq types-to-search restriction
)
3924 (if (< (cdr obj
) (point)) (push (car obj
) types-to-search
)
3925 (push obj next-candidates
)))
3927 ;; Call the appropriate successor function for each type to search
3928 ;; and accumulate matches.
3931 (let* ((successor-fun
3933 (format "org-element-%s-successor"
3934 (or (cdr (assq type org-element-object-successor-alist
))
3936 (obj (funcall successor-fun limit
)))
3937 (and obj
(push obj next-candidates
))))
3944 ;;; Towards A Bijective Process
3946 ;; The parse tree obtained with `org-element-parse-buffer' is really
3947 ;; a snapshot of the corresponding Org buffer. Therefore, it can be
3948 ;; interpreted and expanded into a string with canonical Org syntax.
3949 ;; Hence `org-element-interpret-data'.
3951 ;; The function relies internally on
3952 ;; `org-element--interpret-affiliated-keywords'.
3955 (defun org-element-interpret-data (data &optional parent
)
3956 "Interpret DATA as Org syntax.
3958 DATA is a parse tree, an element, an object or a secondary string
3961 Optional argument PARENT is used for recursive calls. It contains
3962 the element or object containing data, or nil.
3964 Return Org syntax as a string."
3965 (let* ((type (org-element-type data
))
3968 ;; Secondary string.
3971 (lambda (obj) (org-element-interpret-data obj parent
))
3973 ;; Full Org document.
3974 ((eq type
'org-data
)
3976 (lambda (obj) (org-element-interpret-data obj parent
))
3977 (org-element-contents data
) ""))
3979 ((stringp data
) data
)
3980 ;; Element/Object without contents.
3981 ((not (org-element-contents data
))
3982 (funcall (intern (format "org-element-%s-interpreter" type
))
3984 ;; Element/Object with contents.
3986 (let* ((greaterp (memq type org-element-greater-elements
))
3987 (objectp (and (not greaterp
)
3988 (memq type org-element-recursive-objects
)))
3991 (lambda (obj) (org-element-interpret-data obj data
))
3992 (org-element-contents
3993 (if (or greaterp objectp
) data
3994 ;; Elements directly containing objects must
3995 ;; have their indentation normalized first.
3996 (org-element-normalize-contents
3998 ;; When normalizing first paragraph of an
3999 ;; item or a footnote-definition, ignore
4000 ;; first line's indentation.
4001 (and (eq type
'paragraph
)
4002 (equal data
(car (org-element-contents parent
)))
4003 (memq (org-element-type parent
)
4004 '(footnote-definiton item
))))))
4006 (funcall (intern (format "org-element-%s-interpreter" type
))
4008 (if greaterp
(org-element-normalize-contents contents
)
4010 (if (memq type
'(org-data plain-text nil
)) results
4011 ;; Build white spaces. If no `:post-blank' property is
4012 ;; specified, assume its value is 0.
4013 (let ((post-blank (or (org-element-property :post-blank data
) 0)))
4014 (if (memq type org-element-all-objects
)
4015 (concat results
(make-string post-blank
32))
4017 (org-element--interpret-affiliated-keywords data
)
4018 (org-element-normalize-string results
)
4019 (make-string post-blank
10)))))))
4021 (defun org-element--interpret-affiliated-keywords (element)
4022 "Return ELEMENT's affiliated keywords as Org syntax.
4023 If there is no affiliated keyword, return the empty string."
4024 (let ((keyword-to-org
4028 (when (member key org-element-dual-keywords
)
4029 (setq dual
(cdr value
) value
(car value
)))
4032 (format "[%s]" (org-element-interpret-data dual
)))
4034 (if (member key org-element-parsed-keywords
)
4035 (org-element-interpret-data value
)
4040 (let ((value (org-element-property prop element
))
4041 (keyword (upcase (substring (symbol-name prop
) 1))))
4043 (if (or (member keyword org-element-multiple-keywords
)
4044 ;; All attribute keywords can have multiple lines.
4045 (string-match "^ATTR_" keyword
))
4046 (mapconcat (lambda (line) (funcall keyword-to-org keyword line
))
4049 (funcall keyword-to-org keyword value
)))))
4050 ;; List all ELEMENT's properties matching an attribute line or an
4051 ;; affiliated keyword, but ignore translated keywords since they
4052 ;; cannot belong to the property list.
4053 (loop for prop in
(nth 1 element
) by
'cddr
4054 when
(let ((keyword (upcase (substring (symbol-name prop
) 1))))
4055 (or (string-match "^ATTR_" keyword
)
4057 (member keyword org-element-affiliated-keywords
)
4059 org-element-keyword-translation-alist
)))))
4063 ;; Because interpretation of the parse tree must return the same
4064 ;; number of blank lines between elements and the same number of white
4065 ;; space after objects, some special care must be given to white
4068 ;; The first function, `org-element-normalize-string', ensures any
4069 ;; string different from the empty string will end with a single
4070 ;; newline character.
4072 ;; The second function, `org-element-normalize-contents', removes
4073 ;; global indentation from the contents of the current element.
4075 (defun org-element-normalize-string (s)
4076 "Ensure string S ends with a single newline character.
4078 If S isn't a string return it unchanged. If S is the empty
4079 string, return it. Otherwise, return a new string with a single
4080 newline character at its end."
4082 ((not (stringp s
)) s
)
4084 (t (and (string-match "\\(\n[ \t]*\\)*\\'" s
)
4085 (replace-match "\n" nil nil s
)))))
4087 (defun org-element-normalize-contents (element &optional ignore-first
)
4088 "Normalize plain text in ELEMENT's contents.
4090 ELEMENT must only contain plain text and objects.
4092 If optional argument IGNORE-FIRST is non-nil, ignore first line's
4093 indentation to compute maximal common indentation.
4095 Return the normalized element that is element with global
4096 indentation removed from its contents. The function assumes that
4097 indentation is not done with TAB characters."
4098 (let* (ind-list ; for byte-compiler
4099 collect-inds
; for byte-compiler
4102 ;; Return list of indentations within BLOB. This is done by
4103 ;; walking recursively BLOB and updating IND-LIST along the
4104 ;; way. FIRST-FLAG is non-nil when the first string hasn't
4105 ;; been seen yet. It is required as this string is the only
4106 ;; one whose indentation doesn't happen after a newline
4108 (lambda (blob first-flag
)
4111 (when (and first-flag
(stringp object
))
4112 (setq first-flag nil
)
4113 (string-match "\\`\\( *\\)" object
)
4114 (let ((len (length (match-string 1 object
))))
4115 ;; An indentation of zero means no string will be
4116 ;; modified. Quit the process.
4117 (if (zerop len
) (throw 'zero
(setq ind-list nil
))
4118 (push len ind-list
))))
4122 ;; Avoid matching blank or empty lines.
4123 (while (and (string-match "\n\\( *\\)\\(.\\)" object start
)
4124 (not (equal (match-string 2 object
) " ")))
4125 (setq start
(match-end 0))
4126 (push (length (match-string 1 object
)) ind-list
))))
4127 ((memq (org-element-type object
) org-element-recursive-objects
)
4128 (funcall collect-inds object first-flag
))))
4129 (org-element-contents blob
))))))
4130 ;; Collect indentation list in ELEMENT. Possibly remove first
4131 ;; value if IGNORE-FIRST is non-nil.
4132 (catch 'zero
(funcall collect-inds element
(not ignore-first
)))
4133 (if (not ind-list
) element
4134 ;; Build ELEMENT back, replacing each string with the same
4135 ;; string minus common indentation.
4136 (let* (build ; For byte compiler.
4139 (lambda (blob mci first-flag
)
4140 ;; Return BLOB with all its strings indentation
4141 ;; shortened from MCI white spaces. FIRST-FLAG is
4142 ;; non-nil when the first string hasn't been seen
4147 (when (and first-flag
(stringp object
))
4148 (setq first-flag nil
)
4150 (replace-regexp-in-string
4151 (format "\\` \\{%d\\}" mci
) "" object
)))
4154 (replace-regexp-in-string
4155 (format "\n \\{%d\\}" mci
) "\n" object
))
4156 ((memq (org-element-type object
)
4157 org-element-recursive-objects
)
4158 (funcall build object mci first-flag
))
4160 (org-element-contents blob
)))
4162 (funcall build element
(apply 'min ind-list
) (not ignore-first
))))))
4168 ;; The first move is to implement a way to obtain the smallest element
4169 ;; containing point. This is the job of `org-element-at-point'. It
4170 ;; basically jumps back to the beginning of section containing point
4171 ;; and moves, element after element, with
4172 ;; `org-element--current-element' until the container is found. Note:
4173 ;; When using `org-element-at-point', secondary values are never
4174 ;; parsed since the function focuses on elements, not on objects.
4176 ;; At a deeper level, `org-element-context' lists all elements and
4177 ;; objects containing point.
4179 ;; `org-element-nested-p' and `org-element-swap-A-B' may be used
4180 ;; internally by navigation and manipulation tools.
4183 (defun org-element-at-point (&optional keep-trail
)
4184 "Determine closest element around point.
4186 Return value is a list like (TYPE PROPS) where TYPE is the type
4187 of the element and PROPS a plist of properties associated to the
4190 Possible types are defined in `org-element-all-elements'.
4191 Properties depend on element or object type, but always
4192 include :begin, :end, :parent and :post-blank properties.
4194 As a special case, if point is at the very beginning of a list or
4195 sub-list, returned element will be that list instead of the first
4196 item. In the same way, if point is at the beginning of the first
4197 row of a table, returned element will be the table instead of the
4200 If optional argument KEEP-TRAIL is non-nil, the function returns
4201 a list of of elements leading to element at point. The list's
4202 CAR is always the element at point. Following positions contain
4203 element's siblings, then parents, siblings of parents, until the
4204 first element of current section."
4205 (org-with-wide-buffer
4206 ;; If at an headline, parse it. It is the sole element that
4207 ;; doesn't require to know about context. Be sure to disallow
4208 ;; secondary string parsing, though.
4209 (if (org-with-limited-levels (org-at-heading-p))
4212 (if (not keep-trail
) (org-element-headline-parser (point-max) t
)
4213 (list (org-element-headline-parser (point-max) t
))))
4214 ;; Otherwise move at the beginning of the section containing
4216 (let ((origin (point))
4217 (end (save-excursion
4218 (org-with-limited-levels (outline-next-heading)) (point)))
4219 element type special-flag trail struct prevs parent
)
4220 (org-with-limited-levels
4221 (if (org-with-limited-levels (org-before-first-heading-p))
4222 (goto-char (point-min))
4223 (org-back-to-heading)
4225 (org-skip-whitespace)
4227 ;; Parse successively each element, skipping those ending
4228 ;; before original position.
4232 (org-element--current-element end
'element special-flag struct
)
4234 (org-element-put-property element
:parent parent
)
4235 (when keep-trail
(push element trail
))
4237 ;; 1. Skip any element ending before point. Also skip
4238 ;; element ending at point when we're sure that another
4239 ;; element has started.
4240 ((let ((elem-end (org-element-property :end element
)))
4241 (when (or (< elem-end origin
)
4242 (and (= elem-end origin
) (/= elem-end end
)))
4243 (goto-char elem-end
))))
4244 ;; 2. An element containing point is always the element at
4246 ((not (memq type org-element-greater-elements
))
4247 (throw 'exit
(if keep-trail trail element
)))
4248 ;; 3. At any other greater element type, if point is
4249 ;; within contents, move into it.
4251 (let ((cbeg (org-element-property :contents-begin element
))
4252 (cend (org-element-property :contents-end element
)))
4253 (if (or (not cbeg
) (not cend
) (> cbeg origin
) (< cend origin
)
4254 ;; Create an anchor for tables and plain lists:
4255 ;; when point is at the very beginning of these
4256 ;; elements, ignoring affiliated keywords,
4257 ;; target them instead of their contents.
4258 (and (= cbeg origin
) (memq type
'(plain-list table
)))
4259 ;; When point is at contents end, do not move
4260 ;; into elements with an explicit ending, but
4261 ;; return that element instead.
4262 (and (= cend origin
)
4265 drawer dynamic-block inlinetask item
4266 plain-list property-drawer quote-block
4268 (throw 'exit
(if keep-trail trail element
))
4269 (setq parent element
)
4272 (setq special-flag
'item
4273 struct
(org-element-property :structure element
)))
4274 (property-drawer (setq special-flag
'node-property
))
4275 (table (setq special-flag
'table-row
))
4276 (otherwise (setq special-flag nil
)))
4278 (goto-char cbeg
)))))))))))
4281 (defun org-element-context ()
4282 "Return closest element or object around point.
4284 Return value is a list like (TYPE PROPS) where TYPE is the type
4285 of the element or object and PROPS a plist of properties
4288 Possible types are defined in `org-element-all-elements' and
4289 `org-element-all-objects'. Properties depend on element or
4290 object type, but always include :begin, :end, :parent
4291 and :post-blank properties."
4292 (org-with-wide-buffer
4293 (let* ((origin (point))
4294 (element (org-element-at-point))
4295 (type (car element
))
4297 ;; Check if point is inside an element containing objects or at
4298 ;; a secondary string. In that case, move to beginning of the
4299 ;; element or secondary string and set END to the other side.
4300 (if (not (or (and (eq type
'item
)
4301 (let ((tag (org-element-property :tag element
)))
4305 (search-forward tag
(point-at-eol))
4306 (goto-char (match-beginning 0))
4307 (and (>= origin
(point))
4309 ;; `1+' is required so some
4310 ;; successors can match
4311 ;; properly their object.
4312 (setq end
(1+ (match-end 0)))))))))
4313 (and (memq type
'(headline inlinetask
))
4314 (progn (beginning-of-line)
4315 (skip-chars-forward "* ")
4316 (setq end
(point-at-eol))))
4317 (and (memq type
'(paragraph table-cell verse-block
))
4318 (let ((cbeg (org-element-property
4319 :contents-begin element
))
4320 (cend (org-element-property
4321 :contents-end element
)))
4322 (and (>= origin cbeg
)
4324 (progn (goto-char cbeg
) (setq end cend
)))))))
4326 (let ((restriction (org-element-restriction element
))
4330 (while (setq candidates
(org-element--get-next-object-candidates
4331 end restriction candidates
))
4332 (let ((closest-cand (rassq (apply 'min
(mapcar 'cdr candidates
))
4334 ;; If ORIGIN is before next object in element, there's
4335 ;; no point in looking further.
4336 (if (> (cdr closest-cand
) origin
) (throw 'exit element
)
4338 (progn (goto-char (cdr closest-cand
))
4339 (funcall (intern (format "org-element-%s-parser"
4340 (car closest-cand
))))))
4341 (cbeg (org-element-property :contents-begin object
))
4342 (cend (org-element-property :contents-end object
)))
4344 ;; ORIGIN is after OBJECT, so skip it.
4345 ((< (org-element-property :end object
) origin
)
4346 (goto-char (org-element-property :end object
)))
4347 ;; ORIGIN is within a non-recursive object or at an
4348 ;; object boundaries: Return that object.
4349 ((or (not cbeg
) (> cbeg origin
) (< cend origin
))
4351 (org-element-put-property object
:parent parent
)))
4352 ;; Otherwise, move within current object and restrict
4353 ;; search to the end of its contents.
4355 (org-element-put-property object
:parent parent
)
4356 (setq parent object end cend
)))))))
4359 (defsubst org-element-nested-p
(elem-A elem-B
)
4360 "Non-nil when elements ELEM-A and ELEM-B are nested."
4361 (let ((beg-A (org-element-property :begin elem-A
))
4362 (beg-B (org-element-property :begin elem-B
))
4363 (end-A (org-element-property :end elem-A
))
4364 (end-B (org-element-property :end elem-B
)))
4365 (or (and (>= beg-A beg-B
) (<= end-A end-B
))
4366 (and (>= beg-B beg-A
) (<= end-B end-A
)))))
4368 (defun org-element-swap-A-B (elem-A elem-B
)
4369 "Swap elements ELEM-A and ELEM-B.
4370 Assume ELEM-B is after ELEM-A in the buffer. Leave point at the
4372 (goto-char (org-element-property :begin elem-A
))
4373 ;; There are two special cases when an element doesn't start at bol:
4374 ;; the first paragraph in an item or in a footnote definition.
4375 (let ((specialp (not (bolp))))
4376 ;; Only a paragraph without any affiliated keyword can be moved at
4377 ;; ELEM-A position in such a situation. Note that the case of
4378 ;; a footnote definition is impossible: it cannot contain two
4379 ;; paragraphs in a row because it cannot contain a blank line.
4381 (or (not (eq (org-element-type elem-B
) 'paragraph
))
4382 (/= (org-element-property :begin elem-B
)
4383 (org-element-property :contents-begin elem-B
))))
4384 (error "Cannot swap elements"))
4385 ;; In a special situation, ELEM-A will have no indentation. We'll
4386 ;; give it ELEM-B's (which will in, in turn, have no indentation).
4387 (let* ((ind-B (when specialp
4388 (goto-char (org-element-property :begin elem-B
))
4389 (org-get-indentation)))
4390 (beg-A (org-element-property :begin elem-A
))
4391 (end-A (save-excursion
4392 (goto-char (org-element-property :end elem-A
))
4393 (skip-chars-backward " \r\t\n")
4395 (beg-B (org-element-property :begin elem-B
))
4396 (end-B (save-excursion
4397 (goto-char (org-element-property :end elem-B
))
4398 (skip-chars-backward " \r\t\n")
4400 ;; Store overlays responsible for visibility status. We
4401 ;; also need to store their boundaries as they will be
4402 ;; removed from buffer.
4405 (mapcar (lambda (ov) (list ov
(overlay-start ov
) (overlay-end ov
)))
4406 (overlays-in beg-A end-A
))
4407 (mapcar (lambda (ov) (list ov
(overlay-start ov
) (overlay-end ov
)))
4408 (overlays-in beg-B end-B
))))
4410 (body-A (buffer-substring beg-A end-A
))
4411 (body-B (delete-and-extract-region beg-B end-B
)))
4414 (setq body-B
(replace-regexp-in-string "\\`[ \t]*" "" body-B
))
4415 (org-indent-to-column ind-B
))
4417 ;; Restore ex ELEM-A overlays.
4418 (let ((offset (- beg-B beg-A
)))
4421 (car ov
) (+ (nth 1 ov
) offset
) (+ (nth 2 ov
) offset
)))
4424 (delete-region beg-A end-A
)
4426 ;; Restore ex ELEM-B overlays.
4429 (car ov
) (- (nth 1 ov
) offset
) (- (nth 2 ov
) offset
)))
4431 (goto-char (org-element-property :end elem-B
)))))
4434 (provide 'org-element
)
4435 ;;; org-element.el ends here