Update copyright year to 2014 by running admin/update-copyright.
[emacs.git] / lisp / org / org-element.el
blob73d55315575c0b1b4402e82c0ec7fa4bf63de43c
1 ;;; org-element.el --- Parser And Applications for Org syntax
3 ;; Copyright (C) 2012-2014 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; Org syntax can be divided into three categories: "Greater
26 ;; elements", "Elements" and "Objects".
28 ;; Elements are related to the structure of the document. Indeed, all
29 ;; elements are a cover for the document: each position within belongs
30 ;; to at least one element.
32 ;; An element always starts and ends at the beginning of a line. With
33 ;; a few exceptions (`clock', `headline', `inlinetask', `item',
34 ;; `planning', `node-property', `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', `diary-sexp', `example-block', `export-block',
50 ;; `fixed-width', `horizontal-rule', `keyword', `latex-environment',
51 ;; `node-property', `paragraph', `planning', `quote-section',
52 ;; `src-block', `table', `table-row' and `verse-block'. Among them,
53 ;; `paragraph' and `verse-block' types can contain Org objects and
54 ;; plain text.
56 ;; Objects are related to document's contents. Some of them are
57 ;; recursive. Associated types are of the following: `bold', `code',
58 ;; `entity', `export-snippet', `footnote-reference',
59 ;; `inline-babel-call', `inline-src-block', `italic',
60 ;; `latex-fragment', `line-break', `link', `macro', `radio-target',
61 ;; `statistics-cookie', `strike-through', `subscript', `superscript',
62 ;; `table-cell', `target', `timestamp', `underline' and `verbatim'.
64 ;; Some elements also have special properties whose value can hold
65 ;; objects themselves (i.e. an item tag or a headline name). Such
66 ;; values are called "secondary strings". Any object belongs to
67 ;; either an element or a secondary string.
69 ;; Notwithstanding affiliated keywords, each greater element, element
70 ;; and object has a fixed set of properties attached to it. Among
71 ;; them, four are shared by all types: `:begin' and `:end', which
72 ;; refer to the beginning and ending buffer positions of the
73 ;; considered element or object, `:post-blank', which holds the number
74 ;; of blank lines, or white spaces, at its end and `:parent' which
75 ;; refers to the element or object containing it. Greater elements,
76 ;; elements and objects containing objects will also have
77 ;; `:contents-begin' and `:contents-end' properties to delimit
78 ;; contents. Eventually, greater elements and elements accepting
79 ;; affiliated keywords will have a `:post-affiliated' property,
80 ;; referring to the buffer position after all such keywords.
82 ;; At the lowest level, a `:parent' property is also attached to any
83 ;; string, as a text property.
85 ;; Lisp-wise, an element or an object can be represented as a list.
86 ;; It follows the pattern (TYPE PROPERTIES CONTENTS), where:
87 ;; TYPE is a symbol describing the Org element or object.
88 ;; PROPERTIES is the property list attached to it. See docstring of
89 ;; appropriate parsing function to get an exhaustive
90 ;; list.
91 ;; CONTENTS is a list of elements, objects or raw strings contained
92 ;; in the current element or object, when applicable.
94 ;; An Org buffer is a nested list of such elements and objects, whose
95 ;; type is `org-data' and properties is nil.
97 ;; The first part of this file defines Org syntax, while the second
98 ;; one provide accessors and setters functions.
100 ;; The next part implements a parser and an interpreter for each
101 ;; element and object type in Org syntax.
103 ;; The following part creates a fully recursive buffer parser. It
104 ;; also provides a tool to map a function to elements or objects
105 ;; matching some criteria in the parse tree. Functions of interest
106 ;; are `org-element-parse-buffer', `org-element-map' and, to a lesser
107 ;; extent, `org-element-parse-secondary-string'.
109 ;; The penultimate part is the cradle of an interpreter for the
110 ;; obtained parse tree: `org-element-interpret-data'.
112 ;; The library ends by furnishing `org-element-at-point' function, and
113 ;; a way to give information about document structure around point
114 ;; with `org-element-context'.
117 ;;; Code:
119 (eval-when-compile (require 'cl))
120 (require 'org)
124 ;;; Definitions And Rules
126 ;; Define elements, greater elements and specify recursive objects,
127 ;; along with the affiliated keywords recognized. Also set up
128 ;; restrictions on recursive objects combinations.
130 ;; These variables really act as a control center for the parsing
131 ;; process.
133 (defconst org-element-paragraph-separate
134 (concat "^\\(?:"
135 ;; Headlines, inlinetasks.
136 org-outline-regexp "\\|"
137 ;; Footnote definitions.
138 "\\[\\(?:[0-9]+\\|fn:[-_[:word:]]+\\)\\]" "\\|"
139 ;; Diary sexps.
140 "%%(" "\\|"
141 "[ \t]*\\(?:"
142 ;; Empty lines.
143 "$" "\\|"
144 ;; Tables (any type).
145 "\\(?:|\\|\\+-[-+]\\)" "\\|"
146 ;; Blocks (any type), Babel calls and keywords. Note: this
147 ;; is only an indication and need some thorough check.
148 "#\\(?:[+ ]\\|$\\)" "\\|"
149 ;; Drawers (any type) and fixed-width areas. This is also
150 ;; only an indication.
151 ":" "\\|"
152 ;; Horizontal rules.
153 "-\\{5,\\}[ \t]*$" "\\|"
154 ;; LaTeX environments.
155 "\\\\begin{\\([A-Za-z0-9]+\\*?\\)}" "\\|"
156 ;; Planning and Clock lines.
157 (regexp-opt (list org-scheduled-string
158 org-deadline-string
159 org-closed-string
160 org-clock-string))
161 "\\|"
162 ;; Lists.
163 (let ((term (case org-plain-list-ordered-item-terminator
164 (?\) ")") (?. "\\.") (otherwise "[.)]")))
165 (alpha (and org-list-allow-alphabetical "\\|[A-Za-z]")))
166 (concat "\\(?:[-+*]\\|\\(?:[0-9]+" alpha "\\)" term "\\)"
167 "\\(?:[ \t]\\|$\\)"))
168 "\\)\\)")
169 "Regexp to separate paragraphs in an Org buffer.
170 In the case of lines starting with \"#\" and \":\", this regexp
171 is not sufficient to know if point is at a paragraph ending. See
172 `org-element-paragraph-parser' for more information.")
174 (defconst org-element-all-elements
175 '(babel-call center-block clock comment comment-block diary-sexp drawer
176 dynamic-block example-block export-block fixed-width
177 footnote-definition headline horizontal-rule inlinetask item
178 keyword latex-environment node-property paragraph plain-list
179 planning property-drawer quote-block quote-section section
180 special-block src-block table table-row verse-block)
181 "Complete list of element types.")
183 (defconst org-element-greater-elements
184 '(center-block drawer dynamic-block footnote-definition headline inlinetask
185 item plain-list property-drawer quote-block section
186 special-block table)
187 "List of recursive element types aka Greater Elements.")
189 (defconst org-element-all-successors
190 '(export-snippet footnote-reference inline-babel-call inline-src-block
191 latex-or-entity line-break link macro plain-link radio-target
192 statistics-cookie sub/superscript table-cell target
193 text-markup timestamp)
194 "Complete list of successors.")
196 (defconst org-element-object-successor-alist
197 '((subscript . sub/superscript) (superscript . sub/superscript)
198 (bold . text-markup) (code . text-markup) (italic . text-markup)
199 (strike-through . text-markup) (underline . text-markup)
200 (verbatim . text-markup) (entity . latex-or-entity)
201 (latex-fragment . latex-or-entity))
202 "Alist of translations between object type and successor name.
203 Sharing the same successor comes handy when, for example, the
204 regexp matching one object can also match the other object.")
206 (defconst org-element-all-objects
207 '(bold code entity export-snippet footnote-reference inline-babel-call
208 inline-src-block italic line-break latex-fragment link macro
209 radio-target statistics-cookie strike-through subscript superscript
210 table-cell target timestamp underline verbatim)
211 "Complete list of object types.")
213 (defconst org-element-recursive-objects
214 '(bold italic link subscript radio-target strike-through superscript
215 table-cell underline)
216 "List of recursive object types.")
218 (defvar org-element-block-name-alist
219 '(("CENTER" . org-element-center-block-parser)
220 ("COMMENT" . org-element-comment-block-parser)
221 ("EXAMPLE" . org-element-example-block-parser)
222 ("QUOTE" . org-element-quote-block-parser)
223 ("SRC" . org-element-src-block-parser)
224 ("VERSE" . org-element-verse-block-parser))
225 "Alist between block names and the associated parsing function.
226 Names must be uppercase. Any block whose name has no association
227 is parsed with `org-element-special-block-parser'.")
229 (defconst org-element-link-type-is-file
230 '("file" "file+emacs" "file+sys" "docview")
231 "List of link types equivalent to \"file\".
232 Only these types can accept search options and an explicit
233 application to open them.")
235 (defconst org-element-affiliated-keywords
236 '("CAPTION" "DATA" "HEADER" "HEADERS" "LABEL" "NAME" "PLOT" "RESNAME" "RESULT"
237 "RESULTS" "SOURCE" "SRCNAME" "TBLNAME")
238 "List of affiliated keywords as strings.
239 By default, all keywords setting attributes (i.e. \"ATTR_LATEX\")
240 are affiliated keywords and need not to be in this list.")
242 (defconst org-element--affiliated-re
243 (format "[ \t]*#\\+%s:"
244 ;; Regular affiliated keywords.
245 (format "\\(%s\\|ATTR_[-_A-Za-z0-9]+\\)\\(?:\\[\\(.*\\)\\]\\)?"
246 (regexp-opt org-element-affiliated-keywords)))
247 "Regexp matching any affiliated keyword.
249 Keyword name is put in match group 1. Moreover, if keyword
250 belongs to `org-element-dual-keywords', put the dual value in
251 match group 2.
253 Don't modify it, set `org-element-affiliated-keywords' instead.")
255 (defconst org-element-keyword-translation-alist
256 '(("DATA" . "NAME") ("LABEL" . "NAME") ("RESNAME" . "NAME")
257 ("SOURCE" . "NAME") ("SRCNAME" . "NAME") ("TBLNAME" . "NAME")
258 ("RESULT" . "RESULTS") ("HEADERS" . "HEADER"))
259 "Alist of usual translations for keywords.
260 The key is the old name and the value the new one. The property
261 holding their value will be named after the translated name.")
263 (defconst org-element-multiple-keywords '("CAPTION" "HEADER")
264 "List of affiliated keywords that can occur more than once in an element.
266 Their value will be consed into a list of strings, which will be
267 returned as the value of the property.
269 This list is checked after translations have been applied. See
270 `org-element-keyword-translation-alist'.
272 By default, all keywords setting attributes (i.e. \"ATTR_LATEX\")
273 allow multiple occurrences and need not to be in this list.")
275 (defconst org-element-parsed-keywords '("CAPTION")
276 "List of affiliated keywords whose value can be parsed.
278 Their value will be stored as a secondary string: a list of
279 strings and objects.
281 This list is checked after translations have been applied. See
282 `org-element-keyword-translation-alist'.")
284 (defconst org-element-dual-keywords '("CAPTION" "RESULTS")
285 "List of affiliated keywords which can have a secondary value.
287 In Org syntax, they can be written with optional square brackets
288 before the colons. For example, RESULTS keyword can be
289 associated to a hash value with the following:
291 #+RESULTS[hash-string]: some-source
293 This list is checked after translations have been applied. See
294 `org-element-keyword-translation-alist'.")
296 (defconst org-element-document-properties '("AUTHOR" "DATE" "TITLE")
297 "List of properties associated to the whole document.
298 Any keyword in this list will have its value parsed and stored as
299 a secondary string.")
301 (defconst org-element-object-restrictions
302 (let* ((standard-set
303 (remq 'plain-link (remq 'table-cell org-element-all-successors)))
304 (standard-set-no-line-break (remq 'line-break standard-set)))
305 `((bold ,@standard-set)
306 (footnote-reference ,@standard-set)
307 (headline ,@standard-set-no-line-break)
308 (inlinetask ,@standard-set-no-line-break)
309 (italic ,@standard-set)
310 (item ,@standard-set-no-line-break)
311 (keyword ,@standard-set)
312 ;; Ignore all links excepted plain links in a link description.
313 ;; Also ignore radio-targets and line breaks.
314 (link export-snippet inline-babel-call inline-src-block latex-or-entity
315 macro plain-link statistics-cookie sub/superscript text-markup)
316 (paragraph ,@standard-set)
317 ;; Remove any variable object from radio target as it would
318 ;; prevent it from being properly recognized.
319 (radio-target latex-or-entity sub/superscript)
320 (strike-through ,@standard-set)
321 (subscript ,@standard-set)
322 (superscript ,@standard-set)
323 ;; Ignore inline babel call and inline src block as formulas are
324 ;; possible. Also ignore line breaks and statistics cookies.
325 (table-cell export-snippet footnote-reference latex-or-entity link macro
326 radio-target sub/superscript target text-markup timestamp)
327 (table-row table-cell)
328 (underline ,@standard-set)
329 (verse-block ,@standard-set)))
330 "Alist of objects restrictions.
332 CAR is an element or object type containing objects and CDR is
333 a list of successors that will be called within an element or
334 object of such type.
336 For example, in a `radio-target' object, one can only find
337 entities, latex-fragments, subscript and superscript.
339 This alist also applies to secondary string. For example, an
340 `headline' type element doesn't directly contain objects, but
341 still has an entry since one of its properties (`:title') does.")
343 (defconst org-element-secondary-value-alist
344 '((headline . :title)
345 (inlinetask . :title)
346 (item . :tag)
347 (footnote-reference . :inline-definition))
348 "Alist between element types and location of secondary value.")
350 (defconst org-element-object-variables '(org-link-abbrev-alist-local)
351 "List of buffer-local variables used when parsing objects.
352 These variables are copied to the temporary buffer created by
353 `org-export-secondary-string'.")
357 ;;; Accessors and Setters
359 ;; Provide four accessors: `org-element-type', `org-element-property'
360 ;; `org-element-contents' and `org-element-restriction'.
362 ;; Setter functions allow to modify elements by side effect. There is
363 ;; `org-element-put-property', `org-element-set-contents',
364 ;; `org-element-set-element' and `org-element-adopt-element'. Note
365 ;; that `org-element-set-element' and `org-element-adopt-elements' are
366 ;; higher level functions since also update `:parent' property.
368 (defsubst org-element-type (element)
369 "Return type of ELEMENT.
371 The function returns the type of the element or object provided.
372 It can also return the following special value:
373 `plain-text' for a string
374 `org-data' for a complete document
375 nil in any other case."
376 (cond
377 ((not (consp element)) (and (stringp element) 'plain-text))
378 ((symbolp (car element)) (car element))))
380 (defsubst org-element-property (property element)
381 "Extract the value from the PROPERTY of an ELEMENT."
382 (if (stringp element) (get-text-property 0 property element)
383 (plist-get (nth 1 element) property)))
385 (defsubst org-element-contents (element)
386 "Extract contents from an ELEMENT."
387 (cond ((not (consp element)) nil)
388 ((symbolp (car element)) (nthcdr 2 element))
389 (t element)))
391 (defsubst org-element-restriction (element)
392 "Return restriction associated to ELEMENT.
393 ELEMENT can be an element, an object or a symbol representing an
394 element or object type."
395 (cdr (assq (if (symbolp element) element (org-element-type element))
396 org-element-object-restrictions)))
398 (defsubst org-element-put-property (element property value)
399 "In ELEMENT set PROPERTY to VALUE.
400 Return modified element."
401 (if (stringp element) (org-add-props element nil property value)
402 (setcar (cdr element) (plist-put (nth 1 element) property value))
403 element))
405 (defsubst org-element-set-contents (element &rest contents)
406 "Set ELEMENT contents to CONTENTS.
407 Return modified element."
408 (cond ((not element) (list contents))
409 ((not (symbolp (car element))) contents)
410 ((cdr element) (setcdr (cdr element) contents))
411 (t (nconc element contents))))
413 (defsubst org-element-set-element (old new)
414 "Replace element or object OLD with element or object NEW.
415 The function takes care of setting `:parent' property for NEW."
416 ;; Since OLD is going to be changed into NEW by side-effect, first
417 ;; make sure that every element or object within NEW has OLD as
418 ;; parent.
419 (mapc (lambda (blob) (org-element-put-property blob :parent old))
420 (org-element-contents new))
421 ;; Transfer contents.
422 (apply 'org-element-set-contents old (org-element-contents new))
423 ;; Ensure NEW has same parent as OLD, then overwrite OLD properties
424 ;; with NEW's.
425 (org-element-put-property new :parent (org-element-property :parent old))
426 (setcar (cdr old) (nth 1 new))
427 ;; Transfer type.
428 (setcar old (car new)))
430 (defsubst org-element-adopt-elements (parent &rest children)
431 "Append elements to the contents of another element.
433 PARENT is an element or object. CHILDREN can be elements,
434 objects, or a strings.
436 The function takes care of setting `:parent' property for CHILD.
437 Return parent element."
438 ;; Link every child to PARENT. If PARENT is nil, it is a secondary
439 ;; string: parent is the list itself.
440 (mapc (lambda (child)
441 (org-element-put-property child :parent (or parent children)))
442 children)
443 ;; Add CHILDREN at the end of PARENT contents.
444 (when parent
445 (apply 'org-element-set-contents
446 parent
447 (nconc (org-element-contents parent) children)))
448 ;; Return modified PARENT element.
449 (or parent children))
453 ;;; Greater elements
455 ;; For each greater element type, we define a parser and an
456 ;; interpreter.
458 ;; A parser returns the element or object as the list described above.
459 ;; Most of them accepts no argument. Though, exceptions exist. Hence
460 ;; every element containing a secondary string (see
461 ;; `org-element-secondary-value-alist') will accept an optional
462 ;; argument to toggle parsing of that secondary string. Moreover,
463 ;; `item' parser requires current list's structure as its first
464 ;; element.
466 ;; An interpreter accepts two arguments: the list representation of
467 ;; the element or object, and its contents. The latter may be nil,
468 ;; depending on the element or object considered. It returns the
469 ;; appropriate Org syntax, as a string.
471 ;; Parsing functions must follow the naming convention:
472 ;; org-element-TYPE-parser, where TYPE is greater element's type, as
473 ;; defined in `org-element-greater-elements'.
475 ;; Similarly, interpreting functions must follow the naming
476 ;; convention: org-element-TYPE-interpreter.
478 ;; With the exception of `headline' and `item' types, greater elements
479 ;; cannot contain other greater elements of their own type.
481 ;; Beside implementing a parser and an interpreter, adding a new
482 ;; greater element requires to tweak `org-element--current-element'.
483 ;; Moreover, the newly defined type must be added to both
484 ;; `org-element-all-elements' and `org-element-greater-elements'.
487 ;;;; Center Block
489 (defun org-element-center-block-parser (limit affiliated)
490 "Parse a center block.
492 LIMIT bounds the search. AFFILIATED is a list of which CAR is
493 the buffer position at the beginning of the first affiliated
494 keyword and CDR is a plist of affiliated keywords along with
495 their value.
497 Return a list whose CAR is `center-block' and CDR is a plist
498 containing `:begin', `:end', `:hiddenp', `:contents-begin',
499 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
501 Assume point is at the beginning of the block."
502 (let ((case-fold-search t))
503 (if (not (save-excursion
504 (re-search-forward "^[ \t]*#\\+END_CENTER[ \t]*$" limit t)))
505 ;; Incomplete block: parse it as a paragraph.
506 (org-element-paragraph-parser limit affiliated)
507 (let ((block-end-line (match-beginning 0)))
508 (let* ((begin (car affiliated))
509 (post-affiliated (point))
510 ;; Empty blocks have no contents.
511 (contents-begin (progn (forward-line)
512 (and (< (point) block-end-line)
513 (point))))
514 (contents-end (and contents-begin block-end-line))
515 (hidden (org-invisible-p2))
516 (pos-before-blank (progn (goto-char block-end-line)
517 (forward-line)
518 (point)))
519 (end (save-excursion
520 (skip-chars-forward " \r\t\n" limit)
521 (if (eobp) (point) (line-beginning-position)))))
522 (list 'center-block
523 (nconc
524 (list :begin begin
525 :end end
526 :hiddenp hidden
527 :contents-begin contents-begin
528 :contents-end contents-end
529 :post-blank (count-lines pos-before-blank end)
530 :post-affiliated post-affiliated)
531 (cdr affiliated))))))))
533 (defun org-element-center-block-interpreter (center-block contents)
534 "Interpret CENTER-BLOCK element as Org syntax.
535 CONTENTS is the contents of the element."
536 (format "#+BEGIN_CENTER\n%s#+END_CENTER" contents))
539 ;;;; Drawer
541 (defun org-element-drawer-parser (limit affiliated)
542 "Parse a drawer.
544 LIMIT bounds the search. AFFILIATED is a list of which CAR is
545 the buffer position at the beginning of the first affiliated
546 keyword and CDR is a plist of affiliated keywords along with
547 their value.
549 Return a list whose CAR is `drawer' and CDR is a plist containing
550 `:drawer-name', `:begin', `:end', `:hiddenp', `:contents-begin',
551 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
553 Assume point is at beginning of drawer."
554 (let ((case-fold-search t))
555 (if (not (save-excursion (re-search-forward "^[ \t]*:END:[ \t]*$" limit t)))
556 ;; Incomplete drawer: parse it as a paragraph.
557 (org-element-paragraph-parser limit affiliated)
558 (save-excursion
559 (let* ((drawer-end-line (match-beginning 0))
560 (name (progn (looking-at org-drawer-regexp)
561 (org-match-string-no-properties 1)))
562 (begin (car affiliated))
563 (post-affiliated (point))
564 ;; Empty drawers have no contents.
565 (contents-begin (progn (forward-line)
566 (and (< (point) drawer-end-line)
567 (point))))
568 (contents-end (and contents-begin drawer-end-line))
569 (hidden (org-invisible-p2))
570 (pos-before-blank (progn (goto-char drawer-end-line)
571 (forward-line)
572 (point)))
573 (end (progn (skip-chars-forward " \r\t\n" limit)
574 (if (eobp) (point) (line-beginning-position)))))
575 (list 'drawer
576 (nconc
577 (list :begin begin
578 :end end
579 :drawer-name name
580 :hiddenp hidden
581 :contents-begin contents-begin
582 :contents-end contents-end
583 :post-blank (count-lines pos-before-blank end)
584 :post-affiliated post-affiliated)
585 (cdr affiliated))))))))
587 (defun org-element-drawer-interpreter (drawer contents)
588 "Interpret DRAWER element as Org syntax.
589 CONTENTS is the contents of the element."
590 (format ":%s:\n%s:END:"
591 (org-element-property :drawer-name drawer)
592 contents))
595 ;;;; Dynamic Block
597 (defun org-element-dynamic-block-parser (limit affiliated)
598 "Parse a dynamic block.
600 LIMIT bounds the search. AFFILIATED is a list of which CAR is
601 the buffer position at the beginning of the first affiliated
602 keyword and CDR is a plist of affiliated keywords along with
603 their value.
605 Return a list whose CAR is `dynamic-block' and CDR is a plist
606 containing `:block-name', `:begin', `:end', `:hiddenp',
607 `:contents-begin', `:contents-end', `:arguments', `:post-blank'
608 and `:post-affiliated' keywords.
610 Assume point is at beginning of dynamic block."
611 (let ((case-fold-search t))
612 (if (not (save-excursion
613 (re-search-forward "^[ \t]*#\\+END:?[ \t]*$" limit t)))
614 ;; Incomplete block: parse it as a paragraph.
615 (org-element-paragraph-parser limit affiliated)
616 (let ((block-end-line (match-beginning 0)))
617 (save-excursion
618 (let* ((name (progn (looking-at org-dblock-start-re)
619 (org-match-string-no-properties 1)))
620 (arguments (org-match-string-no-properties 3))
621 (begin (car affiliated))
622 (post-affiliated (point))
623 ;; Empty blocks have no contents.
624 (contents-begin (progn (forward-line)
625 (and (< (point) block-end-line)
626 (point))))
627 (contents-end (and contents-begin block-end-line))
628 (hidden (org-invisible-p2))
629 (pos-before-blank (progn (goto-char block-end-line)
630 (forward-line)
631 (point)))
632 (end (progn (skip-chars-forward " \r\t\n" limit)
633 (if (eobp) (point) (line-beginning-position)))))
634 (list 'dynamic-block
635 (nconc
636 (list :begin begin
637 :end end
638 :block-name name
639 :arguments arguments
640 :hiddenp hidden
641 :contents-begin contents-begin
642 :contents-end contents-end
643 :post-blank (count-lines pos-before-blank end)
644 :post-affiliated post-affiliated)
645 (cdr affiliated)))))))))
647 (defun org-element-dynamic-block-interpreter (dynamic-block contents)
648 "Interpret DYNAMIC-BLOCK element as Org syntax.
649 CONTENTS is the contents of the element."
650 (format "#+BEGIN: %s%s\n%s#+END:"
651 (org-element-property :block-name dynamic-block)
652 (let ((args (org-element-property :arguments dynamic-block)))
653 (and args (concat " " args)))
654 contents))
657 ;;;; Footnote Definition
659 (defun org-element-footnote-definition-parser (limit affiliated)
660 "Parse a footnote definition.
662 LIMIT bounds the search. AFFILIATED is a list of which CAR is
663 the buffer position at the beginning of the first affiliated
664 keyword and CDR is a plist of affiliated keywords along with
665 their value.
667 Return a list whose CAR is `footnote-definition' and CDR is
668 a plist containing `:label', `:begin' `:end', `:contents-begin',
669 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
671 Assume point is at the beginning of the footnote definition."
672 (save-excursion
673 (let* ((label (progn (looking-at org-footnote-definition-re)
674 (org-match-string-no-properties 1)))
675 (begin (car affiliated))
676 (post-affiliated (point))
677 (ending (save-excursion
678 (if (progn
679 (end-of-line)
680 (re-search-forward
681 (concat org-outline-regexp-bol "\\|"
682 org-footnote-definition-re "\\|"
683 "^\\([ \t]*\n\\)\\{2,\\}") limit 'move))
684 (match-beginning 0)
685 (point))))
686 (contents-begin (progn
687 (search-forward "]")
688 (skip-chars-forward " \r\t\n" ending)
689 (cond ((= (point) ending) nil)
690 ((= (line-beginning-position) begin) (point))
691 (t (line-beginning-position)))))
692 (contents-end (and contents-begin ending))
693 (end (progn (goto-char ending)
694 (skip-chars-forward " \r\t\n" limit)
695 (if (eobp) (point) (line-beginning-position)))))
696 (list 'footnote-definition
697 (nconc
698 (list :label label
699 :begin begin
700 :end end
701 :contents-begin contents-begin
702 :contents-end contents-end
703 :post-blank (count-lines ending end)
704 :post-affiliated post-affiliated)
705 (cdr affiliated))))))
707 (defun org-element-footnote-definition-interpreter (footnote-definition contents)
708 "Interpret FOOTNOTE-DEFINITION element as Org syntax.
709 CONTENTS is the contents of the footnote-definition."
710 (concat (format "[%s]" (org-element-property :label footnote-definition))
712 contents))
715 ;;;; Headline
717 (defun org-element-headline-parser (limit &optional raw-secondary-p)
718 "Parse a headline.
720 Return a list whose CAR is `headline' and CDR is a plist
721 containing `:raw-value', `:title', `:alt-title', `:begin',
722 `:end', `:pre-blank', `:hiddenp', `:contents-begin' and
723 `:contents-end', `:level', `:priority', `:tags',
724 `:todo-keyword',`:todo-type', `:scheduled', `:deadline',
725 `:closed', `:quotedp', `:archivedp', `:commentedp' and
726 `:footnote-section-p' keywords.
728 The plist also contains any property set in the property drawer,
729 with its name in upper cases and colons added at the
730 beginning (i.e. `:CUSTOM_ID').
732 When RAW-SECONDARY-P is non-nil, headline's title will not be
733 parsed as a secondary string, but as a plain string instead.
735 Assume point is at beginning of the headline."
736 (save-excursion
737 (let* ((components (org-heading-components))
738 (level (nth 1 components))
739 (todo (nth 2 components))
740 (todo-type
741 (and todo (if (member todo org-done-keywords) 'done 'todo)))
742 (tags (let ((raw-tags (nth 5 components)))
743 (and raw-tags (org-split-string raw-tags ":"))))
744 (raw-value (or (nth 4 components) ""))
745 (quotedp
746 (let ((case-fold-search nil))
747 (string-match (format "^%s\\( \\|$\\)" org-quote-string)
748 raw-value)))
749 (commentedp
750 (let ((case-fold-search nil))
751 (string-match (format "^%s\\( \\|$\\)" org-comment-string)
752 raw-value)))
753 (archivedp (member org-archive-tag tags))
754 (footnote-section-p (and org-footnote-section
755 (string= org-footnote-section raw-value)))
756 ;; Upcase property names. It avoids confusion between
757 ;; properties obtained through property drawer and default
758 ;; properties from the parser (e.g. `:end' and :END:)
759 (standard-props
760 (let (plist)
761 (mapc
762 (lambda (p)
763 (setq plist
764 (plist-put plist
765 (intern (concat ":" (upcase (car p))))
766 (cdr p))))
767 (org-entry-properties nil 'standard))
768 plist))
769 (time-props
770 ;; Read time properties on the line below the headline.
771 (save-excursion
772 (when (progn (forward-line)
773 (looking-at org-planning-or-clock-line-re))
774 (let ((end (line-end-position)) plist)
775 (while (re-search-forward
776 org-keyword-time-not-clock-regexp end t)
777 (goto-char (match-end 1))
778 (skip-chars-forward " \t")
779 (let ((keyword (match-string 1))
780 (time (org-element-timestamp-parser)))
781 (cond ((equal keyword org-scheduled-string)
782 (setq plist (plist-put plist :scheduled time)))
783 ((equal keyword org-deadline-string)
784 (setq plist (plist-put plist :deadline time)))
785 (t (setq plist (plist-put plist :closed time))))))
786 plist))))
787 (begin (point))
788 (end (save-excursion (goto-char (org-end-of-subtree t t))))
789 (pos-after-head (progn (forward-line) (point)))
790 (contents-begin (save-excursion
791 (skip-chars-forward " \r\t\n" end)
792 (and (/= (point) end) (line-beginning-position))))
793 (hidden (org-invisible-p2))
794 (contents-end (and contents-begin
795 (progn (goto-char end)
796 (skip-chars-backward " \r\t\n")
797 (forward-line)
798 (point)))))
799 ;; Clean RAW-VALUE from any quote or comment string.
800 (when (or quotedp commentedp)
801 (let ((case-fold-search nil))
802 (setq raw-value
803 (replace-regexp-in-string
804 (concat
805 (regexp-opt (list org-quote-string org-comment-string))
806 "\\(?: \\|$\\)")
808 raw-value))))
809 ;; Clean TAGS from archive tag, if any.
810 (when archivedp (setq tags (delete org-archive-tag tags)))
811 (let ((headline
812 (list 'headline
813 (nconc
814 (list :raw-value raw-value
815 :begin begin
816 :end end
817 :pre-blank
818 (if (not contents-begin) 0
819 (count-lines pos-after-head contents-begin))
820 :hiddenp hidden
821 :contents-begin contents-begin
822 :contents-end contents-end
823 :level level
824 :priority (nth 3 components)
825 :tags tags
826 :todo-keyword todo
827 :todo-type todo-type
828 :post-blank (count-lines
829 (if (not contents-end) pos-after-head
830 (goto-char contents-end)
831 (forward-line)
832 (point))
833 end)
834 :footnote-section-p footnote-section-p
835 :archivedp archivedp
836 :commentedp commentedp
837 :quotedp quotedp)
838 time-props
839 standard-props))))
840 (let ((alt-title (org-element-property :ALT_TITLE headline)))
841 (when alt-title
842 (org-element-put-property
843 headline :alt-title
844 (if raw-secondary-p alt-title
845 (org-element-parse-secondary-string
846 alt-title (org-element-restriction 'headline) headline)))))
847 (org-element-put-property
848 headline :title
849 (if raw-secondary-p raw-value
850 (org-element-parse-secondary-string
851 raw-value (org-element-restriction 'headline) headline)))))))
853 (defun org-element-headline-interpreter (headline contents)
854 "Interpret HEADLINE element as Org syntax.
855 CONTENTS is the contents of the element."
856 (let* ((level (org-element-property :level headline))
857 (todo (org-element-property :todo-keyword headline))
858 (priority (org-element-property :priority headline))
859 (title (org-element-interpret-data
860 (org-element-property :title headline)))
861 (tags (let ((tag-list (if (org-element-property :archivedp headline)
862 (cons org-archive-tag
863 (org-element-property :tags headline))
864 (org-element-property :tags headline))))
865 (and tag-list
866 (format ":%s:" (mapconcat 'identity tag-list ":")))))
867 (commentedp (org-element-property :commentedp headline))
868 (quotedp (org-element-property :quotedp headline))
869 (pre-blank (or (org-element-property :pre-blank headline) 0))
870 (heading (concat (make-string (org-reduced-level level) ?*)
871 (and todo (concat " " todo))
872 (and quotedp (concat " " org-quote-string))
873 (and commentedp (concat " " org-comment-string))
874 (and priority
875 (format " [#%s]" (char-to-string priority)))
876 (cond ((and org-footnote-section
877 (org-element-property
878 :footnote-section-p headline))
879 (concat " " org-footnote-section))
880 (title (concat " " title))))))
881 (concat heading
882 ;; Align tags.
883 (when tags
884 (cond
885 ((zerop org-tags-column) (format " %s" tags))
886 ((< org-tags-column 0)
887 (concat
888 (make-string
889 (max (- (+ org-tags-column (length heading) (length tags))) 1)
891 tags))
893 (concat
894 (make-string (max (- org-tags-column (length heading)) 1) ? )
895 tags))))
896 (make-string (1+ pre-blank) 10)
897 contents)))
900 ;;;; Inlinetask
902 (defun org-element-inlinetask-parser (limit &optional raw-secondary-p)
903 "Parse an inline task.
905 Return a list whose CAR is `inlinetask' and CDR is a plist
906 containing `:title', `:begin', `:end', `:hiddenp',
907 `:contents-begin' and `:contents-end', `:level', `:priority',
908 `:raw-value', `:tags', `:todo-keyword', `:todo-type',
909 `:scheduled', `:deadline', `:closed' and `:post-blank' keywords.
911 The plist also contains any property set in the property drawer,
912 with its name in upper cases and colons added at the
913 beginning (i.e. `:CUSTOM_ID').
915 When optional argument RAW-SECONDARY-P is non-nil, inline-task's
916 title will not be parsed as a secondary string, but as a plain
917 string instead.
919 Assume point is at beginning of the inline task."
920 (save-excursion
921 (let* ((begin (point))
922 (components (org-heading-components))
923 (todo (nth 2 components))
924 (todo-type (and todo
925 (if (member todo org-done-keywords) 'done 'todo)))
926 (tags (let ((raw-tags (nth 5 components)))
927 (and raw-tags (org-split-string raw-tags ":"))))
928 (raw-value (or (nth 4 components) ""))
929 ;; Upcase property names. It avoids confusion between
930 ;; properties obtained through property drawer and default
931 ;; properties from the parser (e.g. `:end' and :END:)
932 (standard-props
933 (let (plist)
934 (mapc
935 (lambda (p)
936 (setq plist
937 (plist-put plist
938 (intern (concat ":" (upcase (car p))))
939 (cdr p))))
940 (org-entry-properties nil 'standard))
941 plist))
942 (time-props
943 ;; Read time properties on the line below the inlinetask
944 ;; opening string.
945 (save-excursion
946 (when (progn (forward-line)
947 (looking-at org-planning-or-clock-line-re))
948 (let ((end (line-end-position)) plist)
949 (while (re-search-forward
950 org-keyword-time-not-clock-regexp end t)
951 (goto-char (match-end 1))
952 (skip-chars-forward " \t")
953 (let ((keyword (match-string 1))
954 (time (org-element-timestamp-parser)))
955 (cond ((equal keyword org-scheduled-string)
956 (setq plist (plist-put plist :scheduled time)))
957 ((equal keyword org-deadline-string)
958 (setq plist (plist-put plist :deadline time)))
959 (t (setq plist (plist-put plist :closed time))))))
960 plist))))
961 (task-end (save-excursion
962 (end-of-line)
963 (and (re-search-forward "^\\*+ END" limit t)
964 (match-beginning 0))))
965 (contents-begin (progn (forward-line)
966 (and task-end (< (point) task-end) (point))))
967 (hidden (and contents-begin (org-invisible-p2)))
968 (contents-end (and contents-begin task-end))
969 (before-blank (if (not task-end) (point)
970 (goto-char task-end)
971 (forward-line)
972 (point)))
973 (end (progn (skip-chars-forward " \r\t\n" limit)
974 (if (eobp) (point) (line-beginning-position))))
975 (inlinetask
976 (list 'inlinetask
977 (nconc
978 (list :raw-value raw-value
979 :begin begin
980 :end end
981 :hiddenp hidden
982 :contents-begin contents-begin
983 :contents-end contents-end
984 :level (nth 1 components)
985 :priority (nth 3 components)
986 :tags tags
987 :todo-keyword todo
988 :todo-type todo-type
989 :post-blank (count-lines before-blank end))
990 time-props
991 standard-props))))
992 (org-element-put-property
993 inlinetask :title
994 (if raw-secondary-p raw-value
995 (org-element-parse-secondary-string
996 raw-value
997 (org-element-restriction 'inlinetask)
998 inlinetask))))))
1000 (defun org-element-inlinetask-interpreter (inlinetask contents)
1001 "Interpret INLINETASK element as Org syntax.
1002 CONTENTS is the contents of inlinetask."
1003 (let* ((level (org-element-property :level inlinetask))
1004 (todo (org-element-property :todo-keyword inlinetask))
1005 (priority (org-element-property :priority inlinetask))
1006 (title (org-element-interpret-data
1007 (org-element-property :title inlinetask)))
1008 (tags (let ((tag-list (org-element-property :tags inlinetask)))
1009 (and tag-list
1010 (format ":%s:" (mapconcat 'identity tag-list ":")))))
1011 (task (concat (make-string level ?*)
1012 (and todo (concat " " todo))
1013 (and priority
1014 (format " [#%s]" (char-to-string priority)))
1015 (and title (concat " " title)))))
1016 (concat task
1017 ;; Align tags.
1018 (when tags
1019 (cond
1020 ((zerop org-tags-column) (format " %s" tags))
1021 ((< org-tags-column 0)
1022 (concat
1023 (make-string
1024 (max (- (+ org-tags-column (length task) (length tags))) 1)
1026 tags))
1028 (concat
1029 (make-string (max (- org-tags-column (length task)) 1) ? )
1030 tags))))
1031 ;; Prefer degenerate inlinetasks when there are no
1032 ;; contents.
1033 (when contents
1034 (concat "\n"
1035 contents
1036 (make-string level ?*) " END")))))
1039 ;;;; Item
1041 (defun org-element-item-parser (limit struct &optional raw-secondary-p)
1042 "Parse an item.
1044 STRUCT is the structure of the plain list.
1046 Return a list whose CAR is `item' and CDR is a plist containing
1047 `:bullet', `:begin', `:end', `:contents-begin', `:contents-end',
1048 `:checkbox', `:counter', `:tag', `:structure', `:hiddenp' and
1049 `:post-blank' keywords.
1051 When optional argument RAW-SECONDARY-P is non-nil, item's tag, if
1052 any, will not be parsed as a secondary string, but as a plain
1053 string instead.
1055 Assume point is at the beginning of the item."
1056 (save-excursion
1057 (beginning-of-line)
1058 (looking-at org-list-full-item-re)
1059 (let* ((begin (point))
1060 (bullet (org-match-string-no-properties 1))
1061 (checkbox (let ((box (org-match-string-no-properties 3)))
1062 (cond ((equal "[ ]" box) 'off)
1063 ((equal "[X]" box) 'on)
1064 ((equal "[-]" box) 'trans))))
1065 (counter (let ((c (org-match-string-no-properties 2)))
1066 (save-match-data
1067 (cond
1068 ((not c) nil)
1069 ((string-match "[A-Za-z]" c)
1070 (- (string-to-char (upcase (match-string 0 c)))
1071 64))
1072 ((string-match "[0-9]+" c)
1073 (string-to-number (match-string 0 c)))))))
1074 (end (save-excursion (goto-char (org-list-get-item-end begin struct))
1075 (unless (bolp) (forward-line))
1076 (point)))
1077 (contents-begin
1078 (progn (goto-char
1079 ;; Ignore tags in un-ordered lists: they are just
1080 ;; a part of item's body.
1081 (if (and (match-beginning 4)
1082 (save-match-data (string-match "[.)]" bullet)))
1083 (match-beginning 4)
1084 (match-end 0)))
1085 (skip-chars-forward " \r\t\n" limit)
1086 ;; If first line isn't empty, contents really start
1087 ;; at the text after item's meta-data.
1088 (if (= (point-at-bol) begin) (point) (point-at-bol))))
1089 (hidden (progn (forward-line)
1090 (and (not (= (point) end)) (org-invisible-p2))))
1091 (contents-end (progn (goto-char end)
1092 (skip-chars-backward " \r\t\n")
1093 (forward-line)
1094 (point)))
1095 (item
1096 (list 'item
1097 (list :bullet bullet
1098 :begin begin
1099 :end end
1100 ;; CONTENTS-BEGIN and CONTENTS-END may be
1101 ;; mixed up in the case of an empty item
1102 ;; separated from the next by a blank line.
1103 ;; Thus ensure the former is always the
1104 ;; smallest.
1105 :contents-begin (min contents-begin contents-end)
1106 :contents-end (max contents-begin contents-end)
1107 :checkbox checkbox
1108 :counter counter
1109 :hiddenp hidden
1110 :structure struct
1111 :post-blank (count-lines contents-end end)))))
1112 (org-element-put-property
1113 item :tag
1114 (let ((raw-tag (org-list-get-tag begin struct)))
1115 (and raw-tag
1116 (if raw-secondary-p raw-tag
1117 (org-element-parse-secondary-string
1118 raw-tag (org-element-restriction 'item) item))))))))
1120 (defun org-element-item-interpreter (item contents)
1121 "Interpret ITEM element as Org syntax.
1122 CONTENTS is the contents of the element."
1123 (let* ((bullet (let ((bullet (org-element-property :bullet item)))
1124 (org-list-bullet-string
1125 (cond ((not (string-match "[0-9a-zA-Z]" bullet)) "- ")
1126 ((eq org-plain-list-ordered-item-terminator ?\)) "1)")
1127 (t "1.")))))
1128 (checkbox (org-element-property :checkbox item))
1129 (counter (org-element-property :counter item))
1130 (tag (let ((tag (org-element-property :tag item)))
1131 (and tag (org-element-interpret-data tag))))
1132 ;; Compute indentation.
1133 (ind (make-string (length bullet) 32))
1134 (item-starts-with-par-p
1135 (eq (org-element-type (car (org-element-contents item)))
1136 'paragraph)))
1137 ;; Indent contents.
1138 (concat
1139 bullet
1140 (and counter (format "[@%d] " counter))
1141 (case checkbox
1142 (on "[X] ")
1143 (off "[ ] ")
1144 (trans "[-] "))
1145 (and tag (format "%s :: " tag))
1146 (when contents
1147 (let ((contents (replace-regexp-in-string
1148 "\\(^\\)[ \t]*\\S-" ind contents nil nil 1)))
1149 (if item-starts-with-par-p (org-trim contents)
1150 (concat "\n" contents)))))))
1153 ;;;; Plain List
1155 (defun org-element--list-struct (limit)
1156 ;; Return structure of list at point. Internal function. See
1157 ;; `org-list-struct' for details.
1158 (let ((case-fold-search t)
1159 (top-ind limit)
1160 (item-re (org-item-re))
1161 (drawers-re (concat ":\\("
1162 (mapconcat 'regexp-quote org-drawers "\\|")
1163 "\\):[ \t]*$"))
1164 (inlinetask-re (and (featurep 'org-inlinetask) "^\\*+ "))
1165 items struct)
1166 (save-excursion
1167 (catch 'exit
1168 (while t
1169 (cond
1170 ;; At limit: end all items.
1171 ((>= (point) limit)
1172 (throw 'exit
1173 (let ((end (progn (skip-chars-backward " \r\t\n")
1174 (forward-line)
1175 (point))))
1176 (dolist (item items (sort (nconc items struct)
1177 'car-less-than-car))
1178 (setcar (nthcdr 6 item) end)))))
1179 ;; At list end: end all items.
1180 ((looking-at org-list-end-re)
1181 (throw 'exit (dolist (item items (sort (nconc items struct)
1182 'car-less-than-car))
1183 (setcar (nthcdr 6 item) (point)))))
1184 ;; At a new item: end previous sibling.
1185 ((looking-at item-re)
1186 (let ((ind (save-excursion (skip-chars-forward " \t")
1187 (current-column))))
1188 (setq top-ind (min top-ind ind))
1189 (while (and items (<= ind (nth 1 (car items))))
1190 (let ((item (pop items)))
1191 (setcar (nthcdr 6 item) (point))
1192 (push item struct)))
1193 (push (progn (looking-at org-list-full-item-re)
1194 (let ((bullet (match-string-no-properties 1)))
1195 (list (point)
1197 bullet
1198 (match-string-no-properties 2) ; counter
1199 (match-string-no-properties 3) ; checkbox
1200 ;; Description tag.
1201 (and (save-match-data
1202 (string-match "[-+*]" bullet))
1203 (match-string-no-properties 4))
1204 ;; Ending position, unknown so far.
1205 nil)))
1206 items))
1207 (forward-line 1))
1208 ;; Skip empty lines.
1209 ((looking-at "^[ \t]*$") (forward-line))
1210 ;; Skip inline tasks and blank lines along the way.
1211 ((and inlinetask-re (looking-at inlinetask-re))
1212 (forward-line)
1213 (let ((origin (point)))
1214 (when (re-search-forward inlinetask-re limit t)
1215 (if (looking-at "^\\*+ END[ \t]*$") (forward-line)
1216 (goto-char origin)))))
1217 ;; At some text line. Check if it ends any previous item.
1219 (let ((ind (progn (skip-chars-forward " \t") (current-column))))
1220 (when (<= ind top-ind)
1221 (skip-chars-backward " \r\t\n")
1222 (forward-line))
1223 (while (<= ind (nth 1 (car items)))
1224 (let ((item (pop items)))
1225 (setcar (nthcdr 6 item) (line-beginning-position))
1226 (push item struct)
1227 (unless items
1228 (throw 'exit (sort struct 'car-less-than-car))))))
1229 ;; Skip blocks (any type) and drawers contents.
1230 (cond
1231 ((and (looking-at "#\\+BEGIN\\(:\\|_\\S-+\\)")
1232 (re-search-forward
1233 (format "^[ \t]*#\\+END%s[ \t]*$"
1234 (org-match-string-no-properties 1))
1235 limit t)))
1236 ((and (looking-at drawers-re)
1237 (re-search-forward "^[ \t]*:END:[ \t]*$" limit t))))
1238 (forward-line))))))))
1240 (defun org-element-plain-list-parser (limit affiliated structure)
1241 "Parse a plain list.
1243 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1244 the buffer position at the beginning of the first affiliated
1245 keyword and CDR is a plist of affiliated keywords along with
1246 their value. STRUCTURE is the structure of the plain list being
1247 parsed.
1249 Return a list whose CAR is `plain-list' and CDR is a plist
1250 containing `:type', `:begin', `:end', `:contents-begin' and
1251 `:contents-end', `:structure', `:post-blank' and
1252 `:post-affiliated' keywords.
1254 Assume point is at the beginning of the list."
1255 (save-excursion
1256 (let* ((struct (or structure (org-element--list-struct limit)))
1257 (prevs (org-list-prevs-alist struct))
1258 (type (org-list-get-list-type (point) struct prevs))
1259 (contents-begin (point))
1260 (begin (car affiliated))
1261 (contents-end
1262 (progn (goto-char (org-list-get-list-end (point) struct prevs))
1263 (unless (bolp) (forward-line))
1264 (point)))
1265 (end (progn (skip-chars-forward " \r\t\n" limit)
1266 (if (= (point) limit) limit (line-beginning-position)))))
1267 ;; Return value.
1268 (list 'plain-list
1269 (nconc
1270 (list :type type
1271 :begin begin
1272 :end end
1273 :contents-begin contents-begin
1274 :contents-end contents-end
1275 :structure struct
1276 :post-blank (count-lines contents-end end)
1277 :post-affiliated contents-begin)
1278 (cdr affiliated))))))
1280 (defun org-element-plain-list-interpreter (plain-list contents)
1281 "Interpret PLAIN-LIST element as Org syntax.
1282 CONTENTS is the contents of the element."
1283 (with-temp-buffer
1284 (insert contents)
1285 (goto-char (point-min))
1286 (org-list-repair)
1287 (buffer-string)))
1290 ;;;; Property Drawer
1292 (defun org-element-property-drawer-parser (limit affiliated)
1293 "Parse a property drawer.
1295 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1296 the buffer position at the beginning of the first affiliated
1297 keyword and CDR is a plist of affiliated keywords along with
1298 their value.
1300 Return a list whose CAR is `property-drawer' and CDR is a plist
1301 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1302 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
1304 Assume point is at the beginning of the property drawer."
1305 (save-excursion
1306 (let ((case-fold-search t))
1307 (if (not (save-excursion
1308 (re-search-forward "^[ \t]*:END:[ \t]*$" limit t)))
1309 ;; Incomplete drawer: parse it as a paragraph.
1310 (org-element-paragraph-parser limit affiliated)
1311 (save-excursion
1312 (let* ((drawer-end-line (match-beginning 0))
1313 (begin (car affiliated))
1314 (post-affiliated (point))
1315 (contents-begin (progn (forward-line)
1316 (and (< (point) drawer-end-line)
1317 (point))))
1318 (contents-end (and contents-begin drawer-end-line))
1319 (hidden (org-invisible-p2))
1320 (pos-before-blank (progn (goto-char drawer-end-line)
1321 (forward-line)
1322 (point)))
1323 (end (progn (skip-chars-forward " \r\t\n" limit)
1324 (if (eobp) (point) (line-beginning-position)))))
1325 (list 'property-drawer
1326 (nconc
1327 (list :begin begin
1328 :end end
1329 :hiddenp hidden
1330 :contents-begin contents-begin
1331 :contents-end contents-end
1332 :post-blank (count-lines pos-before-blank end)
1333 :post-affiliated post-affiliated)
1334 (cdr affiliated)))))))))
1336 (defun org-element-property-drawer-interpreter (property-drawer contents)
1337 "Interpret PROPERTY-DRAWER element as Org syntax.
1338 CONTENTS is the properties within the drawer."
1339 (format ":PROPERTIES:\n%s:END:" contents))
1342 ;;;; Quote Block
1344 (defun org-element-quote-block-parser (limit affiliated)
1345 "Parse a quote block.
1347 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1348 the buffer position at the beginning of the first affiliated
1349 keyword and CDR is a plist of affiliated keywords along with
1350 their value.
1352 Return a list whose CAR is `quote-block' and CDR is a plist
1353 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1354 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
1356 Assume point is at the beginning of the block."
1357 (let ((case-fold-search t))
1358 (if (not (save-excursion
1359 (re-search-forward "^[ \t]*#\\+END_QUOTE[ \t]*$" limit t)))
1360 ;; Incomplete block: parse it as a paragraph.
1361 (org-element-paragraph-parser limit affiliated)
1362 (let ((block-end-line (match-beginning 0)))
1363 (save-excursion
1364 (let* ((begin (car affiliated))
1365 (post-affiliated (point))
1366 ;; Empty blocks have no contents.
1367 (contents-begin (progn (forward-line)
1368 (and (< (point) block-end-line)
1369 (point))))
1370 (contents-end (and contents-begin block-end-line))
1371 (hidden (org-invisible-p2))
1372 (pos-before-blank (progn (goto-char block-end-line)
1373 (forward-line)
1374 (point)))
1375 (end (progn (skip-chars-forward " \r\t\n" limit)
1376 (if (eobp) (point) (line-beginning-position)))))
1377 (list 'quote-block
1378 (nconc
1379 (list :begin begin
1380 :end end
1381 :hiddenp hidden
1382 :contents-begin contents-begin
1383 :contents-end contents-end
1384 :post-blank (count-lines pos-before-blank end)
1385 :post-affiliated post-affiliated)
1386 (cdr affiliated)))))))))
1388 (defun org-element-quote-block-interpreter (quote-block contents)
1389 "Interpret QUOTE-BLOCK element as Org syntax.
1390 CONTENTS is the contents of the element."
1391 (format "#+BEGIN_QUOTE\n%s#+END_QUOTE" contents))
1394 ;;;; Section
1396 (defun org-element-section-parser (limit)
1397 "Parse a section.
1399 LIMIT bounds the search.
1401 Return a list whose CAR is `section' and CDR is a plist
1402 containing `:begin', `:end', `:contents-begin', `contents-end'
1403 and `:post-blank' keywords."
1404 (save-excursion
1405 ;; Beginning of section is the beginning of the first non-blank
1406 ;; line after previous headline.
1407 (let ((begin (point))
1408 (end (progn (org-with-limited-levels (outline-next-heading))
1409 (point)))
1410 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
1411 (forward-line)
1412 (point))))
1413 (list 'section
1414 (list :begin begin
1415 :end end
1416 :contents-begin begin
1417 :contents-end pos-before-blank
1418 :post-blank (count-lines pos-before-blank end))))))
1420 (defun org-element-section-interpreter (section contents)
1421 "Interpret SECTION element as Org syntax.
1422 CONTENTS is the contents of the element."
1423 contents)
1426 ;;;; Special Block
1428 (defun org-element-special-block-parser (limit affiliated)
1429 "Parse a special block.
1431 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1432 the buffer position at the beginning of the first affiliated
1433 keyword and CDR is a plist of affiliated keywords along with
1434 their value.
1436 Return a list whose CAR is `special-block' and CDR is a plist
1437 containing `:type', `:begin', `:end', `:hiddenp',
1438 `:contents-begin', `:contents-end', `:post-blank' and
1439 `:post-affiliated' keywords.
1441 Assume point is at the beginning of the block."
1442 (let* ((case-fold-search t)
1443 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
1444 (upcase (match-string-no-properties 1)))))
1445 (if (not (save-excursion
1446 (re-search-forward
1447 (format "^[ \t]*#\\+END_%s[ \t]*$" (regexp-quote type))
1448 limit t)))
1449 ;; Incomplete block: parse it as a paragraph.
1450 (org-element-paragraph-parser limit affiliated)
1451 (let ((block-end-line (match-beginning 0)))
1452 (save-excursion
1453 (let* ((begin (car affiliated))
1454 (post-affiliated (point))
1455 ;; Empty blocks have no contents.
1456 (contents-begin (progn (forward-line)
1457 (and (< (point) block-end-line)
1458 (point))))
1459 (contents-end (and contents-begin block-end-line))
1460 (hidden (org-invisible-p2))
1461 (pos-before-blank (progn (goto-char block-end-line)
1462 (forward-line)
1463 (point)))
1464 (end (progn (skip-chars-forward " \r\t\n" limit)
1465 (if (eobp) (point) (line-beginning-position)))))
1466 (list 'special-block
1467 (nconc
1468 (list :type type
1469 :begin begin
1470 :end end
1471 :hiddenp hidden
1472 :contents-begin contents-begin
1473 :contents-end contents-end
1474 :post-blank (count-lines pos-before-blank end)
1475 :post-affiliated post-affiliated)
1476 (cdr affiliated)))))))))
1478 (defun org-element-special-block-interpreter (special-block contents)
1479 "Interpret SPECIAL-BLOCK element as Org syntax.
1480 CONTENTS is the contents of the element."
1481 (let ((block-type (org-element-property :type special-block)))
1482 (format "#+BEGIN_%s\n%s#+END_%s" block-type contents block-type)))
1486 ;;; Elements
1488 ;; For each element, a parser and an interpreter are also defined.
1489 ;; Both follow the same naming convention used for greater elements.
1491 ;; Also, as for greater elements, adding a new element type is done
1492 ;; through the following steps: implement a parser and an interpreter,
1493 ;; tweak `org-element--current-element' so that it recognizes the new
1494 ;; type and add that new type to `org-element-all-elements'.
1496 ;; As a special case, when the newly defined type is a block type,
1497 ;; `org-element-block-name-alist' has to be modified accordingly.
1500 ;;;; Babel Call
1502 (defun org-element-babel-call-parser (limit affiliated)
1503 "Parse a babel call.
1505 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1506 the buffer position at the beginning of the first affiliated
1507 keyword and CDR is a plist of affiliated keywords along with
1508 their value.
1510 Return a list whose CAR is `babel-call' and CDR is a plist
1511 containing `:begin', `:end', `:info', `:post-blank' and
1512 `:post-affiliated' as keywords."
1513 (save-excursion
1514 (let ((case-fold-search t)
1515 (info (progn (looking-at org-babel-block-lob-one-liner-regexp)
1516 (org-babel-lob-get-info)))
1517 (begin (car affiliated))
1518 (post-affiliated (point))
1519 (pos-before-blank (progn (forward-line) (point)))
1520 (end (progn (skip-chars-forward " \r\t\n" limit)
1521 (if (eobp) (point) (line-beginning-position)))))
1522 (list 'babel-call
1523 (nconc
1524 (list :begin begin
1525 :end end
1526 :info info
1527 :post-blank (count-lines pos-before-blank end)
1528 :post-affiliated post-affiliated)
1529 (cdr affiliated))))))
1531 (defun org-element-babel-call-interpreter (babel-call contents)
1532 "Interpret BABEL-CALL element as Org syntax.
1533 CONTENTS is nil."
1534 (let* ((babel-info (org-element-property :info babel-call))
1535 (main (car babel-info))
1536 (post-options (nth 1 babel-info)))
1537 (concat "#+CALL: "
1538 (if (not (string-match "\\[\\(\\[.*?\\]\\)\\]" main)) main
1539 ;; Remove redundant square brackets.
1540 (replace-match (match-string 1 main) nil nil main))
1541 (and post-options (format "[%s]" post-options)))))
1544 ;;;; Clock
1546 (defun org-element-clock-parser (limit)
1547 "Parse a clock.
1549 LIMIT bounds the search.
1551 Return a list whose CAR is `clock' and CDR is a plist containing
1552 `:status', `:value', `:time', `:begin', `:end' and `:post-blank'
1553 as keywords."
1554 (save-excursion
1555 (let* ((case-fold-search nil)
1556 (begin (point))
1557 (value (progn (search-forward org-clock-string (line-end-position) t)
1558 (skip-chars-forward " \t")
1559 (org-element-timestamp-parser)))
1560 (duration (and (search-forward " => " (line-end-position) t)
1561 (progn (skip-chars-forward " \t")
1562 (looking-at "\\(\\S-+\\)[ \t]*$"))
1563 (org-match-string-no-properties 1)))
1564 (status (if duration 'closed 'running))
1565 (post-blank (let ((before-blank (progn (forward-line) (point))))
1566 (skip-chars-forward " \r\t\n" limit)
1567 (skip-chars-backward " \t")
1568 (unless (bolp) (end-of-line))
1569 (count-lines before-blank (point))))
1570 (end (point)))
1571 (list 'clock
1572 (list :status status
1573 :value value
1574 :duration duration
1575 :begin begin
1576 :end end
1577 :post-blank post-blank)))))
1579 (defun org-element-clock-interpreter (clock contents)
1580 "Interpret CLOCK element as Org syntax.
1581 CONTENTS is nil."
1582 (concat org-clock-string " "
1583 (org-element-timestamp-interpreter
1584 (org-element-property :value clock) nil)
1585 (let ((duration (org-element-property :duration clock)))
1586 (and duration
1587 (concat " => "
1588 (apply 'format
1589 "%2s:%02s"
1590 (org-split-string duration ":")))))))
1593 ;;;; Comment
1595 (defun org-element-comment-parser (limit affiliated)
1596 "Parse a comment.
1598 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1599 the buffer position at the beginning of the first affiliated
1600 keyword and CDR is a plist of affiliated keywords along with
1601 their value.
1603 Return a list whose CAR is `comment' and CDR is a plist
1604 containing `:begin', `:end', `:value', `:post-blank',
1605 `:post-affiliated' keywords.
1607 Assume point is at comment beginning."
1608 (save-excursion
1609 (let* ((begin (car affiliated))
1610 (post-affiliated (point))
1611 (value (prog2 (looking-at "[ \t]*# ?")
1612 (buffer-substring-no-properties
1613 (match-end 0) (line-end-position))
1614 (forward-line)))
1615 (com-end
1616 ;; Get comments ending.
1617 (progn
1618 (while (and (< (point) limit) (looking-at "[ \t]*#\\( \\|$\\)"))
1619 ;; Accumulate lines without leading hash and first
1620 ;; whitespace.
1621 (setq value
1622 (concat value
1623 "\n"
1624 (buffer-substring-no-properties
1625 (match-end 0) (line-end-position))))
1626 (forward-line))
1627 (point)))
1628 (end (progn (goto-char com-end)
1629 (skip-chars-forward " \r\t\n" limit)
1630 (if (eobp) (point) (line-beginning-position)))))
1631 (list 'comment
1632 (nconc
1633 (list :begin begin
1634 :end end
1635 :value value
1636 :post-blank (count-lines com-end end)
1637 :post-affiliated post-affiliated)
1638 (cdr affiliated))))))
1640 (defun org-element-comment-interpreter (comment contents)
1641 "Interpret COMMENT element as Org syntax.
1642 CONTENTS is nil."
1643 (replace-regexp-in-string "^" "# " (org-element-property :value comment)))
1646 ;;;; Comment Block
1648 (defun org-element-comment-block-parser (limit affiliated)
1649 "Parse an export block.
1651 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1652 the buffer position at the beginning of the first affiliated
1653 keyword and CDR is a plist of affiliated keywords along with
1654 their value.
1656 Return a list whose CAR is `comment-block' and CDR is a plist
1657 containing `:begin', `:end', `:hiddenp', `:value', `:post-blank'
1658 and `:post-affiliated' keywords.
1660 Assume point is at comment block beginning."
1661 (let ((case-fold-search t))
1662 (if (not (save-excursion
1663 (re-search-forward "^[ \t]*#\\+END_COMMENT[ \t]*$" limit t)))
1664 ;; Incomplete block: parse it as a paragraph.
1665 (org-element-paragraph-parser limit affiliated)
1666 (let ((contents-end (match-beginning 0)))
1667 (save-excursion
1668 (let* ((begin (car affiliated))
1669 (post-affiliated (point))
1670 (contents-begin (progn (forward-line) (point)))
1671 (hidden (org-invisible-p2))
1672 (pos-before-blank (progn (goto-char contents-end)
1673 (forward-line)
1674 (point)))
1675 (end (progn (skip-chars-forward " \r\t\n" limit)
1676 (if (eobp) (point) (line-beginning-position))))
1677 (value (buffer-substring-no-properties
1678 contents-begin contents-end)))
1679 (list 'comment-block
1680 (nconc
1681 (list :begin begin
1682 :end end
1683 :value value
1684 :hiddenp hidden
1685 :post-blank (count-lines pos-before-blank end)
1686 :post-affiliated post-affiliated)
1687 (cdr affiliated)))))))))
1689 (defun org-element-comment-block-interpreter (comment-block contents)
1690 "Interpret COMMENT-BLOCK element as Org syntax.
1691 CONTENTS is nil."
1692 (format "#+BEGIN_COMMENT\n%s#+END_COMMENT"
1693 (org-remove-indentation (org-element-property :value comment-block))))
1696 ;;;; Diary Sexp
1698 (defun org-element-diary-sexp-parser (limit affiliated)
1699 "Parse a diary sexp.
1701 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1702 the buffer position at the beginning of the first affiliated
1703 keyword and CDR is a plist of affiliated keywords along with
1704 their value.
1706 Return a list whose CAR is `diary-sexp' and CDR is a plist
1707 containing `:begin', `:end', `:value', `:post-blank' and
1708 `:post-affiliated' keywords."
1709 (save-excursion
1710 (let ((begin (car affiliated))
1711 (post-affiliated (point))
1712 (value (progn (looking-at "\\(%%(.*\\)[ \t]*$")
1713 (org-match-string-no-properties 1)))
1714 (pos-before-blank (progn (forward-line) (point)))
1715 (end (progn (skip-chars-forward " \r\t\n" limit)
1716 (if (eobp) (point) (line-beginning-position)))))
1717 (list 'diary-sexp
1718 (nconc
1719 (list :value value
1720 :begin begin
1721 :end end
1722 :post-blank (count-lines pos-before-blank end)
1723 :post-affiliated post-affiliated)
1724 (cdr affiliated))))))
1726 (defun org-element-diary-sexp-interpreter (diary-sexp contents)
1727 "Interpret DIARY-SEXP as Org syntax.
1728 CONTENTS is nil."
1729 (org-element-property :value diary-sexp))
1732 ;;;; Example Block
1734 (defun org-element--remove-indentation (s &optional n)
1735 "Remove maximum common indentation in string S and return it.
1736 When optional argument N is a positive integer, remove exactly
1737 that much characters from indentation, if possible, or return
1738 S as-is otherwise. Unlike to `org-remove-indentation', this
1739 function doesn't call `untabify' on S."
1740 (catch 'exit
1741 (with-temp-buffer
1742 (insert s)
1743 (goto-char (point-min))
1744 ;; Find maximum common indentation, if not specified.
1745 (setq n (or n
1746 (let ((min-ind (point-max)))
1747 (save-excursion
1748 (while (re-search-forward "^[ \t]*\\S-" nil t)
1749 (let ((ind (1- (current-column))))
1750 (if (zerop ind) (throw 'exit s)
1751 (setq min-ind (min min-ind ind))))))
1752 min-ind)))
1753 (if (zerop n) s
1754 ;; Remove exactly N indentation, but give up if not possible.
1755 (while (not (eobp))
1756 (let ((ind (progn (skip-chars-forward " \t") (current-column))))
1757 (cond ((eolp) (delete-region (line-beginning-position) (point)))
1758 ((< ind n) (throw 'exit s))
1759 (t (org-indent-line-to (- ind n))))
1760 (forward-line)))
1761 (buffer-string)))))
1763 (defun org-element-example-block-parser (limit affiliated)
1764 "Parse an example block.
1766 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1767 the buffer position at the beginning of the first affiliated
1768 keyword and CDR is a plist of affiliated keywords along with
1769 their value.
1771 Return a list whose CAR is `example-block' and CDR is a plist
1772 containing `:begin', `:end', `:number-lines', `:preserve-indent',
1773 `:retain-labels', `:use-labels', `:label-fmt', `:hiddenp',
1774 `:switches', `:value', `:post-blank' and `:post-affiliated'
1775 keywords."
1776 (let ((case-fold-search t))
1777 (if (not (save-excursion
1778 (re-search-forward "^[ \t]*#\\+END_EXAMPLE[ \t]*$" limit t)))
1779 ;; Incomplete block: parse it as a paragraph.
1780 (org-element-paragraph-parser limit affiliated)
1781 (let ((contents-end (match-beginning 0)))
1782 (save-excursion
1783 (let* ((switches
1784 (progn
1785 (looking-at "^[ \t]*#\\+BEGIN_EXAMPLE\\(?: +\\(.*\\)\\)?")
1786 (org-match-string-no-properties 1)))
1787 ;; Switches analysis
1788 (number-lines
1789 (cond ((not switches) nil)
1790 ((string-match "-n\\>" switches) 'new)
1791 ((string-match "+n\\>" switches) 'continued)))
1792 (preserve-indent
1793 (or org-src-preserve-indentation
1794 (and switches (string-match "-i\\>" switches))))
1795 ;; Should labels be retained in (or stripped from) example
1796 ;; blocks?
1797 (retain-labels
1798 (or (not switches)
1799 (not (string-match "-r\\>" switches))
1800 (and number-lines (string-match "-k\\>" switches))))
1801 ;; What should code-references use - labels or
1802 ;; line-numbers?
1803 (use-labels
1804 (or (not switches)
1805 (and retain-labels
1806 (not (string-match "-k\\>" switches)))))
1807 (label-fmt
1808 (and switches
1809 (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
1810 (match-string 1 switches)))
1811 ;; Standard block parsing.
1812 (begin (car affiliated))
1813 (post-affiliated (point))
1814 (block-ind (progn (skip-chars-forward " \t") (current-column)))
1815 (contents-begin (progn (forward-line) (point)))
1816 (hidden (org-invisible-p2))
1817 (value (org-element--remove-indentation
1818 (org-unescape-code-in-string
1819 (buffer-substring-no-properties
1820 contents-begin contents-end))
1821 (and preserve-indent block-ind)))
1822 (pos-before-blank (progn (goto-char contents-end)
1823 (forward-line)
1824 (point)))
1825 (end (progn (skip-chars-forward " \r\t\n" limit)
1826 (if (eobp) (point) (line-beginning-position)))))
1827 (list 'example-block
1828 (nconc
1829 (list :begin begin
1830 :end end
1831 :value value
1832 :switches switches
1833 :number-lines number-lines
1834 :preserve-indent preserve-indent
1835 :retain-labels retain-labels
1836 :use-labels use-labels
1837 :label-fmt label-fmt
1838 :hiddenp hidden
1839 :post-blank (count-lines pos-before-blank end)
1840 :post-affiliated post-affiliated)
1841 (cdr affiliated)))))))))
1843 (defun org-element-example-block-interpreter (example-block contents)
1844 "Interpret EXAMPLE-BLOCK element as Org syntax.
1845 CONTENTS is nil."
1846 (let ((switches (org-element-property :switches example-block)))
1847 (concat "#+BEGIN_EXAMPLE" (and switches (concat " " switches)) "\n"
1848 (org-escape-code-in-string
1849 (org-element-property :value example-block))
1850 "#+END_EXAMPLE")))
1853 ;;;; Export Block
1855 (defun org-element-export-block-parser (limit affiliated)
1856 "Parse an export block.
1858 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1859 the buffer position at the beginning of the first affiliated
1860 keyword and CDR is a plist of affiliated keywords along with
1861 their value.
1863 Return a list whose CAR is `export-block' and CDR is a plist
1864 containing `:begin', `:end', `:type', `:hiddenp', `:value',
1865 `:post-blank' and `:post-affiliated' keywords.
1867 Assume point is at export-block beginning."
1868 (let* ((case-fold-search t)
1869 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
1870 (upcase (org-match-string-no-properties 1)))))
1871 (if (not (save-excursion
1872 (re-search-forward
1873 (format "^[ \t]*#\\+END_%s[ \t]*$" type) limit t)))
1874 ;; Incomplete block: parse it as a paragraph.
1875 (org-element-paragraph-parser limit affiliated)
1876 (let ((contents-end (match-beginning 0)))
1877 (save-excursion
1878 (let* ((begin (car affiliated))
1879 (post-affiliated (point))
1880 (contents-begin (progn (forward-line) (point)))
1881 (hidden (org-invisible-p2))
1882 (pos-before-blank (progn (goto-char contents-end)
1883 (forward-line)
1884 (point)))
1885 (end (progn (skip-chars-forward " \r\t\n" limit)
1886 (if (eobp) (point) (line-beginning-position))))
1887 (value (buffer-substring-no-properties contents-begin
1888 contents-end)))
1889 (list 'export-block
1890 (nconc
1891 (list :begin begin
1892 :end end
1893 :type type
1894 :value value
1895 :hiddenp hidden
1896 :post-blank (count-lines pos-before-blank end)
1897 :post-affiliated post-affiliated)
1898 (cdr affiliated)))))))))
1900 (defun org-element-export-block-interpreter (export-block contents)
1901 "Interpret EXPORT-BLOCK element as Org syntax.
1902 CONTENTS is nil."
1903 (let ((type (org-element-property :type export-block)))
1904 (concat (format "#+BEGIN_%s\n" type)
1905 (org-element-property :value export-block)
1906 (format "#+END_%s" type))))
1909 ;;;; Fixed-width
1911 (defun org-element-fixed-width-parser (limit affiliated)
1912 "Parse a fixed-width section.
1914 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1915 the buffer position at the beginning of the first affiliated
1916 keyword and CDR is a plist of affiliated keywords along with
1917 their value.
1919 Return a list whose CAR is `fixed-width' and CDR is a plist
1920 containing `:begin', `:end', `:value', `:post-blank' and
1921 `:post-affiliated' keywords.
1923 Assume point is at the beginning of the fixed-width area."
1924 (save-excursion
1925 (let* ((begin (car affiliated))
1926 (post-affiliated (point))
1927 value
1928 (end-area
1929 (progn
1930 (while (and (< (point) limit)
1931 (looking-at "[ \t]*:\\( \\|$\\)"))
1932 ;; Accumulate text without starting colons.
1933 (setq value
1934 (concat value
1935 (buffer-substring-no-properties
1936 (match-end 0) (point-at-eol))
1937 "\n"))
1938 (forward-line))
1939 (point)))
1940 (end (progn (skip-chars-forward " \r\t\n" limit)
1941 (if (eobp) (point) (line-beginning-position)))))
1942 (list 'fixed-width
1943 (nconc
1944 (list :begin begin
1945 :end end
1946 :value value
1947 :post-blank (count-lines end-area end)
1948 :post-affiliated post-affiliated)
1949 (cdr affiliated))))))
1951 (defun org-element-fixed-width-interpreter (fixed-width contents)
1952 "Interpret FIXED-WIDTH element as Org syntax.
1953 CONTENTS is nil."
1954 (let ((value (org-element-property :value fixed-width)))
1955 (and value
1956 (replace-regexp-in-string
1957 "^" ": "
1958 (if (string-match "\n\\'" value) (substring value 0 -1) value)))))
1961 ;;;; Horizontal Rule
1963 (defun org-element-horizontal-rule-parser (limit affiliated)
1964 "Parse an horizontal rule.
1966 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1967 the buffer position at the beginning of the first affiliated
1968 keyword and CDR is a plist of affiliated keywords along with
1969 their value.
1971 Return a list whose CAR is `horizontal-rule' and CDR is a plist
1972 containing `:begin', `:end', `:post-blank' and `:post-affiliated'
1973 keywords."
1974 (save-excursion
1975 (let ((begin (car affiliated))
1976 (post-affiliated (point))
1977 (post-hr (progn (forward-line) (point)))
1978 (end (progn (skip-chars-forward " \r\t\n" limit)
1979 (if (eobp) (point) (line-beginning-position)))))
1980 (list 'horizontal-rule
1981 (nconc
1982 (list :begin begin
1983 :end end
1984 :post-blank (count-lines post-hr end)
1985 :post-affiliated post-affiliated)
1986 (cdr affiliated))))))
1988 (defun org-element-horizontal-rule-interpreter (horizontal-rule contents)
1989 "Interpret HORIZONTAL-RULE element as Org syntax.
1990 CONTENTS is nil."
1991 "-----")
1994 ;;;; Keyword
1996 (defun org-element-keyword-parser (limit affiliated)
1997 "Parse a keyword at point.
1999 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2000 the buffer position at the beginning of the first affiliated
2001 keyword and CDR is a plist of affiliated keywords along with
2002 their value.
2004 Return a list whose CAR is `keyword' and CDR is a plist
2005 containing `:key', `:value', `:begin', `:end', `:post-blank' and
2006 `:post-affiliated' keywords."
2007 (save-excursion
2008 (let ((begin (car affiliated))
2009 (post-affiliated (point))
2010 (key (progn (looking-at "[ \t]*#\\+\\(\\S-+*\\):")
2011 (upcase (org-match-string-no-properties 1))))
2012 (value (org-trim (buffer-substring-no-properties
2013 (match-end 0) (point-at-eol))))
2014 (pos-before-blank (progn (forward-line) (point)))
2015 (end (progn (skip-chars-forward " \r\t\n" limit)
2016 (if (eobp) (point) (line-beginning-position)))))
2017 (list 'keyword
2018 (nconc
2019 (list :key key
2020 :value value
2021 :begin begin
2022 :end end
2023 :post-blank (count-lines pos-before-blank end)
2024 :post-affiliated post-affiliated)
2025 (cdr affiliated))))))
2027 (defun org-element-keyword-interpreter (keyword contents)
2028 "Interpret KEYWORD element as Org syntax.
2029 CONTENTS is nil."
2030 (format "#+%s: %s"
2031 (org-element-property :key keyword)
2032 (org-element-property :value keyword)))
2035 ;;;; Latex Environment
2037 (defun org-element-latex-environment-parser (limit affiliated)
2038 "Parse a LaTeX environment.
2040 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2041 the buffer position at the beginning of the first affiliated
2042 keyword and CDR is a plist of affiliated keywords along with
2043 their value.
2045 Return a list whose CAR is `latex-environment' and CDR is a plist
2046 containing `:begin', `:end', `:value', `:post-blank' and
2047 `:post-affiliated' keywords.
2049 Assume point is at the beginning of the latex environment."
2050 (save-excursion
2051 (let ((case-fold-search t)
2052 (code-begin (point)))
2053 (looking-at "[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}")
2054 (if (not (re-search-forward (format "^[ \t]*\\\\end{%s}[ \t]*$"
2055 (regexp-quote (match-string 1)))
2056 limit t))
2057 ;; Incomplete latex environment: parse it as a paragraph.
2058 (org-element-paragraph-parser limit affiliated)
2059 (let* ((code-end (progn (forward-line) (point)))
2060 (begin (car affiliated))
2061 (value (buffer-substring-no-properties code-begin code-end))
2062 (end (progn (skip-chars-forward " \r\t\n" limit)
2063 (if (eobp) (point) (line-beginning-position)))))
2064 (list 'latex-environment
2065 (nconc
2066 (list :begin begin
2067 :end end
2068 :value value
2069 :post-blank (count-lines code-end end)
2070 :post-affiliated code-begin)
2071 (cdr affiliated))))))))
2073 (defun org-element-latex-environment-interpreter (latex-environment contents)
2074 "Interpret LATEX-ENVIRONMENT element as Org syntax.
2075 CONTENTS is nil."
2076 (org-element-property :value latex-environment))
2079 ;;;; Node Property
2081 (defun org-element-node-property-parser (limit)
2082 "Parse a node-property at point.
2084 LIMIT bounds the search.
2086 Return a list whose CAR is `node-property' and CDR is a plist
2087 containing `:key', `:value', `:begin', `:end' and `:post-blank'
2088 keywords."
2089 (save-excursion
2090 (looking-at org-property-re)
2091 (let ((case-fold-search t)
2092 (begin (point))
2093 (key (org-match-string-no-properties 2))
2094 (value (org-match-string-no-properties 3))
2095 (pos-before-blank (progn (forward-line) (point)))
2096 (end (progn (skip-chars-forward " \r\t\n" limit)
2097 (if (eobp) (point) (point-at-bol)))))
2098 (list 'node-property
2099 (list :key key
2100 :value value
2101 :begin begin
2102 :end end
2103 :post-blank (count-lines pos-before-blank end))))))
2105 (defun org-element-node-property-interpreter (node-property contents)
2106 "Interpret NODE-PROPERTY element as Org syntax.
2107 CONTENTS is nil."
2108 (format org-property-format
2109 (format ":%s:" (org-element-property :key node-property))
2110 (org-element-property :value node-property)))
2113 ;;;; Paragraph
2115 (defun org-element-paragraph-parser (limit affiliated)
2116 "Parse a paragraph.
2118 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2119 the buffer position at the beginning of the first affiliated
2120 keyword and CDR is a plist of affiliated keywords along with
2121 their value.
2123 Return a list whose CAR is `paragraph' and CDR is a plist
2124 containing `:begin', `:end', `:contents-begin' and
2125 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
2127 Assume point is at the beginning of the paragraph."
2128 (save-excursion
2129 (let* ((begin (car affiliated))
2130 (contents-begin (point))
2131 (before-blank
2132 (let ((case-fold-search t))
2133 (end-of-line)
2134 (if (not (re-search-forward
2135 org-element-paragraph-separate limit 'm))
2136 limit
2137 ;; A matching `org-element-paragraph-separate' is not
2138 ;; necessarily the end of the paragraph. In
2139 ;; particular, lines starting with # or : as a first
2140 ;; non-space character are ambiguous. We have check
2141 ;; if they are valid Org syntax (i.e. not an
2142 ;; incomplete keyword).
2143 (beginning-of-line)
2144 (while (not
2146 ;; There's no ambiguity for other symbols or
2147 ;; empty lines: stop here.
2148 (looking-at "[ \t]*\\(?:[^:#]\\|$\\)")
2149 ;; Stop at valid fixed-width areas.
2150 (looking-at "[ \t]*:\\(?: \\|$\\)")
2151 ;; Stop at drawers.
2152 (and (looking-at org-drawer-regexp)
2153 (save-excursion
2154 (re-search-forward
2155 "^[ \t]*:END:[ \t]*$" limit t)))
2156 ;; Stop at valid comments.
2157 (looking-at "[ \t]*#\\(?: \\|$\\)")
2158 ;; Stop at valid dynamic blocks.
2159 (and (looking-at org-dblock-start-re)
2160 (save-excursion
2161 (re-search-forward
2162 "^[ \t]*#\\+END:?[ \t]*$" limit t)))
2163 ;; Stop at valid blocks.
2164 (and (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
2165 (save-excursion
2166 (re-search-forward
2167 (format "^[ \t]*#\\+END_%s[ \t]*$"
2168 (regexp-quote
2169 (org-match-string-no-properties 1)))
2170 limit t)))
2171 ;; Stop at valid latex environments.
2172 (and (looking-at
2173 "[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}")
2174 (save-excursion
2175 (re-search-forward
2176 (format "^[ \t]*\\\\end{%s}[ \t]*$"
2177 (regexp-quote
2178 (org-match-string-no-properties 1)))
2179 limit t)))
2180 ;; Stop at valid keywords.
2181 (looking-at "[ \t]*#\\+\\S-+:")
2182 ;; Skip everything else.
2183 (not
2184 (progn
2185 (end-of-line)
2186 (re-search-forward org-element-paragraph-separate
2187 limit 'm)))))
2188 (beginning-of-line)))
2189 (if (= (point) limit) limit
2190 (goto-char (line-beginning-position)))))
2191 (contents-end (progn (skip-chars-backward " \r\t\n" contents-begin)
2192 (forward-line)
2193 (point)))
2194 (end (progn (skip-chars-forward " \r\t\n" limit)
2195 (if (eobp) (point) (line-beginning-position)))))
2196 (list 'paragraph
2197 (nconc
2198 (list :begin begin
2199 :end end
2200 :contents-begin contents-begin
2201 :contents-end contents-end
2202 :post-blank (count-lines before-blank end)
2203 :post-affiliated contents-begin)
2204 (cdr affiliated))))))
2206 (defun org-element-paragraph-interpreter (paragraph contents)
2207 "Interpret PARAGRAPH element as Org syntax.
2208 CONTENTS is the contents of the element."
2209 contents)
2212 ;;;; Planning
2214 (defun org-element-planning-parser (limit)
2215 "Parse a planning.
2217 LIMIT bounds the search.
2219 Return a list whose CAR is `planning' and CDR is a plist
2220 containing `:closed', `:deadline', `:scheduled', `:begin', `:end'
2221 and `:post-blank' keywords."
2222 (save-excursion
2223 (let* ((case-fold-search nil)
2224 (begin (point))
2225 (post-blank (let ((before-blank (progn (forward-line) (point))))
2226 (skip-chars-forward " \r\t\n" limit)
2227 (skip-chars-backward " \t")
2228 (unless (bolp) (end-of-line))
2229 (count-lines before-blank (point))))
2230 (end (point))
2231 closed deadline scheduled)
2232 (goto-char begin)
2233 (while (re-search-forward org-keyword-time-not-clock-regexp end t)
2234 (goto-char (match-end 1))
2235 (skip-chars-forward " \t" end)
2236 (let ((keyword (match-string 1))
2237 (time (org-element-timestamp-parser)))
2238 (cond ((equal keyword org-closed-string) (setq closed time))
2239 ((equal keyword org-deadline-string) (setq deadline time))
2240 (t (setq scheduled time)))))
2241 (list 'planning
2242 (list :closed closed
2243 :deadline deadline
2244 :scheduled scheduled
2245 :begin begin
2246 :end end
2247 :post-blank post-blank)))))
2249 (defun org-element-planning-interpreter (planning contents)
2250 "Interpret PLANNING element as Org syntax.
2251 CONTENTS is nil."
2252 (mapconcat
2253 'identity
2254 (delq nil
2255 (list (let ((deadline (org-element-property :deadline planning)))
2256 (when deadline
2257 (concat org-deadline-string " "
2258 (org-element-timestamp-interpreter deadline nil))))
2259 (let ((scheduled (org-element-property :scheduled planning)))
2260 (when scheduled
2261 (concat org-scheduled-string " "
2262 (org-element-timestamp-interpreter scheduled nil))))
2263 (let ((closed (org-element-property :closed planning)))
2264 (when closed
2265 (concat org-closed-string " "
2266 (org-element-timestamp-interpreter closed nil))))))
2267 " "))
2270 ;;;; Quote Section
2272 (defun org-element-quote-section-parser (limit)
2273 "Parse a quote section.
2275 LIMIT bounds the search.
2277 Return a list whose CAR is `quote-section' and CDR is a plist
2278 containing `:begin', `:end', `:value' and `:post-blank' keywords.
2280 Assume point is at beginning of the section."
2281 (save-excursion
2282 (let* ((begin (point))
2283 (end (progn (org-with-limited-levels (outline-next-heading))
2284 (point)))
2285 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
2286 (forward-line)
2287 (point)))
2288 (value (buffer-substring-no-properties begin pos-before-blank)))
2289 (list 'quote-section
2290 (list :begin begin
2291 :end end
2292 :value value
2293 :post-blank (count-lines pos-before-blank end))))))
2295 (defun org-element-quote-section-interpreter (quote-section contents)
2296 "Interpret QUOTE-SECTION element as Org syntax.
2297 CONTENTS is nil."
2298 (org-element-property :value quote-section))
2301 ;;;; Src Block
2303 (defun org-element-src-block-parser (limit affiliated)
2304 "Parse a src block.
2306 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2307 the buffer position at the beginning of the first affiliated
2308 keyword and CDR is a plist of affiliated keywords along with
2309 their value.
2311 Return a list whose CAR is `src-block' and CDR is a plist
2312 containing `:language', `:switches', `:parameters', `:begin',
2313 `:end', `:hiddenp', `:number-lines', `:retain-labels',
2314 `:use-labels', `:label-fmt', `:preserve-indent', `:value',
2315 `:post-blank' and `:post-affiliated' keywords.
2317 Assume point is at the beginning of the block."
2318 (let ((case-fold-search t))
2319 (if (not (save-excursion (re-search-forward "^[ \t]*#\\+END_SRC[ \t]*$"
2320 limit t)))
2321 ;; Incomplete block: parse it as a paragraph.
2322 (org-element-paragraph-parser limit affiliated)
2323 (let ((contents-end (match-beginning 0)))
2324 (save-excursion
2325 (let* ((begin (car affiliated))
2326 (post-affiliated (point))
2327 ;; Get language as a string.
2328 (language
2329 (progn
2330 (looking-at
2331 (concat "^[ \t]*#\\+BEGIN_SRC"
2332 "\\(?: +\\(\\S-+\\)\\)?"
2333 "\\(\\(?: +\\(?:-l \".*?\"\\|[-+][A-Za-z]\\)\\)+\\)?"
2334 "\\(.*\\)[ \t]*$"))
2335 (org-match-string-no-properties 1)))
2336 ;; Get switches.
2337 (switches (org-match-string-no-properties 2))
2338 ;; Get parameters.
2339 (parameters (org-match-string-no-properties 3))
2340 ;; Switches analysis
2341 (number-lines
2342 (cond ((not switches) nil)
2343 ((string-match "-n\\>" switches) 'new)
2344 ((string-match "+n\\>" switches) 'continued)))
2345 (preserve-indent (or org-src-preserve-indentation
2346 (and switches
2347 (string-match "-i\\>" switches))))
2348 (label-fmt
2349 (and switches
2350 (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
2351 (match-string 1 switches)))
2352 ;; Should labels be retained in (or stripped from)
2353 ;; src blocks?
2354 (retain-labels
2355 (or (not switches)
2356 (not (string-match "-r\\>" switches))
2357 (and number-lines (string-match "-k\\>" switches))))
2358 ;; What should code-references use - labels or
2359 ;; line-numbers?
2360 (use-labels
2361 (or (not switches)
2362 (and retain-labels
2363 (not (string-match "-k\\>" switches)))))
2364 ;; Indentation.
2365 (block-ind (progn (skip-chars-forward " \t") (current-column)))
2366 ;; Get visibility status.
2367 (hidden (progn (forward-line) (org-invisible-p2)))
2368 ;; Retrieve code.
2369 (value (org-element--remove-indentation
2370 (org-unescape-code-in-string
2371 (buffer-substring-no-properties
2372 (point) contents-end))
2373 (and preserve-indent block-ind)))
2374 (pos-before-blank (progn (goto-char contents-end)
2375 (forward-line)
2376 (point)))
2377 ;; Get position after ending blank lines.
2378 (end (progn (skip-chars-forward " \r\t\n" limit)
2379 (if (eobp) (point) (line-beginning-position)))))
2380 (list 'src-block
2381 (nconc
2382 (list :language language
2383 :switches (and (org-string-nw-p switches)
2384 (org-trim switches))
2385 :parameters (and (org-string-nw-p parameters)
2386 (org-trim parameters))
2387 :begin begin
2388 :end end
2389 :number-lines number-lines
2390 :preserve-indent preserve-indent
2391 :retain-labels retain-labels
2392 :use-labels use-labels
2393 :label-fmt label-fmt
2394 :hiddenp hidden
2395 :value value
2396 :post-blank (count-lines pos-before-blank end)
2397 :post-affiliated post-affiliated)
2398 (cdr affiliated)))))))))
2400 (defun org-element-src-block-interpreter (src-block contents)
2401 "Interpret SRC-BLOCK element as Org syntax.
2402 CONTENTS is nil."
2403 (let ((lang (org-element-property :language src-block))
2404 (switches (org-element-property :switches src-block))
2405 (params (org-element-property :parameters src-block))
2406 (value (let ((val (org-element-property :value src-block)))
2407 (cond
2408 ((org-element-property :preserve-indent src-block) val)
2409 ((zerop org-edit-src-content-indentation) val)
2411 (let ((ind (make-string
2412 org-edit-src-content-indentation 32)))
2413 (replace-regexp-in-string
2414 "\\(^\\)[ \t]*\\S-" ind val nil nil 1)))))))
2415 (concat (format "#+BEGIN_SRC%s\n"
2416 (concat (and lang (concat " " lang))
2417 (and switches (concat " " switches))
2418 (and params (concat " " params))))
2419 (org-escape-code-in-string value)
2420 "#+END_SRC")))
2423 ;;;; Table
2425 (defun org-element-table-parser (limit affiliated)
2426 "Parse a table at point.
2428 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2429 the buffer position at the beginning of the first affiliated
2430 keyword and CDR is a plist of affiliated keywords along with
2431 their value.
2433 Return a list whose CAR is `table' and CDR is a plist containing
2434 `:begin', `:end', `:tblfm', `:type', `:contents-begin',
2435 `:contents-end', `:value', `:post-blank' and `:post-affiliated'
2436 keywords.
2438 Assume point is at the beginning of the table."
2439 (save-excursion
2440 (let* ((case-fold-search t)
2441 (table-begin (point))
2442 (type (if (org-at-table.el-p) 'table.el 'org))
2443 (begin (car affiliated))
2444 (table-end
2445 (if (re-search-forward org-table-any-border-regexp limit 'm)
2446 (goto-char (match-beginning 0))
2447 (point)))
2448 (tblfm (let (acc)
2449 (while (looking-at "[ \t]*#\\+TBLFM: +\\(.*\\)[ \t]*$")
2450 (push (org-match-string-no-properties 1) acc)
2451 (forward-line))
2452 acc))
2453 (pos-before-blank (point))
2454 (end (progn (skip-chars-forward " \r\t\n" limit)
2455 (if (eobp) (point) (line-beginning-position)))))
2456 (list 'table
2457 (nconc
2458 (list :begin begin
2459 :end end
2460 :type type
2461 :tblfm tblfm
2462 ;; Only `org' tables have contents. `table.el' tables
2463 ;; use a `:value' property to store raw table as
2464 ;; a string.
2465 :contents-begin (and (eq type 'org) table-begin)
2466 :contents-end (and (eq type 'org) table-end)
2467 :value (and (eq type 'table.el)
2468 (buffer-substring-no-properties
2469 table-begin table-end))
2470 :post-blank (count-lines pos-before-blank end)
2471 :post-affiliated table-begin)
2472 (cdr affiliated))))))
2474 (defun org-element-table-interpreter (table contents)
2475 "Interpret TABLE element as Org syntax.
2476 CONTENTS is nil."
2477 (if (eq (org-element-property :type table) 'table.el)
2478 (org-remove-indentation (org-element-property :value table))
2479 (concat (with-temp-buffer (insert contents)
2480 (org-table-align)
2481 (buffer-string))
2482 (mapconcat (lambda (fm) (concat "#+TBLFM: " fm))
2483 (reverse (org-element-property :tblfm table))
2484 "\n"))))
2487 ;;;; Table Row
2489 (defun org-element-table-row-parser (limit)
2490 "Parse table row at point.
2492 LIMIT bounds the search.
2494 Return a list whose CAR is `table-row' and CDR is a plist
2495 containing `:begin', `:end', `:contents-begin', `:contents-end',
2496 `:type' and `:post-blank' keywords."
2497 (save-excursion
2498 (let* ((type (if (looking-at "^[ \t]*|-") 'rule 'standard))
2499 (begin (point))
2500 ;; A table rule has no contents. In that case, ensure
2501 ;; CONTENTS-BEGIN matches CONTENTS-END.
2502 (contents-begin (and (eq type 'standard)
2503 (search-forward "|")
2504 (point)))
2505 (contents-end (and (eq type 'standard)
2506 (progn
2507 (end-of-line)
2508 (skip-chars-backward " \t")
2509 (point))))
2510 (end (progn (forward-line) (point))))
2511 (list 'table-row
2512 (list :type type
2513 :begin begin
2514 :end end
2515 :contents-begin contents-begin
2516 :contents-end contents-end
2517 :post-blank 0)))))
2519 (defun org-element-table-row-interpreter (table-row contents)
2520 "Interpret TABLE-ROW element as Org syntax.
2521 CONTENTS is the contents of the table row."
2522 (if (eq (org-element-property :type table-row) 'rule) "|-"
2523 (concat "| " contents)))
2526 ;;;; Verse Block
2528 (defun org-element-verse-block-parser (limit affiliated)
2529 "Parse a verse block.
2531 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2532 the buffer position at the beginning of the first affiliated
2533 keyword and CDR is a plist of affiliated keywords along with
2534 their value.
2536 Return a list whose CAR is `verse-block' and CDR is a plist
2537 containing `:begin', `:end', `:contents-begin', `:contents-end',
2538 `:hiddenp', `:post-blank' and `:post-affiliated' keywords.
2540 Assume point is at beginning of the block."
2541 (let ((case-fold-search t))
2542 (if (not (save-excursion
2543 (re-search-forward "^[ \t]*#\\+END_VERSE[ \t]*$" limit t)))
2544 ;; Incomplete block: parse it as a paragraph.
2545 (org-element-paragraph-parser limit affiliated)
2546 (let ((contents-end (match-beginning 0)))
2547 (save-excursion
2548 (let* ((begin (car affiliated))
2549 (post-affiliated (point))
2550 (hidden (progn (forward-line) (org-invisible-p2)))
2551 (contents-begin (point))
2552 (pos-before-blank (progn (goto-char contents-end)
2553 (forward-line)
2554 (point)))
2555 (end (progn (skip-chars-forward " \r\t\n" limit)
2556 (if (eobp) (point) (line-beginning-position)))))
2557 (list 'verse-block
2558 (nconc
2559 (list :begin begin
2560 :end end
2561 :contents-begin contents-begin
2562 :contents-end contents-end
2563 :hiddenp hidden
2564 :post-blank (count-lines pos-before-blank end)
2565 :post-affiliated post-affiliated)
2566 (cdr affiliated)))))))))
2568 (defun org-element-verse-block-interpreter (verse-block contents)
2569 "Interpret VERSE-BLOCK element as Org syntax.
2570 CONTENTS is verse block contents."
2571 (format "#+BEGIN_VERSE\n%s#+END_VERSE" contents))
2575 ;;; Objects
2577 ;; Unlike to elements, interstices can be found between objects.
2578 ;; That's why, along with the parser, successor functions are provided
2579 ;; for each object. Some objects share the same successor (i.e. `code'
2580 ;; and `verbatim' objects).
2582 ;; A successor must accept a single argument bounding the search. It
2583 ;; will return either a cons cell whose CAR is the object's type, as
2584 ;; a symbol, and CDR the position of its next occurrence, or nil.
2586 ;; Successors follow the naming convention:
2587 ;; org-element-NAME-successor, where NAME is the name of the
2588 ;; successor, as defined in `org-element-all-successors'.
2590 ;; Some object types (i.e. `italic') are recursive. Restrictions on
2591 ;; object types they can contain will be specified in
2592 ;; `org-element-object-restrictions'.
2594 ;; Adding a new type of object is simple. Implement a successor,
2595 ;; a parser, and an interpreter for it, all following the naming
2596 ;; convention. Register type in `org-element-all-objects' and
2597 ;; successor in `org-element-all-successors'. Maybe tweak
2598 ;; restrictions about it, and that's it.
2601 ;;;; Bold
2603 (defun org-element-bold-parser ()
2604 "Parse bold object at point.
2606 Return a list whose CAR is `bold' and CDR is a plist with
2607 `:begin', `:end', `:contents-begin' and `:contents-end' and
2608 `:post-blank' keywords.
2610 Assume point is at the first star marker."
2611 (save-excursion
2612 (unless (bolp) (backward-char 1))
2613 (looking-at org-emph-re)
2614 (let ((begin (match-beginning 2))
2615 (contents-begin (match-beginning 4))
2616 (contents-end (match-end 4))
2617 (post-blank (progn (goto-char (match-end 2))
2618 (skip-chars-forward " \t")))
2619 (end (point)))
2620 (list 'bold
2621 (list :begin begin
2622 :end end
2623 :contents-begin contents-begin
2624 :contents-end contents-end
2625 :post-blank post-blank)))))
2627 (defun org-element-bold-interpreter (bold contents)
2628 "Interpret BOLD object as Org syntax.
2629 CONTENTS is the contents of the object."
2630 (format "*%s*" contents))
2632 (defun org-element-text-markup-successor ()
2633 "Search for the next text-markup object.
2635 Return value is a cons cell whose CAR is a symbol among `bold',
2636 `italic', `underline', `strike-through', `code' and `verbatim'
2637 and CDR is beginning position."
2638 (save-excursion
2639 (unless (bolp) (backward-char))
2640 (when (re-search-forward org-emph-re nil t)
2641 (let ((marker (match-string 3)))
2642 (cons (cond
2643 ((equal marker "*") 'bold)
2644 ((equal marker "/") 'italic)
2645 ((equal marker "_") 'underline)
2646 ((equal marker "+") 'strike-through)
2647 ((equal marker "~") 'code)
2648 ((equal marker "=") 'verbatim)
2649 (t (error "Unknown marker at %d" (match-beginning 3))))
2650 (match-beginning 2))))))
2653 ;;;; Code
2655 (defun org-element-code-parser ()
2656 "Parse code object at point.
2658 Return a list whose CAR is `code' and CDR is a plist with
2659 `:value', `:begin', `:end' and `:post-blank' keywords.
2661 Assume point is at the first tilde marker."
2662 (save-excursion
2663 (unless (bolp) (backward-char 1))
2664 (looking-at org-emph-re)
2665 (let ((begin (match-beginning 2))
2666 (value (org-match-string-no-properties 4))
2667 (post-blank (progn (goto-char (match-end 2))
2668 (skip-chars-forward " \t")))
2669 (end (point)))
2670 (list 'code
2671 (list :value value
2672 :begin begin
2673 :end end
2674 :post-blank post-blank)))))
2676 (defun org-element-code-interpreter (code contents)
2677 "Interpret CODE object as Org syntax.
2678 CONTENTS is nil."
2679 (format "~%s~" (org-element-property :value code)))
2682 ;;;; Entity
2684 (defun org-element-entity-parser ()
2685 "Parse entity at point.
2687 Return a list whose CAR is `entity' and CDR a plist with
2688 `:begin', `:end', `:latex', `:latex-math-p', `:html', `:latin1',
2689 `:utf-8', `:ascii', `:use-brackets-p' and `:post-blank' as
2690 keywords.
2692 Assume point is at the beginning of the entity."
2693 (save-excursion
2694 (looking-at "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)")
2695 (let* ((value (org-entity-get (match-string 1)))
2696 (begin (match-beginning 0))
2697 (bracketsp (string= (match-string 2) "{}"))
2698 (post-blank (progn (goto-char (match-end 1))
2699 (when bracketsp (forward-char 2))
2700 (skip-chars-forward " \t")))
2701 (end (point)))
2702 (list 'entity
2703 (list :name (car value)
2704 :latex (nth 1 value)
2705 :latex-math-p (nth 2 value)
2706 :html (nth 3 value)
2707 :ascii (nth 4 value)
2708 :latin1 (nth 5 value)
2709 :utf-8 (nth 6 value)
2710 :begin begin
2711 :end end
2712 :use-brackets-p bracketsp
2713 :post-blank post-blank)))))
2715 (defun org-element-entity-interpreter (entity contents)
2716 "Interpret ENTITY object as Org syntax.
2717 CONTENTS is nil."
2718 (concat "\\"
2719 (org-element-property :name entity)
2720 (when (org-element-property :use-brackets-p entity) "{}")))
2722 (defun org-element-latex-or-entity-successor ()
2723 "Search for the next latex-fragment or entity object.
2725 Return value is a cons cell whose CAR is `entity' or
2726 `latex-fragment' and CDR is beginning position."
2727 (save-excursion
2728 (unless (bolp) (backward-char))
2729 (let ((matchers (cdr org-latex-regexps))
2730 ;; ENTITY-RE matches both LaTeX commands and Org entities.
2731 (entity-re
2732 "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))
2733 (when (re-search-forward
2734 (concat (mapconcat #'cadr matchers "\\|") "\\|" entity-re) nil t)
2735 (goto-char (match-beginning 0))
2736 (if (looking-at entity-re)
2737 ;; Determine if it's a real entity or a LaTeX command.
2738 (cons (if (org-entity-get (match-string 1)) 'entity 'latex-fragment)
2739 (match-beginning 0))
2740 ;; No entity nor command: point is at a LaTeX fragment.
2741 ;; Determine its type to get the correct beginning position.
2742 (cons 'latex-fragment
2743 (catch 'return
2744 (dolist (e matchers)
2745 (when (looking-at (nth 1 e))
2746 (throw 'return (match-beginning (nth 2 e)))))
2747 (point))))))))
2750 ;;;; Export Snippet
2752 (defun org-element-export-snippet-parser ()
2753 "Parse export snippet at point.
2755 Return a list whose CAR is `export-snippet' and CDR a plist with
2756 `:begin', `:end', `:back-end', `:value' and `:post-blank' as
2757 keywords.
2759 Assume point is at the beginning of the snippet."
2760 (save-excursion
2761 (re-search-forward "@@\\([-A-Za-z0-9]+\\):" nil t)
2762 (let* ((begin (match-beginning 0))
2763 (back-end (org-match-string-no-properties 1))
2764 (value (buffer-substring-no-properties
2765 (point)
2766 (progn (re-search-forward "@@" nil t) (match-beginning 0))))
2767 (post-blank (skip-chars-forward " \t"))
2768 (end (point)))
2769 (list 'export-snippet
2770 (list :back-end back-end
2771 :value value
2772 :begin begin
2773 :end end
2774 :post-blank post-blank)))))
2776 (defun org-element-export-snippet-interpreter (export-snippet contents)
2777 "Interpret EXPORT-SNIPPET object as Org syntax.
2778 CONTENTS is nil."
2779 (format "@@%s:%s@@"
2780 (org-element-property :back-end export-snippet)
2781 (org-element-property :value export-snippet)))
2783 (defun org-element-export-snippet-successor ()
2784 "Search for the next export-snippet object.
2786 Return value is a cons cell whose CAR is `export-snippet' and CDR
2787 its beginning position."
2788 (save-excursion
2789 (let (beg)
2790 (when (and (re-search-forward "@@[-A-Za-z0-9]+:" nil t)
2791 (setq beg (match-beginning 0))
2792 (search-forward "@@" nil t))
2793 (cons 'export-snippet beg)))))
2796 ;;;; Footnote Reference
2798 (defun org-element-footnote-reference-parser ()
2799 "Parse footnote reference at point.
2801 Return a list whose CAR is `footnote-reference' and CDR a plist
2802 with `:label', `:type', `:inline-definition', `:begin', `:end'
2803 and `:post-blank' as keywords."
2804 (save-excursion
2805 (looking-at org-footnote-re)
2806 (let* ((begin (point))
2807 (label (or (org-match-string-no-properties 2)
2808 (org-match-string-no-properties 3)
2809 (and (match-string 1)
2810 (concat "fn:" (org-match-string-no-properties 1)))))
2811 (type (if (or (not label) (match-string 1)) 'inline 'standard))
2812 (inner-begin (match-end 0))
2813 (inner-end
2814 (let ((count 1))
2815 (forward-char)
2816 (while (and (> count 0) (re-search-forward "[][]" nil t))
2817 (if (equal (match-string 0) "[") (incf count) (decf count)))
2818 (1- (point))))
2819 (post-blank (progn (goto-char (1+ inner-end))
2820 (skip-chars-forward " \t")))
2821 (end (point))
2822 (footnote-reference
2823 (list 'footnote-reference
2824 (list :label label
2825 :type type
2826 :begin begin
2827 :end end
2828 :post-blank post-blank))))
2829 (org-element-put-property
2830 footnote-reference :inline-definition
2831 (and (eq type 'inline)
2832 (org-element-parse-secondary-string
2833 (buffer-substring inner-begin inner-end)
2834 (org-element-restriction 'footnote-reference)
2835 footnote-reference))))))
2837 (defun org-element-footnote-reference-interpreter (footnote-reference contents)
2838 "Interpret FOOTNOTE-REFERENCE object as Org syntax.
2839 CONTENTS is nil."
2840 (let ((label (or (org-element-property :label footnote-reference) "fn:"))
2841 (def
2842 (let ((inline-def
2843 (org-element-property :inline-definition footnote-reference)))
2844 (if (not inline-def) ""
2845 (concat ":" (org-element-interpret-data inline-def))))))
2846 (format "[%s]" (concat label def))))
2848 (defun org-element-footnote-reference-successor ()
2849 "Search for the next footnote-reference object.
2851 Return value is a cons cell whose CAR is `footnote-reference' and
2852 CDR is beginning position."
2853 (save-excursion
2854 (catch 'exit
2855 (while (re-search-forward org-footnote-re nil t)
2856 (save-excursion
2857 (let ((beg (match-beginning 0))
2858 (count 1))
2859 (backward-char)
2860 (while (re-search-forward "[][]" nil t)
2861 (if (equal (match-string 0) "[") (incf count) (decf count))
2862 (when (zerop count)
2863 (throw 'exit (cons 'footnote-reference beg))))))))))
2866 ;;;; Inline Babel Call
2868 (defun org-element-inline-babel-call-parser ()
2869 "Parse inline babel call at point.
2871 Return a list whose CAR is `inline-babel-call' and CDR a plist
2872 with `:begin', `:end', `:info' and `:post-blank' as keywords.
2874 Assume point is at the beginning of the babel call."
2875 (save-excursion
2876 (unless (bolp) (backward-char))
2877 (looking-at org-babel-inline-lob-one-liner-regexp)
2878 (let ((info (save-match-data (org-babel-lob-get-info)))
2879 (begin (match-end 1))
2880 (post-blank (progn (goto-char (match-end 0))
2881 (skip-chars-forward " \t")))
2882 (end (point)))
2883 (list 'inline-babel-call
2884 (list :begin begin
2885 :end end
2886 :info info
2887 :post-blank post-blank)))))
2889 (defun org-element-inline-babel-call-interpreter (inline-babel-call contents)
2890 "Interpret INLINE-BABEL-CALL object as Org syntax.
2891 CONTENTS is nil."
2892 (let* ((babel-info (org-element-property :info inline-babel-call))
2893 (main-source (car babel-info))
2894 (post-options (nth 1 babel-info)))
2895 (concat "call_"
2896 (if (string-match "\\[\\(\\[.*?\\]\\)\\]" main-source)
2897 ;; Remove redundant square brackets.
2898 (replace-match
2899 (match-string 1 main-source) nil nil main-source)
2900 main-source)
2901 (and post-options (format "[%s]" post-options)))))
2903 (defun org-element-inline-babel-call-successor ()
2904 "Search for the next inline-babel-call object.
2906 Return value is a cons cell whose CAR is `inline-babel-call' and
2907 CDR is beginning position."
2908 (save-excursion
2909 ;; Use a simplified version of
2910 ;; `org-babel-inline-lob-one-liner-regexp'.
2911 (when (re-search-forward
2912 "call_\\([^()\n]+?\\)\\(?:\\[.*?\\]\\)?([^\n]*?)\\(\\[.*?\\]\\)?"
2913 nil t)
2914 (cons 'inline-babel-call (match-beginning 0)))))
2917 ;;;; Inline Src Block
2919 (defun org-element-inline-src-block-parser ()
2920 "Parse inline source block at point.
2922 Return a list whose CAR is `inline-src-block' and CDR a plist
2923 with `:begin', `:end', `:language', `:value', `:parameters' and
2924 `:post-blank' as keywords.
2926 Assume point is at the beginning of the inline src block."
2927 (save-excursion
2928 (unless (bolp) (backward-char))
2929 (looking-at org-babel-inline-src-block-regexp)
2930 (let ((begin (match-beginning 1))
2931 (language (org-match-string-no-properties 2))
2932 (parameters (org-match-string-no-properties 4))
2933 (value (org-match-string-no-properties 5))
2934 (post-blank (progn (goto-char (match-end 0))
2935 (skip-chars-forward " \t")))
2936 (end (point)))
2937 (list 'inline-src-block
2938 (list :language language
2939 :value value
2940 :parameters parameters
2941 :begin begin
2942 :end end
2943 :post-blank post-blank)))))
2945 (defun org-element-inline-src-block-interpreter (inline-src-block contents)
2946 "Interpret INLINE-SRC-BLOCK object as Org syntax.
2947 CONTENTS is nil."
2948 (let ((language (org-element-property :language inline-src-block))
2949 (arguments (org-element-property :parameters inline-src-block))
2950 (body (org-element-property :value inline-src-block)))
2951 (format "src_%s%s{%s}"
2952 language
2953 (if arguments (format "[%s]" arguments) "")
2954 body)))
2956 (defun org-element-inline-src-block-successor ()
2957 "Search for the next inline-babel-call element.
2959 Return value is a cons cell whose CAR is `inline-babel-call' and
2960 CDR is beginning position."
2961 (save-excursion
2962 (unless (bolp) (backward-char))
2963 (when (re-search-forward org-babel-inline-src-block-regexp nil t)
2964 (cons 'inline-src-block (match-beginning 1)))))
2966 ;;;; Italic
2968 (defun org-element-italic-parser ()
2969 "Parse italic object at point.
2971 Return a list whose CAR is `italic' and CDR is a plist with
2972 `:begin', `:end', `:contents-begin' and `:contents-end' and
2973 `:post-blank' keywords.
2975 Assume point is at the first slash marker."
2976 (save-excursion
2977 (unless (bolp) (backward-char 1))
2978 (looking-at org-emph-re)
2979 (let ((begin (match-beginning 2))
2980 (contents-begin (match-beginning 4))
2981 (contents-end (match-end 4))
2982 (post-blank (progn (goto-char (match-end 2))
2983 (skip-chars-forward " \t")))
2984 (end (point)))
2985 (list 'italic
2986 (list :begin begin
2987 :end end
2988 :contents-begin contents-begin
2989 :contents-end contents-end
2990 :post-blank post-blank)))))
2992 (defun org-element-italic-interpreter (italic contents)
2993 "Interpret ITALIC object as Org syntax.
2994 CONTENTS is the contents of the object."
2995 (format "/%s/" contents))
2998 ;;;; Latex Fragment
3000 (defun org-element-latex-fragment-parser ()
3001 "Parse LaTeX fragment at point.
3003 Return a list whose CAR is `latex-fragment' and CDR a plist with
3004 `:value', `:begin', `:end', and `:post-blank' as keywords.
3006 Assume point is at the beginning of the LaTeX fragment."
3007 (save-excursion
3008 (let* ((begin (point))
3009 (substring-match
3010 (catch 'exit
3011 (dolist (e (cdr org-latex-regexps))
3012 (let ((latex-regexp (nth 1 e)))
3013 (when (or (looking-at latex-regexp)
3014 (and (not (bobp))
3015 (save-excursion
3016 (backward-char)
3017 (looking-at latex-regexp))))
3018 (throw 'exit (nth 2 e)))))
3019 ;; None found: it's a macro.
3020 (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")
3022 (value (org-match-string-no-properties substring-match))
3023 (post-blank (progn (goto-char (match-end substring-match))
3024 (skip-chars-forward " \t")))
3025 (end (point)))
3026 (list 'latex-fragment
3027 (list :value value
3028 :begin begin
3029 :end end
3030 :post-blank post-blank)))))
3032 (defun org-element-latex-fragment-interpreter (latex-fragment contents)
3033 "Interpret LATEX-FRAGMENT object as Org syntax.
3034 CONTENTS is nil."
3035 (org-element-property :value latex-fragment))
3037 ;;;; Line Break
3039 (defun org-element-line-break-parser ()
3040 "Parse line break at point.
3042 Return a list whose CAR is `line-break', and CDR a plist with
3043 `:begin', `:end' and `:post-blank' keywords.
3045 Assume point is at the beginning of the line break."
3046 (list 'line-break
3047 (list :begin (point)
3048 :end (progn (forward-line) (point))
3049 :post-blank 0)))
3051 (defun org-element-line-break-interpreter (line-break contents)
3052 "Interpret LINE-BREAK object as Org syntax.
3053 CONTENTS is nil."
3054 "\\\\\n")
3056 (defun org-element-line-break-successor ()
3057 "Search for the next line-break object.
3059 Return value is a cons cell whose CAR is `line-break' and CDR is
3060 beginning position."
3061 (save-excursion
3062 (let ((beg (and (re-search-forward "[^\\\\]\\(\\\\\\\\\\)[ \t]*$" nil t)
3063 (goto-char (match-beginning 1)))))
3064 ;; A line break can only happen on a non-empty line.
3065 (when (and beg (re-search-backward "\\S-" (point-at-bol) t))
3066 (cons 'line-break beg)))))
3069 ;;;; Link
3071 (defun org-element-link-parser ()
3072 "Parse link at point.
3074 Return a list whose CAR is `link' and CDR a plist with `:type',
3075 `:path', `:raw-link', `:application', `:search-option', `:begin',
3076 `:end', `:contents-begin', `:contents-end' and `:post-blank' as
3077 keywords.
3079 Assume point is at the beginning of the link."
3080 (save-excursion
3081 (let ((begin (point))
3082 end contents-begin contents-end link-end post-blank path type
3083 raw-link link search-option application)
3084 (cond
3085 ;; Type 1: Text targeted from a radio target.
3086 ((and org-target-link-regexp (looking-at org-target-link-regexp))
3087 (setq type "radio"
3088 link-end (match-end 0)
3089 path (org-match-string-no-properties 0)))
3090 ;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]]
3091 ((looking-at org-bracket-link-regexp)
3092 (setq contents-begin (match-beginning 3)
3093 contents-end (match-end 3)
3094 link-end (match-end 0)
3095 ;; RAW-LINK is the original link. Expand any
3096 ;; abbreviation in it.
3097 raw-link (org-translate-link
3098 (org-link-expand-abbrev
3099 (org-match-string-no-properties 1))))
3100 ;; Determine TYPE of link and set PATH accordingly.
3101 (cond
3102 ;; File type.
3103 ((or (file-name-absolute-p raw-link)
3104 (string-match "^\\.\\.?/" raw-link))
3105 (setq type "file" path raw-link))
3106 ;; Explicit type (http, irc, bbdb...). See `org-link-types'.
3107 ((string-match org-link-re-with-space3 raw-link)
3108 (setq type (match-string 1 raw-link) path (match-string 2 raw-link)))
3109 ;; Id type: PATH is the id.
3110 ((string-match "^id:\\([-a-f0-9]+\\)" raw-link)
3111 (setq type "id" path (match-string 1 raw-link)))
3112 ;; Code-ref type: PATH is the name of the reference.
3113 ((string-match "^(\\(.*\\))$" raw-link)
3114 (setq type "coderef" path (match-string 1 raw-link)))
3115 ;; Custom-id type: PATH is the name of the custom id.
3116 ((= (aref raw-link 0) ?#)
3117 (setq type "custom-id" path (substring raw-link 1)))
3118 ;; Fuzzy type: Internal link either matches a target, an
3119 ;; headline name or nothing. PATH is the target or
3120 ;; headline's name.
3121 (t (setq type "fuzzy" path raw-link))))
3122 ;; Type 3: Plain link, i.e. http://orgmode.org
3123 ((looking-at org-plain-link-re)
3124 (setq raw-link (org-match-string-no-properties 0)
3125 type (org-match-string-no-properties 1)
3126 link-end (match-end 0)
3127 path (org-match-string-no-properties 2)))
3128 ;; Type 4: Angular link, i.e. <http://orgmode.org>
3129 ((looking-at org-angle-link-re)
3130 (setq raw-link (buffer-substring-no-properties
3131 (match-beginning 1) (match-end 2))
3132 type (org-match-string-no-properties 1)
3133 link-end (match-end 0)
3134 path (org-match-string-no-properties 2))))
3135 ;; In any case, deduce end point after trailing white space from
3136 ;; LINK-END variable.
3137 (setq post-blank (progn (goto-char link-end) (skip-chars-forward " \t"))
3138 end (point))
3139 ;; Extract search option and opening application out of
3140 ;; "file"-type links.
3141 (when (member type org-element-link-type-is-file)
3142 ;; Application.
3143 (cond ((string-match "^file\\+\\(.*\\)$" type)
3144 (setq application (match-string 1 type)))
3145 ((not (string-match "^file" type))
3146 (setq application type)))
3147 ;; Extract search option from PATH.
3148 (when (string-match "::\\(.*\\)$" path)
3149 (setq search-option (match-string 1 path)
3150 path (replace-match "" nil nil path)))
3151 ;; Make sure TYPE always reports "file".
3152 (setq type "file"))
3153 (list 'link
3154 (list :type type
3155 :path path
3156 :raw-link (or raw-link path)
3157 :application application
3158 :search-option search-option
3159 :begin begin
3160 :end end
3161 :contents-begin contents-begin
3162 :contents-end contents-end
3163 :post-blank post-blank)))))
3165 (defun org-element-link-interpreter (link contents)
3166 "Interpret LINK object as Org syntax.
3167 CONTENTS is the contents of the object, or nil."
3168 (let ((type (org-element-property :type link))
3169 (raw-link (org-element-property :raw-link link)))
3170 (if (string= type "radio") raw-link
3171 (format "[[%s]%s]"
3172 raw-link
3173 (if contents (format "[%s]" contents) "")))))
3175 (defun org-element-link-successor ()
3176 "Search for the next link object.
3178 Return value is a cons cell whose CAR is `link' and CDR is
3179 beginning position."
3180 (save-excursion
3181 (let ((link-regexp
3182 (if (not org-target-link-regexp) org-any-link-re
3183 (concat org-any-link-re "\\|" org-target-link-regexp))))
3184 (when (re-search-forward link-regexp nil t)
3185 (cons 'link (match-beginning 0))))))
3187 (defun org-element-plain-link-successor ()
3188 "Search for the next plain link object.
3190 Return value is a cons cell whose CAR is `link' and CDR is
3191 beginning position."
3192 (and (save-excursion (re-search-forward org-plain-link-re nil t))
3193 (cons 'link (match-beginning 0))))
3196 ;;;; Macro
3198 (defun org-element-macro-parser ()
3199 "Parse macro at point.
3201 Return a list whose CAR is `macro' and CDR a plist with `:key',
3202 `:args', `:begin', `:end', `:value' and `:post-blank' as
3203 keywords.
3205 Assume point is at the macro."
3206 (save-excursion
3207 (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
3208 (let ((begin (point))
3209 (key (downcase (org-match-string-no-properties 1)))
3210 (value (org-match-string-no-properties 0))
3211 (post-blank (progn (goto-char (match-end 0))
3212 (skip-chars-forward " \t")))
3213 (end (point))
3214 (args (let ((args (org-match-string-no-properties 3)))
3215 (when args
3216 ;; Do not use `org-split-string' since empty
3217 ;; strings are meaningful here.
3218 (split-string
3219 (replace-regexp-in-string
3220 "\\(\\\\*\\)\\(,\\)"
3221 (lambda (str)
3222 (let ((len (length (match-string 1 str))))
3223 (concat (make-string (/ len 2) ?\\)
3224 (if (zerop (mod len 2)) "\000" ","))))
3225 args nil t)
3226 "\000")))))
3227 (list 'macro
3228 (list :key key
3229 :value value
3230 :args args
3231 :begin begin
3232 :end end
3233 :post-blank post-blank)))))
3235 (defun org-element-macro-interpreter (macro contents)
3236 "Interpret MACRO object as Org syntax.
3237 CONTENTS is nil."
3238 (org-element-property :value macro))
3240 (defun org-element-macro-successor ()
3241 "Search for the next macro object.
3243 Return value is cons cell whose CAR is `macro' and CDR is
3244 beginning position."
3245 (save-excursion
3246 (when (re-search-forward
3247 "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
3248 nil t)
3249 (cons 'macro (match-beginning 0)))))
3252 ;;;; Radio-target
3254 (defun org-element-radio-target-parser ()
3255 "Parse radio target at point.
3257 Return a list whose CAR is `radio-target' and CDR a plist with
3258 `:begin', `:end', `:contents-begin', `:contents-end', `:value'
3259 and `:post-blank' as keywords.
3261 Assume point is at the radio target."
3262 (save-excursion
3263 (looking-at org-radio-target-regexp)
3264 (let ((begin (point))
3265 (contents-begin (match-beginning 1))
3266 (contents-end (match-end 1))
3267 (value (org-match-string-no-properties 1))
3268 (post-blank (progn (goto-char (match-end 0))
3269 (skip-chars-forward " \t")))
3270 (end (point)))
3271 (list 'radio-target
3272 (list :begin begin
3273 :end end
3274 :contents-begin contents-begin
3275 :contents-end contents-end
3276 :post-blank post-blank
3277 :value value)))))
3279 (defun org-element-radio-target-interpreter (target contents)
3280 "Interpret TARGET object as Org syntax.
3281 CONTENTS is the contents of the object."
3282 (concat "<<<" contents ">>>"))
3284 (defun org-element-radio-target-successor ()
3285 "Search for the next radio-target object.
3287 Return value is a cons cell whose CAR is `radio-target' and CDR
3288 is beginning position."
3289 (save-excursion
3290 (when (re-search-forward org-radio-target-regexp nil t)
3291 (cons 'radio-target (match-beginning 0)))))
3294 ;;;; Statistics Cookie
3296 (defun org-element-statistics-cookie-parser ()
3297 "Parse statistics cookie at point.
3299 Return a list whose CAR is `statistics-cookie', and CDR a plist
3300 with `:begin', `:end', `:value' and `:post-blank' keywords.
3302 Assume point is at the beginning of the statistics-cookie."
3303 (save-excursion
3304 (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
3305 (let* ((begin (point))
3306 (value (buffer-substring-no-properties
3307 (match-beginning 0) (match-end 0)))
3308 (post-blank (progn (goto-char (match-end 0))
3309 (skip-chars-forward " \t")))
3310 (end (point)))
3311 (list 'statistics-cookie
3312 (list :begin begin
3313 :end end
3314 :value value
3315 :post-blank post-blank)))))
3317 (defun org-element-statistics-cookie-interpreter (statistics-cookie contents)
3318 "Interpret STATISTICS-COOKIE object as Org syntax.
3319 CONTENTS is nil."
3320 (org-element-property :value statistics-cookie))
3322 (defun org-element-statistics-cookie-successor ()
3323 "Search for the next statistics cookie object.
3325 Return value is a cons cell whose CAR is `statistics-cookie' and
3326 CDR is beginning position."
3327 (save-excursion
3328 (when (re-search-forward "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]" nil t)
3329 (cons 'statistics-cookie (match-beginning 0)))))
3332 ;;;; Strike-Through
3334 (defun org-element-strike-through-parser ()
3335 "Parse strike-through object at point.
3337 Return a list whose CAR is `strike-through' and CDR is a plist
3338 with `:begin', `:end', `:contents-begin' and `:contents-end' and
3339 `:post-blank' keywords.
3341 Assume point is at the first plus sign marker."
3342 (save-excursion
3343 (unless (bolp) (backward-char 1))
3344 (looking-at org-emph-re)
3345 (let ((begin (match-beginning 2))
3346 (contents-begin (match-beginning 4))
3347 (contents-end (match-end 4))
3348 (post-blank (progn (goto-char (match-end 2))
3349 (skip-chars-forward " \t")))
3350 (end (point)))
3351 (list 'strike-through
3352 (list :begin begin
3353 :end end
3354 :contents-begin contents-begin
3355 :contents-end contents-end
3356 :post-blank post-blank)))))
3358 (defun org-element-strike-through-interpreter (strike-through contents)
3359 "Interpret STRIKE-THROUGH object as Org syntax.
3360 CONTENTS is the contents of the object."
3361 (format "+%s+" contents))
3364 ;;;; Subscript
3366 (defun org-element-subscript-parser ()
3367 "Parse subscript at point.
3369 Return a list whose CAR is `subscript' and CDR a plist with
3370 `:begin', `:end', `:contents-begin', `:contents-end',
3371 `:use-brackets-p' and `:post-blank' as keywords.
3373 Assume point is at the underscore."
3374 (save-excursion
3375 (unless (bolp) (backward-char))
3376 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp)
3378 (not (looking-at org-match-substring-regexp))))
3379 (begin (match-beginning 2))
3380 (contents-begin (or (match-beginning 5)
3381 (match-beginning 3)))
3382 (contents-end (or (match-end 5) (match-end 3)))
3383 (post-blank (progn (goto-char (match-end 0))
3384 (skip-chars-forward " \t")))
3385 (end (point)))
3386 (list 'subscript
3387 (list :begin begin
3388 :end end
3389 :use-brackets-p bracketsp
3390 :contents-begin contents-begin
3391 :contents-end contents-end
3392 :post-blank post-blank)))))
3394 (defun org-element-subscript-interpreter (subscript contents)
3395 "Interpret SUBSCRIPT object as Org syntax.
3396 CONTENTS is the contents of the object."
3397 (format
3398 (if (org-element-property :use-brackets-p subscript) "_{%s}" "_%s")
3399 contents))
3401 (defun org-element-sub/superscript-successor ()
3402 "Search for the next sub/superscript object.
3404 Return value is a cons cell whose CAR is either `subscript' or
3405 `superscript' and CDR is beginning position."
3406 (save-excursion
3407 (unless (bolp) (backward-char))
3408 (when (re-search-forward org-match-substring-regexp nil t)
3409 (cons (if (string= (match-string 2) "_") 'subscript 'superscript)
3410 (match-beginning 2)))))
3413 ;;;; Superscript
3415 (defun org-element-superscript-parser ()
3416 "Parse superscript at point.
3418 Return a list whose CAR is `superscript' and CDR a plist with
3419 `:begin', `:end', `:contents-begin', `:contents-end',
3420 `:use-brackets-p' and `:post-blank' as keywords.
3422 Assume point is at the caret."
3423 (save-excursion
3424 (unless (bolp) (backward-char))
3425 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp) t
3426 (not (looking-at org-match-substring-regexp))))
3427 (begin (match-beginning 2))
3428 (contents-begin (or (match-beginning 5)
3429 (match-beginning 3)))
3430 (contents-end (or (match-end 5) (match-end 3)))
3431 (post-blank (progn (goto-char (match-end 0))
3432 (skip-chars-forward " \t")))
3433 (end (point)))
3434 (list 'superscript
3435 (list :begin begin
3436 :end end
3437 :use-brackets-p bracketsp
3438 :contents-begin contents-begin
3439 :contents-end contents-end
3440 :post-blank post-blank)))))
3442 (defun org-element-superscript-interpreter (superscript contents)
3443 "Interpret SUPERSCRIPT object as Org syntax.
3444 CONTENTS is the contents of the object."
3445 (format
3446 (if (org-element-property :use-brackets-p superscript) "^{%s}" "^%s")
3447 contents))
3450 ;;;; Table Cell
3452 (defun org-element-table-cell-parser ()
3453 "Parse table cell at point.
3455 Return a list whose CAR is `table-cell' and CDR is a plist
3456 containing `:begin', `:end', `:contents-begin', `:contents-end'
3457 and `:post-blank' keywords."
3458 (looking-at "[ \t]*\\(.*?\\)[ \t]*|")
3459 (let* ((begin (match-beginning 0))
3460 (end (match-end 0))
3461 (contents-begin (match-beginning 1))
3462 (contents-end (match-end 1)))
3463 (list 'table-cell
3464 (list :begin begin
3465 :end end
3466 :contents-begin contents-begin
3467 :contents-end contents-end
3468 :post-blank 0))))
3470 (defun org-element-table-cell-interpreter (table-cell contents)
3471 "Interpret TABLE-CELL element as Org syntax.
3472 CONTENTS is the contents of the cell, or nil."
3473 (concat " " contents " |"))
3475 (defun org-element-table-cell-successor ()
3476 "Search for the next table-cell object.
3478 Return value is a cons cell whose CAR is `table-cell' and CDR is
3479 beginning position."
3480 (when (looking-at "[ \t]*.*?[ \t]*|") (cons 'table-cell (point))))
3483 ;;;; Target
3485 (defun org-element-target-parser ()
3486 "Parse target at point.
3488 Return a list whose CAR is `target' and CDR a plist with
3489 `:begin', `:end', `:value' and `:post-blank' as keywords.
3491 Assume point is at the target."
3492 (save-excursion
3493 (looking-at org-target-regexp)
3494 (let ((begin (point))
3495 (value (org-match-string-no-properties 1))
3496 (post-blank (progn (goto-char (match-end 0))
3497 (skip-chars-forward " \t")))
3498 (end (point)))
3499 (list 'target
3500 (list :begin begin
3501 :end end
3502 :value value
3503 :post-blank post-blank)))))
3505 (defun org-element-target-interpreter (target contents)
3506 "Interpret TARGET object as Org syntax.
3507 CONTENTS is nil."
3508 (format "<<%s>>" (org-element-property :value target)))
3510 (defun org-element-target-successor ()
3511 "Search for the next target object.
3513 Return value is a cons cell whose CAR is `target' and CDR is
3514 beginning position."
3515 (save-excursion
3516 (when (re-search-forward org-target-regexp nil t)
3517 (cons 'target (match-beginning 0)))))
3520 ;;;; Timestamp
3522 (defun org-element-timestamp-parser ()
3523 "Parse time stamp at point.
3525 Return a list whose CAR is `timestamp', and CDR a plist with
3526 `:type', `:raw-value', `:year-start', `:month-start',
3527 `:day-start', `:hour-start', `:minute-start', `:year-end',
3528 `:month-end', `:day-end', `:hour-end', `:minute-end',
3529 `:repeater-type', `:repeater-value', `:repeater-unit',
3530 `:warning-type', `:warning-value', `:warning-unit', `:begin',
3531 `:end', `:value' and `:post-blank' keywords.
3533 Assume point is at the beginning of the timestamp."
3534 (save-excursion
3535 (let* ((begin (point))
3536 (activep (eq (char-after) ?<))
3537 (raw-value
3538 (progn
3539 (looking-at "\\([<[]\\(%%\\)?.*?\\)[]>]\\(?:--\\([<[].*?[]>]\\)\\)?")
3540 (match-string-no-properties 0)))
3541 (date-start (match-string-no-properties 1))
3542 (date-end (match-string 3))
3543 (diaryp (match-beginning 2))
3544 (post-blank (progn (goto-char (match-end 0))
3545 (skip-chars-forward " \t")))
3546 (end (point))
3547 (time-range
3548 (and (not diaryp)
3549 (string-match
3550 "[012]?[0-9]:[0-5][0-9]\\(-\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)"
3551 date-start)
3552 (cons (string-to-number (match-string 2 date-start))
3553 (string-to-number (match-string 3 date-start)))))
3554 (type (cond (diaryp 'diary)
3555 ((and activep (or date-end time-range)) 'active-range)
3556 (activep 'active)
3557 ((or date-end time-range) 'inactive-range)
3558 (t 'inactive)))
3559 (repeater-props
3560 (and (not diaryp)
3561 (string-match "\\([.+]?\\+\\)\\([0-9]+\\)\\([hdwmy]\\)"
3562 raw-value)
3563 (list
3564 :repeater-type
3565 (let ((type (match-string 1 raw-value)))
3566 (cond ((equal "++" type) 'catch-up)
3567 ((equal ".+" type) 'restart)
3568 (t 'cumulate)))
3569 :repeater-value (string-to-number (match-string 2 raw-value))
3570 :repeater-unit
3571 (case (string-to-char (match-string 3 raw-value))
3572 (?h 'hour) (?d 'day) (?w 'week) (?m 'month) (t 'year)))))
3573 (warning-props
3574 (and (not diaryp)
3575 (string-match "\\(-\\)?-\\([0-9]+\\)\\([hdwmy]\\)" raw-value)
3576 (list
3577 :warning-type (if (match-string 1 raw-value) 'first 'all)
3578 :warning-value (string-to-number (match-string 2 raw-value))
3579 :warning-unit
3580 (case (string-to-char (match-string 3 raw-value))
3581 (?h 'hour) (?d 'day) (?w 'week) (?m 'month) (t 'year)))))
3582 year-start month-start day-start hour-start minute-start year-end
3583 month-end day-end hour-end minute-end)
3584 ;; Parse date-start.
3585 (unless diaryp
3586 (let ((date (org-parse-time-string date-start t)))
3587 (setq year-start (nth 5 date)
3588 month-start (nth 4 date)
3589 day-start (nth 3 date)
3590 hour-start (nth 2 date)
3591 minute-start (nth 1 date))))
3592 ;; Compute date-end. It can be provided directly in time-stamp,
3593 ;; or extracted from time range. Otherwise, it defaults to the
3594 ;; same values as date-start.
3595 (unless diaryp
3596 (let ((date (and date-end (org-parse-time-string date-end t))))
3597 (setq year-end (or (nth 5 date) year-start)
3598 month-end (or (nth 4 date) month-start)
3599 day-end (or (nth 3 date) day-start)
3600 hour-end (or (nth 2 date) (car time-range) hour-start)
3601 minute-end (or (nth 1 date) (cdr time-range) minute-start))))
3602 (list 'timestamp
3603 (nconc (list :type type
3604 :raw-value raw-value
3605 :year-start year-start
3606 :month-start month-start
3607 :day-start day-start
3608 :hour-start hour-start
3609 :minute-start minute-start
3610 :year-end year-end
3611 :month-end month-end
3612 :day-end day-end
3613 :hour-end hour-end
3614 :minute-end minute-end
3615 :begin begin
3616 :end end
3617 :post-blank post-blank)
3618 repeater-props
3619 warning-props)))))
3621 (defun org-element-timestamp-interpreter (timestamp contents)
3622 "Interpret TIMESTAMP object as Org syntax.
3623 CONTENTS is nil."
3624 ;; Use `:raw-value' if specified.
3625 (or (org-element-property :raw-value timestamp)
3626 ;; Otherwise, build timestamp string.
3627 (let* ((repeat-string
3628 (concat
3629 (case (org-element-property :repeater-type timestamp)
3630 (cumulate "+") (catch-up "++") (restart ".+"))
3631 (let ((val (org-element-property :repeater-value timestamp)))
3632 (and val (number-to-string val)))
3633 (case (org-element-property :repeater-unit timestamp)
3634 (hour "h") (day "d") (week "w") (month "m") (year "y"))))
3635 (warning-string
3636 (concat
3637 (case (org-element-property :warning-type timestamp)
3638 (first "--")
3639 (all "-"))
3640 (let ((val (org-element-property :warning-value timestamp)))
3641 (and val (number-to-string val)))
3642 (case (org-element-property :warning-unit timestamp)
3643 (hour "h") (day "d") (week "w") (month "m") (year "y"))))
3644 (build-ts-string
3645 ;; Build an Org timestamp string from TIME. ACTIVEP is
3646 ;; non-nil when time stamp is active. If WITH-TIME-P is
3647 ;; non-nil, add a time part. HOUR-END and MINUTE-END
3648 ;; specify a time range in the timestamp. REPEAT-STRING
3649 ;; is the repeater string, if any.
3650 (lambda (time activep &optional with-time-p hour-end minute-end)
3651 (let ((ts (format-time-string
3652 (funcall (if with-time-p 'cdr 'car)
3653 org-time-stamp-formats)
3654 time)))
3655 (when (and hour-end minute-end)
3656 (string-match "[012]?[0-9]:[0-5][0-9]" ts)
3657 (setq ts
3658 (replace-match
3659 (format "\\&-%02d:%02d" hour-end minute-end)
3660 nil nil ts)))
3661 (unless activep (setq ts (format "[%s]" (substring ts 1 -1))))
3662 (dolist (s (list repeat-string warning-string))
3663 (when (org-string-nw-p s)
3664 (setq ts (concat (substring ts 0 -1)
3667 (substring ts -1)))))
3668 ;; Return value.
3669 ts)))
3670 (type (org-element-property :type timestamp)))
3671 (case type
3672 ((active inactive)
3673 (let* ((minute-start (org-element-property :minute-start timestamp))
3674 (minute-end (org-element-property :minute-end timestamp))
3675 (hour-start (org-element-property :hour-start timestamp))
3676 (hour-end (org-element-property :hour-end timestamp))
3677 (time-range-p (and hour-start hour-end minute-start minute-end
3678 (or (/= hour-start hour-end)
3679 (/= minute-start minute-end)))))
3680 (funcall
3681 build-ts-string
3682 (encode-time 0
3683 (or minute-start 0)
3684 (or hour-start 0)
3685 (org-element-property :day-start timestamp)
3686 (org-element-property :month-start timestamp)
3687 (org-element-property :year-start timestamp))
3688 (eq type 'active)
3689 (and hour-start minute-start)
3690 (and time-range-p hour-end)
3691 (and time-range-p minute-end))))
3692 ((active-range inactive-range)
3693 (let ((minute-start (org-element-property :minute-start timestamp))
3694 (minute-end (org-element-property :minute-end timestamp))
3695 (hour-start (org-element-property :hour-start timestamp))
3696 (hour-end (org-element-property :hour-end timestamp)))
3697 (concat
3698 (funcall
3699 build-ts-string (encode-time
3701 (or minute-start 0)
3702 (or hour-start 0)
3703 (org-element-property :day-start timestamp)
3704 (org-element-property :month-start timestamp)
3705 (org-element-property :year-start timestamp))
3706 (eq type 'active-range)
3707 (and hour-start minute-start))
3708 "--"
3709 (funcall build-ts-string
3710 (encode-time 0
3711 (or minute-end 0)
3712 (or hour-end 0)
3713 (org-element-property :day-end timestamp)
3714 (org-element-property :month-end timestamp)
3715 (org-element-property :year-end timestamp))
3716 (eq type 'active-range)
3717 (and hour-end minute-end)))))))))
3719 (defun org-element-timestamp-successor ()
3720 "Search for the next timestamp object.
3722 Return value is a cons cell whose CAR is `timestamp' and CDR is
3723 beginning position."
3724 (save-excursion
3725 (when (re-search-forward
3726 (concat org-ts-regexp-both
3727 "\\|"
3728 "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
3729 "\\|"
3730 "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
3731 nil t)
3732 (cons 'timestamp (match-beginning 0)))))
3735 ;;;; Underline
3737 (defun org-element-underline-parser ()
3738 "Parse underline object at point.
3740 Return a list whose CAR is `underline' and CDR is a plist with
3741 `:begin', `:end', `:contents-begin' and `:contents-end' and
3742 `:post-blank' keywords.
3744 Assume point is at the first underscore marker."
3745 (save-excursion
3746 (unless (bolp) (backward-char 1))
3747 (looking-at org-emph-re)
3748 (let ((begin (match-beginning 2))
3749 (contents-begin (match-beginning 4))
3750 (contents-end (match-end 4))
3751 (post-blank (progn (goto-char (match-end 2))
3752 (skip-chars-forward " \t")))
3753 (end (point)))
3754 (list 'underline
3755 (list :begin begin
3756 :end end
3757 :contents-begin contents-begin
3758 :contents-end contents-end
3759 :post-blank post-blank)))))
3761 (defun org-element-underline-interpreter (underline contents)
3762 "Interpret UNDERLINE object as Org syntax.
3763 CONTENTS is the contents of the object."
3764 (format "_%s_" contents))
3767 ;;;; Verbatim
3769 (defun org-element-verbatim-parser ()
3770 "Parse verbatim object at point.
3772 Return a list whose CAR is `verbatim' and CDR is a plist with
3773 `:value', `:begin', `:end' and `:post-blank' keywords.
3775 Assume point is at the first equal sign marker."
3776 (save-excursion
3777 (unless (bolp) (backward-char 1))
3778 (looking-at org-emph-re)
3779 (let ((begin (match-beginning 2))
3780 (value (org-match-string-no-properties 4))
3781 (post-blank (progn (goto-char (match-end 2))
3782 (skip-chars-forward " \t")))
3783 (end (point)))
3784 (list 'verbatim
3785 (list :value value
3786 :begin begin
3787 :end end
3788 :post-blank post-blank)))))
3790 (defun org-element-verbatim-interpreter (verbatim contents)
3791 "Interpret VERBATIM object as Org syntax.
3792 CONTENTS is nil."
3793 (format "=%s=" (org-element-property :value verbatim)))
3797 ;;; Parsing Element Starting At Point
3799 ;; `org-element--current-element' is the core function of this section.
3800 ;; It returns the Lisp representation of the element starting at
3801 ;; point.
3803 ;; `org-element--current-element' makes use of special modes. They
3804 ;; are activated for fixed element chaining (i.e. `plain-list' >
3805 ;; `item') or fixed conditional element chaining (i.e. `headline' >
3806 ;; `section'). Special modes are: `first-section', `item',
3807 ;; `node-property', `quote-section', `section' and `table-row'.
3809 (defun org-element--current-element
3810 (limit &optional granularity special structure)
3811 "Parse the element starting at point.
3813 Return value is a list like (TYPE PROPS) where TYPE is the type
3814 of the element and PROPS a plist of properties associated to the
3815 element.
3817 Possible types are defined in `org-element-all-elements'.
3819 LIMIT bounds the search.
3821 Optional argument GRANULARITY determines the depth of the
3822 recursion. Allowed values are `headline', `greater-element',
3823 `element', `object' or nil. When it is broader than `object' (or
3824 nil), secondary values will not be parsed, since they only
3825 contain objects.
3827 Optional argument SPECIAL, when non-nil, can be either
3828 `first-section', `item', `node-property', `quote-section',
3829 `section', and `table-row'.
3831 If STRUCTURE isn't provided but SPECIAL is set to `item', it will
3832 be computed.
3834 This function assumes point is always at the beginning of the
3835 element it has to parse."
3836 (save-excursion
3837 (let ((case-fold-search t)
3838 ;; Determine if parsing depth allows for secondary strings
3839 ;; parsing. It only applies to elements referenced in
3840 ;; `org-element-secondary-value-alist'.
3841 (raw-secondary-p (and granularity (not (eq granularity 'object)))))
3842 (cond
3843 ;; Item.
3844 ((eq special 'item)
3845 (org-element-item-parser limit structure raw-secondary-p))
3846 ;; Table Row.
3847 ((eq special 'table-row) (org-element-table-row-parser limit))
3848 ;; Node Property.
3849 ((eq special 'node-property) (org-element-node-property-parser limit))
3850 ;; Headline.
3851 ((org-with-limited-levels (org-at-heading-p))
3852 (org-element-headline-parser limit raw-secondary-p))
3853 ;; Sections (must be checked after headline).
3854 ((eq special 'section) (org-element-section-parser limit))
3855 ((eq special 'quote-section) (org-element-quote-section-parser limit))
3856 ((eq special 'first-section)
3857 (org-element-section-parser
3858 (or (save-excursion (org-with-limited-levels (outline-next-heading)))
3859 limit)))
3860 ;; When not at bol, point is at the beginning of an item or
3861 ;; a footnote definition: next item is always a paragraph.
3862 ((not (bolp)) (org-element-paragraph-parser limit (list (point))))
3863 ;; Planning and Clock.
3864 ((looking-at org-planning-or-clock-line-re)
3865 (if (equal (match-string 1) org-clock-string)
3866 (org-element-clock-parser limit)
3867 (org-element-planning-parser limit)))
3868 ;; Inlinetask.
3869 ((org-at-heading-p)
3870 (org-element-inlinetask-parser limit raw-secondary-p))
3871 ;; From there, elements can have affiliated keywords.
3872 (t (let ((affiliated (org-element--collect-affiliated-keywords limit)))
3873 (cond
3874 ;; Jumping over affiliated keywords put point off-limits.
3875 ;; Parse them as regular keywords.
3876 ((and (cdr affiliated) (>= (point) limit))
3877 (goto-char (car affiliated))
3878 (org-element-keyword-parser limit nil))
3879 ;; LaTeX Environment.
3880 ((looking-at
3881 "[ \t]*\\\\begin{[A-Za-z0-9*]+}\\(\\[.*?\\]\\|{.*?}\\)*[ \t]*$")
3882 (org-element-latex-environment-parser limit affiliated))
3883 ;; Drawer and Property Drawer.
3884 ((looking-at org-drawer-regexp)
3885 (if (equal (match-string 1) "PROPERTIES")
3886 (org-element-property-drawer-parser limit affiliated)
3887 (org-element-drawer-parser limit affiliated)))
3888 ;; Fixed Width
3889 ((looking-at "[ \t]*:\\( \\|$\\)")
3890 (org-element-fixed-width-parser limit affiliated))
3891 ;; Inline Comments, Blocks, Babel Calls, Dynamic Blocks and
3892 ;; Keywords.
3893 ((looking-at "[ \t]*#")
3894 (goto-char (match-end 0))
3895 (cond ((looking-at "\\(?: \\|$\\)")
3896 (beginning-of-line)
3897 (org-element-comment-parser limit affiliated))
3898 ((looking-at "\\+BEGIN_\\(\\S-+\\)")
3899 (beginning-of-line)
3900 (let ((parser (assoc (upcase (match-string 1))
3901 org-element-block-name-alist)))
3902 (if parser (funcall (cdr parser) limit affiliated)
3903 (org-element-special-block-parser limit affiliated))))
3904 ((looking-at "\\+CALL:")
3905 (beginning-of-line)
3906 (org-element-babel-call-parser limit affiliated))
3907 ((looking-at "\\+BEGIN:? ")
3908 (beginning-of-line)
3909 (org-element-dynamic-block-parser limit affiliated))
3910 ((looking-at "\\+\\S-+:")
3911 (beginning-of-line)
3912 (org-element-keyword-parser limit affiliated))
3914 (beginning-of-line)
3915 (org-element-paragraph-parser limit affiliated))))
3916 ;; Footnote Definition.
3917 ((looking-at org-footnote-definition-re)
3918 (org-element-footnote-definition-parser limit affiliated))
3919 ;; Horizontal Rule.
3920 ((looking-at "[ \t]*-\\{5,\\}[ \t]*$")
3921 (org-element-horizontal-rule-parser limit affiliated))
3922 ;; Diary Sexp.
3923 ((looking-at "%%(")
3924 (org-element-diary-sexp-parser limit affiliated))
3925 ;; Table.
3926 ((org-at-table-p t) (org-element-table-parser limit affiliated))
3927 ;; List.
3928 ((looking-at (org-item-re))
3929 (org-element-plain-list-parser
3930 limit affiliated
3931 (or structure (org-element--list-struct limit))))
3932 ;; Default element: Paragraph.
3933 (t (org-element-paragraph-parser limit affiliated)))))))))
3936 ;; Most elements can have affiliated keywords. When looking for an
3937 ;; element beginning, we want to move before them, as they belong to
3938 ;; that element, and, in the meantime, collect information they give
3939 ;; into appropriate properties. Hence the following function.
3941 (defun org-element--collect-affiliated-keywords (limit)
3942 "Collect affiliated keywords from point down to LIMIT.
3944 Return a list whose CAR is the position at the first of them and
3945 CDR a plist of keywords and values and move point to the
3946 beginning of the first line after them.
3948 As a special case, if element doesn't start at the beginning of
3949 the line (i.e. a paragraph starting an item), CAR is current
3950 position of point and CDR is nil."
3951 (if (not (bolp)) (list (point))
3952 (let ((case-fold-search t)
3953 (origin (point))
3954 ;; RESTRICT is the list of objects allowed in parsed
3955 ;; keywords value.
3956 (restrict (org-element-restriction 'keyword))
3957 output)
3958 (while (and (< (point) limit) (looking-at org-element--affiliated-re))
3959 (let* ((raw-kwd (upcase (match-string 1)))
3960 ;; Apply translation to RAW-KWD. From there, KWD is
3961 ;; the official keyword.
3962 (kwd (or (cdr (assoc raw-kwd
3963 org-element-keyword-translation-alist))
3964 raw-kwd))
3965 ;; Find main value for any keyword.
3966 (value
3967 (save-match-data
3968 (org-trim
3969 (buffer-substring-no-properties
3970 (match-end 0) (point-at-eol)))))
3971 ;; PARSEDP is non-nil when keyword should have its
3972 ;; value parsed.
3973 (parsedp (member kwd org-element-parsed-keywords))
3974 ;; If KWD is a dual keyword, find its secondary
3975 ;; value. Maybe parse it.
3976 (dualp (member kwd org-element-dual-keywords))
3977 (dual-value
3978 (and dualp
3979 (let ((sec (org-match-string-no-properties 2)))
3980 (if (or (not sec) (not parsedp)) sec
3981 (org-element-parse-secondary-string sec restrict)))))
3982 ;; Attribute a property name to KWD.
3983 (kwd-sym (and kwd (intern (concat ":" (downcase kwd))))))
3984 ;; Now set final shape for VALUE.
3985 (when parsedp
3986 (setq value (org-element-parse-secondary-string value restrict)))
3987 (when dualp
3988 (setq value (and (or value dual-value) (cons value dual-value))))
3989 (when (or (member kwd org-element-multiple-keywords)
3990 ;; Attributes can always appear on multiple lines.
3991 (string-match "^ATTR_" kwd))
3992 (setq value (cons value (plist-get output kwd-sym))))
3993 ;; Eventually store the new value in OUTPUT.
3994 (setq output (plist-put output kwd-sym value))
3995 ;; Move to next keyword.
3996 (forward-line)))
3997 ;; If affiliated keywords are orphaned: move back to first one.
3998 ;; They will be parsed as a paragraph.
3999 (when (looking-at "[ \t]*$") (goto-char origin) (setq output nil))
4000 ;; Return value.
4001 (cons origin output))))
4005 ;;; The Org Parser
4007 ;; The two major functions here are `org-element-parse-buffer', which
4008 ;; parses Org syntax inside the current buffer, taking into account
4009 ;; region, narrowing, or even visibility if specified, and
4010 ;; `org-element-parse-secondary-string', which parses objects within
4011 ;; a given string.
4013 ;; The (almost) almighty `org-element-map' allows to apply a function
4014 ;; on elements or objects matching some type, and accumulate the
4015 ;; resulting values. In an export situation, it also skips unneeded
4016 ;; parts of the parse tree.
4018 (defun org-element-parse-buffer (&optional granularity visible-only)
4019 "Recursively parse the buffer and return structure.
4020 If narrowing is in effect, only parse the visible part of the
4021 buffer.
4023 Optional argument GRANULARITY determines the depth of the
4024 recursion. It can be set to the following symbols:
4026 `headline' Only parse headlines.
4027 `greater-element' Don't recurse into greater elements excepted
4028 headlines and sections. Thus, elements
4029 parsed are the top-level ones.
4030 `element' Parse everything but objects and plain text.
4031 `object' Parse the complete buffer (default).
4033 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
4034 elements.
4036 An element or an objects is represented as a list with the
4037 pattern (TYPE PROPERTIES CONTENTS), where :
4039 TYPE is a symbol describing the element or object. See
4040 `org-element-all-elements' and `org-element-all-objects' for an
4041 exhaustive list of such symbols. One can retrieve it with
4042 `org-element-type' function.
4044 PROPERTIES is the list of attributes attached to the element or
4045 object, as a plist. Although most of them are specific to the
4046 element or object type, all types share `:begin', `:end',
4047 `:post-blank' and `:parent' properties, which respectively
4048 refer to buffer position where the element or object starts,
4049 ends, the number of white spaces or blank lines after it, and
4050 the element or object containing it. Properties values can be
4051 obtained by using `org-element-property' function.
4053 CONTENTS is a list of elements, objects or raw strings
4054 contained in the current element or object, when applicable.
4055 One can access them with `org-element-contents' function.
4057 The Org buffer has `org-data' as type and nil as properties.
4058 `org-element-map' function can be used to find specific elements
4059 or objects within the parse tree.
4061 This function assumes that current major mode is `org-mode'."
4062 (save-excursion
4063 (goto-char (point-min))
4064 (org-skip-whitespace)
4065 (org-element--parse-elements
4066 (point-at-bol) (point-max)
4067 ;; Start in `first-section' mode so text before the first
4068 ;; headline belongs to a section.
4069 'first-section nil granularity visible-only (list 'org-data nil))))
4071 (defun org-element-parse-secondary-string (string restriction &optional parent)
4072 "Recursively parse objects in STRING and return structure.
4074 RESTRICTION is a symbol limiting the object types that will be
4075 looked after.
4077 Optional argument PARENT, when non-nil, is the element or object
4078 containing the secondary string. It is used to set correctly
4079 `:parent' property within the string."
4080 ;; Copy buffer-local variables listed in
4081 ;; `org-element-object-variables' into temporary buffer. This is
4082 ;; required since object parsing is dependent on these variables.
4083 (let ((pairs (delq nil (mapcar (lambda (var)
4084 (when (boundp var)
4085 (cons var (symbol-value var))))
4086 org-element-object-variables))))
4087 (with-temp-buffer
4088 (mapc (lambda (pair) (org-set-local (car pair) (cdr pair))) pairs)
4089 (insert string)
4090 (let ((secondary (org-element--parse-objects
4091 (point-min) (point-max) nil restriction)))
4092 (when parent
4093 (mapc (lambda (obj) (org-element-put-property obj :parent parent))
4094 secondary))
4095 secondary))))
4097 (defun org-element-map
4098 (data types fun &optional info first-match no-recursion with-affiliated)
4099 "Map a function on selected elements or objects.
4101 DATA is a parse tree, an element, an object, a string, or a list
4102 of such constructs. TYPES is a symbol or list of symbols of
4103 elements or objects types (see `org-element-all-elements' and
4104 `org-element-all-objects' for a complete list of types). FUN is
4105 the function called on the matching element or object. It has to
4106 accept one argument: the element or object itself.
4108 When optional argument INFO is non-nil, it should be a plist
4109 holding export options. In that case, parts of the parse tree
4110 not exportable according to that property list will be skipped.
4112 When optional argument FIRST-MATCH is non-nil, stop at the first
4113 match for which FUN doesn't return nil, and return that value.
4115 Optional argument NO-RECURSION is a symbol or a list of symbols
4116 representing elements or objects types. `org-element-map' won't
4117 enter any recursive element or object whose type belongs to that
4118 list. Though, FUN can still be applied on them.
4120 When optional argument WITH-AFFILIATED is non-nil, FUN will also
4121 apply to matching objects within parsed affiliated keywords (see
4122 `org-element-parsed-keywords').
4124 Nil values returned from FUN do not appear in the results.
4127 Examples:
4128 ---------
4130 Assuming TREE is a variable containing an Org buffer parse tree,
4131 the following example will return a flat list of all `src-block'
4132 and `example-block' elements in it:
4134 \(org-element-map tree '(example-block src-block) 'identity)
4136 The following snippet will find the first headline with a level
4137 of 1 and a \"phone\" tag, and will return its beginning position:
4139 \(org-element-map tree 'headline
4140 \(lambda (hl)
4141 \(and (= (org-element-property :level hl) 1)
4142 \(member \"phone\" (org-element-property :tags hl))
4143 \(org-element-property :begin hl)))
4144 nil t)
4146 The next example will return a flat list of all `plain-list' type
4147 elements in TREE that are not a sub-list themselves:
4149 \(org-element-map tree 'plain-list 'identity nil nil 'plain-list)
4151 Eventually, this example will return a flat list of all `bold'
4152 type objects containing a `latex-snippet' type object, even
4153 looking into captions:
4155 \(org-element-map tree 'bold
4156 \(lambda (b)
4157 \(and (org-element-map b 'latex-snippet 'identity nil t) b))
4158 nil nil nil t)"
4159 ;; Ensure TYPES and NO-RECURSION are a list, even of one element.
4160 (unless (listp types) (setq types (list types)))
4161 (unless (listp no-recursion) (setq no-recursion (list no-recursion)))
4162 ;; Recursion depth is determined by --CATEGORY.
4163 (let* ((--category
4164 (catch 'found
4165 (let ((category 'greater-elements))
4166 (mapc (lambda (type)
4167 (cond ((or (memq type org-element-all-objects)
4168 (eq type 'plain-text))
4169 ;; If one object is found, the function
4170 ;; has to recurse into every object.
4171 (throw 'found 'objects))
4172 ((not (memq type org-element-greater-elements))
4173 ;; If one regular element is found, the
4174 ;; function has to recurse, at least,
4175 ;; into every element it encounters.
4176 (and (not (eq category 'elements))
4177 (setq category 'elements)))))
4178 types)
4179 category)))
4180 ;; Compute properties for affiliated keywords if necessary.
4181 (--affiliated-alist
4182 (and with-affiliated
4183 (mapcar (lambda (kwd)
4184 (cons kwd (intern (concat ":" (downcase kwd)))))
4185 org-element-affiliated-keywords)))
4186 --acc
4187 --walk-tree
4188 (--walk-tree
4189 (function
4190 (lambda (--data)
4191 ;; Recursively walk DATA. INFO, if non-nil, is a plist
4192 ;; holding contextual information.
4193 (let ((--type (org-element-type --data)))
4194 (cond
4195 ((not --data))
4196 ;; Ignored element in an export context.
4197 ((and info (memq --data (plist-get info :ignore-list))))
4198 ;; List of elements or objects.
4199 ((not --type) (mapc --walk-tree --data))
4200 ;; Unconditionally enter parse trees.
4201 ((eq --type 'org-data)
4202 (mapc --walk-tree (org-element-contents --data)))
4204 ;; Check if TYPE is matching among TYPES. If so,
4205 ;; apply FUN to --DATA and accumulate return value
4206 ;; into --ACC (or exit if FIRST-MATCH is non-nil).
4207 (when (memq --type types)
4208 (let ((result (funcall fun --data)))
4209 (cond ((not result))
4210 (first-match (throw '--map-first-match result))
4211 (t (push result --acc)))))
4212 ;; If --DATA has a secondary string that can contain
4213 ;; objects with their type among TYPES, look into it.
4214 (when (and (eq --category 'objects) (not (stringp --data)))
4215 (let ((sec-prop
4216 (assq --type org-element-secondary-value-alist)))
4217 (when sec-prop
4218 (funcall --walk-tree
4219 (org-element-property (cdr sec-prop) --data)))))
4220 ;; If --DATA has any affiliated keywords and
4221 ;; WITH-AFFILIATED is non-nil, look for objects in
4222 ;; them.
4223 (when (and with-affiliated
4224 (eq --category 'objects)
4225 (memq --type org-element-all-elements))
4226 (mapc (lambda (kwd-pair)
4227 (let ((kwd (car kwd-pair))
4228 (value (org-element-property
4229 (cdr kwd-pair) --data)))
4230 ;; Pay attention to the type of value.
4231 ;; Preserve order for multiple keywords.
4232 (cond
4233 ((not value))
4234 ((and (member kwd org-element-multiple-keywords)
4235 (member kwd org-element-dual-keywords))
4236 (mapc (lambda (line)
4237 (funcall --walk-tree (cdr line))
4238 (funcall --walk-tree (car line)))
4239 (reverse value)))
4240 ((member kwd org-element-multiple-keywords)
4241 (mapc (lambda (line) (funcall --walk-tree line))
4242 (reverse value)))
4243 ((member kwd org-element-dual-keywords)
4244 (funcall --walk-tree (cdr value))
4245 (funcall --walk-tree (car value)))
4246 (t (funcall --walk-tree value)))))
4247 --affiliated-alist))
4248 ;; Determine if a recursion into --DATA is possible.
4249 (cond
4250 ;; --TYPE is explicitly removed from recursion.
4251 ((memq --type no-recursion))
4252 ;; --DATA has no contents.
4253 ((not (org-element-contents --data)))
4254 ;; Looking for greater elements but --DATA is simply
4255 ;; an element or an object.
4256 ((and (eq --category 'greater-elements)
4257 (not (memq --type org-element-greater-elements))))
4258 ;; Looking for elements but --DATA is an object.
4259 ((and (eq --category 'elements)
4260 (memq --type org-element-all-objects)))
4261 ;; In any other case, map contents.
4262 (t (mapc --walk-tree (org-element-contents --data)))))))))))
4263 (catch '--map-first-match
4264 (funcall --walk-tree data)
4265 ;; Return value in a proper order.
4266 (nreverse --acc))))
4267 (put 'org-element-map 'lisp-indent-function 2)
4269 ;; The following functions are internal parts of the parser.
4271 ;; The first one, `org-element--parse-elements' acts at the element's
4272 ;; level.
4274 ;; The second one, `org-element--parse-objects' applies on all objects
4275 ;; of a paragraph or a secondary string. It uses
4276 ;; `org-element--get-next-object-candidates' to optimize the search of
4277 ;; the next object in the buffer.
4279 ;; More precisely, that function looks for every allowed object type
4280 ;; first. Then, it discards failed searches, keeps further matches,
4281 ;; and searches again types matched behind point, for subsequent
4282 ;; calls. Thus, searching for a given type fails only once, and every
4283 ;; object is searched only once at top level (but sometimes more for
4284 ;; nested types).
4286 (defun org-element--parse-elements
4287 (beg end special structure granularity visible-only acc)
4288 "Parse elements between BEG and END positions.
4290 SPECIAL prioritize some elements over the others. It can be set
4291 to `first-section', `quote-section', `section' `item' or
4292 `table-row'.
4294 When value is `item', STRUCTURE will be used as the current list
4295 structure.
4297 GRANULARITY determines the depth of the recursion. See
4298 `org-element-parse-buffer' for more information.
4300 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
4301 elements.
4303 Elements are accumulated into ACC."
4304 (save-excursion
4305 (goto-char beg)
4306 ;; Visible only: skip invisible parts at the beginning of the
4307 ;; element.
4308 (when (and visible-only (org-invisible-p2))
4309 (goto-char (min (1+ (org-find-visible)) end)))
4310 ;; When parsing only headlines, skip any text before first one.
4311 (when (and (eq granularity 'headline) (not (org-at-heading-p)))
4312 (org-with-limited-levels (outline-next-heading)))
4313 ;; Main loop start.
4314 (while (< (point) end)
4315 ;; Find current element's type and parse it accordingly to
4316 ;; its category.
4317 (let* ((element (org-element--current-element
4318 end granularity special structure))
4319 (type (org-element-type element))
4320 (cbeg (org-element-property :contents-begin element)))
4321 (goto-char (org-element-property :end element))
4322 ;; Visible only: skip invisible parts between siblings.
4323 (when (and visible-only (org-invisible-p2))
4324 (goto-char (min (1+ (org-find-visible)) end)))
4325 ;; Fill ELEMENT contents by side-effect.
4326 (cond
4327 ;; If element has no contents, don't modify it.
4328 ((not cbeg))
4329 ;; Greater element: parse it between `contents-begin' and
4330 ;; `contents-end'. Make sure GRANULARITY allows the
4331 ;; recursion, or ELEMENT is a headline, in which case going
4332 ;; inside is mandatory, in order to get sub-level headings.
4333 ((and (memq type org-element-greater-elements)
4334 (or (memq granularity '(element object nil))
4335 (and (eq granularity 'greater-element)
4336 (eq type 'section))
4337 (eq type 'headline)))
4338 (org-element--parse-elements
4339 cbeg (org-element-property :contents-end element)
4340 ;; Possibly switch to a special mode.
4341 (case type
4342 (headline
4343 (if (org-element-property :quotedp element) 'quote-section
4344 'section))
4345 (plain-list 'item)
4346 (property-drawer 'node-property)
4347 (table 'table-row))
4348 (and (memq type '(item plain-list))
4349 (org-element-property :structure element))
4350 granularity visible-only element))
4351 ;; ELEMENT has contents. Parse objects inside, if
4352 ;; GRANULARITY allows it.
4353 ((memq granularity '(object nil))
4354 (org-element--parse-objects
4355 cbeg (org-element-property :contents-end element) element
4356 (org-element-restriction type))))
4357 (org-element-adopt-elements acc element)))
4358 ;; Return result.
4359 acc))
4361 (defun org-element--parse-objects (beg end acc restriction)
4362 "Parse objects between BEG and END and return recursive structure.
4364 Objects are accumulated in ACC.
4366 RESTRICTION is a list of object successors which are allowed in
4367 the current object."
4368 (let ((candidates 'initial))
4369 (save-excursion
4370 (save-restriction
4371 (narrow-to-region beg end)
4372 (goto-char (point-min))
4373 (while (and (not (eobp))
4374 (setq candidates
4375 (org-element--get-next-object-candidates
4376 restriction candidates)))
4377 (let ((next-object
4378 (let ((pos (apply 'min (mapcar 'cdr candidates))))
4379 (save-excursion
4380 (goto-char pos)
4381 (funcall (intern (format "org-element-%s-parser"
4382 (car (rassq pos candidates)))))))))
4383 ;; 1. Text before any object. Untabify it.
4384 (let ((obj-beg (org-element-property :begin next-object)))
4385 (unless (= (point) obj-beg)
4386 (setq acc
4387 (org-element-adopt-elements
4389 (replace-regexp-in-string
4390 "\t" (make-string tab-width ? )
4391 (buffer-substring-no-properties (point) obj-beg))))))
4392 ;; 2. Object...
4393 (let ((obj-end (org-element-property :end next-object))
4394 (cont-beg (org-element-property :contents-begin next-object)))
4395 ;; Fill contents of NEXT-OBJECT by side-effect, if it has
4396 ;; a recursive type.
4397 (when (and cont-beg
4398 (memq (car next-object) org-element-recursive-objects))
4399 (org-element--parse-objects
4400 cont-beg (org-element-property :contents-end next-object)
4401 next-object (org-element-restriction next-object)))
4402 (setq acc (org-element-adopt-elements acc next-object))
4403 (goto-char obj-end))))
4404 ;; 3. Text after last object. Untabify it.
4405 (unless (eobp)
4406 (setq acc
4407 (org-element-adopt-elements
4409 (replace-regexp-in-string
4410 "\t" (make-string tab-width ? )
4411 (buffer-substring-no-properties (point) end)))))
4412 ;; Result.
4413 acc))))
4415 (defun org-element--get-next-object-candidates (restriction objects)
4416 "Return an alist of candidates for the next object.
4418 RESTRICTION is a list of object types, as symbols. Only
4419 candidates with such types are looked after.
4421 OBJECTS is the previous candidates alist. If it is set to
4422 `initial', no search has been done before, and all symbols in
4423 RESTRICTION should be looked after.
4425 Return value is an alist whose CAR is the object type and CDR its
4426 beginning position."
4427 (delq
4429 (if (eq objects 'initial)
4430 ;; When searching for the first time, look for every successor
4431 ;; allowed in RESTRICTION.
4432 (mapcar
4433 (lambda (res)
4434 (funcall (intern (format "org-element-%s-successor" res))))
4435 restriction)
4436 ;; Focus on objects returned during last search. Keep those
4437 ;; still after point. Search again objects before it.
4438 (mapcar
4439 (lambda (obj)
4440 (if (>= (cdr obj) (point)) obj
4441 (let* ((type (car obj))
4442 (succ (or (cdr (assq type org-element-object-successor-alist))
4443 type)))
4444 (and succ
4445 (funcall (intern (format "org-element-%s-successor" succ)))))))
4446 objects))))
4450 ;;; Towards A Bijective Process
4452 ;; The parse tree obtained with `org-element-parse-buffer' is really
4453 ;; a snapshot of the corresponding Org buffer. Therefore, it can be
4454 ;; interpreted and expanded into a string with canonical Org syntax.
4455 ;; Hence `org-element-interpret-data'.
4457 ;; The function relies internally on
4458 ;; `org-element--interpret-affiliated-keywords'.
4460 ;;;###autoload
4461 (defun org-element-interpret-data (data &optional parent)
4462 "Interpret DATA as Org syntax.
4464 DATA is a parse tree, an element, an object or a secondary string
4465 to interpret.
4467 Optional argument PARENT is used for recursive calls. It contains
4468 the element or object containing data, or nil.
4470 Return Org syntax as a string."
4471 (let* ((type (org-element-type data))
4472 (results
4473 (cond
4474 ;; Secondary string.
4475 ((not type)
4476 (mapconcat
4477 (lambda (obj) (org-element-interpret-data obj parent))
4478 data ""))
4479 ;; Full Org document.
4480 ((eq type 'org-data)
4481 (mapconcat
4482 (lambda (obj) (org-element-interpret-data obj parent))
4483 (org-element-contents data) ""))
4484 ;; Plain text: remove `:parent' text property from output.
4485 ((stringp data) (org-no-properties data))
4486 ;; Element/Object without contents.
4487 ((not (org-element-contents data))
4488 (funcall (intern (format "org-element-%s-interpreter" type))
4489 data nil))
4490 ;; Element/Object with contents.
4492 (let* ((greaterp (memq type org-element-greater-elements))
4493 (objectp (and (not greaterp)
4494 (memq type org-element-recursive-objects)))
4495 (contents
4496 (mapconcat
4497 (lambda (obj) (org-element-interpret-data obj data))
4498 (org-element-contents
4499 (if (or greaterp objectp) data
4500 ;; Elements directly containing objects must
4501 ;; have their indentation normalized first.
4502 (org-element-normalize-contents
4503 data
4504 ;; When normalizing first paragraph of an
4505 ;; item or a footnote-definition, ignore
4506 ;; first line's indentation.
4507 (and (eq type 'paragraph)
4508 (equal data (car (org-element-contents parent)))
4509 (memq (org-element-type parent)
4510 '(footnote-definition item))))))
4511 "")))
4512 (funcall (intern (format "org-element-%s-interpreter" type))
4513 data
4514 (if greaterp (org-element-normalize-contents contents)
4515 contents)))))))
4516 (if (memq type '(org-data plain-text nil)) results
4517 ;; Build white spaces. If no `:post-blank' property is
4518 ;; specified, assume its value is 0.
4519 (let ((post-blank (or (org-element-property :post-blank data) 0)))
4520 (if (memq type org-element-all-objects)
4521 (concat results (make-string post-blank 32))
4522 (concat
4523 (org-element--interpret-affiliated-keywords data)
4524 (org-element-normalize-string results)
4525 (make-string post-blank 10)))))))
4527 (defun org-element--interpret-affiliated-keywords (element)
4528 "Return ELEMENT's affiliated keywords as Org syntax.
4529 If there is no affiliated keyword, return the empty string."
4530 (let ((keyword-to-org
4531 (function
4532 (lambda (key value)
4533 (let (dual)
4534 (when (member key org-element-dual-keywords)
4535 (setq dual (cdr value) value (car value)))
4536 (concat "#+" key
4537 (and dual
4538 (format "[%s]" (org-element-interpret-data dual)))
4539 ": "
4540 (if (member key org-element-parsed-keywords)
4541 (org-element-interpret-data value)
4542 value)
4543 "\n"))))))
4544 (mapconcat
4545 (lambda (prop)
4546 (let ((value (org-element-property prop element))
4547 (keyword (upcase (substring (symbol-name prop) 1))))
4548 (when value
4549 (if (or (member keyword org-element-multiple-keywords)
4550 ;; All attribute keywords can have multiple lines.
4551 (string-match "^ATTR_" keyword))
4552 (mapconcat (lambda (line) (funcall keyword-to-org keyword line))
4553 (reverse value)
4555 (funcall keyword-to-org keyword value)))))
4556 ;; List all ELEMENT's properties matching an attribute line or an
4557 ;; affiliated keyword, but ignore translated keywords since they
4558 ;; cannot belong to the property list.
4559 (loop for prop in (nth 1 element) by 'cddr
4560 when (let ((keyword (upcase (substring (symbol-name prop) 1))))
4561 (or (string-match "^ATTR_" keyword)
4562 (and
4563 (member keyword org-element-affiliated-keywords)
4564 (not (assoc keyword
4565 org-element-keyword-translation-alist)))))
4566 collect prop)
4567 "")))
4569 ;; Because interpretation of the parse tree must return the same
4570 ;; number of blank lines between elements and the same number of white
4571 ;; space after objects, some special care must be given to white
4572 ;; spaces.
4574 ;; The first function, `org-element-normalize-string', ensures any
4575 ;; string different from the empty string will end with a single
4576 ;; newline character.
4578 ;; The second function, `org-element-normalize-contents', removes
4579 ;; global indentation from the contents of the current element.
4581 (defun org-element-normalize-string (s)
4582 "Ensure string S ends with a single newline character.
4584 If S isn't a string return it unchanged. If S is the empty
4585 string, return it. Otherwise, return a new string with a single
4586 newline character at its end."
4587 (cond
4588 ((not (stringp s)) s)
4589 ((string= "" s) "")
4590 (t (and (string-match "\\(\n[ \t]*\\)*\\'" s)
4591 (replace-match "\n" nil nil s)))))
4593 (defun org-element-normalize-contents (element &optional ignore-first)
4594 "Normalize plain text in ELEMENT's contents.
4596 ELEMENT must only contain plain text and objects.
4598 If optional argument IGNORE-FIRST is non-nil, ignore first line's
4599 indentation to compute maximal common indentation.
4601 Return the normalized element that is element with global
4602 indentation removed from its contents. The function assumes that
4603 indentation is not done with TAB characters."
4604 (let* (ind-list ; for byte-compiler
4605 collect-inds ; for byte-compiler
4606 (collect-inds
4607 (function
4608 ;; Return list of indentations within BLOB. This is done by
4609 ;; walking recursively BLOB and updating IND-LIST along the
4610 ;; way. FIRST-FLAG is non-nil when the first string hasn't
4611 ;; been seen yet. It is required as this string is the only
4612 ;; one whose indentation doesn't happen after a newline
4613 ;; character.
4614 (lambda (blob first-flag)
4615 (mapc
4616 (lambda (object)
4617 (when (and first-flag (stringp object))
4618 (setq first-flag nil)
4619 (string-match "\\`\\( *\\)" object)
4620 (let ((len (length (match-string 1 object))))
4621 ;; An indentation of zero means no string will be
4622 ;; modified. Quit the process.
4623 (if (zerop len) (throw 'zero (setq ind-list nil))
4624 (push len ind-list))))
4625 (cond
4626 ((stringp object)
4627 (let ((start 0))
4628 ;; Avoid matching blank or empty lines.
4629 (while (and (string-match "\n\\( *\\)\\(.\\)" object start)
4630 (not (equal (match-string 2 object) " ")))
4631 (setq start (match-end 0))
4632 (push (length (match-string 1 object)) ind-list))))
4633 ((memq (org-element-type object) org-element-recursive-objects)
4634 (funcall collect-inds object first-flag))))
4635 (org-element-contents blob))))))
4636 ;; Collect indentation list in ELEMENT. Possibly remove first
4637 ;; value if IGNORE-FIRST is non-nil.
4638 (catch 'zero (funcall collect-inds element (not ignore-first)))
4639 (if (not ind-list) element
4640 ;; Build ELEMENT back, replacing each string with the same
4641 ;; string minus common indentation.
4642 (let* (build ; For byte compiler.
4643 (build
4644 (function
4645 (lambda (blob mci first-flag)
4646 ;; Return BLOB with all its strings indentation
4647 ;; shortened from MCI white spaces. FIRST-FLAG is
4648 ;; non-nil when the first string hasn't been seen
4649 ;; yet.
4650 (setcdr (cdr blob)
4651 (mapcar
4652 (lambda (object)
4653 (when (and first-flag (stringp object))
4654 (setq first-flag nil)
4655 (setq object
4656 (replace-regexp-in-string
4657 (format "\\` \\{%d\\}" mci) "" object)))
4658 (cond
4659 ((stringp object)
4660 (replace-regexp-in-string
4661 (format "\n \\{%d\\}" mci) "\n" object))
4662 ((memq (org-element-type object)
4663 org-element-recursive-objects)
4664 (funcall build object mci first-flag))
4665 (t object)))
4666 (org-element-contents blob)))
4667 blob))))
4668 (funcall build element (apply 'min ind-list) (not ignore-first))))))
4672 ;;; The Toolbox
4674 ;; The first move is to implement a way to obtain the smallest element
4675 ;; containing point. This is the job of `org-element-at-point'. It
4676 ;; basically jumps back to the beginning of section containing point
4677 ;; and moves, element after element, with
4678 ;; `org-element--current-element' until the container is found. Note:
4679 ;; When using `org-element-at-point', secondary values are never
4680 ;; parsed since the function focuses on elements, not on objects.
4682 ;; At a deeper level, `org-element-context' lists all elements and
4683 ;; objects containing point.
4685 ;; `org-element-nested-p' and `org-element-swap-A-B' may be used
4686 ;; internally by navigation and manipulation tools.
4688 ;;;###autoload
4689 (defun org-element-at-point (&optional keep-trail)
4690 "Determine closest element around point.
4692 Return value is a list like (TYPE PROPS) where TYPE is the type
4693 of the element and PROPS a plist of properties associated to the
4694 element.
4696 Possible types are defined in `org-element-all-elements'.
4697 Properties depend on element or object type, but always include
4698 `:begin', `:end', `:parent' and `:post-blank' properties.
4700 As a special case, if point is at the very beginning of a list or
4701 sub-list, returned element will be that list instead of the first
4702 item. In the same way, if point is at the beginning of the first
4703 row of a table, returned element will be the table instead of the
4704 first row.
4706 If optional argument KEEP-TRAIL is non-nil, the function returns
4707 a list of elements leading to element at point. The list's CAR
4708 is always the element at point. The following positions contain
4709 element's siblings, then parents, siblings of parents, until the
4710 first element of current section."
4711 (org-with-wide-buffer
4712 ;; If at a headline, parse it. It is the sole element that
4713 ;; doesn't require to know about context. Be sure to disallow
4714 ;; secondary string parsing, though.
4715 (if (org-with-limited-levels (org-at-heading-p))
4716 (progn
4717 (beginning-of-line)
4718 (if (not keep-trail) (org-element-headline-parser (point-max) t)
4719 (list (org-element-headline-parser (point-max) t))))
4720 ;; Otherwise move at the beginning of the section containing
4721 ;; point.
4722 (catch 'exit
4723 (let ((origin (point))
4724 (end (save-excursion
4725 (org-with-limited-levels (outline-next-heading)) (point)))
4726 element type special-flag trail struct prevs parent)
4727 (org-with-limited-levels
4728 (if (org-before-first-heading-p)
4729 ;; In empty lines at buffer's beginning, return nil.
4730 (progn (goto-char (point-min))
4731 (org-skip-whitespace)
4732 (when (or (eobp) (> (line-beginning-position) origin))
4733 (throw 'exit nil)))
4734 (org-back-to-heading)
4735 (forward-line)
4736 (org-skip-whitespace)
4737 (when (or (eobp) (> (line-beginning-position) origin))
4738 ;; In blank lines just after the headline, point still
4739 ;; belongs to the headline.
4740 (throw 'exit
4741 (progn (skip-chars-backward " \r\t\n")
4742 (beginning-of-line)
4743 (if (not keep-trail)
4744 (org-element-headline-parser (point-max) t)
4745 (list (org-element-headline-parser
4746 (point-max) t))))))))
4747 (beginning-of-line)
4748 ;; Parse successively each element, skipping those ending
4749 ;; before original position.
4750 (while t
4751 (setq element
4752 (org-element--current-element end 'element special-flag struct)
4753 type (car element))
4754 (org-element-put-property element :parent parent)
4755 (when keep-trail (push element trail))
4756 (cond
4757 ;; 1. Skip any element ending before point. Also skip
4758 ;; element ending at point when we're sure that another
4759 ;; element has started.
4760 ((let ((elem-end (org-element-property :end element)))
4761 (when (or (< elem-end origin)
4762 (and (= elem-end origin) (/= elem-end end)))
4763 (goto-char elem-end))))
4764 ;; 2. An element containing point is always the element at
4765 ;; point.
4766 ((not (memq type org-element-greater-elements))
4767 (throw 'exit (if keep-trail trail element)))
4768 ;; 3. At any other greater element type, if point is
4769 ;; within contents, move into it.
4771 (let ((cbeg (org-element-property :contents-begin element))
4772 (cend (org-element-property :contents-end element)))
4773 (if (or (not cbeg) (not cend) (> cbeg origin) (< cend origin)
4774 ;; Create an anchor for tables and plain lists:
4775 ;; when point is at the very beginning of these
4776 ;; elements, ignoring affiliated keywords,
4777 ;; target them instead of their contents.
4778 (and (= cbeg origin) (memq type '(plain-list table)))
4779 ;; When point is at contents end, do not move
4780 ;; into elements with an explicit ending, but
4781 ;; return that element instead.
4782 (and (= cend origin)
4783 (or (memq type
4784 '(center-block
4785 drawer dynamic-block inlinetask
4786 property-drawer quote-block
4787 special-block))
4788 ;; Corner case: if a list ends at the
4789 ;; end of a buffer without a final new
4790 ;; line, return last element in last
4791 ;; item instead.
4792 (and (memq type '(item plain-list))
4793 (progn (goto-char cend)
4794 (or (bolp) (not (eobp))))))))
4795 (throw 'exit (if keep-trail trail element))
4796 (setq parent element)
4797 (case type
4798 (plain-list
4799 (setq special-flag 'item
4800 struct (org-element-property :structure element)))
4801 (item (setq special-flag nil))
4802 (property-drawer
4803 (setq special-flag 'node-property struct nil))
4804 (table (setq special-flag 'table-row struct nil))
4805 (otherwise (setq special-flag nil struct nil)))
4806 (setq end cend)
4807 (goto-char cbeg)))))))))))
4809 ;;;###autoload
4810 (defun org-element-context (&optional element)
4811 "Return closest element or object around point.
4813 Return value is a list like (TYPE PROPS) where TYPE is the type
4814 of the element or object and PROPS a plist of properties
4815 associated to it.
4817 Possible types are defined in `org-element-all-elements' and
4818 `org-element-all-objects'. Properties depend on element or
4819 object type, but always include `:begin', `:end', `:parent' and
4820 `:post-blank'.
4822 Optional argument ELEMENT, when non-nil, is the closest element
4823 containing point, as returned by `org-element-at-point'.
4824 Providing it allows for quicker computation."
4825 (catch 'objects-forbidden
4826 (org-with-wide-buffer
4827 (let* ((origin (point))
4828 (element (or element (org-element-at-point)))
4829 (type (org-element-type element))
4830 context)
4831 ;; Check if point is inside an element containing objects or at
4832 ;; a secondary string. In that case, narrow buffer to the
4833 ;; containing area. Otherwise, return ELEMENT.
4834 (cond
4835 ;; At a parsed affiliated keyword, check if we're inside main
4836 ;; or dual value.
4837 ((let ((post (org-element-property :post-affiliated element)))
4838 (and post (< origin post)))
4839 (beginning-of-line)
4840 (let ((case-fold-search t)) (looking-at org-element--affiliated-re))
4841 (cond
4842 ((not (member-ignore-case (match-string 1)
4843 org-element-parsed-keywords))
4844 (throw 'objects-forbidden element))
4845 ((< (match-end 0) origin)
4846 (narrow-to-region (match-end 0) (line-end-position)))
4847 ((and (match-beginning 2)
4848 (>= origin (match-beginning 2))
4849 (< origin (match-end 2)))
4850 (narrow-to-region (match-beginning 2) (match-end 2)))
4851 (t (throw 'objects-forbidden element)))
4852 ;; Also change type to retrieve correct restrictions.
4853 (setq type 'keyword))
4854 ;; At an item, objects can only be located within tag, if any.
4855 ((eq type 'item)
4856 (let ((tag (org-element-property :tag element)))
4857 (if (not tag) (throw 'objects-forbidden element)
4858 (beginning-of-line)
4859 (search-forward tag (line-end-position))
4860 (goto-char (match-beginning 0))
4861 (if (and (>= origin (point)) (< origin (match-end 0)))
4862 (narrow-to-region (point) (match-end 0))
4863 (throw 'objects-forbidden element)))))
4864 ;; At an headline or inlinetask, objects are located within
4865 ;; their title.
4866 ((memq type '(headline inlinetask))
4867 (goto-char (org-element-property :begin element))
4868 (skip-chars-forward "* ")
4869 (if (and (>= origin (point)) (< origin (line-end-position)))
4870 (narrow-to-region (point) (line-end-position))
4871 (throw 'objects-forbidden element)))
4872 ;; At a paragraph, a table-row or a verse block, objects are
4873 ;; located within their contents.
4874 ((memq type '(paragraph table-row verse-block))
4875 (let ((cbeg (org-element-property :contents-begin element))
4876 (cend (org-element-property :contents-end element)))
4877 ;; CBEG is nil for table rules.
4878 (if (and cbeg cend (>= origin cbeg) (< origin cend))
4879 (narrow-to-region cbeg cend)
4880 (throw 'objects-forbidden element))))
4881 ;; At a parsed keyword, objects are located within value.
4882 ((eq type 'keyword)
4883 (if (not (member (org-element-property :key element)
4884 org-element-document-properties))
4885 (throw 'objects-forbidden element)
4886 (beginning-of-line)
4887 (search-forward ":")
4888 (if (and (>= origin (point)) (< origin (line-end-position)))
4889 (narrow-to-region (point) (line-end-position))
4890 (throw 'objects-forbidden element))))
4891 (t (throw 'objects-forbidden element)))
4892 (goto-char (point-min))
4893 (let ((restriction (org-element-restriction type))
4894 (parent element)
4895 (candidates 'initial))
4896 (catch 'exit
4897 (while (setq candidates
4898 (org-element--get-next-object-candidates
4899 restriction candidates))
4900 (let ((closest-cand (rassq (apply 'min (mapcar 'cdr candidates))
4901 candidates)))
4902 ;; If ORIGIN is before next object in element, there's
4903 ;; no point in looking further.
4904 (if (> (cdr closest-cand) origin) (throw 'exit parent)
4905 (let* ((object
4906 (progn (goto-char (cdr closest-cand))
4907 (funcall (intern (format "org-element-%s-parser"
4908 (car closest-cand))))))
4909 (cbeg (org-element-property :contents-begin object))
4910 (cend (org-element-property :contents-end object))
4911 (obj-end (org-element-property :end object)))
4912 (cond
4913 ;; ORIGIN is after OBJECT, so skip it.
4914 ((<= obj-end origin) (goto-char obj-end))
4915 ;; ORIGIN is within a non-recursive object or at
4916 ;; an object boundaries: Return that object.
4917 ((or (not cbeg) (< origin cbeg) (>= origin cend))
4918 (throw 'exit
4919 (org-element-put-property object :parent parent)))
4920 ;; Otherwise, move within current object and
4921 ;; restrict search to the end of its contents.
4922 (t (goto-char cbeg)
4923 (narrow-to-region (point) cend)
4924 (org-element-put-property object :parent parent)
4925 (setq parent object
4926 restriction (org-element-restriction object)
4927 candidates 'initial)))))))
4928 parent))))))
4930 (defun org-element-nested-p (elem-A elem-B)
4931 "Non-nil when elements ELEM-A and ELEM-B are nested."
4932 (let ((beg-A (org-element-property :begin elem-A))
4933 (beg-B (org-element-property :begin elem-B))
4934 (end-A (org-element-property :end elem-A))
4935 (end-B (org-element-property :end elem-B)))
4936 (or (and (>= beg-A beg-B) (<= end-A end-B))
4937 (and (>= beg-B beg-A) (<= end-B end-A)))))
4939 (defun org-element-swap-A-B (elem-A elem-B)
4940 "Swap elements ELEM-A and ELEM-B.
4941 Assume ELEM-B is after ELEM-A in the buffer. Leave point at the
4942 end of ELEM-A."
4943 (goto-char (org-element-property :begin elem-A))
4944 ;; There are two special cases when an element doesn't start at bol:
4945 ;; the first paragraph in an item or in a footnote definition.
4946 (let ((specialp (not (bolp))))
4947 ;; Only a paragraph without any affiliated keyword can be moved at
4948 ;; ELEM-A position in such a situation. Note that the case of
4949 ;; a footnote definition is impossible: it cannot contain two
4950 ;; paragraphs in a row because it cannot contain a blank line.
4951 (if (and specialp
4952 (or (not (eq (org-element-type elem-B) 'paragraph))
4953 (/= (org-element-property :begin elem-B)
4954 (org-element-property :contents-begin elem-B))))
4955 (error "Cannot swap elements"))
4956 ;; In a special situation, ELEM-A will have no indentation. We'll
4957 ;; give it ELEM-B's (which will in, in turn, have no indentation).
4958 (let* ((ind-B (when specialp
4959 (goto-char (org-element-property :begin elem-B))
4960 (org-get-indentation)))
4961 (beg-A (org-element-property :begin elem-A))
4962 (end-A (save-excursion
4963 (goto-char (org-element-property :end elem-A))
4964 (skip-chars-backward " \r\t\n")
4965 (point-at-eol)))
4966 (beg-B (org-element-property :begin elem-B))
4967 (end-B (save-excursion
4968 (goto-char (org-element-property :end elem-B))
4969 (skip-chars-backward " \r\t\n")
4970 (point-at-eol)))
4971 ;; Store overlays responsible for visibility status. We
4972 ;; also need to store their boundaries as they will be
4973 ;; removed from buffer.
4974 (overlays
4975 (cons
4976 (mapcar (lambda (ov) (list ov (overlay-start ov) (overlay-end ov)))
4977 (overlays-in beg-A end-A))
4978 (mapcar (lambda (ov) (list ov (overlay-start ov) (overlay-end ov)))
4979 (overlays-in beg-B end-B))))
4980 ;; Get contents.
4981 (body-A (buffer-substring beg-A end-A))
4982 (body-B (delete-and-extract-region beg-B end-B)))
4983 (goto-char beg-B)
4984 (when specialp
4985 (setq body-B (replace-regexp-in-string "\\`[ \t]*" "" body-B))
4986 (org-indent-to-column ind-B))
4987 (insert body-A)
4988 ;; Restore ex ELEM-A overlays.
4989 (let ((offset (- beg-B beg-A)))
4990 (mapc (lambda (ov)
4991 (move-overlay
4992 (car ov) (+ (nth 1 ov) offset) (+ (nth 2 ov) offset)))
4993 (car overlays))
4994 (goto-char beg-A)
4995 (delete-region beg-A end-A)
4996 (insert body-B)
4997 ;; Restore ex ELEM-B overlays.
4998 (mapc (lambda (ov)
4999 (move-overlay
5000 (car ov) (- (nth 1 ov) offset) (- (nth 2 ov) offset)))
5001 (cdr overlays)))
5002 (goto-char (org-element-property :end elem-B)))))
5004 (provide 'org-element)
5006 ;; Local variables:
5007 ;; generated-autoload-file: "org-loaddefs.el"
5008 ;; End:
5010 ;;; org-element.el ends here