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