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