1 ;;; org-element.el --- Parser And Applications for Org syntax
3 ;; Copyright (C) 2012-2013 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; Org syntax can be divided into three categories: "Greater
26 ;; elements", "Elements" and "Objects".
28 ;; Elements are related to the structure of the document. Indeed, all
29 ;; elements are a cover for the document: each position within belongs
30 ;; to at least one element.
32 ;; An element always starts and ends at the beginning of a line. With
33 ;; a few exceptions (`clock', `headline', `inlinetask', `item',
34 ;; `planning', `node-property', `quote-section' `section' and
35 ;; `table-row' types), it can also accept a fixed set of keywords as
36 ;; attributes. Those are called "affiliated keywords" to distinguish
37 ;; them from other keywords, which are full-fledged elements. Almost
38 ;; all affiliated keywords are referenced in
39 ;; `org-element-affiliated-keywords'; the others are export attributes
40 ;; and start with "ATTR_" prefix.
42 ;; Element containing other elements (and only elements) are called
43 ;; greater elements. Concerned types are: `center-block', `drawer',
44 ;; `dynamic-block', `footnote-definition', `headline', `inlinetask',
45 ;; `item', `plain-list', `property-drawer', `quote-block', `section'
46 ;; and `special-block'.
48 ;; Other element types are: `babel-call', `clock', `comment',
49 ;; `comment-block', `diary-sexp', `example-block', `export-block',
50 ;; `fixed-width', `horizontal-rule', `keyword', `latex-environment',
51 ;; `node-property', `paragraph', `planning', `quote-section',
52 ;; `src-block', `table', `table-row' and `verse-block'. Among them,
53 ;; `paragraph' and `verse-block' types can contain Org objects and
56 ;; Objects are related to document's contents. Some of them are
57 ;; recursive. Associated types are of the following: `bold', `code',
58 ;; `entity', `export-snippet', `footnote-reference',
59 ;; `inline-babel-call', `inline-src-block', `italic',
60 ;; `latex-fragment', `line-break', `link', `macro', `radio-target',
61 ;; `statistics-cookie', `strike-through', `subscript', `superscript',
62 ;; `table-cell', `target', `timestamp', `underline' and `verbatim'.
64 ;; Some elements also have special properties whose value can hold
65 ;; objects themselves (i.e. an item tag or an headline name). Such
66 ;; values are called "secondary strings". Any object belongs to
67 ;; either an element or a secondary string.
69 ;; Notwithstanding affiliated keywords, each greater element, element
70 ;; and object has a fixed set of properties attached to it. Among
71 ;; them, four are shared by all types: `:begin' and `:end', which
72 ;; refer to the beginning and ending buffer positions of the
73 ;; considered element or object, `:post-blank', which holds the number
74 ;; of blank lines, or white spaces, at its end and `:parent' which
75 ;; refers to the element or object containing it. Greater elements,
76 ;; elements and objects containing objects will also have
77 ;; `:contents-begin' and `:contents-end' properties to delimit
78 ;; contents. Eventually, greater elements and elements accepting
79 ;; affiliated keywords will have a `:post-affiliated' property,
80 ;; referring to the buffer position after all such keywords.
82 ;; At the lowest level, a `:parent' property is also attached to any
83 ;; string, as a text property.
85 ;; Lisp-wise, an element or an object can be represented as a list.
86 ;; It follows the pattern (TYPE PROPERTIES CONTENTS), where:
87 ;; TYPE is a symbol describing the Org element or object.
88 ;; PROPERTIES is the property list attached to it. See docstring of
89 ;; appropriate parsing function to get an exhaustive
91 ;; CONTENTS is a list of elements, objects or raw strings contained
92 ;; in the current element or object, when applicable.
94 ;; An Org buffer is a nested list of such elements and objects, whose
95 ;; type is `org-data' and properties is nil.
97 ;; The first part of this file defines Org syntax, while the second
98 ;; one provide accessors and setters functions.
100 ;; The next part implements a parser and an interpreter for each
101 ;; element and object type in Org syntax.
103 ;; The following part creates a fully recursive buffer parser. It
104 ;; also provides a tool to map a function to elements or objects
105 ;; matching some criteria in the parse tree. Functions of interest
106 ;; are `org-element-parse-buffer', `org-element-map' and, to a lesser
107 ;; extent, `org-element-parse-secondary-string'.
109 ;; The penultimate part is the cradle of an interpreter for the
110 ;; obtained parse tree: `org-element-interpret-data'.
112 ;; The library ends by furnishing `org-element-at-point' function, and
113 ;; a way to give information about document structure around point
114 ;; with `org-element-context'.
119 (eval-when-compile (require 'cl
))
124 ;;; Definitions And Rules
126 ;; Define elements, greater elements and specify recursive objects,
127 ;; along with the affiliated keywords recognized. Also set up
128 ;; restrictions on recursive objects combinations.
130 ;; These variables really act as a control center for the parsing
133 (defconst org-element-paragraph-separate
135 ;; Headlines, inlinetasks.
136 org-outline-regexp
"\\|"
137 ;; Footnote definitions.
138 "\\[\\(?:[0-9]+\\|fn:[-_[:word:]]+\\)\\]" "\\|"
144 ;; Tables (any type).
145 "\\(?:|\\|\\+-[-+]\\)" "\\|"
146 ;; Blocks (any type), Babel calls, drawers (any type),
147 ;; fixed-width areas and keywords. Note: this is only an
148 ;; indication and need some thorough check.
151 "-\\{5,\\}[ \t]*$" "\\|"
152 ;; LaTeX environments.
153 "\\\\begin{\\([A-Za-z0-9]+\\*?\\)}" "\\|"
154 ;; Planning and Clock lines.
155 (regexp-opt (list org-scheduled-string
161 (let ((term (case org-plain-list-ordered-item-terminator
162 (?\
) ")") (?.
"\\.") (otherwise "[.)]")))
163 (alpha (and org-alphabetical-lists
"\\|[A-Za-z]")))
164 (concat "\\(?:[-+*]\\|\\(?:[0-9]+" alpha
"\\)" term
"\\)"
165 "\\(?:[ \t]\\|$\\)"))
167 "Regexp to separate paragraphs in an Org buffer.
168 In the case of lines starting with \"#\" and \":\", this regexp
169 is not sufficient to know if point is at a paragraph ending. See
170 `org-element-paragraph-parser' for more information.")
172 (defconst org-element-all-elements
173 '(babel-call center-block clock comment comment-block diary-sexp drawer
174 dynamic-block example-block export-block fixed-width
175 footnote-definition headline horizontal-rule inlinetask item
176 keyword latex-environment node-property paragraph plain-list
177 planning property-drawer quote-block quote-section section
178 special-block src-block table table-row verse-block
)
179 "Complete list of element types.")
181 (defconst org-element-greater-elements
182 '(center-block drawer dynamic-block footnote-definition headline inlinetask
183 item plain-list property-drawer quote-block section
185 "List of recursive element types aka Greater Elements.")
187 (defconst org-element-all-successors
188 '(export-snippet footnote-reference inline-babel-call inline-src-block
189 latex-or-entity line-break link macro radio-target
190 statistics-cookie sub
/superscript table-cell target
191 text-markup timestamp
)
192 "Complete list of successors.")
194 (defconst org-element-object-successor-alist
195 '((subscript . sub
/superscript
) (superscript . sub
/superscript
)
196 (bold . text-markup
) (code . text-markup
) (italic . text-markup
)
197 (strike-through . text-markup
) (underline . text-markup
)
198 (verbatim . text-markup
) (entity . latex-or-entity
)
199 (latex-fragment . latex-or-entity
))
200 "Alist of translations between object type and successor name.
202 Sharing the same successor comes handy when, for example, the
203 regexp matching one object can also match the other object.")
205 (defconst org-element-all-objects
206 '(bold code entity export-snippet footnote-reference inline-babel-call
207 inline-src-block italic line-break latex-fragment link macro
208 radio-target statistics-cookie strike-through subscript superscript
209 table-cell target timestamp underline verbatim
)
210 "Complete list of object types.")
212 (defconst org-element-recursive-objects
213 '(bold italic link subscript radio-target strike-through superscript
214 table-cell underline
)
215 "List of recursive object types.")
217 (defvar org-element-block-name-alist
218 '(("CENTER" . org-element-center-block-parser
)
219 ("COMMENT" . org-element-comment-block-parser
)
220 ("EXAMPLE" . org-element-example-block-parser
)
221 ("QUOTE" . org-element-quote-block-parser
)
222 ("SRC" . org-element-src-block-parser
)
223 ("VERSE" . org-element-verse-block-parser
))
224 "Alist between block names and the associated parsing function.
225 Names must be uppercase. Any block whose name has no association
226 is parsed with `org-element-special-block-parser'.")
228 (defconst org-element-link-type-is-file
229 '("file" "file+emacs" "file+sys" "docview")
230 "List of link types equivalent to \"file\".
231 Only these types can accept search options and an explicit
232 application to open them.")
234 (defconst org-element-affiliated-keywords
235 '("CAPTION" "DATA" "HEADER" "HEADERS" "LABEL" "NAME" "PLOT" "RESNAME" "RESULT"
236 "RESULTS" "SOURCE" "SRCNAME" "TBLNAME")
237 "List of affiliated keywords as strings.
238 By default, all keywords setting attributes (i.e. \"ATTR_LATEX\")
239 are affiliated keywords and need not to be in this list.")
241 (defconst org-element--affiliated-re
242 (format "[ \t]*#\\+%s:"
243 ;; Regular affiliated keywords.
244 (format "\\(%s\\|ATTR_[-_A-Za-z0-9]+\\)\\(?:\\[\\(.*\\)\\]\\)?"
245 (regexp-opt org-element-affiliated-keywords
)))
246 "Regexp matching any affiliated keyword.
248 Keyword name is put in match group 1. Moreover, if keyword
249 belongs to `org-element-dual-keywords', put the dual value in
252 Don't modify it, set `org-element-affiliated-keywords' instead.")
254 (defconst org-element-keyword-translation-alist
255 '(("DATA" .
"NAME") ("LABEL" .
"NAME") ("RESNAME" .
"NAME")
256 ("SOURCE" .
"NAME") ("SRCNAME" .
"NAME") ("TBLNAME" .
"NAME")
257 ("RESULT" .
"RESULTS") ("HEADERS" .
"HEADER"))
258 "Alist of usual translations for keywords.
259 The key is the old name and the value the new one. The property
260 holding their value will be named after the translated name.")
262 (defconst org-element-multiple-keywords
'("CAPTION" "HEADER")
263 "List of affiliated keywords that can occur more than once in an element.
265 Their value will be consed into a list of strings, which will be
266 returned as the value of the property.
268 This list is checked after translations have been applied. See
269 `org-element-keyword-translation-alist'.
271 By default, all keywords setting attributes (i.e. \"ATTR_LATEX\")
272 allow multiple occurrences and need not to be in this list.")
274 (defconst org-element-parsed-keywords
'("CAPTION")
275 "List of affiliated keywords whose value can be parsed.
277 Their value will be stored as a secondary string: a list of
280 This list is checked after translations have been applied. See
281 `org-element-keyword-translation-alist'.")
283 (defconst org-element-dual-keywords
'("CAPTION" "RESULTS")
284 "List of affiliated keywords which can have a secondary value.
286 In Org syntax, they can be written with optional square brackets
287 before the colons. For example, RESULTS keyword can be
288 associated to a hash value with the following:
290 #+RESULTS[hash-string]: some-source
292 This list is checked after translations have been applied. See
293 `org-element-keyword-translation-alist'.")
295 (defconst org-element-document-properties
'("AUTHOR" "DATE" "TITLE")
296 "List of properties associated to the whole document.
297 Any keyword in this list will have its value parsed and stored as
298 a secondary string.")
300 (defconst org-element-object-restrictions
301 '((bold export-snippet inline-babel-call inline-src-block latex-or-entity link
302 radio-target sub
/superscript target text-markup timestamp
)
303 (footnote-reference export-snippet footnote-reference inline-babel-call
304 inline-src-block latex-or-entity line-break link macro
305 radio-target sub
/superscript target text-markup
307 (headline inline-babel-call inline-src-block latex-or-entity link macro
308 radio-target statistics-cookie sub
/superscript target text-markup
310 (inlinetask inline-babel-call inline-src-block latex-or-entity link macro
311 radio-target sub
/superscript target text-markup timestamp
)
312 (italic export-snippet inline-babel-call inline-src-block latex-or-entity
313 link radio-target sub
/superscript target text-markup timestamp
)
314 (item export-snippet footnote-reference inline-babel-call latex-or-entity
315 link macro radio-target sub
/superscript target text-markup
)
316 (keyword inline-babel-call inline-src-block latex-or-entity link macro
317 sub
/superscript text-markup timestamp
)
318 (link export-snippet inline-babel-call inline-src-block latex-or-entity link
319 sub
/superscript text-markup
)
320 (paragraph export-snippet footnote-reference inline-babel-call
321 inline-src-block latex-or-entity line-break link macro
322 radio-target statistics-cookie sub
/superscript target text-markup
324 (radio-target export-snippet latex-or-entity sub
/superscript
)
325 (strike-through export-snippet inline-babel-call inline-src-block
326 latex-or-entity link radio-target sub
/superscript target
327 text-markup timestamp
)
328 (subscript export-snippet inline-babel-call inline-src-block latex-or-entity
329 sub
/superscript target text-markup
)
330 (superscript export-snippet inline-babel-call inline-src-block
331 latex-or-entity sub
/superscript target text-markup
)
332 (table-cell export-snippet footnote-reference latex-or-entity link macro
333 radio-target sub
/superscript target text-markup timestamp
)
334 (table-row table-cell
)
335 (underline export-snippet inline-babel-call inline-src-block latex-or-entity
336 link radio-target sub
/superscript target text-markup timestamp
)
337 (verse-block footnote-reference inline-babel-call inline-src-block
338 latex-or-entity line-break link macro radio-target
339 sub
/superscript target text-markup timestamp
))
340 "Alist of objects restrictions.
342 CAR is an element or object type containing objects and CDR is
343 a list of successors that will be called within an element or
346 For example, in a `radio-target' object, one can only find
347 entities, export snippets, latex-fragments, subscript and
350 This alist also applies to secondary string. For example, an
351 `headline' type element doesn't directly contain objects, but
352 still has an entry since one of its properties (`:title') does.")
354 (defconst org-element-secondary-value-alist
355 '((headline .
:title
)
356 (inlinetask .
:title
)
358 (footnote-reference .
:inline-definition
))
359 "Alist between element types and location of secondary value.")
361 (defconst org-element-object-variables
'(org-link-abbrev-alist-local)
362 "List of buffer-local variables used when parsing objects.
363 These variables are copied to the temporary buffer created by
364 `org-export-secondary-string'.")
368 ;;; Accessors and Setters
370 ;; Provide four accessors: `org-element-type', `org-element-property'
371 ;; `org-element-contents' and `org-element-restriction'.
373 ;; Setter functions allow to modify elements by side effect. There is
374 ;; `org-element-put-property', `org-element-set-contents',
375 ;; `org-element-set-element' and `org-element-adopt-element'. Note
376 ;; that `org-element-set-element' and `org-element-adopt-elements' are
377 ;; higher level functions since also update `:parent' property.
379 (defsubst org-element-type
(element)
380 "Return type of ELEMENT.
382 The function returns the type of the element or object provided.
383 It can also return the following special value:
384 `plain-text' for a string
385 `org-data' for a complete document
386 nil in any other case."
388 ((not (consp element
)) (and (stringp element
) 'plain-text
))
389 ((symbolp (car element
)) (car element
))))
391 (defsubst org-element-property
(property element
)
392 "Extract the value from the PROPERTY of an ELEMENT."
393 (if (stringp element
) (get-text-property 0 property element
)
394 (plist-get (nth 1 element
) property
)))
396 (defsubst org-element-contents
(element)
397 "Extract contents from an ELEMENT."
398 (cond ((not (consp element
)) nil
)
399 ((symbolp (car element
)) (nthcdr 2 element
))
402 (defsubst org-element-restriction
(element)
403 "Return restriction associated to ELEMENT.
404 ELEMENT can be an element, an object or a symbol representing an
405 element or object type."
406 (cdr (assq (if (symbolp element
) element
(org-element-type element
))
407 org-element-object-restrictions
)))
409 (defsubst org-element-put-property
(element property value
)
410 "In ELEMENT set PROPERTY to VALUE.
411 Return modified element."
412 (if (stringp element
) (org-add-props element nil property value
)
413 (setcar (cdr element
) (plist-put (nth 1 element
) property value
))
416 (defsubst org-element-set-contents
(element &rest contents
)
417 "Set ELEMENT contents to CONTENTS.
418 Return modified element."
419 (cond ((not element
) (list contents
))
420 ((not (symbolp (car element
))) contents
)
421 ((cdr element
) (setcdr (cdr element
) contents
))
422 (t (nconc element contents
))))
424 (defsubst org-element-set-element
(old new
)
425 "Replace element or object OLD with element or object NEW.
426 The function takes care of setting `:parent' property for NEW."
427 ;; Since OLD is going to be changed into NEW by side-effect, first
428 ;; make sure that every element or object within NEW has OLD as
430 (mapc (lambda (blob) (org-element-put-property blob
:parent old
))
431 (org-element-contents new
))
432 ;; Transfer contents.
433 (apply 'org-element-set-contents old
(org-element-contents new
))
434 ;; Ensure NEW has same parent as OLD, then overwrite OLD properties
436 (org-element-put-property new
:parent
(org-element-property :parent old
))
437 (setcar (cdr old
) (nth 1 new
))
439 (setcar old
(car new
)))
441 (defsubst org-element-adopt-elements
(parent &rest children
)
442 "Append elements to the contents of another element.
444 PARENT is an element or object. CHILDREN can be elements,
445 objects, or a strings.
447 The function takes care of setting `:parent' property for CHILD.
448 Return parent element."
449 ;; Link every child to PARENT. If PARENT is nil, it is a secondary
450 ;; string: parent is the list itself.
451 (mapc (lambda (child)
452 (org-element-put-property child
:parent
(or parent children
)))
454 ;; Add CHILDREN at the end of PARENT contents.
456 (apply 'org-element-set-contents
458 (nconc (org-element-contents parent
) children
)))
459 ;; Return modified PARENT element.
460 (or parent children
))
466 ;; For each greater element type, we define a parser and an
469 ;; A parser returns the element or object as the list described above.
470 ;; Most of them accepts no argument. Though, exceptions exist. Hence
471 ;; every element containing a secondary string (see
472 ;; `org-element-secondary-value-alist') will accept an optional
473 ;; argument to toggle parsing of that secondary string. Moreover,
474 ;; `item' parser requires current list's structure as its first
477 ;; An interpreter accepts two arguments: the list representation of
478 ;; the element or object, and its contents. The latter may be nil,
479 ;; depending on the element or object considered. It returns the
480 ;; appropriate Org syntax, as a string.
482 ;; Parsing functions must follow the naming convention:
483 ;; org-element-TYPE-parser, where TYPE is greater element's type, as
484 ;; defined in `org-element-greater-elements'.
486 ;; Similarly, interpreting functions must follow the naming
487 ;; convention: org-element-TYPE-interpreter.
489 ;; With the exception of `headline' and `item' types, greater elements
490 ;; cannot contain other greater elements of their own type.
492 ;; Beside implementing a parser and an interpreter, adding a new
493 ;; greater element requires to tweak `org-element--current-element'.
494 ;; Moreover, the newly defined type must be added to both
495 ;; `org-element-all-elements' and `org-element-greater-elements'.
500 (defun org-element-center-block-parser (limit affiliated
)
501 "Parse a center block.
503 LIMIT bounds the search. AFFILIATED is a list of which CAR is
504 the buffer position at the beginning of the first affiliated
505 keyword and CDR is a plist of affiliated keywords along with
508 Return a list whose CAR is `center-block' and CDR is a plist
509 containing `:begin', `:end', `:hiddenp', `:contents-begin',
510 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
512 Assume point is at the beginning of the block."
513 (let ((case-fold-search t
))
514 (if (not (save-excursion
515 (re-search-forward "^[ \t]*#\\+END_CENTER[ \t]*$" limit t
)))
516 ;; Incomplete block: parse it as a paragraph.
517 (org-element-paragraph-parser limit affiliated
)
518 (let ((block-end-line (match-beginning 0)))
519 (let* ((begin (car affiliated
))
520 (post-affiliated (point))
521 ;; Empty blocks have no contents.
522 (contents-begin (progn (forward-line)
523 (and (< (point) block-end-line
)
525 (contents-end (and contents-begin block-end-line
))
526 (hidden (org-invisible-p2))
527 (pos-before-blank (progn (goto-char block-end-line
)
530 (end (save-excursion (skip-chars-forward " \r\t\n" limit
)
531 (skip-chars-backward " \t")
532 (if (bolp) (point) (line-end-position)))))
538 :contents-begin contents-begin
539 :contents-end contents-end
540 :post-blank
(count-lines pos-before-blank end
)
541 :post-affiliated post-affiliated
)
542 (cdr affiliated
))))))))
544 (defun org-element-center-block-interpreter (center-block contents
)
545 "Interpret CENTER-BLOCK element as Org syntax.
546 CONTENTS is the contents of the element."
547 (format "#+BEGIN_CENTER\n%s#+END_CENTER" contents
))
552 (defun org-element-drawer-parser (limit affiliated
)
555 LIMIT bounds the search. AFFILIATED is a list of which CAR is
556 the buffer position at the beginning of the first affiliated
557 keyword and CDR is a plist of affiliated keywords along with
560 Return a list whose CAR is `drawer' and CDR is a plist containing
561 `:drawer-name', `:begin', `:end', `:hiddenp', `:contents-begin',
562 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
564 Assume point is at beginning of drawer."
565 (let ((case-fold-search t
))
566 (if (not (save-excursion (re-search-forward "^[ \t]*:END:[ \t]*$" limit t
)))
567 ;; Incomplete drawer: parse it as a paragraph.
568 (org-element-paragraph-parser limit affiliated
)
570 (let* ((drawer-end-line (match-beginning 0))
571 (name (progn (looking-at org-drawer-regexp
)
572 (org-match-string-no-properties 1)))
573 (begin (car affiliated
))
574 (post-affiliated (point))
575 ;; Empty drawers have no contents.
576 (contents-begin (progn (forward-line)
577 (and (< (point) drawer-end-line
)
579 (contents-end (and contents-begin drawer-end-line
))
580 (hidden (org-invisible-p2))
581 (pos-before-blank (progn (goto-char drawer-end-line
)
584 (end (progn (skip-chars-forward " \r\t\n" limit
)
585 (skip-chars-backward " \t")
586 (if (bolp) (point) (line-end-position)))))
593 :contents-begin contents-begin
594 :contents-end contents-end
595 :post-blank
(count-lines pos-before-blank end
)
596 :post-affiliated post-affiliated
)
597 (cdr affiliated
))))))))
599 (defun org-element-drawer-interpreter (drawer contents
)
600 "Interpret DRAWER element as Org syntax.
601 CONTENTS is the contents of the element."
602 (format ":%s:\n%s:END:"
603 (org-element-property :drawer-name drawer
)
609 (defun org-element-dynamic-block-parser (limit affiliated
)
610 "Parse a dynamic block.
612 LIMIT bounds the search. AFFILIATED is a list of which CAR is
613 the buffer position at the beginning of the first affiliated
614 keyword and CDR is a plist of affiliated keywords along with
617 Return a list whose CAR is `dynamic-block' and CDR is a plist
618 containing `:block-name', `:begin', `:end', `:hiddenp',
619 `:contents-begin', `:contents-end', `:arguments', `:post-blank'
620 and `:post-affiliated' keywords.
622 Assume point is at beginning of dynamic block."
623 (let ((case-fold-search t
))
624 (if (not (save-excursion
625 (re-search-forward "^[ \t]*#\\+END:?[ \t]*$" limit t
)))
626 ;; Incomplete block: parse it as a paragraph.
627 (org-element-paragraph-parser limit affiliated
)
628 (let ((block-end-line (match-beginning 0)))
630 (let* ((name (progn (looking-at org-dblock-start-re
)
631 (org-match-string-no-properties 1)))
632 (arguments (org-match-string-no-properties 3))
633 (begin (car affiliated
))
634 (post-affiliated (point))
635 ;; Empty blocks have no contents.
636 (contents-begin (progn (forward-line)
637 (and (< (point) block-end-line
)
639 (contents-end (and contents-begin block-end-line
))
640 (hidden (org-invisible-p2))
641 (pos-before-blank (progn (goto-char block-end-line
)
644 (end (progn (skip-chars-forward " \r\t\n" limit
)
645 (skip-chars-backward " \t")
646 (if (bolp) (point) (line-end-position)))))
654 :contents-begin contents-begin
655 :contents-end contents-end
656 :post-blank
(count-lines pos-before-blank end
)
657 :post-affiliated post-affiliated
)
658 (cdr affiliated
)))))))))
660 (defun org-element-dynamic-block-interpreter (dynamic-block contents
)
661 "Interpret DYNAMIC-BLOCK element as Org syntax.
662 CONTENTS is the contents of the element."
663 (format "#+BEGIN: %s%s\n%s#+END:"
664 (org-element-property :block-name dynamic-block
)
665 (let ((args (org-element-property :arguments dynamic-block
)))
666 (and args
(concat " " args
)))
670 ;;;; Footnote Definition
672 (defun org-element-footnote-definition-parser (limit affiliated
)
673 "Parse a footnote definition.
675 LIMIT bounds the search. AFFILIATED is a list of which CAR is
676 the buffer position at the beginning of the first affiliated
677 keyword and CDR is a plist of affiliated keywords along with
680 Return a list whose CAR is `footnote-definition' and CDR is
681 a plist containing `:label', `:begin' `:end', `:contents-begin',
682 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
684 Assume point is at the beginning of the footnote definition."
686 (let* ((label (progn (looking-at org-footnote-definition-re
)
687 (org-match-string-no-properties 1)))
688 (begin (car affiliated
))
689 (post-affiliated (point))
690 (ending (save-excursion
694 (concat org-outline-regexp-bol
"\\|"
695 org-footnote-definition-re
"\\|"
696 "^[ \t]*$") limit
'move
))
699 (contents-begin (progn (search-forward "]")
700 (skip-chars-forward " \r\t\n" ending
)
701 (and (/= (point) ending
) (point))))
702 (contents-end (and contents-begin ending
))
703 (end (progn (goto-char ending
)
704 (skip-chars-forward " \r\t\n" limit
)
705 (skip-chars-backward " \t")
706 (if (bolp) (point) (line-end-position)))))
707 (list 'footnote-definition
712 :contents-begin contents-begin
713 :contents-end contents-end
714 :post-blank
(count-lines ending end
)
715 :post-affiliated post-affiliated
)
716 (cdr affiliated
))))))
718 (defun org-element-footnote-definition-interpreter (footnote-definition contents
)
719 "Interpret FOOTNOTE-DEFINITION element as Org syntax.
720 CONTENTS is the contents of the footnote-definition."
721 (concat (format "[%s]" (org-element-property :label footnote-definition
))
728 (defun org-element-headline-parser (limit &optional raw-secondary-p
)
731 Return a list whose CAR is `headline' and CDR is a plist
732 containing `:raw-value', `:title', `:begin', `:end',
733 `:pre-blank', `:hiddenp', `:contents-begin' and `:contents-end',
734 `:level', `:priority', `:tags', `:todo-keyword',`:todo-type',
735 `:scheduled', `:deadline', `:closed', `:quotedp', `:archivedp',
736 `:commentedp' and `:footnote-section-p' keywords.
738 The plist also contains any property set in the property drawer,
739 with its name in lowercase, the underscores replaced with hyphens
740 and colons at the beginning (i.e. `:custom-id').
742 When RAW-SECONDARY-P is non-nil, headline's title will not be
743 parsed as a secondary string, but as a plain string instead.
745 Assume point is at beginning of the headline."
747 (let* ((components (org-heading-components))
748 (level (nth 1 components
))
749 (todo (nth 2 components
))
751 (and todo
(if (member todo org-done-keywords
) 'done
'todo
)))
752 (tags (let ((raw-tags (nth 5 components
)))
753 (and raw-tags
(org-split-string raw-tags
":"))))
754 (raw-value (or (nth 4 components
) ""))
756 (let ((case-fold-search nil
))
757 (string-match (format "^%s\\( \\|$\\)" org-quote-string
)
760 (let ((case-fold-search nil
))
761 (string-match (format "^%s\\( \\|$\\)" org-comment-string
)
763 (archivedp (member org-archive-tag tags
))
764 (footnote-section-p (and org-footnote-section
765 (string= org-footnote-section raw-value
)))
766 ;; Normalize property names: ":SOME_PROP:" becomes
775 (replace-regexp-in-string
776 "_" "-" (downcase (car p
)))))
778 (org-entry-properties nil
'standard
))
781 ;; Read time properties on the line below the headline.
783 (when (progn (forward-line)
784 (looking-at org-planning-or-clock-line-re
))
785 (let ((end (line-end-position)) plist
)
786 (while (re-search-forward
787 org-keyword-time-not-clock-regexp end t
)
788 (goto-char (match-end 1))
789 (skip-chars-forward " \t")
790 (let ((keyword (match-string 1))
791 (time (org-element-timestamp-parser)))
792 (cond ((equal keyword org-scheduled-string
)
793 (setq plist
(plist-put plist
:scheduled time
)))
794 ((equal keyword org-deadline-string
)
795 (setq plist
(plist-put plist
:deadline time
)))
796 (t (setq plist
(plist-put plist
:closed time
))))))
799 (end (save-excursion (goto-char (org-end-of-subtree t t
))))
800 (pos-after-head (progn (forward-line) (point)))
801 (contents-begin (save-excursion
802 (skip-chars-forward " \r\t\n" end
)
803 (and (/= (point) end
) (line-beginning-position))))
804 (hidden (org-invisible-p2))
805 (contents-end (and contents-begin
806 (progn (goto-char end
)
807 (skip-chars-backward " \r\t\n")
810 ;; Clean RAW-VALUE from any quote or comment string.
811 (when (or quotedp commentedp
)
812 (let ((case-fold-search nil
))
814 (replace-regexp-in-string
816 (regexp-opt (list org-quote-string org-comment-string
))
820 ;; Clean TAGS from archive tag, if any.
821 (when archivedp
(setq tags
(delete org-archive-tag tags
)))
825 (list :raw-value raw-value
829 (if (not contents-begin
) 0
830 (count-lines pos-after-head contents-begin
))
832 :contents-begin contents-begin
833 :contents-end contents-end
835 :priority
(nth 3 components
)
839 :post-blank
(count-lines
840 (if (not contents-end
) pos-after-head
841 (goto-char contents-end
)
845 :footnote-section-p footnote-section-p
847 :commentedp commentedp
851 (org-element-put-property
853 (if raw-secondary-p raw-value
854 (org-element-parse-secondary-string
855 raw-value
(org-element-restriction 'headline
) headline
)))))))
857 (defun org-element-headline-interpreter (headline contents
)
858 "Interpret HEADLINE element as Org syntax.
859 CONTENTS is the contents of the element."
860 (let* ((level (org-element-property :level headline
))
861 (todo (org-element-property :todo-keyword headline
))
862 (priority (org-element-property :priority headline
))
863 (title (org-element-interpret-data
864 (org-element-property :title headline
)))
865 (tags (let ((tag-list (if (org-element-property :archivedp headline
)
866 (cons org-archive-tag
867 (org-element-property :tags headline
))
868 (org-element-property :tags headline
))))
870 (format ":%s:" (mapconcat 'identity tag-list
":")))))
871 (commentedp (org-element-property :commentedp headline
))
872 (quotedp (org-element-property :quotedp headline
))
873 (pre-blank (or (org-element-property :pre-blank headline
) 0))
874 (heading (concat (make-string level ?
*)
875 (and todo
(concat " " todo
))
876 (and quotedp
(concat " " org-quote-string
))
877 (and commentedp
(concat " " org-comment-string
))
879 (format " [#%s]" (char-to-string priority
)))
880 (cond ((and org-footnote-section
881 (org-element-property
882 :footnote-section-p headline
))
883 (concat " " org-footnote-section
))
884 (title (concat " " title
))))))
889 ((zerop org-tags-column
) (format " %s" tags
))
890 ((< org-tags-column
0)
893 (max (- (+ org-tags-column
(length heading
) (length tags
))) 1)
898 (make-string (max (- org-tags-column
(length heading
)) 1) ?
)
900 (make-string (1+ pre-blank
) 10)
906 (defun org-element-inlinetask-parser (limit &optional raw-secondary-p
)
907 "Parse an inline task.
909 Return a list whose CAR is `inlinetask' and CDR is a plist
910 containing `:title', `:begin', `:end', `:hiddenp',
911 `:contents-begin' and `:contents-end', `:level', `:priority',
912 `:raw-value', `:tags', `:todo-keyword', `:todo-type',
913 `:scheduled', `:deadline', `:closed' and `:post-blank' keywords.
915 The plist also contains any property set in the property drawer,
916 with its name in lowercase, the underscores replaced with hyphens
917 and colons at the beginning (i.e. `:custom-id').
919 When optional argument RAW-SECONDARY-P is non-nil, inline-task's
920 title will not be parsed as a secondary string, but as a plain
923 Assume point is at beginning of the inline task."
925 (let* ((begin (point))
926 (components (org-heading-components))
927 (todo (nth 2 components
))
929 (if (member todo org-done-keywords
) 'done
'todo
)))
930 (tags (let ((raw-tags (nth 5 components
)))
931 (and raw-tags
(org-split-string raw-tags
":"))))
932 (raw-value (or (nth 4 components
) ""))
933 ;; Normalize property names: ":SOME_PROP:" becomes
942 (replace-regexp-in-string
943 "_" "-" (downcase (car p
)))))
945 (org-entry-properties nil
'standard
))
948 ;; Read time properties on the line below the inlinetask
951 (when (progn (forward-line)
952 (looking-at org-planning-or-clock-line-re
))
953 (let ((end (line-end-position)) plist
)
954 (while (re-search-forward
955 org-keyword-time-not-clock-regexp end t
)
956 (goto-char (match-end 1))
957 (skip-chars-forward " \t")
958 (let ((keyword (match-string 1))
959 (time (org-element-timestamp-parser)))
960 (cond ((equal keyword org-scheduled-string
)
961 (setq plist
(plist-put plist
:scheduled time
)))
962 ((equal keyword org-deadline-string
)
963 (setq plist
(plist-put plist
:deadline time
)))
964 (t (setq plist
(plist-put plist
:closed time
))))))
966 (task-end (save-excursion
968 (and (re-search-forward "^\\*+ END" limit t
)
969 (match-beginning 0))))
970 (contents-begin (progn (forward-line)
971 (and task-end
(< (point) task-end
) (point))))
972 (hidden (and contents-begin
(org-invisible-p2)))
973 (contents-end (and contents-begin task-end
))
974 (before-blank (if (not task-end
) (point)
978 (end (progn (skip-chars-forward " \r\t\n" limit
)
979 (skip-chars-backward " \t")
980 (if (bolp) (point) (line-end-position))))
984 (list :raw-value raw-value
988 :contents-begin contents-begin
989 :contents-end contents-end
990 :level
(nth 1 components
)
991 :priority
(nth 3 components
)
995 :post-blank
(count-lines before-blank end
))
998 (org-element-put-property
1000 (if raw-secondary-p raw-value
1001 (org-element-parse-secondary-string
1003 (org-element-restriction 'inlinetask
)
1006 (defun org-element-inlinetask-interpreter (inlinetask contents
)
1007 "Interpret INLINETASK element as Org syntax.
1008 CONTENTS is the contents of inlinetask."
1009 (let* ((level (org-element-property :level inlinetask
))
1010 (todo (org-element-property :todo-keyword inlinetask
))
1011 (priority (org-element-property :priority inlinetask
))
1012 (title (org-element-interpret-data
1013 (org-element-property :title inlinetask
)))
1014 (tags (let ((tag-list (org-element-property :tags inlinetask
)))
1016 (format ":%s:" (mapconcat 'identity tag-list
":")))))
1017 (task (concat (make-string level ?
*)
1018 (and todo
(concat " " todo
))
1020 (format " [#%s]" (char-to-string priority
)))
1021 (and title
(concat " " title
)))))
1026 ((zerop org-tags-column
) (format " %s" tags
))
1027 ((< org-tags-column
0)
1030 (max (- (+ org-tags-column
(length task
) (length tags
))) 1)
1035 (make-string (max (- org-tags-column
(length task
)) 1) ?
)
1037 ;; Prefer degenerate inlinetasks when there are no
1042 (make-string level ?
*) " END")))))
1047 (defun org-element-item-parser (limit struct
&optional raw-secondary-p
)
1050 STRUCT is the structure of the plain list.
1052 Return a list whose CAR is `item' and CDR is a plist containing
1053 `:bullet', `:begin', `:end', `:contents-begin', `:contents-end',
1054 `:checkbox', `:counter', `:tag', `:structure', `:hiddenp' and
1055 `:post-blank' keywords.
1057 When optional argument RAW-SECONDARY-P is non-nil, item's tag, if
1058 any, will not be parsed as a secondary string, but as a plain
1061 Assume point is at the beginning of the item."
1064 (looking-at org-list-full-item-re
)
1065 (let* ((begin (point))
1066 (bullet (org-match-string-no-properties 1))
1067 (checkbox (let ((box (org-match-string-no-properties 3)))
1068 (cond ((equal "[ ]" box
) 'off
)
1069 ((equal "[X]" box
) 'on
)
1070 ((equal "[-]" box
) 'trans
))))
1071 (counter (let ((c (org-match-string-no-properties 2)))
1075 ((string-match "[A-Za-z]" c
)
1076 (- (string-to-char (upcase (match-string 0 c
)))
1078 ((string-match "[0-9]+" c
)
1079 (string-to-number (match-string 0 c
)))))))
1080 (end (save-excursion (goto-char (org-list-get-item-end begin struct
))
1081 (unless (bolp) (forward-line))
1085 ;; Ignore tags in un-ordered lists: they are just
1086 ;; a part of item's body.
1087 (if (and (match-beginning 4)
1088 (save-match-data (string-match "[.)]" bullet
)))
1091 (skip-chars-forward " \r\t\n" limit
)
1092 ;; If first line isn't empty, contents really start
1093 ;; at the text after item's meta-data.
1094 (if (= (point-at-bol) begin
) (point) (point-at-bol))))
1095 (hidden (progn (forward-line)
1096 (and (not (= (point) end
)) (org-invisible-p2))))
1097 (contents-end (progn (goto-char end
)
1098 (skip-chars-backward " \r\t\n")
1103 (list :bullet bullet
1106 ;; CONTENTS-BEGIN and CONTENTS-END may be
1107 ;; mixed up in the case of an empty item
1108 ;; separated from the next by a blank line.
1109 ;; Thus ensure the former is always the
1111 :contents-begin
(min contents-begin contents-end
)
1112 :contents-end
(max contents-begin contents-end
)
1117 :post-blank
(count-lines contents-end end
)))))
1118 (org-element-put-property
1120 (let ((raw-tag (org-list-get-tag begin struct
)))
1122 (if raw-secondary-p raw-tag
1123 (org-element-parse-secondary-string
1124 raw-tag
(org-element-restriction 'item
) item
))))))))
1126 (defun org-element-item-interpreter (item contents
)
1127 "Interpret ITEM element as Org syntax.
1128 CONTENTS is the contents of the element."
1129 (let* ((bullet (org-list-bullet-string (org-element-property :bullet item
)))
1130 (checkbox (org-element-property :checkbox item
))
1131 (counter (org-element-property :counter item
))
1132 (tag (let ((tag (org-element-property :tag item
)))
1133 (and tag
(org-element-interpret-data tag
))))
1134 ;; Compute indentation.
1135 (ind (make-string (length bullet
) 32))
1136 (item-starts-with-par-p
1137 (eq (org-element-type (car (org-element-contents item
)))
1142 (and counter
(format "[@%d] " counter
))
1147 (and tag
(format "%s :: " tag
))
1148 (let ((contents (replace-regexp-in-string
1149 "\\(^\\)[ \t]*\\S-" ind contents nil nil
1)))
1150 (if item-starts-with-par-p
(org-trim contents
)
1151 (concat "\n" contents
))))))
1156 (defun org-element-plain-list-parser (limit affiliated structure
)
1157 "Parse a plain list.
1159 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1160 the buffer position at the beginning of the first affiliated
1161 keyword and CDR is a plist of affiliated keywords along with
1162 their value. STRUCTURE is the structure of the plain list being
1165 Return a list whose CAR is `plain-list' and CDR is a plist
1166 containing `:type', `:begin', `:end', `:contents-begin' and
1167 `:contents-end', `:structure', `:post-blank' and
1168 `:post-affiliated' keywords.
1170 Assume point is at the beginning of the list."
1172 (let* ((struct (or structure
(org-list-struct)))
1173 (prevs (org-list-prevs-alist struct
))
1174 (parents (org-list-parents-alist struct
))
1175 (type (org-list-get-list-type (point) struct prevs
))
1176 (contents-begin (point))
1177 (begin (car affiliated
))
1179 (progn (goto-char (org-list-get-list-end (point) struct prevs
))
1180 (unless (bolp) (forward-line))
1182 (end (progn (skip-chars-forward " \r\t\n" limit
)
1183 (skip-chars-backward " \t")
1184 (if (bolp) (point) (line-end-position)))))
1191 :contents-begin contents-begin
1192 :contents-end contents-end
1194 :post-blank
(count-lines contents-end end
)
1195 :post-affiliated contents-begin
)
1196 (cdr affiliated
))))))
1198 (defun org-element-plain-list-interpreter (plain-list contents
)
1199 "Interpret PLAIN-LIST element as Org syntax.
1200 CONTENTS is the contents of the element."
1203 (goto-char (point-min))
1208 ;;;; Property Drawer
1210 (defun org-element-property-drawer-parser (limit affiliated
)
1211 "Parse a property drawer.
1213 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1214 the buffer position at the beginning of the first affiliated
1215 keyword and CDR is a plist of affiliated keywords along with
1218 Return a list whose CAR is `property-drawer' and CDR is a plist
1219 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1220 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
1222 Assume point is at the beginning of the property drawer."
1224 (let ((case-fold-search t
))
1225 (if (not (save-excursion
1226 (re-search-forward "^[ \t]*:END:[ \t]*$" limit t
)))
1227 ;; Incomplete drawer: parse it as a paragraph.
1228 (org-element-paragraph-parser limit affiliated
)
1230 (let* ((drawer-end-line (match-beginning 0))
1231 (begin (car affiliated
))
1232 (post-affiliated (point))
1233 (contents-begin (progn (forward-line)
1234 (and (< (point) drawer-end-line
)
1236 (contents-end (and contents-begin drawer-end-line
))
1237 (hidden (org-invisible-p2))
1238 (pos-before-blank (progn (goto-char drawer-end-line
)
1241 (end (progn (skip-chars-forward " \r\t\n" limit
)
1242 (skip-chars-backward " \t")
1243 (if (bolp) (point) (line-end-position)))))
1244 (list 'property-drawer
1249 :contents-begin contents-begin
1250 :contents-end contents-end
1251 :post-blank
(count-lines pos-before-blank end
)
1252 :post-affiliated post-affiliated
)
1253 (cdr affiliated
)))))))))
1255 (defun org-element-property-drawer-interpreter (property-drawer contents
)
1256 "Interpret PROPERTY-DRAWER element as Org syntax.
1257 CONTENTS is the properties within the drawer."
1258 (format ":PROPERTIES:\n%s:END:" contents
))
1263 (defun org-element-quote-block-parser (limit affiliated
)
1264 "Parse a quote block.
1266 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1267 the buffer position at the beginning of the first affiliated
1268 keyword and CDR is a plist of affiliated keywords along with
1271 Return a list whose CAR is `quote-block' and CDR is a plist
1272 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1273 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
1275 Assume point is at the beginning of the block."
1276 (let ((case-fold-search t
))
1277 (if (not (save-excursion
1278 (re-search-forward "^[ \t]*#\\+END_QUOTE[ \t]*$" limit t
)))
1279 ;; Incomplete block: parse it as a paragraph.
1280 (org-element-paragraph-parser limit affiliated
)
1281 (let ((block-end-line (match-beginning 0)))
1283 (let* ((begin (car affiliated
))
1284 (post-affiliated (point))
1285 ;; Empty blocks have no contents.
1286 (contents-begin (progn (forward-line)
1287 (and (< (point) block-end-line
)
1289 (contents-end (and contents-begin block-end-line
))
1290 (hidden (org-invisible-p2))
1291 (pos-before-blank (progn (goto-char block-end-line
)
1294 (end (progn (skip-chars-forward " \r\t\n" limit
)
1295 (skip-chars-backward " \t")
1296 (if (bolp) (point) (line-end-position)))))
1302 :contents-begin contents-begin
1303 :contents-end contents-end
1304 :post-blank
(count-lines pos-before-blank end
)
1305 :post-affiliated post-affiliated
)
1306 (cdr affiliated
)))))))))
1308 (defun org-element-quote-block-interpreter (quote-block contents
)
1309 "Interpret QUOTE-BLOCK element as Org syntax.
1310 CONTENTS is the contents of the element."
1311 (format "#+BEGIN_QUOTE\n%s#+END_QUOTE" contents
))
1316 (defun org-element-section-parser (limit)
1319 LIMIT bounds the search.
1321 Return a list whose CAR is `section' and CDR is a plist
1322 containing `:begin', `:end', `:contents-begin', `contents-end'
1323 and `:post-blank' keywords."
1325 ;; Beginning of section is the beginning of the first non-blank
1326 ;; line after previous headline.
1327 (let ((begin (point))
1328 (end (progn (org-with-limited-levels (outline-next-heading))
1330 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
1336 :contents-begin begin
1337 :contents-end pos-before-blank
1338 :post-blank
(count-lines pos-before-blank end
))))))
1340 (defun org-element-section-interpreter (section contents
)
1341 "Interpret SECTION element as Org syntax.
1342 CONTENTS is the contents of the element."
1348 (defun org-element-special-block-parser (limit affiliated
)
1349 "Parse a special block.
1351 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1352 the buffer position at the beginning of the first affiliated
1353 keyword and CDR is a plist of affiliated keywords along with
1356 Return a list whose CAR is `special-block' and CDR is a plist
1357 containing `:type', `:begin', `:end', `:hiddenp',
1358 `:contents-begin', `:contents-end', `:post-blank' and
1359 `:post-affiliated' keywords.
1361 Assume point is at the beginning of the block."
1362 (let* ((case-fold-search t
)
1363 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(S-+\\)")
1364 (upcase (match-string-no-properties 1)))))
1365 (if (not (save-excursion
1367 (format "^[ \t]*#\\+END_%s[ \t]*$" type
) limit t
)))
1368 ;; Incomplete block: parse it as a paragraph.
1369 (org-element-paragraph-parser limit affiliated
)
1370 (let ((block-end-line (match-beginning 0)))
1372 (let* ((begin (car affiliated
))
1373 (post-affiliated (point))
1374 ;; Empty blocks have no contents.
1375 (contents-begin (progn (forward-line)
1376 (and (< (point) block-end-line
)
1378 (contents-end (and contents-begin block-end-line
))
1379 (hidden (org-invisible-p2))
1380 (pos-before-blank (progn (goto-char block-end-line
)
1383 (end (progn (skip-chars-forward " \r\t\n" limit
)
1384 (skip-chars-backward " \t")
1385 (if (bolp) (point) (line-end-position)))))
1386 (list 'special-block
1392 :contents-begin contents-begin
1393 :contents-end contents-end
1394 :post-blank
(count-lines pos-before-blank end
)
1395 :post-affiliated post-affiliated
)
1396 (cdr affiliated
)))))))))
1398 (defun org-element-special-block-interpreter (special-block contents
)
1399 "Interpret SPECIAL-BLOCK element as Org syntax.
1400 CONTENTS is the contents of the element."
1401 (let ((block-type (org-element-property :type special-block
)))
1402 (format "#+BEGIN_%s\n%s#+END_%s" block-type contents block-type
)))
1408 ;; For each element, a parser and an interpreter are also defined.
1409 ;; Both follow the same naming convention used for greater elements.
1411 ;; Also, as for greater elements, adding a new element type is done
1412 ;; through the following steps: implement a parser and an interpreter,
1413 ;; tweak `org-element--current-element' so that it recognizes the new
1414 ;; type and add that new type to `org-element-all-elements'.
1416 ;; As a special case, when the newly defined type is a block type,
1417 ;; `org-element-block-name-alist' has to be modified accordingly.
1422 (defun org-element-babel-call-parser (limit affiliated
)
1423 "Parse a babel call.
1425 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1426 the buffer position at the beginning of the first affiliated
1427 keyword and CDR is a plist of affiliated keywords along with
1430 Return a list whose CAR is `babel-call' and CDR is a plist
1431 containing `:begin', `:end', `:info', `:post-blank' and
1432 `:post-affiliated' as keywords."
1434 (let ((case-fold-search t
)
1435 (info (progn (looking-at org-babel-block-lob-one-liner-regexp
)
1436 (org-babel-lob-get-info)))
1437 (begin (car affiliated
))
1438 (post-affiliated (point))
1439 (pos-before-blank (progn (forward-line) (point)))
1440 (end (progn (skip-chars-forward " \r\t\n" limit
)
1441 (skip-chars-backward " \t")
1442 (if (bolp) (point) (line-end-position)))))
1448 :post-blank
(count-lines pos-before-blank end
)
1449 :post-affiliated post-affiliated
)
1450 (cdr affiliated
))))))
1452 (defun org-element-babel-call-interpreter (babel-call contents
)
1453 "Interpret BABEL-CALL element as Org syntax.
1455 (let* ((babel-info (org-element-property :info babel-call
))
1456 (main (car babel-info
))
1457 (post-options (nth 1 babel-info
)))
1459 (if (not (string-match "\\[\\(\\[.*?\\]\\)\\]" main
)) main
1460 ;; Remove redundant square brackets.
1461 (replace-match (match-string 1 main
) nil nil main
))
1462 (and post-options
(format "[%s]" post-options
)))))
1467 (defun org-element-clock-parser (limit)
1470 LIMIT bounds the search.
1472 Return a list whose CAR is `clock' and CDR is a plist containing
1473 `:status', `:value', `:time', `:begin', `:end' and `:post-blank'
1476 (let* ((case-fold-search nil
)
1478 (value (progn (search-forward org-clock-string
(line-end-position) t
)
1479 (skip-chars-forward " \t")
1480 (org-element-timestamp-parser)))
1481 (duration (and (search-forward " => " (line-end-position) t
)
1482 (progn (skip-chars-forward " \t")
1483 (looking-at "\\(\\S-+\\)[ \t]*$"))
1484 (org-match-string-no-properties 1)))
1485 (status (if duration
'closed
'running
))
1486 (post-blank (let ((before-blank (progn (forward-line) (point))))
1487 (skip-chars-forward " \r\t\n" limit
)
1488 (skip-chars-backward " \t")
1489 (unless (bolp) (end-of-line))
1490 (count-lines before-blank
(point))))
1493 (list :status status
1498 :post-blank post-blank
)))))
1500 (defun org-element-clock-interpreter (clock contents
)
1501 "Interpret CLOCK element as Org syntax.
1503 (concat org-clock-string
" "
1504 (org-element-timestamp-interpreter
1505 (org-element-property :value clock
) nil
)
1506 (let ((duration (org-element-property :duration clock
)))
1511 (org-split-string duration
":")))))))
1516 (defun org-element-comment-parser (limit affiliated
)
1519 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1520 the buffer position at the beginning of the first affiliated
1521 keyword and CDR is a plist of affiliated keywords along with
1524 Return a list whose CAR is `comment' and CDR is a plist
1525 containing `:begin', `:end', `:value', `:post-blank',
1526 `:post-affiliated' keywords.
1528 Assume point is at comment beginning."
1530 (let* ((begin (car affiliated
))
1531 (post-affiliated (point))
1532 (value (prog2 (looking-at "[ \t]*# ?")
1533 (buffer-substring-no-properties
1534 (match-end 0) (line-end-position))
1537 ;; Get comments ending.
1539 (while (and (< (point) limit
) (looking-at "[ \t]*#\\( \\|$\\)"))
1540 ;; Accumulate lines without leading hash and first
1545 (buffer-substring-no-properties
1546 (match-end 0) (line-end-position))))
1549 (end (progn (goto-char com-end
)
1550 (skip-chars-forward " \r\t\n" limit
)
1551 (skip-chars-backward " \t")
1552 (if (bolp) (point) (line-end-position)))))
1558 :post-blank
(count-lines com-end end
)
1559 :post-affiliated post-affiliated
)
1560 (cdr affiliated
))))))
1562 (defun org-element-comment-interpreter (comment contents
)
1563 "Interpret COMMENT element as Org syntax.
1565 (replace-regexp-in-string "^" "# " (org-element-property :value comment
)))
1570 (defun org-element-comment-block-parser (limit affiliated
)
1571 "Parse an export block.
1573 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1574 the buffer position at the beginning of the first affiliated
1575 keyword and CDR is a plist of affiliated keywords along with
1578 Return a list whose CAR is `comment-block' and CDR is a plist
1579 containing `:begin', `:end', `:hiddenp', `:value', `:post-blank'
1580 and `:post-affiliated' keywords.
1582 Assume point is at comment block beginning."
1583 (let ((case-fold-search t
))
1584 (if (not (save-excursion
1585 (re-search-forward "^[ \t]*#\\+END_COMMENT[ \t]*$" limit t
)))
1586 ;; Incomplete block: parse it as a paragraph.
1587 (org-element-paragraph-parser limit affiliated
)
1588 (let ((contents-end (match-beginning 0)))
1590 (let* ((begin (car affiliated
))
1591 (post-affiliated (point))
1592 (contents-begin (progn (forward-line) (point)))
1593 (hidden (org-invisible-p2))
1594 (pos-before-blank (progn (goto-char contents-end
)
1597 (end (progn (skip-chars-forward " \r\t\n" limit
)
1598 (skip-chars-backward " \t")
1599 (if (bolp) (point) (line-end-position))))
1600 (value (buffer-substring-no-properties
1601 contents-begin contents-end
)))
1602 (list 'comment-block
1608 :post-blank
(count-lines pos-before-blank end
)
1609 :post-affiliated post-affiliated
)
1610 (cdr affiliated
)))))))))
1612 (defun org-element-comment-block-interpreter (comment-block contents
)
1613 "Interpret COMMENT-BLOCK element as Org syntax.
1615 (format "#+BEGIN_COMMENT\n%s#+END_COMMENT"
1616 (org-remove-indentation (org-element-property :value comment-block
))))
1621 (defun org-element-diary-sexp-parser (limit affiliated
)
1622 "Parse a diary sexp.
1624 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1625 the buffer position at the beginning of the first affiliated
1626 keyword and CDR is a plist of affiliated keywords along with
1629 Return a list whose CAR is `diary-sexp' and CDR is a plist
1630 containing `:begin', `:end', `:value', `:post-blank' and
1631 `:post-affiliated' keywords."
1633 (let ((begin (car affiliated
))
1634 (post-affiliated (point))
1635 (value (progn (looking-at "\\(%%(.*\\)[ \t]*$")
1636 (org-match-string-no-properties 1)))
1637 (pos-before-blank (progn (forward-line) (point)))
1638 (end (progn (skip-chars-forward " \r\t\n" limit
)
1639 (skip-chars-backward " \t")
1640 (if (bolp) (point) (line-end-position)))))
1646 :post-blank
(count-lines pos-before-blank end
)
1647 :post-affiliated post-affiliated
)
1648 (cdr affiliated
))))))
1650 (defun org-element-diary-sexp-interpreter (diary-sexp contents
)
1651 "Interpret DIARY-SEXP as Org syntax.
1653 (org-element-property :value diary-sexp
))
1658 (defun org-element-example-block-parser (limit affiliated
)
1659 "Parse an example block.
1661 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1662 the buffer position at the beginning of the first affiliated
1663 keyword and CDR is a plist of affiliated keywords along with
1666 Return a list whose CAR is `example-block' and CDR is a plist
1667 containing `:begin', `:end', `:number-lines', `:preserve-indent',
1668 `:retain-labels', `:use-labels', `:label-fmt', `:hiddenp',
1669 `:switches', `:value', `:post-blank' and `:post-affiliated'
1671 (let ((case-fold-search t
))
1672 (if (not (save-excursion
1673 (re-search-forward "^[ \t]*#\\+END_EXAMPLE[ \t]*$" limit t
)))
1674 ;; Incomplete block: parse it as a paragraph.
1675 (org-element-paragraph-parser limit affiliated
)
1676 (let ((contents-end (match-beginning 0)))
1679 (progn (looking-at "^[ \t]*#\\+BEGIN_EXAMPLE\\(?: +\\(.*\\)\\)?")
1680 (org-match-string-no-properties 1)))
1681 ;; Switches analysis
1682 (number-lines (cond ((not switches
) nil
)
1683 ((string-match "-n\\>" switches
) 'new
)
1684 ((string-match "+n\\>" switches
) 'continued
)))
1685 (preserve-indent (and switches
(string-match "-i\\>" switches
)))
1686 ;; Should labels be retained in (or stripped from) example
1690 (not (string-match "-r\\>" switches
))
1691 (and number-lines
(string-match "-k\\>" switches
))))
1692 ;; What should code-references use - labels or
1696 (and retain-labels
(not (string-match "-k\\>" switches
)))))
1697 (label-fmt (and switches
1698 (string-match "-l +\"\\([^\"\n]+\\)\"" switches
)
1699 (match-string 1 switches
)))
1700 ;; Standard block parsing.
1701 (begin (car affiliated
))
1702 (post-affiliated (point))
1703 (contents-begin (progn (forward-line) (point)))
1704 (hidden (org-invisible-p2))
1705 (value (org-unescape-code-in-string
1706 (buffer-substring-no-properties
1707 contents-begin contents-end
)))
1708 (pos-before-blank (progn (goto-char contents-end
)
1711 (end (progn (skip-chars-forward " \r\t\n" limit
)
1712 (skip-chars-backward " \t")
1713 (if (bolp) (point) (line-end-position)))))
1714 (list 'example-block
1720 :number-lines number-lines
1721 :preserve-indent preserve-indent
1722 :retain-labels retain-labels
1723 :use-labels use-labels
1724 :label-fmt label-fmt
1726 :post-blank
(count-lines pos-before-blank end
)
1727 :post-affiliated post-affiliated
)
1728 (cdr affiliated
)))))))))
1730 (defun org-element-example-block-interpreter (example-block contents
)
1731 "Interpret EXAMPLE-BLOCK element as Org syntax.
1733 (let ((switches (org-element-property :switches example-block
)))
1734 (concat "#+BEGIN_EXAMPLE" (and switches
(concat " " switches
)) "\n"
1735 (org-remove-indentation
1736 (org-escape-code-in-string
1737 (org-element-property :value example-block
)))
1743 (defun org-element-export-block-parser (limit affiliated
)
1744 "Parse an export block.
1746 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1747 the buffer position at the beginning of the first affiliated
1748 keyword and CDR is a plist of affiliated keywords along with
1751 Return a list whose CAR is `export-block' and CDR is a plist
1752 containing `:begin', `:end', `:type', `:hiddenp', `:value',
1753 `:post-blank' and `:post-affiliated' keywords.
1755 Assume point is at export-block beginning."
1756 (let* ((case-fold-search t
)
1757 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
1758 (upcase (org-match-string-no-properties 1)))))
1759 (if (not (save-excursion
1761 (format "^[ \t]*#\\+END_%s[ \t]*$" type
) limit t
)))
1762 ;; Incomplete block: parse it as a paragraph.
1763 (org-element-paragraph-parser limit affiliated
)
1764 (let ((contents-end (match-beginning 0)))
1766 (let* ((begin (car affiliated
))
1767 (post-affiliated (point))
1768 (contents-begin (progn (forward-line) (point)))
1769 (hidden (org-invisible-p2))
1770 (pos-before-blank (progn (goto-char contents-end
)
1773 (end (progn (skip-chars-forward " \r\t\n" limit
)
1774 (skip-chars-backward " \t")
1775 (if (bolp) (point) (line-end-position))))
1776 (value (buffer-substring-no-properties contents-begin
1785 :post-blank
(count-lines pos-before-blank end
)
1786 :post-affiliated post-affiliated
)
1787 (cdr affiliated
)))))))))
1789 (defun org-element-export-block-interpreter (export-block contents
)
1790 "Interpret EXPORT-BLOCK element as Org syntax.
1792 (let ((type (org-element-property :type export-block
)))
1793 (concat (format "#+BEGIN_%s\n" type
)
1794 (org-element-property :value export-block
)
1795 (format "#+END_%s" type
))))
1800 (defun org-element-fixed-width-parser (limit affiliated
)
1801 "Parse a fixed-width section.
1803 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1804 the buffer position at the beginning of the first affiliated
1805 keyword and CDR is a plist of affiliated keywords along with
1808 Return a list whose CAR is `fixed-width' and CDR is a plist
1809 containing `:begin', `:end', `:value', `:post-blank' and
1810 `:post-affiliated' keywords.
1812 Assume point is at the beginning of the fixed-width area."
1814 (let* ((begin (car affiliated
))
1815 (post-affiliated (point))
1819 (while (and (< (point) limit
)
1820 (looking-at "[ \t]*:\\( \\|$\\)"))
1821 ;; Accumulate text without starting colons.
1824 (buffer-substring-no-properties
1825 (match-end 0) (point-at-eol))
1829 (end (progn (skip-chars-forward " \r\t\n" limit
)
1830 (skip-chars-backward " \t")
1831 (if (bolp) (point) (line-end-position)))))
1837 :post-blank
(count-lines end-area end
)
1838 :post-affiliated post-affiliated
)
1839 (cdr affiliated
))))))
1841 (defun org-element-fixed-width-interpreter (fixed-width contents
)
1842 "Interpret FIXED-WIDTH element as Org syntax.
1844 (replace-regexp-in-string
1845 "^" ": " (substring (org-element-property :value fixed-width
) 0 -
1)))
1848 ;;;; Horizontal Rule
1850 (defun org-element-horizontal-rule-parser (limit affiliated
)
1851 "Parse an horizontal rule.
1853 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1854 the buffer position at the beginning of the first affiliated
1855 keyword and CDR is a plist of affiliated keywords along with
1858 Return a list whose CAR is `horizontal-rule' and CDR is a plist
1859 containing `:begin', `:end', `:post-blank' and `:post-affiliated'
1862 (let ((begin (car affiliated
))
1863 (post-affiliated (point))
1864 (post-hr (progn (forward-line) (point)))
1865 (end (progn (skip-chars-forward " \r\t\n" limit
)
1866 (skip-chars-backward " \t")
1867 (if (bolp) (point) (line-end-position)))))
1868 (list 'horizontal-rule
1872 :post-blank
(count-lines post-hr end
)
1873 :post-affiliated post-affiliated
)
1874 (cdr affiliated
))))))
1876 (defun org-element-horizontal-rule-interpreter (horizontal-rule contents
)
1877 "Interpret HORIZONTAL-RULE element as Org syntax.
1884 (defun org-element-keyword-parser (limit affiliated
)
1885 "Parse a keyword at point.
1887 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1888 the buffer position at the beginning of the first affiliated
1889 keyword and CDR is a plist of affiliated keywords along with
1892 Return a list whose CAR is `keyword' and CDR is a plist
1893 containing `:key', `:value', `:begin', `:end', `:post-blank' and
1894 `:post-affiliated' keywords."
1896 (let ((begin (car affiliated
))
1897 (post-affiliated (point))
1898 (key (progn (looking-at "[ \t]*#\\+\\(\\S-+*\\):")
1899 (upcase (org-match-string-no-properties 1))))
1900 (value (org-trim (buffer-substring-no-properties
1901 (match-end 0) (point-at-eol))))
1902 (pos-before-blank (progn (forward-line) (point)))
1903 (end (progn (skip-chars-forward " \r\t\n" limit
)
1904 (skip-chars-backward " \t")
1905 (if (bolp) (point) (line-end-position)))))
1912 :post-blank
(count-lines pos-before-blank end
)
1913 :post-affiliated post-affiliated
)
1914 (cdr affiliated
))))))
1916 (defun org-element-keyword-interpreter (keyword contents
)
1917 "Interpret KEYWORD element as Org syntax.
1920 (org-element-property :key keyword
)
1921 (org-element-property :value keyword
)))
1924 ;;;; Latex Environment
1926 (defun org-element-latex-environment-parser (limit affiliated
)
1927 "Parse a LaTeX environment.
1929 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1930 the buffer position at the beginning of the first affiliated
1931 keyword and CDR is a plist of affiliated keywords along with
1934 Return a list whose CAR is `latex-environment' and CDR is a plist
1935 containing `:begin', `:end', `:value', `:post-blank' and
1936 `:post-affiliated' keywords.
1938 Assume point is at the beginning of the latex environment."
1940 (let ((case-fold-search t
)
1941 (code-begin (point)))
1942 (looking-at "[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}")
1943 (if (not (re-search-forward (format "^[ \t]*\\\\end{%s}[ \t]*$"
1944 (regexp-quote (match-string 1)))
1946 ;; Incomplete latex environment: parse it as a paragraph.
1947 (org-element-paragraph-parser limit affiliated
)
1948 (let* ((code-end (progn (forward-line) (point)))
1949 (begin (car affiliated
))
1950 (post-affiliated (point))
1951 (value (buffer-substring-no-properties code-begin code-end
))
1952 (end (progn (skip-chars-forward " \r\t\n" limit
)
1953 (skip-chars-backward " \t")
1954 (if (bolp) (point) (line-end-position)))))
1955 (list 'latex-environment
1960 :post-blank
(count-lines code-end end
)
1961 :post-affiliated post-affiliated
)
1962 (cdr affiliated
))))))))
1964 (defun org-element-latex-environment-interpreter (latex-environment contents
)
1965 "Interpret LATEX-ENVIRONMENT element as Org syntax.
1967 (org-element-property :value latex-environment
))
1972 (defun org-element-node-property-parser (limit)
1973 "Parse a node-property at point.
1975 LIMIT bounds the search.
1977 Return a list whose CAR is `node-property' and CDR is a plist
1978 containing `:key', `:value', `:begin', `:end' and `:post-blank'
1981 (let ((case-fold-search t
)
1983 (key (progn (looking-at "[ \t]*:\\(.*?\\):[ \t]+\\(.*?\\)[ \t]*$")
1984 (org-match-string-no-properties 1)))
1985 (value (org-match-string-no-properties 2))
1986 (pos-before-blank (progn (forward-line) (point)))
1987 (end (progn (skip-chars-forward " \r\t\n" limit
)
1988 (if (eobp) (point) (point-at-bol)))))
1989 (list 'node-property
1994 :post-blank
(count-lines pos-before-blank end
))))))
1996 (defun org-element-node-property-interpreter (node-property contents
)
1997 "Interpret NODE-PROPERTY element as Org syntax.
1999 (format org-property-format
2000 (format ":%s:" (org-element-property :key node-property
))
2001 (org-element-property :value node-property
)))
2006 (defun org-element-paragraph-parser (limit affiliated
)
2009 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2010 the buffer position at the beginning of the first affiliated
2011 keyword and CDR is a plist of affiliated keywords along with
2014 Return a list whose CAR is `paragraph' and CDR is a plist
2015 containing `:begin', `:end', `:contents-begin' and
2016 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
2018 Assume point is at the beginning of the paragraph."
2020 (let* ((begin (car affiliated
))
2021 (contents-begin (point))
2023 (let ((case-fold-search t
))
2025 (if (not (re-search-forward
2026 org-element-paragraph-separate limit
'm
))
2028 ;; A matching `org-element-paragraph-separate' is not
2029 ;; necessarily the end of the paragraph. In
2030 ;; particular, lines starting with # or : as a first
2031 ;; non-space character are ambiguous. We have check
2032 ;; if they are valid Org syntax (i.e. not an
2033 ;; incomplete keyword).
2037 ;; There's no ambiguity for other symbols or
2038 ;; empty lines: stop here.
2039 (looking-at "[ \t]*\\(?:[^:#]\\|$\\)")
2040 ;; Stop at valid fixed-width areas.
2041 (looking-at "[ \t]*:\\(?: \\|$\\)")
2043 (and (looking-at org-drawer-regexp
)
2046 "^[ \t]*:END:[ \t]*$" limit t
)))
2047 ;; Stop at valid comments.
2048 (looking-at "[ \t]*#\\(?: \\|$\\)")
2049 ;; Stop at valid dynamic blocks.
2050 (and (looking-at org-dblock-start-re
)
2053 "^[ \t]*#\\+END:?[ \t]*$" limit t
)))
2054 ;; Stop at valid blocks.
2056 "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
2059 (format "^[ \t]*#\\+END_%s[ \t]*$"
2062 ;; Stop at valid latex environments.
2064 "^[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}[ \t]*$")
2067 (format "^[ \t]*\\\\end{%s}[ \t]*$"
2070 ;; Stop at valid keywords.
2071 (looking-at "[ \t]*#\\+\\S-+:")
2072 ;; Skip everything else.
2076 (re-search-forward org-element-paragraph-separate
2078 (beginning-of-line)))
2079 (if (= (point) limit
) limit
2080 (goto-char (line-beginning-position)))))
2081 (contents-end (progn (skip-chars-backward " \r\t\n" contents-begin
)
2084 (end (progn (skip-chars-forward " \r\t\n" limit
)
2085 (skip-chars-backward " \t")
2086 (if (bolp) (point) (line-end-position)))))
2091 :contents-begin contents-begin
2092 :contents-end contents-end
2093 :post-blank
(count-lines before-blank end
)
2094 :post-affiliated contents-begin
)
2095 (cdr affiliated
))))))
2097 (defun org-element-paragraph-interpreter (paragraph contents
)
2098 "Interpret PARAGRAPH element as Org syntax.
2099 CONTENTS is the contents of the element."
2105 (defun org-element-planning-parser (limit)
2108 LIMIT bounds the search.
2110 Return a list whose CAR is `planning' and CDR is a plist
2111 containing `:closed', `:deadline', `:scheduled', `:begin', `:end'
2112 and `:post-blank' keywords."
2114 (let* ((case-fold-search nil
)
2116 (post-blank (let ((before-blank (progn (forward-line) (point))))
2117 (skip-chars-forward " \r\t\n" limit
)
2118 (skip-chars-backward " \t")
2119 (unless (bolp) (end-of-line))
2120 (count-lines before-blank
(point))))
2122 closed deadline scheduled
)
2124 (while (re-search-forward org-keyword-time-not-clock-regexp end t
)
2125 (goto-char (match-end 1))
2126 (skip-chars-forward " \t" end
)
2127 (let ((keyword (match-string 1))
2128 (time (org-element-timestamp-parser)))
2129 (cond ((equal keyword org-closed-string
) (setq closed time
))
2130 ((equal keyword org-deadline-string
) (setq deadline time
))
2131 (t (setq scheduled time
)))))
2133 (list :closed closed
2135 :scheduled scheduled
2138 :post-blank post-blank
)))))
2140 (defun org-element-planning-interpreter (planning contents
)
2141 "Interpret PLANNING element as Org syntax.
2146 (list (let ((deadline (org-element-property :deadline planning
)))
2148 (concat org-deadline-string
" "
2149 (org-element-timestamp-interpreter deadline nil
))))
2150 (let ((scheduled (org-element-property :scheduled planning
)))
2152 (concat org-scheduled-string
" "
2153 (org-element-timestamp-interpreter scheduled nil
))))
2154 (let ((closed (org-element-property :closed planning
)))
2156 (concat org-closed-string
" "
2157 (org-element-timestamp-interpreter closed nil
))))))
2163 (defun org-element-quote-section-parser (limit)
2164 "Parse a quote section.
2166 LIMIT bounds the search.
2168 Return a list whose CAR is `quote-section' and CDR is a plist
2169 containing `:begin', `:end', `:value' and `:post-blank' keywords.
2171 Assume point is at beginning of the section."
2173 (let* ((begin (point))
2174 (end (progn (org-with-limited-levels (outline-next-heading))
2176 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
2179 (value (buffer-substring-no-properties begin pos-before-blank
)))
2180 (list 'quote-section
2184 :post-blank
(count-lines pos-before-blank end
))))))
2186 (defun org-element-quote-section-interpreter (quote-section contents
)
2187 "Interpret QUOTE-SECTION element as Org syntax.
2189 (org-element-property :value quote-section
))
2194 (defun org-element-src-block-parser (limit affiliated
)
2197 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2198 the buffer position at the beginning of the first affiliated
2199 keyword and CDR is a plist of affiliated keywords along with
2202 Return a list whose CAR is `src-block' and CDR is a plist
2203 containing `:language', `:switches', `:parameters', `:begin',
2204 `:end', `:hiddenp', `:number-lines', `:retain-labels',
2205 `:use-labels', `:label-fmt', `:preserve-indent', `:value',
2206 `:post-blank' and `:post-affiliated' keywords.
2208 Assume point is at the beginning of the block."
2209 (let ((case-fold-search t
))
2210 (if (not (save-excursion (re-search-forward "^[ \t]*#\\+END_SRC[ \t]*$"
2212 ;; Incomplete block: parse it as a paragraph.
2213 (org-element-paragraph-parser limit affiliated
)
2214 (let ((contents-end (match-beginning 0)))
2216 (let* ((begin (car affiliated
))
2217 (post-affiliated (point))
2218 ;; Get language as a string.
2222 (concat "^[ \t]*#\\+BEGIN_SRC"
2223 "\\(?: +\\(\\S-+\\)\\)?"
2224 "\\(\\(?: +\\(?:-l \".*?\"\\|[-+][A-Za-z]\\)\\)+\\)?"
2226 (org-match-string-no-properties 1)))
2228 (switches (org-match-string-no-properties 2))
2230 (parameters (org-match-string-no-properties 3))
2231 ;; Switches analysis
2232 (number-lines (cond ((not switches
) nil
)
2233 ((string-match "-n\\>" switches
) 'new
)
2234 ((string-match "+n\\>" switches
) 'continued
)))
2235 (preserve-indent (and switches
(string-match "-i\\>" switches
)))
2236 (label-fmt (and switches
2237 (string-match "-l +\"\\([^\"\n]+\\)\"" switches
)
2238 (match-string 1 switches
)))
2239 ;; Should labels be retained in (or stripped from)
2243 (not (string-match "-r\\>" switches
))
2244 (and number-lines
(string-match "-k\\>" switches
))))
2245 ;; What should code-references use - labels or
2249 (and retain-labels
(not (string-match "-k\\>" switches
)))))
2250 ;; Get visibility status.
2251 (hidden (progn (forward-line) (org-invisible-p2)))
2253 (value (org-unescape-code-in-string
2254 (buffer-substring-no-properties (point) contents-end
)))
2255 (pos-before-blank (progn (goto-char contents-end
)
2258 ;; Get position after ending blank lines.
2259 (end (progn (skip-chars-forward " \r\t\n" limit
)
2260 (skip-chars-backward " \t")
2261 (if (bolp) (point) (line-end-position)))))
2264 (list :language language
2265 :switches
(and (org-string-nw-p switches
)
2266 (org-trim switches
))
2267 :parameters
(and (org-string-nw-p parameters
)
2268 (org-trim parameters
))
2271 :number-lines number-lines
2272 :preserve-indent preserve-indent
2273 :retain-labels retain-labels
2274 :use-labels use-labels
2275 :label-fmt label-fmt
2278 :post-blank
(count-lines pos-before-blank end
)
2279 :post-affiliated post-affiliated
)
2280 (cdr affiliated
)))))))))
2282 (defun org-element-src-block-interpreter (src-block contents
)
2283 "Interpret SRC-BLOCK element as Org syntax.
2285 (let ((lang (org-element-property :language src-block
))
2286 (switches (org-element-property :switches src-block
))
2287 (params (org-element-property :parameters src-block
))
2288 (value (let ((val (org-element-property :value src-block
)))
2290 (org-src-preserve-indentation val
)
2291 ((zerop org-edit-src-content-indentation
)
2292 (org-remove-indentation val
))
2294 (let ((ind (make-string
2295 org-edit-src-content-indentation
32)))
2296 (replace-regexp-in-string
2297 "\\(^\\)[ \t]*\\S-" ind
2298 (org-remove-indentation val
) nil nil
1)))))))
2299 (concat (format "#+BEGIN_SRC%s\n"
2300 (concat (and lang
(concat " " lang
))
2301 (and switches
(concat " " switches
))
2302 (and params
(concat " " params
))))
2303 (org-escape-code-in-string value
)
2309 (defun org-element-table-parser (limit affiliated
)
2310 "Parse a table at point.
2312 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2313 the buffer position at the beginning of the first affiliated
2314 keyword and CDR is a plist of affiliated keywords along with
2317 Return a list whose CAR is `table' and CDR is a plist containing
2318 `:begin', `:end', `:tblfm', `:type', `:contents-begin',
2319 `:contents-end', `:value', `:post-blank' and `:post-affiliated'
2322 Assume point is at the beginning of the table."
2324 (let* ((case-fold-search t
)
2325 (table-begin (point))
2326 (type (if (org-at-table.el-p
) 'table.el
'org
))
2327 (begin (car affiliated
))
2329 (if (re-search-forward org-table-any-border-regexp limit
'm
)
2330 (goto-char (match-beginning 0))
2333 (while (looking-at "[ \t]*#\\+TBLFM: +\\(.*\\)[ \t]*$")
2334 (push (org-match-string-no-properties 1) acc
)
2337 (pos-before-blank (point))
2338 (end (progn (skip-chars-forward " \r\t\n" limit
)
2339 (skip-chars-backward " \t")
2340 (if (bolp) (point) (line-end-position)))))
2347 ;; Only `org' tables have contents. `table.el' tables
2348 ;; use a `:value' property to store raw table as
2350 :contents-begin
(and (eq type
'org
) table-begin
)
2351 :contents-end
(and (eq type
'org
) table-end
)
2352 :value
(and (eq type
'table.el
)
2353 (buffer-substring-no-properties
2354 table-begin table-end
))
2355 :post-blank
(count-lines pos-before-blank end
)
2356 :post-affiliated table-begin
)
2357 (cdr affiliated
))))))
2359 (defun org-element-table-interpreter (table contents
)
2360 "Interpret TABLE element as Org syntax.
2362 (if (eq (org-element-property :type table
) 'table.el
)
2363 (org-remove-indentation (org-element-property :value table
))
2364 (concat (with-temp-buffer (insert contents
)
2367 (mapconcat (lambda (fm) (concat "#+TBLFM: " fm
))
2368 (reverse (org-element-property :tblfm table
))
2374 (defun org-element-table-row-parser (limit)
2375 "Parse table row at point.
2377 LIMIT bounds the search.
2379 Return a list whose CAR is `table-row' and CDR is a plist
2380 containing `:begin', `:end', `:contents-begin', `:contents-end',
2381 `:type' and `:post-blank' keywords."
2383 (let* ((type (if (looking-at "^[ \t]*|-") 'rule
'standard
))
2385 ;; A table rule has no contents. In that case, ensure
2386 ;; CONTENTS-BEGIN matches CONTENTS-END.
2387 (contents-begin (and (eq type
'standard
)
2388 (search-forward "|")
2390 (contents-end (and (eq type
'standard
)
2393 (skip-chars-backward " \t")
2395 (end (progn (forward-line) (point))))
2400 :contents-begin contents-begin
2401 :contents-end contents-end
2404 (defun org-element-table-row-interpreter (table-row contents
)
2405 "Interpret TABLE-ROW element as Org syntax.
2406 CONTENTS is the contents of the table row."
2407 (if (eq (org-element-property :type table-row
) 'rule
) "|-"
2408 (concat "| " contents
)))
2413 (defun org-element-verse-block-parser (limit affiliated
)
2414 "Parse a verse block.
2416 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2417 the buffer position at the beginning of the first affiliated
2418 keyword and CDR is a plist of affiliated keywords along with
2421 Return a list whose CAR is `verse-block' and CDR is a plist
2422 containing `:begin', `:end', `:contents-begin', `:contents-end',
2423 `:hiddenp', `:post-blank' and `:post-affiliated' keywords.
2425 Assume point is at beginning of the block."
2426 (let ((case-fold-search t
))
2427 (if (not (save-excursion
2428 (re-search-forward "^[ \t]*#\\+END_VERSE[ \t]*$" limit t
)))
2429 ;; Incomplete block: parse it as a paragraph.
2430 (org-element-paragraph-parser limit affiliated
)
2431 (let ((contents-end (match-beginning 0)))
2433 (let* ((begin (car affiliated
))
2434 (post-affiliated (point))
2435 (hidden (progn (forward-line) (org-invisible-p2)))
2436 (contents-begin (point))
2437 (pos-before-blank (progn (goto-char contents-end
)
2440 (end (progn (skip-chars-forward " \r\t\n" limit
)
2441 (skip-chars-backward " \t")
2442 (if (bolp) (point) (line-end-position)))))
2447 :contents-begin contents-begin
2448 :contents-end contents-end
2450 :post-blank
(count-lines pos-before-blank end
)
2451 :post-affiliated post-affiliated
)
2452 (cdr affiliated
)))))))))
2454 (defun org-element-verse-block-interpreter (verse-block contents
)
2455 "Interpret VERSE-BLOCK element as Org syntax.
2456 CONTENTS is verse block contents."
2457 (format "#+BEGIN_VERSE\n%s#+END_VERSE" contents
))
2463 ;; Unlike to elements, interstices can be found between objects.
2464 ;; That's why, along with the parser, successor functions are provided
2465 ;; for each object. Some objects share the same successor (i.e. `code'
2466 ;; and `verbatim' objects).
2468 ;; A successor must accept a single argument bounding the search. It
2469 ;; will return either a cons cell whose CAR is the object's type, as
2470 ;; a symbol, and CDR the position of its next occurrence, or nil.
2472 ;; Successors follow the naming convention:
2473 ;; org-element-NAME-successor, where NAME is the name of the
2474 ;; successor, as defined in `org-element-all-successors'.
2476 ;; Some object types (i.e. `italic') are recursive. Restrictions on
2477 ;; object types they can contain will be specified in
2478 ;; `org-element-object-restrictions'.
2480 ;; Adding a new type of object is simple. Implement a successor,
2481 ;; a parser, and an interpreter for it, all following the naming
2482 ;; convention. Register type in `org-element-all-objects' and
2483 ;; successor in `org-element-all-successors'. Maybe tweak
2484 ;; restrictions about it, and that's it.
2489 (defun org-element-bold-parser ()
2490 "Parse bold object at point.
2492 Return a list whose CAR is `bold' and CDR is a plist with
2493 `:begin', `:end', `:contents-begin' and `:contents-end' and
2494 `:post-blank' keywords.
2496 Assume point is at the first star marker."
2498 (unless (bolp) (backward-char 1))
2499 (looking-at org-emph-re
)
2500 (let ((begin (match-beginning 2))
2501 (contents-begin (match-beginning 4))
2502 (contents-end (match-end 4))
2503 (post-blank (progn (goto-char (match-end 2))
2504 (skip-chars-forward " \t")))
2509 :contents-begin contents-begin
2510 :contents-end contents-end
2511 :post-blank post-blank
)))))
2513 (defun org-element-bold-interpreter (bold contents
)
2514 "Interpret BOLD object as Org syntax.
2515 CONTENTS is the contents of the object."
2516 (format "*%s*" contents
))
2518 (defun org-element-text-markup-successor (limit)
2519 "Search for the next text-markup object.
2521 LIMIT bounds the search.
2523 Return value is a cons cell whose CAR is a symbol among `bold',
2524 `italic', `underline', `strike-through', `code' and `verbatim'
2525 and CDR is beginning position."
2527 (unless (bolp) (backward-char))
2528 (when (re-search-forward org-emph-re limit t
)
2529 (let ((marker (match-string 3)))
2531 ((equal marker
"*") 'bold
)
2532 ((equal marker
"/") 'italic
)
2533 ((equal marker
"_") 'underline
)
2534 ((equal marker
"+") 'strike-through
)
2535 ((equal marker
"~") 'code
)
2536 ((equal marker
"=") 'verbatim
)
2537 (t (error "Unknown marker at %d" (match-beginning 3))))
2538 (match-beginning 2))))))
2543 (defun org-element-code-parser ()
2544 "Parse code object at point.
2546 Return a list whose CAR is `code' and CDR is a plist with
2547 `:value', `:begin', `:end' and `:post-blank' keywords.
2549 Assume point is at the first tilde marker."
2551 (unless (bolp) (backward-char 1))
2552 (looking-at org-emph-re
)
2553 (let ((begin (match-beginning 2))
2554 (value (org-match-string-no-properties 4))
2555 (post-blank (progn (goto-char (match-end 2))
2556 (skip-chars-forward " \t")))
2562 :post-blank post-blank
)))))
2564 (defun org-element-code-interpreter (code contents
)
2565 "Interpret CODE object as Org syntax.
2567 (format "~%s~" (org-element-property :value code
)))
2572 (defun org-element-entity-parser ()
2573 "Parse entity at point.
2575 Return a list whose CAR is `entity' and CDR a plist with
2576 `:begin', `:end', `:latex', `:latex-math-p', `:html', `:latin1',
2577 `:utf-8', `:ascii', `:use-brackets-p' and `:post-blank' as
2580 Assume point is at the beginning of the entity."
2582 (looking-at "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)")
2583 (let* ((value (org-entity-get (match-string 1)))
2584 (begin (match-beginning 0))
2585 (bracketsp (string= (match-string 2) "{}"))
2586 (post-blank (progn (goto-char (match-end 1))
2587 (when bracketsp
(forward-char 2))
2588 (skip-chars-forward " \t")))
2591 (list :name
(car value
)
2592 :latex
(nth 1 value
)
2593 :latex-math-p
(nth 2 value
)
2595 :ascii
(nth 4 value
)
2596 :latin1
(nth 5 value
)
2597 :utf-8
(nth 6 value
)
2600 :use-brackets-p bracketsp
2601 :post-blank post-blank
)))))
2603 (defun org-element-entity-interpreter (entity contents
)
2604 "Interpret ENTITY object as Org syntax.
2607 (org-element-property :name entity
)
2608 (when (org-element-property :use-brackets-p entity
) "{}")))
2610 (defun org-element-latex-or-entity-successor (limit)
2611 "Search for the next latex-fragment or entity object.
2613 LIMIT bounds the search.
2615 Return value is a cons cell whose CAR is `entity' or
2616 `latex-fragment' and CDR is beginning position."
2618 (unless (bolp) (backward-char))
2620 (remove "begin" (plist-get org-format-latex-options
:matchers
)))
2621 ;; ENTITY-RE matches both LaTeX commands and Org entities.
2623 "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))
2624 (when (re-search-forward
2625 (concat (mapconcat (lambda (e) (nth 1 (assoc e org-latex-regexps
)))
2629 (goto-char (match-beginning 0))
2630 (if (looking-at entity-re
)
2631 ;; Determine if it's a real entity or a LaTeX command.
2632 (cons (if (org-entity-get (match-string 1)) 'entity
'latex-fragment
)
2633 (match-beginning 0))
2634 ;; No entity nor command: point is at a LaTeX fragment.
2635 ;; Determine its type to get the correct beginning position.
2636 (cons 'latex-fragment
2639 (when (looking-at (nth 1 (assoc e org-latex-regexps
)))
2642 (nth 2 (assoc e org-latex-regexps
))))))
2649 (defun org-element-export-snippet-parser ()
2650 "Parse export snippet at point.
2652 Return a list whose CAR is `export-snippet' and CDR a plist with
2653 `:begin', `:end', `:back-end', `:value' and `:post-blank' as
2656 Assume point is at the beginning of the snippet."
2658 (re-search-forward "@@\\([-A-Za-z0-9]+\\):" nil t
)
2659 (let* ((begin (match-beginning 0))
2660 (back-end (org-match-string-no-properties 1))
2661 (value (buffer-substring-no-properties
2663 (progn (re-search-forward "@@" nil t
) (match-beginning 0))))
2664 (post-blank (skip-chars-forward " \t"))
2666 (list 'export-snippet
2667 (list :back-end back-end
2671 :post-blank post-blank
)))))
2673 (defun org-element-export-snippet-interpreter (export-snippet contents
)
2674 "Interpret EXPORT-SNIPPET object as Org syntax.
2677 (org-element-property :back-end export-snippet
)
2678 (org-element-property :value export-snippet
)))
2680 (defun org-element-export-snippet-successor (limit)
2681 "Search for the next export-snippet object.
2683 LIMIT bounds the search.
2685 Return value is a cons cell whose CAR is `export-snippet' and CDR
2686 its beginning position."
2689 (when (and (re-search-forward "@@[-A-Za-z0-9]+:" limit t
)
2690 (setq beg
(match-beginning 0))
2691 (search-forward "@@" limit t
))
2692 (cons 'export-snippet beg
)))))
2695 ;;;; Footnote Reference
2697 (defun org-element-footnote-reference-parser ()
2698 "Parse footnote reference at point.
2700 Return a list whose CAR is `footnote-reference' and CDR a plist
2701 with `:label', `:type', `:inline-definition', `:begin', `:end'
2702 and `:post-blank' as keywords."
2704 (looking-at org-footnote-re
)
2705 (let* ((begin (point))
2706 (label (or (org-match-string-no-properties 2)
2707 (org-match-string-no-properties 3)
2708 (and (match-string 1)
2709 (concat "fn:" (org-match-string-no-properties 1)))))
2710 (type (if (or (not label
) (match-string 1)) 'inline
'standard
))
2711 (inner-begin (match-end 0))
2715 (while (and (> count
0) (re-search-forward "[][]" nil t
))
2716 (if (equal (match-string 0) "[") (incf count
) (decf count
)))
2718 (post-blank (progn (goto-char (1+ inner-end
))
2719 (skip-chars-forward " \t")))
2722 (list 'footnote-reference
2727 :post-blank post-blank
))))
2728 (org-element-put-property
2729 footnote-reference
:inline-definition
2730 (and (eq type
'inline
)
2731 (org-element-parse-secondary-string
2732 (buffer-substring inner-begin inner-end
)
2733 (org-element-restriction 'footnote-reference
)
2734 footnote-reference
))))))
2736 (defun org-element-footnote-reference-interpreter (footnote-reference contents
)
2737 "Interpret FOOTNOTE-REFERENCE object as Org syntax.
2739 (let ((label (or (org-element-property :label footnote-reference
) "fn:"))
2742 (org-element-property :inline-definition footnote-reference
)))
2743 (if (not inline-def
) ""
2744 (concat ":" (org-element-interpret-data inline-def
))))))
2745 (format "[%s]" (concat label def
))))
2747 (defun org-element-footnote-reference-successor (limit)
2748 "Search for the next footnote-reference object.
2750 LIMIT bounds the search.
2752 Return value is a cons cell whose CAR is `footnote-reference' and
2753 CDR is beginning position."
2756 (while (re-search-forward org-footnote-re limit t
)
2758 (let ((beg (match-beginning 0))
2761 (while (re-search-forward "[][]" limit t
)
2762 (if (equal (match-string 0) "[") (incf count
) (decf count
))
2764 (throw 'exit
(cons 'footnote-reference beg
))))))))))
2767 ;;;; Inline Babel Call
2769 (defun org-element-inline-babel-call-parser ()
2770 "Parse inline babel call at point.
2772 Return a list whose CAR is `inline-babel-call' and CDR a plist
2773 with `:begin', `:end', `:info' and `:post-blank' as keywords.
2775 Assume point is at the beginning of the babel call."
2777 (unless (bolp) (backward-char))
2778 (looking-at org-babel-inline-lob-one-liner-regexp
)
2779 (let ((info (save-match-data (org-babel-lob-get-info)))
2780 (begin (match-end 1))
2781 (post-blank (progn (goto-char (match-end 0))
2782 (skip-chars-forward " \t")))
2784 (list 'inline-babel-call
2788 :post-blank post-blank
)))))
2790 (defun org-element-inline-babel-call-interpreter (inline-babel-call contents
)
2791 "Interpret INLINE-BABEL-CALL object as Org syntax.
2793 (let* ((babel-info (org-element-property :info inline-babel-call
))
2794 (main-source (car babel-info
))
2795 (post-options (nth 1 babel-info
)))
2797 (if (string-match "\\[\\(\\[.*?\\]\\)\\]" main-source
)
2798 ;; Remove redundant square brackets.
2800 (match-string 1 main-source
) nil nil main-source
)
2802 (and post-options
(format "[%s]" post-options
)))))
2804 (defun org-element-inline-babel-call-successor (limit)
2805 "Search for the next inline-babel-call object.
2807 LIMIT bounds the search.
2809 Return value is a cons cell whose CAR is `inline-babel-call' and
2810 CDR is beginning position."
2812 ;; Use a simplified version of
2813 ;; `org-babel-inline-lob-one-liner-regexp'.
2814 (when (re-search-forward
2815 "call_\\([^()\n]+?\\)\\(?:\\[.*?\\]\\)?([^\n]*?)\\(\\[.*?\\]\\)?"
2817 (cons 'inline-babel-call
(match-beginning 0)))))
2820 ;;;; Inline Src Block
2822 (defun org-element-inline-src-block-parser ()
2823 "Parse inline source block at point.
2825 LIMIT bounds the search.
2827 Return a list whose CAR is `inline-src-block' and CDR a plist
2828 with `:begin', `:end', `:language', `:value', `:parameters' and
2829 `:post-blank' as keywords.
2831 Assume point is at the beginning of the inline src block."
2833 (unless (bolp) (backward-char))
2834 (looking-at org-babel-inline-src-block-regexp
)
2835 (let ((begin (match-beginning 1))
2836 (language (org-match-string-no-properties 2))
2837 (parameters (org-match-string-no-properties 4))
2838 (value (org-match-string-no-properties 5))
2839 (post-blank (progn (goto-char (match-end 0))
2840 (skip-chars-forward " \t")))
2842 (list 'inline-src-block
2843 (list :language language
2845 :parameters parameters
2848 :post-blank post-blank
)))))
2850 (defun org-element-inline-src-block-interpreter (inline-src-block contents
)
2851 "Interpret INLINE-SRC-BLOCK object as Org syntax.
2853 (let ((language (org-element-property :language inline-src-block
))
2854 (arguments (org-element-property :parameters inline-src-block
))
2855 (body (org-element-property :value inline-src-block
)))
2856 (format "src_%s%s{%s}"
2858 (if arguments
(format "[%s]" arguments
) "")
2861 (defun org-element-inline-src-block-successor (limit)
2862 "Search for the next inline-babel-call element.
2864 LIMIT bounds the search.
2866 Return value is a cons cell whose CAR is `inline-babel-call' and
2867 CDR is beginning position."
2869 (unless (bolp) (backward-char))
2870 (when (re-search-forward org-babel-inline-src-block-regexp limit t
)
2871 (cons 'inline-src-block
(match-beginning 1)))))
2875 (defun org-element-italic-parser ()
2876 "Parse italic object at point.
2878 Return a list whose CAR is `italic' and CDR is a plist with
2879 `:begin', `:end', `:contents-begin' and `:contents-end' and
2880 `:post-blank' keywords.
2882 Assume point is at the first slash marker."
2884 (unless (bolp) (backward-char 1))
2885 (looking-at org-emph-re
)
2886 (let ((begin (match-beginning 2))
2887 (contents-begin (match-beginning 4))
2888 (contents-end (match-end 4))
2889 (post-blank (progn (goto-char (match-end 2))
2890 (skip-chars-forward " \t")))
2895 :contents-begin contents-begin
2896 :contents-end contents-end
2897 :post-blank post-blank
)))))
2899 (defun org-element-italic-interpreter (italic contents
)
2900 "Interpret ITALIC object as Org syntax.
2901 CONTENTS is the contents of the object."
2902 (format "/%s/" contents
))
2907 (defun org-element-latex-fragment-parser ()
2908 "Parse latex fragment at point.
2910 Return a list whose CAR is `latex-fragment' and CDR a plist with
2911 `:value', `:begin', `:end', and `:post-blank' as keywords.
2913 Assume point is at the beginning of the latex fragment."
2915 (let* ((begin (point))
2919 (let ((latex-regexp (nth 1 (assoc e org-latex-regexps
))))
2920 (when (or (looking-at latex-regexp
)
2924 (looking-at latex-regexp
))))
2925 (throw 'exit
(nth 2 (assoc e org-latex-regexps
))))))
2926 (plist-get org-format-latex-options
:matchers
))
2927 ;; None found: it's a macro.
2928 (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")
2930 (value (match-string-no-properties substring-match
))
2931 (post-blank (progn (goto-char (match-end substring-match
))
2932 (skip-chars-forward " \t")))
2934 (list 'latex-fragment
2938 :post-blank post-blank
)))))
2940 (defun org-element-latex-fragment-interpreter (latex-fragment contents
)
2941 "Interpret LATEX-FRAGMENT object as Org syntax.
2943 (org-element-property :value latex-fragment
))
2947 (defun org-element-line-break-parser ()
2948 "Parse line break at point.
2950 Return a list whose CAR is `line-break', and CDR a plist with
2951 `:begin', `:end' and `:post-blank' keywords.
2953 Assume point is at the beginning of the line break."
2955 (list :begin
(point)
2956 :end
(progn (forward-line) (point))
2959 (defun org-element-line-break-interpreter (line-break contents
)
2960 "Interpret LINE-BREAK object as Org syntax.
2964 (defun org-element-line-break-successor (limit)
2965 "Search for the next line-break object.
2967 LIMIT bounds the search.
2969 Return value is a cons cell whose CAR is `line-break' and CDR is
2970 beginning position."
2972 (let ((beg (and (re-search-forward "[^\\\\]\\(\\\\\\\\\\)[ \t]*$" limit t
)
2973 (goto-char (match-beginning 1)))))
2974 ;; A line break can only happen on a non-empty line.
2975 (when (and beg
(re-search-backward "\\S-" (point-at-bol) t
))
2976 (cons 'line-break beg
)))))
2981 (defun org-element-link-parser ()
2982 "Parse link at point.
2984 Return a list whose CAR is `link' and CDR a plist with `:type',
2985 `:path', `:raw-link', `:application', `:search-option', `:begin',
2986 `:end', `:contents-begin', `:contents-end' and `:post-blank' as
2989 Assume point is at the beginning of the link."
2991 (let ((begin (point))
2992 end contents-begin contents-end link-end post-blank path type
2993 raw-link link search-option application
)
2995 ;; Type 1: Text targeted from a radio target.
2996 ((and org-target-link-regexp
(looking-at org-target-link-regexp
))
2998 link-end
(match-end 0)
2999 path
(org-match-string-no-properties 0)))
3000 ;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]]
3001 ((looking-at org-bracket-link-regexp
)
3002 (setq contents-begin
(match-beginning 3)
3003 contents-end
(match-end 3)
3004 link-end
(match-end 0)
3005 ;; RAW-LINK is the original link.
3006 raw-link
(org-translate-link
3007 (org-link-expand-abbrev
3008 (org-match-string-no-properties 1)))
3009 link
(org-link-unescape raw-link
))
3010 ;; Determine TYPE of link and set PATH accordingly.
3013 ((or (file-name-absolute-p link
) (string-match "^\\.\\.?/" link
))
3014 (setq type
"file" path link
))
3015 ;; Explicit type (http, irc, bbdb...). See `org-link-types'.
3016 ((string-match org-link-re-with-space3 link
)
3017 (setq type
(match-string 1 link
) path
(match-string 2 link
)))
3018 ;; Id type: PATH is the id.
3019 ((string-match "^id:\\([-a-f0-9]+\\)" link
)
3020 (setq type
"id" path
(match-string 1 link
)))
3021 ;; Code-ref type: PATH is the name of the reference.
3022 ((string-match "^(\\(.*\\))$" link
)
3023 (setq type
"coderef" path
(match-string 1 link
)))
3024 ;; Custom-id type: PATH is the name of the custom id.
3025 ((= (aref link
0) ?
#)
3026 (setq type
"custom-id" path
(substring link
1)))
3027 ;; Fuzzy type: Internal link either matches a target, an
3028 ;; headline name or nothing. PATH is the target or
3030 (t (setq type
"fuzzy" path link
))))
3031 ;; Type 3: Plain link, i.e. http://orgmode.org
3032 ((looking-at org-plain-link-re
)
3033 (setq raw-link
(org-match-string-no-properties 0)
3034 type
(org-match-string-no-properties 1)
3035 path
(org-match-string-no-properties 2)
3036 link-end
(match-end 0)))
3037 ;; Type 4: Angular link, i.e. <http://orgmode.org>
3038 ((looking-at org-angle-link-re
)
3039 (setq raw-link
(buffer-substring-no-properties
3040 (match-beginning 1) (match-end 2))
3041 type
(org-match-string-no-properties 1)
3042 path
(org-match-string-no-properties 2)
3043 link-end
(match-end 0))))
3044 ;; In any case, deduce end point after trailing white space from
3045 ;; LINK-END variable.
3046 (setq post-blank
(progn (goto-char link-end
) (skip-chars-forward " \t"))
3048 ;; Extract search option and opening application out of
3049 ;; "file"-type links.
3050 (when (member type org-element-link-type-is-file
)
3052 (cond ((string-match "^file\\+\\(.*\\)$" type
)
3053 (setq application
(match-string 1 type
)))
3054 ((not (string-match "^file" type
))
3055 (setq application type
)))
3056 ;; Extract search option from PATH.
3057 (when (string-match "::\\(.*\\)$" path
)
3058 (setq search-option
(match-string 1 path
)
3059 path
(replace-match "" nil nil path
)))
3060 ;; Make sure TYPE always report "file".
3065 :raw-link
(or raw-link path
)
3066 :application application
3067 :search-option search-option
3070 :contents-begin contents-begin
3071 :contents-end contents-end
3072 :post-blank post-blank
)))))
3074 (defun org-element-link-interpreter (link contents
)
3075 "Interpret LINK object as Org syntax.
3076 CONTENTS is the contents of the object, or nil."
3077 (let ((type (org-element-property :type link
))
3078 (raw-link (org-element-property :raw-link link
)))
3079 (if (string= type
"radio") raw-link
3082 (if contents
(format "[%s]" contents
) "")))))
3084 (defun org-element-link-successor (limit)
3085 "Search for the next link object.
3087 LIMIT bounds the search.
3089 Return value is a cons cell whose CAR is `link' and CDR is
3090 beginning position."
3093 (if (not org-target-link-regexp
) org-any-link-re
3094 (concat org-any-link-re
"\\|" org-target-link-regexp
))))
3095 (when (re-search-forward link-regexp limit t
)
3096 (cons 'link
(match-beginning 0))))))
3101 (defun org-element-macro-parser ()
3102 "Parse macro at point.
3104 Return a list whose CAR is `macro' and CDR a plist with `:key',
3105 `:args', `:begin', `:end', `:value' and `:post-blank' as
3108 Assume point is at the macro."
3110 (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
3111 (let ((begin (point))
3112 (key (downcase (org-match-string-no-properties 1)))
3113 (value (org-match-string-no-properties 0))
3114 (post-blank (progn (goto-char (match-end 0))
3115 (skip-chars-forward " \t")))
3117 (args (let ((args (org-match-string-no-properties 3)) args2
)
3119 (setq args
(org-split-string args
","))
3121 (while (string-match "\\\\\\'" (car args
))
3122 ;; Repair bad splits.
3123 (setcar (cdr args
) (concat (substring (car args
) 0 -
1)
3126 (push (pop args
) args2
))
3127 (mapcar 'org-trim
(nreverse args2
))))))
3134 :post-blank post-blank
)))))
3136 (defun org-element-macro-interpreter (macro contents
)
3137 "Interpret MACRO object as Org syntax.
3139 (org-element-property :value macro
))
3141 (defun org-element-macro-successor (limit)
3142 "Search for the next macro object.
3144 LIMIT bounds the search.
3146 Return value is cons cell whose CAR is `macro' and CDR is
3147 beginning position."
3149 (when (re-search-forward
3150 "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
3152 (cons 'macro
(match-beginning 0)))))
3157 (defun org-element-radio-target-parser ()
3158 "Parse radio target at point.
3160 Return a list whose CAR is `radio-target' and CDR a plist with
3161 `:begin', `:end', `:contents-begin', `:contents-end', `:value'
3162 and `:post-blank' as keywords.
3164 Assume point is at the radio target."
3166 (looking-at org-radio-target-regexp
)
3167 (let ((begin (point))
3168 (contents-begin (match-beginning 1))
3169 (contents-end (match-end 1))
3170 (value (org-match-string-no-properties 1))
3171 (post-blank (progn (goto-char (match-end 0))
3172 (skip-chars-forward " \t")))
3177 :contents-begin contents-begin
3178 :contents-end contents-end
3179 :post-blank post-blank
3182 (defun org-element-radio-target-interpreter (target contents
)
3183 "Interpret TARGET object as Org syntax.
3184 CONTENTS is the contents of the object."
3185 (concat "<<<" contents
">>>"))
3187 (defun org-element-radio-target-successor (limit)
3188 "Search for the next radio-target object.
3190 LIMIT bounds the search.
3192 Return value is a cons cell whose CAR is `radio-target' and CDR
3193 is beginning position."
3195 (when (re-search-forward org-radio-target-regexp limit t
)
3196 (cons 'radio-target
(match-beginning 0)))))
3199 ;;;; Statistics Cookie
3201 (defun org-element-statistics-cookie-parser ()
3202 "Parse statistics cookie at point.
3204 Return a list whose CAR is `statistics-cookie', and CDR a plist
3205 with `:begin', `:end', `:value' and `:post-blank' keywords.
3207 Assume point is at the beginning of the statistics-cookie."
3209 (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
3210 (let* ((begin (point))
3211 (value (buffer-substring-no-properties
3212 (match-beginning 0) (match-end 0)))
3213 (post-blank (progn (goto-char (match-end 0))
3214 (skip-chars-forward " \t")))
3216 (list 'statistics-cookie
3220 :post-blank post-blank
)))))
3222 (defun org-element-statistics-cookie-interpreter (statistics-cookie contents
)
3223 "Interpret STATISTICS-COOKIE object as Org syntax.
3225 (org-element-property :value statistics-cookie
))
3227 (defun org-element-statistics-cookie-successor (limit)
3228 "Search for the next statistics cookie object.
3230 LIMIT bounds the search.
3232 Return value is a cons cell whose CAR is `statistics-cookie' and
3233 CDR is beginning position."
3235 (when (re-search-forward "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]" limit t
)
3236 (cons 'statistics-cookie
(match-beginning 0)))))
3241 (defun org-element-strike-through-parser ()
3242 "Parse strike-through object at point.
3244 Return a list whose CAR is `strike-through' and CDR is a plist
3245 with `:begin', `:end', `:contents-begin' and `:contents-end' and
3246 `:post-blank' keywords.
3248 Assume point is at the first plus sign marker."
3250 (unless (bolp) (backward-char 1))
3251 (looking-at org-emph-re
)
3252 (let ((begin (match-beginning 2))
3253 (contents-begin (match-beginning 4))
3254 (contents-end (match-end 4))
3255 (post-blank (progn (goto-char (match-end 2))
3256 (skip-chars-forward " \t")))
3258 (list 'strike-through
3261 :contents-begin contents-begin
3262 :contents-end contents-end
3263 :post-blank post-blank
)))))
3265 (defun org-element-strike-through-interpreter (strike-through contents
)
3266 "Interpret STRIKE-THROUGH object as Org syntax.
3267 CONTENTS is the contents of the object."
3268 (format "+%s+" contents
))
3273 (defun org-element-subscript-parser ()
3274 "Parse subscript at point.
3276 Return a list whose CAR is `subscript' and CDR a plist with
3277 `:begin', `:end', `:contents-begin', `:contents-end',
3278 `:use-brackets-p' and `:post-blank' as keywords.
3280 Assume point is at the underscore."
3282 (unless (bolp) (backward-char))
3283 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp
)
3285 (not (looking-at org-match-substring-regexp
))))
3286 (begin (match-beginning 2))
3287 (contents-begin (or (match-beginning 5)
3288 (match-beginning 3)))
3289 (contents-end (or (match-end 5) (match-end 3)))
3290 (post-blank (progn (goto-char (match-end 0))
3291 (skip-chars-forward " \t")))
3296 :use-brackets-p bracketsp
3297 :contents-begin contents-begin
3298 :contents-end contents-end
3299 :post-blank post-blank
)))))
3301 (defun org-element-subscript-interpreter (subscript contents
)
3302 "Interpret SUBSCRIPT object as Org syntax.
3303 CONTENTS is the contents of the object."
3305 (if (org-element-property :use-brackets-p subscript
) "_{%s}" "_%s")
3308 (defun org-element-sub/superscript-successor
(limit)
3309 "Search for the next sub/superscript object.
3311 LIMIT bounds the search.
3313 Return value is a cons cell whose CAR is either `subscript' or
3314 `superscript' and CDR is beginning position."
3316 (unless (bolp) (backward-char))
3317 (when (re-search-forward org-match-substring-regexp limit t
)
3318 (cons (if (string= (match-string 2) "_") 'subscript
'superscript
)
3319 (match-beginning 2)))))
3324 (defun org-element-superscript-parser ()
3325 "Parse superscript at point.
3327 Return a list whose CAR is `superscript' and CDR a plist with
3328 `:begin', `:end', `:contents-begin', `:contents-end',
3329 `:use-brackets-p' and `:post-blank' as keywords.
3331 Assume point is at the caret."
3333 (unless (bolp) (backward-char))
3334 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp
) t
3335 (not (looking-at org-match-substring-regexp
))))
3336 (begin (match-beginning 2))
3337 (contents-begin (or (match-beginning 5)
3338 (match-beginning 3)))
3339 (contents-end (or (match-end 5) (match-end 3)))
3340 (post-blank (progn (goto-char (match-end 0))
3341 (skip-chars-forward " \t")))
3346 :use-brackets-p bracketsp
3347 :contents-begin contents-begin
3348 :contents-end contents-end
3349 :post-blank post-blank
)))))
3351 (defun org-element-superscript-interpreter (superscript contents
)
3352 "Interpret SUPERSCRIPT object as Org syntax.
3353 CONTENTS is the contents of the object."
3355 (if (org-element-property :use-brackets-p superscript
) "^{%s}" "^%s")
3361 (defun org-element-table-cell-parser ()
3362 "Parse table cell at point.
3364 Return a list whose CAR is `table-cell' and CDR is a plist
3365 containing `:begin', `:end', `:contents-begin', `:contents-end'
3366 and `:post-blank' keywords."
3367 (looking-at "[ \t]*\\(.*?\\)[ \t]*|")
3368 (let* ((begin (match-beginning 0))
3370 (contents-begin (match-beginning 1))
3371 (contents-end (match-end 1)))
3375 :contents-begin contents-begin
3376 :contents-end contents-end
3379 (defun org-element-table-cell-interpreter (table-cell contents
)
3380 "Interpret TABLE-CELL element as Org syntax.
3381 CONTENTS is the contents of the cell, or nil."
3382 (concat " " contents
" |"))
3384 (defun org-element-table-cell-successor (limit)
3385 "Search for the next table-cell object.
3387 LIMIT bounds the search.
3389 Return value is a cons cell whose CAR is `table-cell' and CDR is
3390 beginning position."
3391 (when (looking-at "[ \t]*.*?[ \t]+|") (cons 'table-cell
(point))))
3396 (defun org-element-target-parser ()
3397 "Parse target at point.
3399 Return a list whose CAR is `target' and CDR a plist with
3400 `:begin', `:end', `:value' and `:post-blank' as keywords.
3402 Assume point is at the target."
3404 (looking-at org-target-regexp
)
3405 (let ((begin (point))
3406 (value (org-match-string-no-properties 1))
3407 (post-blank (progn (goto-char (match-end 0))
3408 (skip-chars-forward " \t")))
3414 :post-blank post-blank
)))))
3416 (defun org-element-target-interpreter (target contents
)
3417 "Interpret TARGET object as Org syntax.
3419 (format "<<%s>>" (org-element-property :value target
)))
3421 (defun org-element-target-successor (limit)
3422 "Search for the next target object.
3424 LIMIT bounds the search.
3426 Return value is a cons cell whose CAR is `target' and CDR is
3427 beginning position."
3429 (when (re-search-forward org-target-regexp limit t
)
3430 (cons 'target
(match-beginning 0)))))
3435 (defun org-element-timestamp-parser ()
3436 "Parse time stamp at point.
3438 Return a list whose CAR is `timestamp', and CDR a plist with
3439 `:type', `:begin', `:end', `:value' and `:post-blank' keywords.
3441 Assume point is at the beginning of the timestamp."
3443 (let* ((begin (point))
3444 (activep (eq (char-after) ?
<))
3447 (looking-at "\\([<[]\\(%%\\)?.*?\\)[]>]\\(?:--\\([<[].*?[]>]\\)\\)?")
3448 (match-string-no-properties 0)))
3449 (date-start (match-string-no-properties 1))
3450 (date-end (match-string 3))
3451 (diaryp (match-beginning 2))
3452 (post-blank (progn (goto-char (match-end 0))
3453 (skip-chars-forward " \t")))
3458 "[012]?[0-9]:[0-5][0-9]\\(-\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)"
3460 (cons (string-to-number (match-string 2 date-start
))
3461 (string-to-number (match-string 3 date-start
)))))
3462 (type (cond (diaryp 'diary
)
3463 ((and activep
(or date-end time-range
)) 'active-range
)
3465 ((or date-end time-range
) 'inactive-range
)
3469 (string-match "\\([.+]?\\+\\)\\([0-9]+\\)\\([hdwmy]\\)>"
3473 (let ((type (match-string 1 raw-value
)))
3474 (cond ((equal "++" type
) 'catch-up
)
3475 ((equal ".+" type
) 'restart
)
3477 :repeater-value
(string-to-number (match-string 2 raw-value
))
3479 (case (string-to-char (match-string 3 raw-value
))
3480 (?h
'hour
) (?d
'day
) (?w
'week
) (?m
'month
) (t 'year
)))))
3481 year-start month-start day-start hour-start minute-start year-end
3482 month-end day-end hour-end minute-end
)
3483 ;; Parse date-start.
3485 (let ((date (org-parse-time-string date-start t
)))
3486 (setq year-start
(nth 5 date
)
3487 month-start
(nth 4 date
)
3488 day-start
(nth 3 date
)
3489 hour-start
(nth 2 date
)
3490 minute-start
(nth 1 date
))))
3491 ;; Compute date-end. It can be provided directly in time-stamp,
3492 ;; or extracted from time range. Otherwise, it defaults to the
3493 ;; same values as date-start.
3495 (let ((date (and date-end
(org-parse-time-string date-end t
))))
3496 (setq year-end
(or (nth 5 date
) year-start
)
3497 month-end
(or (nth 4 date
) month-start
)
3498 day-end
(or (nth 3 date
) day-start
)
3499 hour-end
(or (nth 2 date
) (car time-range
) hour-start
)
3500 minute-end
(or (nth 1 date
) (cdr time-range
) minute-start
))))
3502 (nconc (list :type type
3503 :raw-value raw-value
3504 :year-start year-start
3505 :month-start month-start
3506 :day-start day-start
3507 :hour-start hour-start
3508 :minute-start minute-start
3510 :month-end month-end
3513 :minute-end minute-end
3516 :post-blank post-blank
)
3519 (defun org-element-timestamp-interpreter (timestamp contents
)
3520 "Interpret TIMESTAMP object as Org syntax.
3522 ;; Use `:raw-value' if specified.
3523 (or (org-element-property :raw-value timestamp
)
3524 ;; Otherwise, build timestamp string.
3525 (let* ((repeat-string
3527 (case (org-element-property :repeater-type timestamp
)
3528 (cumulate "+") (catch-up "++") (restart ".+"))
3529 (let ((val (org-element-property :repeater-value timestamp
)))
3530 (and val
(number-to-string val
)))
3531 (case (org-element-property :repeater-unit timestamp
)
3532 (hour "h") (day "d") (week "w") (month "m") (year "y"))))
3534 ;; Build an Org timestamp string from TIME. ACTIVEP is
3535 ;; non-nil when time stamp is active. If WITH-TIME-P is
3536 ;; non-nil, add a time part. HOUR-END and MINUTE-END
3537 ;; specify a time range in the timestamp. REPEAT-STRING
3538 ;; is the repeater string, if any.
3539 (lambda (time activep
&optional with-time-p hour-end minute-end
)
3540 (let ((ts (format-time-string
3541 (funcall (if with-time-p
'cdr
'car
)
3542 org-time-stamp-formats
)
3544 (when (and hour-end minute-end
)
3545 (string-match "[012]?[0-9]:[0-5][0-9]" ts
)
3548 (format "\\&-%02d:%02d" hour-end minute-end
)
3550 (unless activep
(setq ts
(format "[%s]" (substring ts
1 -
1))))
3551 (when (org-string-nw-p repeat-string
)
3552 (setq ts
(concat (substring ts
0 -
1)
3555 (substring ts -
1))))
3558 (type (org-element-property :type timestamp
)))
3561 (let* ((minute-start (org-element-property :minute-start timestamp
))
3562 (minute-end (org-element-property :minute-end timestamp
))
3563 (hour-start (org-element-property :hour-start timestamp
))
3564 (hour-end (org-element-property :hour-end timestamp
))
3565 (time-range-p (and hour-start hour-end minute-start minute-end
3566 (or (/= hour-start hour-end
)
3567 (/= minute-start minute-end
)))))
3573 (org-element-property :day-start timestamp
)
3574 (org-element-property :month-start timestamp
)
3575 (org-element-property :year-start timestamp
))
3577 (and hour-start minute-start
)
3578 (and time-range-p hour-end
)
3579 (and time-range-p minute-end
))))
3580 ((active-range inactive-range
)
3581 (let ((minute-start (org-element-property :minute-start timestamp
))
3582 (minute-end (org-element-property :minute-end timestamp
))
3583 (hour-start (org-element-property :hour-start timestamp
))
3584 (hour-end (org-element-property :hour-end timestamp
)))
3587 build-ts-string
(encode-time
3591 (org-element-property :day-start timestamp
)
3592 (org-element-property :month-start timestamp
)
3593 (org-element-property :year-start timestamp
))
3594 (eq type
'active-range
)
3595 (and hour-start minute-start
))
3597 (funcall build-ts-string
3601 (org-element-property :day-end timestamp
)
3602 (org-element-property :month-end timestamp
)
3603 (org-element-property :year-end timestamp
))
3604 (eq type
'active-range
)
3605 (and hour-end minute-end
)))))))))
3607 (defun org-element-timestamp-successor (limit)
3608 "Search for the next timestamp object.
3610 LIMIT bounds the search.
3612 Return value is a cons cell whose CAR is `timestamp' and CDR is
3613 beginning position."
3615 (when (re-search-forward
3616 (concat org-ts-regexp-both
3618 "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
3620 "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
3622 (cons 'timestamp
(match-beginning 0)))))
3627 (defun org-element-underline-parser ()
3628 "Parse underline object at point.
3630 Return a list whose CAR is `underline' and CDR is a plist with
3631 `:begin', `:end', `:contents-begin' and `:contents-end' and
3632 `:post-blank' keywords.
3634 Assume point is at the first underscore marker."
3636 (unless (bolp) (backward-char 1))
3637 (looking-at org-emph-re
)
3638 (let ((begin (match-beginning 2))
3639 (contents-begin (match-beginning 4))
3640 (contents-end (match-end 4))
3641 (post-blank (progn (goto-char (match-end 2))
3642 (skip-chars-forward " \t")))
3647 :contents-begin contents-begin
3648 :contents-end contents-end
3649 :post-blank post-blank
)))))
3651 (defun org-element-underline-interpreter (underline contents
)
3652 "Interpret UNDERLINE object as Org syntax.
3653 CONTENTS is the contents of the object."
3654 (format "_%s_" contents
))
3659 (defun org-element-verbatim-parser ()
3660 "Parse verbatim object at point.
3662 Return a list whose CAR is `verbatim' and CDR is a plist with
3663 `:value', `:begin', `:end' and `:post-blank' keywords.
3665 Assume point is at the first equal sign marker."
3667 (unless (bolp) (backward-char 1))
3668 (looking-at org-emph-re
)
3669 (let ((begin (match-beginning 2))
3670 (value (org-match-string-no-properties 4))
3671 (post-blank (progn (goto-char (match-end 2))
3672 (skip-chars-forward " \t")))
3678 :post-blank post-blank
)))))
3680 (defun org-element-verbatim-interpreter (verbatim contents
)
3681 "Interpret VERBATIM object as Org syntax.
3683 (format "=%s=" (org-element-property :value verbatim
)))
3687 ;;; Parsing Element Starting At Point
3689 ;; `org-element--current-element' is the core function of this section.
3690 ;; It returns the Lisp representation of the element starting at
3693 ;; `org-element--current-element' makes use of special modes. They
3694 ;; are activated for fixed element chaining (i.e. `plain-list' >
3695 ;; `item') or fixed conditional element chaining (i.e. `headline' >
3696 ;; `section'). Special modes are: `first-section', `item',
3697 ;; `node-property', `quote-section', `section' and `table-row'.
3699 (defun org-element--current-element
3700 (limit &optional granularity special structure
)
3701 "Parse the element starting at point.
3703 LIMIT bounds the search.
3705 Return value is a list like (TYPE PROPS) where TYPE is the type
3706 of the element and PROPS a plist of properties associated to the
3709 Possible types are defined in `org-element-all-elements'.
3711 Optional argument GRANULARITY determines the depth of the
3712 recursion. Allowed values are `headline', `greater-element',
3713 `element', `object' or nil. When it is broader than `object' (or
3714 nil), secondary values will not be parsed, since they only
3717 Optional argument SPECIAL, when non-nil, can be either
3718 `first-section', `item', `node-property', `quote-section',
3719 `section', and `table-row'.
3721 If STRUCTURE isn't provided but SPECIAL is set to `item', it will
3724 This function assumes point is always at the beginning of the
3725 element it has to parse."
3727 (let ((case-fold-search t
)
3728 ;; Determine if parsing depth allows for secondary strings
3729 ;; parsing. It only applies to elements referenced in
3730 ;; `org-element-secondary-value-alist'.
3731 (raw-secondary-p (and granularity
(not (eq granularity
'object
)))))
3735 (org-element-item-parser limit structure raw-secondary-p
))
3737 ((eq special
'table-row
) (org-element-table-row-parser limit
))
3739 ((eq special
'node-property
) (org-element-node-property-parser limit
))
3741 ((org-with-limited-levels (org-at-heading-p))
3742 (org-element-headline-parser limit raw-secondary-p
))
3743 ;; Sections (must be checked after headline).
3744 ((eq special
'section
) (org-element-section-parser limit
))
3745 ((eq special
'quote-section
) (org-element-quote-section-parser limit
))
3746 ((eq special
'first-section
)
3747 (org-element-section-parser
3748 (or (save-excursion (org-with-limited-levels (outline-next-heading)))
3750 ;; When not at bol, point is at the beginning of an item or
3751 ;; a footnote definition: next item is always a paragraph.
3752 ((not (bolp)) (org-element-paragraph-parser limit
(list (point))))
3753 ;; Planning and Clock.
3754 ((looking-at org-planning-or-clock-line-re
)
3755 (if (equal (match-string 1) org-clock-string
)
3756 (org-element-clock-parser limit
)
3757 (org-element-planning-parser limit
)))
3760 (org-element-inlinetask-parser limit raw-secondary-p
))
3761 ;; From there, elements can have affiliated keywords.
3762 (t (let ((affiliated (org-element--collect-affiliated-keywords)))
3764 ;; LaTeX Environment.
3765 ((looking-at "[ \t]*\\\\begin{\\([A-Za-z0-9*]+\\)}[ \t]*$")
3766 (org-element-latex-environment-parser limit affiliated
))
3767 ;; Drawer and Property Drawer.
3768 ((looking-at org-drawer-regexp
)
3769 (if (equal (match-string 1) "PROPERTIES")
3770 (org-element-property-drawer-parser limit affiliated
)
3771 (org-element-drawer-parser limit affiliated
)))
3773 ((looking-at "[ \t]*:\\( \\|$\\)")
3774 (org-element-fixed-width-parser limit affiliated
))
3775 ;; Inline Comments, Blocks, Babel Calls, Dynamic Blocks and
3777 ((looking-at "[ \t]*#")
3778 (goto-char (match-end 0))
3779 (cond ((looking-at "\\(?: \\|$\\)")
3781 (org-element-comment-parser limit affiliated
))
3782 ((looking-at "\\+BEGIN_\\(\\S-+\\)")
3784 (let ((parser (assoc (upcase (match-string 1))
3785 org-element-block-name-alist
)))
3786 (if parser
(funcall (cdr parser
) limit affiliated
)
3787 (org-element-special-block-parser limit affiliated
))))
3788 ((looking-at "\\+CALL:")
3790 (org-element-babel-call-parser limit affiliated
))
3791 ((looking-at "\\+BEGIN:? ")
3793 (org-element-dynamic-block-parser limit affiliated
))
3794 ((looking-at "\\+\\S-+:")
3796 (org-element-keyword-parser limit affiliated
))
3799 (org-element-paragraph-parser limit affiliated
))))
3800 ;; Footnote Definition.
3801 ((looking-at org-footnote-definition-re
)
3802 (org-element-footnote-definition-parser limit affiliated
))
3804 ((looking-at "[ \t]*-\\{5,\\}[ \t]*$")
3805 (org-element-horizontal-rule-parser limit affiliated
))
3808 (org-element-diary-sexp-parser limit affiliated
))
3810 ((org-at-table-p t
) (org-element-table-parser limit affiliated
))
3812 ((looking-at (org-item-re))
3813 (org-element-plain-list-parser
3814 limit affiliated
(or structure
(org-list-struct))))
3815 ;; Default element: Paragraph.
3816 (t (org-element-paragraph-parser limit affiliated
)))))))))
3819 ;; Most elements can have affiliated keywords. When looking for an
3820 ;; element beginning, we want to move before them, as they belong to
3821 ;; that element, and, in the meantime, collect information they give
3822 ;; into appropriate properties. Hence the following function.
3824 (defun org-element--collect-affiliated-keywords ()
3825 "Collect affiliated keywords from point.
3827 Return a list whose CAR is the position at the first of them and
3828 CDR a plist of keywords and values and move point to the
3829 beginning of the first line after them.
3831 As a special case, if element doesn't start at the beginning of
3832 the line (i.e. a paragraph starting an item), CAR is current
3833 position of point and CDR is nil."
3834 (if (not (bolp)) (list (point))
3835 (let ((case-fold-search t
)
3837 ;; RESTRICT is the list of objects allowed in parsed
3839 (restrict (org-element-restriction 'keyword
))
3841 (while (and (not (eobp)) (looking-at org-element--affiliated-re
))
3842 (let* ((raw-kwd (upcase (match-string 1)))
3843 ;; Apply translation to RAW-KWD. From there, KWD is
3844 ;; the official keyword.
3845 (kwd (or (cdr (assoc raw-kwd
3846 org-element-keyword-translation-alist
))
3848 ;; Find main value for any keyword.
3852 (buffer-substring-no-properties
3853 (match-end 0) (point-at-eol)))))
3854 ;; PARSEDP is non-nil when keyword should have its
3856 (parsedp (member kwd org-element-parsed-keywords
))
3857 ;; If KWD is a dual keyword, find its secondary
3858 ;; value. Maybe parse it.
3859 (dualp (member kwd org-element-dual-keywords
))
3862 (let ((sec (org-match-string-no-properties 2)))
3863 (if (or (not sec
) (not parsedp
)) sec
3864 (org-element-parse-secondary-string sec restrict
)))))
3865 ;; Attribute a property name to KWD.
3866 (kwd-sym (and kwd
(intern (concat ":" (downcase kwd
))))))
3867 ;; Now set final shape for VALUE.
3869 (setq value
(org-element-parse-secondary-string value restrict
)))
3871 (setq value
(and (or value dual-value
) (cons value dual-value
))))
3872 (when (or (member kwd org-element-multiple-keywords
)
3873 ;; Attributes can always appear on multiple lines.
3874 (string-match "^ATTR_" kwd
))
3875 (setq value
(cons value
(plist-get output kwd-sym
))))
3876 ;; Eventually store the new value in OUTPUT.
3877 (setq output
(plist-put output kwd-sym value
))
3878 ;; Move to next keyword.
3880 ;; If affiliated keywords are orphaned: move back to first one.
3881 ;; They will be parsed as a paragraph.
3882 (when (looking-at "[ \t]*$") (goto-char origin
) (setq output nil
))
3884 (cons origin output
))))
3890 ;; The two major functions here are `org-element-parse-buffer', which
3891 ;; parses Org syntax inside the current buffer, taking into account
3892 ;; region, narrowing, or even visibility if specified, and
3893 ;; `org-element-parse-secondary-string', which parses objects within
3896 ;; The (almost) almighty `org-element-map' allows to apply a function
3897 ;; on elements or objects matching some type, and accumulate the
3898 ;; resulting values. In an export situation, it also skips unneeded
3899 ;; parts of the parse tree.
3901 (defun org-element-parse-buffer (&optional granularity visible-only
)
3902 "Recursively parse the buffer and return structure.
3903 If narrowing is in effect, only parse the visible part of the
3906 Optional argument GRANULARITY determines the depth of the
3907 recursion. It can be set to the following symbols:
3909 `headline' Only parse headlines.
3910 `greater-element' Don't recurse into greater elements excepted
3911 headlines and sections. Thus, elements
3912 parsed are the top-level ones.
3913 `element' Parse everything but objects and plain text.
3914 `object' Parse the complete buffer (default).
3916 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3919 An element or an objects is represented as a list with the
3920 pattern (TYPE PROPERTIES CONTENTS), where :
3922 TYPE is a symbol describing the element or object. See
3923 `org-element-all-elements' and `org-element-all-objects' for an
3924 exhaustive list of such symbols. One can retrieve it with
3925 `org-element-type' function.
3927 PROPERTIES is the list of attributes attached to the element or
3928 object, as a plist. Although most of them are specific to the
3929 element or object type, all types share `:begin', `:end',
3930 `:post-blank' and `:parent' properties, which respectively
3931 refer to buffer position where the element or object starts,
3932 ends, the number of white spaces or blank lines after it, and
3933 the element or object containing it. Properties values can be
3934 obtained by using `org-element-property' function.
3936 CONTENTS is a list of elements, objects or raw strings
3937 contained in the current element or object, when applicable.
3938 One can access them with `org-element-contents' function.
3940 The Org buffer has `org-data' as type and nil as properties.
3941 `org-element-map' function can be used to find specific elements
3942 or objects within the parse tree.
3944 This function assumes that current major mode is `org-mode'."
3946 (goto-char (point-min))
3947 (org-skip-whitespace)
3948 (org-element--parse-elements
3949 (point-at-bol) (point-max)
3950 ;; Start in `first-section' mode so text before the first
3951 ;; headline belongs to a section.
3952 'first-section nil granularity visible-only
(list 'org-data nil
))))
3954 (defun org-element-parse-secondary-string (string restriction
&optional parent
)
3955 "Recursively parse objects in STRING and return structure.
3957 RESTRICTION is a symbol limiting the object types that will be
3960 Optional argument PARENT, when non-nil, is the element or object
3961 containing the secondary string. It is used to set correctly
3962 `:parent' property within the string."
3963 ;; Copy buffer-local variables listed in
3964 ;; `org-element-object-variables' into temporary buffer. This is
3965 ;; required since object parsing is dependent on these variables.
3966 (let ((pairs (delq nil
(mapcar (lambda (var)
3968 (cons var
(symbol-value var
))))
3969 org-element-object-variables
))))
3971 (mapc (lambda (pair) (org-set-local (car pair
) (cdr pair
))) pairs
)
3973 (let ((secondary (org-element--parse-objects
3974 (point-min) (point-max) nil restriction
)))
3976 (mapc (lambda (obj) (org-element-put-property obj
:parent parent
))
3980 (defun org-element-map
3981 (data types fun
&optional info first-match no-recursion with-affiliated
)
3982 "Map a function on selected elements or objects.
3984 DATA is an Org buffer parse tree, as returned by, i.e,
3985 `org-element-parse-buffer'. TYPES is a symbol or list of symbols
3986 of elements or objects types (see `org-element-all-elements' and
3987 `org-element-all-objects' for a complete list of types). FUN is
3988 the function called on the matching element or object. It has to
3989 accept one argument: the element or object itself.
3991 When optional argument INFO is non-nil, it should be a plist
3992 holding export options. In that case, parts of the parse tree
3993 not exportable according to that property list will be skipped.
3995 When optional argument FIRST-MATCH is non-nil, stop at the first
3996 match for which FUN doesn't return nil, and return that value.
3998 Optional argument NO-RECURSION is a symbol or a list of symbols
3999 representing elements or objects types. `org-element-map' won't
4000 enter any recursive element or object whose type belongs to that
4001 list. Though, FUN can still be applied on them.
4003 When optional argument WITH-AFFILIATED is non-nil, FUN will also
4004 apply to matching objects within parsed affiliated keywords (see
4005 `org-element-parsed-keywords').
4007 Nil values returned from FUN do not appear in the results.
4013 Assuming TREE is a variable containing an Org buffer parse tree,
4014 the following example will return a flat list of all `src-block'
4015 and `example-block' elements in it:
4017 \(org-element-map tree '(example-block src-block) 'identity)
4019 The following snippet will find the first headline with a level
4020 of 1 and a \"phone\" tag, and will return its beginning position:
4022 \(org-element-map tree 'headline
4024 \(and (= (org-element-property :level hl) 1)
4025 \(member \"phone\" (org-element-property :tags hl))
4026 \(org-element-property :begin hl)))
4029 The next example will return a flat list of all `plain-list' type
4030 elements in TREE that are not a sub-list themselves:
4032 \(org-element-map tree 'plain-list 'identity nil nil 'plain-list)
4034 Eventually, this example will return a flat list of all `bold'
4035 type objects containing a `latex-snippet' type object, even
4036 looking into captions:
4038 \(org-element-map tree 'bold
4040 \(and (org-element-map b 'latex-snippet 'identity nil t) b))
4042 ;; Ensure TYPES and NO-RECURSION are a list, even of one element.
4043 (unless (listp types
) (setq types
(list types
)))
4044 (unless (listp no-recursion
) (setq no-recursion
(list no-recursion
)))
4045 ;; Recursion depth is determined by --CATEGORY.
4048 (let ((category 'greater-elements
))
4049 (mapc (lambda (type)
4050 (cond ((or (memq type org-element-all-objects
)
4051 (eq type
'plain-text
))
4052 ;; If one object is found, the function
4053 ;; has to recurse into every object.
4054 (throw 'found
'objects
))
4055 ((not (memq type org-element-greater-elements
))
4056 ;; If one regular element is found, the
4057 ;; function has to recurse, at least,
4058 ;; into every element it encounters.
4059 (and (not (eq category
'elements
))
4060 (setq category
'elements
)))))
4063 ;; Compute properties for affiliated keywords if necessary.
4065 (and with-affiliated
4066 (mapcar (lambda (kwd)
4067 (cons kwd
(intern (concat ":" (downcase kwd
)))))
4068 org-element-affiliated-keywords
)))
4074 ;; Recursively walk DATA. INFO, if non-nil, is a plist
4075 ;; holding contextual information.
4076 (let ((--type (org-element-type --data
)))
4079 ;; Ignored element in an export context.
4080 ((and info
(memq --data
(plist-get info
:ignore-list
))))
4081 ;; Secondary string: only objects can be found there.
4083 (when (eq --category
'objects
) (mapc --walk-tree --data
)))
4084 ;; Unconditionally enter parse trees.
4085 ((eq --type
'org-data
)
4086 (mapc --walk-tree
(org-element-contents --data
)))
4088 ;; Check if TYPE is matching among TYPES. If so,
4089 ;; apply FUN to --DATA and accumulate return value
4090 ;; into --ACC (or exit if FIRST-MATCH is non-nil).
4091 (when (memq --type types
)
4092 (let ((result (funcall fun --data
)))
4093 (cond ((not result
))
4094 (first-match (throw '--map-first-match result
))
4095 (t (push result --acc
)))))
4096 ;; If --DATA has a secondary string that can contain
4097 ;; objects with their type among TYPES, look into it.
4098 (when (and (eq --category
'objects
) (not (stringp --data
)))
4100 (assq --type org-element-secondary-value-alist
)))
4102 (funcall --walk-tree
4103 (org-element-property (cdr sec-prop
) --data
)))))
4104 ;; If --DATA has any affiliated keywords and
4105 ;; WITH-AFFILIATED is non-nil, look for objects in
4107 (when (and with-affiliated
4108 (eq --category
'objects
)
4109 (memq --type org-element-all-elements
))
4110 (mapc (lambda (kwd-pair)
4111 (let ((kwd (car kwd-pair
))
4112 (value (org-element-property
4113 (cdr kwd-pair
) --data
)))
4114 ;; Pay attention to the type of value.
4115 ;; Preserve order for multiple keywords.
4118 ((and (member kwd org-element-multiple-keywords
)
4119 (member kwd org-element-dual-keywords
))
4120 (mapc (lambda (line)
4121 (funcall --walk-tree
(cdr line
))
4122 (funcall --walk-tree
(car line
)))
4124 ((member kwd org-element-multiple-keywords
)
4125 (mapc (lambda (line) (funcall --walk-tree line
))
4127 ((member kwd org-element-dual-keywords
)
4128 (funcall --walk-tree
(cdr value
))
4129 (funcall --walk-tree
(car value
)))
4130 (t (funcall --walk-tree value
)))))
4131 --affiliated-alist
))
4132 ;; Determine if a recursion into --DATA is possible.
4134 ;; --TYPE is explicitly removed from recursion.
4135 ((memq --type no-recursion
))
4136 ;; --DATA has no contents.
4137 ((not (org-element-contents --data
)))
4138 ;; Looking for greater elements but --DATA is simply
4139 ;; an element or an object.
4140 ((and (eq --category
'greater-elements
)
4141 (not (memq --type org-element-greater-elements
))))
4142 ;; Looking for elements but --DATA is an object.
4143 ((and (eq --category
'elements
)
4144 (memq --type org-element-all-objects
)))
4145 ;; In any other case, map contents.
4146 (t (mapc --walk-tree
(org-element-contents --data
)))))))))))
4147 (catch '--map-first-match
4148 (funcall --walk-tree data
)
4149 ;; Return value in a proper order.
4151 (put 'org-element-map
'lisp-indent-function
2)
4153 ;; The following functions are internal parts of the parser.
4155 ;; The first one, `org-element--parse-elements' acts at the element's
4158 ;; The second one, `org-element--parse-objects' applies on all objects
4159 ;; of a paragraph or a secondary string. It uses
4160 ;; `org-element--get-next-object-candidates' to optimize the search of
4161 ;; the next object in the buffer.
4163 ;; More precisely, that function looks for every allowed object type
4164 ;; first. Then, it discards failed searches, keeps further matches,
4165 ;; and searches again types matched behind point, for subsequent
4166 ;; calls. Thus, searching for a given type fails only once, and every
4167 ;; object is searched only once at top level (but sometimes more for
4170 (defun org-element--parse-elements
4171 (beg end special structure granularity visible-only acc
)
4172 "Parse elements between BEG and END positions.
4174 SPECIAL prioritize some elements over the others. It can be set
4175 to `first-section', `quote-section', `section' `item' or
4178 When value is `item', STRUCTURE will be used as the current list
4181 GRANULARITY determines the depth of the recursion. See
4182 `org-element-parse-buffer' for more information.
4184 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
4187 Elements are accumulated into ACC."
4190 ;; When parsing only headlines, skip any text before first one.
4191 (when (and (eq granularity
'headline
) (not (org-at-heading-p)))
4192 (org-with-limited-levels (outline-next-heading)))
4194 (while (< (point) end
)
4195 ;; Find current element's type and parse it accordingly to
4197 (let* ((element (org-element--current-element
4198 end granularity special structure
))
4199 (type (org-element-type element
))
4200 (cbeg (org-element-property :contents-begin element
)))
4201 (goto-char (org-element-property :end element
))
4202 ;; Fill ELEMENT contents by side-effect.
4204 ;; If VISIBLE-ONLY is true and element is hidden or if it has
4205 ;; no contents, don't modify it.
4206 ((or (and visible-only
(org-element-property :hiddenp element
))
4208 ;; Greater element: parse it between `contents-begin' and
4209 ;; `contents-end'. Make sure GRANULARITY allows the
4210 ;; recursion, or ELEMENT is an headline, in which case going
4211 ;; inside is mandatory, in order to get sub-level headings.
4212 ((and (memq type org-element-greater-elements
)
4213 (or (memq granularity
'(element object nil
))
4214 (and (eq granularity
'greater-element
)
4216 (eq type
'headline
)))
4217 (org-element--parse-elements
4218 cbeg
(org-element-property :contents-end element
)
4219 ;; Possibly switch to a special mode.
4222 (if (org-element-property :quotedp element
) 'quote-section
4225 (property-drawer 'node-property
)
4227 (and (memq type
'(item plain-list
))
4228 (org-element-property :structure element
))
4229 granularity visible-only element
))
4230 ;; ELEMENT has contents. Parse objects inside, if
4231 ;; GRANULARITY allows it.
4232 ((memq granularity
'(object nil
))
4233 (org-element--parse-objects
4234 cbeg
(org-element-property :contents-end element
) element
4235 (org-element-restriction type
))))
4236 (org-element-adopt-elements acc element
)))
4240 (defun org-element--parse-objects (beg end acc restriction
)
4241 "Parse objects between BEG and END and return recursive structure.
4243 Objects are accumulated in ACC.
4245 RESTRICTION is a list of object types which are allowed in the
4250 (while (and (< (point) end
)
4251 (setq candidates
(org-element--get-next-object-candidates
4252 end restriction candidates
)))
4254 (let ((pos (apply 'min
(mapcar 'cdr candidates
))))
4257 (funcall (intern (format "org-element-%s-parser"
4258 (car (rassq pos candidates
)))))))))
4259 ;; 1. Text before any object. Untabify it.
4260 (let ((obj-beg (org-element-property :begin next-object
)))
4261 (unless (= (point) obj-beg
)
4263 (org-element-adopt-elements
4265 (replace-regexp-in-string
4266 "\t" (make-string tab-width ?
)
4267 (buffer-substring-no-properties (point) obj-beg
))))))
4269 (let ((obj-end (org-element-property :end next-object
))
4270 (cont-beg (org-element-property :contents-begin next-object
)))
4271 ;; Fill contents of NEXT-OBJECT by side-effect, if it has
4272 ;; a recursive type.
4274 (memq (car next-object
) org-element-recursive-objects
))
4278 (org-element-property :contents-end next-object
))
4279 (org-element--parse-objects
4280 (point-min) (point-max) next-object
4281 (org-element-restriction next-object
))))
4282 (setq acc
(org-element-adopt-elements acc next-object
))
4283 (goto-char obj-end
))))
4284 ;; 3. Text after last object. Untabify it.
4285 (unless (= (point) end
)
4287 (org-element-adopt-elements
4289 (replace-regexp-in-string
4290 "\t" (make-string tab-width ?
)
4291 (buffer-substring-no-properties (point) end
)))))
4295 (defun org-element--get-next-object-candidates (limit restriction objects
)
4296 "Return an alist of candidates for the next object.
4298 LIMIT bounds the search, and RESTRICTION narrows candidates to
4301 Return value is an alist whose CAR is position and CDR the object
4304 OBJECTS is the previous candidates alist."
4305 ;; Filter out any object found but not belonging to RESTRICTION.
4309 (let ((type (car obj
)))
4310 (memq (or (cdr (assq type org-element-object-successor-alist
))
4314 (let (next-candidates types-to-search
)
4315 ;; If no previous result, search every object type in RESTRICTION.
4316 ;; Otherwise, keep potential candidates (old objects located after
4317 ;; point) and ask to search again those which had matched before.
4318 (if (not objects
) (setq types-to-search restriction
)
4320 (if (< (cdr obj
) (point)) (push (car obj
) types-to-search
)
4321 (push obj next-candidates
)))
4323 ;; Call the appropriate successor function for each type to search
4324 ;; and accumulate matches.
4327 (let* ((successor-fun
4329 (format "org-element-%s-successor"
4330 (or (cdr (assq type org-element-object-successor-alist
))
4332 (obj (funcall successor-fun limit
)))
4333 (and obj
(push obj next-candidates
))))
4340 ;;; Towards A Bijective Process
4342 ;; The parse tree obtained with `org-element-parse-buffer' is really
4343 ;; a snapshot of the corresponding Org buffer. Therefore, it can be
4344 ;; interpreted and expanded into a string with canonical Org syntax.
4345 ;; Hence `org-element-interpret-data'.
4347 ;; The function relies internally on
4348 ;; `org-element--interpret-affiliated-keywords'.
4351 (defun org-element-interpret-data (data &optional parent
)
4352 "Interpret DATA as Org syntax.
4354 DATA is a parse tree, an element, an object or a secondary string
4357 Optional argument PARENT is used for recursive calls. It contains
4358 the element or object containing data, or nil.
4360 Return Org syntax as a string."
4361 (let* ((type (org-element-type data
))
4364 ;; Secondary string.
4367 (lambda (obj) (org-element-interpret-data obj parent
))
4369 ;; Full Org document.
4370 ((eq type
'org-data
)
4372 (lambda (obj) (org-element-interpret-data obj parent
))
4373 (org-element-contents data
) ""))
4374 ;; Plain text: remove `:parent' text property from output.
4375 ((stringp data
) (org-no-properties data
))
4376 ;; Element/Object without contents.
4377 ((not (org-element-contents data
))
4378 (funcall (intern (format "org-element-%s-interpreter" type
))
4380 ;; Element/Object with contents.
4382 (let* ((greaterp (memq type org-element-greater-elements
))
4383 (objectp (and (not greaterp
)
4384 (memq type org-element-recursive-objects
)))
4387 (lambda (obj) (org-element-interpret-data obj data
))
4388 (org-element-contents
4389 (if (or greaterp objectp
) data
4390 ;; Elements directly containing objects must
4391 ;; have their indentation normalized first.
4392 (org-element-normalize-contents
4394 ;; When normalizing first paragraph of an
4395 ;; item or a footnote-definition, ignore
4396 ;; first line's indentation.
4397 (and (eq type
'paragraph
)
4398 (equal data
(car (org-element-contents parent
)))
4399 (memq (org-element-type parent
)
4400 '(footnote-definition item
))))))
4402 (funcall (intern (format "org-element-%s-interpreter" type
))
4404 (if greaterp
(org-element-normalize-contents contents
)
4406 (if (memq type
'(org-data plain-text nil
)) results
4407 ;; Build white spaces. If no `:post-blank' property is
4408 ;; specified, assume its value is 0.
4409 (let ((post-blank (or (org-element-property :post-blank data
) 0)))
4410 (if (memq type org-element-all-objects
)
4411 (concat results
(make-string post-blank
32))
4413 (org-element--interpret-affiliated-keywords data
)
4414 (org-element-normalize-string results
)
4415 (make-string post-blank
10)))))))
4417 (defun org-element--interpret-affiliated-keywords (element)
4418 "Return ELEMENT's affiliated keywords as Org syntax.
4419 If there is no affiliated keyword, return the empty string."
4420 (let ((keyword-to-org
4424 (when (member key org-element-dual-keywords
)
4425 (setq dual
(cdr value
) value
(car value
)))
4428 (format "[%s]" (org-element-interpret-data dual
)))
4430 (if (member key org-element-parsed-keywords
)
4431 (org-element-interpret-data value
)
4436 (let ((value (org-element-property prop element
))
4437 (keyword (upcase (substring (symbol-name prop
) 1))))
4439 (if (or (member keyword org-element-multiple-keywords
)
4440 ;; All attribute keywords can have multiple lines.
4441 (string-match "^ATTR_" keyword
))
4442 (mapconcat (lambda (line) (funcall keyword-to-org keyword line
))
4445 (funcall keyword-to-org keyword value
)))))
4446 ;; List all ELEMENT's properties matching an attribute line or an
4447 ;; affiliated keyword, but ignore translated keywords since they
4448 ;; cannot belong to the property list.
4449 (loop for prop in
(nth 1 element
) by
'cddr
4450 when
(let ((keyword (upcase (substring (symbol-name prop
) 1))))
4451 (or (string-match "^ATTR_" keyword
)
4453 (member keyword org-element-affiliated-keywords
)
4455 org-element-keyword-translation-alist
)))))
4459 ;; Because interpretation of the parse tree must return the same
4460 ;; number of blank lines between elements and the same number of white
4461 ;; space after objects, some special care must be given to white
4464 ;; The first function, `org-element-normalize-string', ensures any
4465 ;; string different from the empty string will end with a single
4466 ;; newline character.
4468 ;; The second function, `org-element-normalize-contents', removes
4469 ;; global indentation from the contents of the current element.
4471 (defun org-element-normalize-string (s)
4472 "Ensure string S ends with a single newline character.
4474 If S isn't a string return it unchanged. If S is the empty
4475 string, return it. Otherwise, return a new string with a single
4476 newline character at its end."
4478 ((not (stringp s
)) s
)
4480 (t (and (string-match "\\(\n[ \t]*\\)*\\'" s
)
4481 (replace-match "\n" nil nil s
)))))
4483 (defun org-element-normalize-contents (element &optional ignore-first
)
4484 "Normalize plain text in ELEMENT's contents.
4486 ELEMENT must only contain plain text and objects.
4488 If optional argument IGNORE-FIRST is non-nil, ignore first line's
4489 indentation to compute maximal common indentation.
4491 Return the normalized element that is element with global
4492 indentation removed from its contents. The function assumes that
4493 indentation is not done with TAB characters."
4494 (let* (ind-list ; for byte-compiler
4495 collect-inds
; for byte-compiler
4498 ;; Return list of indentations within BLOB. This is done by
4499 ;; walking recursively BLOB and updating IND-LIST along the
4500 ;; way. FIRST-FLAG is non-nil when the first string hasn't
4501 ;; been seen yet. It is required as this string is the only
4502 ;; one whose indentation doesn't happen after a newline
4504 (lambda (blob first-flag
)
4507 (when (and first-flag
(stringp object
))
4508 (setq first-flag nil
)
4509 (string-match "\\`\\( *\\)" object
)
4510 (let ((len (length (match-string 1 object
))))
4511 ;; An indentation of zero means no string will be
4512 ;; modified. Quit the process.
4513 (if (zerop len
) (throw 'zero
(setq ind-list nil
))
4514 (push len ind-list
))))
4518 ;; Avoid matching blank or empty lines.
4519 (while (and (string-match "\n\\( *\\)\\(.\\)" object start
)
4520 (not (equal (match-string 2 object
) " ")))
4521 (setq start
(match-end 0))
4522 (push (length (match-string 1 object
)) ind-list
))))
4523 ((memq (org-element-type object
) org-element-recursive-objects
)
4524 (funcall collect-inds object first-flag
))))
4525 (org-element-contents blob
))))))
4526 ;; Collect indentation list in ELEMENT. Possibly remove first
4527 ;; value if IGNORE-FIRST is non-nil.
4528 (catch 'zero
(funcall collect-inds element
(not ignore-first
)))
4529 (if (not ind-list
) element
4530 ;; Build ELEMENT back, replacing each string with the same
4531 ;; string minus common indentation.
4532 (let* (build ; For byte compiler.
4535 (lambda (blob mci first-flag
)
4536 ;; Return BLOB with all its strings indentation
4537 ;; shortened from MCI white spaces. FIRST-FLAG is
4538 ;; non-nil when the first string hasn't been seen
4543 (when (and first-flag
(stringp object
))
4544 (setq first-flag nil
)
4546 (replace-regexp-in-string
4547 (format "\\` \\{%d\\}" mci
) "" object
)))
4550 (replace-regexp-in-string
4551 (format "\n \\{%d\\}" mci
) "\n" object
))
4552 ((memq (org-element-type object
)
4553 org-element-recursive-objects
)
4554 (funcall build object mci first-flag
))
4556 (org-element-contents blob
)))
4558 (funcall build element
(apply 'min ind-list
) (not ignore-first
))))))
4564 ;; The first move is to implement a way to obtain the smallest element
4565 ;; containing point. This is the job of `org-element-at-point'. It
4566 ;; basically jumps back to the beginning of section containing point
4567 ;; and moves, element after element, with
4568 ;; `org-element--current-element' until the container is found. Note:
4569 ;; When using `org-element-at-point', secondary values are never
4570 ;; parsed since the function focuses on elements, not on objects.
4572 ;; At a deeper level, `org-element-context' lists all elements and
4573 ;; objects containing point.
4575 ;; `org-element-nested-p' and `org-element-swap-A-B' may be used
4576 ;; internally by navigation and manipulation tools.
4579 (defun org-element-at-point (&optional keep-trail
)
4580 "Determine closest element around point.
4582 Return value is a list like (TYPE PROPS) where TYPE is the type
4583 of the element and PROPS a plist of properties associated to the
4586 Possible types are defined in `org-element-all-elements'.
4587 Properties depend on element or object type, but always include
4588 `:begin', `:end', `:parent' and `:post-blank' properties.
4590 As a special case, if point is at the very beginning of a list or
4591 sub-list, returned element will be that list instead of the first
4592 item. In the same way, if point is at the beginning of the first
4593 row of a table, returned element will be the table instead of the
4596 If optional argument KEEP-TRAIL is non-nil, the function returns
4597 a list of elements leading to element at point. The list's CAR
4598 is always the element at point. The following positions contain
4599 element's siblings, then parents, siblings of parents, until the
4600 first element of current section."
4601 (org-with-wide-buffer
4602 ;; If at an headline, parse it. It is the sole element that
4603 ;; doesn't require to know about context. Be sure to disallow
4604 ;; secondary string parsing, though.
4605 (if (org-with-limited-levels (org-at-heading-p))
4608 (if (not keep-trail
) (org-element-headline-parser (point-max) t
)
4609 (list (org-element-headline-parser (point-max) t
))))
4610 ;; Otherwise move at the beginning of the section containing
4612 (let ((origin (point))
4613 (end (save-excursion
4614 (org-with-limited-levels (outline-next-heading)) (point)))
4615 element type special-flag trail struct prevs parent
)
4616 (org-with-limited-levels
4617 (if (org-before-first-heading-p) (goto-char (point-min))
4618 (org-back-to-heading)
4620 (org-skip-whitespace)
4622 ;; Parse successively each element, skipping those ending
4623 ;; before original position.
4627 (org-element--current-element end
'element special-flag struct
)
4629 (org-element-put-property element
:parent parent
)
4630 (when keep-trail
(push element trail
))
4632 ;; 1. Skip any element ending before point. Also skip
4633 ;; element ending at point when we're sure that another
4634 ;; element has started.
4635 ((let ((elem-end (org-element-property :end element
)))
4636 (when (or (< elem-end origin
)
4637 (and (= elem-end origin
) (/= elem-end end
)))
4638 (goto-char elem-end
))))
4639 ;; 2. An element containing point is always the element at
4641 ((not (memq type org-element-greater-elements
))
4642 (throw 'exit
(if keep-trail trail element
)))
4643 ;; 3. At any other greater element type, if point is
4644 ;; within contents, move into it.
4646 (let ((cbeg (org-element-property :contents-begin element
))
4647 (cend (org-element-property :contents-end element
)))
4648 (if (or (not cbeg
) (not cend
) (> cbeg origin
) (< cend origin
)
4649 ;; Create an anchor for tables and plain lists:
4650 ;; when point is at the very beginning of these
4651 ;; elements, ignoring affiliated keywords,
4652 ;; target them instead of their contents.
4653 (and (= cbeg origin
) (memq type
'(plain-list table
)))
4654 ;; When point is at contents end, do not move
4655 ;; into elements with an explicit ending, but
4656 ;; return that element instead.
4657 (and (= cend origin
)
4660 drawer dynamic-block inlinetask item
4661 plain-list property-drawer quote-block
4663 (throw 'exit
(if keep-trail trail element
))
4664 (setq parent element
)
4667 (setq special-flag
'item
4668 struct
(org-element-property :structure element
)))
4669 (item (setq special-flag nil
))
4671 (setq special-flag
'node-property struct nil
))
4672 (table (setq special-flag
'table-row struct nil
))
4673 (otherwise (setq special-flag nil struct nil
)))
4675 (goto-char cbeg
)))))))))))
4678 (defun org-element-context (&optional element
)
4679 "Return closest element or object around point.
4681 Return value is a list like (TYPE PROPS) where TYPE is the type
4682 of the element or object and PROPS a plist of properties
4685 Possible types are defined in `org-element-all-elements' and
4686 `org-element-all-objects'. Properties depend on element or
4687 object type, but always include `:begin', `:end', `:parent' and
4690 Optional argument ELEMENT, when non-nil, is the closest element
4691 containing point, as returned by `org-element-at-point'.
4692 Providing it allows for quicker computation."
4693 (org-with-wide-buffer
4694 (let* ((origin (point))
4695 (element (or element
(org-element-at-point)))
4696 (type (org-element-type element
))
4698 ;; Check if point is inside an element containing objects or at
4699 ;; a secondary string. In that case, move to beginning of the
4700 ;; element or secondary string and set END to the other side.
4701 (if (not (or (let ((post (org-element-property :post-affiliated element
)))
4702 (and post
(> post origin
)
4703 (< (org-element-property :begin element
) origin
)
4704 (progn (beginning-of-line)
4705 (looking-at org-element--affiliated-re
)
4706 (member (upcase (match-string 1))
4707 org-element-parsed-keywords
))
4708 ;; We're at an affiliated keyword. Change
4709 ;; type to retrieve correct restrictions.
4710 (setq type
'keyword
)
4711 ;; Determine if we're at main or dual value.
4712 (if (and (match-end 2) (<= origin
(match-end 2)))
4713 (progn (goto-char (match-beginning 2))
4714 (setq end
(match-end 2)))
4715 (goto-char (match-end 0))
4716 (setq end
(line-end-position)))))
4717 (and (eq type
'item
)
4718 (let ((tag (org-element-property :tag element
)))
4722 (search-forward tag
(point-at-eol))
4723 (goto-char (match-beginning 0))
4724 (and (>= origin
(point))
4726 ;; `1+' is required so some
4727 ;; successors can match
4728 ;; properly their object.
4729 (setq end
(1+ (match-end 0)))))))))
4730 (and (memq type
'(headline inlinetask
))
4731 (progn (beginning-of-line)
4732 (skip-chars-forward "* ")
4733 (setq end
(point-at-eol))))
4734 (and (memq type
'(paragraph table-row verse-block
))
4735 (let ((cbeg (org-element-property
4736 :contents-begin element
))
4737 (cend (org-element-property
4738 :contents-end element
)))
4739 (and (>= origin cbeg
)
4741 (progn (goto-char cbeg
) (setq end cend
)))))
4742 (and (eq type
'keyword
)
4743 (let ((key (org-element-property :key element
)))
4744 (and (member key org-element-document-properties
)
4745 (progn (beginning-of-line)
4746 (search-forward key
(line-end-position) t
)
4748 (setq end
(line-end-position))))))))
4750 (let ((restriction (org-element-restriction type
))
4754 (while (setq candidates
(org-element--get-next-object-candidates
4755 end restriction candidates
))
4756 (let ((closest-cand (rassq (apply 'min
(mapcar 'cdr candidates
))
4758 ;; If ORIGIN is before next object in element, there's
4759 ;; no point in looking further.
4760 (if (> (cdr closest-cand
) origin
) (throw 'exit parent
)
4762 (progn (goto-char (cdr closest-cand
))
4763 (funcall (intern (format "org-element-%s-parser"
4764 (car closest-cand
))))))
4765 (cbeg (org-element-property :contents-begin object
))
4766 (cend (org-element-property :contents-end object
))
4767 (obj-end (org-element-property :end object
)))
4769 ;; ORIGIN is after OBJECT, so skip it.
4770 ((<= obj-end origin
)
4771 (if (/= obj-end end
) (goto-char obj-end
)
4773 (org-element-put-property
4774 object
:parent parent
))))
4775 ;; ORIGIN is within a non-recursive object or at
4776 ;; an object boundaries: Return that object.
4777 ((or (not cbeg
) (> cbeg origin
) (< cend origin
))
4779 (org-element-put-property object
:parent parent
)))
4780 ;; Otherwise, move within current object and
4781 ;; restrict search to the end of its contents.
4783 (org-element-put-property object
:parent parent
)
4785 restriction
(org-element-restriction object
)
4789 (defsubst org-element-nested-p
(elem-A elem-B
)
4790 "Non-nil when elements ELEM-A and ELEM-B are nested."
4791 (let ((beg-A (org-element-property :begin elem-A
))
4792 (beg-B (org-element-property :begin elem-B
))
4793 (end-A (org-element-property :end elem-A
))
4794 (end-B (org-element-property :end elem-B
)))
4795 (or (and (>= beg-A beg-B
) (<= end-A end-B
))
4796 (and (>= beg-B beg-A
) (<= end-B end-A
)))))
4798 (defun org-element-swap-A-B (elem-A elem-B
)
4799 "Swap elements ELEM-A and ELEM-B.
4800 Assume ELEM-B is after ELEM-A in the buffer. Leave point at the
4802 (goto-char (org-element-property :begin elem-A
))
4803 ;; There are two special cases when an element doesn't start at bol:
4804 ;; the first paragraph in an item or in a footnote definition.
4805 (let ((specialp (not (bolp))))
4806 ;; Only a paragraph without any affiliated keyword can be moved at
4807 ;; ELEM-A position in such a situation. Note that the case of
4808 ;; a footnote definition is impossible: it cannot contain two
4809 ;; paragraphs in a row because it cannot contain a blank line.
4811 (or (not (eq (org-element-type elem-B
) 'paragraph
))
4812 (/= (org-element-property :begin elem-B
)
4813 (org-element-property :contents-begin elem-B
))))
4814 (error "Cannot swap elements"))
4815 ;; In a special situation, ELEM-A will have no indentation. We'll
4816 ;; give it ELEM-B's (which will in, in turn, have no indentation).
4817 (let* ((ind-B (when specialp
4818 (goto-char (org-element-property :begin elem-B
))
4819 (org-get-indentation)))
4820 (beg-A (org-element-property :begin elem-A
))
4821 (end-A (save-excursion
4822 (goto-char (org-element-property :end elem-A
))
4823 (skip-chars-backward " \r\t\n")
4825 (beg-B (org-element-property :begin elem-B
))
4826 (end-B (save-excursion
4827 (goto-char (org-element-property :end elem-B
))
4828 (skip-chars-backward " \r\t\n")
4830 ;; Store overlays responsible for visibility status. We
4831 ;; also need to store their boundaries as they will be
4832 ;; removed from buffer.
4835 (mapcar (lambda (ov) (list ov
(overlay-start ov
) (overlay-end ov
)))
4836 (overlays-in beg-A end-A
))
4837 (mapcar (lambda (ov) (list ov
(overlay-start ov
) (overlay-end ov
)))
4838 (overlays-in beg-B end-B
))))
4840 (body-A (buffer-substring beg-A end-A
))
4841 (body-B (delete-and-extract-region beg-B end-B
)))
4844 (setq body-B
(replace-regexp-in-string "\\`[ \t]*" "" body-B
))
4845 (org-indent-to-column ind-B
))
4847 ;; Restore ex ELEM-A overlays.
4848 (let ((offset (- beg-B beg-A
)))
4851 (car ov
) (+ (nth 1 ov
) offset
) (+ (nth 2 ov
) offset
)))
4854 (delete-region beg-A end-A
)
4856 ;; Restore ex ELEM-B overlays.
4859 (car ov
) (- (nth 1 ov
) offset
) (- (nth 2 ov
) offset
)))
4861 (goto-char (org-element-property :end elem-B
)))))
4863 (provide 'org-element
)
4866 ;; generated-autoload-file: "org-loaddefs.el"
4869 ;;; org-element.el ends here