1 ;;; org-element.el --- Parser And Applications for Org syntax
3 ;; Copyright (C) 2012 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; Org syntax can be divided into three categories: "Greater
26 ;; elements", "Elements" and "Objects".
28 ;; Elements are related to the structure of the document. Indeed, all
29 ;; elements are a cover for the document: each position within belongs
30 ;; to at least one element.
32 ;; An element always starts and ends at the beginning of a line. With
33 ;; a few exceptions (`clock', `headline', `inlinetask', `item',
34 ;; `planning', `node-property', `quote-section' `section' and
35 ;; `table-row' types), it can also accept a fixed set of keywords as
36 ;; attributes. Those are called "affiliated keywords" to distinguish
37 ;; them from other keywords, which are full-fledged elements. Almost
38 ;; all affiliated keywords are referenced in
39 ;; `org-element-affiliated-keywords'; the others are export attributes
40 ;; and start with "ATTR_" prefix.
42 ;; Element containing other elements (and only elements) are called
43 ;; greater elements. Concerned types are: `center-block', `drawer',
44 ;; `dynamic-block', `footnote-definition', `headline', `inlinetask',
45 ;; `item', `plain-list', `property-drawer', `quote-block', `section'
46 ;; and `special-block'.
48 ;; Other element types are: `babel-call', `clock', `comment',
49 ;; `comment-block', `example-block', `export-block', `fixed-width',
50 ;; `horizontal-rule', `keyword', `latex-environment', `node-property',
51 ;; `paragraph', `planning', `quote-section', `src-block', `table',
52 ;; `table-row' and `verse-block'. Among them, `paragraph' and
53 ;; `verse-block' types can contain Org objects and plain text.
55 ;; Objects are related to document's contents. Some of them are
56 ;; recursive. Associated types are of the following: `bold', `code',
57 ;; `entity', `export-snippet', `footnote-reference',
58 ;; `inline-babel-call', `inline-src-block', `italic',
59 ;; `latex-fragment', `line-break', `link', `macro', `radio-target',
60 ;; `statistics-cookie', `strike-through', `subscript', `superscript',
61 ;; `table-cell', `target', `timestamp', `underline' and `verbatim'.
63 ;; Some elements also have special properties whose value can hold
64 ;; objects themselves (i.e. an item tag or an headline name). Such
65 ;; values are called "secondary strings". Any object belongs to
66 ;; either an element or a secondary string.
68 ;; Notwithstanding affiliated keywords, each greater element, element
69 ;; and object has a fixed set of properties attached to it. Among
70 ;; them, four are shared by all types: `:begin' and `:end', which
71 ;; refer to the beginning and ending buffer positions of the
72 ;; considered element or object, `:post-blank', which holds the number
73 ;; of blank lines, or white spaces, at its end and `:parent' which
74 ;; refers to the element or object containing it. Greater elements
75 ;; and elements containing objects will also have `:contents-begin'
76 ;; and `:contents-end' properties to delimit contents.
78 ;; Lisp-wise, an element or an object can be represented as a list.
79 ;; It follows the pattern (TYPE PROPERTIES CONTENTS), where:
80 ;; TYPE is a symbol describing the Org element or object.
81 ;; PROPERTIES is the property list attached to it. See docstring of
82 ;; appropriate parsing function to get an exhaustive
84 ;; CONTENTS is a list of elements, objects or raw strings contained
85 ;; in the current element or object, when applicable.
87 ;; An Org buffer is a nested list of such elements and objects, whose
88 ;; type is `org-data' and properties is nil.
90 ;; The first part of this file defines Org syntax, while the second
91 ;; one provide accessors and setters functions.
93 ;; The next part implements a parser and an interpreter for each
94 ;; element and object type in Org syntax.
96 ;; The following part creates a fully recursive buffer parser. It
97 ;; also provides a tool to map a function to elements or objects
98 ;; matching some criteria in the parse tree. Functions of interest
99 ;; are `org-element-parse-buffer', `org-element-map' and, to a lesser
100 ;; extent, `org-element-parse-secondary-string'.
102 ;; The penultimate part is the cradle of an interpreter for the
103 ;; obtained parse tree: `org-element-interpret-data'.
105 ;; The library ends by furnishing `org-element-at-point' function, and
106 ;; a way to give information about document structure around point
107 ;; with `org-element-context'.
112 (eval-when-compile (require 'cl
))
117 ;;; Definitions And Rules
119 ;; Define elements, greater elements and specify recursive objects,
120 ;; along with the affiliated keywords recognized. Also set up
121 ;; restrictions on recursive objects combinations.
123 ;; These variables really act as a control center for the parsing
126 (defconst org-element-paragraph-separate
128 ;; Headlines, inlinetasks.
129 org-outline-regexp
"\\|"
130 ;; Footnote definitions.
131 "\\[\\(?:[0-9]+\\|fn:[-_[:word:]]+\\)\\]" "\\|"
135 ;; Tables (any type).
136 "\\(?:|\\|\\+-[-+]\\)" "\\|"
137 ;; Blocks (any type), Babel calls, drawers (any type),
138 ;; fixed-width areas and keywords. Note: this is only an
139 ;; indication and need some thorough check.
142 "-\\{5,\\}[ \t]*$" "\\|"
143 ;; LaTeX environments.
144 "\\\\begin{\\([A-Za-z0-9]+\\*?\\)}" "\\|"
145 ;; Planning and Clock lines.
146 (regexp-opt (list org-scheduled-string
152 (let ((term (case org-plain-list-ordered-item-terminator
153 (?\
) ")") (?.
"\\.") (otherwise "[.)]")))
154 (alpha (and org-alphabetical-lists
"\\|[A-Za-z]")))
155 (concat "\\(?:[-+*]\\|\\(?:[0-9]+" alpha
"\\)" term
"\\)"
156 "\\(?:[ \t]\\|$\\)"))
158 "Regexp to separate paragraphs in an Org buffer.
159 In the case of lines starting with \"#\" and \":\", this regexp
160 is not sufficient to know if point is at a paragraph ending. See
161 `org-element-paragraph-parser' for more information.")
163 (defconst org-element-all-elements
164 '(babel-call center-block clock comment comment-block drawer dynamic-block
165 example-block export-block fixed-width footnote-definition
166 headline horizontal-rule inlinetask item keyword
167 latex-environment node-property paragraph plain-list planning
168 property-drawer quote-block quote-section section special-block
169 src-block table table-row verse-block
)
170 "Complete list of element types.")
172 (defconst org-element-greater-elements
173 '(center-block drawer dynamic-block footnote-definition headline inlinetask
174 item plain-list property-drawer quote-block section
176 "List of recursive element types aka Greater Elements.")
178 (defconst org-element-all-successors
179 '(export-snippet footnote-reference inline-babel-call inline-src-block
180 latex-or-entity line-break link macro radio-target
181 statistics-cookie sub
/superscript table-cell target
182 text-markup timestamp
)
183 "Complete list of successors.")
185 (defconst org-element-object-successor-alist
186 '((subscript . sub
/superscript
) (superscript . sub
/superscript
)
187 (bold . text-markup
) (code . text-markup
) (italic . text-markup
)
188 (strike-through . text-markup
) (underline . text-markup
)
189 (verbatim . text-markup
) (entity . latex-or-entity
)
190 (latex-fragment . latex-or-entity
))
191 "Alist of translations between object type and successor name.
193 Sharing the same successor comes handy when, for example, the
194 regexp matching one object can also match the other object.")
196 (defconst org-element-all-objects
197 '(bold code entity export-snippet footnote-reference inline-babel-call
198 inline-src-block italic line-break latex-fragment link macro
199 radio-target statistics-cookie strike-through subscript superscript
200 table-cell target timestamp underline verbatim
)
201 "Complete list of object types.")
203 (defconst org-element-recursive-objects
204 '(bold italic link subscript radio-target strike-through superscript
205 table-cell underline
)
206 "List of recursive object types.")
208 (defconst org-element-block-name-alist
209 '(("CENTER" . org-element-center-block-parser
)
210 ("COMMENT" . org-element-comment-block-parser
)
211 ("EXAMPLE" . org-element-example-block-parser
)
212 ("QUOTE" . org-element-quote-block-parser
)
213 ("SRC" . org-element-src-block-parser
)
214 ("VERSE" . org-element-verse-block-parser
))
215 "Alist between block names and the associated parsing function.
216 Names must be uppercase. Any block whose name has no association
217 is parsed with `org-element-special-block-parser'.")
219 (defconst org-element-link-type-is-file
220 '("file" "file+emacs" "file+sys" "docview")
221 "List of link types equivalent to \"file\".
222 Only these types can accept search options and an explicit
223 application to open them.")
225 (defconst org-element-affiliated-keywords
226 '("CAPTION" "DATA" "HEADER" "HEADERS" "LABEL" "NAME" "PLOT" "RESNAME" "RESULT"
227 "RESULTS" "SOURCE" "SRCNAME" "TBLNAME")
228 "List of affiliated keywords as strings.
229 By default, all keywords setting attributes (i.e. \"ATTR_LATEX\")
230 are affiliated keywords and need not to be in this list.")
232 (defconst org-element--affiliated-re
233 (format "[ \t]*#\\+%s:"
234 ;; Regular affiliated keywords.
235 (format "\\(%s\\|ATTR_[-_A-Za-z0-9]+\\)\\(?:\\[\\(.*\\)\\]\\)?"
236 (regexp-opt org-element-affiliated-keywords
)))
237 "Regexp matching any affiliated keyword.
239 Keyword name is put in match group 1. Moreover, if keyword
240 belongs to `org-element-dual-keywords', put the dual value in
243 Don't modify it, set `org-element-affiliated-keywords' instead.")
245 (defconst org-element-keyword-translation-alist
246 '(("DATA" .
"NAME") ("LABEL" .
"NAME") ("RESNAME" .
"NAME")
247 ("SOURCE" .
"NAME") ("SRCNAME" .
"NAME") ("TBLNAME" .
"NAME")
248 ("RESULT" .
"RESULTS") ("HEADERS" .
"HEADER"))
249 "Alist of usual translations for keywords.
250 The key is the old name and the value the new one. The property
251 holding their value will be named after the translated name.")
253 (defconst org-element-multiple-keywords
'("CAPTION" "HEADER")
254 "List of affiliated keywords that can occur more than once in an element.
256 Their value will be consed into a list of strings, which will be
257 returned as the value of the property.
259 This list is checked after translations have been applied. See
260 `org-element-keyword-translation-alist'.
262 By default, all keywords setting attributes (i.e. \"ATTR_LATEX\")
263 allow multiple occurrences and need not to be in this list.")
265 (defconst org-element-parsed-keywords
'("AUTHOR" "CAPTION" "DATE" "TITLE")
266 "List of keywords whose value can be parsed.
268 Their value will be stored as a secondary string: a list of
271 This list is checked after translations have been applied. See
272 `org-element-keyword-translation-alist'.")
274 (defconst org-element-dual-keywords
'("CAPTION" "RESULTS")
275 "List of keywords which can have a secondary value.
277 In Org syntax, they can be written with optional square brackets
278 before the colons. For example, results keyword can be
279 associated to a hash value with the following:
281 #+RESULTS[hash-string]: some-source
283 This list is checked after translations have been applied. See
284 `org-element-keyword-translation-alist'.")
286 (defconst org-element-object-restrictions
287 '((bold export-snippet inline-babel-call inline-src-block latex-or-entity link
288 radio-target sub
/superscript target text-markup timestamp
)
289 (footnote-reference export-snippet footnote-reference inline-babel-call
290 inline-src-block latex-or-entity line-break link macro
291 radio-target sub
/superscript target text-markup
293 (headline inline-babel-call inline-src-block latex-or-entity link macro
294 radio-target statistics-cookie sub
/superscript target text-markup
296 (inlinetask inline-babel-call inline-src-block latex-or-entity link macro
297 radio-target sub
/superscript target text-markup timestamp
)
298 (italic export-snippet inline-babel-call inline-src-block latex-or-entity
299 link radio-target sub
/superscript target text-markup timestamp
)
300 (item export-snippet footnote-reference inline-babel-call latex-or-entity
301 link macro radio-target sub
/superscript target text-markup
)
302 (keyword latex-or-entity macro sub
/superscript text-markup
)
303 (link export-snippet inline-babel-call inline-src-block latex-or-entity link
304 sub
/superscript text-markup
)
305 (paragraph export-snippet footnote-reference inline-babel-call
306 inline-src-block latex-or-entity line-break link macro
307 radio-target statistics-cookie sub
/superscript target text-markup
309 (radio-target export-snippet latex-or-entity sub
/superscript
)
310 (strike-through export-snippet inline-babel-call inline-src-block
311 latex-or-entity link radio-target sub
/superscript target
312 text-markup timestamp
)
313 (subscript export-snippet inline-babel-call inline-src-block latex-or-entity
314 sub
/superscript target text-markup
)
315 (superscript export-snippet inline-babel-call inline-src-block
316 latex-or-entity sub
/superscript target text-markup
)
317 (table-cell export-snippet latex-or-entity link macro radio-target
318 sub
/superscript target text-markup timestamp
)
319 (table-row table-cell
)
320 (underline export-snippet inline-babel-call inline-src-block latex-or-entity
321 link radio-target sub
/superscript target text-markup timestamp
)
322 (verse-block footnote-reference inline-babel-call inline-src-block
323 latex-or-entity line-break link macro radio-target
324 sub
/superscript target text-markup timestamp
))
325 "Alist of objects restrictions.
327 CAR is an element or object type containing objects and CDR is
328 a list of successors that will be called within an element or
331 For example, in a `radio-target' object, one can only find
332 entities, export snippets, latex-fragments, subscript and
335 This alist also applies to secondary string. For example, an
336 `headline' type element doesn't directly contain objects, but
337 still has an entry since one of its properties (`:title') does.")
339 (defconst org-element-secondary-value-alist
340 '((headline .
:title
)
341 (inlinetask .
:title
)
343 (footnote-reference .
:inline-definition
))
344 "Alist between element types and location of secondary value.")
348 ;;; Accessors and Setters
350 ;; Provide four accessors: `org-element-type', `org-element-property'
351 ;; `org-element-contents' and `org-element-restriction'.
353 ;; Setter functions allow to modify elements by side effect. There is
354 ;; `org-element-put-property', `org-element-set-contents',
355 ;; `org-element-set-element' and `org-element-adopt-element'. Note
356 ;; that `org-element-set-element' and `org-element-adopt-elements' are
357 ;; higher level functions since also update `:parent' property.
359 (defsubst org-element-type
(element)
360 "Return type of ELEMENT.
362 The function returns the type of the element or object provided.
363 It can also return the following special value:
364 `plain-text' for a string
365 `org-data' for a complete document
366 nil in any other case."
368 ((not (consp element
)) (and (stringp element
) 'plain-text
))
369 ((symbolp (car element
)) (car element
))))
371 (defsubst org-element-property
(property element
)
372 "Extract the value from the PROPERTY of an ELEMENT."
373 (plist-get (nth 1 element
) property
))
375 (defsubst org-element-contents
(element)
376 "Extract contents from an ELEMENT."
377 (and (consp element
) (nthcdr 2 element
)))
379 (defsubst org-element-restriction
(element)
380 "Return restriction associated to ELEMENT.
381 ELEMENT can be an element, an object or a symbol representing an
382 element or object type."
383 (cdr (assq (if (symbolp element
) element
(org-element-type element
))
384 org-element-object-restrictions
)))
386 (defsubst org-element-put-property
(element property value
)
387 "In ELEMENT set PROPERTY to VALUE.
388 Return modified element."
389 (when (consp element
)
390 (setcar (cdr element
) (plist-put (nth 1 element
) property value
)))
393 (defsubst org-element-set-contents
(element &rest contents
)
394 "Set ELEMENT contents to CONTENTS.
395 Return modified element."
396 (cond ((not element
) (list contents
))
397 ((cdr element
) (setcdr (cdr element
) contents
))
398 (t (nconc element contents
))))
400 (defsubst org-element-set-element
(old new
)
401 "Replace element or object OLD with element or object NEW.
402 The function takes care of setting `:parent' property for NEW."
403 ;; Since OLD is going to be changed into NEW by side-effect, first
404 ;; make sure that every element or object within NEW has OLD as
406 (mapc (lambda (blob) (org-element-put-property blob
:parent old
))
407 (org-element-contents new
))
408 ;; Transfer contents.
409 (apply 'org-element-set-contents old
(org-element-contents new
))
410 ;; Ensure NEW has same parent as OLD, then overwrite OLD properties
412 (org-element-put-property new
:parent
(org-element-property :parent old
))
413 (setcar (cdr old
) (nth 1 new
))
415 (setcar old
(car new
)))
417 (defsubst org-element-adopt-elements
(parent &rest children
)
418 "Append elements to the contents of another element.
420 PARENT is an element or object. CHILDREN can be elements,
421 objects, or a strings.
423 The function takes care of setting `:parent' property for CHILD.
424 Return parent element."
425 (if (not parent
) children
426 ;; Link every child to PARENT.
427 (mapc (lambda (child)
428 (unless (stringp child
)
429 (org-element-put-property child
:parent parent
)))
431 ;; Add CHILDREN at the end of PARENT contents.
432 (apply 'org-element-set-contents
434 (nconc (org-element-contents parent
) children
))
435 ;; Return modified PARENT element.
442 ;; For each greater element type, we define a parser and an
445 ;; A parser returns the element or object as the list described above.
446 ;; Most of them accepts no argument. Though, exceptions exist. Hence
447 ;; every element containing a secondary string (see
448 ;; `org-element-secondary-value-alist') will accept an optional
449 ;; argument to toggle parsing of that secondary string. Moreover,
450 ;; `item' parser requires current list's structure as its first
453 ;; An interpreter accepts two arguments: the list representation of
454 ;; the element or object, and its contents. The latter may be nil,
455 ;; depending on the element or object considered. It returns the
456 ;; appropriate Org syntax, as a string.
458 ;; Parsing functions must follow the naming convention:
459 ;; org-element-TYPE-parser, where TYPE is greater element's type, as
460 ;; defined in `org-element-greater-elements'.
462 ;; Similarly, interpreting functions must follow the naming
463 ;; convention: org-element-TYPE-interpreter.
465 ;; With the exception of `headline' and `item' types, greater elements
466 ;; cannot contain other greater elements of their own type.
468 ;; Beside implementing a parser and an interpreter, adding a new
469 ;; greater element requires to tweak `org-element--current-element'.
470 ;; Moreover, the newly defined type must be added to both
471 ;; `org-element-all-elements' and `org-element-greater-elements'.
476 (defun org-element-center-block-parser (limit affiliated
)
477 "Parse a center block.
479 LIMIT bounds the search. AFFILIATED is a list of which CAR is
480 the buffer position at the beginning of the first affiliated
481 keyword and CDR is a plist of affiliated keywords along with
484 Return a list whose CAR is `center-block' and CDR is a plist
485 containing `:begin', `:end', `:hiddenp', `:contents-begin',
486 `:contents-end' and `:post-blank' keywords.
488 Assume point is at the beginning of the block."
489 (let ((case-fold-search t
))
490 (if (not (save-excursion
491 (re-search-forward "^[ \t]*#\\+END_CENTER[ \t]*$" limit t
)))
492 ;; Incomplete block: parse it as a paragraph.
493 (org-element-paragraph-parser limit affiliated
)
494 (let ((block-end-line (match-beginning 0)))
495 (let* ((begin (car affiliated
))
496 ;; Empty blocks have no contents.
497 (contents-begin (progn (forward-line)
498 (and (< (point) block-end-line
)
500 (contents-end (and contents-begin block-end-line
))
501 (hidden (org-invisible-p2))
502 (pos-before-blank (progn (goto-char block-end-line
)
505 (end (save-excursion (skip-chars-forward " \r\t\n" limit
)
506 (if (eobp) (point) (point-at-bol)))))
512 :contents-begin contents-begin
513 :contents-end contents-end
514 :post-blank
(count-lines pos-before-blank end
))
515 (cdr affiliated
))))))))
517 (defun org-element-center-block-interpreter (center-block contents
)
518 "Interpret CENTER-BLOCK element as Org syntax.
519 CONTENTS is the contents of the element."
520 (format "#+BEGIN_CENTER\n%s#+END_CENTER" contents
))
525 (defun org-element-drawer-parser (limit affiliated
)
528 LIMIT bounds the search. AFFILIATED is a list of which CAR is
529 the buffer position at the beginning of the first affiliated
530 keyword and CDR is a plist of affiliated keywords along with
533 Return a list whose CAR is `drawer' and CDR is a plist containing
534 `:drawer-name', `:begin', `:end', `:hiddenp', `:contents-begin',
535 `:contents-end' and `:post-blank' keywords.
537 Assume point is at beginning of drawer."
538 (let ((case-fold-search t
))
539 (if (not (save-excursion (re-search-forward "^[ \t]*:END:[ \t]*$" limit t
)))
540 ;; Incomplete drawer: parse it as a paragraph.
541 (org-element-paragraph-parser limit affiliated
)
543 (let* ((drawer-end-line (match-beginning 0))
544 (name (progn (looking-at org-drawer-regexp
)
545 (org-match-string-no-properties 1)))
546 (begin (car affiliated
))
547 ;; Empty drawers have no contents.
548 (contents-begin (progn (forward-line)
549 (and (< (point) drawer-end-line
)
551 (contents-end (and contents-begin drawer-end-line
))
552 (hidden (org-invisible-p2))
553 (pos-before-blank (progn (goto-char drawer-end-line
)
556 (end (progn (skip-chars-forward " \r\t\n" limit
)
557 (if (eobp) (point) (point-at-bol)))))
564 :contents-begin contents-begin
565 :contents-end contents-end
566 :post-blank
(count-lines pos-before-blank end
))
567 (cdr affiliated
))))))))
569 (defun org-element-drawer-interpreter (drawer contents
)
570 "Interpret DRAWER element as Org syntax.
571 CONTENTS is the contents of the element."
572 (format ":%s:\n%s:END:"
573 (org-element-property :drawer-name drawer
)
579 (defun org-element-dynamic-block-parser (limit affiliated
)
580 "Parse a dynamic block.
582 LIMIT bounds the search. AFFILIATED is a list of which CAR is
583 the buffer position at the beginning of the first affiliated
584 keyword and CDR is a plist of affiliated keywords along with
587 Return a list whose CAR is `dynamic-block' and CDR is a plist
588 containing `:block-name', `:begin', `:end', `:hiddenp',
589 `:contents-begin', `:contents-end', `:arguments' and
590 `:post-blank' keywords.
592 Assume point is at beginning of dynamic block."
593 (let ((case-fold-search t
))
594 (if (not (save-excursion
595 (re-search-forward "^[ \t]*#\\+END:?[ \t]*$" limit t
)))
596 ;; Incomplete block: parse it as a paragraph.
597 (org-element-paragraph-parser limit affiliated
)
598 (let ((block-end-line (match-beginning 0)))
600 (let* ((name (progn (looking-at org-dblock-start-re
)
601 (org-match-string-no-properties 1)))
602 (arguments (org-match-string-no-properties 3))
603 (begin (car affiliated
))
604 ;; Empty blocks have no contents.
605 (contents-begin (progn (forward-line)
606 (and (< (point) block-end-line
)
608 (contents-end (and contents-begin block-end-line
))
609 (hidden (org-invisible-p2))
610 (pos-before-blank (progn (goto-char block-end-line
)
613 (end (progn (skip-chars-forward " \r\t\n" limit
)
614 (if (eobp) (point) (point-at-bol)))))
622 :contents-begin contents-begin
623 :contents-end contents-end
624 :post-blank
(count-lines pos-before-blank end
))
625 (cdr affiliated
)))))))))
627 (defun org-element-dynamic-block-interpreter (dynamic-block contents
)
628 "Interpret DYNAMIC-BLOCK element as Org syntax.
629 CONTENTS is the contents of the element."
630 (format "#+BEGIN: %s%s\n%s#+END:"
631 (org-element-property :block-name dynamic-block
)
632 (let ((args (org-element-property :arguments dynamic-block
)))
633 (and args
(concat " " args
)))
637 ;;;; Footnote Definition
639 (defun org-element-footnote-definition-parser (limit affiliated
)
640 "Parse a footnote definition.
642 LIMIT bounds the search. AFFILIATED is a list of which CAR is
643 the buffer position at the beginning of the first affiliated
644 keyword and CDR is a plist of affiliated keywords along with
647 Return a list whose CAR is `footnote-definition' and CDR is
648 a plist containing `:label', `:begin' `:end', `:contents-begin',
649 `:contents-end' and `:post-blank' keywords.
651 Assume point is at the beginning of the footnote definition."
653 (let* ((label (progn (looking-at org-footnote-definition-re
)
654 (org-match-string-no-properties 1)))
655 (begin (car affiliated
))
656 (ending (save-excursion
660 (concat org-outline-regexp-bol
"\\|"
661 org-footnote-definition-re
"\\|"
662 "^[ \t]*$") limit
'move
))
665 (contents-begin (progn (search-forward "]")
666 (skip-chars-forward " \r\t\n" ending
)
667 (and (/= (point) ending
) (point))))
668 (contents-end (and contents-begin ending
))
669 (end (progn (goto-char ending
)
670 (skip-chars-forward " \r\t\n" limit
)
671 (if (eobp) (point) (point-at-bol)))))
672 (list 'footnote-definition
677 :contents-begin contents-begin
678 :contents-end contents-end
679 :post-blank
(count-lines ending end
))
680 (cdr affiliated
))))))
682 (defun org-element-footnote-definition-interpreter (footnote-definition contents
)
683 "Interpret FOOTNOTE-DEFINITION element as Org syntax.
684 CONTENTS is the contents of the footnote-definition."
685 (concat (format "[%s]" (org-element-property :label footnote-definition
))
692 (defun org-element-headline-parser (limit &optional raw-secondary-p
)
695 Return a list whose CAR is `headline' and CDR is a plist
696 containing `:raw-value', `:title', `:begin', `:end',
697 `:pre-blank', `:hiddenp', `:contents-begin' and `:contents-end',
698 `:level', `:priority', `:tags', `:todo-keyword',`:todo-type',
699 `:scheduled', `:deadline', `:timestamp', `:clock', `:category',
700 `:quotedp', `:archivedp', `:commentedp' and `:footnote-section-p'
703 The plist also contains any property set in the property drawer,
704 with its name in lowercase, the underscores replaced with hyphens
705 and colons at the beginning (i.e. `:custom-id').
707 When RAW-SECONDARY-P is non-nil, headline's title will not be
708 parsed as a secondary string, but as a plain string instead.
710 Assume point is at beginning of the headline."
712 (let* ((components (org-heading-components))
713 (level (nth 1 components
))
714 (todo (nth 2 components
))
716 (and todo
(if (member todo org-done-keywords
) 'done
'todo
)))
717 (tags (let ((raw-tags (nth 5 components
)))
718 (and raw-tags
(org-split-string raw-tags
":"))))
719 (raw-value (or (nth 4 components
) ""))
721 (let ((case-fold-search nil
))
722 (string-match (format "^%s\\( \\|$\\)" org-quote-string
)
725 (let ((case-fold-search nil
))
726 (string-match (format "^%s\\( \\|$\\)" org-comment-string
)
728 (archivedp (member org-archive-tag tags
))
729 (footnote-section-p (and org-footnote-section
730 (string= org-footnote-section raw-value
)))
731 ;; Normalize property names: ":SOME_PROP:" becomes
733 (standard-props (let (plist)
736 (let ((p-name (downcase (car p
))))
737 (while (string-match "_" p-name
)
739 (replace-match "-" nil nil p-name
)))
740 (setq p-name
(intern (concat ":" p-name
)))
742 (plist-put plist p-name
(cdr p
)))))
743 (org-entry-properties nil
'standard
))
745 (time-props (org-entry-properties nil
'special
"CLOCK"))
746 (scheduled (cdr (assoc "SCHEDULED" time-props
)))
747 (deadline (cdr (assoc "DEADLINE" time-props
)))
748 (clock (cdr (assoc "CLOCK" time-props
)))
749 (timestamp (cdr (assoc "TIMESTAMP" time-props
)))
751 (end (save-excursion (goto-char (org-end-of-subtree t t
))))
752 (pos-after-head (progn (forward-line) (point)))
753 (contents-begin (save-excursion
754 (skip-chars-forward " \r\t\n" end
)
755 (and (/= (point) end
) (line-beginning-position))))
756 (hidden (org-invisible-p2))
757 (contents-end (and contents-begin
758 (progn (goto-char end
)
759 (skip-chars-backward " \r\t\n")
762 ;; Clean RAW-VALUE from any quote or comment string.
763 (when (or quotedp commentedp
)
764 (let ((case-fold-search nil
))
766 (replace-regexp-in-string
768 (regexp-opt (list org-quote-string org-comment-string
))
772 ;; Clean TAGS from archive tag, if any.
773 (when archivedp
(setq tags
(delete org-archive-tag tags
)))
777 (list :raw-value raw-value
781 (if (not contents-begin
) 0
782 (count-lines pos-after-head contents-begin
))
784 :contents-begin contents-begin
785 :contents-end contents-end
787 :priority
(nth 3 components
)
795 :post-blank
(count-lines
796 (if (not contents-end
) pos-after-head
797 (goto-char contents-end
)
801 :footnote-section-p footnote-section-p
803 :commentedp commentedp
806 (org-element-put-property
808 (if raw-secondary-p raw-value
809 (org-element-parse-secondary-string
810 raw-value
(org-element-restriction 'headline
) headline
)))))))
812 (defun org-element-headline-interpreter (headline contents
)
813 "Interpret HEADLINE element as Org syntax.
814 CONTENTS is the contents of the element."
815 (let* ((level (org-element-property :level headline
))
816 (todo (org-element-property :todo-keyword headline
))
817 (priority (org-element-property :priority headline
))
818 (title (org-element-interpret-data
819 (org-element-property :title headline
)))
820 (tags (let ((tag-list (if (org-element-property :archivedp headline
)
821 (cons org-archive-tag
822 (org-element-property :tags headline
))
823 (org-element-property :tags headline
))))
825 (format ":%s:" (mapconcat 'identity tag-list
":")))))
826 (commentedp (org-element-property :commentedp headline
))
827 (quotedp (org-element-property :quotedp headline
))
828 (pre-blank (or (org-element-property :pre-blank headline
) 0))
829 (heading (concat (make-string level ?
*)
830 (and todo
(concat " " todo
))
831 (and quotedp
(concat " " org-quote-string
))
832 (and commentedp
(concat " " org-comment-string
))
834 (format " [#%s]" (char-to-string priority
)))
835 (cond ((and org-footnote-section
836 (org-element-property
837 :footnote-section-p headline
))
838 (concat " " org-footnote-section
))
839 (title (concat " " title
))))))
844 ((zerop org-tags-column
) (format " %s" tags
))
845 ((< org-tags-column
0)
848 (max (- (+ org-tags-column
(length heading
) (length tags
))) 1)
853 (make-string (max (- org-tags-column
(length heading
)) 1) ?
)
855 (make-string (1+ pre-blank
) 10)
861 (defun org-element-inlinetask-parser (limit &optional raw-secondary-p
)
862 "Parse an inline task.
864 Return a list whose CAR is `inlinetask' and CDR is a plist
865 containing `:title', `:begin', `:end', `:hiddenp',
866 `:contents-begin' and `:contents-end', `:level', `:priority',
867 `:raw-value', `:tags', `:todo-keyword', `:todo-type',
868 `:scheduled', `:deadline', `:timestamp', `:clock' and
869 `:post-blank' keywords.
871 The plist also contains any property set in the property drawer,
872 with its name in lowercase, the underscores replaced with hyphens
873 and colons at the beginning (i.e. `:custom-id').
875 When optional argument RAW-SECONDARY-P is non-nil, inline-task's
876 title will not be parsed as a secondary string, but as a plain
879 Assume point is at beginning of the inline task."
881 (let* ((keywords (org-element--collect-affiliated-keywords))
882 (begin (car keywords
))
883 (components (org-heading-components))
884 (todo (nth 2 components
))
886 (if (member todo org-done-keywords
) 'done
'todo
)))
887 (tags (let ((raw-tags (nth 5 components
)))
888 (and raw-tags
(org-split-string raw-tags
":"))))
889 (raw-value (or (nth 4 components
) ""))
890 ;; Normalize property names: ":SOME_PROP:" becomes
892 (standard-props (let (plist)
895 (let ((p-name (downcase (car p
))))
896 (while (string-match "_" p-name
)
898 (replace-match "-" nil nil p-name
)))
899 (setq p-name
(intern (concat ":" p-name
)))
901 (plist-put plist p-name
(cdr p
)))))
902 (org-entry-properties nil
'standard
))
904 (time-props (org-entry-properties nil
'special
"CLOCK"))
905 (scheduled (cdr (assoc "SCHEDULED" time-props
)))
906 (deadline (cdr (assoc "DEADLINE" time-props
)))
907 (clock (cdr (assoc "CLOCK" time-props
)))
908 (timestamp (cdr (assoc "TIMESTAMP" time-props
)))
909 (task-end (save-excursion
911 (and (re-search-forward "^\\*+ END" limit t
)
912 (match-beginning 0))))
913 (contents-begin (progn (forward-line)
914 (and task-end
(< (point) task-end
) (point))))
915 (hidden (and contents-begin
(org-invisible-p2)))
916 (contents-end (and contents-begin task-end
))
917 (before-blank (if (not task-end
) (point)
921 (end (progn (skip-chars-forward " \r\t\n" limit
)
922 (if (eobp) (point) (point-at-bol))))
926 (list :raw-value raw-value
930 :contents-begin contents-begin
931 :contents-end contents-end
932 :level
(nth 1 components
)
933 :priority
(nth 3 components
)
941 :post-blank
(count-lines before-blank end
))
944 (org-element-put-property
946 (if raw-secondary-p raw-value
947 (org-element-parse-secondary-string
949 (org-element-restriction 'inlinetask
)
952 (defun org-element-inlinetask-interpreter (inlinetask contents
)
953 "Interpret INLINETASK element as Org syntax.
954 CONTENTS is the contents of inlinetask."
955 (let* ((level (org-element-property :level inlinetask
))
956 (todo (org-element-property :todo-keyword inlinetask
))
957 (priority (org-element-property :priority inlinetask
))
958 (title (org-element-interpret-data
959 (org-element-property :title inlinetask
)))
960 (tags (let ((tag-list (org-element-property :tags inlinetask
)))
962 (format ":%s:" (mapconcat 'identity tag-list
":")))))
963 (task (concat (make-string level ?
*)
964 (and todo
(concat " " todo
))
966 (format " [#%s]" (char-to-string priority
)))
967 (and title
(concat " " title
)))))
972 ((zerop org-tags-column
) (format " %s" tags
))
973 ((< org-tags-column
0)
976 (max (- (+ org-tags-column
(length task
) (length tags
))) 1)
981 (make-string (max (- org-tags-column
(length task
)) 1) ?
)
983 ;; Prefer degenerate inlinetasks when there are no
988 (make-string level ?
*) " END")))))
993 (defun org-element-item-parser (limit struct
&optional raw-secondary-p
)
996 STRUCT is the structure of the plain list.
998 Return a list whose CAR is `item' and CDR is a plist containing
999 `:bullet', `:begin', `:end', `:contents-begin', `:contents-end',
1000 `:checkbox', `:counter', `:tag', `:structure', `:hiddenp' and
1001 `:post-blank' keywords.
1003 When optional argument RAW-SECONDARY-P is non-nil, item's tag, if
1004 any, will not be parsed as a secondary string, but as a plain
1007 Assume point is at the beginning of the item."
1010 (looking-at org-list-full-item-re
)
1011 (let* ((begin (point))
1012 (bullet (org-match-string-no-properties 1))
1013 (checkbox (let ((box (org-match-string-no-properties 3)))
1014 (cond ((equal "[ ]" box
) 'off
)
1015 ((equal "[X]" box
) 'on
)
1016 ((equal "[-]" box
) 'trans
))))
1017 (counter (let ((c (org-match-string-no-properties 2)))
1021 ((string-match "[A-Za-z]" c
)
1022 (- (string-to-char (upcase (match-string 0 c
)))
1024 ((string-match "[0-9]+" c
)
1025 (string-to-number (match-string 0 c
)))))))
1026 (end (save-excursion (goto-char (org-list-get-item-end begin struct
))
1027 (unless (bolp) (forward-line))
1031 ;; Ignore tags in un-ordered lists: they are just
1032 ;; a part of item's body.
1033 (if (and (match-beginning 4)
1034 (save-match-data (string-match "[.)]" bullet
)))
1037 (skip-chars-forward " \r\t\n" limit
)
1038 ;; If first line isn't empty, contents really start
1039 ;; at the text after item's meta-data.
1040 (if (= (point-at-bol) begin
) (point) (point-at-bol))))
1041 (hidden (progn (forward-line)
1042 (and (not (= (point) end
)) (org-invisible-p2))))
1043 (contents-end (progn (goto-char end
)
1044 (skip-chars-backward " \r\t\n")
1049 (list :bullet bullet
1052 ;; CONTENTS-BEGIN and CONTENTS-END may be
1053 ;; mixed up in the case of an empty item
1054 ;; separated from the next by a blank line.
1055 ;; Thus ensure the former is always the
1057 :contents-begin
(min contents-begin contents-end
)
1058 :contents-end
(max contents-begin contents-end
)
1063 :post-blank
(count-lines contents-end end
)))))
1064 (org-element-put-property
1066 (let ((raw-tag (org-list-get-tag begin struct
)))
1068 (if raw-secondary-p raw-tag
1069 (org-element-parse-secondary-string
1070 raw-tag
(org-element-restriction 'item
) item
))))))))
1072 (defun org-element-item-interpreter (item contents
)
1073 "Interpret ITEM element as Org syntax.
1074 CONTENTS is the contents of the element."
1075 (let* ((bullet (org-list-bullet-string (org-element-property :bullet item
)))
1076 (checkbox (org-element-property :checkbox item
))
1077 (counter (org-element-property :counter item
))
1078 (tag (let ((tag (org-element-property :tag item
)))
1079 (and tag
(org-element-interpret-data tag
))))
1080 ;; Compute indentation.
1081 (ind (make-string (length bullet
) 32))
1082 (item-starts-with-par-p
1083 (eq (org-element-type (car (org-element-contents item
)))
1088 (and counter
(format "[@%d] " counter
))
1093 (and tag
(format "%s :: " tag
))
1094 (let ((contents (replace-regexp-in-string
1095 "\\(^\\)[ \t]*\\S-" ind contents nil nil
1)))
1096 (if item-starts-with-par-p
(org-trim contents
)
1097 (concat "\n" contents
))))))
1102 (defun org-element-plain-list-parser (limit affiliated structure
)
1103 "Parse a plain list.
1105 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1106 the buffer position at the beginning of the first affiliated
1107 keyword and CDR is a plist of affiliated keywords along with
1108 their value. STRUCTURE is the structure of the plain list being
1111 Return a list whose CAR is `plain-list' and CDR is a plist
1112 containing `:type', `:begin', `:end', `:contents-begin' and
1113 `:contents-end', `:structure' and `:post-blank' keywords.
1115 Assume point is at the beginning of the list."
1117 (let* ((struct (or structure
(org-list-struct)))
1118 (prevs (org-list-prevs-alist struct
))
1119 (parents (org-list-parents-alist struct
))
1120 (type (org-list-get-list-type (point) struct prevs
))
1121 (contents-begin (point))
1122 (begin (car affiliated
))
1124 (progn (goto-char (org-list-get-list-end (point) struct prevs
))
1125 (unless (bolp) (forward-line))
1127 (end (progn (skip-chars-forward " \r\t\n" limit
)
1128 (if (eobp) (point) (point-at-bol)))))
1135 :contents-begin contents-begin
1136 :contents-end contents-end
1138 :post-blank
(count-lines contents-end end
))
1139 (cdr affiliated
))))))
1141 (defun org-element-plain-list-interpreter (plain-list contents
)
1142 "Interpret PLAIN-LIST element as Org syntax.
1143 CONTENTS is the contents of the element."
1146 (goto-char (point-min))
1151 ;;;; Property Drawer
1153 (defun org-element-property-drawer-parser (limit affiliated
)
1154 "Parse a property drawer.
1156 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1157 the buffer position at the beginning of the first affiliated
1158 keyword and CDR is a plist of affiliated keywords along with
1161 Return a list whose CAR is `property-drawer' and CDR is a plist
1162 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1163 `:contents-end' and `:post-blank' keywords.
1165 Assume point is at the beginning of the property drawer."
1167 (let ((case-fold-search t
))
1168 (if (not (save-excursion
1169 (re-search-forward "^[ \t]*:END:[ \t]*$" limit t
)))
1170 ;; Incomplete drawer: parse it as a paragraph.
1171 (org-element-paragraph-parser limit affiliated
)
1173 (let* ((drawer-end-line (match-beginning 0))
1174 (begin (car affiliated
))
1175 (contents-begin (progn (forward-line)
1176 (and (< (point) drawer-end-line
)
1178 (contents-end (and contents-begin drawer-end-line
))
1179 (hidden (org-invisible-p2))
1180 (pos-before-blank (progn (goto-char drawer-end-line
)
1183 (end (progn (skip-chars-forward " \r\t\n" limit
)
1184 (if (eobp) (point) (point-at-bol)))))
1185 (list 'property-drawer
1190 :contents-begin contents-begin
1191 :contents-end contents-end
1192 :post-blank
(count-lines pos-before-blank end
))
1193 (cdr affiliated
)))))))))
1195 (defun org-element-property-drawer-interpreter (property-drawer contents
)
1196 "Interpret PROPERTY-DRAWER element as Org syntax.
1197 CONTENTS is the properties within the drawer."
1198 (format ":PROPERTIES:\n%s:END:" contents
))
1203 (defun org-element-quote-block-parser (limit affiliated
)
1204 "Parse a quote block.
1206 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1207 the buffer position at the beginning of the first affiliated
1208 keyword and CDR is a plist of affiliated keywords along with
1211 Return a list whose CAR is `quote-block' and CDR is a plist
1212 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1213 `:contents-end' and `:post-blank' keywords.
1215 Assume point is at the beginning of the block."
1216 (let ((case-fold-search t
))
1217 (if (not (save-excursion
1218 (re-search-forward "^[ \t]*#\\+END_QUOTE[ \t]*$" limit t
)))
1219 ;; Incomplete block: parse it as a paragraph.
1220 (org-element-paragraph-parser limit affiliated
)
1221 (let ((block-end-line (match-beginning 0)))
1223 (let* ((begin (car affiliated
))
1224 ;; Empty blocks have no contents.
1225 (contents-begin (progn (forward-line)
1226 (and (< (point) block-end-line
)
1228 (contents-end (and contents-begin block-end-line
))
1229 (hidden (org-invisible-p2))
1230 (pos-before-blank (progn (goto-char block-end-line
)
1233 (end (progn (skip-chars-forward " \r\t\n" limit
)
1234 (if (eobp) (point) (point-at-bol)))))
1240 :contents-begin contents-begin
1241 :contents-end contents-end
1242 :post-blank
(count-lines pos-before-blank end
))
1243 (cdr affiliated
)))))))))
1245 (defun org-element-quote-block-interpreter (quote-block contents
)
1246 "Interpret QUOTE-BLOCK element as Org syntax.
1247 CONTENTS is the contents of the element."
1248 (format "#+BEGIN_QUOTE\n%s#+END_QUOTE" contents
))
1253 (defun org-element-section-parser (limit)
1256 LIMIT bounds the search.
1258 Return a list whose CAR is `section' and CDR is a plist
1259 containing `:begin', `:end', `:contents-begin', `contents-end'
1260 and `:post-blank' keywords."
1262 ;; Beginning of section is the beginning of the first non-blank
1263 ;; line after previous headline.
1264 (let ((begin (point))
1265 (end (progn (org-with-limited-levels (outline-next-heading))
1267 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
1273 :contents-begin begin
1274 :contents-end pos-before-blank
1275 :post-blank
(count-lines pos-before-blank end
))))))
1277 (defun org-element-section-interpreter (section contents
)
1278 "Interpret SECTION element as Org syntax.
1279 CONTENTS is the contents of the element."
1285 (defun org-element-special-block-parser (limit affiliated
)
1286 "Parse a special block.
1288 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1289 the buffer position at the beginning of the first affiliated
1290 keyword and CDR is a plist of affiliated keywords along with
1293 Return a list whose CAR is `special-block' and CDR is a plist
1294 containing `:type', `:begin', `:end', `:hiddenp',
1295 `:contents-begin', `:contents-end' and `:post-blank' keywords.
1297 Assume point is at the beginning of the block."
1298 (let* ((case-fold-search t
)
1299 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(S-+\\)")
1300 (upcase (match-string-no-properties 1)))))
1301 (if (not (save-excursion
1303 (format "^[ \t]*#\\+END_%s[ \t]*$" type
) limit t
)))
1304 ;; Incomplete block: parse it as a paragraph.
1305 (org-element-paragraph-parser limit affiliated
)
1306 (let ((block-end-line (match-beginning 0)))
1308 (let* ((begin (car affiliated
))
1309 ;; Empty blocks have no contents.
1310 (contents-begin (progn (forward-line)
1311 (and (< (point) block-end-line
)
1313 (contents-end (and contents-begin block-end-line
))
1314 (hidden (org-invisible-p2))
1315 (pos-before-blank (progn (goto-char block-end-line
)
1318 (end (progn (org-skip-whitespace)
1319 (if (eobp) (point) (point-at-bol)))))
1320 (list 'special-block
1326 :contents-begin contents-begin
1327 :contents-end contents-end
1328 :post-blank
(count-lines pos-before-blank end
))
1329 (cdr affiliated
)))))))))
1331 (defun org-element-special-block-interpreter (special-block contents
)
1332 "Interpret SPECIAL-BLOCK element as Org syntax.
1333 CONTENTS is the contents of the element."
1334 (let ((block-type (org-element-property :type special-block
)))
1335 (format "#+BEGIN_%s\n%s#+END_%s" block-type contents block-type
)))
1341 ;; For each element, a parser and an interpreter are also defined.
1342 ;; Both follow the same naming convention used for greater elements.
1344 ;; Also, as for greater elements, adding a new element type is done
1345 ;; through the following steps: implement a parser and an interpreter,
1346 ;; tweak `org-element--current-element' so that it recognizes the new
1347 ;; type and add that new type to `org-element-all-elements'.
1349 ;; As a special case, when the newly defined type is a block type,
1350 ;; `org-element-block-name-alist' has to be modified accordingly.
1355 (defun org-element-babel-call-parser (limit affiliated
)
1356 "Parse a babel call.
1358 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1359 the buffer position at the beginning of the first affiliated
1360 keyword and CDR is a plist of affiliated keywords along with
1363 Return a list whose CAR is `babel-call' and CDR is a plist
1364 containing `:begin', `:end', `:info' and `:post-blank' as
1367 (let ((case-fold-search t
)
1368 (info (progn (looking-at org-babel-block-lob-one-liner-regexp
)
1369 (org-babel-lob-get-info)))
1370 (begin (car affiliated
))
1371 (pos-before-blank (progn (forward-line) (point)))
1372 (end (progn (skip-chars-forward " \r\t\n" limit
)
1373 (if (eobp) (point) (point-at-bol)))))
1379 :post-blank
(count-lines pos-before-blank end
))
1380 (cdr affiliated
))))))
1382 (defun org-element-babel-call-interpreter (babel-call contents
)
1383 "Interpret BABEL-CALL element as Org syntax.
1385 (let* ((babel-info (org-element-property :info babel-call
))
1386 (main (car babel-info
))
1387 (post-options (nth 1 babel-info
)))
1389 (if (not (string-match "\\[\\(\\[.*?\\]\\)\\]" main
)) main
1390 ;; Remove redundant square brackets.
1391 (replace-match (match-string 1 main
) nil nil main
))
1392 (and post-options
(format "[%s]" post-options
)))))
1397 (defun org-element-clock-parser (limit)
1400 LIMIT bounds the search.
1402 Return a list whose CAR is `clock' and CDR is a plist containing
1403 `:status', `:value', `:time', `:begin', `:end' and `:post-blank'
1406 (let* ((case-fold-search nil
)
1408 (value (progn (search-forward org-clock-string
(line-end-position) t
)
1409 (org-skip-whitespace)
1410 (looking-at "\\[.*\\]")
1411 (org-match-string-no-properties 0)))
1412 (time (and (progn (goto-char (match-end 0))
1413 (looking-at " +=> +\\(\\S-+\\)[ \t]*$"))
1414 (org-match-string-no-properties 1)))
1415 (status (if time
'closed
'running
))
1416 (post-blank (let ((before-blank (progn (forward-line) (point))))
1417 (skip-chars-forward " \r\t\n" limit
)
1418 (unless (eobp) (beginning-of-line))
1419 (count-lines before-blank
(point))))
1422 (list :status status
1427 :post-blank post-blank
)))))
1429 (defun org-element-clock-interpreter (clock contents
)
1430 "Interpret CLOCK element as Org syntax.
1432 (concat org-clock-string
" "
1433 (org-element-property :value clock
)
1434 (let ((time (org-element-property :time clock
)))
1439 (org-split-string time
":")))))))
1444 (defun org-element-comment-parser (limit affiliated
)
1447 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1448 the buffer position at the beginning of the first affiliated
1449 keyword and CDR is a plist of affiliated keywords along with
1452 Return a list whose CAR is `comment' and CDR is a plist
1453 containing `:begin', `:end', `:value' and `:post-blank'
1456 Assume point is at comment beginning."
1458 (let* ((begin (car affiliated
))
1459 (value (prog2 (looking-at "[ \t]*# ?")
1460 (buffer-substring-no-properties
1461 (match-end 0) (line-end-position))
1464 ;; Get comments ending.
1466 (while (and (< (point) limit
) (looking-at "[ \t]*#\\( \\|$\\)"))
1467 ;; Accumulate lines without leading hash and first
1472 (buffer-substring-no-properties
1473 (match-end 0) (line-end-position))))
1476 (end (progn (goto-char com-end
)
1477 (skip-chars-forward " \r\t\n" limit
)
1478 (if (eobp) (point) (point-at-bol)))))
1484 :post-blank
(count-lines com-end end
))
1485 (cdr affiliated
))))))
1487 (defun org-element-comment-interpreter (comment contents
)
1488 "Interpret COMMENT element as Org syntax.
1490 (replace-regexp-in-string "^" "# " (org-element-property :value comment
)))
1495 (defun org-element-comment-block-parser (limit affiliated
)
1496 "Parse an export block.
1498 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1499 the buffer position at the beginning of the first affiliated
1500 keyword and CDR is a plist of affiliated keywords along with
1503 Return a list whose CAR is `comment-block' and CDR is a plist
1504 containing `:begin', `:end', `:hiddenp', `:value' and
1505 `:post-blank' keywords.
1507 Assume point is at comment block beginning."
1508 (let ((case-fold-search t
))
1509 (if (not (save-excursion
1510 (re-search-forward "^[ \t]*#\\+END_COMMENT[ \t]*$" limit t
)))
1511 ;; Incomplete block: parse it as a paragraph.
1512 (org-element-paragraph-parser limit affiliated
)
1513 (let ((contents-end (match-beginning 0)))
1515 (let* ((begin (car affiliated
))
1516 (contents-begin (progn (forward-line) (point)))
1517 (hidden (org-invisible-p2))
1518 (pos-before-blank (progn (goto-char contents-end
)
1521 (end (progn (skip-chars-forward " \r\t\n" limit
)
1522 (if (eobp) (point) (point-at-bol))))
1523 (value (buffer-substring-no-properties
1524 contents-begin contents-end
)))
1525 (list 'comment-block
1531 :post-blank
(count-lines pos-before-blank end
))
1532 (cdr affiliated
)))))))))
1534 (defun org-element-comment-block-interpreter (comment-block contents
)
1535 "Interpret COMMENT-BLOCK element as Org syntax.
1537 (format "#+BEGIN_COMMENT\n%s#+END_COMMENT"
1538 (org-remove-indentation (org-element-property :value comment-block
))))
1543 (defun org-element-example-block-parser (limit affiliated
)
1544 "Parse an example block.
1546 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1547 the buffer position at the beginning of the first affiliated
1548 keyword and CDR is a plist of affiliated keywords along with
1551 Return a list whose CAR is `example-block' and CDR is a plist
1552 containing `:begin', `:end', `:number-lines', `:preserve-indent',
1553 `:retain-labels', `:use-labels', `:label-fmt', `:hiddenp',
1554 `:switches', `:value' and `:post-blank' keywords."
1555 (let ((case-fold-search t
))
1556 (if (not (save-excursion
1557 (re-search-forward "^[ \t]*#\\+END_EXAMPLE[ \t]*$" limit t
)))
1558 ;; Incomplete block: parse it as a paragraph.
1559 (org-element-paragraph-parser limit affiliated
)
1560 (let ((contents-end (match-beginning 0)))
1563 (progn (looking-at "^[ \t]*#\\+BEGIN_EXAMPLE\\(?: +\\(.*\\)\\)?")
1564 (org-match-string-no-properties 1)))
1565 ;; Switches analysis
1566 (number-lines (cond ((not switches
) nil
)
1567 ((string-match "-n\\>" switches
) 'new
)
1568 ((string-match "+n\\>" switches
) 'continued
)))
1569 (preserve-indent (and switches
(string-match "-i\\>" switches
)))
1570 ;; Should labels be retained in (or stripped from) example
1574 (not (string-match "-r\\>" switches
))
1575 (and number-lines
(string-match "-k\\>" switches
))))
1576 ;; What should code-references use - labels or
1580 (and retain-labels
(not (string-match "-k\\>" switches
)))))
1581 (label-fmt (and switches
1582 (string-match "-l +\"\\([^\"\n]+\\)\"" switches
)
1583 (match-string 1 switches
)))
1584 ;; Standard block parsing.
1585 (begin (car affiliated
))
1586 (contents-begin (progn (forward-line) (point)))
1587 (hidden (org-invisible-p2))
1588 (value (buffer-substring-no-properties contents-begin contents-end
))
1589 (pos-before-blank (progn (goto-char contents-end
)
1592 (end (progn (skip-chars-forward " \r\t\n" limit
)
1593 (if (eobp) (point) (point-at-bol)))))
1594 (list 'example-block
1600 :number-lines number-lines
1601 :preserve-indent preserve-indent
1602 :retain-labels retain-labels
1603 :use-labels use-labels
1604 :label-fmt label-fmt
1606 :post-blank
(count-lines pos-before-blank end
))
1607 (cdr affiliated
)))))))))
1609 (defun org-element-example-block-interpreter (example-block contents
)
1610 "Interpret EXAMPLE-BLOCK element as Org syntax.
1612 (let ((switches (org-element-property :switches example-block
)))
1613 (concat "#+BEGIN_EXAMPLE" (and switches
(concat " " switches
)) "\n"
1614 (org-remove-indentation
1615 (org-element-property :value example-block
))
1621 (defun org-element-export-block-parser (limit affiliated
)
1622 "Parse an export block.
1624 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1625 the buffer position at the beginning of the first affiliated
1626 keyword and CDR is a plist of affiliated keywords along with
1629 Return a list whose CAR is `export-block' and CDR is a plist
1630 containing `:begin', `:end', `:type', `:hiddenp', `:value' and
1631 `:post-blank' keywords.
1633 Assume point is at export-block beginning."
1634 (let* ((case-fold-search t
)
1635 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
1636 (upcase (org-match-string-no-properties 1)))))
1637 (if (not (save-excursion
1639 (format "^[ \t]*#\\+END_%s[ \t]*$" type
) limit t
)))
1640 ;; Incomplete block: parse it as a paragraph.
1641 (org-element-paragraph-parser limit affiliated
)
1642 (let ((contents-end (match-beginning 0)))
1644 (let* ((begin (car affiliated
))
1645 (contents-begin (progn (forward-line) (point)))
1646 (hidden (org-invisible-p2))
1647 (pos-before-blank (progn (goto-char contents-end
)
1650 (end (progn (skip-chars-forward " \r\t\n" limit
)
1651 (if (eobp) (point) (point-at-bol))))
1652 (value (buffer-substring-no-properties contents-begin
1661 :post-blank
(count-lines pos-before-blank end
))
1662 (cdr affiliated
)))))))))
1664 (defun org-element-export-block-interpreter (export-block contents
)
1665 "Interpret EXPORT-BLOCK element as Org syntax.
1667 (let ((type (org-element-property :type export-block
)))
1668 (concat (format "#+BEGIN_%s\n" type
)
1669 (org-element-property :value export-block
)
1670 (format "#+END_%s" type
))))
1675 (defun org-element-fixed-width-parser (limit affiliated
)
1676 "Parse a fixed-width section.
1678 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1679 the buffer position at the beginning of the first affiliated
1680 keyword and CDR is a plist of affiliated keywords along with
1683 Return a list whose CAR is `fixed-width' and CDR is a plist
1684 containing `:begin', `:end', `:value' and `:post-blank' keywords.
1686 Assume point is at the beginning of the fixed-width area."
1688 (let* ((begin (car affiliated
))
1692 (while (and (< (point) limit
)
1693 (looking-at "[ \t]*:\\( \\|$\\)"))
1694 ;; Accumulate text without starting colons.
1697 (buffer-substring-no-properties
1698 (match-end 0) (point-at-eol))
1702 (end (progn (skip-chars-forward " \r\t\n" limit
)
1703 (if (eobp) (point) (point-at-bol)))))
1709 :post-blank
(count-lines end-area end
))
1710 (cdr affiliated
))))))
1712 (defun org-element-fixed-width-interpreter (fixed-width contents
)
1713 "Interpret FIXED-WIDTH element as Org syntax.
1715 (replace-regexp-in-string
1716 "^" ": " (substring (org-element-property :value fixed-width
) 0 -
1)))
1719 ;;;; Horizontal Rule
1721 (defun org-element-horizontal-rule-parser (limit affiliated
)
1722 "Parse an horizontal rule.
1724 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1725 the buffer position at the beginning of the first affiliated
1726 keyword and CDR is a plist of affiliated keywords along with
1729 Return a list whose CAR is `horizontal-rule' and CDR is a plist
1730 containing `:begin', `:end' and `:post-blank' keywords."
1732 (let ((begin (car affiliated
))
1733 (post-hr (progn (forward-line) (point)))
1734 (end (progn (skip-chars-forward " \r\t\n" limit
)
1735 (if (eobp) (point) (point-at-bol)))))
1736 (list 'horizontal-rule
1740 :post-blank
(count-lines post-hr end
))
1741 (cdr affiliated
))))))
1743 (defun org-element-horizontal-rule-interpreter (horizontal-rule contents
)
1744 "Interpret HORIZONTAL-RULE element as Org syntax.
1751 (defun org-element-keyword-parser (limit affiliated
)
1752 "Parse a keyword at point.
1754 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1755 the buffer position at the beginning of the first affiliated
1756 keyword and CDR is a plist of affiliated keywords along with
1759 Return a list whose CAR is `keyword' and CDR is a plist
1760 containing `:key', `:value', `:begin', `:end' and `:post-blank'
1763 (let ((case-fold-search t
)
1764 (begin (car affiliated
))
1765 (key (progn (looking-at "[ \t]*#\\+\\(\\S-+*\\):")
1766 (upcase (org-match-string-no-properties 1))))
1767 (value (org-trim (buffer-substring-no-properties
1768 (match-end 0) (point-at-eol))))
1769 (pos-before-blank (progn (forward-line) (point)))
1770 (end (progn (skip-chars-forward " \r\t\n" limit
)
1771 (if (eobp) (point) (point-at-bol)))))
1778 :post-blank
(count-lines pos-before-blank end
))
1779 (cdr affiliated
))))))
1781 (defun org-element-keyword-interpreter (keyword contents
)
1782 "Interpret KEYWORD element as Org syntax.
1785 (org-element-property :key keyword
)
1786 (org-element-property :value keyword
)))
1789 ;;;; Latex Environment
1791 (defun org-element-latex-environment-parser (limit affiliated
)
1792 "Parse a LaTeX environment.
1794 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1795 the buffer position at the beginning of the first affiliated
1796 keyword and CDR is a plist of affiliated keywords along with
1799 Return a list whose CAR is `latex-environment' and CDR is a plist
1800 containing `:begin', `:end', `:value' and `:post-blank'
1803 Assume point is at the beginning of the latex environment."
1805 (let* ((case-fold-search t
)
1806 (code-begin (point))
1807 (begin (car affiliated
))
1808 (env (progn (looking-at "^[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}")
1809 (regexp-quote (match-string 1))))
1811 (progn (re-search-forward
1812 (format "^[ \t]*\\\\end{%s}[ \t]*$" env
) limit t
)
1815 (value (buffer-substring-no-properties code-begin code-end
))
1816 (end (progn (skip-chars-forward " \r\t\n" limit
)
1817 (if (eobp) (point) (point-at-bol)))))
1818 (list 'latex-environment
1823 :post-blank
(count-lines code-end end
))
1824 (cdr affiliated
))))))
1826 (defun org-element-latex-environment-interpreter (latex-environment contents
)
1827 "Interpret LATEX-ENVIRONMENT element as Org syntax.
1829 (org-element-property :value latex-environment
))
1834 (defun org-element-node-property-parser (limit)
1835 "Parse a node-property at point.
1837 LIMIT bounds the search.
1839 Return a list whose CAR is `node-property' and CDR is a plist
1840 containing `:key', `:value', `:begin', `:end' and `:post-blank'
1843 (let ((case-fold-search t
)
1845 (key (progn (looking-at "[ \t]*:\\(.*?\\):[ \t]+\\(.*?\\)[ \t]*$")
1846 (org-match-string-no-properties 1)))
1847 (value (org-match-string-no-properties 2))
1848 (pos-before-blank (progn (forward-line) (point)))
1849 (end (progn (skip-chars-forward " \r\t\n" limit
)
1850 (if (eobp) (point) (point-at-bol)))))
1851 (list 'node-property
1856 :post-blank
(count-lines pos-before-blank end
))))))
1858 (defun org-element-node-property-interpreter (node-property contents
)
1859 "Interpret NODE-PROPERTY element as Org syntax.
1861 (format org-property-format
1862 (format ":%s:" (org-element-property :key node-property
))
1863 (org-element-property :value node-property
)))
1868 (defun org-element-paragraph-parser (limit affiliated
)
1871 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1872 the buffer position at the beginning of the first affiliated
1873 keyword and CDR is a plist of affiliated keywords along with
1876 Return a list whose CAR is `paragraph' and CDR is a plist
1877 containing `:begin', `:end', `:contents-begin' and
1878 `:contents-end' and `:post-blank' keywords.
1880 Assume point is at the beginning of the paragraph."
1882 (let* ((begin (car affiliated
))
1883 (contents-begin (point))
1885 (let ((case-fold-search t
))
1887 (if (not (re-search-forward
1888 org-element-paragraph-separate limit
'm
))
1890 ;; A matching `org-element-paragraph-separate' is not
1891 ;; necessarily the end of the paragraph. In
1892 ;; particular, lines starting with # or : as a first
1893 ;; non-space character are ambiguous. We have check
1894 ;; if they are valid Org syntax (i.e. not an
1895 ;; incomplete keyword).
1899 ;; There's no ambiguity for other symbols or
1900 ;; empty lines: stop here.
1901 (looking-at "[ \t]*\\(?:[^:#]\\|$\\)")
1902 ;; Stop at valid fixed-width areas.
1903 (looking-at "[ \t]*:\\(?: \\|$\\)")
1905 (and (looking-at org-drawer-regexp
)
1908 "^[ \t]*:END:[ \t]*$" limit t
)))
1909 ;; Stop at valid comments.
1910 (looking-at "[ \t]*#\\(?: \\|$\\)")
1911 ;; Stop at valid dynamic blocks.
1912 (and (looking-at org-dblock-start-re
)
1915 "^[ \t]*#\\+END:?[ \t]*$" limit t
)))
1916 ;; Stop at valid blocks.
1918 "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
1921 (format "^[ \t]*#\\+END_%s[ \t]*$"
1924 ;; Stop at valid latex environments.
1926 "^[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}[ \t]*$")
1929 (format "^[ \t]*\\\\end{%s}[ \t]*$"
1932 ;; Stop at valid keywords.
1933 (looking-at "[ \t]*#\\+\\S-+:")
1934 ;; Skip everything else.
1938 (re-search-forward org-element-paragraph-separate
1940 (beginning-of-line)))
1941 (if (= (point) limit
) limit
1942 (goto-char (line-beginning-position)))))
1943 (contents-end (progn (skip-chars-backward " \r\t\n" contents-begin
)
1946 (end (progn (skip-chars-forward " \r\t\n" limit
)
1947 (if (eobp) (point) (point-at-bol)))))
1952 :contents-begin contents-begin
1953 :contents-end contents-end
1954 :post-blank
(count-lines before-blank end
))
1955 (cdr affiliated
))))))
1957 (defun org-element-paragraph-interpreter (paragraph contents
)
1958 "Interpret PARAGRAPH element as Org syntax.
1959 CONTENTS is the contents of the element."
1965 (defun org-element-planning-parser (limit)
1968 LIMIT bounds the search.
1970 Return a list whose CAR is `planning' and CDR is a plist
1971 containing `:closed', `:deadline', `:scheduled', `:begin', `:end'
1972 and `:post-blank' keywords."
1974 (let* ((case-fold-search nil
)
1976 (post-blank (let ((before-blank (progn (forward-line) (point))))
1977 (skip-chars-forward " \r\t\n" limit
)
1978 (unless (eobp) (beginning-of-line))
1979 (count-lines before-blank
(point))))
1981 closed deadline scheduled
)
1983 (while (re-search-forward org-keyword-time-not-clock-regexp
1984 (line-end-position) t
)
1985 (goto-char (match-end 1))
1986 (org-skip-whitespace)
1987 (let ((time (buffer-substring-no-properties
1988 (1+ (point)) (1- (match-end 0))))
1989 (keyword (match-string 1)))
1990 (cond ((equal keyword org-closed-string
) (setq closed time
))
1991 ((equal keyword org-deadline-string
) (setq deadline time
))
1992 (t (setq scheduled time
)))))
1994 (list :closed closed
1996 :scheduled scheduled
1999 :post-blank post-blank
)))))
2001 (defun org-element-planning-interpreter (planning contents
)
2002 "Interpret PLANNING element as Org syntax.
2007 (list (let ((closed (org-element-property :closed planning
)))
2008 (when closed
(concat org-closed-string
" [" closed
"]")))
2009 (let ((deadline (org-element-property :deadline planning
)))
2010 (when deadline
(concat org-deadline-string
" <" deadline
">")))
2011 (let ((scheduled (org-element-property :scheduled planning
)))
2013 (concat org-scheduled-string
" <" scheduled
">")))))
2019 (defun org-element-quote-section-parser (limit)
2020 "Parse a quote section.
2022 LIMIT bounds the search.
2024 Return a list whose CAR is `quote-section' and CDR is a plist
2025 containing `:begin', `:end', `:value' and `:post-blank' keywords.
2027 Assume point is at beginning of the section."
2029 (let* ((begin (point))
2030 (end (progn (org-with-limited-levels (outline-next-heading))
2032 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
2035 (value (buffer-substring-no-properties begin pos-before-blank
)))
2036 (list 'quote-section
2040 :post-blank
(count-lines pos-before-blank end
))))))
2042 (defun org-element-quote-section-interpreter (quote-section contents
)
2043 "Interpret QUOTE-SECTION element as Org syntax.
2045 (org-element-property :value quote-section
))
2050 (defun org-element-src-block-parser (limit affiliated
)
2053 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2054 the buffer position at the beginning of the first affiliated
2055 keyword and CDR is a plist of affiliated keywords along with
2058 Return a list whose CAR is `src-block' and CDR is a plist
2059 containing `:language', `:switches', `:parameters', `:begin',
2060 `:end', `:hiddenp', `:number-lines', `:retain-labels',
2061 `:use-labels', `:label-fmt', `:preserve-indent', `:value' and
2062 `:post-blank' keywords.
2064 Assume point is at the beginning of the block."
2065 (let ((case-fold-search t
))
2066 (if (not (save-excursion (re-search-forward "^[ \t]*#\\+END_SRC[ \t]*$"
2068 ;; Incomplete block: parse it as a paragraph.
2069 (org-element-paragraph-parser limit affiliated
)
2070 (let ((contents-end (match-beginning 0)))
2072 (let* ((begin (car affiliated
))
2073 ;; Get language as a string.
2077 (concat "^[ \t]*#\\+BEGIN_SRC"
2078 "\\(?: +\\(\\S-+\\)\\)?"
2079 "\\(\\(?: +\\(?:-l \".*?\"\\|[-+][A-Za-z]\\)\\)+\\)?"
2081 (org-match-string-no-properties 1)))
2083 (switches (org-match-string-no-properties 2))
2085 (parameters (org-match-string-no-properties 3))
2086 ;; Switches analysis
2087 (number-lines (cond ((not switches
) nil
)
2088 ((string-match "-n\\>" switches
) 'new
)
2089 ((string-match "+n\\>" switches
) 'continued
)))
2090 (preserve-indent (and switches
(string-match "-i\\>" switches
)))
2091 (label-fmt (and switches
2092 (string-match "-l +\"\\([^\"\n]+\\)\"" switches
)
2093 (match-string 1 switches
)))
2094 ;; Should labels be retained in (or stripped from)
2098 (not (string-match "-r\\>" switches
))
2099 (and number-lines
(string-match "-k\\>" switches
))))
2100 ;; What should code-references use - labels or
2104 (and retain-labels
(not (string-match "-k\\>" switches
)))))
2105 ;; Get visibility status.
2106 (hidden (progn (forward-line) (org-invisible-p2)))
2108 (value (buffer-substring-no-properties (point) contents-end
))
2109 (pos-before-blank (progn (goto-char contents-end
)
2112 ;; Get position after ending blank lines.
2113 (end (progn (skip-chars-forward " \r\t\n" limit
)
2114 (if (eobp) (point) (point-at-bol)))))
2117 (list :language language
2118 :switches
(and (org-string-nw-p switches
)
2119 (org-trim switches
))
2120 :parameters
(and (org-string-nw-p parameters
)
2121 (org-trim parameters
))
2124 :number-lines number-lines
2125 :preserve-indent preserve-indent
2126 :retain-labels retain-labels
2127 :use-labels use-labels
2128 :label-fmt label-fmt
2131 :post-blank
(count-lines pos-before-blank end
))
2132 (cdr affiliated
)))))))))
2134 (defun org-element-src-block-interpreter (src-block contents
)
2135 "Interpret SRC-BLOCK element as Org syntax.
2137 (let ((lang (org-element-property :language src-block
))
2138 (switches (org-element-property :switches src-block
))
2139 (params (org-element-property :parameters src-block
))
2140 (value (let ((val (org-element-property :value src-block
)))
2143 (org-src-preserve-indentation val
)
2144 ((zerop org-edit-src-content-indentation
)
2145 (org-remove-indentation val
))
2147 (let ((ind (make-string
2148 org-edit-src-content-indentation
32)))
2149 (replace-regexp-in-string
2150 "\\(^\\)[ \t]*\\S-" ind
2151 (org-remove-indentation val
) nil nil
1)))))))
2152 (concat (format "#+BEGIN_SRC%s\n"
2153 (concat (and lang
(concat " " lang
))
2154 (and switches
(concat " " switches
))
2155 (and params
(concat " " params
))))
2162 (defun org-element-table-parser (limit affiliated
)
2163 "Parse a table at point.
2165 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2166 the buffer position at the beginning of the first affiliated
2167 keyword and CDR is a plist of affiliated keywords along with
2170 Return a list whose CAR is `table' and CDR is a plist containing
2171 `:begin', `:end', `:tblfm', `:type', `:contents-begin',
2172 `:contents-end', `:value' and `:post-blank' keywords.
2174 Assume point is at the beginning of the table."
2176 (let* ((case-fold-search t
)
2177 (table-begin (point))
2178 (type (if (org-at-table.el-p
) 'table.el
'org
))
2179 (keywords (org-element--collect-affiliated-keywords))
2180 (begin (car affiliated
))
2181 (table-end (goto-char (marker-position (org-table-end t
))))
2183 (while (looking-at "[ \t]*#\\+TBLFM: +\\(.*\\)[ \t]*$")
2184 (push (org-match-string-no-properties 1) acc
)
2187 (pos-before-blank (point))
2188 (end (progn (skip-chars-forward " \r\t\n" limit
)
2189 (if (eobp) (point) (point-at-bol)))))
2196 ;; Only `org' tables have contents. `table.el' tables
2197 ;; use a `:value' property to store raw table as
2199 :contents-begin
(and (eq type
'org
) table-begin
)
2200 :contents-end
(and (eq type
'org
) table-end
)
2201 :value
(and (eq type
'table.el
)
2202 (buffer-substring-no-properties
2203 table-begin table-end
))
2204 :post-blank
(count-lines pos-before-blank end
))
2205 (cdr affiliated
))))))
2207 (defun org-element-table-interpreter (table contents
)
2208 "Interpret TABLE element as Org syntax.
2210 (if (eq (org-element-property :type table
) 'table.el
)
2211 (org-remove-indentation (org-element-property :value table
))
2212 (concat (with-temp-buffer (insert contents
)
2215 (mapconcat (lambda (fm) (concat "#+TBLFM: " fm
))
2216 (reverse (org-element-property :tblfm table
))
2222 (defun org-element-table-row-parser (limit)
2223 "Parse table row at point.
2225 LIMIT bounds the search.
2227 Return a list whose CAR is `table-row' and CDR is a plist
2228 containing `:begin', `:end', `:contents-begin', `:contents-end',
2229 `:type' and `:post-blank' keywords."
2231 (let* ((type (if (looking-at "^[ \t]*|-") 'rule
'standard
))
2233 ;; A table rule has no contents. In that case, ensure
2234 ;; CONTENTS-BEGIN matches CONTENTS-END.
2235 (contents-begin (and (eq type
'standard
)
2236 (search-forward "|")
2238 (contents-end (and (eq type
'standard
)
2241 (skip-chars-backward " \t")
2243 (end (progn (forward-line) (point))))
2248 :contents-begin contents-begin
2249 :contents-end contents-end
2252 (defun org-element-table-row-interpreter (table-row contents
)
2253 "Interpret TABLE-ROW element as Org syntax.
2254 CONTENTS is the contents of the table row."
2255 (if (eq (org-element-property :type table-row
) 'rule
) "|-"
2256 (concat "| " contents
)))
2261 (defun org-element-verse-block-parser (limit affiliated
)
2262 "Parse a verse block.
2264 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2265 the buffer position at the beginning of the first affiliated
2266 keyword and CDR is a plist of affiliated keywords along with
2269 Return a list whose CAR is `verse-block' and CDR is a plist
2270 containing `:begin', `:end', `:contents-begin', `:contents-end',
2271 `:hiddenp' and `:post-blank' keywords.
2273 Assume point is at beginning of the block."
2274 (let ((case-fold-search t
))
2275 (if (not (save-excursion
2276 (re-search-forward "^[ \t]*#\\+END_VERSE[ \t]*$" limit t
)))
2277 ;; Incomplete block: parse it as a paragraph.
2278 (org-element-paragraph-parser limit affiliated
)
2279 (let ((contents-end (match-beginning 0)))
2281 (let* ((begin (car affiliated
))
2282 (hidden (progn (forward-line) (org-invisible-p2)))
2283 (contents-begin (point))
2284 (pos-before-blank (progn (goto-char contents-end
)
2287 (end (progn (skip-chars-forward " \r\t\n" limit
)
2288 (if (eobp) (point) (point-at-bol)))))
2293 :contents-begin contents-begin
2294 :contents-end contents-end
2296 :post-blank
(count-lines pos-before-blank end
))
2297 (cdr affiliated
)))))))))
2299 (defun org-element-verse-block-interpreter (verse-block contents
)
2300 "Interpret VERSE-BLOCK element as Org syntax.
2301 CONTENTS is verse block contents."
2302 (format "#+BEGIN_VERSE\n%s#+END_VERSE" contents
))
2308 ;; Unlike to elements, interstices can be found between objects.
2309 ;; That's why, along with the parser, successor functions are provided
2310 ;; for each object. Some objects share the same successor (i.e. `code'
2311 ;; and `verbatim' objects).
2313 ;; A successor must accept a single argument bounding the search. It
2314 ;; will return either a cons cell whose CAR is the object's type, as
2315 ;; a symbol, and CDR the position of its next occurrence, or nil.
2317 ;; Successors follow the naming convention:
2318 ;; org-element-NAME-successor, where NAME is the name of the
2319 ;; successor, as defined in `org-element-all-successors'.
2321 ;; Some object types (i.e. `italic') are recursive. Restrictions on
2322 ;; object types they can contain will be specified in
2323 ;; `org-element-object-restrictions'.
2325 ;; Adding a new type of object is simple. Implement a successor,
2326 ;; a parser, and an interpreter for it, all following the naming
2327 ;; convention. Register type in `org-element-all-objects' and
2328 ;; successor in `org-element-all-successors'. Maybe tweak
2329 ;; restrictions about it, and that's it.
2334 (defun org-element-bold-parser ()
2335 "Parse bold object at point.
2337 Return a list whose CAR is `bold' and CDR is a plist with
2338 `:begin', `:end', `:contents-begin' and `:contents-end' and
2339 `:post-blank' keywords.
2341 Assume point is at the first star marker."
2343 (unless (bolp) (backward-char 1))
2344 (looking-at org-emph-re
)
2345 (let ((begin (match-beginning 2))
2346 (contents-begin (match-beginning 4))
2347 (contents-end (match-end 4))
2348 (post-blank (progn (goto-char (match-end 2))
2349 (skip-chars-forward " \t")))
2354 :contents-begin contents-begin
2355 :contents-end contents-end
2356 :post-blank post-blank
)))))
2358 (defun org-element-bold-interpreter (bold contents
)
2359 "Interpret BOLD object as Org syntax.
2360 CONTENTS is the contents of the object."
2361 (format "*%s*" contents
))
2363 (defun org-element-text-markup-successor (limit)
2364 "Search for the next text-markup object.
2366 LIMIT bounds the search.
2368 Return value is a cons cell whose CAR is a symbol among `bold',
2369 `italic', `underline', `strike-through', `code' and `verbatim'
2370 and CDR is beginning position."
2372 (unless (bolp) (backward-char))
2373 (when (re-search-forward org-emph-re limit t
)
2374 (let ((marker (match-string 3)))
2376 ((equal marker
"*") 'bold
)
2377 ((equal marker
"/") 'italic
)
2378 ((equal marker
"_") 'underline
)
2379 ((equal marker
"+") 'strike-through
)
2380 ((equal marker
"~") 'code
)
2381 ((equal marker
"=") 'verbatim
)
2382 (t (error "Unknown marker at %d" (match-beginning 3))))
2383 (match-beginning 2))))))
2388 (defun org-element-code-parser ()
2389 "Parse code object at point.
2391 Return a list whose CAR is `code' and CDR is a plist with
2392 `:value', `:begin', `:end' and `:post-blank' keywords.
2394 Assume point is at the first tilde marker."
2396 (unless (bolp) (backward-char 1))
2397 (looking-at org-emph-re
)
2398 (let ((begin (match-beginning 2))
2399 (value (org-match-string-no-properties 4))
2400 (post-blank (progn (goto-char (match-end 2))
2401 (skip-chars-forward " \t")))
2407 :post-blank post-blank
)))))
2409 (defun org-element-code-interpreter (code contents
)
2410 "Interpret CODE object as Org syntax.
2412 (format "~%s~" (org-element-property :value code
)))
2417 (defun org-element-entity-parser ()
2418 "Parse entity at point.
2420 Return a list whose CAR is `entity' and CDR a plist with
2421 `:begin', `:end', `:latex', `:latex-math-p', `:html', `:latin1',
2422 `:utf-8', `:ascii', `:use-brackets-p' and `:post-blank' as
2425 Assume point is at the beginning of the entity."
2427 (looking-at "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)")
2428 (let* ((value (org-entity-get (match-string 1)))
2429 (begin (match-beginning 0))
2430 (bracketsp (string= (match-string 2) "{}"))
2431 (post-blank (progn (goto-char (match-end 1))
2432 (when bracketsp
(forward-char 2))
2433 (skip-chars-forward " \t")))
2436 (list :name
(car value
)
2437 :latex
(nth 1 value
)
2438 :latex-math-p
(nth 2 value
)
2440 :ascii
(nth 4 value
)
2441 :latin1
(nth 5 value
)
2442 :utf-8
(nth 6 value
)
2445 :use-brackets-p bracketsp
2446 :post-blank post-blank
)))))
2448 (defun org-element-entity-interpreter (entity contents
)
2449 "Interpret ENTITY object as Org syntax.
2452 (org-element-property :name entity
)
2453 (when (org-element-property :use-brackets-p entity
) "{}")))
2455 (defun org-element-latex-or-entity-successor (limit)
2456 "Search for the next latex-fragment or entity object.
2458 LIMIT bounds the search.
2460 Return value is a cons cell whose CAR is `entity' or
2461 `latex-fragment' and CDR is beginning position."
2464 (remove "begin" (plist-get org-format-latex-options
:matchers
)))
2465 ;; ENTITY-RE matches both LaTeX commands and Org entities.
2467 "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))
2468 (when (re-search-forward
2469 (concat (mapconcat (lambda (e) (nth 1 (assoc e org-latex-regexps
)))
2473 (goto-char (match-beginning 0))
2474 (if (looking-at entity-re
)
2475 ;; Determine if it's a real entity or a LaTeX command.
2476 (cons (if (org-entity-get (match-string 1)) 'entity
'latex-fragment
)
2477 (match-beginning 0))
2478 ;; No entity nor command: point is at a LaTeX fragment.
2479 ;; Determine its type to get the correct beginning position.
2480 (cons 'latex-fragment
2483 (when (looking-at (nth 1 (assoc e org-latex-regexps
)))
2486 (nth 2 (assoc e org-latex-regexps
))))))
2493 (defun org-element-export-snippet-parser ()
2494 "Parse export snippet at point.
2496 Return a list whose CAR is `export-snippet' and CDR a plist with
2497 `:begin', `:end', `:back-end', `:value' and `:post-blank' as
2500 Assume point is at the beginning of the snippet."
2502 (re-search-forward "@@\\([-A-Za-z0-9]+\\):" nil t
)
2503 (let* ((begin (match-beginning 0))
2504 (back-end (org-match-string-no-properties 1))
2505 (value (buffer-substring-no-properties
2507 (progn (re-search-forward "@@" nil t
) (match-beginning 0))))
2508 (post-blank (skip-chars-forward " \t"))
2510 (list 'export-snippet
2511 (list :back-end back-end
2515 :post-blank post-blank
)))))
2517 (defun org-element-export-snippet-interpreter (export-snippet contents
)
2518 "Interpret EXPORT-SNIPPET object as Org syntax.
2521 (org-element-property :back-end export-snippet
)
2522 (org-element-property :value export-snippet
)))
2524 (defun org-element-export-snippet-successor (limit)
2525 "Search for the next export-snippet object.
2527 LIMIT bounds the search.
2529 Return value is a cons cell whose CAR is `export-snippet' and CDR
2530 its beginning position."
2533 (when (and (re-search-forward "@@[-A-Za-z0-9]+:" limit t
)
2534 (setq beg
(match-beginning 0))
2535 (search-forward "@@" limit t
))
2536 (cons 'export-snippet beg
)))))
2539 ;;;; Footnote Reference
2541 (defun org-element-footnote-reference-parser ()
2542 "Parse footnote reference at point.
2544 Return a list whose CAR is `footnote-reference' and CDR a plist
2545 with `:label', `:type', `:inline-definition', `:begin', `:end'
2546 and `:post-blank' as keywords."
2548 (looking-at org-footnote-re
)
2549 (let* ((begin (point))
2550 (label (or (org-match-string-no-properties 2)
2551 (org-match-string-no-properties 3)
2552 (and (match-string 1)
2553 (concat "fn:" (org-match-string-no-properties 1)))))
2554 (type (if (or (not label
) (match-string 1)) 'inline
'standard
))
2555 (inner-begin (match-end 0))
2559 (while (and (> count
0) (re-search-forward "[][]" nil t
))
2560 (if (equal (match-string 0) "[") (incf count
) (decf count
)))
2562 (post-blank (progn (goto-char (1+ inner-end
))
2563 (skip-chars-forward " \t")))
2566 (list 'footnote-reference
2571 :post-blank post-blank
))))
2572 (org-element-put-property
2573 footnote-reference
:inline-definition
2574 (and (eq type
'inline
)
2575 (org-element-parse-secondary-string
2576 (buffer-substring inner-begin inner-end
)
2577 (org-element-restriction 'footnote-reference
)
2578 footnote-reference
))))))
2580 (defun org-element-footnote-reference-interpreter (footnote-reference contents
)
2581 "Interpret FOOTNOTE-REFERENCE object as Org syntax.
2583 (let ((label (or (org-element-property :label footnote-reference
) "fn:"))
2586 (org-element-property :inline-definition footnote-reference
)))
2587 (if (not inline-def
) ""
2588 (concat ":" (org-element-interpret-data inline-def
))))))
2589 (format "[%s]" (concat label def
))))
2591 (defun org-element-footnote-reference-successor (limit)
2592 "Search for the next footnote-reference object.
2594 LIMIT bounds the search.
2596 Return value is a cons cell whose CAR is `footnote-reference' and
2597 CDR is beginning position."
2600 (while (re-search-forward org-footnote-re limit t
)
2602 (let ((beg (match-beginning 0))
2605 (while (re-search-forward "[][]" limit t
)
2606 (if (equal (match-string 0) "[") (incf count
) (decf count
))
2608 (throw 'exit
(cons 'footnote-reference beg
))))))))))
2611 ;;;; Inline Babel Call
2613 (defun org-element-inline-babel-call-parser ()
2614 "Parse inline babel call at point.
2616 Return a list whose CAR is `inline-babel-call' and CDR a plist
2617 with `:begin', `:end', `:info' and `:post-blank' as keywords.
2619 Assume point is at the beginning of the babel call."
2621 (unless (bolp) (backward-char))
2622 (looking-at org-babel-inline-lob-one-liner-regexp
)
2623 (let ((info (save-match-data (org-babel-lob-get-info)))
2624 (begin (match-end 1))
2625 (post-blank (progn (goto-char (match-end 0))
2626 (skip-chars-forward " \t")))
2628 (list 'inline-babel-call
2632 :post-blank post-blank
)))))
2634 (defun org-element-inline-babel-call-interpreter (inline-babel-call contents
)
2635 "Interpret INLINE-BABEL-CALL object as Org syntax.
2637 (let* ((babel-info (org-element-property :info inline-babel-call
))
2638 (main-source (car babel-info
))
2639 (post-options (nth 1 babel-info
)))
2641 (if (string-match "\\[\\(\\[.*?\\]\\)\\]" main-source
)
2642 ;; Remove redundant square brackets.
2644 (match-string 1 main-source
) nil nil main-source
)
2646 (and post-options
(format "[%s]" post-options
)))))
2648 (defun org-element-inline-babel-call-successor (limit)
2649 "Search for the next inline-babel-call object.
2651 LIMIT bounds the search.
2653 Return value is a cons cell whose CAR is `inline-babel-call' and
2654 CDR is beginning position."
2656 ;; Use a simplified version of
2657 ;; `org-babel-inline-lob-one-liner-regexp'.
2658 (when (re-search-forward
2659 "call_\\([^()\n]+?\\)\\(?:\\[.*?\\]\\)?([^\n]*?)\\(\\[.*?\\]\\)?"
2661 (cons 'inline-babel-call
(match-beginning 0)))))
2664 ;;;; Inline Src Block
2666 (defun org-element-inline-src-block-parser ()
2667 "Parse inline source block at point.
2669 LIMIT bounds the search.
2671 Return a list whose CAR is `inline-src-block' and CDR a plist
2672 with `:begin', `:end', `:language', `:value', `:parameters' and
2673 `:post-blank' as keywords.
2675 Assume point is at the beginning of the inline src block."
2677 (unless (bolp) (backward-char))
2678 (looking-at org-babel-inline-src-block-regexp
)
2679 (let ((begin (match-beginning 1))
2680 (language (org-match-string-no-properties 2))
2681 (parameters (org-match-string-no-properties 4))
2682 (value (org-match-string-no-properties 5))
2683 (post-blank (progn (goto-char (match-end 0))
2684 (skip-chars-forward " \t")))
2686 (list 'inline-src-block
2687 (list :language language
2689 :parameters parameters
2692 :post-blank post-blank
)))))
2694 (defun org-element-inline-src-block-interpreter (inline-src-block contents
)
2695 "Interpret INLINE-SRC-BLOCK object as Org syntax.
2697 (let ((language (org-element-property :language inline-src-block
))
2698 (arguments (org-element-property :parameters inline-src-block
))
2699 (body (org-element-property :value inline-src-block
)))
2700 (format "src_%s%s{%s}"
2702 (if arguments
(format "[%s]" arguments
) "")
2705 (defun org-element-inline-src-block-successor (limit)
2706 "Search for the next inline-babel-call element.
2708 LIMIT bounds the search.
2710 Return value is a cons cell whose CAR is `inline-babel-call' and
2711 CDR is beginning position."
2713 (unless (bolp) (backward-char))
2714 (when (re-search-forward org-babel-inline-src-block-regexp limit t
)
2715 (cons 'inline-src-block
(match-beginning 1)))))
2719 (defun org-element-italic-parser ()
2720 "Parse italic object at point.
2722 Return a list whose CAR is `italic' and CDR is a plist with
2723 `:begin', `:end', `:contents-begin' and `:contents-end' and
2724 `:post-blank' keywords.
2726 Assume point is at the first slash marker."
2728 (unless (bolp) (backward-char 1))
2729 (looking-at org-emph-re
)
2730 (let ((begin (match-beginning 2))
2731 (contents-begin (match-beginning 4))
2732 (contents-end (match-end 4))
2733 (post-blank (progn (goto-char (match-end 2))
2734 (skip-chars-forward " \t")))
2739 :contents-begin contents-begin
2740 :contents-end contents-end
2741 :post-blank post-blank
)))))
2743 (defun org-element-italic-interpreter (italic contents
)
2744 "Interpret ITALIC object as Org syntax.
2745 CONTENTS is the contents of the object."
2746 (format "/%s/" contents
))
2751 (defun org-element-latex-fragment-parser ()
2752 "Parse latex fragment at point.
2754 Return a list whose CAR is `latex-fragment' and CDR a plist with
2755 `:value', `:begin', `:end', and `:post-blank' as keywords.
2757 Assume point is at the beginning of the latex fragment."
2759 (let* ((begin (point))
2763 (let ((latex-regexp (nth 1 (assoc e org-latex-regexps
))))
2764 (when (or (looking-at latex-regexp
)
2768 (looking-at latex-regexp
))))
2769 (throw 'exit
(nth 2 (assoc e org-latex-regexps
))))))
2770 (plist-get org-format-latex-options
:matchers
))
2771 ;; None found: it's a macro.
2772 (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")
2774 (value (match-string-no-properties substring-match
))
2775 (post-blank (progn (goto-char (match-end substring-match
))
2776 (skip-chars-forward " \t")))
2778 (list 'latex-fragment
2782 :post-blank post-blank
)))))
2784 (defun org-element-latex-fragment-interpreter (latex-fragment contents
)
2785 "Interpret LATEX-FRAGMENT object as Org syntax.
2787 (org-element-property :value latex-fragment
))
2791 (defun org-element-line-break-parser ()
2792 "Parse line break at point.
2794 Return a list whose CAR is `line-break', and CDR a plist with
2795 `:begin', `:end' and `:post-blank' keywords.
2797 Assume point is at the beginning of the line break."
2798 (list 'line-break
(list :begin
(point) :end
(point-at-eol) :post-blank
0)))
2800 (defun org-element-line-break-interpreter (line-break contents
)
2801 "Interpret LINE-BREAK object as Org syntax.
2805 (defun org-element-line-break-successor (limit)
2806 "Search for the next line-break object.
2808 LIMIT bounds the search.
2810 Return value is a cons cell whose CAR is `line-break' and CDR is
2811 beginning position."
2813 (let ((beg (and (re-search-forward "[^\\\\]\\(\\\\\\\\\\)[ \t]*$" limit t
)
2814 (goto-char (match-beginning 1)))))
2815 ;; A line break can only happen on a non-empty line.
2816 (when (and beg
(re-search-backward "\\S-" (point-at-bol) t
))
2817 (cons 'line-break beg
)))))
2822 (defun org-element-link-parser ()
2823 "Parse link at point.
2825 Return a list whose CAR is `link' and CDR a plist with `:type',
2826 `:path', `:raw-link', `:application', `:search-option', `:begin',
2827 `:end', `:contents-begin', `:contents-end' and `:post-blank' as
2830 Assume point is at the beginning of the link."
2832 (let ((begin (point))
2833 end contents-begin contents-end link-end post-blank path type
2834 raw-link link search-option application
)
2836 ;; Type 1: Text targeted from a radio target.
2837 ((and org-target-link-regexp
(looking-at org-target-link-regexp
))
2839 link-end
(match-end 0)
2840 path
(org-match-string-no-properties 0)))
2841 ;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]]
2842 ((looking-at org-bracket-link-regexp
)
2843 (setq contents-begin
(match-beginning 3)
2844 contents-end
(match-end 3)
2845 link-end
(match-end 0)
2846 ;; RAW-LINK is the original link.
2847 raw-link
(org-match-string-no-properties 1)
2848 link
(org-translate-link
2849 (org-link-expand-abbrev
2850 (org-link-unescape raw-link
))))
2851 ;; Determine TYPE of link and set PATH accordingly.
2854 ((or (file-name-absolute-p link
) (string-match "^\\.\\.?/" link
))
2855 (setq type
"file" path link
))
2856 ;; Explicit type (http, irc, bbdb...). See `org-link-types'.
2857 ((string-match org-link-re-with-space3 link
)
2858 (setq type
(match-string 1 link
) path
(match-string 2 link
)))
2859 ;; Id type: PATH is the id.
2860 ((string-match "^id:\\([-a-f0-9]+\\)" link
)
2861 (setq type
"id" path
(match-string 1 link
)))
2862 ;; Code-ref type: PATH is the name of the reference.
2863 ((string-match "^(\\(.*\\))$" link
)
2864 (setq type
"coderef" path
(match-string 1 link
)))
2865 ;; Custom-id type: PATH is the name of the custom id.
2866 ((= (aref link
0) ?
#)
2867 (setq type
"custom-id" path
(substring link
1)))
2868 ;; Fuzzy type: Internal link either matches a target, an
2869 ;; headline name or nothing. PATH is the target or
2871 (t (setq type
"fuzzy" path link
))))
2872 ;; Type 3: Plain link, i.e. http://orgmode.org
2873 ((looking-at org-plain-link-re
)
2874 (setq raw-link
(org-match-string-no-properties 0)
2875 type
(org-match-string-no-properties 1)
2876 path
(org-match-string-no-properties 2)
2877 link-end
(match-end 0)))
2878 ;; Type 4: Angular link, i.e. <http://orgmode.org>
2879 ((looking-at org-angle-link-re
)
2880 (setq raw-link
(buffer-substring-no-properties
2881 (match-beginning 1) (match-end 2))
2882 type
(org-match-string-no-properties 1)
2883 path
(org-match-string-no-properties 2)
2884 link-end
(match-end 0))))
2885 ;; In any case, deduce end point after trailing white space from
2886 ;; LINK-END variable.
2887 (setq post-blank
(progn (goto-char link-end
) (skip-chars-forward " \t"))
2889 ;; Extract search option and opening application out of
2890 ;; "file"-type links.
2891 (when (member type org-element-link-type-is-file
)
2893 (cond ((string-match "^file\\+\\(.*\\)$" type
)
2894 (setq application
(match-string 1 type
)))
2895 ((not (string-match "^file" type
))
2896 (setq application type
)))
2897 ;; Extract search option from PATH.
2898 (when (string-match "::\\(.*\\)$" path
)
2899 (setq search-option
(match-string 1 path
)
2900 path
(replace-match "" nil nil path
)))
2901 ;; Make sure TYPE always report "file".
2906 :raw-link
(or raw-link path
)
2907 :application application
2908 :search-option search-option
2911 :contents-begin contents-begin
2912 :contents-end contents-end
2913 :post-blank post-blank
)))))
2915 (defun org-element-link-interpreter (link contents
)
2916 "Interpret LINK object as Org syntax.
2917 CONTENTS is the contents of the object, or nil."
2918 (let ((type (org-element-property :type link
))
2919 (raw-link (org-element-property :raw-link link
)))
2920 (if (string= type
"radio") raw-link
2923 (if contents
(format "[%s]" contents
) "")))))
2925 (defun org-element-link-successor (limit)
2926 "Search for the next link object.
2928 LIMIT bounds the search.
2930 Return value is a cons cell whose CAR is `link' and CDR is
2931 beginning position."
2934 (if (not org-target-link-regexp
) org-any-link-re
2935 (concat org-any-link-re
"\\|" org-target-link-regexp
))))
2936 (when (re-search-forward link-regexp limit t
)
2937 (cons 'link
(match-beginning 0))))))
2942 (defun org-element-macro-parser ()
2943 "Parse macro at point.
2945 Return a list whose CAR is `macro' and CDR a plist with `:key',
2946 `:args', `:begin', `:end', `:value' and `:post-blank' as
2949 Assume point is at the macro."
2951 (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
2952 (let ((begin (point))
2953 (key (downcase (org-match-string-no-properties 1)))
2954 (value (org-match-string-no-properties 0))
2955 (post-blank (progn (goto-char (match-end 0))
2956 (skip-chars-forward " \t")))
2958 (args (let ((args (org-match-string-no-properties 3)) args2
)
2960 (setq args
(org-split-string args
","))
2962 (while (string-match "\\\\\\'" (car args
))
2963 ;; Repair bad splits.
2964 (setcar (cdr args
) (concat (substring (car args
) 0 -
1)
2967 (push (pop args
) args2
))
2968 (mapcar 'org-trim
(nreverse args2
))))))
2975 :post-blank post-blank
)))))
2977 (defun org-element-macro-interpreter (macro contents
)
2978 "Interpret MACRO object as Org syntax.
2980 (org-element-property :value macro
))
2982 (defun org-element-macro-successor (limit)
2983 "Search for the next macro object.
2985 LIMIT bounds the search.
2987 Return value is cons cell whose CAR is `macro' and CDR is
2988 beginning position."
2990 (when (re-search-forward
2991 "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
2993 (cons 'macro
(match-beginning 0)))))
2998 (defun org-element-radio-target-parser ()
2999 "Parse radio target at point.
3001 Return a list whose CAR is `radio-target' and CDR a plist with
3002 `:begin', `:end', `:contents-begin', `:contents-end', `:value'
3003 and `:post-blank' as keywords.
3005 Assume point is at the radio target."
3007 (looking-at org-radio-target-regexp
)
3008 (let ((begin (point))
3009 (contents-begin (match-beginning 1))
3010 (contents-end (match-end 1))
3011 (value (org-match-string-no-properties 1))
3012 (post-blank (progn (goto-char (match-end 0))
3013 (skip-chars-forward " \t")))
3018 :contents-begin contents-begin
3019 :contents-end contents-end
3020 :post-blank post-blank
3023 (defun org-element-radio-target-interpreter (target contents
)
3024 "Interpret TARGET object as Org syntax.
3025 CONTENTS is the contents of the object."
3026 (concat "<<<" contents
">>>"))
3028 (defun org-element-radio-target-successor (limit)
3029 "Search for the next radio-target object.
3031 LIMIT bounds the search.
3033 Return value is a cons cell whose CAR is `radio-target' and CDR
3034 is beginning position."
3036 (when (re-search-forward org-radio-target-regexp limit t
)
3037 (cons 'radio-target
(match-beginning 0)))))
3040 ;;;; Statistics Cookie
3042 (defun org-element-statistics-cookie-parser ()
3043 "Parse statistics cookie at point.
3045 Return a list whose CAR is `statistics-cookie', and CDR a plist
3046 with `:begin', `:end', `:value' and `:post-blank' keywords.
3048 Assume point is at the beginning of the statistics-cookie."
3050 (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
3051 (let* ((begin (point))
3052 (value (buffer-substring-no-properties
3053 (match-beginning 0) (match-end 0)))
3054 (post-blank (progn (goto-char (match-end 0))
3055 (skip-chars-forward " \t")))
3057 (list 'statistics-cookie
3061 :post-blank post-blank
)))))
3063 (defun org-element-statistics-cookie-interpreter (statistics-cookie contents
)
3064 "Interpret STATISTICS-COOKIE object as Org syntax.
3066 (org-element-property :value statistics-cookie
))
3068 (defun org-element-statistics-cookie-successor (limit)
3069 "Search for the next statistics cookie object.
3071 LIMIT bounds the search.
3073 Return value is a cons cell whose CAR is `statistics-cookie' and
3074 CDR is beginning position."
3076 (when (re-search-forward "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]" limit t
)
3077 (cons 'statistics-cookie
(match-beginning 0)))))
3082 (defun org-element-strike-through-parser ()
3083 "Parse strike-through object at point.
3085 Return a list whose CAR is `strike-through' and CDR is a plist
3086 with `:begin', `:end', `:contents-begin' and `:contents-end' and
3087 `:post-blank' keywords.
3089 Assume point is at the first plus sign marker."
3091 (unless (bolp) (backward-char 1))
3092 (looking-at org-emph-re
)
3093 (let ((begin (match-beginning 2))
3094 (contents-begin (match-beginning 4))
3095 (contents-end (match-end 4))
3096 (post-blank (progn (goto-char (match-end 2))
3097 (skip-chars-forward " \t")))
3099 (list 'strike-through
3102 :contents-begin contents-begin
3103 :contents-end contents-end
3104 :post-blank post-blank
)))))
3106 (defun org-element-strike-through-interpreter (strike-through contents
)
3107 "Interpret STRIKE-THROUGH object as Org syntax.
3108 CONTENTS is the contents of the object."
3109 (format "+%s+" contents
))
3114 (defun org-element-subscript-parser ()
3115 "Parse subscript at point.
3117 Return a list whose CAR is `subscript' and CDR a plist with
3118 `:begin', `:end', `:contents-begin', `:contents-end',
3119 `:use-brackets-p' and `:post-blank' as keywords.
3121 Assume point is at the underscore."
3123 (unless (bolp) (backward-char))
3124 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp
)
3126 (not (looking-at org-match-substring-regexp
))))
3127 (begin (match-beginning 2))
3128 (contents-begin (or (match-beginning 5)
3129 (match-beginning 3)))
3130 (contents-end (or (match-end 5) (match-end 3)))
3131 (post-blank (progn (goto-char (match-end 0))
3132 (skip-chars-forward " \t")))
3137 :use-brackets-p bracketsp
3138 :contents-begin contents-begin
3139 :contents-end contents-end
3140 :post-blank post-blank
)))))
3142 (defun org-element-subscript-interpreter (subscript contents
)
3143 "Interpret SUBSCRIPT object as Org syntax.
3144 CONTENTS is the contents of the object."
3146 (if (org-element-property :use-brackets-p subscript
) "_{%s}" "_%s")
3149 (defun org-element-sub/superscript-successor
(limit)
3150 "Search for the next sub/superscript object.
3152 LIMIT bounds the search.
3154 Return value is a cons cell whose CAR is either `subscript' or
3155 `superscript' and CDR is beginning position."
3157 (when (re-search-forward org-match-substring-regexp limit t
)
3158 (cons (if (string= (match-string 2) "_") 'subscript
'superscript
)
3159 (match-beginning 2)))))
3164 (defun org-element-superscript-parser ()
3165 "Parse superscript at point.
3167 Return a list whose CAR is `superscript' and CDR a plist with
3168 `:begin', `:end', `:contents-begin', `:contents-end',
3169 `:use-brackets-p' and `:post-blank' as keywords.
3171 Assume point is at the caret."
3173 (unless (bolp) (backward-char))
3174 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp
) t
3175 (not (looking-at org-match-substring-regexp
))))
3176 (begin (match-beginning 2))
3177 (contents-begin (or (match-beginning 5)
3178 (match-beginning 3)))
3179 (contents-end (or (match-end 5) (match-end 3)))
3180 (post-blank (progn (goto-char (match-end 0))
3181 (skip-chars-forward " \t")))
3186 :use-brackets-p bracketsp
3187 :contents-begin contents-begin
3188 :contents-end contents-end
3189 :post-blank post-blank
)))))
3191 (defun org-element-superscript-interpreter (superscript contents
)
3192 "Interpret SUPERSCRIPT object as Org syntax.
3193 CONTENTS is the contents of the object."
3195 (if (org-element-property :use-brackets-p superscript
) "^{%s}" "^%s")
3201 (defun org-element-table-cell-parser ()
3202 "Parse table cell at point.
3204 Return a list whose CAR is `table-cell' and CDR is a plist
3205 containing `:begin', `:end', `:contents-begin', `:contents-end'
3206 and `:post-blank' keywords."
3207 (looking-at "[ \t]*\\(.*?\\)[ \t]*|")
3208 (let* ((begin (match-beginning 0))
3210 (contents-begin (match-beginning 1))
3211 (contents-end (match-end 1)))
3215 :contents-begin contents-begin
3216 :contents-end contents-end
3219 (defun org-element-table-cell-interpreter (table-cell contents
)
3220 "Interpret TABLE-CELL element as Org syntax.
3221 CONTENTS is the contents of the cell, or nil."
3222 (concat " " contents
" |"))
3224 (defun org-element-table-cell-successor (limit)
3225 "Search for the next table-cell object.
3227 LIMIT bounds the search.
3229 Return value is a cons cell whose CAR is `table-cell' and CDR is
3230 beginning position."
3231 (when (looking-at "[ \t]*.*?[ \t]+|") (cons 'table-cell
(point))))
3236 (defun org-element-target-parser ()
3237 "Parse target at point.
3239 Return a list whose CAR is `target' and CDR a plist with
3240 `:begin', `:end', `:value' and `:post-blank' as keywords.
3242 Assume point is at the target."
3244 (looking-at org-target-regexp
)
3245 (let ((begin (point))
3246 (value (org-match-string-no-properties 1))
3247 (post-blank (progn (goto-char (match-end 0))
3248 (skip-chars-forward " \t")))
3254 :post-blank post-blank
)))))
3256 (defun org-element-target-interpreter (target contents
)
3257 "Interpret TARGET object as Org syntax.
3259 (format "<<%s>>" (org-element-property :value target
)))
3261 (defun org-element-target-successor (limit)
3262 "Search for the next target object.
3264 LIMIT bounds the search.
3266 Return value is a cons cell whose CAR is `target' and CDR is
3267 beginning position."
3269 (when (re-search-forward org-target-regexp limit t
)
3270 (cons 'target
(match-beginning 0)))))
3275 (defun org-element-timestamp-parser ()
3276 "Parse time stamp at point.
3278 Return a list whose CAR is `timestamp', and CDR a plist with
3279 `:type', `:begin', `:end', `:value' and `:post-blank' keywords.
3281 Assume point is at the beginning of the timestamp."
3283 (let* ((begin (point))
3284 (activep (eq (char-after) ?
<))
3287 (looking-at "[<[]\\(\\(%%\\)?.*?\\)[]>]\\(?:--[<[]\\(.*?\\)[]>]\\)?")
3288 (match-string-no-properties 1)))
3289 (range-end (match-string-no-properties 3))
3290 (type (cond ((match-string 2) 'diary
)
3291 ((and activep range-end
) 'active-range
)
3293 (range-end 'inactive-range
)
3295 (post-blank (progn (goto-char (match-end 0))
3296 (skip-chars-forward " \t")))
3301 :range-end range-end
3304 :post-blank post-blank
)))))
3306 (defun org-element-timestamp-interpreter (timestamp contents
)
3307 "Interpret TIMESTAMP object as Org syntax.
3309 (let ((type (org-element-property :type timestamp
) ))
3311 (format (if (memq type
'(inactive inactive-range
)) "[%s]" "<%s>")
3312 (org-element-property :value timestamp
))
3313 (let ((range-end (org-element-property :range-end timestamp
)))
3316 (format (if (eq type
'inactive-range
) "[%s]" "<%s>")
3319 (defun org-element-timestamp-successor (limit)
3320 "Search for the next timestamp object.
3322 LIMIT bounds the search.
3324 Return value is a cons cell whose CAR is `timestamp' and CDR is
3325 beginning position."
3327 (when (re-search-forward
3328 (concat org-ts-regexp-both
3330 "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
3332 "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
3334 (cons 'timestamp
(match-beginning 0)))))
3339 (defun org-element-underline-parser ()
3340 "Parse underline object at point.
3342 Return a list whose CAR is `underline' and CDR is a plist with
3343 `:begin', `:end', `:contents-begin' and `:contents-end' and
3344 `:post-blank' keywords.
3346 Assume point is at the first underscore marker."
3348 (unless (bolp) (backward-char 1))
3349 (looking-at org-emph-re
)
3350 (let ((begin (match-beginning 2))
3351 (contents-begin (match-beginning 4))
3352 (contents-end (match-end 4))
3353 (post-blank (progn (goto-char (match-end 2))
3354 (skip-chars-forward " \t")))
3359 :contents-begin contents-begin
3360 :contents-end contents-end
3361 :post-blank post-blank
)))))
3363 (defun org-element-underline-interpreter (underline contents
)
3364 "Interpret UNDERLINE object as Org syntax.
3365 CONTENTS is the contents of the object."
3366 (format "_%s_" contents
))
3371 (defun org-element-verbatim-parser ()
3372 "Parse verbatim object at point.
3374 Return a list whose CAR is `verbatim' and CDR is a plist with
3375 `:value', `:begin', `:end' and `:post-blank' keywords.
3377 Assume point is at the first equal sign marker."
3379 (unless (bolp) (backward-char 1))
3380 (looking-at org-emph-re
)
3381 (let ((begin (match-beginning 2))
3382 (value (org-match-string-no-properties 4))
3383 (post-blank (progn (goto-char (match-end 2))
3384 (skip-chars-forward " \t")))
3390 :post-blank post-blank
)))))
3392 (defun org-element-verbatim-interpreter (verbatim contents
)
3393 "Interpret VERBATIM object as Org syntax.
3395 (format "=%s=" (org-element-property :value verbatim
)))
3399 ;;; Parsing Element Starting At Point
3401 ;; `org-element--current-element' is the core function of this section.
3402 ;; It returns the Lisp representation of the element starting at
3405 ;; `org-element--current-element' makes use of special modes. They
3406 ;; are activated for fixed element chaining (i.e. `plain-list' >
3407 ;; `item') or fixed conditional element chaining (i.e. `headline' >
3408 ;; `section'). Special modes are: `first-section', `item',
3409 ;; `node-property', `quote-section', `section' and `table-row'.
3411 (defun org-element--current-element
3412 (limit &optional granularity special structure
)
3413 "Parse the element starting at point.
3415 LIMIT bounds the search.
3417 Return value is a list like (TYPE PROPS) where TYPE is the type
3418 of the element and PROPS a plist of properties associated to the
3421 Possible types are defined in `org-element-all-elements'.
3423 Optional argument GRANULARITY determines the depth of the
3424 recursion. Allowed values are `headline', `greater-element',
3425 `element', `object' or nil. When it is broader than `object' (or
3426 nil), secondary values will not be parsed, since they only
3429 Optional argument SPECIAL, when non-nil, can be either
3430 `first-section', `item', `node-property', `quote-section',
3431 `section', and `table-row'.
3433 If STRUCTURE isn't provided but SPECIAL is set to `item', it will
3436 This function assumes point is always at the beginning of the
3437 element it has to parse."
3439 (let ((case-fold-search t
)
3440 ;; Determine if parsing depth allows for secondary strings
3441 ;; parsing. It only applies to elements referenced in
3442 ;; `org-element-secondary-value-alist'.
3443 (raw-secondary-p (and granularity
(not (eq granularity
'object
)))))
3447 (org-element-item-parser limit structure raw-secondary-p
))
3449 ((eq special
'table-row
) (org-element-table-row-parser limit
))
3451 ((eq special
'node-property
) (org-element-node-property-parser limit
))
3453 ((org-with-limited-levels (org-at-heading-p))
3454 (org-element-headline-parser limit raw-secondary-p
))
3455 ;; Sections (must be checked after headline).
3456 ((eq special
'section
) (org-element-section-parser limit
))
3457 ((eq special
'quote-section
) (org-element-quote-section-parser limit
))
3458 ((eq special
'first-section
)
3459 (org-element-section-parser
3460 (or (save-excursion (org-with-limited-levels (outline-next-heading)))
3462 ;; When not at bol, point is at the beginning of an item or
3463 ;; a footnote definition: next item is always a paragraph.
3464 ((not (bolp)) (org-element-paragraph-parser limit
(list (point))))
3465 ;; Planning and Clock.
3466 ((and (looking-at org-planning-or-clock-line-re
))
3467 (if (equal (match-string 1) org-clock-string
)
3468 (org-element-clock-parser limit
)
3469 (org-element-planning-parser limit
)))
3472 (org-element-inlinetask-parser limit raw-secondary-p
))
3473 ;; From there, elements can have affiliated keywords.
3474 (t (let ((affiliated (org-element--collect-affiliated-keywords)))
3476 ;; LaTeX Environment.
3477 ((looking-at "[ \t]*\\\\begin{\\([A-Za-z0-9*]+\\)}[ \t]*$")
3480 (format "^[ \t]*\\\\end{%s}[ \t]*$"
3481 (regexp-quote (match-string 1)))
3483 (org-element-latex-environment-parser limit affiliated
)
3484 (org-element-paragraph-parser limit affiliated
)))
3485 ;; Drawer and Property Drawer.
3486 ((looking-at org-drawer-regexp
)
3487 (let ((name (match-string 1)))
3489 ((not (save-excursion
3490 (re-search-forward "^[ \t]*:END:[ \t]*$" nil t
)))
3491 (org-element-paragraph-parser limit affiliated
))
3492 ((equal "PROPERTIES" name
)
3493 (org-element-property-drawer-parser limit affiliated
))
3494 (t (org-element-drawer-parser limit affiliated
)))))
3496 ((looking-at "[ \t]*:\\( \\|$\\)")
3497 (org-element-fixed-width-parser limit affiliated
))
3498 ;; Inline Comments, Blocks, Babel Calls, Dynamic Blocks and
3500 ((looking-at "[ \t]*#")
3501 (goto-char (match-end 0))
3502 (cond ((looking-at "\\(?: \\|$\\)")
3504 (org-element-comment-parser limit affiliated
))
3505 ((looking-at "\\+BEGIN_\\(\\S-+\\)")
3507 (let ((parser (assoc (upcase (match-string 1))
3508 org-element-block-name-alist
)))
3509 (if parser
(funcall (cdr parser
) limit affiliated
)
3510 (org-element-special-block-parser limit affiliated
))))
3511 ((looking-at "\\+CALL:")
3513 (org-element-babel-call-parser limit affiliated
))
3514 ((looking-at "\\+BEGIN:? ")
3516 (org-element-dynamic-block-parser limit affiliated
))
3517 ((looking-at "\\+\\S-+:")
3519 (org-element-keyword-parser limit affiliated
))
3522 (org-element-paragraph-parser limit affiliated
))))
3523 ;; Footnote Definition.
3524 ((looking-at org-footnote-definition-re
)
3525 (org-element-footnote-definition-parser limit affiliated
))
3527 ((looking-at "[ \t]*-\\{5,\\}[ \t]*$")
3528 (org-element-horizontal-rule-parser limit affiliated
))
3530 ((org-at-table-p t
) (org-element-table-parser limit affiliated
))
3532 ((looking-at (org-item-re))
3533 (org-element-plain-list-parser
3534 limit affiliated
(or structure
(org-list-struct))))
3535 ;; Default element: Paragraph.
3536 (t (org-element-paragraph-parser limit affiliated
)))))))))
3539 ;; Most elements can have affiliated keywords. When looking for an
3540 ;; element beginning, we want to move before them, as they belong to
3541 ;; that element, and, in the meantime, collect information they give
3542 ;; into appropriate properties. Hence the following function.
3544 (defun org-element--collect-affiliated-keywords ()
3545 "Collect affiliated keywords from point.
3547 Return a list whose CAR is the position at the first of them and
3548 CDR a plist of keywords and values and move point to the
3549 beginning of the first line after them.
3551 As a special case, if element doesn't start at the beginning of
3552 the line (i.e. a paragraph starting an item), CAR is current
3553 position of point and CDR is nil."
3554 (if (not (bolp)) (list (point))
3555 (let ((case-fold-search t
)
3557 ;; RESTRICT is the list of objects allowed in parsed
3559 (restrict (org-element-restriction 'keyword
))
3561 (while (and (not (eobp)) (looking-at org-element--affiliated-re
))
3562 (let* ((raw-kwd (upcase (match-string 1)))
3563 ;; Apply translation to RAW-KWD. From there, KWD is
3564 ;; the official keyword.
3565 (kwd (or (cdr (assoc raw-kwd
3566 org-element-keyword-translation-alist
))
3568 ;; Find main value for any keyword.
3572 (buffer-substring-no-properties
3573 (match-end 0) (point-at-eol)))))
3574 ;; PARSEDP is non-nil when keyword should have its
3576 (parsedp (member kwd org-element-parsed-keywords
))
3577 ;; If KWD is a dual keyword, find its secondary
3578 ;; value. Maybe parse it.
3579 (dualp (member kwd org-element-dual-keywords
))
3582 (let ((sec (org-match-string-no-properties 2)))
3583 (if (or (not sec
) (not parsedp
)) sec
3584 (org-element-parse-secondary-string sec restrict
)))))
3585 ;; Attribute a property name to KWD.
3586 (kwd-sym (and kwd
(intern (concat ":" (downcase kwd
))))))
3587 ;; Now set final shape for VALUE.
3589 (setq value
(org-element-parse-secondary-string value restrict
)))
3590 (when dualp
(setq value
(and value
(cons value dual-value
))))
3591 (when (or (member kwd org-element-multiple-keywords
)
3592 ;; Attributes can always appear on multiple lines.
3593 (string-match "^ATTR_" kwd
))
3594 (setq value
(cons value
(plist-get output kwd-sym
))))
3595 ;; Eventually store the new value in OUTPUT.
3596 (setq output
(plist-put output kwd-sym value
))
3597 ;; Move to next keyword.
3599 ;; If affiliated keywords are orphaned: move back to first one.
3600 ;; They will be parsed as a paragraph.
3601 (when (looking-at "[ \t]*$") (goto-char origin
) (setq output nil
))
3603 (cons origin output
))))
3609 ;; The two major functions here are `org-element-parse-buffer', which
3610 ;; parses Org syntax inside the current buffer, taking into account
3611 ;; region, narrowing, or even visibility if specified, and
3612 ;; `org-element-parse-secondary-string', which parses objects within
3615 ;; The (almost) almighty `org-element-map' allows to apply a function
3616 ;; on elements or objects matching some type, and accumulate the
3617 ;; resulting values. In an export situation, it also skips unneeded
3618 ;; parts of the parse tree.
3620 (defun org-element-parse-buffer (&optional granularity visible-only
)
3621 "Recursively parse the buffer and return structure.
3622 If narrowing is in effect, only parse the visible part of the
3625 Optional argument GRANULARITY determines the depth of the
3626 recursion. It can be set to the following symbols:
3628 `headline' Only parse headlines.
3629 `greater-element' Don't recurse into greater elements excepted
3630 headlines and sections. Thus, elements
3631 parsed are the top-level ones.
3632 `element' Parse everything but objects and plain text.
3633 `object' Parse the complete buffer (default).
3635 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3638 Assume buffer is in Org mode."
3640 (goto-char (point-min))
3641 (org-skip-whitespace)
3642 (org-element--parse-elements
3643 (point-at-bol) (point-max)
3644 ;; Start in `first-section' mode so text before the first
3645 ;; headline belongs to a section.
3646 'first-section nil granularity visible-only
(list 'org-data nil
))))
3648 (defun org-element-parse-secondary-string (string restriction
&optional parent
)
3649 "Recursively parse objects in STRING and return structure.
3651 RESTRICTION is a symbol limiting the object types that will be
3654 Optional argument PARENT, when non-nil, is the element or object
3655 containing the secondary string. It is used to set correctly
3656 `:parent' property within the string."
3659 (let ((secondary (org-element--parse-objects
3660 (point-min) (point-max) nil restriction
)))
3661 (mapc (lambda (obj) (org-element-put-property obj
:parent parent
))
3664 (defun org-element-map (data types fun
&optional info first-match no-recursion
)
3665 "Map a function on selected elements or objects.
3667 DATA is the parsed tree, as returned by, i.e,
3668 `org-element-parse-buffer'. TYPES is a symbol or list of symbols
3669 of elements or objects types. FUN is the function called on the
3670 matching element or object. It must accept one arguments: the
3671 element or object itself.
3673 When optional argument INFO is non-nil, it should be a plist
3674 holding export options. In that case, parts of the parse tree
3675 not exportable according to that property list will be skipped.
3677 When optional argument FIRST-MATCH is non-nil, stop at the first
3678 match for which FUN doesn't return nil, and return that value.
3680 Optional argument NO-RECURSION is a symbol or a list of symbols
3681 representing elements or objects types. `org-element-map' won't
3682 enter any recursive element or object whose type belongs to that
3683 list. Though, FUN can still be applied on them.
3685 Nil values returned from FUN do not appear in the results."
3686 ;; Ensure TYPES and NO-RECURSION are a list, even of one element.
3687 (unless (listp types
) (setq types
(list types
)))
3688 (unless (listp no-recursion
) (setq no-recursion
(list no-recursion
)))
3689 ;; Recursion depth is determined by --CATEGORY.
3692 (let ((category 'greater-elements
))
3693 (mapc (lambda (type)
3694 (cond ((or (memq type org-element-all-objects
)
3695 (eq type
'plain-text
))
3696 ;; If one object is found, the function
3697 ;; has to recurse into every object.
3698 (throw 'found
'objects
))
3699 ((not (memq type org-element-greater-elements
))
3700 ;; If one regular element is found, the
3701 ;; function has to recurse, at least,
3702 ;; into every element it encounters.
3703 (and (not (eq category
'elements
))
3704 (setq category
'elements
)))))
3712 ;; Recursively walk DATA. INFO, if non-nil, is a plist
3713 ;; holding contextual information.
3714 (let ((--type (org-element-type --data
)))
3717 ;; Ignored element in an export context.
3718 ((and info
(memq --data
(plist-get info
:ignore-list
))))
3719 ;; Secondary string: only objects can be found there.
3721 (when (eq --category
'objects
) (mapc --walk-tree --data
)))
3722 ;; Unconditionally enter parse trees.
3723 ((eq --type
'org-data
)
3724 (mapc --walk-tree
(org-element-contents --data
)))
3726 ;; Check if TYPE is matching among TYPES. If so,
3727 ;; apply FUN to --DATA and accumulate return value
3728 ;; into --ACC (or exit if FIRST-MATCH is non-nil).
3729 (when (memq --type types
)
3730 (let ((result (funcall fun --data
)))
3731 (cond ((not result
))
3732 (first-match (throw '--map-first-match result
))
3733 (t (push result --acc
)))))
3734 ;; If --DATA has a secondary string that can contain
3735 ;; objects with their type among TYPES, look into it.
3736 (when (eq --category
'objects
)
3738 (assq --type org-element-secondary-value-alist
)))
3740 (funcall --walk-tree
3741 (org-element-property (cdr sec-prop
) --data
)))))
3742 ;; Determine if a recursion into --DATA is possible.
3744 ;; --TYPE is explicitly removed from recursion.
3745 ((memq --type no-recursion
))
3746 ;; --DATA has no contents.
3747 ((not (org-element-contents --data
)))
3748 ;; Looking for greater elements but --DATA is simply
3749 ;; an element or an object.
3750 ((and (eq --category
'greater-elements
)
3751 (not (memq --type org-element-greater-elements
))))
3752 ;; Looking for elements but --DATA is an object.
3753 ((and (eq --category
'elements
)
3754 (memq --type org-element-all-objects
)))
3755 ;; In any other case, map contents.
3756 (t (mapc --walk-tree
(org-element-contents --data
)))))))))))
3757 (catch '--map-first-match
3758 (funcall --walk-tree data
)
3759 ;; Return value in a proper order.
3762 ;; The following functions are internal parts of the parser.
3764 ;; The first one, `org-element--parse-elements' acts at the element's
3767 ;; The second one, `org-element--parse-objects' applies on all objects
3768 ;; of a paragraph or a secondary string. It uses
3769 ;; `org-element--get-next-object-candidates' to optimize the search of
3770 ;; the next object in the buffer.
3772 ;; More precisely, that function looks for every allowed object type
3773 ;; first. Then, it discards failed searches, keeps further matches,
3774 ;; and searches again types matched behind point, for subsequent
3775 ;; calls. Thus, searching for a given type fails only once, and every
3776 ;; object is searched only once at top level (but sometimes more for
3779 (defun org-element--parse-elements
3780 (beg end special structure granularity visible-only acc
)
3781 "Parse elements between BEG and END positions.
3783 SPECIAL prioritize some elements over the others. It can be set
3784 to `first-section', `quote-section', `section' `item' or
3787 When value is `item', STRUCTURE will be used as the current list
3790 GRANULARITY determines the depth of the recursion. See
3791 `org-element-parse-buffer' for more information.
3793 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3796 Elements are accumulated into ACC."
3799 ;; When parsing only headlines, skip any text before first one.
3800 (when (and (eq granularity
'headline
) (not (org-at-heading-p)))
3801 (org-with-limited-levels (outline-next-heading)))
3803 (while (< (point) end
)
3804 ;; Find current element's type and parse it accordingly to
3806 (let* ((element (org-element--current-element
3807 end granularity special structure
))
3808 (type (org-element-type element
))
3809 (cbeg (org-element-property :contents-begin element
)))
3810 (goto-char (org-element-property :end element
))
3811 ;; Fill ELEMENT contents by side-effect.
3813 ;; If VISIBLE-ONLY is true and element is hidden or if it has
3814 ;; no contents, don't modify it.
3815 ((or (and visible-only
(org-element-property :hiddenp element
))
3817 ;; Greater element: parse it between `contents-begin' and
3818 ;; `contents-end'. Make sure GRANULARITY allows the
3819 ;; recursion, or ELEMENT is an headline, in which case going
3820 ;; inside is mandatory, in order to get sub-level headings.
3821 ((and (memq type org-element-greater-elements
)
3822 (or (memq granularity
'(element object nil
))
3823 (and (eq granularity
'greater-element
)
3825 (eq type
'headline
)))
3826 (org-element--parse-elements
3827 cbeg
(org-element-property :contents-end element
)
3828 ;; Possibly switch to a special mode.
3831 (if (org-element-property :quotedp element
) 'quote-section
3834 (property-drawer 'node-property
)
3836 (org-element-property :structure element
)
3837 granularity visible-only element
))
3838 ;; ELEMENT has contents. Parse objects inside, if
3839 ;; GRANULARITY allows it.
3840 ((memq granularity
'(object nil
))
3841 (org-element--parse-objects
3842 cbeg
(org-element-property :contents-end element
) element
3843 (org-element-restriction type
))))
3844 (org-element-adopt-elements acc element
)))
3848 (defun org-element--parse-objects (beg end acc restriction
)
3849 "Parse objects between BEG and END and return recursive structure.
3851 Objects are accumulated in ACC.
3853 RESTRICTION is a list of object types which are allowed in the
3858 (while (and (< (point) end
)
3859 (setq candidates
(org-element--get-next-object-candidates
3860 end restriction candidates
)))
3862 (let ((pos (apply 'min
(mapcar 'cdr candidates
))))
3865 (funcall (intern (format "org-element-%s-parser"
3866 (car (rassq pos candidates
)))))))))
3867 ;; 1. Text before any object. Untabify it.
3868 (let ((obj-beg (org-element-property :begin next-object
)))
3869 (unless (= (point) obj-beg
)
3871 (org-element-adopt-elements
3873 (replace-regexp-in-string
3874 "\t" (make-string tab-width ?
)
3875 (buffer-substring-no-properties (point) obj-beg
))))))
3877 (let ((obj-end (org-element-property :end next-object
))
3878 (cont-beg (org-element-property :contents-begin next-object
)))
3879 ;; Fill contents of NEXT-OBJECT by side-effect, if it has
3880 ;; a recursive type.
3882 (memq (car next-object
) org-element-recursive-objects
))
3886 (org-element-property :contents-end next-object
))
3887 (org-element--parse-objects
3888 (point-min) (point-max) next-object
3889 (org-element-restriction next-object
))))
3890 (setq acc
(org-element-adopt-elements acc next-object
))
3891 (goto-char obj-end
))))
3892 ;; 3. Text after last object. Untabify it.
3893 (unless (= (point) end
)
3895 (org-element-adopt-elements
3897 (replace-regexp-in-string
3898 "\t" (make-string tab-width ?
)
3899 (buffer-substring-no-properties (point) end
)))))
3903 (defun org-element--get-next-object-candidates (limit restriction objects
)
3904 "Return an alist of candidates for the next object.
3906 LIMIT bounds the search, and RESTRICTION narrows candidates to
3909 Return value is an alist whose CAR is position and CDR the object
3912 OBJECTS is the previous candidates alist."
3913 (let (next-candidates types-to-search
)
3914 ;; If no previous result, search every object type in RESTRICTION.
3915 ;; Otherwise, keep potential candidates (old objects located after
3916 ;; point) and ask to search again those which had matched before.
3917 (if (not objects
) (setq types-to-search restriction
)
3919 (if (< (cdr obj
) (point)) (push (car obj
) types-to-search
)
3920 (push obj next-candidates
)))
3922 ;; Call the appropriate successor function for each type to search
3923 ;; and accumulate matches.
3926 (let* ((successor-fun
3928 (format "org-element-%s-successor"
3929 (or (cdr (assq type org-element-object-successor-alist
))
3931 (obj (funcall successor-fun limit
)))
3932 (and obj
(push obj next-candidates
))))
3939 ;;; Towards A Bijective Process
3941 ;; The parse tree obtained with `org-element-parse-buffer' is really
3942 ;; a snapshot of the corresponding Org buffer. Therefore, it can be
3943 ;; interpreted and expanded into a string with canonical Org syntax.
3944 ;; Hence `org-element-interpret-data'.
3946 ;; The function relies internally on
3947 ;; `org-element--interpret-affiliated-keywords'.
3950 (defun org-element-interpret-data (data &optional parent
)
3951 "Interpret DATA as Org syntax.
3953 DATA is a parse tree, an element, an object or a secondary string
3956 Optional argument PARENT is used for recursive calls. It contains
3957 the element or object containing data, or nil.
3959 Return Org syntax as a string."
3960 (let* ((type (org-element-type data
))
3963 ;; Secondary string.
3966 (lambda (obj) (org-element-interpret-data obj parent
))
3968 ;; Full Org document.
3969 ((eq type
'org-data
)
3971 (lambda (obj) (org-element-interpret-data obj parent
))
3972 (org-element-contents data
) ""))
3974 ((stringp data
) data
)
3975 ;; Element/Object without contents.
3976 ((not (org-element-contents data
))
3977 (funcall (intern (format "org-element-%s-interpreter" type
))
3979 ;; Element/Object with contents.
3981 (let* ((greaterp (memq type org-element-greater-elements
))
3982 (objectp (and (not greaterp
)
3983 (memq type org-element-recursive-objects
)))
3986 (lambda (obj) (org-element-interpret-data obj data
))
3987 (org-element-contents
3988 (if (or greaterp objectp
) data
3989 ;; Elements directly containing objects must
3990 ;; have their indentation normalized first.
3991 (org-element-normalize-contents
3993 ;; When normalizing first paragraph of an
3994 ;; item or a footnote-definition, ignore
3995 ;; first line's indentation.
3996 (and (eq type
'paragraph
)
3997 (equal data
(car (org-element-contents parent
)))
3998 (memq (org-element-type parent
)
3999 '(footnote-definiton item
))))))
4001 (funcall (intern (format "org-element-%s-interpreter" type
))
4003 (if greaterp
(org-element-normalize-contents contents
)
4005 (if (memq type
'(org-data plain-text nil
)) results
4006 ;; Build white spaces. If no `:post-blank' property is
4007 ;; specified, assume its value is 0.
4008 (let ((post-blank (or (org-element-property :post-blank data
) 0)))
4009 (if (memq type org-element-all-objects
)
4010 (concat results
(make-string post-blank
32))
4012 (org-element--interpret-affiliated-keywords data
)
4013 (org-element-normalize-string results
)
4014 (make-string post-blank
10)))))))
4016 (defun org-element--interpret-affiliated-keywords (element)
4017 "Return ELEMENT's affiliated keywords as Org syntax.
4018 If there is no affiliated keyword, return the empty string."
4019 (let ((keyword-to-org
4023 (when (member key org-element-dual-keywords
)
4024 (setq dual
(cdr value
) value
(car value
)))
4027 (format "[%s]" (org-element-interpret-data dual
)))
4029 (if (member key org-element-parsed-keywords
)
4030 (org-element-interpret-data value
)
4035 (let ((value (org-element-property prop element
))
4036 (keyword (upcase (substring (symbol-name prop
) 1))))
4038 (if (or (member keyword org-element-multiple-keywords
)
4039 ;; All attribute keywords can have multiple lines.
4040 (string-match "^ATTR_" keyword
))
4041 (mapconcat (lambda (line) (funcall keyword-to-org keyword line
))
4044 (funcall keyword-to-org keyword value
)))))
4045 ;; List all ELEMENT's properties matching an attribute line or an
4046 ;; affiliated keyword, but ignore translated keywords since they
4047 ;; cannot belong to the property list.
4048 (loop for prop in
(nth 1 element
) by
'cddr
4049 when
(let ((keyword (upcase (substring (symbol-name prop
) 1))))
4050 (or (string-match "^ATTR_" keyword
)
4052 (member keyword org-element-affiliated-keywords
)
4054 org-element-keyword-translation-alist
)))))
4058 ;; Because interpretation of the parse tree must return the same
4059 ;; number of blank lines between elements and the same number of white
4060 ;; space after objects, some special care must be given to white
4063 ;; The first function, `org-element-normalize-string', ensures any
4064 ;; string different from the empty string will end with a single
4065 ;; newline character.
4067 ;; The second function, `org-element-normalize-contents', removes
4068 ;; global indentation from the contents of the current element.
4070 (defun org-element-normalize-string (s)
4071 "Ensure string S ends with a single newline character.
4073 If S isn't a string return it unchanged. If S is the empty
4074 string, return it. Otherwise, return a new string with a single
4075 newline character at its end."
4077 ((not (stringp s
)) s
)
4079 (t (and (string-match "\\(\n[ \t]*\\)*\\'" s
)
4080 (replace-match "\n" nil nil s
)))))
4082 (defun org-element-normalize-contents (element &optional ignore-first
)
4083 "Normalize plain text in ELEMENT's contents.
4085 ELEMENT must only contain plain text and objects.
4087 If optional argument IGNORE-FIRST is non-nil, ignore first line's
4088 indentation to compute maximal common indentation.
4090 Return the normalized element that is element with global
4091 indentation removed from its contents. The function assumes that
4092 indentation is not done with TAB characters."
4093 (let* (ind-list ; for byte-compiler
4094 collect-inds
; for byte-compiler
4097 ;; Return list of indentations within BLOB. This is done by
4098 ;; walking recursively BLOB and updating IND-LIST along the
4099 ;; way. FIRST-FLAG is non-nil when the first string hasn't
4100 ;; been seen yet. It is required as this string is the only
4101 ;; one whose indentation doesn't happen after a newline
4103 (lambda (blob first-flag
)
4106 (when (and first-flag
(stringp object
))
4107 (setq first-flag nil
)
4108 (string-match "\\`\\( *\\)" object
)
4109 (let ((len (length (match-string 1 object
))))
4110 ;; An indentation of zero means no string will be
4111 ;; modified. Quit the process.
4112 (if (zerop len
) (throw 'zero
(setq ind-list nil
))
4113 (push len ind-list
))))
4117 ;; Avoid matching blank or empty lines.
4118 (while (and (string-match "\n\\( *\\)\\(.\\)" object start
)
4119 (not (equal (match-string 2 object
) " ")))
4120 (setq start
(match-end 0))
4121 (push (length (match-string 1 object
)) ind-list
))))
4122 ((memq (org-element-type object
) org-element-recursive-objects
)
4123 (funcall collect-inds object first-flag
))))
4124 (org-element-contents blob
))))))
4125 ;; Collect indentation list in ELEMENT. Possibly remove first
4126 ;; value if IGNORE-FIRST is non-nil.
4127 (catch 'zero
(funcall collect-inds element
(not ignore-first
)))
4128 (if (not ind-list
) element
4129 ;; Build ELEMENT back, replacing each string with the same
4130 ;; string minus common indentation.
4131 (let* (build ; For byte compiler.
4134 (lambda (blob mci first-flag
)
4135 ;; Return BLOB with all its strings indentation
4136 ;; shortened from MCI white spaces. FIRST-FLAG is
4137 ;; non-nil when the first string hasn't been seen
4142 (when (and first-flag
(stringp object
))
4143 (setq first-flag nil
)
4145 (replace-regexp-in-string
4146 (format "\\` \\{%d\\}" mci
) "" object
)))
4149 (replace-regexp-in-string
4150 (format "\n \\{%d\\}" mci
) "\n" object
))
4151 ((memq (org-element-type object
)
4152 org-element-recursive-objects
)
4153 (funcall build object mci first-flag
))
4155 (org-element-contents blob
)))
4157 (funcall build element
(apply 'min ind-list
) (not ignore-first
))))))
4163 ;; The first move is to implement a way to obtain the smallest element
4164 ;; containing point. This is the job of `org-element-at-point'. It
4165 ;; basically jumps back to the beginning of section containing point
4166 ;; and moves, element after element, with
4167 ;; `org-element--current-element' until the container is found. Note:
4168 ;; When using `org-element-at-point', secondary values are never
4169 ;; parsed since the function focuses on elements, not on objects.
4171 ;; At a deeper level, `org-element-context' lists all elements and
4172 ;; objects containing point.
4174 ;; `org-element-nested-p' and `org-element-swap-A-B' may be used
4175 ;; internally by navigation and manipulation tools.
4178 (defun org-element-at-point (&optional keep-trail
)
4179 "Determine closest element around point.
4181 Return value is a list like (TYPE PROPS) where TYPE is the type
4182 of the element and PROPS a plist of properties associated to the
4185 Possible types are defined in `org-element-all-elements'.
4186 Properties depend on element or object type, but always
4187 include :begin, :end, :parent and :post-blank properties.
4189 As a special case, if point is at the very beginning of a list or
4190 sub-list, returned element will be that list instead of the first
4191 item. In the same way, if point is at the beginning of the first
4192 row of a table, returned element will be the table instead of the
4195 If optional argument KEEP-TRAIL is non-nil, the function returns
4196 a list of of elements leading to element at point. The list's
4197 CAR is always the element at point. Following positions contain
4198 element's siblings, then parents, siblings of parents, until the
4199 first element of current section."
4200 (org-with-wide-buffer
4201 ;; If at an headline, parse it. It is the sole element that
4202 ;; doesn't require to know about context. Be sure to disallow
4203 ;; secondary string parsing, though.
4204 (if (org-with-limited-levels (org-at-heading-p))
4207 (if (not keep-trail
) (org-element-headline-parser (point-max) t
)
4208 (list (org-element-headline-parser (point-max) t
))))
4209 ;; Otherwise move at the beginning of the section containing
4211 (let ((origin (point))
4212 (end (save-excursion
4213 (org-with-limited-levels (outline-next-heading)) (point)))
4214 element type special-flag trail struct prevs parent
)
4215 (org-with-limited-levels
4216 (if (org-with-limited-levels (org-before-first-heading-p))
4217 (goto-char (point-min))
4218 (org-back-to-heading)
4220 (org-skip-whitespace)
4222 ;; Parse successively each element, skipping those ending
4223 ;; before original position.
4227 (org-element--current-element end
'element special-flag struct
)
4229 (org-element-put-property element
:parent parent
)
4230 (when keep-trail
(push element trail
))
4232 ;; 1. Skip any element ending before point. Also skip
4233 ;; element ending at point when we're sure that another
4234 ;; element has started.
4235 ((let ((elem-end (org-element-property :end element
)))
4236 (when (or (< elem-end origin
)
4237 (and (= elem-end origin
) (/= elem-end end
)))
4238 (goto-char elem-end
))))
4239 ;; 2. An element containing point is always the element at
4241 ((not (memq type org-element-greater-elements
))
4242 (throw 'exit
(if keep-trail trail element
)))
4243 ;; 3. At any other greater element type, if point is
4244 ;; within contents, move into it.
4246 (let ((cbeg (org-element-property :contents-begin element
))
4247 (cend (org-element-property :contents-end element
)))
4248 (if (or (not cbeg
) (not cend
) (> cbeg origin
) (< cend origin
)
4249 ;; Create an anchor for tables and plain lists:
4250 ;; when point is at the very beginning of these
4251 ;; elements, ignoring affiliated keywords,
4252 ;; target them instead of their contents.
4253 (and (= cbeg origin
) (memq type
'(plain-list table
)))
4254 ;; When point is at contents end, do not move
4255 ;; into elements with an explicit ending, but
4256 ;; return that element instead.
4257 (and (= cend origin
)
4260 drawer dynamic-block inlinetask item
4261 plain-list property-drawer quote-block
4263 (throw 'exit
(if keep-trail trail element
))
4264 (setq parent element
)
4267 (setq special-flag
'item
4268 struct
(org-element-property :structure element
)))
4269 (property-drawer (setq special-flag
'node-property
))
4270 (table (setq special-flag
'table-row
))
4271 (otherwise (setq special-flag nil
)))
4273 (goto-char cbeg
)))))))))))
4276 (defun org-element-context ()
4277 "Return closest element or object around point.
4279 Return value is a list like (TYPE PROPS) where TYPE is the type
4280 of the element or object and PROPS a plist of properties
4283 Possible types are defined in `org-element-all-elements' and
4284 `org-element-all-objects'. Properties depend on element or
4285 object type, but always include :begin, :end, :parent
4286 and :post-blank properties."
4287 (org-with-wide-buffer
4288 (let* ((origin (point))
4289 (element (org-element-at-point))
4290 (type (car element
))
4292 ;; Check if point is inside an element containing objects or at
4293 ;; a secondary string. In that case, move to beginning of the
4294 ;; element or secondary string and set END to the other side.
4295 (if (not (or (and (eq type
'item
)
4296 (let ((tag (org-element-property :tag element
)))
4300 (search-forward tag
(point-at-eol))
4301 (goto-char (match-beginning 0))
4302 (and (>= origin
(point))
4304 ;; `1+' is required so some
4305 ;; successors can match
4306 ;; properly their object.
4307 (setq end
(1+ (match-end 0)))))))))
4308 (and (memq type
'(headline inlinetask
))
4309 (progn (beginning-of-line)
4310 (skip-chars-forward "* ")
4311 (setq end
(point-at-eol))))
4312 (and (memq type
'(paragraph table-cell verse-block
))
4313 (let ((cbeg (org-element-property
4314 :contents-begin element
))
4315 (cend (org-element-property
4316 :contents-end element
)))
4317 (and (>= origin cbeg
)
4319 (progn (goto-char cbeg
) (setq end cend
)))))))
4321 (let ((restriction (org-element-restriction element
))
4325 (while (setq candidates
(org-element--get-next-object-candidates
4326 end restriction candidates
))
4327 (let ((closest-cand (rassq (apply 'min
(mapcar 'cdr candidates
))
4329 ;; If ORIGIN is before next object in element, there's
4330 ;; no point in looking further.
4331 (if (> (cdr closest-cand
) origin
) (throw 'exit element
)
4333 (progn (goto-char (cdr closest-cand
))
4334 (funcall (intern (format "org-element-%s-parser"
4335 (car closest-cand
))))))
4336 (cbeg (org-element-property :contents-begin object
))
4337 (cend (org-element-property :contents-end object
)))
4339 ;; ORIGIN is after OBJECT, so skip it.
4340 ((< (org-element-property :end object
) origin
)
4341 (goto-char (org-element-property :end object
)))
4342 ;; ORIGIN is within a non-recursive object or at an
4343 ;; object boundaries: Return that object.
4344 ((or (not cbeg
) (> cbeg origin
) (< cend origin
))
4346 (org-element-put-property object
:parent parent
)))
4347 ;; Otherwise, move within current object and restrict
4348 ;; search to the end of its contents.
4350 (org-element-put-property object
:parent parent
)
4351 (setq parent object end cend
)))))))
4354 (defsubst org-element-nested-p
(elem-A elem-B
)
4355 "Non-nil when elements ELEM-A and ELEM-B are nested."
4356 (let ((beg-A (org-element-property :begin elem-A
))
4357 (beg-B (org-element-property :begin elem-B
))
4358 (end-A (org-element-property :end elem-A
))
4359 (end-B (org-element-property :end elem-B
)))
4360 (or (and (>= beg-A beg-B
) (<= end-A end-B
))
4361 (and (>= beg-B beg-A
) (<= end-B end-A
)))))
4363 (defun org-element-swap-A-B (elem-A elem-B
)
4364 "Swap elements ELEM-A and ELEM-B.
4365 Assume ELEM-B is after ELEM-A in the buffer. Leave point at the
4367 (goto-char (org-element-property :begin elem-A
))
4368 ;; There are two special cases when an element doesn't start at bol:
4369 ;; the first paragraph in an item or in a footnote definition.
4370 (let ((specialp (not (bolp))))
4371 ;; Only a paragraph without any affiliated keyword can be moved at
4372 ;; ELEM-A position in such a situation. Note that the case of
4373 ;; a footnote definition is impossible: it cannot contain two
4374 ;; paragraphs in a row because it cannot contain a blank line.
4376 (or (not (eq (org-element-type elem-B
) 'paragraph
))
4377 (/= (org-element-property :begin elem-B
)
4378 (org-element-property :contents-begin elem-B
))))
4379 (error "Cannot swap elements"))
4380 ;; In a special situation, ELEM-A will have no indentation. We'll
4381 ;; give it ELEM-B's (which will in, in turn, have no indentation).
4382 (let* ((ind-B (when specialp
4383 (goto-char (org-element-property :begin elem-B
))
4384 (org-get-indentation)))
4385 (beg-A (org-element-property :begin elem-A
))
4386 (end-A (save-excursion
4387 (goto-char (org-element-property :end elem-A
))
4388 (skip-chars-backward " \r\t\n")
4390 (beg-B (org-element-property :begin elem-B
))
4391 (end-B (save-excursion
4392 (goto-char (org-element-property :end elem-B
))
4393 (skip-chars-backward " \r\t\n")
4395 ;; Store overlays responsible for visibility status. We
4396 ;; also need to store their boundaries as they will be
4397 ;; removed from buffer.
4400 (mapcar (lambda (ov) (list ov
(overlay-start ov
) (overlay-end ov
)))
4401 (overlays-in beg-A end-A
))
4402 (mapcar (lambda (ov) (list ov
(overlay-start ov
) (overlay-end ov
)))
4403 (overlays-in beg-B end-B
))))
4405 (body-A (buffer-substring beg-A end-A
))
4406 (body-B (delete-and-extract-region beg-B end-B
)))
4409 (setq body-B
(replace-regexp-in-string "\\`[ \t]*" "" body-B
))
4410 (org-indent-to-column ind-B
))
4412 ;; Restore ex ELEM-A overlays.
4413 (let ((offset (- beg-B beg-A
)))
4416 (car ov
) (+ (nth 1 ov
) offset
) (+ (nth 2 ov
) offset
)))
4419 (delete-region beg-A end-A
)
4421 ;; Restore ex ELEM-B overlays.
4424 (car ov
) (- (nth 1 ov
) offset
) (- (nth 2 ov
) offset
)))
4426 (goto-char (org-element-property :end elem-B
)))))
4429 (provide 'org-element
)
4430 ;;; org-element.el ends here