org-element: Fix `org-element-context' when in recursive objects
[org-mode.git] / lisp / org-element.el
blobfe4a18405ab1d7e1f51bd3873d62cf7428f59609
1 ;;; org-element.el --- Parser And Applications for Org syntax
3 ;; Copyright (C) 2012 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
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 (namely `babel-call', `clock', `headline', `item',
34 ;; `keyword', `planning', `property-drawer' and `section' types), it
35 ;; can also accept a fixed set of keywords as attributes. Those are
36 ;; called "affiliated keywords" to distinguish them from other
37 ;; keywords, which are full-fledged elements. Almost all affiliated
38 ;; keywords are referenced in `org-element-affiliated-keywords'; the
39 ;; others are export attributes and start with "ATTR_" prefix.
41 ;; Element containing other elements (and only elements) are called
42 ;; greater elements. Concerned types are: `center-block', `drawer',
43 ;; `dynamic-block', `footnote-definition', `headline', `inlinetask',
44 ;; `item', `plain-list', `quote-block', `section' and `special-block'.
46 ;; Other element types are: `babel-call', `clock', `comment',
47 ;; `comment-block', `example-block', `export-block', `fixed-width',
48 ;; `horizontal-rule', `keyword', `latex-environment', `paragraph',
49 ;; `planning', `property-drawer', `quote-section', `src-block',
50 ;; `table', `table-row' and `verse-block'. Among them, `paragraph'
51 ;; and `verse-block' types can contain Org objects and plain text.
53 ;; Objects are related to document's contents. Some of them are
54 ;; recursive. Associated types are of the following: `bold', `code',
55 ;; `entity', `export-snippet', `footnote-reference',
56 ;; `inline-babel-call', `inline-src-block', `italic',
57 ;; `latex-fragment', `line-break', `link', `macro', `radio-target',
58 ;; `statistics-cookie', `strike-through', `subscript', `superscript',
59 ;; `table-cell', `target', `timestamp', `underline' and `verbatim'.
61 ;; Some elements also have special properties whose value can hold
62 ;; objects themselves (i.e. an item tag or an headline name). Such
63 ;; values are called "secondary strings". Any object belongs to
64 ;; either an element or a secondary string.
66 ;; Notwithstanding affiliated keywords, each greater element, element
67 ;; and object has a fixed set of properties attached to it. Among
68 ;; them, four are shared by all types: `:begin' and `:end', which
69 ;; refer to the beginning and ending buffer positions of the
70 ;; considered element or object, `:post-blank', which holds the number
71 ;; of blank lines, or white spaces, at its end and `:parent' which
72 ;; refers to the element or object containing it. Greater elements
73 ;; and elements containing objects will also have `:contents-begin'
74 ;; and `:contents-end' properties to delimit contents.
76 ;; Lisp-wise, an element or an object can be represented as a list.
77 ;; It follows the pattern (TYPE PROPERTIES CONTENTS), where:
78 ;; TYPE is a symbol describing the Org element or object.
79 ;; PROPERTIES is the property list attached to it. See docstring of
80 ;; appropriate parsing function to get an exhaustive
81 ;; list.
82 ;; CONTENTS is a list of elements, objects or raw strings contained
83 ;; in the current element or object, when applicable.
85 ;; An Org buffer is a nested list of such elements and objects, whose
86 ;; type is `org-data' and properties is nil.
88 ;; The first part of this file defines Org syntax, while the second
89 ;; one provide accessors and setters functions.
91 ;; The next part implements a parser and an interpreter for each
92 ;; element and object type in Org syntax.
94 ;; The following part creates a fully recursive buffer parser. It
95 ;; also provides a tool to map a function to elements or objects
96 ;; matching some criteria in the parse tree. Functions of interest
97 ;; are `org-element-parse-buffer', `org-element-map' and, to a lesser
98 ;; extent, `org-element-parse-secondary-string'.
100 ;; The penultimate part is the cradle of an interpreter for the
101 ;; obtained parse tree: `org-element-interpret-data'.
103 ;; The library ends by furnishing `org-element-at-point' function, and
104 ;; a way to give information about document structure around point
105 ;; with `org-element-context'.
108 ;;; Code:
110 (eval-when-compile
111 (require 'cl))
113 (require 'org)
116 ;;; Definitions And Rules
118 ;; Define elements, greater elements and specify recursive objects,
119 ;; along with the affiliated keywords recognized. Also set up
120 ;; restrictions on recursive objects combinations.
122 ;; These variables really act as a control center for the parsing
123 ;; process.
125 (defconst org-element-paragraph-separate
126 (concat "^\\(?:"
127 ;; Headlines, inlinetasks.
128 org-outline-regexp "\\|"
129 ;; Footnote definitions.
130 "\\[\\(?:[0-9]+\\|fn:[-_[:word:]]+\\)\\]" "\\|"
131 "[ \t]*\\(?:"
132 ;; Empty lines.
133 "$" "\\|"
134 ;; Tables (any type).
135 "\\(?:|\\|\\+-[-+]\\)" "\\|"
136 ;; Blocks (any type), Babel calls, drawers (any type),
137 ;; fixed-width areas and keywords. Note: this is only an
138 ;; indication and need some thorough check.
139 "[#:]" "\\|"
140 ;; Horizontal rules.
141 "-\\{5,\\}[ \t]*$" "\\|"
142 ;; LaTeX environments.
143 "\\\\begin{\\([A-Za-z0-9]+\\*?\\)}" "\\|"
144 ;; Planning and Clock lines.
145 (regexp-opt (list org-scheduled-string
146 org-deadline-string
147 org-closed-string
148 org-clock-string))
149 "\\|"
150 ;; Lists.
151 (let ((term (case org-plain-list-ordered-item-terminator
152 (?\) ")") (?. "\\.") (otherwise "[.)]")))
153 (alpha (and org-alphabetical-lists "\\|[A-Za-z]")))
154 (concat "\\(?:[-+*]\\|\\(?:[0-9]+" alpha "\\)" term "\\)"
155 "\\(?:[ \t]\\|$\\)"))
156 "\\)\\)")
157 "Regexp to separate paragraphs in an Org buffer.
158 In the case of lines starting with \"#\" and \":\", this regexp
159 is not sufficient to know if point is at a paragraph ending. See
160 `org-element-paragraph-parser' for more information.")
162 (defconst org-element-all-elements
163 '(center-block clock comment comment-block drawer dynamic-block example-block
164 export-block fixed-width footnote-definition headline
165 horizontal-rule inlinetask item keyword latex-environment
166 babel-call paragraph plain-list planning property-drawer
167 quote-block quote-section section special-block src-block table
168 table-row verse-block)
169 "Complete list of element types.")
171 (defconst org-element-greater-elements
172 '(center-block drawer dynamic-block footnote-definition headline inlinetask
173 item plain-list quote-block section special-block table)
174 "List of recursive element types aka Greater Elements.")
176 (defconst org-element-all-successors
177 '(export-snippet footnote-reference inline-babel-call inline-src-block
178 latex-or-entity line-break link macro radio-target
179 statistics-cookie sub/superscript table-cell target
180 text-markup timestamp)
181 "Complete list of successors.")
183 (defconst org-element-object-successor-alist
184 '((subscript . sub/superscript) (superscript . sub/superscript)
185 (bold . text-markup) (code . text-markup) (italic . text-markup)
186 (strike-through . text-markup) (underline . text-markup)
187 (verbatim . text-markup) (entity . latex-or-entity)
188 (latex-fragment . latex-or-entity))
189 "Alist of translations between object type and successor name.
191 Sharing the same successor comes handy when, for example, the
192 regexp matching one object can also match the other object.")
194 (defconst org-element-all-objects
195 '(bold code entity export-snippet footnote-reference inline-babel-call
196 inline-src-block italic line-break latex-fragment link macro
197 radio-target statistics-cookie strike-through subscript superscript
198 table-cell target timestamp underline verbatim)
199 "Complete list of object types.")
201 (defconst org-element-recursive-objects
202 '(bold italic link macro subscript radio-target strike-through superscript
203 table-cell underline)
204 "List of recursive object types.")
206 (defconst org-element-block-name-alist
207 '(("CENTER" . org-element-center-block-parser)
208 ("COMMENT" . org-element-comment-block-parser)
209 ("EXAMPLE" . org-element-example-block-parser)
210 ("QUOTE" . org-element-quote-block-parser)
211 ("SRC" . org-element-src-block-parser)
212 ("VERSE" . org-element-verse-block-parser))
213 "Alist between block names and the associated parsing function.
214 Names must be uppercase. Any block whose name has no association
215 is parsed with `org-element-special-block-parser'.")
217 (defconst org-element-affiliated-keywords
218 '("CAPTION" "DATA" "HEADER" "HEADERS" "LABEL" "NAME" "PLOT" "RESNAME" "RESULT"
219 "RESULTS" "SOURCE" "SRCNAME" "TBLNAME")
220 "List of affiliated keywords as strings.
221 By default, all keywords setting attributes (i.e. \"ATTR_LATEX\")
222 are affiliated keywords and need not to be in this list.")
224 (defconst org-element--affiliated-re
225 (format "[ \t]*#\\+%s:"
226 ;; Regular affiliated keywords.
227 (format "\\(%s\\|ATTR_[-_A-Za-z0-9]+\\)\\(?:\\[\\(.*\\)\\]\\)?"
228 (regexp-opt org-element-affiliated-keywords)))
229 "Regexp matching any affiliated keyword.
231 Keyword name is put in match group 1. Moreover, if keyword
232 belongs to `org-element-dual-keywords', put the dual value in
233 match group 2.
235 Don't modify it, set `org-element-affiliated-keywords' instead.")
237 (defconst org-element-keyword-translation-alist
238 '(("DATA" . "NAME") ("LABEL" . "NAME") ("RESNAME" . "NAME")
239 ("SOURCE" . "NAME") ("SRCNAME" . "NAME") ("TBLNAME" . "NAME")
240 ("RESULT" . "RESULTS") ("HEADERS" . "HEADER"))
241 "Alist of usual translations for keywords.
242 The key is the old name and the value the new one. The property
243 holding their value will be named after the translated name.")
245 (defconst org-element-multiple-keywords '("HEADER")
246 "List of affiliated keywords that can occur more that once in an element.
248 Their value will be consed into a list of strings, which will be
249 returned as the value of the property.
251 This list is checked after translations have been applied. See
252 `org-element-keyword-translation-alist'.
254 By default, all keywords setting attributes (i.e. \"ATTR_LATEX\")
255 allow multiple occurrences and need not to be in this list.")
257 (defconst org-element-parsed-keywords '("AUTHOR" "CAPTION" "DATE" "TITLE")
258 "List of keywords whose value can be parsed.
260 Their value will be stored as a secondary string: a list of
261 strings and objects.
263 This list is checked after translations have been applied. See
264 `org-element-keyword-translation-alist'.")
266 (defconst org-element-dual-keywords '("CAPTION" "RESULTS")
267 "List of keywords which can have a secondary value.
269 In Org syntax, they can be written with optional square brackets
270 before the colons. For example, results keyword can be
271 associated to a hash value with the following:
273 #+RESULTS[hash-string]: some-source
275 This list is checked after translations have been applied. See
276 `org-element-keyword-translation-alist'.")
278 (defconst org-element-object-restrictions
279 '((bold export-snippet inline-babel-call inline-src-block latex-or-entity link
280 radio-target sub/superscript target text-markup timestamp)
281 (footnote-reference export-snippet footnote-reference inline-babel-call
282 inline-src-block latex-or-entity line-break link macro
283 radio-target sub/superscript target text-markup
284 timestamp)
285 (headline inline-babel-call inline-src-block latex-or-entity link macro
286 radio-target statistics-cookie sub/superscript target text-markup
287 timestamp)
288 (inlinetask inline-babel-call inline-src-block latex-or-entity link macro
289 radio-target sub/superscript target text-markup timestamp)
290 (italic export-snippet inline-babel-call inline-src-block latex-or-entity
291 link radio-target sub/superscript target text-markup timestamp)
292 (item export-snippet footnote-reference inline-babel-call latex-or-entity
293 link macro radio-target sub/superscript target text-markup)
294 (keyword latex-or-entity macro sub/superscript text-markup)
295 (link export-snippet inline-babel-call inline-src-block latex-or-entity link
296 sub/superscript text-markup)
297 (macro macro)
298 (paragraph export-snippet footnote-reference inline-babel-call
299 inline-src-block latex-or-entity line-break link macro
300 radio-target statistics-cookie sub/superscript target text-markup
301 timestamp)
302 (radio-target export-snippet latex-or-entity sub/superscript)
303 (strike-through export-snippet inline-babel-call inline-src-block
304 latex-or-entity link radio-target sub/superscript target
305 text-markup timestamp)
306 (subscript export-snippet inline-babel-call inline-src-block latex-or-entity
307 sub/superscript target text-markup)
308 (superscript export-snippet inline-babel-call inline-src-block
309 latex-or-entity sub/superscript target text-markup)
310 (table-cell export-snippet latex-or-entity link macro radio-target
311 sub/superscript target text-markup timestamp)
312 (table-row table-cell)
313 (underline export-snippet inline-babel-call inline-src-block latex-or-entity
314 link radio-target sub/superscript target text-markup timestamp)
315 (verse-block footnote-reference inline-babel-call inline-src-block
316 latex-or-entity line-break link macro radio-target
317 sub/superscript target text-markup timestamp))
318 "Alist of objects restrictions.
320 CAR is an element or object type containing objects and CDR is
321 a list of successors that will be called within an element or
322 object of such type.
324 For example, in a `radio-target' object, one can only find
325 entities, export snippets, latex-fragments, subscript and
326 superscript.
328 This alist also applies to secondary string. For example, an
329 `headline' type element doesn't directly contain objects, but
330 still has an entry since one of its properties (`:title') does.")
332 (defconst org-element-secondary-value-alist
333 '((headline . :title)
334 (inlinetask . :title)
335 (item . :tag)
336 (footnote-reference . :inline-definition))
337 "Alist between element types and location of secondary value.")
341 ;;; Accessors and Setters
343 ;; Provide four accessors: `org-element-type', `org-element-property'
344 ;; `org-element-contents' and `org-element-restriction'.
346 ;; Setter functions allow to modify elements by side effect. There is
347 ;; `org-element-put-property', `org-element-set-contents',
348 ;; `org-element-set-element' and `org-element-adopt-element'. Note
349 ;; that `org-element-set-element' and `org-element-adopt-elements' are
350 ;; higher level functions since also update `:parent' property.
352 (defsubst org-element-type (element)
353 "Return type of ELEMENT.
355 The function returns the type of the element or object provided.
356 It can also return the following special value:
357 `plain-text' for a string
358 `org-data' for a complete document
359 nil in any other case."
360 (cond
361 ((not (consp element)) (and (stringp element) 'plain-text))
362 ((symbolp (car element)) (car element))))
364 (defsubst org-element-property (property element)
365 "Extract the value from the PROPERTY of an ELEMENT."
366 (plist-get (nth 1 element) property))
368 (defsubst org-element-contents (element)
369 "Extract contents from an ELEMENT."
370 (and (consp element) (nthcdr 2 element)))
372 (defsubst org-element-restriction (element)
373 "Return restriction associated to ELEMENT.
374 ELEMENT can be an element, an object or a symbol representing an
375 element or object type."
376 (cdr (assq (if (symbolp element) element (org-element-type element))
377 org-element-object-restrictions)))
379 (defsubst org-element-put-property (element property value)
380 "In ELEMENT set PROPERTY to VALUE.
381 Return modified element."
382 (when (consp element)
383 (setcar (cdr element) (plist-put (nth 1 element) property value)))
384 element)
386 (defsubst org-element-set-contents (element &rest contents)
387 "Set ELEMENT contents to CONTENTS.
388 Return modified element."
389 (cond ((not element) (list contents))
390 ((cdr element) (setcdr (cdr element) contents))
391 (t (nconc element contents))))
393 (defsubst org-element-set-element (old new)
394 "Replace element or object OLD with element or object NEW.
395 The function takes care of setting `:parent' property for NEW."
396 ;; Since OLD is going to be changed into NEW by side-effect, first
397 ;; make sure that every element or object within NEW has OLD as
398 ;; parent.
399 (mapc (lambda (blob) (org-element-put-property blob :parent old))
400 (org-element-contents new))
401 ;; Transfer contents.
402 (apply 'org-element-set-contents old (org-element-contents new))
403 ;; Ensure NEW has same parent as OLD, then overwrite OLD properties
404 ;; with NEW's.
405 (org-element-put-property new :parent (org-element-property :parent old))
406 (setcar (cdr old) (nth 1 new))
407 ;; Transfer type.
408 (setcar old (car new)))
410 (defsubst org-element-adopt-elements (parent &rest children)
411 "Append elements to the contents of another element.
413 PARENT is an element or object. CHILDREN can be elements,
414 objects, or a strings.
416 The function takes care of setting `:parent' property for CHILD.
417 Return parent element."
418 (if (not parent) children
419 ;; Link every child to PARENT.
420 (mapc (lambda (child)
421 (unless (stringp child)
422 (org-element-put-property child :parent parent)))
423 children)
424 ;; Add CHILDREN at the end of PARENT contents.
425 (apply 'org-element-set-contents
426 parent
427 (nconc (org-element-contents parent) children))
428 ;; Return modified PARENT element.
429 parent))
433 ;;; Greater elements
435 ;; For each greater element type, we define a parser and an
436 ;; interpreter.
438 ;; A parser returns the element or object as the list described above.
439 ;; Most of them accepts no argument. Though, exceptions exist. Hence
440 ;; every element containing a secondary string (see
441 ;; `org-element-secondary-value-alist') will accept an optional
442 ;; argument to toggle parsing of that secondary string. Moreover,
443 ;; `item' parser requires current list's structure as its first
444 ;; element.
446 ;; An interpreter accepts two arguments: the list representation of
447 ;; the element or object, and its contents. The latter may be nil,
448 ;; depending on the element or object considered. It returns the
449 ;; appropriate Org syntax, as a string.
451 ;; Parsing functions must follow the naming convention:
452 ;; org-element-TYPE-parser, where TYPE is greater element's type, as
453 ;; defined in `org-element-greater-elements'.
455 ;; Similarly, interpreting functions must follow the naming
456 ;; convention: org-element-TYPE-interpreter.
458 ;; With the exception of `headline' and `item' types, greater elements
459 ;; cannot contain other greater elements of their own type.
461 ;; Beside implementing a parser and an interpreter, adding a new
462 ;; greater element requires to tweak `org-element--current-element'.
463 ;; Moreover, the newly defined type must be added to both
464 ;; `org-element-all-elements' and `org-element-greater-elements'.
467 ;;;; Center Block
469 (defun org-element-center-block-parser (limit)
470 "Parse a center block.
472 LIMIT bounds the search.
474 Return a list whose CAR is `center-block' and CDR is a plist
475 containing `:begin', `:end', `:hiddenp', `:contents-begin',
476 `:contents-end' and `:post-blank' keywords.
478 Assume point is at the beginning of the block."
479 (let ((case-fold-search t))
480 (if (not (save-excursion
481 (re-search-forward "^[ \t]*#\\+END_CENTER[ \t]*$" limit t)))
482 ;; Incomplete block: parse it as a paragraph.
483 (org-element-paragraph-parser limit)
484 (let ((block-end-line (match-beginning 0)))
485 (let* ((keywords (org-element--collect-affiliated-keywords))
486 (begin (car keywords))
487 ;; Empty blocks have no contents.
488 (contents-begin (progn (forward-line)
489 (and (< (point) block-end-line)
490 (point))))
491 (contents-end (and contents-begin block-end-line))
492 (hidden (org-invisible-p2))
493 (pos-before-blank (progn (goto-char block-end-line)
494 (forward-line)
495 (point)))
496 (end (save-excursion (skip-chars-forward " \r\t\n" limit)
497 (if (eobp) (point) (point-at-bol)))))
498 (list 'center-block
499 (nconc
500 (list :begin begin
501 :end end
502 :hiddenp hidden
503 :contents-begin contents-begin
504 :contents-end contents-end
505 :post-blank (count-lines pos-before-blank end))
506 (cadr keywords))))))))
508 (defun org-element-center-block-interpreter (center-block contents)
509 "Interpret CENTER-BLOCK element as Org syntax.
510 CONTENTS is the contents of the element."
511 (format "#+BEGIN_CENTER\n%s#+END_CENTER" contents))
514 ;;;; Drawer
516 (defun org-element-drawer-parser (limit)
517 "Parse a drawer.
519 LIMIT bounds the search.
521 Return a list whose CAR is `drawer' and CDR is a plist containing
522 `:drawer-name', `:begin', `:end', `:hiddenp', `:contents-begin',
523 `:contents-end' and `:post-blank' keywords.
525 Assume point is at beginning of drawer."
526 (let ((case-fold-search t))
527 (if (not (save-excursion (re-search-forward "^[ \t]*:END:[ \t]*$" limit t)))
528 ;; Incomplete drawer: parse it as a paragraph.
529 (org-element-paragraph-parser limit)
530 (let ((drawer-end-line (match-beginning 0)))
531 (save-excursion
532 (let* ((case-fold-search t)
533 (name (progn (looking-at org-drawer-regexp)
534 (org-match-string-no-properties 1)))
535 (keywords (org-element--collect-affiliated-keywords))
536 (begin (car keywords))
537 ;; Empty drawers have no contents.
538 (contents-begin (progn (forward-line)
539 (and (< (point) drawer-end-line)
540 (point))))
541 (contents-end (and contents-begin drawer-end-line))
542 (hidden (org-invisible-p2))
543 (pos-before-blank (progn (goto-char drawer-end-line)
544 (forward-line)
545 (point)))
546 (end (progn (skip-chars-forward " \r\t\n" limit)
547 (if (eobp) (point) (point-at-bol)))))
548 (list 'drawer
549 (nconc
550 (list :begin begin
551 :end end
552 :drawer-name name
553 :hiddenp hidden
554 :contents-begin contents-begin
555 :contents-end contents-end
556 :post-blank (count-lines pos-before-blank end))
557 (cadr keywords)))))))))
559 (defun org-element-drawer-interpreter (drawer contents)
560 "Interpret DRAWER element as Org syntax.
561 CONTENTS is the contents of the element."
562 (format ":%s:\n%s:END:"
563 (org-element-property :drawer-name drawer)
564 contents))
567 ;;;; Dynamic Block
569 (defun org-element-dynamic-block-parser (limit)
570 "Parse a dynamic block.
572 LIMIT bounds the search.
574 Return a list whose CAR is `dynamic-block' and CDR is a plist
575 containing `:block-name', `:begin', `:end', `:hiddenp',
576 `:contents-begin', `:contents-end', `:arguments' and
577 `:post-blank' keywords.
579 Assume point is at beginning of dynamic block."
580 (let ((case-fold-search t))
581 (if (not (save-excursion
582 (re-search-forward "^[ \t]*#\\+END:?[ \t]*$" limit t)))
583 ;; Incomplete block: parse it as a paragraph.
584 (org-element-paragraph-parser limit)
585 (let ((block-end-line (match-beginning 0)))
586 (save-excursion
587 (let* ((name (progn (looking-at org-dblock-start-re)
588 (org-match-string-no-properties 1)))
589 (arguments (org-match-string-no-properties 3))
590 (keywords (org-element--collect-affiliated-keywords))
591 (begin (car keywords))
592 ;; Empty blocks have no contents.
593 (contents-begin (progn (forward-line)
594 (and (< (point) block-end-line)
595 (point))))
596 (contents-end (and contents-begin block-end-line))
597 (hidden (org-invisible-p2))
598 (pos-before-blank (progn (goto-char block-end-line)
599 (forward-line)
600 (point)))
601 (end (progn (skip-chars-forward " \r\t\n" limit)
602 (if (eobp) (point) (point-at-bol)))))
603 (list 'dynamic-block
604 (nconc
605 (list :begin begin
606 :end end
607 :block-name name
608 :arguments arguments
609 :hiddenp hidden
610 :contents-begin contents-begin
611 :contents-end contents-end
612 :post-blank (count-lines pos-before-blank end))
613 (cadr keywords)))))))))
615 (defun org-element-dynamic-block-interpreter (dynamic-block contents)
616 "Interpret DYNAMIC-BLOCK element as Org syntax.
617 CONTENTS is the contents of the element."
618 (format "#+BEGIN: %s%s\n%s#+END:"
619 (org-element-property :block-name dynamic-block)
620 (let ((args (org-element-property :arguments dynamic-block)))
621 (and args (concat " " args)))
622 contents))
625 ;;;; Footnote Definition
627 (defun org-element-footnote-definition-parser (limit)
628 "Parse a footnote definition.
630 LIMIT bounds the search.
632 Return a list whose CAR is `footnote-definition' and CDR is
633 a plist containing `:label', `:begin' `:end', `:contents-begin',
634 `:contents-end' and `:post-blank' keywords.
636 Assume point is at the beginning of the footnote definition."
637 (save-excursion
638 (let* ((label (progn (looking-at org-footnote-definition-re)
639 (org-match-string-no-properties 1)))
640 (keywords (org-element--collect-affiliated-keywords))
641 (begin (car keywords))
642 (ending (save-excursion
643 (if (progn
644 (end-of-line)
645 (re-search-forward
646 (concat org-outline-regexp-bol "\\|"
647 org-footnote-definition-re "\\|"
648 "^[ \t]*$") limit 'move))
649 (match-beginning 0)
650 (point))))
651 (contents-begin (progn (search-forward "]")
652 (skip-chars-forward " \r\t\n" ending)
653 (and (/= (point) ending) (point))))
654 (contents-end (and contents-begin ending))
655 (end (progn (goto-char ending)
656 (skip-chars-forward " \r\t\n" limit)
657 (if (eobp) (point) (point-at-bol)))))
658 (list 'footnote-definition
659 (nconc
660 (list :label label
661 :begin begin
662 :end end
663 :contents-begin contents-begin
664 :contents-end contents-end
665 :post-blank (count-lines ending end))
666 (cadr keywords))))))
668 (defun org-element-footnote-definition-interpreter (footnote-definition contents)
669 "Interpret FOOTNOTE-DEFINITION element as Org syntax.
670 CONTENTS is the contents of the footnote-definition."
671 (concat (format "[%s]" (org-element-property :label footnote-definition))
673 contents))
676 ;;;; Headline
678 (defun org-element-headline-parser (limit &optional raw-secondary-p)
679 "Parse an headline.
681 Return a list whose CAR is `headline' and CDR is a plist
682 containing `:raw-value', `:title', `:begin', `:end',
683 `:pre-blank', `:hiddenp', `:contents-begin' and `:contents-end',
684 `:level', `:priority', `:tags', `:todo-keyword',`:todo-type',
685 `:scheduled', `:deadline', `:timestamp', `:clock', `:category',
686 `:quotedp', `:archivedp', `:commentedp' and `:footnote-section-p'
687 keywords.
689 The plist also contains any property set in the property drawer,
690 with its name in lowercase, the underscores replaced with hyphens
691 and colons at the beginning (i.e. `:custom-id').
693 When RAW-SECONDARY-P is non-nil, headline's title will not be
694 parsed as a secondary string, but as a plain string instead.
696 Assume point is at beginning of the headline."
697 (save-excursion
698 (let* ((components (org-heading-components))
699 (level (nth 1 components))
700 (todo (nth 2 components))
701 (todo-type
702 (and todo (if (member todo org-done-keywords) 'done 'todo)))
703 (tags (let ((raw-tags (nth 5 components)))
704 (and raw-tags (org-split-string raw-tags ":"))))
705 (raw-value (or (nth 4 components) ""))
706 (quotedp
707 (let ((case-fold-search nil))
708 (string-match (format "^%s\\( \\|$\\)" org-quote-string)
709 raw-value)))
710 (commentedp
711 (let ((case-fold-search nil))
712 (string-match (format "^%s\\( \\|$\\)" org-comment-string)
713 raw-value)))
714 (archivedp (member org-archive-tag tags))
715 (footnote-section-p (and org-footnote-section
716 (string= org-footnote-section raw-value)))
717 ;; Normalize property names: ":SOME_PROP:" becomes
718 ;; ":some-prop".
719 (standard-props (let (plist)
720 (mapc
721 (lambda (p)
722 (let ((p-name (downcase (car p))))
723 (while (string-match "_" p-name)
724 (setq p-name
725 (replace-match "-" nil nil p-name)))
726 (setq p-name (intern (concat ":" p-name)))
727 (setq plist
728 (plist-put plist p-name (cdr p)))))
729 (org-entry-properties nil 'standard))
730 plist))
731 (time-props (org-entry-properties nil 'special "CLOCK"))
732 (scheduled (cdr (assoc "SCHEDULED" time-props)))
733 (deadline (cdr (assoc "DEADLINE" time-props)))
734 (clock (cdr (assoc "CLOCK" time-props)))
735 (timestamp (cdr (assoc "TIMESTAMP" time-props)))
736 (begin (point))
737 (end (save-excursion (goto-char (org-end-of-subtree t t))))
738 (pos-after-head (progn (forward-line) (point)))
739 (contents-begin (save-excursion
740 (skip-chars-forward " \r\t\n" end)
741 (and (/= (point) end) (line-beginning-position))))
742 (hidden (org-invisible-p2))
743 (contents-end (and contents-begin
744 (progn (goto-char end)
745 (skip-chars-backward " \r\t\n")
746 (forward-line)
747 (point)))))
748 ;; Clean RAW-VALUE from any quote or comment string.
749 (when (or quotedp commentedp)
750 (let ((case-fold-search nil))
751 (setq raw-value
752 (replace-regexp-in-string
753 (concat
754 (regexp-opt (list org-quote-string org-comment-string))
755 "\\(?: \\|$\\)")
757 raw-value))))
758 ;; Clean TAGS from archive tag, if any.
759 (when archivedp (setq tags (delete org-archive-tag tags)))
760 (let ((headline
761 (list 'headline
762 (nconc
763 (list :raw-value raw-value
764 :begin begin
765 :end end
766 :pre-blank
767 (if (not contents-begin) 0
768 (count-lines pos-after-head contents-begin))
769 :hiddenp hidden
770 :contents-begin contents-begin
771 :contents-end contents-end
772 :level level
773 :priority (nth 3 components)
774 :tags tags
775 :todo-keyword todo
776 :todo-type todo-type
777 :scheduled scheduled
778 :deadline deadline
779 :timestamp timestamp
780 :clock clock
781 :post-blank (count-lines
782 (if (not contents-end) pos-after-head
783 (goto-char contents-end)
784 (forward-line)
785 (point))
786 end)
787 :footnote-section-p footnote-section-p
788 :archivedp archivedp
789 :commentedp commentedp
790 :quotedp quotedp)
791 standard-props))))
792 (org-element-put-property
793 headline :title
794 (if raw-secondary-p raw-value
795 (org-element-parse-secondary-string
796 raw-value (org-element-restriction 'headline) headline)))))))
798 (defun org-element-headline-interpreter (headline contents)
799 "Interpret HEADLINE element as Org syntax.
800 CONTENTS is the contents of the element."
801 (let* ((level (org-element-property :level headline))
802 (todo (org-element-property :todo-keyword headline))
803 (priority (org-element-property :priority headline))
804 (title (org-element-interpret-data
805 (org-element-property :title headline)))
806 (tags (let ((tag-list (if (org-element-property :archivedp headline)
807 (cons org-archive-tag
808 (org-element-property :tags headline))
809 (org-element-property :tags headline))))
810 (and tag-list
811 (format ":%s:" (mapconcat 'identity tag-list ":")))))
812 (commentedp (org-element-property :commentedp headline))
813 (quotedp (org-element-property :quotedp headline))
814 (pre-blank (or (org-element-property :pre-blank headline) 0))
815 (heading (concat (make-string level ?*)
816 (and todo (concat " " todo))
817 (and quotedp (concat " " org-quote-string))
818 (and commentedp (concat " " org-comment-string))
819 (and priority
820 (format " [#%s]" (char-to-string priority)))
821 (cond ((and org-footnote-section
822 (org-element-property
823 :footnote-section-p headline))
824 (concat " " org-footnote-section))
825 (title (concat " " title))))))
826 (concat heading
827 ;; Align tags.
828 (when tags
829 (cond
830 ((zerop org-tags-column) (format " %s" tags))
831 ((< org-tags-column 0)
832 (concat
833 (make-string
834 (max (- (+ org-tags-column (length heading) (length tags))) 1)
836 tags))
838 (concat
839 (make-string (max (- org-tags-column (length heading)) 1) ? )
840 tags))))
841 (make-string (1+ pre-blank) 10)
842 contents)))
845 ;;;; Inlinetask
847 (defun org-element-inlinetask-parser (limit &optional raw-secondary-p)
848 "Parse an inline task.
850 Return a list whose CAR is `inlinetask' and CDR is a plist
851 containing `:title', `:begin', `:end', `:hiddenp',
852 `:contents-begin' and `:contents-end', `:level', `:priority',
853 `:raw-value', `:tags', `:todo-keyword', `:todo-type',
854 `:scheduled', `:deadline', `:timestamp', `:clock' and
855 `:post-blank' keywords.
857 The plist also contains any property set in the property drawer,
858 with its name in lowercase, the underscores replaced with hyphens
859 and colons at the beginning (i.e. `:custom-id').
861 When optional argument RAW-SECONDARY-P is non-nil, inline-task's
862 title will not be parsed as a secondary string, but as a plain
863 string instead.
865 Assume point is at beginning of the inline task."
866 (save-excursion
867 (let* ((keywords (org-element--collect-affiliated-keywords))
868 (begin (car keywords))
869 (components (org-heading-components))
870 (todo (nth 2 components))
871 (todo-type (and todo
872 (if (member todo org-done-keywords) 'done 'todo)))
873 (tags (let ((raw-tags (nth 5 components)))
874 (and raw-tags (org-split-string raw-tags ":"))))
875 (raw-value (or (nth 4 components) ""))
876 ;; Normalize property names: ":SOME_PROP:" becomes
877 ;; ":some-prop".
878 (standard-props (let (plist)
879 (mapc
880 (lambda (p)
881 (let ((p-name (downcase (car p))))
882 (while (string-match "_" p-name)
883 (setq p-name
884 (replace-match "-" nil nil p-name)))
885 (setq p-name (intern (concat ":" p-name)))
886 (setq plist
887 (plist-put plist p-name (cdr p)))))
888 (org-entry-properties nil 'standard))
889 plist))
890 (time-props (org-entry-properties nil 'special "CLOCK"))
891 (scheduled (cdr (assoc "SCHEDULED" time-props)))
892 (deadline (cdr (assoc "DEADLINE" time-props)))
893 (clock (cdr (assoc "CLOCK" time-props)))
894 (timestamp (cdr (assoc "TIMESTAMP" time-props)))
895 (task-end (save-excursion
896 (end-of-line)
897 (and (re-search-forward "^\\*+ END" limit t)
898 (match-beginning 0))))
899 (contents-begin (progn (forward-line)
900 (and task-end (< (point) task-end) (point))))
901 (hidden (and contents-begin (org-invisible-p2)))
902 (contents-end (and contents-begin task-end))
903 (before-blank (if (not task-end) (point)
904 (goto-char task-end)
905 (forward-line)
906 (point)))
907 (end (progn (skip-chars-forward " \r\t\n" limit)
908 (if (eobp) (point) (point-at-bol))))
909 (inlinetask
910 (list 'inlinetask
911 (nconc
912 (list :raw-value raw-value
913 :begin begin
914 :end end
915 :hiddenp hidden
916 :contents-begin contents-begin
917 :contents-end contents-end
918 :level (nth 1 components)
919 :priority (nth 3 components)
920 :tags tags
921 :todo-keyword todo
922 :todo-type todo-type
923 :scheduled scheduled
924 :deadline deadline
925 :timestamp timestamp
926 :clock clock
927 :post-blank (count-lines before-blank end))
928 standard-props
929 (cadr keywords)))))
930 (org-element-put-property
931 inlinetask :title
932 (if raw-secondary-p raw-value
933 (org-element-parse-secondary-string
934 raw-value
935 (org-element-restriction 'inlinetask)
936 inlinetask))))))
938 (defun org-element-inlinetask-interpreter (inlinetask contents)
939 "Interpret INLINETASK element as Org syntax.
940 CONTENTS is the contents of inlinetask."
941 (let* ((level (org-element-property :level inlinetask))
942 (todo (org-element-property :todo-keyword inlinetask))
943 (priority (org-element-property :priority inlinetask))
944 (title (org-element-interpret-data
945 (org-element-property :title inlinetask)))
946 (tags (let ((tag-list (org-element-property :tags inlinetask)))
947 (and tag-list
948 (format ":%s:" (mapconcat 'identity tag-list ":")))))
949 (task (concat (make-string level ?*)
950 (and todo (concat " " todo))
951 (and priority
952 (format " [#%s]" (char-to-string priority)))
953 (and title (concat " " title)))))
954 (concat task
955 ;; Align tags.
956 (when tags
957 (cond
958 ((zerop org-tags-column) (format " %s" tags))
959 ((< org-tags-column 0)
960 (concat
961 (make-string
962 (max (- (+ org-tags-column (length task) (length tags))) 1)
964 tags))
966 (concat
967 (make-string (max (- org-tags-column (length task)) 1) ? )
968 tags))))
969 ;; Prefer degenerate inlinetasks when there are no
970 ;; contents.
971 (when contents
972 (concat "\n"
973 contents
974 (make-string level ?*) " END")))))
977 ;;;; Item
979 (defun org-element-item-parser (limit struct &optional raw-secondary-p)
980 "Parse an item.
982 STRUCT is the structure of the plain list.
984 Return a list whose CAR is `item' and CDR is a plist containing
985 `:bullet', `:begin', `:end', `:contents-begin', `:contents-end',
986 `:checkbox', `:counter', `:tag', `:structure', `:hiddenp' and
987 `:post-blank' keywords.
989 When optional argument RAW-SECONDARY-P is non-nil, item's tag, if
990 any, will not be parsed as a secondary string, but as a plain
991 string instead.
993 Assume point is at the beginning of the item."
994 (save-excursion
995 (beginning-of-line)
996 (looking-at org-list-full-item-re)
997 (let* ((begin (point))
998 (bullet (org-match-string-no-properties 1))
999 (checkbox (let ((box (org-match-string-no-properties 3)))
1000 (cond ((equal "[ ]" box) 'off)
1001 ((equal "[X]" box) 'on)
1002 ((equal "[-]" box) 'trans))))
1003 (counter (let ((c (org-match-string-no-properties 2)))
1004 (save-match-data
1005 (cond
1006 ((not c) nil)
1007 ((string-match "[A-Za-z]" c)
1008 (- (string-to-char (upcase (match-string 0 c)))
1009 64))
1010 ((string-match "[0-9]+" c)
1011 (string-to-number (match-string 0 c)))))))
1012 (end (save-excursion (goto-char (org-list-get-item-end begin struct))
1013 (unless (bolp) (forward-line))
1014 (point)))
1015 (contents-begin
1016 (progn (goto-char
1017 ;; Ignore tags in un-ordered lists: they are just
1018 ;; a part of item's body.
1019 (if (and (match-beginning 4)
1020 (save-match-data (string-match "[.)]" bullet)))
1021 (match-beginning 4)
1022 (match-end 0)))
1023 (skip-chars-forward " \r\t\n" limit)
1024 ;; If first line isn't empty, contents really start
1025 ;; at the text after item's meta-data.
1026 (if (= (point-at-bol) begin) (point) (point-at-bol))))
1027 (hidden (progn (forward-line)
1028 (and (not (= (point) end)) (org-invisible-p2))))
1029 (contents-end (progn (goto-char end)
1030 (skip-chars-backward " \r\t\n")
1031 (forward-line)
1032 (point)))
1033 (item
1034 (list 'item
1035 (list :bullet bullet
1036 :begin begin
1037 :end end
1038 ;; CONTENTS-BEGIN and CONTENTS-END may be
1039 ;; mixed up in the case of an empty item
1040 ;; separated from the next by a blank line.
1041 ;; Thus ensure the former is always the
1042 ;; smallest.
1043 :contents-begin (min contents-begin contents-end)
1044 :contents-end (max contents-begin contents-end)
1045 :checkbox checkbox
1046 :counter counter
1047 :hiddenp hidden
1048 :structure struct
1049 :post-blank (count-lines contents-end end)))))
1050 (org-element-put-property
1051 item :tag
1052 (let ((raw-tag (org-list-get-tag begin struct)))
1053 (and raw-tag
1054 (if raw-secondary-p raw-tag
1055 (org-element-parse-secondary-string
1056 raw-tag (org-element-restriction 'item) item))))))))
1058 (defun org-element-item-interpreter (item contents)
1059 "Interpret ITEM element as Org syntax.
1060 CONTENTS is the contents of the element."
1061 (let* ((bullet (org-list-bullet-string (org-element-property :bullet item)))
1062 (checkbox (org-element-property :checkbox item))
1063 (counter (org-element-property :counter item))
1064 (tag (let ((tag (org-element-property :tag item)))
1065 (and tag (org-element-interpret-data tag))))
1066 ;; Compute indentation.
1067 (ind (make-string (length bullet) 32))
1068 (item-starts-with-par-p
1069 (eq (org-element-type (car (org-element-contents item)))
1070 'paragraph)))
1071 ;; Indent contents.
1072 (concat
1073 bullet
1074 (and counter (format "[@%d] " counter))
1075 (case checkbox
1076 (on "[X] ")
1077 (off "[ ] ")
1078 (trans "[-] "))
1079 (and tag (format "%s :: " tag))
1080 (let ((contents (replace-regexp-in-string
1081 "\\(^\\)[ \t]*\\S-" ind contents nil nil 1)))
1082 (if item-starts-with-par-p (org-trim contents)
1083 (concat "\n" contents))))))
1086 ;;;; Plain List
1088 (defun org-element-plain-list-parser (limit &optional structure)
1089 "Parse a plain list.
1091 Optional argument STRUCTURE, when non-nil, is the structure of
1092 the plain list being parsed.
1094 Return a list whose CAR is `plain-list' and CDR is a plist
1095 containing `:type', `:begin', `:end', `:contents-begin' and
1096 `:contents-end', `:structure' and `:post-blank' keywords.
1098 Assume point is at the beginning of the list."
1099 (save-excursion
1100 (let* ((struct (or structure (org-list-struct)))
1101 (prevs (org-list-prevs-alist struct))
1102 (parents (org-list-parents-alist struct))
1103 (type (org-list-get-list-type (point) struct prevs))
1104 (contents-begin (point))
1105 (keywords (org-element--collect-affiliated-keywords))
1106 (begin (car keywords))
1107 (contents-end
1108 (progn (goto-char (org-list-get-list-end (point) struct prevs))
1109 (unless (bolp) (forward-line))
1110 (point)))
1111 (end (progn (skip-chars-forward " \r\t\n" limit)
1112 (if (eobp) (point) (point-at-bol)))))
1113 ;; Return value.
1114 (list 'plain-list
1115 (nconc
1116 (list :type type
1117 :begin begin
1118 :end end
1119 :contents-begin contents-begin
1120 :contents-end contents-end
1121 :structure struct
1122 :post-blank (count-lines contents-end end))
1123 (cadr keywords))))))
1125 (defun org-element-plain-list-interpreter (plain-list contents)
1126 "Interpret PLAIN-LIST element as Org syntax.
1127 CONTENTS is the contents of the element."
1128 (with-temp-buffer
1129 (insert contents)
1130 (goto-char (point-min))
1131 (org-list-repair)
1132 (buffer-string)))
1135 ;;;; Quote Block
1137 (defun org-element-quote-block-parser (limit)
1138 "Parse a quote block.
1140 LIMIT bounds the search.
1142 Return a list whose CAR is `quote-block' and CDR is a plist
1143 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1144 `:contents-end' and `:post-blank' keywords.
1146 Assume point is at the beginning of the block."
1147 (let ((case-fold-search t))
1148 (if (not (save-excursion
1149 (re-search-forward "^[ \t]*#\\+END_QUOTE[ \t]*$" limit t)))
1150 ;; Incomplete block: parse it as a paragraph.
1151 (org-element-paragraph-parser limit)
1152 (let ((block-end-line (match-beginning 0)))
1153 (save-excursion
1154 (let* ((keywords (org-element--collect-affiliated-keywords))
1155 (begin (car keywords))
1156 ;; Empty blocks have no contents.
1157 (contents-begin (progn (forward-line)
1158 (and (< (point) block-end-line)
1159 (point))))
1160 (contents-end (and contents-begin block-end-line))
1161 (hidden (org-invisible-p2))
1162 (pos-before-blank (progn (goto-char block-end-line)
1163 (forward-line)
1164 (point)))
1165 (end (progn (skip-chars-forward " \r\t\n" limit)
1166 (if (eobp) (point) (point-at-bol)))))
1167 (list 'quote-block
1168 (nconc
1169 (list :begin begin
1170 :end end
1171 :hiddenp hidden
1172 :contents-begin contents-begin
1173 :contents-end contents-end
1174 :post-blank (count-lines pos-before-blank end))
1175 (cadr keywords)))))))))
1177 (defun org-element-quote-block-interpreter (quote-block contents)
1178 "Interpret QUOTE-BLOCK element as Org syntax.
1179 CONTENTS is the contents of the element."
1180 (format "#+BEGIN_QUOTE\n%s#+END_QUOTE" contents))
1183 ;;;; Section
1185 (defun org-element-section-parser (limit)
1186 "Parse a section.
1188 LIMIT bounds the search.
1190 Return a list whose CAR is `section' and CDR is a plist
1191 containing `:begin', `:end', `:contents-begin', `contents-end'
1192 and `:post-blank' keywords."
1193 (save-excursion
1194 ;; Beginning of section is the beginning of the first non-blank
1195 ;; line after previous headline.
1196 (let ((begin (point))
1197 (end (progn (org-with-limited-levels (outline-next-heading))
1198 (point)))
1199 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
1200 (forward-line)
1201 (point))))
1202 (list 'section
1203 (list :begin begin
1204 :end end
1205 :contents-begin begin
1206 :contents-end pos-before-blank
1207 :post-blank (count-lines pos-before-blank end))))))
1209 (defun org-element-section-interpreter (section contents)
1210 "Interpret SECTION element as Org syntax.
1211 CONTENTS is the contents of the element."
1212 contents)
1215 ;;;; Special Block
1217 (defun org-element-special-block-parser (limit)
1218 "Parse a special block.
1220 LIMIT bounds the search.
1222 Return a list whose CAR is `special-block' and CDR is a plist
1223 containing `:type', `:begin', `:end', `:hiddenp',
1224 `:contents-begin', `:contents-end' and `:post-blank' keywords.
1226 Assume point is at the beginning of the block."
1227 (let* ((case-fold-search t)
1228 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(S-+\\)")
1229 (upcase (match-string-no-properties 1)))))
1230 (if (not (save-excursion
1231 (re-search-forward
1232 (format "^[ \t]*#\\+END_%s[ \t]*$" type) limit t)))
1233 ;; Incomplete block: parse it as a paragraph.
1234 (org-element-paragraph-parser limit)
1235 (let ((block-end-line (match-beginning 0)))
1236 (save-excursion
1237 (let* ((keywords (org-element--collect-affiliated-keywords))
1238 (begin (car keywords))
1239 ;; Empty blocks have no contents.
1240 (contents-begin (progn (forward-line)
1241 (and (< (point) block-end-line)
1242 (point))))
1243 (contents-end (and contents-begin block-end-line))
1244 (hidden (org-invisible-p2))
1245 (pos-before-blank (progn (goto-char block-end-line)
1246 (forward-line)
1247 (point)))
1248 (end (progn (org-skip-whitespace)
1249 (if (eobp) (point) (point-at-bol)))))
1250 (list 'special-block
1251 (nconc
1252 (list :type type
1253 :begin begin
1254 :end end
1255 :hiddenp hidden
1256 :contents-begin contents-begin
1257 :contents-end contents-end
1258 :post-blank (count-lines pos-before-blank end))
1259 (cadr keywords)))))))))
1261 (defun org-element-special-block-interpreter (special-block contents)
1262 "Interpret SPECIAL-BLOCK element as Org syntax.
1263 CONTENTS is the contents of the element."
1264 (let ((block-type (org-element-property :type special-block)))
1265 (format "#+BEGIN_%s\n%s#+END_%s" block-type contents block-type)))
1269 ;;; Elements
1271 ;; For each element, a parser and an interpreter are also defined.
1272 ;; Both follow the same naming convention used for greater elements.
1274 ;; Also, as for greater elements, adding a new element type is done
1275 ;; through the following steps: implement a parser and an interpreter,
1276 ;; tweak `org-element--current-element' so that it recognizes the new
1277 ;; type and add that new type to `org-element-all-elements'.
1279 ;; As a special case, when the newly defined type is a block type,
1280 ;; `org-element-block-name-alist' has to be modified accordingly.
1283 ;;;; Babel Call
1285 (defun org-element-babel-call-parser (limit)
1286 "Parse a babel call.
1288 LIMIT bounds the search.
1290 Return a list whose CAR is `babel-call' and CDR is a plist
1291 containing `:begin', `:end', `:info' and `:post-blank' as
1292 keywords."
1293 (save-excursion
1294 (let ((case-fold-search t)
1295 (info (progn (looking-at org-babel-block-lob-one-liner-regexp)
1296 (org-babel-lob-get-info)))
1297 (begin (point-at-bol))
1298 (pos-before-blank (progn (forward-line) (point)))
1299 (end (progn (skip-chars-forward " \r\t\n" limit)
1300 (if (eobp) (point) (point-at-bol)))))
1301 (list 'babel-call
1302 (list :begin begin
1303 :end end
1304 :info info
1305 :post-blank (count-lines pos-before-blank end))))))
1307 (defun org-element-babel-call-interpreter (babel-call contents)
1308 "Interpret BABEL-CALL element as Org syntax.
1309 CONTENTS is nil."
1310 (let* ((babel-info (org-element-property :info babel-call))
1311 (main (car babel-info))
1312 (post-options (nth 1 babel-info)))
1313 (concat "#+CALL: "
1314 (if (not (string-match "\\[\\(\\[.*?\\]\\)\\]" main)) main
1315 ;; Remove redundant square brackets.
1316 (replace-match (match-string 1 main) nil nil main))
1317 (and post-options (format "[%s]" post-options)))))
1320 ;;;; Clock
1322 (defun org-element-clock-parser (limit)
1323 "Parse a clock.
1325 LIMIT bounds the search.
1327 Return a list whose CAR is `clock' and CDR is a plist containing
1328 `:status', `:value', `:time', `:begin', `:end' and `:post-blank'
1329 as keywords."
1330 (save-excursion
1331 (let* ((case-fold-search nil)
1332 (begin (point))
1333 (value (progn (search-forward org-clock-string (line-end-position) t)
1334 (org-skip-whitespace)
1335 (looking-at "\\[.*\\]")
1336 (org-match-string-no-properties 0)))
1337 (time (and (progn (goto-char (match-end 0))
1338 (looking-at " +=> +\\(\\S-+\\)[ \t]*$"))
1339 (org-match-string-no-properties 1)))
1340 (status (if time 'closed 'running))
1341 (post-blank (let ((before-blank (progn (forward-line) (point))))
1342 (skip-chars-forward " \r\t\n" limit)
1343 (unless (eobp) (beginning-of-line))
1344 (count-lines before-blank (point))))
1345 (end (point)))
1346 (list 'clock
1347 (list :status status
1348 :value value
1349 :time time
1350 :begin begin
1351 :end end
1352 :post-blank post-blank)))))
1354 (defun org-element-clock-interpreter (clock contents)
1355 "Interpret CLOCK element as Org syntax.
1356 CONTENTS is nil."
1357 (concat org-clock-string " "
1358 (org-element-property :value clock)
1359 (let ((time (org-element-property :time clock)))
1360 (and time
1361 (concat " => "
1362 (apply 'format
1363 "%2s:%02s"
1364 (org-split-string time ":")))))))
1367 ;;;; Comment
1369 (defun org-element-comment-parser (limit)
1370 "Parse a comment.
1372 LIMIT bounds the search.
1374 Return a list whose CAR is `comment' and CDR is a plist
1375 containing `:begin', `:end', `:value' and `:post-blank'
1376 keywords.
1378 Assume point is at comment beginning."
1379 (save-excursion
1380 (let* ((keywords (org-element--collect-affiliated-keywords))
1381 (begin (car keywords))
1382 (value (prog2 (looking-at "[ \t]*# ?")
1383 (buffer-substring-no-properties
1384 (match-end 0) (line-end-position))
1385 (forward-line)))
1386 (com-end
1387 ;; Get comments ending.
1388 (progn
1389 (while (and (< (point) limit) (looking-at "[ \t]*#\\( \\|$\\)"))
1390 ;; Accumulate lines without leading hash and first
1391 ;; whitespace.
1392 (setq value
1393 (concat value
1394 "\n"
1395 (buffer-substring-no-properties
1396 (match-end 0) (line-end-position))))
1397 (forward-line))
1398 (point)))
1399 (end (progn (goto-char com-end)
1400 (skip-chars-forward " \r\t\n" limit)
1401 (if (eobp) (point) (point-at-bol)))))
1402 (list 'comment
1403 (nconc
1404 (list :begin begin
1405 :end end
1406 :value value
1407 :post-blank (count-lines com-end end))
1408 (cadr keywords))))))
1410 (defun org-element-comment-interpreter (comment contents)
1411 "Interpret COMMENT element as Org syntax.
1412 CONTENTS is nil."
1413 (replace-regexp-in-string "^" "# " (org-element-property :value comment)))
1416 ;;;; Comment Block
1418 (defun org-element-comment-block-parser (limit)
1419 "Parse an export block.
1421 LIMIT bounds the search.
1423 Return a list whose CAR is `comment-block' and CDR is a plist
1424 containing `:begin', `:end', `:hiddenp', `:value' and
1425 `:post-blank' keywords.
1427 Assume point is at comment block beginning."
1428 (let ((case-fold-search t))
1429 (if (not (save-excursion
1430 (re-search-forward "^[ \t]*#\\+END_COMMENT[ \t]*$" limit t)))
1431 ;; Incomplete block: parse it as a paragraph.
1432 (org-element-paragraph-parser limit)
1433 (let ((contents-end (match-beginning 0)))
1434 (save-excursion
1435 (let* ((keywords (org-element--collect-affiliated-keywords))
1436 (begin (car keywords))
1437 (contents-begin (progn (forward-line) (point)))
1438 (hidden (org-invisible-p2))
1439 (pos-before-blank (progn (goto-char contents-end)
1440 (forward-line)
1441 (point)))
1442 (end (progn (skip-chars-forward " \r\t\n" limit)
1443 (if (eobp) (point) (point-at-bol))))
1444 (value (buffer-substring-no-properties
1445 contents-begin contents-end)))
1446 (list 'comment-block
1447 (nconc
1448 (list :begin begin
1449 :end end
1450 :value value
1451 :hiddenp hidden
1452 :post-blank (count-lines pos-before-blank end))
1453 (cadr keywords)))))))))
1455 (defun org-element-comment-block-interpreter (comment-block contents)
1456 "Interpret COMMENT-BLOCK element as Org syntax.
1457 CONTENTS is nil."
1458 (format "#+BEGIN_COMMENT\n%s#+END_COMMENT"
1459 (org-remove-indentation (org-element-property :value comment-block))))
1462 ;;;; Example Block
1464 (defun org-element-example-block-parser (limit)
1465 "Parse an example block.
1467 LIMIT bounds the search.
1469 Return a list whose CAR is `example-block' and CDR is a plist
1470 containing `:begin', `:end', `:number-lines', `:preserve-indent',
1471 `:retain-labels', `:use-labels', `:label-fmt', `:hiddenp',
1472 `:switches', `:value' and `:post-blank' keywords."
1473 (let ((case-fold-search t))
1474 (if (not (save-excursion
1475 (re-search-forward "^[ \t]*#\\+END_EXAMPLE[ \t]*$" limit t)))
1476 ;; Incomplete block: parse it as a paragraph.
1477 (org-element-paragraph-parser limit)
1478 (let ((contents-end (match-beginning 0)))
1479 (save-excursion
1480 (let* ((switches
1481 (progn (looking-at "^[ \t]*#\\+BEGIN_EXAMPLE\\(?: +\\(.*\\)\\)?")
1482 (org-match-string-no-properties 1)))
1483 ;; Switches analysis
1484 (number-lines (cond ((not switches) nil)
1485 ((string-match "-n\\>" switches) 'new)
1486 ((string-match "+n\\>" switches) 'continued)))
1487 (preserve-indent (and switches (string-match "-i\\>" switches)))
1488 ;; Should labels be retained in (or stripped from) example
1489 ;; blocks?
1490 (retain-labels
1491 (or (not switches)
1492 (not (string-match "-r\\>" switches))
1493 (and number-lines (string-match "-k\\>" switches))))
1494 ;; What should code-references use - labels or
1495 ;; line-numbers?
1496 (use-labels
1497 (or (not switches)
1498 (and retain-labels (not (string-match "-k\\>" switches)))))
1499 (label-fmt (and switches
1500 (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
1501 (match-string 1 switches)))
1502 ;; Standard block parsing.
1503 (keywords (org-element--collect-affiliated-keywords))
1504 (begin (car keywords))
1505 (contents-begin (progn (forward-line) (point)))
1506 (hidden (org-invisible-p2))
1507 (value (org-unescape-code-in-string
1508 (buffer-substring-no-properties
1509 contents-begin contents-end)))
1510 (pos-before-blank (progn (goto-char contents-end)
1511 (forward-line)
1512 (point)))
1513 (end (progn (skip-chars-forward " \r\t\n" limit)
1514 (if (eobp) (point) (point-at-bol)))))
1515 (list 'example-block
1516 (nconc
1517 (list :begin begin
1518 :end end
1519 :value value
1520 :switches switches
1521 :number-lines number-lines
1522 :preserve-indent preserve-indent
1523 :retain-labels retain-labels
1524 :use-labels use-labels
1525 :label-fmt label-fmt
1526 :hiddenp hidden
1527 :post-blank (count-lines pos-before-blank end))
1528 (cadr keywords)))))))))
1530 (defun org-element-example-block-interpreter (example-block contents)
1531 "Interpret EXAMPLE-BLOCK element as Org syntax.
1532 CONTENTS is nil."
1533 (let ((switches (org-element-property :switches example-block)))
1534 (concat "#+BEGIN_EXAMPLE" (and switches (concat " " switches)) "\n"
1535 (org-remove-indentation
1536 (org-escape-code-in-string
1537 (org-element-property :value example-block)))
1538 "#+END_EXAMPLE")))
1541 ;;;; Export Block
1543 (defun org-element-export-block-parser (limit)
1544 "Parse an export block.
1546 LIMIT bounds the search.
1548 Return a list whose CAR is `export-block' and CDR is a plist
1549 containing `:begin', `:end', `:type', `:hiddenp', `:value' and
1550 `:post-blank' keywords.
1552 Assume point is at export-block beginning."
1553 (let* ((case-fold-search t)
1554 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
1555 (upcase (org-match-string-no-properties 1)))))
1556 (if (not (save-excursion
1557 (re-search-forward
1558 (format "^[ \t]*#\\+END_%s[ \t]*$" type) limit t)))
1559 ;; Incomplete block: parse it as a paragraph.
1560 (org-element-paragraph-parser limit)
1561 (let ((contents-end (match-beginning 0)))
1562 (save-excursion
1563 (let* ((keywords (org-element--collect-affiliated-keywords))
1564 (begin (car keywords))
1565 (contents-begin (progn (forward-line) (point)))
1566 (hidden (org-invisible-p2))
1567 (pos-before-blank (progn (goto-char contents-end)
1568 (forward-line)
1569 (point)))
1570 (end (progn (skip-chars-forward " \r\t\n" limit)
1571 (if (eobp) (point) (point-at-bol))))
1572 (value (buffer-substring-no-properties contents-begin
1573 contents-end)))
1574 (list 'export-block
1575 (nconc
1576 (list :begin begin
1577 :end end
1578 :type type
1579 :value value
1580 :hiddenp hidden
1581 :post-blank (count-lines pos-before-blank end))
1582 (cadr keywords)))))))))
1584 (defun org-element-export-block-interpreter (export-block contents)
1585 "Interpret EXPORT-BLOCK element as Org syntax.
1586 CONTENTS is nil."
1587 (let ((type (org-element-property :type export-block)))
1588 (concat (format "#+BEGIN_%s\n" type)
1589 (org-element-property :value export-block)
1590 (format "#+END_%s" type))))
1593 ;;;; Fixed-width
1595 (defun org-element-fixed-width-parser (limit)
1596 "Parse a fixed-width section.
1598 LIMIT bounds the search.
1600 Return a list whose CAR is `fixed-width' and CDR is a plist
1601 containing `:begin', `:end', `:value' and `:post-blank' keywords.
1603 Assume point is at the beginning of the fixed-width area."
1604 (save-excursion
1605 (let* ((keywords (org-element--collect-affiliated-keywords))
1606 (begin (car keywords))
1607 value
1608 (end-area
1609 (progn
1610 (while (and (< (point) limit)
1611 (looking-at "[ \t]*:\\( \\|$\\)"))
1612 ;; Accumulate text without starting colons.
1613 (setq value
1614 (concat value
1615 (buffer-substring-no-properties
1616 (match-end 0) (point-at-eol))
1617 "\n"))
1618 (forward-line))
1619 (point)))
1620 (end (progn (skip-chars-forward " \r\t\n" limit)
1621 (if (eobp) (point) (point-at-bol)))))
1622 (list 'fixed-width
1623 (nconc
1624 (list :begin begin
1625 :end end
1626 :value value
1627 :post-blank (count-lines end-area end))
1628 (cadr keywords))))))
1630 (defun org-element-fixed-width-interpreter (fixed-width contents)
1631 "Interpret FIXED-WIDTH element as Org syntax.
1632 CONTENTS is nil."
1633 (replace-regexp-in-string
1634 "^" ": " (substring (org-element-property :value fixed-width) 0 -1)))
1637 ;;;; Horizontal Rule
1639 (defun org-element-horizontal-rule-parser (limit)
1640 "Parse an horizontal rule.
1642 LIMIT bounds the search.
1644 Return a list whose CAR is `horizontal-rule' and CDR is a plist
1645 containing `:begin', `:end' and `:post-blank' keywords."
1646 (save-excursion
1647 (let* ((keywords (org-element--collect-affiliated-keywords))
1648 (begin (car keywords))
1649 (post-hr (progn (forward-line) (point)))
1650 (end (progn (skip-chars-forward " \r\t\n" limit)
1651 (if (eobp) (point) (point-at-bol)))))
1652 (list 'horizontal-rule
1653 (nconc
1654 (list :begin begin
1655 :end end
1656 :post-blank (count-lines post-hr end))
1657 (cadr keywords))))))
1659 (defun org-element-horizontal-rule-interpreter (horizontal-rule contents)
1660 "Interpret HORIZONTAL-RULE element as Org syntax.
1661 CONTENTS is nil."
1662 "-----")
1665 ;;;; Keyword
1667 (defun org-element-keyword-parser (limit)
1668 "Parse a keyword at point.
1670 LIMIT bounds the search.
1672 Return a list whose CAR is `keyword' and CDR is a plist
1673 containing `:key', `:value', `:begin', `:end' and `:post-blank'
1674 keywords."
1675 (save-excursion
1676 (let* ((case-fold-search t)
1677 (begin (point))
1678 (key (progn (looking-at "[ \t]*#\\+\\(\\S-+\\):")
1679 (upcase (org-match-string-no-properties 1))))
1680 (value (org-trim (buffer-substring-no-properties
1681 (match-end 0) (point-at-eol))))
1682 (pos-before-blank (progn (forward-line) (point)))
1683 (end (progn (skip-chars-forward " \r\t\n" limit)
1684 (if (eobp) (point) (point-at-bol)))))
1685 (list 'keyword
1686 (list :key key
1687 :value value
1688 :begin begin
1689 :end end
1690 :post-blank (count-lines pos-before-blank end))))))
1692 (defun org-element-keyword-interpreter (keyword contents)
1693 "Interpret KEYWORD element as Org syntax.
1694 CONTENTS is nil."
1695 (format "#+%s: %s"
1696 (org-element-property :key keyword)
1697 (org-element-property :value keyword)))
1700 ;;;; Latex Environment
1702 (defun org-element-latex-environment-parser (limit)
1703 "Parse a LaTeX environment.
1705 LIMIT bounds the search.
1707 Return a list whose CAR is `latex-environment' and CDR is a plist
1708 containing `:begin', `:end', `:value' and `:post-blank'
1709 keywords.
1711 Assume point is at the beginning of the latex environment."
1712 (save-excursion
1713 (let* ((case-fold-search t)
1714 (code-begin (point))
1715 (keywords (org-element--collect-affiliated-keywords))
1716 (begin (car keywords))
1717 (env (progn (looking-at "^[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}")
1718 (regexp-quote (match-string 1))))
1719 (code-end
1720 (progn (re-search-forward
1721 (format "^[ \t]*\\\\end{%s}[ \t]*$" env) limit t)
1722 (forward-line)
1723 (point)))
1724 (value (buffer-substring-no-properties code-begin code-end))
1725 (end (progn (skip-chars-forward " \r\t\n" limit)
1726 (if (eobp) (point) (point-at-bol)))))
1727 (list 'latex-environment
1728 (nconc
1729 (list :begin begin
1730 :end end
1731 :value value
1732 :post-blank (count-lines code-end end))
1733 (cadr keywords))))))
1735 (defun org-element-latex-environment-interpreter (latex-environment contents)
1736 "Interpret LATEX-ENVIRONMENT element as Org syntax.
1737 CONTENTS is nil."
1738 (org-element-property :value latex-environment))
1741 ;;;; Paragraph
1743 (defun org-element-paragraph-parser (limit)
1744 "Parse a paragraph.
1746 LIMIT bounds the search.
1748 Return a list whose CAR is `paragraph' and CDR is a plist
1749 containing `:begin', `:end', `:contents-begin' and
1750 `:contents-end' and `:post-blank' keywords.
1752 Assume point is at the beginning of the paragraph."
1753 (save-excursion
1754 (let* ((contents-begin (point))
1755 ;; INNER-PAR-P is non-nil when paragraph is at the
1756 ;; beginning of an item or a footnote reference. In that
1757 ;; case, we mustn't look for affiliated keywords since they
1758 ;; belong to the container.
1759 (inner-par-p (not (bolp)))
1760 (keywords (unless inner-par-p
1761 (org-element--collect-affiliated-keywords)))
1762 (begin (if inner-par-p contents-begin (car keywords)))
1763 (before-blank
1764 (let ((case-fold-search t))
1765 (end-of-line)
1766 (if (not (re-search-forward
1767 org-element-paragraph-separate limit 'm))
1768 limit
1769 ;; A matching `org-element-paragraph-separate' is not
1770 ;; necessarily the end of the paragraph. In
1771 ;; particular, lines starting with # or : as a first
1772 ;; non-space character are ambiguous. We have check
1773 ;; if they are valid Org syntax (i.e. not an
1774 ;; incomplete keyword).
1775 (beginning-of-line)
1776 (while (not
1778 ;; There's no ambiguity for other symbols or
1779 ;; empty lines: stop here.
1780 (looking-at "[ \t]*\\(?:[^:#]\\|$\\)")
1781 ;; Stop at valid fixed-width areas.
1782 (looking-at "[ \t]*:\\(?: \\|$\\)")
1783 ;; Stop at drawers.
1784 (and (looking-at org-drawer-regexp)
1785 (save-excursion
1786 (re-search-forward
1787 "^[ \t]*:END:[ \t]*$" limit t)))
1788 ;; Stop at valid comments.
1789 (looking-at "[ \t]*#\\(?: \\|$\\)")
1790 ;; Stop at valid dynamic blocks.
1791 (and (looking-at org-dblock-start-re)
1792 (save-excursion
1793 (re-search-forward
1794 "^[ \t]*#\\+END:?[ \t]*$" limit t)))
1795 ;; Stop at valid blocks.
1796 (and (looking-at
1797 "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
1798 (save-excursion
1799 (re-search-forward
1800 (format "^[ \t]*#\\+END_%s[ \t]*$"
1801 (match-string 1))
1802 limit t)))
1803 ;; Stop at valid latex environments.
1804 (and (looking-at
1805 "^[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}[ \t]*$")
1806 (save-excursion
1807 (re-search-forward
1808 (format "^[ \t]*\\\\end{%s}[ \t]*$"
1809 (match-string 1))
1810 limit t)))
1811 ;; Stop at valid keywords.
1812 (looking-at "[ \t]*#\\+\\S-+:")
1813 ;; Skip everything else.
1814 (not
1815 (progn
1816 (end-of-line)
1817 (re-search-forward org-element-paragraph-separate
1818 limit 'm)))))
1819 (beginning-of-line)))
1820 (if (= (point) limit) limit
1821 (goto-char (line-beginning-position)))))
1822 (contents-end (progn (skip-chars-backward " \r\t\n" contents-begin)
1823 (forward-line)
1824 (point)))
1825 (end (progn (skip-chars-forward " \r\t\n" limit)
1826 (if (eobp) (point) (point-at-bol)))))
1827 (list 'paragraph
1828 (nconc
1829 (list :begin begin
1830 :end end
1831 :contents-begin contents-begin
1832 :contents-end contents-end
1833 :post-blank (count-lines before-blank end))
1834 (cadr keywords))))))
1836 (defun org-element-paragraph-interpreter (paragraph contents)
1837 "Interpret PARAGRAPH element as Org syntax.
1838 CONTENTS is the contents of the element."
1839 contents)
1842 ;;;; Planning
1844 (defun org-element-planning-parser (limit)
1845 "Parse a planning.
1847 LIMIT bounds the search.
1849 Return a list whose CAR is `planning' and CDR is a plist
1850 containing `:closed', `:deadline', `:scheduled', `:begin', `:end'
1851 and `:post-blank' keywords."
1852 (save-excursion
1853 (let* ((case-fold-search nil)
1854 (begin (point))
1855 (post-blank (let ((before-blank (progn (forward-line) (point))))
1856 (skip-chars-forward " \r\t\n" limit)
1857 (unless (eobp) (beginning-of-line))
1858 (count-lines before-blank (point))))
1859 (end (point))
1860 closed deadline scheduled)
1861 (goto-char begin)
1862 (while (re-search-forward org-keyword-time-not-clock-regexp
1863 (line-end-position) t)
1864 (goto-char (match-end 1))
1865 (org-skip-whitespace)
1866 (let ((time (buffer-substring-no-properties
1867 (1+ (point)) (1- (match-end 0))))
1868 (keyword (match-string 1)))
1869 (cond ((equal keyword org-closed-string) (setq closed time))
1870 ((equal keyword org-deadline-string) (setq deadline time))
1871 (t (setq scheduled time)))))
1872 (list 'planning
1873 (list :closed closed
1874 :deadline deadline
1875 :scheduled scheduled
1876 :begin begin
1877 :end end
1878 :post-blank post-blank)))))
1880 (defun org-element-planning-interpreter (planning contents)
1881 "Interpret PLANNING element as Org syntax.
1882 CONTENTS is nil."
1883 (mapconcat
1884 'identity
1885 (delq nil
1886 (list (let ((closed (org-element-property :closed planning)))
1887 (when closed (concat org-closed-string " [" closed "]")))
1888 (let ((deadline (org-element-property :deadline planning)))
1889 (when deadline (concat org-deadline-string " <" deadline ">")))
1890 (let ((scheduled (org-element-property :scheduled planning)))
1891 (when scheduled
1892 (concat org-scheduled-string " <" scheduled ">")))))
1893 " "))
1896 ;;;; Property Drawer
1898 (defun org-element-property-drawer-parser (limit)
1899 "Parse a property drawer.
1901 LIMIT bounds the search.
1903 Return a list whose CAR is `property-drawer' and CDR is a plist
1904 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1905 `:contents-end', `:properties' and `:post-blank' keywords.
1907 Assume point is at the beginning of the property drawer."
1908 (save-excursion
1909 (let ((case-fold-search t)
1910 (begin (point))
1911 (prop-begin (progn (forward-line) (point)))
1912 (hidden (org-invisible-p2))
1913 (properties
1914 (let (val)
1915 (while (not (looking-at "^[ \t]*:END:[ \t]*$"))
1916 (when (looking-at "[ \t]*:\\([A-Za-z][-_A-Za-z0-9]*\\):")
1917 (push (cons (org-match-string-no-properties 1)
1918 (org-trim
1919 (buffer-substring-no-properties
1920 (match-end 0) (point-at-eol))))
1921 val))
1922 (forward-line))
1923 val))
1924 (prop-end (progn (re-search-forward "^[ \t]*:END:" limit t)
1925 (point-at-bol)))
1926 (pos-before-blank (progn (forward-line) (point)))
1927 (end (progn (skip-chars-forward " \r\t\n" limit)
1928 (if (eobp) (point) (point-at-bol)))))
1929 (list 'property-drawer
1930 (list :begin begin
1931 :end end
1932 :hiddenp hidden
1933 :properties properties
1934 :post-blank (count-lines pos-before-blank end))))))
1936 (defun org-element-property-drawer-interpreter (property-drawer contents)
1937 "Interpret PROPERTY-DRAWER element as Org syntax.
1938 CONTENTS is nil."
1939 (let ((props (org-element-property :properties property-drawer)))
1940 (concat
1941 ":PROPERTIES:\n"
1942 (mapconcat (lambda (p)
1943 (format org-property-format (format ":%s:" (car p)) (cdr p)))
1944 (nreverse props) "\n")
1945 "\n:END:")))
1948 ;;;; Quote Section
1950 (defun org-element-quote-section-parser (limit)
1951 "Parse a quote section.
1953 LIMIT bounds the search.
1955 Return a list whose CAR is `quote-section' and CDR is a plist
1956 containing `:begin', `:end', `:value' and `:post-blank' keywords.
1958 Assume point is at beginning of the section."
1959 (save-excursion
1960 (let* ((begin (point))
1961 (end (progn (org-with-limited-levels (outline-next-heading))
1962 (point)))
1963 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
1964 (forward-line)
1965 (point)))
1966 (value (buffer-substring-no-properties begin pos-before-blank)))
1967 (list 'quote-section
1968 (list :begin begin
1969 :end end
1970 :value value
1971 :post-blank (count-lines pos-before-blank end))))))
1973 (defun org-element-quote-section-interpreter (quote-section contents)
1974 "Interpret QUOTE-SECTION element as Org syntax.
1975 CONTENTS is nil."
1976 (org-element-property :value quote-section))
1979 ;;;; Src Block
1981 (defun org-element-src-block-parser (limit)
1982 "Parse a src block.
1984 LIMIT bounds the search.
1986 Return a list whose CAR is `src-block' and CDR is a plist
1987 containing `:language', `:switches', `:parameters', `:begin',
1988 `:end', `:hiddenp', `:number-lines', `:retain-labels',
1989 `:use-labels', `:label-fmt', `:preserve-indent', `:value' and
1990 `:post-blank' keywords.
1992 Assume point is at the beginning of the block."
1993 (let ((case-fold-search t))
1994 (if (not (save-excursion (re-search-forward "^[ \t]*#\\+END_SRC[ \t]*$"
1995 limit t)))
1996 ;; Incomplete block: parse it as a paragraph.
1997 (org-element-paragraph-parser limit)
1998 (let ((contents-end (match-beginning 0)))
1999 (save-excursion
2000 (let* ((keywords (org-element--collect-affiliated-keywords))
2001 ;; Get beginning position.
2002 (begin (car keywords))
2003 ;; Get language as a string.
2004 (language
2005 (progn
2006 (looking-at
2007 (concat "^[ \t]*#\\+BEGIN_SRC"
2008 "\\(?: +\\(\\S-+\\)\\)?"
2009 "\\(\\(?: +\\(?:-l \".*?\"\\|[-+][A-Za-z]\\)\\)+\\)?"
2010 "\\(.*\\)[ \t]*$"))
2011 (org-match-string-no-properties 1)))
2012 ;; Get switches.
2013 (switches (org-match-string-no-properties 2))
2014 ;; Get parameters.
2015 (parameters (org-match-string-no-properties 3))
2016 ;; Switches analysis
2017 (number-lines (cond ((not switches) nil)
2018 ((string-match "-n\\>" switches) 'new)
2019 ((string-match "+n\\>" switches) 'continued)))
2020 (preserve-indent (and switches (string-match "-i\\>" switches)))
2021 (label-fmt (and switches
2022 (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
2023 (match-string 1 switches)))
2024 ;; Should labels be retained in (or stripped from)
2025 ;; src blocks?
2026 (retain-labels
2027 (or (not switches)
2028 (not (string-match "-r\\>" switches))
2029 (and number-lines (string-match "-k\\>" switches))))
2030 ;; What should code-references use - labels or
2031 ;; line-numbers?
2032 (use-labels
2033 (or (not switches)
2034 (and retain-labels (not (string-match "-k\\>" switches)))))
2035 ;; Get visibility status.
2036 (hidden (progn (forward-line) (org-invisible-p2)))
2037 ;; Retrieve code.
2038 (value (org-unescape-code-in-string
2039 (buffer-substring-no-properties (point) contents-end)))
2040 (pos-before-blank (progn (goto-char contents-end)
2041 (forward-line)
2042 (point)))
2043 ;; Get position after ending blank lines.
2044 (end (progn (skip-chars-forward " \r\t\n" limit)
2045 (if (eobp) (point) (point-at-bol)))))
2046 (list 'src-block
2047 (nconc
2048 (list :language language
2049 :switches (and (org-string-nw-p switches)
2050 (org-trim switches))
2051 :parameters (and (org-string-nw-p parameters)
2052 (org-trim parameters))
2053 :begin begin
2054 :end end
2055 :number-lines number-lines
2056 :preserve-indent preserve-indent
2057 :retain-labels retain-labels
2058 :use-labels use-labels
2059 :label-fmt label-fmt
2060 :hiddenp hidden
2061 :value value
2062 :post-blank (count-lines pos-before-blank end))
2063 (cadr keywords)))))))))
2065 (defun org-element-src-block-interpreter (src-block contents)
2066 "Interpret SRC-BLOCK element as Org syntax.
2067 CONTENTS is nil."
2068 (let ((lang (org-element-property :language src-block))
2069 (switches (org-element-property :switches src-block))
2070 (params (org-element-property :parameters src-block))
2071 (value (let ((val (org-element-property :value src-block)))
2072 (cond
2074 (org-src-preserve-indentation val)
2075 ((zerop org-edit-src-content-indentation)
2076 (org-remove-indentation val))
2078 (let ((ind (make-string
2079 org-edit-src-content-indentation 32)))
2080 (replace-regexp-in-string
2081 "\\(^\\)[ \t]*\\S-" ind
2082 (org-remove-indentation val) nil nil 1)))))))
2083 (concat (format "#+BEGIN_SRC%s\n"
2084 (concat (and lang (concat " " lang))
2085 (and switches (concat " " switches))
2086 (and params (concat " " params))))
2087 (org-escape-code-in-string value)
2088 "#+END_SRC")))
2091 ;;;; Table
2093 (defun org-element-table-parser (limit)
2094 "Parse a table at point.
2096 LIMIT bounds the search.
2098 Return a list whose CAR is `table' and CDR is a plist containing
2099 `:begin', `:end', `:tblfm', `:type', `:contents-begin',
2100 `:contents-end', `:value' and `:post-blank' keywords.
2102 Assume point is at the beginning of the table."
2103 (save-excursion
2104 (let* ((case-fold-search t)
2105 (table-begin (point))
2106 (type (if (org-at-table.el-p) 'table.el 'org))
2107 (keywords (org-element--collect-affiliated-keywords))
2108 (begin (car keywords))
2109 (table-end (goto-char (marker-position (org-table-end t))))
2110 (tblfm (let (acc)
2111 (while (looking-at "[ \t]*#\\+TBLFM: +\\(.*\\)[ \t]*$")
2112 (push (org-match-string-no-properties 1) acc)
2113 (forward-line))
2114 acc))
2115 (pos-before-blank (point))
2116 (end (progn (skip-chars-forward " \r\t\n" limit)
2117 (if (eobp) (point) (point-at-bol)))))
2118 (list 'table
2119 (nconc
2120 (list :begin begin
2121 :end end
2122 :type type
2123 :tblfm tblfm
2124 ;; Only `org' tables have contents. `table.el' tables
2125 ;; use a `:value' property to store raw table as
2126 ;; a string.
2127 :contents-begin (and (eq type 'org) table-begin)
2128 :contents-end (and (eq type 'org) table-end)
2129 :value (and (eq type 'table.el)
2130 (buffer-substring-no-properties
2131 table-begin table-end))
2132 :post-blank (count-lines pos-before-blank end))
2133 (cadr keywords))))))
2135 (defun org-element-table-interpreter (table contents)
2136 "Interpret TABLE element as Org syntax.
2137 CONTENTS is nil."
2138 (if (eq (org-element-property :type table) 'table.el)
2139 (org-remove-indentation (org-element-property :value table))
2140 (concat (with-temp-buffer (insert contents)
2141 (org-table-align)
2142 (buffer-string))
2143 (mapconcat (lambda (fm) (concat "#+TBLFM: " fm))
2144 (reverse (org-element-property :tblfm table))
2145 "\n"))))
2148 ;;;; Table Row
2150 (defun org-element-table-row-parser (limit)
2151 "Parse table row at point.
2153 LIMIT bounds the search.
2155 Return a list whose CAR is `table-row' and CDR is a plist
2156 containing `:begin', `:end', `:contents-begin', `:contents-end',
2157 `:type' and `:post-blank' keywords."
2158 (save-excursion
2159 (let* ((type (if (looking-at "^[ \t]*|-") 'rule 'standard))
2160 (begin (point))
2161 ;; A table rule has no contents. In that case, ensure
2162 ;; CONTENTS-BEGIN matches CONTENTS-END.
2163 (contents-begin (and (eq type 'standard)
2164 (search-forward "|")
2165 (point)))
2166 (contents-end (and (eq type 'standard)
2167 (progn
2168 (end-of-line)
2169 (skip-chars-backward " \t")
2170 (point))))
2171 (end (progn (forward-line) (point))))
2172 (list 'table-row
2173 (list :type type
2174 :begin begin
2175 :end end
2176 :contents-begin contents-begin
2177 :contents-end contents-end
2178 :post-blank 0)))))
2180 (defun org-element-table-row-interpreter (table-row contents)
2181 "Interpret TABLE-ROW element as Org syntax.
2182 CONTENTS is the contents of the table row."
2183 (if (eq (org-element-property :type table-row) 'rule) "|-"
2184 (concat "| " contents)))
2187 ;;;; Verse Block
2189 (defun org-element-verse-block-parser (limit)
2190 "Parse a verse block.
2192 LIMIT bounds the search.
2194 Return a list whose CAR is `verse-block' and CDR is a plist
2195 containing `:begin', `:end', `:contents-begin', `:contents-end',
2196 `:hiddenp' and `:post-blank' keywords.
2198 Assume point is at beginning of the block."
2199 (let ((case-fold-search t))
2200 (if (not (save-excursion
2201 (re-search-forward "^[ \t]*#\\+END_VERSE[ \t]*$" limit t)))
2202 ;; Incomplete block: parse it as a paragraph.
2203 (org-element-paragraph-parser limit)
2204 (let ((contents-end (match-beginning 0)))
2205 (save-excursion
2206 (let* ((keywords (org-element--collect-affiliated-keywords))
2207 (begin (car keywords))
2208 (hidden (progn (forward-line) (org-invisible-p2)))
2209 (contents-begin (point))
2210 (pos-before-blank (progn (goto-char contents-end)
2211 (forward-line)
2212 (point)))
2213 (end (progn (skip-chars-forward " \r\t\n" limit)
2214 (if (eobp) (point) (point-at-bol)))))
2215 (list 'verse-block
2216 (nconc
2217 (list :begin begin
2218 :end end
2219 :contents-begin contents-begin
2220 :contents-end contents-end
2221 :hiddenp hidden
2222 :post-blank (count-lines pos-before-blank end))
2223 (cadr keywords)))))))))
2225 (defun org-element-verse-block-interpreter (verse-block contents)
2226 "Interpret VERSE-BLOCK element as Org syntax.
2227 CONTENTS is verse block contents."
2228 (format "#+BEGIN_VERSE\n%s#+END_VERSE" contents))
2232 ;;; Objects
2234 ;; Unlike to elements, interstices can be found between objects.
2235 ;; That's why, along with the parser, successor functions are provided
2236 ;; for each object. Some objects share the same successor (i.e. `code'
2237 ;; and `verbatim' objects).
2239 ;; A successor must accept a single argument bounding the search. It
2240 ;; will return either a cons cell whose CAR is the object's type, as
2241 ;; a symbol, and CDR the position of its next occurrence, or nil.
2243 ;; Successors follow the naming convention:
2244 ;; org-element-NAME-successor, where NAME is the name of the
2245 ;; successor, as defined in `org-element-all-successors'.
2247 ;; Some object types (i.e. `italic') are recursive. Restrictions on
2248 ;; object types they can contain will be specified in
2249 ;; `org-element-object-restrictions'.
2251 ;; Adding a new type of object is simple. Implement a successor,
2252 ;; a parser, and an interpreter for it, all following the naming
2253 ;; convention. Register type in `org-element-all-objects' and
2254 ;; successor in `org-element-all-successors'. Maybe tweak
2255 ;; restrictions about it, and that's it.
2258 ;;;; Bold
2260 (defun org-element-bold-parser ()
2261 "Parse bold object at point.
2263 Return a list whose CAR is `bold' and CDR is a plist with
2264 `:begin', `:end', `:contents-begin' and `:contents-end' and
2265 `:post-blank' keywords.
2267 Assume point is at the first star marker."
2268 (save-excursion
2269 (unless (bolp) (backward-char 1))
2270 (looking-at org-emph-re)
2271 (let ((begin (match-beginning 2))
2272 (contents-begin (match-beginning 4))
2273 (contents-end (match-end 4))
2274 (post-blank (progn (goto-char (match-end 2))
2275 (skip-chars-forward " \t")))
2276 (end (point)))
2277 (list 'bold
2278 (list :begin begin
2279 :end end
2280 :contents-begin contents-begin
2281 :contents-end contents-end
2282 :post-blank post-blank)))))
2284 (defun org-element-bold-interpreter (bold contents)
2285 "Interpret BOLD object as Org syntax.
2286 CONTENTS is the contents of the object."
2287 (format "*%s*" contents))
2289 (defun org-element-text-markup-successor (limit)
2290 "Search for the next text-markup object.
2292 LIMIT bounds the search.
2294 Return value is a cons cell whose CAR is a symbol among `bold',
2295 `italic', `underline', `strike-through', `code' and `verbatim'
2296 and CDR is beginning position."
2297 (save-excursion
2298 (unless (bolp) (backward-char))
2299 (when (re-search-forward org-emph-re limit t)
2300 (let ((marker (match-string 3)))
2301 (cons (cond
2302 ((equal marker "*") 'bold)
2303 ((equal marker "/") 'italic)
2304 ((equal marker "_") 'underline)
2305 ((equal marker "+") 'strike-through)
2306 ((equal marker "~") 'code)
2307 ((equal marker "=") 'verbatim)
2308 (t (error "Unknown marker at %d" (match-beginning 3))))
2309 (match-beginning 2))))))
2312 ;;;; Code
2314 (defun org-element-code-parser ()
2315 "Parse code object at point.
2317 Return a list whose CAR is `code' and CDR is a plist with
2318 `:value', `:begin', `:end' and `:post-blank' keywords.
2320 Assume point is at the first tilde marker."
2321 (save-excursion
2322 (unless (bolp) (backward-char 1))
2323 (looking-at org-emph-re)
2324 (let ((begin (match-beginning 2))
2325 (value (org-match-string-no-properties 4))
2326 (post-blank (progn (goto-char (match-end 2))
2327 (skip-chars-forward " \t")))
2328 (end (point)))
2329 (list 'code
2330 (list :value value
2331 :begin begin
2332 :end end
2333 :post-blank post-blank)))))
2335 (defun org-element-code-interpreter (code contents)
2336 "Interpret CODE object as Org syntax.
2337 CONTENTS is nil."
2338 (format "~%s~" (org-element-property :value code)))
2341 ;;;; Entity
2343 (defun org-element-entity-parser ()
2344 "Parse entity at point.
2346 Return a list whose CAR is `entity' and CDR a plist with
2347 `:begin', `:end', `:latex', `:latex-math-p', `:html', `:latin1',
2348 `:utf-8', `:ascii', `:use-brackets-p' and `:post-blank' as
2349 keywords.
2351 Assume point is at the beginning of the entity."
2352 (save-excursion
2353 (looking-at "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)")
2354 (let* ((value (org-entity-get (match-string 1)))
2355 (begin (match-beginning 0))
2356 (bracketsp (string= (match-string 2) "{}"))
2357 (post-blank (progn (goto-char (match-end 1))
2358 (when bracketsp (forward-char 2))
2359 (skip-chars-forward " \t")))
2360 (end (point)))
2361 (list 'entity
2362 (list :name (car value)
2363 :latex (nth 1 value)
2364 :latex-math-p (nth 2 value)
2365 :html (nth 3 value)
2366 :ascii (nth 4 value)
2367 :latin1 (nth 5 value)
2368 :utf-8 (nth 6 value)
2369 :begin begin
2370 :end end
2371 :use-brackets-p bracketsp
2372 :post-blank post-blank)))))
2374 (defun org-element-entity-interpreter (entity contents)
2375 "Interpret ENTITY object as Org syntax.
2376 CONTENTS is nil."
2377 (concat "\\"
2378 (org-element-property :name entity)
2379 (when (org-element-property :use-brackets-p entity) "{}")))
2381 (defun org-element-latex-or-entity-successor (limit)
2382 "Search for the next latex-fragment or entity object.
2384 LIMIT bounds the search.
2386 Return value is a cons cell whose CAR is `entity' or
2387 `latex-fragment' and CDR is beginning position."
2388 (save-excursion
2389 (let ((matchers
2390 (remove "begin" (plist-get org-format-latex-options :matchers)))
2391 ;; ENTITY-RE matches both LaTeX commands and Org entities.
2392 (entity-re
2393 "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))
2394 (when (re-search-forward
2395 (concat (mapconcat (lambda (e) (nth 1 (assoc e org-latex-regexps)))
2396 matchers "\\|")
2397 "\\|" entity-re)
2398 limit t)
2399 (goto-char (match-beginning 0))
2400 (if (looking-at entity-re)
2401 ;; Determine if it's a real entity or a LaTeX command.
2402 (cons (if (org-entity-get (match-string 1)) 'entity 'latex-fragment)
2403 (match-beginning 0))
2404 ;; No entity nor command: point is at a LaTeX fragment.
2405 ;; Determine its type to get the correct beginning position.
2406 (cons 'latex-fragment
2407 (catch 'return
2408 (mapc (lambda (e)
2409 (when (looking-at (nth 1 (assoc e org-latex-regexps)))
2410 (throw 'return
2411 (match-beginning
2412 (nth 2 (assoc e org-latex-regexps))))))
2413 matchers)
2414 (point))))))))
2417 ;;;; Export Snippet
2419 (defun org-element-export-snippet-parser ()
2420 "Parse export snippet at point.
2422 Return a list whose CAR is `export-snippet' and CDR a plist with
2423 `:begin', `:end', `:back-end', `:value' and `:post-blank' as
2424 keywords.
2426 Assume point is at the beginning of the snippet."
2427 (save-excursion
2428 (re-search-forward "@@\\([-A-Za-z0-9]+\\):" nil t)
2429 (let* ((begin (match-beginning 0))
2430 (back-end (org-match-string-no-properties 1))
2431 (value (buffer-substring-no-properties
2432 (point)
2433 (progn (re-search-forward "@@" nil t) (match-beginning 0))))
2434 (post-blank (skip-chars-forward " \t"))
2435 (end (point)))
2436 (list 'export-snippet
2437 (list :back-end back-end
2438 :value value
2439 :begin begin
2440 :end end
2441 :post-blank post-blank)))))
2443 (defun org-element-export-snippet-interpreter (export-snippet contents)
2444 "Interpret EXPORT-SNIPPET object as Org syntax.
2445 CONTENTS is nil."
2446 (format "@@%s:%s@@"
2447 (org-element-property :back-end export-snippet)
2448 (org-element-property :value export-snippet)))
2450 (defun org-element-export-snippet-successor (limit)
2451 "Search for the next export-snippet object.
2453 LIMIT bounds the search.
2455 Return value is a cons cell whose CAR is `export-snippet' and CDR
2456 its beginning position."
2457 (save-excursion
2458 (let (beg)
2459 (when (and (re-search-forward "@@[-A-Za-z0-9]+:" limit t)
2460 (setq beg (match-beginning 0))
2461 (search-forward "@@" limit t))
2462 (cons 'export-snippet beg)))))
2465 ;;;; Footnote Reference
2467 (defun org-element-footnote-reference-parser ()
2468 "Parse footnote reference at point.
2470 Return a list whose CAR is `footnote-reference' and CDR a plist
2471 with `:label', `:type', `:inline-definition', `:begin', `:end'
2472 and `:post-blank' as keywords."
2473 (save-excursion
2474 (looking-at org-footnote-re)
2475 (let* ((begin (point))
2476 (label (or (org-match-string-no-properties 2)
2477 (org-match-string-no-properties 3)
2478 (and (match-string 1)
2479 (concat "fn:" (org-match-string-no-properties 1)))))
2480 (type (if (or (not label) (match-string 1)) 'inline 'standard))
2481 (inner-begin (match-end 0))
2482 (inner-end
2483 (let ((count 1))
2484 (forward-char)
2485 (while (and (> count 0) (re-search-forward "[][]" nil t))
2486 (if (equal (match-string 0) "[") (incf count) (decf count)))
2487 (1- (point))))
2488 (post-blank (progn (goto-char (1+ inner-end))
2489 (skip-chars-forward " \t")))
2490 (end (point))
2491 (footnote-reference
2492 (list 'footnote-reference
2493 (list :label label
2494 :type type
2495 :begin begin
2496 :end end
2497 :post-blank post-blank))))
2498 (org-element-put-property
2499 footnote-reference :inline-definition
2500 (and (eq type 'inline)
2501 (org-element-parse-secondary-string
2502 (buffer-substring inner-begin inner-end)
2503 (org-element-restriction 'footnote-reference)
2504 footnote-reference))))))
2506 (defun org-element-footnote-reference-interpreter (footnote-reference contents)
2507 "Interpret FOOTNOTE-REFERENCE object as Org syntax.
2508 CONTENTS is nil."
2509 (let ((label (or (org-element-property :label footnote-reference) "fn:"))
2510 (def
2511 (let ((inline-def
2512 (org-element-property :inline-definition footnote-reference)))
2513 (if (not inline-def) ""
2514 (concat ":" (org-element-interpret-data inline-def))))))
2515 (format "[%s]" (concat label def))))
2517 (defun org-element-footnote-reference-successor (limit)
2518 "Search for the next footnote-reference object.
2520 LIMIT bounds the search.
2522 Return value is a cons cell whose CAR is `footnote-reference' and
2523 CDR is beginning position."
2524 (save-excursion
2525 (catch 'exit
2526 (while (re-search-forward org-footnote-re limit t)
2527 (save-excursion
2528 (let ((beg (match-beginning 0))
2529 (count 1))
2530 (backward-char)
2531 (while (re-search-forward "[][]" limit t)
2532 (if (equal (match-string 0) "[") (incf count) (decf count))
2533 (when (zerop count)
2534 (throw 'exit (cons 'footnote-reference beg))))))))))
2537 ;;;; Inline Babel Call
2539 (defun org-element-inline-babel-call-parser ()
2540 "Parse inline babel call at point.
2542 Return a list whose CAR is `inline-babel-call' and CDR a plist
2543 with `:begin', `:end', `:info' and `:post-blank' as keywords.
2545 Assume point is at the beginning of the babel call."
2546 (save-excursion
2547 (unless (bolp) (backward-char))
2548 (looking-at org-babel-inline-lob-one-liner-regexp)
2549 (let ((info (save-match-data (org-babel-lob-get-info)))
2550 (begin (match-end 1))
2551 (post-blank (progn (goto-char (match-end 0))
2552 (skip-chars-forward " \t")))
2553 (end (point)))
2554 (list 'inline-babel-call
2555 (list :begin begin
2556 :end end
2557 :info info
2558 :post-blank post-blank)))))
2560 (defun org-element-inline-babel-call-interpreter (inline-babel-call contents)
2561 "Interpret INLINE-BABEL-CALL object as Org syntax.
2562 CONTENTS is nil."
2563 (let* ((babel-info (org-element-property :info inline-babel-call))
2564 (main-source (car babel-info))
2565 (post-options (nth 1 babel-info)))
2566 (concat "call_"
2567 (if (string-match "\\[\\(\\[.*?\\]\\)\\]" main-source)
2568 ;; Remove redundant square brackets.
2569 (replace-match
2570 (match-string 1 main-source) nil nil main-source)
2571 main-source)
2572 (and post-options (format "[%s]" post-options)))))
2574 (defun org-element-inline-babel-call-successor (limit)
2575 "Search for the next inline-babel-call object.
2577 LIMIT bounds the search.
2579 Return value is a cons cell whose CAR is `inline-babel-call' and
2580 CDR is beginning position."
2581 (save-excursion
2582 ;; Use a simplified version of
2583 ;; `org-babel-inline-lob-one-liner-regexp'.
2584 (when (re-search-forward
2585 "call_\\([^()\n]+?\\)\\(?:\\[.*?\\]\\)?([^\n]*?)\\(\\[.*?\\]\\)?"
2586 limit t)
2587 (cons 'inline-babel-call (match-beginning 0)))))
2590 ;;;; Inline Src Block
2592 (defun org-element-inline-src-block-parser ()
2593 "Parse inline source block at point.
2595 LIMIT bounds the search.
2597 Return a list whose CAR is `inline-src-block' and CDR a plist
2598 with `:begin', `:end', `:language', `:value', `:parameters' and
2599 `:post-blank' as keywords.
2601 Assume point is at the beginning of the inline src block."
2602 (save-excursion
2603 (unless (bolp) (backward-char))
2604 (looking-at org-babel-inline-src-block-regexp)
2605 (let ((begin (match-beginning 1))
2606 (language (org-match-string-no-properties 2))
2607 (parameters (org-match-string-no-properties 4))
2608 (value (org-match-string-no-properties 5))
2609 (post-blank (progn (goto-char (match-end 0))
2610 (skip-chars-forward " \t")))
2611 (end (point)))
2612 (list 'inline-src-block
2613 (list :language language
2614 :value value
2615 :parameters parameters
2616 :begin begin
2617 :end end
2618 :post-blank post-blank)))))
2620 (defun org-element-inline-src-block-interpreter (inline-src-block contents)
2621 "Interpret INLINE-SRC-BLOCK object as Org syntax.
2622 CONTENTS is nil."
2623 (let ((language (org-element-property :language inline-src-block))
2624 (arguments (org-element-property :parameters inline-src-block))
2625 (body (org-element-property :value inline-src-block)))
2626 (format "src_%s%s{%s}"
2627 language
2628 (if arguments (format "[%s]" arguments) "")
2629 body)))
2631 (defun org-element-inline-src-block-successor (limit)
2632 "Search for the next inline-babel-call element.
2634 LIMIT bounds the search.
2636 Return value is a cons cell whose CAR is `inline-babel-call' and
2637 CDR is beginning position."
2638 (save-excursion
2639 (unless (bolp) (backward-char))
2640 (when (re-search-forward org-babel-inline-src-block-regexp limit t)
2641 (cons 'inline-src-block (match-beginning 1)))))
2643 ;;;; Italic
2645 (defun org-element-italic-parser ()
2646 "Parse italic object at point.
2648 Return a list whose CAR is `italic' and CDR is a plist with
2649 `:begin', `:end', `:contents-begin' and `:contents-end' and
2650 `:post-blank' keywords.
2652 Assume point is at the first slash marker."
2653 (save-excursion
2654 (unless (bolp) (backward-char 1))
2655 (looking-at org-emph-re)
2656 (let ((begin (match-beginning 2))
2657 (contents-begin (match-beginning 4))
2658 (contents-end (match-end 4))
2659 (post-blank (progn (goto-char (match-end 2))
2660 (skip-chars-forward " \t")))
2661 (end (point)))
2662 (list 'italic
2663 (list :begin begin
2664 :end end
2665 :contents-begin contents-begin
2666 :contents-end contents-end
2667 :post-blank post-blank)))))
2669 (defun org-element-italic-interpreter (italic contents)
2670 "Interpret ITALIC object as Org syntax.
2671 CONTENTS is the contents of the object."
2672 (format "/%s/" contents))
2675 ;;;; Latex Fragment
2677 (defun org-element-latex-fragment-parser ()
2678 "Parse latex fragment at point.
2680 Return a list whose CAR is `latex-fragment' and CDR a plist with
2681 `:value', `:begin', `:end', and `:post-blank' as keywords.
2683 Assume point is at the beginning of the latex fragment."
2684 (save-excursion
2685 (let* ((begin (point))
2686 (substring-match
2687 (catch 'exit
2688 (mapc (lambda (e)
2689 (let ((latex-regexp (nth 1 (assoc e org-latex-regexps))))
2690 (when (or (looking-at latex-regexp)
2691 (and (not (bobp))
2692 (save-excursion
2693 (backward-char)
2694 (looking-at latex-regexp))))
2695 (throw 'exit (nth 2 (assoc e org-latex-regexps))))))
2696 (plist-get org-format-latex-options :matchers))
2697 ;; None found: it's a macro.
2698 (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")
2700 (value (match-string-no-properties substring-match))
2701 (post-blank (progn (goto-char (match-end substring-match))
2702 (skip-chars-forward " \t")))
2703 (end (point)))
2704 (list 'latex-fragment
2705 (list :value value
2706 :begin begin
2707 :end end
2708 :post-blank post-blank)))))
2710 (defun org-element-latex-fragment-interpreter (latex-fragment contents)
2711 "Interpret LATEX-FRAGMENT object as Org syntax.
2712 CONTENTS is nil."
2713 (org-element-property :value latex-fragment))
2715 ;;;; Line Break
2717 (defun org-element-line-break-parser ()
2718 "Parse line break at point.
2720 Return a list whose CAR is `line-break', and CDR a plist with
2721 `:begin', `:end' and `:post-blank' keywords.
2723 Assume point is at the beginning of the line break."
2724 (list 'line-break (list :begin (point) :end (point-at-eol) :post-blank 0)))
2726 (defun org-element-line-break-interpreter (line-break contents)
2727 "Interpret LINE-BREAK object as Org syntax.
2728 CONTENTS is nil."
2729 "\\\\")
2731 (defun org-element-line-break-successor (limit)
2732 "Search for the next line-break object.
2734 LIMIT bounds the search.
2736 Return value is a cons cell whose CAR is `line-break' and CDR is
2737 beginning position."
2738 (save-excursion
2739 (let ((beg (and (re-search-forward "[^\\\\]\\(\\\\\\\\\\)[ \t]*$" limit t)
2740 (goto-char (match-beginning 1)))))
2741 ;; A line break can only happen on a non-empty line.
2742 (when (and beg (re-search-backward "\\S-" (point-at-bol) t))
2743 (cons 'line-break beg)))))
2746 ;;;; Link
2748 (defun org-element-link-parser ()
2749 "Parse link at point.
2751 Return a list whose CAR is `link' and CDR a plist with `:type',
2752 `:path', `:raw-link', `:begin', `:end', `:contents-begin',
2753 `:contents-end' and `:post-blank' as keywords.
2755 Assume point is at the beginning of the link."
2756 (save-excursion
2757 (let ((begin (point))
2758 end contents-begin contents-end link-end post-blank path type
2759 raw-link link)
2760 (cond
2761 ;; Type 1: Text targeted from a radio target.
2762 ((and org-target-link-regexp (looking-at org-target-link-regexp))
2763 (setq type "radio"
2764 link-end (match-end 0)
2765 path (org-match-string-no-properties 0)))
2766 ;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]]
2767 ((looking-at org-bracket-link-regexp)
2768 (setq contents-begin (match-beginning 3)
2769 contents-end (match-end 3)
2770 link-end (match-end 0)
2771 ;; RAW-LINK is the original link.
2772 raw-link (org-match-string-no-properties 1)
2773 link (org-translate-link
2774 (org-link-expand-abbrev
2775 (org-link-unescape raw-link))))
2776 ;; Determine TYPE of link and set PATH accordingly.
2777 (cond
2778 ;; File type.
2779 ((or (file-name-absolute-p link) (string-match "^\\.\\.?/" link))
2780 (setq type "file" path link))
2781 ;; Explicit type (http, irc, bbdb...). See `org-link-types'.
2782 ((string-match org-link-re-with-space3 link)
2783 (setq type (match-string 1 link) path (match-string 2 link)))
2784 ;; Id type: PATH is the id.
2785 ((string-match "^id:\\([-a-f0-9]+\\)" link)
2786 (setq type "id" path (match-string 1 link)))
2787 ;; Code-ref type: PATH is the name of the reference.
2788 ((string-match "^(\\(.*\\))$" link)
2789 (setq type "coderef" path (match-string 1 link)))
2790 ;; Custom-id type: PATH is the name of the custom id.
2791 ((= (aref link 0) ?#)
2792 (setq type "custom-id" path (substring link 1)))
2793 ;; Fuzzy type: Internal link either matches a target, an
2794 ;; headline name or nothing. PATH is the target or
2795 ;; headline's name.
2796 (t (setq type "fuzzy" path link))))
2797 ;; Type 3: Plain link, i.e. http://orgmode.org
2798 ((looking-at org-plain-link-re)
2799 (setq raw-link (org-match-string-no-properties 0)
2800 type (org-match-string-no-properties 1)
2801 path (org-match-string-no-properties 2)
2802 link-end (match-end 0)))
2803 ;; Type 4: Angular link, i.e. <http://orgmode.org>
2804 ((looking-at org-angle-link-re)
2805 (setq raw-link (buffer-substring-no-properties
2806 (match-beginning 1) (match-end 2))
2807 type (org-match-string-no-properties 1)
2808 path (org-match-string-no-properties 2)
2809 link-end (match-end 0))))
2810 ;; In any case, deduce end point after trailing white space from
2811 ;; LINK-END variable.
2812 (setq post-blank (progn (goto-char link-end) (skip-chars-forward " \t"))
2813 end (point))
2814 (list 'link
2815 (list :type type
2816 :path path
2817 :raw-link (or raw-link path)
2818 :begin begin
2819 :end end
2820 :contents-begin contents-begin
2821 :contents-end contents-end
2822 :post-blank post-blank)))))
2824 (defun org-element-link-interpreter (link contents)
2825 "Interpret LINK object as Org syntax.
2826 CONTENTS is the contents of the object, or nil."
2827 (let ((type (org-element-property :type link))
2828 (raw-link (org-element-property :raw-link link)))
2829 (if (string= type "radio") raw-link
2830 (format "[[%s]%s]"
2831 raw-link
2832 (if contents (format "[%s]" contents) "")))))
2834 (defun org-element-link-successor (limit)
2835 "Search for the next link object.
2837 LIMIT bounds the search.
2839 Return value is a cons cell whose CAR is `link' and CDR is
2840 beginning position."
2841 (save-excursion
2842 (let ((link-regexp
2843 (if (not org-target-link-regexp) org-any-link-re
2844 (concat org-any-link-re "\\|" org-target-link-regexp))))
2845 (when (re-search-forward link-regexp limit t)
2846 (cons 'link (match-beginning 0))))))
2849 ;;;; Macro
2851 (defun org-element-macro-parser ()
2852 "Parse macro at point.
2854 Return a list whose CAR is `macro' and CDR a plist with `:key',
2855 `:args', `:begin', `:end', `:value' and `:post-blank' as
2856 keywords.
2858 Assume point is at the macro."
2859 (save-excursion
2860 (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
2861 (let ((begin (point))
2862 (key (downcase (org-match-string-no-properties 1)))
2863 (value (org-match-string-no-properties 0))
2864 (post-blank (progn (goto-char (match-end 0))
2865 (skip-chars-forward " \t")))
2866 (end (point))
2867 (args (let ((args (org-match-string-no-properties 3)) args2)
2868 (when args
2869 (setq args (org-split-string args ","))
2870 (while args
2871 (while (string-match "\\\\\\'" (car args))
2872 ;; Repair bad splits.
2873 (setcar (cdr args) (concat (substring (car args) 0 -1)
2874 "," (nth 1 args)))
2875 (pop args))
2876 (push (pop args) args2))
2877 (mapcar 'org-trim (nreverse args2))))))
2878 (list 'macro
2879 (list :key key
2880 :value value
2881 :args args
2882 :begin begin
2883 :end end
2884 :post-blank post-blank)))))
2886 (defun org-element-macro-interpreter (macro contents)
2887 "Interpret MACRO object as Org syntax.
2888 CONTENTS is nil."
2889 (org-element-property :value macro))
2891 (defun org-element-macro-successor (limit)
2892 "Search for the next macro object.
2894 LIMIT bounds the search.
2896 Return value is cons cell whose CAR is `macro' and CDR is
2897 beginning position."
2898 (save-excursion
2899 (when (re-search-forward
2900 "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
2901 limit t)
2902 (cons 'macro (match-beginning 0)))))
2905 ;;;; Radio-target
2907 (defun org-element-radio-target-parser ()
2908 "Parse radio target at point.
2910 Return a list whose CAR is `radio-target' and CDR a plist with
2911 `:begin', `:end', `:contents-begin', `:contents-end', `:value'
2912 and `:post-blank' as keywords.
2914 Assume point is at the radio target."
2915 (save-excursion
2916 (looking-at org-radio-target-regexp)
2917 (let ((begin (point))
2918 (contents-begin (match-beginning 1))
2919 (contents-end (match-end 1))
2920 (value (org-match-string-no-properties 1))
2921 (post-blank (progn (goto-char (match-end 0))
2922 (skip-chars-forward " \t")))
2923 (end (point)))
2924 (list 'radio-target
2925 (list :begin begin
2926 :end end
2927 :contents-begin contents-begin
2928 :contents-end contents-end
2929 :post-blank post-blank
2930 :value value)))))
2932 (defun org-element-radio-target-interpreter (target contents)
2933 "Interpret TARGET object as Org syntax.
2934 CONTENTS is the contents of the object."
2935 (concat "<<<" contents ">>>"))
2937 (defun org-element-radio-target-successor (limit)
2938 "Search for the next radio-target object.
2940 LIMIT bounds the search.
2942 Return value is a cons cell whose CAR is `radio-target' and CDR
2943 is beginning position."
2944 (save-excursion
2945 (when (re-search-forward org-radio-target-regexp limit t)
2946 (cons 'radio-target (match-beginning 0)))))
2949 ;;;; Statistics Cookie
2951 (defun org-element-statistics-cookie-parser ()
2952 "Parse statistics cookie at point.
2954 Return a list whose CAR is `statistics-cookie', and CDR a plist
2955 with `:begin', `:end', `:value' and `:post-blank' keywords.
2957 Assume point is at the beginning of the statistics-cookie."
2958 (save-excursion
2959 (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
2960 (let* ((begin (point))
2961 (value (buffer-substring-no-properties
2962 (match-beginning 0) (match-end 0)))
2963 (post-blank (progn (goto-char (match-end 0))
2964 (skip-chars-forward " \t")))
2965 (end (point)))
2966 (list 'statistics-cookie
2967 (list :begin begin
2968 :end end
2969 :value value
2970 :post-blank post-blank)))))
2972 (defun org-element-statistics-cookie-interpreter (statistics-cookie contents)
2973 "Interpret STATISTICS-COOKIE object as Org syntax.
2974 CONTENTS is nil."
2975 (org-element-property :value statistics-cookie))
2977 (defun org-element-statistics-cookie-successor (limit)
2978 "Search for the next statistics cookie object.
2980 LIMIT bounds the search.
2982 Return value is a cons cell whose CAR is `statistics-cookie' and
2983 CDR is beginning position."
2984 (save-excursion
2985 (when (re-search-forward "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]" limit t)
2986 (cons 'statistics-cookie (match-beginning 0)))))
2989 ;;;; Strike-Through
2991 (defun org-element-strike-through-parser ()
2992 "Parse strike-through object at point.
2994 Return a list whose CAR is `strike-through' and CDR is a plist
2995 with `:begin', `:end', `:contents-begin' and `:contents-end' and
2996 `:post-blank' keywords.
2998 Assume point is at the first plus sign marker."
2999 (save-excursion
3000 (unless (bolp) (backward-char 1))
3001 (looking-at org-emph-re)
3002 (let ((begin (match-beginning 2))
3003 (contents-begin (match-beginning 4))
3004 (contents-end (match-end 4))
3005 (post-blank (progn (goto-char (match-end 2))
3006 (skip-chars-forward " \t")))
3007 (end (point)))
3008 (list 'strike-through
3009 (list :begin begin
3010 :end end
3011 :contents-begin contents-begin
3012 :contents-end contents-end
3013 :post-blank post-blank)))))
3015 (defun org-element-strike-through-interpreter (strike-through contents)
3016 "Interpret STRIKE-THROUGH object as Org syntax.
3017 CONTENTS is the contents of the object."
3018 (format "+%s+" contents))
3021 ;;;; Subscript
3023 (defun org-element-subscript-parser ()
3024 "Parse subscript at point.
3026 Return a list whose CAR is `subscript' and CDR a plist with
3027 `:begin', `:end', `:contents-begin', `:contents-end',
3028 `:use-brackets-p' and `:post-blank' as keywords.
3030 Assume point is at the underscore."
3031 (save-excursion
3032 (unless (bolp) (backward-char))
3033 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp)
3035 (not (looking-at org-match-substring-regexp))))
3036 (begin (match-beginning 2))
3037 (contents-begin (or (match-beginning 5)
3038 (match-beginning 3)))
3039 (contents-end (or (match-end 5) (match-end 3)))
3040 (post-blank (progn (goto-char (match-end 0))
3041 (skip-chars-forward " \t")))
3042 (end (point)))
3043 (list 'subscript
3044 (list :begin begin
3045 :end end
3046 :use-brackets-p bracketsp
3047 :contents-begin contents-begin
3048 :contents-end contents-end
3049 :post-blank post-blank)))))
3051 (defun org-element-subscript-interpreter (subscript contents)
3052 "Interpret SUBSCRIPT object as Org syntax.
3053 CONTENTS is the contents of the object."
3054 (format
3055 (if (org-element-property :use-brackets-p subscript) "_{%s}" "_%s")
3056 contents))
3058 (defun org-element-sub/superscript-successor (limit)
3059 "Search for the next sub/superscript object.
3061 LIMIT bounds the search.
3063 Return value is a cons cell whose CAR is either `subscript' or
3064 `superscript' and CDR is beginning position."
3065 (save-excursion
3066 (when (re-search-forward org-match-substring-regexp limit t)
3067 (cons (if (string= (match-string 2) "_") 'subscript 'superscript)
3068 (match-beginning 2)))))
3071 ;;;; Superscript
3073 (defun org-element-superscript-parser ()
3074 "Parse superscript at point.
3076 Return a list whose CAR is `superscript' and CDR a plist with
3077 `:begin', `:end', `:contents-begin', `:contents-end',
3078 `:use-brackets-p' and `:post-blank' as keywords.
3080 Assume point is at the caret."
3081 (save-excursion
3082 (unless (bolp) (backward-char))
3083 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp) t
3084 (not (looking-at org-match-substring-regexp))))
3085 (begin (match-beginning 2))
3086 (contents-begin (or (match-beginning 5)
3087 (match-beginning 3)))
3088 (contents-end (or (match-end 5) (match-end 3)))
3089 (post-blank (progn (goto-char (match-end 0))
3090 (skip-chars-forward " \t")))
3091 (end (point)))
3092 (list 'superscript
3093 (list :begin begin
3094 :end end
3095 :use-brackets-p bracketsp
3096 :contents-begin contents-begin
3097 :contents-end contents-end
3098 :post-blank post-blank)))))
3100 (defun org-element-superscript-interpreter (superscript contents)
3101 "Interpret SUPERSCRIPT object as Org syntax.
3102 CONTENTS is the contents of the object."
3103 (format
3104 (if (org-element-property :use-brackets-p superscript) "^{%s}" "^%s")
3105 contents))
3108 ;;;; Table Cell
3110 (defun org-element-table-cell-parser ()
3111 "Parse table cell at point.
3113 Return a list whose CAR is `table-cell' and CDR is a plist
3114 containing `:begin', `:end', `:contents-begin', `:contents-end'
3115 and `:post-blank' keywords."
3116 (looking-at "[ \t]*\\(.*?\\)[ \t]*|")
3117 (let* ((begin (match-beginning 0))
3118 (end (match-end 0))
3119 (contents-begin (match-beginning 1))
3120 (contents-end (match-end 1)))
3121 (list 'table-cell
3122 (list :begin begin
3123 :end end
3124 :contents-begin contents-begin
3125 :contents-end contents-end
3126 :post-blank 0))))
3128 (defun org-element-table-cell-interpreter (table-cell contents)
3129 "Interpret TABLE-CELL element as Org syntax.
3130 CONTENTS is the contents of the cell, or nil."
3131 (concat " " contents " |"))
3133 (defun org-element-table-cell-successor (limit)
3134 "Search for the next table-cell object.
3136 LIMIT bounds the search.
3138 Return value is a cons cell whose CAR is `table-cell' and CDR is
3139 beginning position."
3140 (when (looking-at "[ \t]*.*?[ \t]+|") (cons 'table-cell (point))))
3143 ;;;; Target
3145 (defun org-element-target-parser ()
3146 "Parse target at point.
3148 Return a list whose CAR is `target' and CDR a plist with
3149 `:begin', `:end', `:value' and `:post-blank' as keywords.
3151 Assume point is at the target."
3152 (save-excursion
3153 (looking-at org-target-regexp)
3154 (let ((begin (point))
3155 (value (org-match-string-no-properties 1))
3156 (post-blank (progn (goto-char (match-end 0))
3157 (skip-chars-forward " \t")))
3158 (end (point)))
3159 (list 'target
3160 (list :begin begin
3161 :end end
3162 :value value
3163 :post-blank post-blank)))))
3165 (defun org-element-target-interpreter (target contents)
3166 "Interpret TARGET object as Org syntax.
3167 CONTENTS is nil."
3168 (format "<<%s>>" (org-element-property :value target)))
3170 (defun org-element-target-successor (limit)
3171 "Search for the next target object.
3173 LIMIT bounds the search.
3175 Return value is a cons cell whose CAR is `target' and CDR is
3176 beginning position."
3177 (save-excursion
3178 (when (re-search-forward org-target-regexp limit t)
3179 (cons 'target (match-beginning 0)))))
3182 ;;;; Timestamp
3184 (defun org-element-timestamp-parser ()
3185 "Parse time stamp at point.
3187 Return a list whose CAR is `timestamp', and CDR a plist with
3188 `:type', `:begin', `:end', `:value' and `:post-blank' keywords.
3190 Assume point is at the beginning of the timestamp."
3191 (save-excursion
3192 (let* ((begin (point))
3193 (activep (eq (char-after) ?<))
3194 (main-value
3195 (progn
3196 (looking-at "[<[]\\(\\(%%\\)?.*?\\)[]>]\\(?:--[<[]\\(.*?\\)[]>]\\)?")
3197 (match-string-no-properties 1)))
3198 (range-end (match-string-no-properties 3))
3199 (type (cond ((match-string 2) 'diary)
3200 ((and activep range-end) 'active-range)
3201 (activep 'active)
3202 (range-end 'inactive-range)
3203 (t 'inactive)))
3204 (post-blank (progn (goto-char (match-end 0))
3205 (skip-chars-forward " \t")))
3206 (end (point)))
3207 (list 'timestamp
3208 (list :type type
3209 :value main-value
3210 :range-end range-end
3211 :begin begin
3212 :end end
3213 :post-blank post-blank)))))
3215 (defun org-element-timestamp-interpreter (timestamp contents)
3216 "Interpret TIMESTAMP object as Org syntax.
3217 CONTENTS is nil."
3218 (let ((type (org-element-property :type timestamp) ))
3219 (concat
3220 (format (if (memq type '(inactive inactive-range)) "[%s]" "<%s>")
3221 (org-element-property :value timestamp))
3222 (let ((range-end (org-element-property :range-end timestamp)))
3223 (when range-end
3224 (concat "--"
3225 (format (if (eq type 'inactive-range) "[%s]" "<%s>")
3226 range-end)))))))
3228 (defun org-element-timestamp-successor (limit)
3229 "Search for the next timestamp object.
3231 LIMIT bounds the search.
3233 Return value is a cons cell whose CAR is `timestamp' and CDR is
3234 beginning position."
3235 (save-excursion
3236 (when (re-search-forward
3237 (concat org-ts-regexp-both
3238 "\\|"
3239 "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
3240 "\\|"
3241 "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
3242 limit t)
3243 (cons 'timestamp (match-beginning 0)))))
3246 ;;;; Underline
3248 (defun org-element-underline-parser ()
3249 "Parse underline object at point.
3251 Return a list whose CAR is `underline' and CDR is a plist with
3252 `:begin', `:end', `:contents-begin' and `:contents-end' and
3253 `:post-blank' keywords.
3255 Assume point is at the first underscore marker."
3256 (save-excursion
3257 (unless (bolp) (backward-char 1))
3258 (looking-at org-emph-re)
3259 (let ((begin (match-beginning 2))
3260 (contents-begin (match-beginning 4))
3261 (contents-end (match-end 4))
3262 (post-blank (progn (goto-char (match-end 2))
3263 (skip-chars-forward " \t")))
3264 (end (point)))
3265 (list 'underline
3266 (list :begin begin
3267 :end end
3268 :contents-begin contents-begin
3269 :contents-end contents-end
3270 :post-blank post-blank)))))
3272 (defun org-element-underline-interpreter (underline contents)
3273 "Interpret UNDERLINE object as Org syntax.
3274 CONTENTS is the contents of the object."
3275 (format "_%s_" contents))
3278 ;;;; Verbatim
3280 (defun org-element-verbatim-parser ()
3281 "Parse verbatim object at point.
3283 Return a list whose CAR is `verbatim' and CDR is a plist with
3284 `:value', `:begin', `:end' and `:post-blank' keywords.
3286 Assume point is at the first equal sign marker."
3287 (save-excursion
3288 (unless (bolp) (backward-char 1))
3289 (looking-at org-emph-re)
3290 (let ((begin (match-beginning 2))
3291 (value (org-match-string-no-properties 4))
3292 (post-blank (progn (goto-char (match-end 2))
3293 (skip-chars-forward " \t")))
3294 (end (point)))
3295 (list 'verbatim
3296 (list :value value
3297 :begin begin
3298 :end end
3299 :post-blank post-blank)))))
3301 (defun org-element-verbatim-interpreter (verbatim contents)
3302 "Interpret VERBATIM object as Org syntax.
3303 CONTENTS is nil."
3304 (format "=%s=" (org-element-property :value verbatim)))
3308 ;;; Parsing Element Starting At Point
3310 ;; `org-element--current-element' is the core function of this section.
3311 ;; It returns the Lisp representation of the element starting at
3312 ;; point.
3314 ;; `org-element--current-element' makes use of special modes. They
3315 ;; are activated for fixed element chaining (i.e. `plain-list' >
3316 ;; `item') or fixed conditional element chaining (i.e. `headline' >
3317 ;; `section'). Special modes are: `first-section', `section',
3318 ;; `quote-section', `item' and `table-row'.
3320 (defun org-element--current-element
3321 (limit &optional granularity special structure)
3322 "Parse the element starting at point.
3324 LIMIT bounds the search.
3326 Return value is a list like (TYPE PROPS) where TYPE is the type
3327 of the element and PROPS a plist of properties associated to the
3328 element.
3330 Possible types are defined in `org-element-all-elements'.
3332 Optional argument GRANULARITY determines the depth of the
3333 recursion. Allowed values are `headline', `greater-element',
3334 `element', `object' or nil. When it is broader than `object' (or
3335 nil), secondary values will not be parsed, since they only
3336 contain objects.
3338 Optional argument SPECIAL, when non-nil, can be either
3339 `first-section', `section', `quote-section', `table-row' and
3340 `item'.
3342 If STRUCTURE isn't provided but SPECIAL is set to `item', it will
3343 be computed.
3345 This function assumes point is always at the beginning of the
3346 element it has to parse."
3347 (save-excursion
3348 ;; If point is at an affiliated keyword, try moving to the
3349 ;; beginning of the associated element. If none is found, the
3350 ;; keyword is orphaned and will be treated as plain text.
3351 (when (looking-at org-element--affiliated-re)
3352 (let ((opoint (point)))
3353 (while (looking-at org-element--affiliated-re) (forward-line))
3354 (when (looking-at "[ \t]*$") (goto-char opoint))))
3355 (let ((case-fold-search t)
3356 ;; Determine if parsing depth allows for secondary strings
3357 ;; parsing. It only applies to elements referenced in
3358 ;; `org-element-secondary-value-alist'.
3359 (raw-secondary-p (and granularity (not (eq granularity 'object)))))
3360 (cond
3361 ;; Item.
3362 ((eq special 'item)
3363 (org-element-item-parser limit structure raw-secondary-p))
3364 ;; Table Row.
3365 ((eq special 'table-row) (org-element-table-row-parser limit))
3366 ;; Headline.
3367 ((org-with-limited-levels (org-at-heading-p))
3368 (org-element-headline-parser limit raw-secondary-p))
3369 ;; Sections (must be checked after headline).
3370 ((eq special 'section) (org-element-section-parser limit))
3371 ((eq special 'quote-section) (org-element-quote-section-parser limit))
3372 ((eq special 'first-section)
3373 (org-element-section-parser
3374 (or (save-excursion (org-with-limited-levels (outline-next-heading)))
3375 limit)))
3376 ;; When not at bol, point is at the beginning of an item or
3377 ;; a footnote definition: next item is always a paragraph.
3378 ((not (bolp)) (org-element-paragraph-parser limit))
3379 ;; Planning and Clock.
3380 ((and (looking-at org-planning-or-clock-line-re))
3381 (if (equal (match-string 1) org-clock-string)
3382 (org-element-clock-parser limit)
3383 (org-element-planning-parser limit)))
3384 ;; Inlinetask.
3385 ((org-at-heading-p)
3386 (org-element-inlinetask-parser limit raw-secondary-p))
3387 ;; LaTeX Environment.
3388 ((looking-at "[ \t]*\\\\begin{\\([A-Za-z0-9*]+\\)}")
3389 (if (save-excursion
3390 (re-search-forward
3391 (format "[ \t]*\\\\end{%s}[ \t]*"
3392 (regexp-quote (match-string 1)))
3393 nil t))
3394 (org-element-latex-environment-parser limit)
3395 (org-element-paragraph-parser limit)))
3396 ;; Drawer and Property Drawer.
3397 ((looking-at org-drawer-regexp)
3398 (let ((name (match-string 1)))
3399 (cond
3400 ((not (save-excursion
3401 (re-search-forward "^[ \t]*:END:[ \t]*$" nil t)))
3402 (org-element-paragraph-parser limit))
3403 ((equal "PROPERTIES" name)
3404 (org-element-property-drawer-parser limit))
3405 (t (org-element-drawer-parser limit)))))
3406 ;; Fixed Width
3407 ((looking-at "[ \t]*:\\( \\|$\\)")
3408 (org-element-fixed-width-parser limit))
3409 ;; Inline Comments, Blocks, Babel Calls, Dynamic Blocks and
3410 ;; Keywords.
3411 ((looking-at "[ \t]*#")
3412 (goto-char (match-end 0))
3413 (cond ((looking-at "\\(?: \\|$\\)")
3414 (beginning-of-line)
3415 (org-element-comment-parser limit))
3416 ((looking-at "\\+BEGIN_\\(\\S-+\\)")
3417 (beginning-of-line)
3418 (let ((parser (assoc (upcase (match-string 1))
3419 org-element-block-name-alist)))
3420 (if parser (funcall (cdr parser) limit)
3421 (org-element-special-block-parser limit))))
3422 ((looking-at "\\+CALL:")
3423 (beginning-of-line)
3424 (org-element-babel-call-parser limit))
3425 ((looking-at "\\+BEGIN:? ")
3426 (beginning-of-line)
3427 (org-element-dynamic-block-parser limit))
3428 ((looking-at "\\+\\S-+:")
3429 (beginning-of-line)
3430 (org-element-keyword-parser limit))
3432 (beginning-of-line)
3433 (org-element-paragraph-parser limit))))
3434 ;; Footnote Definition.
3435 ((looking-at org-footnote-definition-re)
3436 (org-element-footnote-definition-parser limit))
3437 ;; Horizontal Rule.
3438 ((looking-at "[ \t]*-\\{5,\\}[ \t]*$")
3439 (org-element-horizontal-rule-parser limit))
3440 ;; Table.
3441 ((org-at-table-p t) (org-element-table-parser limit))
3442 ;; List.
3443 ((looking-at (org-item-re))
3444 (org-element-plain-list-parser limit (or structure (org-list-struct))))
3445 ;; Default element: Paragraph.
3446 (t (org-element-paragraph-parser limit))))))
3449 ;; Most elements can have affiliated keywords. When looking for an
3450 ;; element beginning, we want to move before them, as they belong to
3451 ;; that element, and, in the meantime, collect information they give
3452 ;; into appropriate properties. Hence the following function.
3454 ;; Usage of optional arguments may not be obvious at first glance:
3456 ;; - TRANS-LIST is used to polish keywords names that have evolved
3457 ;; during Org history. In example, even though =result= and
3458 ;; =results= coexist, we want to have them under the same =result=
3459 ;; property. It's also true for "srcname" and "name", where the
3460 ;; latter seems to be preferred nowadays (thus the "name" property).
3462 ;; - CONSED allows to regroup multi-lines keywords under the same
3463 ;; property, while preserving their own identity. This is mostly
3464 ;; used for "attr_latex" and al.
3466 ;; - PARSED prepares a keyword value for export. This is useful for
3467 ;; "caption". Objects restrictions for such keywords are defined in
3468 ;; `org-element-object-restrictions'.
3470 ;; - DUALS is used to take care of keywords accepting a main and an
3471 ;; optional secondary values. For example "results" has its
3472 ;; source's name as the main value, and may have an hash string in
3473 ;; optional square brackets as the secondary one.
3475 ;; A keyword may belong to more than one category.
3477 (defun org-element--collect-affiliated-keywords
3478 (&optional key-re trans-list consed parsed duals)
3479 "Collect affiliated keywords before point.
3481 Optional argument KEY-RE is a regexp matching keywords, which
3482 puts matched keyword in group 1. It defaults to
3483 `org-element--affiliated-re'.
3485 TRANS-LIST is an alist where key is the keyword and value the
3486 property name it should be translated to, without the colons. It
3487 defaults to `org-element-keyword-translation-alist'.
3489 CONSED is a list of strings. Any keyword belonging to that list
3490 will have its value consed. The check is done after keyword
3491 translation. It defaults to `org-element-multiple-keywords'.
3493 PARSED is a list of strings. Any keyword member of this list
3494 will have its value parsed. The check is done after keyword
3495 translation. If a keyword is a member of both CONSED and PARSED,
3496 it's value will be a list of parsed strings. It defaults to
3497 `org-element-parsed-keywords'.
3499 DUALS is a list of strings. Any keyword member of this list can
3500 have two parts: one mandatory and one optional. Its value is
3501 a cons cell whose CAR is the former, and the CDR the latter. If
3502 a keyword is a member of both PARSED and DUALS, both values will
3503 be parsed. It defaults to `org-element-dual-keywords'.
3505 Return a list whose CAR is the position at the first of them and
3506 CDR a plist of keywords and values."
3507 (save-excursion
3508 (let ((case-fold-search t)
3509 (key-re (or key-re org-element--affiliated-re))
3510 (trans-list (or trans-list org-element-keyword-translation-alist))
3511 (consed (or consed org-element-multiple-keywords))
3512 (parsed (or parsed org-element-parsed-keywords))
3513 (duals (or duals org-element-dual-keywords))
3514 ;; RESTRICT is the list of objects allowed in parsed
3515 ;; keywords value.
3516 (restrict (org-element-restriction 'keyword))
3517 output)
3518 (unless (bobp)
3519 (while (and (not (bobp)) (progn (forward-line -1) (looking-at key-re)))
3520 (let* ((raw-kwd (upcase (match-string 1)))
3521 ;; Apply translation to RAW-KWD. From there, KWD is
3522 ;; the official keyword.
3523 (kwd (or (cdr (assoc raw-kwd trans-list)) raw-kwd))
3524 ;; Find main value for any keyword.
3525 (value
3526 (save-match-data
3527 (org-trim
3528 (buffer-substring-no-properties
3529 (match-end 0) (point-at-eol)))))
3530 ;; If KWD is a dual keyword, find its secondary
3531 ;; value. Maybe parse it.
3532 (dual-value
3533 (and (member kwd duals)
3534 (let ((sec (org-match-string-no-properties 2)))
3535 (if (or (not sec) (not (member kwd parsed))) sec
3536 (org-element-parse-secondary-string sec restrict)))))
3537 ;; Attribute a property name to KWD.
3538 (kwd-sym (and kwd (intern (concat ":" (downcase kwd))))))
3539 ;; Now set final shape for VALUE.
3540 (when (member kwd parsed)
3541 (setq value (org-element-parse-secondary-string value restrict)))
3542 (when (member kwd duals)
3543 ;; VALUE is mandatory. Set it to nil if there is none.
3544 (setq value (and value (cons value dual-value))))
3545 ;; Attributes are always consed.
3546 (when (or (member kwd consed) (string-match "^ATTR_" kwd))
3547 (setq value (cons value (plist-get output kwd-sym))))
3548 ;; Eventually store the new value in OUTPUT.
3549 (setq output (plist-put output kwd-sym value))))
3550 (unless (looking-at key-re) (forward-line 1)))
3551 (list (point) output))))
3555 ;;; The Org Parser
3557 ;; The two major functions here are `org-element-parse-buffer', which
3558 ;; parses Org syntax inside the current buffer, taking into account
3559 ;; region, narrowing, or even visibility if specified, and
3560 ;; `org-element-parse-secondary-string', which parses objects within
3561 ;; a given string.
3563 ;; The (almost) almighty `org-element-map' allows to apply a function
3564 ;; on elements or objects matching some type, and accumulate the
3565 ;; resulting values. In an export situation, it also skips unneeded
3566 ;; parts of the parse tree.
3568 (defun org-element-parse-buffer (&optional granularity visible-only)
3569 "Recursively parse the buffer and return structure.
3570 If narrowing is in effect, only parse the visible part of the
3571 buffer.
3573 Optional argument GRANULARITY determines the depth of the
3574 recursion. It can be set to the following symbols:
3576 `headline' Only parse headlines.
3577 `greater-element' Don't recurse into greater elements excepted
3578 headlines and sections. Thus, elements
3579 parsed are the top-level ones.
3580 `element' Parse everything but objects and plain text.
3581 `object' Parse the complete buffer (default).
3583 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3584 elements.
3586 Assume buffer is in Org mode."
3587 (save-excursion
3588 (goto-char (point-min))
3589 (org-skip-whitespace)
3590 (org-element--parse-elements
3591 (point-at-bol) (point-max)
3592 ;; Start in `first-section' mode so text before the first
3593 ;; headline belongs to a section.
3594 'first-section nil granularity visible-only (list 'org-data nil))))
3596 (defun org-element-parse-secondary-string (string restriction &optional parent)
3597 "Recursively parse objects in STRING and return structure.
3599 RESTRICTION is a symbol limiting the object types that will be
3600 looked after.
3602 Optional argument PARENT, when non-nil, is the element or object
3603 containing the secondary string. It is used to set correctly
3604 `:parent' property within the string."
3605 (with-temp-buffer
3606 (insert string)
3607 (let ((secondary (org-element--parse-objects
3608 (point-min) (point-max) nil restriction)))
3609 (mapc (lambda (obj) (org-element-put-property obj :parent parent))
3610 secondary))))
3612 (defun org-element-map (data types fun &optional info first-match no-recursion)
3613 "Map a function on selected elements or objects.
3615 DATA is the parsed tree, as returned by, i.e,
3616 `org-element-parse-buffer'. TYPES is a symbol or list of symbols
3617 of elements or objects types. FUN is the function called on the
3618 matching element or object. It must accept one arguments: the
3619 element or object itself.
3621 When optional argument INFO is non-nil, it should be a plist
3622 holding export options. In that case, parts of the parse tree
3623 not exportable according to that property list will be skipped.
3625 When optional argument FIRST-MATCH is non-nil, stop at the first
3626 match for which FUN doesn't return nil, and return that value.
3628 Optional argument NO-RECURSION is a symbol or a list of symbols
3629 representing elements or objects types. `org-element-map' won't
3630 enter any recursive element or object whose type belongs to that
3631 list. Though, FUN can still be applied on them.
3633 Nil values returned from FUN do not appear in the results."
3634 ;; Ensure TYPES and NO-RECURSION are a list, even of one element.
3635 (unless (listp types) (setq types (list types)))
3636 (unless (listp no-recursion) (setq no-recursion (list no-recursion)))
3637 ;; Recursion depth is determined by --CATEGORY.
3638 (let* ((--category
3639 (catch 'found
3640 (let ((category 'greater-elements))
3641 (mapc (lambda (type)
3642 (cond ((or (memq type org-element-all-objects)
3643 (eq type 'plain-text))
3644 ;; If one object is found, the function
3645 ;; has to recurse into every object.
3646 (throw 'found 'objects))
3647 ((not (memq type org-element-greater-elements))
3648 ;; If one regular element is found, the
3649 ;; function has to recurse, at least,
3650 ;; into every element it encounters.
3651 (and (not (eq category 'elements))
3652 (setq category 'elements)))))
3653 types)
3654 category)))
3655 --acc
3656 --walk-tree
3657 (--walk-tree
3658 (function
3659 (lambda (--data)
3660 ;; Recursively walk DATA. INFO, if non-nil, is a plist
3661 ;; holding contextual information.
3662 (let ((--type (org-element-type --data)))
3663 (cond
3664 ((not --data))
3665 ;; Ignored element in an export context.
3666 ((and info (memq --data (plist-get info :ignore-list))))
3667 ;; Secondary string: only objects can be found there.
3668 ((not --type)
3669 (when (eq --category 'objects) (mapc --walk-tree --data)))
3670 ;; Unconditionally enter parse trees.
3671 ((eq --type 'org-data)
3672 (mapc --walk-tree (org-element-contents --data)))
3674 ;; Check if TYPE is matching among TYPES. If so,
3675 ;; apply FUN to --DATA and accumulate return value
3676 ;; into --ACC (or exit if FIRST-MATCH is non-nil).
3677 (when (memq --type types)
3678 (let ((result (funcall fun --data)))
3679 (cond ((not result))
3680 (first-match (throw '--map-first-match result))
3681 (t (push result --acc)))))
3682 ;; If --DATA has a secondary string that can contain
3683 ;; objects with their type among TYPES, look into it.
3684 (when (eq --category 'objects)
3685 (let ((sec-prop
3686 (assq --type org-element-secondary-value-alist)))
3687 (when sec-prop
3688 (funcall --walk-tree
3689 (org-element-property (cdr sec-prop) --data)))))
3690 ;; Determine if a recursion into --DATA is possible.
3691 (cond
3692 ;; --TYPE is explicitly removed from recursion.
3693 ((memq --type no-recursion))
3694 ;; --DATA has no contents.
3695 ((not (org-element-contents --data)))
3696 ;; Looking for greater elements but --DATA is simply
3697 ;; an element or an object.
3698 ((and (eq --category 'greater-elements)
3699 (not (memq --type org-element-greater-elements))))
3700 ;; Looking for elements but --DATA is an object.
3701 ((and (eq --category 'elements)
3702 (memq --type org-element-all-objects)))
3703 ;; In any other case, map contents.
3704 (t (mapc --walk-tree (org-element-contents --data)))))))))))
3705 (catch '--map-first-match
3706 (funcall --walk-tree data)
3707 ;; Return value in a proper order.
3708 (nreverse --acc))))
3710 ;; The following functions are internal parts of the parser.
3712 ;; The first one, `org-element--parse-elements' acts at the element's
3713 ;; level.
3715 ;; The second one, `org-element--parse-objects' applies on all objects
3716 ;; of a paragraph or a secondary string. It uses
3717 ;; `org-element--get-next-object-candidates' to optimize the search of
3718 ;; the next object in the buffer.
3720 ;; More precisely, that function looks for every allowed object type
3721 ;; first. Then, it discards failed searches, keeps further matches,
3722 ;; and searches again types matched behind point, for subsequent
3723 ;; calls. Thus, searching for a given type fails only once, and every
3724 ;; object is searched only once at top level (but sometimes more for
3725 ;; nested types).
3727 (defun org-element--parse-elements
3728 (beg end special structure granularity visible-only acc)
3729 "Parse elements between BEG and END positions.
3731 SPECIAL prioritize some elements over the others. It can be set
3732 to `first-section', `quote-section', `section' `item' or
3733 `table-row'.
3735 When value is `item', STRUCTURE will be used as the current list
3736 structure.
3738 GRANULARITY determines the depth of the recursion. See
3739 `org-element-parse-buffer' for more information.
3741 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3742 elements.
3744 Elements are accumulated into ACC."
3745 (save-excursion
3746 (goto-char beg)
3747 ;; When parsing only headlines, skip any text before first one.
3748 (when (and (eq granularity 'headline) (not (org-at-heading-p)))
3749 (org-with-limited-levels (outline-next-heading)))
3750 ;; Main loop start.
3751 (while (< (point) end)
3752 ;; Find current element's type and parse it accordingly to
3753 ;; its category.
3754 (let* ((element (org-element--current-element
3755 end granularity special structure))
3756 (type (org-element-type element))
3757 (cbeg (org-element-property :contents-begin element)))
3758 (goto-char (org-element-property :end element))
3759 ;; Fill ELEMENT contents by side-effect.
3760 (cond
3761 ;; If VISIBLE-ONLY is true and element is hidden or if it has
3762 ;; no contents, don't modify it.
3763 ((or (and visible-only (org-element-property :hiddenp element))
3764 (not cbeg)))
3765 ;; Greater element: parse it between `contents-begin' and
3766 ;; `contents-end'. Make sure GRANULARITY allows the
3767 ;; recursion, or ELEMENT is an headline, in which case going
3768 ;; inside is mandatory, in order to get sub-level headings.
3769 ((and (memq type org-element-greater-elements)
3770 (or (memq granularity '(element object nil))
3771 (and (eq granularity 'greater-element)
3772 (eq type 'section))
3773 (eq type 'headline)))
3774 (org-element--parse-elements
3775 cbeg (org-element-property :contents-end element)
3776 ;; Possibly switch to a special mode.
3777 (case type
3778 (headline
3779 (if (org-element-property :quotedp element) 'quote-section
3780 'section))
3781 (plain-list 'item)
3782 (table 'table-row))
3783 (org-element-property :structure element)
3784 granularity visible-only element))
3785 ;; ELEMENT has contents. Parse objects inside, if
3786 ;; GRANULARITY allows it.
3787 ((memq granularity '(object nil))
3788 (org-element--parse-objects
3789 cbeg (org-element-property :contents-end element) element
3790 (org-element-restriction type))))
3791 (org-element-adopt-elements acc element)))
3792 ;; Return result.
3793 acc))
3795 (defun org-element--parse-objects (beg end acc restriction)
3796 "Parse objects between BEG and END and return recursive structure.
3798 Objects are accumulated in ACC.
3800 RESTRICTION is a list of object types which are allowed in the
3801 current object."
3802 (let (candidates)
3803 (save-excursion
3804 (goto-char beg)
3805 (while (and (< (point) end)
3806 (setq candidates (org-element--get-next-object-candidates
3807 end restriction candidates)))
3808 (let ((next-object
3809 (let ((pos (apply 'min (mapcar 'cdr candidates))))
3810 (save-excursion
3811 (goto-char pos)
3812 (funcall (intern (format "org-element-%s-parser"
3813 (car (rassq pos candidates)))))))))
3814 ;; 1. Text before any object. Untabify it.
3815 (let ((obj-beg (org-element-property :begin next-object)))
3816 (unless (= (point) obj-beg)
3817 (setq acc
3818 (org-element-adopt-elements
3820 (replace-regexp-in-string
3821 "\t" (make-string tab-width ? )
3822 (buffer-substring-no-properties (point) obj-beg))))))
3823 ;; 2. Object...
3824 (let ((obj-end (org-element-property :end next-object))
3825 (cont-beg (org-element-property :contents-begin next-object)))
3826 ;; Fill contents of NEXT-OBJECT by side-effect, if it has
3827 ;; a recursive type.
3828 (when (and cont-beg
3829 (memq (car next-object) org-element-recursive-objects))
3830 (save-restriction
3831 (narrow-to-region
3832 cont-beg
3833 (org-element-property :contents-end next-object))
3834 (org-element--parse-objects
3835 (point-min) (point-max) next-object
3836 (org-element-restriction next-object))))
3837 (setq acc (org-element-adopt-elements acc next-object))
3838 (goto-char obj-end))))
3839 ;; 3. Text after last object. Untabify it.
3840 (unless (= (point) end)
3841 (setq acc
3842 (org-element-adopt-elements
3844 (replace-regexp-in-string
3845 "\t" (make-string tab-width ? )
3846 (buffer-substring-no-properties (point) end)))))
3847 ;; Result.
3848 acc)))
3850 (defun org-element--get-next-object-candidates (limit restriction objects)
3851 "Return an alist of candidates for the next object.
3853 LIMIT bounds the search, and RESTRICTION narrows candidates to
3854 some object types.
3856 Return value is an alist whose CAR is position and CDR the object
3857 type, as a symbol.
3859 OBJECTS is the previous candidates alist."
3860 ;; Filter out any object found but not belonging to RESTRICTION.
3861 (setq objects (org-remove-if-not (lambda (obj) (memq (car obj) restriction))
3862 objects))
3863 (let (next-candidates types-to-search)
3864 ;; If no previous result, search every object type in RESTRICTION.
3865 ;; Otherwise, keep potential candidates (old objects located after
3866 ;; point) and ask to search again those which had matched before.
3867 (if (not objects) (setq types-to-search restriction)
3868 (mapc (lambda (obj)
3869 (if (< (cdr obj) (point)) (push (car obj) types-to-search)
3870 (push obj next-candidates)))
3871 objects))
3872 ;; Call the appropriate successor function for each type to search
3873 ;; and accumulate matches.
3874 (mapc
3875 (lambda (type)
3876 (let* ((successor-fun
3877 (intern
3878 (format "org-element-%s-successor"
3879 (or (cdr (assq type org-element-object-successor-alist))
3880 type))))
3881 (obj (funcall successor-fun limit)))
3882 (and obj (push obj next-candidates))))
3883 types-to-search)
3884 ;; Return alist.
3885 next-candidates))
3889 ;;; Towards A Bijective Process
3891 ;; The parse tree obtained with `org-element-parse-buffer' is really
3892 ;; a snapshot of the corresponding Org buffer. Therefore, it can be
3893 ;; interpreted and expanded into a string with canonical Org syntax.
3894 ;; Hence `org-element-interpret-data'.
3896 ;; The function relies internally on
3897 ;; `org-element--interpret-affiliated-keywords'.
3899 ;;;###autoload
3900 (defun org-element-interpret-data (data &optional parent)
3901 "Interpret DATA as Org syntax.
3903 DATA is a parse tree, an element, an object or a secondary string
3904 to interpret.
3906 Optional argument PARENT is used for recursive calls. It contains
3907 the element or object containing data, or nil.
3909 Return Org syntax as a string."
3910 (let* ((type (org-element-type data))
3911 (results
3912 (cond
3913 ;; Secondary string.
3914 ((not type)
3915 (mapconcat
3916 (lambda (obj) (org-element-interpret-data obj parent))
3917 data ""))
3918 ;; Full Org document.
3919 ((eq type 'org-data)
3920 (mapconcat
3921 (lambda (obj) (org-element-interpret-data obj parent))
3922 (org-element-contents data) ""))
3923 ;; Plain text.
3924 ((stringp data) data)
3925 ;; Element/Object without contents.
3926 ((not (org-element-contents data))
3927 (funcall (intern (format "org-element-%s-interpreter" type))
3928 data nil))
3929 ;; Element/Object with contents.
3931 (let* ((greaterp (memq type org-element-greater-elements))
3932 (objectp (and (not greaterp)
3933 (memq type org-element-recursive-objects)))
3934 (contents
3935 (mapconcat
3936 (lambda (obj) (org-element-interpret-data obj data))
3937 (org-element-contents
3938 (if (or greaterp objectp) data
3939 ;; Elements directly containing objects must
3940 ;; have their indentation normalized first.
3941 (org-element-normalize-contents
3942 data
3943 ;; When normalizing first paragraph of an
3944 ;; item or a footnote-definition, ignore
3945 ;; first line's indentation.
3946 (and (eq type 'paragraph)
3947 (equal data (car (org-element-contents parent)))
3948 (memq (org-element-type parent)
3949 '(footnote-definiton item))))))
3950 "")))
3951 (funcall (intern (format "org-element-%s-interpreter" type))
3952 data
3953 (if greaterp (org-element-normalize-contents contents)
3954 contents)))))))
3955 (if (memq type '(org-data plain-text nil)) results
3956 ;; Build white spaces. If no `:post-blank' property is
3957 ;; specified, assume its value is 0.
3958 (let ((post-blank (or (org-element-property :post-blank data) 0)))
3959 (if (memq type org-element-all-objects)
3960 (concat results (make-string post-blank 32))
3961 (concat
3962 (org-element--interpret-affiliated-keywords data)
3963 (org-element-normalize-string results)
3964 (make-string post-blank 10)))))))
3966 (defun org-element--interpret-affiliated-keywords (element)
3967 "Return ELEMENT's affiliated keywords as Org syntax.
3968 If there is no affiliated keyword, return the empty string."
3969 (let ((keyword-to-org
3970 (function
3971 (lambda (key value)
3972 (let (dual)
3973 (when (member key org-element-dual-keywords)
3974 (setq dual (cdr value) value (car value)))
3975 (concat "#+" key
3976 (and dual
3977 (format "[%s]" (org-element-interpret-data dual)))
3978 ": "
3979 (if (member key org-element-parsed-keywords)
3980 (org-element-interpret-data value)
3981 value)
3982 "\n"))))))
3983 (mapconcat
3984 (lambda (prop)
3985 (let ((value (org-element-property prop element))
3986 (keyword (upcase (substring (symbol-name prop) 1))))
3987 (when value
3988 (if (or (member keyword org-element-multiple-keywords)
3989 ;; All attribute keywords can have multiple lines.
3990 (string-match "^ATTR_" keyword))
3991 (mapconcat (lambda (line) (funcall keyword-to-org keyword line))
3992 value
3994 (funcall keyword-to-org keyword value)))))
3995 ;; List all ELEMENT's properties matching an attribute line or an
3996 ;; affiliated keyword, but ignore translated keywords since they
3997 ;; cannot belong to the property list.
3998 (loop for prop in (nth 1 element) by 'cddr
3999 when (let ((keyword (upcase (substring (symbol-name prop) 1))))
4000 (or (string-match "^ATTR_" keyword)
4001 (and
4002 (member keyword org-element-affiliated-keywords)
4003 (not (assoc keyword
4004 org-element-keyword-translation-alist)))))
4005 collect prop)
4006 "")))
4008 ;; Because interpretation of the parse tree must return the same
4009 ;; number of blank lines between elements and the same number of white
4010 ;; space after objects, some special care must be given to white
4011 ;; spaces.
4013 ;; The first function, `org-element-normalize-string', ensures any
4014 ;; string different from the empty string will end with a single
4015 ;; newline character.
4017 ;; The second function, `org-element-normalize-contents', removes
4018 ;; global indentation from the contents of the current element.
4020 (defun org-element-normalize-string (s)
4021 "Ensure string S ends with a single newline character.
4023 If S isn't a string return it unchanged. If S is the empty
4024 string, return it. Otherwise, return a new string with a single
4025 newline character at its end."
4026 (cond
4027 ((not (stringp s)) s)
4028 ((string= "" s) "")
4029 (t (and (string-match "\\(\n[ \t]*\\)*\\'" s)
4030 (replace-match "\n" nil nil s)))))
4032 (defun org-element-normalize-contents (element &optional ignore-first)
4033 "Normalize plain text in ELEMENT's contents.
4035 ELEMENT must only contain plain text and objects.
4037 If optional argument IGNORE-FIRST is non-nil, ignore first line's
4038 indentation to compute maximal common indentation.
4040 Return the normalized element that is element with global
4041 indentation removed from its contents. The function assumes that
4042 indentation is not done with TAB characters."
4043 (let* (ind-list ; for byte-compiler
4044 collect-inds ; for byte-compiler
4045 (collect-inds
4046 (function
4047 ;; Return list of indentations within BLOB. This is done by
4048 ;; walking recursively BLOB and updating IND-LIST along the
4049 ;; way. FIRST-FLAG is non-nil when the first string hasn't
4050 ;; been seen yet. It is required as this string is the only
4051 ;; one whose indentation doesn't happen after a newline
4052 ;; character.
4053 (lambda (blob first-flag)
4054 (mapc
4055 (lambda (object)
4056 (when (and first-flag (stringp object))
4057 (setq first-flag nil)
4058 (string-match "\\`\\( *\\)" object)
4059 (let ((len (length (match-string 1 object))))
4060 ;; An indentation of zero means no string will be
4061 ;; modified. Quit the process.
4062 (if (zerop len) (throw 'zero (setq ind-list nil))
4063 (push len ind-list))))
4064 (cond
4065 ((stringp object)
4066 (let ((start 0))
4067 ;; Avoid matching blank or empty lines.
4068 (while (and (string-match "\n\\( *\\)\\(.\\)" object start)
4069 (not (equal (match-string 2 object) " ")))
4070 (setq start (match-end 0))
4071 (push (length (match-string 1 object)) ind-list))))
4072 ((memq (org-element-type object) org-element-recursive-objects)
4073 (funcall collect-inds object first-flag))))
4074 (org-element-contents blob))))))
4075 ;; Collect indentation list in ELEMENT. Possibly remove first
4076 ;; value if IGNORE-FIRST is non-nil.
4077 (catch 'zero (funcall collect-inds element (not ignore-first)))
4078 (if (not ind-list) element
4079 ;; Build ELEMENT back, replacing each string with the same
4080 ;; string minus common indentation.
4081 (let* (build ; For byte compiler.
4082 (build
4083 (function
4084 (lambda (blob mci first-flag)
4085 ;; Return BLOB with all its strings indentation
4086 ;; shortened from MCI white spaces. FIRST-FLAG is
4087 ;; non-nil when the first string hasn't been seen
4088 ;; yet.
4089 (setcdr (cdr blob)
4090 (mapcar
4091 (lambda (object)
4092 (when (and first-flag (stringp object))
4093 (setq first-flag nil)
4094 (setq object
4095 (replace-regexp-in-string
4096 (format "\\` \\{%d\\}" mci) "" object)))
4097 (cond
4098 ((stringp object)
4099 (replace-regexp-in-string
4100 (format "\n \\{%d\\}" mci) "\n" object))
4101 ((memq (org-element-type object)
4102 org-element-recursive-objects)
4103 (funcall build object mci first-flag))
4104 (t object)))
4105 (org-element-contents blob)))
4106 blob))))
4107 (funcall build element (apply 'min ind-list) (not ignore-first))))))
4111 ;;; The Toolbox
4113 ;; The first move is to implement a way to obtain the smallest element
4114 ;; containing point. This is the job of `org-element-at-point'. It
4115 ;; basically jumps back to the beginning of section containing point
4116 ;; and moves, element after element, with
4117 ;; `org-element--current-element' until the container is found. Note:
4118 ;; When using `org-element-at-point', secondary values are never
4119 ;; parsed since the function focuses on elements, not on objects.
4121 ;; At a deeper level, `org-element-context' lists all elements and
4122 ;; objects containing point.
4124 ;; `org-element-nested-p' and `org-element-swap-A-B' may be used
4125 ;; internally by navigation and manipulation tools.
4127 ;;;###autoload
4128 (defun org-element-at-point (&optional keep-trail)
4129 "Determine closest element around point.
4131 Return value is a list like (TYPE PROPS) where TYPE is the type
4132 of the element and PROPS a plist of properties associated to the
4133 element.
4135 Possible types are defined in `org-element-all-elements'.
4136 Properties depend on element or object type, but always
4137 include :begin, :end, :parent and :post-blank properties.
4139 As a special case, if point is at the very beginning of a list or
4140 sub-list, returned element will be that list instead of the first
4141 item. In the same way, if point is at the beginning of the first
4142 row of a table, returned element will be the table instead of the
4143 first row.
4145 If optional argument KEEP-TRAIL is non-nil, the function returns
4146 a list of of elements leading to element at point. The list's
4147 CAR is always the element at point. Following positions contain
4148 element's siblings, then parents, siblings of parents, until the
4149 first element of current section."
4150 (org-with-wide-buffer
4151 ;; If at an headline, parse it. It is the sole element that
4152 ;; doesn't require to know about context. Be sure to disallow
4153 ;; secondary string parsing, though.
4154 (if (org-with-limited-levels (org-at-heading-p))
4155 (progn
4156 (beginning-of-line)
4157 (if (not keep-trail) (org-element-headline-parser (point-max) t)
4158 (list (org-element-headline-parser (point-max) t))))
4159 ;; Otherwise move at the beginning of the section containing
4160 ;; point.
4161 (let ((origin (point))
4162 (end (save-excursion
4163 (org-with-limited-levels (outline-next-heading)) (point)))
4164 element type special-flag trail struct prevs parent)
4165 (org-with-limited-levels
4166 (if (org-with-limited-levels (org-before-first-heading-p))
4167 (goto-char (point-min))
4168 (org-back-to-heading)
4169 (forward-line)))
4170 (org-skip-whitespace)
4171 (beginning-of-line)
4172 ;; Parse successively each element, skipping those ending
4173 ;; before original position.
4174 (catch 'exit
4175 (while t
4176 (setq element
4177 (org-element--current-element end 'element special-flag struct)
4178 type (car element))
4179 (org-element-put-property element :parent parent)
4180 (when keep-trail (push element trail))
4181 (cond
4182 ;; 1. Skip any element ending before point. Also skip
4183 ;; element ending at point when we're sure that another
4184 ;; element has started.
4185 ((let ((elem-end (org-element-property :end element)))
4186 (when (or (< elem-end origin)
4187 (and (= elem-end origin) (/= elem-end end)))
4188 (goto-char elem-end))))
4189 ;; 2. An element containing point is always the element at
4190 ;; point.
4191 ((not (memq type org-element-greater-elements))
4192 (throw 'exit (if keep-trail trail element)))
4193 ;; 3. At any other greater element type, if point is
4194 ;; within contents, move into it.
4196 (let ((cbeg (org-element-property :contents-begin element))
4197 (cend (org-element-property :contents-end element)))
4198 (if (or (not cbeg) (not cend) (> cbeg origin) (< cend origin)
4199 ;; Create an anchor for tables and plain lists:
4200 ;; when point is at the very beginning of these
4201 ;; elements, ignoring affiliated keywords,
4202 ;; target them instead of their contents.
4203 (and (= cbeg origin) (memq type '(plain-list table)))
4204 ;; When point is at contents end, do not move
4205 ;; into elements with an explicit ending, but
4206 ;; return that element instead.
4207 (and (= cend origin)
4208 (memq type
4209 '(center-block
4210 drawer dynamic-block inlinetask item
4211 plain-list quote-block special-block))))
4212 (throw 'exit (if keep-trail trail element))
4213 (setq parent element)
4214 (case type
4215 (plain-list
4216 (setq special-flag 'item
4217 struct (org-element-property :structure element)))
4218 (table (setq special-flag 'table-row))
4219 (otherwise (setq special-flag nil)))
4220 (setq end cend)
4221 (goto-char cbeg)))))))))))
4223 ;;;###autoload
4224 (defun org-element-context ()
4225 "Return closest element or object around point.
4227 Return value is a list like (TYPE PROPS) where TYPE is the type
4228 of the element or object and PROPS a plist of properties
4229 associated to it.
4231 Possible types are defined in `org-element-all-elements' and
4232 `org-element-all-objects'. Properties depend on element or
4233 object type, but always include :begin, :end, :parent
4234 and :post-blank properties."
4235 (org-with-wide-buffer
4236 (let* ((origin (point))
4237 (element (org-element-at-point))
4238 (type (car element))
4239 end)
4240 ;; Check if point is inside an element containing objects or at
4241 ;; a secondary string. In that case, move to beginning of the
4242 ;; element or secondary string and set END to the other side.
4243 (if (not (or (and (eq type 'item)
4244 (let ((tag (org-element-property :tag element)))
4245 (and tag
4246 (progn
4247 (beginning-of-line)
4248 (search-forward tag (point-at-eol))
4249 (goto-char (match-beginning 0))
4250 (and (>= origin (point))
4251 (<= origin
4252 ;; `1+' is required so some
4253 ;; successors can match
4254 ;; properly their object.
4255 (setq end (1+ (match-end 0)))))))))
4256 (and (memq type '(headline inlinetask))
4257 (progn (beginning-of-line)
4258 (skip-chars-forward "* ")
4259 (setq end (point-at-eol))))
4260 (and (memq type '(paragraph table-row verse-block))
4261 (let ((cbeg (org-element-property
4262 :contents-begin element))
4263 (cend (org-element-property
4264 :contents-end element)))
4265 (and (>= origin cbeg)
4266 (<= origin cend)
4267 (progn (goto-char cbeg) (setq end cend)))))))
4268 element
4269 (let ((restriction (org-element-restriction element))
4270 (parent element)
4271 candidates)
4272 (catch 'exit
4273 (while (setq candidates (org-element--get-next-object-candidates
4274 end restriction candidates))
4275 (let ((closest-cand (rassq (apply 'min (mapcar 'cdr candidates))
4276 candidates)))
4277 ;; If ORIGIN is before next object in element, there's
4278 ;; no point in looking further.
4279 (if (> (cdr closest-cand) origin) (throw 'exit parent)
4280 (let* ((object
4281 (progn (goto-char (cdr closest-cand))
4282 (funcall (intern (format "org-element-%s-parser"
4283 (car closest-cand))))))
4284 (cbeg (org-element-property :contents-begin object))
4285 (cend (org-element-property :contents-end object)))
4286 (cond
4287 ;; ORIGIN is after OBJECT, so skip it.
4288 ((< (org-element-property :end object) origin)
4289 (goto-char (org-element-property :end object)))
4290 ;; ORIGIN is within a non-recursive object or at an
4291 ;; object boundaries: Return that object.
4292 ((or (not cbeg) (> cbeg origin) (< cend origin))
4293 (throw 'exit
4294 (org-element-put-property object :parent parent)))
4295 ;; Otherwise, move within current object and restrict
4296 ;; search to the end of its contents.
4297 (t (goto-char cbeg)
4298 (org-element-put-property object :parent parent)
4299 (setq parent object
4300 restriction (org-element-restriction object)
4301 end cend)))))))
4302 parent))))))
4304 (defsubst org-element-nested-p (elem-A elem-B)
4305 "Non-nil when elements ELEM-A and ELEM-B are nested."
4306 (let ((beg-A (org-element-property :begin elem-A))
4307 (beg-B (org-element-property :begin elem-B))
4308 (end-A (org-element-property :end elem-A))
4309 (end-B (org-element-property :end elem-B)))
4310 (or (and (>= beg-A beg-B) (<= end-A end-B))
4311 (and (>= beg-B beg-A) (<= end-B end-A)))))
4313 (defun org-element-swap-A-B (elem-A elem-B)
4314 "Swap elements ELEM-A and ELEM-B.
4315 Assume ELEM-B is after ELEM-A in the buffer. Leave point at the
4316 end of ELEM-A."
4317 (goto-char (org-element-property :begin elem-A))
4318 ;; There are two special cases when an element doesn't start at bol:
4319 ;; the first paragraph in an item or in a footnote definition.
4320 (let ((specialp (not (bolp))))
4321 ;; Only a paragraph without any affiliated keyword can be moved at
4322 ;; ELEM-A position in such a situation. Note that the case of
4323 ;; a footnote definition is impossible: it cannot contain two
4324 ;; paragraphs in a row because it cannot contain a blank line.
4325 (if (and specialp
4326 (or (not (eq (org-element-type elem-B) 'paragraph))
4327 (/= (org-element-property :begin elem-B)
4328 (org-element-property :contents-begin elem-B))))
4329 (error "Cannot swap elements"))
4330 ;; In a special situation, ELEM-A will have no indentation. We'll
4331 ;; give it ELEM-B's (which will in, in turn, have no indentation).
4332 (let* ((ind-B (when specialp
4333 (goto-char (org-element-property :begin elem-B))
4334 (org-get-indentation)))
4335 (beg-A (org-element-property :begin elem-A))
4336 (end-A (save-excursion
4337 (goto-char (org-element-property :end elem-A))
4338 (skip-chars-backward " \r\t\n")
4339 (point-at-eol)))
4340 (beg-B (org-element-property :begin elem-B))
4341 (end-B (save-excursion
4342 (goto-char (org-element-property :end elem-B))
4343 (skip-chars-backward " \r\t\n")
4344 (point-at-eol)))
4345 ;; Store overlays responsible for visibility status. We
4346 ;; also need to store their boundaries as they will be
4347 ;; removed from buffer.
4348 (overlays
4349 (cons
4350 (mapcar (lambda (ov) (list ov (overlay-start ov) (overlay-end ov)))
4351 (overlays-in beg-A end-A))
4352 (mapcar (lambda (ov) (list ov (overlay-start ov) (overlay-end ov)))
4353 (overlays-in beg-B end-B))))
4354 ;; Get contents.
4355 (body-A (buffer-substring beg-A end-A))
4356 (body-B (delete-and-extract-region beg-B end-B)))
4357 (goto-char beg-B)
4358 (when specialp
4359 (setq body-B (replace-regexp-in-string "\\`[ \t]*" "" body-B))
4360 (org-indent-to-column ind-B))
4361 (insert body-A)
4362 ;; Restore ex ELEM-A overlays.
4363 (let ((offset (- beg-B beg-A)))
4364 (mapc (lambda (ov)
4365 (move-overlay
4366 (car ov) (+ (nth 1 ov) offset) (+ (nth 2 ov) offset)))
4367 (car overlays))
4368 (goto-char beg-A)
4369 (delete-region beg-A end-A)
4370 (insert body-B)
4371 ;; Restore ex ELEM-B overlays.
4372 (mapc (lambda (ov)
4373 (move-overlay
4374 (car ov) (- (nth 1 ov) offset) (- (nth 2 ov) offset)))
4375 (cdr overlays)))
4376 (goto-char (org-element-property :end elem-B)))))
4378 (provide 'org-element)
4380 ;; Local variables:
4381 ;; generated-autoload-file: "org-loaddefs.el"
4382 ;; End:
4384 ;;; org-element.el ends here