org-e-publish: Mirror changes made to org-publish.el
[org-mode.git] / lisp / org-element.el
blobe173774fd5a48cf286f7c94bb4a5ab94426d9a87
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) raw-value)))
722 (commentedp
723 (let ((case-fold-search nil))
724 (string-match (format "^%s +" org-comment-string) raw-value)))
725 (archivedp (member org-archive-tag tags))
726 (footnote-section-p (and org-footnote-section
727 (string= org-footnote-section raw-value)))
728 ;; Normalize property names: ":SOME_PROP:" becomes
729 ;; ":some-prop".
730 (standard-props (let (plist)
731 (mapc
732 (lambda (p)
733 (let ((p-name (downcase (car p))))
734 (while (string-match "_" p-name)
735 (setq p-name
736 (replace-match "-" nil nil p-name)))
737 (setq p-name (intern (concat ":" p-name)))
738 (setq plist
739 (plist-put plist p-name (cdr p)))))
740 (org-entry-properties nil 'standard))
741 plist))
742 (time-props (org-entry-properties nil 'special "CLOCK"))
743 (scheduled (cdr (assoc "SCHEDULED" time-props)))
744 (deadline (cdr (assoc "DEADLINE" time-props)))
745 (clock (cdr (assoc "CLOCK" time-props)))
746 (timestamp (cdr (assoc "TIMESTAMP" time-props)))
747 (begin (point))
748 (end (save-excursion (goto-char (org-end-of-subtree t t))))
749 (pos-after-head (progn (forward-line) (point)))
750 (contents-begin (save-excursion
751 (skip-chars-forward " \r\t\n" end)
752 (and (/= (point) end) (line-beginning-position))))
753 (hidden (org-invisible-p2))
754 (contents-end (and contents-begin
755 (progn (goto-char end)
756 (skip-chars-backward " \r\t\n")
757 (forward-line)
758 (point)))))
759 ;; Clean RAW-VALUE from any quote or comment string.
760 (when (or quotedp commentedp)
761 (setq raw-value
762 (replace-regexp-in-string
763 (concat "\\(" org-quote-string "\\|" org-comment-string "\\) +")
765 raw-value)))
766 ;; Clean TAGS from archive tag, if any.
767 (when archivedp (setq tags (delete org-archive-tag tags)))
768 (let ((headline
769 (list 'headline
770 (nconc
771 (list :raw-value raw-value
772 :begin begin
773 :end end
774 :pre-blank
775 (if (not contents-begin) 0
776 (count-lines pos-after-head contents-begin))
777 :hiddenp hidden
778 :contents-begin contents-begin
779 :contents-end contents-end
780 :level level
781 :priority (nth 3 components)
782 :tags tags
783 :todo-keyword todo
784 :todo-type todo-type
785 :scheduled scheduled
786 :deadline deadline
787 :timestamp timestamp
788 :clock clock
789 :post-blank (count-lines
790 (if (not contents-end) pos-after-head
791 (goto-char contents-end)
792 (forward-line)
793 (point))
794 end)
795 :footnote-section-p footnote-section-p
796 :archivedp archivedp
797 :commentedp commentedp
798 :quotedp quotedp)
799 standard-props))))
800 (org-element-put-property
801 headline :title
802 (if raw-secondary-p raw-value
803 (org-element-parse-secondary-string
804 raw-value (org-element-restriction 'headline) headline)))))))
806 (defun org-element-headline-interpreter (headline contents)
807 "Interpret HEADLINE element as Org syntax.
808 CONTENTS is the contents of the element."
809 (let* ((level (org-element-property :level headline))
810 (todo (org-element-property :todo-keyword headline))
811 (priority (org-element-property :priority headline))
812 (title (org-element-interpret-data
813 (org-element-property :title headline)))
814 (tags (let ((tag-list (if (org-element-property :archivedp headline)
815 (cons org-archive-tag
816 (org-element-property :tags headline))
817 (org-element-property :tags headline))))
818 (and tag-list
819 (format ":%s:" (mapconcat 'identity tag-list ":")))))
820 (commentedp (org-element-property :commentedp headline))
821 (quotedp (org-element-property :quotedp headline))
822 (pre-blank (or (org-element-property :pre-blank headline) 0))
823 (heading (concat (make-string level ?*)
824 (and todo (concat " " todo))
825 (and quotedp (concat " " org-quote-string))
826 (and commentedp (concat " " org-comment-string))
827 (and priority
828 (format " [#%s]" (char-to-string priority)))
829 (cond ((and org-footnote-section
830 (org-element-property
831 :footnote-section-p headline))
832 (concat " " org-footnote-section))
833 (title (concat " " title))))))
834 (concat heading
835 ;; Align tags.
836 (when tags
837 (cond
838 ((zerop org-tags-column) (format " %s" tags))
839 ((< org-tags-column 0)
840 (concat
841 (make-string
842 (max (- (+ org-tags-column (length heading) (length tags))) 1)
844 tags))
846 (concat
847 (make-string (max (- org-tags-column (length heading)) 1) ? )
848 tags))))
849 (make-string (1+ pre-blank) 10)
850 contents)))
853 ;;;; Inlinetask
855 (defun org-element-inlinetask-parser (limit &optional raw-secondary-p)
856 "Parse an inline task.
858 Return a list whose CAR is `inlinetask' and CDR is a plist
859 containing `:title', `:begin', `:end', `:hiddenp',
860 `:contents-begin' and `:contents-end', `:level', `:priority',
861 `:raw-value', `:tags', `:todo-keyword', `:todo-type',
862 `:scheduled', `:deadline', `:timestamp', `:clock' and
863 `:post-blank' keywords.
865 The plist also contains any property set in the property drawer,
866 with its name in lowercase, the underscores replaced with hyphens
867 and colons at the beginning (i.e. `:custom-id').
869 When optional argument RAW-SECONDARY-P is non-nil, inline-task's
870 title will not be parsed as a secondary string, but as a plain
871 string instead.
873 Assume point is at beginning of the inline task."
874 (save-excursion
875 (let* ((keywords (org-element--collect-affiliated-keywords))
876 (begin (car keywords))
877 (components (org-heading-components))
878 (todo (nth 2 components))
879 (todo-type (and todo
880 (if (member todo org-done-keywords) 'done 'todo)))
881 (tags (let ((raw-tags (nth 5 components)))
882 (and raw-tags (org-split-string raw-tags ":"))))
883 (raw-value (or (nth 4 components) ""))
884 ;; Normalize property names: ":SOME_PROP:" becomes
885 ;; ":some-prop".
886 (standard-props (let (plist)
887 (mapc
888 (lambda (p)
889 (let ((p-name (downcase (car p))))
890 (while (string-match "_" p-name)
891 (setq p-name
892 (replace-match "-" nil nil p-name)))
893 (setq p-name (intern (concat ":" p-name)))
894 (setq plist
895 (plist-put plist p-name (cdr p)))))
896 (org-entry-properties nil 'standard))
897 plist))
898 (time-props (org-entry-properties nil 'special "CLOCK"))
899 (scheduled (cdr (assoc "SCHEDULED" time-props)))
900 (deadline (cdr (assoc "DEADLINE" time-props)))
901 (clock (cdr (assoc "CLOCK" time-props)))
902 (timestamp (cdr (assoc "TIMESTAMP" time-props)))
903 (task-end (save-excursion
904 (end-of-line)
905 (and (re-search-forward "^\\*+ END" limit t)
906 (match-beginning 0))))
907 (contents-begin (progn (forward-line)
908 (and task-end (< (point) task-end) (point))))
909 (hidden (and contents-begin (org-invisible-p2)))
910 (contents-end (and contents-begin task-end))
911 (before-blank (if (not task-end) (point)
912 (goto-char task-end)
913 (forward-line)
914 (point)))
915 (end (progn (skip-chars-forward " \r\t\n" limit)
916 (if (eobp) (point) (point-at-bol))))
917 (inlinetask
918 (list 'inlinetask
919 (nconc
920 (list :raw-value raw-value
921 :begin begin
922 :end end
923 :hiddenp hidden
924 :contents-begin contents-begin
925 :contents-end contents-end
926 :level (nth 1 components)
927 :priority (nth 3 components)
928 :tags tags
929 :todo-keyword todo
930 :todo-type todo-type
931 :scheduled scheduled
932 :deadline deadline
933 :timestamp timestamp
934 :clock clock
935 :post-blank (count-lines before-blank end))
936 standard-props
937 (cadr keywords)))))
938 (org-element-put-property
939 inlinetask :title
940 (if raw-secondary-p raw-value
941 (org-element-parse-secondary-string
942 raw-value
943 (org-element-restriction 'inlinetask)
944 inlinetask))))))
946 (defun org-element-inlinetask-interpreter (inlinetask contents)
947 "Interpret INLINETASK element as Org syntax.
948 CONTENTS is the contents of inlinetask."
949 (let* ((level (org-element-property :level inlinetask))
950 (todo (org-element-property :todo-keyword inlinetask))
951 (priority (org-element-property :priority inlinetask))
952 (title (org-element-interpret-data
953 (org-element-property :title inlinetask)))
954 (tags (let ((tag-list (org-element-property :tags inlinetask)))
955 (and tag-list
956 (format ":%s:" (mapconcat 'identity tag-list ":")))))
957 (task (concat (make-string level ?*)
958 (and todo (concat " " todo))
959 (and priority
960 (format " [#%s]" (char-to-string priority)))
961 (and title (concat " " title)))))
962 (concat task
963 ;; Align tags.
964 (when tags
965 (cond
966 ((zerop org-tags-column) (format " %s" tags))
967 ((< org-tags-column 0)
968 (concat
969 (make-string
970 (max (- (+ org-tags-column (length task) (length tags))) 1)
972 tags))
974 (concat
975 (make-string (max (- org-tags-column (length task)) 1) ? )
976 tags))))
977 ;; Prefer degenerate inlinetasks when there are no
978 ;; contents.
979 (when contents
980 (concat "\n"
981 contents
982 (make-string level ?*) " END")))))
985 ;;;; Item
987 (defun org-element-item-parser (limit struct &optional raw-secondary-p)
988 "Parse an item.
990 STRUCT is the structure of the plain list.
992 Return a list whose CAR is `item' and CDR is a plist containing
993 `:bullet', `:begin', `:end', `:contents-begin', `:contents-end',
994 `:checkbox', `:counter', `:tag', `:structure', `:hiddenp' and
995 `:post-blank' keywords.
997 When optional argument RAW-SECONDARY-P is non-nil, item's tag, if
998 any, will not be parsed as a secondary string, but as a plain
999 string instead.
1001 Assume point is at the beginning of the item."
1002 (save-excursion
1003 (beginning-of-line)
1004 (looking-at org-list-full-item-re)
1005 (let* ((begin (point))
1006 (bullet (org-match-string-no-properties 1))
1007 (checkbox (let ((box (org-match-string-no-properties 3)))
1008 (cond ((equal "[ ]" box) 'off)
1009 ((equal "[X]" box) 'on)
1010 ((equal "[-]" box) 'trans))))
1011 (counter (let ((c (org-match-string-no-properties 2)))
1012 (save-match-data
1013 (cond
1014 ((not c) nil)
1015 ((string-match "[A-Za-z]" c)
1016 (- (string-to-char (upcase (match-string 0 c)))
1017 64))
1018 ((string-match "[0-9]+" c)
1019 (string-to-number (match-string 0 c)))))))
1020 (end (save-excursion (goto-char (org-list-get-item-end begin struct))
1021 (unless (bolp) (forward-line))
1022 (point)))
1023 (contents-begin
1024 (progn (goto-char
1025 ;; Ignore tags in un-ordered lists: they are just
1026 ;; a part of item's body.
1027 (if (and (match-beginning 4)
1028 (save-match-data (string-match "[.)]" bullet)))
1029 (match-beginning 4)
1030 (match-end 0)))
1031 (skip-chars-forward " \r\t\n" limit)
1032 ;; If first line isn't empty, contents really start
1033 ;; at the text after item's meta-data.
1034 (if (= (point-at-bol) begin) (point) (point-at-bol))))
1035 (hidden (progn (forward-line)
1036 (and (not (= (point) end)) (org-invisible-p2))))
1037 (contents-end (progn (goto-char end)
1038 (skip-chars-backward " \r\t\n")
1039 (forward-line)
1040 (point)))
1041 (item
1042 (list 'item
1043 (list :bullet bullet
1044 :begin begin
1045 :end end
1046 ;; CONTENTS-BEGIN and CONTENTS-END may be
1047 ;; mixed up in the case of an empty item
1048 ;; separated from the next by a blank line.
1049 ;; Thus ensure the former is always the
1050 ;; smallest.
1051 :contents-begin (min contents-begin contents-end)
1052 :contents-end (max contents-begin contents-end)
1053 :checkbox checkbox
1054 :counter counter
1055 :hiddenp hidden
1056 :structure struct
1057 :post-blank (count-lines contents-end end)))))
1058 (org-element-put-property
1059 item :tag
1060 (let ((raw-tag (org-list-get-tag begin struct)))
1061 (and raw-tag
1062 (if raw-secondary-p raw-tag
1063 (org-element-parse-secondary-string
1064 raw-tag (org-element-restriction 'item) item))))))))
1066 (defun org-element-item-interpreter (item contents)
1067 "Interpret ITEM element as Org syntax.
1068 CONTENTS is the contents of the element."
1069 (let* ((bullet (org-list-bullet-string (org-element-property :bullet item)))
1070 (checkbox (org-element-property :checkbox item))
1071 (counter (org-element-property :counter item))
1072 (tag (let ((tag (org-element-property :tag item)))
1073 (and tag (org-element-interpret-data tag))))
1074 ;; Compute indentation.
1075 (ind (make-string (length bullet) 32))
1076 (item-starts-with-par-p
1077 (eq (org-element-type (car (org-element-contents item)))
1078 'paragraph)))
1079 ;; Indent contents.
1080 (concat
1081 bullet
1082 (and counter (format "[@%d] " counter))
1083 (case checkbox
1084 (on "[X] ")
1085 (off "[ ] ")
1086 (trans "[-] "))
1087 (and tag (format "%s :: " tag))
1088 (let ((contents (replace-regexp-in-string
1089 "\\(^\\)[ \t]*\\S-" ind contents nil nil 1)))
1090 (if item-starts-with-par-p (org-trim contents)
1091 (concat "\n" contents))))))
1094 ;;;; Plain List
1096 (defun org-element-plain-list-parser (limit affiliated structure)
1097 "Parse a plain list.
1099 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1100 the buffer position at the beginning of the first affiliated
1101 keyword and CDR is a plist of affiliated keywords along with
1102 their value. STRUCTURE is the structure of the plain list being
1103 parsed.
1105 Return a list whose CAR is `plain-list' and CDR is a plist
1106 containing `:type', `:begin', `:end', `:contents-begin' and
1107 `:contents-end', `:structure' and `:post-blank' keywords.
1109 Assume point is at the beginning of the list."
1110 (save-excursion
1111 (let* ((struct (or structure (org-list-struct)))
1112 (prevs (org-list-prevs-alist struct))
1113 (parents (org-list-parents-alist struct))
1114 (type (org-list-get-list-type (point) struct prevs))
1115 (contents-begin (point))
1116 (begin (car affiliated))
1117 (contents-end
1118 (progn (goto-char (org-list-get-list-end (point) struct prevs))
1119 (unless (bolp) (forward-line))
1120 (point)))
1121 (end (progn (skip-chars-forward " \r\t\n" limit)
1122 (if (eobp) (point) (point-at-bol)))))
1123 ;; Return value.
1124 (list 'plain-list
1125 (nconc
1126 (list :type type
1127 :begin begin
1128 :end end
1129 :contents-begin contents-begin
1130 :contents-end contents-end
1131 :structure struct
1132 :post-blank (count-lines contents-end end))
1133 (cdr affiliated))))))
1135 (defun org-element-plain-list-interpreter (plain-list contents)
1136 "Interpret PLAIN-LIST element as Org syntax.
1137 CONTENTS is the contents of the element."
1138 (with-temp-buffer
1139 (insert contents)
1140 (goto-char (point-min))
1141 (org-list-repair)
1142 (buffer-string)))
1145 ;;;; Property Drawer
1147 (defun org-element-property-drawer-parser (limit affiliated)
1148 "Parse a property drawer.
1150 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1151 the buffer position at the beginning of the first affiliated
1152 keyword and CDR is a plist of affiliated keywords along with
1153 their value.
1155 Return a list whose CAR is `property-drawer' and CDR is a plist
1156 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1157 `:contents-end' and `:post-blank' keywords.
1159 Assume point is at the beginning of the property drawer."
1160 (save-excursion
1161 (let ((case-fold-search t))
1162 (if (not (save-excursion
1163 (re-search-forward "^[ \t]*:END:[ \t]*$" limit t)))
1164 ;; Incomplete drawer: parse it as a paragraph.
1165 (org-element-paragraph-parser limit affiliated)
1166 (save-excursion
1167 (let* ((drawer-end-line (match-beginning 0))
1168 (begin (car affiliated))
1169 (contents-begin (progn (forward-line)
1170 (and (< (point) drawer-end-line)
1171 (point))))
1172 (contents-end (and contents-begin drawer-end-line))
1173 (hidden (org-invisible-p2))
1174 (pos-before-blank (progn (goto-char drawer-end-line)
1175 (forward-line)
1176 (point)))
1177 (end (progn (skip-chars-forward " \r\t\n" limit)
1178 (if (eobp) (point) (point-at-bol)))))
1179 (list 'property-drawer
1180 (nconc
1181 (list :begin begin
1182 :end end
1183 :hiddenp hidden
1184 :contents-begin contents-begin
1185 :contents-end contents-end
1186 :post-blank (count-lines pos-before-blank end))
1187 (cdr affiliated)))))))))
1189 (defun org-element-property-drawer-interpreter (property-drawer contents)
1190 "Interpret PROPERTY-DRAWER element as Org syntax.
1191 CONTENTS is the properties within the drawer."
1192 (format ":PROPERTIES:\n%s:END:" contents))
1195 ;;;; Quote Block
1197 (defun org-element-quote-block-parser (limit affiliated)
1198 "Parse a quote block.
1200 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1201 the buffer position at the beginning of the first affiliated
1202 keyword and CDR is a plist of affiliated keywords along with
1203 their value.
1205 Return a list whose CAR is `quote-block' and CDR is a plist
1206 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1207 `:contents-end' and `:post-blank' keywords.
1209 Assume point is at the beginning of the block."
1210 (let ((case-fold-search t))
1211 (if (not (save-excursion
1212 (re-search-forward "^[ \t]*#\\+END_QUOTE" limit t)))
1213 ;; Incomplete block: parse it as a paragraph.
1214 (org-element-paragraph-parser limit affiliated)
1215 (let ((block-end-line (match-beginning 0)))
1216 (save-excursion
1217 (let* ((begin (car affiliated))
1218 ;; Empty blocks have no contents.
1219 (contents-begin (progn (forward-line)
1220 (and (< (point) block-end-line)
1221 (point))))
1222 (contents-end (and contents-begin block-end-line))
1223 (hidden (org-invisible-p2))
1224 (pos-before-blank (progn (goto-char block-end-line)
1225 (forward-line)
1226 (point)))
1227 (end (progn (skip-chars-forward " \r\t\n" limit)
1228 (if (eobp) (point) (point-at-bol)))))
1229 (list 'quote-block
1230 (nconc
1231 (list :begin begin
1232 :end end
1233 :hiddenp hidden
1234 :contents-begin contents-begin
1235 :contents-end contents-end
1236 :post-blank (count-lines pos-before-blank end))
1237 (cdr affiliated)))))))))
1239 (defun org-element-quote-block-interpreter (quote-block contents)
1240 "Interpret QUOTE-BLOCK element as Org syntax.
1241 CONTENTS is the contents of the element."
1242 (format "#+BEGIN_QUOTE\n%s#+END_QUOTE" contents))
1245 ;;;; Section
1247 (defun org-element-section-parser (limit)
1248 "Parse a section.
1250 LIMIT bounds the search.
1252 Return a list whose CAR is `section' and CDR is a plist
1253 containing `:begin', `:end', `:contents-begin', `contents-end'
1254 and `:post-blank' keywords."
1255 (save-excursion
1256 ;; Beginning of section is the beginning of the first non-blank
1257 ;; line after previous headline.
1258 (let ((begin (point))
1259 (end (progn (org-with-limited-levels (outline-next-heading))
1260 (point)))
1261 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
1262 (forward-line)
1263 (point))))
1264 (list 'section
1265 (list :begin begin
1266 :end end
1267 :contents-begin begin
1268 :contents-end pos-before-blank
1269 :post-blank (count-lines pos-before-blank end))))))
1271 (defun org-element-section-interpreter (section contents)
1272 "Interpret SECTION element as Org syntax.
1273 CONTENTS is the contents of the element."
1274 contents)
1277 ;;;; Special Block
1279 (defun org-element-special-block-parser (limit affiliated)
1280 "Parse a special block.
1282 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1283 the buffer position at the beginning of the first affiliated
1284 keyword and CDR is a plist of affiliated keywords along with
1285 their value.
1287 Return a list whose CAR is `special-block' and CDR is a plist
1288 containing `:type', `:begin', `:end', `:hiddenp',
1289 `:contents-begin', `:contents-end' and `:post-blank' keywords.
1291 Assume point is at the beginning of the block."
1292 (let* ((case-fold-search t)
1293 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(S-+\\)")
1294 (upcase (match-string-no-properties 1)))))
1295 (if (not (save-excursion
1296 (re-search-forward (concat "^[ \t]*#\\+END_" type) limit t)))
1297 ;; Incomplete block: parse it as a paragraph.
1298 (org-element-paragraph-parser limit affiliated)
1299 (let ((block-end-line (match-beginning 0)))
1300 (save-excursion
1301 (let* ((begin (car affiliated))
1302 ;; Empty blocks have no contents.
1303 (contents-begin (progn (forward-line)
1304 (and (< (point) block-end-line)
1305 (point))))
1306 (contents-end (and contents-begin block-end-line))
1307 (hidden (org-invisible-p2))
1308 (pos-before-blank (progn (goto-char block-end-line)
1309 (forward-line)
1310 (point)))
1311 (end (progn (org-skip-whitespace)
1312 (if (eobp) (point) (point-at-bol)))))
1313 (list 'special-block
1314 (nconc
1315 (list :type type
1316 :begin begin
1317 :end end
1318 :hiddenp hidden
1319 :contents-begin contents-begin
1320 :contents-end contents-end
1321 :post-blank (count-lines pos-before-blank end))
1322 (cdr affiliated)))))))))
1324 (defun org-element-special-block-interpreter (special-block contents)
1325 "Interpret SPECIAL-BLOCK element as Org syntax.
1326 CONTENTS is the contents of the element."
1327 (let ((block-type (org-element-property :type special-block)))
1328 (format "#+BEGIN_%s\n%s#+END_%s" block-type contents block-type)))
1332 ;;; Elements
1334 ;; For each element, a parser and an interpreter are also defined.
1335 ;; Both follow the same naming convention used for greater elements.
1337 ;; Also, as for greater elements, adding a new element type is done
1338 ;; through the following steps: implement a parser and an interpreter,
1339 ;; tweak `org-element--current-element' so that it recognizes the new
1340 ;; type and add that new type to `org-element-all-elements'.
1342 ;; As a special case, when the newly defined type is a block type,
1343 ;; `org-element-block-name-alist' has to be modified accordingly.
1346 ;;;; Babel Call
1348 (defun org-element-babel-call-parser (limit affiliated)
1349 "Parse a babel call.
1351 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1352 the buffer position at the beginning of the first affiliated
1353 keyword and CDR is a plist of affiliated keywords along with
1354 their value.
1356 Return a list whose CAR is `babel-call' and CDR is a plist
1357 containing `:begin', `:end', `:info' and `:post-blank' as
1358 keywords."
1359 (save-excursion
1360 (let ((case-fold-search t)
1361 (info (progn (looking-at org-babel-block-lob-one-liner-regexp)
1362 (org-babel-lob-get-info)))
1363 (begin (car affiliated))
1364 (pos-before-blank (progn (forward-line) (point)))
1365 (end (progn (skip-chars-forward " \r\t\n" limit)
1366 (if (eobp) (point) (point-at-bol)))))
1367 (list 'babel-call
1368 (nconc
1369 (list :begin begin
1370 :end end
1371 :info info
1372 :post-blank (count-lines pos-before-blank end))
1373 (cdr affiliated))))))
1375 (defun org-element-babel-call-interpreter (babel-call contents)
1376 "Interpret BABEL-CALL element as Org syntax.
1377 CONTENTS is nil."
1378 (let* ((babel-info (org-element-property :info babel-call))
1379 (main (car babel-info))
1380 (post-options (nth 1 babel-info)))
1381 (concat "#+CALL: "
1382 (if (not (string-match "\\[\\(\\[.*?\\]\\)\\]" main)) main
1383 ;; Remove redundant square brackets.
1384 (replace-match (match-string 1 main) nil nil main))
1385 (and post-options (format "[%s]" post-options)))))
1388 ;;;; Clock
1390 (defun org-element-clock-parser (limit)
1391 "Parse a clock.
1393 LIMIT bounds the search.
1395 Return a list whose CAR is `clock' and CDR is a plist containing
1396 `:status', `:value', `:time', `:begin', `:end' and `:post-blank'
1397 as keywords."
1398 (save-excursion
1399 (let* ((case-fold-search nil)
1400 (begin (point))
1401 (value (progn (search-forward org-clock-string (line-end-position) t)
1402 (org-skip-whitespace)
1403 (looking-at "\\[.*\\]")
1404 (org-match-string-no-properties 0)))
1405 (time (and (progn (goto-char (match-end 0))
1406 (looking-at " +=> +\\(\\S-+\\)[ \t]*$"))
1407 (org-match-string-no-properties 1)))
1408 (status (if time 'closed 'running))
1409 (post-blank (let ((before-blank (progn (forward-line) (point))))
1410 (skip-chars-forward " \r\t\n" limit)
1411 (unless (eobp) (beginning-of-line))
1412 (count-lines before-blank (point))))
1413 (end (point)))
1414 (list 'clock
1415 (list :status status
1416 :value value
1417 :time time
1418 :begin begin
1419 :end end
1420 :post-blank post-blank)))))
1422 (defun org-element-clock-interpreter (clock contents)
1423 "Interpret CLOCK element as Org syntax.
1424 CONTENTS is nil."
1425 (concat org-clock-string " "
1426 (org-element-property :value clock)
1427 (let ((time (org-element-property :time clock)))
1428 (and time
1429 (concat " => "
1430 (apply 'format
1431 "%2s:%02s"
1432 (org-split-string time ":")))))))
1435 ;;;; Comment
1437 (defun org-element-comment-parser (limit affiliated)
1438 "Parse a comment.
1440 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1441 the buffer position at the beginning of the first affiliated
1442 keyword and CDR is a plist of affiliated keywords along with
1443 their value.
1445 Return a list whose CAR is `comment' and CDR is a plist
1446 containing `:begin', `:end', `:value' and `:post-blank'
1447 keywords.
1449 Assume point is at comment beginning."
1450 (save-excursion
1451 (let* ((begin (car affiliated))
1452 (value (prog2 (looking-at "[ \t]*# ?")
1453 (buffer-substring-no-properties
1454 (match-end 0) (line-end-position))
1455 (forward-line)))
1456 (com-end
1457 ;; Get comments ending.
1458 (progn
1459 (while (and (< (point) limit) (looking-at "[ \t]*#\\( \\|$\\)"))
1460 ;; Accumulate lines without leading hash and first
1461 ;; whitespace.
1462 (setq value
1463 (concat value
1464 "\n"
1465 (buffer-substring-no-properties
1466 (match-end 0) (line-end-position))))
1467 (forward-line))
1468 (point)))
1469 (end (progn (goto-char com-end)
1470 (skip-chars-forward " \r\t\n" limit)
1471 (if (eobp) (point) (point-at-bol)))))
1472 (list 'comment
1473 (nconc
1474 (list :begin begin
1475 :end end
1476 :value value
1477 :post-blank (count-lines com-end end))
1478 (cdr affiliated))))))
1480 (defun org-element-comment-interpreter (comment contents)
1481 "Interpret COMMENT element as Org syntax.
1482 CONTENTS is nil."
1483 (replace-regexp-in-string "^" "# " (org-element-property :value comment)))
1486 ;;;; Comment Block
1488 (defun org-element-comment-block-parser (limit affiliated)
1489 "Parse an export block.
1491 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1492 the buffer position at the beginning of the first affiliated
1493 keyword and CDR is a plist of affiliated keywords along with
1494 their value.
1496 Return a list whose CAR is `comment-block' and CDR is a plist
1497 containing `:begin', `:end', `:hiddenp', `:value' and
1498 `:post-blank' keywords.
1500 Assume point is at comment block beginning."
1501 (let ((case-fold-search t))
1502 (if (not (save-excursion
1503 (re-search-forward "^[ \t]*#\\+END_COMMENT" limit t)))
1504 ;; Incomplete block: parse it as a paragraph.
1505 (org-element-paragraph-parser limit affiliated)
1506 (let ((contents-end (match-beginning 0)))
1507 (save-excursion
1508 (let* ((begin (car affiliated))
1509 (contents-begin (progn (forward-line) (point)))
1510 (hidden (org-invisible-p2))
1511 (pos-before-blank (progn (goto-char contents-end)
1512 (forward-line)
1513 (point)))
1514 (end (progn (skip-chars-forward " \r\t\n" limit)
1515 (if (eobp) (point) (point-at-bol))))
1516 (value (buffer-substring-no-properties
1517 contents-begin contents-end)))
1518 (list 'comment-block
1519 (nconc
1520 (list :begin begin
1521 :end end
1522 :value value
1523 :hiddenp hidden
1524 :post-blank (count-lines pos-before-blank end))
1525 (cdr affiliated)))))))))
1527 (defun org-element-comment-block-interpreter (comment-block contents)
1528 "Interpret COMMENT-BLOCK element as Org syntax.
1529 CONTENTS is nil."
1530 (format "#+BEGIN_COMMENT\n%s#+END_COMMENT"
1531 (org-remove-indentation (org-element-property :value comment-block))))
1534 ;;;; Example Block
1536 (defun org-element-example-block-parser (limit affiliated)
1537 "Parse an example block.
1539 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1540 the buffer position at the beginning of the first affiliated
1541 keyword and CDR is a plist of affiliated keywords along with
1542 their value.
1544 Return a list whose CAR is `example-block' and CDR is a plist
1545 containing `:begin', `:end', `:number-lines', `:preserve-indent',
1546 `:retain-labels', `:use-labels', `:label-fmt', `:hiddenp',
1547 `:switches', `:value' and `:post-blank' keywords."
1548 (let ((case-fold-search t))
1549 (if (not (save-excursion
1550 (re-search-forward "^[ \t]*#\\+END_EXAMPLE" limit t)))
1551 ;; Incomplete block: parse it as a paragraph.
1552 (org-element-paragraph-parser limit affiliated)
1553 (let ((contents-end (match-beginning 0)))
1554 (save-excursion
1555 (let* ((switches
1556 (progn (looking-at "^[ \t]*#\\+BEGIN_EXAMPLE\\(?: +\\(.*\\)\\)?")
1557 (org-match-string-no-properties 1)))
1558 ;; Switches analysis
1559 (number-lines (cond ((not switches) nil)
1560 ((string-match "-n\\>" switches) 'new)
1561 ((string-match "+n\\>" switches) 'continued)))
1562 (preserve-indent (and switches (string-match "-i\\>" switches)))
1563 ;; Should labels be retained in (or stripped from) example
1564 ;; blocks?
1565 (retain-labels
1566 (or (not switches)
1567 (not (string-match "-r\\>" switches))
1568 (and number-lines (string-match "-k\\>" switches))))
1569 ;; What should code-references use - labels or
1570 ;; line-numbers?
1571 (use-labels
1572 (or (not switches)
1573 (and retain-labels (not (string-match "-k\\>" switches)))))
1574 (label-fmt (and switches
1575 (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
1576 (match-string 1 switches)))
1577 ;; Standard block parsing.
1578 (begin (car affiliated))
1579 (contents-begin (progn (forward-line) (point)))
1580 (hidden (org-invisible-p2))
1581 (value (buffer-substring-no-properties contents-begin contents-end))
1582 (pos-before-blank (progn (goto-char contents-end)
1583 (forward-line)
1584 (point)))
1585 (end (progn (skip-chars-forward " \r\t\n" limit)
1586 (if (eobp) (point) (point-at-bol)))))
1587 (list 'example-block
1588 (nconc
1589 (list :begin begin
1590 :end end
1591 :value value
1592 :switches switches
1593 :number-lines number-lines
1594 :preserve-indent preserve-indent
1595 :retain-labels retain-labels
1596 :use-labels use-labels
1597 :label-fmt label-fmt
1598 :hiddenp hidden
1599 :post-blank (count-lines pos-before-blank end))
1600 (cdr affiliated)))))))))
1602 (defun org-element-example-block-interpreter (example-block contents)
1603 "Interpret EXAMPLE-BLOCK element as Org syntax.
1604 CONTENTS is nil."
1605 (let ((switches (org-element-property :switches example-block)))
1606 (concat "#+BEGIN_EXAMPLE" (and switches (concat " " switches)) "\n"
1607 (org-remove-indentation
1608 (org-element-property :value example-block))
1609 "#+END_EXAMPLE")))
1612 ;;;; Export Block
1614 (defun org-element-export-block-parser (limit affiliated)
1615 "Parse an export block.
1617 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1618 the buffer position at the beginning of the first affiliated
1619 keyword and CDR is a plist of affiliated keywords along with
1620 their value.
1622 Return a list whose CAR is `export-block' and CDR is a plist
1623 containing `:begin', `:end', `:type', `:hiddenp', `:value' and
1624 `:post-blank' keywords.
1626 Assume point is at export-block beginning."
1627 (let* ((case-fold-search t)
1628 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
1629 (upcase (org-match-string-no-properties 1)))))
1630 (if (not (save-excursion
1631 (re-search-forward (concat "^[ \t]*#\\+END_" type) limit t)))
1632 ;; Incomplete block: parse it as a paragraph.
1633 (org-element-paragraph-parser limit affiliated)
1634 (let ((contents-end (match-beginning 0)))
1635 (save-excursion
1636 (let* ((begin (car affiliated))
1637 (contents-begin (progn (forward-line) (point)))
1638 (hidden (org-invisible-p2))
1639 (pos-before-blank (progn (goto-char contents-end)
1640 (forward-line)
1641 (point)))
1642 (end (progn (skip-chars-forward " \r\t\n" limit)
1643 (if (eobp) (point) (point-at-bol))))
1644 (value (buffer-substring-no-properties contents-begin
1645 contents-end)))
1646 (list 'export-block
1647 (nconc
1648 (list :begin begin
1649 :end end
1650 :type type
1651 :value value
1652 :hiddenp hidden
1653 :post-blank (count-lines pos-before-blank end))
1654 (cdr affiliated)))))))))
1656 (defun org-element-export-block-interpreter (export-block contents)
1657 "Interpret EXPORT-BLOCK element as Org syntax.
1658 CONTENTS is nil."
1659 (let ((type (org-element-property :type export-block)))
1660 (concat (format "#+BEGIN_%s\n" type)
1661 (org-element-property :value export-block)
1662 (format "#+END_%s" type))))
1665 ;;;; Fixed-width
1667 (defun org-element-fixed-width-parser (limit affiliated)
1668 "Parse a fixed-width section.
1670 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1671 the buffer position at the beginning of the first affiliated
1672 keyword and CDR is a plist of affiliated keywords along with
1673 their value.
1675 Return a list whose CAR is `fixed-width' and CDR is a plist
1676 containing `:begin', `:end', `:value' and `:post-blank' keywords.
1678 Assume point is at the beginning of the fixed-width area."
1679 (save-excursion
1680 (let* ((begin (car affiliated))
1681 value
1682 (end-area
1683 (progn
1684 (while (and (< (point) limit)
1685 (looking-at "[ \t]*:\\( \\|$\\)"))
1686 ;; Accumulate text without starting colons.
1687 (setq value
1688 (concat value
1689 (buffer-substring-no-properties
1690 (match-end 0) (point-at-eol))
1691 "\n"))
1692 (forward-line))
1693 (point)))
1694 (end (progn (skip-chars-forward " \r\t\n" limit)
1695 (if (eobp) (point) (point-at-bol)))))
1696 (list 'fixed-width
1697 (nconc
1698 (list :begin begin
1699 :end end
1700 :value value
1701 :post-blank (count-lines end-area end))
1702 (cdr affiliated))))))
1704 (defun org-element-fixed-width-interpreter (fixed-width contents)
1705 "Interpret FIXED-WIDTH element as Org syntax.
1706 CONTENTS is nil."
1707 (replace-regexp-in-string
1708 "^" ": " (substring (org-element-property :value fixed-width) 0 -1)))
1711 ;;;; Horizontal Rule
1713 (defun org-element-horizontal-rule-parser (limit affiliated)
1714 "Parse an horizontal rule.
1716 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1717 the buffer position at the beginning of the first affiliated
1718 keyword and CDR is a plist of affiliated keywords along with
1719 their value.
1721 Return a list whose CAR is `horizontal-rule' and CDR is a plist
1722 containing `:begin', `:end' and `:post-blank' keywords."
1723 (save-excursion
1724 (let ((begin (car affiliated))
1725 (post-hr (progn (forward-line) (point)))
1726 (end (progn (skip-chars-forward " \r\t\n" limit)
1727 (if (eobp) (point) (point-at-bol)))))
1728 (list 'horizontal-rule
1729 (nconc
1730 (list :begin begin
1731 :end end
1732 :post-blank (count-lines post-hr end))
1733 (cdr affiliated))))))
1735 (defun org-element-horizontal-rule-interpreter (horizontal-rule contents)
1736 "Interpret HORIZONTAL-RULE element as Org syntax.
1737 CONTENTS is nil."
1738 "-----")
1741 ;;;; Keyword
1743 (defun org-element-keyword-parser (limit affiliated)
1744 "Parse a keyword at point.
1746 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1747 the buffer position at the beginning of the first affiliated
1748 keyword and CDR is a plist of affiliated keywords along with
1749 their value.
1751 Return a list whose CAR is `keyword' and CDR is a plist
1752 containing `:key', `:value', `:begin', `:end' and `:post-blank'
1753 keywords."
1754 (save-excursion
1755 (let ((case-fold-search t)
1756 (begin (car affiliated))
1757 (key (progn (looking-at "[ \t]*#\\+\\(\\S-+*\\):")
1758 (upcase (org-match-string-no-properties 1))))
1759 (value (org-trim (buffer-substring-no-properties
1760 (match-end 0) (point-at-eol))))
1761 (pos-before-blank (progn (forward-line) (point)))
1762 (end (progn (skip-chars-forward " \r\t\n" limit)
1763 (if (eobp) (point) (point-at-bol)))))
1764 (list 'keyword
1765 (nconc
1766 (list :key key
1767 :value value
1768 :begin begin
1769 :end end
1770 :post-blank (count-lines pos-before-blank end))
1771 (cdr affiliated))))))
1773 (defun org-element-keyword-interpreter (keyword contents)
1774 "Interpret KEYWORD element as Org syntax.
1775 CONTENTS is nil."
1776 (format "#+%s: %s"
1777 (org-element-property :key keyword)
1778 (org-element-property :value keyword)))
1781 ;;;; Latex Environment
1783 (defun org-element-latex-environment-parser (limit affiliated)
1784 "Parse a LaTeX environment.
1786 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1787 the buffer position at the beginning of the first affiliated
1788 keyword and CDR is a plist of affiliated keywords along with
1789 their value.
1791 Return a list whose CAR is `latex-environment' and CDR is a plist
1792 containing `:begin', `:end', `:value' and `:post-blank'
1793 keywords.
1795 Assume point is at the beginning of the latex environment."
1796 (save-excursion
1797 (let* ((case-fold-search t)
1798 (code-begin (point))
1799 (begin (car affiliated))
1800 (env (progn (looking-at "^[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}")
1801 (regexp-quote (match-string 1))))
1802 (code-end
1803 (progn (re-search-forward (format "^[ \t]*\\\\end{%s}" env) limit t)
1804 (forward-line)
1805 (point)))
1806 (value (buffer-substring-no-properties code-begin code-end))
1807 (end (progn (skip-chars-forward " \r\t\n" limit)
1808 (if (eobp) (point) (point-at-bol)))))
1809 (list 'latex-environment
1810 (nconc
1811 (list :begin begin
1812 :end end
1813 :value value
1814 :post-blank (count-lines code-end end))
1815 (cdr affiliated))))))
1817 (defun org-element-latex-environment-interpreter (latex-environment contents)
1818 "Interpret LATEX-ENVIRONMENT element as Org syntax.
1819 CONTENTS is nil."
1820 (org-element-property :value latex-environment))
1823 ;;;; Node Property
1825 (defun org-element-node-property-parser (limit)
1826 "Parse a node-property at point.
1828 LIMIT bounds the search.
1830 Return a list whose CAR is `node-property' and CDR is a plist
1831 containing `:key', `:value', `:begin', `:end' and `:post-blank'
1832 keywords."
1833 (save-excursion
1834 (let ((case-fold-search t)
1835 (begin (point))
1836 (key (progn (looking-at "[ \t]*:\\(.*?\\):[ \t]+\\(.*?\\)[ \t]*$")
1837 (org-match-string-no-properties 1)))
1838 (value (org-match-string-no-properties 2))
1839 (pos-before-blank (progn (forward-line) (point)))
1840 (end (progn (skip-chars-forward " \r\t\n" limit)
1841 (if (eobp) (point) (point-at-bol)))))
1842 (list 'node-property
1843 (list :key key
1844 :value value
1845 :begin begin
1846 :end end
1847 :post-blank (count-lines pos-before-blank end))))))
1849 (defun org-element-node-property-interpreter (node-property contents)
1850 "Interpret NODE-PROPERTY element as Org syntax.
1851 CONTENTS is nil."
1852 (format org-property-format
1853 (format ":%s:" (org-element-property :key node-property))
1854 (org-element-property :value node-property)))
1857 ;;;; Paragraph
1859 (defun org-element-paragraph-parser (limit affiliated)
1860 "Parse a paragraph.
1862 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1863 the buffer position at the beginning of the first affiliated
1864 keyword and CDR is a plist of affiliated keywords along with
1865 their value.
1867 Return a list whose CAR is `paragraph' and CDR is a plist
1868 containing `:begin', `:end', `:contents-begin' and
1869 `:contents-end' and `:post-blank' keywords.
1871 Assume point is at the beginning of the paragraph."
1872 (save-excursion
1873 (let* ((begin (car affiliated))
1874 (contents-begin (point))
1875 (before-blank
1876 (let ((case-fold-search t))
1877 (end-of-line)
1878 (re-search-forward org-element-paragraph-separate limit 'm)
1879 (while (and (/= (point) limit)
1880 (cond
1881 ;; Skip non-existent or incomplete drawer.
1882 ((save-excursion
1883 (beginning-of-line)
1884 (and (looking-at "[ \t]*:\\S-")
1885 (or (not (looking-at org-drawer-regexp))
1886 (not (save-excursion
1887 (re-search-forward
1888 "^[ \t]*:END:" limit t)))))))
1889 ;; Stop at comments.
1890 ((save-excursion
1891 (beginning-of-line)
1892 (not (looking-at "[ \t]*#\\S-"))) nil)
1893 ;; Skip incomplete dynamic blocks.
1894 ((save-excursion
1895 (beginning-of-line)
1896 (looking-at "[ \t]*#\\+BEGIN: "))
1897 (not (save-excursion
1898 (re-search-forward
1899 "^[ \t]*\\+END:" limit t))))
1900 ;; Skip incomplete blocks.
1901 ((save-excursion
1902 (beginning-of-line)
1903 (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)"))
1904 (not (save-excursion
1905 (re-search-forward
1906 (concat "^[ \t]*#\\+END_"
1907 (match-string 1))
1908 limit t))))
1909 ;; Skip incomplete latex environments.
1910 ((save-excursion
1911 (beginning-of-line)
1912 (looking-at "^[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}"))
1913 (not (save-excursion
1914 (re-search-forward
1915 (format "^[ \t]*\\\\end{%s}"
1916 (match-string 1))
1917 limit t))))
1918 ;; Skip ill-formed keywords.
1919 ((not (save-excursion
1920 (beginning-of-line)
1921 (looking-at "[ \t]*#\\+\\S-+:"))))))
1922 (re-search-forward org-element-paragraph-separate limit 'm))
1923 (if (eobp) (point) (goto-char (line-beginning-position)))))
1924 (contents-end (progn (skip-chars-backward " \r\t\n" contents-begin)
1925 (forward-line)
1926 (point)))
1927 (end (progn (skip-chars-forward " \r\t\n" limit)
1928 (if (eobp) (point) (point-at-bol)))))
1929 (list 'paragraph
1930 (nconc
1931 (list :begin begin
1932 :end end
1933 :contents-begin contents-begin
1934 :contents-end contents-end
1935 :post-blank (count-lines before-blank end))
1936 (cdr affiliated))))))
1938 (defun org-element-paragraph-interpreter (paragraph contents)
1939 "Interpret PARAGRAPH element as Org syntax.
1940 CONTENTS is the contents of the element."
1941 contents)
1944 ;;;; Planning
1946 (defun org-element-planning-parser (limit)
1947 "Parse a planning.
1949 LIMIT bounds the search.
1951 Return a list whose CAR is `planning' and CDR is a plist
1952 containing `:closed', `:deadline', `:scheduled', `:begin', `:end'
1953 and `:post-blank' keywords."
1954 (save-excursion
1955 (let* ((case-fold-search nil)
1956 (begin (point))
1957 (post-blank (let ((before-blank (progn (forward-line) (point))))
1958 (skip-chars-forward " \r\t\n" limit)
1959 (unless (eobp) (beginning-of-line))
1960 (count-lines before-blank (point))))
1961 (end (point))
1962 closed deadline scheduled)
1963 (goto-char begin)
1964 (while (re-search-forward org-keyword-time-not-clock-regexp
1965 (line-end-position) t)
1966 (goto-char (match-end 1))
1967 (org-skip-whitespace)
1968 (let ((time (buffer-substring-no-properties
1969 (1+ (point)) (1- (match-end 0))))
1970 (keyword (match-string 1)))
1971 (cond ((equal keyword org-closed-string) (setq closed time))
1972 ((equal keyword org-deadline-string) (setq deadline time))
1973 (t (setq scheduled time)))))
1974 (list 'planning
1975 (list :closed closed
1976 :deadline deadline
1977 :scheduled scheduled
1978 :begin begin
1979 :end end
1980 :post-blank post-blank)))))
1982 (defun org-element-planning-interpreter (planning contents)
1983 "Interpret PLANNING element as Org syntax.
1984 CONTENTS is nil."
1985 (mapconcat
1986 'identity
1987 (delq nil
1988 (list (let ((closed (org-element-property :closed planning)))
1989 (when closed (concat org-closed-string " [" closed "]")))
1990 (let ((deadline (org-element-property :deadline planning)))
1991 (when deadline (concat org-deadline-string " <" deadline ">")))
1992 (let ((scheduled (org-element-property :scheduled planning)))
1993 (when scheduled
1994 (concat org-scheduled-string " <" scheduled ">")))))
1995 " "))
1998 ;;;; Quote Section
2000 (defun org-element-quote-section-parser (limit)
2001 "Parse a quote section.
2003 LIMIT bounds the search.
2005 Return a list whose CAR is `quote-section' and CDR is a plist
2006 containing `:begin', `:end', `:value' and `:post-blank' keywords.
2008 Assume point is at beginning of the section."
2009 (save-excursion
2010 (let* ((begin (point))
2011 (end (progn (org-with-limited-levels (outline-next-heading))
2012 (point)))
2013 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
2014 (forward-line)
2015 (point)))
2016 (value (buffer-substring-no-properties begin pos-before-blank)))
2017 (list 'quote-section
2018 (list :begin begin
2019 :end end
2020 :value value
2021 :post-blank (count-lines pos-before-blank end))))))
2023 (defun org-element-quote-section-interpreter (quote-section contents)
2024 "Interpret QUOTE-SECTION element as Org syntax.
2025 CONTENTS is nil."
2026 (org-element-property :value quote-section))
2029 ;;;; Src Block
2031 (defun org-element-src-block-parser (limit affiliated)
2032 "Parse a src block.
2034 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2035 the buffer position at the beginning of the first affiliated
2036 keyword and CDR is a plist of affiliated keywords along with
2037 their value.
2039 Return a list whose CAR is `src-block' and CDR is a plist
2040 containing `:language', `:switches', `:parameters', `:begin',
2041 `:end', `:hiddenp', `:number-lines', `:retain-labels',
2042 `:use-labels', `:label-fmt', `:preserve-indent', `:value' and
2043 `:post-blank' keywords.
2045 Assume point is at the beginning of the block."
2046 (let ((case-fold-search t))
2047 (if (not (save-excursion (re-search-forward "^[ \t]*#\\+END_SRC" limit t)))
2048 ;; Incomplete block: parse it as a paragraph.
2049 (org-element-paragraph-parser limit affiliated)
2050 (let ((contents-end (match-beginning 0)))
2051 (save-excursion
2052 (let* ((begin (car affiliated))
2053 ;; Get language as a string.
2054 (language
2055 (progn
2056 (looking-at
2057 (concat "^[ \t]*#\\+BEGIN_SRC"
2058 "\\(?: +\\(\\S-+\\)\\)?"
2059 "\\(\\(?: +\\(?:-l \".*?\"\\|[-+][A-Za-z]\\)\\)+\\)?"
2060 "\\(.*\\)[ \t]*$"))
2061 (org-match-string-no-properties 1)))
2062 ;; Get switches.
2063 (switches (org-match-string-no-properties 2))
2064 ;; Get parameters.
2065 (parameters (org-match-string-no-properties 3))
2066 ;; Switches analysis
2067 (number-lines (cond ((not switches) nil)
2068 ((string-match "-n\\>" switches) 'new)
2069 ((string-match "+n\\>" switches) 'continued)))
2070 (preserve-indent (and switches (string-match "-i\\>" switches)))
2071 (label-fmt (and switches
2072 (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
2073 (match-string 1 switches)))
2074 ;; Should labels be retained in (or stripped from)
2075 ;; src blocks?
2076 (retain-labels
2077 (or (not switches)
2078 (not (string-match "-r\\>" switches))
2079 (and number-lines (string-match "-k\\>" switches))))
2080 ;; What should code-references use - labels or
2081 ;; line-numbers?
2082 (use-labels
2083 (or (not switches)
2084 (and retain-labels (not (string-match "-k\\>" switches)))))
2085 ;; Get visibility status.
2086 (hidden (progn (forward-line) (org-invisible-p2)))
2087 ;; Retrieve code.
2088 (value (buffer-substring-no-properties (point) contents-end))
2089 (pos-before-blank (progn (goto-char contents-end)
2090 (forward-line)
2091 (point)))
2092 ;; Get position after ending blank lines.
2093 (end (progn (skip-chars-forward " \r\t\n" limit)
2094 (if (eobp) (point) (point-at-bol)))))
2095 (list 'src-block
2096 (nconc
2097 (list :language language
2098 :switches (and (org-string-nw-p switches)
2099 (org-trim switches))
2100 :parameters (and (org-string-nw-p parameters)
2101 (org-trim parameters))
2102 :begin begin
2103 :end end
2104 :number-lines number-lines
2105 :preserve-indent preserve-indent
2106 :retain-labels retain-labels
2107 :use-labels use-labels
2108 :label-fmt label-fmt
2109 :hiddenp hidden
2110 :value value
2111 :post-blank (count-lines pos-before-blank end))
2112 (cdr affiliated)))))))))
2114 (defun org-element-src-block-interpreter (src-block contents)
2115 "Interpret SRC-BLOCK element as Org syntax.
2116 CONTENTS is nil."
2117 (let ((lang (org-element-property :language src-block))
2118 (switches (org-element-property :switches src-block))
2119 (params (org-element-property :parameters src-block))
2120 (value (let ((val (org-element-property :value src-block)))
2121 (cond
2123 (org-src-preserve-indentation val)
2124 ((zerop org-edit-src-content-indentation)
2125 (org-remove-indentation val))
2127 (let ((ind (make-string
2128 org-edit-src-content-indentation 32)))
2129 (replace-regexp-in-string
2130 "\\(^\\)[ \t]*\\S-" ind
2131 (org-remove-indentation val) nil nil 1)))))))
2132 (concat (format "#+BEGIN_SRC%s\n"
2133 (concat (and lang (concat " " lang))
2134 (and switches (concat " " switches))
2135 (and params (concat " " params))))
2136 value
2137 "#+END_SRC")))
2140 ;;;; Table
2142 (defun org-element-table-parser (limit affiliated)
2143 "Parse a table at point.
2145 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2146 the buffer position at the beginning of the first affiliated
2147 keyword and CDR is a plist of affiliated keywords along with
2148 their value.
2150 Return a list whose CAR is `table' and CDR is a plist containing
2151 `:begin', `:end', `:tblfm', `:type', `:contents-begin',
2152 `:contents-end', `:value' and `:post-blank' keywords.
2154 Assume point is at the beginning of the table."
2155 (save-excursion
2156 (let* ((case-fold-search t)
2157 (table-begin (point))
2158 (type (if (org-at-table.el-p) 'table.el 'org))
2159 (keywords (org-element--collect-affiliated-keywords))
2160 (begin (car affiliated))
2161 (table-end (goto-char (marker-position (org-table-end t))))
2162 (tblfm (let (acc)
2163 (while (looking-at "[ \t]*#\\+TBLFM: +\\(.*\\)[ \t]*$")
2164 (push (org-match-string-no-properties 1) acc)
2165 (forward-line))
2166 acc))
2167 (pos-before-blank (point))
2168 (end (progn (skip-chars-forward " \r\t\n" limit)
2169 (if (eobp) (point) (point-at-bol)))))
2170 (list 'table
2171 (nconc
2172 (list :begin begin
2173 :end end
2174 :type type
2175 :tblfm tblfm
2176 ;; Only `org' tables have contents. `table.el' tables
2177 ;; use a `:value' property to store raw table as
2178 ;; a string.
2179 :contents-begin (and (eq type 'org) table-begin)
2180 :contents-end (and (eq type 'org) table-end)
2181 :value (and (eq type 'table.el)
2182 (buffer-substring-no-properties
2183 table-begin table-end))
2184 :post-blank (count-lines pos-before-blank end))
2185 (cdr affiliated))))))
2187 (defun org-element-table-interpreter (table contents)
2188 "Interpret TABLE element as Org syntax.
2189 CONTENTS is nil."
2190 (if (eq (org-element-property :type table) 'table.el)
2191 (org-remove-indentation (org-element-property :value table))
2192 (concat (with-temp-buffer (insert contents)
2193 (org-table-align)
2194 (buffer-string))
2195 (mapconcat (lambda (fm) (concat "#+TBLFM: " fm))
2196 (reverse (org-element-property :tblfm table))
2197 "\n"))))
2200 ;;;; Table Row
2202 (defun org-element-table-row-parser (limit)
2203 "Parse table row at point.
2205 LIMIT bounds the search.
2207 Return a list whose CAR is `table-row' and CDR is a plist
2208 containing `:begin', `:end', `:contents-begin', `:contents-end',
2209 `:type' and `:post-blank' keywords."
2210 (save-excursion
2211 (let* ((type (if (looking-at "^[ \t]*|-") 'rule 'standard))
2212 (begin (point))
2213 ;; A table rule has no contents. In that case, ensure
2214 ;; CONTENTS-BEGIN matches CONTENTS-END.
2215 (contents-begin (and (eq type 'standard)
2216 (search-forward "|")
2217 (point)))
2218 (contents-end (and (eq type 'standard)
2219 (progn
2220 (end-of-line)
2221 (skip-chars-backward " \t")
2222 (point))))
2223 (end (progn (forward-line) (point))))
2224 (list 'table-row
2225 (list :type type
2226 :begin begin
2227 :end end
2228 :contents-begin contents-begin
2229 :contents-end contents-end
2230 :post-blank 0)))))
2232 (defun org-element-table-row-interpreter (table-row contents)
2233 "Interpret TABLE-ROW element as Org syntax.
2234 CONTENTS is the contents of the table row."
2235 (if (eq (org-element-property :type table-row) 'rule) "|-"
2236 (concat "| " contents)))
2239 ;;;; Verse Block
2241 (defun org-element-verse-block-parser (limit affiliated)
2242 "Parse a verse block.
2244 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2245 the buffer position at the beginning of the first affiliated
2246 keyword and CDR is a plist of affiliated keywords along with
2247 their value.
2249 Return a list whose CAR is `verse-block' and CDR is a plist
2250 containing `:begin', `:end', `:contents-begin', `:contents-end',
2251 `:hiddenp' and `:post-blank' keywords.
2253 Assume point is at beginning of the block."
2254 (let ((case-fold-search t))
2255 (if (not (save-excursion
2256 (re-search-forward "^[ \t]*#\\+END_VERSE" limit t)))
2257 ;; Incomplete block: parse it as a paragraph.
2258 (org-element-paragraph-parser limit affiliated)
2259 (let ((contents-end (match-beginning 0)))
2260 (save-excursion
2261 (let* ((begin (car affiliated))
2262 (hidden (progn (forward-line) (org-invisible-p2)))
2263 (contents-begin (point))
2264 (pos-before-blank (progn (goto-char contents-end)
2265 (forward-line)
2266 (point)))
2267 (end (progn (skip-chars-forward " \r\t\n" limit)
2268 (if (eobp) (point) (point-at-bol)))))
2269 (list 'verse-block
2270 (nconc
2271 (list :begin begin
2272 :end end
2273 :contents-begin contents-begin
2274 :contents-end contents-end
2275 :hiddenp hidden
2276 :post-blank (count-lines pos-before-blank end))
2277 (cdr affiliated)))))))))
2279 (defun org-element-verse-block-interpreter (verse-block contents)
2280 "Interpret VERSE-BLOCK element as Org syntax.
2281 CONTENTS is verse block contents."
2282 (format "#+BEGIN_VERSE\n%s#+END_VERSE" contents))
2286 ;;; Objects
2288 ;; Unlike to elements, interstices can be found between objects.
2289 ;; That's why, along with the parser, successor functions are provided
2290 ;; for each object. Some objects share the same successor (i.e. `code'
2291 ;; and `verbatim' objects).
2293 ;; A successor must accept a single argument bounding the search. It
2294 ;; will return either a cons cell whose CAR is the object's type, as
2295 ;; a symbol, and CDR the position of its next occurrence, or nil.
2297 ;; Successors follow the naming convention:
2298 ;; org-element-NAME-successor, where NAME is the name of the
2299 ;; successor, as defined in `org-element-all-successors'.
2301 ;; Some object types (i.e. `italic') are recursive. Restrictions on
2302 ;; object types they can contain will be specified in
2303 ;; `org-element-object-restrictions'.
2305 ;; Adding a new type of object is simple. Implement a successor,
2306 ;; a parser, and an interpreter for it, all following the naming
2307 ;; convention. Register type in `org-element-all-objects' and
2308 ;; successor in `org-element-all-successors'. Maybe tweak
2309 ;; restrictions about it, and that's it.
2312 ;;;; Bold
2314 (defun org-element-bold-parser ()
2315 "Parse bold object at point.
2317 Return a list whose CAR is `bold' and CDR is a plist with
2318 `:begin', `:end', `:contents-begin' and `:contents-end' and
2319 `:post-blank' keywords.
2321 Assume point is at the first star marker."
2322 (save-excursion
2323 (unless (bolp) (backward-char 1))
2324 (looking-at org-emph-re)
2325 (let ((begin (match-beginning 2))
2326 (contents-begin (match-beginning 4))
2327 (contents-end (match-end 4))
2328 (post-blank (progn (goto-char (match-end 2))
2329 (skip-chars-forward " \t")))
2330 (end (point)))
2331 (list 'bold
2332 (list :begin begin
2333 :end end
2334 :contents-begin contents-begin
2335 :contents-end contents-end
2336 :post-blank post-blank)))))
2338 (defun org-element-bold-interpreter (bold contents)
2339 "Interpret BOLD object as Org syntax.
2340 CONTENTS is the contents of the object."
2341 (format "*%s*" contents))
2343 (defun org-element-text-markup-successor (limit)
2344 "Search for the next text-markup object.
2346 LIMIT bounds the search.
2348 Return value is a cons cell whose CAR is a symbol among `bold',
2349 `italic', `underline', `strike-through', `code' and `verbatim'
2350 and CDR is beginning position."
2351 (save-excursion
2352 (unless (bolp) (backward-char))
2353 (when (re-search-forward org-emph-re limit t)
2354 (let ((marker (match-string 3)))
2355 (cons (cond
2356 ((equal marker "*") 'bold)
2357 ((equal marker "/") 'italic)
2358 ((equal marker "_") 'underline)
2359 ((equal marker "+") 'strike-through)
2360 ((equal marker "~") 'code)
2361 ((equal marker "=") 'verbatim)
2362 (t (error "Unknown marker at %d" (match-beginning 3))))
2363 (match-beginning 2))))))
2366 ;;;; Code
2368 (defun org-element-code-parser ()
2369 "Parse code object at point.
2371 Return a list whose CAR is `code' and CDR is a plist with
2372 `:value', `:begin', `:end' and `:post-blank' keywords.
2374 Assume point is at the first tilde marker."
2375 (save-excursion
2376 (unless (bolp) (backward-char 1))
2377 (looking-at org-emph-re)
2378 (let ((begin (match-beginning 2))
2379 (value (org-match-string-no-properties 4))
2380 (post-blank (progn (goto-char (match-end 2))
2381 (skip-chars-forward " \t")))
2382 (end (point)))
2383 (list 'code
2384 (list :value value
2385 :begin begin
2386 :end end
2387 :post-blank post-blank)))))
2389 (defun org-element-code-interpreter (code contents)
2390 "Interpret CODE object as Org syntax.
2391 CONTENTS is nil."
2392 (format "~%s~" (org-element-property :value code)))
2395 ;;;; Entity
2397 (defun org-element-entity-parser ()
2398 "Parse entity at point.
2400 Return a list whose CAR is `entity' and CDR a plist with
2401 `:begin', `:end', `:latex', `:latex-math-p', `:html', `:latin1',
2402 `:utf-8', `:ascii', `:use-brackets-p' and `:post-blank' as
2403 keywords.
2405 Assume point is at the beginning of the entity."
2406 (save-excursion
2407 (looking-at "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)")
2408 (let* ((value (org-entity-get (match-string 1)))
2409 (begin (match-beginning 0))
2410 (bracketsp (string= (match-string 2) "{}"))
2411 (post-blank (progn (goto-char (match-end 1))
2412 (when bracketsp (forward-char 2))
2413 (skip-chars-forward " \t")))
2414 (end (point)))
2415 (list 'entity
2416 (list :name (car value)
2417 :latex (nth 1 value)
2418 :latex-math-p (nth 2 value)
2419 :html (nth 3 value)
2420 :ascii (nth 4 value)
2421 :latin1 (nth 5 value)
2422 :utf-8 (nth 6 value)
2423 :begin begin
2424 :end end
2425 :use-brackets-p bracketsp
2426 :post-blank post-blank)))))
2428 (defun org-element-entity-interpreter (entity contents)
2429 "Interpret ENTITY object as Org syntax.
2430 CONTENTS is nil."
2431 (concat "\\"
2432 (org-element-property :name entity)
2433 (when (org-element-property :use-brackets-p entity) "{}")))
2435 (defun org-element-latex-or-entity-successor (limit)
2436 "Search for the next latex-fragment or entity object.
2438 LIMIT bounds the search.
2440 Return value is a cons cell whose CAR is `entity' or
2441 `latex-fragment' and CDR is beginning position."
2442 (save-excursion
2443 (let ((matchers
2444 (remove "begin" (plist-get org-format-latex-options :matchers)))
2445 ;; ENTITY-RE matches both LaTeX commands and Org entities.
2446 (entity-re
2447 "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))
2448 (when (re-search-forward
2449 (concat (mapconcat (lambda (e) (nth 1 (assoc e org-latex-regexps)))
2450 matchers "\\|")
2451 "\\|" entity-re)
2452 limit t)
2453 (goto-char (match-beginning 0))
2454 (if (looking-at entity-re)
2455 ;; Determine if it's a real entity or a LaTeX command.
2456 (cons (if (org-entity-get (match-string 1)) 'entity 'latex-fragment)
2457 (match-beginning 0))
2458 ;; No entity nor command: point is at a LaTeX fragment.
2459 ;; Determine its type to get the correct beginning position.
2460 (cons 'latex-fragment
2461 (catch 'return
2462 (mapc (lambda (e)
2463 (when (looking-at (nth 1 (assoc e org-latex-regexps)))
2464 (throw 'return
2465 (match-beginning
2466 (nth 2 (assoc e org-latex-regexps))))))
2467 matchers)
2468 (point))))))))
2471 ;;;; Export Snippet
2473 (defun org-element-export-snippet-parser ()
2474 "Parse export snippet at point.
2476 Return a list whose CAR is `export-snippet' and CDR a plist with
2477 `:begin', `:end', `:back-end', `:value' and `:post-blank' as
2478 keywords.
2480 Assume point is at the beginning of the snippet."
2481 (save-excursion
2482 (re-search-forward "@@\\([-A-Za-z0-9]+\\):" nil t)
2483 (let* ((begin (match-beginning 0))
2484 (back-end (org-match-string-no-properties 1))
2485 (value (buffer-substring-no-properties
2486 (point)
2487 (progn (re-search-forward "@@" nil t) (match-beginning 0))))
2488 (post-blank (skip-chars-forward " \t"))
2489 (end (point)))
2490 (list 'export-snippet
2491 (list :back-end back-end
2492 :value value
2493 :begin begin
2494 :end end
2495 :post-blank post-blank)))))
2497 (defun org-element-export-snippet-interpreter (export-snippet contents)
2498 "Interpret EXPORT-SNIPPET object as Org syntax.
2499 CONTENTS is nil."
2500 (format "@@%s:%s@@"
2501 (org-element-property :back-end export-snippet)
2502 (org-element-property :value export-snippet)))
2504 (defun org-element-export-snippet-successor (limit)
2505 "Search for the next export-snippet object.
2507 LIMIT bounds the search.
2509 Return value is a cons cell whose CAR is `export-snippet' and CDR
2510 its beginning position."
2511 (save-excursion
2512 (let (beg)
2513 (when (and (re-search-forward "@@[-A-Za-z0-9]+:" limit t)
2514 (setq beg (match-beginning 0))
2515 (search-forward "@@" limit t))
2516 (cons 'export-snippet beg)))))
2519 ;;;; Footnote Reference
2521 (defun org-element-footnote-reference-parser ()
2522 "Parse footnote reference at point.
2524 Return a list whose CAR is `footnote-reference' and CDR a plist
2525 with `:label', `:type', `:inline-definition', `:begin', `:end'
2526 and `:post-blank' as keywords."
2527 (save-excursion
2528 (looking-at org-footnote-re)
2529 (let* ((begin (point))
2530 (label (or (org-match-string-no-properties 2)
2531 (org-match-string-no-properties 3)
2532 (and (match-string 1)
2533 (concat "fn:" (org-match-string-no-properties 1)))))
2534 (type (if (or (not label) (match-string 1)) 'inline 'standard))
2535 (inner-begin (match-end 0))
2536 (inner-end
2537 (let ((count 1))
2538 (forward-char)
2539 (while (and (> count 0) (re-search-forward "[][]" nil t))
2540 (if (equal (match-string 0) "[") (incf count) (decf count)))
2541 (1- (point))))
2542 (post-blank (progn (goto-char (1+ inner-end))
2543 (skip-chars-forward " \t")))
2544 (end (point))
2545 (footnote-reference
2546 (list 'footnote-reference
2547 (list :label label
2548 :type type
2549 :begin begin
2550 :end end
2551 :post-blank post-blank))))
2552 (org-element-put-property
2553 footnote-reference :inline-definition
2554 (and (eq type 'inline)
2555 (org-element-parse-secondary-string
2556 (buffer-substring inner-begin inner-end)
2557 (org-element-restriction 'footnote-reference)
2558 footnote-reference))))))
2560 (defun org-element-footnote-reference-interpreter (footnote-reference contents)
2561 "Interpret FOOTNOTE-REFERENCE object as Org syntax.
2562 CONTENTS is nil."
2563 (let ((label (or (org-element-property :label footnote-reference) "fn:"))
2564 (def
2565 (let ((inline-def
2566 (org-element-property :inline-definition footnote-reference)))
2567 (if (not inline-def) ""
2568 (concat ":" (org-element-interpret-data inline-def))))))
2569 (format "[%s]" (concat label def))))
2571 (defun org-element-footnote-reference-successor (limit)
2572 "Search for the next footnote-reference object.
2574 LIMIT bounds the search.
2576 Return value is a cons cell whose CAR is `footnote-reference' and
2577 CDR is beginning position."
2578 (save-excursion
2579 (catch 'exit
2580 (while (re-search-forward org-footnote-re limit t)
2581 (save-excursion
2582 (let ((beg (match-beginning 0))
2583 (count 1))
2584 (backward-char)
2585 (while (re-search-forward "[][]" limit t)
2586 (if (equal (match-string 0) "[") (incf count) (decf count))
2587 (when (zerop count)
2588 (throw 'exit (cons 'footnote-reference beg))))))))))
2591 ;;;; Inline Babel Call
2593 (defun org-element-inline-babel-call-parser ()
2594 "Parse inline babel call at point.
2596 Return a list whose CAR is `inline-babel-call' and CDR a plist
2597 with `:begin', `:end', `:info' and `:post-blank' as keywords.
2599 Assume point is at the beginning of the babel call."
2600 (save-excursion
2601 (unless (bolp) (backward-char))
2602 (looking-at org-babel-inline-lob-one-liner-regexp)
2603 (let ((info (save-match-data (org-babel-lob-get-info)))
2604 (begin (match-end 1))
2605 (post-blank (progn (goto-char (match-end 0))
2606 (skip-chars-forward " \t")))
2607 (end (point)))
2608 (list 'inline-babel-call
2609 (list :begin begin
2610 :end end
2611 :info info
2612 :post-blank post-blank)))))
2614 (defun org-element-inline-babel-call-interpreter (inline-babel-call contents)
2615 "Interpret INLINE-BABEL-CALL object as Org syntax.
2616 CONTENTS is nil."
2617 (let* ((babel-info (org-element-property :info inline-babel-call))
2618 (main-source (car babel-info))
2619 (post-options (nth 1 babel-info)))
2620 (concat "call_"
2621 (if (string-match "\\[\\(\\[.*?\\]\\)\\]" main-source)
2622 ;; Remove redundant square brackets.
2623 (replace-match
2624 (match-string 1 main-source) nil nil main-source)
2625 main-source)
2626 (and post-options (format "[%s]" post-options)))))
2628 (defun org-element-inline-babel-call-successor (limit)
2629 "Search for the next inline-babel-call object.
2631 LIMIT bounds the search.
2633 Return value is a cons cell whose CAR is `inline-babel-call' and
2634 CDR is beginning position."
2635 (save-excursion
2636 ;; Use a simplified version of
2637 ;; `org-babel-inline-lob-one-liner-regexp'.
2638 (when (re-search-forward
2639 "call_\\([^()\n]+?\\)\\(?:\\[.*?\\]\\)?([^\n]*?)\\(\\[.*?\\]\\)?"
2640 limit t)
2641 (cons 'inline-babel-call (match-beginning 0)))))
2644 ;;;; Inline Src Block
2646 (defun org-element-inline-src-block-parser ()
2647 "Parse inline source block at point.
2649 LIMIT bounds the search.
2651 Return a list whose CAR is `inline-src-block' and CDR a plist
2652 with `:begin', `:end', `:language', `:value', `:parameters' and
2653 `:post-blank' as keywords.
2655 Assume point is at the beginning of the inline src block."
2656 (save-excursion
2657 (unless (bolp) (backward-char))
2658 (looking-at org-babel-inline-src-block-regexp)
2659 (let ((begin (match-beginning 1))
2660 (language (org-match-string-no-properties 2))
2661 (parameters (org-match-string-no-properties 4))
2662 (value (org-match-string-no-properties 5))
2663 (post-blank (progn (goto-char (match-end 0))
2664 (skip-chars-forward " \t")))
2665 (end (point)))
2666 (list 'inline-src-block
2667 (list :language language
2668 :value value
2669 :parameters parameters
2670 :begin begin
2671 :end end
2672 :post-blank post-blank)))))
2674 (defun org-element-inline-src-block-interpreter (inline-src-block contents)
2675 "Interpret INLINE-SRC-BLOCK object as Org syntax.
2676 CONTENTS is nil."
2677 (let ((language (org-element-property :language inline-src-block))
2678 (arguments (org-element-property :parameters inline-src-block))
2679 (body (org-element-property :value inline-src-block)))
2680 (format "src_%s%s{%s}"
2681 language
2682 (if arguments (format "[%s]" arguments) "")
2683 body)))
2685 (defun org-element-inline-src-block-successor (limit)
2686 "Search for the next inline-babel-call element.
2688 LIMIT bounds the search.
2690 Return value is a cons cell whose CAR is `inline-babel-call' and
2691 CDR is beginning position."
2692 (save-excursion
2693 (unless (bolp) (backward-char))
2694 (when (re-search-forward org-babel-inline-src-block-regexp limit t)
2695 (cons 'inline-src-block (match-beginning 1)))))
2697 ;;;; Italic
2699 (defun org-element-italic-parser ()
2700 "Parse italic object at point.
2702 Return a list whose CAR is `italic' and CDR is a plist with
2703 `:begin', `:end', `:contents-begin' and `:contents-end' and
2704 `:post-blank' keywords.
2706 Assume point is at the first slash marker."
2707 (save-excursion
2708 (unless (bolp) (backward-char 1))
2709 (looking-at org-emph-re)
2710 (let ((begin (match-beginning 2))
2711 (contents-begin (match-beginning 4))
2712 (contents-end (match-end 4))
2713 (post-blank (progn (goto-char (match-end 2))
2714 (skip-chars-forward " \t")))
2715 (end (point)))
2716 (list 'italic
2717 (list :begin begin
2718 :end end
2719 :contents-begin contents-begin
2720 :contents-end contents-end
2721 :post-blank post-blank)))))
2723 (defun org-element-italic-interpreter (italic contents)
2724 "Interpret ITALIC object as Org syntax.
2725 CONTENTS is the contents of the object."
2726 (format "/%s/" contents))
2729 ;;;; Latex Fragment
2731 (defun org-element-latex-fragment-parser ()
2732 "Parse latex fragment at point.
2734 Return a list whose CAR is `latex-fragment' and CDR a plist with
2735 `:value', `:begin', `:end', and `:post-blank' as keywords.
2737 Assume point is at the beginning of the latex fragment."
2738 (save-excursion
2739 (let* ((begin (point))
2740 (substring-match
2741 (catch 'exit
2742 (mapc (lambda (e)
2743 (let ((latex-regexp (nth 1 (assoc e org-latex-regexps))))
2744 (when (or (looking-at latex-regexp)
2745 (and (not (bobp))
2746 (save-excursion
2747 (backward-char)
2748 (looking-at latex-regexp))))
2749 (throw 'exit (nth 2 (assoc e org-latex-regexps))))))
2750 (plist-get org-format-latex-options :matchers))
2751 ;; None found: it's a macro.
2752 (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")
2754 (value (match-string-no-properties substring-match))
2755 (post-blank (progn (goto-char (match-end substring-match))
2756 (skip-chars-forward " \t")))
2757 (end (point)))
2758 (list 'latex-fragment
2759 (list :value value
2760 :begin begin
2761 :end end
2762 :post-blank post-blank)))))
2764 (defun org-element-latex-fragment-interpreter (latex-fragment contents)
2765 "Interpret LATEX-FRAGMENT object as Org syntax.
2766 CONTENTS is nil."
2767 (org-element-property :value latex-fragment))
2769 ;;;; Line Break
2771 (defun org-element-line-break-parser ()
2772 "Parse line break at point.
2774 Return a list whose CAR is `line-break', and CDR a plist with
2775 `:begin', `:end' and `:post-blank' keywords.
2777 Assume point is at the beginning of the line break."
2778 (list 'line-break (list :begin (point) :end (point-at-eol) :post-blank 0)))
2780 (defun org-element-line-break-interpreter (line-break contents)
2781 "Interpret LINE-BREAK object as Org syntax.
2782 CONTENTS is nil."
2783 "\\\\")
2785 (defun org-element-line-break-successor (limit)
2786 "Search for the next line-break object.
2788 LIMIT bounds the search.
2790 Return value is a cons cell whose CAR is `line-break' and CDR is
2791 beginning position."
2792 (save-excursion
2793 (let ((beg (and (re-search-forward "[^\\\\]\\(\\\\\\\\\\)[ \t]*$" limit t)
2794 (goto-char (match-beginning 1)))))
2795 ;; A line break can only happen on a non-empty line.
2796 (when (and beg (re-search-backward "\\S-" (point-at-bol) t))
2797 (cons 'line-break beg)))))
2800 ;;;; Link
2802 (defun org-element-link-parser ()
2803 "Parse link at point.
2805 Return a list whose CAR is `link' and CDR a plist with `:type',
2806 `:path', `:raw-link', `:application', `:search-option', `:begin',
2807 `:end', `:contents-begin', `:contents-end' and `:post-blank' as
2808 keywords.
2810 Assume point is at the beginning of the link."
2811 (save-excursion
2812 (let ((begin (point))
2813 end contents-begin contents-end link-end post-blank path type
2814 raw-link link search-option application)
2815 (cond
2816 ;; Type 1: Text targeted from a radio target.
2817 ((and org-target-link-regexp (looking-at org-target-link-regexp))
2818 (setq type "radio"
2819 link-end (match-end 0)
2820 path (org-match-string-no-properties 0)))
2821 ;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]]
2822 ((looking-at org-bracket-link-regexp)
2823 (setq contents-begin (match-beginning 3)
2824 contents-end (match-end 3)
2825 link-end (match-end 0)
2826 ;; RAW-LINK is the original link.
2827 raw-link (org-match-string-no-properties 1)
2828 link (org-translate-link
2829 (org-link-expand-abbrev
2830 (org-link-unescape raw-link))))
2831 ;; Determine TYPE of link and set PATH accordingly.
2832 (cond
2833 ;; File type.
2834 ((or (file-name-absolute-p link) (string-match "^\\.\\.?/" link))
2835 (setq type "file" path link))
2836 ;; Explicit type (http, irc, bbdb...). See `org-link-types'.
2837 ((string-match org-link-re-with-space3 link)
2838 (setq type (match-string 1 link) path (match-string 2 link)))
2839 ;; Id type: PATH is the id.
2840 ((string-match "^id:\\([-a-f0-9]+\\)" link)
2841 (setq type "id" path (match-string 1 link)))
2842 ;; Code-ref type: PATH is the name of the reference.
2843 ((string-match "^(\\(.*\\))$" link)
2844 (setq type "coderef" path (match-string 1 link)))
2845 ;; Custom-id type: PATH is the name of the custom id.
2846 ((= (aref link 0) ?#)
2847 (setq type "custom-id" path (substring link 1)))
2848 ;; Fuzzy type: Internal link either matches a target, an
2849 ;; headline name or nothing. PATH is the target or
2850 ;; headline's name.
2851 (t (setq type "fuzzy" path link))))
2852 ;; Type 3: Plain link, i.e. http://orgmode.org
2853 ((looking-at org-plain-link-re)
2854 (setq raw-link (org-match-string-no-properties 0)
2855 type (org-match-string-no-properties 1)
2856 path (org-match-string-no-properties 2)
2857 link-end (match-end 0)))
2858 ;; Type 4: Angular link, i.e. <http://orgmode.org>
2859 ((looking-at org-angle-link-re)
2860 (setq raw-link (buffer-substring-no-properties
2861 (match-beginning 1) (match-end 2))
2862 type (org-match-string-no-properties 1)
2863 path (org-match-string-no-properties 2)
2864 link-end (match-end 0))))
2865 ;; In any case, deduce end point after trailing white space from
2866 ;; LINK-END variable.
2867 (setq post-blank (progn (goto-char link-end) (skip-chars-forward " \t"))
2868 end (point))
2869 ;; Extract search option and opening application out of
2870 ;; "file"-type links.
2871 (when (member type org-element-link-type-is-file)
2872 ;; Application.
2873 (cond ((string-match "^file\\+\\(.*\\)$" type)
2874 (setq application (match-string 1 type)))
2875 ((not (string-match "^file" type))
2876 (setq application type)))
2877 ;; Extract search option from PATH.
2878 (when (string-match "::\\(.*\\)$" path)
2879 (setq search-option (match-string 1 path)
2880 path (replace-match "" nil nil path)))
2881 ;; Make sure TYPE always report "file".
2882 (setq type "file"))
2883 (list 'link
2884 (list :type type
2885 :path path
2886 :raw-link (or raw-link path)
2887 :application application
2888 :search-option search-option
2889 :begin begin
2890 :end end
2891 :contents-begin contents-begin
2892 :contents-end contents-end
2893 :post-blank post-blank)))))
2895 (defun org-element-link-interpreter (link contents)
2896 "Interpret LINK object as Org syntax.
2897 CONTENTS is the contents of the object, or nil."
2898 (let ((type (org-element-property :type link))
2899 (raw-link (org-element-property :raw-link link)))
2900 (if (string= type "radio") raw-link
2901 (format "[[%s]%s]"
2902 raw-link
2903 (if contents (format "[%s]" contents) "")))))
2905 (defun org-element-link-successor (limit)
2906 "Search for the next link object.
2908 LIMIT bounds the search.
2910 Return value is a cons cell whose CAR is `link' and CDR is
2911 beginning position."
2912 (save-excursion
2913 (let ((link-regexp
2914 (if (not org-target-link-regexp) org-any-link-re
2915 (concat org-any-link-re "\\|" org-target-link-regexp))))
2916 (when (re-search-forward link-regexp limit t)
2917 (cons 'link (match-beginning 0))))))
2920 ;;;; Macro
2922 (defun org-element-macro-parser ()
2923 "Parse macro at point.
2925 Return a list whose CAR is `macro' and CDR a plist with `:key',
2926 `:args', `:begin', `:end', `:value' and `:post-blank' as
2927 keywords.
2929 Assume point is at the macro."
2930 (save-excursion
2931 (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
2932 (let ((begin (point))
2933 (key (downcase (org-match-string-no-properties 1)))
2934 (value (org-match-string-no-properties 0))
2935 (post-blank (progn (goto-char (match-end 0))
2936 (skip-chars-forward " \t")))
2937 (end (point))
2938 (args (let ((args (org-match-string-no-properties 3)) args2)
2939 (when args
2940 (setq args (org-split-string args ","))
2941 (while args
2942 (while (string-match "\\\\\\'" (car args))
2943 ;; Repair bad splits.
2944 (setcar (cdr args) (concat (substring (car args) 0 -1)
2945 "," (nth 1 args)))
2946 (pop args))
2947 (push (pop args) args2))
2948 (mapcar 'org-trim (nreverse args2))))))
2949 (list 'macro
2950 (list :key key
2951 :value value
2952 :args args
2953 :begin begin
2954 :end end
2955 :post-blank post-blank)))))
2957 (defun org-element-macro-interpreter (macro contents)
2958 "Interpret MACRO object as Org syntax.
2959 CONTENTS is nil."
2960 (org-element-property :value macro))
2962 (defun org-element-macro-successor (limit)
2963 "Search for the next macro object.
2965 LIMIT bounds the search.
2967 Return value is cons cell whose CAR is `macro' and CDR is
2968 beginning position."
2969 (save-excursion
2970 (when (re-search-forward
2971 "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
2972 limit t)
2973 (cons 'macro (match-beginning 0)))))
2976 ;;;; Radio-target
2978 (defun org-element-radio-target-parser ()
2979 "Parse radio target at point.
2981 Return a list whose CAR is `radio-target' and CDR a plist with
2982 `:begin', `:end', `:contents-begin', `:contents-end', `:value'
2983 and `:post-blank' as keywords.
2985 Assume point is at the radio target."
2986 (save-excursion
2987 (looking-at org-radio-target-regexp)
2988 (let ((begin (point))
2989 (contents-begin (match-beginning 1))
2990 (contents-end (match-end 1))
2991 (value (org-match-string-no-properties 1))
2992 (post-blank (progn (goto-char (match-end 0))
2993 (skip-chars-forward " \t")))
2994 (end (point)))
2995 (list 'radio-target
2996 (list :begin begin
2997 :end end
2998 :contents-begin contents-begin
2999 :contents-end contents-end
3000 :post-blank post-blank
3001 :value value)))))
3003 (defun org-element-radio-target-interpreter (target contents)
3004 "Interpret TARGET object as Org syntax.
3005 CONTENTS is the contents of the object."
3006 (concat "<<<" contents ">>>"))
3008 (defun org-element-radio-target-successor (limit)
3009 "Search for the next radio-target object.
3011 LIMIT bounds the search.
3013 Return value is a cons cell whose CAR is `radio-target' and CDR
3014 is beginning position."
3015 (save-excursion
3016 (when (re-search-forward org-radio-target-regexp limit t)
3017 (cons 'radio-target (match-beginning 0)))))
3020 ;;;; Statistics Cookie
3022 (defun org-element-statistics-cookie-parser ()
3023 "Parse statistics cookie at point.
3025 Return a list whose CAR is `statistics-cookie', and CDR a plist
3026 with `:begin', `:end', `:value' and `:post-blank' keywords.
3028 Assume point is at the beginning of the statistics-cookie."
3029 (save-excursion
3030 (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
3031 (let* ((begin (point))
3032 (value (buffer-substring-no-properties
3033 (match-beginning 0) (match-end 0)))
3034 (post-blank (progn (goto-char (match-end 0))
3035 (skip-chars-forward " \t")))
3036 (end (point)))
3037 (list 'statistics-cookie
3038 (list :begin begin
3039 :end end
3040 :value value
3041 :post-blank post-blank)))))
3043 (defun org-element-statistics-cookie-interpreter (statistics-cookie contents)
3044 "Interpret STATISTICS-COOKIE object as Org syntax.
3045 CONTENTS is nil."
3046 (org-element-property :value statistics-cookie))
3048 (defun org-element-statistics-cookie-successor (limit)
3049 "Search for the next statistics cookie object.
3051 LIMIT bounds the search.
3053 Return value is a cons cell whose CAR is `statistics-cookie' and
3054 CDR is beginning position."
3055 (save-excursion
3056 (when (re-search-forward "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]" limit t)
3057 (cons 'statistics-cookie (match-beginning 0)))))
3060 ;;;; Strike-Through
3062 (defun org-element-strike-through-parser ()
3063 "Parse strike-through object at point.
3065 Return a list whose CAR is `strike-through' and CDR is a plist
3066 with `:begin', `:end', `:contents-begin' and `:contents-end' and
3067 `:post-blank' keywords.
3069 Assume point is at the first plus sign marker."
3070 (save-excursion
3071 (unless (bolp) (backward-char 1))
3072 (looking-at org-emph-re)
3073 (let ((begin (match-beginning 2))
3074 (contents-begin (match-beginning 4))
3075 (contents-end (match-end 4))
3076 (post-blank (progn (goto-char (match-end 2))
3077 (skip-chars-forward " \t")))
3078 (end (point)))
3079 (list 'strike-through
3080 (list :begin begin
3081 :end end
3082 :contents-begin contents-begin
3083 :contents-end contents-end
3084 :post-blank post-blank)))))
3086 (defun org-element-strike-through-interpreter (strike-through contents)
3087 "Interpret STRIKE-THROUGH object as Org syntax.
3088 CONTENTS is the contents of the object."
3089 (format "+%s+" contents))
3092 ;;;; Subscript
3094 (defun org-element-subscript-parser ()
3095 "Parse subscript at point.
3097 Return a list whose CAR is `subscript' and CDR a plist with
3098 `:begin', `:end', `:contents-begin', `:contents-end',
3099 `:use-brackets-p' and `:post-blank' as keywords.
3101 Assume point is at the underscore."
3102 (save-excursion
3103 (unless (bolp) (backward-char))
3104 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp)
3106 (not (looking-at org-match-substring-regexp))))
3107 (begin (match-beginning 2))
3108 (contents-begin (or (match-beginning 5)
3109 (match-beginning 3)))
3110 (contents-end (or (match-end 5) (match-end 3)))
3111 (post-blank (progn (goto-char (match-end 0))
3112 (skip-chars-forward " \t")))
3113 (end (point)))
3114 (list 'subscript
3115 (list :begin begin
3116 :end end
3117 :use-brackets-p bracketsp
3118 :contents-begin contents-begin
3119 :contents-end contents-end
3120 :post-blank post-blank)))))
3122 (defun org-element-subscript-interpreter (subscript contents)
3123 "Interpret SUBSCRIPT object as Org syntax.
3124 CONTENTS is the contents of the object."
3125 (format
3126 (if (org-element-property :use-brackets-p subscript) "_{%s}" "_%s")
3127 contents))
3129 (defun org-element-sub/superscript-successor (limit)
3130 "Search for the next sub/superscript object.
3132 LIMIT bounds the search.
3134 Return value is a cons cell whose CAR is either `subscript' or
3135 `superscript' and CDR is beginning position."
3136 (save-excursion
3137 (when (re-search-forward org-match-substring-regexp limit t)
3138 (cons (if (string= (match-string 2) "_") 'subscript 'superscript)
3139 (match-beginning 2)))))
3142 ;;;; Superscript
3144 (defun org-element-superscript-parser ()
3145 "Parse superscript at point.
3147 Return a list whose CAR is `superscript' and CDR a plist with
3148 `:begin', `:end', `:contents-begin', `:contents-end',
3149 `:use-brackets-p' and `:post-blank' as keywords.
3151 Assume point is at the caret."
3152 (save-excursion
3153 (unless (bolp) (backward-char))
3154 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp) t
3155 (not (looking-at org-match-substring-regexp))))
3156 (begin (match-beginning 2))
3157 (contents-begin (or (match-beginning 5)
3158 (match-beginning 3)))
3159 (contents-end (or (match-end 5) (match-end 3)))
3160 (post-blank (progn (goto-char (match-end 0))
3161 (skip-chars-forward " \t")))
3162 (end (point)))
3163 (list 'superscript
3164 (list :begin begin
3165 :end end
3166 :use-brackets-p bracketsp
3167 :contents-begin contents-begin
3168 :contents-end contents-end
3169 :post-blank post-blank)))))
3171 (defun org-element-superscript-interpreter (superscript contents)
3172 "Interpret SUPERSCRIPT object as Org syntax.
3173 CONTENTS is the contents of the object."
3174 (format
3175 (if (org-element-property :use-brackets-p superscript) "^{%s}" "^%s")
3176 contents))
3179 ;;;; Table Cell
3181 (defun org-element-table-cell-parser ()
3182 "Parse table cell at point.
3184 Return a list whose CAR is `table-cell' and CDR is a plist
3185 containing `:begin', `:end', `:contents-begin', `:contents-end'
3186 and `:post-blank' keywords."
3187 (looking-at "[ \t]*\\(.*?\\)[ \t]*|")
3188 (let* ((begin (match-beginning 0))
3189 (end (match-end 0))
3190 (contents-begin (match-beginning 1))
3191 (contents-end (match-end 1)))
3192 (list 'table-cell
3193 (list :begin begin
3194 :end end
3195 :contents-begin contents-begin
3196 :contents-end contents-end
3197 :post-blank 0))))
3199 (defun org-element-table-cell-interpreter (table-cell contents)
3200 "Interpret TABLE-CELL element as Org syntax.
3201 CONTENTS is the contents of the cell, or nil."
3202 (concat " " contents " |"))
3204 (defun org-element-table-cell-successor (limit)
3205 "Search for the next table-cell object.
3207 LIMIT bounds the search.
3209 Return value is a cons cell whose CAR is `table-cell' and CDR is
3210 beginning position."
3211 (when (looking-at "[ \t]*.*?[ \t]+|") (cons 'table-cell (point))))
3214 ;;;; Target
3216 (defun org-element-target-parser ()
3217 "Parse target at point.
3219 Return a list whose CAR is `target' and CDR a plist with
3220 `:begin', `:end', `:value' and `:post-blank' as keywords.
3222 Assume point is at the target."
3223 (save-excursion
3224 (looking-at org-target-regexp)
3225 (let ((begin (point))
3226 (value (org-match-string-no-properties 1))
3227 (post-blank (progn (goto-char (match-end 0))
3228 (skip-chars-forward " \t")))
3229 (end (point)))
3230 (list 'target
3231 (list :begin begin
3232 :end end
3233 :value value
3234 :post-blank post-blank)))))
3236 (defun org-element-target-interpreter (target contents)
3237 "Interpret TARGET object as Org syntax.
3238 CONTENTS is nil."
3239 (format "<<%s>>" (org-element-property :value target)))
3241 (defun org-element-target-successor (limit)
3242 "Search for the next target object.
3244 LIMIT bounds the search.
3246 Return value is a cons cell whose CAR is `target' and CDR is
3247 beginning position."
3248 (save-excursion
3249 (when (re-search-forward org-target-regexp limit t)
3250 (cons 'target (match-beginning 0)))))
3253 ;;;; Timestamp
3255 (defun org-element-timestamp-parser ()
3256 "Parse time stamp at point.
3258 Return a list whose CAR is `timestamp', and CDR a plist with
3259 `:type', `:begin', `:end', `:value' and `:post-blank' keywords.
3261 Assume point is at the beginning of the timestamp."
3262 (save-excursion
3263 (let* ((begin (point))
3264 (activep (eq (char-after) ?<))
3265 (main-value
3266 (progn
3267 (looking-at "[<[]\\(\\(%%\\)?.*?\\)[]>]\\(?:--[<[]\\(.*?\\)[]>]\\)?")
3268 (match-string-no-properties 1)))
3269 (range-end (match-string-no-properties 3))
3270 (type (cond ((match-string 2) 'diary)
3271 ((and activep range-end) 'active-range)
3272 (activep 'active)
3273 (range-end 'inactive-range)
3274 (t 'inactive)))
3275 (post-blank (progn (goto-char (match-end 0))
3276 (skip-chars-forward " \t")))
3277 (end (point)))
3278 (list 'timestamp
3279 (list :type type
3280 :value main-value
3281 :range-end range-end
3282 :begin begin
3283 :end end
3284 :post-blank post-blank)))))
3286 (defun org-element-timestamp-interpreter (timestamp contents)
3287 "Interpret TIMESTAMP object as Org syntax.
3288 CONTENTS is nil."
3289 (let ((type (org-element-property :type timestamp) ))
3290 (concat
3291 (format (if (memq type '(inactive inactive-range)) "[%s]" "<%s>")
3292 (org-element-property :value timestamp))
3293 (let ((range-end (org-element-property :range-end timestamp)))
3294 (when range-end
3295 (concat "--"
3296 (format (if (eq type 'inactive-range) "[%s]" "<%s>")
3297 range-end)))))))
3299 (defun org-element-timestamp-successor (limit)
3300 "Search for the next timestamp object.
3302 LIMIT bounds the search.
3304 Return value is a cons cell whose CAR is `timestamp' and CDR is
3305 beginning position."
3306 (save-excursion
3307 (when (re-search-forward
3308 (concat org-ts-regexp-both
3309 "\\|"
3310 "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
3311 "\\|"
3312 "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
3313 limit t)
3314 (cons 'timestamp (match-beginning 0)))))
3317 ;;;; Underline
3319 (defun org-element-underline-parser ()
3320 "Parse underline object at point.
3322 Return a list whose CAR is `underline' and CDR is a plist with
3323 `:begin', `:end', `:contents-begin' and `:contents-end' and
3324 `:post-blank' keywords.
3326 Assume point is at the first underscore marker."
3327 (save-excursion
3328 (unless (bolp) (backward-char 1))
3329 (looking-at org-emph-re)
3330 (let ((begin (match-beginning 2))
3331 (contents-begin (match-beginning 4))
3332 (contents-end (match-end 4))
3333 (post-blank (progn (goto-char (match-end 2))
3334 (skip-chars-forward " \t")))
3335 (end (point)))
3336 (list 'underline
3337 (list :begin begin
3338 :end end
3339 :contents-begin contents-begin
3340 :contents-end contents-end
3341 :post-blank post-blank)))))
3343 (defun org-element-underline-interpreter (underline contents)
3344 "Interpret UNDERLINE object as Org syntax.
3345 CONTENTS is the contents of the object."
3346 (format "_%s_" contents))
3349 ;;;; Verbatim
3351 (defun org-element-verbatim-parser ()
3352 "Parse verbatim object at point.
3354 Return a list whose CAR is `verbatim' and CDR is a plist with
3355 `:value', `:begin', `:end' and `:post-blank' keywords.
3357 Assume point is at the first equal sign marker."
3358 (save-excursion
3359 (unless (bolp) (backward-char 1))
3360 (looking-at org-emph-re)
3361 (let ((begin (match-beginning 2))
3362 (value (org-match-string-no-properties 4))
3363 (post-blank (progn (goto-char (match-end 2))
3364 (skip-chars-forward " \t")))
3365 (end (point)))
3366 (list 'verbatim
3367 (list :value value
3368 :begin begin
3369 :end end
3370 :post-blank post-blank)))))
3372 (defun org-element-verbatim-interpreter (verbatim contents)
3373 "Interpret VERBATIM object as Org syntax.
3374 CONTENTS is nil."
3375 (format "=%s=" (org-element-property :value verbatim)))
3379 ;;; Parsing Element Starting At Point
3381 ;; `org-element--current-element' is the core function of this section.
3382 ;; It returns the Lisp representation of the element starting at
3383 ;; point.
3385 ;; `org-element--current-element' makes use of special modes. They
3386 ;; are activated for fixed element chaining (i.e. `plain-list' >
3387 ;; `item') or fixed conditional element chaining (i.e. `headline' >
3388 ;; `section'). Special modes are: `first-section', `item',
3389 ;; `node-property', `quote-section', `section' and `table-row'.
3391 (defun org-element--current-element
3392 (limit &optional granularity special structure)
3393 "Parse the element starting at point.
3395 LIMIT bounds the search.
3397 Return value is a list like (TYPE PROPS) where TYPE is the type
3398 of the element and PROPS a plist of properties associated to the
3399 element.
3401 Possible types are defined in `org-element-all-elements'.
3403 Optional argument GRANULARITY determines the depth of the
3404 recursion. Allowed values are `headline', `greater-element',
3405 `element', `object' or nil. When it is broader than `object' (or
3406 nil), secondary values will not be parsed, since they only
3407 contain objects.
3409 Optional argument SPECIAL, when non-nil, can be either
3410 `first-section', `item', `node-property', `quote-section',
3411 `section', and `table-row'.
3413 If STRUCTURE isn't provided but SPECIAL is set to `item', it will
3414 be computed.
3416 This function assumes point is always at the beginning of the
3417 element it has to parse."
3418 (save-excursion
3419 (let ((case-fold-search t)
3420 ;; Determine if parsing depth allows for secondary strings
3421 ;; parsing. It only applies to elements referenced in
3422 ;; `org-element-secondary-value-alist'.
3423 (raw-secondary-p (and granularity (not (eq granularity 'object)))))
3424 (cond
3425 ;; Item.
3426 ((eq special 'item)
3427 (org-element-item-parser limit structure raw-secondary-p))
3428 ;; Table Row.
3429 ((eq special 'table-row) (org-element-table-row-parser limit))
3430 ;; Node Property.
3431 ((eq special 'node-property) (org-element-node-property-parser limit))
3432 ;; Headline.
3433 ((org-with-limited-levels (org-at-heading-p))
3434 (org-element-headline-parser limit raw-secondary-p))
3435 ;; Sections (must be checked after headline).
3436 ((eq special 'section) (org-element-section-parser limit))
3437 ((eq special 'quote-section) (org-element-quote-section-parser limit))
3438 ((eq special 'first-section)
3439 (org-element-section-parser
3440 (or (save-excursion (org-with-limited-levels (outline-next-heading)))
3441 limit)))
3442 ;; When not at bol, point is at the beginning of an item or
3443 ;; a footnote definition: next item is always a paragraph.
3444 ((not (bolp)) (org-element-paragraph-parser limit (list (point))))
3445 ;; Planning and Clock.
3446 ((and (looking-at org-planning-or-clock-line-re))
3447 (if (equal (match-string 1) org-clock-string)
3448 (org-element-clock-parser limit)
3449 (org-element-planning-parser limit)))
3450 ;; Inlinetask.
3451 ((org-at-heading-p)
3452 (org-element-inlinetask-parser limit raw-secondary-p))
3453 ;; From there, elements can have affiliated keywords.
3454 (t (let ((affiliated (org-element--collect-affiliated-keywords)))
3455 (cond
3456 ;; LaTeX Environment.
3457 ((looking-at "[ \t]*\\\\begin{\\([A-Za-z0-9*]+\\)}[ \t]*$")
3458 (if (save-excursion
3459 (re-search-forward
3460 (format "^[ \t]*\\\\end{%s}[ \t]*$"
3461 (regexp-quote (match-string 1)))
3462 nil t))
3463 (org-element-latex-environment-parser limit affiliated)
3464 (org-element-paragraph-parser limit affiliated)))
3465 ;; Drawer and Property Drawer.
3466 ((looking-at org-drawer-regexp)
3467 (let ((name (match-string 1)))
3468 (cond
3469 ((not (save-excursion
3470 (re-search-forward "^[ \t]*:END:[ \t]*$" nil t)))
3471 (org-element-paragraph-parser limit affiliated))
3472 ((equal "PROPERTIES" name)
3473 (org-element-property-drawer-parser limit affiliated))
3474 (t (org-element-drawer-parser limit affiliated)))))
3475 ;; Fixed Width
3476 ((looking-at "[ \t]*:\\( \\|$\\)")
3477 (org-element-fixed-width-parser limit affiliated))
3478 ;; Inline Comments, Blocks, Babel Calls, Dynamic Blocks and
3479 ;; Keywords.
3480 ((looking-at "[ \t]*#")
3481 (goto-char (match-end 0))
3482 (cond ((looking-at "\\(?: \\|$\\)")
3483 (beginning-of-line)
3484 (org-element-comment-parser limit affiliated))
3485 ((looking-at "\\+BEGIN_\\(\\S-+\\)")
3486 (beginning-of-line)
3487 (let ((parser (assoc (upcase (match-string 1))
3488 org-element-block-name-alist)))
3489 (if parser (funcall (cdr parser) limit affiliated)
3490 (org-element-special-block-parser limit affiliated))))
3491 ((looking-at "\\+CALL:")
3492 (beginning-of-line)
3493 (org-element-babel-call-parser limit affiliated))
3494 ((looking-at "\\+BEGIN:? ")
3495 (beginning-of-line)
3496 (org-element-dynamic-block-parser limit affiliated))
3497 ((looking-at "\\+\\S-+:")
3498 (beginning-of-line)
3499 (org-element-keyword-parser limit affiliated))
3501 (beginning-of-line)
3502 (org-element-paragraph-parser limit affiliated))))
3503 ;; Footnote Definition.
3504 ((looking-at org-footnote-definition-re)
3505 (org-element-footnote-definition-parser limit affiliated))
3506 ;; Horizontal Rule.
3507 ((looking-at "[ \t]*-\\{5,\\}[ \t]*$")
3508 (org-element-horizontal-rule-parser limit affiliated))
3509 ;; Table.
3510 ((org-at-table-p t) (org-element-table-parser limit affiliated))
3511 ;; List.
3512 ((looking-at (org-item-re))
3513 (org-element-plain-list-parser
3514 limit affiliated (or structure (org-list-struct))))
3515 ;; Default element: Paragraph.
3516 (t (org-element-paragraph-parser limit affiliated)))))))))
3519 ;; Most elements can have affiliated keywords. When looking for an
3520 ;; element beginning, we want to move before them, as they belong to
3521 ;; that element, and, in the meantime, collect information they give
3522 ;; into appropriate properties. Hence the following function.
3524 (defun org-element--collect-affiliated-keywords ()
3525 "Collect affiliated keywords from point.
3527 Return a list whose CAR is the position at the first of them and
3528 CDR a plist of keywords and values and move point to the
3529 beginning of the first line after them.
3531 As a special case, if element doesn't start at the beginning of
3532 the line (i.e. a paragraph starting an item), CAR is current
3533 position of point and CDR is nil."
3534 (if (not (bolp)) (list (point))
3535 (let ((case-fold-search t)
3536 (origin (point))
3537 ;; RESTRICT is the list of objects allowed in parsed
3538 ;; keywords value.
3539 (restrict (org-element-restriction 'keyword))
3540 output)
3541 (while (and (not (eobp)) (looking-at org-element--affiliated-re))
3542 (let* ((raw-kwd (upcase (match-string 1)))
3543 ;; Apply translation to RAW-KWD. From there, KWD is
3544 ;; the official keyword.
3545 (kwd (or (cdr (assoc raw-kwd
3546 org-element-keyword-translation-alist))
3547 raw-kwd))
3548 ;; Find main value for any keyword.
3549 (value
3550 (save-match-data
3551 (org-trim
3552 (buffer-substring-no-properties
3553 (match-end 0) (point-at-eol)))))
3554 ;; PARSEDP is non-nil when keyword should have its
3555 ;; value parsed.
3556 (parsedp (member kwd org-element-parsed-keywords))
3557 ;; If KWD is a dual keyword, find its secondary
3558 ;; value. Maybe parse it.
3559 (dualp (member kwd org-element-dual-keywords))
3560 (dual-value
3561 (and dualp
3562 (let ((sec (org-match-string-no-properties 2)))
3563 (if (or (not sec) (not parsedp)) sec
3564 (org-element-parse-secondary-string sec restrict)))))
3565 ;; Attribute a property name to KWD.
3566 (kwd-sym (and kwd (intern (concat ":" (downcase kwd))))))
3567 ;; Now set final shape for VALUE.
3568 (when parsedp
3569 (setq value (org-element-parse-secondary-string value restrict)))
3570 (when dualp (setq value (and value (cons value dual-value))))
3571 (when (or (member kwd org-element-multiple-keywords)
3572 ;; Attributes can always appear on multiple lines.
3573 (string-match "^ATTR_" kwd))
3574 (setq value (cons value (plist-get output kwd-sym))))
3575 ;; Eventually store the new value in OUTPUT.
3576 (setq output (plist-put output kwd-sym value))
3577 ;; Move to next keyword.
3578 (forward-line)))
3579 ;; If affiliated keywords are orphaned: move back to first one.
3580 ;; They will be parsed as a paragraph.
3581 (when (looking-at "[ \t]*$") (goto-char origin) (setq output nil))
3582 ;; Return value.
3583 (cons origin output))))
3587 ;;; The Org Parser
3589 ;; The two major functions here are `org-element-parse-buffer', which
3590 ;; parses Org syntax inside the current buffer, taking into account
3591 ;; region, narrowing, or even visibility if specified, and
3592 ;; `org-element-parse-secondary-string', which parses objects within
3593 ;; a given string.
3595 ;; The (almost) almighty `org-element-map' allows to apply a function
3596 ;; on elements or objects matching some type, and accumulate the
3597 ;; resulting values. In an export situation, it also skips unneeded
3598 ;; parts of the parse tree.
3600 (defun org-element-parse-buffer (&optional granularity visible-only)
3601 "Recursively parse the buffer and return structure.
3602 If narrowing is in effect, only parse the visible part of the
3603 buffer.
3605 Optional argument GRANULARITY determines the depth of the
3606 recursion. It can be set to the following symbols:
3608 `headline' Only parse headlines.
3609 `greater-element' Don't recurse into greater elements excepted
3610 headlines and sections. Thus, elements
3611 parsed are the top-level ones.
3612 `element' Parse everything but objects and plain text.
3613 `object' Parse the complete buffer (default).
3615 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3616 elements.
3618 Assume buffer is in Org mode."
3619 (save-excursion
3620 (goto-char (point-min))
3621 (org-skip-whitespace)
3622 (org-element--parse-elements
3623 (point-at-bol) (point-max)
3624 ;; Start in `first-section' mode so text before the first
3625 ;; headline belongs to a section.
3626 'first-section nil granularity visible-only (list 'org-data nil))))
3628 (defun org-element-parse-secondary-string (string restriction &optional parent)
3629 "Recursively parse objects in STRING and return structure.
3631 RESTRICTION is a symbol limiting the object types that will be
3632 looked after.
3634 Optional argument PARENT, when non-nil, is the element or object
3635 containing the secondary string. It is used to set correctly
3636 `:parent' property within the string."
3637 (with-temp-buffer
3638 (insert string)
3639 (let ((secondary (org-element--parse-objects
3640 (point-min) (point-max) nil restriction)))
3641 (mapc (lambda (obj) (org-element-put-property obj :parent parent))
3642 secondary))))
3644 (defun org-element-map (data types fun &optional info first-match no-recursion)
3645 "Map a function on selected elements or objects.
3647 DATA is the parsed tree, as returned by, i.e,
3648 `org-element-parse-buffer'. TYPES is a symbol or list of symbols
3649 of elements or objects types. FUN is the function called on the
3650 matching element or object. It must accept one arguments: the
3651 element or object itself.
3653 When optional argument INFO is non-nil, it should be a plist
3654 holding export options. In that case, parts of the parse tree
3655 not exportable according to that property list will be skipped.
3657 When optional argument FIRST-MATCH is non-nil, stop at the first
3658 match for which FUN doesn't return nil, and return that value.
3660 Optional argument NO-RECURSION is a symbol or a list of symbols
3661 representing elements or objects types. `org-element-map' won't
3662 enter any recursive element or object whose type belongs to that
3663 list. Though, FUN can still be applied on them.
3665 Nil values returned from FUN do not appear in the results."
3666 ;; Ensure TYPES and NO-RECURSION are a list, even of one element.
3667 (unless (listp types) (setq types (list types)))
3668 (unless (listp no-recursion) (setq no-recursion (list no-recursion)))
3669 ;; Recursion depth is determined by --CATEGORY.
3670 (let* ((--category
3671 (catch 'found
3672 (let ((category 'greater-elements))
3673 (mapc (lambda (type)
3674 (cond ((or (memq type org-element-all-objects)
3675 (eq type 'plain-text))
3676 ;; If one object is found, the function
3677 ;; has to recurse into every object.
3678 (throw 'found 'objects))
3679 ((not (memq type org-element-greater-elements))
3680 ;; If one regular element is found, the
3681 ;; function has to recurse, at least,
3682 ;; into every element it encounters.
3683 (and (not (eq category 'elements))
3684 (setq category 'elements)))))
3685 types)
3686 category)))
3687 --acc
3688 --walk-tree
3689 (--walk-tree
3690 (function
3691 (lambda (--data)
3692 ;; Recursively walk DATA. INFO, if non-nil, is a plist
3693 ;; holding contextual information.
3694 (let ((--type (org-element-type --data)))
3695 (cond
3696 ((not --data))
3697 ;; Ignored element in an export context.
3698 ((and info (memq --data (plist-get info :ignore-list))))
3699 ;; Secondary string: only objects can be found there.
3700 ((not --type)
3701 (when (eq --category 'objects) (mapc --walk-tree --data)))
3702 ;; Unconditionally enter parse trees.
3703 ((eq --type 'org-data)
3704 (mapc --walk-tree (org-element-contents --data)))
3706 ;; Check if TYPE is matching among TYPES. If so,
3707 ;; apply FUN to --DATA and accumulate return value
3708 ;; into --ACC (or exit if FIRST-MATCH is non-nil).
3709 (when (memq --type types)
3710 (let ((result (funcall fun --data)))
3711 (cond ((not result))
3712 (first-match (throw '--map-first-match result))
3713 (t (push result --acc)))))
3714 ;; If --DATA has a secondary string that can contain
3715 ;; objects with their type among TYPES, look into it.
3716 (when (eq --category 'objects)
3717 (let ((sec-prop
3718 (assq --type org-element-secondary-value-alist)))
3719 (when sec-prop
3720 (funcall --walk-tree
3721 (org-element-property (cdr sec-prop) --data)))))
3722 ;; Determine if a recursion into --DATA is possible.
3723 (cond
3724 ;; --TYPE is explicitly removed from recursion.
3725 ((memq --type no-recursion))
3726 ;; --DATA has no contents.
3727 ((not (org-element-contents --data)))
3728 ;; Looking for greater elements but --DATA is simply
3729 ;; an element or an object.
3730 ((and (eq --category 'greater-elements)
3731 (not (memq --type org-element-greater-elements))))
3732 ;; Looking for elements but --DATA is an object.
3733 ((and (eq --category 'elements)
3734 (memq --type org-element-all-objects)))
3735 ;; In any other case, map contents.
3736 (t (mapc --walk-tree (org-element-contents --data)))))))))))
3737 (catch '--map-first-match
3738 (funcall --walk-tree data)
3739 ;; Return value in a proper order.
3740 (nreverse --acc))))
3742 ;; The following functions are internal parts of the parser.
3744 ;; The first one, `org-element--parse-elements' acts at the element's
3745 ;; level.
3747 ;; The second one, `org-element--parse-objects' applies on all objects
3748 ;; of a paragraph or a secondary string. It uses
3749 ;; `org-element--get-next-object-candidates' to optimize the search of
3750 ;; the next object in the buffer.
3752 ;; More precisely, that function looks for every allowed object type
3753 ;; first. Then, it discards failed searches, keeps further matches,
3754 ;; and searches again types matched behind point, for subsequent
3755 ;; calls. Thus, searching for a given type fails only once, and every
3756 ;; object is searched only once at top level (but sometimes more for
3757 ;; nested types).
3759 (defun org-element--parse-elements
3760 (beg end special structure granularity visible-only acc)
3761 "Parse elements between BEG and END positions.
3763 SPECIAL prioritize some elements over the others. It can be set
3764 to `first-section', `quote-section', `section' `item' or
3765 `table-row'.
3767 When value is `item', STRUCTURE will be used as the current list
3768 structure.
3770 GRANULARITY determines the depth of the recursion. See
3771 `org-element-parse-buffer' for more information.
3773 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3774 elements.
3776 Elements are accumulated into ACC."
3777 (save-excursion
3778 (goto-char beg)
3779 ;; When parsing only headlines, skip any text before first one.
3780 (when (and (eq granularity 'headline) (not (org-at-heading-p)))
3781 (org-with-limited-levels (outline-next-heading)))
3782 ;; Main loop start.
3783 (while (< (point) end)
3784 ;; Find current element's type and parse it accordingly to
3785 ;; its category.
3786 (let* ((element (org-element--current-element
3787 end granularity special structure))
3788 (type (org-element-type element))
3789 (cbeg (org-element-property :contents-begin element)))
3790 (goto-char (org-element-property :end element))
3791 ;; Fill ELEMENT contents by side-effect.
3792 (cond
3793 ;; If VISIBLE-ONLY is true and element is hidden or if it has
3794 ;; no contents, don't modify it.
3795 ((or (and visible-only (org-element-property :hiddenp element))
3796 (not cbeg)))
3797 ;; Greater element: parse it between `contents-begin' and
3798 ;; `contents-end'. Make sure GRANULARITY allows the
3799 ;; recursion, or ELEMENT is an headline, in which case going
3800 ;; inside is mandatory, in order to get sub-level headings.
3801 ((and (memq type org-element-greater-elements)
3802 (or (memq granularity '(element object nil))
3803 (and (eq granularity 'greater-element)
3804 (eq type 'section))
3805 (eq type 'headline)))
3806 (org-element--parse-elements
3807 cbeg (org-element-property :contents-end element)
3808 ;; Possibly switch to a special mode.
3809 (case type
3810 (headline
3811 (if (org-element-property :quotedp element) 'quote-section
3812 'section))
3813 (plain-list 'item)
3814 (property-drawer 'node-property)
3815 (table 'table-row))
3816 (org-element-property :structure element)
3817 granularity visible-only element))
3818 ;; ELEMENT has contents. Parse objects inside, if
3819 ;; GRANULARITY allows it.
3820 ((memq granularity '(object nil))
3821 (org-element--parse-objects
3822 cbeg (org-element-property :contents-end element) element
3823 (org-element-restriction type))))
3824 (org-element-adopt-elements acc element)))
3825 ;; Return result.
3826 acc))
3828 (defun org-element--parse-objects (beg end acc restriction)
3829 "Parse objects between BEG and END and return recursive structure.
3831 Objects are accumulated in ACC.
3833 RESTRICTION is a list of object types which are allowed in the
3834 current object."
3835 (let (candidates)
3836 (save-excursion
3837 (goto-char beg)
3838 (while (and (< (point) end)
3839 (setq candidates (org-element--get-next-object-candidates
3840 end restriction candidates)))
3841 (let ((next-object
3842 (let ((pos (apply 'min (mapcar 'cdr candidates))))
3843 (save-excursion
3844 (goto-char pos)
3845 (funcall (intern (format "org-element-%s-parser"
3846 (car (rassq pos candidates)))))))))
3847 ;; 1. Text before any object. Untabify it.
3848 (let ((obj-beg (org-element-property :begin next-object)))
3849 (unless (= (point) obj-beg)
3850 (setq acc
3851 (org-element-adopt-elements
3853 (replace-regexp-in-string
3854 "\t" (make-string tab-width ? )
3855 (buffer-substring-no-properties (point) obj-beg))))))
3856 ;; 2. Object...
3857 (let ((obj-end (org-element-property :end next-object))
3858 (cont-beg (org-element-property :contents-begin next-object)))
3859 ;; Fill contents of NEXT-OBJECT by side-effect, if it has
3860 ;; a recursive type.
3861 (when (and cont-beg
3862 (memq (car next-object) org-element-recursive-objects))
3863 (save-restriction
3864 (narrow-to-region
3865 cont-beg
3866 (org-element-property :contents-end next-object))
3867 (org-element--parse-objects
3868 (point-min) (point-max) next-object
3869 (org-element-restriction next-object))))
3870 (setq acc (org-element-adopt-elements acc next-object))
3871 (goto-char obj-end))))
3872 ;; 3. Text after last object. Untabify it.
3873 (unless (= (point) end)
3874 (setq acc
3875 (org-element-adopt-elements
3877 (replace-regexp-in-string
3878 "\t" (make-string tab-width ? )
3879 (buffer-substring-no-properties (point) end)))))
3880 ;; Result.
3881 acc)))
3883 (defun org-element--get-next-object-candidates (limit restriction objects)
3884 "Return an alist of candidates for the next object.
3886 LIMIT bounds the search, and RESTRICTION narrows candidates to
3887 some object types.
3889 Return value is an alist whose CAR is position and CDR the object
3890 type, as a symbol.
3892 OBJECTS is the previous candidates alist."
3893 (let (next-candidates types-to-search)
3894 ;; If no previous result, search every object type in RESTRICTION.
3895 ;; Otherwise, keep potential candidates (old objects located after
3896 ;; point) and ask to search again those which had matched before.
3897 (if (not objects) (setq types-to-search restriction)
3898 (mapc (lambda (obj)
3899 (if (< (cdr obj) (point)) (push (car obj) types-to-search)
3900 (push obj next-candidates)))
3901 objects))
3902 ;; Call the appropriate successor function for each type to search
3903 ;; and accumulate matches.
3904 (mapc
3905 (lambda (type)
3906 (let* ((successor-fun
3907 (intern
3908 (format "org-element-%s-successor"
3909 (or (cdr (assq type org-element-object-successor-alist))
3910 type))))
3911 (obj (funcall successor-fun limit)))
3912 (and obj (push obj next-candidates))))
3913 types-to-search)
3914 ;; Return alist.
3915 next-candidates))
3919 ;;; Towards A Bijective Process
3921 ;; The parse tree obtained with `org-element-parse-buffer' is really
3922 ;; a snapshot of the corresponding Org buffer. Therefore, it can be
3923 ;; interpreted and expanded into a string with canonical Org syntax.
3924 ;; Hence `org-element-interpret-data'.
3926 ;; The function relies internally on
3927 ;; `org-element--interpret-affiliated-keywords'.
3929 ;;;###autoload
3930 (defun org-element-interpret-data (data &optional parent)
3931 "Interpret DATA as Org syntax.
3933 DATA is a parse tree, an element, an object or a secondary string
3934 to interpret.
3936 Optional argument PARENT is used for recursive calls. It contains
3937 the element or object containing data, or nil.
3939 Return Org syntax as a string."
3940 (let* ((type (org-element-type data))
3941 (results
3942 (cond
3943 ;; Secondary string.
3944 ((not type)
3945 (mapconcat
3946 (lambda (obj) (org-element-interpret-data obj parent))
3947 data ""))
3948 ;; Full Org document.
3949 ((eq type 'org-data)
3950 (mapconcat
3951 (lambda (obj) (org-element-interpret-data obj parent))
3952 (org-element-contents data) ""))
3953 ;; Plain text.
3954 ((stringp data) data)
3955 ;; Element/Object without contents.
3956 ((not (org-element-contents data))
3957 (funcall (intern (format "org-element-%s-interpreter" type))
3958 data nil))
3959 ;; Element/Object with contents.
3961 (let* ((greaterp (memq type org-element-greater-elements))
3962 (objectp (and (not greaterp)
3963 (memq type org-element-recursive-objects)))
3964 (contents
3965 (mapconcat
3966 (lambda (obj) (org-element-interpret-data obj data))
3967 (org-element-contents
3968 (if (or greaterp objectp) data
3969 ;; Elements directly containing objects must
3970 ;; have their indentation normalized first.
3971 (org-element-normalize-contents
3972 data
3973 ;; When normalizing first paragraph of an
3974 ;; item or a footnote-definition, ignore
3975 ;; first line's indentation.
3976 (and (eq type 'paragraph)
3977 (equal data (car (org-element-contents parent)))
3978 (memq (org-element-type parent)
3979 '(footnote-definiton item))))))
3980 "")))
3981 (funcall (intern (format "org-element-%s-interpreter" type))
3982 data
3983 (if greaterp (org-element-normalize-contents contents)
3984 contents)))))))
3985 (if (memq type '(org-data plain-text nil)) results
3986 ;; Build white spaces. If no `:post-blank' property is
3987 ;; specified, assume its value is 0.
3988 (let ((post-blank (or (org-element-property :post-blank data) 0)))
3989 (if (memq type org-element-all-objects)
3990 (concat results (make-string post-blank 32))
3991 (concat
3992 (org-element--interpret-affiliated-keywords data)
3993 (org-element-normalize-string results)
3994 (make-string post-blank 10)))))))
3996 (defun org-element--interpret-affiliated-keywords (element)
3997 "Return ELEMENT's affiliated keywords as Org syntax.
3998 If there is no affiliated keyword, return the empty string."
3999 (let ((keyword-to-org
4000 (function
4001 (lambda (key value)
4002 (let (dual)
4003 (when (member key org-element-dual-keywords)
4004 (setq dual (cdr value) value (car value)))
4005 (concat "#+" key
4006 (and dual
4007 (format "[%s]" (org-element-interpret-data dual)))
4008 ": "
4009 (if (member key org-element-parsed-keywords)
4010 (org-element-interpret-data value)
4011 value)
4012 "\n"))))))
4013 (mapconcat
4014 (lambda (prop)
4015 (let ((value (org-element-property prop element))
4016 (keyword (upcase (substring (symbol-name prop) 1))))
4017 (when value
4018 (if (or (member keyword org-element-multiple-keywords)
4019 ;; All attribute keywords can have multiple lines.
4020 (string-match "^ATTR_" keyword))
4021 (mapconcat (lambda (line) (funcall keyword-to-org keyword line))
4022 (reverse value)
4024 (funcall keyword-to-org keyword value)))))
4025 ;; List all ELEMENT's properties matching an attribute line or an
4026 ;; affiliated keyword, but ignore translated keywords since they
4027 ;; cannot belong to the property list.
4028 (loop for prop in (nth 1 element) by 'cddr
4029 when (let ((keyword (upcase (substring (symbol-name prop) 1))))
4030 (or (string-match "^ATTR_" keyword)
4031 (and
4032 (member keyword org-element-affiliated-keywords)
4033 (not (assoc keyword
4034 org-element-keyword-translation-alist)))))
4035 collect prop)
4036 "")))
4038 ;; Because interpretation of the parse tree must return the same
4039 ;; number of blank lines between elements and the same number of white
4040 ;; space after objects, some special care must be given to white
4041 ;; spaces.
4043 ;; The first function, `org-element-normalize-string', ensures any
4044 ;; string different from the empty string will end with a single
4045 ;; newline character.
4047 ;; The second function, `org-element-normalize-contents', removes
4048 ;; global indentation from the contents of the current element.
4050 (defun org-element-normalize-string (s)
4051 "Ensure string S ends with a single newline character.
4053 If S isn't a string return it unchanged. If S is the empty
4054 string, return it. Otherwise, return a new string with a single
4055 newline character at its end."
4056 (cond
4057 ((not (stringp s)) s)
4058 ((string= "" s) "")
4059 (t (and (string-match "\\(\n[ \t]*\\)*\\'" s)
4060 (replace-match "\n" nil nil s)))))
4062 (defun org-element-normalize-contents (element &optional ignore-first)
4063 "Normalize plain text in ELEMENT's contents.
4065 ELEMENT must only contain plain text and objects.
4067 If optional argument IGNORE-FIRST is non-nil, ignore first line's
4068 indentation to compute maximal common indentation.
4070 Return the normalized element that is element with global
4071 indentation removed from its contents. The function assumes that
4072 indentation is not done with TAB characters."
4073 (let* (ind-list ; for byte-compiler
4074 collect-inds ; for byte-compiler
4075 (collect-inds
4076 (function
4077 ;; Return list of indentations within BLOB. This is done by
4078 ;; walking recursively BLOB and updating IND-LIST along the
4079 ;; way. FIRST-FLAG is non-nil when the first string hasn't
4080 ;; been seen yet. It is required as this string is the only
4081 ;; one whose indentation doesn't happen after a newline
4082 ;; character.
4083 (lambda (blob first-flag)
4084 (mapc
4085 (lambda (object)
4086 (when (and first-flag (stringp object))
4087 (setq first-flag nil)
4088 (string-match "\\`\\( *\\)" object)
4089 (let ((len (length (match-string 1 object))))
4090 ;; An indentation of zero means no string will be
4091 ;; modified. Quit the process.
4092 (if (zerop len) (throw 'zero (setq ind-list nil))
4093 (push len ind-list))))
4094 (cond
4095 ((stringp object)
4096 (let ((start 0))
4097 ;; Avoid matching blank or empty lines.
4098 (while (and (string-match "\n\\( *\\)\\(.\\)" object start)
4099 (not (equal (match-string 2 object) " ")))
4100 (setq start (match-end 0))
4101 (push (length (match-string 1 object)) ind-list))))
4102 ((memq (org-element-type object) org-element-recursive-objects)
4103 (funcall collect-inds object first-flag))))
4104 (org-element-contents blob))))))
4105 ;; Collect indentation list in ELEMENT. Possibly remove first
4106 ;; value if IGNORE-FIRST is non-nil.
4107 (catch 'zero (funcall collect-inds element (not ignore-first)))
4108 (if (not ind-list) element
4109 ;; Build ELEMENT back, replacing each string with the same
4110 ;; string minus common indentation.
4111 (let* (build ; For byte compiler.
4112 (build
4113 (function
4114 (lambda (blob mci first-flag)
4115 ;; Return BLOB with all its strings indentation
4116 ;; shortened from MCI white spaces. FIRST-FLAG is
4117 ;; non-nil when the first string hasn't been seen
4118 ;; yet.
4119 (setcdr (cdr blob)
4120 (mapcar
4121 (lambda (object)
4122 (when (and first-flag (stringp object))
4123 (setq first-flag nil)
4124 (setq object
4125 (replace-regexp-in-string
4126 (format "\\` \\{%d\\}" mci) "" object)))
4127 (cond
4128 ((stringp object)
4129 (replace-regexp-in-string
4130 (format "\n \\{%d\\}" mci) "\n" object))
4131 ((memq (org-element-type object)
4132 org-element-recursive-objects)
4133 (funcall build object mci first-flag))
4134 (t object)))
4135 (org-element-contents blob)))
4136 blob))))
4137 (funcall build element (apply 'min ind-list) (not ignore-first))))))
4141 ;;; The Toolbox
4143 ;; The first move is to implement a way to obtain the smallest element
4144 ;; containing point. This is the job of `org-element-at-point'. It
4145 ;; basically jumps back to the beginning of section containing point
4146 ;; and moves, element after element, with
4147 ;; `org-element--current-element' until the container is found. Note:
4148 ;; When using `org-element-at-point', secondary values are never
4149 ;; parsed since the function focuses on elements, not on objects.
4151 ;; At a deeper level, `org-element-context' lists all elements and
4152 ;; objects containing point.
4154 ;; `org-element-nested-p' and `org-element-swap-A-B' may be used
4155 ;; internally by navigation and manipulation tools.
4157 ;;;###autoload
4158 (defun org-element-at-point (&optional keep-trail)
4159 "Determine closest element around point.
4161 Return value is a list like (TYPE PROPS) where TYPE is the type
4162 of the element and PROPS a plist of properties associated to the
4163 element.
4165 Possible types are defined in `org-element-all-elements'.
4166 Properties depend on element or object type, but always
4167 include :begin, :end, :parent and :post-blank properties.
4169 As a special case, if point is at the very beginning of a list or
4170 sub-list, returned element will be that list instead of the first
4171 item. In the same way, if point is at the beginning of the first
4172 row of a table, returned element will be the table instead of the
4173 first row.
4175 If optional argument KEEP-TRAIL is non-nil, the function returns
4176 a list of of elements leading to element at point. The list's
4177 CAR is always the element at point. Following positions contain
4178 element's siblings, then parents, siblings of parents, until the
4179 first element of current section."
4180 (org-with-wide-buffer
4181 ;; If at an headline, parse it. It is the sole element that
4182 ;; doesn't require to know about context. Be sure to disallow
4183 ;; secondary string parsing, though.
4184 (if (org-with-limited-levels (org-at-heading-p))
4185 (progn
4186 (beginning-of-line)
4187 (if (not keep-trail) (org-element-headline-parser (point-max) t)
4188 (list (org-element-headline-parser (point-max) t))))
4189 ;; Otherwise move at the beginning of the section containing
4190 ;; point.
4191 (let ((origin (point))
4192 (end (save-excursion
4193 (org-with-limited-levels (outline-next-heading)) (point)))
4194 element type special-flag trail struct prevs parent)
4195 (org-with-limited-levels
4196 (if (org-with-limited-levels (org-before-first-heading-p))
4197 (goto-char (point-min))
4198 (org-back-to-heading)
4199 (forward-line)))
4200 (org-skip-whitespace)
4201 (beginning-of-line)
4202 ;; Parse successively each element, skipping those ending
4203 ;; before original position.
4204 (catch 'exit
4205 (while t
4206 (setq element
4207 (org-element--current-element end 'element special-flag struct)
4208 type (car element))
4209 (org-element-put-property element :parent parent)
4210 (when keep-trail (push element trail))
4211 (cond
4212 ;; 1. Skip any element ending before point. Also skip
4213 ;; element ending at point when we're sure that another
4214 ;; element has started.
4215 ((let ((elem-end (org-element-property :end element)))
4216 (when (or (< elem-end origin)
4217 (and (= elem-end origin) (/= elem-end end)))
4218 (goto-char elem-end))))
4219 ;; 2. An element containing point is always the element at
4220 ;; point.
4221 ((not (memq type org-element-greater-elements))
4222 (throw 'exit (if keep-trail trail element)))
4223 ;; 3. At any other greater element type, if point is
4224 ;; within contents, move into it.
4226 (let ((cbeg (org-element-property :contents-begin element))
4227 (cend (org-element-property :contents-end element)))
4228 (if (or (not cbeg) (not cend) (> cbeg origin) (< cend origin)
4229 ;; Create an anchor for tables and plain lists:
4230 ;; when point is at the very beginning of these
4231 ;; elements, ignoring affiliated keywords,
4232 ;; target them instead of their contents.
4233 (and (= cbeg origin) (memq type '(plain-list table)))
4234 ;; When point is at contents end, do not move
4235 ;; into elements with an explicit ending, but
4236 ;; return that element instead.
4237 (and (= cend origin)
4238 (memq type
4239 '(center-block
4240 drawer dynamic-block inlinetask item
4241 plain-list property-drawer quote-block
4242 special-block))))
4243 (throw 'exit (if keep-trail trail element))
4244 (setq parent element)
4245 (case type
4246 (plain-list
4247 (setq special-flag 'item
4248 struct (org-element-property :structure element)))
4249 (property-drawer (setq special-flag 'node-property))
4250 (table (setq special-flag 'table-row))
4251 (otherwise (setq special-flag nil)))
4252 (setq end cend)
4253 (goto-char cbeg)))))))))))
4255 ;;;###autoload
4256 (defun org-element-context ()
4257 "Return closest element or object around point.
4259 Return value is a list like (TYPE PROPS) where TYPE is the type
4260 of the element or object and PROPS a plist of properties
4261 associated to it.
4263 Possible types are defined in `org-element-all-elements' and
4264 `org-element-all-objects'. Properties depend on element or
4265 object type, but always include :begin, :end, :parent
4266 and :post-blank properties."
4267 (org-with-wide-buffer
4268 (let* ((origin (point))
4269 (element (org-element-at-point))
4270 (type (car element))
4271 end)
4272 ;; Check if point is inside an element containing objects or at
4273 ;; a secondary string. In that case, move to beginning of the
4274 ;; element or secondary string and set END to the other side.
4275 (if (not (or (and (eq type 'item)
4276 (let ((tag (org-element-property :tag element)))
4277 (and tag
4278 (progn
4279 (beginning-of-line)
4280 (search-forward tag (point-at-eol))
4281 (goto-char (match-beginning 0))
4282 (and (>= origin (point))
4283 (<= origin
4284 ;; `1+' is required so some
4285 ;; successors can match
4286 ;; properly their object.
4287 (setq end (1+ (match-end 0)))))))))
4288 (and (memq type '(headline inlinetask))
4289 (progn (beginning-of-line)
4290 (skip-chars-forward "* ")
4291 (setq end (point-at-eol))))
4292 (and (memq type '(paragraph table-cell verse-block))
4293 (let ((cbeg (org-element-property
4294 :contents-begin element))
4295 (cend (org-element-property
4296 :contents-end element)))
4297 (and (>= origin cbeg)
4298 (<= origin cend)
4299 (progn (goto-char cbeg) (setq end cend)))))))
4300 element
4301 (let ((restriction (org-element-restriction element))
4302 (parent element)
4303 candidates)
4304 (catch 'exit
4305 (while (setq candidates (org-element--get-next-object-candidates
4306 end restriction candidates))
4307 (let ((closest-cand (rassq (apply 'min (mapcar 'cdr candidates))
4308 candidates)))
4309 ;; If ORIGIN is before next object in element, there's
4310 ;; no point in looking further.
4311 (if (> (cdr closest-cand) origin) (throw 'exit element)
4312 (let* ((object
4313 (progn (goto-char (cdr closest-cand))
4314 (funcall (intern (format "org-element-%s-parser"
4315 (car closest-cand))))))
4316 (cbeg (org-element-property :contents-begin object))
4317 (cend (org-element-property :contents-end object)))
4318 (cond
4319 ;; ORIGIN is after OBJECT, so skip it.
4320 ((< (org-element-property :end object) origin)
4321 (goto-char (org-element-property :end object)))
4322 ;; ORIGIN is within a non-recursive object or at an
4323 ;; object boundaries: Return that object.
4324 ((or (not cbeg) (> cbeg origin) (< cend origin))
4325 (throw 'exit
4326 (org-element-put-property object :parent parent)))
4327 ;; Otherwise, move within current object and restrict
4328 ;; search to the end of its contents.
4329 (t (goto-char cbeg)
4330 (org-element-put-property object :parent parent)
4331 (setq parent object end cend)))))))
4332 parent))))))
4334 (defsubst org-element-nested-p (elem-A elem-B)
4335 "Non-nil when elements ELEM-A and ELEM-B are nested."
4336 (let ((beg-A (org-element-property :begin elem-A))
4337 (beg-B (org-element-property :begin elem-B))
4338 (end-A (org-element-property :end elem-A))
4339 (end-B (org-element-property :end elem-B)))
4340 (or (and (>= beg-A beg-B) (<= end-A end-B))
4341 (and (>= beg-B beg-A) (<= end-B end-A)))))
4343 (defun org-element-swap-A-B (elem-A elem-B)
4344 "Swap elements ELEM-A and ELEM-B.
4345 Assume ELEM-B is after ELEM-A in the buffer. Leave point at the
4346 end of ELEM-A."
4347 (goto-char (org-element-property :begin elem-A))
4348 ;; There are two special cases when an element doesn't start at bol:
4349 ;; the first paragraph in an item or in a footnote definition.
4350 (let ((specialp (not (bolp))))
4351 ;; Only a paragraph without any affiliated keyword can be moved at
4352 ;; ELEM-A position in such a situation. Note that the case of
4353 ;; a footnote definition is impossible: it cannot contain two
4354 ;; paragraphs in a row because it cannot contain a blank line.
4355 (if (and specialp
4356 (or (not (eq (org-element-type elem-B) 'paragraph))
4357 (/= (org-element-property :begin elem-B)
4358 (org-element-property :contents-begin elem-B))))
4359 (error "Cannot swap elements"))
4360 ;; In a special situation, ELEM-A will have no indentation. We'll
4361 ;; give it ELEM-B's (which will in, in turn, have no indentation).
4362 (let* ((ind-B (when specialp
4363 (goto-char (org-element-property :begin elem-B))
4364 (org-get-indentation)))
4365 (beg-A (org-element-property :begin elem-A))
4366 (end-A (save-excursion
4367 (goto-char (org-element-property :end elem-A))
4368 (skip-chars-backward " \r\t\n")
4369 (point-at-eol)))
4370 (beg-B (org-element-property :begin elem-B))
4371 (end-B (save-excursion
4372 (goto-char (org-element-property :end elem-B))
4373 (skip-chars-backward " \r\t\n")
4374 (point-at-eol)))
4375 ;; Store overlays responsible for visibility status. We
4376 ;; also need to store their boundaries as they will be
4377 ;; removed from buffer.
4378 (overlays
4379 (cons
4380 (mapcar (lambda (ov) (list ov (overlay-start ov) (overlay-end ov)))
4381 (overlays-in beg-A end-A))
4382 (mapcar (lambda (ov) (list ov (overlay-start ov) (overlay-end ov)))
4383 (overlays-in beg-B end-B))))
4384 ;; Get contents.
4385 (body-A (buffer-substring beg-A end-A))
4386 (body-B (delete-and-extract-region beg-B end-B)))
4387 (goto-char beg-B)
4388 (when specialp
4389 (setq body-B (replace-regexp-in-string "\\`[ \t]*" "" body-B))
4390 (org-indent-to-column ind-B))
4391 (insert body-A)
4392 ;; Restore ex ELEM-A overlays.
4393 (let ((offset (- beg-B beg-A)))
4394 (mapc (lambda (ov)
4395 (move-overlay
4396 (car ov) (+ (nth 1 ov) offset) (+ (nth 2 ov) offset)))
4397 (car overlays))
4398 (goto-char beg-A)
4399 (delete-region beg-A end-A)
4400 (insert body-B)
4401 ;; Restore ex ELEM-B overlays.
4402 (mapc (lambda (ov)
4403 (move-overlay
4404 (car ov) (- (nth 1 ov) offset) (- (nth 2 ov) offset)))
4405 (cdr overlays)))
4406 (goto-char (org-element-property :end elem-B)))))
4409 (provide 'org-element)
4410 ;;; org-element.el ends here