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