Merge branch 'master' of orgmode.org:org-mode
[org-mode.git] / lisp / org-element.el
blob3bf217dd64dad2339726bdaa693617b2890f0b5e
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 program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; This file is not part of GNU Emacs.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with this program. 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\\|end\\)" "\\|"
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 (t "[.)]") (?\) ")") (?. "\\.") (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 (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 `:tags', `:todo-keyword', `:todo-type', `:scheduled',
848 `:deadline', `:timestamp', `:clock' and `:post-blank' keywords.
850 The plist also contains any property set in the property drawer,
851 with its name in lowercase, the underscores replaced with hyphens
852 and colons at the beginning (i.e. `:custom-id').
854 When optional argument RAW-SECONDARY-P is non-nil, inline-task's
855 title will not be parsed as a secondary string, but as a plain
856 string instead.
858 Assume point is at beginning of the inline task."
859 (save-excursion
860 (let* ((keywords (org-element--collect-affiliated-keywords))
861 (begin (car keywords))
862 (components (org-heading-components))
863 (todo (nth 2 components))
864 (todo-type (and todo
865 (if (member todo org-done-keywords) 'done 'todo)))
866 (tags (let ((raw-tags (nth 5 components)))
867 (and raw-tags (org-split-string raw-tags ":"))))
868 ;; Normalize property names: ":SOME_PROP:" becomes
869 ;; ":some-prop".
870 (standard-props (let (plist)
871 (mapc
872 (lambda (p)
873 (let ((p-name (downcase (car p))))
874 (while (string-match "_" p-name)
875 (setq p-name
876 (replace-match "-" nil nil p-name)))
877 (setq p-name (intern (concat ":" p-name)))
878 (setq plist
879 (plist-put plist p-name (cdr p)))))
880 (org-entry-properties nil 'standard))
881 plist))
882 (time-props (org-entry-properties nil 'special "CLOCK"))
883 (scheduled (cdr (assoc "SCHEDULED" time-props)))
884 (deadline (cdr (assoc "DEADLINE" time-props)))
885 (clock (cdr (assoc "CLOCK" time-props)))
886 (timestamp (cdr (assoc "TIMESTAMP" time-props)))
887 (task-end (save-excursion
888 (end-of-line)
889 (and (re-search-forward "^\\*+ END" limit t)
890 (match-beginning 0))))
891 (contents-begin (progn (forward-line)
892 (and task-end (< (point) task-end) (point))))
893 (hidden (and contents-begin (org-invisible-p2)))
894 (contents-end (and contents-begin task-end))
895 (before-blank (if (not task-end) (point)
896 (goto-char task-end)
897 (forward-line)
898 (point)))
899 (end (progn (skip-chars-forward " \r\t\n" limit)
900 (if (eobp) (point) (point-at-bol))))
901 (inlinetask
902 (list 'inlinetask
903 (nconc
904 (list :begin begin
905 :end end
906 :hiddenp hidden
907 :contents-begin contents-begin
908 :contents-end contents-end
909 :level (nth 1 components)
910 :priority (nth 3 components)
911 :tags tags
912 :todo-keyword todo
913 :todo-type todo-type
914 :scheduled scheduled
915 :deadline deadline
916 :timestamp timestamp
917 :clock clock
918 :post-blank (count-lines before-blank end))
919 standard-props
920 (cadr keywords)))))
921 (org-element-put-property
922 inlinetask :title
923 (if raw-secondary-p (nth 4 components)
924 (org-element-parse-secondary-string
925 (nth 4 components)
926 (org-element-restriction 'inlinetask)
927 inlinetask))))))
929 (defun org-element-inlinetask-interpreter (inlinetask contents)
930 "Interpret INLINETASK element as Org syntax.
931 CONTENTS is the contents of inlinetask."
932 (let* ((level (org-element-property :level inlinetask))
933 (todo (org-element-property :todo-keyword inlinetask))
934 (priority (org-element-property :priority inlinetask))
935 (title (org-element-interpret-data
936 (org-element-property :title inlinetask)))
937 (tags (let ((tag-list (org-element-property :tags inlinetask)))
938 (and tag-list
939 (format ":%s:" (mapconcat 'identity tag-list ":")))))
940 (task (concat (make-string level ?*)
941 (and todo (concat " " todo))
942 (and priority
943 (format " [#%s]" (char-to-string priority)))
944 (and title (concat " " title)))))
945 (concat task
946 ;; Align tags.
947 (when tags
948 (cond
949 ((zerop org-tags-column) (format " %s" tags))
950 ((< org-tags-column 0)
951 (concat
952 (make-string
953 (max (- (+ org-tags-column (length task) (length tags))) 1)
955 tags))
957 (concat
958 (make-string (max (- org-tags-column (length task)) 1) ? )
959 tags))))
960 ;; Prefer degenerate inlinetasks when there are no
961 ;; contents.
962 (when contents
963 (concat "\n"
964 contents
965 (make-string level ?*) " END")))))
968 ;;;; Item
970 (defun org-element-item-parser (limit struct &optional raw-secondary-p)
971 "Parse an item.
973 STRUCT is the structure of the plain list.
975 Return a list whose CAR is `item' and CDR is a plist containing
976 `:bullet', `:begin', `:end', `:contents-begin', `:contents-end',
977 `:checkbox', `:counter', `:tag', `:structure', `:hiddenp' and
978 `:post-blank' keywords.
980 When optional argument RAW-SECONDARY-P is non-nil, item's tag, if
981 any, will not be parsed as a secondary string, but as a plain
982 string instead.
984 Assume point is at the beginning of the item."
985 (save-excursion
986 (beginning-of-line)
987 (looking-at org-list-full-item-re)
988 (let* ((begin (point))
989 (bullet (org-match-string-no-properties 1))
990 (checkbox (let ((box (org-match-string-no-properties 3)))
991 (cond ((equal "[ ]" box) 'off)
992 ((equal "[X]" box) 'on)
993 ((equal "[-]" box) 'trans))))
994 (counter (let ((c (org-match-string-no-properties 2)))
995 (save-match-data
996 (cond
997 ((not c) nil)
998 ((string-match "[A-Za-z]" c)
999 (- (string-to-char (upcase (match-string 0 c)))
1000 64))
1001 ((string-match "[0-9]+" c)
1002 (string-to-number (match-string 0 c)))))))
1003 (end (save-excursion (goto-char (org-list-get-item-end begin struct))
1004 (unless (bolp) (forward-line))
1005 (point)))
1006 (contents-begin
1007 (progn (goto-char
1008 ;; Ignore tags in un-ordered lists: they are just
1009 ;; a part of item's body.
1010 (if (and (match-beginning 4)
1011 (save-match-data (string-match "[.)]" bullet)))
1012 (match-beginning 4)
1013 (match-end 0)))
1014 (skip-chars-forward " \r\t\n" limit)
1015 ;; If first line isn't empty, contents really start
1016 ;; at the text after item's meta-data.
1017 (if (= (point-at-bol) begin) (point) (point-at-bol))))
1018 (hidden (progn (forward-line)
1019 (and (not (= (point) end)) (org-invisible-p2))))
1020 (contents-end (progn (goto-char end)
1021 (skip-chars-backward " \r\t\n")
1022 (forward-line)
1023 (point)))
1024 (item
1025 (list 'item
1026 (list :bullet bullet
1027 :begin begin
1028 :end end
1029 ;; CONTENTS-BEGIN and CONTENTS-END may be
1030 ;; mixed up in the case of an empty item
1031 ;; separated from the next by a blank line.
1032 ;; Thus ensure the former is always the
1033 ;; smallest.
1034 :contents-begin (min contents-begin contents-end)
1035 :contents-end (max contents-begin contents-end)
1036 :checkbox checkbox
1037 :counter counter
1038 :hiddenp hidden
1039 :structure struct
1040 :post-blank (count-lines contents-end end)))))
1041 (org-element-put-property
1042 item :tag
1043 (let ((raw-tag (org-list-get-tag begin struct)))
1044 (and raw-tag
1045 (if raw-secondary-p raw-tag
1046 (org-element-parse-secondary-string
1047 raw-tag (org-element-restriction 'item) item))))))))
1049 (defun org-element-item-interpreter (item contents)
1050 "Interpret ITEM element as Org syntax.
1051 CONTENTS is the contents of the element."
1052 (let* ((bullet (org-list-bullet-string (org-element-property :bullet item)))
1053 (checkbox (org-element-property :checkbox item))
1054 (counter (org-element-property :counter item))
1055 (tag (let ((tag (org-element-property :tag item)))
1056 (and tag (org-element-interpret-data tag))))
1057 ;; Compute indentation.
1058 (ind (make-string (length bullet) 32))
1059 (item-starts-with-par-p
1060 (eq (org-element-type (car (org-element-contents item)))
1061 'paragraph)))
1062 ;; Indent contents.
1063 (concat
1064 bullet
1065 (and counter (format "[@%d] " counter))
1066 (case checkbox
1067 (on "[X] ")
1068 (off "[ ] ")
1069 (trans "[-] "))
1070 (and tag (format "%s :: " tag))
1071 (let ((contents (replace-regexp-in-string
1072 "\\(^\\)[ \t]*\\S-" ind contents nil nil 1)))
1073 (if item-starts-with-par-p (org-trim contents)
1074 (concat "\n" contents))))))
1077 ;;;; Plain List
1079 (defun org-element-plain-list-parser (limit &optional structure)
1080 "Parse a plain list.
1082 Optional argument STRUCTURE, when non-nil, is the structure of
1083 the plain list being parsed.
1085 Return a list whose CAR is `plain-list' and CDR is a plist
1086 containing `:type', `:begin', `:end', `:contents-begin' and
1087 `:contents-end', `:structure' and `:post-blank' keywords.
1089 Assume point is at the beginning of the list."
1090 (save-excursion
1091 (let* ((struct (or structure (org-list-struct)))
1092 (prevs (org-list-prevs-alist struct))
1093 (parents (org-list-parents-alist struct))
1094 (type (org-list-get-list-type (point) struct prevs))
1095 (contents-begin (point))
1096 (keywords (org-element--collect-affiliated-keywords))
1097 (begin (car keywords))
1098 (contents-end
1099 (progn (goto-char (org-list-get-list-end (point) struct prevs))
1100 (unless (bolp) (forward-line))
1101 (point)))
1102 (end (progn (skip-chars-forward " \r\t\n" limit)
1103 (if (eobp) (point) (point-at-bol)))))
1104 ;; Return value.
1105 (list 'plain-list
1106 (nconc
1107 (list :type type
1108 :begin begin
1109 :end end
1110 :contents-begin contents-begin
1111 :contents-end contents-end
1112 :structure struct
1113 :post-blank (count-lines contents-end end))
1114 (cadr keywords))))))
1116 (defun org-element-plain-list-interpreter (plain-list contents)
1117 "Interpret PLAIN-LIST element as Org syntax.
1118 CONTENTS is the contents of the element."
1119 (with-temp-buffer
1120 (insert contents)
1121 (goto-char (point-min))
1122 (org-list-repair)
1123 (buffer-string)))
1126 ;;;; Quote Block
1128 (defun org-element-quote-block-parser (limit)
1129 "Parse a quote block.
1131 LIMIT bounds the search.
1133 Return a list whose CAR is `quote-block' and CDR is a plist
1134 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1135 `:contents-end' and `:post-blank' keywords.
1137 Assume point is at the beginning of the block."
1138 (let ((case-fold-search t))
1139 (if (not (save-excursion
1140 (re-search-forward "^[ \t]*#\\+END_QUOTE" limit t)))
1141 ;; Incomplete block: parse it as a paragraph.
1142 (org-element-paragraph-parser limit)
1143 (let ((block-end-line (match-beginning 0)))
1144 (save-excursion
1145 (let* ((keywords (org-element--collect-affiliated-keywords))
1146 (begin (car keywords))
1147 ;; Empty blocks have no contents.
1148 (contents-begin (progn (forward-line)
1149 (and (< (point) block-end-line)
1150 (point))))
1151 (contents-end (and contents-begin block-end-line))
1152 (hidden (org-invisible-p2))
1153 (pos-before-blank (progn (goto-char block-end-line)
1154 (forward-line)
1155 (point)))
1156 (end (progn (skip-chars-forward " \r\t\n" limit)
1157 (if (eobp) (point) (point-at-bol)))))
1158 (list 'quote-block
1159 (nconc
1160 (list :begin begin
1161 :end end
1162 :hiddenp hidden
1163 :contents-begin contents-begin
1164 :contents-end contents-end
1165 :post-blank (count-lines pos-before-blank end))
1166 (cadr keywords)))))))))
1168 (defun org-element-quote-block-interpreter (quote-block contents)
1169 "Interpret QUOTE-BLOCK element as Org syntax.
1170 CONTENTS is the contents of the element."
1171 (format "#+BEGIN_QUOTE\n%s#+END_QUOTE" contents))
1174 ;;;; Section
1176 (defun org-element-section-parser (limit)
1177 "Parse a section.
1179 LIMIT bounds the search.
1181 Return a list whose CAR is `section' and CDR is a plist
1182 containing `:begin', `:end', `:contents-begin', `contents-end'
1183 and `:post-blank' keywords."
1184 (save-excursion
1185 ;; Beginning of section is the beginning of the first non-blank
1186 ;; line after previous headline.
1187 (org-with-limited-levels
1188 (let ((begin (point))
1189 (end (progn (goto-char limit) (point)))
1190 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
1191 (forward-line)
1192 (point))))
1193 (list 'section
1194 (list :begin begin
1195 :end end
1196 :contents-begin begin
1197 :contents-end pos-before-blank
1198 :post-blank (count-lines pos-before-blank end)))))))
1200 (defun org-element-section-interpreter (section contents)
1201 "Interpret SECTION element as Org syntax.
1202 CONTENTS is the contents of the element."
1203 contents)
1206 ;;;; Special Block
1208 (defun org-element-special-block-parser (limit)
1209 "Parse a special block.
1211 LIMIT bounds the search.
1213 Return a list whose CAR is `special-block' and CDR is a plist
1214 containing `:type', `:begin', `:end', `:hiddenp',
1215 `:contents-begin', `:contents-end' and `:post-blank' keywords.
1217 Assume point is at the beginning of the block."
1218 (let* ((case-fold-search t)
1219 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(S-+\\)")
1220 (upcase (match-string-no-properties 1)))))
1221 (if (not (save-excursion
1222 (re-search-forward (concat "^[ \t]*#\\+END_" type) limit t)))
1223 ;; Incomplete block: parse it as a paragraph.
1224 (org-element-paragraph-parser limit)
1225 (let ((block-end-line (match-beginning 0)))
1226 (save-excursion
1227 (let* ((keywords (org-element--collect-affiliated-keywords))
1228 (begin (car keywords))
1229 ;; Empty blocks have no contents.
1230 (contents-begin (progn (forward-line)
1231 (and (< (point) block-end-line)
1232 (point))))
1233 (contents-end (and contents-begin block-end-line))
1234 (hidden (org-invisible-p2))
1235 (pos-before-blank (progn (goto-char block-end-line)
1236 (forward-line)
1237 (point)))
1238 (end (progn (org-skip-whitespace)
1239 (if (eobp) (point) (point-at-bol)))))
1240 (list 'special-block
1241 (nconc
1242 (list :type type
1243 :begin begin
1244 :end end
1245 :hiddenp hidden
1246 :contents-begin contents-begin
1247 :contents-end contents-end
1248 :post-blank (count-lines pos-before-blank end))
1249 (cadr keywords)))))))))
1251 (defun org-element-special-block-interpreter (special-block contents)
1252 "Interpret SPECIAL-BLOCK element as Org syntax.
1253 CONTENTS is the contents of the element."
1254 (let ((block-type (org-element-property :type special-block)))
1255 (format "#+BEGIN_%s\n%s#+END_%s" block-type contents block-type)))
1259 ;;; Elements
1261 ;; For each element, a parser and an interpreter are also defined.
1262 ;; Both follow the same naming convention used for greater elements.
1264 ;; Also, as for greater elements, adding a new element type is done
1265 ;; through the following steps: implement a parser and an interpreter,
1266 ;; tweak `org-element--current-element' so that it recognizes the new
1267 ;; type and add that new type to `org-element-all-elements'.
1269 ;; As a special case, when the newly defined type is a block type,
1270 ;; `org-element-block-name-alist' has to be modified accordingly.
1273 ;;;; Babel Call
1275 (defun org-element-babel-call-parser (limit)
1276 "Parse a babel call.
1278 LIMIT bounds the search.
1280 Return a list whose CAR is `babel-call' and CDR is a plist
1281 containing `:begin', `:end', `:info' and `:post-blank' as
1282 keywords."
1283 (save-excursion
1284 (let ((case-fold-search t)
1285 (info (progn (looking-at org-babel-block-lob-one-liner-regexp)
1286 (org-babel-lob-get-info)))
1287 (begin (point-at-bol))
1288 (pos-before-blank (progn (forward-line) (point)))
1289 (end (progn (skip-chars-forward " \r\t\n" limit)
1290 (if (eobp) (point) (point-at-bol)))))
1291 (list 'babel-call
1292 (list :begin begin
1293 :end end
1294 :info info
1295 :post-blank (count-lines pos-before-blank end))))))
1297 (defun org-element-babel-call-interpreter (babel-call contents)
1298 "Interpret BABEL-CALL element as Org syntax.
1299 CONTENTS is nil."
1300 (let* ((babel-info (org-element-property :info babel-call))
1301 (main (car babel-info))
1302 (post-options (nth 1 babel-info)))
1303 (concat "#+CALL: "
1304 (if (not (string-match "\\[\\(\\[.*?\\]\\)\\]" main)) main
1305 ;; Remove redundant square brackets.
1306 (replace-match (match-string 1 main) nil nil main))
1307 (and post-options (format "[%s]" post-options)))))
1310 ;;;; Clock
1312 (defun org-element-clock-parser (limit)
1313 "Parse a clock.
1315 LIMIT bounds the search.
1317 Return a list whose CAR is `clock' and CDR is a plist containing
1318 `:status', `:value', `:time', `:begin', `:end' and `:post-blank'
1319 as keywords."
1320 (save-excursion
1321 (let* ((case-fold-search nil)
1322 (begin (point))
1323 (value (progn (search-forward org-clock-string (line-end-position) t)
1324 (org-skip-whitespace)
1325 (looking-at "\\[.*\\]")
1326 (org-match-string-no-properties 0)))
1327 (time (and (progn (goto-char (match-end 0))
1328 (looking-at " +=> +\\(\\S-+\\)[ \t]*$"))
1329 (org-match-string-no-properties 1)))
1330 (status (if time 'closed 'running))
1331 (post-blank (let ((before-blank (progn (forward-line) (point))))
1332 (skip-chars-forward " \r\t\n" limit)
1333 (unless (eobp) (beginning-of-line))
1334 (count-lines before-blank (point))))
1335 (end (point)))
1336 (list 'clock
1337 (list :status status
1338 :value value
1339 :time time
1340 :begin begin
1341 :end end
1342 :post-blank post-blank)))))
1344 (defun org-element-clock-interpreter (clock contents)
1345 "Interpret CLOCK element as Org syntax.
1346 CONTENTS is nil."
1347 (concat org-clock-string " "
1348 (org-element-property :value clock)
1349 (let ((time (org-element-property :time clock)))
1350 (and time
1351 (concat " => "
1352 (apply 'format
1353 "%2s:%02s"
1354 (org-split-string time ":")))))))
1357 ;;;; Comment
1359 (defun org-element-comment-parser (limit)
1360 "Parse a comment.
1362 LIMIT bounds the search.
1364 Return a list whose CAR is `comment' and CDR is a plist
1365 containing `:begin', `:end', `:value' and `:post-blank'
1366 keywords.
1368 Assume point is at comment beginning."
1369 (save-excursion
1370 (let* ((keywords (org-element--collect-affiliated-keywords))
1371 (begin (car keywords))
1372 (value (prog2 (looking-at "[ \t]*# ?")
1373 (buffer-substring-no-properties
1374 (match-end 0) (line-end-position))
1375 (forward-line)))
1376 (com-end
1377 ;; Get comments ending.
1378 (progn
1379 (while (and (< (point) limit) (looking-at "[ \t]*#\\( \\|$\\)"))
1380 ;; Accumulate lines without leading hash and first
1381 ;; whitespace.
1382 (setq value
1383 (concat value
1384 "\n"
1385 (buffer-substring-no-properties
1386 (match-end 0) (line-end-position))))
1387 (forward-line))
1388 (point)))
1389 (end (progn (goto-char com-end)
1390 (skip-chars-forward " \r\t\n" limit)
1391 (if (eobp) (point) (point-at-bol)))))
1392 (list 'comment
1393 (nconc
1394 (list :begin begin
1395 :end end
1396 :value value
1397 :post-blank (count-lines com-end end))
1398 (cadr keywords))))))
1400 (defun org-element-comment-interpreter (comment contents)
1401 "Interpret COMMENT element as Org syntax.
1402 CONTENTS is nil."
1403 (replace-regexp-in-string "^" "# " (org-element-property :value comment)))
1406 ;;;; Comment Block
1408 (defun org-element-comment-block-parser (limit)
1409 "Parse an export block.
1411 LIMIT bounds the search.
1413 Return a list whose CAR is `comment-block' and CDR is a plist
1414 containing `:begin', `:end', `:hiddenp', `:value' and
1415 `:post-blank' keywords.
1417 Assume point is at comment block beginning."
1418 (let ((case-fold-search t))
1419 (if (not (save-excursion
1420 (re-search-forward "^[ \t]*#\\+END_COMMENT" limit t)))
1421 ;; Incomplete block: parse it as a paragraph.
1422 (org-element-paragraph-parser limit)
1423 (let ((contents-end (match-beginning 0)))
1424 (save-excursion
1425 (let* ((keywords (org-element--collect-affiliated-keywords))
1426 (begin (car keywords))
1427 (contents-begin (progn (forward-line) (point)))
1428 (hidden (org-invisible-p2))
1429 (pos-before-blank (progn (goto-char contents-end)
1430 (forward-line)
1431 (point)))
1432 (end (progn (skip-chars-forward " \r\t\n" limit)
1433 (if (eobp) (point) (point-at-bol))))
1434 (value (buffer-substring-no-properties
1435 contents-begin contents-end)))
1436 (list 'comment-block
1437 (nconc
1438 (list :begin begin
1439 :end end
1440 :value value
1441 :hiddenp hidden
1442 :post-blank (count-lines pos-before-blank end))
1443 (cadr keywords)))))))))
1445 (defun org-element-comment-block-interpreter (comment-block contents)
1446 "Interpret COMMENT-BLOCK element as Org syntax.
1447 CONTENTS is nil."
1448 (format "#+BEGIN_COMMENT\n%s#+END_COMMENT"
1449 (org-remove-indentation (org-element-property :value comment-block))))
1452 ;;;; Example Block
1454 (defun org-element-example-block-parser (limit)
1455 "Parse an example block.
1457 LIMIT bounds the search.
1459 Return a list whose CAR is `example-block' and CDR is a plist
1460 containing `:begin', `:end', `:number-lines', `:preserve-indent',
1461 `:retain-labels', `:use-labels', `:label-fmt', `:hiddenp',
1462 `:switches', `:value' and `:post-blank' keywords."
1463 (let ((case-fold-search t))
1464 (if (not (save-excursion
1465 (re-search-forward "^[ \t]*#\\+END_EXAMPLE" limit t)))
1466 ;; Incomplete block: parse it as a paragraph.
1467 (org-element-paragraph-parser limit)
1468 (let ((contents-end (match-beginning 0)))
1469 (save-excursion
1470 (let* ((switches
1471 (progn (looking-at "^[ \t]*#\\+BEGIN_EXAMPLE\\(?: +\\(.*\\)\\)?")
1472 (org-match-string-no-properties 1)))
1473 ;; Switches analysis
1474 (number-lines (cond ((not switches) nil)
1475 ((string-match "-n\\>" switches) 'new)
1476 ((string-match "+n\\>" switches) 'continued)))
1477 (preserve-indent (and switches (string-match "-i\\>" switches)))
1478 ;; Should labels be retained in (or stripped from) example
1479 ;; blocks?
1480 (retain-labels
1481 (or (not switches)
1482 (not (string-match "-r\\>" switches))
1483 (and number-lines (string-match "-k\\>" switches))))
1484 ;; What should code-references use - labels or
1485 ;; line-numbers?
1486 (use-labels
1487 (or (not switches)
1488 (and retain-labels (not (string-match "-k\\>" switches)))))
1489 (label-fmt (and switches
1490 (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
1491 (match-string 1 switches)))
1492 ;; Standard block parsing.
1493 (keywords (org-element--collect-affiliated-keywords))
1494 (begin (car keywords))
1495 (contents-begin (progn (forward-line) (point)))
1496 (hidden (org-invisible-p2))
1497 (value (buffer-substring-no-properties contents-begin contents-end))
1498 (pos-before-blank (progn (goto-char contents-end)
1499 (forward-line)
1500 (point)))
1501 (end (progn (skip-chars-forward " \r\t\n" limit)
1502 (if (eobp) (point) (point-at-bol)))))
1503 (list 'example-block
1504 (nconc
1505 (list :begin begin
1506 :end end
1507 :value value
1508 :switches switches
1509 :number-lines number-lines
1510 :preserve-indent preserve-indent
1511 :retain-labels retain-labels
1512 :use-labels use-labels
1513 :label-fmt label-fmt
1514 :hiddenp hidden
1515 :post-blank (count-lines pos-before-blank end))
1516 (cadr keywords)))))))))
1518 (defun org-element-example-block-interpreter (example-block contents)
1519 "Interpret EXAMPLE-BLOCK element as Org syntax.
1520 CONTENTS is nil."
1521 (let ((switches (org-element-property :switches example-block)))
1522 (concat "#+BEGIN_EXAMPLE" (and switches (concat " " switches)) "\n"
1523 (org-remove-indentation
1524 (org-element-property :value example-block))
1525 "#+END_EXAMPLE")))
1528 ;;;; Export Block
1530 (defun org-element-export-block-parser (limit)
1531 "Parse an export block.
1533 LIMIT bounds the search.
1535 Return a list whose CAR is `export-block' and CDR is a plist
1536 containing `:begin', `:end', `:type', `:hiddenp', `:value' and
1537 `:post-blank' keywords.
1539 Assume point is at export-block beginning."
1540 (let* ((case-fold-search t)
1541 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
1542 (upcase (org-match-string-no-properties 1)))))
1543 (if (not (save-excursion
1544 (re-search-forward (concat "^[ \t]*#\\+END_" type) limit t)))
1545 ;; Incomplete block: parse it as a paragraph.
1546 (org-element-paragraph-parser limit)
1547 (let ((contents-end (match-beginning 0)))
1548 (save-excursion
1549 (let* ((keywords (org-element--collect-affiliated-keywords))
1550 (begin (car keywords))
1551 (contents-begin (progn (forward-line) (point)))
1552 (hidden (org-invisible-p2))
1553 (pos-before-blank (progn (goto-char contents-end)
1554 (forward-line)
1555 (point)))
1556 (end (progn (skip-chars-forward " \r\t\n" limit)
1557 (if (eobp) (point) (point-at-bol))))
1558 (value (buffer-substring-no-properties contents-begin
1559 contents-end)))
1560 (list 'export-block
1561 (nconc
1562 (list :begin begin
1563 :end end
1564 :type type
1565 :value value
1566 :hiddenp hidden
1567 :post-blank (count-lines pos-before-blank end))
1568 (cadr keywords)))))))))
1570 (defun org-element-export-block-interpreter (export-block contents)
1571 "Interpret EXPORT-BLOCK element as Org syntax.
1572 CONTENTS is nil."
1573 (let ((type (org-element-property :type export-block)))
1574 (concat (format "#+BEGIN_%s\n" type)
1575 (org-element-property :value export-block)
1576 (format "#+END_%s" type))))
1579 ;;;; Fixed-width
1581 (defun org-element-fixed-width-parser (limit)
1582 "Parse a fixed-width section.
1584 LIMIT bounds the search.
1586 Return a list whose CAR is `fixed-width' and CDR is a plist
1587 containing `:begin', `:end', `:value' and `:post-blank' keywords.
1589 Assume point is at the beginning of the fixed-width area."
1590 (save-excursion
1591 (let* ((keywords (org-element--collect-affiliated-keywords))
1592 (begin (car keywords))
1593 value
1594 (end-area
1595 (progn
1596 (while (and (< (point) limit)
1597 (looking-at "[ \t]*:\\( \\|$\\)"))
1598 ;; Accumulate text without starting colons.
1599 (setq value
1600 (concat value
1601 (buffer-substring-no-properties
1602 (match-end 0) (point-at-eol))
1603 "\n"))
1604 (forward-line))
1605 (point)))
1606 (end (progn (skip-chars-forward " \r\t\n" limit)
1607 (if (eobp) (point) (point-at-bol)))))
1608 (list 'fixed-width
1609 (nconc
1610 (list :begin begin
1611 :end end
1612 :value value
1613 :post-blank (count-lines end-area end))
1614 (cadr keywords))))))
1616 (defun org-element-fixed-width-interpreter (fixed-width contents)
1617 "Interpret FIXED-WIDTH element as Org syntax.
1618 CONTENTS is nil."
1619 (replace-regexp-in-string
1620 "^" ": " (substring (org-element-property :value fixed-width) 0 -1)))
1623 ;;;; Horizontal Rule
1625 (defun org-element-horizontal-rule-parser (limit)
1626 "Parse an horizontal rule.
1628 LIMIT bounds the search.
1630 Return a list whose CAR is `horizontal-rule' and CDR is a plist
1631 containing `:begin', `:end' and `:post-blank' keywords."
1632 (save-excursion
1633 (let* ((keywords (org-element--collect-affiliated-keywords))
1634 (begin (car keywords))
1635 (post-hr (progn (forward-line) (point)))
1636 (end (progn (skip-chars-forward " \r\t\n" limit)
1637 (if (eobp) (point) (point-at-bol)))))
1638 (list 'horizontal-rule
1639 (nconc
1640 (list :begin begin
1641 :end end
1642 :post-blank (count-lines post-hr end))
1643 (cadr keywords))))))
1645 (defun org-element-horizontal-rule-interpreter (horizontal-rule contents)
1646 "Interpret HORIZONTAL-RULE element as Org syntax.
1647 CONTENTS is nil."
1648 "-----")
1651 ;;;; Keyword
1653 (defun org-element-keyword-parser (limit)
1654 "Parse a keyword at point.
1656 LIMIT bounds the search.
1658 Return a list whose CAR is `keyword' and CDR is a plist
1659 containing `:key', `:value', `:begin', `:end' and `:post-blank'
1660 keywords."
1661 (save-excursion
1662 (let* ((case-fold-search t)
1663 (begin (point))
1664 (key (progn (looking-at "[ \t]*#\\+\\(\\S-+\\):")
1665 (upcase (org-match-string-no-properties 1))))
1666 (value (org-trim (buffer-substring-no-properties
1667 (match-end 0) (point-at-eol))))
1668 (pos-before-blank (progn (forward-line) (point)))
1669 (end (progn (skip-chars-forward " \r\t\n" limit)
1670 (if (eobp) (point) (point-at-bol)))))
1671 (list 'keyword
1672 (list :key key
1673 :value value
1674 :begin begin
1675 :end end
1676 :post-blank (count-lines pos-before-blank end))))))
1678 (defun org-element-keyword-interpreter (keyword contents)
1679 "Interpret KEYWORD element as Org syntax.
1680 CONTENTS is nil."
1681 (format "#+%s: %s"
1682 (org-element-property :key keyword)
1683 (org-element-property :value keyword)))
1686 ;;;; Latex Environment
1688 (defun org-element-latex-environment-parser (limit)
1689 "Parse a LaTeX environment.
1691 LIMIT bounds the search.
1693 Return a list whose CAR is `latex-environment' and CDR is a plist
1694 containing `:begin', `:end', `:value' and `:post-blank'
1695 keywords.
1697 Assume point is at the beginning of the latex environment."
1698 (save-excursion
1699 (let* ((case-fold-search t)
1700 (code-begin (point))
1701 (keywords (org-element--collect-affiliated-keywords))
1702 (begin (car keywords))
1703 (env (progn (looking-at "^[ \t]*\\\\begin{\\([A-Za-z0-9*]+\\)}")
1704 (regexp-quote (match-string 1))))
1705 (code-end
1706 (progn (re-search-forward (format "^[ \t]*\\\\end{%s}" env) limit t)
1707 (forward-line)
1708 (point)))
1709 (value (buffer-substring-no-properties code-begin code-end))
1710 (end (progn (skip-chars-forward " \r\t\n" limit)
1711 (if (eobp) (point) (point-at-bol)))))
1712 (list 'latex-environment
1713 (nconc
1714 (list :begin begin
1715 :end end
1716 :value value
1717 :post-blank (count-lines code-end end))
1718 (cadr keywords))))))
1720 (defun org-element-latex-environment-interpreter (latex-environment contents)
1721 "Interpret LATEX-ENVIRONMENT element as Org syntax.
1722 CONTENTS is nil."
1723 (org-element-property :value latex-environment))
1726 ;;;; Paragraph
1728 (defun org-element-paragraph-parser (limit)
1729 "Parse a paragraph.
1731 LIMIT bounds the search.
1733 Return a list whose CAR is `paragraph' and CDR is a plist
1734 containing `:begin', `:end', `:contents-begin' and
1735 `:contents-end' and `:post-blank' keywords.
1737 Assume point is at the beginning of the paragraph."
1738 (save-excursion
1739 (let* ((contents-begin (point))
1740 (keywords (org-element--collect-affiliated-keywords))
1741 (begin (car keywords))
1742 (before-blank
1743 (let ((case-fold-search t))
1744 (end-of-line)
1745 (re-search-forward org-element-paragraph-separate limit 'm)
1746 (while (and (/= (point) limit)
1747 (cond
1748 ;; Skip non-existent or incomplete drawer.
1749 ((save-excursion
1750 (beginning-of-line)
1751 (and (looking-at "[ \t]*:\\S-")
1752 (or (not (looking-at org-drawer-regexp))
1753 (not (save-excursion
1754 (re-search-forward
1755 "^[ \t]*:END:" limit t)))))))
1756 ;; Stop at comments.
1757 ((save-excursion
1758 (beginning-of-line)
1759 (not (looking-at "[ \t]*#\\S-"))) nil)
1760 ;; Skip incomplete dynamic blocks.
1761 ((save-excursion
1762 (beginning-of-line)
1763 (looking-at "[ \t]*#\\+BEGIN: "))
1764 (not (save-excursion
1765 (re-search-forward
1766 "^[ \t]*\\+END:" limit t))))
1767 ;; Skip incomplete blocks.
1768 ((save-excursion
1769 (beginning-of-line)
1770 (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)"))
1771 (not (save-excursion
1772 (re-search-forward
1773 (concat "^[ \t]*#\\+END_"
1774 (match-string 1))
1775 limit t))))
1776 ;; Skip ill-formed keywords.
1777 ((not (save-excursion
1778 (beginning-of-line)
1779 (looking-at "[ \t]*#\\+\\S-+:"))))))
1780 (re-search-forward org-element-paragraph-separate limit 'm))
1781 (if (eobp) (point) (goto-char (line-beginning-position)))))
1782 (contents-end (progn (skip-chars-backward " \r\t\n" contents-begin)
1783 (forward-line)
1784 (point)))
1785 (end (progn (skip-chars-forward " \r\t\n" limit)
1786 (if (eobp) (point) (point-at-bol)))))
1787 (list 'paragraph
1788 ;; If paragraph has no affiliated keywords, it may not begin
1789 ;; at beginning of line if it starts an item.
1790 (nconc
1791 (list :begin (if (cadr keywords) begin contents-begin)
1792 :end end
1793 :contents-begin contents-begin
1794 :contents-end contents-end
1795 :post-blank (count-lines before-blank end))
1796 (cadr keywords))))))
1798 (defun org-element-paragraph-interpreter (paragraph contents)
1799 "Interpret PARAGRAPH element as Org syntax.
1800 CONTENTS is the contents of the element."
1801 contents)
1804 ;;;; Planning
1806 (defun org-element-planning-parser (limit)
1807 "Parse a planning.
1809 LIMIT bounds the search.
1811 Return a list whose CAR is `planning' and CDR is a plist
1812 containing `:closed', `:deadline', `:scheduled', `:begin', `:end'
1813 and `:post-blank' keywords."
1814 (save-excursion
1815 (let* ((case-fold-search nil)
1816 (begin (point))
1817 (post-blank (let ((before-blank (progn (forward-line) (point))))
1818 (skip-chars-forward " \r\t\n" limit)
1819 (unless (eobp) (beginning-of-line))
1820 (count-lines before-blank (point))))
1821 (end (point))
1822 closed deadline scheduled)
1823 (goto-char begin)
1824 (while (re-search-forward org-keyword-time-not-clock-regexp
1825 (line-end-position) t)
1826 (goto-char (match-end 1))
1827 (org-skip-whitespace)
1828 (let ((time (buffer-substring-no-properties
1829 (1+ (point)) (1- (match-end 0))))
1830 (keyword (match-string 1)))
1831 (cond ((equal keyword org-closed-string) (setq closed time))
1832 ((equal keyword org-deadline-string) (setq deadline time))
1833 (t (setq scheduled time)))))
1834 (list 'planning
1835 (list :closed closed
1836 :deadline deadline
1837 :scheduled scheduled
1838 :begin begin
1839 :end end
1840 :post-blank post-blank)))))
1842 (defun org-element-planning-interpreter (planning contents)
1843 "Interpret PLANNING element as Org syntax.
1844 CONTENTS is nil."
1845 (mapconcat
1846 'identity
1847 (delq nil
1848 (list (let ((closed (org-element-property :closed planning)))
1849 (when closed (concat org-closed-string " [" closed "]")))
1850 (let ((deadline (org-element-property :deadline planning)))
1851 (when deadline (concat org-deadline-string " <" deadline ">")))
1852 (let ((scheduled (org-element-property :scheduled planning)))
1853 (when scheduled
1854 (concat org-scheduled-string " <" scheduled ">")))))
1855 " "))
1858 ;;;; Property Drawer
1860 (defun org-element-property-drawer-parser (limit)
1861 "Parse a property drawer.
1863 LIMIT bounds the search.
1865 Return a list whose CAR is `property-drawer' and CDR is a plist
1866 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1867 `:contents-end', `:properties' and `:post-blank' keywords.
1869 Assume point is at the beginning of the property drawer."
1870 (save-excursion
1871 (let ((case-fold-search t)
1872 (begin (point))
1873 (prop-begin (progn (forward-line) (point)))
1874 (hidden (org-invisible-p2))
1875 (properties
1876 (let (val)
1877 (while (not (looking-at "^[ \t]*:END:"))
1878 (when (looking-at "[ \t]*:\\([A-Za-z][-_A-Za-z0-9]*\\):")
1879 (push (cons (org-match-string-no-properties 1)
1880 (org-trim
1881 (buffer-substring-no-properties
1882 (match-end 0) (point-at-eol))))
1883 val))
1884 (forward-line))
1885 val))
1886 (prop-end (progn (re-search-forward "^[ \t]*:END:" limit t)
1887 (point-at-bol)))
1888 (pos-before-blank (progn (forward-line) (point)))
1889 (end (progn (skip-chars-forward " \r\t\n" limit)
1890 (if (eobp) (point) (point-at-bol)))))
1891 (list 'property-drawer
1892 (list :begin begin
1893 :end end
1894 :hiddenp hidden
1895 :properties properties
1896 :post-blank (count-lines pos-before-blank end))))))
1898 (defun org-element-property-drawer-interpreter (property-drawer contents)
1899 "Interpret PROPERTY-DRAWER element as Org syntax.
1900 CONTENTS is nil."
1901 (let ((props (org-element-property :properties property-drawer)))
1902 (concat
1903 ":PROPERTIES:\n"
1904 (mapconcat (lambda (p)
1905 (format org-property-format (format ":%s:" (car p)) (cdr p)))
1906 (nreverse props) "\n")
1907 "\n:END:")))
1910 ;;;; Quote Section
1912 (defun org-element-quote-section-parser (limit)
1913 "Parse a quote section.
1915 LIMIT bounds the search.
1917 Return a list whose CAR is `quote-section' and CDR is a plist
1918 containing `:begin', `:end', `:value' and `:post-blank' keywords.
1920 Assume point is at beginning of the section."
1921 (save-excursion
1922 (let* ((begin (point))
1923 (end (progn (org-with-limited-levels (outline-next-heading))
1924 (point)))
1925 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
1926 (forward-line)
1927 (point)))
1928 (value (buffer-substring-no-properties begin pos-before-blank)))
1929 (list 'quote-section
1930 (list :begin begin
1931 :end end
1932 :value value
1933 :post-blank (count-lines pos-before-blank end))))))
1935 (defun org-element-quote-section-interpreter (quote-section contents)
1936 "Interpret QUOTE-SECTION element as Org syntax.
1937 CONTENTS is nil."
1938 (org-element-property :value quote-section))
1941 ;;;; Src Block
1943 (defun org-element-src-block-parser (limit)
1944 "Parse a src block.
1946 LIMIT bounds the search.
1948 Return a list whose CAR is `src-block' and CDR is a plist
1949 containing `:language', `:switches', `:parameters', `:begin',
1950 `:end', `:hiddenp', `:number-lines', `:retain-labels',
1951 `:use-labels', `:label-fmt', `:preserve-indent', `:value' and
1952 `:post-blank' keywords.
1954 Assume point is at the beginning of the block."
1955 (let ((case-fold-search t))
1956 (if (not (save-excursion (re-search-forward "^[ \t]*#\\+END_SRC" limit t)))
1957 ;; Incomplete block: parse it as a paragraph.
1958 (org-element-paragraph-parser limit)
1959 (let ((contents-end (match-beginning 0)))
1960 (save-excursion
1961 (let* ((keywords (org-element--collect-affiliated-keywords))
1962 ;; Get beginning position.
1963 (begin (car keywords))
1964 ;; Get language as a string.
1965 (language
1966 (progn
1967 (looking-at
1968 (concat "^[ \t]*#\\+BEGIN_SRC"
1969 "\\(?: +\\(\\S-+\\)\\)?"
1970 "\\(\\(?: +\\(?:-l \".*?\"\\|[-+][A-Za-z]\\)\\)+\\)?"
1971 "\\(.*\\)[ \t]*$"))
1972 (org-match-string-no-properties 1)))
1973 ;; Get switches.
1974 (switches (org-match-string-no-properties 2))
1975 ;; Get parameters.
1976 (parameters (org-match-string-no-properties 3))
1977 ;; Switches analysis
1978 (number-lines (cond ((not switches) nil)
1979 ((string-match "-n\\>" switches) 'new)
1980 ((string-match "+n\\>" switches) 'continued)))
1981 (preserve-indent (and switches (string-match "-i\\>" switches)))
1982 (label-fmt (and switches
1983 (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
1984 (match-string 1 switches)))
1985 ;; Should labels be retained in (or stripped from)
1986 ;; src blocks?
1987 (retain-labels
1988 (or (not switches)
1989 (not (string-match "-r\\>" switches))
1990 (and number-lines (string-match "-k\\>" switches))))
1991 ;; What should code-references use - labels or
1992 ;; line-numbers?
1993 (use-labels
1994 (or (not switches)
1995 (and retain-labels (not (string-match "-k\\>" switches)))))
1996 ;; Get visibility status.
1997 (hidden (progn (forward-line) (org-invisible-p2)))
1998 ;; Retrieve code.
1999 (value (buffer-substring-no-properties (point) contents-end))
2000 (pos-before-blank (progn (goto-char contents-end)
2001 (forward-line)
2002 (point)))
2003 ;; Get position after ending blank lines.
2004 (end (progn (skip-chars-forward " \r\t\n" limit)
2005 (if (eobp) (point) (point-at-bol)))))
2006 (list 'src-block
2007 (nconc
2008 (list :language language
2009 :switches (and (org-string-nw-p switches)
2010 (org-trim switches))
2011 :parameters (and (org-string-nw-p parameters)
2012 (org-trim parameters))
2013 :begin begin
2014 :end end
2015 :number-lines number-lines
2016 :preserve-indent preserve-indent
2017 :retain-labels retain-labels
2018 :use-labels use-labels
2019 :label-fmt label-fmt
2020 :hiddenp hidden
2021 :value value
2022 :post-blank (count-lines pos-before-blank end))
2023 (cadr keywords)))))))))
2025 (defun org-element-src-block-interpreter (src-block contents)
2026 "Interpret SRC-BLOCK element as Org syntax.
2027 CONTENTS is nil."
2028 (let ((lang (org-element-property :language src-block))
2029 (switches (org-element-property :switches src-block))
2030 (params (org-element-property :parameters src-block))
2031 (value (let ((val (org-element-property :value src-block)))
2032 (cond
2034 (org-src-preserve-indentation val)
2035 ((zerop org-edit-src-content-indentation)
2036 (org-remove-indentation val))
2038 (let ((ind (make-string
2039 org-edit-src-content-indentation 32)))
2040 (replace-regexp-in-string
2041 "\\(^\\)[ \t]*\\S-" ind
2042 (org-remove-indentation val) nil nil 1)))))))
2043 (concat (format "#+BEGIN_SRC%s\n"
2044 (concat (and lang (concat " " lang))
2045 (and switches (concat " " switches))
2046 (and params (concat " " params))))
2047 value
2048 "#+END_SRC")))
2051 ;;;; Table
2053 (defun org-element-table-parser (limit)
2054 "Parse a table at point.
2056 LIMIT bounds the search.
2058 Return a list whose CAR is `table' and CDR is a plist containing
2059 `:begin', `:end', `:tblfm', `:type', `:contents-begin',
2060 `:contents-end', `:value' and `:post-blank' keywords.
2062 Assume point is at the beginning of the table."
2063 (save-excursion
2064 (let* ((case-fold-search t)
2065 (table-begin (point))
2066 (type (if (org-at-table.el-p) 'table.el 'org))
2067 (keywords (org-element--collect-affiliated-keywords))
2068 (begin (car keywords))
2069 (table-end (goto-char (marker-position (org-table-end t))))
2070 (tblfm (let (acc)
2071 (while (looking-at "[ \t]*#\\+TBLFM: +\\(.*\\)[ \t]*$")
2072 (push (org-match-string-no-properties 1) acc)
2073 (forward-line))
2074 acc))
2075 (pos-before-blank (point))
2076 (end (progn (skip-chars-forward " \r\t\n" limit)
2077 (if (eobp) (point) (point-at-bol)))))
2078 (list 'table
2079 (nconc
2080 (list :begin begin
2081 :end end
2082 :type type
2083 :tblfm tblfm
2084 ;; Only `org' tables have contents. `table.el' tables
2085 ;; use a `:value' property to store raw table as
2086 ;; a string.
2087 :contents-begin (and (eq type 'org) table-begin)
2088 :contents-end (and (eq type 'org) table-end)
2089 :value (and (eq type 'table.el)
2090 (buffer-substring-no-properties
2091 table-begin table-end))
2092 :post-blank (count-lines pos-before-blank end))
2093 (cadr keywords))))))
2095 (defun org-element-table-interpreter (table contents)
2096 "Interpret TABLE element as Org syntax.
2097 CONTENTS is nil."
2098 (if (eq (org-element-property :type table) 'table.el)
2099 (org-remove-indentation (org-element-property :value table))
2100 (concat (with-temp-buffer (insert contents)
2101 (org-table-align)
2102 (buffer-string))
2103 (mapconcat (lambda (fm) (concat "#+TBLFM: " fm))
2104 (reverse (org-element-property :tblfm table))
2105 "\n"))))
2108 ;;;; Table Row
2110 (defun org-element-table-row-parser (limit)
2111 "Parse table row at point.
2113 LIMIT bounds the search.
2115 Return a list whose CAR is `table-row' and CDR is a plist
2116 containing `:begin', `:end', `:contents-begin', `:contents-end',
2117 `:type' and `:post-blank' keywords."
2118 (save-excursion
2119 (let* ((type (if (looking-at "^[ \t]*|-") 'rule 'standard))
2120 (begin (point))
2121 ;; A table rule has no contents. In that case, ensure
2122 ;; CONTENTS-BEGIN matches CONTENTS-END.
2123 (contents-begin (and (eq type 'standard)
2124 (search-forward "|")
2125 (point)))
2126 (contents-end (and (eq type 'standard)
2127 (progn
2128 (end-of-line)
2129 (skip-chars-backward " \t")
2130 (point))))
2131 (end (progn (forward-line) (point))))
2132 (list 'table-row
2133 (list :type type
2134 :begin begin
2135 :end end
2136 :contents-begin contents-begin
2137 :contents-end contents-end
2138 :post-blank 0)))))
2140 (defun org-element-table-row-interpreter (table-row contents)
2141 "Interpret TABLE-ROW element as Org syntax.
2142 CONTENTS is the contents of the table row."
2143 (if (eq (org-element-property :type table-row) 'rule) "|-"
2144 (concat "| " contents)))
2147 ;;;; Verse Block
2149 (defun org-element-verse-block-parser (limit)
2150 "Parse a verse block.
2152 LIMIT bounds the search.
2154 Return a list whose CAR is `verse-block' and CDR is a plist
2155 containing `:begin', `:end', `:contents-begin', `:contents-end',
2156 `:hiddenp' and `:post-blank' keywords.
2158 Assume point is at beginning of the block."
2159 (let ((case-fold-search t))
2160 (if (not (save-excursion
2161 (re-search-forward "^[ \t]*#\\+END_VERSE" limit t)))
2162 ;; Incomplete block: parse it as a paragraph.
2163 (org-element-paragraph-parser limit)
2164 (let ((contents-end (match-beginning 0)))
2165 (save-excursion
2166 (let* ((keywords (org-element--collect-affiliated-keywords))
2167 (begin (car keywords))
2168 (hidden (progn (forward-line) (org-invisible-p2)))
2169 (contents-begin (point))
2170 (pos-before-blank (progn (goto-char contents-end)
2171 (forward-line)
2172 (point)))
2173 (end (progn (skip-chars-forward " \r\t\n" limit)
2174 (if (eobp) (point) (point-at-bol)))))
2175 (list 'verse-block
2176 (nconc
2177 (list :begin begin
2178 :end end
2179 :contents-begin contents-begin
2180 :contents-end contents-end
2181 :hiddenp hidden
2182 :post-blank (count-lines pos-before-blank end))
2183 (cadr keywords)))))))))
2185 (defun org-element-verse-block-interpreter (verse-block contents)
2186 "Interpret VERSE-BLOCK element as Org syntax.
2187 CONTENTS is verse block contents."
2188 (format "#+BEGIN_VERSE\n%s#+END_VERSE" contents))
2192 ;;; Objects
2194 ;; Unlike to elements, interstices can be found between objects.
2195 ;; That's why, along with the parser, successor functions are provided
2196 ;; for each object. Some objects share the same successor (i.e. `code'
2197 ;; and `verbatim' objects).
2199 ;; A successor must accept a single argument bounding the search. It
2200 ;; will return either a cons cell whose CAR is the object's type, as
2201 ;; a symbol, and CDR the position of its next occurrence, or nil.
2203 ;; Successors follow the naming convention:
2204 ;; org-element-NAME-successor, where NAME is the name of the
2205 ;; successor, as defined in `org-element-all-successors'.
2207 ;; Some object types (i.e. `italic') are recursive. Restrictions on
2208 ;; object types they can contain will be specified in
2209 ;; `org-element-object-restrictions'.
2211 ;; Adding a new type of object is simple. Implement a successor,
2212 ;; a parser, and an interpreter for it, all following the naming
2213 ;; convention. Register type in `org-element-all-objects' and
2214 ;; successor in `org-element-all-successors'. Maybe tweak
2215 ;; restrictions about it, and that's it.
2218 ;;;; Bold
2220 (defun org-element-bold-parser ()
2221 "Parse bold object at point.
2223 Return a list whose CAR is `bold' and CDR is a plist with
2224 `:begin', `:end', `:contents-begin' and `:contents-end' and
2225 `:post-blank' keywords.
2227 Assume point is at the first star marker."
2228 (save-excursion
2229 (unless (bolp) (backward-char 1))
2230 (looking-at org-emph-re)
2231 (let ((begin (match-beginning 2))
2232 (contents-begin (match-beginning 4))
2233 (contents-end (match-end 4))
2234 (post-blank (progn (goto-char (match-end 2))
2235 (skip-chars-forward " \t")))
2236 (end (point)))
2237 (list 'bold
2238 (list :begin begin
2239 :end end
2240 :contents-begin contents-begin
2241 :contents-end contents-end
2242 :post-blank post-blank)))))
2244 (defun org-element-bold-interpreter (bold contents)
2245 "Interpret BOLD object as Org syntax.
2246 CONTENTS is the contents of the object."
2247 (format "*%s*" contents))
2249 (defun org-element-text-markup-successor (limit)
2250 "Search for the next text-markup object.
2252 LIMIT bounds the search.
2254 Return value is a cons cell whose CAR is a symbol among `bold',
2255 `italic', `underline', `strike-through', `code' and `verbatim'
2256 and CDR is beginning position."
2257 (save-excursion
2258 (unless (bolp) (backward-char))
2259 (when (re-search-forward org-emph-re limit t)
2260 (let ((marker (match-string 3)))
2261 (cons (cond
2262 ((equal marker "*") 'bold)
2263 ((equal marker "/") 'italic)
2264 ((equal marker "_") 'underline)
2265 ((equal marker "+") 'strike-through)
2266 ((equal marker "~") 'code)
2267 ((equal marker "=") 'verbatim)
2268 (t (error "Unknown marker at %d" (match-beginning 3))))
2269 (match-beginning 2))))))
2272 ;;;; Code
2274 (defun org-element-code-parser ()
2275 "Parse code object at point.
2277 Return a list whose CAR is `code' and CDR is a plist with
2278 `:value', `:begin', `:end' and `:post-blank' keywords.
2280 Assume point is at the first tilde marker."
2281 (save-excursion
2282 (unless (bolp) (backward-char 1))
2283 (looking-at org-emph-re)
2284 (let ((begin (match-beginning 2))
2285 (value (org-match-string-no-properties 4))
2286 (post-blank (progn (goto-char (match-end 2))
2287 (skip-chars-forward " \t")))
2288 (end (point)))
2289 (list 'code
2290 (list :value value
2291 :begin begin
2292 :end end
2293 :post-blank post-blank)))))
2295 (defun org-element-code-interpreter (code contents)
2296 "Interpret CODE object as Org syntax.
2297 CONTENTS is nil."
2298 (format "~%s~" (org-element-property :value code)))
2301 ;;;; Entity
2303 (defun org-element-entity-parser ()
2304 "Parse entity at point.
2306 Return a list whose CAR is `entity' and CDR a plist with
2307 `:begin', `:end', `:latex', `:latex-math-p', `:html', `:latin1',
2308 `:utf-8', `:ascii', `:use-brackets-p' and `:post-blank' as
2309 keywords.
2311 Assume point is at the beginning of the entity."
2312 (save-excursion
2313 (looking-at "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)")
2314 (let* ((value (org-entity-get (match-string 1)))
2315 (begin (match-beginning 0))
2316 (bracketsp (string= (match-string 2) "{}"))
2317 (post-blank (progn (goto-char (match-end 1))
2318 (when bracketsp (forward-char 2))
2319 (skip-chars-forward " \t")))
2320 (end (point)))
2321 (list 'entity
2322 (list :name (car value)
2323 :latex (nth 1 value)
2324 :latex-math-p (nth 2 value)
2325 :html (nth 3 value)
2326 :ascii (nth 4 value)
2327 :latin1 (nth 5 value)
2328 :utf-8 (nth 6 value)
2329 :begin begin
2330 :end end
2331 :use-brackets-p bracketsp
2332 :post-blank post-blank)))))
2334 (defun org-element-entity-interpreter (entity contents)
2335 "Interpret ENTITY object as Org syntax.
2336 CONTENTS is nil."
2337 (concat "\\"
2338 (org-element-property :name entity)
2339 (when (org-element-property :use-brackets-p entity) "{}")))
2341 (defun org-element-latex-or-entity-successor (limit)
2342 "Search for the next latex-fragment or entity object.
2344 LIMIT bounds the search.
2346 Return value is a cons cell whose CAR is `entity' or
2347 `latex-fragment' and CDR is beginning position."
2348 (save-excursion
2349 (let ((matchers (plist-get org-format-latex-options :matchers))
2350 ;; ENTITY-RE matches both LaTeX commands and Org entities.
2351 (entity-re
2352 "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))
2353 (when (re-search-forward
2354 (concat (mapconcat (lambda (e) (nth 1 (assoc e org-latex-regexps)))
2355 matchers "\\|")
2356 "\\|" entity-re)
2357 limit t)
2358 (goto-char (match-beginning 0))
2359 (if (looking-at entity-re)
2360 ;; Determine if it's a real entity or a LaTeX command.
2361 (cons (if (org-entity-get (match-string 1)) 'entity 'latex-fragment)
2362 (match-beginning 0))
2363 ;; No entity nor command: point is at a LaTeX fragment.
2364 ;; Determine its type to get the correct beginning position.
2365 (cons 'latex-fragment
2366 (catch 'return
2367 (mapc (lambda (e)
2368 (when (looking-at (nth 1 (assoc e org-latex-regexps)))
2369 (throw 'return
2370 (match-beginning
2371 (nth 2 (assoc e org-latex-regexps))))))
2372 matchers)
2373 (point))))))))
2376 ;;;; Export Snippet
2378 (defun org-element-export-snippet-parser ()
2379 "Parse export snippet at point.
2381 Return a list whose CAR is `export-snippet' and CDR a plist with
2382 `:begin', `:end', `:back-end', `:value' and `:post-blank' as
2383 keywords.
2385 Assume point is at the beginning of the snippet."
2386 (save-excursion
2387 (re-search-forward "@@\\([-A-Za-z0-9]+\\):" nil t)
2388 (let* ((begin (match-beginning 0))
2389 (back-end (org-match-string-no-properties 1))
2390 (value (buffer-substring-no-properties
2391 (point)
2392 (progn (re-search-forward "@@" nil t) (match-beginning 0))))
2393 (post-blank (skip-chars-forward " \t"))
2394 (end (point)))
2395 (list 'export-snippet
2396 (list :back-end back-end
2397 :value value
2398 :begin begin
2399 :end end
2400 :post-blank post-blank)))))
2402 (defun org-element-export-snippet-interpreter (export-snippet contents)
2403 "Interpret EXPORT-SNIPPET object as Org syntax.
2404 CONTENTS is nil."
2405 (format "@@%s:%s@@"
2406 (org-element-property :back-end export-snippet)
2407 (org-element-property :value export-snippet)))
2409 (defun org-element-export-snippet-successor (limit)
2410 "Search for the next export-snippet object.
2412 LIMIT bounds the search.
2414 Return value is a cons cell whose CAR is `export-snippet' and CDR
2415 its beginning position."
2416 (save-excursion
2417 (let (beg)
2418 (when (and (re-search-forward "@@[-A-Za-z0-9]+:" limit t)
2419 (setq beg (match-beginning 0))
2420 (re-search-forward "@@" limit t))
2421 (cons 'export-snippet beg)))))
2424 ;;;; Footnote Reference
2426 (defun org-element-footnote-reference-parser ()
2427 "Parse footnote reference at point.
2429 Return a list whose CAR is `footnote-reference' and CDR a plist
2430 with `:label', `:type', `:inline-definition', `:begin', `:end'
2431 and `:post-blank' as keywords."
2432 (save-excursion
2433 (looking-at org-footnote-re)
2434 (let* ((begin (point))
2435 (label (or (org-match-string-no-properties 2)
2436 (org-match-string-no-properties 3)
2437 (and (match-string 1)
2438 (concat "fn:" (org-match-string-no-properties 1)))))
2439 (type (if (or (not label) (match-string 1)) 'inline 'standard))
2440 (inner-begin (match-end 0))
2441 (inner-end
2442 (let ((count 1))
2443 (forward-char)
2444 (while (and (> count 0) (re-search-forward "[][]" nil t))
2445 (if (equal (match-string 0) "[") (incf count) (decf count)))
2446 (1- (point))))
2447 (post-blank (progn (goto-char (1+ inner-end))
2448 (skip-chars-forward " \t")))
2449 (end (point))
2450 (footnote-reference
2451 (list 'footnote-reference
2452 (list :label label
2453 :type type
2454 :begin begin
2455 :end end
2456 :post-blank post-blank))))
2457 (org-element-put-property
2458 footnote-reference :inline-definition
2459 (and (eq type 'inline)
2460 (org-element-parse-secondary-string
2461 (buffer-substring inner-begin inner-end)
2462 (org-element-restriction 'footnote-reference)
2463 footnote-reference))))))
2465 (defun org-element-footnote-reference-interpreter (footnote-reference contents)
2466 "Interpret FOOTNOTE-REFERENCE object as Org syntax.
2467 CONTENTS is nil."
2468 (let ((label (or (org-element-property :label footnote-reference) "fn:"))
2469 (def
2470 (let ((inline-def
2471 (org-element-property :inline-definition footnote-reference)))
2472 (if (not inline-def) ""
2473 (concat ":" (org-element-interpret-data inline-def))))))
2474 (format "[%s]" (concat label def))))
2476 (defun org-element-footnote-reference-successor (limit)
2477 "Search for the next footnote-reference object.
2479 LIMIT bounds the search.
2481 Return value is a cons cell whose CAR is `footnote-reference' and
2482 CDR is beginning position."
2483 (save-excursion
2484 (catch 'exit
2485 (while (re-search-forward org-footnote-re limit t)
2486 (save-excursion
2487 (let ((beg (match-beginning 0))
2488 (count 1))
2489 (backward-char)
2490 (while (re-search-forward "[][]" limit t)
2491 (if (equal (match-string 0) "[") (incf count) (decf count))
2492 (when (zerop count)
2493 (throw 'exit (cons 'footnote-reference beg))))))))))
2496 ;;;; Inline Babel Call
2498 (defun org-element-inline-babel-call-parser ()
2499 "Parse inline babel call at point.
2501 Return a list whose CAR is `inline-babel-call' and CDR a plist
2502 with `:begin', `:end', `:info' and `:post-blank' as keywords.
2504 Assume point is at the beginning of the babel call."
2505 (save-excursion
2506 (unless (bolp) (backward-char))
2507 (looking-at org-babel-inline-lob-one-liner-regexp)
2508 (let ((info (save-match-data (org-babel-lob-get-info)))
2509 (begin (match-end 1))
2510 (post-blank (progn (goto-char (match-end 0))
2511 (skip-chars-forward " \t")))
2512 (end (point)))
2513 (list 'inline-babel-call
2514 (list :begin begin
2515 :end end
2516 :info info
2517 :post-blank post-blank)))))
2519 (defun org-element-inline-babel-call-interpreter (inline-babel-call contents)
2520 "Interpret INLINE-BABEL-CALL object as Org syntax.
2521 CONTENTS is nil."
2522 (let* ((babel-info (org-element-property :info inline-babel-call))
2523 (main-source (car babel-info))
2524 (post-options (nth 1 babel-info)))
2525 (concat "call_"
2526 (if (string-match "\\[\\(\\[.*?\\]\\)\\]" main-source)
2527 ;; Remove redundant square brackets.
2528 (replace-match
2529 (match-string 1 main-source) nil nil main-source)
2530 main-source)
2531 (and post-options (format "[%s]" post-options)))))
2533 (defun org-element-inline-babel-call-successor (limit)
2534 "Search for the next inline-babel-call object.
2536 LIMIT bounds the search.
2538 Return value is a cons cell whose CAR is `inline-babel-call' and
2539 CDR is beginning position."
2540 (save-excursion
2541 ;; Use a simplified version of
2542 ;; org-babel-inline-lob-one-liner-regexp as regexp for more speed.
2543 (when (re-search-forward
2544 "\\(?:babel\\|call\\)_\\([^()\n]+?\\)\\(\\[\\(.*\\)\\]\\|\\(\\)\\)(\\([^\n]*\\))\\(\\[\\(.*?\\)\\]\\)?"
2545 limit t)
2546 (cons 'inline-babel-call (match-beginning 0)))))
2549 ;;;; Inline Src Block
2551 (defun org-element-inline-src-block-parser ()
2552 "Parse inline source block at point.
2554 LIMIT bounds the search.
2556 Return a list whose CAR is `inline-src-block' and CDR a plist
2557 with `:begin', `:end', `:language', `:value', `:parameters' and
2558 `:post-blank' as keywords.
2560 Assume point is at the beginning of the inline src block."
2561 (save-excursion
2562 (unless (bolp) (backward-char))
2563 (looking-at org-babel-inline-src-block-regexp)
2564 (let ((begin (match-beginning 1))
2565 (language (org-match-string-no-properties 2))
2566 (parameters (org-match-string-no-properties 4))
2567 (value (org-match-string-no-properties 5))
2568 (post-blank (progn (goto-char (match-end 0))
2569 (skip-chars-forward " \t")))
2570 (end (point)))
2571 (list 'inline-src-block
2572 (list :language language
2573 :value value
2574 :parameters parameters
2575 :begin begin
2576 :end end
2577 :post-blank post-blank)))))
2579 (defun org-element-inline-src-block-interpreter (inline-src-block contents)
2580 "Interpret INLINE-SRC-BLOCK object as Org syntax.
2581 CONTENTS is nil."
2582 (let ((language (org-element-property :language inline-src-block))
2583 (arguments (org-element-property :parameters inline-src-block))
2584 (body (org-element-property :value inline-src-block)))
2585 (format "src_%s%s{%s}"
2586 language
2587 (if arguments (format "[%s]" arguments) "")
2588 body)))
2590 (defun org-element-inline-src-block-successor (limit)
2591 "Search for the next inline-babel-call element.
2593 LIMIT bounds the search.
2595 Return value is a cons cell whose CAR is `inline-babel-call' and
2596 CDR is beginning position."
2597 (save-excursion
2598 (when (re-search-forward org-babel-inline-src-block-regexp limit t)
2599 (cons 'inline-src-block (match-beginning 1)))))
2601 ;;;; Italic
2603 (defun org-element-italic-parser ()
2604 "Parse italic object at point.
2606 Return a list whose CAR is `italic' and CDR is a plist with
2607 `:begin', `:end', `:contents-begin' and `:contents-end' and
2608 `:post-blank' keywords.
2610 Assume point is at the first slash marker."
2611 (save-excursion
2612 (unless (bolp) (backward-char 1))
2613 (looking-at org-emph-re)
2614 (let ((begin (match-beginning 2))
2615 (contents-begin (match-beginning 4))
2616 (contents-end (match-end 4))
2617 (post-blank (progn (goto-char (match-end 2))
2618 (skip-chars-forward " \t")))
2619 (end (point)))
2620 (list 'italic
2621 (list :begin begin
2622 :end end
2623 :contents-begin contents-begin
2624 :contents-end contents-end
2625 :post-blank post-blank)))))
2627 (defun org-element-italic-interpreter (italic contents)
2628 "Interpret ITALIC object as Org syntax.
2629 CONTENTS is the contents of the object."
2630 (format "/%s/" contents))
2633 ;;;; Latex Fragment
2635 (defun org-element-latex-fragment-parser ()
2636 "Parse latex fragment at point.
2638 Return a list whose CAR is `latex-fragment' and CDR a plist with
2639 `:value', `:begin', `:end', and `:post-blank' as keywords.
2641 Assume point is at the beginning of the latex fragment."
2642 (save-excursion
2643 (let* ((begin (point))
2644 (substring-match
2645 (catch 'exit
2646 (mapc (lambda (e)
2647 (let ((latex-regexp (nth 1 (assoc e org-latex-regexps))))
2648 (when (or (looking-at latex-regexp)
2649 (and (not (bobp))
2650 (save-excursion
2651 (backward-char)
2652 (looking-at latex-regexp))))
2653 (throw 'exit (nth 2 (assoc e org-latex-regexps))))))
2654 (plist-get org-format-latex-options :matchers))
2655 ;; None found: it's a macro.
2656 (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")
2658 (value (match-string-no-properties substring-match))
2659 (post-blank (progn (goto-char (match-end substring-match))
2660 (skip-chars-forward " \t")))
2661 (end (point)))
2662 (list 'latex-fragment
2663 (list :value value
2664 :begin begin
2665 :end end
2666 :post-blank post-blank)))))
2668 (defun org-element-latex-fragment-interpreter (latex-fragment contents)
2669 "Interpret LATEX-FRAGMENT object as Org syntax.
2670 CONTENTS is nil."
2671 (org-element-property :value latex-fragment))
2673 ;;;; Line Break
2675 (defun org-element-line-break-parser ()
2676 "Parse line break at point.
2678 Return a list whose CAR is `line-break', and CDR a plist with
2679 `:begin', `:end' and `:post-blank' keywords.
2681 Assume point is at the beginning of the line break."
2682 (list 'line-break (list :begin (point) :end (point-at-eol) :post-blank 0)))
2684 (defun org-element-line-break-interpreter (line-break contents)
2685 "Interpret LINE-BREAK object as Org syntax.
2686 CONTENTS is nil."
2687 "\\\\")
2689 (defun org-element-line-break-successor (limit)
2690 "Search for the next line-break object.
2692 LIMIT bounds the search.
2694 Return value is a cons cell whose CAR is `line-break' and CDR is
2695 beginning position."
2696 (save-excursion
2697 (let ((beg (and (re-search-forward "[^\\\\]\\(\\\\\\\\\\)[ \t]*$" limit t)
2698 (goto-char (match-beginning 1)))))
2699 ;; A line break can only happen on a non-empty line.
2700 (when (and beg (re-search-backward "\\S-" (point-at-bol) t))
2701 (cons 'line-break beg)))))
2704 ;;;; Link
2706 (defun org-element-link-parser ()
2707 "Parse link at point.
2709 Return a list whose CAR is `link' and CDR a plist with `:type',
2710 `:path', `:raw-link', `:begin', `:end', `:contents-begin',
2711 `:contents-end' and `:post-blank' as keywords.
2713 Assume point is at the beginning of the link."
2714 (save-excursion
2715 (let ((begin (point))
2716 end contents-begin contents-end link-end post-blank path type
2717 raw-link link)
2718 (cond
2719 ;; Type 1: Text targeted from a radio target.
2720 ((and org-target-link-regexp (looking-at org-target-link-regexp))
2721 (setq type "radio"
2722 link-end (match-end 0)
2723 path (org-match-string-no-properties 0)))
2724 ;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]]
2725 ((looking-at org-bracket-link-regexp)
2726 (setq contents-begin (match-beginning 3)
2727 contents-end (match-end 3)
2728 link-end (match-end 0)
2729 ;; RAW-LINK is the original link.
2730 raw-link (org-match-string-no-properties 1)
2731 link (org-translate-link
2732 (org-link-expand-abbrev
2733 (org-link-unescape raw-link))))
2734 ;; Determine TYPE of link and set PATH accordingly.
2735 (cond
2736 ;; File type.
2737 ((or (file-name-absolute-p link) (string-match "^\\.\\.?/" link))
2738 (setq type "file" path link))
2739 ;; Explicit type (http, irc, bbdb...). See `org-link-types'.
2740 ((string-match org-link-re-with-space3 link)
2741 (setq type (match-string 1 link) path (match-string 2 link)))
2742 ;; Id type: PATH is the id.
2743 ((string-match "^id:\\([-a-f0-9]+\\)" link)
2744 (setq type "id" path (match-string 1 link)))
2745 ;; Code-ref type: PATH is the name of the reference.
2746 ((string-match "^(\\(.*\\))$" link)
2747 (setq type "coderef" path (match-string 1 link)))
2748 ;; Custom-id type: PATH is the name of the custom id.
2749 ((= (aref link 0) ?#)
2750 (setq type "custom-id" path (substring link 1)))
2751 ;; Fuzzy type: Internal link either matches a target, an
2752 ;; headline name or nothing. PATH is the target or
2753 ;; headline's name.
2754 (t (setq type "fuzzy" path link))))
2755 ;; Type 3: Plain link, i.e. http://orgmode.org
2756 ((looking-at org-plain-link-re)
2757 (setq raw-link (org-match-string-no-properties 0)
2758 type (org-match-string-no-properties 1)
2759 path (org-match-string-no-properties 2)
2760 link-end (match-end 0)))
2761 ;; Type 4: Angular link, i.e. <http://orgmode.org>
2762 ((looking-at org-angle-link-re)
2763 (setq raw-link (buffer-substring-no-properties
2764 (match-beginning 1) (match-end 2))
2765 type (org-match-string-no-properties 1)
2766 path (org-match-string-no-properties 2)
2767 link-end (match-end 0))))
2768 ;; In any case, deduce end point after trailing white space from
2769 ;; LINK-END variable.
2770 (setq post-blank (progn (goto-char link-end) (skip-chars-forward " \t"))
2771 end (point))
2772 (list 'link
2773 (list :type type
2774 :path path
2775 :raw-link (or raw-link path)
2776 :begin begin
2777 :end end
2778 :contents-begin contents-begin
2779 :contents-end contents-end
2780 :post-blank post-blank)))))
2782 (defun org-element-link-interpreter (link contents)
2783 "Interpret LINK object as Org syntax.
2784 CONTENTS is the contents of the object, or nil."
2785 (let ((type (org-element-property :type link))
2786 (raw-link (org-element-property :raw-link link)))
2787 (if (string= type "radio") raw-link
2788 (format "[[%s]%s]"
2789 raw-link
2790 (if contents (format "[%s]" contents) "")))))
2792 (defun org-element-link-successor (limit)
2793 "Search for the next link object.
2795 LIMIT bounds the search.
2797 Return value is a cons cell whose CAR is `link' and CDR is
2798 beginning position."
2799 (save-excursion
2800 (let ((link-regexp
2801 (if (not org-target-link-regexp) org-any-link-re
2802 (concat org-any-link-re "\\|" org-target-link-regexp))))
2803 (when (re-search-forward link-regexp limit t)
2804 (cons 'link (match-beginning 0))))))
2807 ;;;; Macro
2809 (defun org-element-macro-parser ()
2810 "Parse macro at point.
2812 Return a list whose CAR is `macro' and CDR a plist with `:key',
2813 `:args', `:begin', `:end', `:value' and `:post-blank' as
2814 keywords.
2816 Assume point is at the macro."
2817 (save-excursion
2818 (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
2819 (let ((begin (point))
2820 (key (downcase (org-match-string-no-properties 1)))
2821 (value (org-match-string-no-properties 0))
2822 (post-blank (progn (goto-char (match-end 0))
2823 (skip-chars-forward " \t")))
2824 (end (point))
2825 (args (let ((args (org-match-string-no-properties 3)) args2)
2826 (when args
2827 (setq args (org-split-string args ","))
2828 (while args
2829 (while (string-match "\\\\\\'" (car args))
2830 ;; Repair bad splits.
2831 (setcar (cdr args) (concat (substring (car args) 0 -1)
2832 "," (nth 1 args)))
2833 (pop args))
2834 (push (pop args) args2))
2835 (mapcar 'org-trim (nreverse args2))))))
2836 (list 'macro
2837 (list :key key
2838 :value value
2839 :args args
2840 :begin begin
2841 :end end
2842 :post-blank post-blank)))))
2844 (defun org-element-macro-interpreter (macro contents)
2845 "Interpret MACRO object as Org syntax.
2846 CONTENTS is nil."
2847 (org-element-property :value macro))
2849 (defun org-element-macro-successor (limit)
2850 "Search for the next macro object.
2852 LIMIT bounds the search.
2854 Return value is cons cell whose CAR is `macro' and CDR is
2855 beginning position."
2856 (save-excursion
2857 (when (re-search-forward
2858 "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
2859 limit t)
2860 (cons 'macro (match-beginning 0)))))
2863 ;;;; Radio-target
2865 (defun org-element-radio-target-parser ()
2866 "Parse radio target at point.
2868 Return a list whose CAR is `radio-target' and CDR a plist with
2869 `:begin', `:end', `:contents-begin', `:contents-end', `:value'
2870 and `:post-blank' as keywords.
2872 Assume point is at the radio target."
2873 (save-excursion
2874 (looking-at org-radio-target-regexp)
2875 (let ((begin (point))
2876 (contents-begin (match-beginning 1))
2877 (contents-end (match-end 1))
2878 (value (org-match-string-no-properties 1))
2879 (post-blank (progn (goto-char (match-end 0))
2880 (skip-chars-forward " \t")))
2881 (end (point)))
2882 (list 'radio-target
2883 (list :begin begin
2884 :end end
2885 :contents-begin contents-begin
2886 :contents-end contents-end
2887 :post-blank post-blank
2888 :value value)))))
2890 (defun org-element-radio-target-interpreter (target contents)
2891 "Interpret TARGET object as Org syntax.
2892 CONTENTS is the contents of the object."
2893 (concat "<<<" contents ">>>"))
2895 (defun org-element-radio-target-successor (limit)
2896 "Search for the next radio-target object.
2898 LIMIT bounds the search.
2900 Return value is a cons cell whose CAR is `radio-target' and CDR
2901 is beginning position."
2902 (save-excursion
2903 (when (re-search-forward org-radio-target-regexp limit t)
2904 (cons 'radio-target (match-beginning 0)))))
2907 ;;;; Statistics Cookie
2909 (defun org-element-statistics-cookie-parser ()
2910 "Parse statistics cookie at point.
2912 Return a list whose CAR is `statistics-cookie', and CDR a plist
2913 with `:begin', `:end', `:value' and `:post-blank' keywords.
2915 Assume point is at the beginning of the statistics-cookie."
2916 (save-excursion
2917 (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
2918 (let* ((begin (point))
2919 (value (buffer-substring-no-properties
2920 (match-beginning 0) (match-end 0)))
2921 (post-blank (progn (goto-char (match-end 0))
2922 (skip-chars-forward " \t")))
2923 (end (point)))
2924 (list 'statistics-cookie
2925 (list :begin begin
2926 :end end
2927 :value value
2928 :post-blank post-blank)))))
2930 (defun org-element-statistics-cookie-interpreter (statistics-cookie contents)
2931 "Interpret STATISTICS-COOKIE object as Org syntax.
2932 CONTENTS is nil."
2933 (org-element-property :value statistics-cookie))
2935 (defun org-element-statistics-cookie-successor (limit)
2936 "Search for the next statistics cookie object.
2938 LIMIT bounds the search.
2940 Return value is a cons cell whose CAR is `statistics-cookie' and
2941 CDR is beginning position."
2942 (save-excursion
2943 (when (re-search-forward "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]" limit t)
2944 (cons 'statistics-cookie (match-beginning 0)))))
2947 ;;;; Strike-Through
2949 (defun org-element-strike-through-parser ()
2950 "Parse strike-through object at point.
2952 Return a list whose CAR is `strike-through' and CDR is a plist
2953 with `:begin', `:end', `:contents-begin' and `:contents-end' and
2954 `:post-blank' keywords.
2956 Assume point is at the first plus sign marker."
2957 (save-excursion
2958 (unless (bolp) (backward-char 1))
2959 (looking-at org-emph-re)
2960 (let ((begin (match-beginning 2))
2961 (contents-begin (match-beginning 4))
2962 (contents-end (match-end 4))
2963 (post-blank (progn (goto-char (match-end 2))
2964 (skip-chars-forward " \t")))
2965 (end (point)))
2966 (list 'strike-through
2967 (list :begin begin
2968 :end end
2969 :contents-begin contents-begin
2970 :contents-end contents-end
2971 :post-blank post-blank)))))
2973 (defun org-element-strike-through-interpreter (strike-through contents)
2974 "Interpret STRIKE-THROUGH object as Org syntax.
2975 CONTENTS is the contents of the object."
2976 (format "+%s+" contents))
2979 ;;;; Subscript
2981 (defun org-element-subscript-parser ()
2982 "Parse subscript at point.
2984 Return a list whose CAR is `subscript' and CDR a plist with
2985 `:begin', `:end', `:contents-begin', `:contents-end',
2986 `:use-brackets-p' and `:post-blank' as keywords.
2988 Assume point is at the underscore."
2989 (save-excursion
2990 (unless (bolp) (backward-char))
2991 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp)
2993 (not (looking-at org-match-substring-regexp))))
2994 (begin (match-beginning 2))
2995 (contents-begin (or (match-beginning 5)
2996 (match-beginning 3)))
2997 (contents-end (or (match-end 5) (match-end 3)))
2998 (post-blank (progn (goto-char (match-end 0))
2999 (skip-chars-forward " \t")))
3000 (end (point)))
3001 (list 'subscript
3002 (list :begin begin
3003 :end end
3004 :use-brackets-p bracketsp
3005 :contents-begin contents-begin
3006 :contents-end contents-end
3007 :post-blank post-blank)))))
3009 (defun org-element-subscript-interpreter (subscript contents)
3010 "Interpret SUBSCRIPT object as Org syntax.
3011 CONTENTS is the contents of the object."
3012 (format
3013 (if (org-element-property :use-brackets-p subscript) "_{%s}" "_%s")
3014 contents))
3016 (defun org-element-sub/superscript-successor (limit)
3017 "Search for the next sub/superscript object.
3019 LIMIT bounds the search.
3021 Return value is a cons cell whose CAR is either `subscript' or
3022 `superscript' and CDR is beginning position."
3023 (save-excursion
3024 (when (re-search-forward org-match-substring-regexp limit t)
3025 (cons (if (string= (match-string 2) "_") 'subscript 'superscript)
3026 (match-beginning 2)))))
3029 ;;;; Superscript
3031 (defun org-element-superscript-parser ()
3032 "Parse superscript at point.
3034 Return a list whose CAR is `superscript' and CDR a plist with
3035 `:begin', `:end', `:contents-begin', `:contents-end',
3036 `:use-brackets-p' and `:post-blank' as keywords.
3038 Assume point is at the caret."
3039 (save-excursion
3040 (unless (bolp) (backward-char))
3041 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp) t
3042 (not (looking-at org-match-substring-regexp))))
3043 (begin (match-beginning 2))
3044 (contents-begin (or (match-beginning 5)
3045 (match-beginning 3)))
3046 (contents-end (or (match-end 5) (match-end 3)))
3047 (post-blank (progn (goto-char (match-end 0))
3048 (skip-chars-forward " \t")))
3049 (end (point)))
3050 (list 'superscript
3051 (list :begin begin
3052 :end end
3053 :use-brackets-p bracketsp
3054 :contents-begin contents-begin
3055 :contents-end contents-end
3056 :post-blank post-blank)))))
3058 (defun org-element-superscript-interpreter (superscript contents)
3059 "Interpret SUPERSCRIPT object as Org syntax.
3060 CONTENTS is the contents of the object."
3061 (format
3062 (if (org-element-property :use-brackets-p superscript) "^{%s}" "^%s")
3063 contents))
3066 ;;;; Table Cell
3068 (defun org-element-table-cell-parser ()
3069 "Parse table cell at point.
3071 Return a list whose CAR is `table-cell' and CDR is a plist
3072 containing `:begin', `:end', `:contents-begin', `:contents-end'
3073 and `:post-blank' keywords."
3074 (looking-at "[ \t]*\\(.*?\\)[ \t]*|")
3075 (let* ((begin (match-beginning 0))
3076 (end (match-end 0))
3077 (contents-begin (match-beginning 1))
3078 (contents-end (match-end 1)))
3079 (list 'table-cell
3080 (list :begin begin
3081 :end end
3082 :contents-begin contents-begin
3083 :contents-end contents-end
3084 :post-blank 0))))
3086 (defun org-element-table-cell-interpreter (table-cell contents)
3087 "Interpret TABLE-CELL element as Org syntax.
3088 CONTENTS is the contents of the cell, or nil."
3089 (concat " " contents " |"))
3091 (defun org-element-table-cell-successor (limit)
3092 "Search for the next table-cell object.
3094 LIMIT bounds the search.
3096 Return value is a cons cell whose CAR is `table-cell' and CDR is
3097 beginning position."
3098 (when (looking-at "[ \t]*.*?[ \t]+|") (cons 'table-cell (point))))
3101 ;;;; Target
3103 (defun org-element-target-parser ()
3104 "Parse target at point.
3106 Return a list whose CAR is `target' and CDR a plist with
3107 `:begin', `:end', `:value' and `:post-blank' as keywords.
3109 Assume point is at the target."
3110 (save-excursion
3111 (looking-at org-target-regexp)
3112 (let ((begin (point))
3113 (value (org-match-string-no-properties 1))
3114 (post-blank (progn (goto-char (match-end 0))
3115 (skip-chars-forward " \t")))
3116 (end (point)))
3117 (list 'target
3118 (list :begin begin
3119 :end end
3120 :value value
3121 :post-blank post-blank)))))
3123 (defun org-element-target-interpreter (target contents)
3124 "Interpret TARGET object as Org syntax.
3125 CONTENTS is nil."
3126 (format "<<%s>>" (org-element-property :value target)))
3128 (defun org-element-target-successor (limit)
3129 "Search for the next target object.
3131 LIMIT bounds the search.
3133 Return value is a cons cell whose CAR is `target' and CDR is
3134 beginning position."
3135 (save-excursion
3136 (when (re-search-forward org-target-regexp limit t)
3137 (cons 'target (match-beginning 0)))))
3140 ;;;; Timestamp
3142 (defun org-element-timestamp-parser ()
3143 "Parse time stamp at point.
3145 Return a list whose CAR is `timestamp', and CDR a plist with
3146 `:type', `:begin', `:end', `:value' and `:post-blank' keywords.
3148 Assume point is at the beginning of the timestamp."
3149 (save-excursion
3150 (let* ((begin (point))
3151 (activep (eq (char-after) ?<))
3152 (main-value
3153 (progn
3154 (looking-at "[<[]\\(\\(%%\\)?.*?\\)[]>]\\(?:--[<[]\\(.*?\\)[]>]\\)?")
3155 (match-string-no-properties 1)))
3156 (range-end (match-string-no-properties 3))
3157 (type (cond ((match-string 2) 'diary)
3158 ((and activep range-end) 'active-range)
3159 (activep 'active)
3160 (range-end 'inactive-range)
3161 (t 'inactive)))
3162 (post-blank (progn (goto-char (match-end 0))
3163 (skip-chars-forward " \t")))
3164 (end (point)))
3165 (list 'timestamp
3166 (list :type type
3167 :value main-value
3168 :range-end range-end
3169 :begin begin
3170 :end end
3171 :post-blank post-blank)))))
3173 (defun org-element-timestamp-interpreter (timestamp contents)
3174 "Interpret TIMESTAMP object as Org syntax.
3175 CONTENTS is nil."
3176 (let ((type (org-element-property :type timestamp) ))
3177 (concat
3178 (format (if (memq type '(inactive inactive-range)) "[%s]" "<%s>")
3179 (org-element-property :value timestamp))
3180 (let ((range-end (org-element-property :range-end timestamp)))
3181 (when range-end
3182 (concat "--"
3183 (format (if (eq type 'inactive-range) "[%s]" "<%s>")
3184 range-end)))))))
3186 (defun org-element-timestamp-successor (limit)
3187 "Search for the next timestamp object.
3189 LIMIT bounds the search.
3191 Return value is a cons cell whose CAR is `timestamp' and CDR is
3192 beginning position."
3193 (save-excursion
3194 (when (re-search-forward
3195 (concat org-ts-regexp-both
3196 "\\|"
3197 "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
3198 "\\|"
3199 "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
3200 limit t)
3201 (cons 'timestamp (match-beginning 0)))))
3204 ;;;; Underline
3206 (defun org-element-underline-parser ()
3207 "Parse underline object at point.
3209 Return a list whose CAR is `underline' and CDR is a plist with
3210 `:begin', `:end', `:contents-begin' and `:contents-end' and
3211 `:post-blank' keywords.
3213 Assume point is at the first underscore marker."
3214 (save-excursion
3215 (unless (bolp) (backward-char 1))
3216 (looking-at org-emph-re)
3217 (let ((begin (match-beginning 2))
3218 (contents-begin (match-beginning 4))
3219 (contents-end (match-end 4))
3220 (post-blank (progn (goto-char (match-end 2))
3221 (skip-chars-forward " \t")))
3222 (end (point)))
3223 (list 'underline
3224 (list :begin begin
3225 :end end
3226 :contents-begin contents-begin
3227 :contents-end contents-end
3228 :post-blank post-blank)))))
3230 (defun org-element-underline-interpreter (underline contents)
3231 "Interpret UNDERLINE object as Org syntax.
3232 CONTENTS is the contents of the object."
3233 (format "_%s_" contents))
3236 ;;;; Verbatim
3238 (defun org-element-verbatim-parser ()
3239 "Parse verbatim object at point.
3241 Return a list whose CAR is `verbatim' and CDR is a plist with
3242 `:value', `:begin', `:end' and `:post-blank' keywords.
3244 Assume point is at the first equal sign marker."
3245 (save-excursion
3246 (unless (bolp) (backward-char 1))
3247 (looking-at org-emph-re)
3248 (let ((begin (match-beginning 2))
3249 (value (org-match-string-no-properties 4))
3250 (post-blank (progn (goto-char (match-end 2))
3251 (skip-chars-forward " \t")))
3252 (end (point)))
3253 (list 'verbatim
3254 (list :value value
3255 :begin begin
3256 :end end
3257 :post-blank post-blank)))))
3259 (defun org-element-verbatim-interpreter (verbatim contents)
3260 "Interpret VERBATIM object as Org syntax.
3261 CONTENTS is nil."
3262 (format "=%s=" (org-element-property :value verbatim)))
3266 ;;; Parsing Element Starting At Point
3268 ;; `org-element--current-element' is the core function of this section.
3269 ;; It returns the Lisp representation of the element starting at
3270 ;; point.
3272 ;; `org-element--current-element' makes use of special modes. They
3273 ;; are activated for fixed element chaining (i.e. `plain-list' >
3274 ;; `item') or fixed conditional element chaining (i.e. `headline' >
3275 ;; `section'). Special modes are: `first-section', `section',
3276 ;; `quote-section', `item' and `table-row'.
3278 (defun org-element--current-element
3279 (limit &optional granularity special structure)
3280 "Parse the element starting at point.
3282 LIMIT bounds the search.
3284 Return value is a list like (TYPE PROPS) where TYPE is the type
3285 of the element and PROPS a plist of properties associated to the
3286 element.
3288 Possible types are defined in `org-element-all-elements'.
3290 Optional argument GRANULARITY determines the depth of the
3291 recursion. Allowed values are `headline', `greater-element',
3292 `element', `object' or nil. When it is broader than `object' (or
3293 nil), secondary values will not be parsed, since they only
3294 contain objects.
3296 Optional argument SPECIAL, when non-nil, can be either
3297 `first-section', `section', `quote-section', `table-row' and
3298 `item'.
3300 If STRUCTURE isn't provided but SPECIAL is set to `item', it will
3301 be computed.
3303 This function assumes point is always at the beginning of the
3304 element it has to parse."
3305 (save-excursion
3306 ;; If point is at an affiliated keyword, try moving to the
3307 ;; beginning of the associated element. If none is found, the
3308 ;; keyword is orphaned and will be treated as plain text.
3309 (when (looking-at org-element--affiliated-re)
3310 (let ((opoint (point)))
3311 (while (looking-at org-element--affiliated-re) (forward-line))
3312 (when (looking-at "[ \t]*$") (goto-char opoint))))
3313 (let ((case-fold-search t)
3314 ;; Determine if parsing depth allows for secondary strings
3315 ;; parsing. It only applies to elements referenced in
3316 ;; `org-element-secondary-value-alist'.
3317 (raw-secondary-p (and granularity (not (eq granularity 'object)))))
3318 (cond
3319 ;; Item.
3320 ((eq special 'item)
3321 (org-element-item-parser limit structure raw-secondary-p))
3322 ;; Quote Section.
3323 ((eq special 'quote-section) (org-element-quote-section-parser limit))
3324 ;; Table Row.
3325 ((eq special 'table-row) (org-element-table-row-parser limit))
3326 ;; Headline.
3327 ((org-with-limited-levels (org-at-heading-p))
3328 (org-element-headline-parser limit raw-secondary-p))
3329 ;; Section (must be checked after headline).
3330 ((eq special 'section) (org-element-section-parser limit))
3331 ((eq special 'first-section)
3332 (org-element-section-parser
3333 (or (save-excursion (org-with-limited-levels (outline-next-heading)))
3334 limit)))
3335 ;; Planning and Clock.
3336 ((and (looking-at org-planning-or-clock-line-re))
3337 (if (equal (match-string 1) org-clock-string)
3338 (org-element-clock-parser limit)
3339 (org-element-planning-parser limit)))
3340 ;; Inlinetask.
3341 ((org-at-heading-p)
3342 (org-element-inlinetask-parser limit raw-secondary-p))
3343 ;; LaTeX Environment.
3344 ((looking-at "[ \t]*\\\\begin{\\([A-Za-z0-9*]+\\)}")
3345 (if (save-excursion
3346 (re-search-forward
3347 (format "[ \t]*\\\\end{%s}[ \t]*"
3348 (regexp-quote (match-string 1)))
3349 nil t))
3350 (org-element-latex-environment-parser limit)
3351 (org-element-paragraph-parser limit)))
3352 ;; Drawer and Property Drawer.
3353 ((looking-at org-drawer-regexp)
3354 (let ((name (match-string 1)))
3355 (cond
3356 ((not (save-excursion
3357 (re-search-forward "^[ \t]*:END:[ \t]*$" nil t)))
3358 (org-element-paragraph-parser limit))
3359 ((equal "PROPERTIES" name)
3360 (org-element-property-drawer-parser limit))
3361 (t (org-element-drawer-parser limit)))))
3362 ;; Fixed Width
3363 ((looking-at "[ \t]*:\\( \\|$\\)")
3364 (org-element-fixed-width-parser limit))
3365 ;; Inline Comments, Blocks, Babel Calls, Dynamic Blocks and
3366 ;; Keywords.
3367 ((looking-at "[ \t]*#")
3368 (goto-char (match-end 0))
3369 (cond ((looking-at "\\(?: \\|$\\)")
3370 (beginning-of-line)
3371 (org-element-comment-parser limit))
3372 ((looking-at "\\+BEGIN_\\(\\S-+\\)")
3373 (beginning-of-line)
3374 (let ((parser (assoc (upcase (match-string 1))
3375 org-element-block-name-alist)))
3376 (if parser (funcall (cdr parser) limit)
3377 (org-element-special-block-parser limit))))
3378 ((looking-at "\\+CALL:")
3379 (beginning-of-line)
3380 (org-element-babel-call-parser limit))
3381 ((looking-at "\\+BEGIN:? ")
3382 (beginning-of-line)
3383 (org-element-dynamic-block-parser limit))
3384 ((looking-at "\\+\\S-+:")
3385 (beginning-of-line)
3386 (org-element-keyword-parser limit))
3388 (beginning-of-line)
3389 (org-element-paragraph-parser limit))))
3390 ;; Footnote Definition.
3391 ((looking-at org-footnote-definition-re)
3392 (org-element-footnote-definition-parser limit))
3393 ;; Horizontal Rule.
3394 ((looking-at "[ \t]*-\\{5,\\}[ \t]*$")
3395 (org-element-horizontal-rule-parser limit))
3396 ;; Table.
3397 ((org-at-table-p t) (org-element-table-parser limit))
3398 ;; List.
3399 ((looking-at (org-item-re))
3400 (org-element-plain-list-parser limit (or structure (org-list-struct))))
3401 ;; Default element: Paragraph.
3402 (t (org-element-paragraph-parser limit))))))
3405 ;; Most elements can have affiliated keywords. When looking for an
3406 ;; element beginning, we want to move before them, as they belong to
3407 ;; that element, and, in the meantime, collect information they give
3408 ;; into appropriate properties. Hence the following function.
3410 ;; Usage of optional arguments may not be obvious at first glance:
3412 ;; - TRANS-LIST is used to polish keywords names that have evolved
3413 ;; during Org history. In example, even though =result= and
3414 ;; =results= coexist, we want to have them under the same =result=
3415 ;; property. It's also true for "srcname" and "name", where the
3416 ;; latter seems to be preferred nowadays (thus the "name" property).
3418 ;; - CONSED allows to regroup multi-lines keywords under the same
3419 ;; property, while preserving their own identity. This is mostly
3420 ;; used for "attr_latex" and al.
3422 ;; - PARSED prepares a keyword value for export. This is useful for
3423 ;; "caption". Objects restrictions for such keywords are defined in
3424 ;; `org-element-object-restrictions'.
3426 ;; - DUALS is used to take care of keywords accepting a main and an
3427 ;; optional secondary values. For example "results" has its
3428 ;; source's name as the main value, and may have an hash string in
3429 ;; optional square brackets as the secondary one.
3431 ;; A keyword may belong to more than one category.
3433 (defun org-element--collect-affiliated-keywords
3434 (&optional key-re trans-list consed parsed duals)
3435 "Collect affiliated keywords before point.
3437 Optional argument KEY-RE is a regexp matching keywords, which
3438 puts matched keyword in group 1. It defaults to
3439 `org-element--affiliated-re'.
3441 TRANS-LIST is an alist where key is the keyword and value the
3442 property name it should be translated to, without the colons. It
3443 defaults to `org-element-keyword-translation-alist'.
3445 CONSED is a list of strings. Any keyword belonging to that list
3446 will have its value consed. The check is done after keyword
3447 translation. It defaults to `org-element-multiple-keywords'.
3449 PARSED is a list of strings. Any keyword member of this list
3450 will have its value parsed. The check is done after keyword
3451 translation. If a keyword is a member of both CONSED and PARSED,
3452 it's value will be a list of parsed strings. It defaults to
3453 `org-element-parsed-keywords'.
3455 DUALS is a list of strings. Any keyword member of this list can
3456 have two parts: one mandatory and one optional. Its value is
3457 a cons cell whose CAR is the former, and the CDR the latter. If
3458 a keyword is a member of both PARSED and DUALS, both values will
3459 be parsed. It defaults to `org-element-dual-keywords'.
3461 Return a list whose CAR is the position at the first of them and
3462 CDR a plist of keywords and values."
3463 (save-excursion
3464 (let ((case-fold-search t)
3465 (key-re (or key-re org-element--affiliated-re))
3466 (trans-list (or trans-list org-element-keyword-translation-alist))
3467 (consed (or consed org-element-multiple-keywords))
3468 (parsed (or parsed org-element-parsed-keywords))
3469 (duals (or duals org-element-dual-keywords))
3470 ;; RESTRICT is the list of objects allowed in parsed
3471 ;; keywords value.
3472 (restrict (org-element-restriction 'keyword))
3473 output)
3474 (unless (bobp)
3475 (while (and (not (bobp)) (progn (forward-line -1) (looking-at key-re)))
3476 (let* ((raw-kwd (upcase (or (match-string 2) (match-string 1))))
3477 ;; Apply translation to RAW-KWD. From there, KWD is
3478 ;; the official keyword.
3479 (kwd (or (cdr (assoc raw-kwd trans-list)) raw-kwd))
3480 ;; Find main value for any keyword.
3481 (value
3482 (save-match-data
3483 (org-trim
3484 (buffer-substring-no-properties
3485 (match-end 0) (point-at-eol)))))
3486 ;; If KWD is a dual keyword, find its secondary
3487 ;; value. Maybe parse it.
3488 (dual-value
3489 (and (member kwd duals)
3490 (let ((sec (org-match-string-no-properties 3)))
3491 (if (or (not sec) (not (member kwd parsed))) sec
3492 (org-element-parse-secondary-string sec restrict)))))
3493 ;; Attribute a property name to KWD.
3494 (kwd-sym (and kwd (intern (concat ":" (downcase kwd))))))
3495 ;; Now set final shape for VALUE.
3496 (when (member kwd parsed)
3497 (setq value (org-element-parse-secondary-string value restrict)))
3498 (when (member kwd duals)
3499 ;; VALUE is mandatory. Set it to nil if there is none.
3500 (setq value (and value (cons value dual-value))))
3501 ;; Attributes are always consed.
3502 (when (or (member kwd consed) (string-match "^ATTR_" kwd))
3503 (setq value (cons value (plist-get output kwd-sym))))
3504 ;; Eventually store the new value in OUTPUT.
3505 (setq output (plist-put output kwd-sym value))))
3506 (unless (looking-at key-re) (forward-line 1)))
3507 (list (point) output))))
3511 ;;; The Org Parser
3513 ;; The two major functions here are `org-element-parse-buffer', which
3514 ;; parses Org syntax inside the current buffer, taking into account
3515 ;; region, narrowing, or even visibility if specified, and
3516 ;; `org-element-parse-secondary-string', which parses objects within
3517 ;; a given string.
3519 ;; The (almost) almighty `org-element-map' allows to apply a function
3520 ;; on elements or objects matching some type, and accumulate the
3521 ;; resulting values. In an export situation, it also skips unneeded
3522 ;; parts of the parse tree.
3524 (defun org-element-parse-buffer (&optional granularity visible-only)
3525 "Recursively parse the buffer and return structure.
3526 If narrowing is in effect, only parse the visible part of the
3527 buffer.
3529 Optional argument GRANULARITY determines the depth of the
3530 recursion. It can be set to the following symbols:
3532 `headline' Only parse headlines.
3533 `greater-element' Don't recurse into greater elements excepted
3534 headlines and sections. Thus, elements
3535 parsed are the top-level ones.
3536 `element' Parse everything but objects and plain text.
3537 `object' Parse the complete buffer (default).
3539 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3540 elements.
3542 Assume buffer is in Org mode."
3543 (save-excursion
3544 (goto-char (point-min))
3545 (org-skip-whitespace)
3546 (org-element--parse-elements
3547 (point-at-bol) (point-max)
3548 ;; Start in `first-section' mode so text before the first
3549 ;; headline belongs to a section.
3550 'first-section nil granularity visible-only (list 'org-data nil))))
3552 (defun org-element-parse-secondary-string (string restriction &optional parent)
3553 "Recursively parse objects in STRING and return structure.
3555 RESTRICTION is a symbol limiting the object types that will be
3556 looked after.
3558 Optional argument PARENT, when non-nil, is the element or object
3559 containing the secondary string. It is used to set correctly
3560 `:parent' property within the string."
3561 (with-temp-buffer
3562 (insert string)
3563 (let ((secondary (org-element--parse-objects
3564 (point-min) (point-max) nil restriction)))
3565 (mapc (lambda (obj) (org-element-put-property obj :parent parent))
3566 secondary))))
3568 (defun org-element-map (data types fun &optional info first-match no-recursion)
3569 "Map a function on selected elements or objects.
3571 DATA is the parsed tree, as returned by, i.e,
3572 `org-element-parse-buffer'. TYPES is a symbol or list of symbols
3573 of elements or objects types. FUN is the function called on the
3574 matching element or object. It must accept one arguments: the
3575 element or object itself.
3577 When optional argument INFO is non-nil, it should be a plist
3578 holding export options. In that case, parts of the parse tree
3579 not exportable according to that property list will be skipped.
3581 When optional argument FIRST-MATCH is non-nil, stop at the first
3582 match for which FUN doesn't return nil, and return that value.
3584 Optional argument NO-RECURSION is a symbol or a list of symbols
3585 representing elements or objects types. `org-element-map' won't
3586 enter any recursive element or object whose type belongs to that
3587 list. Though, FUN can still be applied on them.
3589 Nil values returned from FUN do not appear in the results."
3590 ;; Ensure TYPES and NO-RECURSION are a list, even of one element.
3591 (unless (listp types) (setq types (list types)))
3592 (unless (listp no-recursion) (setq no-recursion (list no-recursion)))
3593 ;; Recursion depth is determined by --CATEGORY.
3594 (let* ((--category
3595 (catch 'found
3596 (let ((category 'greater-elements))
3597 (mapc (lambda (type)
3598 (cond ((or (memq type org-element-all-objects)
3599 (eq type 'plain-text))
3600 ;; If one object is found, the function
3601 ;; has to recurse into every object.
3602 (throw 'found 'objects))
3603 ((not (memq type org-element-greater-elements))
3604 ;; If one regular element is found, the
3605 ;; function has to recurse, at least,
3606 ;; into every element it encounters.
3607 (and (not (eq category 'elements))
3608 (setq category 'elements)))))
3609 types)
3610 category)))
3611 --acc
3612 --walk-tree
3613 (--walk-tree
3614 (function
3615 (lambda (--data)
3616 ;; Recursively walk DATA. INFO, if non-nil, is a plist
3617 ;; holding contextual information.
3618 (let ((--type (org-element-type --data)))
3619 (cond
3620 ((not --data))
3621 ;; Ignored element in an export context.
3622 ((and info (memq --data (plist-get info :ignore-list))))
3623 ;; Secondary string: only objects can be found there.
3624 ((not --type)
3625 (when (eq --category 'objects) (mapc --walk-tree --data)))
3626 ;; Unconditionally enter parse trees.
3627 ((eq --type 'org-data)
3628 (mapc --walk-tree (org-element-contents --data)))
3630 ;; Check if TYPE is matching among TYPES. If so,
3631 ;; apply FUN to --DATA and accumulate return value
3632 ;; into --ACC (or exit if FIRST-MATCH is non-nil).
3633 (when (memq --type types)
3634 (let ((result (funcall fun --data)))
3635 (cond ((not result))
3636 (first-match (throw '--map-first-match result))
3637 (t (push result --acc)))))
3638 ;; If --DATA has a secondary string that can contain
3639 ;; objects with their type among TYPES, look into it.
3640 (when (eq --category 'objects)
3641 (let ((sec-prop
3642 (assq --type org-element-secondary-value-alist)))
3643 (when sec-prop
3644 (funcall --walk-tree
3645 (org-element-property (cdr sec-prop) --data)))))
3646 ;; Determine if a recursion into --DATA is possible.
3647 (cond
3648 ;; --TYPE is explicitly removed from recursion.
3649 ((memq --type no-recursion))
3650 ;; --DATA has no contents.
3651 ((not (org-element-contents --data)))
3652 ;; Looking for greater elements but --DATA is simply
3653 ;; an element or an object.
3654 ((and (eq --category 'greater-elements)
3655 (not (memq --type org-element-greater-elements))))
3656 ;; Looking for elements but --DATA is an object.
3657 ((and (eq --category 'elements)
3658 (memq --type org-element-all-objects)))
3659 ;; In any other case, map contents.
3660 (t (mapc --walk-tree (org-element-contents --data)))))))))))
3661 (catch '--map-first-match
3662 (funcall --walk-tree data)
3663 ;; Return value in a proper order.
3664 (nreverse --acc))))
3666 ;; The following functions are internal parts of the parser.
3668 ;; The first one, `org-element--parse-elements' acts at the element's
3669 ;; level.
3671 ;; The second one, `org-element--parse-objects' applies on all objects
3672 ;; of a paragraph or a secondary string. It uses
3673 ;; `org-element--get-next-object-candidates' to optimize the search of
3674 ;; the next object in the buffer.
3676 ;; More precisely, that function looks for every allowed object type
3677 ;; first. Then, it discards failed searches, keeps further matches,
3678 ;; and searches again types matched behind point, for subsequent
3679 ;; calls. Thus, searching for a given type fails only once, and every
3680 ;; object is searched only once at top level (but sometimes more for
3681 ;; nested types).
3683 (defun org-element--parse-elements
3684 (beg end special structure granularity visible-only acc)
3685 "Parse elements between BEG and END positions.
3687 SPECIAL prioritize some elements over the others. It can be set
3688 to `first-section', `quote-section', `section' `item' or
3689 `table-row'.
3691 When value is `item', STRUCTURE will be used as the current list
3692 structure.
3694 GRANULARITY determines the depth of the recursion. See
3695 `org-element-parse-buffer' for more information.
3697 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3698 elements.
3700 Elements are accumulated into ACC."
3701 (save-excursion
3702 (goto-char beg)
3703 ;; When parsing only headlines, skip any text before first one.
3704 (when (and (eq granularity 'headline) (not (org-at-heading-p)))
3705 (org-with-limited-levels (outline-next-heading)))
3706 ;; Main loop start.
3707 (while (< (point) end)
3708 ;; Find current element's type and parse it accordingly to
3709 ;; its category.
3710 (let* ((element (org-element--current-element
3711 end granularity special structure))
3712 (type (org-element-type element))
3713 (cbeg (org-element-property :contents-begin element)))
3714 (goto-char (org-element-property :end element))
3715 ;; Fill ELEMENT contents by side-effect.
3716 (cond
3717 ;; If VISIBLE-ONLY is true and element is hidden or if it has
3718 ;; no contents, don't modify it.
3719 ((or (and visible-only (org-element-property :hiddenp element))
3720 (not cbeg)))
3721 ;; Greater element: parse it between `contents-begin' and
3722 ;; `contents-end'. Make sure GRANULARITY allows the
3723 ;; recursion, or ELEMENT is an headline, in which case going
3724 ;; inside is mandatory, in order to get sub-level headings.
3725 ((and (memq type org-element-greater-elements)
3726 (or (memq granularity '(element object nil))
3727 (and (eq granularity 'greater-element)
3728 (eq type 'section))
3729 (eq type 'headline)))
3730 (org-element--parse-elements
3731 cbeg (org-element-property :contents-end element)
3732 ;; Possibly switch to a special mode.
3733 (case type
3734 (headline
3735 (if (org-element-property :quotedp element) 'quote-section
3736 'section))
3737 (plain-list 'item)
3738 (table 'table-row))
3739 (org-element-property :structure element)
3740 granularity visible-only element))
3741 ;; ELEMENT has contents. Parse objects inside, if
3742 ;; GRANULARITY allows it.
3743 ((memq granularity '(object nil))
3744 (org-element--parse-objects
3745 cbeg (org-element-property :contents-end element) element
3746 (org-element-restriction type))))
3747 (org-element-adopt-elements acc element)))
3748 ;; Return result.
3749 acc))
3751 (defun org-element--parse-objects (beg end acc restriction)
3752 "Parse objects between BEG and END and return recursive structure.
3754 Objects are accumulated in ACC.
3756 RESTRICTION is a list of object types which are allowed in the
3757 current object."
3758 (let (candidates)
3759 (save-excursion
3760 (goto-char beg)
3761 (while (and (< (point) end)
3762 (setq candidates (org-element--get-next-object-candidates
3763 end restriction candidates)))
3764 (let ((next-object
3765 (let ((pos (apply 'min (mapcar 'cdr candidates))))
3766 (save-excursion
3767 (goto-char pos)
3768 (funcall (intern (format "org-element-%s-parser"
3769 (car (rassq pos candidates)))))))))
3770 ;; 1. Text before any object. Untabify it.
3771 (let ((obj-beg (org-element-property :begin next-object)))
3772 (unless (= (point) obj-beg)
3773 (setq acc
3774 (org-element-adopt-elements
3776 (replace-regexp-in-string
3777 "\t" (make-string tab-width ? )
3778 (buffer-substring-no-properties (point) obj-beg))))))
3779 ;; 2. Object...
3780 (let ((obj-end (org-element-property :end next-object))
3781 (cont-beg (org-element-property :contents-begin next-object)))
3782 ;; Fill contents of NEXT-OBJECT by side-effect, if it has
3783 ;; a recursive type.
3784 (when (and cont-beg
3785 (memq (car next-object) org-element-recursive-objects))
3786 (save-restriction
3787 (narrow-to-region
3788 cont-beg
3789 (org-element-property :contents-end next-object))
3790 (org-element--parse-objects
3791 (point-min) (point-max) next-object
3792 (org-element-restriction next-object))))
3793 (setq acc (org-element-adopt-elements acc next-object))
3794 (goto-char obj-end))))
3795 ;; 3. Text after last object. Untabify it.
3796 (unless (= (point) end)
3797 (setq acc
3798 (org-element-adopt-elements
3800 (replace-regexp-in-string
3801 "\t" (make-string tab-width ? )
3802 (buffer-substring-no-properties (point) end)))))
3803 ;; Result.
3804 acc)))
3806 (defun org-element--get-next-object-candidates (limit restriction objects)
3807 "Return an alist of candidates for the next object.
3809 LIMIT bounds the search, and RESTRICTION narrows candidates to
3810 some object types.
3812 Return value is an alist whose CAR is position and CDR the object
3813 type, as a symbol.
3815 OBJECTS is the previous candidates alist."
3816 (let (next-candidates types-to-search)
3817 ;; If no previous result, search every object type in RESTRICTION.
3818 ;; Otherwise, keep potential candidates (old objects located after
3819 ;; point) and ask to search again those which had matched before.
3820 (if (not objects) (setq types-to-search restriction)
3821 (mapc (lambda (obj)
3822 (if (< (cdr obj) (point)) (push (car obj) types-to-search)
3823 (push obj next-candidates)))
3824 objects))
3825 ;; Call the appropriate successor function for each type to search
3826 ;; and accumulate matches.
3827 (mapc
3828 (lambda (type)
3829 (let* ((successor-fun
3830 (intern
3831 (format "org-element-%s-successor"
3832 (or (cdr (assq type org-element-object-successor-alist))
3833 type))))
3834 (obj (funcall successor-fun limit)))
3835 (and obj (push obj next-candidates))))
3836 types-to-search)
3837 ;; Return alist.
3838 next-candidates))
3842 ;;; Towards A Bijective Process
3844 ;; The parse tree obtained with `org-element-parse-buffer' is really
3845 ;; a snapshot of the corresponding Org buffer. Therefore, it can be
3846 ;; interpreted and expanded into a string with canonical Org syntax.
3847 ;; Hence `org-element-interpret-data'.
3849 ;; The function relies internally on
3850 ;; `org-element--interpret-affiliated-keywords'.
3852 ;;;###autoload
3853 (defun org-element-interpret-data (data &optional parent)
3854 "Interpret DATA as Org syntax.
3856 DATA is a parse tree, an element, an object or a secondary string
3857 to interpret.
3859 Optional argument PARENT is used for recursive calls. It contains
3860 the element or object containing data, or nil.
3862 Return Org syntax as a string."
3863 (let* ((type (org-element-type data))
3864 (results
3865 (cond
3866 ;; Secondary string.
3867 ((not type)
3868 (mapconcat
3869 (lambda (obj) (org-element-interpret-data obj parent))
3870 data ""))
3871 ;; Full Org document.
3872 ((eq type 'org-data)
3873 (mapconcat
3874 (lambda (obj) (org-element-interpret-data obj parent))
3875 (org-element-contents data) ""))
3876 ;; Plain text.
3877 ((stringp data) data)
3878 ;; Element/Object without contents.
3879 ((not (org-element-contents data))
3880 (funcall (intern (format "org-element-%s-interpreter" type))
3881 data nil))
3882 ;; Element/Object with contents.
3884 (let* ((greaterp (memq type org-element-greater-elements))
3885 (objectp (and (not greaterp)
3886 (memq type org-element-recursive-objects)))
3887 (contents
3888 (mapconcat
3889 (lambda (obj) (org-element-interpret-data obj data))
3890 (org-element-contents
3891 (if (or greaterp objectp) data
3892 ;; Elements directly containing objects must
3893 ;; have their indentation normalized first.
3894 (org-element-normalize-contents
3895 data
3896 ;; When normalizing first paragraph of an
3897 ;; item or a footnote-definition, ignore
3898 ;; first line's indentation.
3899 (and (eq type 'paragraph)
3900 (equal data (car (org-element-contents parent)))
3901 (memq (org-element-type parent)
3902 '(footnote-definiton item))))))
3903 "")))
3904 (funcall (intern (format "org-element-%s-interpreter" type))
3905 data
3906 (if greaterp (org-element-normalize-contents contents)
3907 contents)))))))
3908 (if (memq type '(org-data plain-text nil)) results
3909 ;; Build white spaces. If no `:post-blank' property is
3910 ;; specified, assume its value is 0.
3911 (let ((post-blank (or (org-element-property :post-blank data) 0)))
3912 (if (memq type org-element-all-objects)
3913 (concat results (make-string post-blank 32))
3914 (concat
3915 (org-element--interpret-affiliated-keywords data)
3916 (org-element-normalize-string results)
3917 (make-string post-blank 10)))))))
3919 (defun org-element--interpret-affiliated-keywords (element)
3920 "Return ELEMENT's affiliated keywords as Org syntax.
3921 If there is no affiliated keyword, return the empty string."
3922 (let ((keyword-to-org
3923 (function
3924 (lambda (key value)
3925 (let (dual)
3926 (when (member key org-element-dual-keywords)
3927 (setq dual (cdr value) value (car value)))
3928 (concat "#+" key
3929 (and dual
3930 (format "[%s]" (org-element-interpret-data dual)))
3931 ": "
3932 (if (member key org-element-parsed-keywords)
3933 (org-element-interpret-data value)
3934 value)
3935 "\n"))))))
3936 (mapconcat
3937 (lambda (prop)
3938 (let ((value (org-element-property prop element))
3939 (keyword (upcase (substring (symbol-name prop) 1))))
3940 (when value
3941 (if (or (member keyword org-element-multiple-keywords)
3942 ;; All attribute keywords can have multiple lines.
3943 (string-match "^ATTR_" keyword))
3944 (mapconcat (lambda (line) (funcall keyword-to-org keyword line))
3945 value
3947 (funcall keyword-to-org keyword value)))))
3948 ;; List all ELEMENT's properties matching an attribute line or an
3949 ;; affiliated keyword, but ignore translated keywords since they
3950 ;; cannot belong to the property list.
3951 (loop for prop in (nth 1 element) by 'cddr
3952 when (let ((keyword (upcase (substring (symbol-name prop) 1))))
3953 (or (string-match "^ATTR_" keyword)
3954 (and
3955 (member keyword org-element-affiliated-keywords)
3956 (not (assoc keyword
3957 org-element-keyword-translation-alist)))))
3958 collect prop)
3959 "")))
3961 ;; Because interpretation of the parse tree must return the same
3962 ;; number of blank lines between elements and the same number of white
3963 ;; space after objects, some special care must be given to white
3964 ;; spaces.
3966 ;; The first function, `org-element-normalize-string', ensures any
3967 ;; string different from the empty string will end with a single
3968 ;; newline character.
3970 ;; The second function, `org-element-normalize-contents', removes
3971 ;; global indentation from the contents of the current element.
3973 (defun org-element-normalize-string (s)
3974 "Ensure string S ends with a single newline character.
3976 If S isn't a string return it unchanged. If S is the empty
3977 string, return it. Otherwise, return a new string with a single
3978 newline character at its end."
3979 (cond
3980 ((not (stringp s)) s)
3981 ((string= "" s) "")
3982 (t (and (string-match "\\(\n[ \t]*\\)*\\'" s)
3983 (replace-match "\n" nil nil s)))))
3985 (defun org-element-normalize-contents (element &optional ignore-first)
3986 "Normalize plain text in ELEMENT's contents.
3988 ELEMENT must only contain plain text and objects.
3990 If optional argument IGNORE-FIRST is non-nil, ignore first line's
3991 indentation to compute maximal common indentation.
3993 Return the normalized element that is element with global
3994 indentation removed from its contents. The function assumes that
3995 indentation is not done with TAB characters."
3996 (let* (ind-list ; for byte-compiler
3997 collect-inds ; for byte-compiler
3998 (collect-inds
3999 (function
4000 ;; Return list of indentations within BLOB. This is done by
4001 ;; walking recursively BLOB and updating IND-LIST along the
4002 ;; way. FIRST-FLAG is non-nil when the first string hasn't
4003 ;; been seen yet. It is required as this string is the only
4004 ;; one whose indentation doesn't happen after a newline
4005 ;; character.
4006 (lambda (blob first-flag)
4007 (mapc
4008 (lambda (object)
4009 (when (and first-flag (stringp object))
4010 (setq first-flag nil)
4011 (string-match "\\`\\( *\\)" object)
4012 (let ((len (length (match-string 1 object))))
4013 ;; An indentation of zero means no string will be
4014 ;; modified. Quit the process.
4015 (if (zerop len) (throw 'zero (setq ind-list nil))
4016 (push len ind-list))))
4017 (cond
4018 ((stringp object)
4019 (let ((start 0))
4020 ;; Avoid matching blank or empty lines.
4021 (while (and (string-match "\n\\( *\\)\\(.\\)" object start)
4022 (not (equal (match-string 2 object) " ")))
4023 (setq start (match-end 0))
4024 (push (length (match-string 1 object)) ind-list))))
4025 ((memq (org-element-type object) org-element-recursive-objects)
4026 (funcall collect-inds object first-flag))))
4027 (org-element-contents blob))))))
4028 ;; Collect indentation list in ELEMENT. Possibly remove first
4029 ;; value if IGNORE-FIRST is non-nil.
4030 (catch 'zero (funcall collect-inds element (not ignore-first)))
4031 (if (not ind-list) element
4032 ;; Build ELEMENT back, replacing each string with the same
4033 ;; string minus common indentation.
4034 (let* (build ; For byte compiler.
4035 (build
4036 (function
4037 (lambda (blob mci first-flag)
4038 ;; Return BLOB with all its strings indentation
4039 ;; shortened from MCI white spaces. FIRST-FLAG is
4040 ;; non-nil when the first string hasn't been seen
4041 ;; yet.
4042 (setcdr (cdr blob)
4043 (mapcar
4044 (lambda (object)
4045 (when (and first-flag (stringp object))
4046 (setq first-flag nil)
4047 (setq object
4048 (replace-regexp-in-string
4049 (format "\\` \\{%d\\}" mci) "" object)))
4050 (cond
4051 ((stringp object)
4052 (replace-regexp-in-string
4053 (format "\n \\{%d\\}" mci) "\n" object))
4054 ((memq (org-element-type object)
4055 org-element-recursive-objects)
4056 (funcall build object mci first-flag))
4057 (t object)))
4058 (org-element-contents blob)))
4059 blob))))
4060 (funcall build element (apply 'min ind-list) (not ignore-first))))))
4064 ;;; The Toolbox
4066 ;; The first move is to implement a way to obtain the smallest element
4067 ;; containing point. This is the job of `org-element-at-point'. It
4068 ;; basically jumps back to the beginning of section containing point
4069 ;; and moves, element after element, with
4070 ;; `org-element--current-element' until the container is found. Note:
4071 ;; When using `org-element-at-point', secondary values are never
4072 ;; parsed since the function focuses on elements, not on objects.
4074 ;; At a deeper level, `org-element-context' lists all elements and
4075 ;; objects containing point.
4077 ;; `org-element-nested-p' and `org-element-swap-A-B' may be used
4078 ;; internally by navigation and manipulation tools.
4080 ;;;###autoload
4081 (defun org-element-at-point (&optional keep-trail)
4082 "Determine closest element around point.
4084 Return value is a list like (TYPE PROPS) where TYPE is the type
4085 of the element and PROPS a plist of properties associated to the
4086 element.
4088 Possible types are defined in `org-element-all-elements'.
4089 Properties depend on element or object type, but always
4090 include :begin, :end, :parent and :post-blank properties.
4092 As a special case, if point is at the very beginning of a list or
4093 sub-list, returned element will be that list instead of the first
4094 item. In the same way, if point is at the beginning of the first
4095 row of a table, returned element will be the table instead of the
4096 first row.
4098 If optional argument KEEP-TRAIL is non-nil, the function returns
4099 a list of of elements leading to element at point. The list's
4100 CAR is always the element at point. Following positions contain
4101 element's siblings, then parents, siblings of parents, until the
4102 first element of current section."
4103 (org-with-wide-buffer
4104 ;; If at an headline, parse it. It is the sole element that
4105 ;; doesn't require to know about context. Be sure to disallow
4106 ;; secondary string parsing, though.
4107 (if (org-with-limited-levels (org-at-heading-p))
4108 (progn
4109 (beginning-of-line)
4110 (if (not keep-trail) (org-element-headline-parser (point-max) t)
4111 (list (org-element-headline-parser (point-max) t))))
4112 ;; Otherwise move at the beginning of the section containing
4113 ;; point.
4114 (let ((origin (point))
4115 (end (save-excursion
4116 (org-with-limited-levels (outline-next-heading)) (point)))
4117 element type special-flag trail struct prevs parent)
4118 (org-with-limited-levels
4119 (if (org-with-limited-levels (org-before-first-heading-p))
4120 (goto-char (point-min))
4121 (org-back-to-heading)
4122 (forward-line)))
4123 (org-skip-whitespace)
4124 (beginning-of-line)
4125 ;; Parse successively each element, skipping those ending
4126 ;; before original position.
4127 (catch 'exit
4128 (while t
4129 (setq element
4130 (org-element--current-element end 'element special-flag struct)
4131 type (car element))
4132 (org-element-put-property element :parent parent)
4133 (when keep-trail (push element trail))
4134 (cond
4135 ;; 1. Skip any element ending before point. Also skip
4136 ;; element ending at point when we're sure that another
4137 ;; element has started.
4138 ((let ((elem-end (org-element-property :end element)))
4139 (when (or (< elem-end origin)
4140 (and (= elem-end origin) (/= elem-end end)))
4141 (goto-char elem-end))))
4142 ;; 2. An element containing point is always the element at
4143 ;; point.
4144 ((not (memq type org-element-greater-elements))
4145 (throw 'exit (if keep-trail trail element)))
4146 ;; 3. At any other greater element type, if point is
4147 ;; within contents, move into it.
4149 (let ((cbeg (org-element-property :contents-begin element))
4150 (cend (org-element-property :contents-end element)))
4151 (if (or (not cbeg) (not cend) (> cbeg origin) (< cend origin)
4152 ;; Create an anchor for tables and plain lists:
4153 ;; when point is at the very beginning of these
4154 ;; elements, ignoring affiliated keywords,
4155 ;; target them instead of their contents.
4156 (and (= cbeg origin) (memq type '(plain-list table)))
4157 ;; When point is at contents end, do not move
4158 ;; into elements with an explicit ending, but
4159 ;; return that element instead.
4160 (and (= cend origin)
4161 (memq type
4162 '(center-block
4163 drawer dynamic-block inlinetask item
4164 plain-list quote-block special-block))))
4165 (throw 'exit (if keep-trail trail element))
4166 (setq parent element)
4167 (case type
4168 (plain-list
4169 (setq special-flag 'item
4170 struct (org-element-property :structure element)))
4171 (table (setq special-flag 'table-row))
4172 (otherwise (setq special-flag nil)))
4173 (setq end cend)
4174 (goto-char cbeg)))))))))))
4176 ;;;###autoload
4177 (defun org-element-context ()
4178 "Return closest element or object around point.
4180 Return value is a list like (TYPE PROPS) where TYPE is the type
4181 of the element or object and PROPS a plist of properties
4182 associated to it.
4184 Possible types are defined in `org-element-all-elements' and
4185 `org-element-all-objects'. Properties depend on element or
4186 object type, but always include :begin, :end, :parent
4187 and :post-blank properties."
4188 (org-with-wide-buffer
4189 (let* ((origin (point))
4190 (element (org-element-at-point))
4191 (type (car element))
4192 end)
4193 ;; Check if point is inside an element containing objects or at
4194 ;; a secondary string. In that case, move to beginning of the
4195 ;; element or secondary string and set END to the other side.
4196 (if (not (or (and (eq type 'item)
4197 (let ((tag (org-element-property :tag element)))
4198 (and tag
4199 (progn
4200 (beginning-of-line)
4201 (search-forward tag (point-at-eol))
4202 (goto-char (match-beginning 0))
4203 (and (>= origin (point))
4204 (<= origin
4205 ;; `1+' is required so some
4206 ;; successors can match
4207 ;; properly their object.
4208 (setq end (1+ (match-end 0)))))))))
4209 (and (memq type '(headline inlinetask))
4210 (progn (beginning-of-line)
4211 (skip-chars-forward "* ")
4212 (setq end (point-at-eol))))
4213 (and (memq type '(paragraph table-cell verse-block))
4214 (let ((cbeg (org-element-property
4215 :contents-begin element))
4216 (cend (org-element-property
4217 :contents-end element)))
4218 (and (>= origin cbeg)
4219 (<= origin cend)
4220 (progn (goto-char cbeg) (setq end cend)))))))
4221 element
4222 (let ((restriction (org-element-restriction element))
4223 (parent element)
4224 candidates)
4225 (catch 'exit
4226 (while (setq candidates (org-element--get-next-object-candidates
4227 end restriction candidates))
4228 (let ((closest-cand (rassq (apply 'min (mapcar 'cdr candidates))
4229 candidates)))
4230 ;; If ORIGIN is before next object in element, there's
4231 ;; no point in looking further.
4232 (if (> (cdr closest-cand) origin) (throw 'exit element)
4233 (let* ((object
4234 (progn (goto-char (cdr closest-cand))
4235 (funcall (intern (format "org-element-%s-parser"
4236 (car closest-cand))))))
4237 (cbeg (org-element-property :contents-begin object))
4238 (cend (org-element-property :contents-end object)))
4239 (cond
4240 ;; ORIGIN is after OBJECT, so skip it.
4241 ((< (org-element-property :end object) origin)
4242 (goto-char (org-element-property :end object)))
4243 ;; ORIGIN is within a non-recursive object or at an
4244 ;; object boundaries: Return that object.
4245 ((or (not cbeg) (> cbeg origin) (< cend origin))
4246 (throw 'exit
4247 (org-element-put-property object :parent parent)))
4248 ;; Otherwise, move within current object and restrict
4249 ;; search to the end of its contents.
4250 (t (goto-char cbeg)
4251 (org-element-put-property object :parent parent)
4252 (setq parent object end cend)))))))
4253 parent))))))
4255 (defsubst org-element-nested-p (elem-A elem-B)
4256 "Non-nil when elements ELEM-A and ELEM-B are nested."
4257 (let ((beg-A (org-element-property :begin elem-A))
4258 (beg-B (org-element-property :begin elem-B))
4259 (end-A (org-element-property :end elem-A))
4260 (end-B (org-element-property :end elem-B)))
4261 (or (and (>= beg-A beg-B) (<= end-A end-B))
4262 (and (>= beg-B beg-A) (<= end-B end-A)))))
4264 (defun org-element-swap-A-B (elem-A elem-B)
4265 "Swap elements ELEM-A and ELEM-B.
4266 Assume ELEM-B is after ELEM-A in the buffer. Leave point at the
4267 end of ELEM-A."
4268 (goto-char (org-element-property :begin elem-A))
4269 ;; There are two special cases when an element doesn't start at bol:
4270 ;; the first paragraph in an item or in a footnote definition.
4271 (let ((specialp (not (bolp))))
4272 ;; Only a paragraph without any affiliated keyword can be moved at
4273 ;; ELEM-A position in such a situation. Note that the case of
4274 ;; a footnote definition is impossible: it cannot contain two
4275 ;; paragraphs in a row because it cannot contain a blank line.
4276 (if (and specialp
4277 (or (not (eq (org-element-type elem-B) 'paragraph))
4278 (/= (org-element-property :begin elem-B)
4279 (org-element-property :contents-begin elem-B))))
4280 (error "Cannot swap elements"))
4281 ;; In a special situation, ELEM-A will have no indentation. We'll
4282 ;; give it ELEM-B's (which will in, in turn, have no indentation).
4283 (let* ((ind-B (when specialp
4284 (goto-char (org-element-property :begin elem-B))
4285 (org-get-indentation)))
4286 (beg-A (org-element-property :begin elem-A))
4287 (end-A (save-excursion
4288 (goto-char (org-element-property :end elem-A))
4289 (skip-chars-backward " \r\t\n")
4290 (point-at-eol)))
4291 (beg-B (org-element-property :begin elem-B))
4292 (end-B (save-excursion
4293 (goto-char (org-element-property :end elem-B))
4294 (skip-chars-backward " \r\t\n")
4295 (point-at-eol)))
4296 ;; Store overlays responsible for visibility status. We
4297 ;; also need to store their boundaries as they will be
4298 ;; removed from buffer.
4299 (overlays
4300 (cons
4301 (mapcar (lambda (ov) (list ov (overlay-start ov) (overlay-end ov)))
4302 (overlays-in beg-A end-A))
4303 (mapcar (lambda (ov) (list ov (overlay-start ov) (overlay-end ov)))
4304 (overlays-in beg-B end-B))))
4305 ;; Get contents.
4306 (body-A (buffer-substring beg-A end-A))
4307 (body-B (delete-and-extract-region beg-B end-B)))
4308 (goto-char beg-B)
4309 (when specialp
4310 (setq body-B (replace-regexp-in-string "\\`[ \t]*" "" body-B))
4311 (org-indent-to-column ind-B))
4312 (insert body-A)
4313 ;; Restore ex ELEM-A overlays.
4314 (let ((offset (- beg-B beg-A)))
4315 (mapc (lambda (ov)
4316 (move-overlay
4317 (car ov) (+ (nth 1 ov) offset) (+ (nth 2 ov) offset)))
4318 (car overlays))
4319 (goto-char beg-A)
4320 (delete-region beg-A end-A)
4321 (insert body-B)
4322 ;; Restore ex ELEM-B overlays.
4323 (mapc (lambda (ov)
4324 (move-overlay
4325 (car ov) (- (nth 1 ov) offset) (- (nth 2 ov) offset)))
4326 (cdr overlays)))
4327 (goto-char (org-element-property :end elem-B)))))
4330 (provide 'org-element)
4331 ;;; org-element.el ends here