1 ;;; org-element.el --- Parser And Applications for Org syntax
3 ;; Copyright (C) 2012 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; Org syntax can be divided into three categories: "Greater
26 ;; elements", "Elements" and "Objects".
28 ;; Elements are related to the structure of the document. Indeed, all
29 ;; elements are a cover for the document: each position within belongs
30 ;; to at least one element.
32 ;; An element always starts and ends at the beginning of a line. With
33 ;; a few exceptions (`clock', `headline', `inlinetask', `item',
34 ;; `planning', `node-property', `quote-section' `section' and
35 ;; `table-row' types), it can also accept a fixed set of keywords as
36 ;; attributes. Those are called "affiliated keywords" to distinguish
37 ;; them from other keywords, which are full-fledged elements. Almost
38 ;; all affiliated keywords are referenced in
39 ;; `org-element-affiliated-keywords'; the others are export attributes
40 ;; and start with "ATTR_" prefix.
42 ;; Element containing other elements (and only elements) are called
43 ;; greater elements. Concerned types are: `center-block', `drawer',
44 ;; `dynamic-block', `footnote-definition', `headline', `inlinetask',
45 ;; `item', `plain-list', `property-drawer', `quote-block', `section'
46 ;; and `special-block'.
48 ;; Other element types are: `babel-call', `clock', `comment',
49 ;; `comment-block', `example-block', `export-block', `fixed-width',
50 ;; `horizontal-rule', `keyword', `latex-environment', `node-property',
51 ;; `paragraph', `planning', `quote-section', `src-block', `table',
52 ;; `table-row' and `verse-block'. Among them, `paragraph' and
53 ;; `verse-block' types can contain Org objects and plain text.
55 ;; Objects are related to document's contents. Some of them are
56 ;; recursive. Associated types are of the following: `bold', `code',
57 ;; `entity', `export-snippet', `footnote-reference',
58 ;; `inline-babel-call', `inline-src-block', `italic',
59 ;; `latex-fragment', `line-break', `link', `macro', `radio-target',
60 ;; `statistics-cookie', `strike-through', `subscript', `superscript',
61 ;; `table-cell', `target', `timestamp', `underline' and `verbatim'.
63 ;; Some elements also have special properties whose value can hold
64 ;; objects themselves (i.e. an item tag or an headline name). Such
65 ;; values are called "secondary strings". Any object belongs to
66 ;; either an element or a secondary string.
68 ;; Notwithstanding affiliated keywords, each greater element, element
69 ;; and object has a fixed set of properties attached to it. Among
70 ;; them, four are shared by all types: `:begin' and `:end', which
71 ;; refer to the beginning and ending buffer positions of the
72 ;; considered element or object, `:post-blank', which holds the number
73 ;; of blank lines, or white spaces, at its end and `:parent' which
74 ;; refers to the element or object containing it. Greater elements
75 ;; and elements containing objects will also have `:contents-begin'
76 ;; and `:contents-end' properties to delimit contents.
78 ;; Lisp-wise, an element or an object can be represented as a list.
79 ;; It follows the pattern (TYPE PROPERTIES CONTENTS), where:
80 ;; TYPE is a symbol describing the Org element or object.
81 ;; PROPERTIES is the property list attached to it. See docstring of
82 ;; appropriate parsing function to get an exhaustive
84 ;; CONTENTS is a list of elements, objects or raw strings contained
85 ;; in the current element or object, when applicable.
87 ;; An Org buffer is a nested list of such elements and objects, whose
88 ;; type is `org-data' and properties is nil.
90 ;; The first part of this file defines Org syntax, while the second
91 ;; one provide accessors and setters functions.
93 ;; The next part implements a parser and an interpreter for each
94 ;; element and object type in Org syntax.
96 ;; The following part creates a fully recursive buffer parser. It
97 ;; also provides a tool to map a function to elements or objects
98 ;; matching some criteria in the parse tree. Functions of interest
99 ;; are `org-element-parse-buffer', `org-element-map' and, to a lesser
100 ;; extent, `org-element-parse-secondary-string'.
102 ;; The penultimate part is the cradle of an interpreter for the
103 ;; obtained parse tree: `org-element-interpret-data'.
105 ;; The library ends by furnishing `org-element-at-point' function, and
106 ;; a way to give information about document structure around point
107 ;; with `org-element-context'.
112 (eval-when-compile (require 'cl
))
117 ;;; Definitions And Rules
119 ;; Define elements, greater elements and specify recursive objects,
120 ;; along with the affiliated keywords recognized. Also set up
121 ;; restrictions on recursive objects combinations.
123 ;; These variables really act as a control center for the parsing
126 (defconst org-element-paragraph-separate
128 ;; Headlines, inlinetasks.
129 org-outline-regexp
"\\|"
130 ;; Footnote definitions.
131 "\\[\\(?:[0-9]+\\|fn:[-_[:word:]]+\\)\\]" "\\|"
135 ;; Tables (any type).
136 "\\(?:|\\|\\+-[-+]\\)" "\\|"
137 ;; Blocks (any type), Babel calls, drawers (any type),
138 ;; fixed-width areas and keywords. Note: this is only an
139 ;; indication and need some thorough check.
142 "-\\{5,\\}[ \t]*$" "\\|"
143 ;; LaTeX environments.
144 "\\\\begin{\\([A-Za-z0-9]+\\*?\\)}" "\\|"
145 ;; Planning and Clock lines.
146 (regexp-opt (list org-scheduled-string
152 (let ((term (case org-plain-list-ordered-item-terminator
153 (?\
) ")") (?.
"\\.") (otherwise "[.)]")))
154 (alpha (and org-alphabetical-lists
"\\|[A-Za-z]")))
155 (concat "\\(?:[-+*]\\|\\(?:[0-9]+" alpha
"\\)" term
"\\)"
156 "\\(?:[ \t]\\|$\\)"))
158 "Regexp to separate paragraphs in an Org buffer.
159 In the case of lines starting with \"#\" and \":\", this regexp
160 is not sufficient to know if point is at a paragraph ending. See
161 `org-element-paragraph-parser' for more information.")
163 (defconst org-element-all-elements
164 '(babel-call center-block clock comment comment-block drawer dynamic-block
165 example-block export-block fixed-width footnote-definition
166 headline horizontal-rule inlinetask item keyword
167 latex-environment node-property paragraph plain-list planning
168 property-drawer quote-block quote-section section special-block
169 src-block table table-row verse-block
)
170 "Complete list of element types.")
172 (defconst org-element-greater-elements
173 '(center-block drawer dynamic-block footnote-definition headline inlinetask
174 item plain-list property-drawer quote-block section
176 "List of recursive element types aka Greater Elements.")
178 (defconst org-element-all-successors
179 '(export-snippet footnote-reference inline-babel-call inline-src-block
180 latex-or-entity line-break link macro radio-target
181 statistics-cookie sub
/superscript table-cell target
182 text-markup timestamp
)
183 "Complete list of successors.")
185 (defconst org-element-object-successor-alist
186 '((subscript . sub
/superscript
) (superscript . sub
/superscript
)
187 (bold . text-markup
) (code . text-markup
) (italic . text-markup
)
188 (strike-through . text-markup
) (underline . text-markup
)
189 (verbatim . text-markup
) (entity . latex-or-entity
)
190 (latex-fragment . latex-or-entity
))
191 "Alist of translations between object type and successor name.
193 Sharing the same successor comes handy when, for example, the
194 regexp matching one object can also match the other object.")
196 (defconst org-element-all-objects
197 '(bold code entity export-snippet footnote-reference inline-babel-call
198 inline-src-block italic line-break latex-fragment link macro
199 radio-target statistics-cookie strike-through subscript superscript
200 table-cell target timestamp underline verbatim
)
201 "Complete list of object types.")
203 (defconst org-element-recursive-objects
204 '(bold italic link subscript radio-target strike-through superscript
205 table-cell underline
)
206 "List of recursive object types.")
208 (defconst org-element-block-name-alist
209 '(("CENTER" . org-element-center-block-parser
)
210 ("COMMENT" . org-element-comment-block-parser
)
211 ("EXAMPLE" . org-element-example-block-parser
)
212 ("QUOTE" . org-element-quote-block-parser
)
213 ("SRC" . org-element-src-block-parser
)
214 ("VERSE" . org-element-verse-block-parser
))
215 "Alist between block names and the associated parsing function.
216 Names must be uppercase. Any block whose name has no association
217 is parsed with `org-element-special-block-parser'.")
219 (defconst org-element-link-type-is-file
220 '("file" "file+emacs" "file+sys" "docview")
221 "List of link types equivalent to \"file\".
222 Only these types can accept search options and an explicit
223 application to open them.")
225 (defconst org-element-affiliated-keywords
226 '("CAPTION" "DATA" "HEADER" "HEADERS" "LABEL" "NAME" "PLOT" "RESNAME" "RESULT"
227 "RESULTS" "SOURCE" "SRCNAME" "TBLNAME")
228 "List of affiliated keywords as strings.
229 By default, all keywords setting attributes (i.e. \"ATTR_LATEX\")
230 are affiliated keywords and need not to be in this list.")
232 (defconst org-element--affiliated-re
233 (format "[ \t]*#\\+%s:"
234 ;; Regular affiliated keywords.
235 (format "\\(%s\\|ATTR_[-_A-Za-z0-9]+\\)\\(?:\\[\\(.*\\)\\]\\)?"
236 (regexp-opt org-element-affiliated-keywords
)))
237 "Regexp matching any affiliated keyword.
239 Keyword name is put in match group 1. Moreover, if keyword
240 belongs to `org-element-dual-keywords', put the dual value in
243 Don't modify it, set `org-element-affiliated-keywords' instead.")
245 (defconst org-element-keyword-translation-alist
246 '(("DATA" .
"NAME") ("LABEL" .
"NAME") ("RESNAME" .
"NAME")
247 ("SOURCE" .
"NAME") ("SRCNAME" .
"NAME") ("TBLNAME" .
"NAME")
248 ("RESULT" .
"RESULTS") ("HEADERS" .
"HEADER"))
249 "Alist of usual translations for keywords.
250 The key is the old name and the value the new one. The property
251 holding their value will be named after the translated name.")
253 (defconst org-element-multiple-keywords
'("CAPTION" "HEADER")
254 "List of affiliated keywords that can occur more than once in an element.
256 Their value will be consed into a list of strings, which will be
257 returned as the value of the property.
259 This list is checked after translations have been applied. See
260 `org-element-keyword-translation-alist'.
262 By default, all keywords setting attributes (i.e. \"ATTR_LATEX\")
263 allow multiple occurrences and need not to be in this list.")
265 (defconst org-element-parsed-keywords
'("CAPTION")
266 "List of affiliated keywords whose value can be parsed.
268 Their value will be stored as a secondary string: a list of
271 This list is checked after translations have been applied. See
272 `org-element-keyword-translation-alist'.")
274 (defconst org-element-dual-keywords
'("CAPTION" "RESULTS")
275 "List of affiliated keywords which can have a secondary value.
277 In Org syntax, they can be written with optional square brackets
278 before the colons. For example, RESULTS keyword can be
279 associated to a hash value with the following:
281 #+RESULTS[hash-string]: some-source
283 This list is checked after translations have been applied. See
284 `org-element-keyword-translation-alist'.")
286 (defconst org-element-document-properties
'("AUTHOR" "DATE" "TITLE")
287 "List of properties associated to the whole document.
288 Any keyword in this list will have its value parsed and stored as
289 a secondary string.")
291 (defconst org-element-object-restrictions
292 '((bold export-snippet inline-babel-call inline-src-block latex-or-entity link
293 radio-target sub
/superscript target text-markup timestamp
)
294 (footnote-reference export-snippet footnote-reference inline-babel-call
295 inline-src-block latex-or-entity line-break link macro
296 radio-target sub
/superscript target text-markup
298 (headline inline-babel-call inline-src-block latex-or-entity link macro
299 radio-target statistics-cookie sub
/superscript target text-markup
301 (inlinetask inline-babel-call inline-src-block latex-or-entity link macro
302 radio-target sub
/superscript target text-markup timestamp
)
303 (italic export-snippet inline-babel-call inline-src-block latex-or-entity
304 link radio-target sub
/superscript target text-markup timestamp
)
305 (item export-snippet footnote-reference inline-babel-call latex-or-entity
306 link macro radio-target sub
/superscript target text-markup
)
307 (keyword inline-babel-call inline-src-block latex-or-entity link macro
308 sub
/superscript text-markup
)
309 (link export-snippet inline-babel-call inline-src-block latex-or-entity link
310 sub
/superscript text-markup
)
311 (paragraph export-snippet footnote-reference inline-babel-call
312 inline-src-block latex-or-entity line-break link macro
313 radio-target statistics-cookie sub
/superscript target text-markup
315 (radio-target export-snippet latex-or-entity sub
/superscript
)
316 (strike-through export-snippet inline-babel-call inline-src-block
317 latex-or-entity link radio-target sub
/superscript target
318 text-markup timestamp
)
319 (subscript export-snippet inline-babel-call inline-src-block latex-or-entity
320 sub
/superscript target text-markup
)
321 (superscript export-snippet inline-babel-call inline-src-block
322 latex-or-entity sub
/superscript target text-markup
)
323 (table-cell export-snippet latex-or-entity link macro radio-target
324 sub
/superscript target text-markup timestamp
)
325 (table-row table-cell
)
326 (underline export-snippet inline-babel-call inline-src-block latex-or-entity
327 link radio-target sub
/superscript target text-markup timestamp
)
328 (verse-block footnote-reference inline-babel-call inline-src-block
329 latex-or-entity line-break link macro radio-target
330 sub
/superscript target text-markup timestamp
))
331 "Alist of objects restrictions.
333 CAR is an element or object type containing objects and CDR is
334 a list of successors that will be called within an element or
337 For example, in a `radio-target' object, one can only find
338 entities, export snippets, latex-fragments, subscript and
341 This alist also applies to secondary string. For example, an
342 `headline' type element doesn't directly contain objects, but
343 still has an entry since one of its properties (`:title') does.")
345 (defconst org-element-secondary-value-alist
346 '((headline .
:title
)
347 (inlinetask .
:title
)
349 (footnote-reference .
:inline-definition
))
350 "Alist between element types and location of secondary value.")
354 ;;; Accessors and Setters
356 ;; Provide four accessors: `org-element-type', `org-element-property'
357 ;; `org-element-contents' and `org-element-restriction'.
359 ;; Setter functions allow to modify elements by side effect. There is
360 ;; `org-element-put-property', `org-element-set-contents',
361 ;; `org-element-set-element' and `org-element-adopt-element'. Note
362 ;; that `org-element-set-element' and `org-element-adopt-elements' are
363 ;; higher level functions since also update `:parent' property.
365 (defsubst org-element-type
(element)
366 "Return type of ELEMENT.
368 The function returns the type of the element or object provided.
369 It can also return the following special value:
370 `plain-text' for a string
371 `org-data' for a complete document
372 nil in any other case."
374 ((not (consp element
)) (and (stringp element
) 'plain-text
))
375 ((symbolp (car element
)) (car element
))))
377 (defsubst org-element-property
(property element
)
378 "Extract the value from the PROPERTY of an ELEMENT."
379 (plist-get (nth 1 element
) property
))
381 (defsubst org-element-contents
(element)
382 "Extract contents from an ELEMENT."
383 (and (consp element
) (nthcdr 2 element
)))
385 (defsubst org-element-restriction
(element)
386 "Return restriction associated to ELEMENT.
387 ELEMENT can be an element, an object or a symbol representing an
388 element or object type."
389 (cdr (assq (if (symbolp element
) element
(org-element-type element
))
390 org-element-object-restrictions
)))
392 (defsubst org-element-put-property
(element property value
)
393 "In ELEMENT set PROPERTY to VALUE.
394 Return modified element."
395 (when (consp element
)
396 (setcar (cdr element
) (plist-put (nth 1 element
) property value
)))
399 (defsubst org-element-set-contents
(element &rest contents
)
400 "Set ELEMENT contents to CONTENTS.
401 Return modified element."
402 (cond ((not element
) (list contents
))
403 ((cdr element
) (setcdr (cdr element
) contents
))
404 (t (nconc element contents
))))
406 (defsubst org-element-set-element
(old new
)
407 "Replace element or object OLD with element or object NEW.
408 The function takes care of setting `:parent' property for NEW."
409 ;; Since OLD is going to be changed into NEW by side-effect, first
410 ;; make sure that every element or object within NEW has OLD as
412 (mapc (lambda (blob) (org-element-put-property blob
:parent old
))
413 (org-element-contents new
))
414 ;; Transfer contents.
415 (apply 'org-element-set-contents old
(org-element-contents new
))
416 ;; Ensure NEW has same parent as OLD, then overwrite OLD properties
418 (org-element-put-property new
:parent
(org-element-property :parent old
))
419 (setcar (cdr old
) (nth 1 new
))
421 (setcar old
(car new
)))
423 (defsubst org-element-adopt-elements
(parent &rest children
)
424 "Append elements to the contents of another element.
426 PARENT is an element or object. CHILDREN can be elements,
427 objects, or a strings.
429 The function takes care of setting `:parent' property for CHILD.
430 Return parent element."
431 (if (not parent
) children
432 ;; Link every child to PARENT.
433 (mapc (lambda (child)
434 (unless (stringp child
)
435 (org-element-put-property child
:parent parent
)))
437 ;; Add CHILDREN at the end of PARENT contents.
438 (apply 'org-element-set-contents
440 (nconc (org-element-contents parent
) children
))
441 ;; Return modified PARENT element.
448 ;; For each greater element type, we define a parser and an
451 ;; A parser returns the element or object as the list described above.
452 ;; Most of them accepts no argument. Though, exceptions exist. Hence
453 ;; every element containing a secondary string (see
454 ;; `org-element-secondary-value-alist') will accept an optional
455 ;; argument to toggle parsing of that secondary string. Moreover,
456 ;; `item' parser requires current list's structure as its first
459 ;; An interpreter accepts two arguments: the list representation of
460 ;; the element or object, and its contents. The latter may be nil,
461 ;; depending on the element or object considered. It returns the
462 ;; appropriate Org syntax, as a string.
464 ;; Parsing functions must follow the naming convention:
465 ;; org-element-TYPE-parser, where TYPE is greater element's type, as
466 ;; defined in `org-element-greater-elements'.
468 ;; Similarly, interpreting functions must follow the naming
469 ;; convention: org-element-TYPE-interpreter.
471 ;; With the exception of `headline' and `item' types, greater elements
472 ;; cannot contain other greater elements of their own type.
474 ;; Beside implementing a parser and an interpreter, adding a new
475 ;; greater element requires to tweak `org-element--current-element'.
476 ;; Moreover, the newly defined type must be added to both
477 ;; `org-element-all-elements' and `org-element-greater-elements'.
482 (defun org-element-center-block-parser (limit affiliated
)
483 "Parse a center block.
485 LIMIT bounds the search. AFFILIATED is a list of which CAR is
486 the buffer position at the beginning of the first affiliated
487 keyword and CDR is a plist of affiliated keywords along with
490 Return a list whose CAR is `center-block' and CDR is a plist
491 containing `:begin', `:end', `:hiddenp', `:contents-begin',
492 `:contents-end' and `:post-blank' keywords.
494 Assume point is at the beginning of the block."
495 (let ((case-fold-search t
))
496 (if (not (save-excursion
497 (re-search-forward "^[ \t]*#\\+END_CENTER[ \t]*$" limit t
)))
498 ;; Incomplete block: parse it as a paragraph.
499 (org-element-paragraph-parser limit affiliated
)
500 (let ((block-end-line (match-beginning 0)))
501 (let* ((begin (car affiliated
))
502 ;; Empty blocks have no contents.
503 (contents-begin (progn (forward-line)
504 (and (< (point) block-end-line
)
506 (contents-end (and contents-begin block-end-line
))
507 (hidden (org-invisible-p2))
508 (pos-before-blank (progn (goto-char block-end-line
)
511 (end (save-excursion (skip-chars-forward " \r\t\n" limit
)
512 (if (eobp) (point) (point-at-bol)))))
518 :contents-begin contents-begin
519 :contents-end contents-end
520 :post-blank
(count-lines pos-before-blank end
))
521 (cdr affiliated
))))))))
523 (defun org-element-center-block-interpreter (center-block contents
)
524 "Interpret CENTER-BLOCK element as Org syntax.
525 CONTENTS is the contents of the element."
526 (format "#+BEGIN_CENTER\n%s#+END_CENTER" contents
))
531 (defun org-element-drawer-parser (limit affiliated
)
534 LIMIT bounds the search. AFFILIATED is a list of which CAR is
535 the buffer position at the beginning of the first affiliated
536 keyword and CDR is a plist of affiliated keywords along with
539 Return a list whose CAR is `drawer' and CDR is a plist containing
540 `:drawer-name', `:begin', `:end', `:hiddenp', `:contents-begin',
541 `:contents-end' and `:post-blank' keywords.
543 Assume point is at beginning of drawer."
544 (let ((case-fold-search t
))
545 (if (not (save-excursion (re-search-forward "^[ \t]*:END:[ \t]*$" limit t
)))
546 ;; Incomplete drawer: parse it as a paragraph.
547 (org-element-paragraph-parser limit affiliated
)
549 (let* ((drawer-end-line (match-beginning 0))
550 (name (progn (looking-at org-drawer-regexp
)
551 (org-match-string-no-properties 1)))
552 (begin (car affiliated
))
553 ;; Empty drawers have no contents.
554 (contents-begin (progn (forward-line)
555 (and (< (point) drawer-end-line
)
557 (contents-end (and contents-begin drawer-end-line
))
558 (hidden (org-invisible-p2))
559 (pos-before-blank (progn (goto-char drawer-end-line
)
562 (end (progn (skip-chars-forward " \r\t\n" limit
)
563 (if (eobp) (point) (point-at-bol)))))
570 :contents-begin contents-begin
571 :contents-end contents-end
572 :post-blank
(count-lines pos-before-blank end
))
573 (cdr affiliated
))))))))
575 (defun org-element-drawer-interpreter (drawer contents
)
576 "Interpret DRAWER element as Org syntax.
577 CONTENTS is the contents of the element."
578 (format ":%s:\n%s:END:"
579 (org-element-property :drawer-name drawer
)
585 (defun org-element-dynamic-block-parser (limit affiliated
)
586 "Parse a dynamic block.
588 LIMIT bounds the search. AFFILIATED is a list of which CAR is
589 the buffer position at the beginning of the first affiliated
590 keyword and CDR is a plist of affiliated keywords along with
593 Return a list whose CAR is `dynamic-block' and CDR is a plist
594 containing `:block-name', `:begin', `:end', `:hiddenp',
595 `:contents-begin', `:contents-end', `:arguments' and
596 `:post-blank' keywords.
598 Assume point is at beginning of dynamic block."
599 (let ((case-fold-search t
))
600 (if (not (save-excursion
601 (re-search-forward "^[ \t]*#\\+END:?[ \t]*$" limit t
)))
602 ;; Incomplete block: parse it as a paragraph.
603 (org-element-paragraph-parser limit affiliated
)
604 (let ((block-end-line (match-beginning 0)))
606 (let* ((name (progn (looking-at org-dblock-start-re
)
607 (org-match-string-no-properties 1)))
608 (arguments (org-match-string-no-properties 3))
609 (begin (car affiliated
))
610 ;; Empty blocks have no contents.
611 (contents-begin (progn (forward-line)
612 (and (< (point) block-end-line
)
614 (contents-end (and contents-begin block-end-line
))
615 (hidden (org-invisible-p2))
616 (pos-before-blank (progn (goto-char block-end-line
)
619 (end (progn (skip-chars-forward " \r\t\n" limit
)
620 (if (eobp) (point) (point-at-bol)))))
628 :contents-begin contents-begin
629 :contents-end contents-end
630 :post-blank
(count-lines pos-before-blank end
))
631 (cdr affiliated
)))))))))
633 (defun org-element-dynamic-block-interpreter (dynamic-block contents
)
634 "Interpret DYNAMIC-BLOCK element as Org syntax.
635 CONTENTS is the contents of the element."
636 (format "#+BEGIN: %s%s\n%s#+END:"
637 (org-element-property :block-name dynamic-block
)
638 (let ((args (org-element-property :arguments dynamic-block
)))
639 (and args
(concat " " args
)))
643 ;;;; Footnote Definition
645 (defun org-element-footnote-definition-parser (limit affiliated
)
646 "Parse a footnote definition.
648 LIMIT bounds the search. AFFILIATED is a list of which CAR is
649 the buffer position at the beginning of the first affiliated
650 keyword and CDR is a plist of affiliated keywords along with
653 Return a list whose CAR is `footnote-definition' and CDR is
654 a plist containing `:label', `:begin' `:end', `:contents-begin',
655 `:contents-end' and `:post-blank' keywords.
657 Assume point is at the beginning of the footnote definition."
659 (let* ((label (progn (looking-at org-footnote-definition-re
)
660 (org-match-string-no-properties 1)))
661 (begin (car affiliated
))
662 (ending (save-excursion
666 (concat org-outline-regexp-bol
"\\|"
667 org-footnote-definition-re
"\\|"
668 "^[ \t]*$") limit
'move
))
671 (contents-begin (progn (search-forward "]")
672 (skip-chars-forward " \r\t\n" ending
)
673 (and (/= (point) ending
) (point))))
674 (contents-end (and contents-begin ending
))
675 (end (progn (goto-char ending
)
676 (skip-chars-forward " \r\t\n" limit
)
677 (if (eobp) (point) (point-at-bol)))))
678 (list 'footnote-definition
683 :contents-begin contents-begin
684 :contents-end contents-end
685 :post-blank
(count-lines ending end
))
686 (cdr affiliated
))))))
688 (defun org-element-footnote-definition-interpreter (footnote-definition contents
)
689 "Interpret FOOTNOTE-DEFINITION element as Org syntax.
690 CONTENTS is the contents of the footnote-definition."
691 (concat (format "[%s]" (org-element-property :label footnote-definition
))
698 (defun org-element-headline-parser (limit &optional raw-secondary-p
)
701 Return a list whose CAR is `headline' and CDR is a plist
702 containing `:raw-value', `:title', `:begin', `:end',
703 `:pre-blank', `:hiddenp', `:contents-begin' and `:contents-end',
704 `:level', `:priority', `:tags', `:todo-keyword',`:todo-type',
705 `:scheduled', `:deadline', `:timestamp', `:clock', `:category',
706 `:quotedp', `:archivedp', `:commentedp' and `:footnote-section-p'
709 The plist also contains any property set in the property drawer,
710 with its name in lowercase, the underscores replaced with hyphens
711 and colons at the beginning (i.e. `:custom-id').
713 When RAW-SECONDARY-P is non-nil, headline's title will not be
714 parsed as a secondary string, but as a plain string instead.
716 Assume point is at beginning of the headline."
718 (let* ((components (org-heading-components))
719 (level (nth 1 components
))
720 (todo (nth 2 components
))
722 (and todo
(if (member todo org-done-keywords
) 'done
'todo
)))
723 (tags (let ((raw-tags (nth 5 components
)))
724 (and raw-tags
(org-split-string raw-tags
":"))))
725 (raw-value (or (nth 4 components
) ""))
727 (let ((case-fold-search nil
))
728 (string-match (format "^%s\\( \\|$\\)" org-quote-string
)
731 (let ((case-fold-search nil
))
732 (string-match (format "^%s\\( \\|$\\)" org-comment-string
)
734 (archivedp (member org-archive-tag tags
))
735 (footnote-section-p (and org-footnote-section
736 (string= org-footnote-section raw-value
)))
737 ;; Normalize property names: ":SOME_PROP:" becomes
739 (standard-props (let (plist)
742 (let ((p-name (downcase (car p
))))
743 (while (string-match "_" p-name
)
745 (replace-match "-" nil nil p-name
)))
746 (setq p-name
(intern (concat ":" p-name
)))
748 (plist-put plist p-name
(cdr p
)))))
749 (org-entry-properties nil
'standard
))
751 (time-props (org-entry-properties nil
'special
"CLOCK"))
752 (scheduled (cdr (assoc "SCHEDULED" time-props
)))
753 (deadline (cdr (assoc "DEADLINE" time-props
)))
754 (clock (cdr (assoc "CLOCK" time-props
)))
755 (timestamp (cdr (assoc "TIMESTAMP" time-props
)))
757 (end (save-excursion (goto-char (org-end-of-subtree t t
))))
758 (pos-after-head (progn (forward-line) (point)))
759 (contents-begin (save-excursion
760 (skip-chars-forward " \r\t\n" end
)
761 (and (/= (point) end
) (line-beginning-position))))
762 (hidden (org-invisible-p2))
763 (contents-end (and contents-begin
764 (progn (goto-char end
)
765 (skip-chars-backward " \r\t\n")
768 ;; Clean RAW-VALUE from any quote or comment string.
769 (when (or quotedp commentedp
)
770 (let ((case-fold-search nil
))
772 (replace-regexp-in-string
774 (regexp-opt (list org-quote-string org-comment-string
))
778 ;; Clean TAGS from archive tag, if any.
779 (when archivedp
(setq tags
(delete org-archive-tag tags
)))
783 (list :raw-value raw-value
787 (if (not contents-begin
) 0
788 (count-lines pos-after-head contents-begin
))
790 :contents-begin contents-begin
791 :contents-end contents-end
793 :priority
(nth 3 components
)
801 :post-blank
(count-lines
802 (if (not contents-end
) pos-after-head
803 (goto-char contents-end
)
807 :footnote-section-p footnote-section-p
809 :commentedp commentedp
812 (org-element-put-property
814 (if raw-secondary-p raw-value
815 (org-element-parse-secondary-string
816 raw-value
(org-element-restriction 'headline
) headline
)))))))
818 (defun org-element-headline-interpreter (headline contents
)
819 "Interpret HEADLINE element as Org syntax.
820 CONTENTS is the contents of the element."
821 (let* ((level (org-element-property :level headline
))
822 (todo (org-element-property :todo-keyword headline
))
823 (priority (org-element-property :priority headline
))
824 (title (org-element-interpret-data
825 (org-element-property :title headline
)))
826 (tags (let ((tag-list (if (org-element-property :archivedp headline
)
827 (cons org-archive-tag
828 (org-element-property :tags headline
))
829 (org-element-property :tags headline
))))
831 (format ":%s:" (mapconcat 'identity tag-list
":")))))
832 (commentedp (org-element-property :commentedp headline
))
833 (quotedp (org-element-property :quotedp headline
))
834 (pre-blank (or (org-element-property :pre-blank headline
) 0))
835 (heading (concat (make-string level ?
*)
836 (and todo
(concat " " todo
))
837 (and quotedp
(concat " " org-quote-string
))
838 (and commentedp
(concat " " org-comment-string
))
840 (format " [#%s]" (char-to-string priority
)))
841 (cond ((and org-footnote-section
842 (org-element-property
843 :footnote-section-p headline
))
844 (concat " " org-footnote-section
))
845 (title (concat " " title
))))))
850 ((zerop org-tags-column
) (format " %s" tags
))
851 ((< org-tags-column
0)
854 (max (- (+ org-tags-column
(length heading
) (length tags
))) 1)
859 (make-string (max (- org-tags-column
(length heading
)) 1) ?
)
861 (make-string (1+ pre-blank
) 10)
867 (defun org-element-inlinetask-parser (limit &optional raw-secondary-p
)
868 "Parse an inline task.
870 Return a list whose CAR is `inlinetask' and CDR is a plist
871 containing `:title', `:begin', `:end', `:hiddenp',
872 `:contents-begin' and `:contents-end', `:level', `:priority',
873 `:raw-value', `:tags', `:todo-keyword', `:todo-type',
874 `:scheduled', `:deadline', `:timestamp', `:clock' and
875 `:post-blank' keywords.
877 The plist also contains any property set in the property drawer,
878 with its name in lowercase, the underscores replaced with hyphens
879 and colons at the beginning (i.e. `:custom-id').
881 When optional argument RAW-SECONDARY-P is non-nil, inline-task's
882 title will not be parsed as a secondary string, but as a plain
885 Assume point is at beginning of the inline task."
887 (let* ((keywords (org-element--collect-affiliated-keywords))
888 (begin (car keywords
))
889 (components (org-heading-components))
890 (todo (nth 2 components
))
892 (if (member todo org-done-keywords
) 'done
'todo
)))
893 (tags (let ((raw-tags (nth 5 components
)))
894 (and raw-tags
(org-split-string raw-tags
":"))))
895 (raw-value (or (nth 4 components
) ""))
896 ;; Normalize property names: ":SOME_PROP:" becomes
898 (standard-props (let (plist)
901 (let ((p-name (downcase (car p
))))
902 (while (string-match "_" p-name
)
904 (replace-match "-" nil nil p-name
)))
905 (setq p-name
(intern (concat ":" p-name
)))
907 (plist-put plist p-name
(cdr p
)))))
908 (org-entry-properties nil
'standard
))
910 (time-props (org-entry-properties nil
'special
"CLOCK"))
911 (scheduled (cdr (assoc "SCHEDULED" time-props
)))
912 (deadline (cdr (assoc "DEADLINE" time-props
)))
913 (clock (cdr (assoc "CLOCK" time-props
)))
914 (timestamp (cdr (assoc "TIMESTAMP" time-props
)))
915 (task-end (save-excursion
917 (and (re-search-forward "^\\*+ END" limit t
)
918 (match-beginning 0))))
919 (contents-begin (progn (forward-line)
920 (and task-end
(< (point) task-end
) (point))))
921 (hidden (and contents-begin
(org-invisible-p2)))
922 (contents-end (and contents-begin task-end
))
923 (before-blank (if (not task-end
) (point)
927 (end (progn (skip-chars-forward " \r\t\n" limit
)
928 (if (eobp) (point) (point-at-bol))))
932 (list :raw-value raw-value
936 :contents-begin contents-begin
937 :contents-end contents-end
938 :level
(nth 1 components
)
939 :priority
(nth 3 components
)
947 :post-blank
(count-lines before-blank end
))
950 (org-element-put-property
952 (if raw-secondary-p raw-value
953 (org-element-parse-secondary-string
955 (org-element-restriction 'inlinetask
)
958 (defun org-element-inlinetask-interpreter (inlinetask contents
)
959 "Interpret INLINETASK element as Org syntax.
960 CONTENTS is the contents of inlinetask."
961 (let* ((level (org-element-property :level inlinetask
))
962 (todo (org-element-property :todo-keyword inlinetask
))
963 (priority (org-element-property :priority inlinetask
))
964 (title (org-element-interpret-data
965 (org-element-property :title inlinetask
)))
966 (tags (let ((tag-list (org-element-property :tags inlinetask
)))
968 (format ":%s:" (mapconcat 'identity tag-list
":")))))
969 (task (concat (make-string level ?
*)
970 (and todo
(concat " " todo
))
972 (format " [#%s]" (char-to-string priority
)))
973 (and title
(concat " " title
)))))
978 ((zerop org-tags-column
) (format " %s" tags
))
979 ((< org-tags-column
0)
982 (max (- (+ org-tags-column
(length task
) (length tags
))) 1)
987 (make-string (max (- org-tags-column
(length task
)) 1) ?
)
989 ;; Prefer degenerate inlinetasks when there are no
994 (make-string level ?
*) " END")))))
999 (defun org-element-item-parser (limit struct
&optional raw-secondary-p
)
1002 STRUCT is the structure of the plain list.
1004 Return a list whose CAR is `item' and CDR is a plist containing
1005 `:bullet', `:begin', `:end', `:contents-begin', `:contents-end',
1006 `:checkbox', `:counter', `:tag', `:structure', `:hiddenp' and
1007 `:post-blank' keywords.
1009 When optional argument RAW-SECONDARY-P is non-nil, item's tag, if
1010 any, will not be parsed as a secondary string, but as a plain
1013 Assume point is at the beginning of the item."
1016 (looking-at org-list-full-item-re
)
1017 (let* ((begin (point))
1018 (bullet (org-match-string-no-properties 1))
1019 (checkbox (let ((box (org-match-string-no-properties 3)))
1020 (cond ((equal "[ ]" box
) 'off
)
1021 ((equal "[X]" box
) 'on
)
1022 ((equal "[-]" box
) 'trans
))))
1023 (counter (let ((c (org-match-string-no-properties 2)))
1027 ((string-match "[A-Za-z]" c
)
1028 (- (string-to-char (upcase (match-string 0 c
)))
1030 ((string-match "[0-9]+" c
)
1031 (string-to-number (match-string 0 c
)))))))
1032 (end (save-excursion (goto-char (org-list-get-item-end begin struct
))
1033 (unless (bolp) (forward-line))
1037 ;; Ignore tags in un-ordered lists: they are just
1038 ;; a part of item's body.
1039 (if (and (match-beginning 4)
1040 (save-match-data (string-match "[.)]" bullet
)))
1043 (skip-chars-forward " \r\t\n" limit
)
1044 ;; If first line isn't empty, contents really start
1045 ;; at the text after item's meta-data.
1046 (if (= (point-at-bol) begin
) (point) (point-at-bol))))
1047 (hidden (progn (forward-line)
1048 (and (not (= (point) end
)) (org-invisible-p2))))
1049 (contents-end (progn (goto-char end
)
1050 (skip-chars-backward " \r\t\n")
1055 (list :bullet bullet
1058 ;; CONTENTS-BEGIN and CONTENTS-END may be
1059 ;; mixed up in the case of an empty item
1060 ;; separated from the next by a blank line.
1061 ;; Thus ensure the former is always the
1063 :contents-begin
(min contents-begin contents-end
)
1064 :contents-end
(max contents-begin contents-end
)
1069 :post-blank
(count-lines contents-end end
)))))
1070 (org-element-put-property
1072 (let ((raw-tag (org-list-get-tag begin struct
)))
1074 (if raw-secondary-p raw-tag
1075 (org-element-parse-secondary-string
1076 raw-tag
(org-element-restriction 'item
) item
))))))))
1078 (defun org-element-item-interpreter (item contents
)
1079 "Interpret ITEM element as Org syntax.
1080 CONTENTS is the contents of the element."
1081 (let* ((bullet (org-list-bullet-string (org-element-property :bullet item
)))
1082 (checkbox (org-element-property :checkbox item
))
1083 (counter (org-element-property :counter item
))
1084 (tag (let ((tag (org-element-property :tag item
)))
1085 (and tag
(org-element-interpret-data tag
))))
1086 ;; Compute indentation.
1087 (ind (make-string (length bullet
) 32))
1088 (item-starts-with-par-p
1089 (eq (org-element-type (car (org-element-contents item
)))
1094 (and counter
(format "[@%d] " counter
))
1099 (and tag
(format "%s :: " tag
))
1100 (let ((contents (replace-regexp-in-string
1101 "\\(^\\)[ \t]*\\S-" ind contents nil nil
1)))
1102 (if item-starts-with-par-p
(org-trim contents
)
1103 (concat "\n" contents
))))))
1108 (defun org-element-plain-list-parser (limit affiliated structure
)
1109 "Parse a plain list.
1111 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1112 the buffer position at the beginning of the first affiliated
1113 keyword and CDR is a plist of affiliated keywords along with
1114 their value. STRUCTURE is the structure of the plain list being
1117 Return a list whose CAR is `plain-list' and CDR is a plist
1118 containing `:type', `:begin', `:end', `:contents-begin' and
1119 `:contents-end', `:structure' and `:post-blank' keywords.
1121 Assume point is at the beginning of the list."
1123 (let* ((struct (or structure
(org-list-struct)))
1124 (prevs (org-list-prevs-alist struct
))
1125 (parents (org-list-parents-alist struct
))
1126 (type (org-list-get-list-type (point) struct prevs
))
1127 (contents-begin (point))
1128 (begin (car affiliated
))
1130 (progn (goto-char (org-list-get-list-end (point) struct prevs
))
1131 (unless (bolp) (forward-line))
1133 (end (progn (skip-chars-forward " \r\t\n" limit
)
1134 (if (eobp) (point) (point-at-bol)))))
1141 :contents-begin contents-begin
1142 :contents-end contents-end
1144 :post-blank
(count-lines contents-end end
))
1145 (cdr affiliated
))))))
1147 (defun org-element-plain-list-interpreter (plain-list contents
)
1148 "Interpret PLAIN-LIST element as Org syntax.
1149 CONTENTS is the contents of the element."
1152 (goto-char (point-min))
1157 ;;;; Property Drawer
1159 (defun org-element-property-drawer-parser (limit affiliated
)
1160 "Parse a property drawer.
1162 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1163 the buffer position at the beginning of the first affiliated
1164 keyword and CDR is a plist of affiliated keywords along with
1167 Return a list whose CAR is `property-drawer' and CDR is a plist
1168 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1169 `:contents-end' and `:post-blank' keywords.
1171 Assume point is at the beginning of the property drawer."
1173 (let ((case-fold-search t
))
1174 (if (not (save-excursion
1175 (re-search-forward "^[ \t]*:END:[ \t]*$" limit t
)))
1176 ;; Incomplete drawer: parse it as a paragraph.
1177 (org-element-paragraph-parser limit affiliated
)
1179 (let* ((drawer-end-line (match-beginning 0))
1180 (begin (car affiliated
))
1181 (contents-begin (progn (forward-line)
1182 (and (< (point) drawer-end-line
)
1184 (contents-end (and contents-begin drawer-end-line
))
1185 (hidden (org-invisible-p2))
1186 (pos-before-blank (progn (goto-char drawer-end-line
)
1189 (end (progn (skip-chars-forward " \r\t\n" limit
)
1190 (if (eobp) (point) (point-at-bol)))))
1191 (list 'property-drawer
1196 :contents-begin contents-begin
1197 :contents-end contents-end
1198 :post-blank
(count-lines pos-before-blank end
))
1199 (cdr affiliated
)))))))))
1201 (defun org-element-property-drawer-interpreter (property-drawer contents
)
1202 "Interpret PROPERTY-DRAWER element as Org syntax.
1203 CONTENTS is the properties within the drawer."
1204 (format ":PROPERTIES:\n%s:END:" contents
))
1209 (defun org-element-quote-block-parser (limit affiliated
)
1210 "Parse a quote block.
1212 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1213 the buffer position at the beginning of the first affiliated
1214 keyword and CDR is a plist of affiliated keywords along with
1217 Return a list whose CAR is `quote-block' and CDR is a plist
1218 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1219 `:contents-end' and `:post-blank' keywords.
1221 Assume point is at the beginning of the block."
1222 (let ((case-fold-search t
))
1223 (if (not (save-excursion
1224 (re-search-forward "^[ \t]*#\\+END_QUOTE[ \t]*$" limit t
)))
1225 ;; Incomplete block: parse it as a paragraph.
1226 (org-element-paragraph-parser limit affiliated
)
1227 (let ((block-end-line (match-beginning 0)))
1229 (let* ((begin (car affiliated
))
1230 ;; Empty blocks have no contents.
1231 (contents-begin (progn (forward-line)
1232 (and (< (point) block-end-line
)
1234 (contents-end (and contents-begin block-end-line
))
1235 (hidden (org-invisible-p2))
1236 (pos-before-blank (progn (goto-char block-end-line
)
1239 (end (progn (skip-chars-forward " \r\t\n" limit
)
1240 (if (eobp) (point) (point-at-bol)))))
1246 :contents-begin contents-begin
1247 :contents-end contents-end
1248 :post-blank
(count-lines pos-before-blank end
))
1249 (cdr affiliated
)))))))))
1251 (defun org-element-quote-block-interpreter (quote-block contents
)
1252 "Interpret QUOTE-BLOCK element as Org syntax.
1253 CONTENTS is the contents of the element."
1254 (format "#+BEGIN_QUOTE\n%s#+END_QUOTE" contents
))
1259 (defun org-element-section-parser (limit)
1262 LIMIT bounds the search.
1264 Return a list whose CAR is `section' and CDR is a plist
1265 containing `:begin', `:end', `:contents-begin', `contents-end'
1266 and `:post-blank' keywords."
1268 ;; Beginning of section is the beginning of the first non-blank
1269 ;; line after previous headline.
1270 (let ((begin (point))
1271 (end (progn (org-with-limited-levels (outline-next-heading))
1273 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
1279 :contents-begin begin
1280 :contents-end pos-before-blank
1281 :post-blank
(count-lines pos-before-blank end
))))))
1283 (defun org-element-section-interpreter (section contents
)
1284 "Interpret SECTION element as Org syntax.
1285 CONTENTS is the contents of the element."
1291 (defun org-element-special-block-parser (limit affiliated
)
1292 "Parse a special block.
1294 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1295 the buffer position at the beginning of the first affiliated
1296 keyword and CDR is a plist of affiliated keywords along with
1299 Return a list whose CAR is `special-block' and CDR is a plist
1300 containing `:type', `:begin', `:end', `:hiddenp',
1301 `:contents-begin', `:contents-end' and `:post-blank' keywords.
1303 Assume point is at the beginning of the block."
1304 (let* ((case-fold-search t
)
1305 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(S-+\\)")
1306 (upcase (match-string-no-properties 1)))))
1307 (if (not (save-excursion
1309 (format "^[ \t]*#\\+END_%s[ \t]*$" type
) limit t
)))
1310 ;; Incomplete block: parse it as a paragraph.
1311 (org-element-paragraph-parser limit affiliated
)
1312 (let ((block-end-line (match-beginning 0)))
1314 (let* ((begin (car affiliated
))
1315 ;; Empty blocks have no contents.
1316 (contents-begin (progn (forward-line)
1317 (and (< (point) block-end-line
)
1319 (contents-end (and contents-begin block-end-line
))
1320 (hidden (org-invisible-p2))
1321 (pos-before-blank (progn (goto-char block-end-line
)
1324 (end (progn (org-skip-whitespace)
1325 (if (eobp) (point) (point-at-bol)))))
1326 (list 'special-block
1332 :contents-begin contents-begin
1333 :contents-end contents-end
1334 :post-blank
(count-lines pos-before-blank end
))
1335 (cdr affiliated
)))))))))
1337 (defun org-element-special-block-interpreter (special-block contents
)
1338 "Interpret SPECIAL-BLOCK element as Org syntax.
1339 CONTENTS is the contents of the element."
1340 (let ((block-type (org-element-property :type special-block
)))
1341 (format "#+BEGIN_%s\n%s#+END_%s" block-type contents block-type
)))
1347 ;; For each element, a parser and an interpreter are also defined.
1348 ;; Both follow the same naming convention used for greater elements.
1350 ;; Also, as for greater elements, adding a new element type is done
1351 ;; through the following steps: implement a parser and an interpreter,
1352 ;; tweak `org-element--current-element' so that it recognizes the new
1353 ;; type and add that new type to `org-element-all-elements'.
1355 ;; As a special case, when the newly defined type is a block type,
1356 ;; `org-element-block-name-alist' has to be modified accordingly.
1361 (defun org-element-babel-call-parser (limit affiliated
)
1362 "Parse a babel call.
1364 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1365 the buffer position at the beginning of the first affiliated
1366 keyword and CDR is a plist of affiliated keywords along with
1369 Return a list whose CAR is `babel-call' and CDR is a plist
1370 containing `:begin', `:end', `:info' and `:post-blank' as
1373 (let ((case-fold-search t
)
1374 (info (progn (looking-at org-babel-block-lob-one-liner-regexp
)
1375 (org-babel-lob-get-info)))
1376 (begin (car affiliated
))
1377 (pos-before-blank (progn (forward-line) (point)))
1378 (end (progn (skip-chars-forward " \r\t\n" limit
)
1379 (if (eobp) (point) (point-at-bol)))))
1385 :post-blank
(count-lines pos-before-blank end
))
1386 (cdr affiliated
))))))
1388 (defun org-element-babel-call-interpreter (babel-call contents
)
1389 "Interpret BABEL-CALL element as Org syntax.
1391 (let* ((babel-info (org-element-property :info babel-call
))
1392 (main (car babel-info
))
1393 (post-options (nth 1 babel-info
)))
1395 (if (not (string-match "\\[\\(\\[.*?\\]\\)\\]" main
)) main
1396 ;; Remove redundant square brackets.
1397 (replace-match (match-string 1 main
) nil nil main
))
1398 (and post-options
(format "[%s]" post-options
)))))
1403 (defun org-element-clock-parser (limit)
1406 LIMIT bounds the search.
1408 Return a list whose CAR is `clock' and CDR is a plist containing
1409 `:status', `:value', `:time', `:begin', `:end' and `:post-blank'
1412 (let* ((case-fold-search nil
)
1414 (value (progn (search-forward org-clock-string
(line-end-position) t
)
1415 (org-skip-whitespace)
1416 (looking-at "\\[.*\\]")
1417 (org-match-string-no-properties 0)))
1418 (time (and (progn (goto-char (match-end 0))
1419 (looking-at " +=> +\\(\\S-+\\)[ \t]*$"))
1420 (org-match-string-no-properties 1)))
1421 (status (if time
'closed
'running
))
1422 (post-blank (let ((before-blank (progn (forward-line) (point))))
1423 (skip-chars-forward " \r\t\n" limit
)
1424 (unless (eobp) (beginning-of-line))
1425 (count-lines before-blank
(point))))
1428 (list :status status
1433 :post-blank post-blank
)))))
1435 (defun org-element-clock-interpreter (clock contents
)
1436 "Interpret CLOCK element as Org syntax.
1438 (concat org-clock-string
" "
1439 (org-element-property :value clock
)
1440 (let ((time (org-element-property :time clock
)))
1445 (org-split-string time
":")))))))
1450 (defun org-element-comment-parser (limit affiliated
)
1453 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1454 the buffer position at the beginning of the first affiliated
1455 keyword and CDR is a plist of affiliated keywords along with
1458 Return a list whose CAR is `comment' and CDR is a plist
1459 containing `:begin', `:end', `:value' and `:post-blank'
1462 Assume point is at comment beginning."
1464 (let* ((begin (car affiliated
))
1465 (value (prog2 (looking-at "[ \t]*# ?")
1466 (buffer-substring-no-properties
1467 (match-end 0) (line-end-position))
1470 ;; Get comments ending.
1472 (while (and (< (point) limit
) (looking-at "[ \t]*#\\( \\|$\\)"))
1473 ;; Accumulate lines without leading hash and first
1478 (buffer-substring-no-properties
1479 (match-end 0) (line-end-position))))
1482 (end (progn (goto-char com-end
)
1483 (skip-chars-forward " \r\t\n" limit
)
1484 (if (eobp) (point) (point-at-bol)))))
1490 :post-blank
(count-lines com-end end
))
1491 (cdr affiliated
))))))
1493 (defun org-element-comment-interpreter (comment contents
)
1494 "Interpret COMMENT element as Org syntax.
1496 (replace-regexp-in-string "^" "# " (org-element-property :value comment
)))
1501 (defun org-element-comment-block-parser (limit affiliated
)
1502 "Parse an export block.
1504 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1505 the buffer position at the beginning of the first affiliated
1506 keyword and CDR is a plist of affiliated keywords along with
1509 Return a list whose CAR is `comment-block' and CDR is a plist
1510 containing `:begin', `:end', `:hiddenp', `:value' and
1511 `:post-blank' keywords.
1513 Assume point is at comment block beginning."
1514 (let ((case-fold-search t
))
1515 (if (not (save-excursion
1516 (re-search-forward "^[ \t]*#\\+END_COMMENT[ \t]*$" limit t
)))
1517 ;; Incomplete block: parse it as a paragraph.
1518 (org-element-paragraph-parser limit affiliated
)
1519 (let ((contents-end (match-beginning 0)))
1521 (let* ((begin (car affiliated
))
1522 (contents-begin (progn (forward-line) (point)))
1523 (hidden (org-invisible-p2))
1524 (pos-before-blank (progn (goto-char contents-end
)
1527 (end (progn (skip-chars-forward " \r\t\n" limit
)
1528 (if (eobp) (point) (point-at-bol))))
1529 (value (buffer-substring-no-properties
1530 contents-begin contents-end
)))
1531 (list 'comment-block
1537 :post-blank
(count-lines pos-before-blank end
))
1538 (cdr affiliated
)))))))))
1540 (defun org-element-comment-block-interpreter (comment-block contents
)
1541 "Interpret COMMENT-BLOCK element as Org syntax.
1543 (format "#+BEGIN_COMMENT\n%s#+END_COMMENT"
1544 (org-remove-indentation (org-element-property :value comment-block
))))
1549 (defun org-element-example-block-parser (limit affiliated
)
1550 "Parse an example block.
1552 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1553 the buffer position at the beginning of the first affiliated
1554 keyword and CDR is a plist of affiliated keywords along with
1557 Return a list whose CAR is `example-block' and CDR is a plist
1558 containing `:begin', `:end', `:number-lines', `:preserve-indent',
1559 `:retain-labels', `:use-labels', `:label-fmt', `:hiddenp',
1560 `:switches', `:value' and `:post-blank' keywords."
1561 (let ((case-fold-search t
))
1562 (if (not (save-excursion
1563 (re-search-forward "^[ \t]*#\\+END_EXAMPLE[ \t]*$" limit t
)))
1564 ;; Incomplete block: parse it as a paragraph.
1565 (org-element-paragraph-parser limit affiliated
)
1566 (let ((contents-end (match-beginning 0)))
1569 (progn (looking-at "^[ \t]*#\\+BEGIN_EXAMPLE\\(?: +\\(.*\\)\\)?")
1570 (org-match-string-no-properties 1)))
1571 ;; Switches analysis
1572 (number-lines (cond ((not switches
) nil
)
1573 ((string-match "-n\\>" switches
) 'new
)
1574 ((string-match "+n\\>" switches
) 'continued
)))
1575 (preserve-indent (and switches
(string-match "-i\\>" switches
)))
1576 ;; Should labels be retained in (or stripped from) example
1580 (not (string-match "-r\\>" switches
))
1581 (and number-lines
(string-match "-k\\>" switches
))))
1582 ;; What should code-references use - labels or
1586 (and retain-labels
(not (string-match "-k\\>" switches
)))))
1587 (label-fmt (and switches
1588 (string-match "-l +\"\\([^\"\n]+\\)\"" switches
)
1589 (match-string 1 switches
)))
1590 ;; Standard block parsing.
1591 (begin (car affiliated
))
1592 (contents-begin (progn (forward-line) (point)))
1593 (hidden (org-invisible-p2))
1594 (value (org-unescape-code-in-string
1595 (buffer-substring-no-properties
1596 contents-begin contents-end
)))
1597 (pos-before-blank (progn (goto-char contents-end
)
1600 (end (progn (skip-chars-forward " \r\t\n" limit
)
1601 (if (eobp) (point) (point-at-bol)))))
1602 (list 'example-block
1608 :number-lines number-lines
1609 :preserve-indent preserve-indent
1610 :retain-labels retain-labels
1611 :use-labels use-labels
1612 :label-fmt label-fmt
1614 :post-blank
(count-lines pos-before-blank end
))
1615 (cdr affiliated
)))))))))
1617 (defun org-element-example-block-interpreter (example-block contents
)
1618 "Interpret EXAMPLE-BLOCK element as Org syntax.
1620 (let ((switches (org-element-property :switches example-block
)))
1621 (concat "#+BEGIN_EXAMPLE" (and switches
(concat " " switches
)) "\n"
1622 (org-remove-indentation
1623 (org-escape-code-in-string
1624 (org-element-property :value example-block
)))
1630 (defun org-element-export-block-parser (limit affiliated
)
1631 "Parse an export block.
1633 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1634 the buffer position at the beginning of the first affiliated
1635 keyword and CDR is a plist of affiliated keywords along with
1638 Return a list whose CAR is `export-block' and CDR is a plist
1639 containing `:begin', `:end', `:type', `:hiddenp', `:value' and
1640 `:post-blank' keywords.
1642 Assume point is at export-block beginning."
1643 (let* ((case-fold-search t
)
1644 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
1645 (upcase (org-match-string-no-properties 1)))))
1646 (if (not (save-excursion
1648 (format "^[ \t]*#\\+END_%s[ \t]*$" type
) limit t
)))
1649 ;; Incomplete block: parse it as a paragraph.
1650 (org-element-paragraph-parser limit affiliated
)
1651 (let ((contents-end (match-beginning 0)))
1653 (let* ((begin (car affiliated
))
1654 (contents-begin (progn (forward-line) (point)))
1655 (hidden (org-invisible-p2))
1656 (pos-before-blank (progn (goto-char contents-end
)
1659 (end (progn (skip-chars-forward " \r\t\n" limit
)
1660 (if (eobp) (point) (point-at-bol))))
1661 (value (buffer-substring-no-properties contents-begin
1670 :post-blank
(count-lines pos-before-blank end
))
1671 (cdr affiliated
)))))))))
1673 (defun org-element-export-block-interpreter (export-block contents
)
1674 "Interpret EXPORT-BLOCK element as Org syntax.
1676 (let ((type (org-element-property :type export-block
)))
1677 (concat (format "#+BEGIN_%s\n" type
)
1678 (org-element-property :value export-block
)
1679 (format "#+END_%s" type
))))
1684 (defun org-element-fixed-width-parser (limit affiliated
)
1685 "Parse a fixed-width section.
1687 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1688 the buffer position at the beginning of the first affiliated
1689 keyword and CDR is a plist of affiliated keywords along with
1692 Return a list whose CAR is `fixed-width' and CDR is a plist
1693 containing `:begin', `:end', `:value' and `:post-blank' keywords.
1695 Assume point is at the beginning of the fixed-width area."
1697 (let* ((begin (car affiliated
))
1701 (while (and (< (point) limit
)
1702 (looking-at "[ \t]*:\\( \\|$\\)"))
1703 ;; Accumulate text without starting colons.
1706 (buffer-substring-no-properties
1707 (match-end 0) (point-at-eol))
1711 (end (progn (skip-chars-forward " \r\t\n" limit
)
1712 (if (eobp) (point) (point-at-bol)))))
1718 :post-blank
(count-lines end-area end
))
1719 (cdr affiliated
))))))
1721 (defun org-element-fixed-width-interpreter (fixed-width contents
)
1722 "Interpret FIXED-WIDTH element as Org syntax.
1724 (replace-regexp-in-string
1725 "^" ": " (substring (org-element-property :value fixed-width
) 0 -
1)))
1728 ;;;; Horizontal Rule
1730 (defun org-element-horizontal-rule-parser (limit affiliated
)
1731 "Parse an horizontal rule.
1733 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1734 the buffer position at the beginning of the first affiliated
1735 keyword and CDR is a plist of affiliated keywords along with
1738 Return a list whose CAR is `horizontal-rule' and CDR is a plist
1739 containing `:begin', `:end' and `:post-blank' keywords."
1741 (let ((begin (car affiliated
))
1742 (post-hr (progn (forward-line) (point)))
1743 (end (progn (skip-chars-forward " \r\t\n" limit
)
1744 (if (eobp) (point) (point-at-bol)))))
1745 (list 'horizontal-rule
1749 :post-blank
(count-lines post-hr end
))
1750 (cdr affiliated
))))))
1752 (defun org-element-horizontal-rule-interpreter (horizontal-rule contents
)
1753 "Interpret HORIZONTAL-RULE element as Org syntax.
1760 (defun org-element-keyword-parser (limit affiliated
)
1761 "Parse a keyword at point.
1763 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1764 the buffer position at the beginning of the first affiliated
1765 keyword and CDR is a plist of affiliated keywords along with
1768 Return a list whose CAR is `keyword' and CDR is a plist
1769 containing `:key', `:value', `:begin', `:end' and `:post-blank'
1772 (let ((case-fold-search t
)
1773 (begin (car affiliated
))
1774 (key (progn (looking-at "[ \t]*#\\+\\(\\S-+*\\):")
1775 (upcase (org-match-string-no-properties 1))))
1776 (value (org-trim (buffer-substring-no-properties
1777 (match-end 0) (point-at-eol))))
1778 (pos-before-blank (progn (forward-line) (point)))
1779 (end (progn (skip-chars-forward " \r\t\n" limit
)
1780 (if (eobp) (point) (point-at-bol)))))
1787 :post-blank
(count-lines pos-before-blank end
))
1788 (cdr affiliated
))))))
1790 (defun org-element-keyword-interpreter (keyword contents
)
1791 "Interpret KEYWORD element as Org syntax.
1794 (org-element-property :key keyword
)
1795 (org-element-property :value keyword
)))
1798 ;;;; Latex Environment
1800 (defun org-element-latex-environment-parser (limit affiliated
)
1801 "Parse a LaTeX environment.
1803 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1804 the buffer position at the beginning of the first affiliated
1805 keyword and CDR is a plist of affiliated keywords along with
1808 Return a list whose CAR is `latex-environment' and CDR is a plist
1809 containing `:begin', `:end', `:value' and `:post-blank'
1812 Assume point is at the beginning of the latex environment."
1814 (let* ((case-fold-search t
)
1815 (code-begin (point))
1816 (begin (car affiliated
))
1817 (env (progn (looking-at "^[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}")
1818 (regexp-quote (match-string 1))))
1820 (progn (re-search-forward
1821 (format "^[ \t]*\\\\end{%s}[ \t]*$" env
) limit t
)
1824 (value (buffer-substring-no-properties code-begin code-end
))
1825 (end (progn (skip-chars-forward " \r\t\n" limit
)
1826 (if (eobp) (point) (point-at-bol)))))
1827 (list 'latex-environment
1832 :post-blank
(count-lines code-end end
))
1833 (cdr affiliated
))))))
1835 (defun org-element-latex-environment-interpreter (latex-environment contents
)
1836 "Interpret LATEX-ENVIRONMENT element as Org syntax.
1838 (org-element-property :value latex-environment
))
1843 (defun org-element-node-property-parser (limit)
1844 "Parse a node-property at point.
1846 LIMIT bounds the search.
1848 Return a list whose CAR is `node-property' and CDR is a plist
1849 containing `:key', `:value', `:begin', `:end' and `:post-blank'
1852 (let ((case-fold-search t
)
1854 (key (progn (looking-at "[ \t]*:\\(.*?\\):[ \t]+\\(.*?\\)[ \t]*$")
1855 (org-match-string-no-properties 1)))
1856 (value (org-match-string-no-properties 2))
1857 (pos-before-blank (progn (forward-line) (point)))
1858 (end (progn (skip-chars-forward " \r\t\n" limit
)
1859 (if (eobp) (point) (point-at-bol)))))
1860 (list 'node-property
1865 :post-blank
(count-lines pos-before-blank end
))))))
1867 (defun org-element-node-property-interpreter (node-property contents
)
1868 "Interpret NODE-PROPERTY element as Org syntax.
1870 (format org-property-format
1871 (format ":%s:" (org-element-property :key node-property
))
1872 (org-element-property :value node-property
)))
1877 (defun org-element-paragraph-parser (limit affiliated
)
1880 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1881 the buffer position at the beginning of the first affiliated
1882 keyword and CDR is a plist of affiliated keywords along with
1885 Return a list whose CAR is `paragraph' and CDR is a plist
1886 containing `:begin', `:end', `:contents-begin' and
1887 `:contents-end' and `:post-blank' keywords.
1889 Assume point is at the beginning of the paragraph."
1891 (let* ((begin (car affiliated
))
1892 (contents-begin (point))
1894 (let ((case-fold-search t
))
1896 (if (not (re-search-forward
1897 org-element-paragraph-separate limit
'm
))
1899 ;; A matching `org-element-paragraph-separate' is not
1900 ;; necessarily the end of the paragraph. In
1901 ;; particular, lines starting with # or : as a first
1902 ;; non-space character are ambiguous. We have check
1903 ;; if they are valid Org syntax (i.e. not an
1904 ;; incomplete keyword).
1908 ;; There's no ambiguity for other symbols or
1909 ;; empty lines: stop here.
1910 (looking-at "[ \t]*\\(?:[^:#]\\|$\\)")
1911 ;; Stop at valid fixed-width areas.
1912 (looking-at "[ \t]*:\\(?: \\|$\\)")
1914 (and (looking-at org-drawer-regexp
)
1917 "^[ \t]*:END:[ \t]*$" limit t
)))
1918 ;; Stop at valid comments.
1919 (looking-at "[ \t]*#\\(?: \\|$\\)")
1920 ;; Stop at valid dynamic blocks.
1921 (and (looking-at org-dblock-start-re
)
1924 "^[ \t]*#\\+END:?[ \t]*$" limit t
)))
1925 ;; Stop at valid blocks.
1927 "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
1930 (format "^[ \t]*#\\+END_%s[ \t]*$"
1933 ;; Stop at valid latex environments.
1935 "^[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}[ \t]*$")
1938 (format "^[ \t]*\\\\end{%s}[ \t]*$"
1941 ;; Stop at valid keywords.
1942 (looking-at "[ \t]*#\\+\\S-+:")
1943 ;; Skip everything else.
1947 (re-search-forward org-element-paragraph-separate
1949 (beginning-of-line)))
1950 (if (= (point) limit
) limit
1951 (goto-char (line-beginning-position)))))
1952 (contents-end (progn (skip-chars-backward " \r\t\n" contents-begin
)
1955 (end (progn (skip-chars-forward " \r\t\n" limit
)
1956 (if (eobp) (point) (point-at-bol)))))
1961 :contents-begin contents-begin
1962 :contents-end contents-end
1963 :post-blank
(count-lines before-blank end
))
1964 (cdr affiliated
))))))
1966 (defun org-element-paragraph-interpreter (paragraph contents
)
1967 "Interpret PARAGRAPH element as Org syntax.
1968 CONTENTS is the contents of the element."
1974 (defun org-element-planning-parser (limit)
1977 LIMIT bounds the search.
1979 Return a list whose CAR is `planning' and CDR is a plist
1980 containing `:closed', `:deadline', `:scheduled', `:begin', `:end'
1981 and `:post-blank' keywords."
1983 (let* ((case-fold-search nil
)
1985 (post-blank (let ((before-blank (progn (forward-line) (point))))
1986 (skip-chars-forward " \r\t\n" limit
)
1987 (unless (eobp) (beginning-of-line))
1988 (count-lines before-blank
(point))))
1990 closed deadline scheduled
)
1992 (while (re-search-forward org-keyword-time-not-clock-regexp
1993 (line-end-position) t
)
1994 (goto-char (match-end 1))
1995 (org-skip-whitespace)
1996 (let ((time (buffer-substring-no-properties
1997 (1+ (point)) (1- (match-end 0))))
1998 (keyword (match-string 1)))
1999 (cond ((equal keyword org-closed-string
) (setq closed time
))
2000 ((equal keyword org-deadline-string
) (setq deadline time
))
2001 (t (setq scheduled time
)))))
2003 (list :closed closed
2005 :scheduled scheduled
2008 :post-blank post-blank
)))))
2010 (defun org-element-planning-interpreter (planning contents
)
2011 "Interpret PLANNING element as Org syntax.
2016 (list (let ((closed (org-element-property :closed planning
)))
2017 (when closed
(concat org-closed-string
" [" closed
"]")))
2018 (let ((deadline (org-element-property :deadline planning
)))
2019 (when deadline
(concat org-deadline-string
" <" deadline
">")))
2020 (let ((scheduled (org-element-property :scheduled planning
)))
2022 (concat org-scheduled-string
" <" scheduled
">")))))
2028 (defun org-element-quote-section-parser (limit)
2029 "Parse a quote section.
2031 LIMIT bounds the search.
2033 Return a list whose CAR is `quote-section' and CDR is a plist
2034 containing `:begin', `:end', `:value' and `:post-blank' keywords.
2036 Assume point is at beginning of the section."
2038 (let* ((begin (point))
2039 (end (progn (org-with-limited-levels (outline-next-heading))
2041 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
2044 (value (buffer-substring-no-properties begin pos-before-blank
)))
2045 (list 'quote-section
2049 :post-blank
(count-lines pos-before-blank end
))))))
2051 (defun org-element-quote-section-interpreter (quote-section contents
)
2052 "Interpret QUOTE-SECTION element as Org syntax.
2054 (org-element-property :value quote-section
))
2059 (defun org-element-src-block-parser (limit affiliated
)
2062 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2063 the buffer position at the beginning of the first affiliated
2064 keyword and CDR is a plist of affiliated keywords along with
2067 Return a list whose CAR is `src-block' and CDR is a plist
2068 containing `:language', `:switches', `:parameters', `:begin',
2069 `:end', `:hiddenp', `:number-lines', `:retain-labels',
2070 `:use-labels', `:label-fmt', `:preserve-indent', `:value' and
2071 `:post-blank' keywords.
2073 Assume point is at the beginning of the block."
2074 (let ((case-fold-search t
))
2075 (if (not (save-excursion (re-search-forward "^[ \t]*#\\+END_SRC[ \t]*$"
2077 ;; Incomplete block: parse it as a paragraph.
2078 (org-element-paragraph-parser limit affiliated
)
2079 (let ((contents-end (match-beginning 0)))
2081 (let* ((begin (car affiliated
))
2082 ;; Get language as a string.
2086 (concat "^[ \t]*#\\+BEGIN_SRC"
2087 "\\(?: +\\(\\S-+\\)\\)?"
2088 "\\(\\(?: +\\(?:-l \".*?\"\\|[-+][A-Za-z]\\)\\)+\\)?"
2090 (org-match-string-no-properties 1)))
2092 (switches (org-match-string-no-properties 2))
2094 (parameters (org-match-string-no-properties 3))
2095 ;; Switches analysis
2096 (number-lines (cond ((not switches
) nil
)
2097 ((string-match "-n\\>" switches
) 'new
)
2098 ((string-match "+n\\>" switches
) 'continued
)))
2099 (preserve-indent (and switches
(string-match "-i\\>" switches
)))
2100 (label-fmt (and switches
2101 (string-match "-l +\"\\([^\"\n]+\\)\"" switches
)
2102 (match-string 1 switches
)))
2103 ;; Should labels be retained in (or stripped from)
2107 (not (string-match "-r\\>" switches
))
2108 (and number-lines
(string-match "-k\\>" switches
))))
2109 ;; What should code-references use - labels or
2113 (and retain-labels
(not (string-match "-k\\>" switches
)))))
2114 ;; Get visibility status.
2115 (hidden (progn (forward-line) (org-invisible-p2)))
2117 (value (org-unescape-code-in-string
2118 (buffer-substring-no-properties (point) contents-end
)))
2119 (pos-before-blank (progn (goto-char contents-end
)
2122 ;; Get position after ending blank lines.
2123 (end (progn (skip-chars-forward " \r\t\n" limit
)
2124 (if (eobp) (point) (point-at-bol)))))
2127 (list :language language
2128 :switches
(and (org-string-nw-p switches
)
2129 (org-trim switches
))
2130 :parameters
(and (org-string-nw-p parameters
)
2131 (org-trim parameters
))
2134 :number-lines number-lines
2135 :preserve-indent preserve-indent
2136 :retain-labels retain-labels
2137 :use-labels use-labels
2138 :label-fmt label-fmt
2141 :post-blank
(count-lines pos-before-blank end
))
2142 (cdr affiliated
)))))))))
2144 (defun org-element-src-block-interpreter (src-block contents
)
2145 "Interpret SRC-BLOCK element as Org syntax.
2147 (let ((lang (org-element-property :language src-block
))
2148 (switches (org-element-property :switches src-block
))
2149 (params (org-element-property :parameters src-block
))
2150 (value (let ((val (org-element-property :value src-block
)))
2153 (org-src-preserve-indentation val
)
2154 ((zerop org-edit-src-content-indentation
)
2155 (org-remove-indentation val
))
2157 (let ((ind (make-string
2158 org-edit-src-content-indentation
32)))
2159 (replace-regexp-in-string
2160 "\\(^\\)[ \t]*\\S-" ind
2161 (org-remove-indentation val
) nil nil
1)))))))
2162 (concat (format "#+BEGIN_SRC%s\n"
2163 (concat (and lang
(concat " " lang
))
2164 (and switches
(concat " " switches
))
2165 (and params
(concat " " params
))))
2166 (org-escape-code-in-string value
)
2172 (defun org-element-table-parser (limit affiliated
)
2173 "Parse a table at point.
2175 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2176 the buffer position at the beginning of the first affiliated
2177 keyword and CDR is a plist of affiliated keywords along with
2180 Return a list whose CAR is `table' and CDR is a plist containing
2181 `:begin', `:end', `:tblfm', `:type', `:contents-begin',
2182 `:contents-end', `:value' and `:post-blank' keywords.
2184 Assume point is at the beginning of the table."
2186 (let* ((case-fold-search t
)
2187 (table-begin (point))
2188 (type (if (org-at-table.el-p
) 'table.el
'org
))
2189 (keywords (org-element--collect-affiliated-keywords))
2190 (begin (car affiliated
))
2191 (table-end (goto-char (marker-position (org-table-end t
))))
2193 (while (looking-at "[ \t]*#\\+TBLFM: +\\(.*\\)[ \t]*$")
2194 (push (org-match-string-no-properties 1) acc
)
2197 (pos-before-blank (point))
2198 (end (progn (skip-chars-forward " \r\t\n" limit
)
2199 (if (eobp) (point) (point-at-bol)))))
2206 ;; Only `org' tables have contents. `table.el' tables
2207 ;; use a `:value' property to store raw table as
2209 :contents-begin
(and (eq type
'org
) table-begin
)
2210 :contents-end
(and (eq type
'org
) table-end
)
2211 :value
(and (eq type
'table.el
)
2212 (buffer-substring-no-properties
2213 table-begin table-end
))
2214 :post-blank
(count-lines pos-before-blank end
))
2215 (cdr affiliated
))))))
2217 (defun org-element-table-interpreter (table contents
)
2218 "Interpret TABLE element as Org syntax.
2220 (if (eq (org-element-property :type table
) 'table.el
)
2221 (org-remove-indentation (org-element-property :value table
))
2222 (concat (with-temp-buffer (insert contents
)
2225 (mapconcat (lambda (fm) (concat "#+TBLFM: " fm
))
2226 (reverse (org-element-property :tblfm table
))
2232 (defun org-element-table-row-parser (limit)
2233 "Parse table row at point.
2235 LIMIT bounds the search.
2237 Return a list whose CAR is `table-row' and CDR is a plist
2238 containing `:begin', `:end', `:contents-begin', `:contents-end',
2239 `:type' and `:post-blank' keywords."
2241 (let* ((type (if (looking-at "^[ \t]*|-") 'rule
'standard
))
2243 ;; A table rule has no contents. In that case, ensure
2244 ;; CONTENTS-BEGIN matches CONTENTS-END.
2245 (contents-begin (and (eq type
'standard
)
2246 (search-forward "|")
2248 (contents-end (and (eq type
'standard
)
2251 (skip-chars-backward " \t")
2253 (end (progn (forward-line) (point))))
2258 :contents-begin contents-begin
2259 :contents-end contents-end
2262 (defun org-element-table-row-interpreter (table-row contents
)
2263 "Interpret TABLE-ROW element as Org syntax.
2264 CONTENTS is the contents of the table row."
2265 (if (eq (org-element-property :type table-row
) 'rule
) "|-"
2266 (concat "| " contents
)))
2271 (defun org-element-verse-block-parser (limit affiliated
)
2272 "Parse a verse block.
2274 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2275 the buffer position at the beginning of the first affiliated
2276 keyword and CDR is a plist of affiliated keywords along with
2279 Return a list whose CAR is `verse-block' and CDR is a plist
2280 containing `:begin', `:end', `:contents-begin', `:contents-end',
2281 `:hiddenp' and `:post-blank' keywords.
2283 Assume point is at beginning of the block."
2284 (let ((case-fold-search t
))
2285 (if (not (save-excursion
2286 (re-search-forward "^[ \t]*#\\+END_VERSE[ \t]*$" limit t
)))
2287 ;; Incomplete block: parse it as a paragraph.
2288 (org-element-paragraph-parser limit affiliated
)
2289 (let ((contents-end (match-beginning 0)))
2291 (let* ((begin (car affiliated
))
2292 (hidden (progn (forward-line) (org-invisible-p2)))
2293 (contents-begin (point))
2294 (pos-before-blank (progn (goto-char contents-end
)
2297 (end (progn (skip-chars-forward " \r\t\n" limit
)
2298 (if (eobp) (point) (point-at-bol)))))
2303 :contents-begin contents-begin
2304 :contents-end contents-end
2306 :post-blank
(count-lines pos-before-blank end
))
2307 (cdr affiliated
)))))))))
2309 (defun org-element-verse-block-interpreter (verse-block contents
)
2310 "Interpret VERSE-BLOCK element as Org syntax.
2311 CONTENTS is verse block contents."
2312 (format "#+BEGIN_VERSE\n%s#+END_VERSE" contents
))
2318 ;; Unlike to elements, interstices can be found between objects.
2319 ;; That's why, along with the parser, successor functions are provided
2320 ;; for each object. Some objects share the same successor (i.e. `code'
2321 ;; and `verbatim' objects).
2323 ;; A successor must accept a single argument bounding the search. It
2324 ;; will return either a cons cell whose CAR is the object's type, as
2325 ;; a symbol, and CDR the position of its next occurrence, or nil.
2327 ;; Successors follow the naming convention:
2328 ;; org-element-NAME-successor, where NAME is the name of the
2329 ;; successor, as defined in `org-element-all-successors'.
2331 ;; Some object types (i.e. `italic') are recursive. Restrictions on
2332 ;; object types they can contain will be specified in
2333 ;; `org-element-object-restrictions'.
2335 ;; Adding a new type of object is simple. Implement a successor,
2336 ;; a parser, and an interpreter for it, all following the naming
2337 ;; convention. Register type in `org-element-all-objects' and
2338 ;; successor in `org-element-all-successors'. Maybe tweak
2339 ;; restrictions about it, and that's it.
2344 (defun org-element-bold-parser ()
2345 "Parse bold object at point.
2347 Return a list whose CAR is `bold' and CDR is a plist with
2348 `:begin', `:end', `:contents-begin' and `:contents-end' and
2349 `:post-blank' keywords.
2351 Assume point is at the first star marker."
2353 (unless (bolp) (backward-char 1))
2354 (looking-at org-emph-re
)
2355 (let ((begin (match-beginning 2))
2356 (contents-begin (match-beginning 4))
2357 (contents-end (match-end 4))
2358 (post-blank (progn (goto-char (match-end 2))
2359 (skip-chars-forward " \t")))
2364 :contents-begin contents-begin
2365 :contents-end contents-end
2366 :post-blank post-blank
)))))
2368 (defun org-element-bold-interpreter (bold contents
)
2369 "Interpret BOLD object as Org syntax.
2370 CONTENTS is the contents of the object."
2371 (format "*%s*" contents
))
2373 (defun org-element-text-markup-successor (limit)
2374 "Search for the next text-markup object.
2376 LIMIT bounds the search.
2378 Return value is a cons cell whose CAR is a symbol among `bold',
2379 `italic', `underline', `strike-through', `code' and `verbatim'
2380 and CDR is beginning position."
2382 (unless (bolp) (backward-char))
2383 (when (re-search-forward org-emph-re limit t
)
2384 (let ((marker (match-string 3)))
2386 ((equal marker
"*") 'bold
)
2387 ((equal marker
"/") 'italic
)
2388 ((equal marker
"_") 'underline
)
2389 ((equal marker
"+") 'strike-through
)
2390 ((equal marker
"~") 'code
)
2391 ((equal marker
"=") 'verbatim
)
2392 (t (error "Unknown marker at %d" (match-beginning 3))))
2393 (match-beginning 2))))))
2398 (defun org-element-code-parser ()
2399 "Parse code object at point.
2401 Return a list whose CAR is `code' and CDR is a plist with
2402 `:value', `:begin', `:end' and `:post-blank' keywords.
2404 Assume point is at the first tilde marker."
2406 (unless (bolp) (backward-char 1))
2407 (looking-at org-emph-re
)
2408 (let ((begin (match-beginning 2))
2409 (value (org-match-string-no-properties 4))
2410 (post-blank (progn (goto-char (match-end 2))
2411 (skip-chars-forward " \t")))
2417 :post-blank post-blank
)))))
2419 (defun org-element-code-interpreter (code contents
)
2420 "Interpret CODE object as Org syntax.
2422 (format "~%s~" (org-element-property :value code
)))
2427 (defun org-element-entity-parser ()
2428 "Parse entity at point.
2430 Return a list whose CAR is `entity' and CDR a plist with
2431 `:begin', `:end', `:latex', `:latex-math-p', `:html', `:latin1',
2432 `:utf-8', `:ascii', `:use-brackets-p' and `:post-blank' as
2435 Assume point is at the beginning of the entity."
2437 (looking-at "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)")
2438 (let* ((value (org-entity-get (match-string 1)))
2439 (begin (match-beginning 0))
2440 (bracketsp (string= (match-string 2) "{}"))
2441 (post-blank (progn (goto-char (match-end 1))
2442 (when bracketsp
(forward-char 2))
2443 (skip-chars-forward " \t")))
2446 (list :name
(car value
)
2447 :latex
(nth 1 value
)
2448 :latex-math-p
(nth 2 value
)
2450 :ascii
(nth 4 value
)
2451 :latin1
(nth 5 value
)
2452 :utf-8
(nth 6 value
)
2455 :use-brackets-p bracketsp
2456 :post-blank post-blank
)))))
2458 (defun org-element-entity-interpreter (entity contents
)
2459 "Interpret ENTITY object as Org syntax.
2462 (org-element-property :name entity
)
2463 (when (org-element-property :use-brackets-p entity
) "{}")))
2465 (defun org-element-latex-or-entity-successor (limit)
2466 "Search for the next latex-fragment or entity object.
2468 LIMIT bounds the search.
2470 Return value is a cons cell whose CAR is `entity' or
2471 `latex-fragment' and CDR is beginning position."
2474 (remove "begin" (plist-get org-format-latex-options
:matchers
)))
2475 ;; ENTITY-RE matches both LaTeX commands and Org entities.
2477 "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))
2478 (when (re-search-forward
2479 (concat (mapconcat (lambda (e) (nth 1 (assoc e org-latex-regexps
)))
2483 (goto-char (match-beginning 0))
2484 (if (looking-at entity-re
)
2485 ;; Determine if it's a real entity or a LaTeX command.
2486 (cons (if (org-entity-get (match-string 1)) 'entity
'latex-fragment
)
2487 (match-beginning 0))
2488 ;; No entity nor command: point is at a LaTeX fragment.
2489 ;; Determine its type to get the correct beginning position.
2490 (cons 'latex-fragment
2493 (when (looking-at (nth 1 (assoc e org-latex-regexps
)))
2496 (nth 2 (assoc e org-latex-regexps
))))))
2503 (defun org-element-export-snippet-parser ()
2504 "Parse export snippet at point.
2506 Return a list whose CAR is `export-snippet' and CDR a plist with
2507 `:begin', `:end', `:back-end', `:value' and `:post-blank' as
2510 Assume point is at the beginning of the snippet."
2512 (re-search-forward "@@\\([-A-Za-z0-9]+\\):" nil t
)
2513 (let* ((begin (match-beginning 0))
2514 (back-end (org-match-string-no-properties 1))
2515 (value (buffer-substring-no-properties
2517 (progn (re-search-forward "@@" nil t
) (match-beginning 0))))
2518 (post-blank (skip-chars-forward " \t"))
2520 (list 'export-snippet
2521 (list :back-end back-end
2525 :post-blank post-blank
)))))
2527 (defun org-element-export-snippet-interpreter (export-snippet contents
)
2528 "Interpret EXPORT-SNIPPET object as Org syntax.
2531 (org-element-property :back-end export-snippet
)
2532 (org-element-property :value export-snippet
)))
2534 (defun org-element-export-snippet-successor (limit)
2535 "Search for the next export-snippet object.
2537 LIMIT bounds the search.
2539 Return value is a cons cell whose CAR is `export-snippet' and CDR
2540 its beginning position."
2543 (when (and (re-search-forward "@@[-A-Za-z0-9]+:" limit t
)
2544 (setq beg
(match-beginning 0))
2545 (search-forward "@@" limit t
))
2546 (cons 'export-snippet beg
)))))
2549 ;;;; Footnote Reference
2551 (defun org-element-footnote-reference-parser ()
2552 "Parse footnote reference at point.
2554 Return a list whose CAR is `footnote-reference' and CDR a plist
2555 with `:label', `:type', `:inline-definition', `:begin', `:end'
2556 and `:post-blank' as keywords."
2558 (looking-at org-footnote-re
)
2559 (let* ((begin (point))
2560 (label (or (org-match-string-no-properties 2)
2561 (org-match-string-no-properties 3)
2562 (and (match-string 1)
2563 (concat "fn:" (org-match-string-no-properties 1)))))
2564 (type (if (or (not label
) (match-string 1)) 'inline
'standard
))
2565 (inner-begin (match-end 0))
2569 (while (and (> count
0) (re-search-forward "[][]" nil t
))
2570 (if (equal (match-string 0) "[") (incf count
) (decf count
)))
2572 (post-blank (progn (goto-char (1+ inner-end
))
2573 (skip-chars-forward " \t")))
2576 (list 'footnote-reference
2581 :post-blank post-blank
))))
2582 (org-element-put-property
2583 footnote-reference
:inline-definition
2584 (and (eq type
'inline
)
2585 (org-element-parse-secondary-string
2586 (buffer-substring inner-begin inner-end
)
2587 (org-element-restriction 'footnote-reference
)
2588 footnote-reference
))))))
2590 (defun org-element-footnote-reference-interpreter (footnote-reference contents
)
2591 "Interpret FOOTNOTE-REFERENCE object as Org syntax.
2593 (let ((label (or (org-element-property :label footnote-reference
) "fn:"))
2596 (org-element-property :inline-definition footnote-reference
)))
2597 (if (not inline-def
) ""
2598 (concat ":" (org-element-interpret-data inline-def
))))))
2599 (format "[%s]" (concat label def
))))
2601 (defun org-element-footnote-reference-successor (limit)
2602 "Search for the next footnote-reference object.
2604 LIMIT bounds the search.
2606 Return value is a cons cell whose CAR is `footnote-reference' and
2607 CDR is beginning position."
2610 (while (re-search-forward org-footnote-re limit t
)
2612 (let ((beg (match-beginning 0))
2615 (while (re-search-forward "[][]" limit t
)
2616 (if (equal (match-string 0) "[") (incf count
) (decf count
))
2618 (throw 'exit
(cons 'footnote-reference beg
))))))))))
2621 ;;;; Inline Babel Call
2623 (defun org-element-inline-babel-call-parser ()
2624 "Parse inline babel call at point.
2626 Return a list whose CAR is `inline-babel-call' and CDR a plist
2627 with `:begin', `:end', `:info' and `:post-blank' as keywords.
2629 Assume point is at the beginning of the babel call."
2631 (unless (bolp) (backward-char))
2632 (looking-at org-babel-inline-lob-one-liner-regexp
)
2633 (let ((info (save-match-data (org-babel-lob-get-info)))
2634 (begin (match-end 1))
2635 (post-blank (progn (goto-char (match-end 0))
2636 (skip-chars-forward " \t")))
2638 (list 'inline-babel-call
2642 :post-blank post-blank
)))))
2644 (defun org-element-inline-babel-call-interpreter (inline-babel-call contents
)
2645 "Interpret INLINE-BABEL-CALL object as Org syntax.
2647 (let* ((babel-info (org-element-property :info inline-babel-call
))
2648 (main-source (car babel-info
))
2649 (post-options (nth 1 babel-info
)))
2651 (if (string-match "\\[\\(\\[.*?\\]\\)\\]" main-source
)
2652 ;; Remove redundant square brackets.
2654 (match-string 1 main-source
) nil nil main-source
)
2656 (and post-options
(format "[%s]" post-options
)))))
2658 (defun org-element-inline-babel-call-successor (limit)
2659 "Search for the next inline-babel-call object.
2661 LIMIT bounds the search.
2663 Return value is a cons cell whose CAR is `inline-babel-call' and
2664 CDR is beginning position."
2666 ;; Use a simplified version of
2667 ;; `org-babel-inline-lob-one-liner-regexp'.
2668 (when (re-search-forward
2669 "call_\\([^()\n]+?\\)\\(?:\\[.*?\\]\\)?([^\n]*?)\\(\\[.*?\\]\\)?"
2671 (cons 'inline-babel-call
(match-beginning 0)))))
2674 ;;;; Inline Src Block
2676 (defun org-element-inline-src-block-parser ()
2677 "Parse inline source block at point.
2679 LIMIT bounds the search.
2681 Return a list whose CAR is `inline-src-block' and CDR a plist
2682 with `:begin', `:end', `:language', `:value', `:parameters' and
2683 `:post-blank' as keywords.
2685 Assume point is at the beginning of the inline src block."
2687 (unless (bolp) (backward-char))
2688 (looking-at org-babel-inline-src-block-regexp
)
2689 (let ((begin (match-beginning 1))
2690 (language (org-match-string-no-properties 2))
2691 (parameters (org-match-string-no-properties 4))
2692 (value (org-match-string-no-properties 5))
2693 (post-blank (progn (goto-char (match-end 0))
2694 (skip-chars-forward " \t")))
2696 (list 'inline-src-block
2697 (list :language language
2699 :parameters parameters
2702 :post-blank post-blank
)))))
2704 (defun org-element-inline-src-block-interpreter (inline-src-block contents
)
2705 "Interpret INLINE-SRC-BLOCK object as Org syntax.
2707 (let ((language (org-element-property :language inline-src-block
))
2708 (arguments (org-element-property :parameters inline-src-block
))
2709 (body (org-element-property :value inline-src-block
)))
2710 (format "src_%s%s{%s}"
2712 (if arguments
(format "[%s]" arguments
) "")
2715 (defun org-element-inline-src-block-successor (limit)
2716 "Search for the next inline-babel-call element.
2718 LIMIT bounds the search.
2720 Return value is a cons cell whose CAR is `inline-babel-call' and
2721 CDR is beginning position."
2723 (unless (bolp) (backward-char))
2724 (when (re-search-forward org-babel-inline-src-block-regexp limit t
)
2725 (cons 'inline-src-block
(match-beginning 1)))))
2729 (defun org-element-italic-parser ()
2730 "Parse italic object at point.
2732 Return a list whose CAR is `italic' and CDR is a plist with
2733 `:begin', `:end', `:contents-begin' and `:contents-end' and
2734 `:post-blank' keywords.
2736 Assume point is at the first slash marker."
2738 (unless (bolp) (backward-char 1))
2739 (looking-at org-emph-re
)
2740 (let ((begin (match-beginning 2))
2741 (contents-begin (match-beginning 4))
2742 (contents-end (match-end 4))
2743 (post-blank (progn (goto-char (match-end 2))
2744 (skip-chars-forward " \t")))
2749 :contents-begin contents-begin
2750 :contents-end contents-end
2751 :post-blank post-blank
)))))
2753 (defun org-element-italic-interpreter (italic contents
)
2754 "Interpret ITALIC object as Org syntax.
2755 CONTENTS is the contents of the object."
2756 (format "/%s/" contents
))
2761 (defun org-element-latex-fragment-parser ()
2762 "Parse latex fragment at point.
2764 Return a list whose CAR is `latex-fragment' and CDR a plist with
2765 `:value', `:begin', `:end', and `:post-blank' as keywords.
2767 Assume point is at the beginning of the latex fragment."
2769 (let* ((begin (point))
2773 (let ((latex-regexp (nth 1 (assoc e org-latex-regexps
))))
2774 (when (or (looking-at latex-regexp
)
2778 (looking-at latex-regexp
))))
2779 (throw 'exit
(nth 2 (assoc e org-latex-regexps
))))))
2780 (plist-get org-format-latex-options
:matchers
))
2781 ;; None found: it's a macro.
2782 (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")
2784 (value (match-string-no-properties substring-match
))
2785 (post-blank (progn (goto-char (match-end substring-match
))
2786 (skip-chars-forward " \t")))
2788 (list 'latex-fragment
2792 :post-blank post-blank
)))))
2794 (defun org-element-latex-fragment-interpreter (latex-fragment contents
)
2795 "Interpret LATEX-FRAGMENT object as Org syntax.
2797 (org-element-property :value latex-fragment
))
2801 (defun org-element-line-break-parser ()
2802 "Parse line break at point.
2804 Return a list whose CAR is `line-break', and CDR a plist with
2805 `:begin', `:end' and `:post-blank' keywords.
2807 Assume point is at the beginning of the line break."
2808 (list 'line-break
(list :begin
(point) :end
(point-at-eol) :post-blank
0)))
2810 (defun org-element-line-break-interpreter (line-break contents
)
2811 "Interpret LINE-BREAK object as Org syntax.
2815 (defun org-element-line-break-successor (limit)
2816 "Search for the next line-break object.
2818 LIMIT bounds the search.
2820 Return value is a cons cell whose CAR is `line-break' and CDR is
2821 beginning position."
2823 (let ((beg (and (re-search-forward "[^\\\\]\\(\\\\\\\\\\)[ \t]*$" limit t
)
2824 (goto-char (match-beginning 1)))))
2825 ;; A line break can only happen on a non-empty line.
2826 (when (and beg
(re-search-backward "\\S-" (point-at-bol) t
))
2827 (cons 'line-break beg
)))))
2832 (defun org-element-link-parser ()
2833 "Parse link at point.
2835 Return a list whose CAR is `link' and CDR a plist with `:type',
2836 `:path', `:raw-link', `:application', `:search-option', `:begin',
2837 `:end', `:contents-begin', `:contents-end' and `:post-blank' as
2840 Assume point is at the beginning of the link."
2842 (let ((begin (point))
2843 end contents-begin contents-end link-end post-blank path type
2844 raw-link link search-option application
)
2846 ;; Type 1: Text targeted from a radio target.
2847 ((and org-target-link-regexp
(looking-at org-target-link-regexp
))
2849 link-end
(match-end 0)
2850 path
(org-match-string-no-properties 0)))
2851 ;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]]
2852 ((looking-at org-bracket-link-regexp
)
2853 (setq contents-begin
(match-beginning 3)
2854 contents-end
(match-end 3)
2855 link-end
(match-end 0)
2856 ;; RAW-LINK is the original link.
2857 raw-link
(org-match-string-no-properties 1)
2858 link
(org-translate-link
2859 (org-link-expand-abbrev
2860 (org-link-unescape raw-link
))))
2861 ;; Determine TYPE of link and set PATH accordingly.
2864 ((or (file-name-absolute-p link
) (string-match "^\\.\\.?/" link
))
2865 (setq type
"file" path link
))
2866 ;; Explicit type (http, irc, bbdb...). See `org-link-types'.
2867 ((string-match org-link-re-with-space3 link
)
2868 (setq type
(match-string 1 link
) path
(match-string 2 link
)))
2869 ;; Id type: PATH is the id.
2870 ((string-match "^id:\\([-a-f0-9]+\\)" link
)
2871 (setq type
"id" path
(match-string 1 link
)))
2872 ;; Code-ref type: PATH is the name of the reference.
2873 ((string-match "^(\\(.*\\))$" link
)
2874 (setq type
"coderef" path
(match-string 1 link
)))
2875 ;; Custom-id type: PATH is the name of the custom id.
2876 ((= (aref link
0) ?
#)
2877 (setq type
"custom-id" path
(substring link
1)))
2878 ;; Fuzzy type: Internal link either matches a target, an
2879 ;; headline name or nothing. PATH is the target or
2881 (t (setq type
"fuzzy" path link
))))
2882 ;; Type 3: Plain link, i.e. http://orgmode.org
2883 ((looking-at org-plain-link-re
)
2884 (setq raw-link
(org-match-string-no-properties 0)
2885 type
(org-match-string-no-properties 1)
2886 path
(org-match-string-no-properties 2)
2887 link-end
(match-end 0)))
2888 ;; Type 4: Angular link, i.e. <http://orgmode.org>
2889 ((looking-at org-angle-link-re
)
2890 (setq raw-link
(buffer-substring-no-properties
2891 (match-beginning 1) (match-end 2))
2892 type
(org-match-string-no-properties 1)
2893 path
(org-match-string-no-properties 2)
2894 link-end
(match-end 0))))
2895 ;; In any case, deduce end point after trailing white space from
2896 ;; LINK-END variable.
2897 (setq post-blank
(progn (goto-char link-end
) (skip-chars-forward " \t"))
2899 ;; Extract search option and opening application out of
2900 ;; "file"-type links.
2901 (when (member type org-element-link-type-is-file
)
2903 (cond ((string-match "^file\\+\\(.*\\)$" type
)
2904 (setq application
(match-string 1 type
)))
2905 ((not (string-match "^file" type
))
2906 (setq application type
)))
2907 ;; Extract search option from PATH.
2908 (when (string-match "::\\(.*\\)$" path
)
2909 (setq search-option
(match-string 1 path
)
2910 path
(replace-match "" nil nil path
)))
2911 ;; Make sure TYPE always report "file".
2916 :raw-link
(or raw-link path
)
2917 :application application
2918 :search-option search-option
2921 :contents-begin contents-begin
2922 :contents-end contents-end
2923 :post-blank post-blank
)))))
2925 (defun org-element-link-interpreter (link contents
)
2926 "Interpret LINK object as Org syntax.
2927 CONTENTS is the contents of the object, or nil."
2928 (let ((type (org-element-property :type link
))
2929 (raw-link (org-element-property :raw-link link
)))
2930 (if (string= type
"radio") raw-link
2933 (if contents
(format "[%s]" contents
) "")))))
2935 (defun org-element-link-successor (limit)
2936 "Search for the next link object.
2938 LIMIT bounds the search.
2940 Return value is a cons cell whose CAR is `link' and CDR is
2941 beginning position."
2944 (if (not org-target-link-regexp
) org-any-link-re
2945 (concat org-any-link-re
"\\|" org-target-link-regexp
))))
2946 (when (re-search-forward link-regexp limit t
)
2947 (cons 'link
(match-beginning 0))))))
2952 (defun org-element-macro-parser ()
2953 "Parse macro at point.
2955 Return a list whose CAR is `macro' and CDR a plist with `:key',
2956 `:args', `:begin', `:end', `:value' and `:post-blank' as
2959 Assume point is at the macro."
2961 (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
2962 (let ((begin (point))
2963 (key (downcase (org-match-string-no-properties 1)))
2964 (value (org-match-string-no-properties 0))
2965 (post-blank (progn (goto-char (match-end 0))
2966 (skip-chars-forward " \t")))
2968 (args (let ((args (org-match-string-no-properties 3)) args2
)
2970 (setq args
(org-split-string args
","))
2972 (while (string-match "\\\\\\'" (car args
))
2973 ;; Repair bad splits.
2974 (setcar (cdr args
) (concat (substring (car args
) 0 -
1)
2977 (push (pop args
) args2
))
2978 (mapcar 'org-trim
(nreverse args2
))))))
2985 :post-blank post-blank
)))))
2987 (defun org-element-macro-interpreter (macro contents
)
2988 "Interpret MACRO object as Org syntax.
2990 (org-element-property :value macro
))
2992 (defun org-element-macro-successor (limit)
2993 "Search for the next macro object.
2995 LIMIT bounds the search.
2997 Return value is cons cell whose CAR is `macro' and CDR is
2998 beginning position."
3000 (when (re-search-forward
3001 "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
3003 (cons 'macro
(match-beginning 0)))))
3008 (defun org-element-radio-target-parser ()
3009 "Parse radio target at point.
3011 Return a list whose CAR is `radio-target' and CDR a plist with
3012 `:begin', `:end', `:contents-begin', `:contents-end', `:value'
3013 and `:post-blank' as keywords.
3015 Assume point is at the radio target."
3017 (looking-at org-radio-target-regexp
)
3018 (let ((begin (point))
3019 (contents-begin (match-beginning 1))
3020 (contents-end (match-end 1))
3021 (value (org-match-string-no-properties 1))
3022 (post-blank (progn (goto-char (match-end 0))
3023 (skip-chars-forward " \t")))
3028 :contents-begin contents-begin
3029 :contents-end contents-end
3030 :post-blank post-blank
3033 (defun org-element-radio-target-interpreter (target contents
)
3034 "Interpret TARGET object as Org syntax.
3035 CONTENTS is the contents of the object."
3036 (concat "<<<" contents
">>>"))
3038 (defun org-element-radio-target-successor (limit)
3039 "Search for the next radio-target object.
3041 LIMIT bounds the search.
3043 Return value is a cons cell whose CAR is `radio-target' and CDR
3044 is beginning position."
3046 (when (re-search-forward org-radio-target-regexp limit t
)
3047 (cons 'radio-target
(match-beginning 0)))))
3050 ;;;; Statistics Cookie
3052 (defun org-element-statistics-cookie-parser ()
3053 "Parse statistics cookie at point.
3055 Return a list whose CAR is `statistics-cookie', and CDR a plist
3056 with `:begin', `:end', `:value' and `:post-blank' keywords.
3058 Assume point is at the beginning of the statistics-cookie."
3060 (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
3061 (let* ((begin (point))
3062 (value (buffer-substring-no-properties
3063 (match-beginning 0) (match-end 0)))
3064 (post-blank (progn (goto-char (match-end 0))
3065 (skip-chars-forward " \t")))
3067 (list 'statistics-cookie
3071 :post-blank post-blank
)))))
3073 (defun org-element-statistics-cookie-interpreter (statistics-cookie contents
)
3074 "Interpret STATISTICS-COOKIE object as Org syntax.
3076 (org-element-property :value statistics-cookie
))
3078 (defun org-element-statistics-cookie-successor (limit)
3079 "Search for the next statistics cookie object.
3081 LIMIT bounds the search.
3083 Return value is a cons cell whose CAR is `statistics-cookie' and
3084 CDR is beginning position."
3086 (when (re-search-forward "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]" limit t
)
3087 (cons 'statistics-cookie
(match-beginning 0)))))
3092 (defun org-element-strike-through-parser ()
3093 "Parse strike-through object at point.
3095 Return a list whose CAR is `strike-through' and CDR is a plist
3096 with `:begin', `:end', `:contents-begin' and `:contents-end' and
3097 `:post-blank' keywords.
3099 Assume point is at the first plus sign marker."
3101 (unless (bolp) (backward-char 1))
3102 (looking-at org-emph-re
)
3103 (let ((begin (match-beginning 2))
3104 (contents-begin (match-beginning 4))
3105 (contents-end (match-end 4))
3106 (post-blank (progn (goto-char (match-end 2))
3107 (skip-chars-forward " \t")))
3109 (list 'strike-through
3112 :contents-begin contents-begin
3113 :contents-end contents-end
3114 :post-blank post-blank
)))))
3116 (defun org-element-strike-through-interpreter (strike-through contents
)
3117 "Interpret STRIKE-THROUGH object as Org syntax.
3118 CONTENTS is the contents of the object."
3119 (format "+%s+" contents
))
3124 (defun org-element-subscript-parser ()
3125 "Parse subscript at point.
3127 Return a list whose CAR is `subscript' and CDR a plist with
3128 `:begin', `:end', `:contents-begin', `:contents-end',
3129 `:use-brackets-p' and `:post-blank' as keywords.
3131 Assume point is at the underscore."
3133 (unless (bolp) (backward-char))
3134 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp
)
3136 (not (looking-at org-match-substring-regexp
))))
3137 (begin (match-beginning 2))
3138 (contents-begin (or (match-beginning 5)
3139 (match-beginning 3)))
3140 (contents-end (or (match-end 5) (match-end 3)))
3141 (post-blank (progn (goto-char (match-end 0))
3142 (skip-chars-forward " \t")))
3147 :use-brackets-p bracketsp
3148 :contents-begin contents-begin
3149 :contents-end contents-end
3150 :post-blank post-blank
)))))
3152 (defun org-element-subscript-interpreter (subscript contents
)
3153 "Interpret SUBSCRIPT object as Org syntax.
3154 CONTENTS is the contents of the object."
3156 (if (org-element-property :use-brackets-p subscript
) "_{%s}" "_%s")
3159 (defun org-element-sub/superscript-successor
(limit)
3160 "Search for the next sub/superscript object.
3162 LIMIT bounds the search.
3164 Return value is a cons cell whose CAR is either `subscript' or
3165 `superscript' and CDR is beginning position."
3167 (when (re-search-forward org-match-substring-regexp limit t
)
3168 (cons (if (string= (match-string 2) "_") 'subscript
'superscript
)
3169 (match-beginning 2)))))
3174 (defun org-element-superscript-parser ()
3175 "Parse superscript at point.
3177 Return a list whose CAR is `superscript' and CDR a plist with
3178 `:begin', `:end', `:contents-begin', `:contents-end',
3179 `:use-brackets-p' and `:post-blank' as keywords.
3181 Assume point is at the caret."
3183 (unless (bolp) (backward-char))
3184 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp
) t
3185 (not (looking-at org-match-substring-regexp
))))
3186 (begin (match-beginning 2))
3187 (contents-begin (or (match-beginning 5)
3188 (match-beginning 3)))
3189 (contents-end (or (match-end 5) (match-end 3)))
3190 (post-blank (progn (goto-char (match-end 0))
3191 (skip-chars-forward " \t")))
3196 :use-brackets-p bracketsp
3197 :contents-begin contents-begin
3198 :contents-end contents-end
3199 :post-blank post-blank
)))))
3201 (defun org-element-superscript-interpreter (superscript contents
)
3202 "Interpret SUPERSCRIPT object as Org syntax.
3203 CONTENTS is the contents of the object."
3205 (if (org-element-property :use-brackets-p superscript
) "^{%s}" "^%s")
3211 (defun org-element-table-cell-parser ()
3212 "Parse table cell at point.
3214 Return a list whose CAR is `table-cell' and CDR is a plist
3215 containing `:begin', `:end', `:contents-begin', `:contents-end'
3216 and `:post-blank' keywords."
3217 (looking-at "[ \t]*\\(.*?\\)[ \t]*|")
3218 (let* ((begin (match-beginning 0))
3220 (contents-begin (match-beginning 1))
3221 (contents-end (match-end 1)))
3225 :contents-begin contents-begin
3226 :contents-end contents-end
3229 (defun org-element-table-cell-interpreter (table-cell contents
)
3230 "Interpret TABLE-CELL element as Org syntax.
3231 CONTENTS is the contents of the cell, or nil."
3232 (concat " " contents
" |"))
3234 (defun org-element-table-cell-successor (limit)
3235 "Search for the next table-cell object.
3237 LIMIT bounds the search.
3239 Return value is a cons cell whose CAR is `table-cell' and CDR is
3240 beginning position."
3241 (when (looking-at "[ \t]*.*?[ \t]+|") (cons 'table-cell
(point))))
3246 (defun org-element-target-parser ()
3247 "Parse target at point.
3249 Return a list whose CAR is `target' and CDR a plist with
3250 `:begin', `:end', `:value' and `:post-blank' as keywords.
3252 Assume point is at the target."
3254 (looking-at org-target-regexp
)
3255 (let ((begin (point))
3256 (value (org-match-string-no-properties 1))
3257 (post-blank (progn (goto-char (match-end 0))
3258 (skip-chars-forward " \t")))
3264 :post-blank post-blank
)))))
3266 (defun org-element-target-interpreter (target contents
)
3267 "Interpret TARGET object as Org syntax.
3269 (format "<<%s>>" (org-element-property :value target
)))
3271 (defun org-element-target-successor (limit)
3272 "Search for the next target object.
3274 LIMIT bounds the search.
3276 Return value is a cons cell whose CAR is `target' and CDR is
3277 beginning position."
3279 (when (re-search-forward org-target-regexp limit t
)
3280 (cons 'target
(match-beginning 0)))))
3285 (defun org-element-timestamp-parser ()
3286 "Parse time stamp at point.
3288 Return a list whose CAR is `timestamp', and CDR a plist with
3289 `:type', `:begin', `:end', `:value' and `:post-blank' keywords.
3291 Assume point is at the beginning of the timestamp."
3293 (let* ((begin (point))
3294 (activep (eq (char-after) ?
<))
3297 (looking-at "[<[]\\(\\(%%\\)?.*?\\)[]>]\\(?:--[<[]\\(.*?\\)[]>]\\)?")
3298 (match-string-no-properties 1)))
3299 (range-end (match-string-no-properties 3))
3300 (type (cond ((match-string 2) 'diary
)
3301 ((and activep range-end
) 'active-range
)
3303 (range-end 'inactive-range
)
3305 (post-blank (progn (goto-char (match-end 0))
3306 (skip-chars-forward " \t")))
3311 :range-end range-end
3314 :post-blank post-blank
)))))
3316 (defun org-element-timestamp-interpreter (timestamp contents
)
3317 "Interpret TIMESTAMP object as Org syntax.
3319 (let ((type (org-element-property :type timestamp
) ))
3321 (format (if (memq type
'(inactive inactive-range
)) "[%s]" "<%s>")
3322 (org-element-property :value timestamp
))
3323 (let ((range-end (org-element-property :range-end timestamp
)))
3326 (format (if (eq type
'inactive-range
) "[%s]" "<%s>")
3329 (defun org-element-timestamp-successor (limit)
3330 "Search for the next timestamp object.
3332 LIMIT bounds the search.
3334 Return value is a cons cell whose CAR is `timestamp' and CDR is
3335 beginning position."
3337 (when (re-search-forward
3338 (concat org-ts-regexp-both
3340 "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
3342 "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
3344 (cons 'timestamp
(match-beginning 0)))))
3349 (defun org-element-underline-parser ()
3350 "Parse underline object at point.
3352 Return a list whose CAR is `underline' and CDR is a plist with
3353 `:begin', `:end', `:contents-begin' and `:contents-end' and
3354 `:post-blank' keywords.
3356 Assume point is at the first underscore marker."
3358 (unless (bolp) (backward-char 1))
3359 (looking-at org-emph-re
)
3360 (let ((begin (match-beginning 2))
3361 (contents-begin (match-beginning 4))
3362 (contents-end (match-end 4))
3363 (post-blank (progn (goto-char (match-end 2))
3364 (skip-chars-forward " \t")))
3369 :contents-begin contents-begin
3370 :contents-end contents-end
3371 :post-blank post-blank
)))))
3373 (defun org-element-underline-interpreter (underline contents
)
3374 "Interpret UNDERLINE object as Org syntax.
3375 CONTENTS is the contents of the object."
3376 (format "_%s_" contents
))
3381 (defun org-element-verbatim-parser ()
3382 "Parse verbatim object at point.
3384 Return a list whose CAR is `verbatim' and CDR is a plist with
3385 `:value', `:begin', `:end' and `:post-blank' keywords.
3387 Assume point is at the first equal sign marker."
3389 (unless (bolp) (backward-char 1))
3390 (looking-at org-emph-re
)
3391 (let ((begin (match-beginning 2))
3392 (value (org-match-string-no-properties 4))
3393 (post-blank (progn (goto-char (match-end 2))
3394 (skip-chars-forward " \t")))
3400 :post-blank post-blank
)))))
3402 (defun org-element-verbatim-interpreter (verbatim contents
)
3403 "Interpret VERBATIM object as Org syntax.
3405 (format "=%s=" (org-element-property :value verbatim
)))
3409 ;;; Parsing Element Starting At Point
3411 ;; `org-element--current-element' is the core function of this section.
3412 ;; It returns the Lisp representation of the element starting at
3415 ;; `org-element--current-element' makes use of special modes. They
3416 ;; are activated for fixed element chaining (i.e. `plain-list' >
3417 ;; `item') or fixed conditional element chaining (i.e. `headline' >
3418 ;; `section'). Special modes are: `first-section', `item',
3419 ;; `node-property', `quote-section', `section' and `table-row'.
3421 (defun org-element--current-element
3422 (limit &optional granularity special structure
)
3423 "Parse the element starting at point.
3425 LIMIT bounds the search.
3427 Return value is a list like (TYPE PROPS) where TYPE is the type
3428 of the element and PROPS a plist of properties associated to the
3431 Possible types are defined in `org-element-all-elements'.
3433 Optional argument GRANULARITY determines the depth of the
3434 recursion. Allowed values are `headline', `greater-element',
3435 `element', `object' or nil. When it is broader than `object' (or
3436 nil), secondary values will not be parsed, since they only
3439 Optional argument SPECIAL, when non-nil, can be either
3440 `first-section', `item', `node-property', `quote-section',
3441 `section', and `table-row'.
3443 If STRUCTURE isn't provided but SPECIAL is set to `item', it will
3446 This function assumes point is always at the beginning of the
3447 element it has to parse."
3449 (let ((case-fold-search t
)
3450 ;; Determine if parsing depth allows for secondary strings
3451 ;; parsing. It only applies to elements referenced in
3452 ;; `org-element-secondary-value-alist'.
3453 (raw-secondary-p (and granularity
(not (eq granularity
'object
)))))
3457 (org-element-item-parser limit structure raw-secondary-p
))
3459 ((eq special
'table-row
) (org-element-table-row-parser limit
))
3461 ((eq special
'node-property
) (org-element-node-property-parser limit
))
3463 ((org-with-limited-levels (org-at-heading-p))
3464 (org-element-headline-parser limit raw-secondary-p
))
3465 ;; Sections (must be checked after headline).
3466 ((eq special
'section
) (org-element-section-parser limit
))
3467 ((eq special
'quote-section
) (org-element-quote-section-parser limit
))
3468 ((eq special
'first-section
)
3469 (org-element-section-parser
3470 (or (save-excursion (org-with-limited-levels (outline-next-heading)))
3472 ;; When not at bol, point is at the beginning of an item or
3473 ;; a footnote definition: next item is always a paragraph.
3474 ((not (bolp)) (org-element-paragraph-parser limit
(list (point))))
3475 ;; Planning and Clock.
3476 ((and (looking-at org-planning-or-clock-line-re
))
3477 (if (equal (match-string 1) org-clock-string
)
3478 (org-element-clock-parser limit
)
3479 (org-element-planning-parser limit
)))
3482 (org-element-inlinetask-parser limit raw-secondary-p
))
3483 ;; From there, elements can have affiliated keywords.
3484 (t (let ((affiliated (org-element--collect-affiliated-keywords)))
3486 ;; LaTeX Environment.
3487 ((looking-at "[ \t]*\\\\begin{\\([A-Za-z0-9*]+\\)}[ \t]*$")
3490 (format "^[ \t]*\\\\end{%s}[ \t]*$"
3491 (regexp-quote (match-string 1)))
3493 (org-element-latex-environment-parser limit affiliated
)
3494 (org-element-paragraph-parser limit affiliated
)))
3495 ;; Drawer and Property Drawer.
3496 ((looking-at org-drawer-regexp
)
3497 (let ((name (match-string 1)))
3499 ((not (save-excursion
3500 (re-search-forward "^[ \t]*:END:[ \t]*$" nil t
)))
3501 (org-element-paragraph-parser limit affiliated
))
3502 ((equal "PROPERTIES" name
)
3503 (org-element-property-drawer-parser limit affiliated
))
3504 (t (org-element-drawer-parser limit affiliated
)))))
3506 ((looking-at "[ \t]*:\\( \\|$\\)")
3507 (org-element-fixed-width-parser limit affiliated
))
3508 ;; Inline Comments, Blocks, Babel Calls, Dynamic Blocks and
3510 ((looking-at "[ \t]*#")
3511 (goto-char (match-end 0))
3512 (cond ((looking-at "\\(?: \\|$\\)")
3514 (org-element-comment-parser limit affiliated
))
3515 ((looking-at "\\+BEGIN_\\(\\S-+\\)")
3517 (let ((parser (assoc (upcase (match-string 1))
3518 org-element-block-name-alist
)))
3519 (if parser
(funcall (cdr parser
) limit affiliated
)
3520 (org-element-special-block-parser limit affiliated
))))
3521 ((looking-at "\\+CALL:")
3523 (org-element-babel-call-parser limit affiliated
))
3524 ((looking-at "\\+BEGIN:? ")
3526 (org-element-dynamic-block-parser limit affiliated
))
3527 ((looking-at "\\+\\S-+:")
3529 (org-element-keyword-parser limit affiliated
))
3532 (org-element-paragraph-parser limit affiliated
))))
3533 ;; Footnote Definition.
3534 ((looking-at org-footnote-definition-re
)
3535 (org-element-footnote-definition-parser limit affiliated
))
3537 ((looking-at "[ \t]*-\\{5,\\}[ \t]*$")
3538 (org-element-horizontal-rule-parser limit affiliated
))
3540 ((org-at-table-p t
) (org-element-table-parser limit affiliated
))
3542 ((looking-at (org-item-re))
3543 (org-element-plain-list-parser
3544 limit affiliated
(or structure
(org-list-struct))))
3545 ;; Default element: Paragraph.
3546 (t (org-element-paragraph-parser limit affiliated
)))))))))
3549 ;; Most elements can have affiliated keywords. When looking for an
3550 ;; element beginning, we want to move before them, as they belong to
3551 ;; that element, and, in the meantime, collect information they give
3552 ;; into appropriate properties. Hence the following function.
3554 (defun org-element--collect-affiliated-keywords ()
3555 "Collect affiliated keywords from point.
3557 Return a list whose CAR is the position at the first of them and
3558 CDR a plist of keywords and values and move point to the
3559 beginning of the first line after them.
3561 As a special case, if element doesn't start at the beginning of
3562 the line (i.e. a paragraph starting an item), CAR is current
3563 position of point and CDR is nil."
3564 (if (not (bolp)) (list (point))
3565 (let ((case-fold-search t
)
3567 ;; RESTRICT is the list of objects allowed in parsed
3569 (restrict (org-element-restriction 'keyword
))
3571 (while (and (not (eobp)) (looking-at org-element--affiliated-re
))
3572 (let* ((raw-kwd (upcase (match-string 1)))
3573 ;; Apply translation to RAW-KWD. From there, KWD is
3574 ;; the official keyword.
3575 (kwd (or (cdr (assoc raw-kwd
3576 org-element-keyword-translation-alist
))
3578 ;; Find main value for any keyword.
3582 (buffer-substring-no-properties
3583 (match-end 0) (point-at-eol)))))
3584 ;; PARSEDP is non-nil when keyword should have its
3586 (parsedp (member kwd org-element-parsed-keywords
))
3587 ;; If KWD is a dual keyword, find its secondary
3588 ;; value. Maybe parse it.
3589 (dualp (member kwd org-element-dual-keywords
))
3592 (let ((sec (org-match-string-no-properties 2)))
3593 (if (or (not sec
) (not parsedp
)) sec
3594 (org-element-parse-secondary-string sec restrict
)))))
3595 ;; Attribute a property name to KWD.
3596 (kwd-sym (and kwd
(intern (concat ":" (downcase kwd
))))))
3597 ;; Now set final shape for VALUE.
3599 (setq value
(org-element-parse-secondary-string value restrict
)))
3600 (when dualp
(setq value
(and value
(cons value dual-value
))))
3601 (when (or (member kwd org-element-multiple-keywords
)
3602 ;; Attributes can always appear on multiple lines.
3603 (string-match "^ATTR_" kwd
))
3604 (setq value
(cons value
(plist-get output kwd-sym
))))
3605 ;; Eventually store the new value in OUTPUT.
3606 (setq output
(plist-put output kwd-sym value
))
3607 ;; Move to next keyword.
3609 ;; If affiliated keywords are orphaned: move back to first one.
3610 ;; They will be parsed as a paragraph.
3611 (when (looking-at "[ \t]*$") (goto-char origin
) (setq output nil
))
3613 (cons origin output
))))
3619 ;; The two major functions here are `org-element-parse-buffer', which
3620 ;; parses Org syntax inside the current buffer, taking into account
3621 ;; region, narrowing, or even visibility if specified, and
3622 ;; `org-element-parse-secondary-string', which parses objects within
3625 ;; The (almost) almighty `org-element-map' allows to apply a function
3626 ;; on elements or objects matching some type, and accumulate the
3627 ;; resulting values. In an export situation, it also skips unneeded
3628 ;; parts of the parse tree.
3630 (defun org-element-parse-buffer (&optional granularity visible-only
)
3631 "Recursively parse the buffer and return structure.
3632 If narrowing is in effect, only parse the visible part of the
3635 Optional argument GRANULARITY determines the depth of the
3636 recursion. It can be set to the following symbols:
3638 `headline' Only parse headlines.
3639 `greater-element' Don't recurse into greater elements excepted
3640 headlines and sections. Thus, elements
3641 parsed are the top-level ones.
3642 `element' Parse everything but objects and plain text.
3643 `object' Parse the complete buffer (default).
3645 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3648 Assume buffer is in Org mode."
3650 (goto-char (point-min))
3651 (org-skip-whitespace)
3652 (org-element--parse-elements
3653 (point-at-bol) (point-max)
3654 ;; Start in `first-section' mode so text before the first
3655 ;; headline belongs to a section.
3656 'first-section nil granularity visible-only
(list 'org-data nil
))))
3658 (defun org-element-parse-secondary-string (string restriction
&optional parent
)
3659 "Recursively parse objects in STRING and return structure.
3661 RESTRICTION is a symbol limiting the object types that will be
3664 Optional argument PARENT, when non-nil, is the element or object
3665 containing the secondary string. It is used to set correctly
3666 `:parent' property within the string."
3669 (let ((secondary (org-element--parse-objects
3670 (point-min) (point-max) nil restriction
)))
3671 (mapc (lambda (obj) (org-element-put-property obj
:parent parent
))
3674 (defun org-element-map
3675 (data types fun
&optional info first-match no-recursion with-affiliated
)
3676 "Map a function on selected elements or objects.
3678 DATA is the parsed tree, as returned by, i.e,
3679 `org-element-parse-buffer'. TYPES is a symbol or list of symbols
3680 of elements or objects types. FUN is the function called on the
3681 matching element or object. It must accept one arguments: the
3682 element or object itself.
3684 When optional argument INFO is non-nil, it should be a plist
3685 holding export options. In that case, parts of the parse tree
3686 not exportable according to that property list will be skipped.
3688 When optional argument FIRST-MATCH is non-nil, stop at the first
3689 match for which FUN doesn't return nil, and return that value.
3691 Optional argument NO-RECURSION is a symbol or a list of symbols
3692 representing elements or objects types. `org-element-map' won't
3693 enter any recursive element or object whose type belongs to that
3694 list. Though, FUN can still be applied on them.
3696 When optional argument WITH-AFFILIATED is non-nil, also move into
3697 affiliated keywords to find objects.
3699 Nil values returned from FUN do not appear in the results."
3700 ;; Ensure TYPES and NO-RECURSION are a list, even of one element.
3701 (unless (listp types
) (setq types
(list types
)))
3702 (unless (listp no-recursion
) (setq no-recursion
(list no-recursion
)))
3703 ;; Recursion depth is determined by --CATEGORY.
3706 (let ((category 'greater-elements
))
3707 (mapc (lambda (type)
3708 (cond ((or (memq type org-element-all-objects
)
3709 (eq type
'plain-text
))
3710 ;; If one object is found, the function
3711 ;; has to recurse into every object.
3712 (throw 'found
'objects
))
3713 ((not (memq type org-element-greater-elements
))
3714 ;; If one regular element is found, the
3715 ;; function has to recurse, at least,
3716 ;; into every element it encounters.
3717 (and (not (eq category
'elements
))
3718 (setq category
'elements
)))))
3721 ;; Compute properties for affiliated keywords if necessary.
3723 (and with-affiliated
3724 (mapcar (lambda (kwd)
3725 (cons kwd
(intern (concat ":" (downcase kwd
)))))
3726 org-element-affiliated-keywords
)))
3732 ;; Recursively walk DATA. INFO, if non-nil, is a plist
3733 ;; holding contextual information.
3734 (let ((--type (org-element-type --data
)))
3737 ;; Ignored element in an export context.
3738 ((and info
(memq --data
(plist-get info
:ignore-list
))))
3739 ;; Secondary string: only objects can be found there.
3741 (when (eq --category
'objects
) (mapc --walk-tree --data
)))
3742 ;; Unconditionally enter parse trees.
3743 ((eq --type
'org-data
)
3744 (mapc --walk-tree
(org-element-contents --data
)))
3746 ;; Check if TYPE is matching among TYPES. If so,
3747 ;; apply FUN to --DATA and accumulate return value
3748 ;; into --ACC (or exit if FIRST-MATCH is non-nil).
3749 (when (memq --type types
)
3750 (let ((result (funcall fun --data
)))
3751 (cond ((not result
))
3752 (first-match (throw '--map-first-match result
))
3753 (t (push result --acc
)))))
3754 ;; If --DATA has a secondary string that can contain
3755 ;; objects with their type among TYPES, look into it.
3756 (when (and (eq --category
'objects
) (not (stringp --data
)))
3758 (assq --type org-element-secondary-value-alist
)))
3760 (funcall --walk-tree
3761 (org-element-property (cdr sec-prop
) --data
)))))
3762 ;; If --DATA has any affiliated keywords and
3763 ;; WITH-AFFILIATED is non-nil, look for objects in
3765 (when (and with-affiliated
3766 (eq --category
'objects
)
3767 (memq --type org-element-all-elements
))
3768 (mapc (lambda (kwd-pair)
3769 (let ((kwd (car kwd-pair
))
3770 (value (org-element-property
3771 (cdr kwd-pair
) --data
)))
3772 ;; Pay attention to the type of value.
3773 ;; Preserve order for multiple keywords.
3776 ((and (member kwd org-element-multiple-keywords
)
3777 (member kwd org-element-dual-keywords
))
3778 (mapc (lambda (line)
3779 (funcall --walk-tree
(cdr line
))
3780 (funcall --walk-tree
(car line
)))
3782 ((member kwd org-element-multiple-keywords
)
3783 (mapc (lambda (line) (funcall --walk-tree line
))
3785 ((member kwd org-element-dual-keywords
)
3786 (funcall --walk-tree
(cdr value
))
3787 (funcall --walk-tree
(car value
)))
3788 (t (funcall --walk-tree value
)))))
3789 --affiliated-alist
))
3790 ;; Determine if a recursion into --DATA is possible.
3792 ;; --TYPE is explicitly removed from recursion.
3793 ((memq --type no-recursion
))
3794 ;; --DATA has no contents.
3795 ((not (org-element-contents --data
)))
3796 ;; Looking for greater elements but --DATA is simply
3797 ;; an element or an object.
3798 ((and (eq --category
'greater-elements
)
3799 (not (memq --type org-element-greater-elements
))))
3800 ;; Looking for elements but --DATA is an object.
3801 ((and (eq --category
'elements
)
3802 (memq --type org-element-all-objects
)))
3803 ;; In any other case, map contents.
3804 (t (mapc --walk-tree
(org-element-contents --data
)))))))))))
3805 (catch '--map-first-match
3806 (funcall --walk-tree data
)
3807 ;; Return value in a proper order.
3810 ;; The following functions are internal parts of the parser.
3812 ;; The first one, `org-element--parse-elements' acts at the element's
3815 ;; The second one, `org-element--parse-objects' applies on all objects
3816 ;; of a paragraph or a secondary string. It uses
3817 ;; `org-element--get-next-object-candidates' to optimize the search of
3818 ;; the next object in the buffer.
3820 ;; More precisely, that function looks for every allowed object type
3821 ;; first. Then, it discards failed searches, keeps further matches,
3822 ;; and searches again types matched behind point, for subsequent
3823 ;; calls. Thus, searching for a given type fails only once, and every
3824 ;; object is searched only once at top level (but sometimes more for
3827 (defun org-element--parse-elements
3828 (beg end special structure granularity visible-only acc
)
3829 "Parse elements between BEG and END positions.
3831 SPECIAL prioritize some elements over the others. It can be set
3832 to `first-section', `quote-section', `section' `item' or
3835 When value is `item', STRUCTURE will be used as the current list
3838 GRANULARITY determines the depth of the recursion. See
3839 `org-element-parse-buffer' for more information.
3841 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3844 Elements are accumulated into ACC."
3847 ;; When parsing only headlines, skip any text before first one.
3848 (when (and (eq granularity
'headline
) (not (org-at-heading-p)))
3849 (org-with-limited-levels (outline-next-heading)))
3851 (while (< (point) end
)
3852 ;; Find current element's type and parse it accordingly to
3854 (let* ((element (org-element--current-element
3855 end granularity special structure
))
3856 (type (org-element-type element
))
3857 (cbeg (org-element-property :contents-begin element
)))
3858 (goto-char (org-element-property :end element
))
3859 ;; Fill ELEMENT contents by side-effect.
3861 ;; If VISIBLE-ONLY is true and element is hidden or if it has
3862 ;; no contents, don't modify it.
3863 ((or (and visible-only
(org-element-property :hiddenp element
))
3865 ;; Greater element: parse it between `contents-begin' and
3866 ;; `contents-end'. Make sure GRANULARITY allows the
3867 ;; recursion, or ELEMENT is an headline, in which case going
3868 ;; inside is mandatory, in order to get sub-level headings.
3869 ((and (memq type org-element-greater-elements
)
3870 (or (memq granularity
'(element object nil
))
3871 (and (eq granularity
'greater-element
)
3873 (eq type
'headline
)))
3874 (org-element--parse-elements
3875 cbeg
(org-element-property :contents-end element
)
3876 ;; Possibly switch to a special mode.
3879 (if (org-element-property :quotedp element
) 'quote-section
3882 (property-drawer 'node-property
)
3884 (org-element-property :structure element
)
3885 granularity visible-only element
))
3886 ;; ELEMENT has contents. Parse objects inside, if
3887 ;; GRANULARITY allows it.
3888 ((memq granularity
'(object nil
))
3889 (org-element--parse-objects
3890 cbeg
(org-element-property :contents-end element
) element
3891 (org-element-restriction type
))))
3892 (org-element-adopt-elements acc element
)))
3896 (defun org-element--parse-objects (beg end acc restriction
)
3897 "Parse objects between BEG and END and return recursive structure.
3899 Objects are accumulated in ACC.
3901 RESTRICTION is a list of object types which are allowed in the
3906 (while (and (< (point) end
)
3907 (setq candidates
(org-element--get-next-object-candidates
3908 end restriction candidates
)))
3910 (let ((pos (apply 'min
(mapcar 'cdr candidates
))))
3913 (funcall (intern (format "org-element-%s-parser"
3914 (car (rassq pos candidates
)))))))))
3915 ;; 1. Text before any object. Untabify it.
3916 (let ((obj-beg (org-element-property :begin next-object
)))
3917 (unless (= (point) obj-beg
)
3919 (org-element-adopt-elements
3921 (replace-regexp-in-string
3922 "\t" (make-string tab-width ?
)
3923 (buffer-substring-no-properties (point) obj-beg
))))))
3925 (let ((obj-end (org-element-property :end next-object
))
3926 (cont-beg (org-element-property :contents-begin next-object
)))
3927 ;; Fill contents of NEXT-OBJECT by side-effect, if it has
3928 ;; a recursive type.
3930 (memq (car next-object
) org-element-recursive-objects
))
3934 (org-element-property :contents-end next-object
))
3935 (org-element--parse-objects
3936 (point-min) (point-max) next-object
3937 (org-element-restriction next-object
))))
3938 (setq acc
(org-element-adopt-elements acc next-object
))
3939 (goto-char obj-end
))))
3940 ;; 3. Text after last object. Untabify it.
3941 (unless (= (point) end
)
3943 (org-element-adopt-elements
3945 (replace-regexp-in-string
3946 "\t" (make-string tab-width ?
)
3947 (buffer-substring-no-properties (point) end
)))))
3951 (defun org-element--get-next-object-candidates (limit restriction objects
)
3952 "Return an alist of candidates for the next object.
3954 LIMIT bounds the search, and RESTRICTION narrows candidates to
3957 Return value is an alist whose CAR is position and CDR the object
3960 OBJECTS is the previous candidates alist."
3961 (let (next-candidates types-to-search
)
3962 ;; If no previous result, search every object type in RESTRICTION.
3963 ;; Otherwise, keep potential candidates (old objects located after
3964 ;; point) and ask to search again those which had matched before.
3965 (if (not objects
) (setq types-to-search restriction
)
3967 (if (< (cdr obj
) (point)) (push (car obj
) types-to-search
)
3968 (push obj next-candidates
)))
3970 ;; Call the appropriate successor function for each type to search
3971 ;; and accumulate matches.
3974 (let* ((successor-fun
3976 (format "org-element-%s-successor"
3977 (or (cdr (assq type org-element-object-successor-alist
))
3979 (obj (funcall successor-fun limit
)))
3980 (and obj
(push obj next-candidates
))))
3987 ;;; Towards A Bijective Process
3989 ;; The parse tree obtained with `org-element-parse-buffer' is really
3990 ;; a snapshot of the corresponding Org buffer. Therefore, it can be
3991 ;; interpreted and expanded into a string with canonical Org syntax.
3992 ;; Hence `org-element-interpret-data'.
3994 ;; The function relies internally on
3995 ;; `org-element--interpret-affiliated-keywords'.
3998 (defun org-element-interpret-data (data &optional parent
)
3999 "Interpret DATA as Org syntax.
4001 DATA is a parse tree, an element, an object or a secondary string
4004 Optional argument PARENT is used for recursive calls. It contains
4005 the element or object containing data, or nil.
4007 Return Org syntax as a string."
4008 (let* ((type (org-element-type data
))
4011 ;; Secondary string.
4014 (lambda (obj) (org-element-interpret-data obj parent
))
4016 ;; Full Org document.
4017 ((eq type
'org-data
)
4019 (lambda (obj) (org-element-interpret-data obj parent
))
4020 (org-element-contents data
) ""))
4022 ((stringp data
) data
)
4023 ;; Element/Object without contents.
4024 ((not (org-element-contents data
))
4025 (funcall (intern (format "org-element-%s-interpreter" type
))
4027 ;; Element/Object with contents.
4029 (let* ((greaterp (memq type org-element-greater-elements
))
4030 (objectp (and (not greaterp
)
4031 (memq type org-element-recursive-objects
)))
4034 (lambda (obj) (org-element-interpret-data obj data
))
4035 (org-element-contents
4036 (if (or greaterp objectp
) data
4037 ;; Elements directly containing objects must
4038 ;; have their indentation normalized first.
4039 (org-element-normalize-contents
4041 ;; When normalizing first paragraph of an
4042 ;; item or a footnote-definition, ignore
4043 ;; first line's indentation.
4044 (and (eq type
'paragraph
)
4045 (equal data
(car (org-element-contents parent
)))
4046 (memq (org-element-type parent
)
4047 '(footnote-definiton item
))))))
4049 (funcall (intern (format "org-element-%s-interpreter" type
))
4051 (if greaterp
(org-element-normalize-contents contents
)
4053 (if (memq type
'(org-data plain-text nil
)) results
4054 ;; Build white spaces. If no `:post-blank' property is
4055 ;; specified, assume its value is 0.
4056 (let ((post-blank (or (org-element-property :post-blank data
) 0)))
4057 (if (memq type org-element-all-objects
)
4058 (concat results
(make-string post-blank
32))
4060 (org-element--interpret-affiliated-keywords data
)
4061 (org-element-normalize-string results
)
4062 (make-string post-blank
10)))))))
4064 (defun org-element--interpret-affiliated-keywords (element)
4065 "Return ELEMENT's affiliated keywords as Org syntax.
4066 If there is no affiliated keyword, return the empty string."
4067 (let ((keyword-to-org
4071 (when (member key org-element-dual-keywords
)
4072 (setq dual
(cdr value
) value
(car value
)))
4075 (format "[%s]" (org-element-interpret-data dual
)))
4077 (if (member key org-element-parsed-keywords
)
4078 (org-element-interpret-data value
)
4083 (let ((value (org-element-property prop element
))
4084 (keyword (upcase (substring (symbol-name prop
) 1))))
4086 (if (or (member keyword org-element-multiple-keywords
)
4087 ;; All attribute keywords can have multiple lines.
4088 (string-match "^ATTR_" keyword
))
4089 (mapconcat (lambda (line) (funcall keyword-to-org keyword line
))
4092 (funcall keyword-to-org keyword value
)))))
4093 ;; List all ELEMENT's properties matching an attribute line or an
4094 ;; affiliated keyword, but ignore translated keywords since they
4095 ;; cannot belong to the property list.
4096 (loop for prop in
(nth 1 element
) by
'cddr
4097 when
(let ((keyword (upcase (substring (symbol-name prop
) 1))))
4098 (or (string-match "^ATTR_" keyword
)
4100 (member keyword org-element-affiliated-keywords
)
4102 org-element-keyword-translation-alist
)))))
4106 ;; Because interpretation of the parse tree must return the same
4107 ;; number of blank lines between elements and the same number of white
4108 ;; space after objects, some special care must be given to white
4111 ;; The first function, `org-element-normalize-string', ensures any
4112 ;; string different from the empty string will end with a single
4113 ;; newline character.
4115 ;; The second function, `org-element-normalize-contents', removes
4116 ;; global indentation from the contents of the current element.
4118 (defun org-element-normalize-string (s)
4119 "Ensure string S ends with a single newline character.
4121 If S isn't a string return it unchanged. If S is the empty
4122 string, return it. Otherwise, return a new string with a single
4123 newline character at its end."
4125 ((not (stringp s
)) s
)
4127 (t (and (string-match "\\(\n[ \t]*\\)*\\'" s
)
4128 (replace-match "\n" nil nil s
)))))
4130 (defun org-element-normalize-contents (element &optional ignore-first
)
4131 "Normalize plain text in ELEMENT's contents.
4133 ELEMENT must only contain plain text and objects.
4135 If optional argument IGNORE-FIRST is non-nil, ignore first line's
4136 indentation to compute maximal common indentation.
4138 Return the normalized element that is element with global
4139 indentation removed from its contents. The function assumes that
4140 indentation is not done with TAB characters."
4141 (let* (ind-list ; for byte-compiler
4142 collect-inds
; for byte-compiler
4145 ;; Return list of indentations within BLOB. This is done by
4146 ;; walking recursively BLOB and updating IND-LIST along the
4147 ;; way. FIRST-FLAG is non-nil when the first string hasn't
4148 ;; been seen yet. It is required as this string is the only
4149 ;; one whose indentation doesn't happen after a newline
4151 (lambda (blob first-flag
)
4154 (when (and first-flag
(stringp object
))
4155 (setq first-flag nil
)
4156 (string-match "\\`\\( *\\)" object
)
4157 (let ((len (length (match-string 1 object
))))
4158 ;; An indentation of zero means no string will be
4159 ;; modified. Quit the process.
4160 (if (zerop len
) (throw 'zero
(setq ind-list nil
))
4161 (push len ind-list
))))
4165 ;; Avoid matching blank or empty lines.
4166 (while (and (string-match "\n\\( *\\)\\(.\\)" object start
)
4167 (not (equal (match-string 2 object
) " ")))
4168 (setq start
(match-end 0))
4169 (push (length (match-string 1 object
)) ind-list
))))
4170 ((memq (org-element-type object
) org-element-recursive-objects
)
4171 (funcall collect-inds object first-flag
))))
4172 (org-element-contents blob
))))))
4173 ;; Collect indentation list in ELEMENT. Possibly remove first
4174 ;; value if IGNORE-FIRST is non-nil.
4175 (catch 'zero
(funcall collect-inds element
(not ignore-first
)))
4176 (if (not ind-list
) element
4177 ;; Build ELEMENT back, replacing each string with the same
4178 ;; string minus common indentation.
4179 (let* (build ; For byte compiler.
4182 (lambda (blob mci first-flag
)
4183 ;; Return BLOB with all its strings indentation
4184 ;; shortened from MCI white spaces. FIRST-FLAG is
4185 ;; non-nil when the first string hasn't been seen
4190 (when (and first-flag
(stringp object
))
4191 (setq first-flag nil
)
4193 (replace-regexp-in-string
4194 (format "\\` \\{%d\\}" mci
) "" object
)))
4197 (replace-regexp-in-string
4198 (format "\n \\{%d\\}" mci
) "\n" object
))
4199 ((memq (org-element-type object
)
4200 org-element-recursive-objects
)
4201 (funcall build object mci first-flag
))
4203 (org-element-contents blob
)))
4205 (funcall build element
(apply 'min ind-list
) (not ignore-first
))))))
4211 ;; The first move is to implement a way to obtain the smallest element
4212 ;; containing point. This is the job of `org-element-at-point'. It
4213 ;; basically jumps back to the beginning of section containing point
4214 ;; and moves, element after element, with
4215 ;; `org-element--current-element' until the container is found. Note:
4216 ;; When using `org-element-at-point', secondary values are never
4217 ;; parsed since the function focuses on elements, not on objects.
4219 ;; At a deeper level, `org-element-context' lists all elements and
4220 ;; objects containing point.
4222 ;; `org-element-nested-p' and `org-element-swap-A-B' may be used
4223 ;; internally by navigation and manipulation tools.
4226 (defun org-element-at-point (&optional keep-trail
)
4227 "Determine closest element around point.
4229 Return value is a list like (TYPE PROPS) where TYPE is the type
4230 of the element and PROPS a plist of properties associated to the
4233 Possible types are defined in `org-element-all-elements'.
4234 Properties depend on element or object type, but always
4235 include :begin, :end, :parent and :post-blank properties.
4237 As a special case, if point is at the very beginning of a list or
4238 sub-list, returned element will be that list instead of the first
4239 item. In the same way, if point is at the beginning of the first
4240 row of a table, returned element will be the table instead of the
4243 If optional argument KEEP-TRAIL is non-nil, the function returns
4244 a list of of elements leading to element at point. The list's
4245 CAR is always the element at point. Following positions contain
4246 element's siblings, then parents, siblings of parents, until the
4247 first element of current section."
4248 (org-with-wide-buffer
4249 ;; If at an headline, parse it. It is the sole element that
4250 ;; doesn't require to know about context. Be sure to disallow
4251 ;; secondary string parsing, though.
4252 (if (org-with-limited-levels (org-at-heading-p))
4255 (if (not keep-trail
) (org-element-headline-parser (point-max) t
)
4256 (list (org-element-headline-parser (point-max) t
))))
4257 ;; Otherwise move at the beginning of the section containing
4259 (let ((origin (point))
4260 (end (save-excursion
4261 (org-with-limited-levels (outline-next-heading)) (point)))
4262 element type special-flag trail struct prevs parent
)
4263 (org-with-limited-levels
4264 (if (org-with-limited-levels (org-before-first-heading-p))
4265 (goto-char (point-min))
4266 (org-back-to-heading)
4268 (org-skip-whitespace)
4270 ;; Parse successively each element, skipping those ending
4271 ;; before original position.
4275 (org-element--current-element end
'element special-flag struct
)
4277 (org-element-put-property element
:parent parent
)
4278 (when keep-trail
(push element trail
))
4280 ;; 1. Skip any element ending before point. Also skip
4281 ;; element ending at point when we're sure that another
4282 ;; element has started.
4283 ((let ((elem-end (org-element-property :end element
)))
4284 (when (or (< elem-end origin
)
4285 (and (= elem-end origin
) (/= elem-end end
)))
4286 (goto-char elem-end
))))
4287 ;; 2. An element containing point is always the element at
4289 ((not (memq type org-element-greater-elements
))
4290 (throw 'exit
(if keep-trail trail element
)))
4291 ;; 3. At any other greater element type, if point is
4292 ;; within contents, move into it.
4294 (let ((cbeg (org-element-property :contents-begin element
))
4295 (cend (org-element-property :contents-end element
)))
4296 (if (or (not cbeg
) (not cend
) (> cbeg origin
) (< cend origin
)
4297 ;; Create an anchor for tables and plain lists:
4298 ;; when point is at the very beginning of these
4299 ;; elements, ignoring affiliated keywords,
4300 ;; target them instead of their contents.
4301 (and (= cbeg origin
) (memq type
'(plain-list table
)))
4302 ;; When point is at contents end, do not move
4303 ;; into elements with an explicit ending, but
4304 ;; return that element instead.
4305 (and (= cend origin
)
4308 drawer dynamic-block inlinetask item
4309 plain-list property-drawer quote-block
4311 (throw 'exit
(if keep-trail trail element
))
4312 (setq parent element
)
4315 (setq special-flag
'item
4316 struct
(org-element-property :structure element
)))
4317 (property-drawer (setq special-flag
'node-property
))
4318 (table (setq special-flag
'table-row
))
4319 (otherwise (setq special-flag nil
)))
4321 (goto-char cbeg
)))))))))))
4324 (defun org-element-context ()
4325 "Return closest element or object around point.
4327 Return value is a list like (TYPE PROPS) where TYPE is the type
4328 of the element or object and PROPS a plist of properties
4331 Possible types are defined in `org-element-all-elements' and
4332 `org-element-all-objects'. Properties depend on element or
4333 object type, but always include :begin, :end, :parent
4334 and :post-blank properties."
4335 (org-with-wide-buffer
4336 (let* ((origin (point))
4337 (element (org-element-at-point))
4338 (type (car element
))
4340 ;; Check if point is inside an element containing objects or at
4341 ;; a secondary string. In that case, move to beginning of the
4342 ;; element or secondary string and set END to the other side.
4343 (if (not (or (and (eq type
'item
)
4344 (let ((tag (org-element-property :tag element
)))
4348 (search-forward tag
(point-at-eol))
4349 (goto-char (match-beginning 0))
4350 (and (>= origin
(point))
4352 ;; `1+' is required so some
4353 ;; successors can match
4354 ;; properly their object.
4355 (setq end
(1+ (match-end 0)))))))))
4356 (and (memq type
'(headline inlinetask
))
4357 (progn (beginning-of-line)
4358 (skip-chars-forward "* ")
4359 (setq end
(point-at-eol))))
4360 (and (memq type
'(paragraph table-cell verse-block
))
4361 (let ((cbeg (org-element-property
4362 :contents-begin element
))
4363 (cend (org-element-property
4364 :contents-end element
)))
4365 (and (>= origin cbeg
)
4367 (progn (goto-char cbeg
) (setq end cend
)))))))
4369 (let ((restriction (org-element-restriction element
))
4373 (while (setq candidates
(org-element--get-next-object-candidates
4374 end restriction candidates
))
4375 (let ((closest-cand (rassq (apply 'min
(mapcar 'cdr candidates
))
4377 ;; If ORIGIN is before next object in element, there's
4378 ;; no point in looking further.
4379 (if (> (cdr closest-cand
) origin
) (throw 'exit element
)
4381 (progn (goto-char (cdr closest-cand
))
4382 (funcall (intern (format "org-element-%s-parser"
4383 (car closest-cand
))))))
4384 (cbeg (org-element-property :contents-begin object
))
4385 (cend (org-element-property :contents-end object
)))
4387 ;; ORIGIN is after OBJECT, so skip it.
4388 ((< (org-element-property :end object
) origin
)
4389 (goto-char (org-element-property :end object
)))
4390 ;; ORIGIN is within a non-recursive object or at an
4391 ;; object boundaries: Return that object.
4392 ((or (not cbeg
) (> cbeg origin
) (< cend origin
))
4394 (org-element-put-property object
:parent parent
)))
4395 ;; Otherwise, move within current object and restrict
4396 ;; search to the end of its contents.
4398 (org-element-put-property object
:parent parent
)
4399 (setq parent object end cend
)))))))
4402 (defsubst org-element-nested-p
(elem-A elem-B
)
4403 "Non-nil when elements ELEM-A and ELEM-B are nested."
4404 (let ((beg-A (org-element-property :begin elem-A
))
4405 (beg-B (org-element-property :begin elem-B
))
4406 (end-A (org-element-property :end elem-A
))
4407 (end-B (org-element-property :end elem-B
)))
4408 (or (and (>= beg-A beg-B
) (<= end-A end-B
))
4409 (and (>= beg-B beg-A
) (<= end-B end-A
)))))
4411 (defun org-element-swap-A-B (elem-A elem-B
)
4412 "Swap elements ELEM-A and ELEM-B.
4413 Assume ELEM-B is after ELEM-A in the buffer. Leave point at the
4415 (goto-char (org-element-property :begin elem-A
))
4416 ;; There are two special cases when an element doesn't start at bol:
4417 ;; the first paragraph in an item or in a footnote definition.
4418 (let ((specialp (not (bolp))))
4419 ;; Only a paragraph without any affiliated keyword can be moved at
4420 ;; ELEM-A position in such a situation. Note that the case of
4421 ;; a footnote definition is impossible: it cannot contain two
4422 ;; paragraphs in a row because it cannot contain a blank line.
4424 (or (not (eq (org-element-type elem-B
) 'paragraph
))
4425 (/= (org-element-property :begin elem-B
)
4426 (org-element-property :contents-begin elem-B
))))
4427 (error "Cannot swap elements"))
4428 ;; In a special situation, ELEM-A will have no indentation. We'll
4429 ;; give it ELEM-B's (which will in, in turn, have no indentation).
4430 (let* ((ind-B (when specialp
4431 (goto-char (org-element-property :begin elem-B
))
4432 (org-get-indentation)))
4433 (beg-A (org-element-property :begin elem-A
))
4434 (end-A (save-excursion
4435 (goto-char (org-element-property :end elem-A
))
4436 (skip-chars-backward " \r\t\n")
4438 (beg-B (org-element-property :begin elem-B
))
4439 (end-B (save-excursion
4440 (goto-char (org-element-property :end elem-B
))
4441 (skip-chars-backward " \r\t\n")
4443 ;; Store overlays responsible for visibility status. We
4444 ;; also need to store their boundaries as they will be
4445 ;; removed from buffer.
4448 (mapcar (lambda (ov) (list ov
(overlay-start ov
) (overlay-end ov
)))
4449 (overlays-in beg-A end-A
))
4450 (mapcar (lambda (ov) (list ov
(overlay-start ov
) (overlay-end ov
)))
4451 (overlays-in beg-B end-B
))))
4453 (body-A (buffer-substring beg-A end-A
))
4454 (body-B (delete-and-extract-region beg-B end-B
)))
4457 (setq body-B
(replace-regexp-in-string "\\`[ \t]*" "" body-B
))
4458 (org-indent-to-column ind-B
))
4460 ;; Restore ex ELEM-A overlays.
4461 (let ((offset (- beg-B beg-A
)))
4464 (car ov
) (+ (nth 1 ov
) offset
) (+ (nth 2 ov
) offset
)))
4467 (delete-region beg-A end-A
)
4469 ;; Restore ex ELEM-B overlays.
4472 (car ov
) (- (nth 1 ov
) offset
) (- (nth 2 ov
) offset
)))
4474 (goto-char (org-element-property :end elem-B
)))))
4477 (provide 'org-element
)
4478 ;;; org-element.el ends here