org-element: Fix 1ad58a8230694fefb5a7cb867b9e6c8dbed50cbc
[org-mode.git] / lisp / org-element.el
blob21628b5029f2631c9eddd5759489c47d2dcee998
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 (buffer-substring-no-properties contents-begin contents-end))
1508 (pos-before-blank (progn (goto-char contents-end)
1509 (forward-line)
1510 (point)))
1511 (end (progn (skip-chars-forward " \r\t\n" limit)
1512 (if (eobp) (point) (point-at-bol)))))
1513 (list 'example-block
1514 (nconc
1515 (list :begin begin
1516 :end end
1517 :value value
1518 :switches switches
1519 :number-lines number-lines
1520 :preserve-indent preserve-indent
1521 :retain-labels retain-labels
1522 :use-labels use-labels
1523 :label-fmt label-fmt
1524 :hiddenp hidden
1525 :post-blank (count-lines pos-before-blank end))
1526 (cadr keywords)))))))))
1528 (defun org-element-example-block-interpreter (example-block contents)
1529 "Interpret EXAMPLE-BLOCK element as Org syntax.
1530 CONTENTS is nil."
1531 (let ((switches (org-element-property :switches example-block)))
1532 (concat "#+BEGIN_EXAMPLE" (and switches (concat " " switches)) "\n"
1533 (org-remove-indentation
1534 (org-element-property :value example-block))
1535 "#+END_EXAMPLE")))
1538 ;;;; Export Block
1540 (defun org-element-export-block-parser (limit)
1541 "Parse an export block.
1543 LIMIT bounds the search.
1545 Return a list whose CAR is `export-block' and CDR is a plist
1546 containing `:begin', `:end', `:type', `:hiddenp', `:value' and
1547 `:post-blank' keywords.
1549 Assume point is at export-block beginning."
1550 (let* ((case-fold-search t)
1551 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
1552 (upcase (org-match-string-no-properties 1)))))
1553 (if (not (save-excursion
1554 (re-search-forward
1555 (format "^[ \t]*#\\+END_%s[ \t]*$" type) limit t)))
1556 ;; Incomplete block: parse it as a paragraph.
1557 (org-element-paragraph-parser limit)
1558 (let ((contents-end (match-beginning 0)))
1559 (save-excursion
1560 (let* ((keywords (org-element--collect-affiliated-keywords))
1561 (begin (car keywords))
1562 (contents-begin (progn (forward-line) (point)))
1563 (hidden (org-invisible-p2))
1564 (pos-before-blank (progn (goto-char contents-end)
1565 (forward-line)
1566 (point)))
1567 (end (progn (skip-chars-forward " \r\t\n" limit)
1568 (if (eobp) (point) (point-at-bol))))
1569 (value (buffer-substring-no-properties contents-begin
1570 contents-end)))
1571 (list 'export-block
1572 (nconc
1573 (list :begin begin
1574 :end end
1575 :type type
1576 :value value
1577 :hiddenp hidden
1578 :post-blank (count-lines pos-before-blank end))
1579 (cadr keywords)))))))))
1581 (defun org-element-export-block-interpreter (export-block contents)
1582 "Interpret EXPORT-BLOCK element as Org syntax.
1583 CONTENTS is nil."
1584 (let ((type (org-element-property :type export-block)))
1585 (concat (format "#+BEGIN_%s\n" type)
1586 (org-element-property :value export-block)
1587 (format "#+END_%s" type))))
1590 ;;;; Fixed-width
1592 (defun org-element-fixed-width-parser (limit)
1593 "Parse a fixed-width section.
1595 LIMIT bounds the search.
1597 Return a list whose CAR is `fixed-width' and CDR is a plist
1598 containing `:begin', `:end', `:value' and `:post-blank' keywords.
1600 Assume point is at the beginning of the fixed-width area."
1601 (save-excursion
1602 (let* ((keywords (org-element--collect-affiliated-keywords))
1603 (begin (car keywords))
1604 value
1605 (end-area
1606 (progn
1607 (while (and (< (point) limit)
1608 (looking-at "[ \t]*:\\( \\|$\\)"))
1609 ;; Accumulate text without starting colons.
1610 (setq value
1611 (concat value
1612 (buffer-substring-no-properties
1613 (match-end 0) (point-at-eol))
1614 "\n"))
1615 (forward-line))
1616 (point)))
1617 (end (progn (skip-chars-forward " \r\t\n" limit)
1618 (if (eobp) (point) (point-at-bol)))))
1619 (list 'fixed-width
1620 (nconc
1621 (list :begin begin
1622 :end end
1623 :value value
1624 :post-blank (count-lines end-area end))
1625 (cadr keywords))))))
1627 (defun org-element-fixed-width-interpreter (fixed-width contents)
1628 "Interpret FIXED-WIDTH element as Org syntax.
1629 CONTENTS is nil."
1630 (replace-regexp-in-string
1631 "^" ": " (substring (org-element-property :value fixed-width) 0 -1)))
1634 ;;;; Horizontal Rule
1636 (defun org-element-horizontal-rule-parser (limit)
1637 "Parse an horizontal rule.
1639 LIMIT bounds the search.
1641 Return a list whose CAR is `horizontal-rule' and CDR is a plist
1642 containing `:begin', `:end' and `:post-blank' keywords."
1643 (save-excursion
1644 (let* ((keywords (org-element--collect-affiliated-keywords))
1645 (begin (car keywords))
1646 (post-hr (progn (forward-line) (point)))
1647 (end (progn (skip-chars-forward " \r\t\n" limit)
1648 (if (eobp) (point) (point-at-bol)))))
1649 (list 'horizontal-rule
1650 (nconc
1651 (list :begin begin
1652 :end end
1653 :post-blank (count-lines post-hr end))
1654 (cadr keywords))))))
1656 (defun org-element-horizontal-rule-interpreter (horizontal-rule contents)
1657 "Interpret HORIZONTAL-RULE element as Org syntax.
1658 CONTENTS is nil."
1659 "-----")
1662 ;;;; Keyword
1664 (defun org-element-keyword-parser (limit)
1665 "Parse a keyword at point.
1667 LIMIT bounds the search.
1669 Return a list whose CAR is `keyword' and CDR is a plist
1670 containing `:key', `:value', `:begin', `:end' and `:post-blank'
1671 keywords."
1672 (save-excursion
1673 (let* ((case-fold-search t)
1674 (begin (point))
1675 (key (progn (looking-at "[ \t]*#\\+\\(\\S-+\\):")
1676 (upcase (org-match-string-no-properties 1))))
1677 (value (org-trim (buffer-substring-no-properties
1678 (match-end 0) (point-at-eol))))
1679 (pos-before-blank (progn (forward-line) (point)))
1680 (end (progn (skip-chars-forward " \r\t\n" limit)
1681 (if (eobp) (point) (point-at-bol)))))
1682 (list 'keyword
1683 (list :key key
1684 :value value
1685 :begin begin
1686 :end end
1687 :post-blank (count-lines pos-before-blank end))))))
1689 (defun org-element-keyword-interpreter (keyword contents)
1690 "Interpret KEYWORD element as Org syntax.
1691 CONTENTS is nil."
1692 (format "#+%s: %s"
1693 (org-element-property :key keyword)
1694 (org-element-property :value keyword)))
1697 ;;;; Latex Environment
1699 (defun org-element-latex-environment-parser (limit)
1700 "Parse a LaTeX environment.
1702 LIMIT bounds the search.
1704 Return a list whose CAR is `latex-environment' and CDR is a plist
1705 containing `:begin', `:end', `:value' and `:post-blank'
1706 keywords.
1708 Assume point is at the beginning of the latex environment."
1709 (save-excursion
1710 (let* ((case-fold-search t)
1711 (code-begin (point))
1712 (keywords (org-element--collect-affiliated-keywords))
1713 (begin (car keywords))
1714 (env (progn (looking-at "^[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}")
1715 (regexp-quote (match-string 1))))
1716 (code-end
1717 (progn (re-search-forward
1718 (format "^[ \t]*\\\\end{%s}[ \t]*$" env) limit t)
1719 (forward-line)
1720 (point)))
1721 (value (buffer-substring-no-properties code-begin code-end))
1722 (end (progn (skip-chars-forward " \r\t\n" limit)
1723 (if (eobp) (point) (point-at-bol)))))
1724 (list 'latex-environment
1725 (nconc
1726 (list :begin begin
1727 :end end
1728 :value value
1729 :post-blank (count-lines code-end end))
1730 (cadr keywords))))))
1732 (defun org-element-latex-environment-interpreter (latex-environment contents)
1733 "Interpret LATEX-ENVIRONMENT element as Org syntax.
1734 CONTENTS is nil."
1735 (org-element-property :value latex-environment))
1738 ;;;; Paragraph
1740 (defun org-element-paragraph-parser (limit)
1741 "Parse a paragraph.
1743 LIMIT bounds the search.
1745 Return a list whose CAR is `paragraph' and CDR is a plist
1746 containing `:begin', `:end', `:contents-begin' and
1747 `:contents-end' and `:post-blank' keywords.
1749 Assume point is at the beginning of the paragraph."
1750 (save-excursion
1751 (let* ((contents-begin (point))
1752 ;; INNER-PAR-P is non-nil when paragraph is at the
1753 ;; beginning of an item or a footnote reference. In that
1754 ;; case, we mustn't look for affiliated keywords since they
1755 ;; belong to the container.
1756 (inner-par-p (not (bolp)))
1757 (keywords (unless inner-par-p
1758 (org-element--collect-affiliated-keywords)))
1759 (begin (if inner-par-p contents-begin (car keywords)))
1760 (before-blank
1761 (let ((case-fold-search t))
1762 (end-of-line)
1763 (if (not (re-search-forward
1764 org-element-paragraph-separate limit 'm))
1765 limit
1766 ;; A matching `org-element-paragraph-separate' is not
1767 ;; necessarily the end of the paragraph. In
1768 ;; particular, lines starting with # or : as a first
1769 ;; non-space character are ambiguous. We have check
1770 ;; if they are valid Org syntax (i.e. not an
1771 ;; incomplete keyword).
1772 (beginning-of-line)
1773 (while (not
1775 ;; There's no ambiguity for other symbols or
1776 ;; empty lines: stop here.
1777 (looking-at "[ \t]*\\(?:[^:#]\\|$\\)")
1778 ;; Stop at valid fixed-width areas.
1779 (looking-at "[ \t]*:\\(?: \\|$\\)")
1780 ;; Stop at drawers.
1781 (and (looking-at org-drawer-regexp)
1782 (save-excursion
1783 (re-search-forward
1784 "^[ \t]*:END:[ \t]*$" limit t)))
1785 ;; Stop at valid comments.
1786 (looking-at "[ \t]*#\\(?: \\|$\\)")
1787 ;; Stop at valid dynamic blocks.
1788 (and (looking-at org-dblock-start-re)
1789 (save-excursion
1790 (re-search-forward
1791 "^[ \t]*#\\+END:?[ \t]*$" limit t)))
1792 ;; Stop at valid blocks.
1793 (and (looking-at
1794 "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
1795 (save-excursion
1796 (re-search-forward
1797 (format "^[ \t]*#\\+END_%s[ \t]*$"
1798 (match-string 1))
1799 limit t)))
1800 ;; Stop at valid latex environments.
1801 (and (looking-at
1802 "^[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}[ \t]*$")
1803 (save-excursion
1804 (re-search-forward
1805 (format "^[ \t]*\\\\end{%s}[ \t]*$"
1806 (match-string 1))
1807 limit t)))
1808 ;; Stop at valid keywords.
1809 (looking-at "[ \t]*#\\+\\S-+:")
1810 ;; Skip everything else.
1811 (not
1812 (progn
1813 (end-of-line)
1814 (re-search-forward org-element-paragraph-separate
1815 limit 'm)))))
1816 (beginning-of-line)))
1817 (if (= (point) limit) limit
1818 (goto-char (line-beginning-position)))))
1819 (contents-end (progn (skip-chars-backward " \r\t\n" contents-begin)
1820 (forward-line)
1821 (point)))
1822 (end (progn (skip-chars-forward " \r\t\n" limit)
1823 (if (eobp) (point) (point-at-bol)))))
1824 (list 'paragraph
1825 (nconc
1826 (list :begin begin
1827 :end end
1828 :contents-begin contents-begin
1829 :contents-end contents-end
1830 :post-blank (count-lines before-blank end))
1831 (cadr keywords))))))
1833 (defun org-element-paragraph-interpreter (paragraph contents)
1834 "Interpret PARAGRAPH element as Org syntax.
1835 CONTENTS is the contents of the element."
1836 contents)
1839 ;;;; Planning
1841 (defun org-element-planning-parser (limit)
1842 "Parse a planning.
1844 LIMIT bounds the search.
1846 Return a list whose CAR is `planning' and CDR is a plist
1847 containing `:closed', `:deadline', `:scheduled', `:begin', `:end'
1848 and `:post-blank' keywords."
1849 (save-excursion
1850 (let* ((case-fold-search nil)
1851 (begin (point))
1852 (post-blank (let ((before-blank (progn (forward-line) (point))))
1853 (skip-chars-forward " \r\t\n" limit)
1854 (unless (eobp) (beginning-of-line))
1855 (count-lines before-blank (point))))
1856 (end (point))
1857 closed deadline scheduled)
1858 (goto-char begin)
1859 (while (re-search-forward org-keyword-time-not-clock-regexp
1860 (line-end-position) t)
1861 (goto-char (match-end 1))
1862 (org-skip-whitespace)
1863 (let ((time (buffer-substring-no-properties
1864 (1+ (point)) (1- (match-end 0))))
1865 (keyword (match-string 1)))
1866 (cond ((equal keyword org-closed-string) (setq closed time))
1867 ((equal keyword org-deadline-string) (setq deadline time))
1868 (t (setq scheduled time)))))
1869 (list 'planning
1870 (list :closed closed
1871 :deadline deadline
1872 :scheduled scheduled
1873 :begin begin
1874 :end end
1875 :post-blank post-blank)))))
1877 (defun org-element-planning-interpreter (planning contents)
1878 "Interpret PLANNING element as Org syntax.
1879 CONTENTS is nil."
1880 (mapconcat
1881 'identity
1882 (delq nil
1883 (list (let ((closed (org-element-property :closed planning)))
1884 (when closed (concat org-closed-string " [" closed "]")))
1885 (let ((deadline (org-element-property :deadline planning)))
1886 (when deadline (concat org-deadline-string " <" deadline ">")))
1887 (let ((scheduled (org-element-property :scheduled planning)))
1888 (when scheduled
1889 (concat org-scheduled-string " <" scheduled ">")))))
1890 " "))
1893 ;;;; Property Drawer
1895 (defun org-element-property-drawer-parser (limit)
1896 "Parse a property drawer.
1898 LIMIT bounds the search.
1900 Return a list whose CAR is `property-drawer' and CDR is a plist
1901 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1902 `:contents-end', `:properties' and `:post-blank' keywords.
1904 Assume point is at the beginning of the property drawer."
1905 (save-excursion
1906 (let ((case-fold-search t)
1907 (begin (point))
1908 (prop-begin (progn (forward-line) (point)))
1909 (hidden (org-invisible-p2))
1910 (properties
1911 (let (val)
1912 (while (not (looking-at "^[ \t]*:END:[ \t]*$"))
1913 (when (looking-at "[ \t]*:\\([A-Za-z][-_A-Za-z0-9]*\\):")
1914 (push (cons (org-match-string-no-properties 1)
1915 (org-trim
1916 (buffer-substring-no-properties
1917 (match-end 0) (point-at-eol))))
1918 val))
1919 (forward-line))
1920 val))
1921 (prop-end (progn (re-search-forward "^[ \t]*:END:" limit t)
1922 (point-at-bol)))
1923 (pos-before-blank (progn (forward-line) (point)))
1924 (end (progn (skip-chars-forward " \r\t\n" limit)
1925 (if (eobp) (point) (point-at-bol)))))
1926 (list 'property-drawer
1927 (list :begin begin
1928 :end end
1929 :hiddenp hidden
1930 :properties properties
1931 :post-blank (count-lines pos-before-blank end))))))
1933 (defun org-element-property-drawer-interpreter (property-drawer contents)
1934 "Interpret PROPERTY-DRAWER element as Org syntax.
1935 CONTENTS is nil."
1936 (let ((props (org-element-property :properties property-drawer)))
1937 (concat
1938 ":PROPERTIES:\n"
1939 (mapconcat (lambda (p)
1940 (format org-property-format (format ":%s:" (car p)) (cdr p)))
1941 (nreverse props) "\n")
1942 "\n:END:")))
1945 ;;;; Quote Section
1947 (defun org-element-quote-section-parser (limit)
1948 "Parse a quote section.
1950 LIMIT bounds the search.
1952 Return a list whose CAR is `quote-section' and CDR is a plist
1953 containing `:begin', `:end', `:value' and `:post-blank' keywords.
1955 Assume point is at beginning of the section."
1956 (save-excursion
1957 (let* ((begin (point))
1958 (end (progn (org-with-limited-levels (outline-next-heading))
1959 (point)))
1960 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
1961 (forward-line)
1962 (point)))
1963 (value (buffer-substring-no-properties begin pos-before-blank)))
1964 (list 'quote-section
1965 (list :begin begin
1966 :end end
1967 :value value
1968 :post-blank (count-lines pos-before-blank end))))))
1970 (defun org-element-quote-section-interpreter (quote-section contents)
1971 "Interpret QUOTE-SECTION element as Org syntax.
1972 CONTENTS is nil."
1973 (org-element-property :value quote-section))
1976 ;;;; Src Block
1978 (defun org-element-src-block-parser (limit)
1979 "Parse a src block.
1981 LIMIT bounds the search.
1983 Return a list whose CAR is `src-block' and CDR is a plist
1984 containing `:language', `:switches', `:parameters', `:begin',
1985 `:end', `:hiddenp', `:number-lines', `:retain-labels',
1986 `:use-labels', `:label-fmt', `:preserve-indent', `:value' and
1987 `:post-blank' keywords.
1989 Assume point is at the beginning of the block."
1990 (let ((case-fold-search t))
1991 (if (not (save-excursion (re-search-forward "^[ \t]*#\\+END_SRC[ \t]*$"
1992 limit t)))
1993 ;; Incomplete block: parse it as a paragraph.
1994 (org-element-paragraph-parser limit)
1995 (let ((contents-end (match-beginning 0)))
1996 (save-excursion
1997 (let* ((keywords (org-element--collect-affiliated-keywords))
1998 ;; Get beginning position.
1999 (begin (car keywords))
2000 ;; Get language as a string.
2001 (language
2002 (progn
2003 (looking-at
2004 (concat "^[ \t]*#\\+BEGIN_SRC"
2005 "\\(?: +\\(\\S-+\\)\\)?"
2006 "\\(\\(?: +\\(?:-l \".*?\"\\|[-+][A-Za-z]\\)\\)+\\)?"
2007 "\\(.*\\)[ \t]*$"))
2008 (org-match-string-no-properties 1)))
2009 ;; Get switches.
2010 (switches (org-match-string-no-properties 2))
2011 ;; Get parameters.
2012 (parameters (org-match-string-no-properties 3))
2013 ;; Switches analysis
2014 (number-lines (cond ((not switches) nil)
2015 ((string-match "-n\\>" switches) 'new)
2016 ((string-match "+n\\>" switches) 'continued)))
2017 (preserve-indent (and switches (string-match "-i\\>" switches)))
2018 (label-fmt (and switches
2019 (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
2020 (match-string 1 switches)))
2021 ;; Should labels be retained in (or stripped from)
2022 ;; src blocks?
2023 (retain-labels
2024 (or (not switches)
2025 (not (string-match "-r\\>" switches))
2026 (and number-lines (string-match "-k\\>" switches))))
2027 ;; What should code-references use - labels or
2028 ;; line-numbers?
2029 (use-labels
2030 (or (not switches)
2031 (and retain-labels (not (string-match "-k\\>" switches)))))
2032 ;; Get visibility status.
2033 (hidden (progn (forward-line) (org-invisible-p2)))
2034 ;; Retrieve code.
2035 (value (buffer-substring-no-properties (point) contents-end))
2036 (pos-before-blank (progn (goto-char contents-end)
2037 (forward-line)
2038 (point)))
2039 ;; Get position after ending blank lines.
2040 (end (progn (skip-chars-forward " \r\t\n" limit)
2041 (if (eobp) (point) (point-at-bol)))))
2042 (list 'src-block
2043 (nconc
2044 (list :language language
2045 :switches (and (org-string-nw-p switches)
2046 (org-trim switches))
2047 :parameters (and (org-string-nw-p parameters)
2048 (org-trim parameters))
2049 :begin begin
2050 :end end
2051 :number-lines number-lines
2052 :preserve-indent preserve-indent
2053 :retain-labels retain-labels
2054 :use-labels use-labels
2055 :label-fmt label-fmt
2056 :hiddenp hidden
2057 :value value
2058 :post-blank (count-lines pos-before-blank end))
2059 (cadr keywords)))))))))
2061 (defun org-element-src-block-interpreter (src-block contents)
2062 "Interpret SRC-BLOCK element as Org syntax.
2063 CONTENTS is nil."
2064 (let ((lang (org-element-property :language src-block))
2065 (switches (org-element-property :switches src-block))
2066 (params (org-element-property :parameters src-block))
2067 (value (let ((val (org-element-property :value src-block)))
2068 (cond
2070 (org-src-preserve-indentation val)
2071 ((zerop org-edit-src-content-indentation)
2072 (org-remove-indentation val))
2074 (let ((ind (make-string
2075 org-edit-src-content-indentation 32)))
2076 (replace-regexp-in-string
2077 "\\(^\\)[ \t]*\\S-" ind
2078 (org-remove-indentation val) nil nil 1)))))))
2079 (concat (format "#+BEGIN_SRC%s\n"
2080 (concat (and lang (concat " " lang))
2081 (and switches (concat " " switches))
2082 (and params (concat " " params))))
2083 value
2084 "#+END_SRC")))
2087 ;;;; Table
2089 (defun org-element-table-parser (limit)
2090 "Parse a table at point.
2092 LIMIT bounds the search.
2094 Return a list whose CAR is `table' and CDR is a plist containing
2095 `:begin', `:end', `:tblfm', `:type', `:contents-begin',
2096 `:contents-end', `:value' and `:post-blank' keywords.
2098 Assume point is at the beginning of the table."
2099 (save-excursion
2100 (let* ((case-fold-search t)
2101 (table-begin (point))
2102 (type (if (org-at-table.el-p) 'table.el 'org))
2103 (keywords (org-element--collect-affiliated-keywords))
2104 (begin (car keywords))
2105 (table-end (goto-char (marker-position (org-table-end t))))
2106 (tblfm (let (acc)
2107 (while (looking-at "[ \t]*#\\+TBLFM: +\\(.*\\)[ \t]*$")
2108 (push (org-match-string-no-properties 1) acc)
2109 (forward-line))
2110 acc))
2111 (pos-before-blank (point))
2112 (end (progn (skip-chars-forward " \r\t\n" limit)
2113 (if (eobp) (point) (point-at-bol)))))
2114 (list 'table
2115 (nconc
2116 (list :begin begin
2117 :end end
2118 :type type
2119 :tblfm tblfm
2120 ;; Only `org' tables have contents. `table.el' tables
2121 ;; use a `:value' property to store raw table as
2122 ;; a string.
2123 :contents-begin (and (eq type 'org) table-begin)
2124 :contents-end (and (eq type 'org) table-end)
2125 :value (and (eq type 'table.el)
2126 (buffer-substring-no-properties
2127 table-begin table-end))
2128 :post-blank (count-lines pos-before-blank end))
2129 (cadr keywords))))))
2131 (defun org-element-table-interpreter (table contents)
2132 "Interpret TABLE element as Org syntax.
2133 CONTENTS is nil."
2134 (if (eq (org-element-property :type table) 'table.el)
2135 (org-remove-indentation (org-element-property :value table))
2136 (concat (with-temp-buffer (insert contents)
2137 (org-table-align)
2138 (buffer-string))
2139 (mapconcat (lambda (fm) (concat "#+TBLFM: " fm))
2140 (reverse (org-element-property :tblfm table))
2141 "\n"))))
2144 ;;;; Table Row
2146 (defun org-element-table-row-parser (limit)
2147 "Parse table row at point.
2149 LIMIT bounds the search.
2151 Return a list whose CAR is `table-row' and CDR is a plist
2152 containing `:begin', `:end', `:contents-begin', `:contents-end',
2153 `:type' and `:post-blank' keywords."
2154 (save-excursion
2155 (let* ((type (if (looking-at "^[ \t]*|-") 'rule 'standard))
2156 (begin (point))
2157 ;; A table rule has no contents. In that case, ensure
2158 ;; CONTENTS-BEGIN matches CONTENTS-END.
2159 (contents-begin (and (eq type 'standard)
2160 (search-forward "|")
2161 (point)))
2162 (contents-end (and (eq type 'standard)
2163 (progn
2164 (end-of-line)
2165 (skip-chars-backward " \t")
2166 (point))))
2167 (end (progn (forward-line) (point))))
2168 (list 'table-row
2169 (list :type type
2170 :begin begin
2171 :end end
2172 :contents-begin contents-begin
2173 :contents-end contents-end
2174 :post-blank 0)))))
2176 (defun org-element-table-row-interpreter (table-row contents)
2177 "Interpret TABLE-ROW element as Org syntax.
2178 CONTENTS is the contents of the table row."
2179 (if (eq (org-element-property :type table-row) 'rule) "|-"
2180 (concat "| " contents)))
2183 ;;;; Verse Block
2185 (defun org-element-verse-block-parser (limit)
2186 "Parse a verse block.
2188 LIMIT bounds the search.
2190 Return a list whose CAR is `verse-block' and CDR is a plist
2191 containing `:begin', `:end', `:contents-begin', `:contents-end',
2192 `:hiddenp' and `:post-blank' keywords.
2194 Assume point is at beginning of the block."
2195 (let ((case-fold-search t))
2196 (if (not (save-excursion
2197 (re-search-forward "^[ \t]*#\\+END_VERSE[ \t]*$" limit t)))
2198 ;; Incomplete block: parse it as a paragraph.
2199 (org-element-paragraph-parser limit)
2200 (let ((contents-end (match-beginning 0)))
2201 (save-excursion
2202 (let* ((keywords (org-element--collect-affiliated-keywords))
2203 (begin (car keywords))
2204 (hidden (progn (forward-line) (org-invisible-p2)))
2205 (contents-begin (point))
2206 (pos-before-blank (progn (goto-char contents-end)
2207 (forward-line)
2208 (point)))
2209 (end (progn (skip-chars-forward " \r\t\n" limit)
2210 (if (eobp) (point) (point-at-bol)))))
2211 (list 'verse-block
2212 (nconc
2213 (list :begin begin
2214 :end end
2215 :contents-begin contents-begin
2216 :contents-end contents-end
2217 :hiddenp hidden
2218 :post-blank (count-lines pos-before-blank end))
2219 (cadr keywords)))))))))
2221 (defun org-element-verse-block-interpreter (verse-block contents)
2222 "Interpret VERSE-BLOCK element as Org syntax.
2223 CONTENTS is verse block contents."
2224 (format "#+BEGIN_VERSE\n%s#+END_VERSE" contents))
2228 ;;; Objects
2230 ;; Unlike to elements, interstices can be found between objects.
2231 ;; That's why, along with the parser, successor functions are provided
2232 ;; for each object. Some objects share the same successor (i.e. `code'
2233 ;; and `verbatim' objects).
2235 ;; A successor must accept a single argument bounding the search. It
2236 ;; will return either a cons cell whose CAR is the object's type, as
2237 ;; a symbol, and CDR the position of its next occurrence, or nil.
2239 ;; Successors follow the naming convention:
2240 ;; org-element-NAME-successor, where NAME is the name of the
2241 ;; successor, as defined in `org-element-all-successors'.
2243 ;; Some object types (i.e. `italic') are recursive. Restrictions on
2244 ;; object types they can contain will be specified in
2245 ;; `org-element-object-restrictions'.
2247 ;; Adding a new type of object is simple. Implement a successor,
2248 ;; a parser, and an interpreter for it, all following the naming
2249 ;; convention. Register type in `org-element-all-objects' and
2250 ;; successor in `org-element-all-successors'. Maybe tweak
2251 ;; restrictions about it, and that's it.
2254 ;;;; Bold
2256 (defun org-element-bold-parser ()
2257 "Parse bold object at point.
2259 Return a list whose CAR is `bold' and CDR is a plist with
2260 `:begin', `:end', `:contents-begin' and `:contents-end' and
2261 `:post-blank' keywords.
2263 Assume point is at the first star marker."
2264 (save-excursion
2265 (unless (bolp) (backward-char 1))
2266 (looking-at org-emph-re)
2267 (let ((begin (match-beginning 2))
2268 (contents-begin (match-beginning 4))
2269 (contents-end (match-end 4))
2270 (post-blank (progn (goto-char (match-end 2))
2271 (skip-chars-forward " \t")))
2272 (end (point)))
2273 (list 'bold
2274 (list :begin begin
2275 :end end
2276 :contents-begin contents-begin
2277 :contents-end contents-end
2278 :post-blank post-blank)))))
2280 (defun org-element-bold-interpreter (bold contents)
2281 "Interpret BOLD object as Org syntax.
2282 CONTENTS is the contents of the object."
2283 (format "*%s*" contents))
2285 (defun org-element-text-markup-successor (limit)
2286 "Search for the next text-markup object.
2288 LIMIT bounds the search.
2290 Return value is a cons cell whose CAR is a symbol among `bold',
2291 `italic', `underline', `strike-through', `code' and `verbatim'
2292 and CDR is beginning position."
2293 (save-excursion
2294 (unless (bolp) (backward-char))
2295 (when (re-search-forward org-emph-re limit t)
2296 (let ((marker (match-string 3)))
2297 (cons (cond
2298 ((equal marker "*") 'bold)
2299 ((equal marker "/") 'italic)
2300 ((equal marker "_") 'underline)
2301 ((equal marker "+") 'strike-through)
2302 ((equal marker "~") 'code)
2303 ((equal marker "=") 'verbatim)
2304 (t (error "Unknown marker at %d" (match-beginning 3))))
2305 (match-beginning 2))))))
2308 ;;;; Code
2310 (defun org-element-code-parser ()
2311 "Parse code object at point.
2313 Return a list whose CAR is `code' and CDR is a plist with
2314 `:value', `:begin', `:end' and `:post-blank' keywords.
2316 Assume point is at the first tilde marker."
2317 (save-excursion
2318 (unless (bolp) (backward-char 1))
2319 (looking-at org-emph-re)
2320 (let ((begin (match-beginning 2))
2321 (value (org-match-string-no-properties 4))
2322 (post-blank (progn (goto-char (match-end 2))
2323 (skip-chars-forward " \t")))
2324 (end (point)))
2325 (list 'code
2326 (list :value value
2327 :begin begin
2328 :end end
2329 :post-blank post-blank)))))
2331 (defun org-element-code-interpreter (code contents)
2332 "Interpret CODE object as Org syntax.
2333 CONTENTS is nil."
2334 (format "~%s~" (org-element-property :value code)))
2337 ;;;; Entity
2339 (defun org-element-entity-parser ()
2340 "Parse entity at point.
2342 Return a list whose CAR is `entity' and CDR a plist with
2343 `:begin', `:end', `:latex', `:latex-math-p', `:html', `:latin1',
2344 `:utf-8', `:ascii', `:use-brackets-p' and `:post-blank' as
2345 keywords.
2347 Assume point is at the beginning of the entity."
2348 (save-excursion
2349 (looking-at "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)")
2350 (let* ((value (org-entity-get (match-string 1)))
2351 (begin (match-beginning 0))
2352 (bracketsp (string= (match-string 2) "{}"))
2353 (post-blank (progn (goto-char (match-end 1))
2354 (when bracketsp (forward-char 2))
2355 (skip-chars-forward " \t")))
2356 (end (point)))
2357 (list 'entity
2358 (list :name (car value)
2359 :latex (nth 1 value)
2360 :latex-math-p (nth 2 value)
2361 :html (nth 3 value)
2362 :ascii (nth 4 value)
2363 :latin1 (nth 5 value)
2364 :utf-8 (nth 6 value)
2365 :begin begin
2366 :end end
2367 :use-brackets-p bracketsp
2368 :post-blank post-blank)))))
2370 (defun org-element-entity-interpreter (entity contents)
2371 "Interpret ENTITY object as Org syntax.
2372 CONTENTS is nil."
2373 (concat "\\"
2374 (org-element-property :name entity)
2375 (when (org-element-property :use-brackets-p entity) "{}")))
2377 (defun org-element-latex-or-entity-successor (limit)
2378 "Search for the next latex-fragment or entity object.
2380 LIMIT bounds the search.
2382 Return value is a cons cell whose CAR is `entity' or
2383 `latex-fragment' and CDR is beginning position."
2384 (save-excursion
2385 (let ((matchers
2386 (remove "begin" (plist-get org-format-latex-options :matchers)))
2387 ;; ENTITY-RE matches both LaTeX commands and Org entities.
2388 (entity-re
2389 "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))
2390 (when (re-search-forward
2391 (concat (mapconcat (lambda (e) (nth 1 (assoc e org-latex-regexps)))
2392 matchers "\\|")
2393 "\\|" entity-re)
2394 limit t)
2395 (goto-char (match-beginning 0))
2396 (if (looking-at entity-re)
2397 ;; Determine if it's a real entity or a LaTeX command.
2398 (cons (if (org-entity-get (match-string 1)) 'entity 'latex-fragment)
2399 (match-beginning 0))
2400 ;; No entity nor command: point is at a LaTeX fragment.
2401 ;; Determine its type to get the correct beginning position.
2402 (cons 'latex-fragment
2403 (catch 'return
2404 (mapc (lambda (e)
2405 (when (looking-at (nth 1 (assoc e org-latex-regexps)))
2406 (throw 'return
2407 (match-beginning
2408 (nth 2 (assoc e org-latex-regexps))))))
2409 matchers)
2410 (point))))))))
2413 ;;;; Export Snippet
2415 (defun org-element-export-snippet-parser ()
2416 "Parse export snippet at point.
2418 Return a list whose CAR is `export-snippet' and CDR a plist with
2419 `:begin', `:end', `:back-end', `:value' and `:post-blank' as
2420 keywords.
2422 Assume point is at the beginning of the snippet."
2423 (save-excursion
2424 (re-search-forward "@@\\([-A-Za-z0-9]+\\):" nil t)
2425 (let* ((begin (match-beginning 0))
2426 (back-end (org-match-string-no-properties 1))
2427 (value (buffer-substring-no-properties
2428 (point)
2429 (progn (re-search-forward "@@" nil t) (match-beginning 0))))
2430 (post-blank (skip-chars-forward " \t"))
2431 (end (point)))
2432 (list 'export-snippet
2433 (list :back-end back-end
2434 :value value
2435 :begin begin
2436 :end end
2437 :post-blank post-blank)))))
2439 (defun org-element-export-snippet-interpreter (export-snippet contents)
2440 "Interpret EXPORT-SNIPPET object as Org syntax.
2441 CONTENTS is nil."
2442 (format "@@%s:%s@@"
2443 (org-element-property :back-end export-snippet)
2444 (org-element-property :value export-snippet)))
2446 (defun org-element-export-snippet-successor (limit)
2447 "Search for the next export-snippet object.
2449 LIMIT bounds the search.
2451 Return value is a cons cell whose CAR is `export-snippet' and CDR
2452 its beginning position."
2453 (save-excursion
2454 (let (beg)
2455 (when (and (re-search-forward "@@[-A-Za-z0-9]+:" limit t)
2456 (setq beg (match-beginning 0))
2457 (search-forward "@@" limit t))
2458 (cons 'export-snippet beg)))))
2461 ;;;; Footnote Reference
2463 (defun org-element-footnote-reference-parser ()
2464 "Parse footnote reference at point.
2466 Return a list whose CAR is `footnote-reference' and CDR a plist
2467 with `:label', `:type', `:inline-definition', `:begin', `:end'
2468 and `:post-blank' as keywords."
2469 (save-excursion
2470 (looking-at org-footnote-re)
2471 (let* ((begin (point))
2472 (label (or (org-match-string-no-properties 2)
2473 (org-match-string-no-properties 3)
2474 (and (match-string 1)
2475 (concat "fn:" (org-match-string-no-properties 1)))))
2476 (type (if (or (not label) (match-string 1)) 'inline 'standard))
2477 (inner-begin (match-end 0))
2478 (inner-end
2479 (let ((count 1))
2480 (forward-char)
2481 (while (and (> count 0) (re-search-forward "[][]" nil t))
2482 (if (equal (match-string 0) "[") (incf count) (decf count)))
2483 (1- (point))))
2484 (post-blank (progn (goto-char (1+ inner-end))
2485 (skip-chars-forward " \t")))
2486 (end (point))
2487 (footnote-reference
2488 (list 'footnote-reference
2489 (list :label label
2490 :type type
2491 :begin begin
2492 :end end
2493 :post-blank post-blank))))
2494 (org-element-put-property
2495 footnote-reference :inline-definition
2496 (and (eq type 'inline)
2497 (org-element-parse-secondary-string
2498 (buffer-substring inner-begin inner-end)
2499 (org-element-restriction 'footnote-reference)
2500 footnote-reference))))))
2502 (defun org-element-footnote-reference-interpreter (footnote-reference contents)
2503 "Interpret FOOTNOTE-REFERENCE object as Org syntax.
2504 CONTENTS is nil."
2505 (let ((label (or (org-element-property :label footnote-reference) "fn:"))
2506 (def
2507 (let ((inline-def
2508 (org-element-property :inline-definition footnote-reference)))
2509 (if (not inline-def) ""
2510 (concat ":" (org-element-interpret-data inline-def))))))
2511 (format "[%s]" (concat label def))))
2513 (defun org-element-footnote-reference-successor (limit)
2514 "Search for the next footnote-reference object.
2516 LIMIT bounds the search.
2518 Return value is a cons cell whose CAR is `footnote-reference' and
2519 CDR is beginning position."
2520 (save-excursion
2521 (catch 'exit
2522 (while (re-search-forward org-footnote-re limit t)
2523 (save-excursion
2524 (let ((beg (match-beginning 0))
2525 (count 1))
2526 (backward-char)
2527 (while (re-search-forward "[][]" limit t)
2528 (if (equal (match-string 0) "[") (incf count) (decf count))
2529 (when (zerop count)
2530 (throw 'exit (cons 'footnote-reference beg))))))))))
2533 ;;;; Inline Babel Call
2535 (defun org-element-inline-babel-call-parser ()
2536 "Parse inline babel call at point.
2538 Return a list whose CAR is `inline-babel-call' and CDR a plist
2539 with `:begin', `:end', `:info' and `:post-blank' as keywords.
2541 Assume point is at the beginning of the babel call."
2542 (save-excursion
2543 (unless (bolp) (backward-char))
2544 (looking-at org-babel-inline-lob-one-liner-regexp)
2545 (let ((info (save-match-data (org-babel-lob-get-info)))
2546 (begin (match-end 1))
2547 (post-blank (progn (goto-char (match-end 0))
2548 (skip-chars-forward " \t")))
2549 (end (point)))
2550 (list 'inline-babel-call
2551 (list :begin begin
2552 :end end
2553 :info info
2554 :post-blank post-blank)))))
2556 (defun org-element-inline-babel-call-interpreter (inline-babel-call contents)
2557 "Interpret INLINE-BABEL-CALL object as Org syntax.
2558 CONTENTS is nil."
2559 (let* ((babel-info (org-element-property :info inline-babel-call))
2560 (main-source (car babel-info))
2561 (post-options (nth 1 babel-info)))
2562 (concat "call_"
2563 (if (string-match "\\[\\(\\[.*?\\]\\)\\]" main-source)
2564 ;; Remove redundant square brackets.
2565 (replace-match
2566 (match-string 1 main-source) nil nil main-source)
2567 main-source)
2568 (and post-options (format "[%s]" post-options)))))
2570 (defun org-element-inline-babel-call-successor (limit)
2571 "Search for the next inline-babel-call object.
2573 LIMIT bounds the search.
2575 Return value is a cons cell whose CAR is `inline-babel-call' and
2576 CDR is beginning position."
2577 (save-excursion
2578 ;; Use a simplified version of
2579 ;; `org-babel-inline-lob-one-liner-regexp'.
2580 (when (re-search-forward
2581 "call_\\([^()\n]+?\\)\\(?:\\[.*?\\]\\)?([^\n]*?)\\(\\[.*?\\]\\)?"
2582 limit t)
2583 (cons 'inline-babel-call (match-beginning 0)))))
2586 ;;;; Inline Src Block
2588 (defun org-element-inline-src-block-parser ()
2589 "Parse inline source block at point.
2591 LIMIT bounds the search.
2593 Return a list whose CAR is `inline-src-block' and CDR a plist
2594 with `:begin', `:end', `:language', `:value', `:parameters' and
2595 `:post-blank' as keywords.
2597 Assume point is at the beginning of the inline src block."
2598 (save-excursion
2599 (unless (bolp) (backward-char))
2600 (looking-at org-babel-inline-src-block-regexp)
2601 (let ((begin (match-beginning 1))
2602 (language (org-match-string-no-properties 2))
2603 (parameters (org-match-string-no-properties 4))
2604 (value (org-match-string-no-properties 5))
2605 (post-blank (progn (goto-char (match-end 0))
2606 (skip-chars-forward " \t")))
2607 (end (point)))
2608 (list 'inline-src-block
2609 (list :language language
2610 :value value
2611 :parameters parameters
2612 :begin begin
2613 :end end
2614 :post-blank post-blank)))))
2616 (defun org-element-inline-src-block-interpreter (inline-src-block contents)
2617 "Interpret INLINE-SRC-BLOCK object as Org syntax.
2618 CONTENTS is nil."
2619 (let ((language (org-element-property :language inline-src-block))
2620 (arguments (org-element-property :parameters inline-src-block))
2621 (body (org-element-property :value inline-src-block)))
2622 (format "src_%s%s{%s}"
2623 language
2624 (if arguments (format "[%s]" arguments) "")
2625 body)))
2627 (defun org-element-inline-src-block-successor (limit)
2628 "Search for the next inline-babel-call element.
2630 LIMIT bounds the search.
2632 Return value is a cons cell whose CAR is `inline-babel-call' and
2633 CDR is beginning position."
2634 (save-excursion
2635 (unless (bolp) (backward-char))
2636 (when (re-search-forward org-babel-inline-src-block-regexp limit t)
2637 (cons 'inline-src-block (match-beginning 1)))))
2639 ;;;; Italic
2641 (defun org-element-italic-parser ()
2642 "Parse italic object at point.
2644 Return a list whose CAR is `italic' and CDR is a plist with
2645 `:begin', `:end', `:contents-begin' and `:contents-end' and
2646 `:post-blank' keywords.
2648 Assume point is at the first slash marker."
2649 (save-excursion
2650 (unless (bolp) (backward-char 1))
2651 (looking-at org-emph-re)
2652 (let ((begin (match-beginning 2))
2653 (contents-begin (match-beginning 4))
2654 (contents-end (match-end 4))
2655 (post-blank (progn (goto-char (match-end 2))
2656 (skip-chars-forward " \t")))
2657 (end (point)))
2658 (list 'italic
2659 (list :begin begin
2660 :end end
2661 :contents-begin contents-begin
2662 :contents-end contents-end
2663 :post-blank post-blank)))))
2665 (defun org-element-italic-interpreter (italic contents)
2666 "Interpret ITALIC object as Org syntax.
2667 CONTENTS is the contents of the object."
2668 (format "/%s/" contents))
2671 ;;;; Latex Fragment
2673 (defun org-element-latex-fragment-parser ()
2674 "Parse latex fragment at point.
2676 Return a list whose CAR is `latex-fragment' and CDR a plist with
2677 `:value', `:begin', `:end', and `:post-blank' as keywords.
2679 Assume point is at the beginning of the latex fragment."
2680 (save-excursion
2681 (let* ((begin (point))
2682 (substring-match
2683 (catch 'exit
2684 (mapc (lambda (e)
2685 (let ((latex-regexp (nth 1 (assoc e org-latex-regexps))))
2686 (when (or (looking-at latex-regexp)
2687 (and (not (bobp))
2688 (save-excursion
2689 (backward-char)
2690 (looking-at latex-regexp))))
2691 (throw 'exit (nth 2 (assoc e org-latex-regexps))))))
2692 (plist-get org-format-latex-options :matchers))
2693 ;; None found: it's a macro.
2694 (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")
2696 (value (match-string-no-properties substring-match))
2697 (post-blank (progn (goto-char (match-end substring-match))
2698 (skip-chars-forward " \t")))
2699 (end (point)))
2700 (list 'latex-fragment
2701 (list :value value
2702 :begin begin
2703 :end end
2704 :post-blank post-blank)))))
2706 (defun org-element-latex-fragment-interpreter (latex-fragment contents)
2707 "Interpret LATEX-FRAGMENT object as Org syntax.
2708 CONTENTS is nil."
2709 (org-element-property :value latex-fragment))
2711 ;;;; Line Break
2713 (defun org-element-line-break-parser ()
2714 "Parse line break at point.
2716 Return a list whose CAR is `line-break', and CDR a plist with
2717 `:begin', `:end' and `:post-blank' keywords.
2719 Assume point is at the beginning of the line break."
2720 (list 'line-break (list :begin (point) :end (point-at-eol) :post-blank 0)))
2722 (defun org-element-line-break-interpreter (line-break contents)
2723 "Interpret LINE-BREAK object as Org syntax.
2724 CONTENTS is nil."
2725 "\\\\")
2727 (defun org-element-line-break-successor (limit)
2728 "Search for the next line-break object.
2730 LIMIT bounds the search.
2732 Return value is a cons cell whose CAR is `line-break' and CDR is
2733 beginning position."
2734 (save-excursion
2735 (let ((beg (and (re-search-forward "[^\\\\]\\(\\\\\\\\\\)[ \t]*$" limit t)
2736 (goto-char (match-beginning 1)))))
2737 ;; A line break can only happen on a non-empty line.
2738 (when (and beg (re-search-backward "\\S-" (point-at-bol) t))
2739 (cons 'line-break beg)))))
2742 ;;;; Link
2744 (defun org-element-link-parser ()
2745 "Parse link at point.
2747 Return a list whose CAR is `link' and CDR a plist with `:type',
2748 `:path', `:raw-link', `:begin', `:end', `:contents-begin',
2749 `:contents-end' and `:post-blank' as keywords.
2751 Assume point is at the beginning of the link."
2752 (save-excursion
2753 (let ((begin (point))
2754 end contents-begin contents-end link-end post-blank path type
2755 raw-link link)
2756 (cond
2757 ;; Type 1: Text targeted from a radio target.
2758 ((and org-target-link-regexp (looking-at org-target-link-regexp))
2759 (setq type "radio"
2760 link-end (match-end 0)
2761 path (org-match-string-no-properties 0)))
2762 ;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]]
2763 ((looking-at org-bracket-link-regexp)
2764 (setq contents-begin (match-beginning 3)
2765 contents-end (match-end 3)
2766 link-end (match-end 0)
2767 ;; RAW-LINK is the original link.
2768 raw-link (org-match-string-no-properties 1)
2769 link (org-translate-link
2770 (org-link-expand-abbrev
2771 (org-link-unescape raw-link))))
2772 ;; Determine TYPE of link and set PATH accordingly.
2773 (cond
2774 ;; File type.
2775 ((or (file-name-absolute-p link) (string-match "^\\.\\.?/" link))
2776 (setq type "file" path link))
2777 ;; Explicit type (http, irc, bbdb...). See `org-link-types'.
2778 ((string-match org-link-re-with-space3 link)
2779 (setq type (match-string 1 link) path (match-string 2 link)))
2780 ;; Id type: PATH is the id.
2781 ((string-match "^id:\\([-a-f0-9]+\\)" link)
2782 (setq type "id" path (match-string 1 link)))
2783 ;; Code-ref type: PATH is the name of the reference.
2784 ((string-match "^(\\(.*\\))$" link)
2785 (setq type "coderef" path (match-string 1 link)))
2786 ;; Custom-id type: PATH is the name of the custom id.
2787 ((= (aref link 0) ?#)
2788 (setq type "custom-id" path (substring link 1)))
2789 ;; Fuzzy type: Internal link either matches a target, an
2790 ;; headline name or nothing. PATH is the target or
2791 ;; headline's name.
2792 (t (setq type "fuzzy" path link))))
2793 ;; Type 3: Plain link, i.e. http://orgmode.org
2794 ((looking-at org-plain-link-re)
2795 (setq raw-link (org-match-string-no-properties 0)
2796 type (org-match-string-no-properties 1)
2797 path (org-match-string-no-properties 2)
2798 link-end (match-end 0)))
2799 ;; Type 4: Angular link, i.e. <http://orgmode.org>
2800 ((looking-at org-angle-link-re)
2801 (setq raw-link (buffer-substring-no-properties
2802 (match-beginning 1) (match-end 2))
2803 type (org-match-string-no-properties 1)
2804 path (org-match-string-no-properties 2)
2805 link-end (match-end 0))))
2806 ;; In any case, deduce end point after trailing white space from
2807 ;; LINK-END variable.
2808 (setq post-blank (progn (goto-char link-end) (skip-chars-forward " \t"))
2809 end (point))
2810 (list 'link
2811 (list :type type
2812 :path path
2813 :raw-link (or raw-link path)
2814 :begin begin
2815 :end end
2816 :contents-begin contents-begin
2817 :contents-end contents-end
2818 :post-blank post-blank)))))
2820 (defun org-element-link-interpreter (link contents)
2821 "Interpret LINK object as Org syntax.
2822 CONTENTS is the contents of the object, or nil."
2823 (let ((type (org-element-property :type link))
2824 (raw-link (org-element-property :raw-link link)))
2825 (if (string= type "radio") raw-link
2826 (format "[[%s]%s]"
2827 raw-link
2828 (if contents (format "[%s]" contents) "")))))
2830 (defun org-element-link-successor (limit)
2831 "Search for the next link object.
2833 LIMIT bounds the search.
2835 Return value is a cons cell whose CAR is `link' and CDR is
2836 beginning position."
2837 (save-excursion
2838 (let ((link-regexp
2839 (if (not org-target-link-regexp) org-any-link-re
2840 (concat org-any-link-re "\\|" org-target-link-regexp))))
2841 (when (re-search-forward link-regexp limit t)
2842 (cons 'link (match-beginning 0))))))
2845 ;;;; Macro
2847 (defun org-element-macro-parser ()
2848 "Parse macro at point.
2850 Return a list whose CAR is `macro' and CDR a plist with `:key',
2851 `:args', `:begin', `:end', `:value' and `:post-blank' as
2852 keywords.
2854 Assume point is at the macro."
2855 (save-excursion
2856 (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
2857 (let ((begin (point))
2858 (key (downcase (org-match-string-no-properties 1)))
2859 (value (org-match-string-no-properties 0))
2860 (post-blank (progn (goto-char (match-end 0))
2861 (skip-chars-forward " \t")))
2862 (end (point))
2863 (args (let ((args (org-match-string-no-properties 3)) args2)
2864 (when args
2865 (setq args (org-split-string args ","))
2866 (while args
2867 (while (string-match "\\\\\\'" (car args))
2868 ;; Repair bad splits.
2869 (setcar (cdr args) (concat (substring (car args) 0 -1)
2870 "," (nth 1 args)))
2871 (pop args))
2872 (push (pop args) args2))
2873 (mapcar 'org-trim (nreverse args2))))))
2874 (list 'macro
2875 (list :key key
2876 :value value
2877 :args args
2878 :begin begin
2879 :end end
2880 :post-blank post-blank)))))
2882 (defun org-element-macro-interpreter (macro contents)
2883 "Interpret MACRO object as Org syntax.
2884 CONTENTS is nil."
2885 (org-element-property :value macro))
2887 (defun org-element-macro-successor (limit)
2888 "Search for the next macro object.
2890 LIMIT bounds the search.
2892 Return value is cons cell whose CAR is `macro' and CDR is
2893 beginning position."
2894 (save-excursion
2895 (when (re-search-forward
2896 "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
2897 limit t)
2898 (cons 'macro (match-beginning 0)))))
2901 ;;;; Radio-target
2903 (defun org-element-radio-target-parser ()
2904 "Parse radio target at point.
2906 Return a list whose CAR is `radio-target' and CDR a plist with
2907 `:begin', `:end', `:contents-begin', `:contents-end', `:value'
2908 and `:post-blank' as keywords.
2910 Assume point is at the radio target."
2911 (save-excursion
2912 (looking-at org-radio-target-regexp)
2913 (let ((begin (point))
2914 (contents-begin (match-beginning 1))
2915 (contents-end (match-end 1))
2916 (value (org-match-string-no-properties 1))
2917 (post-blank (progn (goto-char (match-end 0))
2918 (skip-chars-forward " \t")))
2919 (end (point)))
2920 (list 'radio-target
2921 (list :begin begin
2922 :end end
2923 :contents-begin contents-begin
2924 :contents-end contents-end
2925 :post-blank post-blank
2926 :value value)))))
2928 (defun org-element-radio-target-interpreter (target contents)
2929 "Interpret TARGET object as Org syntax.
2930 CONTENTS is the contents of the object."
2931 (concat "<<<" contents ">>>"))
2933 (defun org-element-radio-target-successor (limit)
2934 "Search for the next radio-target object.
2936 LIMIT bounds the search.
2938 Return value is a cons cell whose CAR is `radio-target' and CDR
2939 is beginning position."
2940 (save-excursion
2941 (when (re-search-forward org-radio-target-regexp limit t)
2942 (cons 'radio-target (match-beginning 0)))))
2945 ;;;; Statistics Cookie
2947 (defun org-element-statistics-cookie-parser ()
2948 "Parse statistics cookie at point.
2950 Return a list whose CAR is `statistics-cookie', and CDR a plist
2951 with `:begin', `:end', `:value' and `:post-blank' keywords.
2953 Assume point is at the beginning of the statistics-cookie."
2954 (save-excursion
2955 (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
2956 (let* ((begin (point))
2957 (value (buffer-substring-no-properties
2958 (match-beginning 0) (match-end 0)))
2959 (post-blank (progn (goto-char (match-end 0))
2960 (skip-chars-forward " \t")))
2961 (end (point)))
2962 (list 'statistics-cookie
2963 (list :begin begin
2964 :end end
2965 :value value
2966 :post-blank post-blank)))))
2968 (defun org-element-statistics-cookie-interpreter (statistics-cookie contents)
2969 "Interpret STATISTICS-COOKIE object as Org syntax.
2970 CONTENTS is nil."
2971 (org-element-property :value statistics-cookie))
2973 (defun org-element-statistics-cookie-successor (limit)
2974 "Search for the next statistics cookie object.
2976 LIMIT bounds the search.
2978 Return value is a cons cell whose CAR is `statistics-cookie' and
2979 CDR is beginning position."
2980 (save-excursion
2981 (when (re-search-forward "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]" limit t)
2982 (cons 'statistics-cookie (match-beginning 0)))))
2985 ;;;; Strike-Through
2987 (defun org-element-strike-through-parser ()
2988 "Parse strike-through object at point.
2990 Return a list whose CAR is `strike-through' and CDR is a plist
2991 with `:begin', `:end', `:contents-begin' and `:contents-end' and
2992 `:post-blank' keywords.
2994 Assume point is at the first plus sign marker."
2995 (save-excursion
2996 (unless (bolp) (backward-char 1))
2997 (looking-at org-emph-re)
2998 (let ((begin (match-beginning 2))
2999 (contents-begin (match-beginning 4))
3000 (contents-end (match-end 4))
3001 (post-blank (progn (goto-char (match-end 2))
3002 (skip-chars-forward " \t")))
3003 (end (point)))
3004 (list 'strike-through
3005 (list :begin begin
3006 :end end
3007 :contents-begin contents-begin
3008 :contents-end contents-end
3009 :post-blank post-blank)))))
3011 (defun org-element-strike-through-interpreter (strike-through contents)
3012 "Interpret STRIKE-THROUGH object as Org syntax.
3013 CONTENTS is the contents of the object."
3014 (format "+%s+" contents))
3017 ;;;; Subscript
3019 (defun org-element-subscript-parser ()
3020 "Parse subscript at point.
3022 Return a list whose CAR is `subscript' and CDR a plist with
3023 `:begin', `:end', `:contents-begin', `:contents-end',
3024 `:use-brackets-p' and `:post-blank' as keywords.
3026 Assume point is at the underscore."
3027 (save-excursion
3028 (unless (bolp) (backward-char))
3029 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp)
3031 (not (looking-at org-match-substring-regexp))))
3032 (begin (match-beginning 2))
3033 (contents-begin (or (match-beginning 5)
3034 (match-beginning 3)))
3035 (contents-end (or (match-end 5) (match-end 3)))
3036 (post-blank (progn (goto-char (match-end 0))
3037 (skip-chars-forward " \t")))
3038 (end (point)))
3039 (list 'subscript
3040 (list :begin begin
3041 :end end
3042 :use-brackets-p bracketsp
3043 :contents-begin contents-begin
3044 :contents-end contents-end
3045 :post-blank post-blank)))))
3047 (defun org-element-subscript-interpreter (subscript contents)
3048 "Interpret SUBSCRIPT object as Org syntax.
3049 CONTENTS is the contents of the object."
3050 (format
3051 (if (org-element-property :use-brackets-p subscript) "_{%s}" "_%s")
3052 contents))
3054 (defun org-element-sub/superscript-successor (limit)
3055 "Search for the next sub/superscript object.
3057 LIMIT bounds the search.
3059 Return value is a cons cell whose CAR is either `subscript' or
3060 `superscript' and CDR is beginning position."
3061 (save-excursion
3062 (when (re-search-forward org-match-substring-regexp limit t)
3063 (cons (if (string= (match-string 2) "_") 'subscript 'superscript)
3064 (match-beginning 2)))))
3067 ;;;; Superscript
3069 (defun org-element-superscript-parser ()
3070 "Parse superscript at point.
3072 Return a list whose CAR is `superscript' and CDR a plist with
3073 `:begin', `:end', `:contents-begin', `:contents-end',
3074 `:use-brackets-p' and `:post-blank' as keywords.
3076 Assume point is at the caret."
3077 (save-excursion
3078 (unless (bolp) (backward-char))
3079 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp) t
3080 (not (looking-at org-match-substring-regexp))))
3081 (begin (match-beginning 2))
3082 (contents-begin (or (match-beginning 5)
3083 (match-beginning 3)))
3084 (contents-end (or (match-end 5) (match-end 3)))
3085 (post-blank (progn (goto-char (match-end 0))
3086 (skip-chars-forward " \t")))
3087 (end (point)))
3088 (list 'superscript
3089 (list :begin begin
3090 :end end
3091 :use-brackets-p bracketsp
3092 :contents-begin contents-begin
3093 :contents-end contents-end
3094 :post-blank post-blank)))))
3096 (defun org-element-superscript-interpreter (superscript contents)
3097 "Interpret SUPERSCRIPT object as Org syntax.
3098 CONTENTS is the contents of the object."
3099 (format
3100 (if (org-element-property :use-brackets-p superscript) "^{%s}" "^%s")
3101 contents))
3104 ;;;; Table Cell
3106 (defun org-element-table-cell-parser ()
3107 "Parse table cell at point.
3109 Return a list whose CAR is `table-cell' and CDR is a plist
3110 containing `:begin', `:end', `:contents-begin', `:contents-end'
3111 and `:post-blank' keywords."
3112 (looking-at "[ \t]*\\(.*?\\)[ \t]*|")
3113 (let* ((begin (match-beginning 0))
3114 (end (match-end 0))
3115 (contents-begin (match-beginning 1))
3116 (contents-end (match-end 1)))
3117 (list 'table-cell
3118 (list :begin begin
3119 :end end
3120 :contents-begin contents-begin
3121 :contents-end contents-end
3122 :post-blank 0))))
3124 (defun org-element-table-cell-interpreter (table-cell contents)
3125 "Interpret TABLE-CELL element as Org syntax.
3126 CONTENTS is the contents of the cell, or nil."
3127 (concat " " contents " |"))
3129 (defun org-element-table-cell-successor (limit)
3130 "Search for the next table-cell object.
3132 LIMIT bounds the search.
3134 Return value is a cons cell whose CAR is `table-cell' and CDR is
3135 beginning position."
3136 (when (looking-at "[ \t]*.*?[ \t]+|") (cons 'table-cell (point))))
3139 ;;;; Target
3141 (defun org-element-target-parser ()
3142 "Parse target at point.
3144 Return a list whose CAR is `target' and CDR a plist with
3145 `:begin', `:end', `:value' and `:post-blank' as keywords.
3147 Assume point is at the target."
3148 (save-excursion
3149 (looking-at org-target-regexp)
3150 (let ((begin (point))
3151 (value (org-match-string-no-properties 1))
3152 (post-blank (progn (goto-char (match-end 0))
3153 (skip-chars-forward " \t")))
3154 (end (point)))
3155 (list 'target
3156 (list :begin begin
3157 :end end
3158 :value value
3159 :post-blank post-blank)))))
3161 (defun org-element-target-interpreter (target contents)
3162 "Interpret TARGET object as Org syntax.
3163 CONTENTS is nil."
3164 (format "<<%s>>" (org-element-property :value target)))
3166 (defun org-element-target-successor (limit)
3167 "Search for the next target object.
3169 LIMIT bounds the search.
3171 Return value is a cons cell whose CAR is `target' and CDR is
3172 beginning position."
3173 (save-excursion
3174 (when (re-search-forward org-target-regexp limit t)
3175 (cons 'target (match-beginning 0)))))
3178 ;;;; Timestamp
3180 (defun org-element-timestamp-parser ()
3181 "Parse time stamp at point.
3183 Return a list whose CAR is `timestamp', and CDR a plist with
3184 `:type', `:begin', `:end', `:value' and `:post-blank' keywords.
3186 Assume point is at the beginning of the timestamp."
3187 (save-excursion
3188 (let* ((begin (point))
3189 (activep (eq (char-after) ?<))
3190 (main-value
3191 (progn
3192 (looking-at "[<[]\\(\\(%%\\)?.*?\\)[]>]\\(?:--[<[]\\(.*?\\)[]>]\\)?")
3193 (match-string-no-properties 1)))
3194 (range-end (match-string-no-properties 3))
3195 (type (cond ((match-string 2) 'diary)
3196 ((and activep range-end) 'active-range)
3197 (activep 'active)
3198 (range-end 'inactive-range)
3199 (t 'inactive)))
3200 (post-blank (progn (goto-char (match-end 0))
3201 (skip-chars-forward " \t")))
3202 (end (point)))
3203 (list 'timestamp
3204 (list :type type
3205 :value main-value
3206 :range-end range-end
3207 :begin begin
3208 :end end
3209 :post-blank post-blank)))))
3211 (defun org-element-timestamp-interpreter (timestamp contents)
3212 "Interpret TIMESTAMP object as Org syntax.
3213 CONTENTS is nil."
3214 (let ((type (org-element-property :type timestamp) ))
3215 (concat
3216 (format (if (memq type '(inactive inactive-range)) "[%s]" "<%s>")
3217 (org-element-property :value timestamp))
3218 (let ((range-end (org-element-property :range-end timestamp)))
3219 (when range-end
3220 (concat "--"
3221 (format (if (eq type 'inactive-range) "[%s]" "<%s>")
3222 range-end)))))))
3224 (defun org-element-timestamp-successor (limit)
3225 "Search for the next timestamp object.
3227 LIMIT bounds the search.
3229 Return value is a cons cell whose CAR is `timestamp' and CDR is
3230 beginning position."
3231 (save-excursion
3232 (when (re-search-forward
3233 (concat org-ts-regexp-both
3234 "\\|"
3235 "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
3236 "\\|"
3237 "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
3238 limit t)
3239 (cons 'timestamp (match-beginning 0)))))
3242 ;;;; Underline
3244 (defun org-element-underline-parser ()
3245 "Parse underline object at point.
3247 Return a list whose CAR is `underline' and CDR is a plist with
3248 `:begin', `:end', `:contents-begin' and `:contents-end' and
3249 `:post-blank' keywords.
3251 Assume point is at the first underscore marker."
3252 (save-excursion
3253 (unless (bolp) (backward-char 1))
3254 (looking-at org-emph-re)
3255 (let ((begin (match-beginning 2))
3256 (contents-begin (match-beginning 4))
3257 (contents-end (match-end 4))
3258 (post-blank (progn (goto-char (match-end 2))
3259 (skip-chars-forward " \t")))
3260 (end (point)))
3261 (list 'underline
3262 (list :begin begin
3263 :end end
3264 :contents-begin contents-begin
3265 :contents-end contents-end
3266 :post-blank post-blank)))))
3268 (defun org-element-underline-interpreter (underline contents)
3269 "Interpret UNDERLINE object as Org syntax.
3270 CONTENTS is the contents of the object."
3271 (format "_%s_" contents))
3274 ;;;; Verbatim
3276 (defun org-element-verbatim-parser ()
3277 "Parse verbatim object at point.
3279 Return a list whose CAR is `verbatim' and CDR is a plist with
3280 `:value', `:begin', `:end' and `:post-blank' keywords.
3282 Assume point is at the first equal sign marker."
3283 (save-excursion
3284 (unless (bolp) (backward-char 1))
3285 (looking-at org-emph-re)
3286 (let ((begin (match-beginning 2))
3287 (value (org-match-string-no-properties 4))
3288 (post-blank (progn (goto-char (match-end 2))
3289 (skip-chars-forward " \t")))
3290 (end (point)))
3291 (list 'verbatim
3292 (list :value value
3293 :begin begin
3294 :end end
3295 :post-blank post-blank)))))
3297 (defun org-element-verbatim-interpreter (verbatim contents)
3298 "Interpret VERBATIM object as Org syntax.
3299 CONTENTS is nil."
3300 (format "=%s=" (org-element-property :value verbatim)))
3304 ;;; Parsing Element Starting At Point
3306 ;; `org-element--current-element' is the core function of this section.
3307 ;; It returns the Lisp representation of the element starting at
3308 ;; point.
3310 ;; `org-element--current-element' makes use of special modes. They
3311 ;; are activated for fixed element chaining (i.e. `plain-list' >
3312 ;; `item') or fixed conditional element chaining (i.e. `headline' >
3313 ;; `section'). Special modes are: `first-section', `section',
3314 ;; `quote-section', `item' and `table-row'.
3316 (defun org-element--current-element
3317 (limit &optional granularity special structure)
3318 "Parse the element starting at point.
3320 LIMIT bounds the search.
3322 Return value is a list like (TYPE PROPS) where TYPE is the type
3323 of the element and PROPS a plist of properties associated to the
3324 element.
3326 Possible types are defined in `org-element-all-elements'.
3328 Optional argument GRANULARITY determines the depth of the
3329 recursion. Allowed values are `headline', `greater-element',
3330 `element', `object' or nil. When it is broader than `object' (or
3331 nil), secondary values will not be parsed, since they only
3332 contain objects.
3334 Optional argument SPECIAL, when non-nil, can be either
3335 `first-section', `section', `quote-section', `table-row' and
3336 `item'.
3338 If STRUCTURE isn't provided but SPECIAL is set to `item', it will
3339 be computed.
3341 This function assumes point is always at the beginning of the
3342 element it has to parse."
3343 (save-excursion
3344 ;; If point is at an affiliated keyword, try moving to the
3345 ;; beginning of the associated element. If none is found, the
3346 ;; keyword is orphaned and will be treated as plain text.
3347 (when (looking-at org-element--affiliated-re)
3348 (let ((opoint (point)))
3349 (while (looking-at org-element--affiliated-re) (forward-line))
3350 (when (looking-at "[ \t]*$") (goto-char opoint))))
3351 (let ((case-fold-search t)
3352 ;; Determine if parsing depth allows for secondary strings
3353 ;; parsing. It only applies to elements referenced in
3354 ;; `org-element-secondary-value-alist'.
3355 (raw-secondary-p (and granularity (not (eq granularity 'object)))))
3356 (cond
3357 ;; Item.
3358 ((eq special 'item)
3359 (org-element-item-parser limit structure raw-secondary-p))
3360 ;; Table Row.
3361 ((eq special 'table-row) (org-element-table-row-parser limit))
3362 ;; Headline.
3363 ((org-with-limited-levels (org-at-heading-p))
3364 (org-element-headline-parser limit raw-secondary-p))
3365 ;; Sections (must be checked after headline).
3366 ((eq special 'section) (org-element-section-parser limit))
3367 ((eq special 'quote-section) (org-element-quote-section-parser limit))
3368 ((eq special 'first-section)
3369 (org-element-section-parser
3370 (or (save-excursion (org-with-limited-levels (outline-next-heading)))
3371 limit)))
3372 ;; When not at bol, point is at the beginning of an item or
3373 ;; a footnote definition: next item is always a paragraph.
3374 ((not (bolp)) (org-element-paragraph-parser limit))
3375 ;; Planning and Clock.
3376 ((and (looking-at org-planning-or-clock-line-re))
3377 (if (equal (match-string 1) org-clock-string)
3378 (org-element-clock-parser limit)
3379 (org-element-planning-parser limit)))
3380 ;; Inlinetask.
3381 ((org-at-heading-p)
3382 (org-element-inlinetask-parser limit raw-secondary-p))
3383 ;; LaTeX Environment.
3384 ((looking-at "[ \t]*\\\\begin{\\([A-Za-z0-9*]+\\)}")
3385 (if (save-excursion
3386 (re-search-forward
3387 (format "[ \t]*\\\\end{%s}[ \t]*"
3388 (regexp-quote (match-string 1)))
3389 nil t))
3390 (org-element-latex-environment-parser limit)
3391 (org-element-paragraph-parser limit)))
3392 ;; Drawer and Property Drawer.
3393 ((looking-at org-drawer-regexp)
3394 (let ((name (match-string 1)))
3395 (cond
3396 ((not (save-excursion
3397 (re-search-forward "^[ \t]*:END:[ \t]*$" nil t)))
3398 (org-element-paragraph-parser limit))
3399 ((equal "PROPERTIES" name)
3400 (org-element-property-drawer-parser limit))
3401 (t (org-element-drawer-parser limit)))))
3402 ;; Fixed Width
3403 ((looking-at "[ \t]*:\\( \\|$\\)")
3404 (org-element-fixed-width-parser limit))
3405 ;; Inline Comments, Blocks, Babel Calls, Dynamic Blocks and
3406 ;; Keywords.
3407 ((looking-at "[ \t]*#")
3408 (goto-char (match-end 0))
3409 (cond ((looking-at "\\(?: \\|$\\)")
3410 (beginning-of-line)
3411 (org-element-comment-parser limit))
3412 ((looking-at "\\+BEGIN_\\(\\S-+\\)")
3413 (beginning-of-line)
3414 (let ((parser (assoc (upcase (match-string 1))
3415 org-element-block-name-alist)))
3416 (if parser (funcall (cdr parser) limit)
3417 (org-element-special-block-parser limit))))
3418 ((looking-at "\\+CALL:")
3419 (beginning-of-line)
3420 (org-element-babel-call-parser limit))
3421 ((looking-at "\\+BEGIN:? ")
3422 (beginning-of-line)
3423 (org-element-dynamic-block-parser limit))
3424 ((looking-at "\\+\\S-+:")
3425 (beginning-of-line)
3426 (org-element-keyword-parser limit))
3428 (beginning-of-line)
3429 (org-element-paragraph-parser limit))))
3430 ;; Footnote Definition.
3431 ((looking-at org-footnote-definition-re)
3432 (org-element-footnote-definition-parser limit))
3433 ;; Horizontal Rule.
3434 ((looking-at "[ \t]*-\\{5,\\}[ \t]*$")
3435 (org-element-horizontal-rule-parser limit))
3436 ;; Table.
3437 ((org-at-table-p t) (org-element-table-parser limit))
3438 ;; List.
3439 ((looking-at (org-item-re))
3440 (org-element-plain-list-parser limit (or structure (org-list-struct))))
3441 ;; Default element: Paragraph.
3442 (t (org-element-paragraph-parser limit))))))
3445 ;; Most elements can have affiliated keywords. When looking for an
3446 ;; element beginning, we want to move before them, as they belong to
3447 ;; that element, and, in the meantime, collect information they give
3448 ;; into appropriate properties. Hence the following function.
3450 ;; Usage of optional arguments may not be obvious at first glance:
3452 ;; - TRANS-LIST is used to polish keywords names that have evolved
3453 ;; during Org history. In example, even though =result= and
3454 ;; =results= coexist, we want to have them under the same =result=
3455 ;; property. It's also true for "srcname" and "name", where the
3456 ;; latter seems to be preferred nowadays (thus the "name" property).
3458 ;; - CONSED allows to regroup multi-lines keywords under the same
3459 ;; property, while preserving their own identity. This is mostly
3460 ;; used for "attr_latex" and al.
3462 ;; - PARSED prepares a keyword value for export. This is useful for
3463 ;; "caption". Objects restrictions for such keywords are defined in
3464 ;; `org-element-object-restrictions'.
3466 ;; - DUALS is used to take care of keywords accepting a main and an
3467 ;; optional secondary values. For example "results" has its
3468 ;; source's name as the main value, and may have an hash string in
3469 ;; optional square brackets as the secondary one.
3471 ;; A keyword may belong to more than one category.
3473 (defun org-element--collect-affiliated-keywords
3474 (&optional key-re trans-list consed parsed duals)
3475 "Collect affiliated keywords before point.
3477 Optional argument KEY-RE is a regexp matching keywords, which
3478 puts matched keyword in group 1. It defaults to
3479 `org-element--affiliated-re'.
3481 TRANS-LIST is an alist where key is the keyword and value the
3482 property name it should be translated to, without the colons. It
3483 defaults to `org-element-keyword-translation-alist'.
3485 CONSED is a list of strings. Any keyword belonging to that list
3486 will have its value consed. The check is done after keyword
3487 translation. It defaults to `org-element-multiple-keywords'.
3489 PARSED is a list of strings. Any keyword member of this list
3490 will have its value parsed. The check is done after keyword
3491 translation. If a keyword is a member of both CONSED and PARSED,
3492 it's value will be a list of parsed strings. It defaults to
3493 `org-element-parsed-keywords'.
3495 DUALS is a list of strings. Any keyword member of this list can
3496 have two parts: one mandatory and one optional. Its value is
3497 a cons cell whose CAR is the former, and the CDR the latter. If
3498 a keyword is a member of both PARSED and DUALS, both values will
3499 be parsed. It defaults to `org-element-dual-keywords'.
3501 Return a list whose CAR is the position at the first of them and
3502 CDR a plist of keywords and values."
3503 (save-excursion
3504 (let ((case-fold-search t)
3505 (key-re (or key-re org-element--affiliated-re))
3506 (trans-list (or trans-list org-element-keyword-translation-alist))
3507 (consed (or consed org-element-multiple-keywords))
3508 (parsed (or parsed org-element-parsed-keywords))
3509 (duals (or duals org-element-dual-keywords))
3510 ;; RESTRICT is the list of objects allowed in parsed
3511 ;; keywords value.
3512 (restrict (org-element-restriction 'keyword))
3513 output)
3514 (unless (bobp)
3515 (while (and (not (bobp)) (progn (forward-line -1) (looking-at key-re)))
3516 (let* ((raw-kwd (upcase (match-string 1)))
3517 ;; Apply translation to RAW-KWD. From there, KWD is
3518 ;; the official keyword.
3519 (kwd (or (cdr (assoc raw-kwd trans-list)) raw-kwd))
3520 ;; Find main value for any keyword.
3521 (value
3522 (save-match-data
3523 (org-trim
3524 (buffer-substring-no-properties
3525 (match-end 0) (point-at-eol)))))
3526 ;; If KWD is a dual keyword, find its secondary
3527 ;; value. Maybe parse it.
3528 (dual-value
3529 (and (member kwd duals)
3530 (let ((sec (org-match-string-no-properties 2)))
3531 (if (or (not sec) (not (member kwd parsed))) sec
3532 (org-element-parse-secondary-string sec restrict)))))
3533 ;; Attribute a property name to KWD.
3534 (kwd-sym (and kwd (intern (concat ":" (downcase kwd))))))
3535 ;; Now set final shape for VALUE.
3536 (when (member kwd parsed)
3537 (setq value (org-element-parse-secondary-string value restrict)))
3538 (when (member kwd duals)
3539 ;; VALUE is mandatory. Set it to nil if there is none.
3540 (setq value (and value (cons value dual-value))))
3541 ;; Attributes are always consed.
3542 (when (or (member kwd consed) (string-match "^ATTR_" kwd))
3543 (setq value (cons value (plist-get output kwd-sym))))
3544 ;; Eventually store the new value in OUTPUT.
3545 (setq output (plist-put output kwd-sym value))))
3546 (unless (looking-at key-re) (forward-line 1)))
3547 (list (point) output))))
3551 ;;; The Org Parser
3553 ;; The two major functions here are `org-element-parse-buffer', which
3554 ;; parses Org syntax inside the current buffer, taking into account
3555 ;; region, narrowing, or even visibility if specified, and
3556 ;; `org-element-parse-secondary-string', which parses objects within
3557 ;; a given string.
3559 ;; The (almost) almighty `org-element-map' allows to apply a function
3560 ;; on elements or objects matching some type, and accumulate the
3561 ;; resulting values. In an export situation, it also skips unneeded
3562 ;; parts of the parse tree.
3564 (defun org-element-parse-buffer (&optional granularity visible-only)
3565 "Recursively parse the buffer and return structure.
3566 If narrowing is in effect, only parse the visible part of the
3567 buffer.
3569 Optional argument GRANULARITY determines the depth of the
3570 recursion. It can be set to the following symbols:
3572 `headline' Only parse headlines.
3573 `greater-element' Don't recurse into greater elements excepted
3574 headlines and sections. Thus, elements
3575 parsed are the top-level ones.
3576 `element' Parse everything but objects and plain text.
3577 `object' Parse the complete buffer (default).
3579 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3580 elements.
3582 Assume buffer is in Org mode."
3583 (save-excursion
3584 (goto-char (point-min))
3585 (org-skip-whitespace)
3586 (org-element--parse-elements
3587 (point-at-bol) (point-max)
3588 ;; Start in `first-section' mode so text before the first
3589 ;; headline belongs to a section.
3590 'first-section nil granularity visible-only (list 'org-data nil))))
3592 (defun org-element-parse-secondary-string (string restriction &optional parent)
3593 "Recursively parse objects in STRING and return structure.
3595 RESTRICTION is a symbol limiting the object types that will be
3596 looked after.
3598 Optional argument PARENT, when non-nil, is the element or object
3599 containing the secondary string. It is used to set correctly
3600 `:parent' property within the string."
3601 (with-temp-buffer
3602 (insert string)
3603 (let ((secondary (org-element--parse-objects
3604 (point-min) (point-max) nil restriction)))
3605 (mapc (lambda (obj) (org-element-put-property obj :parent parent))
3606 secondary))))
3608 (defun org-element-map (data types fun &optional info first-match no-recursion)
3609 "Map a function on selected elements or objects.
3611 DATA is the parsed tree, as returned by, i.e,
3612 `org-element-parse-buffer'. TYPES is a symbol or list of symbols
3613 of elements or objects types. FUN is the function called on the
3614 matching element or object. It must accept one arguments: the
3615 element or object itself.
3617 When optional argument INFO is non-nil, it should be a plist
3618 holding export options. In that case, parts of the parse tree
3619 not exportable according to that property list will be skipped.
3621 When optional argument FIRST-MATCH is non-nil, stop at the first
3622 match for which FUN doesn't return nil, and return that value.
3624 Optional argument NO-RECURSION is a symbol or a list of symbols
3625 representing elements or objects types. `org-element-map' won't
3626 enter any recursive element or object whose type belongs to that
3627 list. Though, FUN can still be applied on them.
3629 Nil values returned from FUN do not appear in the results."
3630 ;; Ensure TYPES and NO-RECURSION are a list, even of one element.
3631 (unless (listp types) (setq types (list types)))
3632 (unless (listp no-recursion) (setq no-recursion (list no-recursion)))
3633 ;; Recursion depth is determined by --CATEGORY.
3634 (let* ((--category
3635 (catch 'found
3636 (let ((category 'greater-elements))
3637 (mapc (lambda (type)
3638 (cond ((or (memq type org-element-all-objects)
3639 (eq type 'plain-text))
3640 ;; If one object is found, the function
3641 ;; has to recurse into every object.
3642 (throw 'found 'objects))
3643 ((not (memq type org-element-greater-elements))
3644 ;; If one regular element is found, the
3645 ;; function has to recurse, at least,
3646 ;; into every element it encounters.
3647 (and (not (eq category 'elements))
3648 (setq category 'elements)))))
3649 types)
3650 category)))
3651 --acc
3652 --walk-tree
3653 (--walk-tree
3654 (function
3655 (lambda (--data)
3656 ;; Recursively walk DATA. INFO, if non-nil, is a plist
3657 ;; holding contextual information.
3658 (let ((--type (org-element-type --data)))
3659 (cond
3660 ((not --data))
3661 ;; Ignored element in an export context.
3662 ((and info (memq --data (plist-get info :ignore-list))))
3663 ;; Secondary string: only objects can be found there.
3664 ((not --type)
3665 (when (eq --category 'objects) (mapc --walk-tree --data)))
3666 ;; Unconditionally enter parse trees.
3667 ((eq --type 'org-data)
3668 (mapc --walk-tree (org-element-contents --data)))
3670 ;; Check if TYPE is matching among TYPES. If so,
3671 ;; apply FUN to --DATA and accumulate return value
3672 ;; into --ACC (or exit if FIRST-MATCH is non-nil).
3673 (when (memq --type types)
3674 (let ((result (funcall fun --data)))
3675 (cond ((not result))
3676 (first-match (throw '--map-first-match result))
3677 (t (push result --acc)))))
3678 ;; If --DATA has a secondary string that can contain
3679 ;; objects with their type among TYPES, look into it.
3680 (when (eq --category 'objects)
3681 (let ((sec-prop
3682 (assq --type org-element-secondary-value-alist)))
3683 (when sec-prop
3684 (funcall --walk-tree
3685 (org-element-property (cdr sec-prop) --data)))))
3686 ;; Determine if a recursion into --DATA is possible.
3687 (cond
3688 ;; --TYPE is explicitly removed from recursion.
3689 ((memq --type no-recursion))
3690 ;; --DATA has no contents.
3691 ((not (org-element-contents --data)))
3692 ;; Looking for greater elements but --DATA is simply
3693 ;; an element or an object.
3694 ((and (eq --category 'greater-elements)
3695 (not (memq --type org-element-greater-elements))))
3696 ;; Looking for elements but --DATA is an object.
3697 ((and (eq --category 'elements)
3698 (memq --type org-element-all-objects)))
3699 ;; In any other case, map contents.
3700 (t (mapc --walk-tree (org-element-contents --data)))))))))))
3701 (catch '--map-first-match
3702 (funcall --walk-tree data)
3703 ;; Return value in a proper order.
3704 (nreverse --acc))))
3706 ;; The following functions are internal parts of the parser.
3708 ;; The first one, `org-element--parse-elements' acts at the element's
3709 ;; level.
3711 ;; The second one, `org-element--parse-objects' applies on all objects
3712 ;; of a paragraph or a secondary string. It uses
3713 ;; `org-element--get-next-object-candidates' to optimize the search of
3714 ;; the next object in the buffer.
3716 ;; More precisely, that function looks for every allowed object type
3717 ;; first. Then, it discards failed searches, keeps further matches,
3718 ;; and searches again types matched behind point, for subsequent
3719 ;; calls. Thus, searching for a given type fails only once, and every
3720 ;; object is searched only once at top level (but sometimes more for
3721 ;; nested types).
3723 (defun org-element--parse-elements
3724 (beg end special structure granularity visible-only acc)
3725 "Parse elements between BEG and END positions.
3727 SPECIAL prioritize some elements over the others. It can be set
3728 to `first-section', `quote-section', `section' `item' or
3729 `table-row'.
3731 When value is `item', STRUCTURE will be used as the current list
3732 structure.
3734 GRANULARITY determines the depth of the recursion. See
3735 `org-element-parse-buffer' for more information.
3737 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3738 elements.
3740 Elements are accumulated into ACC."
3741 (save-excursion
3742 (goto-char beg)
3743 ;; When parsing only headlines, skip any text before first one.
3744 (when (and (eq granularity 'headline) (not (org-at-heading-p)))
3745 (org-with-limited-levels (outline-next-heading)))
3746 ;; Main loop start.
3747 (while (< (point) end)
3748 ;; Find current element's type and parse it accordingly to
3749 ;; its category.
3750 (let* ((element (org-element--current-element
3751 end granularity special structure))
3752 (type (org-element-type element))
3753 (cbeg (org-element-property :contents-begin element)))
3754 (goto-char (org-element-property :end element))
3755 ;; Fill ELEMENT contents by side-effect.
3756 (cond
3757 ;; If VISIBLE-ONLY is true and element is hidden or if it has
3758 ;; no contents, don't modify it.
3759 ((or (and visible-only (org-element-property :hiddenp element))
3760 (not cbeg)))
3761 ;; Greater element: parse it between `contents-begin' and
3762 ;; `contents-end'. Make sure GRANULARITY allows the
3763 ;; recursion, or ELEMENT is an headline, in which case going
3764 ;; inside is mandatory, in order to get sub-level headings.
3765 ((and (memq type org-element-greater-elements)
3766 (or (memq granularity '(element object nil))
3767 (and (eq granularity 'greater-element)
3768 (eq type 'section))
3769 (eq type 'headline)))
3770 (org-element--parse-elements
3771 cbeg (org-element-property :contents-end element)
3772 ;; Possibly switch to a special mode.
3773 (case type
3774 (headline
3775 (if (org-element-property :quotedp element) 'quote-section
3776 'section))
3777 (plain-list 'item)
3778 (table 'table-row))
3779 (org-element-property :structure element)
3780 granularity visible-only element))
3781 ;; ELEMENT has contents. Parse objects inside, if
3782 ;; GRANULARITY allows it.
3783 ((memq granularity '(object nil))
3784 (org-element--parse-objects
3785 cbeg (org-element-property :contents-end element) element
3786 (org-element-restriction type))))
3787 (org-element-adopt-elements acc element)))
3788 ;; Return result.
3789 acc))
3791 (defun org-element--parse-objects (beg end acc restriction)
3792 "Parse objects between BEG and END and return recursive structure.
3794 Objects are accumulated in ACC.
3796 RESTRICTION is a list of object types which are allowed in the
3797 current object."
3798 (let (candidates)
3799 (save-excursion
3800 (goto-char beg)
3801 (while (and (< (point) end)
3802 (setq candidates (org-element--get-next-object-candidates
3803 end restriction candidates)))
3804 (let ((next-object
3805 (let ((pos (apply 'min (mapcar 'cdr candidates))))
3806 (save-excursion
3807 (goto-char pos)
3808 (funcall (intern (format "org-element-%s-parser"
3809 (car (rassq pos candidates)))))))))
3810 ;; 1. Text before any object. Untabify it.
3811 (let ((obj-beg (org-element-property :begin next-object)))
3812 (unless (= (point) obj-beg)
3813 (setq acc
3814 (org-element-adopt-elements
3816 (replace-regexp-in-string
3817 "\t" (make-string tab-width ? )
3818 (buffer-substring-no-properties (point) obj-beg))))))
3819 ;; 2. Object...
3820 (let ((obj-end (org-element-property :end next-object))
3821 (cont-beg (org-element-property :contents-begin next-object)))
3822 ;; Fill contents of NEXT-OBJECT by side-effect, if it has
3823 ;; a recursive type.
3824 (when (and cont-beg
3825 (memq (car next-object) org-element-recursive-objects))
3826 (save-restriction
3827 (narrow-to-region
3828 cont-beg
3829 (org-element-property :contents-end next-object))
3830 (org-element--parse-objects
3831 (point-min) (point-max) next-object
3832 (org-element-restriction next-object))))
3833 (setq acc (org-element-adopt-elements acc next-object))
3834 (goto-char obj-end))))
3835 ;; 3. Text after last object. Untabify it.
3836 (unless (= (point) end)
3837 (setq acc
3838 (org-element-adopt-elements
3840 (replace-regexp-in-string
3841 "\t" (make-string tab-width ? )
3842 (buffer-substring-no-properties (point) end)))))
3843 ;; Result.
3844 acc)))
3846 (defun org-element--get-next-object-candidates (limit restriction objects)
3847 "Return an alist of candidates for the next object.
3849 LIMIT bounds the search, and RESTRICTION narrows candidates to
3850 some object types.
3852 Return value is an alist whose CAR is position and CDR the object
3853 type, as a symbol.
3855 OBJECTS is the previous candidates alist."
3856 (let (next-candidates types-to-search)
3857 ;; If no previous result, search every object type in RESTRICTION.
3858 ;; Otherwise, keep potential candidates (old objects located after
3859 ;; point) and ask to search again those which had matched before.
3860 (if (not objects) (setq types-to-search restriction)
3861 (mapc (lambda (obj)
3862 (if (< (cdr obj) (point)) (push (car obj) types-to-search)
3863 (push obj next-candidates)))
3864 objects))
3865 ;; Call the appropriate successor function for each type to search
3866 ;; and accumulate matches.
3867 (mapc
3868 (lambda (type)
3869 (let* ((successor-fun
3870 (intern
3871 (format "org-element-%s-successor"
3872 (or (cdr (assq type org-element-object-successor-alist))
3873 type))))
3874 (obj (funcall successor-fun limit)))
3875 (and obj (push obj next-candidates))))
3876 types-to-search)
3877 ;; Return alist.
3878 next-candidates))
3882 ;;; Towards A Bijective Process
3884 ;; The parse tree obtained with `org-element-parse-buffer' is really
3885 ;; a snapshot of the corresponding Org buffer. Therefore, it can be
3886 ;; interpreted and expanded into a string with canonical Org syntax.
3887 ;; Hence `org-element-interpret-data'.
3889 ;; The function relies internally on
3890 ;; `org-element--interpret-affiliated-keywords'.
3892 ;;;###autoload
3893 (defun org-element-interpret-data (data &optional parent)
3894 "Interpret DATA as Org syntax.
3896 DATA is a parse tree, an element, an object or a secondary string
3897 to interpret.
3899 Optional argument PARENT is used for recursive calls. It contains
3900 the element or object containing data, or nil.
3902 Return Org syntax as a string."
3903 (let* ((type (org-element-type data))
3904 (results
3905 (cond
3906 ;; Secondary string.
3907 ((not type)
3908 (mapconcat
3909 (lambda (obj) (org-element-interpret-data obj parent))
3910 data ""))
3911 ;; Full Org document.
3912 ((eq type 'org-data)
3913 (mapconcat
3914 (lambda (obj) (org-element-interpret-data obj parent))
3915 (org-element-contents data) ""))
3916 ;; Plain text.
3917 ((stringp data) data)
3918 ;; Element/Object without contents.
3919 ((not (org-element-contents data))
3920 (funcall (intern (format "org-element-%s-interpreter" type))
3921 data nil))
3922 ;; Element/Object with contents.
3924 (let* ((greaterp (memq type org-element-greater-elements))
3925 (objectp (and (not greaterp)
3926 (memq type org-element-recursive-objects)))
3927 (contents
3928 (mapconcat
3929 (lambda (obj) (org-element-interpret-data obj data))
3930 (org-element-contents
3931 (if (or greaterp objectp) data
3932 ;; Elements directly containing objects must
3933 ;; have their indentation normalized first.
3934 (org-element-normalize-contents
3935 data
3936 ;; When normalizing first paragraph of an
3937 ;; item or a footnote-definition, ignore
3938 ;; first line's indentation.
3939 (and (eq type 'paragraph)
3940 (equal data (car (org-element-contents parent)))
3941 (memq (org-element-type parent)
3942 '(footnote-definiton item))))))
3943 "")))
3944 (funcall (intern (format "org-element-%s-interpreter" type))
3945 data
3946 (if greaterp (org-element-normalize-contents contents)
3947 contents)))))))
3948 (if (memq type '(org-data plain-text nil)) results
3949 ;; Build white spaces. If no `:post-blank' property is
3950 ;; specified, assume its value is 0.
3951 (let ((post-blank (or (org-element-property :post-blank data) 0)))
3952 (if (memq type org-element-all-objects)
3953 (concat results (make-string post-blank 32))
3954 (concat
3955 (org-element--interpret-affiliated-keywords data)
3956 (org-element-normalize-string results)
3957 (make-string post-blank 10)))))))
3959 (defun org-element--interpret-affiliated-keywords (element)
3960 "Return ELEMENT's affiliated keywords as Org syntax.
3961 If there is no affiliated keyword, return the empty string."
3962 (let ((keyword-to-org
3963 (function
3964 (lambda (key value)
3965 (let (dual)
3966 (when (member key org-element-dual-keywords)
3967 (setq dual (cdr value) value (car value)))
3968 (concat "#+" key
3969 (and dual
3970 (format "[%s]" (org-element-interpret-data dual)))
3971 ": "
3972 (if (member key org-element-parsed-keywords)
3973 (org-element-interpret-data value)
3974 value)
3975 "\n"))))))
3976 (mapconcat
3977 (lambda (prop)
3978 (let ((value (org-element-property prop element))
3979 (keyword (upcase (substring (symbol-name prop) 1))))
3980 (when value
3981 (if (or (member keyword org-element-multiple-keywords)
3982 ;; All attribute keywords can have multiple lines.
3983 (string-match "^ATTR_" keyword))
3984 (mapconcat (lambda (line) (funcall keyword-to-org keyword line))
3985 value
3987 (funcall keyword-to-org keyword value)))))
3988 ;; List all ELEMENT's properties matching an attribute line or an
3989 ;; affiliated keyword, but ignore translated keywords since they
3990 ;; cannot belong to the property list.
3991 (loop for prop in (nth 1 element) by 'cddr
3992 when (let ((keyword (upcase (substring (symbol-name prop) 1))))
3993 (or (string-match "^ATTR_" keyword)
3994 (and
3995 (member keyword org-element-affiliated-keywords)
3996 (not (assoc keyword
3997 org-element-keyword-translation-alist)))))
3998 collect prop)
3999 "")))
4001 ;; Because interpretation of the parse tree must return the same
4002 ;; number of blank lines between elements and the same number of white
4003 ;; space after objects, some special care must be given to white
4004 ;; spaces.
4006 ;; The first function, `org-element-normalize-string', ensures any
4007 ;; string different from the empty string will end with a single
4008 ;; newline character.
4010 ;; The second function, `org-element-normalize-contents', removes
4011 ;; global indentation from the contents of the current element.
4013 (defun org-element-normalize-string (s)
4014 "Ensure string S ends with a single newline character.
4016 If S isn't a string return it unchanged. If S is the empty
4017 string, return it. Otherwise, return a new string with a single
4018 newline character at its end."
4019 (cond
4020 ((not (stringp s)) s)
4021 ((string= "" s) "")
4022 (t (and (string-match "\\(\n[ \t]*\\)*\\'" s)
4023 (replace-match "\n" nil nil s)))))
4025 (defun org-element-normalize-contents (element &optional ignore-first)
4026 "Normalize plain text in ELEMENT's contents.
4028 ELEMENT must only contain plain text and objects.
4030 If optional argument IGNORE-FIRST is non-nil, ignore first line's
4031 indentation to compute maximal common indentation.
4033 Return the normalized element that is element with global
4034 indentation removed from its contents. The function assumes that
4035 indentation is not done with TAB characters."
4036 (let* (ind-list ; for byte-compiler
4037 collect-inds ; for byte-compiler
4038 (collect-inds
4039 (function
4040 ;; Return list of indentations within BLOB. This is done by
4041 ;; walking recursively BLOB and updating IND-LIST along the
4042 ;; way. FIRST-FLAG is non-nil when the first string hasn't
4043 ;; been seen yet. It is required as this string is the only
4044 ;; one whose indentation doesn't happen after a newline
4045 ;; character.
4046 (lambda (blob first-flag)
4047 (mapc
4048 (lambda (object)
4049 (when (and first-flag (stringp object))
4050 (setq first-flag nil)
4051 (string-match "\\`\\( *\\)" object)
4052 (let ((len (length (match-string 1 object))))
4053 ;; An indentation of zero means no string will be
4054 ;; modified. Quit the process.
4055 (if (zerop len) (throw 'zero (setq ind-list nil))
4056 (push len ind-list))))
4057 (cond
4058 ((stringp object)
4059 (let ((start 0))
4060 ;; Avoid matching blank or empty lines.
4061 (while (and (string-match "\n\\( *\\)\\(.\\)" object start)
4062 (not (equal (match-string 2 object) " ")))
4063 (setq start (match-end 0))
4064 (push (length (match-string 1 object)) ind-list))))
4065 ((memq (org-element-type object) org-element-recursive-objects)
4066 (funcall collect-inds object first-flag))))
4067 (org-element-contents blob))))))
4068 ;; Collect indentation list in ELEMENT. Possibly remove first
4069 ;; value if IGNORE-FIRST is non-nil.
4070 (catch 'zero (funcall collect-inds element (not ignore-first)))
4071 (if (not ind-list) element
4072 ;; Build ELEMENT back, replacing each string with the same
4073 ;; string minus common indentation.
4074 (let* (build ; For byte compiler.
4075 (build
4076 (function
4077 (lambda (blob mci first-flag)
4078 ;; Return BLOB with all its strings indentation
4079 ;; shortened from MCI white spaces. FIRST-FLAG is
4080 ;; non-nil when the first string hasn't been seen
4081 ;; yet.
4082 (setcdr (cdr blob)
4083 (mapcar
4084 (lambda (object)
4085 (when (and first-flag (stringp object))
4086 (setq first-flag nil)
4087 (setq object
4088 (replace-regexp-in-string
4089 (format "\\` \\{%d\\}" mci) "" object)))
4090 (cond
4091 ((stringp object)
4092 (replace-regexp-in-string
4093 (format "\n \\{%d\\}" mci) "\n" object))
4094 ((memq (org-element-type object)
4095 org-element-recursive-objects)
4096 (funcall build object mci first-flag))
4097 (t object)))
4098 (org-element-contents blob)))
4099 blob))))
4100 (funcall build element (apply 'min ind-list) (not ignore-first))))))
4104 ;;; The Toolbox
4106 ;; The first move is to implement a way to obtain the smallest element
4107 ;; containing point. This is the job of `org-element-at-point'. It
4108 ;; basically jumps back to the beginning of section containing point
4109 ;; and moves, element after element, with
4110 ;; `org-element--current-element' until the container is found. Note:
4111 ;; When using `org-element-at-point', secondary values are never
4112 ;; parsed since the function focuses on elements, not on objects.
4114 ;; At a deeper level, `org-element-context' lists all elements and
4115 ;; objects containing point.
4117 ;; `org-element-nested-p' and `org-element-swap-A-B' may be used
4118 ;; internally by navigation and manipulation tools.
4120 ;;;###autoload
4121 (defun org-element-at-point (&optional keep-trail)
4122 "Determine closest element around point.
4124 Return value is a list like (TYPE PROPS) where TYPE is the type
4125 of the element and PROPS a plist of properties associated to the
4126 element.
4128 Possible types are defined in `org-element-all-elements'.
4129 Properties depend on element or object type, but always
4130 include :begin, :end, :parent and :post-blank properties.
4132 As a special case, if point is at the very beginning of a list or
4133 sub-list, returned element will be that list instead of the first
4134 item. In the same way, if point is at the beginning of the first
4135 row of a table, returned element will be the table instead of the
4136 first row.
4138 If optional argument KEEP-TRAIL is non-nil, the function returns
4139 a list of of elements leading to element at point. The list's
4140 CAR is always the element at point. Following positions contain
4141 element's siblings, then parents, siblings of parents, until the
4142 first element of current section."
4143 (org-with-wide-buffer
4144 ;; If at an headline, parse it. It is the sole element that
4145 ;; doesn't require to know about context. Be sure to disallow
4146 ;; secondary string parsing, though.
4147 (if (org-with-limited-levels (org-at-heading-p))
4148 (progn
4149 (beginning-of-line)
4150 (if (not keep-trail) (org-element-headline-parser (point-max) t)
4151 (list (org-element-headline-parser (point-max) t))))
4152 ;; Otherwise move at the beginning of the section containing
4153 ;; point.
4154 (let ((origin (point))
4155 (end (save-excursion
4156 (org-with-limited-levels (outline-next-heading)) (point)))
4157 element type special-flag trail struct prevs parent)
4158 (org-with-limited-levels
4159 (if (org-with-limited-levels (org-before-first-heading-p))
4160 (goto-char (point-min))
4161 (org-back-to-heading)
4162 (forward-line)))
4163 (org-skip-whitespace)
4164 (beginning-of-line)
4165 ;; Parse successively each element, skipping those ending
4166 ;; before original position.
4167 (catch 'exit
4168 (while t
4169 (setq element
4170 (org-element--current-element end 'element special-flag struct)
4171 type (car element))
4172 (org-element-put-property element :parent parent)
4173 (when keep-trail (push element trail))
4174 (cond
4175 ;; 1. Skip any element ending before point. Also skip
4176 ;; element ending at point when we're sure that another
4177 ;; element has started.
4178 ((let ((elem-end (org-element-property :end element)))
4179 (when (or (< elem-end origin)
4180 (and (= elem-end origin) (/= elem-end end)))
4181 (goto-char elem-end))))
4182 ;; 2. An element containing point is always the element at
4183 ;; point.
4184 ((not (memq type org-element-greater-elements))
4185 (throw 'exit (if keep-trail trail element)))
4186 ;; 3. At any other greater element type, if point is
4187 ;; within contents, move into it.
4189 (let ((cbeg (org-element-property :contents-begin element))
4190 (cend (org-element-property :contents-end element)))
4191 (if (or (not cbeg) (not cend) (> cbeg origin) (< cend origin)
4192 ;; Create an anchor for tables and plain lists:
4193 ;; when point is at the very beginning of these
4194 ;; elements, ignoring affiliated keywords,
4195 ;; target them instead of their contents.
4196 (and (= cbeg origin) (memq type '(plain-list table)))
4197 ;; When point is at contents end, do not move
4198 ;; into elements with an explicit ending, but
4199 ;; return that element instead.
4200 (and (= cend origin)
4201 (memq type
4202 '(center-block
4203 drawer dynamic-block inlinetask item
4204 plain-list quote-block special-block))))
4205 (throw 'exit (if keep-trail trail element))
4206 (setq parent element)
4207 (case type
4208 (plain-list
4209 (setq special-flag 'item
4210 struct (org-element-property :structure element)))
4211 (table (setq special-flag 'table-row))
4212 (otherwise (setq special-flag nil)))
4213 (setq end cend)
4214 (goto-char cbeg)))))))))))
4216 ;;;###autoload
4217 (defun org-element-context ()
4218 "Return closest element or object around point.
4220 Return value is a list like (TYPE PROPS) where TYPE is the type
4221 of the element or object and PROPS a plist of properties
4222 associated to it.
4224 Possible types are defined in `org-element-all-elements' and
4225 `org-element-all-objects'. Properties depend on element or
4226 object type, but always include :begin, :end, :parent
4227 and :post-blank properties."
4228 (org-with-wide-buffer
4229 (let* ((origin (point))
4230 (element (org-element-at-point))
4231 (type (car element))
4232 end)
4233 ;; Check if point is inside an element containing objects or at
4234 ;; a secondary string. In that case, move to beginning of the
4235 ;; element or secondary string and set END to the other side.
4236 (if (not (or (and (eq type 'item)
4237 (let ((tag (org-element-property :tag element)))
4238 (and tag
4239 (progn
4240 (beginning-of-line)
4241 (search-forward tag (point-at-eol))
4242 (goto-char (match-beginning 0))
4243 (and (>= origin (point))
4244 (<= origin
4245 ;; `1+' is required so some
4246 ;; successors can match
4247 ;; properly their object.
4248 (setq end (1+ (match-end 0)))))))))
4249 (and (memq type '(headline inlinetask))
4250 (progn (beginning-of-line)
4251 (skip-chars-forward "* ")
4252 (setq end (point-at-eol))))
4253 (and (memq type '(paragraph table-cell verse-block))
4254 (let ((cbeg (org-element-property
4255 :contents-begin element))
4256 (cend (org-element-property
4257 :contents-end element)))
4258 (and (>= origin cbeg)
4259 (<= origin cend)
4260 (progn (goto-char cbeg) (setq end cend)))))))
4261 element
4262 (let ((restriction (org-element-restriction element))
4263 (parent element)
4264 candidates)
4265 (catch 'exit
4266 (while (setq candidates (org-element--get-next-object-candidates
4267 end restriction candidates))
4268 (let ((closest-cand (rassq (apply 'min (mapcar 'cdr candidates))
4269 candidates)))
4270 ;; If ORIGIN is before next object in element, there's
4271 ;; no point in looking further.
4272 (if (> (cdr closest-cand) origin) (throw 'exit element)
4273 (let* ((object
4274 (progn (goto-char (cdr closest-cand))
4275 (funcall (intern (format "org-element-%s-parser"
4276 (car closest-cand))))))
4277 (cbeg (org-element-property :contents-begin object))
4278 (cend (org-element-property :contents-end object)))
4279 (cond
4280 ;; ORIGIN is after OBJECT, so skip it.
4281 ((< (org-element-property :end object) origin)
4282 (goto-char (org-element-property :end object)))
4283 ;; ORIGIN is within a non-recursive object or at an
4284 ;; object boundaries: Return that object.
4285 ((or (not cbeg) (> cbeg origin) (< cend origin))
4286 (throw 'exit
4287 (org-element-put-property object :parent parent)))
4288 ;; Otherwise, move within current object and restrict
4289 ;; search to the end of its contents.
4290 (t (goto-char cbeg)
4291 (org-element-put-property object :parent parent)
4292 (setq parent object end cend)))))))
4293 parent))))))
4295 (defsubst org-element-nested-p (elem-A elem-B)
4296 "Non-nil when elements ELEM-A and ELEM-B are nested."
4297 (let ((beg-A (org-element-property :begin elem-A))
4298 (beg-B (org-element-property :begin elem-B))
4299 (end-A (org-element-property :end elem-A))
4300 (end-B (org-element-property :end elem-B)))
4301 (or (and (>= beg-A beg-B) (<= end-A end-B))
4302 (and (>= beg-B beg-A) (<= end-B end-A)))))
4304 (defun org-element-swap-A-B (elem-A elem-B)
4305 "Swap elements ELEM-A and ELEM-B.
4306 Assume ELEM-B is after ELEM-A in the buffer. Leave point at the
4307 end of ELEM-A."
4308 (goto-char (org-element-property :begin elem-A))
4309 ;; There are two special cases when an element doesn't start at bol:
4310 ;; the first paragraph in an item or in a footnote definition.
4311 (let ((specialp (not (bolp))))
4312 ;; Only a paragraph without any affiliated keyword can be moved at
4313 ;; ELEM-A position in such a situation. Note that the case of
4314 ;; a footnote definition is impossible: it cannot contain two
4315 ;; paragraphs in a row because it cannot contain a blank line.
4316 (if (and specialp
4317 (or (not (eq (org-element-type elem-B) 'paragraph))
4318 (/= (org-element-property :begin elem-B)
4319 (org-element-property :contents-begin elem-B))))
4320 (error "Cannot swap elements"))
4321 ;; In a special situation, ELEM-A will have no indentation. We'll
4322 ;; give it ELEM-B's (which will in, in turn, have no indentation).
4323 (let* ((ind-B (when specialp
4324 (goto-char (org-element-property :begin elem-B))
4325 (org-get-indentation)))
4326 (beg-A (org-element-property :begin elem-A))
4327 (end-A (save-excursion
4328 (goto-char (org-element-property :end elem-A))
4329 (skip-chars-backward " \r\t\n")
4330 (point-at-eol)))
4331 (beg-B (org-element-property :begin elem-B))
4332 (end-B (save-excursion
4333 (goto-char (org-element-property :end elem-B))
4334 (skip-chars-backward " \r\t\n")
4335 (point-at-eol)))
4336 ;; Store overlays responsible for visibility status. We
4337 ;; also need to store their boundaries as they will be
4338 ;; removed from buffer.
4339 (overlays
4340 (cons
4341 (mapcar (lambda (ov) (list ov (overlay-start ov) (overlay-end ov)))
4342 (overlays-in beg-A end-A))
4343 (mapcar (lambda (ov) (list ov (overlay-start ov) (overlay-end ov)))
4344 (overlays-in beg-B end-B))))
4345 ;; Get contents.
4346 (body-A (buffer-substring beg-A end-A))
4347 (body-B (delete-and-extract-region beg-B end-B)))
4348 (goto-char beg-B)
4349 (when specialp
4350 (setq body-B (replace-regexp-in-string "\\`[ \t]*" "" body-B))
4351 (org-indent-to-column ind-B))
4352 (insert body-A)
4353 ;; Restore ex ELEM-A overlays.
4354 (let ((offset (- beg-B beg-A)))
4355 (mapc (lambda (ov)
4356 (move-overlay
4357 (car ov) (+ (nth 1 ov) offset) (+ (nth 2 ov) offset)))
4358 (car overlays))
4359 (goto-char beg-A)
4360 (delete-region beg-A end-A)
4361 (insert body-B)
4362 ;; Restore ex ELEM-B overlays.
4363 (mapc (lambda (ov)
4364 (move-overlay
4365 (car ov) (- (nth 1 ov) offset) (- (nth 2 ov) offset)))
4366 (cdr overlays)))
4367 (goto-char (org-element-property :end elem-B)))))
4370 (provide 'org-element)
4371 ;;; org-element.el ends here