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 (namely `babel-call', `clock', `headline', `item',
34 ;; `keyword', `planning', `property-drawer' and `section' types), it
35 ;; can also accept a fixed set of keywords as attributes. Those are
36 ;; called "affiliated keywords" to distinguish them from other
37 ;; keywords, which are full-fledged elements. Almost all affiliated
38 ;; keywords are referenced in `org-element-affiliated-keywords'; the
39 ;; others are export attributes and start with "ATTR_" prefix.
41 ;; Element containing other elements (and only elements) are called
42 ;; greater elements. Concerned types are: `center-block', `drawer',
43 ;; `dynamic-block', `footnote-definition', `headline', `inlinetask',
44 ;; `item', `plain-list', `quote-block', `section' and `special-block'.
46 ;; Other element types are: `babel-call', `clock', `comment',
47 ;; `comment-block', `example-block', `export-block', `fixed-width',
48 ;; `horizontal-rule', `keyword', `latex-environment', `paragraph',
49 ;; `planning', `property-drawer', `quote-section', `src-block',
50 ;; `table', `table-row' and `verse-block'. Among them, `paragraph'
51 ;; and `verse-block' types can contain Org objects and plain text.
53 ;; Objects are related to document's contents. Some of them are
54 ;; recursive. Associated types are of the following: `bold', `code',
55 ;; `entity', `export-snippet', `footnote-reference',
56 ;; `inline-babel-call', `inline-src-block', `italic',
57 ;; `latex-fragment', `line-break', `link', `macro', `radio-target',
58 ;; `statistics-cookie', `strike-through', `subscript', `superscript',
59 ;; `table-cell', `target', `timestamp', `underline' and `verbatim'.
61 ;; Some elements also have special properties whose value can hold
62 ;; objects themselves (i.e. an item tag or an headline name). Such
63 ;; values are called "secondary strings". Any object belongs to
64 ;; either an element or a secondary string.
66 ;; Notwithstanding affiliated keywords, each greater element, element
67 ;; and object has a fixed set of properties attached to it. Among
68 ;; them, four are shared by all types: `:begin' and `:end', which
69 ;; refer to the beginning and ending buffer positions of the
70 ;; considered element or object, `:post-blank', which holds the number
71 ;; of blank lines, or white spaces, at its end and `:parent' which
72 ;; refers to the element or object containing it. Greater elements
73 ;; and elements containing objects will also have `:contents-begin'
74 ;; and `:contents-end' properties to delimit contents.
76 ;; Lisp-wise, an element or an object can be represented as a list.
77 ;; It follows the pattern (TYPE PROPERTIES CONTENTS), where:
78 ;; TYPE is a symbol describing the Org element or object.
79 ;; PROPERTIES is the property list attached to it. See docstring of
80 ;; appropriate parsing function to get an exhaustive
82 ;; CONTENTS is a list of elements, objects or raw strings contained
83 ;; in the current element or object, when applicable.
85 ;; An Org buffer is a nested list of such elements and objects, whose
86 ;; type is `org-data' and properties is nil.
88 ;; The first part of this file defines Org syntax, while the second
89 ;; one provide accessors and setters functions.
91 ;; The next part implements a parser and an interpreter for each
92 ;; element and object type in Org syntax.
94 ;; The following part creates a fully recursive buffer parser. It
95 ;; also provides a tool to map a function to elements or objects
96 ;; matching some criteria in the parse tree. Functions of interest
97 ;; are `org-element-parse-buffer', `org-element-map' and, to a lesser
98 ;; extent, `org-element-parse-secondary-string'.
100 ;; The penultimate part is the cradle of an interpreter for the
101 ;; obtained parse tree: `org-element-interpret-data'.
103 ;; The library ends by furnishing `org-element-at-point' function, and
104 ;; a way to give information about document structure around point
105 ;; with `org-element-context'.
116 ;;; Definitions And Rules
118 ;; Define elements, greater elements and specify recursive objects,
119 ;; along with the affiliated keywords recognized. Also set up
120 ;; restrictions on recursive objects combinations.
122 ;; These variables really act as a control center for the parsing
125 (defconst org-element-paragraph-separate
127 ;; Headlines, inlinetasks.
128 org-outline-regexp
"\\|"
129 ;; Footnote definitions.
130 "\\[\\(?:[0-9]+\\|fn:[-_[:word:]]+\\)\\]" "\\|"
134 ;; Tables (any type).
135 "\\(?:|\\|\\+-[-+]\\)" "\\|"
136 ;; Blocks (any type), Babel calls, drawers (any type),
137 ;; fixed-width areas and keywords. Note: this is only an
138 ;; indication and need some thorough check.
141 "-\\{5,\\}[ \t]*$" "\\|"
142 ;; LaTeX environments.
143 "\\\\begin{\\([A-Za-z0-9]+\\*?\\)}" "\\|"
144 ;; Planning and Clock lines.
145 (regexp-opt (list org-scheduled-string
151 (let ((term (case org-plain-list-ordered-item-terminator
152 (?\
) ")") (?.
"\\.") (otherwise "[.)]")))
153 (alpha (and org-alphabetical-lists
"\\|[A-Za-z]")))
154 (concat "\\(?:[-+*]\\|\\(?:[0-9]+" alpha
"\\)" term
"\\)"
155 "\\(?:[ \t]\\|$\\)"))
157 "Regexp to separate paragraphs in an Org buffer.
158 In the case of lines starting with \"#\" and \":\", this regexp
159 is not sufficient to know if point is at a paragraph ending. See
160 `org-element-paragraph-parser' for more information.")
162 (defconst org-element-all-elements
163 '(center-block clock comment comment-block drawer dynamic-block example-block
164 export-block fixed-width footnote-definition headline
165 horizontal-rule inlinetask item keyword latex-environment
166 babel-call paragraph plain-list planning property-drawer
167 quote-block quote-section section special-block src-block table
168 table-row verse-block
)
169 "Complete list of element types.")
171 (defconst org-element-greater-elements
172 '(center-block drawer dynamic-block footnote-definition headline inlinetask
173 item plain-list quote-block section special-block table
)
174 "List of recursive element types aka Greater Elements.")
176 (defconst org-element-all-successors
177 '(export-snippet footnote-reference inline-babel-call inline-src-block
178 latex-or-entity line-break link macro radio-target
179 statistics-cookie sub
/superscript table-cell target
180 text-markup timestamp
)
181 "Complete list of successors.")
183 (defconst org-element-object-successor-alist
184 '((subscript . sub
/superscript
) (superscript . sub
/superscript
)
185 (bold . text-markup
) (code . text-markup
) (italic . text-markup
)
186 (strike-through . text-markup
) (underline . text-markup
)
187 (verbatim . text-markup
) (entity . latex-or-entity
)
188 (latex-fragment . latex-or-entity
))
189 "Alist of translations between object type and successor name.
191 Sharing the same successor comes handy when, for example, the
192 regexp matching one object can also match the other object.")
194 (defconst org-element-all-objects
195 '(bold code entity export-snippet footnote-reference inline-babel-call
196 inline-src-block italic line-break latex-fragment link macro
197 radio-target statistics-cookie strike-through subscript superscript
198 table-cell target timestamp underline verbatim
)
199 "Complete list of object types.")
201 (defconst org-element-recursive-objects
202 '(bold italic link macro subscript radio-target strike-through superscript
203 table-cell underline
)
204 "List of recursive object types.")
206 (defconst org-element-block-name-alist
207 '(("CENTER" . org-element-center-block-parser
)
208 ("COMMENT" . org-element-comment-block-parser
)
209 ("EXAMPLE" . org-element-example-block-parser
)
210 ("QUOTE" . org-element-quote-block-parser
)
211 ("SRC" . org-element-src-block-parser
)
212 ("VERSE" . org-element-verse-block-parser
))
213 "Alist between block names and the associated parsing function.
214 Names must be uppercase. Any block whose name has no association
215 is parsed with `org-element-special-block-parser'.")
217 (defconst org-element-affiliated-keywords
218 '("CAPTION" "DATA" "HEADER" "HEADERS" "LABEL" "NAME" "PLOT" "RESNAME" "RESULT"
219 "RESULTS" "SOURCE" "SRCNAME" "TBLNAME")
220 "List of affiliated keywords as strings.
221 By default, all keywords setting attributes (i.e. \"ATTR_LATEX\")
222 are affiliated keywords and need not to be in this list.")
224 (defconst org-element--affiliated-re
225 (format "[ \t]*#\\+%s:"
226 ;; Regular affiliated keywords.
227 (format "\\(%s\\|ATTR_[-_A-Za-z0-9]+\\)\\(?:\\[\\(.*\\)\\]\\)?"
228 (regexp-opt org-element-affiliated-keywords
)))
229 "Regexp matching any affiliated keyword.
231 Keyword name is put in match group 1. Moreover, if keyword
232 belongs to `org-element-dual-keywords', put the dual value in
235 Don't modify it, set `org-element-affiliated-keywords' instead.")
237 (defconst org-element-keyword-translation-alist
238 '(("DATA" .
"NAME") ("LABEL" .
"NAME") ("RESNAME" .
"NAME")
239 ("SOURCE" .
"NAME") ("SRCNAME" .
"NAME") ("TBLNAME" .
"NAME")
240 ("RESULT" .
"RESULTS") ("HEADERS" .
"HEADER"))
241 "Alist of usual translations for keywords.
242 The key is the old name and the value the new one. The property
243 holding their value will be named after the translated name.")
245 (defconst org-element-multiple-keywords
'("HEADER")
246 "List of affiliated keywords that can occur more that once in an element.
248 Their value will be consed into a list of strings, which will be
249 returned as the value of the property.
251 This list is checked after translations have been applied. See
252 `org-element-keyword-translation-alist'.
254 By default, all keywords setting attributes (i.e. \"ATTR_LATEX\")
255 allow multiple occurrences and need not to be in this list.")
257 (defconst org-element-parsed-keywords
'("AUTHOR" "CAPTION" "DATE" "TITLE")
258 "List of keywords whose value can be parsed.
260 Their value will be stored as a secondary string: a list of
263 This list is checked after translations have been applied. See
264 `org-element-keyword-translation-alist'.")
266 (defconst org-element-dual-keywords
'("CAPTION" "RESULTS")
267 "List of keywords which can have a secondary value.
269 In Org syntax, they can be written with optional square brackets
270 before the colons. For example, results keyword can be
271 associated to a hash value with the following:
273 #+RESULTS[hash-string]: some-source
275 This list is checked after translations have been applied. See
276 `org-element-keyword-translation-alist'.")
278 (defconst org-element-object-restrictions
279 '((bold export-snippet inline-babel-call inline-src-block latex-or-entity link
280 radio-target sub
/superscript target text-markup timestamp
)
281 (footnote-reference export-snippet footnote-reference inline-babel-call
282 inline-src-block latex-or-entity line-break link macro
283 radio-target sub
/superscript target text-markup
285 (headline inline-babel-call inline-src-block latex-or-entity link macro
286 radio-target statistics-cookie sub
/superscript target text-markup
288 (inlinetask inline-babel-call inline-src-block latex-or-entity link macro
289 radio-target sub
/superscript target text-markup timestamp
)
290 (italic export-snippet inline-babel-call inline-src-block latex-or-entity
291 link radio-target sub
/superscript target text-markup timestamp
)
292 (item export-snippet footnote-reference inline-babel-call latex-or-entity
293 link macro radio-target sub
/superscript target text-markup
)
294 (keyword latex-or-entity macro sub
/superscript text-markup
)
295 (link export-snippet inline-babel-call inline-src-block latex-or-entity link
296 sub
/superscript text-markup
)
298 (paragraph export-snippet footnote-reference inline-babel-call
299 inline-src-block latex-or-entity line-break link macro
300 radio-target statistics-cookie sub
/superscript target text-markup
302 (radio-target export-snippet latex-or-entity sub
/superscript
)
303 (strike-through export-snippet inline-babel-call inline-src-block
304 latex-or-entity link radio-target sub
/superscript target
305 text-markup timestamp
)
306 (subscript export-snippet inline-babel-call inline-src-block latex-or-entity
307 sub
/superscript target text-markup
)
308 (superscript export-snippet inline-babel-call inline-src-block
309 latex-or-entity sub
/superscript target text-markup
)
310 (table-cell export-snippet latex-or-entity link macro radio-target
311 sub
/superscript target text-markup timestamp
)
312 (table-row table-cell
)
313 (underline export-snippet inline-babel-call inline-src-block latex-or-entity
314 link radio-target sub
/superscript target text-markup timestamp
)
315 (verse-block footnote-reference inline-babel-call inline-src-block
316 latex-or-entity line-break link macro radio-target
317 sub
/superscript target text-markup timestamp
))
318 "Alist of objects restrictions.
320 CAR is an element or object type containing objects and CDR is
321 a list of successors that will be called within an element or
324 For example, in a `radio-target' object, one can only find
325 entities, export snippets, latex-fragments, subscript and
328 This alist also applies to secondary string. For example, an
329 `headline' type element doesn't directly contain objects, but
330 still has an entry since one of its properties (`:title') does.")
332 (defconst org-element-secondary-value-alist
333 '((headline .
:title
)
334 (inlinetask .
:title
)
336 (footnote-reference .
:inline-definition
))
337 "Alist between element types and location of secondary value.")
341 ;;; Accessors and Setters
343 ;; Provide four accessors: `org-element-type', `org-element-property'
344 ;; `org-element-contents' and `org-element-restriction'.
346 ;; Setter functions allow to modify elements by side effect. There is
347 ;; `org-element-put-property', `org-element-set-contents',
348 ;; `org-element-set-element' and `org-element-adopt-element'. Note
349 ;; that `org-element-set-element' and `org-element-adopt-elements' are
350 ;; higher level functions since also update `:parent' property.
352 (defsubst org-element-type
(element)
353 "Return type of ELEMENT.
355 The function returns the type of the element or object provided.
356 It can also return the following special value:
357 `plain-text' for a string
358 `org-data' for a complete document
359 nil in any other case."
361 ((not (consp element
)) (and (stringp element
) 'plain-text
))
362 ((symbolp (car element
)) (car element
))))
364 (defsubst org-element-property
(property element
)
365 "Extract the value from the PROPERTY of an ELEMENT."
366 (plist-get (nth 1 element
) property
))
368 (defsubst org-element-contents
(element)
369 "Extract contents from an ELEMENT."
370 (and (consp element
) (nthcdr 2 element
)))
372 (defsubst org-element-restriction
(element)
373 "Return restriction associated to ELEMENT.
374 ELEMENT can be an element, an object or a symbol representing an
375 element or object type."
376 (cdr (assq (if (symbolp element
) element
(org-element-type element
))
377 org-element-object-restrictions
)))
379 (defsubst org-element-put-property
(element property value
)
380 "In ELEMENT set PROPERTY to VALUE.
381 Return modified element."
382 (when (consp element
)
383 (setcar (cdr element
) (plist-put (nth 1 element
) property value
)))
386 (defsubst org-element-set-contents
(element &rest contents
)
387 "Set ELEMENT contents to CONTENTS.
388 Return modified element."
389 (cond ((not element
) (list contents
))
390 ((cdr element
) (setcdr (cdr element
) contents
))
391 (t (nconc element contents
))))
393 (defsubst org-element-set-element
(old new
)
394 "Replace element or object OLD with element or object NEW.
395 The function takes care of setting `:parent' property for NEW."
396 ;; Since OLD is going to be changed into NEW by side-effect, first
397 ;; make sure that every element or object within NEW has OLD as
399 (mapc (lambda (blob) (org-element-put-property blob
:parent old
))
400 (org-element-contents new
))
401 ;; Transfer contents.
402 (apply 'org-element-set-contents old
(org-element-contents new
))
403 ;; Ensure NEW has same parent as OLD, then overwrite OLD properties
405 (org-element-put-property new
:parent
(org-element-property :parent old
))
406 (setcar (cdr old
) (nth 1 new
))
408 (setcar old
(car new
)))
410 (defsubst org-element-adopt-elements
(parent &rest children
)
411 "Append elements to the contents of another element.
413 PARENT is an element or object. CHILDREN can be elements,
414 objects, or a strings.
416 The function takes care of setting `:parent' property for CHILD.
417 Return parent element."
418 (if (not parent
) children
419 ;; Link every child to PARENT.
420 (mapc (lambda (child)
421 (unless (stringp child
)
422 (org-element-put-property child
:parent parent
)))
424 ;; Add CHILDREN at the end of PARENT contents.
425 (apply 'org-element-set-contents
427 (nconc (org-element-contents parent
) children
))
428 ;; Return modified PARENT element.
435 ;; For each greater element type, we define a parser and an
438 ;; A parser returns the element or object as the list described above.
439 ;; Most of them accepts no argument. Though, exceptions exist. Hence
440 ;; every element containing a secondary string (see
441 ;; `org-element-secondary-value-alist') will accept an optional
442 ;; argument to toggle parsing of that secondary string. Moreover,
443 ;; `item' parser requires current list's structure as its first
446 ;; An interpreter accepts two arguments: the list representation of
447 ;; the element or object, and its contents. The latter may be nil,
448 ;; depending on the element or object considered. It returns the
449 ;; appropriate Org syntax, as a string.
451 ;; Parsing functions must follow the naming convention:
452 ;; org-element-TYPE-parser, where TYPE is greater element's type, as
453 ;; defined in `org-element-greater-elements'.
455 ;; Similarly, interpreting functions must follow the naming
456 ;; convention: org-element-TYPE-interpreter.
458 ;; With the exception of `headline' and `item' types, greater elements
459 ;; cannot contain other greater elements of their own type.
461 ;; Beside implementing a parser and an interpreter, adding a new
462 ;; greater element requires to tweak `org-element--current-element'.
463 ;; Moreover, the newly defined type must be added to both
464 ;; `org-element-all-elements' and `org-element-greater-elements'.
469 (defun org-element-center-block-parser (limit)
470 "Parse a center block.
472 LIMIT bounds the search.
474 Return a list whose CAR is `center-block' and CDR is a plist
475 containing `:begin', `:end', `:hiddenp', `:contents-begin',
476 `:contents-end' and `:post-blank' keywords.
478 Assume point is at the beginning of the block."
479 (let ((case-fold-search t
))
480 (if (not (save-excursion
481 (re-search-forward "^[ \t]*#\\+END_CENTER[ \t]*$" limit t
)))
482 ;; Incomplete block: parse it as a paragraph.
483 (org-element-paragraph-parser limit
)
484 (let ((block-end-line (match-beginning 0)))
485 (let* ((keywords (org-element--collect-affiliated-keywords))
486 (begin (car keywords
))
487 ;; Empty blocks have no contents.
488 (contents-begin (progn (forward-line)
489 (and (< (point) block-end-line
)
491 (contents-end (and contents-begin block-end-line
))
492 (hidden (org-invisible-p2))
493 (pos-before-blank (progn (goto-char block-end-line
)
496 (end (save-excursion (skip-chars-forward " \r\t\n" limit
)
497 (skip-chars-backward " \t")
498 (if (bolp) (point) (line-end-position)))))
504 :contents-begin contents-begin
505 :contents-end contents-end
506 :post-blank
(count-lines pos-before-blank end
))
507 (cadr keywords
))))))))
509 (defun org-element-center-block-interpreter (center-block contents
)
510 "Interpret CENTER-BLOCK element as Org syntax.
511 CONTENTS is the contents of the element."
512 (format "#+BEGIN_CENTER\n%s#+END_CENTER" contents
))
517 (defun org-element-drawer-parser (limit)
520 LIMIT bounds the search.
522 Return a list whose CAR is `drawer' and CDR is a plist containing
523 `:drawer-name', `:begin', `:end', `:hiddenp', `:contents-begin',
524 `:contents-end' and `:post-blank' keywords.
526 Assume point is at beginning of drawer."
527 (let ((case-fold-search t
))
528 (if (not (save-excursion (re-search-forward "^[ \t]*:END:[ \t]*$" limit t
)))
529 ;; Incomplete drawer: parse it as a paragraph.
530 (org-element-paragraph-parser limit
)
531 (let ((drawer-end-line (match-beginning 0)))
533 (let* ((case-fold-search t
)
534 (name (progn (looking-at org-drawer-regexp
)
535 (org-match-string-no-properties 1)))
536 (keywords (org-element--collect-affiliated-keywords))
537 (begin (car keywords
))
538 ;; Empty drawers have no contents.
539 (contents-begin (progn (forward-line)
540 (and (< (point) drawer-end-line
)
542 (contents-end (and contents-begin drawer-end-line
))
543 (hidden (org-invisible-p2))
544 (pos-before-blank (progn (goto-char drawer-end-line
)
547 (end (progn (skip-chars-forward " \r\t\n" limit
)
548 (skip-chars-backward " \t")
549 (if (bolp) (point) (line-end-position)))))
556 :contents-begin contents-begin
557 :contents-end contents-end
558 :post-blank
(count-lines pos-before-blank end
))
559 (cadr keywords
)))))))))
561 (defun org-element-drawer-interpreter (drawer contents
)
562 "Interpret DRAWER element as Org syntax.
563 CONTENTS is the contents of the element."
564 (format ":%s:\n%s:END:"
565 (org-element-property :drawer-name drawer
)
571 (defun org-element-dynamic-block-parser (limit)
572 "Parse a dynamic block.
574 LIMIT bounds the search.
576 Return a list whose CAR is `dynamic-block' and CDR is a plist
577 containing `:block-name', `:begin', `:end', `:hiddenp',
578 `:contents-begin', `:contents-end', `:arguments' and
579 `:post-blank' keywords.
581 Assume point is at beginning of dynamic block."
582 (let ((case-fold-search t
))
583 (if (not (save-excursion
584 (re-search-forward "^[ \t]*#\\+END:?[ \t]*$" limit t
)))
585 ;; Incomplete block: parse it as a paragraph.
586 (org-element-paragraph-parser limit
)
587 (let ((block-end-line (match-beginning 0)))
589 (let* ((name (progn (looking-at org-dblock-start-re
)
590 (org-match-string-no-properties 1)))
591 (arguments (org-match-string-no-properties 3))
592 (keywords (org-element--collect-affiliated-keywords))
593 (begin (car keywords
))
594 ;; Empty blocks have no contents.
595 (contents-begin (progn (forward-line)
596 (and (< (point) block-end-line
)
598 (contents-end (and contents-begin block-end-line
))
599 (hidden (org-invisible-p2))
600 (pos-before-blank (progn (goto-char block-end-line
)
603 (end (progn (skip-chars-forward " \r\t\n" limit
)
604 (skip-chars-backward " \t")
605 (if (bolp) (point) (line-end-position)))))
613 :contents-begin contents-begin
614 :contents-end contents-end
615 :post-blank
(count-lines pos-before-blank end
))
616 (cadr keywords
)))))))))
618 (defun org-element-dynamic-block-interpreter (dynamic-block contents
)
619 "Interpret DYNAMIC-BLOCK element as Org syntax.
620 CONTENTS is the contents of the element."
621 (format "#+BEGIN: %s%s\n%s#+END:"
622 (org-element-property :block-name dynamic-block
)
623 (let ((args (org-element-property :arguments dynamic-block
)))
624 (and args
(concat " " args
)))
628 ;;;; Footnote Definition
630 (defun org-element-footnote-definition-parser (limit)
631 "Parse a footnote definition.
633 LIMIT bounds the search.
635 Return a list whose CAR is `footnote-definition' and CDR is
636 a plist containing `:label', `:begin' `:end', `:contents-begin',
637 `:contents-end' and `:post-blank' keywords.
639 Assume point is at the beginning of the footnote definition."
641 (let* ((label (progn (looking-at org-footnote-definition-re
)
642 (org-match-string-no-properties 1)))
643 (keywords (org-element--collect-affiliated-keywords))
644 (begin (car keywords
))
645 (ending (save-excursion
649 (concat org-outline-regexp-bol
"\\|"
650 org-footnote-definition-re
"\\|"
651 "^[ \t]*$") limit
'move
))
654 (contents-begin (progn (search-forward "]")
655 (skip-chars-forward " \r\t\n" ending
)
656 (and (/= (point) ending
) (point))))
657 (contents-end (and contents-begin ending
))
658 (end (progn (goto-char ending
)
659 (skip-chars-forward " \r\t\n" limit
)
660 (skip-chars-backward " \t")
661 (if (bolp) (point) (line-end-position)))))
662 (list 'footnote-definition
667 :contents-begin contents-begin
668 :contents-end contents-end
669 :post-blank
(count-lines ending end
))
672 (defun org-element-footnote-definition-interpreter (footnote-definition contents
)
673 "Interpret FOOTNOTE-DEFINITION element as Org syntax.
674 CONTENTS is the contents of the footnote-definition."
675 (concat (format "[%s]" (org-element-property :label footnote-definition
))
682 (defun org-element-headline-parser (limit &optional raw-secondary-p
)
685 Return a list whose CAR is `headline' and CDR is a plist
686 containing `:raw-value', `:title', `:begin', `:end',
687 `:pre-blank', `:hiddenp', `:contents-begin' and `:contents-end',
688 `:level', `:priority', `:tags', `:todo-keyword',`:todo-type',
689 `:scheduled', `:deadline', `:timestamp', `:clock', `:category',
690 `:quotedp', `:archivedp', `:commentedp' and `:footnote-section-p'
693 The plist also contains any property set in the property drawer,
694 with its name in lowercase, the underscores replaced with hyphens
695 and colons at the beginning (i.e. `:custom-id').
697 When RAW-SECONDARY-P is non-nil, headline's title will not be
698 parsed as a secondary string, but as a plain string instead.
700 Assume point is at beginning of the headline."
702 (let* ((components (org-heading-components))
703 (level (nth 1 components
))
704 (todo (nth 2 components
))
706 (and todo
(if (member todo org-done-keywords
) 'done
'todo
)))
707 (tags (let ((raw-tags (nth 5 components
)))
708 (and raw-tags
(org-split-string raw-tags
":"))))
709 (raw-value (or (nth 4 components
) ""))
711 (let ((case-fold-search nil
))
712 (string-match (format "^%s\\( \\|$\\)" org-quote-string
)
715 (let ((case-fold-search nil
))
716 (string-match (format "^%s\\( \\|$\\)" org-comment-string
)
718 (archivedp (member org-archive-tag tags
))
719 (footnote-section-p (and org-footnote-section
720 (string= org-footnote-section raw-value
)))
721 ;; Normalize property names: ":SOME_PROP:" becomes
723 (standard-props (let (plist)
726 (let ((p-name (downcase (car p
))))
727 (while (string-match "_" p-name
)
729 (replace-match "-" nil nil p-name
)))
730 (setq p-name
(intern (concat ":" p-name
)))
732 (plist-put plist p-name
(cdr p
)))))
733 (org-entry-properties nil
'standard
))
735 (time-props (org-entry-properties nil
'special
"CLOCK"))
736 (scheduled (cdr (assoc "SCHEDULED" time-props
)))
737 (deadline (cdr (assoc "DEADLINE" time-props
)))
738 (clock (cdr (assoc "CLOCK" time-props
)))
739 (timestamp (cdr (assoc "TIMESTAMP" time-props
)))
741 (end (save-excursion (goto-char (org-end-of-subtree t t
))))
742 (pos-after-head (progn (forward-line) (point)))
743 (contents-begin (save-excursion
744 (skip-chars-forward " \r\t\n" end
)
745 (and (/= (point) end
) (line-beginning-position))))
746 (hidden (org-invisible-p2))
747 (contents-end (and contents-begin
748 (progn (goto-char end
)
749 (skip-chars-backward " \r\t\n")
752 ;; Clean RAW-VALUE from any quote or comment string.
753 (when (or quotedp commentedp
)
754 (let ((case-fold-search nil
))
756 (replace-regexp-in-string
758 (regexp-opt (list org-quote-string org-comment-string
))
762 ;; Clean TAGS from archive tag, if any.
763 (when archivedp
(setq tags
(delete org-archive-tag tags
)))
767 (list :raw-value raw-value
771 (if (not contents-begin
) 0
772 (count-lines pos-after-head contents-begin
))
774 :contents-begin contents-begin
775 :contents-end contents-end
777 :priority
(nth 3 components
)
785 :post-blank
(count-lines
786 (if (not contents-end
) pos-after-head
787 (goto-char contents-end
)
791 :footnote-section-p footnote-section-p
793 :commentedp commentedp
796 (org-element-put-property
798 (if raw-secondary-p raw-value
799 (org-element-parse-secondary-string
800 raw-value
(org-element-restriction 'headline
) headline
)))))))
802 (defun org-element-headline-interpreter (headline contents
)
803 "Interpret HEADLINE element as Org syntax.
804 CONTENTS is the contents of the element."
805 (let* ((level (org-element-property :level headline
))
806 (todo (org-element-property :todo-keyword headline
))
807 (priority (org-element-property :priority headline
))
808 (title (org-element-interpret-data
809 (org-element-property :title headline
)))
810 (tags (let ((tag-list (if (org-element-property :archivedp headline
)
811 (cons org-archive-tag
812 (org-element-property :tags headline
))
813 (org-element-property :tags headline
))))
815 (format ":%s:" (mapconcat 'identity tag-list
":")))))
816 (commentedp (org-element-property :commentedp headline
))
817 (quotedp (org-element-property :quotedp headline
))
818 (pre-blank (or (org-element-property :pre-blank headline
) 0))
819 (heading (concat (make-string level ?
*)
820 (and todo
(concat " " todo
))
821 (and quotedp
(concat " " org-quote-string
))
822 (and commentedp
(concat " " org-comment-string
))
824 (format " [#%s]" (char-to-string priority
)))
825 (cond ((and org-footnote-section
826 (org-element-property
827 :footnote-section-p headline
))
828 (concat " " org-footnote-section
))
829 (title (concat " " title
))))))
834 ((zerop org-tags-column
) (format " %s" tags
))
835 ((< org-tags-column
0)
838 (max (- (+ org-tags-column
(length heading
) (length tags
))) 1)
843 (make-string (max (- org-tags-column
(length heading
)) 1) ?
)
845 (make-string (1+ pre-blank
) 10)
851 (defun org-element-inlinetask-parser (limit &optional raw-secondary-p
)
852 "Parse an inline task.
854 Return a list whose CAR is `inlinetask' and CDR is a plist
855 containing `:title', `:begin', `:end', `:hiddenp',
856 `:contents-begin' and `:contents-end', `:level', `:priority',
857 `:raw-value', `:tags', `:todo-keyword', `:todo-type',
858 `:scheduled', `:deadline', `:timestamp', `:clock' and
859 `:post-blank' keywords.
861 The plist also contains any property set in the property drawer,
862 with its name in lowercase, the underscores replaced with hyphens
863 and colons at the beginning (i.e. `:custom-id').
865 When optional argument RAW-SECONDARY-P is non-nil, inline-task's
866 title will not be parsed as a secondary string, but as a plain
869 Assume point is at beginning of the inline task."
871 (let* ((keywords (org-element--collect-affiliated-keywords))
872 (begin (car keywords
))
873 (components (org-heading-components))
874 (todo (nth 2 components
))
876 (if (member todo org-done-keywords
) 'done
'todo
)))
877 (tags (let ((raw-tags (nth 5 components
)))
878 (and raw-tags
(org-split-string raw-tags
":"))))
879 (raw-value (or (nth 4 components
) ""))
880 ;; Normalize property names: ":SOME_PROP:" becomes
882 (standard-props (let (plist)
885 (let ((p-name (downcase (car p
))))
886 (while (string-match "_" p-name
)
888 (replace-match "-" nil nil p-name
)))
889 (setq p-name
(intern (concat ":" p-name
)))
891 (plist-put plist p-name
(cdr p
)))))
892 (org-entry-properties nil
'standard
))
894 (time-props (org-entry-properties nil
'special
"CLOCK"))
895 (scheduled (cdr (assoc "SCHEDULED" time-props
)))
896 (deadline (cdr (assoc "DEADLINE" time-props
)))
897 (clock (cdr (assoc "CLOCK" time-props
)))
898 (timestamp (cdr (assoc "TIMESTAMP" time-props
)))
899 (task-end (save-excursion
901 (and (re-search-forward "^\\*+ END" limit t
)
902 (match-beginning 0))))
903 (contents-begin (progn (forward-line)
904 (and task-end
(< (point) task-end
) (point))))
905 (hidden (and contents-begin
(org-invisible-p2)))
906 (contents-end (and contents-begin task-end
))
907 (before-blank (if (not task-end
) (point)
911 (end (progn (skip-chars-forward " \r\t\n" limit
)
912 (skip-chars-backward " \t")
913 (if (bolp) (point) (line-end-position))))
917 (list :raw-value raw-value
921 :contents-begin contents-begin
922 :contents-end contents-end
923 :level
(nth 1 components
)
924 :priority
(nth 3 components
)
932 :post-blank
(count-lines before-blank end
))
935 (org-element-put-property
937 (if raw-secondary-p raw-value
938 (org-element-parse-secondary-string
940 (org-element-restriction 'inlinetask
)
943 (defun org-element-inlinetask-interpreter (inlinetask contents
)
944 "Interpret INLINETASK element as Org syntax.
945 CONTENTS is the contents of inlinetask."
946 (let* ((level (org-element-property :level inlinetask
))
947 (todo (org-element-property :todo-keyword inlinetask
))
948 (priority (org-element-property :priority inlinetask
))
949 (title (org-element-interpret-data
950 (org-element-property :title inlinetask
)))
951 (tags (let ((tag-list (org-element-property :tags inlinetask
)))
953 (format ":%s:" (mapconcat 'identity tag-list
":")))))
954 (task (concat (make-string level ?
*)
955 (and todo
(concat " " todo
))
957 (format " [#%s]" (char-to-string priority
)))
958 (and title
(concat " " title
)))))
963 ((zerop org-tags-column
) (format " %s" tags
))
964 ((< org-tags-column
0)
967 (max (- (+ org-tags-column
(length task
) (length tags
))) 1)
972 (make-string (max (- org-tags-column
(length task
)) 1) ?
)
974 ;; Prefer degenerate inlinetasks when there are no
979 (make-string level ?
*) " END")))))
984 (defun org-element-item-parser (limit struct
&optional raw-secondary-p
)
987 STRUCT is the structure of the plain list.
989 Return a list whose CAR is `item' and CDR is a plist containing
990 `:bullet', `:begin', `:end', `:contents-begin', `:contents-end',
991 `:checkbox', `:counter', `:tag', `:structure', `:hiddenp' and
992 `:post-blank' keywords.
994 When optional argument RAW-SECONDARY-P is non-nil, item's tag, if
995 any, will not be parsed as a secondary string, but as a plain
998 Assume point is at the beginning of the item."
1001 (looking-at org-list-full-item-re
)
1002 (let* ((begin (point))
1003 (bullet (org-match-string-no-properties 1))
1004 (checkbox (let ((box (org-match-string-no-properties 3)))
1005 (cond ((equal "[ ]" box
) 'off
)
1006 ((equal "[X]" box
) 'on
)
1007 ((equal "[-]" box
) 'trans
))))
1008 (counter (let ((c (org-match-string-no-properties 2)))
1012 ((string-match "[A-Za-z]" c
)
1013 (- (string-to-char (upcase (match-string 0 c
)))
1015 ((string-match "[0-9]+" c
)
1016 (string-to-number (match-string 0 c
)))))))
1017 (end (save-excursion (goto-char (org-list-get-item-end begin struct
))
1018 (unless (bolp) (forward-line))
1022 ;; Ignore tags in un-ordered lists: they are just
1023 ;; a part of item's body.
1024 (if (and (match-beginning 4)
1025 (save-match-data (string-match "[.)]" bullet
)))
1028 (skip-chars-forward " \r\t\n" limit
)
1029 ;; If first line isn't empty, contents really start
1030 ;; at the text after item's meta-data.
1031 (if (= (point-at-bol) begin
) (point) (point-at-bol))))
1032 (hidden (progn (forward-line)
1033 (and (not (= (point) end
)) (org-invisible-p2))))
1034 (contents-end (progn (goto-char end
)
1035 (skip-chars-backward " \r\t\n")
1040 (list :bullet bullet
1043 ;; CONTENTS-BEGIN and CONTENTS-END may be
1044 ;; mixed up in the case of an empty item
1045 ;; separated from the next by a blank line.
1046 ;; Thus ensure the former is always the
1048 :contents-begin
(min contents-begin contents-end
)
1049 :contents-end
(max contents-begin contents-end
)
1054 :post-blank
(count-lines contents-end end
)))))
1055 (org-element-put-property
1057 (let ((raw-tag (org-list-get-tag begin struct
)))
1059 (if raw-secondary-p raw-tag
1060 (org-element-parse-secondary-string
1061 raw-tag
(org-element-restriction 'item
) item
))))))))
1063 (defun org-element-item-interpreter (item contents
)
1064 "Interpret ITEM element as Org syntax.
1065 CONTENTS is the contents of the element."
1066 (let* ((bullet (org-list-bullet-string (org-element-property :bullet item
)))
1067 (checkbox (org-element-property :checkbox item
))
1068 (counter (org-element-property :counter item
))
1069 (tag (let ((tag (org-element-property :tag item
)))
1070 (and tag
(org-element-interpret-data tag
))))
1071 ;; Compute indentation.
1072 (ind (make-string (length bullet
) 32))
1073 (item-starts-with-par-p
1074 (eq (org-element-type (car (org-element-contents item
)))
1079 (and counter
(format "[@%d] " counter
))
1084 (and tag
(format "%s :: " tag
))
1085 (let ((contents (replace-regexp-in-string
1086 "\\(^\\)[ \t]*\\S-" ind contents nil nil
1)))
1087 (if item-starts-with-par-p
(org-trim contents
)
1088 (concat "\n" contents
))))))
1093 (defun org-element-plain-list-parser (limit &optional structure
)
1094 "Parse a plain list.
1096 Optional argument STRUCTURE, when non-nil, is the structure of
1097 the plain list being parsed.
1099 Return a list whose CAR is `plain-list' and CDR is a plist
1100 containing `:type', `:begin', `:end', `:contents-begin' and
1101 `:contents-end', `:structure' and `:post-blank' keywords.
1103 Assume point is at the beginning of the list."
1105 (let* ((struct (or structure
(org-list-struct)))
1106 (prevs (org-list-prevs-alist struct
))
1107 (parents (org-list-parents-alist struct
))
1108 (type (org-list-get-list-type (point) struct prevs
))
1109 (contents-begin (point))
1110 (keywords (org-element--collect-affiliated-keywords))
1111 (begin (car keywords
))
1113 (progn (goto-char (org-list-get-list-end (point) struct prevs
))
1114 (unless (bolp) (forward-line))
1116 (end (progn (skip-chars-forward " \r\t\n" limit
)
1117 (skip-chars-backward " \t")
1118 (if (bolp) (point) (line-end-position)))))
1125 :contents-begin contents-begin
1126 :contents-end contents-end
1128 :post-blank
(count-lines contents-end end
))
1129 (cadr keywords
))))))
1131 (defun org-element-plain-list-interpreter (plain-list contents
)
1132 "Interpret PLAIN-LIST element as Org syntax.
1133 CONTENTS is the contents of the element."
1136 (goto-char (point-min))
1143 (defun org-element-quote-block-parser (limit)
1144 "Parse a quote block.
1146 LIMIT bounds the search.
1148 Return a list whose CAR is `quote-block' and CDR is a plist
1149 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1150 `:contents-end' and `:post-blank' keywords.
1152 Assume point is at the beginning of the block."
1153 (let ((case-fold-search t
))
1154 (if (not (save-excursion
1155 (re-search-forward "^[ \t]*#\\+END_QUOTE[ \t]*$" limit t
)))
1156 ;; Incomplete block: parse it as a paragraph.
1157 (org-element-paragraph-parser limit
)
1158 (let ((block-end-line (match-beginning 0)))
1160 (let* ((keywords (org-element--collect-affiliated-keywords))
1161 (begin (car keywords
))
1162 ;; Empty blocks have no contents.
1163 (contents-begin (progn (forward-line)
1164 (and (< (point) block-end-line
)
1166 (contents-end (and contents-begin block-end-line
))
1167 (hidden (org-invisible-p2))
1168 (pos-before-blank (progn (goto-char block-end-line
)
1171 (end (progn (skip-chars-forward " \r\t\n" limit
)
1172 (skip-chars-backward " \t")
1173 (if (bolp) (point) (line-end-position)))))
1179 :contents-begin contents-begin
1180 :contents-end contents-end
1181 :post-blank
(count-lines pos-before-blank end
))
1182 (cadr keywords
)))))))))
1184 (defun org-element-quote-block-interpreter (quote-block contents
)
1185 "Interpret QUOTE-BLOCK element as Org syntax.
1186 CONTENTS is the contents of the element."
1187 (format "#+BEGIN_QUOTE\n%s#+END_QUOTE" contents
))
1192 (defun org-element-section-parser (limit)
1195 LIMIT bounds the search.
1197 Return a list whose CAR is `section' and CDR is a plist
1198 containing `:begin', `:end', `:contents-begin', `contents-end'
1199 and `:post-blank' keywords."
1201 ;; Beginning of section is the beginning of the first non-blank
1202 ;; line after previous headline.
1203 (let ((begin (point))
1204 (end (progn (org-with-limited-levels (outline-next-heading))
1206 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
1212 :contents-begin begin
1213 :contents-end pos-before-blank
1214 :post-blank
(count-lines pos-before-blank end
))))))
1216 (defun org-element-section-interpreter (section contents
)
1217 "Interpret SECTION element as Org syntax.
1218 CONTENTS is the contents of the element."
1224 (defun org-element-special-block-parser (limit)
1225 "Parse a special block.
1227 LIMIT bounds the search.
1229 Return a list whose CAR is `special-block' and CDR is a plist
1230 containing `:type', `:begin', `:end', `:hiddenp',
1231 `:contents-begin', `:contents-end' and `:post-blank' keywords.
1233 Assume point is at the beginning of the block."
1234 (let* ((case-fold-search t
)
1235 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(S-+\\)")
1236 (upcase (match-string-no-properties 1)))))
1237 (if (not (save-excursion
1239 (format "^[ \t]*#\\+END_%s[ \t]*$" type
) limit t
)))
1240 ;; Incomplete block: parse it as a paragraph.
1241 (org-element-paragraph-parser limit
)
1242 (let ((block-end-line (match-beginning 0)))
1244 (let* ((keywords (org-element--collect-affiliated-keywords))
1245 (begin (car keywords
))
1246 ;; Empty blocks have no contents.
1247 (contents-begin (progn (forward-line)
1248 (and (< (point) block-end-line
)
1250 (contents-end (and contents-begin block-end-line
))
1251 (hidden (org-invisible-p2))
1252 (pos-before-blank (progn (goto-char block-end-line
)
1255 (end (progn (skip-chars-forward " \r\t\n" limit
)
1256 (skip-chars-backward " \t")
1257 (if (bolp) (point) (line-end-position)))))
1258 (list 'special-block
1264 :contents-begin contents-begin
1265 :contents-end contents-end
1266 :post-blank
(count-lines pos-before-blank end
))
1267 (cadr keywords
)))))))))
1269 (defun org-element-special-block-interpreter (special-block contents
)
1270 "Interpret SPECIAL-BLOCK element as Org syntax.
1271 CONTENTS is the contents of the element."
1272 (let ((block-type (org-element-property :type special-block
)))
1273 (format "#+BEGIN_%s\n%s#+END_%s" block-type contents block-type
)))
1279 ;; For each element, a parser and an interpreter are also defined.
1280 ;; Both follow the same naming convention used for greater elements.
1282 ;; Also, as for greater elements, adding a new element type is done
1283 ;; through the following steps: implement a parser and an interpreter,
1284 ;; tweak `org-element--current-element' so that it recognizes the new
1285 ;; type and add that new type to `org-element-all-elements'.
1287 ;; As a special case, when the newly defined type is a block type,
1288 ;; `org-element-block-name-alist' has to be modified accordingly.
1293 (defun org-element-babel-call-parser (limit)
1294 "Parse a babel call.
1296 LIMIT bounds the search.
1298 Return a list whose CAR is `babel-call' and CDR is a plist
1299 containing `:begin', `:end', `:info' and `:post-blank' as
1302 (let ((case-fold-search t
)
1303 (info (progn (looking-at org-babel-block-lob-one-liner-regexp
)
1304 (org-babel-lob-get-info)))
1305 (begin (point-at-bol))
1306 (pos-before-blank (progn (forward-line) (point)))
1307 (end (progn (skip-chars-forward " \r\t\n" limit
)
1308 (skip-chars-backward " \t")
1309 (if (bolp) (point) (line-end-position)))))
1314 :post-blank
(count-lines pos-before-blank end
))))))
1316 (defun org-element-babel-call-interpreter (babel-call contents
)
1317 "Interpret BABEL-CALL element as Org syntax.
1319 (let* ((babel-info (org-element-property :info babel-call
))
1320 (main (car babel-info
))
1321 (post-options (nth 1 babel-info
)))
1323 (if (not (string-match "\\[\\(\\[.*?\\]\\)\\]" main
)) main
1324 ;; Remove redundant square brackets.
1325 (replace-match (match-string 1 main
) nil nil main
))
1326 (and post-options
(format "[%s]" post-options
)))))
1331 (defun org-element-clock-parser (limit)
1334 LIMIT bounds the search.
1336 Return a list whose CAR is `clock' and CDR is a plist containing
1337 `:status', `:value', `:time', `:begin', `:end' and `:post-blank'
1340 (let* ((case-fold-search nil
)
1342 (value (progn (search-forward org-clock-string
(line-end-position) t
)
1343 (org-skip-whitespace)
1344 (looking-at "\\[.*\\]")
1345 (org-match-string-no-properties 0)))
1346 (time (and (progn (goto-char (match-end 0))
1347 (looking-at " +=> +\\(\\S-+\\)[ \t]*$"))
1348 (org-match-string-no-properties 1)))
1349 (status (if time
'closed
'running
))
1350 (post-blank (let ((before-blank (progn (forward-line) (point))))
1351 (skip-chars-forward " \r\t\n" limit
)
1352 (skip-chars-backward " \t")
1353 (unless (bolp) (end-of-line))
1354 (count-lines before-blank
(point))))
1357 (list :status status
1362 :post-blank post-blank
)))))
1364 (defun org-element-clock-interpreter (clock contents
)
1365 "Interpret CLOCK element as Org syntax.
1367 (concat org-clock-string
" "
1368 (org-element-property :value clock
)
1369 (let ((time (org-element-property :time clock
)))
1374 (org-split-string time
":")))))))
1379 (defun org-element-comment-parser (limit)
1382 LIMIT bounds the search.
1384 Return a list whose CAR is `comment' and CDR is a plist
1385 containing `:begin', `:end', `:value' and `:post-blank'
1388 Assume point is at comment beginning."
1390 (let* ((keywords (org-element--collect-affiliated-keywords))
1391 (begin (car keywords
))
1392 (value (prog2 (looking-at "[ \t]*# ?")
1393 (buffer-substring-no-properties
1394 (match-end 0) (line-end-position))
1397 ;; Get comments ending.
1399 (while (and (< (point) limit
) (looking-at "[ \t]*#\\( \\|$\\)"))
1400 ;; Accumulate lines without leading hash and first
1405 (buffer-substring-no-properties
1406 (match-end 0) (line-end-position))))
1409 (end (progn (goto-char com-end
)
1410 (skip-chars-forward " \r\t\n" limit
)
1411 (skip-chars-backward " \t")
1412 (if (bolp) (point) (line-end-position)))))
1418 :post-blank
(count-lines com-end end
))
1419 (cadr keywords
))))))
1421 (defun org-element-comment-interpreter (comment contents
)
1422 "Interpret COMMENT element as Org syntax.
1424 (replace-regexp-in-string "^" "# " (org-element-property :value comment
)))
1429 (defun org-element-comment-block-parser (limit)
1430 "Parse an export block.
1432 LIMIT bounds the search.
1434 Return a list whose CAR is `comment-block' and CDR is a plist
1435 containing `:begin', `:end', `:hiddenp', `:value' and
1436 `:post-blank' keywords.
1438 Assume point is at comment block beginning."
1439 (let ((case-fold-search t
))
1440 (if (not (save-excursion
1441 (re-search-forward "^[ \t]*#\\+END_COMMENT[ \t]*$" limit t
)))
1442 ;; Incomplete block: parse it as a paragraph.
1443 (org-element-paragraph-parser limit
)
1444 (let ((contents-end (match-beginning 0)))
1446 (let* ((keywords (org-element--collect-affiliated-keywords))
1447 (begin (car keywords
))
1448 (contents-begin (progn (forward-line) (point)))
1449 (hidden (org-invisible-p2))
1450 (pos-before-blank (progn (goto-char contents-end
)
1453 (end (progn (skip-chars-forward " \r\t\n" limit
)
1454 (skip-chars-backward " \t")
1455 (if (bolp) (point) (line-end-position))))
1456 (value (buffer-substring-no-properties
1457 contents-begin contents-end
)))
1458 (list 'comment-block
1464 :post-blank
(count-lines pos-before-blank end
))
1465 (cadr keywords
)))))))))
1467 (defun org-element-comment-block-interpreter (comment-block contents
)
1468 "Interpret COMMENT-BLOCK element as Org syntax.
1470 (format "#+BEGIN_COMMENT\n%s#+END_COMMENT"
1471 (org-remove-indentation (org-element-property :value comment-block
))))
1476 (defun org-element-example-block-parser (limit)
1477 "Parse an example block.
1479 LIMIT bounds the search.
1481 Return a list whose CAR is `example-block' and CDR is a plist
1482 containing `:begin', `:end', `:number-lines', `:preserve-indent',
1483 `:retain-labels', `:use-labels', `:label-fmt', `:hiddenp',
1484 `:switches', `:value' and `:post-blank' keywords."
1485 (let ((case-fold-search t
))
1486 (if (not (save-excursion
1487 (re-search-forward "^[ \t]*#\\+END_EXAMPLE[ \t]*$" limit t
)))
1488 ;; Incomplete block: parse it as a paragraph.
1489 (org-element-paragraph-parser limit
)
1490 (let ((contents-end (match-beginning 0)))
1493 (progn (looking-at "^[ \t]*#\\+BEGIN_EXAMPLE\\(?: +\\(.*\\)\\)?")
1494 (org-match-string-no-properties 1)))
1495 ;; Switches analysis
1496 (number-lines (cond ((not switches
) nil
)
1497 ((string-match "-n\\>" switches
) 'new
)
1498 ((string-match "+n\\>" switches
) 'continued
)))
1499 (preserve-indent (and switches
(string-match "-i\\>" switches
)))
1500 ;; Should labels be retained in (or stripped from) example
1504 (not (string-match "-r\\>" switches
))
1505 (and number-lines
(string-match "-k\\>" switches
))))
1506 ;; What should code-references use - labels or
1510 (and retain-labels
(not (string-match "-k\\>" switches
)))))
1511 (label-fmt (and switches
1512 (string-match "-l +\"\\([^\"\n]+\\)\"" switches
)
1513 (match-string 1 switches
)))
1514 ;; Standard block parsing.
1515 (keywords (org-element--collect-affiliated-keywords))
1516 (begin (car keywords
))
1517 (contents-begin (progn (forward-line) (point)))
1518 (hidden (org-invisible-p2))
1519 (value (org-unescape-code-in-string
1520 (buffer-substring-no-properties
1521 contents-begin contents-end
)))
1522 (pos-before-blank (progn (goto-char contents-end
)
1525 (end (progn (skip-chars-forward " \r\t\n" limit
)
1526 (skip-chars-backward " \t")
1527 (if (bolp) (point) (line-end-position)))))
1528 (list 'example-block
1534 :number-lines number-lines
1535 :preserve-indent preserve-indent
1536 :retain-labels retain-labels
1537 :use-labels use-labels
1538 :label-fmt label-fmt
1540 :post-blank
(count-lines pos-before-blank end
))
1541 (cadr keywords
)))))))))
1543 (defun org-element-example-block-interpreter (example-block contents
)
1544 "Interpret EXAMPLE-BLOCK element as Org syntax.
1546 (let ((switches (org-element-property :switches example-block
)))
1547 (concat "#+BEGIN_EXAMPLE" (and switches
(concat " " switches
)) "\n"
1548 (org-remove-indentation
1549 (org-escape-code-in-string
1550 (org-element-property :value example-block
)))
1556 (defun org-element-export-block-parser (limit)
1557 "Parse an export block.
1559 LIMIT bounds the search.
1561 Return a list whose CAR is `export-block' and CDR is a plist
1562 containing `:begin', `:end', `:type', `:hiddenp', `:value' and
1563 `:post-blank' keywords.
1565 Assume point is at export-block beginning."
1566 (let* ((case-fold-search t
)
1567 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
1568 (upcase (org-match-string-no-properties 1)))))
1569 (if (not (save-excursion
1571 (format "^[ \t]*#\\+END_%s[ \t]*$" type
) limit t
)))
1572 ;; Incomplete block: parse it as a paragraph.
1573 (org-element-paragraph-parser limit
)
1574 (let ((contents-end (match-beginning 0)))
1576 (let* ((keywords (org-element--collect-affiliated-keywords))
1577 (begin (car keywords
))
1578 (contents-begin (progn (forward-line) (point)))
1579 (hidden (org-invisible-p2))
1580 (pos-before-blank (progn (goto-char contents-end
)
1583 (end (progn (skip-chars-forward " \r\t\n" limit
)
1584 (skip-chars-backward " \t")
1585 (if (bolp) (point) (line-end-position))))
1586 (value (buffer-substring-no-properties contents-begin
1595 :post-blank
(count-lines pos-before-blank end
))
1596 (cadr keywords
)))))))))
1598 (defun org-element-export-block-interpreter (export-block contents
)
1599 "Interpret EXPORT-BLOCK element as Org syntax.
1601 (let ((type (org-element-property :type export-block
)))
1602 (concat (format "#+BEGIN_%s\n" type
)
1603 (org-element-property :value export-block
)
1604 (format "#+END_%s" type
))))
1609 (defun org-element-fixed-width-parser (limit)
1610 "Parse a fixed-width section.
1612 LIMIT bounds the search.
1614 Return a list whose CAR is `fixed-width' and CDR is a plist
1615 containing `:begin', `:end', `:value' and `:post-blank' keywords.
1617 Assume point is at the beginning of the fixed-width area."
1619 (let* ((keywords (org-element--collect-affiliated-keywords))
1620 (begin (car keywords
))
1624 (while (and (< (point) limit
)
1625 (looking-at "[ \t]*:\\( \\|$\\)"))
1626 ;; Accumulate text without starting colons.
1629 (buffer-substring-no-properties
1630 (match-end 0) (point-at-eol))
1634 (end (progn (skip-chars-forward " \r\t\n" limit
)
1635 (skip-chars-backward " \t")
1636 (if (bolp) (point) (line-end-position)))))
1642 :post-blank
(count-lines end-area end
))
1643 (cadr keywords
))))))
1645 (defun org-element-fixed-width-interpreter (fixed-width contents
)
1646 "Interpret FIXED-WIDTH element as Org syntax.
1648 (replace-regexp-in-string
1649 "^" ": " (substring (org-element-property :value fixed-width
) 0 -
1)))
1652 ;;;; Horizontal Rule
1654 (defun org-element-horizontal-rule-parser (limit)
1655 "Parse an horizontal rule.
1657 LIMIT bounds the search.
1659 Return a list whose CAR is `horizontal-rule' and CDR is a plist
1660 containing `:begin', `:end' and `:post-blank' keywords."
1662 (let* ((keywords (org-element--collect-affiliated-keywords))
1663 (begin (car keywords
))
1664 (post-hr (progn (forward-line) (point)))
1665 (end (progn (skip-chars-forward " \r\t\n" limit
)
1666 (skip-chars-backward " \t")
1667 (if (bolp) (point) (line-end-position)))))
1668 (list 'horizontal-rule
1672 :post-blank
(count-lines post-hr end
))
1673 (cadr keywords
))))))
1675 (defun org-element-horizontal-rule-interpreter (horizontal-rule contents
)
1676 "Interpret HORIZONTAL-RULE element as Org syntax.
1683 (defun org-element-keyword-parser (limit)
1684 "Parse a keyword at point.
1686 LIMIT bounds the search.
1688 Return a list whose CAR is `keyword' and CDR is a plist
1689 containing `:key', `:value', `:begin', `:end' and `:post-blank'
1692 (let* ((case-fold-search t
)
1694 (key (progn (looking-at "[ \t]*#\\+\\(\\S-+\\):")
1695 (upcase (org-match-string-no-properties 1))))
1696 (value (org-trim (buffer-substring-no-properties
1697 (match-end 0) (point-at-eol))))
1698 (pos-before-blank (progn (forward-line) (point)))
1699 (end (progn (skip-chars-forward " \r\t\n" limit
)
1700 (skip-chars-backward " \t")
1701 (if (bolp) (point) (line-end-position)))))
1707 :post-blank
(count-lines pos-before-blank end
))))))
1709 (defun org-element-keyword-interpreter (keyword contents
)
1710 "Interpret KEYWORD element as Org syntax.
1713 (org-element-property :key keyword
)
1714 (org-element-property :value keyword
)))
1717 ;;;; Latex Environment
1719 (defun org-element-latex-environment-parser (limit)
1720 "Parse a LaTeX environment.
1722 LIMIT bounds the search.
1724 Return a list whose CAR is `latex-environment' and CDR is a plist
1725 containing `:begin', `:end', `:value' and `:post-blank'
1728 Assume point is at the beginning of the latex environment."
1730 (let* ((case-fold-search t
)
1731 (code-begin (point))
1732 (keywords (org-element--collect-affiliated-keywords))
1733 (begin (car keywords
))
1734 (env (progn (looking-at "^[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}")
1735 (regexp-quote (match-string 1))))
1737 (progn (re-search-forward
1738 (format "^[ \t]*\\\\end{%s}[ \t]*$" env
) limit t
)
1741 (value (buffer-substring-no-properties code-begin code-end
))
1742 (end (progn (skip-chars-forward " \r\t\n" limit
)
1743 (skip-chars-backward " \t")
1744 (if (bolp) (point) (line-end-position)))))
1745 (list 'latex-environment
1750 :post-blank
(count-lines code-end end
))
1751 (cadr keywords
))))))
1753 (defun org-element-latex-environment-interpreter (latex-environment contents
)
1754 "Interpret LATEX-ENVIRONMENT element as Org syntax.
1756 (org-element-property :value latex-environment
))
1761 (defun org-element-paragraph-parser (limit)
1764 LIMIT bounds the search.
1766 Return a list whose CAR is `paragraph' and CDR is a plist
1767 containing `:begin', `:end', `:contents-begin' and
1768 `:contents-end' and `:post-blank' keywords.
1770 Assume point is at the beginning of the paragraph."
1772 (let* ((contents-begin (point))
1773 ;; INNER-PAR-P is non-nil when paragraph is at the
1774 ;; beginning of an item or a footnote reference. In that
1775 ;; case, we mustn't look for affiliated keywords since they
1776 ;; belong to the container.
1777 (inner-par-p (not (bolp)))
1778 (keywords (unless inner-par-p
1779 (org-element--collect-affiliated-keywords)))
1780 (begin (if inner-par-p contents-begin
(car keywords
)))
1782 (let ((case-fold-search t
))
1784 (if (not (re-search-forward
1785 org-element-paragraph-separate limit
'm
))
1787 ;; A matching `org-element-paragraph-separate' is not
1788 ;; necessarily the end of the paragraph. In
1789 ;; particular, lines starting with # or : as a first
1790 ;; non-space character are ambiguous. We have check
1791 ;; if they are valid Org syntax (i.e. not an
1792 ;; incomplete keyword).
1796 ;; There's no ambiguity for other symbols or
1797 ;; empty lines: stop here.
1798 (looking-at "[ \t]*\\(?:[^:#]\\|$\\)")
1799 ;; Stop at valid fixed-width areas.
1800 (looking-at "[ \t]*:\\(?: \\|$\\)")
1802 (and (looking-at org-drawer-regexp
)
1805 "^[ \t]*:END:[ \t]*$" limit t
)))
1806 ;; Stop at valid comments.
1807 (looking-at "[ \t]*#\\(?: \\|$\\)")
1808 ;; Stop at valid dynamic blocks.
1809 (and (looking-at org-dblock-start-re
)
1812 "^[ \t]*#\\+END:?[ \t]*$" limit t
)))
1813 ;; Stop at valid blocks.
1815 "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
1818 (format "^[ \t]*#\\+END_%s[ \t]*$"
1821 ;; Stop at valid latex environments.
1823 "^[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}[ \t]*$")
1826 (format "^[ \t]*\\\\end{%s}[ \t]*$"
1829 ;; Stop at valid keywords.
1830 (looking-at "[ \t]*#\\+\\S-+:")
1831 ;; Skip everything else.
1835 (re-search-forward org-element-paragraph-separate
1837 (beginning-of-line)))
1838 (if (= (point) limit
) limit
1839 (goto-char (line-beginning-position)))))
1840 (contents-end (progn (skip-chars-backward " \r\t\n" contents-begin
)
1843 (end (progn (skip-chars-forward " \r\t\n" limit
)
1844 (skip-chars-backward " \t")
1845 (if (bolp) (point) (line-end-position)))))
1850 :contents-begin contents-begin
1851 :contents-end contents-end
1852 :post-blank
(count-lines before-blank end
))
1853 (cadr keywords
))))))
1855 (defun org-element-paragraph-interpreter (paragraph contents
)
1856 "Interpret PARAGRAPH element as Org syntax.
1857 CONTENTS is the contents of the element."
1863 (defun org-element-planning-parser (limit)
1866 LIMIT bounds the search.
1868 Return a list whose CAR is `planning' and CDR is a plist
1869 containing `:closed', `:deadline', `:scheduled', `:begin', `:end'
1870 and `:post-blank' keywords."
1872 (let* ((case-fold-search nil
)
1874 (post-blank (let ((before-blank (progn (forward-line) (point))))
1875 (skip-chars-forward " \r\t\n" limit
)
1876 (skip-chars-backward " \t")
1877 (unless (bolp) (end-of-line))
1878 (count-lines before-blank
(point))))
1880 closed deadline scheduled
)
1882 (while (re-search-forward org-keyword-time-not-clock-regexp
1883 (line-end-position) t
)
1884 (goto-char (match-end 1))
1885 (org-skip-whitespace)
1886 (let ((time (buffer-substring-no-properties
1887 (1+ (point)) (1- (match-end 0))))
1888 (keyword (match-string 1)))
1889 (cond ((equal keyword org-closed-string
) (setq closed time
))
1890 ((equal keyword org-deadline-string
) (setq deadline time
))
1891 (t (setq scheduled time
)))))
1893 (list :closed closed
1895 :scheduled scheduled
1898 :post-blank post-blank
)))))
1900 (defun org-element-planning-interpreter (planning contents
)
1901 "Interpret PLANNING element as Org syntax.
1906 (list (let ((closed (org-element-property :closed planning
)))
1907 (when closed
(concat org-closed-string
" [" closed
"]")))
1908 (let ((deadline (org-element-property :deadline planning
)))
1909 (when deadline
(concat org-deadline-string
" <" deadline
">")))
1910 (let ((scheduled (org-element-property :scheduled planning
)))
1912 (concat org-scheduled-string
" <" scheduled
">")))))
1916 ;;;; Property Drawer
1918 (defun org-element-property-drawer-parser (limit)
1919 "Parse a property drawer.
1921 LIMIT bounds the search.
1923 Return a list whose CAR is `property-drawer' and CDR is a plist
1924 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1925 `:contents-end', `:properties' and `:post-blank' keywords.
1927 Assume point is at the beginning of the property drawer."
1929 (let ((case-fold-search t
)
1931 (prop-begin (progn (forward-line) (point)))
1932 (hidden (org-invisible-p2))
1935 (while (not (looking-at "^[ \t]*:END:[ \t]*$"))
1936 (when (looking-at "[ \t]*:\\([A-Za-z][-_A-Za-z0-9]*\\):")
1937 (push (cons (org-match-string-no-properties 1)
1939 (buffer-substring-no-properties
1940 (match-end 0) (point-at-eol))))
1944 (prop-end (progn (re-search-forward "^[ \t]*:END:" limit t
)
1946 (pos-before-blank (progn (forward-line) (point)))
1947 (end (progn (skip-chars-forward " \r\t\n" limit
)
1948 (skip-chars-backward " \t")
1949 (if (bolp) (point) (line-end-position)))))
1950 (list 'property-drawer
1954 :properties properties
1955 :post-blank
(count-lines pos-before-blank end
))))))
1957 (defun org-element-property-drawer-interpreter (property-drawer contents
)
1958 "Interpret PROPERTY-DRAWER element as Org syntax.
1960 (let ((props (org-element-property :properties property-drawer
)))
1963 (mapconcat (lambda (p)
1964 (format org-property-format
(format ":%s:" (car p
)) (cdr p
)))
1965 (nreverse props
) "\n")
1971 (defun org-element-quote-section-parser (limit)
1972 "Parse a quote section.
1974 LIMIT bounds the search.
1976 Return a list whose CAR is `quote-section' and CDR is a plist
1977 containing `:begin', `:end', `:value' and `:post-blank' keywords.
1979 Assume point is at beginning of the section."
1981 (let* ((begin (point))
1982 (end (progn (org-with-limited-levels (outline-next-heading))
1984 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
1987 (value (buffer-substring-no-properties begin pos-before-blank
)))
1988 (list 'quote-section
1992 :post-blank
(count-lines pos-before-blank end
))))))
1994 (defun org-element-quote-section-interpreter (quote-section contents
)
1995 "Interpret QUOTE-SECTION element as Org syntax.
1997 (org-element-property :value quote-section
))
2002 (defun org-element-src-block-parser (limit)
2005 LIMIT bounds the search.
2007 Return a list whose CAR is `src-block' and CDR is a plist
2008 containing `:language', `:switches', `:parameters', `:begin',
2009 `:end', `:hiddenp', `:number-lines', `:retain-labels',
2010 `:use-labels', `:label-fmt', `:preserve-indent', `:value' and
2011 `:post-blank' keywords.
2013 Assume point is at the beginning of the block."
2014 (let ((case-fold-search t
))
2015 (if (not (save-excursion (re-search-forward "^[ \t]*#\\+END_SRC[ \t]*$"
2017 ;; Incomplete block: parse it as a paragraph.
2018 (org-element-paragraph-parser limit
)
2019 (let ((contents-end (match-beginning 0)))
2021 (let* ((keywords (org-element--collect-affiliated-keywords))
2022 ;; Get beginning position.
2023 (begin (car keywords
))
2024 ;; Get language as a string.
2028 (concat "^[ \t]*#\\+BEGIN_SRC"
2029 "\\(?: +\\(\\S-+\\)\\)?"
2030 "\\(\\(?: +\\(?:-l \".*?\"\\|[-+][A-Za-z]\\)\\)+\\)?"
2032 (org-match-string-no-properties 1)))
2034 (switches (org-match-string-no-properties 2))
2036 (parameters (org-match-string-no-properties 3))
2037 ;; Switches analysis
2038 (number-lines (cond ((not switches
) nil
)
2039 ((string-match "-n\\>" switches
) 'new
)
2040 ((string-match "+n\\>" switches
) 'continued
)))
2041 (preserve-indent (and switches
(string-match "-i\\>" switches
)))
2042 (label-fmt (and switches
2043 (string-match "-l +\"\\([^\"\n]+\\)\"" switches
)
2044 (match-string 1 switches
)))
2045 ;; Should labels be retained in (or stripped from)
2049 (not (string-match "-r\\>" switches
))
2050 (and number-lines
(string-match "-k\\>" switches
))))
2051 ;; What should code-references use - labels or
2055 (and retain-labels
(not (string-match "-k\\>" switches
)))))
2056 ;; Get visibility status.
2057 (hidden (progn (forward-line) (org-invisible-p2)))
2059 (value (org-unescape-code-in-string
2060 (buffer-substring-no-properties (point) contents-end
)))
2061 (pos-before-blank (progn (goto-char contents-end
)
2064 ;; Get position after ending blank lines.
2065 (end (progn (skip-chars-forward " \r\t\n" limit
)
2066 (skip-chars-backward " \t")
2067 (if (bolp) (point) (line-end-position)))))
2070 (list :language language
2071 :switches
(and (org-string-nw-p switches
)
2072 (org-trim switches
))
2073 :parameters
(and (org-string-nw-p parameters
)
2074 (org-trim parameters
))
2077 :number-lines number-lines
2078 :preserve-indent preserve-indent
2079 :retain-labels retain-labels
2080 :use-labels use-labels
2081 :label-fmt label-fmt
2084 :post-blank
(count-lines pos-before-blank end
))
2085 (cadr keywords
)))))))))
2087 (defun org-element-src-block-interpreter (src-block contents
)
2088 "Interpret SRC-BLOCK element as Org syntax.
2090 (let ((lang (org-element-property :language src-block
))
2091 (switches (org-element-property :switches src-block
))
2092 (params (org-element-property :parameters src-block
))
2093 (value (let ((val (org-element-property :value src-block
)))
2095 (org-src-preserve-indentation val
)
2096 ((zerop org-edit-src-content-indentation
)
2097 (org-remove-indentation val
))
2099 (let ((ind (make-string
2100 org-edit-src-content-indentation
32)))
2101 (replace-regexp-in-string
2102 "\\(^\\)[ \t]*\\S-" ind
2103 (org-remove-indentation val
) nil nil
1)))))))
2104 (concat (format "#+BEGIN_SRC%s\n"
2105 (concat (and lang
(concat " " lang
))
2106 (and switches
(concat " " switches
))
2107 (and params
(concat " " params
))))
2108 (org-escape-code-in-string value
)
2114 (defun org-element-table-parser (limit)
2115 "Parse a table at point.
2117 LIMIT bounds the search.
2119 Return a list whose CAR is `table' and CDR is a plist containing
2120 `:begin', `:end', `:tblfm', `:type', `:contents-begin',
2121 `:contents-end', `:value' and `:post-blank' keywords.
2123 Assume point is at the beginning of the table."
2125 (let* ((case-fold-search t
)
2126 (table-begin (point))
2127 (type (if (org-at-table.el-p
) 'table.el
'org
))
2128 (keywords (org-element--collect-affiliated-keywords))
2129 (begin (car keywords
))
2131 (if (re-search-forward org-table-any-border-regexp limit
'm
)
2132 (goto-char (match-beginning 0))
2135 (while (looking-at "[ \t]*#\\+TBLFM: +\\(.*\\)[ \t]*$")
2136 (push (org-match-string-no-properties 1) acc
)
2139 (pos-before-blank (point))
2140 (end (progn (skip-chars-forward " \r\t\n" limit
)
2141 (skip-chars-backward " \t")
2142 (if (bolp) (point) (line-end-position)))))
2149 ;; Only `org' tables have contents. `table.el' tables
2150 ;; use a `:value' property to store raw table as
2152 :contents-begin
(and (eq type
'org
) table-begin
)
2153 :contents-end
(and (eq type
'org
) table-end
)
2154 :value
(and (eq type
'table.el
)
2155 (buffer-substring-no-properties
2156 table-begin table-end
))
2157 :post-blank
(count-lines pos-before-blank end
))
2158 (cadr keywords
))))))
2160 (defun org-element-table-interpreter (table contents
)
2161 "Interpret TABLE element as Org syntax.
2163 (if (eq (org-element-property :type table
) 'table.el
)
2164 (org-remove-indentation (org-element-property :value table
))
2165 (concat (with-temp-buffer (insert contents
)
2168 (mapconcat (lambda (fm) (concat "#+TBLFM: " fm
))
2169 (reverse (org-element-property :tblfm table
))
2175 (defun org-element-table-row-parser (limit)
2176 "Parse table row at point.
2178 LIMIT bounds the search.
2180 Return a list whose CAR is `table-row' and CDR is a plist
2181 containing `:begin', `:end', `:contents-begin', `:contents-end',
2182 `:type' and `:post-blank' keywords."
2184 (let* ((type (if (looking-at "^[ \t]*|-") 'rule
'standard
))
2186 ;; A table rule has no contents. In that case, ensure
2187 ;; CONTENTS-BEGIN matches CONTENTS-END.
2188 (contents-begin (and (eq type
'standard
)
2189 (search-forward "|")
2191 (contents-end (and (eq type
'standard
)
2194 (skip-chars-backward " \t")
2196 (end (progn (forward-line) (point))))
2201 :contents-begin contents-begin
2202 :contents-end contents-end
2205 (defun org-element-table-row-interpreter (table-row contents
)
2206 "Interpret TABLE-ROW element as Org syntax.
2207 CONTENTS is the contents of the table row."
2208 (if (eq (org-element-property :type table-row
) 'rule
) "|-"
2209 (concat "| " contents
)))
2214 (defun org-element-verse-block-parser (limit)
2215 "Parse a verse block.
2217 LIMIT bounds the search.
2219 Return a list whose CAR is `verse-block' and CDR is a plist
2220 containing `:begin', `:end', `:contents-begin', `:contents-end',
2221 `:hiddenp' and `:post-blank' keywords.
2223 Assume point is at beginning of the block."
2224 (let ((case-fold-search t
))
2225 (if (not (save-excursion
2226 (re-search-forward "^[ \t]*#\\+END_VERSE[ \t]*$" limit t
)))
2227 ;; Incomplete block: parse it as a paragraph.
2228 (org-element-paragraph-parser limit
)
2229 (let ((contents-end (match-beginning 0)))
2231 (let* ((keywords (org-element--collect-affiliated-keywords))
2232 (begin (car keywords
))
2233 (hidden (progn (forward-line) (org-invisible-p2)))
2234 (contents-begin (point))
2235 (pos-before-blank (progn (goto-char contents-end
)
2238 (end (progn (skip-chars-forward " \r\t\n" limit
)
2239 (skip-chars-backward " \t")
2240 (if (bolp) (point) (line-end-position)))))
2245 :contents-begin contents-begin
2246 :contents-end contents-end
2248 :post-blank
(count-lines pos-before-blank end
))
2249 (cadr keywords
)))))))))
2251 (defun org-element-verse-block-interpreter (verse-block contents
)
2252 "Interpret VERSE-BLOCK element as Org syntax.
2253 CONTENTS is verse block contents."
2254 (format "#+BEGIN_VERSE\n%s#+END_VERSE" contents
))
2260 ;; Unlike to elements, interstices can be found between objects.
2261 ;; That's why, along with the parser, successor functions are provided
2262 ;; for each object. Some objects share the same successor (i.e. `code'
2263 ;; and `verbatim' objects).
2265 ;; A successor must accept a single argument bounding the search. It
2266 ;; will return either a cons cell whose CAR is the object's type, as
2267 ;; a symbol, and CDR the position of its next occurrence, or nil.
2269 ;; Successors follow the naming convention:
2270 ;; org-element-NAME-successor, where NAME is the name of the
2271 ;; successor, as defined in `org-element-all-successors'.
2273 ;; Some object types (i.e. `italic') are recursive. Restrictions on
2274 ;; object types they can contain will be specified in
2275 ;; `org-element-object-restrictions'.
2277 ;; Adding a new type of object is simple. Implement a successor,
2278 ;; a parser, and an interpreter for it, all following the naming
2279 ;; convention. Register type in `org-element-all-objects' and
2280 ;; successor in `org-element-all-successors'. Maybe tweak
2281 ;; restrictions about it, and that's it.
2286 (defun org-element-bold-parser ()
2287 "Parse bold object at point.
2289 Return a list whose CAR is `bold' and CDR is a plist with
2290 `:begin', `:end', `:contents-begin' and `:contents-end' and
2291 `:post-blank' keywords.
2293 Assume point is at the first star marker."
2295 (unless (bolp) (backward-char 1))
2296 (looking-at org-emph-re
)
2297 (let ((begin (match-beginning 2))
2298 (contents-begin (match-beginning 4))
2299 (contents-end (match-end 4))
2300 (post-blank (progn (goto-char (match-end 2))
2301 (skip-chars-forward " \t")))
2306 :contents-begin contents-begin
2307 :contents-end contents-end
2308 :post-blank post-blank
)))))
2310 (defun org-element-bold-interpreter (bold contents
)
2311 "Interpret BOLD object as Org syntax.
2312 CONTENTS is the contents of the object."
2313 (format "*%s*" contents
))
2315 (defun org-element-text-markup-successor (limit)
2316 "Search for the next text-markup object.
2318 LIMIT bounds the search.
2320 Return value is a cons cell whose CAR is a symbol among `bold',
2321 `italic', `underline', `strike-through', `code' and `verbatim'
2322 and CDR is beginning position."
2324 (unless (bolp) (backward-char))
2325 (when (re-search-forward org-emph-re limit t
)
2326 (let ((marker (match-string 3)))
2328 ((equal marker
"*") 'bold
)
2329 ((equal marker
"/") 'italic
)
2330 ((equal marker
"_") 'underline
)
2331 ((equal marker
"+") 'strike-through
)
2332 ((equal marker
"~") 'code
)
2333 ((equal marker
"=") 'verbatim
)
2334 (t (error "Unknown marker at %d" (match-beginning 3))))
2335 (match-beginning 2))))))
2340 (defun org-element-code-parser ()
2341 "Parse code object at point.
2343 Return a list whose CAR is `code' and CDR is a plist with
2344 `:value', `:begin', `:end' and `:post-blank' keywords.
2346 Assume point is at the first tilde marker."
2348 (unless (bolp) (backward-char 1))
2349 (looking-at org-emph-re
)
2350 (let ((begin (match-beginning 2))
2351 (value (org-match-string-no-properties 4))
2352 (post-blank (progn (goto-char (match-end 2))
2353 (skip-chars-forward " \t")))
2359 :post-blank post-blank
)))))
2361 (defun org-element-code-interpreter (code contents
)
2362 "Interpret CODE object as Org syntax.
2364 (format "~%s~" (org-element-property :value code
)))
2369 (defun org-element-entity-parser ()
2370 "Parse entity at point.
2372 Return a list whose CAR is `entity' and CDR a plist with
2373 `:begin', `:end', `:latex', `:latex-math-p', `:html', `:latin1',
2374 `:utf-8', `:ascii', `:use-brackets-p' and `:post-blank' as
2377 Assume point is at the beginning of the entity."
2379 (looking-at "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)")
2380 (let* ((value (org-entity-get (match-string 1)))
2381 (begin (match-beginning 0))
2382 (bracketsp (string= (match-string 2) "{}"))
2383 (post-blank (progn (goto-char (match-end 1))
2384 (when bracketsp
(forward-char 2))
2385 (skip-chars-forward " \t")))
2388 (list :name
(car value
)
2389 :latex
(nth 1 value
)
2390 :latex-math-p
(nth 2 value
)
2392 :ascii
(nth 4 value
)
2393 :latin1
(nth 5 value
)
2394 :utf-8
(nth 6 value
)
2397 :use-brackets-p bracketsp
2398 :post-blank post-blank
)))))
2400 (defun org-element-entity-interpreter (entity contents
)
2401 "Interpret ENTITY object as Org syntax.
2404 (org-element-property :name entity
)
2405 (when (org-element-property :use-brackets-p entity
) "{}")))
2407 (defun org-element-latex-or-entity-successor (limit)
2408 "Search for the next latex-fragment or entity object.
2410 LIMIT bounds the search.
2412 Return value is a cons cell whose CAR is `entity' or
2413 `latex-fragment' and CDR is beginning position."
2415 (unless (bolp) (backward-char))
2417 (remove "begin" (plist-get org-format-latex-options
:matchers
)))
2418 ;; ENTITY-RE matches both LaTeX commands and Org entities.
2420 "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))
2421 (when (re-search-forward
2422 (concat (mapconcat (lambda (e) (nth 1 (assoc e org-latex-regexps
)))
2426 (goto-char (match-beginning 0))
2427 (if (looking-at entity-re
)
2428 ;; Determine if it's a real entity or a LaTeX command.
2429 (cons (if (org-entity-get (match-string 1)) 'entity
'latex-fragment
)
2430 (match-beginning 0))
2431 ;; No entity nor command: point is at a LaTeX fragment.
2432 ;; Determine its type to get the correct beginning position.
2433 (cons 'latex-fragment
2436 (when (looking-at (nth 1 (assoc e org-latex-regexps
)))
2439 (nth 2 (assoc e org-latex-regexps
))))))
2446 (defun org-element-export-snippet-parser ()
2447 "Parse export snippet at point.
2449 Return a list whose CAR is `export-snippet' and CDR a plist with
2450 `:begin', `:end', `:back-end', `:value' and `:post-blank' as
2453 Assume point is at the beginning of the snippet."
2455 (re-search-forward "@@\\([-A-Za-z0-9]+\\):" nil t
)
2456 (let* ((begin (match-beginning 0))
2457 (back-end (org-match-string-no-properties 1))
2458 (value (buffer-substring-no-properties
2460 (progn (re-search-forward "@@" nil t
) (match-beginning 0))))
2461 (post-blank (skip-chars-forward " \t"))
2463 (list 'export-snippet
2464 (list :back-end back-end
2468 :post-blank post-blank
)))))
2470 (defun org-element-export-snippet-interpreter (export-snippet contents
)
2471 "Interpret EXPORT-SNIPPET object as Org syntax.
2474 (org-element-property :back-end export-snippet
)
2475 (org-element-property :value export-snippet
)))
2477 (defun org-element-export-snippet-successor (limit)
2478 "Search for the next export-snippet object.
2480 LIMIT bounds the search.
2482 Return value is a cons cell whose CAR is `export-snippet' and CDR
2483 its beginning position."
2486 (when (and (re-search-forward "@@[-A-Za-z0-9]+:" limit t
)
2487 (setq beg
(match-beginning 0))
2488 (search-forward "@@" limit t
))
2489 (cons 'export-snippet beg
)))))
2492 ;;;; Footnote Reference
2494 (defun org-element-footnote-reference-parser ()
2495 "Parse footnote reference at point.
2497 Return a list whose CAR is `footnote-reference' and CDR a plist
2498 with `:label', `:type', `:inline-definition', `:begin', `:end'
2499 and `:post-blank' as keywords."
2501 (looking-at org-footnote-re
)
2502 (let* ((begin (point))
2503 (label (or (org-match-string-no-properties 2)
2504 (org-match-string-no-properties 3)
2505 (and (match-string 1)
2506 (concat "fn:" (org-match-string-no-properties 1)))))
2507 (type (if (or (not label
) (match-string 1)) 'inline
'standard
))
2508 (inner-begin (match-end 0))
2512 (while (and (> count
0) (re-search-forward "[][]" nil t
))
2513 (if (equal (match-string 0) "[") (incf count
) (decf count
)))
2515 (post-blank (progn (goto-char (1+ inner-end
))
2516 (skip-chars-forward " \t")))
2519 (list 'footnote-reference
2524 :post-blank post-blank
))))
2525 (org-element-put-property
2526 footnote-reference
:inline-definition
2527 (and (eq type
'inline
)
2528 (org-element-parse-secondary-string
2529 (buffer-substring inner-begin inner-end
)
2530 (org-element-restriction 'footnote-reference
)
2531 footnote-reference
))))))
2533 (defun org-element-footnote-reference-interpreter (footnote-reference contents
)
2534 "Interpret FOOTNOTE-REFERENCE object as Org syntax.
2536 (let ((label (or (org-element-property :label footnote-reference
) "fn:"))
2539 (org-element-property :inline-definition footnote-reference
)))
2540 (if (not inline-def
) ""
2541 (concat ":" (org-element-interpret-data inline-def
))))))
2542 (format "[%s]" (concat label def
))))
2544 (defun org-element-footnote-reference-successor (limit)
2545 "Search for the next footnote-reference object.
2547 LIMIT bounds the search.
2549 Return value is a cons cell whose CAR is `footnote-reference' and
2550 CDR is beginning position."
2553 (while (re-search-forward org-footnote-re limit t
)
2555 (let ((beg (match-beginning 0))
2558 (while (re-search-forward "[][]" limit t
)
2559 (if (equal (match-string 0) "[") (incf count
) (decf count
))
2561 (throw 'exit
(cons 'footnote-reference beg
))))))))))
2564 ;;;; Inline Babel Call
2566 (defun org-element-inline-babel-call-parser ()
2567 "Parse inline babel call at point.
2569 Return a list whose CAR is `inline-babel-call' and CDR a plist
2570 with `:begin', `:end', `:info' and `:post-blank' as keywords.
2572 Assume point is at the beginning of the babel call."
2574 (unless (bolp) (backward-char))
2575 (looking-at org-babel-inline-lob-one-liner-regexp
)
2576 (let ((info (save-match-data (org-babel-lob-get-info)))
2577 (begin (match-end 1))
2578 (post-blank (progn (goto-char (match-end 0))
2579 (skip-chars-forward " \t")))
2581 (list 'inline-babel-call
2585 :post-blank post-blank
)))))
2587 (defun org-element-inline-babel-call-interpreter (inline-babel-call contents
)
2588 "Interpret INLINE-BABEL-CALL object as Org syntax.
2590 (let* ((babel-info (org-element-property :info inline-babel-call
))
2591 (main-source (car babel-info
))
2592 (post-options (nth 1 babel-info
)))
2594 (if (string-match "\\[\\(\\[.*?\\]\\)\\]" main-source
)
2595 ;; Remove redundant square brackets.
2597 (match-string 1 main-source
) nil nil main-source
)
2599 (and post-options
(format "[%s]" post-options
)))))
2601 (defun org-element-inline-babel-call-successor (limit)
2602 "Search for the next inline-babel-call object.
2604 LIMIT bounds the search.
2606 Return value is a cons cell whose CAR is `inline-babel-call' and
2607 CDR is beginning position."
2609 ;; Use a simplified version of
2610 ;; `org-babel-inline-lob-one-liner-regexp'.
2611 (when (re-search-forward
2612 "call_\\([^()\n]+?\\)\\(?:\\[.*?\\]\\)?([^\n]*?)\\(\\[.*?\\]\\)?"
2614 (cons 'inline-babel-call
(match-beginning 0)))))
2617 ;;;; Inline Src Block
2619 (defun org-element-inline-src-block-parser ()
2620 "Parse inline source block at point.
2622 LIMIT bounds the search.
2624 Return a list whose CAR is `inline-src-block' and CDR a plist
2625 with `:begin', `:end', `:language', `:value', `:parameters' and
2626 `:post-blank' as keywords.
2628 Assume point is at the beginning of the inline src block."
2630 (unless (bolp) (backward-char))
2631 (looking-at org-babel-inline-src-block-regexp
)
2632 (let ((begin (match-beginning 1))
2633 (language (org-match-string-no-properties 2))
2634 (parameters (org-match-string-no-properties 4))
2635 (value (org-match-string-no-properties 5))
2636 (post-blank (progn (goto-char (match-end 0))
2637 (skip-chars-forward " \t")))
2639 (list 'inline-src-block
2640 (list :language language
2642 :parameters parameters
2645 :post-blank post-blank
)))))
2647 (defun org-element-inline-src-block-interpreter (inline-src-block contents
)
2648 "Interpret INLINE-SRC-BLOCK object as Org syntax.
2650 (let ((language (org-element-property :language inline-src-block
))
2651 (arguments (org-element-property :parameters inline-src-block
))
2652 (body (org-element-property :value inline-src-block
)))
2653 (format "src_%s%s{%s}"
2655 (if arguments
(format "[%s]" arguments
) "")
2658 (defun org-element-inline-src-block-successor (limit)
2659 "Search for the next inline-babel-call element.
2661 LIMIT bounds the search.
2663 Return value is a cons cell whose CAR is `inline-babel-call' and
2664 CDR is beginning position."
2666 (unless (bolp) (backward-char))
2667 (when (re-search-forward org-babel-inline-src-block-regexp limit t
)
2668 (cons 'inline-src-block
(match-beginning 1)))))
2672 (defun org-element-italic-parser ()
2673 "Parse italic object at point.
2675 Return a list whose CAR is `italic' and CDR is a plist with
2676 `:begin', `:end', `:contents-begin' and `:contents-end' and
2677 `:post-blank' keywords.
2679 Assume point is at the first slash marker."
2681 (unless (bolp) (backward-char 1))
2682 (looking-at org-emph-re
)
2683 (let ((begin (match-beginning 2))
2684 (contents-begin (match-beginning 4))
2685 (contents-end (match-end 4))
2686 (post-blank (progn (goto-char (match-end 2))
2687 (skip-chars-forward " \t")))
2692 :contents-begin contents-begin
2693 :contents-end contents-end
2694 :post-blank post-blank
)))))
2696 (defun org-element-italic-interpreter (italic contents
)
2697 "Interpret ITALIC object as Org syntax.
2698 CONTENTS is the contents of the object."
2699 (format "/%s/" contents
))
2704 (defun org-element-latex-fragment-parser ()
2705 "Parse latex fragment at point.
2707 Return a list whose CAR is `latex-fragment' and CDR a plist with
2708 `:value', `:begin', `:end', and `:post-blank' as keywords.
2710 Assume point is at the beginning of the latex fragment."
2712 (let* ((begin (point))
2716 (let ((latex-regexp (nth 1 (assoc e org-latex-regexps
))))
2717 (when (or (looking-at latex-regexp
)
2721 (looking-at latex-regexp
))))
2722 (throw 'exit
(nth 2 (assoc e org-latex-regexps
))))))
2723 (plist-get org-format-latex-options
:matchers
))
2724 ;; None found: it's a macro.
2725 (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")
2727 (value (match-string-no-properties substring-match
))
2728 (post-blank (progn (goto-char (match-end substring-match
))
2729 (skip-chars-forward " \t")))
2731 (list 'latex-fragment
2735 :post-blank post-blank
)))))
2737 (defun org-element-latex-fragment-interpreter (latex-fragment contents
)
2738 "Interpret LATEX-FRAGMENT object as Org syntax.
2740 (org-element-property :value latex-fragment
))
2744 (defun org-element-line-break-parser ()
2745 "Parse line break at point.
2747 Return a list whose CAR is `line-break', and CDR a plist with
2748 `:begin', `:end' and `:post-blank' keywords.
2750 Assume point is at the beginning of the line break."
2751 (list 'line-break
(list :begin
(point) :end
(point-at-eol) :post-blank
0)))
2753 (defun org-element-line-break-interpreter (line-break contents
)
2754 "Interpret LINE-BREAK object as Org syntax.
2758 (defun org-element-line-break-successor (limit)
2759 "Search for the next line-break object.
2761 LIMIT bounds the search.
2763 Return value is a cons cell whose CAR is `line-break' and CDR is
2764 beginning position."
2766 (let ((beg (and (re-search-forward "[^\\\\]\\(\\\\\\\\\\)[ \t]*$" limit t
)
2767 (goto-char (match-beginning 1)))))
2768 ;; A line break can only happen on a non-empty line.
2769 (when (and beg
(re-search-backward "\\S-" (point-at-bol) t
))
2770 (cons 'line-break beg
)))))
2775 (defun org-element-link-parser ()
2776 "Parse link at point.
2778 Return a list whose CAR is `link' and CDR a plist with `:type',
2779 `:path', `:raw-link', `:begin', `:end', `:contents-begin',
2780 `:contents-end' and `:post-blank' as keywords.
2782 Assume point is at the beginning of the link."
2784 (let ((begin (point))
2785 end contents-begin contents-end link-end post-blank path type
2788 ;; Type 1: Text targeted from a radio target.
2789 ((and org-target-link-regexp
(looking-at org-target-link-regexp
))
2791 link-end
(match-end 0)
2792 path
(org-match-string-no-properties 0)))
2793 ;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]]
2794 ((looking-at org-bracket-link-regexp
)
2795 (setq contents-begin
(match-beginning 3)
2796 contents-end
(match-end 3)
2797 link-end
(match-end 0)
2798 ;; RAW-LINK is the original link.
2799 raw-link
(org-match-string-no-properties 1)
2800 link
(org-translate-link
2801 (org-link-expand-abbrev
2802 (org-link-unescape raw-link
))))
2803 ;; Determine TYPE of link and set PATH accordingly.
2806 ((or (file-name-absolute-p link
) (string-match "^\\.\\.?/" link
))
2807 (setq type
"file" path link
))
2808 ;; Explicit type (http, irc, bbdb...). See `org-link-types'.
2809 ((string-match org-link-re-with-space3 link
)
2810 (setq type
(match-string 1 link
) path
(match-string 2 link
)))
2811 ;; Id type: PATH is the id.
2812 ((string-match "^id:\\([-a-f0-9]+\\)" link
)
2813 (setq type
"id" path
(match-string 1 link
)))
2814 ;; Code-ref type: PATH is the name of the reference.
2815 ((string-match "^(\\(.*\\))$" link
)
2816 (setq type
"coderef" path
(match-string 1 link
)))
2817 ;; Custom-id type: PATH is the name of the custom id.
2818 ((= (aref link
0) ?
#)
2819 (setq type
"custom-id" path
(substring link
1)))
2820 ;; Fuzzy type: Internal link either matches a target, an
2821 ;; headline name or nothing. PATH is the target or
2823 (t (setq type
"fuzzy" path link
))))
2824 ;; Type 3: Plain link, i.e. http://orgmode.org
2825 ((looking-at org-plain-link-re
)
2826 (setq raw-link
(org-match-string-no-properties 0)
2827 type
(org-match-string-no-properties 1)
2828 path
(org-match-string-no-properties 2)
2829 link-end
(match-end 0)))
2830 ;; Type 4: Angular link, i.e. <http://orgmode.org>
2831 ((looking-at org-angle-link-re
)
2832 (setq raw-link
(buffer-substring-no-properties
2833 (match-beginning 1) (match-end 2))
2834 type
(org-match-string-no-properties 1)
2835 path
(org-match-string-no-properties 2)
2836 link-end
(match-end 0))))
2837 ;; In any case, deduce end point after trailing white space from
2838 ;; LINK-END variable.
2839 (setq post-blank
(progn (goto-char link-end
) (skip-chars-forward " \t"))
2844 :raw-link
(or raw-link path
)
2847 :contents-begin contents-begin
2848 :contents-end contents-end
2849 :post-blank post-blank
)))))
2851 (defun org-element-link-interpreter (link contents
)
2852 "Interpret LINK object as Org syntax.
2853 CONTENTS is the contents of the object, or nil."
2854 (let ((type (org-element-property :type link
))
2855 (raw-link (org-element-property :raw-link link
)))
2856 (if (string= type
"radio") raw-link
2859 (if contents
(format "[%s]" contents
) "")))))
2861 (defun org-element-link-successor (limit)
2862 "Search for the next link object.
2864 LIMIT bounds the search.
2866 Return value is a cons cell whose CAR is `link' and CDR is
2867 beginning position."
2870 (if (not org-target-link-regexp
) org-any-link-re
2871 (concat org-any-link-re
"\\|" org-target-link-regexp
))))
2872 (when (re-search-forward link-regexp limit t
)
2873 (cons 'link
(match-beginning 0))))))
2878 (defun org-element-macro-parser ()
2879 "Parse macro at point.
2881 Return a list whose CAR is `macro' and CDR a plist with `:key',
2882 `:args', `:begin', `:end', `:value' and `:post-blank' as
2885 Assume point is at the macro."
2887 (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
2888 (let ((begin (point))
2889 (key (downcase (org-match-string-no-properties 1)))
2890 (value (org-match-string-no-properties 0))
2891 (post-blank (progn (goto-char (match-end 0))
2892 (skip-chars-forward " \t")))
2894 (args (let ((args (org-match-string-no-properties 3)) args2
)
2896 (setq args
(org-split-string args
","))
2898 (while (string-match "\\\\\\'" (car args
))
2899 ;; Repair bad splits.
2900 (setcar (cdr args
) (concat (substring (car args
) 0 -
1)
2903 (push (pop args
) args2
))
2904 (mapcar 'org-trim
(nreverse args2
))))))
2911 :post-blank post-blank
)))))
2913 (defun org-element-macro-interpreter (macro contents
)
2914 "Interpret MACRO object as Org syntax.
2916 (org-element-property :value macro
))
2918 (defun org-element-macro-successor (limit)
2919 "Search for the next macro object.
2921 LIMIT bounds the search.
2923 Return value is cons cell whose CAR is `macro' and CDR is
2924 beginning position."
2926 (when (re-search-forward
2927 "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
2929 (cons 'macro
(match-beginning 0)))))
2934 (defun org-element-radio-target-parser ()
2935 "Parse radio target at point.
2937 Return a list whose CAR is `radio-target' and CDR a plist with
2938 `:begin', `:end', `:contents-begin', `:contents-end', `:value'
2939 and `:post-blank' as keywords.
2941 Assume point is at the radio target."
2943 (looking-at org-radio-target-regexp
)
2944 (let ((begin (point))
2945 (contents-begin (match-beginning 1))
2946 (contents-end (match-end 1))
2947 (value (org-match-string-no-properties 1))
2948 (post-blank (progn (goto-char (match-end 0))
2949 (skip-chars-forward " \t")))
2954 :contents-begin contents-begin
2955 :contents-end contents-end
2956 :post-blank post-blank
2959 (defun org-element-radio-target-interpreter (target contents
)
2960 "Interpret TARGET object as Org syntax.
2961 CONTENTS is the contents of the object."
2962 (concat "<<<" contents
">>>"))
2964 (defun org-element-radio-target-successor (limit)
2965 "Search for the next radio-target object.
2967 LIMIT bounds the search.
2969 Return value is a cons cell whose CAR is `radio-target' and CDR
2970 is beginning position."
2972 (when (re-search-forward org-radio-target-regexp limit t
)
2973 (cons 'radio-target
(match-beginning 0)))))
2976 ;;;; Statistics Cookie
2978 (defun org-element-statistics-cookie-parser ()
2979 "Parse statistics cookie at point.
2981 Return a list whose CAR is `statistics-cookie', and CDR a plist
2982 with `:begin', `:end', `:value' and `:post-blank' keywords.
2984 Assume point is at the beginning of the statistics-cookie."
2986 (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
2987 (let* ((begin (point))
2988 (value (buffer-substring-no-properties
2989 (match-beginning 0) (match-end 0)))
2990 (post-blank (progn (goto-char (match-end 0))
2991 (skip-chars-forward " \t")))
2993 (list 'statistics-cookie
2997 :post-blank post-blank
)))))
2999 (defun org-element-statistics-cookie-interpreter (statistics-cookie contents
)
3000 "Interpret STATISTICS-COOKIE object as Org syntax.
3002 (org-element-property :value statistics-cookie
))
3004 (defun org-element-statistics-cookie-successor (limit)
3005 "Search for the next statistics cookie object.
3007 LIMIT bounds the search.
3009 Return value is a cons cell whose CAR is `statistics-cookie' and
3010 CDR is beginning position."
3012 (when (re-search-forward "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]" limit t
)
3013 (cons 'statistics-cookie
(match-beginning 0)))))
3018 (defun org-element-strike-through-parser ()
3019 "Parse strike-through object at point.
3021 Return a list whose CAR is `strike-through' and CDR is a plist
3022 with `:begin', `:end', `:contents-begin' and `:contents-end' and
3023 `:post-blank' keywords.
3025 Assume point is at the first plus sign marker."
3027 (unless (bolp) (backward-char 1))
3028 (looking-at org-emph-re
)
3029 (let ((begin (match-beginning 2))
3030 (contents-begin (match-beginning 4))
3031 (contents-end (match-end 4))
3032 (post-blank (progn (goto-char (match-end 2))
3033 (skip-chars-forward " \t")))
3035 (list 'strike-through
3038 :contents-begin contents-begin
3039 :contents-end contents-end
3040 :post-blank post-blank
)))))
3042 (defun org-element-strike-through-interpreter (strike-through contents
)
3043 "Interpret STRIKE-THROUGH object as Org syntax.
3044 CONTENTS is the contents of the object."
3045 (format "+%s+" contents
))
3050 (defun org-element-subscript-parser ()
3051 "Parse subscript at point.
3053 Return a list whose CAR is `subscript' and CDR a plist with
3054 `:begin', `:end', `:contents-begin', `:contents-end',
3055 `:use-brackets-p' and `:post-blank' as keywords.
3057 Assume point is at the underscore."
3059 (unless (bolp) (backward-char))
3060 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp
)
3062 (not (looking-at org-match-substring-regexp
))))
3063 (begin (match-beginning 2))
3064 (contents-begin (or (match-beginning 5)
3065 (match-beginning 3)))
3066 (contents-end (or (match-end 5) (match-end 3)))
3067 (post-blank (progn (goto-char (match-end 0))
3068 (skip-chars-forward " \t")))
3073 :use-brackets-p bracketsp
3074 :contents-begin contents-begin
3075 :contents-end contents-end
3076 :post-blank post-blank
)))))
3078 (defun org-element-subscript-interpreter (subscript contents
)
3079 "Interpret SUBSCRIPT object as Org syntax.
3080 CONTENTS is the contents of the object."
3082 (if (org-element-property :use-brackets-p subscript
) "_{%s}" "_%s")
3085 (defun org-element-sub/superscript-successor
(limit)
3086 "Search for the next sub/superscript object.
3088 LIMIT bounds the search.
3090 Return value is a cons cell whose CAR is either `subscript' or
3091 `superscript' and CDR is beginning position."
3093 (unless (bolp) (backward-char))
3094 (when (re-search-forward org-match-substring-regexp limit t
)
3095 (cons (if (string= (match-string 2) "_") 'subscript
'superscript
)
3096 (match-beginning 2)))))
3101 (defun org-element-superscript-parser ()
3102 "Parse superscript at point.
3104 Return a list whose CAR is `superscript' and CDR a plist with
3105 `:begin', `:end', `:contents-begin', `:contents-end',
3106 `:use-brackets-p' and `:post-blank' as keywords.
3108 Assume point is at the caret."
3110 (unless (bolp) (backward-char))
3111 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp
) t
3112 (not (looking-at org-match-substring-regexp
))))
3113 (begin (match-beginning 2))
3114 (contents-begin (or (match-beginning 5)
3115 (match-beginning 3)))
3116 (contents-end (or (match-end 5) (match-end 3)))
3117 (post-blank (progn (goto-char (match-end 0))
3118 (skip-chars-forward " \t")))
3123 :use-brackets-p bracketsp
3124 :contents-begin contents-begin
3125 :contents-end contents-end
3126 :post-blank post-blank
)))))
3128 (defun org-element-superscript-interpreter (superscript contents
)
3129 "Interpret SUPERSCRIPT object as Org syntax.
3130 CONTENTS is the contents of the object."
3132 (if (org-element-property :use-brackets-p superscript
) "^{%s}" "^%s")
3138 (defun org-element-table-cell-parser ()
3139 "Parse table cell at point.
3141 Return a list whose CAR is `table-cell' and CDR is a plist
3142 containing `:begin', `:end', `:contents-begin', `:contents-end'
3143 and `:post-blank' keywords."
3144 (looking-at "[ \t]*\\(.*?\\)[ \t]*|")
3145 (let* ((begin (match-beginning 0))
3147 (contents-begin (match-beginning 1))
3148 (contents-end (match-end 1)))
3152 :contents-begin contents-begin
3153 :contents-end contents-end
3156 (defun org-element-table-cell-interpreter (table-cell contents
)
3157 "Interpret TABLE-CELL element as Org syntax.
3158 CONTENTS is the contents of the cell, or nil."
3159 (concat " " contents
" |"))
3161 (defun org-element-table-cell-successor (limit)
3162 "Search for the next table-cell object.
3164 LIMIT bounds the search.
3166 Return value is a cons cell whose CAR is `table-cell' and CDR is
3167 beginning position."
3168 (when (looking-at "[ \t]*.*?[ \t]+|") (cons 'table-cell
(point))))
3173 (defun org-element-target-parser ()
3174 "Parse target at point.
3176 Return a list whose CAR is `target' and CDR a plist with
3177 `:begin', `:end', `:value' and `:post-blank' as keywords.
3179 Assume point is at the target."
3181 (looking-at org-target-regexp
)
3182 (let ((begin (point))
3183 (value (org-match-string-no-properties 1))
3184 (post-blank (progn (goto-char (match-end 0))
3185 (skip-chars-forward " \t")))
3191 :post-blank post-blank
)))))
3193 (defun org-element-target-interpreter (target contents
)
3194 "Interpret TARGET object as Org syntax.
3196 (format "<<%s>>" (org-element-property :value target
)))
3198 (defun org-element-target-successor (limit)
3199 "Search for the next target object.
3201 LIMIT bounds the search.
3203 Return value is a cons cell whose CAR is `target' and CDR is
3204 beginning position."
3206 (when (re-search-forward org-target-regexp limit t
)
3207 (cons 'target
(match-beginning 0)))))
3212 (defun org-element-timestamp-parser ()
3213 "Parse time stamp at point.
3215 Return a list whose CAR is `timestamp', and CDR a plist with
3216 `:type', `:begin', `:end', `:value' and `:post-blank' keywords.
3218 Assume point is at the beginning of the timestamp."
3220 (let* ((begin (point))
3221 (activep (eq (char-after) ?
<))
3224 (looking-at "[<[]\\(\\(%%\\)?.*?\\)[]>]\\(?:--[<[]\\(.*?\\)[]>]\\)?")
3225 (match-string-no-properties 1)))
3226 (range-end (match-string-no-properties 3))
3227 (type (cond ((match-string 2) 'diary
)
3228 ((and activep range-end
) 'active-range
)
3230 (range-end 'inactive-range
)
3232 (post-blank (progn (goto-char (match-end 0))
3233 (skip-chars-forward " \t")))
3238 :range-end range-end
3241 :post-blank post-blank
)))))
3243 (defun org-element-timestamp-interpreter (timestamp contents
)
3244 "Interpret TIMESTAMP object as Org syntax.
3246 (let ((type (org-element-property :type timestamp
) ))
3248 (format (if (memq type
'(inactive inactive-range
)) "[%s]" "<%s>")
3249 (org-element-property :value timestamp
))
3250 (let ((range-end (org-element-property :range-end timestamp
)))
3253 (format (if (eq type
'inactive-range
) "[%s]" "<%s>")
3256 (defun org-element-timestamp-successor (limit)
3257 "Search for the next timestamp object.
3259 LIMIT bounds the search.
3261 Return value is a cons cell whose CAR is `timestamp' and CDR is
3262 beginning position."
3264 (when (re-search-forward
3265 (concat org-ts-regexp-both
3267 "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
3269 "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
3271 (cons 'timestamp
(match-beginning 0)))))
3276 (defun org-element-underline-parser ()
3277 "Parse underline object at point.
3279 Return a list whose CAR is `underline' and CDR is a plist with
3280 `:begin', `:end', `:contents-begin' and `:contents-end' and
3281 `:post-blank' keywords.
3283 Assume point is at the first underscore marker."
3285 (unless (bolp) (backward-char 1))
3286 (looking-at org-emph-re
)
3287 (let ((begin (match-beginning 2))
3288 (contents-begin (match-beginning 4))
3289 (contents-end (match-end 4))
3290 (post-blank (progn (goto-char (match-end 2))
3291 (skip-chars-forward " \t")))
3296 :contents-begin contents-begin
3297 :contents-end contents-end
3298 :post-blank post-blank
)))))
3300 (defun org-element-underline-interpreter (underline contents
)
3301 "Interpret UNDERLINE object as Org syntax.
3302 CONTENTS is the contents of the object."
3303 (format "_%s_" contents
))
3308 (defun org-element-verbatim-parser ()
3309 "Parse verbatim object at point.
3311 Return a list whose CAR is `verbatim' and CDR is a plist with
3312 `:value', `:begin', `:end' and `:post-blank' keywords.
3314 Assume point is at the first equal sign marker."
3316 (unless (bolp) (backward-char 1))
3317 (looking-at org-emph-re
)
3318 (let ((begin (match-beginning 2))
3319 (value (org-match-string-no-properties 4))
3320 (post-blank (progn (goto-char (match-end 2))
3321 (skip-chars-forward " \t")))
3327 :post-blank post-blank
)))))
3329 (defun org-element-verbatim-interpreter (verbatim contents
)
3330 "Interpret VERBATIM object as Org syntax.
3332 (format "=%s=" (org-element-property :value verbatim
)))
3336 ;;; Parsing Element Starting At Point
3338 ;; `org-element--current-element' is the core function of this section.
3339 ;; It returns the Lisp representation of the element starting at
3342 ;; `org-element--current-element' makes use of special modes. They
3343 ;; are activated for fixed element chaining (i.e. `plain-list' >
3344 ;; `item') or fixed conditional element chaining (i.e. `headline' >
3345 ;; `section'). Special modes are: `first-section', `section',
3346 ;; `quote-section', `item' and `table-row'.
3348 (defun org-element--current-element
3349 (limit &optional granularity special structure
)
3350 "Parse the element starting at point.
3352 LIMIT bounds the search.
3354 Return value is a list like (TYPE PROPS) where TYPE is the type
3355 of the element and PROPS a plist of properties associated to the
3358 Possible types are defined in `org-element-all-elements'.
3360 Optional argument GRANULARITY determines the depth of the
3361 recursion. Allowed values are `headline', `greater-element',
3362 `element', `object' or nil. When it is broader than `object' (or
3363 nil), secondary values will not be parsed, since they only
3366 Optional argument SPECIAL, when non-nil, can be either
3367 `first-section', `section', `quote-section', `table-row' and
3370 If STRUCTURE isn't provided but SPECIAL is set to `item', it will
3373 This function assumes point is always at the beginning of the
3374 element it has to parse."
3376 ;; If point is at an affiliated keyword, try moving to the
3377 ;; beginning of the associated element. If none is found, the
3378 ;; keyword is orphaned and will be treated as plain text.
3379 (when (looking-at org-element--affiliated-re
)
3380 (let ((opoint (point)))
3381 (while (looking-at org-element--affiliated-re
) (forward-line))
3382 (when (looking-at "[ \t]*$") (goto-char opoint
))))
3383 (let ((case-fold-search t
)
3384 ;; Determine if parsing depth allows for secondary strings
3385 ;; parsing. It only applies to elements referenced in
3386 ;; `org-element-secondary-value-alist'.
3387 (raw-secondary-p (and granularity
(not (eq granularity
'object
)))))
3391 (org-element-item-parser limit structure raw-secondary-p
))
3393 ((eq special
'table-row
) (org-element-table-row-parser limit
))
3395 ((org-with-limited-levels (org-at-heading-p))
3396 (org-element-headline-parser limit raw-secondary-p
))
3397 ;; Sections (must be checked after headline).
3398 ((eq special
'section
) (org-element-section-parser limit
))
3399 ((eq special
'quote-section
) (org-element-quote-section-parser limit
))
3400 ((eq special
'first-section
)
3401 (org-element-section-parser
3402 (or (save-excursion (org-with-limited-levels (outline-next-heading)))
3404 ;; When not at bol, point is at the beginning of an item or
3405 ;; a footnote definition: next item is always a paragraph.
3406 ((not (bolp)) (org-element-paragraph-parser limit
))
3407 ;; Planning and Clock.
3408 ((and (looking-at org-planning-or-clock-line-re
))
3409 (if (equal (match-string 1) org-clock-string
)
3410 (org-element-clock-parser limit
)
3411 (org-element-planning-parser limit
)))
3414 (org-element-inlinetask-parser limit raw-secondary-p
))
3415 ;; LaTeX Environment.
3416 ((looking-at "[ \t]*\\\\begin{\\([A-Za-z0-9*]+\\)}")
3419 (format "[ \t]*\\\\end{%s}[ \t]*"
3420 (regexp-quote (match-string 1)))
3422 (org-element-latex-environment-parser limit
)
3423 (org-element-paragraph-parser limit
)))
3424 ;; Drawer and Property Drawer.
3425 ((looking-at org-drawer-regexp
)
3426 (let ((name (match-string 1)))
3428 ((not (save-excursion
3429 (re-search-forward "^[ \t]*:END:[ \t]*$" nil t
)))
3430 (org-element-paragraph-parser limit
))
3431 ((equal "PROPERTIES" name
)
3432 (org-element-property-drawer-parser limit
))
3433 (t (org-element-drawer-parser limit
)))))
3435 ((looking-at "[ \t]*:\\( \\|$\\)")
3436 (org-element-fixed-width-parser limit
))
3437 ;; Inline Comments, Blocks, Babel Calls, Dynamic Blocks and
3439 ((looking-at "[ \t]*#")
3440 (goto-char (match-end 0))
3441 (cond ((looking-at "\\(?: \\|$\\)")
3443 (org-element-comment-parser limit
))
3444 ((looking-at "\\+BEGIN_\\(\\S-+\\)")
3446 (let ((parser (assoc (upcase (match-string 1))
3447 org-element-block-name-alist
)))
3448 (if parser
(funcall (cdr parser
) limit
)
3449 (org-element-special-block-parser limit
))))
3450 ((looking-at "\\+CALL:")
3452 (org-element-babel-call-parser limit
))
3453 ((looking-at "\\+BEGIN:? ")
3455 (org-element-dynamic-block-parser limit
))
3456 ((looking-at "\\+\\S-+:")
3458 (org-element-keyword-parser limit
))
3461 (org-element-paragraph-parser limit
))))
3462 ;; Footnote Definition.
3463 ((looking-at org-footnote-definition-re
)
3464 (org-element-footnote-definition-parser limit
))
3466 ((looking-at "[ \t]*-\\{5,\\}[ \t]*$")
3467 (org-element-horizontal-rule-parser limit
))
3469 ((org-at-table-p t
) (org-element-table-parser limit
))
3471 ((looking-at (org-item-re))
3472 (org-element-plain-list-parser limit
(or structure
(org-list-struct))))
3473 ;; Default element: Paragraph.
3474 (t (org-element-paragraph-parser limit
))))))
3477 ;; Most elements can have affiliated keywords. When looking for an
3478 ;; element beginning, we want to move before them, as they belong to
3479 ;; that element, and, in the meantime, collect information they give
3480 ;; into appropriate properties. Hence the following function.
3482 ;; Usage of optional arguments may not be obvious at first glance:
3484 ;; - TRANS-LIST is used to polish keywords names that have evolved
3485 ;; during Org history. In example, even though =result= and
3486 ;; =results= coexist, we want to have them under the same =result=
3487 ;; property. It's also true for "srcname" and "name", where the
3488 ;; latter seems to be preferred nowadays (thus the "name" property).
3490 ;; - CONSED allows to regroup multi-lines keywords under the same
3491 ;; property, while preserving their own identity. This is mostly
3492 ;; used for "attr_latex" and al.
3494 ;; - PARSED prepares a keyword value for export. This is useful for
3495 ;; "caption". Objects restrictions for such keywords are defined in
3496 ;; `org-element-object-restrictions'.
3498 ;; - DUALS is used to take care of keywords accepting a main and an
3499 ;; optional secondary values. For example "results" has its
3500 ;; source's name as the main value, and may have an hash string in
3501 ;; optional square brackets as the secondary one.
3503 ;; A keyword may belong to more than one category.
3505 (defun org-element--collect-affiliated-keywords
3506 (&optional key-re trans-list consed parsed duals
)
3507 "Collect affiliated keywords before point.
3509 Optional argument KEY-RE is a regexp matching keywords, which
3510 puts matched keyword in group 1. It defaults to
3511 `org-element--affiliated-re'.
3513 TRANS-LIST is an alist where key is the keyword and value the
3514 property name it should be translated to, without the colons. It
3515 defaults to `org-element-keyword-translation-alist'.
3517 CONSED is a list of strings. Any keyword belonging to that list
3518 will have its value consed. The check is done after keyword
3519 translation. It defaults to `org-element-multiple-keywords'.
3521 PARSED is a list of strings. Any keyword member of this list
3522 will have its value parsed. The check is done after keyword
3523 translation. If a keyword is a member of both CONSED and PARSED,
3524 it's value will be a list of parsed strings. It defaults to
3525 `org-element-parsed-keywords'.
3527 DUALS is a list of strings. Any keyword member of this list can
3528 have two parts: one mandatory and one optional. Its value is
3529 a cons cell whose CAR is the former, and the CDR the latter. If
3530 a keyword is a member of both PARSED and DUALS, both values will
3531 be parsed. It defaults to `org-element-dual-keywords'.
3533 Return a list whose CAR is the position at the first of them and
3534 CDR a plist of keywords and values."
3536 (let ((case-fold-search t
)
3537 (key-re (or key-re org-element--affiliated-re
))
3538 (trans-list (or trans-list org-element-keyword-translation-alist
))
3539 (consed (or consed org-element-multiple-keywords
))
3540 (parsed (or parsed org-element-parsed-keywords
))
3541 (duals (or duals org-element-dual-keywords
))
3542 ;; RESTRICT is the list of objects allowed in parsed
3544 (restrict (org-element-restriction 'keyword
))
3547 (while (and (not (bobp)) (progn (forward-line -
1) (looking-at key-re
)))
3548 (let* ((raw-kwd (upcase (match-string 1)))
3549 ;; Apply translation to RAW-KWD. From there, KWD is
3550 ;; the official keyword.
3551 (kwd (or (cdr (assoc raw-kwd trans-list
)) raw-kwd
))
3552 ;; Find main value for any keyword.
3556 (buffer-substring-no-properties
3557 (match-end 0) (point-at-eol)))))
3558 ;; If KWD is a dual keyword, find its secondary
3559 ;; value. Maybe parse it.
3561 (and (member kwd duals
)
3562 (let ((sec (org-match-string-no-properties 2)))
3563 (if (or (not sec
) (not (member kwd parsed
))) sec
3564 (org-element-parse-secondary-string sec restrict
)))))
3565 ;; Attribute a property name to KWD.
3566 (kwd-sym (and kwd
(intern (concat ":" (downcase kwd
))))))
3567 ;; Now set final shape for VALUE.
3568 (when (member kwd parsed
)
3569 (setq value
(org-element-parse-secondary-string value restrict
)))
3570 (when (member kwd duals
)
3571 ;; VALUE is mandatory. Set it to nil if there is none.
3572 (setq value
(and value
(cons value dual-value
))))
3573 ;; Attributes are always consed.
3574 (when (or (member kwd consed
) (string-match "^ATTR_" kwd
))
3575 (setq value
(cons value
(plist-get output kwd-sym
))))
3576 ;; Eventually store the new value in OUTPUT.
3577 (setq output
(plist-put output kwd-sym value
))))
3578 (unless (looking-at key-re
) (forward-line 1)))
3579 (list (point) output
))))
3585 ;; The two major functions here are `org-element-parse-buffer', which
3586 ;; parses Org syntax inside the current buffer, taking into account
3587 ;; region, narrowing, or even visibility if specified, and
3588 ;; `org-element-parse-secondary-string', which parses objects within
3591 ;; The (almost) almighty `org-element-map' allows to apply a function
3592 ;; on elements or objects matching some type, and accumulate the
3593 ;; resulting values. In an export situation, it also skips unneeded
3594 ;; parts of the parse tree.
3596 (defun org-element-parse-buffer (&optional granularity visible-only
)
3597 "Recursively parse the buffer and return structure.
3598 If narrowing is in effect, only parse the visible part of the
3601 Optional argument GRANULARITY determines the depth of the
3602 recursion. It can be set to the following symbols:
3604 `headline' Only parse headlines.
3605 `greater-element' Don't recurse into greater elements excepted
3606 headlines and sections. Thus, elements
3607 parsed are the top-level ones.
3608 `element' Parse everything but objects and plain text.
3609 `object' Parse the complete buffer (default).
3611 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3614 Assume buffer is in Org mode."
3616 (goto-char (point-min))
3617 (org-skip-whitespace)
3618 (org-element--parse-elements
3619 (point-at-bol) (point-max)
3620 ;; Start in `first-section' mode so text before the first
3621 ;; headline belongs to a section.
3622 'first-section nil granularity visible-only
(list 'org-data nil
))))
3624 (defun org-element-parse-secondary-string (string restriction
&optional parent
)
3625 "Recursively parse objects in STRING and return structure.
3627 RESTRICTION is a symbol limiting the object types that will be
3630 Optional argument PARENT, when non-nil, is the element or object
3631 containing the secondary string. It is used to set correctly
3632 `:parent' property within the string."
3635 (let ((secondary (org-element--parse-objects
3636 (point-min) (point-max) nil restriction
)))
3637 (mapc (lambda (obj) (org-element-put-property obj
:parent parent
))
3640 (defun org-element-map (data types fun
&optional info first-match no-recursion
)
3641 "Map a function on selected elements or objects.
3643 DATA is the parsed tree, as returned by, i.e,
3644 `org-element-parse-buffer'. TYPES is a symbol or list of symbols
3645 of elements or objects types. FUN is the function called on the
3646 matching element or object. It must accept one arguments: the
3647 element or object itself.
3649 When optional argument INFO is non-nil, it should be a plist
3650 holding export options. In that case, parts of the parse tree
3651 not exportable according to that property list will be skipped.
3653 When optional argument FIRST-MATCH is non-nil, stop at the first
3654 match for which FUN doesn't return nil, and return that value.
3656 Optional argument NO-RECURSION is a symbol or a list of symbols
3657 representing elements or objects types. `org-element-map' won't
3658 enter any recursive element or object whose type belongs to that
3659 list. Though, FUN can still be applied on them.
3661 Nil values returned from FUN do not appear in the results."
3662 ;; Ensure TYPES and NO-RECURSION are a list, even of one element.
3663 (unless (listp types
) (setq types
(list types
)))
3664 (unless (listp no-recursion
) (setq no-recursion
(list no-recursion
)))
3665 ;; Recursion depth is determined by --CATEGORY.
3668 (let ((category 'greater-elements
))
3669 (mapc (lambda (type)
3670 (cond ((or (memq type org-element-all-objects
)
3671 (eq type
'plain-text
))
3672 ;; If one object is found, the function
3673 ;; has to recurse into every object.
3674 (throw 'found
'objects
))
3675 ((not (memq type org-element-greater-elements
))
3676 ;; If one regular element is found, the
3677 ;; function has to recurse, at least,
3678 ;; into every element it encounters.
3679 (and (not (eq category
'elements
))
3680 (setq category
'elements
)))))
3688 ;; Recursively walk DATA. INFO, if non-nil, is a plist
3689 ;; holding contextual information.
3690 (let ((--type (org-element-type --data
)))
3693 ;; Ignored element in an export context.
3694 ((and info
(memq --data
(plist-get info
:ignore-list
))))
3695 ;; Secondary string: only objects can be found there.
3697 (when (eq --category
'objects
) (mapc --walk-tree --data
)))
3698 ;; Unconditionally enter parse trees.
3699 ((eq --type
'org-data
)
3700 (mapc --walk-tree
(org-element-contents --data
)))
3702 ;; Check if TYPE is matching among TYPES. If so,
3703 ;; apply FUN to --DATA and accumulate return value
3704 ;; into --ACC (or exit if FIRST-MATCH is non-nil).
3705 (when (memq --type types
)
3706 (let ((result (funcall fun --data
)))
3707 (cond ((not result
))
3708 (first-match (throw '--map-first-match result
))
3709 (t (push result --acc
)))))
3710 ;; If --DATA has a secondary string that can contain
3711 ;; objects with their type among TYPES, look into it.
3712 (when (eq --category
'objects
)
3714 (assq --type org-element-secondary-value-alist
)))
3716 (funcall --walk-tree
3717 (org-element-property (cdr sec-prop
) --data
)))))
3718 ;; Determine if a recursion into --DATA is possible.
3720 ;; --TYPE is explicitly removed from recursion.
3721 ((memq --type no-recursion
))
3722 ;; --DATA has no contents.
3723 ((not (org-element-contents --data
)))
3724 ;; Looking for greater elements but --DATA is simply
3725 ;; an element or an object.
3726 ((and (eq --category
'greater-elements
)
3727 (not (memq --type org-element-greater-elements
))))
3728 ;; Looking for elements but --DATA is an object.
3729 ((and (eq --category
'elements
)
3730 (memq --type org-element-all-objects
)))
3731 ;; In any other case, map contents.
3732 (t (mapc --walk-tree
(org-element-contents --data
)))))))))))
3733 (catch '--map-first-match
3734 (funcall --walk-tree data
)
3735 ;; Return value in a proper order.
3738 ;; The following functions are internal parts of the parser.
3740 ;; The first one, `org-element--parse-elements' acts at the element's
3743 ;; The second one, `org-element--parse-objects' applies on all objects
3744 ;; of a paragraph or a secondary string. It uses
3745 ;; `org-element--get-next-object-candidates' to optimize the search of
3746 ;; the next object in the buffer.
3748 ;; More precisely, that function looks for every allowed object type
3749 ;; first. Then, it discards failed searches, keeps further matches,
3750 ;; and searches again types matched behind point, for subsequent
3751 ;; calls. Thus, searching for a given type fails only once, and every
3752 ;; object is searched only once at top level (but sometimes more for
3755 (defun org-element--parse-elements
3756 (beg end special structure granularity visible-only acc
)
3757 "Parse elements between BEG and END positions.
3759 SPECIAL prioritize some elements over the others. It can be set
3760 to `first-section', `quote-section', `section' `item' or
3763 When value is `item', STRUCTURE will be used as the current list
3766 GRANULARITY determines the depth of the recursion. See
3767 `org-element-parse-buffer' for more information.
3769 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3772 Elements are accumulated into ACC."
3775 ;; When parsing only headlines, skip any text before first one.
3776 (when (and (eq granularity
'headline
) (not (org-at-heading-p)))
3777 (org-with-limited-levels (outline-next-heading)))
3779 (while (< (point) end
)
3780 ;; Find current element's type and parse it accordingly to
3782 (let* ((element (org-element--current-element
3783 end granularity special structure
))
3784 (type (org-element-type element
))
3785 (cbeg (org-element-property :contents-begin element
)))
3786 (goto-char (org-element-property :end element
))
3787 ;; Fill ELEMENT contents by side-effect.
3789 ;; If VISIBLE-ONLY is true and element is hidden or if it has
3790 ;; no contents, don't modify it.
3791 ((or (and visible-only
(org-element-property :hiddenp element
))
3793 ;; Greater element: parse it between `contents-begin' and
3794 ;; `contents-end'. Make sure GRANULARITY allows the
3795 ;; recursion, or ELEMENT is an headline, in which case going
3796 ;; inside is mandatory, in order to get sub-level headings.
3797 ((and (memq type org-element-greater-elements
)
3798 (or (memq granularity
'(element object nil
))
3799 (and (eq granularity
'greater-element
)
3801 (eq type
'headline
)))
3802 (org-element--parse-elements
3803 cbeg
(org-element-property :contents-end element
)
3804 ;; Possibly switch to a special mode.
3807 (if (org-element-property :quotedp element
) 'quote-section
3811 (org-element-property :structure element
)
3812 granularity visible-only element
))
3813 ;; ELEMENT has contents. Parse objects inside, if
3814 ;; GRANULARITY allows it.
3815 ((memq granularity
'(object nil
))
3816 (org-element--parse-objects
3817 cbeg
(org-element-property :contents-end element
) element
3818 (org-element-restriction type
))))
3819 (org-element-adopt-elements acc element
)))
3823 (defun org-element--parse-objects (beg end acc restriction
)
3824 "Parse objects between BEG and END and return recursive structure.
3826 Objects are accumulated in ACC.
3828 RESTRICTION is a list of object types which are allowed in the
3833 (while (and (< (point) end
)
3834 (setq candidates
(org-element--get-next-object-candidates
3835 end restriction candidates
)))
3837 (let ((pos (apply 'min
(mapcar 'cdr candidates
))))
3840 (funcall (intern (format "org-element-%s-parser"
3841 (car (rassq pos candidates
)))))))))
3842 ;; 1. Text before any object. Untabify it.
3843 (let ((obj-beg (org-element-property :begin next-object
)))
3844 (unless (= (point) obj-beg
)
3846 (org-element-adopt-elements
3848 (replace-regexp-in-string
3849 "\t" (make-string tab-width ?
)
3850 (buffer-substring-no-properties (point) obj-beg
))))))
3852 (let ((obj-end (org-element-property :end next-object
))
3853 (cont-beg (org-element-property :contents-begin next-object
)))
3854 ;; Fill contents of NEXT-OBJECT by side-effect, if it has
3855 ;; a recursive type.
3857 (memq (car next-object
) org-element-recursive-objects
))
3861 (org-element-property :contents-end next-object
))
3862 (org-element--parse-objects
3863 (point-min) (point-max) next-object
3864 (org-element-restriction next-object
))))
3865 (setq acc
(org-element-adopt-elements acc next-object
))
3866 (goto-char obj-end
))))
3867 ;; 3. Text after last object. Untabify it.
3868 (unless (= (point) end
)
3870 (org-element-adopt-elements
3872 (replace-regexp-in-string
3873 "\t" (make-string tab-width ?
)
3874 (buffer-substring-no-properties (point) end
)))))
3878 (defun org-element--get-next-object-candidates (limit restriction objects
)
3879 "Return an alist of candidates for the next object.
3881 LIMIT bounds the search, and RESTRICTION narrows candidates to
3884 Return value is an alist whose CAR is position and CDR the object
3887 OBJECTS is the previous candidates alist."
3888 ;; Filter out any object found but not belonging to RESTRICTION.
3892 (let ((type (car obj
)))
3893 (memq (or (cdr (assq type org-element-object-successor-alist
))
3897 (let (next-candidates types-to-search
)
3898 ;; If no previous result, search every object type in RESTRICTION.
3899 ;; Otherwise, keep potential candidates (old objects located after
3900 ;; point) and ask to search again those which had matched before.
3901 (if (not objects
) (setq types-to-search restriction
)
3903 (if (< (cdr obj
) (point)) (push (car obj
) types-to-search
)
3904 (push obj next-candidates
)))
3906 ;; Call the appropriate successor function for each type to search
3907 ;; and accumulate matches.
3910 (let* ((successor-fun
3912 (format "org-element-%s-successor"
3913 (or (cdr (assq type org-element-object-successor-alist
))
3915 (obj (funcall successor-fun limit
)))
3916 (and obj
(push obj next-candidates
))))
3923 ;;; Towards A Bijective Process
3925 ;; The parse tree obtained with `org-element-parse-buffer' is really
3926 ;; a snapshot of the corresponding Org buffer. Therefore, it can be
3927 ;; interpreted and expanded into a string with canonical Org syntax.
3928 ;; Hence `org-element-interpret-data'.
3930 ;; The function relies internally on
3931 ;; `org-element--interpret-affiliated-keywords'.
3934 (defun org-element-interpret-data (data &optional parent
)
3935 "Interpret DATA as Org syntax.
3937 DATA is a parse tree, an element, an object or a secondary string
3940 Optional argument PARENT is used for recursive calls. It contains
3941 the element or object containing data, or nil.
3943 Return Org syntax as a string."
3944 (let* ((type (org-element-type data
))
3947 ;; Secondary string.
3950 (lambda (obj) (org-element-interpret-data obj parent
))
3952 ;; Full Org document.
3953 ((eq type
'org-data
)
3955 (lambda (obj) (org-element-interpret-data obj parent
))
3956 (org-element-contents data
) ""))
3958 ((stringp data
) data
)
3959 ;; Element/Object without contents.
3960 ((not (org-element-contents data
))
3961 (funcall (intern (format "org-element-%s-interpreter" type
))
3963 ;; Element/Object with contents.
3965 (let* ((greaterp (memq type org-element-greater-elements
))
3966 (objectp (and (not greaterp
)
3967 (memq type org-element-recursive-objects
)))
3970 (lambda (obj) (org-element-interpret-data obj data
))
3971 (org-element-contents
3972 (if (or greaterp objectp
) data
3973 ;; Elements directly containing objects must
3974 ;; have their indentation normalized first.
3975 (org-element-normalize-contents
3977 ;; When normalizing first paragraph of an
3978 ;; item or a footnote-definition, ignore
3979 ;; first line's indentation.
3980 (and (eq type
'paragraph
)
3981 (equal data
(car (org-element-contents parent
)))
3982 (memq (org-element-type parent
)
3983 '(footnote-definition item
))))))
3985 (funcall (intern (format "org-element-%s-interpreter" type
))
3987 (if greaterp
(org-element-normalize-contents contents
)
3989 (if (memq type
'(org-data plain-text nil
)) results
3990 ;; Build white spaces. If no `:post-blank' property is
3991 ;; specified, assume its value is 0.
3992 (let ((post-blank (or (org-element-property :post-blank data
) 0)))
3993 (if (memq type org-element-all-objects
)
3994 (concat results
(make-string post-blank
32))
3996 (org-element--interpret-affiliated-keywords data
)
3997 (org-element-normalize-string results
)
3998 (make-string post-blank
10)))))))
4000 (defun org-element--interpret-affiliated-keywords (element)
4001 "Return ELEMENT's affiliated keywords as Org syntax.
4002 If there is no affiliated keyword, return the empty string."
4003 (let ((keyword-to-org
4007 (when (member key org-element-dual-keywords
)
4008 (setq dual
(cdr value
) value
(car value
)))
4011 (format "[%s]" (org-element-interpret-data dual
)))
4013 (if (member key org-element-parsed-keywords
)
4014 (org-element-interpret-data value
)
4019 (let ((value (org-element-property prop element
))
4020 (keyword (upcase (substring (symbol-name prop
) 1))))
4022 (if (or (member keyword org-element-multiple-keywords
)
4023 ;; All attribute keywords can have multiple lines.
4024 (string-match "^ATTR_" keyword
))
4025 (mapconcat (lambda (line) (funcall keyword-to-org keyword line
))
4028 (funcall keyword-to-org keyword value
)))))
4029 ;; List all ELEMENT's properties matching an attribute line or an
4030 ;; affiliated keyword, but ignore translated keywords since they
4031 ;; cannot belong to the property list.
4032 (loop for prop in
(nth 1 element
) by
'cddr
4033 when
(let ((keyword (upcase (substring (symbol-name prop
) 1))))
4034 (or (string-match "^ATTR_" keyword
)
4036 (member keyword org-element-affiliated-keywords
)
4038 org-element-keyword-translation-alist
)))))
4042 ;; Because interpretation of the parse tree must return the same
4043 ;; number of blank lines between elements and the same number of white
4044 ;; space after objects, some special care must be given to white
4047 ;; The first function, `org-element-normalize-string', ensures any
4048 ;; string different from the empty string will end with a single
4049 ;; newline character.
4051 ;; The second function, `org-element-normalize-contents', removes
4052 ;; global indentation from the contents of the current element.
4054 (defun org-element-normalize-string (s)
4055 "Ensure string S ends with a single newline character.
4057 If S isn't a string return it unchanged. If S is the empty
4058 string, return it. Otherwise, return a new string with a single
4059 newline character at its end."
4061 ((not (stringp s
)) s
)
4063 (t (and (string-match "\\(\n[ \t]*\\)*\\'" s
)
4064 (replace-match "\n" nil nil s
)))))
4066 (defun org-element-normalize-contents (element &optional ignore-first
)
4067 "Normalize plain text in ELEMENT's contents.
4069 ELEMENT must only contain plain text and objects.
4071 If optional argument IGNORE-FIRST is non-nil, ignore first line's
4072 indentation to compute maximal common indentation.
4074 Return the normalized element that is element with global
4075 indentation removed from its contents. The function assumes that
4076 indentation is not done with TAB characters."
4077 (let* (ind-list ; for byte-compiler
4078 collect-inds
; for byte-compiler
4081 ;; Return list of indentations within BLOB. This is done by
4082 ;; walking recursively BLOB and updating IND-LIST along the
4083 ;; way. FIRST-FLAG is non-nil when the first string hasn't
4084 ;; been seen yet. It is required as this string is the only
4085 ;; one whose indentation doesn't happen after a newline
4087 (lambda (blob first-flag
)
4090 (when (and first-flag
(stringp object
))
4091 (setq first-flag nil
)
4092 (string-match "\\`\\( *\\)" object
)
4093 (let ((len (length (match-string 1 object
))))
4094 ;; An indentation of zero means no string will be
4095 ;; modified. Quit the process.
4096 (if (zerop len
) (throw 'zero
(setq ind-list nil
))
4097 (push len ind-list
))))
4101 ;; Avoid matching blank or empty lines.
4102 (while (and (string-match "\n\\( *\\)\\(.\\)" object start
)
4103 (not (equal (match-string 2 object
) " ")))
4104 (setq start
(match-end 0))
4105 (push (length (match-string 1 object
)) ind-list
))))
4106 ((memq (org-element-type object
) org-element-recursive-objects
)
4107 (funcall collect-inds object first-flag
))))
4108 (org-element-contents blob
))))))
4109 ;; Collect indentation list in ELEMENT. Possibly remove first
4110 ;; value if IGNORE-FIRST is non-nil.
4111 (catch 'zero
(funcall collect-inds element
(not ignore-first
)))
4112 (if (not ind-list
) element
4113 ;; Build ELEMENT back, replacing each string with the same
4114 ;; string minus common indentation.
4115 (let* (build ; For byte compiler.
4118 (lambda (blob mci first-flag
)
4119 ;; Return BLOB with all its strings indentation
4120 ;; shortened from MCI white spaces. FIRST-FLAG is
4121 ;; non-nil when the first string hasn't been seen
4126 (when (and first-flag
(stringp object
))
4127 (setq first-flag nil
)
4129 (replace-regexp-in-string
4130 (format "\\` \\{%d\\}" mci
) "" object
)))
4133 (replace-regexp-in-string
4134 (format "\n \\{%d\\}" mci
) "\n" object
))
4135 ((memq (org-element-type object
)
4136 org-element-recursive-objects
)
4137 (funcall build object mci first-flag
))
4139 (org-element-contents blob
)))
4141 (funcall build element
(apply 'min ind-list
) (not ignore-first
))))))
4147 ;; The first move is to implement a way to obtain the smallest element
4148 ;; containing point. This is the job of `org-element-at-point'. It
4149 ;; basically jumps back to the beginning of section containing point
4150 ;; and moves, element after element, with
4151 ;; `org-element--current-element' until the container is found. Note:
4152 ;; When using `org-element-at-point', secondary values are never
4153 ;; parsed since the function focuses on elements, not on objects.
4155 ;; At a deeper level, `org-element-context' lists all elements and
4156 ;; objects containing point.
4158 ;; `org-element-nested-p' and `org-element-swap-A-B' may be used
4159 ;; internally by navigation and manipulation tools.
4162 (defun org-element-at-point (&optional keep-trail
)
4163 "Determine closest element around point.
4165 Return value is a list like (TYPE PROPS) where TYPE is the type
4166 of the element and PROPS a plist of properties associated to the
4169 Possible types are defined in `org-element-all-elements'.
4170 Properties depend on element or object type, but always
4171 include :begin, :end, :parent and :post-blank properties.
4173 As a special case, if point is at the very beginning of a list or
4174 sub-list, returned element will be that list instead of the first
4175 item. In the same way, if point is at the beginning of the first
4176 row of a table, returned element will be the table instead of the
4179 If optional argument KEEP-TRAIL is non-nil, the function returns
4180 a list of of elements leading to element at point. The list's
4181 CAR is always the element at point. Following positions contain
4182 element's siblings, then parents, siblings of parents, until the
4183 first element of current section."
4184 (org-with-wide-buffer
4185 ;; If at an headline, parse it. It is the sole element that
4186 ;; doesn't require to know about context. Be sure to disallow
4187 ;; secondary string parsing, though.
4188 (if (org-with-limited-levels (org-at-heading-p))
4191 (if (not keep-trail
) (org-element-headline-parser (point-max) t
)
4192 (list (org-element-headline-parser (point-max) t
))))
4193 ;; Otherwise move at the beginning of the section containing
4195 (let ((origin (point))
4196 (end (save-excursion
4197 (org-with-limited-levels (outline-next-heading)) (point)))
4198 element type special-flag trail struct prevs parent
)
4199 (org-with-limited-levels
4200 (if (org-with-limited-levels (org-before-first-heading-p))
4201 (goto-char (point-min))
4202 (org-back-to-heading)
4204 (org-skip-whitespace)
4206 ;; Parse successively each element, skipping those ending
4207 ;; before original position.
4211 (org-element--current-element end
'element special-flag struct
)
4213 (org-element-put-property element
:parent parent
)
4214 (when keep-trail
(push element trail
))
4216 ;; 1. Skip any element ending before point. Also skip
4217 ;; element ending at point when we're sure that another
4218 ;; element has started.
4219 ((let ((elem-end (org-element-property :end element
)))
4220 (when (or (< elem-end origin
)
4221 (and (= elem-end origin
) (/= elem-end end
)))
4222 (goto-char elem-end
))))
4223 ;; 2. An element containing point is always the element at
4225 ((not (memq type org-element-greater-elements
))
4226 (throw 'exit
(if keep-trail trail element
)))
4227 ;; 3. At any other greater element type, if point is
4228 ;; within contents, move into it.
4230 (let ((cbeg (org-element-property :contents-begin element
))
4231 (cend (org-element-property :contents-end element
)))
4232 (if (or (not cbeg
) (not cend
) (> cbeg origin
) (< cend origin
)
4233 ;; Create an anchor for tables and plain lists:
4234 ;; when point is at the very beginning of these
4235 ;; elements, ignoring affiliated keywords,
4236 ;; target them instead of their contents.
4237 (and (= cbeg origin
) (memq type
'(plain-list table
)))
4238 ;; When point is at contents end, do not move
4239 ;; into elements with an explicit ending, but
4240 ;; return that element instead.
4241 (and (= cend origin
)
4244 drawer dynamic-block inlinetask item
4245 plain-list quote-block special-block
))))
4246 (throw 'exit
(if keep-trail trail element
))
4247 (setq parent element
)
4250 (setq special-flag
'item
4251 struct
(org-element-property :structure element
)))
4252 (table (setq special-flag
'table-row
))
4253 (otherwise (setq special-flag nil
)))
4255 (goto-char cbeg
)))))))))))
4258 (defun org-element-context ()
4259 "Return closest element or object around point.
4261 Return value is a list like (TYPE PROPS) where TYPE is the type
4262 of the element or object and PROPS a plist of properties
4265 Possible types are defined in `org-element-all-elements' and
4266 `org-element-all-objects'. Properties depend on element or
4267 object type, but always include :begin, :end, :parent
4268 and :post-blank properties."
4269 (org-with-wide-buffer
4270 (let* ((origin (point))
4271 (element (org-element-at-point))
4272 (type (car element
))
4274 ;; Check if point is inside an element containing objects or at
4275 ;; a secondary string. In that case, move to beginning of the
4276 ;; element or secondary string and set END to the other side.
4277 (if (not (or (and (eq type
'item
)
4278 (let ((tag (org-element-property :tag element
)))
4282 (search-forward tag
(point-at-eol))
4283 (goto-char (match-beginning 0))
4284 (and (>= origin
(point))
4286 ;; `1+' is required so some
4287 ;; successors can match
4288 ;; properly their object.
4289 (setq end
(1+ (match-end 0)))))))))
4290 (and (memq type
'(headline inlinetask
))
4291 (progn (beginning-of-line)
4292 (skip-chars-forward "* ")
4293 (setq end
(point-at-eol))))
4294 (and (memq type
'(paragraph table-row verse-block
))
4295 (let ((cbeg (org-element-property
4296 :contents-begin element
))
4297 (cend (org-element-property
4298 :contents-end element
)))
4299 (and (>= origin cbeg
)
4301 (progn (goto-char cbeg
) (setq end cend
)))))))
4303 (let ((restriction (org-element-restriction element
))
4307 (while (setq candidates
(org-element--get-next-object-candidates
4308 end restriction candidates
))
4309 (let ((closest-cand (rassq (apply 'min
(mapcar 'cdr candidates
))
4311 ;; If ORIGIN is before next object in element, there's
4312 ;; no point in looking further.
4313 (if (> (cdr closest-cand
) origin
) (throw 'exit parent
)
4315 (progn (goto-char (cdr closest-cand
))
4316 (funcall (intern (format "org-element-%s-parser"
4317 (car closest-cand
))))))
4318 (cbeg (org-element-property :contents-begin object
))
4319 (cend (org-element-property :contents-end object
)))
4321 ;; ORIGIN is after OBJECT, so skip it.
4322 ((< (org-element-property :end object
) origin
)
4323 (goto-char (org-element-property :end object
)))
4324 ;; ORIGIN is within a non-recursive object or at an
4325 ;; object boundaries: Return that object.
4326 ((or (not cbeg
) (> cbeg origin
) (< cend origin
))
4328 (org-element-put-property object
:parent parent
)))
4329 ;; Otherwise, move within current object and restrict
4330 ;; search to the end of its contents.
4332 (org-element-put-property object
:parent parent
)
4334 restriction
(org-element-restriction object
)
4338 (defsubst org-element-nested-p
(elem-A elem-B
)
4339 "Non-nil when elements ELEM-A and ELEM-B are nested."
4340 (let ((beg-A (org-element-property :begin elem-A
))
4341 (beg-B (org-element-property :begin elem-B
))
4342 (end-A (org-element-property :end elem-A
))
4343 (end-B (org-element-property :end elem-B
)))
4344 (or (and (>= beg-A beg-B
) (<= end-A end-B
))
4345 (and (>= beg-B beg-A
) (<= end-B end-A
)))))
4347 (defun org-element-swap-A-B (elem-A elem-B
)
4348 "Swap elements ELEM-A and ELEM-B.
4349 Assume ELEM-B is after ELEM-A in the buffer. Leave point at the
4351 (goto-char (org-element-property :begin elem-A
))
4352 ;; There are two special cases when an element doesn't start at bol:
4353 ;; the first paragraph in an item or in a footnote definition.
4354 (let ((specialp (not (bolp))))
4355 ;; Only a paragraph without any affiliated keyword can be moved at
4356 ;; ELEM-A position in such a situation. Note that the case of
4357 ;; a footnote definition is impossible: it cannot contain two
4358 ;; paragraphs in a row because it cannot contain a blank line.
4360 (or (not (eq (org-element-type elem-B
) 'paragraph
))
4361 (/= (org-element-property :begin elem-B
)
4362 (org-element-property :contents-begin elem-B
))))
4363 (error "Cannot swap elements"))
4364 ;; In a special situation, ELEM-A will have no indentation. We'll
4365 ;; give it ELEM-B's (which will in, in turn, have no indentation).
4366 (let* ((ind-B (when specialp
4367 (goto-char (org-element-property :begin elem-B
))
4368 (org-get-indentation)))
4369 (beg-A (org-element-property :begin elem-A
))
4370 (end-A (save-excursion
4371 (goto-char (org-element-property :end elem-A
))
4372 (skip-chars-backward " \r\t\n")
4374 (beg-B (org-element-property :begin elem-B
))
4375 (end-B (save-excursion
4376 (goto-char (org-element-property :end elem-B
))
4377 (skip-chars-backward " \r\t\n")
4379 ;; Store overlays responsible for visibility status. We
4380 ;; also need to store their boundaries as they will be
4381 ;; removed from buffer.
4384 (mapcar (lambda (ov) (list ov
(overlay-start ov
) (overlay-end ov
)))
4385 (overlays-in beg-A end-A
))
4386 (mapcar (lambda (ov) (list ov
(overlay-start ov
) (overlay-end ov
)))
4387 (overlays-in beg-B end-B
))))
4389 (body-A (buffer-substring beg-A end-A
))
4390 (body-B (delete-and-extract-region beg-B end-B
)))
4393 (setq body-B
(replace-regexp-in-string "\\`[ \t]*" "" body-B
))
4394 (org-indent-to-column ind-B
))
4396 ;; Restore ex ELEM-A overlays.
4397 (let ((offset (- beg-B beg-A
)))
4400 (car ov
) (+ (nth 1 ov
) offset
) (+ (nth 2 ov
) offset
)))
4403 (delete-region beg-A end-A
)
4405 ;; Restore ex ELEM-B overlays.
4408 (car ov
) (- (nth 1 ov
) offset
) (- (nth 2 ov
) offset
)))
4410 (goto-char (org-element-property :end elem-B
)))))
4412 (provide 'org-element
)
4415 ;; generated-autoload-file: "org-loaddefs.el"
4418 ;;; org-element.el ends here