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 a 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]*\n\\)\\{2,\\}") 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', `:optional-title', `:begin',
733 `:end', `:pre-blank', `:hiddenp', `:contents-begin' and
734 `:contents-end', `:level', `:priority', `:tags',
735 `:todo-keyword',`:todo-type', `:scheduled', `:deadline',
736 `:closed', `:quotedp', `:archivedp', `:commentedp' and
737 `:footnote-section-p' keywords.
739 The plist also contains any property set in the property drawer,
740 with its name in upper cases and colons added at the
741 beginning (i.e. `:CUSTOM_ID').
743 When RAW-SECONDARY-P is non-nil, headline's title will not be
744 parsed as a secondary string, but as a plain string instead.
746 Assume point is at beginning of the headline."
748 (let* ((components (org-heading-components))
749 (level (nth 1 components
))
750 (todo (nth 2 components
))
752 (and todo
(if (member todo org-done-keywords
) 'done
'todo
)))
753 (tags (let ((raw-tags (nth 5 components
)))
754 (and raw-tags
(org-split-string raw-tags
":"))))
755 (raw-value (or (nth 4 components
) ""))
757 (let ((case-fold-search nil
))
758 (string-match (format "^%s\\( \\|$\\)" org-quote-string
)
761 (let ((case-fold-search nil
))
762 (string-match (format "^%s\\( \\|$\\)" org-comment-string
)
764 (archivedp (member org-archive-tag tags
))
765 (footnote-section-p (and org-footnote-section
766 (string= org-footnote-section raw-value
)))
767 ;; Upcase property names. It avoids confusion between
768 ;; properties obtained through property drawer and default
769 ;; properties from the parser (e.g. `:end' and :END:)
776 (intern (concat ":" (upcase (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 (let ((opt-title (org-element-property :OPTIONAL_TITLE headline
)))
853 (org-element-put-property
854 headline
:optional-title
855 (if raw-secondary-p opt-title
856 (org-element-parse-secondary-string
857 opt-title
(org-element-restriction 'headline
) headline
)))))
858 (org-element-put-property
860 (if raw-secondary-p raw-value
861 (org-element-parse-secondary-string
862 raw-value
(org-element-restriction 'headline
) headline
)))))))
864 (defun org-element-headline-interpreter (headline contents
)
865 "Interpret HEADLINE element as Org syntax.
866 CONTENTS is the contents of the element."
867 (let* ((level (org-element-property :level headline
))
868 (todo (org-element-property :todo-keyword headline
))
869 (priority (org-element-property :priority headline
))
870 (title (org-element-interpret-data
871 (org-element-property :title headline
)))
872 (tags (let ((tag-list (if (org-element-property :archivedp headline
)
873 (cons org-archive-tag
874 (org-element-property :tags headline
))
875 (org-element-property :tags headline
))))
877 (format ":%s:" (mapconcat 'identity tag-list
":")))))
878 (commentedp (org-element-property :commentedp headline
))
879 (quotedp (org-element-property :quotedp headline
))
880 (pre-blank (or (org-element-property :pre-blank headline
) 0))
881 (heading (concat (make-string level ?
*)
882 (and todo
(concat " " todo
))
883 (and quotedp
(concat " " org-quote-string
))
884 (and commentedp
(concat " " org-comment-string
))
886 (format " [#%s]" (char-to-string priority
)))
887 (cond ((and org-footnote-section
888 (org-element-property
889 :footnote-section-p headline
))
890 (concat " " org-footnote-section
))
891 (title (concat " " title
))))))
896 ((zerop org-tags-column
) (format " %s" tags
))
897 ((< org-tags-column
0)
900 (max (- (+ org-tags-column
(length heading
) (length tags
))) 1)
905 (make-string (max (- org-tags-column
(length heading
)) 1) ?
)
907 (make-string (1+ pre-blank
) 10)
913 (defun org-element-inlinetask-parser (limit &optional raw-secondary-p
)
914 "Parse an inline task.
916 Return a list whose CAR is `inlinetask' and CDR is a plist
917 containing `:title', `:begin', `:end', `:hiddenp',
918 `:contents-begin' and `:contents-end', `:level', `:priority',
919 `:raw-value', `:tags', `:todo-keyword', `:todo-type',
920 `:scheduled', `:deadline', `:closed' and `:post-blank' keywords.
922 The plist also contains any property set in the property drawer,
923 with its name in upper cases and colons added at the
924 beginning (i.e. `:CUSTOM_ID').
926 When optional argument RAW-SECONDARY-P is non-nil, inline-task's
927 title will not be parsed as a secondary string, but as a plain
930 Assume point is at beginning of the inline task."
932 (let* ((begin (point))
933 (components (org-heading-components))
934 (todo (nth 2 components
))
936 (if (member todo org-done-keywords
) 'done
'todo
)))
937 (tags (let ((raw-tags (nth 5 components
)))
938 (and raw-tags
(org-split-string raw-tags
":"))))
939 (raw-value (or (nth 4 components
) ""))
940 ;; Upcase property names. It avoids confusion between
941 ;; properties obtained through property drawer and default
942 ;; properties from the parser (e.g. `:end' and :END:)
949 (intern (concat ":" (upcase (car p
))))
951 (org-entry-properties nil
'standard
))
954 ;; Read time properties on the line below the inlinetask
957 (when (progn (forward-line)
958 (looking-at org-planning-or-clock-line-re
))
959 (let ((end (line-end-position)) plist
)
960 (while (re-search-forward
961 org-keyword-time-not-clock-regexp end t
)
962 (goto-char (match-end 1))
963 (skip-chars-forward " \t")
964 (let ((keyword (match-string 1))
965 (time (org-element-timestamp-parser)))
966 (cond ((equal keyword org-scheduled-string
)
967 (setq plist
(plist-put plist
:scheduled time
)))
968 ((equal keyword org-deadline-string
)
969 (setq plist
(plist-put plist
:deadline time
)))
970 (t (setq plist
(plist-put plist
:closed time
))))))
972 (task-end (save-excursion
974 (and (re-search-forward "^\\*+ END" limit t
)
975 (match-beginning 0))))
976 (contents-begin (progn (forward-line)
977 (and task-end
(< (point) task-end
) (point))))
978 (hidden (and contents-begin
(org-invisible-p2)))
979 (contents-end (and contents-begin task-end
))
980 (before-blank (if (not task-end
) (point)
984 (end (progn (skip-chars-forward " \r\t\n" limit
)
985 (skip-chars-backward " \t")
986 (if (bolp) (point) (line-end-position))))
990 (list :raw-value raw-value
994 :contents-begin contents-begin
995 :contents-end contents-end
996 :level
(nth 1 components
)
997 :priority
(nth 3 components
)
1000 :todo-type todo-type
1001 :post-blank
(count-lines before-blank end
))
1004 (org-element-put-property
1006 (if raw-secondary-p raw-value
1007 (org-element-parse-secondary-string
1009 (org-element-restriction 'inlinetask
)
1012 (defun org-element-inlinetask-interpreter (inlinetask contents
)
1013 "Interpret INLINETASK element as Org syntax.
1014 CONTENTS is the contents of inlinetask."
1015 (let* ((level (org-element-property :level inlinetask
))
1016 (todo (org-element-property :todo-keyword inlinetask
))
1017 (priority (org-element-property :priority inlinetask
))
1018 (title (org-element-interpret-data
1019 (org-element-property :title inlinetask
)))
1020 (tags (let ((tag-list (org-element-property :tags inlinetask
)))
1022 (format ":%s:" (mapconcat 'identity tag-list
":")))))
1023 (task (concat (make-string level ?
*)
1024 (and todo
(concat " " todo
))
1026 (format " [#%s]" (char-to-string priority
)))
1027 (and title
(concat " " title
)))))
1032 ((zerop org-tags-column
) (format " %s" tags
))
1033 ((< org-tags-column
0)
1036 (max (- (+ org-tags-column
(length task
) (length tags
))) 1)
1041 (make-string (max (- org-tags-column
(length task
)) 1) ?
)
1043 ;; Prefer degenerate inlinetasks when there are no
1048 (make-string level ?
*) " END")))))
1053 (defun org-element-item-parser (limit struct
&optional raw-secondary-p
)
1056 STRUCT is the structure of the plain list.
1058 Return a list whose CAR is `item' and CDR is a plist containing
1059 `:bullet', `:begin', `:end', `:contents-begin', `:contents-end',
1060 `:checkbox', `:counter', `:tag', `:structure', `:hiddenp' and
1061 `:post-blank' keywords.
1063 When optional argument RAW-SECONDARY-P is non-nil, item's tag, if
1064 any, will not be parsed as a secondary string, but as a plain
1067 Assume point is at the beginning of the item."
1070 (looking-at org-list-full-item-re
)
1071 (let* ((begin (point))
1072 (bullet (org-match-string-no-properties 1))
1073 (checkbox (let ((box (org-match-string-no-properties 3)))
1074 (cond ((equal "[ ]" box
) 'off
)
1075 ((equal "[X]" box
) 'on
)
1076 ((equal "[-]" box
) 'trans
))))
1077 (counter (let ((c (org-match-string-no-properties 2)))
1081 ((string-match "[A-Za-z]" c
)
1082 (- (string-to-char (upcase (match-string 0 c
)))
1084 ((string-match "[0-9]+" c
)
1085 (string-to-number (match-string 0 c
)))))))
1086 (end (save-excursion (goto-char (org-list-get-item-end begin struct
))
1087 (unless (bolp) (forward-line))
1091 ;; Ignore tags in un-ordered lists: they are just
1092 ;; a part of item's body.
1093 (if (and (match-beginning 4)
1094 (save-match-data (string-match "[.)]" bullet
)))
1097 (skip-chars-forward " \r\t\n" limit
)
1098 ;; If first line isn't empty, contents really start
1099 ;; at the text after item's meta-data.
1100 (if (= (point-at-bol) begin
) (point) (point-at-bol))))
1101 (hidden (progn (forward-line)
1102 (and (not (= (point) end
)) (org-invisible-p2))))
1103 (contents-end (progn (goto-char end
)
1104 (skip-chars-backward " \r\t\n")
1109 (list :bullet bullet
1112 ;; CONTENTS-BEGIN and CONTENTS-END may be
1113 ;; mixed up in the case of an empty item
1114 ;; separated from the next by a blank line.
1115 ;; Thus ensure the former is always the
1117 :contents-begin
(min contents-begin contents-end
)
1118 :contents-end
(max contents-begin contents-end
)
1123 :post-blank
(count-lines contents-end end
)))))
1124 (org-element-put-property
1126 (let ((raw-tag (org-list-get-tag begin struct
)))
1128 (if raw-secondary-p raw-tag
1129 (org-element-parse-secondary-string
1130 raw-tag
(org-element-restriction 'item
) item
))))))))
1132 (defun org-element-item-interpreter (item contents
)
1133 "Interpret ITEM element as Org syntax.
1134 CONTENTS is the contents of the element."
1135 (let* ((bullet (org-list-bullet-string (org-element-property :bullet item
)))
1136 (checkbox (org-element-property :checkbox item
))
1137 (counter (org-element-property :counter item
))
1138 (tag (let ((tag (org-element-property :tag item
)))
1139 (and tag
(org-element-interpret-data tag
))))
1140 ;; Compute indentation.
1141 (ind (make-string (length bullet
) 32))
1142 (item-starts-with-par-p
1143 (eq (org-element-type (car (org-element-contents item
)))
1148 (and counter
(format "[@%d] " counter
))
1153 (and tag
(format "%s :: " tag
))
1154 (let ((contents (replace-regexp-in-string
1155 "\\(^\\)[ \t]*\\S-" ind contents nil nil
1)))
1156 (if item-starts-with-par-p
(org-trim contents
)
1157 (concat "\n" contents
))))))
1162 (defun org-element-plain-list-parser (limit affiliated structure
)
1163 "Parse a plain list.
1165 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1166 the buffer position at the beginning of the first affiliated
1167 keyword and CDR is a plist of affiliated keywords along with
1168 their value. STRUCTURE is the structure of the plain list being
1171 Return a list whose CAR is `plain-list' and CDR is a plist
1172 containing `:type', `:begin', `:end', `:contents-begin' and
1173 `:contents-end', `:structure', `:post-blank' and
1174 `:post-affiliated' keywords.
1176 Assume point is at the beginning of the list."
1178 (let* ((struct (or structure
(org-list-struct)))
1179 (prevs (org-list-prevs-alist struct
))
1180 (parents (org-list-parents-alist struct
))
1181 (type (org-list-get-list-type (point) struct prevs
))
1182 (contents-begin (point))
1183 (begin (car affiliated
))
1185 (progn (goto-char (org-list-get-list-end (point) struct prevs
))
1186 (unless (bolp) (forward-line))
1188 (end (progn (skip-chars-forward " \r\t\n" limit
)
1189 (skip-chars-backward " \t")
1190 (if (bolp) (point) (line-end-position)))))
1197 :contents-begin contents-begin
1198 :contents-end contents-end
1200 :post-blank
(count-lines contents-end end
)
1201 :post-affiliated contents-begin
)
1202 (cdr affiliated
))))))
1204 (defun org-element-plain-list-interpreter (plain-list contents
)
1205 "Interpret PLAIN-LIST element as Org syntax.
1206 CONTENTS is the contents of the element."
1209 (goto-char (point-min))
1214 ;;;; Property Drawer
1216 (defun org-element-property-drawer-parser (limit affiliated
)
1217 "Parse a property drawer.
1219 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1220 the buffer position at the beginning of the first affiliated
1221 keyword and CDR is a plist of affiliated keywords along with
1224 Return a list whose CAR is `property-drawer' and CDR is a plist
1225 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1226 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
1228 Assume point is at the beginning of the property drawer."
1230 (let ((case-fold-search t
))
1231 (if (not (save-excursion
1232 (re-search-forward "^[ \t]*:END:[ \t]*$" limit t
)))
1233 ;; Incomplete drawer: parse it as a paragraph.
1234 (org-element-paragraph-parser limit affiliated
)
1236 (let* ((drawer-end-line (match-beginning 0))
1237 (begin (car affiliated
))
1238 (post-affiliated (point))
1239 (contents-begin (progn (forward-line)
1240 (and (< (point) drawer-end-line
)
1242 (contents-end (and contents-begin drawer-end-line
))
1243 (hidden (org-invisible-p2))
1244 (pos-before-blank (progn (goto-char drawer-end-line
)
1247 (end (progn (skip-chars-forward " \r\t\n" limit
)
1248 (skip-chars-backward " \t")
1249 (if (bolp) (point) (line-end-position)))))
1250 (list 'property-drawer
1255 :contents-begin contents-begin
1256 :contents-end contents-end
1257 :post-blank
(count-lines pos-before-blank end
)
1258 :post-affiliated post-affiliated
)
1259 (cdr affiliated
)))))))))
1261 (defun org-element-property-drawer-interpreter (property-drawer contents
)
1262 "Interpret PROPERTY-DRAWER element as Org syntax.
1263 CONTENTS is the properties within the drawer."
1264 (format ":PROPERTIES:\n%s:END:" contents
))
1269 (defun org-element-quote-block-parser (limit affiliated
)
1270 "Parse a quote block.
1272 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1273 the buffer position at the beginning of the first affiliated
1274 keyword and CDR is a plist of affiliated keywords along with
1277 Return a list whose CAR is `quote-block' and CDR is a plist
1278 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1279 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
1281 Assume point is at the beginning of the block."
1282 (let ((case-fold-search t
))
1283 (if (not (save-excursion
1284 (re-search-forward "^[ \t]*#\\+END_QUOTE[ \t]*$" limit t
)))
1285 ;; Incomplete block: parse it as a paragraph.
1286 (org-element-paragraph-parser limit affiliated
)
1287 (let ((block-end-line (match-beginning 0)))
1289 (let* ((begin (car affiliated
))
1290 (post-affiliated (point))
1291 ;; Empty blocks have no contents.
1292 (contents-begin (progn (forward-line)
1293 (and (< (point) block-end-line
)
1295 (contents-end (and contents-begin block-end-line
))
1296 (hidden (org-invisible-p2))
1297 (pos-before-blank (progn (goto-char block-end-line
)
1300 (end (progn (skip-chars-forward " \r\t\n" limit
)
1301 (skip-chars-backward " \t")
1302 (if (bolp) (point) (line-end-position)))))
1308 :contents-begin contents-begin
1309 :contents-end contents-end
1310 :post-blank
(count-lines pos-before-blank end
)
1311 :post-affiliated post-affiliated
)
1312 (cdr affiliated
)))))))))
1314 (defun org-element-quote-block-interpreter (quote-block contents
)
1315 "Interpret QUOTE-BLOCK element as Org syntax.
1316 CONTENTS is the contents of the element."
1317 (format "#+BEGIN_QUOTE\n%s#+END_QUOTE" contents
))
1322 (defun org-element-section-parser (limit)
1325 LIMIT bounds the search.
1327 Return a list whose CAR is `section' and CDR is a plist
1328 containing `:begin', `:end', `:contents-begin', `contents-end'
1329 and `:post-blank' keywords."
1331 ;; Beginning of section is the beginning of the first non-blank
1332 ;; line after previous headline.
1333 (let ((begin (point))
1334 (end (progn (org-with-limited-levels (outline-next-heading))
1336 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
1342 :contents-begin begin
1343 :contents-end pos-before-blank
1344 :post-blank
(count-lines pos-before-blank end
))))))
1346 (defun org-element-section-interpreter (section contents
)
1347 "Interpret SECTION element as Org syntax.
1348 CONTENTS is the contents of the element."
1354 (defun org-element-special-block-parser (limit affiliated
)
1355 "Parse a special block.
1357 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1358 the buffer position at the beginning of the first affiliated
1359 keyword and CDR is a plist of affiliated keywords along with
1362 Return a list whose CAR is `special-block' and CDR is a plist
1363 containing `:type', `:begin', `:end', `:hiddenp',
1364 `:contents-begin', `:contents-end', `:post-blank' and
1365 `:post-affiliated' keywords.
1367 Assume point is at the beginning of the block."
1368 (let* ((case-fold-search t
)
1369 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(S-+\\)")
1370 (upcase (match-string-no-properties 1)))))
1371 (if (not (save-excursion
1373 (format "^[ \t]*#\\+END_%s[ \t]*$" type
) limit t
)))
1374 ;; Incomplete block: parse it as a paragraph.
1375 (org-element-paragraph-parser limit affiliated
)
1376 (let ((block-end-line (match-beginning 0)))
1378 (let* ((begin (car affiliated
))
1379 (post-affiliated (point))
1380 ;; Empty blocks have no contents.
1381 (contents-begin (progn (forward-line)
1382 (and (< (point) block-end-line
)
1384 (contents-end (and contents-begin block-end-line
))
1385 (hidden (org-invisible-p2))
1386 (pos-before-blank (progn (goto-char block-end-line
)
1389 (end (progn (skip-chars-forward " \r\t\n" limit
)
1390 (skip-chars-backward " \t")
1391 (if (bolp) (point) (line-end-position)))))
1392 (list 'special-block
1398 :contents-begin contents-begin
1399 :contents-end contents-end
1400 :post-blank
(count-lines pos-before-blank end
)
1401 :post-affiliated post-affiliated
)
1402 (cdr affiliated
)))))))))
1404 (defun org-element-special-block-interpreter (special-block contents
)
1405 "Interpret SPECIAL-BLOCK element as Org syntax.
1406 CONTENTS is the contents of the element."
1407 (let ((block-type (org-element-property :type special-block
)))
1408 (format "#+BEGIN_%s\n%s#+END_%s" block-type contents block-type
)))
1414 ;; For each element, a parser and an interpreter are also defined.
1415 ;; Both follow the same naming convention used for greater elements.
1417 ;; Also, as for greater elements, adding a new element type is done
1418 ;; through the following steps: implement a parser and an interpreter,
1419 ;; tweak `org-element--current-element' so that it recognizes the new
1420 ;; type and add that new type to `org-element-all-elements'.
1422 ;; As a special case, when the newly defined type is a block type,
1423 ;; `org-element-block-name-alist' has to be modified accordingly.
1428 (defun org-element-babel-call-parser (limit affiliated
)
1429 "Parse a babel call.
1431 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1432 the buffer position at the beginning of the first affiliated
1433 keyword and CDR is a plist of affiliated keywords along with
1436 Return a list whose CAR is `babel-call' and CDR is a plist
1437 containing `:begin', `:end', `:info', `:post-blank' and
1438 `:post-affiliated' as keywords."
1440 (let ((case-fold-search t
)
1441 (info (progn (looking-at org-babel-block-lob-one-liner-regexp
)
1442 (org-babel-lob-get-info)))
1443 (begin (car affiliated
))
1444 (post-affiliated (point))
1445 (pos-before-blank (progn (forward-line) (point)))
1446 (end (progn (skip-chars-forward " \r\t\n" limit
)
1447 (skip-chars-backward " \t")
1448 (if (bolp) (point) (line-end-position)))))
1454 :post-blank
(count-lines pos-before-blank end
)
1455 :post-affiliated post-affiliated
)
1456 (cdr affiliated
))))))
1458 (defun org-element-babel-call-interpreter (babel-call contents
)
1459 "Interpret BABEL-CALL element as Org syntax.
1461 (let* ((babel-info (org-element-property :info babel-call
))
1462 (main (car babel-info
))
1463 (post-options (nth 1 babel-info
)))
1465 (if (not (string-match "\\[\\(\\[.*?\\]\\)\\]" main
)) main
1466 ;; Remove redundant square brackets.
1467 (replace-match (match-string 1 main
) nil nil main
))
1468 (and post-options
(format "[%s]" post-options
)))))
1473 (defun org-element-clock-parser (limit)
1476 LIMIT bounds the search.
1478 Return a list whose CAR is `clock' and CDR is a plist containing
1479 `:status', `:value', `:time', `:begin', `:end' and `:post-blank'
1482 (let* ((case-fold-search nil
)
1484 (value (progn (search-forward org-clock-string
(line-end-position) t
)
1485 (skip-chars-forward " \t")
1486 (org-element-timestamp-parser)))
1487 (duration (and (search-forward " => " (line-end-position) t
)
1488 (progn (skip-chars-forward " \t")
1489 (looking-at "\\(\\S-+\\)[ \t]*$"))
1490 (org-match-string-no-properties 1)))
1491 (status (if duration
'closed
'running
))
1492 (post-blank (let ((before-blank (progn (forward-line) (point))))
1493 (skip-chars-forward " \r\t\n" limit
)
1494 (skip-chars-backward " \t")
1495 (unless (bolp) (end-of-line))
1496 (count-lines before-blank
(point))))
1499 (list :status status
1504 :post-blank post-blank
)))))
1506 (defun org-element-clock-interpreter (clock contents
)
1507 "Interpret CLOCK element as Org syntax.
1509 (concat org-clock-string
" "
1510 (org-element-timestamp-interpreter
1511 (org-element-property :value clock
) nil
)
1512 (let ((duration (org-element-property :duration clock
)))
1517 (org-split-string duration
":")))))))
1522 (defun org-element-comment-parser (limit affiliated
)
1525 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1526 the buffer position at the beginning of the first affiliated
1527 keyword and CDR is a plist of affiliated keywords along with
1530 Return a list whose CAR is `comment' and CDR is a plist
1531 containing `:begin', `:end', `:value', `:post-blank',
1532 `:post-affiliated' keywords.
1534 Assume point is at comment beginning."
1536 (let* ((begin (car affiliated
))
1537 (post-affiliated (point))
1538 (value (prog2 (looking-at "[ \t]*# ?")
1539 (buffer-substring-no-properties
1540 (match-end 0) (line-end-position))
1543 ;; Get comments ending.
1545 (while (and (< (point) limit
) (looking-at "[ \t]*#\\( \\|$\\)"))
1546 ;; Accumulate lines without leading hash and first
1551 (buffer-substring-no-properties
1552 (match-end 0) (line-end-position))))
1555 (end (progn (goto-char com-end
)
1556 (skip-chars-forward " \r\t\n" limit
)
1557 (skip-chars-backward " \t")
1558 (if (bolp) (point) (line-end-position)))))
1564 :post-blank
(count-lines com-end end
)
1565 :post-affiliated post-affiliated
)
1566 (cdr affiliated
))))))
1568 (defun org-element-comment-interpreter (comment contents
)
1569 "Interpret COMMENT element as Org syntax.
1571 (replace-regexp-in-string "^" "# " (org-element-property :value comment
)))
1576 (defun org-element-comment-block-parser (limit affiliated
)
1577 "Parse an export block.
1579 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1580 the buffer position at the beginning of the first affiliated
1581 keyword and CDR is a plist of affiliated keywords along with
1584 Return a list whose CAR is `comment-block' and CDR is a plist
1585 containing `:begin', `:end', `:hiddenp', `:value', `:post-blank'
1586 and `:post-affiliated' keywords.
1588 Assume point is at comment block beginning."
1589 (let ((case-fold-search t
))
1590 (if (not (save-excursion
1591 (re-search-forward "^[ \t]*#\\+END_COMMENT[ \t]*$" limit t
)))
1592 ;; Incomplete block: parse it as a paragraph.
1593 (org-element-paragraph-parser limit affiliated
)
1594 (let ((contents-end (match-beginning 0)))
1596 (let* ((begin (car affiliated
))
1597 (post-affiliated (point))
1598 (contents-begin (progn (forward-line) (point)))
1599 (hidden (org-invisible-p2))
1600 (pos-before-blank (progn (goto-char contents-end
)
1603 (end (progn (skip-chars-forward " \r\t\n" limit
)
1604 (skip-chars-backward " \t")
1605 (if (bolp) (point) (line-end-position))))
1606 (value (buffer-substring-no-properties
1607 contents-begin contents-end
)))
1608 (list 'comment-block
1614 :post-blank
(count-lines pos-before-blank end
)
1615 :post-affiliated post-affiliated
)
1616 (cdr affiliated
)))))))))
1618 (defun org-element-comment-block-interpreter (comment-block contents
)
1619 "Interpret COMMENT-BLOCK element as Org syntax.
1621 (format "#+BEGIN_COMMENT\n%s#+END_COMMENT"
1622 (org-remove-indentation (org-element-property :value comment-block
))))
1627 (defun org-element-diary-sexp-parser (limit affiliated
)
1628 "Parse a diary sexp.
1630 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1631 the buffer position at the beginning of the first affiliated
1632 keyword and CDR is a plist of affiliated keywords along with
1635 Return a list whose CAR is `diary-sexp' and CDR is a plist
1636 containing `:begin', `:end', `:value', `:post-blank' and
1637 `:post-affiliated' keywords."
1639 (let ((begin (car affiliated
))
1640 (post-affiliated (point))
1641 (value (progn (looking-at "\\(%%(.*\\)[ \t]*$")
1642 (org-match-string-no-properties 1)))
1643 (pos-before-blank (progn (forward-line) (point)))
1644 (end (progn (skip-chars-forward " \r\t\n" limit
)
1645 (skip-chars-backward " \t")
1646 (if (bolp) (point) (line-end-position)))))
1652 :post-blank
(count-lines pos-before-blank end
)
1653 :post-affiliated post-affiliated
)
1654 (cdr affiliated
))))))
1656 (defun org-element-diary-sexp-interpreter (diary-sexp contents
)
1657 "Interpret DIARY-SEXP as Org syntax.
1659 (org-element-property :value diary-sexp
))
1664 (defun org-element-example-block-parser (limit affiliated
)
1665 "Parse an example block.
1667 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1668 the buffer position at the beginning of the first affiliated
1669 keyword and CDR is a plist of affiliated keywords along with
1672 Return a list whose CAR is `example-block' and CDR is a plist
1673 containing `:begin', `:end', `:number-lines', `:preserve-indent',
1674 `:retain-labels', `:use-labels', `:label-fmt', `:hiddenp',
1675 `:switches', `:value', `:post-blank' and `:post-affiliated'
1677 (let ((case-fold-search t
))
1678 (if (not (save-excursion
1679 (re-search-forward "^[ \t]*#\\+END_EXAMPLE[ \t]*$" limit t
)))
1680 ;; Incomplete block: parse it as a paragraph.
1681 (org-element-paragraph-parser limit affiliated
)
1682 (let ((contents-end (match-beginning 0)))
1685 (progn (looking-at "^[ \t]*#\\+BEGIN_EXAMPLE\\(?: +\\(.*\\)\\)?")
1686 (org-match-string-no-properties 1)))
1687 ;; Switches analysis
1688 (number-lines (cond ((not switches
) nil
)
1689 ((string-match "-n\\>" switches
) 'new
)
1690 ((string-match "+n\\>" switches
) 'continued
)))
1691 (preserve-indent (and switches
(string-match "-i\\>" switches
)))
1692 ;; Should labels be retained in (or stripped from) example
1696 (not (string-match "-r\\>" switches
))
1697 (and number-lines
(string-match "-k\\>" switches
))))
1698 ;; What should code-references use - labels or
1702 (and retain-labels
(not (string-match "-k\\>" switches
)))))
1703 (label-fmt (and switches
1704 (string-match "-l +\"\\([^\"\n]+\\)\"" switches
)
1705 (match-string 1 switches
)))
1706 ;; Standard block parsing.
1707 (begin (car affiliated
))
1708 (post-affiliated (point))
1709 (contents-begin (progn (forward-line) (point)))
1710 (hidden (org-invisible-p2))
1711 (value (org-unescape-code-in-string
1712 (buffer-substring-no-properties
1713 contents-begin contents-end
)))
1714 (pos-before-blank (progn (goto-char contents-end
)
1717 (end (progn (skip-chars-forward " \r\t\n" limit
)
1718 (skip-chars-backward " \t")
1719 (if (bolp) (point) (line-end-position)))))
1720 (list 'example-block
1726 :number-lines number-lines
1727 :preserve-indent preserve-indent
1728 :retain-labels retain-labels
1729 :use-labels use-labels
1730 :label-fmt label-fmt
1732 :post-blank
(count-lines pos-before-blank end
)
1733 :post-affiliated post-affiliated
)
1734 (cdr affiliated
)))))))))
1736 (defun org-element-example-block-interpreter (example-block contents
)
1737 "Interpret EXAMPLE-BLOCK element as Org syntax.
1739 (let ((switches (org-element-property :switches example-block
)))
1740 (concat "#+BEGIN_EXAMPLE" (and switches
(concat " " switches
)) "\n"
1741 (org-remove-indentation
1742 (org-escape-code-in-string
1743 (org-element-property :value example-block
)))
1749 (defun org-element-export-block-parser (limit affiliated
)
1750 "Parse an export block.
1752 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1753 the buffer position at the beginning of the first affiliated
1754 keyword and CDR is a plist of affiliated keywords along with
1757 Return a list whose CAR is `export-block' and CDR is a plist
1758 containing `:begin', `:end', `:type', `:hiddenp', `:value',
1759 `:post-blank' and `:post-affiliated' keywords.
1761 Assume point is at export-block beginning."
1762 (let* ((case-fold-search t
)
1763 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
1764 (upcase (org-match-string-no-properties 1)))))
1765 (if (not (save-excursion
1767 (format "^[ \t]*#\\+END_%s[ \t]*$" type
) limit t
)))
1768 ;; Incomplete block: parse it as a paragraph.
1769 (org-element-paragraph-parser limit affiliated
)
1770 (let ((contents-end (match-beginning 0)))
1772 (let* ((begin (car affiliated
))
1773 (post-affiliated (point))
1774 (contents-begin (progn (forward-line) (point)))
1775 (hidden (org-invisible-p2))
1776 (pos-before-blank (progn (goto-char contents-end
)
1779 (end (progn (skip-chars-forward " \r\t\n" limit
)
1780 (skip-chars-backward " \t")
1781 (if (bolp) (point) (line-end-position))))
1782 (value (buffer-substring-no-properties contents-begin
1791 :post-blank
(count-lines pos-before-blank end
)
1792 :post-affiliated post-affiliated
)
1793 (cdr affiliated
)))))))))
1795 (defun org-element-export-block-interpreter (export-block contents
)
1796 "Interpret EXPORT-BLOCK element as Org syntax.
1798 (let ((type (org-element-property :type export-block
)))
1799 (concat (format "#+BEGIN_%s\n" type
)
1800 (org-element-property :value export-block
)
1801 (format "#+END_%s" type
))))
1806 (defun org-element-fixed-width-parser (limit affiliated
)
1807 "Parse a fixed-width section.
1809 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1810 the buffer position at the beginning of the first affiliated
1811 keyword and CDR is a plist of affiliated keywords along with
1814 Return a list whose CAR is `fixed-width' and CDR is a plist
1815 containing `:begin', `:end', `:value', `:post-blank' and
1816 `:post-affiliated' keywords.
1818 Assume point is at the beginning of the fixed-width area."
1820 (let* ((begin (car affiliated
))
1821 (post-affiliated (point))
1825 (while (and (< (point) limit
)
1826 (looking-at "[ \t]*:\\( \\|$\\)"))
1827 ;; Accumulate text without starting colons.
1830 (buffer-substring-no-properties
1831 (match-end 0) (point-at-eol))
1835 (end (progn (skip-chars-forward " \r\t\n" limit
)
1836 (skip-chars-backward " \t")
1837 (if (bolp) (point) (line-end-position)))))
1843 :post-blank
(count-lines end-area end
)
1844 :post-affiliated post-affiliated
)
1845 (cdr affiliated
))))))
1847 (defun org-element-fixed-width-interpreter (fixed-width contents
)
1848 "Interpret FIXED-WIDTH element as Org syntax.
1850 (replace-regexp-in-string
1851 "^" ": " (substring (org-element-property :value fixed-width
) 0 -
1)))
1854 ;;;; Horizontal Rule
1856 (defun org-element-horizontal-rule-parser (limit affiliated
)
1857 "Parse an horizontal rule.
1859 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1860 the buffer position at the beginning of the first affiliated
1861 keyword and CDR is a plist of affiliated keywords along with
1864 Return a list whose CAR is `horizontal-rule' and CDR is a plist
1865 containing `:begin', `:end', `:post-blank' and `:post-affiliated'
1868 (let ((begin (car affiliated
))
1869 (post-affiliated (point))
1870 (post-hr (progn (forward-line) (point)))
1871 (end (progn (skip-chars-forward " \r\t\n" limit
)
1872 (skip-chars-backward " \t")
1873 (if (bolp) (point) (line-end-position)))))
1874 (list 'horizontal-rule
1878 :post-blank
(count-lines post-hr end
)
1879 :post-affiliated post-affiliated
)
1880 (cdr affiliated
))))))
1882 (defun org-element-horizontal-rule-interpreter (horizontal-rule contents
)
1883 "Interpret HORIZONTAL-RULE element as Org syntax.
1890 (defun org-element-keyword-parser (limit affiliated
)
1891 "Parse a keyword at point.
1893 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1894 the buffer position at the beginning of the first affiliated
1895 keyword and CDR is a plist of affiliated keywords along with
1898 Return a list whose CAR is `keyword' and CDR is a plist
1899 containing `:key', `:value', `:begin', `:end', `:post-blank' and
1900 `:post-affiliated' keywords."
1902 (let ((begin (car affiliated
))
1903 (post-affiliated (point))
1904 (key (progn (looking-at "[ \t]*#\\+\\(\\S-+*\\):")
1905 (upcase (org-match-string-no-properties 1))))
1906 (value (org-trim (buffer-substring-no-properties
1907 (match-end 0) (point-at-eol))))
1908 (pos-before-blank (progn (forward-line) (point)))
1909 (end (progn (skip-chars-forward " \r\t\n" limit
)
1910 (skip-chars-backward " \t")
1911 (if (bolp) (point) (line-end-position)))))
1918 :post-blank
(count-lines pos-before-blank end
)
1919 :post-affiliated post-affiliated
)
1920 (cdr affiliated
))))))
1922 (defun org-element-keyword-interpreter (keyword contents
)
1923 "Interpret KEYWORD element as Org syntax.
1926 (org-element-property :key keyword
)
1927 (org-element-property :value keyword
)))
1930 ;;;; Latex Environment
1932 (defun org-element-latex-environment-parser (limit affiliated
)
1933 "Parse a LaTeX environment.
1935 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1936 the buffer position at the beginning of the first affiliated
1937 keyword and CDR is a plist of affiliated keywords along with
1940 Return a list whose CAR is `latex-environment' and CDR is a plist
1941 containing `:begin', `:end', `:value', `:post-blank' and
1942 `:post-affiliated' keywords.
1944 Assume point is at the beginning of the latex environment."
1946 (let ((case-fold-search t
)
1947 (code-begin (point)))
1948 (looking-at "[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}")
1949 (if (not (re-search-forward (format "^[ \t]*\\\\end{%s}[ \t]*$"
1950 (regexp-quote (match-string 1)))
1952 ;; Incomplete latex environment: parse it as a paragraph.
1953 (org-element-paragraph-parser limit affiliated
)
1954 (let* ((code-end (progn (forward-line) (point)))
1955 (begin (car affiliated
))
1956 (value (buffer-substring-no-properties code-begin code-end
))
1957 (end (progn (skip-chars-forward " \r\t\n" limit
)
1958 (skip-chars-backward " \t")
1959 (if (bolp) (point) (line-end-position)))))
1960 (list 'latex-environment
1965 :post-blank
(count-lines code-end end
)
1966 :post-affiliated code-begin
)
1967 (cdr affiliated
))))))))
1969 (defun org-element-latex-environment-interpreter (latex-environment contents
)
1970 "Interpret LATEX-ENVIRONMENT element as Org syntax.
1972 (org-element-property :value latex-environment
))
1977 (defun org-element-node-property-parser (limit)
1978 "Parse a node-property at point.
1980 LIMIT bounds the search.
1982 Return a list whose CAR is `node-property' and CDR is a plist
1983 containing `:key', `:value', `:begin', `:end' and `:post-blank'
1986 (let ((case-fold-search t
)
1988 (key (progn (looking-at "[ \t]*:\\(.*?\\):[ \t]+\\(.*?\\)[ \t]*$")
1989 (org-match-string-no-properties 1)))
1990 (value (org-match-string-no-properties 2))
1991 (pos-before-blank (progn (forward-line) (point)))
1992 (end (progn (skip-chars-forward " \r\t\n" limit
)
1993 (if (eobp) (point) (point-at-bol)))))
1994 (list 'node-property
1999 :post-blank
(count-lines pos-before-blank end
))))))
2001 (defun org-element-node-property-interpreter (node-property contents
)
2002 "Interpret NODE-PROPERTY element as Org syntax.
2004 (format org-property-format
2005 (format ":%s:" (org-element-property :key node-property
))
2006 (org-element-property :value node-property
)))
2011 (defun org-element-paragraph-parser (limit affiliated
)
2014 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2015 the buffer position at the beginning of the first affiliated
2016 keyword and CDR is a plist of affiliated keywords along with
2019 Return a list whose CAR is `paragraph' and CDR is a plist
2020 containing `:begin', `:end', `:contents-begin' and
2021 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
2023 Assume point is at the beginning of the paragraph."
2025 (let* ((begin (car affiliated
))
2026 (contents-begin (point))
2028 (let ((case-fold-search t
))
2030 (if (not (re-search-forward
2031 org-element-paragraph-separate limit
'm
))
2033 ;; A matching `org-element-paragraph-separate' is not
2034 ;; necessarily the end of the paragraph. In
2035 ;; particular, lines starting with # or : as a first
2036 ;; non-space character are ambiguous. We have check
2037 ;; if they are valid Org syntax (i.e. not an
2038 ;; incomplete keyword).
2042 ;; There's no ambiguity for other symbols or
2043 ;; empty lines: stop here.
2044 (looking-at "[ \t]*\\(?:[^:#]\\|$\\)")
2045 ;; Stop at valid fixed-width areas.
2046 (looking-at "[ \t]*:\\(?: \\|$\\)")
2048 (and (looking-at org-drawer-regexp
)
2051 "^[ \t]*:END:[ \t]*$" limit t
)))
2052 ;; Stop at valid comments.
2053 (looking-at "[ \t]*#\\(?: \\|$\\)")
2054 ;; Stop at valid dynamic blocks.
2055 (and (looking-at org-dblock-start-re
)
2058 "^[ \t]*#\\+END:?[ \t]*$" limit t
)))
2059 ;; Stop at valid blocks.
2061 "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
2064 (format "^[ \t]*#\\+END_%s[ \t]*$"
2067 ;; Stop at valid latex environments.
2069 "^[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}[ \t]*$")
2072 (format "^[ \t]*\\\\end{%s}[ \t]*$"
2075 ;; Stop at valid keywords.
2076 (looking-at "[ \t]*#\\+\\S-+:")
2077 ;; Skip everything else.
2081 (re-search-forward org-element-paragraph-separate
2083 (beginning-of-line)))
2084 (if (= (point) limit
) limit
2085 (goto-char (line-beginning-position)))))
2086 (contents-end (progn (skip-chars-backward " \r\t\n" contents-begin
)
2089 (end (progn (skip-chars-forward " \r\t\n" limit
)
2090 (skip-chars-backward " \t")
2091 (if (bolp) (point) (line-end-position)))))
2096 :contents-begin contents-begin
2097 :contents-end contents-end
2098 :post-blank
(count-lines before-blank end
)
2099 :post-affiliated contents-begin
)
2100 (cdr affiliated
))))))
2102 (defun org-element-paragraph-interpreter (paragraph contents
)
2103 "Interpret PARAGRAPH element as Org syntax.
2104 CONTENTS is the contents of the element."
2110 (defun org-element-planning-parser (limit)
2113 LIMIT bounds the search.
2115 Return a list whose CAR is `planning' and CDR is a plist
2116 containing `:closed', `:deadline', `:scheduled', `:begin', `:end'
2117 and `:post-blank' keywords."
2119 (let* ((case-fold-search nil
)
2121 (post-blank (let ((before-blank (progn (forward-line) (point))))
2122 (skip-chars-forward " \r\t\n" limit
)
2123 (skip-chars-backward " \t")
2124 (unless (bolp) (end-of-line))
2125 (count-lines before-blank
(point))))
2127 closed deadline scheduled
)
2129 (while (re-search-forward org-keyword-time-not-clock-regexp end t
)
2130 (goto-char (match-end 1))
2131 (skip-chars-forward " \t" end
)
2132 (let ((keyword (match-string 1))
2133 (time (org-element-timestamp-parser)))
2134 (cond ((equal keyword org-closed-string
) (setq closed time
))
2135 ((equal keyword org-deadline-string
) (setq deadline time
))
2136 (t (setq scheduled time
)))))
2138 (list :closed closed
2140 :scheduled scheduled
2143 :post-blank post-blank
)))))
2145 (defun org-element-planning-interpreter (planning contents
)
2146 "Interpret PLANNING element as Org syntax.
2151 (list (let ((deadline (org-element-property :deadline planning
)))
2153 (concat org-deadline-string
" "
2154 (org-element-timestamp-interpreter deadline nil
))))
2155 (let ((scheduled (org-element-property :scheduled planning
)))
2157 (concat org-scheduled-string
" "
2158 (org-element-timestamp-interpreter scheduled nil
))))
2159 (let ((closed (org-element-property :closed planning
)))
2161 (concat org-closed-string
" "
2162 (org-element-timestamp-interpreter closed nil
))))))
2168 (defun org-element-quote-section-parser (limit)
2169 "Parse a quote section.
2171 LIMIT bounds the search.
2173 Return a list whose CAR is `quote-section' and CDR is a plist
2174 containing `:begin', `:end', `:value' and `:post-blank' keywords.
2176 Assume point is at beginning of the section."
2178 (let* ((begin (point))
2179 (end (progn (org-with-limited-levels (outline-next-heading))
2181 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
2184 (value (buffer-substring-no-properties begin pos-before-blank
)))
2185 (list 'quote-section
2189 :post-blank
(count-lines pos-before-blank end
))))))
2191 (defun org-element-quote-section-interpreter (quote-section contents
)
2192 "Interpret QUOTE-SECTION element as Org syntax.
2194 (org-element-property :value quote-section
))
2199 (defun org-element-src-block-parser (limit affiliated
)
2202 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2203 the buffer position at the beginning of the first affiliated
2204 keyword and CDR is a plist of affiliated keywords along with
2207 Return a list whose CAR is `src-block' and CDR is a plist
2208 containing `:language', `:switches', `:parameters', `:begin',
2209 `:end', `:hiddenp', `:number-lines', `:retain-labels',
2210 `:use-labels', `:label-fmt', `:preserve-indent', `:value',
2211 `:post-blank' and `:post-affiliated' keywords.
2213 Assume point is at the beginning of the block."
2214 (let ((case-fold-search t
))
2215 (if (not (save-excursion (re-search-forward "^[ \t]*#\\+END_SRC[ \t]*$"
2217 ;; Incomplete block: parse it as a paragraph.
2218 (org-element-paragraph-parser limit affiliated
)
2219 (let ((contents-end (match-beginning 0)))
2221 (let* ((begin (car affiliated
))
2222 (post-affiliated (point))
2223 ;; Get language as a string.
2227 (concat "^[ \t]*#\\+BEGIN_SRC"
2228 "\\(?: +\\(\\S-+\\)\\)?"
2229 "\\(\\(?: +\\(?:-l \".*?\"\\|[-+][A-Za-z]\\)\\)+\\)?"
2231 (org-match-string-no-properties 1)))
2233 (switches (org-match-string-no-properties 2))
2235 (parameters (org-match-string-no-properties 3))
2236 ;; Switches analysis
2237 (number-lines (cond ((not switches
) nil
)
2238 ((string-match "-n\\>" switches
) 'new
)
2239 ((string-match "+n\\>" switches
) 'continued
)))
2240 (preserve-indent (and switches
(string-match "-i\\>" switches
)))
2241 (label-fmt (and switches
2242 (string-match "-l +\"\\([^\"\n]+\\)\"" switches
)
2243 (match-string 1 switches
)))
2244 ;; Should labels be retained in (or stripped from)
2248 (not (string-match "-r\\>" switches
))
2249 (and number-lines
(string-match "-k\\>" switches
))))
2250 ;; What should code-references use - labels or
2254 (and retain-labels
(not (string-match "-k\\>" switches
)))))
2255 ;; Get visibility status.
2256 (hidden (progn (forward-line) (org-invisible-p2)))
2258 (value (org-unescape-code-in-string
2259 (buffer-substring-no-properties (point) contents-end
)))
2260 (pos-before-blank (progn (goto-char contents-end
)
2263 ;; Get position after ending blank lines.
2264 (end (progn (skip-chars-forward " \r\t\n" limit
)
2265 (skip-chars-backward " \t")
2266 (if (bolp) (point) (line-end-position)))))
2269 (list :language language
2270 :switches
(and (org-string-nw-p switches
)
2271 (org-trim switches
))
2272 :parameters
(and (org-string-nw-p parameters
)
2273 (org-trim parameters
))
2276 :number-lines number-lines
2277 :preserve-indent preserve-indent
2278 :retain-labels retain-labels
2279 :use-labels use-labels
2280 :label-fmt label-fmt
2283 :post-blank
(count-lines pos-before-blank end
)
2284 :post-affiliated post-affiliated
)
2285 (cdr affiliated
)))))))))
2287 (defun org-element-src-block-interpreter (src-block contents
)
2288 "Interpret SRC-BLOCK element as Org syntax.
2290 (let ((lang (org-element-property :language src-block
))
2291 (switches (org-element-property :switches src-block
))
2292 (params (org-element-property :parameters src-block
))
2293 (value (let ((val (org-element-property :value src-block
)))
2295 (org-src-preserve-indentation val
)
2296 ((zerop org-edit-src-content-indentation
)
2297 (org-remove-indentation val
))
2299 (let ((ind (make-string
2300 org-edit-src-content-indentation
32)))
2301 (replace-regexp-in-string
2302 "\\(^\\)[ \t]*\\S-" ind
2303 (org-remove-indentation val
) nil nil
1)))))))
2304 (concat (format "#+BEGIN_SRC%s\n"
2305 (concat (and lang
(concat " " lang
))
2306 (and switches
(concat " " switches
))
2307 (and params
(concat " " params
))))
2308 (org-escape-code-in-string value
)
2314 (defun org-element-table-parser (limit affiliated
)
2315 "Parse a table at point.
2317 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2318 the buffer position at the beginning of the first affiliated
2319 keyword and CDR is a plist of affiliated keywords along with
2322 Return a list whose CAR is `table' and CDR is a plist containing
2323 `:begin', `:end', `:tblfm', `:type', `:contents-begin',
2324 `:contents-end', `:value', `:post-blank' and `:post-affiliated'
2327 Assume point is at the beginning of the table."
2329 (let* ((case-fold-search t
)
2330 (table-begin (point))
2331 (type (if (org-at-table.el-p
) 'table.el
'org
))
2332 (begin (car affiliated
))
2334 (if (re-search-forward org-table-any-border-regexp limit
'm
)
2335 (goto-char (match-beginning 0))
2338 (while (looking-at "[ \t]*#\\+TBLFM: +\\(.*\\)[ \t]*$")
2339 (push (org-match-string-no-properties 1) acc
)
2342 (pos-before-blank (point))
2343 (end (progn (skip-chars-forward " \r\t\n" limit
)
2344 (skip-chars-backward " \t")
2345 (if (bolp) (point) (line-end-position)))))
2352 ;; Only `org' tables have contents. `table.el' tables
2353 ;; use a `:value' property to store raw table as
2355 :contents-begin
(and (eq type
'org
) table-begin
)
2356 :contents-end
(and (eq type
'org
) table-end
)
2357 :value
(and (eq type
'table.el
)
2358 (buffer-substring-no-properties
2359 table-begin table-end
))
2360 :post-blank
(count-lines pos-before-blank end
)
2361 :post-affiliated table-begin
)
2362 (cdr affiliated
))))))
2364 (defun org-element-table-interpreter (table contents
)
2365 "Interpret TABLE element as Org syntax.
2367 (if (eq (org-element-property :type table
) 'table.el
)
2368 (org-remove-indentation (org-element-property :value table
))
2369 (concat (with-temp-buffer (insert contents
)
2372 (mapconcat (lambda (fm) (concat "#+TBLFM: " fm
))
2373 (reverse (org-element-property :tblfm table
))
2379 (defun org-element-table-row-parser (limit)
2380 "Parse table row at point.
2382 LIMIT bounds the search.
2384 Return a list whose CAR is `table-row' and CDR is a plist
2385 containing `:begin', `:end', `:contents-begin', `:contents-end',
2386 `:type' and `:post-blank' keywords."
2388 (let* ((type (if (looking-at "^[ \t]*|-") 'rule
'standard
))
2390 ;; A table rule has no contents. In that case, ensure
2391 ;; CONTENTS-BEGIN matches CONTENTS-END.
2392 (contents-begin (and (eq type
'standard
)
2393 (search-forward "|")
2395 (contents-end (and (eq type
'standard
)
2398 (skip-chars-backward " \t")
2400 (end (progn (forward-line) (point))))
2405 :contents-begin contents-begin
2406 :contents-end contents-end
2409 (defun org-element-table-row-interpreter (table-row contents
)
2410 "Interpret TABLE-ROW element as Org syntax.
2411 CONTENTS is the contents of the table row."
2412 (if (eq (org-element-property :type table-row
) 'rule
) "|-"
2413 (concat "| " contents
)))
2418 (defun org-element-verse-block-parser (limit affiliated
)
2419 "Parse a verse block.
2421 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2422 the buffer position at the beginning of the first affiliated
2423 keyword and CDR is a plist of affiliated keywords along with
2426 Return a list whose CAR is `verse-block' and CDR is a plist
2427 containing `:begin', `:end', `:contents-begin', `:contents-end',
2428 `:hiddenp', `:post-blank' and `:post-affiliated' keywords.
2430 Assume point is at beginning of the block."
2431 (let ((case-fold-search t
))
2432 (if (not (save-excursion
2433 (re-search-forward "^[ \t]*#\\+END_VERSE[ \t]*$" limit t
)))
2434 ;; Incomplete block: parse it as a paragraph.
2435 (org-element-paragraph-parser limit affiliated
)
2436 (let ((contents-end (match-beginning 0)))
2438 (let* ((begin (car affiliated
))
2439 (post-affiliated (point))
2440 (hidden (progn (forward-line) (org-invisible-p2)))
2441 (contents-begin (point))
2442 (pos-before-blank (progn (goto-char contents-end
)
2445 (end (progn (skip-chars-forward " \r\t\n" limit
)
2446 (skip-chars-backward " \t")
2447 (if (bolp) (point) (line-end-position)))))
2452 :contents-begin contents-begin
2453 :contents-end contents-end
2455 :post-blank
(count-lines pos-before-blank end
)
2456 :post-affiliated post-affiliated
)
2457 (cdr affiliated
)))))))))
2459 (defun org-element-verse-block-interpreter (verse-block contents
)
2460 "Interpret VERSE-BLOCK element as Org syntax.
2461 CONTENTS is verse block contents."
2462 (format "#+BEGIN_VERSE\n%s#+END_VERSE" contents
))
2468 ;; Unlike to elements, interstices can be found between objects.
2469 ;; That's why, along with the parser, successor functions are provided
2470 ;; for each object. Some objects share the same successor (i.e. `code'
2471 ;; and `verbatim' objects).
2473 ;; A successor must accept a single argument bounding the search. It
2474 ;; will return either a cons cell whose CAR is the object's type, as
2475 ;; a symbol, and CDR the position of its next occurrence, or nil.
2477 ;; Successors follow the naming convention:
2478 ;; org-element-NAME-successor, where NAME is the name of the
2479 ;; successor, as defined in `org-element-all-successors'.
2481 ;; Some object types (i.e. `italic') are recursive. Restrictions on
2482 ;; object types they can contain will be specified in
2483 ;; `org-element-object-restrictions'.
2485 ;; Adding a new type of object is simple. Implement a successor,
2486 ;; a parser, and an interpreter for it, all following the naming
2487 ;; convention. Register type in `org-element-all-objects' and
2488 ;; successor in `org-element-all-successors'. Maybe tweak
2489 ;; restrictions about it, and that's it.
2494 (defun org-element-bold-parser ()
2495 "Parse bold object at point.
2497 Return a list whose CAR is `bold' and CDR is a plist with
2498 `:begin', `:end', `:contents-begin' and `:contents-end' and
2499 `:post-blank' keywords.
2501 Assume point is at the first star marker."
2503 (unless (bolp) (backward-char 1))
2504 (looking-at org-emph-re
)
2505 (let ((begin (match-beginning 2))
2506 (contents-begin (match-beginning 4))
2507 (contents-end (match-end 4))
2508 (post-blank (progn (goto-char (match-end 2))
2509 (skip-chars-forward " \t")))
2514 :contents-begin contents-begin
2515 :contents-end contents-end
2516 :post-blank post-blank
)))))
2518 (defun org-element-bold-interpreter (bold contents
)
2519 "Interpret BOLD object as Org syntax.
2520 CONTENTS is the contents of the object."
2521 (format "*%s*" contents
))
2523 (defun org-element-text-markup-successor (limit)
2524 "Search for the next text-markup object.
2526 LIMIT bounds the search.
2528 Return value is a cons cell whose CAR is a symbol among `bold',
2529 `italic', `underline', `strike-through', `code' and `verbatim'
2530 and CDR is beginning position."
2532 (unless (bolp) (backward-char))
2533 (when (re-search-forward org-emph-re limit t
)
2534 (let ((marker (match-string 3)))
2536 ((equal marker
"*") 'bold
)
2537 ((equal marker
"/") 'italic
)
2538 ((equal marker
"_") 'underline
)
2539 ((equal marker
"+") 'strike-through
)
2540 ((equal marker
"~") 'code
)
2541 ((equal marker
"=") 'verbatim
)
2542 (t (error "Unknown marker at %d" (match-beginning 3))))
2543 (match-beginning 2))))))
2548 (defun org-element-code-parser ()
2549 "Parse code object at point.
2551 Return a list whose CAR is `code' and CDR is a plist with
2552 `:value', `:begin', `:end' and `:post-blank' keywords.
2554 Assume point is at the first tilde marker."
2556 (unless (bolp) (backward-char 1))
2557 (looking-at org-emph-re
)
2558 (let ((begin (match-beginning 2))
2559 (value (org-match-string-no-properties 4))
2560 (post-blank (progn (goto-char (match-end 2))
2561 (skip-chars-forward " \t")))
2567 :post-blank post-blank
)))))
2569 (defun org-element-code-interpreter (code contents
)
2570 "Interpret CODE object as Org syntax.
2572 (format "~%s~" (org-element-property :value code
)))
2577 (defun org-element-entity-parser ()
2578 "Parse entity at point.
2580 Return a list whose CAR is `entity' and CDR a plist with
2581 `:begin', `:end', `:latex', `:latex-math-p', `:html', `:latin1',
2582 `:utf-8', `:ascii', `:use-brackets-p' and `:post-blank' as
2585 Assume point is at the beginning of the entity."
2587 (looking-at "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)")
2588 (let* ((value (org-entity-get (match-string 1)))
2589 (begin (match-beginning 0))
2590 (bracketsp (string= (match-string 2) "{}"))
2591 (post-blank (progn (goto-char (match-end 1))
2592 (when bracketsp
(forward-char 2))
2593 (skip-chars-forward " \t")))
2596 (list :name
(car value
)
2597 :latex
(nth 1 value
)
2598 :latex-math-p
(nth 2 value
)
2600 :ascii
(nth 4 value
)
2601 :latin1
(nth 5 value
)
2602 :utf-8
(nth 6 value
)
2605 :use-brackets-p bracketsp
2606 :post-blank post-blank
)))))
2608 (defun org-element-entity-interpreter (entity contents
)
2609 "Interpret ENTITY object as Org syntax.
2612 (org-element-property :name entity
)
2613 (when (org-element-property :use-brackets-p entity
) "{}")))
2615 (defun org-element-latex-or-entity-successor (limit)
2616 "Search for the next latex-fragment or entity object.
2618 LIMIT bounds the search.
2620 Return value is a cons cell whose CAR is `entity' or
2621 `latex-fragment' and CDR is beginning position."
2623 (unless (bolp) (backward-char))
2625 (remove "begin" (plist-get org-format-latex-options
:matchers
)))
2626 ;; ENTITY-RE matches both LaTeX commands and Org entities.
2628 "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))
2629 (when (re-search-forward
2630 (concat (mapconcat (lambda (e) (nth 1 (assoc e org-latex-regexps
)))
2634 (goto-char (match-beginning 0))
2635 (if (looking-at entity-re
)
2636 ;; Determine if it's a real entity or a LaTeX command.
2637 (cons (if (org-entity-get (match-string 1)) 'entity
'latex-fragment
)
2638 (match-beginning 0))
2639 ;; No entity nor command: point is at a LaTeX fragment.
2640 ;; Determine its type to get the correct beginning position.
2641 (cons 'latex-fragment
2644 (when (looking-at (nth 1 (assoc e org-latex-regexps
)))
2647 (nth 2 (assoc e org-latex-regexps
))))))
2654 (defun org-element-export-snippet-parser ()
2655 "Parse export snippet at point.
2657 Return a list whose CAR is `export-snippet' and CDR a plist with
2658 `:begin', `:end', `:back-end', `:value' and `:post-blank' as
2661 Assume point is at the beginning of the snippet."
2663 (re-search-forward "@@\\([-A-Za-z0-9]+\\):" nil t
)
2664 (let* ((begin (match-beginning 0))
2665 (back-end (org-match-string-no-properties 1))
2666 (value (buffer-substring-no-properties
2668 (progn (re-search-forward "@@" nil t
) (match-beginning 0))))
2669 (post-blank (skip-chars-forward " \t"))
2671 (list 'export-snippet
2672 (list :back-end back-end
2676 :post-blank post-blank
)))))
2678 (defun org-element-export-snippet-interpreter (export-snippet contents
)
2679 "Interpret EXPORT-SNIPPET object as Org syntax.
2682 (org-element-property :back-end export-snippet
)
2683 (org-element-property :value export-snippet
)))
2685 (defun org-element-export-snippet-successor (limit)
2686 "Search for the next export-snippet object.
2688 LIMIT bounds the search.
2690 Return value is a cons cell whose CAR is `export-snippet' and CDR
2691 its beginning position."
2694 (when (and (re-search-forward "@@[-A-Za-z0-9]+:" limit t
)
2695 (setq beg
(match-beginning 0))
2696 (search-forward "@@" limit t
))
2697 (cons 'export-snippet beg
)))))
2700 ;;;; Footnote Reference
2702 (defun org-element-footnote-reference-parser ()
2703 "Parse footnote reference at point.
2705 Return a list whose CAR is `footnote-reference' and CDR a plist
2706 with `:label', `:type', `:inline-definition', `:begin', `:end'
2707 and `:post-blank' as keywords."
2709 (looking-at org-footnote-re
)
2710 (let* ((begin (point))
2711 (label (or (org-match-string-no-properties 2)
2712 (org-match-string-no-properties 3)
2713 (and (match-string 1)
2714 (concat "fn:" (org-match-string-no-properties 1)))))
2715 (type (if (or (not label
) (match-string 1)) 'inline
'standard
))
2716 (inner-begin (match-end 0))
2720 (while (and (> count
0) (re-search-forward "[][]" nil t
))
2721 (if (equal (match-string 0) "[") (incf count
) (decf count
)))
2723 (post-blank (progn (goto-char (1+ inner-end
))
2724 (skip-chars-forward " \t")))
2727 (list 'footnote-reference
2732 :post-blank post-blank
))))
2733 (org-element-put-property
2734 footnote-reference
:inline-definition
2735 (and (eq type
'inline
)
2736 (org-element-parse-secondary-string
2737 (buffer-substring inner-begin inner-end
)
2738 (org-element-restriction 'footnote-reference
)
2739 footnote-reference
))))))
2741 (defun org-element-footnote-reference-interpreter (footnote-reference contents
)
2742 "Interpret FOOTNOTE-REFERENCE object as Org syntax.
2744 (let ((label (or (org-element-property :label footnote-reference
) "fn:"))
2747 (org-element-property :inline-definition footnote-reference
)))
2748 (if (not inline-def
) ""
2749 (concat ":" (org-element-interpret-data inline-def
))))))
2750 (format "[%s]" (concat label def
))))
2752 (defun org-element-footnote-reference-successor (limit)
2753 "Search for the next footnote-reference object.
2755 LIMIT bounds the search.
2757 Return value is a cons cell whose CAR is `footnote-reference' and
2758 CDR is beginning position."
2761 (while (re-search-forward org-footnote-re limit t
)
2763 (let ((beg (match-beginning 0))
2766 (while (re-search-forward "[][]" limit t
)
2767 (if (equal (match-string 0) "[") (incf count
) (decf count
))
2769 (throw 'exit
(cons 'footnote-reference beg
))))))))))
2772 ;;;; Inline Babel Call
2774 (defun org-element-inline-babel-call-parser ()
2775 "Parse inline babel call at point.
2777 Return a list whose CAR is `inline-babel-call' and CDR a plist
2778 with `:begin', `:end', `:info' and `:post-blank' as keywords.
2780 Assume point is at the beginning of the babel call."
2782 (unless (bolp) (backward-char))
2783 (looking-at org-babel-inline-lob-one-liner-regexp
)
2784 (let ((info (save-match-data (org-babel-lob-get-info)))
2785 (begin (match-end 1))
2786 (post-blank (progn (goto-char (match-end 0))
2787 (skip-chars-forward " \t")))
2789 (list 'inline-babel-call
2793 :post-blank post-blank
)))))
2795 (defun org-element-inline-babel-call-interpreter (inline-babel-call contents
)
2796 "Interpret INLINE-BABEL-CALL object as Org syntax.
2798 (let* ((babel-info (org-element-property :info inline-babel-call
))
2799 (main-source (car babel-info
))
2800 (post-options (nth 1 babel-info
)))
2802 (if (string-match "\\[\\(\\[.*?\\]\\)\\]" main-source
)
2803 ;; Remove redundant square brackets.
2805 (match-string 1 main-source
) nil nil main-source
)
2807 (and post-options
(format "[%s]" post-options
)))))
2809 (defun org-element-inline-babel-call-successor (limit)
2810 "Search for the next inline-babel-call object.
2812 LIMIT bounds the search.
2814 Return value is a cons cell whose CAR is `inline-babel-call' and
2815 CDR is beginning position."
2817 ;; Use a simplified version of
2818 ;; `org-babel-inline-lob-one-liner-regexp'.
2819 (when (re-search-forward
2820 "call_\\([^()\n]+?\\)\\(?:\\[.*?\\]\\)?([^\n]*?)\\(\\[.*?\\]\\)?"
2822 (cons 'inline-babel-call
(match-beginning 0)))))
2825 ;;;; Inline Src Block
2827 (defun org-element-inline-src-block-parser ()
2828 "Parse inline source block at point.
2830 LIMIT bounds the search.
2832 Return a list whose CAR is `inline-src-block' and CDR a plist
2833 with `:begin', `:end', `:language', `:value', `:parameters' and
2834 `:post-blank' as keywords.
2836 Assume point is at the beginning of the inline src block."
2838 (unless (bolp) (backward-char))
2839 (looking-at org-babel-inline-src-block-regexp
)
2840 (let ((begin (match-beginning 1))
2841 (language (org-match-string-no-properties 2))
2842 (parameters (org-match-string-no-properties 4))
2843 (value (org-match-string-no-properties 5))
2844 (post-blank (progn (goto-char (match-end 0))
2845 (skip-chars-forward " \t")))
2847 (list 'inline-src-block
2848 (list :language language
2850 :parameters parameters
2853 :post-blank post-blank
)))))
2855 (defun org-element-inline-src-block-interpreter (inline-src-block contents
)
2856 "Interpret INLINE-SRC-BLOCK object as Org syntax.
2858 (let ((language (org-element-property :language inline-src-block
))
2859 (arguments (org-element-property :parameters inline-src-block
))
2860 (body (org-element-property :value inline-src-block
)))
2861 (format "src_%s%s{%s}"
2863 (if arguments
(format "[%s]" arguments
) "")
2866 (defun org-element-inline-src-block-successor (limit)
2867 "Search for the next inline-babel-call element.
2869 LIMIT bounds the search.
2871 Return value is a cons cell whose CAR is `inline-babel-call' and
2872 CDR is beginning position."
2874 (unless (bolp) (backward-char))
2875 (when (re-search-forward org-babel-inline-src-block-regexp limit t
)
2876 (cons 'inline-src-block
(match-beginning 1)))))
2880 (defun org-element-italic-parser ()
2881 "Parse italic object at point.
2883 Return a list whose CAR is `italic' and CDR is a plist with
2884 `:begin', `:end', `:contents-begin' and `:contents-end' and
2885 `:post-blank' keywords.
2887 Assume point is at the first slash marker."
2889 (unless (bolp) (backward-char 1))
2890 (looking-at org-emph-re
)
2891 (let ((begin (match-beginning 2))
2892 (contents-begin (match-beginning 4))
2893 (contents-end (match-end 4))
2894 (post-blank (progn (goto-char (match-end 2))
2895 (skip-chars-forward " \t")))
2900 :contents-begin contents-begin
2901 :contents-end contents-end
2902 :post-blank post-blank
)))))
2904 (defun org-element-italic-interpreter (italic contents
)
2905 "Interpret ITALIC object as Org syntax.
2906 CONTENTS is the contents of the object."
2907 (format "/%s/" contents
))
2912 (defun org-element-latex-fragment-parser ()
2913 "Parse latex fragment at point.
2915 Return a list whose CAR is `latex-fragment' and CDR a plist with
2916 `:value', `:begin', `:end', and `:post-blank' as keywords.
2918 Assume point is at the beginning of the latex fragment."
2920 (let* ((begin (point))
2924 (let ((latex-regexp (nth 1 (assoc e org-latex-regexps
))))
2925 (when (or (looking-at latex-regexp
)
2929 (looking-at latex-regexp
))))
2930 (throw 'exit
(nth 2 (assoc e org-latex-regexps
))))))
2931 (plist-get org-format-latex-options
:matchers
))
2932 ;; None found: it's a macro.
2933 (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")
2935 (value (match-string-no-properties substring-match
))
2936 (post-blank (progn (goto-char (match-end substring-match
))
2937 (skip-chars-forward " \t")))
2939 (list 'latex-fragment
2943 :post-blank post-blank
)))))
2945 (defun org-element-latex-fragment-interpreter (latex-fragment contents
)
2946 "Interpret LATEX-FRAGMENT object as Org syntax.
2948 (org-element-property :value latex-fragment
))
2952 (defun org-element-line-break-parser ()
2953 "Parse line break at point.
2955 Return a list whose CAR is `line-break', and CDR a plist with
2956 `:begin', `:end' and `:post-blank' keywords.
2958 Assume point is at the beginning of the line break."
2960 (list :begin
(point)
2961 :end
(progn (forward-line) (point))
2964 (defun org-element-line-break-interpreter (line-break contents
)
2965 "Interpret LINE-BREAK object as Org syntax.
2969 (defun org-element-line-break-successor (limit)
2970 "Search for the next line-break object.
2972 LIMIT bounds the search.
2974 Return value is a cons cell whose CAR is `line-break' and CDR is
2975 beginning position."
2977 (let ((beg (and (re-search-forward "[^\\\\]\\(\\\\\\\\\\)[ \t]*$" limit t
)
2978 (goto-char (match-beginning 1)))))
2979 ;; A line break can only happen on a non-empty line.
2980 (when (and beg
(re-search-backward "\\S-" (point-at-bol) t
))
2981 (cons 'line-break beg
)))))
2986 (defun org-element-link-parser ()
2987 "Parse link at point.
2989 Return a list whose CAR is `link' and CDR a plist with `:type',
2990 `:path', `:raw-link', `:application', `:search-option', `:begin',
2991 `:end', `:contents-begin', `:contents-end' and `:post-blank' as
2994 Assume point is at the beginning of the link."
2996 (let ((begin (point))
2997 end contents-begin contents-end link-end post-blank path type
2998 raw-link link search-option application
)
3000 ;; Type 1: Text targeted from a radio target.
3001 ((and org-target-link-regexp
(looking-at org-target-link-regexp
))
3003 link-end
(match-end 0)
3004 path
(org-match-string-no-properties 0)))
3005 ;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]]
3006 ((looking-at org-bracket-link-regexp
)
3007 (setq contents-begin
(match-beginning 3)
3008 contents-end
(match-end 3)
3009 link-end
(match-end 0)
3010 ;; RAW-LINK is the original link. Expand any
3011 ;; abbreviation in it.
3012 raw-link
(org-translate-link
3013 (org-link-expand-abbrev
3014 (org-match-string-no-properties 1)))
3015 link
(org-link-unescape raw-link
))
3016 ;; Determine TYPE of link and set PATH accordingly.
3019 ((or (file-name-absolute-p link
) (string-match "^\\.\\.?/" link
))
3020 (setq type
"file" path link
))
3021 ;; Explicit type (http, irc, bbdb...). See `org-link-types'.
3022 ((string-match org-link-re-with-space3 link
)
3023 (setq type
(match-string 1 link
) path
(match-string 2 link
)))
3024 ;; Id type: PATH is the id.
3025 ((string-match "^id:\\([-a-f0-9]+\\)" link
)
3026 (setq type
"id" path
(match-string 1 link
)))
3027 ;; Code-ref type: PATH is the name of the reference.
3028 ((string-match "^(\\(.*\\))$" link
)
3029 (setq type
"coderef" path
(match-string 1 link
)))
3030 ;; Custom-id type: PATH is the name of the custom id.
3031 ((= (aref link
0) ?
#)
3032 (setq type
"custom-id" path
(substring link
1)))
3033 ;; Fuzzy type: Internal link either matches a target, an
3034 ;; headline name or nothing. PATH is the target or
3036 (t (setq type
"fuzzy" path link
))))
3037 ;; Type 3: Plain link, i.e. http://orgmode.org
3038 ((looking-at org-plain-link-re
)
3039 (setq raw-link
(org-match-string-no-properties 0)
3040 type
(org-match-string-no-properties 1)
3041 path
(org-match-string-no-properties 2)
3042 link-end
(match-end 0)))
3043 ;; Type 4: Angular link, i.e. <http://orgmode.org>
3044 ((looking-at org-angle-link-re
)
3045 (setq raw-link
(buffer-substring-no-properties
3046 (match-beginning 1) (match-end 2))
3047 type
(org-match-string-no-properties 1)
3048 path
(org-match-string-no-properties 2)
3049 link-end
(match-end 0))))
3050 ;; In any case, deduce end point after trailing white space from
3051 ;; LINK-END variable.
3052 (setq post-blank
(progn (goto-char link-end
) (skip-chars-forward " \t"))
3054 ;; Extract search option and opening application out of
3055 ;; "file"-type links.
3056 (when (member type org-element-link-type-is-file
)
3058 (cond ((string-match "^file\\+\\(.*\\)$" type
)
3059 (setq application
(match-string 1 type
)))
3060 ((not (string-match "^file" type
))
3061 (setq application type
)))
3062 ;; Extract search option from PATH.
3063 (when (string-match "::\\(.*\\)$" path
)
3064 (setq search-option
(match-string 1 path
)
3065 path
(replace-match "" nil nil path
)))
3066 ;; Make sure TYPE always report "file".
3071 :raw-link
(or raw-link path
)
3072 :application application
3073 :search-option search-option
3076 :contents-begin contents-begin
3077 :contents-end contents-end
3078 :post-blank post-blank
)))))
3080 (defun org-element-link-interpreter (link contents
)
3081 "Interpret LINK object as Org syntax.
3082 CONTENTS is the contents of the object, or nil."
3083 (let ((type (org-element-property :type link
))
3084 (raw-link (org-element-property :raw-link link
)))
3085 (if (string= type
"radio") raw-link
3088 (if contents
(format "[%s]" contents
) "")))))
3090 (defun org-element-link-successor (limit)
3091 "Search for the next link object.
3093 LIMIT bounds the search.
3095 Return value is a cons cell whose CAR is `link' and CDR is
3096 beginning position."
3099 (if (not org-target-link-regexp
) org-any-link-re
3100 (concat org-any-link-re
"\\|" org-target-link-regexp
))))
3101 (when (re-search-forward link-regexp limit t
)
3102 (cons 'link
(match-beginning 0))))))
3107 (defun org-element-macro-parser ()
3108 "Parse macro at point.
3110 Return a list whose CAR is `macro' and CDR a plist with `:key',
3111 `:args', `:begin', `:end', `:value' and `:post-blank' as
3114 Assume point is at the macro."
3116 (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
3117 (let ((begin (point))
3118 (key (downcase (org-match-string-no-properties 1)))
3119 (value (org-match-string-no-properties 0))
3120 (post-blank (progn (goto-char (match-end 0))
3121 (skip-chars-forward " \t")))
3123 (args (let ((args (org-match-string-no-properties 3)) args2
)
3125 ;; Do not use `org-split-string' since empty
3126 ;; strings are meaningful here.
3127 (setq args
(split-string args
","))
3129 (while (string-match "\\\\\\'" (car args
))
3130 ;; Repair bad splits, when comma is protected,
3131 ;; and thus not a real separator.
3132 (setcar (cdr args
) (concat (substring (car args
) 0 -
1)
3135 (push (pop args
) args2
))
3136 (mapcar 'org-trim
(nreverse args2
))))))
3143 :post-blank post-blank
)))))
3145 (defun org-element-macro-interpreter (macro contents
)
3146 "Interpret MACRO object as Org syntax.
3148 (org-element-property :value macro
))
3150 (defun org-element-macro-successor (limit)
3151 "Search for the next macro object.
3153 LIMIT bounds the search.
3155 Return value is cons cell whose CAR is `macro' and CDR is
3156 beginning position."
3158 (when (re-search-forward
3159 "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
3161 (cons 'macro
(match-beginning 0)))))
3166 (defun org-element-radio-target-parser ()
3167 "Parse radio target at point.
3169 Return a list whose CAR is `radio-target' and CDR a plist with
3170 `:begin', `:end', `:contents-begin', `:contents-end', `:value'
3171 and `:post-blank' as keywords.
3173 Assume point is at the radio target."
3175 (looking-at org-radio-target-regexp
)
3176 (let ((begin (point))
3177 (contents-begin (match-beginning 1))
3178 (contents-end (match-end 1))
3179 (value (org-match-string-no-properties 1))
3180 (post-blank (progn (goto-char (match-end 0))
3181 (skip-chars-forward " \t")))
3186 :contents-begin contents-begin
3187 :contents-end contents-end
3188 :post-blank post-blank
3191 (defun org-element-radio-target-interpreter (target contents
)
3192 "Interpret TARGET object as Org syntax.
3193 CONTENTS is the contents of the object."
3194 (concat "<<<" contents
">>>"))
3196 (defun org-element-radio-target-successor (limit)
3197 "Search for the next radio-target object.
3199 LIMIT bounds the search.
3201 Return value is a cons cell whose CAR is `radio-target' and CDR
3202 is beginning position."
3204 (when (re-search-forward org-radio-target-regexp limit t
)
3205 (cons 'radio-target
(match-beginning 0)))))
3208 ;;;; Statistics Cookie
3210 (defun org-element-statistics-cookie-parser ()
3211 "Parse statistics cookie at point.
3213 Return a list whose CAR is `statistics-cookie', and CDR a plist
3214 with `:begin', `:end', `:value' and `:post-blank' keywords.
3216 Assume point is at the beginning of the statistics-cookie."
3218 (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
3219 (let* ((begin (point))
3220 (value (buffer-substring-no-properties
3221 (match-beginning 0) (match-end 0)))
3222 (post-blank (progn (goto-char (match-end 0))
3223 (skip-chars-forward " \t")))
3225 (list 'statistics-cookie
3229 :post-blank post-blank
)))))
3231 (defun org-element-statistics-cookie-interpreter (statistics-cookie contents
)
3232 "Interpret STATISTICS-COOKIE object as Org syntax.
3234 (org-element-property :value statistics-cookie
))
3236 (defun org-element-statistics-cookie-successor (limit)
3237 "Search for the next statistics cookie object.
3239 LIMIT bounds the search.
3241 Return value is a cons cell whose CAR is `statistics-cookie' and
3242 CDR is beginning position."
3244 (when (re-search-forward "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]" limit t
)
3245 (cons 'statistics-cookie
(match-beginning 0)))))
3250 (defun org-element-strike-through-parser ()
3251 "Parse strike-through object at point.
3253 Return a list whose CAR is `strike-through' and CDR is a plist
3254 with `:begin', `:end', `:contents-begin' and `:contents-end' and
3255 `:post-blank' keywords.
3257 Assume point is at the first plus sign marker."
3259 (unless (bolp) (backward-char 1))
3260 (looking-at org-emph-re
)
3261 (let ((begin (match-beginning 2))
3262 (contents-begin (match-beginning 4))
3263 (contents-end (match-end 4))
3264 (post-blank (progn (goto-char (match-end 2))
3265 (skip-chars-forward " \t")))
3267 (list 'strike-through
3270 :contents-begin contents-begin
3271 :contents-end contents-end
3272 :post-blank post-blank
)))))
3274 (defun org-element-strike-through-interpreter (strike-through contents
)
3275 "Interpret STRIKE-THROUGH object as Org syntax.
3276 CONTENTS is the contents of the object."
3277 (format "+%s+" contents
))
3282 (defun org-element-subscript-parser ()
3283 "Parse subscript at point.
3285 Return a list whose CAR is `subscript' and CDR a plist with
3286 `:begin', `:end', `:contents-begin', `:contents-end',
3287 `:use-brackets-p' and `:post-blank' as keywords.
3289 Assume point is at the underscore."
3291 (unless (bolp) (backward-char))
3292 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp
)
3294 (not (looking-at org-match-substring-regexp
))))
3295 (begin (match-beginning 2))
3296 (contents-begin (or (match-beginning 5)
3297 (match-beginning 3)))
3298 (contents-end (or (match-end 5) (match-end 3)))
3299 (post-blank (progn (goto-char (match-end 0))
3300 (skip-chars-forward " \t")))
3305 :use-brackets-p bracketsp
3306 :contents-begin contents-begin
3307 :contents-end contents-end
3308 :post-blank post-blank
)))))
3310 (defun org-element-subscript-interpreter (subscript contents
)
3311 "Interpret SUBSCRIPT object as Org syntax.
3312 CONTENTS is the contents of the object."
3314 (if (org-element-property :use-brackets-p subscript
) "_{%s}" "_%s")
3317 (defun org-element-sub/superscript-successor
(limit)
3318 "Search for the next sub/superscript object.
3320 LIMIT bounds the search.
3322 Return value is a cons cell whose CAR is either `subscript' or
3323 `superscript' and CDR is beginning position."
3325 (unless (bolp) (backward-char))
3326 (when (re-search-forward org-match-substring-regexp limit t
)
3327 (cons (if (string= (match-string 2) "_") 'subscript
'superscript
)
3328 (match-beginning 2)))))
3333 (defun org-element-superscript-parser ()
3334 "Parse superscript at point.
3336 Return a list whose CAR is `superscript' and CDR a plist with
3337 `:begin', `:end', `:contents-begin', `:contents-end',
3338 `:use-brackets-p' and `:post-blank' as keywords.
3340 Assume point is at the caret."
3342 (unless (bolp) (backward-char))
3343 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp
) t
3344 (not (looking-at org-match-substring-regexp
))))
3345 (begin (match-beginning 2))
3346 (contents-begin (or (match-beginning 5)
3347 (match-beginning 3)))
3348 (contents-end (or (match-end 5) (match-end 3)))
3349 (post-blank (progn (goto-char (match-end 0))
3350 (skip-chars-forward " \t")))
3355 :use-brackets-p bracketsp
3356 :contents-begin contents-begin
3357 :contents-end contents-end
3358 :post-blank post-blank
)))))
3360 (defun org-element-superscript-interpreter (superscript contents
)
3361 "Interpret SUPERSCRIPT object as Org syntax.
3362 CONTENTS is the contents of the object."
3364 (if (org-element-property :use-brackets-p superscript
) "^{%s}" "^%s")
3370 (defun org-element-table-cell-parser ()
3371 "Parse table cell at point.
3373 Return a list whose CAR is `table-cell' and CDR is a plist
3374 containing `:begin', `:end', `:contents-begin', `:contents-end'
3375 and `:post-blank' keywords."
3376 (looking-at "[ \t]*\\(.*?\\)[ \t]*|")
3377 (let* ((begin (match-beginning 0))
3379 (contents-begin (match-beginning 1))
3380 (contents-end (match-end 1)))
3384 :contents-begin contents-begin
3385 :contents-end contents-end
3388 (defun org-element-table-cell-interpreter (table-cell contents
)
3389 "Interpret TABLE-CELL element as Org syntax.
3390 CONTENTS is the contents of the cell, or nil."
3391 (concat " " contents
" |"))
3393 (defun org-element-table-cell-successor (limit)
3394 "Search for the next table-cell object.
3396 LIMIT bounds the search.
3398 Return value is a cons cell whose CAR is `table-cell' and CDR is
3399 beginning position."
3400 (when (looking-at "[ \t]*.*?[ \t]+|") (cons 'table-cell
(point))))
3405 (defun org-element-target-parser ()
3406 "Parse target at point.
3408 Return a list whose CAR is `target' and CDR a plist with
3409 `:begin', `:end', `:value' and `:post-blank' as keywords.
3411 Assume point is at the target."
3413 (looking-at org-target-regexp
)
3414 (let ((begin (point))
3415 (value (org-match-string-no-properties 1))
3416 (post-blank (progn (goto-char (match-end 0))
3417 (skip-chars-forward " \t")))
3423 :post-blank post-blank
)))))
3425 (defun org-element-target-interpreter (target contents
)
3426 "Interpret TARGET object as Org syntax.
3428 (format "<<%s>>" (org-element-property :value target
)))
3430 (defun org-element-target-successor (limit)
3431 "Search for the next target object.
3433 LIMIT bounds the search.
3435 Return value is a cons cell whose CAR is `target' and CDR is
3436 beginning position."
3438 (when (re-search-forward org-target-regexp limit t
)
3439 (cons 'target
(match-beginning 0)))))
3444 (defun org-element-timestamp-parser ()
3445 "Parse time stamp at point.
3447 Return a list whose CAR is `timestamp', and CDR a plist with
3448 `:type', `:begin', `:end', `:value' and `:post-blank' keywords.
3450 Assume point is at the beginning of the timestamp."
3452 (let* ((begin (point))
3453 (activep (eq (char-after) ?
<))
3456 (looking-at "\\([<[]\\(%%\\)?.*?\\)[]>]\\(?:--\\([<[].*?[]>]\\)\\)?")
3457 (match-string-no-properties 0)))
3458 (date-start (match-string-no-properties 1))
3459 (date-end (match-string 3))
3460 (diaryp (match-beginning 2))
3461 (post-blank (progn (goto-char (match-end 0))
3462 (skip-chars-forward " \t")))
3467 "[012]?[0-9]:[0-5][0-9]\\(-\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)"
3469 (cons (string-to-number (match-string 2 date-start
))
3470 (string-to-number (match-string 3 date-start
)))))
3471 (type (cond (diaryp 'diary
)
3472 ((and activep
(or date-end time-range
)) 'active-range
)
3474 ((or date-end time-range
) 'inactive-range
)
3478 (string-match "\\([.+]?\\+\\)\\([0-9]+\\)\\([hdwmy]\\)>"
3482 (let ((type (match-string 1 raw-value
)))
3483 (cond ((equal "++" type
) 'catch-up
)
3484 ((equal ".+" type
) 'restart
)
3486 :repeater-value
(string-to-number (match-string 2 raw-value
))
3488 (case (string-to-char (match-string 3 raw-value
))
3489 (?h
'hour
) (?d
'day
) (?w
'week
) (?m
'month
) (t 'year
)))))
3490 year-start month-start day-start hour-start minute-start year-end
3491 month-end day-end hour-end minute-end
)
3492 ;; Parse date-start.
3494 (let ((date (org-parse-time-string date-start t
)))
3495 (setq year-start
(nth 5 date
)
3496 month-start
(nth 4 date
)
3497 day-start
(nth 3 date
)
3498 hour-start
(nth 2 date
)
3499 minute-start
(nth 1 date
))))
3500 ;; Compute date-end. It can be provided directly in time-stamp,
3501 ;; or extracted from time range. Otherwise, it defaults to the
3502 ;; same values as date-start.
3504 (let ((date (and date-end
(org-parse-time-string date-end t
))))
3505 (setq year-end
(or (nth 5 date
) year-start
)
3506 month-end
(or (nth 4 date
) month-start
)
3507 day-end
(or (nth 3 date
) day-start
)
3508 hour-end
(or (nth 2 date
) (car time-range
) hour-start
)
3509 minute-end
(or (nth 1 date
) (cdr time-range
) minute-start
))))
3511 (nconc (list :type type
3512 :raw-value raw-value
3513 :year-start year-start
3514 :month-start month-start
3515 :day-start day-start
3516 :hour-start hour-start
3517 :minute-start minute-start
3519 :month-end month-end
3522 :minute-end minute-end
3525 :post-blank post-blank
)
3528 (defun org-element-timestamp-interpreter (timestamp contents
)
3529 "Interpret TIMESTAMP object as Org syntax.
3531 ;; Use `:raw-value' if specified.
3532 (or (org-element-property :raw-value timestamp
)
3533 ;; Otherwise, build timestamp string.
3534 (let* ((repeat-string
3536 (case (org-element-property :repeater-type timestamp
)
3537 (cumulate "+") (catch-up "++") (restart ".+"))
3538 (let ((val (org-element-property :repeater-value timestamp
)))
3539 (and val
(number-to-string val
)))
3540 (case (org-element-property :repeater-unit timestamp
)
3541 (hour "h") (day "d") (week "w") (month "m") (year "y"))))
3543 ;; Build an Org timestamp string from TIME. ACTIVEP is
3544 ;; non-nil when time stamp is active. If WITH-TIME-P is
3545 ;; non-nil, add a time part. HOUR-END and MINUTE-END
3546 ;; specify a time range in the timestamp. REPEAT-STRING
3547 ;; is the repeater string, if any.
3548 (lambda (time activep
&optional with-time-p hour-end minute-end
)
3549 (let ((ts (format-time-string
3550 (funcall (if with-time-p
'cdr
'car
)
3551 org-time-stamp-formats
)
3553 (when (and hour-end minute-end
)
3554 (string-match "[012]?[0-9]:[0-5][0-9]" ts
)
3557 (format "\\&-%02d:%02d" hour-end minute-end
)
3559 (unless activep
(setq ts
(format "[%s]" (substring ts
1 -
1))))
3560 (when (org-string-nw-p repeat-string
)
3561 (setq ts
(concat (substring ts
0 -
1)
3564 (substring ts -
1))))
3567 (type (org-element-property :type timestamp
)))
3570 (let* ((minute-start (org-element-property :minute-start timestamp
))
3571 (minute-end (org-element-property :minute-end timestamp
))
3572 (hour-start (org-element-property :hour-start timestamp
))
3573 (hour-end (org-element-property :hour-end timestamp
))
3574 (time-range-p (and hour-start hour-end minute-start minute-end
3575 (or (/= hour-start hour-end
)
3576 (/= minute-start minute-end
)))))
3582 (org-element-property :day-start timestamp
)
3583 (org-element-property :month-start timestamp
)
3584 (org-element-property :year-start timestamp
))
3586 (and hour-start minute-start
)
3587 (and time-range-p hour-end
)
3588 (and time-range-p minute-end
))))
3589 ((active-range inactive-range
)
3590 (let ((minute-start (org-element-property :minute-start timestamp
))
3591 (minute-end (org-element-property :minute-end timestamp
))
3592 (hour-start (org-element-property :hour-start timestamp
))
3593 (hour-end (org-element-property :hour-end timestamp
)))
3596 build-ts-string
(encode-time
3600 (org-element-property :day-start timestamp
)
3601 (org-element-property :month-start timestamp
)
3602 (org-element-property :year-start timestamp
))
3603 (eq type
'active-range
)
3604 (and hour-start minute-start
))
3606 (funcall build-ts-string
3610 (org-element-property :day-end timestamp
)
3611 (org-element-property :month-end timestamp
)
3612 (org-element-property :year-end timestamp
))
3613 (eq type
'active-range
)
3614 (and hour-end minute-end
)))))))))
3616 (defun org-element-timestamp-successor (limit)
3617 "Search for the next timestamp object.
3619 LIMIT bounds the search.
3621 Return value is a cons cell whose CAR is `timestamp' and CDR is
3622 beginning position."
3624 (when (re-search-forward
3625 (concat org-ts-regexp-both
3627 "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
3629 "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
3631 (cons 'timestamp
(match-beginning 0)))))
3636 (defun org-element-underline-parser ()
3637 "Parse underline object at point.
3639 Return a list whose CAR is `underline' and CDR is a plist with
3640 `:begin', `:end', `:contents-begin' and `:contents-end' and
3641 `:post-blank' keywords.
3643 Assume point is at the first underscore marker."
3645 (unless (bolp) (backward-char 1))
3646 (looking-at org-emph-re
)
3647 (let ((begin (match-beginning 2))
3648 (contents-begin (match-beginning 4))
3649 (contents-end (match-end 4))
3650 (post-blank (progn (goto-char (match-end 2))
3651 (skip-chars-forward " \t")))
3656 :contents-begin contents-begin
3657 :contents-end contents-end
3658 :post-blank post-blank
)))))
3660 (defun org-element-underline-interpreter (underline contents
)
3661 "Interpret UNDERLINE object as Org syntax.
3662 CONTENTS is the contents of the object."
3663 (format "_%s_" contents
))
3668 (defun org-element-verbatim-parser ()
3669 "Parse verbatim object at point.
3671 Return a list whose CAR is `verbatim' and CDR is a plist with
3672 `:value', `:begin', `:end' and `:post-blank' keywords.
3674 Assume point is at the first equal sign marker."
3676 (unless (bolp) (backward-char 1))
3677 (looking-at org-emph-re
)
3678 (let ((begin (match-beginning 2))
3679 (value (org-match-string-no-properties 4))
3680 (post-blank (progn (goto-char (match-end 2))
3681 (skip-chars-forward " \t")))
3687 :post-blank post-blank
)))))
3689 (defun org-element-verbatim-interpreter (verbatim contents
)
3690 "Interpret VERBATIM object as Org syntax.
3692 (format "=%s=" (org-element-property :value verbatim
)))
3696 ;;; Parsing Element Starting At Point
3698 ;; `org-element--current-element' is the core function of this section.
3699 ;; It returns the Lisp representation of the element starting at
3702 ;; `org-element--current-element' makes use of special modes. They
3703 ;; are activated for fixed element chaining (i.e. `plain-list' >
3704 ;; `item') or fixed conditional element chaining (i.e. `headline' >
3705 ;; `section'). Special modes are: `first-section', `item',
3706 ;; `node-property', `quote-section', `section' and `table-row'.
3708 (defun org-element--current-element
3709 (limit &optional granularity special structure
)
3710 "Parse the element starting at point.
3712 LIMIT bounds the search.
3714 Return value is a list like (TYPE PROPS) where TYPE is the type
3715 of the element and PROPS a plist of properties associated to the
3718 Possible types are defined in `org-element-all-elements'.
3720 Optional argument GRANULARITY determines the depth of the
3721 recursion. Allowed values are `headline', `greater-element',
3722 `element', `object' or nil. When it is broader than `object' (or
3723 nil), secondary values will not be parsed, since they only
3726 Optional argument SPECIAL, when non-nil, can be either
3727 `first-section', `item', `node-property', `quote-section',
3728 `section', and `table-row'.
3730 If STRUCTURE isn't provided but SPECIAL is set to `item', it will
3733 This function assumes point is always at the beginning of the
3734 element it has to parse."
3736 (let ((case-fold-search t
)
3737 ;; Determine if parsing depth allows for secondary strings
3738 ;; parsing. It only applies to elements referenced in
3739 ;; `org-element-secondary-value-alist'.
3740 (raw-secondary-p (and granularity
(not (eq granularity
'object
)))))
3744 (org-element-item-parser limit structure raw-secondary-p
))
3746 ((eq special
'table-row
) (org-element-table-row-parser limit
))
3748 ((eq special
'node-property
) (org-element-node-property-parser limit
))
3750 ((org-with-limited-levels (org-at-heading-p))
3751 (org-element-headline-parser limit raw-secondary-p
))
3752 ;; Sections (must be checked after headline).
3753 ((eq special
'section
) (org-element-section-parser limit
))
3754 ((eq special
'quote-section
) (org-element-quote-section-parser limit
))
3755 ((eq special
'first-section
)
3756 (org-element-section-parser
3757 (or (save-excursion (org-with-limited-levels (outline-next-heading)))
3759 ;; When not at bol, point is at the beginning of an item or
3760 ;; a footnote definition: next item is always a paragraph.
3761 ((not (bolp)) (org-element-paragraph-parser limit
(list (point))))
3762 ;; Planning and Clock.
3763 ((looking-at org-planning-or-clock-line-re
)
3764 (if (equal (match-string 1) org-clock-string
)
3765 (org-element-clock-parser limit
)
3766 (org-element-planning-parser limit
)))
3769 (org-element-inlinetask-parser limit raw-secondary-p
))
3770 ;; From there, elements can have affiliated keywords.
3771 (t (let ((affiliated (org-element--collect-affiliated-keywords limit
)))
3773 ;; Jumping over affiliated keywords put point off-limits.
3774 ;; Parse them as regular keywords.
3776 (goto-char (car affiliated
))
3777 (org-element-keyword-parser limit nil
))
3778 ;; LaTeX Environment.
3779 ((looking-at "[ \t]*\\\\begin{\\([A-Za-z0-9*]+\\)}[ \t]*$")
3780 (org-element-latex-environment-parser limit affiliated
))
3781 ;; Drawer and Property Drawer.
3782 ((looking-at org-drawer-regexp
)
3783 (if (equal (match-string 1) "PROPERTIES")
3784 (org-element-property-drawer-parser limit affiliated
)
3785 (org-element-drawer-parser limit affiliated
)))
3787 ((looking-at "[ \t]*:\\( \\|$\\)")
3788 (org-element-fixed-width-parser limit affiliated
))
3789 ;; Inline Comments, Blocks, Babel Calls, Dynamic Blocks and
3791 ((looking-at "[ \t]*#")
3792 (goto-char (match-end 0))
3793 (cond ((looking-at "\\(?: \\|$\\)")
3795 (org-element-comment-parser limit affiliated
))
3796 ((looking-at "\\+BEGIN_\\(\\S-+\\)")
3798 (let ((parser (assoc (upcase (match-string 1))
3799 org-element-block-name-alist
)))
3800 (if parser
(funcall (cdr parser
) limit affiliated
)
3801 (org-element-special-block-parser limit affiliated
))))
3802 ((looking-at "\\+CALL:")
3804 (org-element-babel-call-parser limit affiliated
))
3805 ((looking-at "\\+BEGIN:? ")
3807 (org-element-dynamic-block-parser limit affiliated
))
3808 ((looking-at "\\+\\S-+:")
3810 (org-element-keyword-parser limit affiliated
))
3813 (org-element-paragraph-parser limit affiliated
))))
3814 ;; Footnote Definition.
3815 ((looking-at org-footnote-definition-re
)
3816 (org-element-footnote-definition-parser limit affiliated
))
3818 ((looking-at "[ \t]*-\\{5,\\}[ \t]*$")
3819 (org-element-horizontal-rule-parser limit affiliated
))
3822 (org-element-diary-sexp-parser limit affiliated
))
3824 ((org-at-table-p t
) (org-element-table-parser limit affiliated
))
3826 ((looking-at (org-item-re))
3827 (org-element-plain-list-parser
3828 limit affiliated
(or structure
(org-list-struct))))
3829 ;; Default element: Paragraph.
3830 (t (org-element-paragraph-parser limit affiliated
)))))))))
3833 ;; Most elements can have affiliated keywords. When looking for an
3834 ;; element beginning, we want to move before them, as they belong to
3835 ;; that element, and, in the meantime, collect information they give
3836 ;; into appropriate properties. Hence the following function.
3838 (defun org-element--collect-affiliated-keywords (limit)
3839 "Collect affiliated keywords from point down to LIMIT.
3841 Return a list whose CAR is the position at the first of them and
3842 CDR a plist of keywords and values and move point to the
3843 beginning of the first line after them.
3845 As a special case, if element doesn't start at the beginning of
3846 the line (i.e. a paragraph starting an item), CAR is current
3847 position of point and CDR is nil."
3848 (if (not (bolp)) (list (point))
3849 (let ((case-fold-search t
)
3851 ;; RESTRICT is the list of objects allowed in parsed
3853 (restrict (org-element-restriction 'keyword
))
3855 (while (and (< (point) limit
) (looking-at org-element--affiliated-re
))
3856 (let* ((raw-kwd (upcase (match-string 1)))
3857 ;; Apply translation to RAW-KWD. From there, KWD is
3858 ;; the official keyword.
3859 (kwd (or (cdr (assoc raw-kwd
3860 org-element-keyword-translation-alist
))
3862 ;; Find main value for any keyword.
3866 (buffer-substring-no-properties
3867 (match-end 0) (point-at-eol)))))
3868 ;; PARSEDP is non-nil when keyword should have its
3870 (parsedp (member kwd org-element-parsed-keywords
))
3871 ;; If KWD is a dual keyword, find its secondary
3872 ;; value. Maybe parse it.
3873 (dualp (member kwd org-element-dual-keywords
))
3876 (let ((sec (org-match-string-no-properties 2)))
3877 (if (or (not sec
) (not parsedp
)) sec
3878 (org-element-parse-secondary-string sec restrict
)))))
3879 ;; Attribute a property name to KWD.
3880 (kwd-sym (and kwd
(intern (concat ":" (downcase kwd
))))))
3881 ;; Now set final shape for VALUE.
3883 (setq value
(org-element-parse-secondary-string value restrict
)))
3885 (setq value
(and (or value dual-value
) (cons value dual-value
))))
3886 (when (or (member kwd org-element-multiple-keywords
)
3887 ;; Attributes can always appear on multiple lines.
3888 (string-match "^ATTR_" kwd
))
3889 (setq value
(cons value
(plist-get output kwd-sym
))))
3890 ;; Eventually store the new value in OUTPUT.
3891 (setq output
(plist-put output kwd-sym value
))
3892 ;; Move to next keyword.
3894 ;; If affiliated keywords are orphaned: move back to first one.
3895 ;; They will be parsed as a paragraph.
3896 (when (looking-at "[ \t]*$") (goto-char origin
) (setq output nil
))
3898 (cons origin output
))))
3904 ;; The two major functions here are `org-element-parse-buffer', which
3905 ;; parses Org syntax inside the current buffer, taking into account
3906 ;; region, narrowing, or even visibility if specified, and
3907 ;; `org-element-parse-secondary-string', which parses objects within
3910 ;; The (almost) almighty `org-element-map' allows to apply a function
3911 ;; on elements or objects matching some type, and accumulate the
3912 ;; resulting values. In an export situation, it also skips unneeded
3913 ;; parts of the parse tree.
3915 (defun org-element-parse-buffer (&optional granularity visible-only
)
3916 "Recursively parse the buffer and return structure.
3917 If narrowing is in effect, only parse the visible part of the
3920 Optional argument GRANULARITY determines the depth of the
3921 recursion. It can be set to the following symbols:
3923 `headline' Only parse headlines.
3924 `greater-element' Don't recurse into greater elements excepted
3925 headlines and sections. Thus, elements
3926 parsed are the top-level ones.
3927 `element' Parse everything but objects and plain text.
3928 `object' Parse the complete buffer (default).
3930 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3933 An element or an objects is represented as a list with the
3934 pattern (TYPE PROPERTIES CONTENTS), where :
3936 TYPE is a symbol describing the element or object. See
3937 `org-element-all-elements' and `org-element-all-objects' for an
3938 exhaustive list of such symbols. One can retrieve it with
3939 `org-element-type' function.
3941 PROPERTIES is the list of attributes attached to the element or
3942 object, as a plist. Although most of them are specific to the
3943 element or object type, all types share `:begin', `:end',
3944 `:post-blank' and `:parent' properties, which respectively
3945 refer to buffer position where the element or object starts,
3946 ends, the number of white spaces or blank lines after it, and
3947 the element or object containing it. Properties values can be
3948 obtained by using `org-element-property' function.
3950 CONTENTS is a list of elements, objects or raw strings
3951 contained in the current element or object, when applicable.
3952 One can access them with `org-element-contents' function.
3954 The Org buffer has `org-data' as type and nil as properties.
3955 `org-element-map' function can be used to find specific elements
3956 or objects within the parse tree.
3958 This function assumes that current major mode is `org-mode'."
3960 (goto-char (point-min))
3961 (org-skip-whitespace)
3962 (org-element--parse-elements
3963 (point-at-bol) (point-max)
3964 ;; Start in `first-section' mode so text before the first
3965 ;; headline belongs to a section.
3966 'first-section nil granularity visible-only
(list 'org-data nil
))))
3968 (defun org-element-parse-secondary-string (string restriction
&optional parent
)
3969 "Recursively parse objects in STRING and return structure.
3971 RESTRICTION is a symbol limiting the object types that will be
3974 Optional argument PARENT, when non-nil, is the element or object
3975 containing the secondary string. It is used to set correctly
3976 `:parent' property within the string."
3977 ;; Copy buffer-local variables listed in
3978 ;; `org-element-object-variables' into temporary buffer. This is
3979 ;; required since object parsing is dependent on these variables.
3980 (let ((pairs (delq nil
(mapcar (lambda (var)
3982 (cons var
(symbol-value var
))))
3983 org-element-object-variables
))))
3985 (mapc (lambda (pair) (org-set-local (car pair
) (cdr pair
))) pairs
)
3987 (let ((secondary (org-element--parse-objects
3988 (point-min) (point-max) nil restriction
)))
3990 (mapc (lambda (obj) (org-element-put-property obj
:parent parent
))
3994 (defun org-element-map
3995 (data types fun
&optional info first-match no-recursion with-affiliated
)
3996 "Map a function on selected elements or objects.
3998 DATA is a parse tree, an element, an object, a string, or a list
3999 of such constructs. TYPES is a symbol or list of symbols of
4000 elements or objects types (see `org-element-all-elements' and
4001 `org-element-all-objects' for a complete list of types). FUN is
4002 the function called on the matching element or object. It has to
4003 accept one argument: the element or object itself.
4005 When optional argument INFO is non-nil, it should be a plist
4006 holding export options. In that case, parts of the parse tree
4007 not exportable according to that property list will be skipped.
4009 When optional argument FIRST-MATCH is non-nil, stop at the first
4010 match for which FUN doesn't return nil, and return that value.
4012 Optional argument NO-RECURSION is a symbol or a list of symbols
4013 representing elements or objects types. `org-element-map' won't
4014 enter any recursive element or object whose type belongs to that
4015 list. Though, FUN can still be applied on them.
4017 When optional argument WITH-AFFILIATED is non-nil, FUN will also
4018 apply to matching objects within parsed affiliated keywords (see
4019 `org-element-parsed-keywords').
4021 Nil values returned from FUN do not appear in the results.
4027 Assuming TREE is a variable containing an Org buffer parse tree,
4028 the following example will return a flat list of all `src-block'
4029 and `example-block' elements in it:
4031 \(org-element-map tree '(example-block src-block) 'identity)
4033 The following snippet will find the first headline with a level
4034 of 1 and a \"phone\" tag, and will return its beginning position:
4036 \(org-element-map tree 'headline
4038 \(and (= (org-element-property :level hl) 1)
4039 \(member \"phone\" (org-element-property :tags hl))
4040 \(org-element-property :begin hl)))
4043 The next example will return a flat list of all `plain-list' type
4044 elements in TREE that are not a sub-list themselves:
4046 \(org-element-map tree 'plain-list 'identity nil nil 'plain-list)
4048 Eventually, this example will return a flat list of all `bold'
4049 type objects containing a `latex-snippet' type object, even
4050 looking into captions:
4052 \(org-element-map tree 'bold
4054 \(and (org-element-map b 'latex-snippet 'identity nil t) b))
4056 ;; Ensure TYPES and NO-RECURSION are a list, even of one element.
4057 (unless (listp types
) (setq types
(list types
)))
4058 (unless (listp no-recursion
) (setq no-recursion
(list no-recursion
)))
4059 ;; Recursion depth is determined by --CATEGORY.
4062 (let ((category 'greater-elements
))
4063 (mapc (lambda (type)
4064 (cond ((or (memq type org-element-all-objects
)
4065 (eq type
'plain-text
))
4066 ;; If one object is found, the function
4067 ;; has to recurse into every object.
4068 (throw 'found
'objects
))
4069 ((not (memq type org-element-greater-elements
))
4070 ;; If one regular element is found, the
4071 ;; function has to recurse, at least,
4072 ;; into every element it encounters.
4073 (and (not (eq category
'elements
))
4074 (setq category
'elements
)))))
4077 ;; Compute properties for affiliated keywords if necessary.
4079 (and with-affiliated
4080 (mapcar (lambda (kwd)
4081 (cons kwd
(intern (concat ":" (downcase kwd
)))))
4082 org-element-affiliated-keywords
)))
4088 ;; Recursively walk DATA. INFO, if non-nil, is a plist
4089 ;; holding contextual information.
4090 (let ((--type (org-element-type --data
)))
4093 ;; Ignored element in an export context.
4094 ((and info
(memq --data
(plist-get info
:ignore-list
))))
4095 ;; List of elements or objects.
4096 ((not --type
) (mapc --walk-tree --data
))
4097 ;; Unconditionally enter parse trees.
4098 ((eq --type
'org-data
)
4099 (mapc --walk-tree
(org-element-contents --data
)))
4101 ;; Check if TYPE is matching among TYPES. If so,
4102 ;; apply FUN to --DATA and accumulate return value
4103 ;; into --ACC (or exit if FIRST-MATCH is non-nil).
4104 (when (memq --type types
)
4105 (let ((result (funcall fun --data
)))
4106 (cond ((not result
))
4107 (first-match (throw '--map-first-match result
))
4108 (t (push result --acc
)))))
4109 ;; If --DATA has a secondary string that can contain
4110 ;; objects with their type among TYPES, look into it.
4111 (when (and (eq --category
'objects
) (not (stringp --data
)))
4113 (assq --type org-element-secondary-value-alist
)))
4115 (funcall --walk-tree
4116 (org-element-property (cdr sec-prop
) --data
)))))
4117 ;; If --DATA has any affiliated keywords and
4118 ;; WITH-AFFILIATED is non-nil, look for objects in
4120 (when (and with-affiliated
4121 (eq --category
'objects
)
4122 (memq --type org-element-all-elements
))
4123 (mapc (lambda (kwd-pair)
4124 (let ((kwd (car kwd-pair
))
4125 (value (org-element-property
4126 (cdr kwd-pair
) --data
)))
4127 ;; Pay attention to the type of value.
4128 ;; Preserve order for multiple keywords.
4131 ((and (member kwd org-element-multiple-keywords
)
4132 (member kwd org-element-dual-keywords
))
4133 (mapc (lambda (line)
4134 (funcall --walk-tree
(cdr line
))
4135 (funcall --walk-tree
(car line
)))
4137 ((member kwd org-element-multiple-keywords
)
4138 (mapc (lambda (line) (funcall --walk-tree line
))
4140 ((member kwd org-element-dual-keywords
)
4141 (funcall --walk-tree
(cdr value
))
4142 (funcall --walk-tree
(car value
)))
4143 (t (funcall --walk-tree value
)))))
4144 --affiliated-alist
))
4145 ;; Determine if a recursion into --DATA is possible.
4147 ;; --TYPE is explicitly removed from recursion.
4148 ((memq --type no-recursion
))
4149 ;; --DATA has no contents.
4150 ((not (org-element-contents --data
)))
4151 ;; Looking for greater elements but --DATA is simply
4152 ;; an element or an object.
4153 ((and (eq --category
'greater-elements
)
4154 (not (memq --type org-element-greater-elements
))))
4155 ;; Looking for elements but --DATA is an object.
4156 ((and (eq --category
'elements
)
4157 (memq --type org-element-all-objects
)))
4158 ;; In any other case, map contents.
4159 (t (mapc --walk-tree
(org-element-contents --data
)))))))))))
4160 (catch '--map-first-match
4161 (funcall --walk-tree data
)
4162 ;; Return value in a proper order.
4164 (put 'org-element-map
'lisp-indent-function
2)
4166 ;; The following functions are internal parts of the parser.
4168 ;; The first one, `org-element--parse-elements' acts at the element's
4171 ;; The second one, `org-element--parse-objects' applies on all objects
4172 ;; of a paragraph or a secondary string. It uses
4173 ;; `org-element--get-next-object-candidates' to optimize the search of
4174 ;; the next object in the buffer.
4176 ;; More precisely, that function looks for every allowed object type
4177 ;; first. Then, it discards failed searches, keeps further matches,
4178 ;; and searches again types matched behind point, for subsequent
4179 ;; calls. Thus, searching for a given type fails only once, and every
4180 ;; object is searched only once at top level (but sometimes more for
4183 (defun org-element--parse-elements
4184 (beg end special structure granularity visible-only acc
)
4185 "Parse elements between BEG and END positions.
4187 SPECIAL prioritize some elements over the others. It can be set
4188 to `first-section', `quote-section', `section' `item' or
4191 When value is `item', STRUCTURE will be used as the current list
4194 GRANULARITY determines the depth of the recursion. See
4195 `org-element-parse-buffer' for more information.
4197 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
4200 Elements are accumulated into ACC."
4203 ;; When parsing only headlines, skip any text before first one.
4204 (when (and (eq granularity
'headline
) (not (org-at-heading-p)))
4205 (org-with-limited-levels (outline-next-heading)))
4207 (while (< (point) end
)
4208 ;; Find current element's type and parse it accordingly to
4210 (let* ((element (org-element--current-element
4211 end granularity special structure
))
4212 (type (org-element-type element
))
4213 (cbeg (org-element-property :contents-begin element
)))
4214 (goto-char (org-element-property :end element
))
4215 ;; Fill ELEMENT contents by side-effect.
4217 ;; If VISIBLE-ONLY is true and element is hidden or if it has
4218 ;; no contents, don't modify it.
4219 ((or (and visible-only
(org-element-property :hiddenp element
))
4221 ;; Greater element: parse it between `contents-begin' and
4222 ;; `contents-end'. Make sure GRANULARITY allows the
4223 ;; recursion, or ELEMENT is a headline, in which case going
4224 ;; inside is mandatory, in order to get sub-level headings.
4225 ((and (memq type org-element-greater-elements
)
4226 (or (memq granularity
'(element object nil
))
4227 (and (eq granularity
'greater-element
)
4229 (eq type
'headline
)))
4230 (org-element--parse-elements
4231 cbeg
(org-element-property :contents-end element
)
4232 ;; Possibly switch to a special mode.
4235 (if (org-element-property :quotedp element
) 'quote-section
4238 (property-drawer 'node-property
)
4240 (and (memq type
'(item plain-list
))
4241 (org-element-property :structure element
))
4242 granularity visible-only element
))
4243 ;; ELEMENT has contents. Parse objects inside, if
4244 ;; GRANULARITY allows it.
4245 ((memq granularity
'(object nil
))
4246 (org-element--parse-objects
4247 cbeg
(org-element-property :contents-end element
) element
4248 (org-element-restriction type
))))
4249 (org-element-adopt-elements acc element
)))
4253 (defun org-element--parse-objects (beg end acc restriction
)
4254 "Parse objects between BEG and END and return recursive structure.
4256 Objects are accumulated in ACC.
4258 RESTRICTION is a list of object successors which are allowed in
4259 the current object."
4260 (let ((candidates 'initial
))
4263 (while (and (< (point) end
)
4264 (setq candidates
(org-element--get-next-object-candidates
4265 end restriction candidates
)))
4267 (let ((pos (apply 'min
(mapcar 'cdr candidates
))))
4270 (funcall (intern (format "org-element-%s-parser"
4271 (car (rassq pos candidates
)))))))))
4272 ;; 1. Text before any object. Untabify it.
4273 (let ((obj-beg (org-element-property :begin next-object
)))
4274 (unless (= (point) obj-beg
)
4276 (org-element-adopt-elements
4278 (replace-regexp-in-string
4279 "\t" (make-string tab-width ?
)
4280 (buffer-substring-no-properties (point) obj-beg
))))))
4282 (let ((obj-end (org-element-property :end next-object
))
4283 (cont-beg (org-element-property :contents-begin next-object
)))
4284 ;; Fill contents of NEXT-OBJECT by side-effect, if it has
4285 ;; a recursive type.
4287 (memq (car next-object
) org-element-recursive-objects
))
4291 (org-element-property :contents-end next-object
))
4292 (org-element--parse-objects
4293 (point-min) (point-max) next-object
4294 (org-element-restriction next-object
))))
4295 (setq acc
(org-element-adopt-elements acc next-object
))
4296 (goto-char obj-end
))))
4297 ;; 3. Text after last object. Untabify it.
4298 (unless (= (point) end
)
4300 (org-element-adopt-elements
4302 (replace-regexp-in-string
4303 "\t" (make-string tab-width ?
)
4304 (buffer-substring-no-properties (point) end
)))))
4308 (defun org-element--get-next-object-candidates (limit restriction objects
)
4309 "Return an alist of candidates for the next object.
4311 LIMIT bounds the search, and RESTRICTION narrows candidates to
4312 some object successors.
4314 OBJECTS is the previous candidates alist. If it is set to
4315 `initial', no search has been done before, and all symbols in
4316 RESTRICTION should be looked after.
4318 Return value is an alist whose CAR is the object type and CDR its
4319 beginning position."
4322 (if (eq objects
'initial
)
4323 ;; When searching for the first time, look for every successor
4324 ;; allowed in RESTRICTION.
4327 (funcall (intern (format "org-element-%s-successor" res
)) limit
))
4329 ;; Focus on objects returned during last search. Keep those
4330 ;; still after point. Search again objects before it.
4333 (if (>= (cdr obj
) (point)) obj
4334 (let* ((type (car obj
))
4335 (succ (or (cdr (assq type org-element-object-successor-alist
))
4338 (funcall (intern (format "org-element-%s-successor" succ
))
4344 ;;; Towards A Bijective Process
4346 ;; The parse tree obtained with `org-element-parse-buffer' is really
4347 ;; a snapshot of the corresponding Org buffer. Therefore, it can be
4348 ;; interpreted and expanded into a string with canonical Org syntax.
4349 ;; Hence `org-element-interpret-data'.
4351 ;; The function relies internally on
4352 ;; `org-element--interpret-affiliated-keywords'.
4355 (defun org-element-interpret-data (data &optional parent
)
4356 "Interpret DATA as Org syntax.
4358 DATA is a parse tree, an element, an object or a secondary string
4361 Optional argument PARENT is used for recursive calls. It contains
4362 the element or object containing data, or nil.
4364 Return Org syntax as a string."
4365 (let* ((type (org-element-type data
))
4368 ;; Secondary string.
4371 (lambda (obj) (org-element-interpret-data obj parent
))
4373 ;; Full Org document.
4374 ((eq type
'org-data
)
4376 (lambda (obj) (org-element-interpret-data obj parent
))
4377 (org-element-contents data
) ""))
4378 ;; Plain text: remove `:parent' text property from output.
4379 ((stringp data
) (org-no-properties data
))
4380 ;; Element/Object without contents.
4381 ((not (org-element-contents data
))
4382 (funcall (intern (format "org-element-%s-interpreter" type
))
4384 ;; Element/Object with contents.
4386 (let* ((greaterp (memq type org-element-greater-elements
))
4387 (objectp (and (not greaterp
)
4388 (memq type org-element-recursive-objects
)))
4391 (lambda (obj) (org-element-interpret-data obj data
))
4392 (org-element-contents
4393 (if (or greaterp objectp
) data
4394 ;; Elements directly containing objects must
4395 ;; have their indentation normalized first.
4396 (org-element-normalize-contents
4398 ;; When normalizing first paragraph of an
4399 ;; item or a footnote-definition, ignore
4400 ;; first line's indentation.
4401 (and (eq type
'paragraph
)
4402 (equal data
(car (org-element-contents parent
)))
4403 (memq (org-element-type parent
)
4404 '(footnote-definition item
))))))
4406 (funcall (intern (format "org-element-%s-interpreter" type
))
4408 (if greaterp
(org-element-normalize-contents contents
)
4410 (if (memq type
'(org-data plain-text nil
)) results
4411 ;; Build white spaces. If no `:post-blank' property is
4412 ;; specified, assume its value is 0.
4413 (let ((post-blank (or (org-element-property :post-blank data
) 0)))
4414 (if (memq type org-element-all-objects
)
4415 (concat results
(make-string post-blank
32))
4417 (org-element--interpret-affiliated-keywords data
)
4418 (org-element-normalize-string results
)
4419 (make-string post-blank
10)))))))
4421 (defun org-element--interpret-affiliated-keywords (element)
4422 "Return ELEMENT's affiliated keywords as Org syntax.
4423 If there is no affiliated keyword, return the empty string."
4424 (let ((keyword-to-org
4428 (when (member key org-element-dual-keywords
)
4429 (setq dual
(cdr value
) value
(car value
)))
4432 (format "[%s]" (org-element-interpret-data dual
)))
4434 (if (member key org-element-parsed-keywords
)
4435 (org-element-interpret-data value
)
4440 (let ((value (org-element-property prop element
))
4441 (keyword (upcase (substring (symbol-name prop
) 1))))
4443 (if (or (member keyword org-element-multiple-keywords
)
4444 ;; All attribute keywords can have multiple lines.
4445 (string-match "^ATTR_" keyword
))
4446 (mapconcat (lambda (line) (funcall keyword-to-org keyword line
))
4449 (funcall keyword-to-org keyword value
)))))
4450 ;; List all ELEMENT's properties matching an attribute line or an
4451 ;; affiliated keyword, but ignore translated keywords since they
4452 ;; cannot belong to the property list.
4453 (loop for prop in
(nth 1 element
) by
'cddr
4454 when
(let ((keyword (upcase (substring (symbol-name prop
) 1))))
4455 (or (string-match "^ATTR_" keyword
)
4457 (member keyword org-element-affiliated-keywords
)
4459 org-element-keyword-translation-alist
)))))
4463 ;; Because interpretation of the parse tree must return the same
4464 ;; number of blank lines between elements and the same number of white
4465 ;; space after objects, some special care must be given to white
4468 ;; The first function, `org-element-normalize-string', ensures any
4469 ;; string different from the empty string will end with a single
4470 ;; newline character.
4472 ;; The second function, `org-element-normalize-contents', removes
4473 ;; global indentation from the contents of the current element.
4475 (defun org-element-normalize-string (s)
4476 "Ensure string S ends with a single newline character.
4478 If S isn't a string return it unchanged. If S is the empty
4479 string, return it. Otherwise, return a new string with a single
4480 newline character at its end."
4482 ((not (stringp s
)) s
)
4484 (t (and (string-match "\\(\n[ \t]*\\)*\\'" s
)
4485 (replace-match "\n" nil nil s
)))))
4487 (defun org-element-normalize-contents (element &optional ignore-first
)
4488 "Normalize plain text in ELEMENT's contents.
4490 ELEMENT must only contain plain text and objects.
4492 If optional argument IGNORE-FIRST is non-nil, ignore first line's
4493 indentation to compute maximal common indentation.
4495 Return the normalized element that is element with global
4496 indentation removed from its contents. The function assumes that
4497 indentation is not done with TAB characters."
4498 (let* (ind-list ; for byte-compiler
4499 collect-inds
; for byte-compiler
4502 ;; Return list of indentations within BLOB. This is done by
4503 ;; walking recursively BLOB and updating IND-LIST along the
4504 ;; way. FIRST-FLAG is non-nil when the first string hasn't
4505 ;; been seen yet. It is required as this string is the only
4506 ;; one whose indentation doesn't happen after a newline
4508 (lambda (blob first-flag
)
4511 (when (and first-flag
(stringp object
))
4512 (setq first-flag nil
)
4513 (string-match "\\`\\( *\\)" object
)
4514 (let ((len (length (match-string 1 object
))))
4515 ;; An indentation of zero means no string will be
4516 ;; modified. Quit the process.
4517 (if (zerop len
) (throw 'zero
(setq ind-list nil
))
4518 (push len ind-list
))))
4522 ;; Avoid matching blank or empty lines.
4523 (while (and (string-match "\n\\( *\\)\\(.\\)" object start
)
4524 (not (equal (match-string 2 object
) " ")))
4525 (setq start
(match-end 0))
4526 (push (length (match-string 1 object
)) ind-list
))))
4527 ((memq (org-element-type object
) org-element-recursive-objects
)
4528 (funcall collect-inds object first-flag
))))
4529 (org-element-contents blob
))))))
4530 ;; Collect indentation list in ELEMENT. Possibly remove first
4531 ;; value if IGNORE-FIRST is non-nil.
4532 (catch 'zero
(funcall collect-inds element
(not ignore-first
)))
4533 (if (not ind-list
) element
4534 ;; Build ELEMENT back, replacing each string with the same
4535 ;; string minus common indentation.
4536 (let* (build ; For byte compiler.
4539 (lambda (blob mci first-flag
)
4540 ;; Return BLOB with all its strings indentation
4541 ;; shortened from MCI white spaces. FIRST-FLAG is
4542 ;; non-nil when the first string hasn't been seen
4547 (when (and first-flag
(stringp object
))
4548 (setq first-flag nil
)
4550 (replace-regexp-in-string
4551 (format "\\` \\{%d\\}" mci
) "" object
)))
4554 (replace-regexp-in-string
4555 (format "\n \\{%d\\}" mci
) "\n" object
))
4556 ((memq (org-element-type object
)
4557 org-element-recursive-objects
)
4558 (funcall build object mci first-flag
))
4560 (org-element-contents blob
)))
4562 (funcall build element
(apply 'min ind-list
) (not ignore-first
))))))
4568 ;; The first move is to implement a way to obtain the smallest element
4569 ;; containing point. This is the job of `org-element-at-point'. It
4570 ;; basically jumps back to the beginning of section containing point
4571 ;; and moves, element after element, with
4572 ;; `org-element--current-element' until the container is found. Note:
4573 ;; When using `org-element-at-point', secondary values are never
4574 ;; parsed since the function focuses on elements, not on objects.
4576 ;; At a deeper level, `org-element-context' lists all elements and
4577 ;; objects containing point.
4579 ;; `org-element-nested-p' and `org-element-swap-A-B' may be used
4580 ;; internally by navigation and manipulation tools.
4583 (defun org-element-at-point (&optional keep-trail
)
4584 "Determine closest element around point.
4586 Return value is a list like (TYPE PROPS) where TYPE is the type
4587 of the element and PROPS a plist of properties associated to the
4590 Possible types are defined in `org-element-all-elements'.
4591 Properties depend on element or object type, but always include
4592 `:begin', `:end', `:parent' and `:post-blank' properties.
4594 As a special case, if point is at the very beginning of a list or
4595 sub-list, returned element will be that list instead of the first
4596 item. In the same way, if point is at the beginning of the first
4597 row of a table, returned element will be the table instead of the
4600 If optional argument KEEP-TRAIL is non-nil, the function returns
4601 a list of elements leading to element at point. The list's CAR
4602 is always the element at point. The following positions contain
4603 element's siblings, then parents, siblings of parents, until the
4604 first element of current section."
4605 (org-with-wide-buffer
4606 ;; If at a headline, parse it. It is the sole element that
4607 ;; doesn't require to know about context. Be sure to disallow
4608 ;; secondary string parsing, though.
4609 (if (org-with-limited-levels (org-at-heading-p))
4612 (if (not keep-trail
) (org-element-headline-parser (point-max) t
)
4613 (list (org-element-headline-parser (point-max) t
))))
4614 ;; Otherwise move at the beginning of the section containing
4617 (let ((origin (point))
4618 (end (save-excursion
4619 (org-with-limited-levels (outline-next-heading)) (point)))
4620 element type special-flag trail struct prevs parent
)
4621 (org-with-limited-levels
4622 (if (org-before-first-heading-p)
4623 ;; In empty lines at buffer's beginning, return nil.
4624 (progn (goto-char (point-min))
4625 (org-skip-whitespace)
4626 (when (or (eobp) (> (line-beginning-position) origin
))
4628 (org-back-to-heading)
4630 (org-skip-whitespace)
4631 (when (> (line-beginning-position) origin
)
4632 ;; In blank lines just after the headline, point still
4633 ;; belongs to the headline.
4635 (progn (org-back-to-heading)
4636 (if (not keep-trail
)
4637 (org-element-headline-parser (point-max) t
)
4638 (list (org-element-headline-parser
4639 (point-max) t
))))))))
4641 ;; Parse successively each element, skipping those ending
4642 ;; before original position.
4645 (org-element--current-element end
'element special-flag struct
)
4647 (org-element-put-property element
:parent parent
)
4648 (when keep-trail
(push element trail
))
4650 ;; 1. Skip any element ending before point. Also skip
4651 ;; element ending at point when we're sure that another
4652 ;; element has started.
4653 ((let ((elem-end (org-element-property :end element
)))
4654 (when (or (< elem-end origin
)
4655 (and (= elem-end origin
) (/= elem-end end
)))
4656 (goto-char elem-end
))))
4657 ;; 2. An element containing point is always the element at
4659 ((not (memq type org-element-greater-elements
))
4660 (throw 'exit
(if keep-trail trail element
)))
4661 ;; 3. At any other greater element type, if point is
4662 ;; within contents, move into it.
4664 (let ((cbeg (org-element-property :contents-begin element
))
4665 (cend (org-element-property :contents-end element
)))
4666 (if (or (not cbeg
) (not cend
) (> cbeg origin
) (< cend origin
)
4667 ;; Create an anchor for tables and plain lists:
4668 ;; when point is at the very beginning of these
4669 ;; elements, ignoring affiliated keywords,
4670 ;; target them instead of their contents.
4671 (and (= cbeg origin
) (memq type
'(plain-list table
)))
4672 ;; When point is at contents end, do not move
4673 ;; into elements with an explicit ending, but
4674 ;; return that element instead.
4675 (and (= cend origin
)
4678 drawer dynamic-block inlinetask item
4679 plain-list property-drawer quote-block
4681 (throw 'exit
(if keep-trail trail element
))
4682 (setq parent element
)
4685 (setq special-flag
'item
4686 struct
(org-element-property :structure element
)))
4687 (item (setq special-flag nil
))
4689 (setq special-flag
'node-property struct nil
))
4690 (table (setq special-flag
'table-row struct nil
))
4691 (otherwise (setq special-flag nil struct nil
)))
4693 (goto-char cbeg
)))))))))))
4696 (defun org-element-context (&optional element
)
4697 "Return closest element or object around point.
4699 Return value is a list like (TYPE PROPS) where TYPE is the type
4700 of the element or object and PROPS a plist of properties
4703 Possible types are defined in `org-element-all-elements' and
4704 `org-element-all-objects'. Properties depend on element or
4705 object type, but always include `:begin', `:end', `:parent' and
4708 Optional argument ELEMENT, when non-nil, is the closest element
4709 containing point, as returned by `org-element-at-point'.
4710 Providing it allows for quicker computation."
4711 (org-with-wide-buffer
4712 (let* ((origin (point))
4713 (element (or element
(org-element-at-point)))
4714 (type (org-element-type element
))
4716 ;; Check if point is inside an element containing objects or at
4717 ;; a secondary string. In that case, move to beginning of the
4718 ;; element or secondary string and set END to the other side.
4719 (if (not (or (let ((post (org-element-property :post-affiliated element
)))
4720 (and post
(> post origin
)
4721 (< (org-element-property :begin element
) origin
)
4722 (progn (beginning-of-line)
4723 (looking-at org-element--affiliated-re
)
4724 (member (upcase (match-string 1))
4725 org-element-parsed-keywords
))
4726 ;; We're at an affiliated keyword. Change
4727 ;; type to retrieve correct restrictions.
4728 (setq type
'keyword
)
4729 ;; Determine if we're at main or dual value.
4730 (if (and (match-end 2) (<= origin
(match-end 2)))
4731 (progn (goto-char (match-beginning 2))
4732 (setq end
(match-end 2)))
4733 (goto-char (match-end 0))
4734 (setq end
(line-end-position)))))
4735 (and (eq type
'item
)
4736 (let ((tag (org-element-property :tag element
)))
4740 (search-forward tag
(point-at-eol))
4741 (goto-char (match-beginning 0))
4742 (and (>= origin
(point))
4744 ;; `1+' is required so some
4745 ;; successors can match
4746 ;; properly their object.
4747 (setq end
(1+ (match-end 0)))))))))
4748 (and (memq type
'(headline inlinetask
))
4749 (progn (beginning-of-line)
4750 (skip-chars-forward "* ")
4751 (setq end
(point-at-eol))))
4752 (and (memq type
'(paragraph table-row verse-block
))
4753 (let ((cbeg (org-element-property
4754 :contents-begin element
))
4755 (cend (org-element-property
4756 :contents-end element
)))
4757 (and (>= origin cbeg
)
4759 (progn (goto-char cbeg
) (setq end cend
)))))
4760 (and (eq type
'keyword
)
4761 (let ((key (org-element-property :key element
)))
4762 (and (member key org-element-document-properties
)
4763 (progn (beginning-of-line)
4764 (search-forward key
(line-end-position) t
)
4766 (setq end
(line-end-position))))))))
4768 (let ((restriction (org-element-restriction type
))
4770 (candidates 'initial
))
4772 (while (setq candidates
(org-element--get-next-object-candidates
4773 end restriction candidates
))
4774 (let ((closest-cand (rassq (apply 'min
(mapcar 'cdr candidates
))
4776 ;; If ORIGIN is before next object in element, there's
4777 ;; no point in looking further.
4778 (if (> (cdr closest-cand
) origin
) (throw 'exit parent
)
4780 (progn (goto-char (cdr closest-cand
))
4781 (funcall (intern (format "org-element-%s-parser"
4782 (car closest-cand
))))))
4783 (cbeg (org-element-property :contents-begin object
))
4784 (cend (org-element-property :contents-end object
))
4785 (obj-end (org-element-property :end object
)))
4787 ;; ORIGIN is after OBJECT, so skip it.
4788 ((<= obj-end origin
)
4789 (if (/= obj-end end
) (goto-char obj-end
)
4791 (org-element-put-property
4792 object
:parent parent
))))
4793 ;; ORIGIN is within a non-recursive object or at
4794 ;; an object boundaries: Return that object.
4795 ((or (not cbeg
) (> cbeg origin
) (< cend origin
))
4797 (org-element-put-property object
:parent parent
)))
4798 ;; Otherwise, move within current object and
4799 ;; restrict search to the end of its contents.
4801 (org-element-put-property object
:parent parent
)
4803 restriction
(org-element-restriction object
)
4808 (defun org-element-nested-p (elem-A elem-B
)
4809 "Non-nil when elements ELEM-A and ELEM-B are nested."
4810 (let ((beg-A (org-element-property :begin elem-A
))
4811 (beg-B (org-element-property :begin elem-B
))
4812 (end-A (org-element-property :end elem-A
))
4813 (end-B (org-element-property :end elem-B
)))
4814 (or (and (>= beg-A beg-B
) (<= end-A end-B
))
4815 (and (>= beg-B beg-A
) (<= end-B end-A
)))))
4817 (defun org-element-swap-A-B (elem-A elem-B
)
4818 "Swap elements ELEM-A and ELEM-B.
4819 Assume ELEM-B is after ELEM-A in the buffer. Leave point at the
4821 (goto-char (org-element-property :begin elem-A
))
4822 ;; There are two special cases when an element doesn't start at bol:
4823 ;; the first paragraph in an item or in a footnote definition.
4824 (let ((specialp (not (bolp))))
4825 ;; Only a paragraph without any affiliated keyword can be moved at
4826 ;; ELEM-A position in such a situation. Note that the case of
4827 ;; a footnote definition is impossible: it cannot contain two
4828 ;; paragraphs in a row because it cannot contain a blank line.
4830 (or (not (eq (org-element-type elem-B
) 'paragraph
))
4831 (/= (org-element-property :begin elem-B
)
4832 (org-element-property :contents-begin elem-B
))))
4833 (error "Cannot swap elements"))
4834 ;; In a special situation, ELEM-A will have no indentation. We'll
4835 ;; give it ELEM-B's (which will in, in turn, have no indentation).
4836 (let* ((ind-B (when specialp
4837 (goto-char (org-element-property :begin elem-B
))
4838 (org-get-indentation)))
4839 (beg-A (org-element-property :begin elem-A
))
4840 (end-A (save-excursion
4841 (goto-char (org-element-property :end elem-A
))
4842 (skip-chars-backward " \r\t\n")
4844 (beg-B (org-element-property :begin elem-B
))
4845 (end-B (save-excursion
4846 (goto-char (org-element-property :end elem-B
))
4847 (skip-chars-backward " \r\t\n")
4849 ;; Store overlays responsible for visibility status. We
4850 ;; also need to store their boundaries as they will be
4851 ;; removed from buffer.
4854 (mapcar (lambda (ov) (list ov
(overlay-start ov
) (overlay-end ov
)))
4855 (overlays-in beg-A end-A
))
4856 (mapcar (lambda (ov) (list ov
(overlay-start ov
) (overlay-end ov
)))
4857 (overlays-in beg-B end-B
))))
4859 (body-A (buffer-substring beg-A end-A
))
4860 (body-B (delete-and-extract-region beg-B end-B
)))
4863 (setq body-B
(replace-regexp-in-string "\\`[ \t]*" "" body-B
))
4864 (org-indent-to-column ind-B
))
4866 ;; Restore ex ELEM-A overlays.
4867 (let ((offset (- beg-B beg-A
)))
4870 (car ov
) (+ (nth 1 ov
) offset
) (+ (nth 2 ov
) offset
)))
4873 (delete-region beg-A end-A
)
4875 ;; Restore ex ELEM-B overlays.
4878 (car ov
) (- (nth 1 ov
) offset
) (- (nth 2 ov
) offset
)))
4880 (goto-char (org-element-property :end elem-B
)))))
4882 (provide 'org-element
)
4885 ;; generated-autoload-file: "org-loaddefs.el"
4888 ;;; org-element.el ends here