1 ;;; org-element.el --- Parser And Applications for Org syntax
3 ;; Copyright (C) 2012 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; Org syntax can be divided into three categories: "Greater
26 ;; elements", "Elements" and "Objects".
28 ;; Elements are related to the structure of the document. Indeed, all
29 ;; elements are a cover for the document: each position within belongs
30 ;; to at least one element.
32 ;; An element always starts and ends at the beginning of a line. With
33 ;; a few exceptions (`clock', `headline', `inlinetask', `item',
34 ;; `planning', `node-property', `quote-section' `section' and
35 ;; `table-row' types), it can also accept a fixed set of keywords as
36 ;; attributes. Those are called "affiliated keywords" to distinguish
37 ;; them from other keywords, which are full-fledged elements. Almost
38 ;; all affiliated keywords are referenced in
39 ;; `org-element-affiliated-keywords'; the others are export attributes
40 ;; and start with "ATTR_" prefix.
42 ;; Element containing other elements (and only elements) are called
43 ;; greater elements. Concerned types are: `center-block', `drawer',
44 ;; `dynamic-block', `footnote-definition', `headline', `inlinetask',
45 ;; `item', `plain-list', `property-drawer', `quote-block', `section'
46 ;; and `special-block'.
48 ;; Other element types are: `babel-call', `clock', `comment',
49 ;; `comment-block', `diary-sexp', `example-block', `export-block',
50 ;; `fixed-width', `horizontal-rule', `keyword', `latex-environment',
51 ;; `node-property', `paragraph', `planning', `quote-section',
52 ;; `src-block', `table', `table-row' and `verse-block'. Among them,
53 ;; `paragraph' and `verse-block' types can contain Org objects and
56 ;; Objects are related to document's contents. Some of them are
57 ;; recursive. Associated types are of the following: `bold', `code',
58 ;; `entity', `export-snippet', `footnote-reference',
59 ;; `inline-babel-call', `inline-src-block', `italic',
60 ;; `latex-fragment', `line-break', `link', `macro', `radio-target',
61 ;; `statistics-cookie', `strike-through', `subscript', `superscript',
62 ;; `table-cell', `target', `timestamp', `underline' and `verbatim'.
64 ;; Some elements also have special properties whose value can hold
65 ;; objects themselves (i.e. an item tag or an headline name). Such
66 ;; values are called "secondary strings". Any object belongs to
67 ;; either an element or a secondary string.
69 ;; Notwithstanding affiliated keywords, each greater element, element
70 ;; and object has a fixed set of properties attached to it. Among
71 ;; them, four are shared by all types: `:begin' and `:end', which
72 ;; refer to the beginning and ending buffer positions of the
73 ;; considered element or object, `:post-blank', which holds the number
74 ;; of blank lines, or white spaces, at its end and `:parent' which
75 ;; refers to the element or object containing it. Greater elements,
76 ;; elements and objects containing objects will also have
77 ;; `:contents-begin' and `:contents-end' properties to delimit
78 ;; contents. Eventually, greater elements and elements accepting
79 ;; affiliated keywords will have a `:post-affiliated' property,
80 ;; referring to the buffer position after all such keywords.
82 ;; At the lowest level, a `:parent' property is also attached to any
83 ;; string, as a text property.
85 ;; Lisp-wise, an element or an object can be represented as a list.
86 ;; It follows the pattern (TYPE PROPERTIES CONTENTS), where:
87 ;; TYPE is a symbol describing the Org element or object.
88 ;; PROPERTIES is the property list attached to it. See docstring of
89 ;; appropriate parsing function to get an exhaustive
91 ;; CONTENTS is a list of elements, objects or raw strings contained
92 ;; in the current element or object, when applicable.
94 ;; An Org buffer is a nested list of such elements and objects, whose
95 ;; type is `org-data' and properties is nil.
97 ;; The first part of this file defines Org syntax, while the second
98 ;; one provide accessors and setters functions.
100 ;; The next part implements a parser and an interpreter for each
101 ;; element and object type in Org syntax.
103 ;; The following part creates a fully recursive buffer parser. It
104 ;; also provides a tool to map a function to elements or objects
105 ;; matching some criteria in the parse tree. Functions of interest
106 ;; are `org-element-parse-buffer', `org-element-map' and, to a lesser
107 ;; extent, `org-element-parse-secondary-string'.
109 ;; The penultimate part is the cradle of an interpreter for the
110 ;; obtained parse tree: `org-element-interpret-data'.
112 ;; The library ends by furnishing `org-element-at-point' function, and
113 ;; a way to give information about document structure around point
114 ;; with `org-element-context'.
119 (eval-when-compile (require 'cl
))
124 ;;; Definitions And Rules
126 ;; Define elements, greater elements and specify recursive objects,
127 ;; along with the affiliated keywords recognized. Also set up
128 ;; restrictions on recursive objects combinations.
130 ;; These variables really act as a control center for the parsing
133 (defconst org-element-paragraph-separate
135 ;; Headlines, inlinetasks.
136 org-outline-regexp
"\\|"
137 ;; Footnote definitions.
138 "\\[\\(?:[0-9]+\\|fn:[-_[:word:]]+\\)\\]" "\\|"
144 ;; Tables (any type).
145 "\\(?:|\\|\\+-[-+]\\)" "\\|"
146 ;; Blocks (any type), Babel calls, drawers (any type),
147 ;; fixed-width areas and keywords. Note: this is only an
148 ;; indication and need some thorough check.
151 "-\\{5,\\}[ \t]*$" "\\|"
152 ;; LaTeX environments.
153 "\\\\begin{\\([A-Za-z0-9]+\\*?\\)}" "\\|"
154 ;; Planning and Clock lines.
155 (regexp-opt (list org-scheduled-string
161 (let ((term (case org-plain-list-ordered-item-terminator
162 (?\
) ")") (?.
"\\.") (otherwise "[.)]")))
163 (alpha (and org-alphabetical-lists
"\\|[A-Za-z]")))
164 (concat "\\(?:[-+*]\\|\\(?:[0-9]+" alpha
"\\)" term
"\\)"
165 "\\(?:[ \t]\\|$\\)"))
167 "Regexp to separate paragraphs in an Org buffer.
168 In the case of lines starting with \"#\" and \":\", this regexp
169 is not sufficient to know if point is at a paragraph ending. See
170 `org-element-paragraph-parser' for more information.")
172 (defconst org-element-all-elements
173 '(babel-call center-block clock comment comment-block diary-sexp drawer
174 dynamic-block example-block export-block fixed-width
175 footnote-definition headline horizontal-rule inlinetask item
176 keyword latex-environment node-property paragraph plain-list
177 planning property-drawer quote-block quote-section section
178 special-block src-block table table-row verse-block
)
179 "Complete list of element types.")
181 (defconst org-element-greater-elements
182 '(center-block drawer dynamic-block footnote-definition headline inlinetask
183 item plain-list property-drawer quote-block section
185 "List of recursive element types aka Greater Elements.")
187 (defconst org-element-all-successors
188 '(export-snippet footnote-reference inline-babel-call inline-src-block
189 latex-or-entity line-break link macro radio-target
190 statistics-cookie sub
/superscript table-cell target
191 text-markup timestamp
)
192 "Complete list of successors.")
194 (defconst org-element-object-successor-alist
195 '((subscript . sub
/superscript
) (superscript . sub
/superscript
)
196 (bold . text-markup
) (code . text-markup
) (italic . text-markup
)
197 (strike-through . text-markup
) (underline . text-markup
)
198 (verbatim . text-markup
) (entity . latex-or-entity
)
199 (latex-fragment . latex-or-entity
))
200 "Alist of translations between object type and successor name.
202 Sharing the same successor comes handy when, for example, the
203 regexp matching one object can also match the other object.")
205 (defconst org-element-all-objects
206 '(bold code entity export-snippet footnote-reference inline-babel-call
207 inline-src-block italic line-break latex-fragment link macro
208 radio-target statistics-cookie strike-through subscript superscript
209 table-cell target timestamp underline verbatim
)
210 "Complete list of object types.")
212 (defconst org-element-recursive-objects
213 '(bold italic link subscript radio-target strike-through superscript
214 table-cell underline
)
215 "List of recursive object types.")
217 (defconst 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
)
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 latex-or-entity link macro radio-target
333 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.")
363 ;;; Accessors and Setters
365 ;; Provide four accessors: `org-element-type', `org-element-property'
366 ;; `org-element-contents' and `org-element-restriction'.
368 ;; Setter functions allow to modify elements by side effect. There is
369 ;; `org-element-put-property', `org-element-set-contents',
370 ;; `org-element-set-element' and `org-element-adopt-element'. Note
371 ;; that `org-element-set-element' and `org-element-adopt-elements' are
372 ;; higher level functions since also update `:parent' property.
374 (defsubst org-element-type
(element)
375 "Return type of ELEMENT.
377 The function returns the type of the element or object provided.
378 It can also return the following special value:
379 `plain-text' for a string
380 `org-data' for a complete document
381 nil in any other case."
383 ((not (consp element
)) (and (stringp element
) 'plain-text
))
384 ((symbolp (car element
)) (car element
))))
386 (defsubst org-element-property
(property element
)
387 "Extract the value from the PROPERTY of an ELEMENT."
388 (if (stringp element
) (get-text-property 0 property element
)
389 (plist-get (nth 1 element
) property
)))
391 (defsubst org-element-contents
(element)
392 "Extract contents from an ELEMENT."
393 (cond ((not (consp element
)) nil
)
394 ((symbolp (car element
)) (nthcdr 2 element
))
397 (defsubst org-element-restriction
(element)
398 "Return restriction associated to ELEMENT.
399 ELEMENT can be an element, an object or a symbol representing an
400 element or object type."
401 (cdr (assq (if (symbolp element
) element
(org-element-type element
))
402 org-element-object-restrictions
)))
404 (defsubst org-element-put-property
(element property value
)
405 "In ELEMENT set PROPERTY to VALUE.
406 Return modified element."
407 (if (stringp element
) (org-add-props element nil property value
)
408 (setcar (cdr element
) (plist-put (nth 1 element
) property value
))
411 (defsubst org-element-set-contents
(element &rest contents
)
412 "Set ELEMENT contents to CONTENTS.
413 Return modified element."
414 (cond ((not element
) (list contents
))
415 ((not (symbolp (car element
))) contents
)
416 ((cdr element
) (setcdr (cdr element
) contents
))
417 (t (nconc element contents
))))
419 (defsubst org-element-set-element
(old new
)
420 "Replace element or object OLD with element or object NEW.
421 The function takes care of setting `:parent' property for NEW."
422 ;; Since OLD is going to be changed into NEW by side-effect, first
423 ;; make sure that every element or object within NEW has OLD as
425 (mapc (lambda (blob) (org-element-put-property blob
:parent old
))
426 (org-element-contents new
))
427 ;; Transfer contents.
428 (apply 'org-element-set-contents old
(org-element-contents new
))
429 ;; Ensure NEW has same parent as OLD, then overwrite OLD properties
431 (org-element-put-property new
:parent
(org-element-property :parent old
))
432 (setcar (cdr old
) (nth 1 new
))
434 (setcar old
(car new
)))
436 (defsubst org-element-adopt-elements
(parent &rest children
)
437 "Append elements to the contents of another element.
439 PARENT is an element or object. CHILDREN can be elements,
440 objects, or a strings.
442 The function takes care of setting `:parent' property for CHILD.
443 Return parent element."
444 ;; Link every child to PARENT. If PARENT is nil, it is a secondary
445 ;; string: parent is the list itself.
446 (mapc (lambda (child)
447 (org-element-put-property child
:parent
(or parent children
)))
449 ;; Add CHILDREN at the end of PARENT contents.
451 (apply 'org-element-set-contents
453 (nconc (org-element-contents parent
) children
)))
454 ;; Return modified PARENT element.
455 (or parent children
))
461 ;; For each greater element type, we define a parser and an
464 ;; A parser returns the element or object as the list described above.
465 ;; Most of them accepts no argument. Though, exceptions exist. Hence
466 ;; every element containing a secondary string (see
467 ;; `org-element-secondary-value-alist') will accept an optional
468 ;; argument to toggle parsing of that secondary string. Moreover,
469 ;; `item' parser requires current list's structure as its first
472 ;; An interpreter accepts two arguments: the list representation of
473 ;; the element or object, and its contents. The latter may be nil,
474 ;; depending on the element or object considered. It returns the
475 ;; appropriate Org syntax, as a string.
477 ;; Parsing functions must follow the naming convention:
478 ;; org-element-TYPE-parser, where TYPE is greater element's type, as
479 ;; defined in `org-element-greater-elements'.
481 ;; Similarly, interpreting functions must follow the naming
482 ;; convention: org-element-TYPE-interpreter.
484 ;; With the exception of `headline' and `item' types, greater elements
485 ;; cannot contain other greater elements of their own type.
487 ;; Beside implementing a parser and an interpreter, adding a new
488 ;; greater element requires to tweak `org-element--current-element'.
489 ;; Moreover, the newly defined type must be added to both
490 ;; `org-element-all-elements' and `org-element-greater-elements'.
495 (defun org-element-center-block-parser (limit affiliated
)
496 "Parse a center block.
498 LIMIT bounds the search. AFFILIATED is a list of which CAR is
499 the buffer position at the beginning of the first affiliated
500 keyword and CDR is a plist of affiliated keywords along with
503 Return a list whose CAR is `center-block' and CDR is a plist
504 containing `:begin', `:end', `:hiddenp', `:contents-begin',
505 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
507 Assume point is at the beginning of the block."
508 (let ((case-fold-search t
))
509 (if (not (save-excursion
510 (re-search-forward "^[ \t]*#\\+END_CENTER[ \t]*$" limit t
)))
511 ;; Incomplete block: parse it as a paragraph.
512 (org-element-paragraph-parser limit affiliated
)
513 (let ((block-end-line (match-beginning 0)))
514 (let* ((begin (car affiliated
))
515 (post-affiliated (point))
516 ;; Empty blocks have no contents.
517 (contents-begin (progn (forward-line)
518 (and (< (point) block-end-line
)
520 (contents-end (and contents-begin block-end-line
))
521 (hidden (org-invisible-p2))
522 (pos-before-blank (progn (goto-char block-end-line
)
525 (end (save-excursion (skip-chars-forward " \r\t\n" limit
)
526 (skip-chars-backward " \t")
527 (if (bolp) (point) (line-end-position)))))
533 :contents-begin contents-begin
534 :contents-end contents-end
535 :post-blank
(count-lines pos-before-blank end
)
536 :post-affiliated post-affiliated
)
537 (cdr affiliated
))))))))
539 (defun org-element-center-block-interpreter (center-block contents
)
540 "Interpret CENTER-BLOCK element as Org syntax.
541 CONTENTS is the contents of the element."
542 (format "#+BEGIN_CENTER\n%s#+END_CENTER" contents
))
547 (defun org-element-drawer-parser (limit affiliated
)
550 LIMIT bounds the search. AFFILIATED is a list of which CAR is
551 the buffer position at the beginning of the first affiliated
552 keyword and CDR is a plist of affiliated keywords along with
555 Return a list whose CAR is `drawer' and CDR is a plist containing
556 `:drawer-name', `:begin', `:end', `:hiddenp', `:contents-begin',
557 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
559 Assume point is at beginning of drawer."
560 (let ((case-fold-search t
))
561 (if (not (save-excursion (re-search-forward "^[ \t]*:END:[ \t]*$" limit t
)))
562 ;; Incomplete drawer: parse it as a paragraph.
563 (org-element-paragraph-parser limit affiliated
)
565 (let* ((drawer-end-line (match-beginning 0))
566 (name (progn (looking-at org-drawer-regexp
)
567 (org-match-string-no-properties 1)))
568 (begin (car affiliated
))
569 (post-affiliated (point))
570 ;; Empty drawers have no contents.
571 (contents-begin (progn (forward-line)
572 (and (< (point) drawer-end-line
)
574 (contents-end (and contents-begin drawer-end-line
))
575 (hidden (org-invisible-p2))
576 (pos-before-blank (progn (goto-char drawer-end-line
)
579 (end (progn (skip-chars-forward " \r\t\n" limit
)
580 (skip-chars-backward " \t")
581 (if (bolp) (point) (line-end-position)))))
588 :contents-begin contents-begin
589 :contents-end contents-end
590 :post-blank
(count-lines pos-before-blank end
)
591 :post-affiliated post-affiliated
)
592 (cdr affiliated
))))))))
594 (defun org-element-drawer-interpreter (drawer contents
)
595 "Interpret DRAWER element as Org syntax.
596 CONTENTS is the contents of the element."
597 (format ":%s:\n%s:END:"
598 (org-element-property :drawer-name drawer
)
604 (defun org-element-dynamic-block-parser (limit affiliated
)
605 "Parse a dynamic block.
607 LIMIT bounds the search. AFFILIATED is a list of which CAR is
608 the buffer position at the beginning of the first affiliated
609 keyword and CDR is a plist of affiliated keywords along with
612 Return a list whose CAR is `dynamic-block' and CDR is a plist
613 containing `:block-name', `:begin', `:end', `:hiddenp',
614 `:contents-begin', `:contents-end', `:arguments', `:post-blank'
615 and `:post-affiliated' keywords.
617 Assume point is at beginning of dynamic block."
618 (let ((case-fold-search t
))
619 (if (not (save-excursion
620 (re-search-forward "^[ \t]*#\\+END:?[ \t]*$" limit t
)))
621 ;; Incomplete block: parse it as a paragraph.
622 (org-element-paragraph-parser limit affiliated
)
623 (let ((block-end-line (match-beginning 0)))
625 (let* ((name (progn (looking-at org-dblock-start-re
)
626 (org-match-string-no-properties 1)))
627 (arguments (org-match-string-no-properties 3))
628 (begin (car affiliated
))
629 (post-affiliated (point))
630 ;; Empty blocks have no contents.
631 (contents-begin (progn (forward-line)
632 (and (< (point) block-end-line
)
634 (contents-end (and contents-begin block-end-line
))
635 (hidden (org-invisible-p2))
636 (pos-before-blank (progn (goto-char block-end-line
)
639 (end (progn (skip-chars-forward " \r\t\n" limit
)
640 (skip-chars-backward " \t")
641 (if (bolp) (point) (line-end-position)))))
649 :contents-begin contents-begin
650 :contents-end contents-end
651 :post-blank
(count-lines pos-before-blank end
)
652 :post-affiliated post-affiliated
)
653 (cdr affiliated
)))))))))
655 (defun org-element-dynamic-block-interpreter (dynamic-block contents
)
656 "Interpret DYNAMIC-BLOCK element as Org syntax.
657 CONTENTS is the contents of the element."
658 (format "#+BEGIN: %s%s\n%s#+END:"
659 (org-element-property :block-name dynamic-block
)
660 (let ((args (org-element-property :arguments dynamic-block
)))
661 (and args
(concat " " args
)))
665 ;;;; Footnote Definition
667 (defun org-element-footnote-definition-parser (limit affiliated
)
668 "Parse a footnote definition.
670 LIMIT bounds the search. AFFILIATED is a list of which CAR is
671 the buffer position at the beginning of the first affiliated
672 keyword and CDR is a plist of affiliated keywords along with
675 Return a list whose CAR is `footnote-definition' and CDR is
676 a plist containing `:label', `:begin' `:end', `:contents-begin',
677 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
679 Assume point is at the beginning of the footnote definition."
681 (let* ((label (progn (looking-at org-footnote-definition-re
)
682 (org-match-string-no-properties 1)))
683 (begin (car affiliated
))
684 (post-affiliated (point))
685 (ending (save-excursion
689 (concat org-outline-regexp-bol
"\\|"
690 org-footnote-definition-re
"\\|"
691 "^[ \t]*$") limit
'move
))
694 (contents-begin (progn (search-forward "]")
695 (skip-chars-forward " \r\t\n" ending
)
696 (and (/= (point) ending
) (point))))
697 (contents-end (and contents-begin ending
))
698 (end (progn (goto-char ending
)
699 (skip-chars-forward " \r\t\n" limit
)
700 (skip-chars-backward " \t")
701 (if (bolp) (point) (line-end-position)))))
702 (list 'footnote-definition
707 :contents-begin contents-begin
708 :contents-end contents-end
709 :post-blank
(count-lines ending end
)
710 :post-affiliated post-affiliated
)
711 (cdr affiliated
))))))
713 (defun org-element-footnote-definition-interpreter (footnote-definition contents
)
714 "Interpret FOOTNOTE-DEFINITION element as Org syntax.
715 CONTENTS is the contents of the footnote-definition."
716 (concat (format "[%s]" (org-element-property :label footnote-definition
))
723 (defun org-element-headline-parser (limit &optional raw-secondary-p
)
726 Return a list whose CAR is `headline' and CDR is a plist
727 containing `:raw-value', `:title', `:begin', `:end',
728 `:pre-blank', `:hiddenp', `:contents-begin' and `:contents-end',
729 `:level', `:priority', `:tags', `:todo-keyword',`:todo-type',
730 `:scheduled', `:deadline', `:closed', `:quotedp', `:archivedp',
731 `:commentedp' and `:footnote-section-p' keywords.
733 The plist also contains any property set in the property drawer,
734 with its name in lowercase, the underscores replaced with hyphens
735 and colons at the beginning (i.e. `:custom-id').
737 When RAW-SECONDARY-P is non-nil, headline's title will not be
738 parsed as a secondary string, but as a plain string instead.
740 Assume point is at beginning of the headline."
742 (let* ((components (org-heading-components))
743 (level (nth 1 components
))
744 (todo (nth 2 components
))
746 (and todo
(if (member todo org-done-keywords
) 'done
'todo
)))
747 (tags (let ((raw-tags (nth 5 components
)))
748 (and raw-tags
(org-split-string raw-tags
":"))))
749 (raw-value (or (nth 4 components
) ""))
751 (let ((case-fold-search nil
))
752 (string-match (format "^%s\\( \\|$\\)" org-quote-string
)
755 (let ((case-fold-search nil
))
756 (string-match (format "^%s\\( \\|$\\)" org-comment-string
)
758 (archivedp (member org-archive-tag tags
))
759 (footnote-section-p (and org-footnote-section
760 (string= org-footnote-section raw-value
)))
761 ;; Normalize property names: ":SOME_PROP:" becomes
770 (replace-regexp-in-string
771 "_" "-" (downcase (car p
)))))
773 (org-entry-properties nil
'standard
))
776 ;; Read time properties on the line below the headline.
778 (when (progn (forward-line)
779 (looking-at org-planning-or-clock-line-re
))
780 (let ((end (line-end-position)) plist
)
781 (while (re-search-forward
782 org-keyword-time-not-clock-regexp end t
)
783 (goto-char (match-end 1))
784 (skip-chars-forward " \t")
785 (let ((keyword (match-string 1))
786 (time (org-element-timestamp-parser)))
787 (cond ((equal keyword org-scheduled-string
)
788 (setq plist
(plist-put plist
:scheduled time
)))
789 ((equal keyword org-deadline-string
)
790 (setq plist
(plist-put plist
:deadline time
)))
791 (t (setq plist
(plist-put plist
:closed time
))))))
794 (end (save-excursion (goto-char (org-end-of-subtree t t
))))
795 (pos-after-head (progn (forward-line) (point)))
796 (contents-begin (save-excursion
797 (skip-chars-forward " \r\t\n" end
)
798 (and (/= (point) end
) (line-beginning-position))))
799 (hidden (org-invisible-p2))
800 (contents-end (and contents-begin
801 (progn (goto-char end
)
802 (skip-chars-backward " \r\t\n")
805 ;; Clean RAW-VALUE from any quote or comment string.
806 (when (or quotedp commentedp
)
807 (let ((case-fold-search nil
))
809 (replace-regexp-in-string
811 (regexp-opt (list org-quote-string org-comment-string
))
815 ;; Clean TAGS from archive tag, if any.
816 (when archivedp
(setq tags
(delete org-archive-tag tags
)))
820 (list :raw-value raw-value
824 (if (not contents-begin
) 0
825 (count-lines pos-after-head contents-begin
))
827 :contents-begin contents-begin
828 :contents-end contents-end
830 :priority
(nth 3 components
)
834 :post-blank
(count-lines
835 (if (not contents-end
) pos-after-head
836 (goto-char contents-end
)
840 :footnote-section-p footnote-section-p
842 :commentedp commentedp
846 (org-element-put-property
848 (if raw-secondary-p raw-value
849 (org-element-parse-secondary-string
850 raw-value
(org-element-restriction 'headline
) headline
)))))))
852 (defun org-element-headline-interpreter (headline contents
)
853 "Interpret HEADLINE element as Org syntax.
854 CONTENTS is the contents of the element."
855 (let* ((level (org-element-property :level headline
))
856 (todo (org-element-property :todo-keyword headline
))
857 (priority (org-element-property :priority headline
))
858 (title (org-element-interpret-data
859 (org-element-property :title headline
)))
860 (tags (let ((tag-list (if (org-element-property :archivedp headline
)
861 (cons org-archive-tag
862 (org-element-property :tags headline
))
863 (org-element-property :tags headline
))))
865 (format ":%s:" (mapconcat 'identity tag-list
":")))))
866 (commentedp (org-element-property :commentedp headline
))
867 (quotedp (org-element-property :quotedp headline
))
868 (pre-blank (or (org-element-property :pre-blank headline
) 0))
869 (heading (concat (make-string level ?
*)
870 (and todo
(concat " " todo
))
871 (and quotedp
(concat " " org-quote-string
))
872 (and commentedp
(concat " " org-comment-string
))
874 (format " [#%s]" (char-to-string priority
)))
875 (cond ((and org-footnote-section
876 (org-element-property
877 :footnote-section-p headline
))
878 (concat " " org-footnote-section
))
879 (title (concat " " title
))))))
884 ((zerop org-tags-column
) (format " %s" tags
))
885 ((< org-tags-column
0)
888 (max (- (+ org-tags-column
(length heading
) (length tags
))) 1)
893 (make-string (max (- org-tags-column
(length heading
)) 1) ?
)
895 (make-string (1+ pre-blank
) 10)
901 (defun org-element-inlinetask-parser (limit &optional raw-secondary-p
)
902 "Parse an inline task.
904 Return a list whose CAR is `inlinetask' and CDR is a plist
905 containing `:title', `:begin', `:end', `:hiddenp',
906 `:contents-begin' and `:contents-end', `:level', `:priority',
907 `:raw-value', `:tags', `:todo-keyword', `:todo-type',
908 `:scheduled', `:deadline', `:closed' and `:post-blank' keywords.
910 The plist also contains any property set in the property drawer,
911 with its name in lowercase, the underscores replaced with hyphens
912 and colons at the beginning (i.e. `:custom-id').
914 When optional argument RAW-SECONDARY-P is non-nil, inline-task's
915 title will not be parsed as a secondary string, but as a plain
918 Assume point is at beginning of the inline task."
920 (let* ((begin (point))
921 (components (org-heading-components))
922 (todo (nth 2 components
))
924 (if (member todo org-done-keywords
) 'done
'todo
)))
925 (tags (let ((raw-tags (nth 5 components
)))
926 (and raw-tags
(org-split-string raw-tags
":"))))
927 (raw-value (or (nth 4 components
) ""))
928 ;; Normalize property names: ":SOME_PROP:" becomes
937 (replace-regexp-in-string
938 "_" "-" (downcase (car p
)))))
940 (org-entry-properties nil
'standard
))
943 ;; Read time properties on the line below the inlinetask
946 (when (progn (forward-line)
947 (looking-at org-planning-or-clock-line-re
))
948 (let ((end (line-end-position)) plist
)
949 (while (re-search-forward
950 org-keyword-time-not-clock-regexp end t
)
951 (goto-char (match-end 1))
952 (skip-chars-forward " \t")
953 (let ((keyword (match-string 1))
954 (time (org-element-timestamp-parser)))
955 (cond ((equal keyword org-scheduled-string
)
956 (setq plist
(plist-put plist
:scheduled time
)))
957 ((equal keyword org-deadline-string
)
958 (setq plist
(plist-put plist
:deadline time
)))
959 (t (setq plist
(plist-put plist
:closed time
))))))
961 (task-end (save-excursion
963 (and (re-search-forward "^\\*+ END" limit t
)
964 (match-beginning 0))))
965 (contents-begin (progn (forward-line)
966 (and task-end
(< (point) task-end
) (point))))
967 (hidden (and contents-begin
(org-invisible-p2)))
968 (contents-end (and contents-begin task-end
))
969 (before-blank (if (not task-end
) (point)
973 (end (progn (skip-chars-forward " \r\t\n" limit
)
974 (skip-chars-backward " \t")
975 (if (bolp) (point) (line-end-position))))
979 (list :raw-value raw-value
983 :contents-begin contents-begin
984 :contents-end contents-end
985 :level
(nth 1 components
)
986 :priority
(nth 3 components
)
990 :post-blank
(count-lines before-blank end
))
993 (org-element-put-property
995 (if raw-secondary-p raw-value
996 (org-element-parse-secondary-string
998 (org-element-restriction 'inlinetask
)
1001 (defun org-element-inlinetask-interpreter (inlinetask contents
)
1002 "Interpret INLINETASK element as Org syntax.
1003 CONTENTS is the contents of inlinetask."
1004 (let* ((level (org-element-property :level inlinetask
))
1005 (todo (org-element-property :todo-keyword inlinetask
))
1006 (priority (org-element-property :priority inlinetask
))
1007 (title (org-element-interpret-data
1008 (org-element-property :title inlinetask
)))
1009 (tags (let ((tag-list (org-element-property :tags inlinetask
)))
1011 (format ":%s:" (mapconcat 'identity tag-list
":")))))
1012 (task (concat (make-string level ?
*)
1013 (and todo
(concat " " todo
))
1015 (format " [#%s]" (char-to-string priority
)))
1016 (and title
(concat " " title
)))))
1021 ((zerop org-tags-column
) (format " %s" tags
))
1022 ((< org-tags-column
0)
1025 (max (- (+ org-tags-column
(length task
) (length tags
))) 1)
1030 (make-string (max (- org-tags-column
(length task
)) 1) ?
)
1032 ;; Prefer degenerate inlinetasks when there are no
1037 (make-string level ?
*) " END")))))
1042 (defun org-element-item-parser (limit struct
&optional raw-secondary-p
)
1045 STRUCT is the structure of the plain list.
1047 Return a list whose CAR is `item' and CDR is a plist containing
1048 `:bullet', `:begin', `:end', `:contents-begin', `:contents-end',
1049 `:checkbox', `:counter', `:tag', `:structure', `:hiddenp' and
1050 `:post-blank' keywords.
1052 When optional argument RAW-SECONDARY-P is non-nil, item's tag, if
1053 any, will not be parsed as a secondary string, but as a plain
1056 Assume point is at the beginning of the item."
1059 (looking-at org-list-full-item-re
)
1060 (let* ((begin (point))
1061 (bullet (org-match-string-no-properties 1))
1062 (checkbox (let ((box (org-match-string-no-properties 3)))
1063 (cond ((equal "[ ]" box
) 'off
)
1064 ((equal "[X]" box
) 'on
)
1065 ((equal "[-]" box
) 'trans
))))
1066 (counter (let ((c (org-match-string-no-properties 2)))
1070 ((string-match "[A-Za-z]" c
)
1071 (- (string-to-char (upcase (match-string 0 c
)))
1073 ((string-match "[0-9]+" c
)
1074 (string-to-number (match-string 0 c
)))))))
1075 (end (save-excursion (goto-char (org-list-get-item-end begin struct
))
1076 (unless (bolp) (forward-line))
1080 ;; Ignore tags in un-ordered lists: they are just
1081 ;; a part of item's body.
1082 (if (and (match-beginning 4)
1083 (save-match-data (string-match "[.)]" bullet
)))
1086 (skip-chars-forward " \r\t\n" limit
)
1087 ;; If first line isn't empty, contents really start
1088 ;; at the text after item's meta-data.
1089 (if (= (point-at-bol) begin
) (point) (point-at-bol))))
1090 (hidden (progn (forward-line)
1091 (and (not (= (point) end
)) (org-invisible-p2))))
1092 (contents-end (progn (goto-char end
)
1093 (skip-chars-backward " \r\t\n")
1098 (list :bullet bullet
1101 ;; CONTENTS-BEGIN and CONTENTS-END may be
1102 ;; mixed up in the case of an empty item
1103 ;; separated from the next by a blank line.
1104 ;; Thus ensure the former is always the
1106 :contents-begin
(min contents-begin contents-end
)
1107 :contents-end
(max contents-begin contents-end
)
1112 :post-blank
(count-lines contents-end end
)))))
1113 (org-element-put-property
1115 (let ((raw-tag (org-list-get-tag begin struct
)))
1117 (if raw-secondary-p raw-tag
1118 (org-element-parse-secondary-string
1119 raw-tag
(org-element-restriction 'item
) item
))))))))
1121 (defun org-element-item-interpreter (item contents
)
1122 "Interpret ITEM element as Org syntax.
1123 CONTENTS is the contents of the element."
1124 (let* ((bullet (org-list-bullet-string (org-element-property :bullet item
)))
1125 (checkbox (org-element-property :checkbox item
))
1126 (counter (org-element-property :counter item
))
1127 (tag (let ((tag (org-element-property :tag item
)))
1128 (and tag
(org-element-interpret-data tag
))))
1129 ;; Compute indentation.
1130 (ind (make-string (length bullet
) 32))
1131 (item-starts-with-par-p
1132 (eq (org-element-type (car (org-element-contents item
)))
1137 (and counter
(format "[@%d] " counter
))
1142 (and tag
(format "%s :: " tag
))
1143 (let ((contents (replace-regexp-in-string
1144 "\\(^\\)[ \t]*\\S-" ind contents nil nil
1)))
1145 (if item-starts-with-par-p
(org-trim contents
)
1146 (concat "\n" contents
))))))
1151 (defun org-element-plain-list-parser (limit affiliated structure
)
1152 "Parse a plain list.
1154 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1155 the buffer position at the beginning of the first affiliated
1156 keyword and CDR is a plist of affiliated keywords along with
1157 their value. STRUCTURE is the structure of the plain list being
1160 Return a list whose CAR is `plain-list' and CDR is a plist
1161 containing `:type', `:begin', `:end', `:contents-begin' and
1162 `:contents-end', `:structure', `:post-blank' and
1163 `:post-affiliated' keywords.
1165 Assume point is at the beginning of the list."
1167 (let* ((struct (or structure
(org-list-struct)))
1168 (prevs (org-list-prevs-alist struct
))
1169 (parents (org-list-parents-alist struct
))
1170 (type (org-list-get-list-type (point) struct prevs
))
1171 (contents-begin (point))
1172 (begin (car affiliated
))
1174 (progn (goto-char (org-list-get-list-end (point) struct prevs
))
1175 (unless (bolp) (forward-line))
1177 (end (progn (skip-chars-forward " \r\t\n" limit
)
1178 (skip-chars-backward " \t")
1179 (if (bolp) (point) (line-end-position)))))
1186 :contents-begin contents-begin
1187 :contents-end contents-end
1189 :post-blank
(count-lines contents-end end
)
1190 :post-affiliated contents-begin
)
1191 (cdr affiliated
))))))
1193 (defun org-element-plain-list-interpreter (plain-list contents
)
1194 "Interpret PLAIN-LIST element as Org syntax.
1195 CONTENTS is the contents of the element."
1198 (goto-char (point-min))
1203 ;;;; Property Drawer
1205 (defun org-element-property-drawer-parser (limit affiliated
)
1206 "Parse a property drawer.
1208 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1209 the buffer position at the beginning of the first affiliated
1210 keyword and CDR is a plist of affiliated keywords along with
1213 Return a list whose CAR is `property-drawer' and CDR is a plist
1214 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1215 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
1217 Assume point is at the beginning of the property drawer."
1219 (let ((case-fold-search t
))
1220 (if (not (save-excursion
1221 (re-search-forward "^[ \t]*:END:[ \t]*$" limit t
)))
1222 ;; Incomplete drawer: parse it as a paragraph.
1223 (org-element-paragraph-parser limit affiliated
)
1225 (let* ((drawer-end-line (match-beginning 0))
1226 (begin (car affiliated
))
1227 (post-affiliated (point))
1228 (contents-begin (progn (forward-line)
1229 (and (< (point) drawer-end-line
)
1231 (contents-end (and contents-begin drawer-end-line
))
1232 (hidden (org-invisible-p2))
1233 (pos-before-blank (progn (goto-char drawer-end-line
)
1236 (end (progn (skip-chars-forward " \r\t\n" limit
)
1237 (skip-chars-backward " \t")
1238 (if (bolp) (point) (line-end-position)))))
1239 (list 'property-drawer
1244 :contents-begin contents-begin
1245 :contents-end contents-end
1246 :post-blank
(count-lines pos-before-blank end
)
1247 :post-affiliated post-affiliated
)
1248 (cdr affiliated
)))))))))
1250 (defun org-element-property-drawer-interpreter (property-drawer contents
)
1251 "Interpret PROPERTY-DRAWER element as Org syntax.
1252 CONTENTS is the properties within the drawer."
1253 (format ":PROPERTIES:\n%s:END:" contents
))
1258 (defun org-element-quote-block-parser (limit affiliated
)
1259 "Parse a quote block.
1261 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1262 the buffer position at the beginning of the first affiliated
1263 keyword and CDR is a plist of affiliated keywords along with
1266 Return a list whose CAR is `quote-block' and CDR is a plist
1267 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1268 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
1270 Assume point is at the beginning of the block."
1271 (let ((case-fold-search t
))
1272 (if (not (save-excursion
1273 (re-search-forward "^[ \t]*#\\+END_QUOTE[ \t]*$" limit t
)))
1274 ;; Incomplete block: parse it as a paragraph.
1275 (org-element-paragraph-parser limit affiliated
)
1276 (let ((block-end-line (match-beginning 0)))
1278 (let* ((begin (car affiliated
))
1279 (post-affiliated (point))
1280 ;; Empty blocks have no contents.
1281 (contents-begin (progn (forward-line)
1282 (and (< (point) block-end-line
)
1284 (contents-end (and contents-begin block-end-line
))
1285 (hidden (org-invisible-p2))
1286 (pos-before-blank (progn (goto-char block-end-line
)
1289 (end (progn (skip-chars-forward " \r\t\n" limit
)
1290 (skip-chars-backward " \t")
1291 (if (bolp) (point) (line-end-position)))))
1297 :contents-begin contents-begin
1298 :contents-end contents-end
1299 :post-blank
(count-lines pos-before-blank end
)
1300 :post-affiliated post-affiliated
)
1301 (cdr affiliated
)))))))))
1303 (defun org-element-quote-block-interpreter (quote-block contents
)
1304 "Interpret QUOTE-BLOCK element as Org syntax.
1305 CONTENTS is the contents of the element."
1306 (format "#+BEGIN_QUOTE\n%s#+END_QUOTE" contents
))
1311 (defun org-element-section-parser (limit)
1314 LIMIT bounds the search.
1316 Return a list whose CAR is `section' and CDR is a plist
1317 containing `:begin', `:end', `:contents-begin', `contents-end'
1318 and `:post-blank' keywords."
1320 ;; Beginning of section is the beginning of the first non-blank
1321 ;; line after previous headline.
1322 (let ((begin (point))
1323 (end (progn (org-with-limited-levels (outline-next-heading))
1325 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
1331 :contents-begin begin
1332 :contents-end pos-before-blank
1333 :post-blank
(count-lines pos-before-blank end
))))))
1335 (defun org-element-section-interpreter (section contents
)
1336 "Interpret SECTION element as Org syntax.
1337 CONTENTS is the contents of the element."
1343 (defun org-element-special-block-parser (limit affiliated
)
1344 "Parse a special block.
1346 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1347 the buffer position at the beginning of the first affiliated
1348 keyword and CDR is a plist of affiliated keywords along with
1351 Return a list whose CAR is `special-block' and CDR is a plist
1352 containing `:type', `:begin', `:end', `:hiddenp',
1353 `:contents-begin', `:contents-end', `:post-blank' and
1354 `:post-affiliated' keywords.
1356 Assume point is at the beginning of the block."
1357 (let* ((case-fold-search t
)
1358 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(S-+\\)")
1359 (upcase (match-string-no-properties 1)))))
1360 (if (not (save-excursion
1362 (format "^[ \t]*#\\+END_%s[ \t]*$" type
) limit t
)))
1363 ;; Incomplete block: parse it as a paragraph.
1364 (org-element-paragraph-parser limit affiliated
)
1365 (let ((block-end-line (match-beginning 0)))
1367 (let* ((begin (car affiliated
))
1368 (post-affiliated (point))
1369 ;; Empty blocks have no contents.
1370 (contents-begin (progn (forward-line)
1371 (and (< (point) block-end-line
)
1373 (contents-end (and contents-begin block-end-line
))
1374 (hidden (org-invisible-p2))
1375 (pos-before-blank (progn (goto-char block-end-line
)
1378 (end (progn (skip-chars-forward " \r\t\n" limit
)
1379 (skip-chars-backward " \t")
1380 (if (bolp) (point) (line-end-position)))))
1381 (list 'special-block
1387 :contents-begin contents-begin
1388 :contents-end contents-end
1389 :post-blank
(count-lines pos-before-blank end
)
1390 :post-affiliated post-affiliated
)
1391 (cdr affiliated
)))))))))
1393 (defun org-element-special-block-interpreter (special-block contents
)
1394 "Interpret SPECIAL-BLOCK element as Org syntax.
1395 CONTENTS is the contents of the element."
1396 (let ((block-type (org-element-property :type special-block
)))
1397 (format "#+BEGIN_%s\n%s#+END_%s" block-type contents block-type
)))
1403 ;; For each element, a parser and an interpreter are also defined.
1404 ;; Both follow the same naming convention used for greater elements.
1406 ;; Also, as for greater elements, adding a new element type is done
1407 ;; through the following steps: implement a parser and an interpreter,
1408 ;; tweak `org-element--current-element' so that it recognizes the new
1409 ;; type and add that new type to `org-element-all-elements'.
1411 ;; As a special case, when the newly defined type is a block type,
1412 ;; `org-element-block-name-alist' has to be modified accordingly.
1417 (defun org-element-babel-call-parser (limit affiliated
)
1418 "Parse a babel call.
1420 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1421 the buffer position at the beginning of the first affiliated
1422 keyword and CDR is a plist of affiliated keywords along with
1425 Return a list whose CAR is `babel-call' and CDR is a plist
1426 containing `:begin', `:end', `:info', `:post-blank' and
1427 `:post-affiliated' as keywords."
1429 (let ((case-fold-search t
)
1430 (info (progn (looking-at org-babel-block-lob-one-liner-regexp
)
1431 (org-babel-lob-get-info)))
1432 (begin (car affiliated
))
1433 (post-affiliated (point))
1434 (pos-before-blank (progn (forward-line) (point)))
1435 (end (progn (skip-chars-forward " \r\t\n" limit
)
1436 (skip-chars-backward " \t")
1437 (if (bolp) (point) (line-end-position)))))
1443 :post-blank
(count-lines pos-before-blank end
)
1444 :post-affiliated post-affiliated
)
1445 (cdr affiliated
))))))
1447 (defun org-element-babel-call-interpreter (babel-call contents
)
1448 "Interpret BABEL-CALL element as Org syntax.
1450 (let* ((babel-info (org-element-property :info babel-call
))
1451 (main (car babel-info
))
1452 (post-options (nth 1 babel-info
)))
1454 (if (not (string-match "\\[\\(\\[.*?\\]\\)\\]" main
)) main
1455 ;; Remove redundant square brackets.
1456 (replace-match (match-string 1 main
) nil nil main
))
1457 (and post-options
(format "[%s]" post-options
)))))
1462 (defun org-element-clock-parser (limit)
1465 LIMIT bounds the search.
1467 Return a list whose CAR is `clock' and CDR is a plist containing
1468 `:status', `:value', `:time', `:begin', `:end' and `:post-blank'
1471 (let* ((case-fold-search nil
)
1473 (value (progn (search-forward org-clock-string
(line-end-position) t
)
1474 (skip-chars-forward " \t")
1475 (org-element-timestamp-parser)))
1476 (duration (and (search-forward " => " (line-end-position) t
)
1477 (progn (skip-chars-forward " \t")
1478 (looking-at "\\(\\S-+\\)[ \t]*$"))
1479 (org-match-string-no-properties 1)))
1480 (status (if duration
'closed
'running
))
1481 (post-blank (let ((before-blank (progn (forward-line) (point))))
1482 (skip-chars-forward " \r\t\n" limit
)
1483 (skip-chars-backward " \t")
1484 (unless (bolp) (end-of-line))
1485 (count-lines before-blank
(point))))
1488 (list :status status
1493 :post-blank post-blank
)))))
1495 (defun org-element-clock-interpreter (clock contents
)
1496 "Interpret CLOCK element as Org syntax.
1498 (concat org-clock-string
" "
1499 (org-element-timestamp-interpreter
1500 (org-element-property :value clock
) nil
)
1501 (let ((duration (org-element-property :duration clock
)))
1506 (org-split-string duration
":")))))))
1511 (defun org-element-comment-parser (limit affiliated
)
1514 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1515 the buffer position at the beginning of the first affiliated
1516 keyword and CDR is a plist of affiliated keywords along with
1519 Return a list whose CAR is `comment' and CDR is a plist
1520 containing `:begin', `:end', `:value', `:post-blank',
1521 `:post-affiliated' keywords.
1523 Assume point is at comment beginning."
1525 (let* ((begin (car affiliated
))
1526 (post-affiliated (point))
1527 (value (prog2 (looking-at "[ \t]*# ?")
1528 (buffer-substring-no-properties
1529 (match-end 0) (line-end-position))
1532 ;; Get comments ending.
1534 (while (and (< (point) limit
) (looking-at "[ \t]*#\\( \\|$\\)"))
1535 ;; Accumulate lines without leading hash and first
1540 (buffer-substring-no-properties
1541 (match-end 0) (line-end-position))))
1544 (end (progn (goto-char com-end
)
1545 (skip-chars-forward " \r\t\n" limit
)
1546 (skip-chars-backward " \t")
1547 (if (bolp) (point) (line-end-position)))))
1553 :post-blank
(count-lines com-end end
)
1554 :post-affiliated post-affiliated
)
1555 (cdr affiliated
))))))
1557 (defun org-element-comment-interpreter (comment contents
)
1558 "Interpret COMMENT element as Org syntax.
1560 (replace-regexp-in-string "^" "# " (org-element-property :value comment
)))
1565 (defun org-element-comment-block-parser (limit affiliated
)
1566 "Parse an export block.
1568 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1569 the buffer position at the beginning of the first affiliated
1570 keyword and CDR is a plist of affiliated keywords along with
1573 Return a list whose CAR is `comment-block' and CDR is a plist
1574 containing `:begin', `:end', `:hiddenp', `:value', `:post-blank'
1575 and `:post-affiliated' keywords.
1577 Assume point is at comment block beginning."
1578 (let ((case-fold-search t
))
1579 (if (not (save-excursion
1580 (re-search-forward "^[ \t]*#\\+END_COMMENT[ \t]*$" limit t
)))
1581 ;; Incomplete block: parse it as a paragraph.
1582 (org-element-paragraph-parser limit affiliated
)
1583 (let ((contents-end (match-beginning 0)))
1585 (let* ((begin (car affiliated
))
1586 (post-affiliated (point))
1587 (contents-begin (progn (forward-line) (point)))
1588 (hidden (org-invisible-p2))
1589 (pos-before-blank (progn (goto-char contents-end
)
1592 (end (progn (skip-chars-forward " \r\t\n" limit
)
1593 (skip-chars-backward " \t")
1594 (if (bolp) (point) (line-end-position))))
1595 (value (buffer-substring-no-properties
1596 contents-begin contents-end
)))
1597 (list 'comment-block
1603 :post-blank
(count-lines pos-before-blank end
)
1604 :post-affiliated post-affiliated
)
1605 (cdr affiliated
)))))))))
1607 (defun org-element-comment-block-interpreter (comment-block contents
)
1608 "Interpret COMMENT-BLOCK element as Org syntax.
1610 (format "#+BEGIN_COMMENT\n%s#+END_COMMENT"
1611 (org-remove-indentation (org-element-property :value comment-block
))))
1616 (defun org-element-diary-sexp-parser (limit affiliated
)
1617 "Parse a diary sexp.
1619 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1620 the buffer position at the beginning of the first affiliated
1621 keyword and CDR is a plist of affiliated keywords along with
1624 Return a list whose CAR is `diary-sexp' and CDR is a plist
1625 containing `:begin', `:end', `:value', `:post-blank' and
1626 `:post-affiliated' keywords."
1628 (let ((begin (car affiliated
))
1629 (post-affiliated (point))
1630 (value (progn (looking-at "\\(%%(.*\\)[ \t]*$")
1631 (org-match-string-no-properties 1)))
1632 (pos-before-blank (progn (forward-line) (point)))
1633 (end (progn (skip-chars-forward " \r\t\n" limit
)
1634 (skip-chars-backward " \t")
1635 (if (bolp) (point) (line-end-position)))))
1641 :post-blank
(count-lines pos-before-blank end
)
1642 :post-affiliated post-affiliated
)
1643 (cdr affiliated
))))))
1645 (defun org-element-diary-sexp-interpreter (diary-sexp contents
)
1646 "Interpret DIARY-SEXP as Org syntax.
1648 (org-element-property :value diary-sexp
))
1653 (defun org-element-example-block-parser (limit affiliated
)
1654 "Parse an example block.
1656 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1657 the buffer position at the beginning of the first affiliated
1658 keyword and CDR is a plist of affiliated keywords along with
1661 Return a list whose CAR is `example-block' and CDR is a plist
1662 containing `:begin', `:end', `:number-lines', `:preserve-indent',
1663 `:retain-labels', `:use-labels', `:label-fmt', `:hiddenp',
1664 `:switches', `:value', `:post-blank' and `:post-affiliated'
1666 (let ((case-fold-search t
))
1667 (if (not (save-excursion
1668 (re-search-forward "^[ \t]*#\\+END_EXAMPLE[ \t]*$" limit t
)))
1669 ;; Incomplete block: parse it as a paragraph.
1670 (org-element-paragraph-parser limit affiliated
)
1671 (let ((contents-end (match-beginning 0)))
1674 (progn (looking-at "^[ \t]*#\\+BEGIN_EXAMPLE\\(?: +\\(.*\\)\\)?")
1675 (org-match-string-no-properties 1)))
1676 ;; Switches analysis
1677 (number-lines (cond ((not switches
) nil
)
1678 ((string-match "-n\\>" switches
) 'new
)
1679 ((string-match "+n\\>" switches
) 'continued
)))
1680 (preserve-indent (and switches
(string-match "-i\\>" switches
)))
1681 ;; Should labels be retained in (or stripped from) example
1685 (not (string-match "-r\\>" switches
))
1686 (and number-lines
(string-match "-k\\>" switches
))))
1687 ;; What should code-references use - labels or
1691 (and retain-labels
(not (string-match "-k\\>" switches
)))))
1692 (label-fmt (and switches
1693 (string-match "-l +\"\\([^\"\n]+\\)\"" switches
)
1694 (match-string 1 switches
)))
1695 ;; Standard block parsing.
1696 (begin (car affiliated
))
1697 (post-affiliated (point))
1698 (contents-begin (progn (forward-line) (point)))
1699 (hidden (org-invisible-p2))
1700 (value (org-unescape-code-in-string
1701 (buffer-substring-no-properties
1702 contents-begin contents-end
)))
1703 (pos-before-blank (progn (goto-char contents-end
)
1706 (end (progn (skip-chars-forward " \r\t\n" limit
)
1707 (skip-chars-backward " \t")
1708 (if (bolp) (point) (line-end-position)))))
1709 (list 'example-block
1715 :number-lines number-lines
1716 :preserve-indent preserve-indent
1717 :retain-labels retain-labels
1718 :use-labels use-labels
1719 :label-fmt label-fmt
1721 :post-blank
(count-lines pos-before-blank end
)
1722 :post-affiliated post-affiliated
)
1723 (cdr affiliated
)))))))))
1725 (defun org-element-example-block-interpreter (example-block contents
)
1726 "Interpret EXAMPLE-BLOCK element as Org syntax.
1728 (let ((switches (org-element-property :switches example-block
)))
1729 (concat "#+BEGIN_EXAMPLE" (and switches
(concat " " switches
)) "\n"
1730 (org-remove-indentation
1731 (org-escape-code-in-string
1732 (org-element-property :value example-block
)))
1738 (defun org-element-export-block-parser (limit affiliated
)
1739 "Parse an export block.
1741 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1742 the buffer position at the beginning of the first affiliated
1743 keyword and CDR is a plist of affiliated keywords along with
1746 Return a list whose CAR is `export-block' and CDR is a plist
1747 containing `:begin', `:end', `:type', `:hiddenp', `:value',
1748 `:post-blank' and `:post-affiliated' keywords.
1750 Assume point is at export-block beginning."
1751 (let* ((case-fold-search t
)
1752 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
1753 (upcase (org-match-string-no-properties 1)))))
1754 (if (not (save-excursion
1756 (format "^[ \t]*#\\+END_%s[ \t]*$" type
) limit t
)))
1757 ;; Incomplete block: parse it as a paragraph.
1758 (org-element-paragraph-parser limit affiliated
)
1759 (let ((contents-end (match-beginning 0)))
1761 (let* ((begin (car affiliated
))
1762 (post-affiliated (point))
1763 (contents-begin (progn (forward-line) (point)))
1764 (hidden (org-invisible-p2))
1765 (pos-before-blank (progn (goto-char contents-end
)
1768 (end (progn (skip-chars-forward " \r\t\n" limit
)
1769 (skip-chars-backward " \t")
1770 (if (bolp) (point) (line-end-position))))
1771 (value (buffer-substring-no-properties contents-begin
1780 :post-blank
(count-lines pos-before-blank end
)
1781 :post-affiliated post-affiliated
)
1782 (cdr affiliated
)))))))))
1784 (defun org-element-export-block-interpreter (export-block contents
)
1785 "Interpret EXPORT-BLOCK element as Org syntax.
1787 (let ((type (org-element-property :type export-block
)))
1788 (concat (format "#+BEGIN_%s\n" type
)
1789 (org-element-property :value export-block
)
1790 (format "#+END_%s" type
))))
1795 (defun org-element-fixed-width-parser (limit affiliated
)
1796 "Parse a fixed-width section.
1798 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1799 the buffer position at the beginning of the first affiliated
1800 keyword and CDR is a plist of affiliated keywords along with
1803 Return a list whose CAR is `fixed-width' and CDR is a plist
1804 containing `:begin', `:end', `:value', `:post-blank' and
1805 `:post-affiliated' keywords.
1807 Assume point is at the beginning of the fixed-width area."
1809 (let* ((begin (car affiliated
))
1810 (post-affiliated (point))
1814 (while (and (< (point) limit
)
1815 (looking-at "[ \t]*:\\( \\|$\\)"))
1816 ;; Accumulate text without starting colons.
1819 (buffer-substring-no-properties
1820 (match-end 0) (point-at-eol))
1824 (end (progn (skip-chars-forward " \r\t\n" limit
)
1825 (skip-chars-backward " \t")
1826 (if (bolp) (point) (line-end-position)))))
1832 :post-blank
(count-lines end-area end
)
1833 :post-affiliated post-affiliated
)
1834 (cdr affiliated
))))))
1836 (defun org-element-fixed-width-interpreter (fixed-width contents
)
1837 "Interpret FIXED-WIDTH element as Org syntax.
1839 (replace-regexp-in-string
1840 "^" ": " (substring (org-element-property :value fixed-width
) 0 -
1)))
1843 ;;;; Horizontal Rule
1845 (defun org-element-horizontal-rule-parser (limit affiliated
)
1846 "Parse an horizontal rule.
1848 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1849 the buffer position at the beginning of the first affiliated
1850 keyword and CDR is a plist of affiliated keywords along with
1853 Return a list whose CAR is `horizontal-rule' and CDR is a plist
1854 containing `:begin', `:end', `:post-blank' and `:post-affiliated'
1857 (let ((begin (car affiliated
))
1858 (post-affiliated (point))
1859 (post-hr (progn (forward-line) (point)))
1860 (end (progn (skip-chars-forward " \r\t\n" limit
)
1861 (skip-chars-backward " \t")
1862 (if (bolp) (point) (line-end-position)))))
1863 (list 'horizontal-rule
1867 :post-blank
(count-lines post-hr end
)
1868 :post-affiliated post-affiliated
)
1869 (cdr affiliated
))))))
1871 (defun org-element-horizontal-rule-interpreter (horizontal-rule contents
)
1872 "Interpret HORIZONTAL-RULE element as Org syntax.
1879 (defun org-element-keyword-parser (limit affiliated
)
1880 "Parse a keyword at point.
1882 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1883 the buffer position at the beginning of the first affiliated
1884 keyword and CDR is a plist of affiliated keywords along with
1887 Return a list whose CAR is `keyword' and CDR is a plist
1888 containing `:key', `:value', `:begin', `:end', `:post-blank' and
1889 `:post-affiliated' keywords."
1891 (let ((begin (car affiliated
))
1892 (post-affiliated (point))
1893 (key (progn (looking-at "[ \t]*#\\+\\(\\S-+*\\):")
1894 (upcase (org-match-string-no-properties 1))))
1895 (value (org-trim (buffer-substring-no-properties
1896 (match-end 0) (point-at-eol))))
1897 (pos-before-blank (progn (forward-line) (point)))
1898 (end (progn (skip-chars-forward " \r\t\n" limit
)
1899 (skip-chars-backward " \t")
1900 (if (bolp) (point) (line-end-position)))))
1907 :post-blank
(count-lines pos-before-blank end
)
1908 :post-affiliated post-affiliated
)
1909 (cdr affiliated
))))))
1911 (defun org-element-keyword-interpreter (keyword contents
)
1912 "Interpret KEYWORD element as Org syntax.
1915 (org-element-property :key keyword
)
1916 (org-element-property :value keyword
)))
1919 ;;;; Latex Environment
1921 (defun org-element-latex-environment-parser (limit affiliated
)
1922 "Parse a LaTeX environment.
1924 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1925 the buffer position at the beginning of the first affiliated
1926 keyword and CDR is a plist of affiliated keywords along with
1929 Return a list whose CAR is `latex-environment' and CDR is a plist
1930 containing `:begin', `:end', `:value', `:post-blank' and
1931 `:post-affiliated' keywords.
1933 Assume point is at the beginning of the latex environment."
1935 (let ((case-fold-search t
)
1936 (code-begin (point)))
1937 (looking-at "[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}")
1938 (if (not (re-search-forward (format "^[ \t]*\\\\end{%s}[ \t]*$"
1939 (regexp-quote (match-string 1)))
1941 ;; Incomplete latex environment: parse it as a paragraph.
1942 (org-element-paragraph-parser limit affiliated
)
1943 (let* ((code-end (progn (forward-line) (point)))
1944 (begin (car affiliated
))
1945 (post-affiliated (point))
1946 (value (buffer-substring-no-properties code-begin code-end
))
1947 (end (progn (skip-chars-forward " \r\t\n" limit
)
1948 (skip-chars-backward " \t")
1949 (if (bolp) (point) (line-end-position)))))
1950 (list 'latex-environment
1955 :post-blank
(count-lines code-end end
)
1956 :post-affiliated post-affiliated
)
1957 (cdr affiliated
))))))))
1959 (defun org-element-latex-environment-interpreter (latex-environment contents
)
1960 "Interpret LATEX-ENVIRONMENT element as Org syntax.
1962 (org-element-property :value latex-environment
))
1967 (defun org-element-node-property-parser (limit)
1968 "Parse a node-property at point.
1970 LIMIT bounds the search.
1972 Return a list whose CAR is `node-property' and CDR is a plist
1973 containing `:key', `:value', `:begin', `:end' and `:post-blank'
1976 (let ((case-fold-search t
)
1978 (key (progn (looking-at "[ \t]*:\\(.*?\\):[ \t]+\\(.*?\\)[ \t]*$")
1979 (org-match-string-no-properties 1)))
1980 (value (org-match-string-no-properties 2))
1981 (pos-before-blank (progn (forward-line) (point)))
1982 (end (progn (skip-chars-forward " \r\t\n" limit
)
1983 (if (eobp) (point) (point-at-bol)))))
1984 (list 'node-property
1989 :post-blank
(count-lines pos-before-blank end
))))))
1991 (defun org-element-node-property-interpreter (node-property contents
)
1992 "Interpret NODE-PROPERTY element as Org syntax.
1994 (format org-property-format
1995 (format ":%s:" (org-element-property :key node-property
))
1996 (org-element-property :value node-property
)))
2001 (defun org-element-paragraph-parser (limit affiliated
)
2004 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2005 the buffer position at the beginning of the first affiliated
2006 keyword and CDR is a plist of affiliated keywords along with
2009 Return a list whose CAR is `paragraph' and CDR is a plist
2010 containing `:begin', `:end', `:contents-begin' and
2011 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
2013 Assume point is at the beginning of the paragraph."
2015 (let* ((begin (car affiliated
))
2016 (contents-begin (point))
2018 (let ((case-fold-search t
))
2020 (if (not (re-search-forward
2021 org-element-paragraph-separate limit
'm
))
2023 ;; A matching `org-element-paragraph-separate' is not
2024 ;; necessarily the end of the paragraph. In
2025 ;; particular, lines starting with # or : as a first
2026 ;; non-space character are ambiguous. We have check
2027 ;; if they are valid Org syntax (i.e. not an
2028 ;; incomplete keyword).
2032 ;; There's no ambiguity for other symbols or
2033 ;; empty lines: stop here.
2034 (looking-at "[ \t]*\\(?:[^:#]\\|$\\)")
2035 ;; Stop at valid fixed-width areas.
2036 (looking-at "[ \t]*:\\(?: \\|$\\)")
2038 (and (looking-at org-drawer-regexp
)
2041 "^[ \t]*:END:[ \t]*$" limit t
)))
2042 ;; Stop at valid comments.
2043 (looking-at "[ \t]*#\\(?: \\|$\\)")
2044 ;; Stop at valid dynamic blocks.
2045 (and (looking-at org-dblock-start-re
)
2048 "^[ \t]*#\\+END:?[ \t]*$" limit t
)))
2049 ;; Stop at valid blocks.
2051 "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
2054 (format "^[ \t]*#\\+END_%s[ \t]*$"
2057 ;; Stop at valid latex environments.
2059 "^[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}[ \t]*$")
2062 (format "^[ \t]*\\\\end{%s}[ \t]*$"
2065 ;; Stop at valid keywords.
2066 (looking-at "[ \t]*#\\+\\S-+:")
2067 ;; Skip everything else.
2071 (re-search-forward org-element-paragraph-separate
2073 (beginning-of-line)))
2074 (if (= (point) limit
) limit
2075 (goto-char (line-beginning-position)))))
2076 (contents-end (progn (skip-chars-backward " \r\t\n" contents-begin
)
2079 (end (progn (skip-chars-forward " \r\t\n" limit
)
2080 (skip-chars-backward " \t")
2081 (if (bolp) (point) (line-end-position)))))
2086 :contents-begin contents-begin
2087 :contents-end contents-end
2088 :post-blank
(count-lines before-blank end
)
2089 :post-affiliated contents-begin
)
2090 (cdr affiliated
))))))
2092 (defun org-element-paragraph-interpreter (paragraph contents
)
2093 "Interpret PARAGRAPH element as Org syntax.
2094 CONTENTS is the contents of the element."
2100 (defun org-element-planning-parser (limit)
2103 LIMIT bounds the search.
2105 Return a list whose CAR is `planning' and CDR is a plist
2106 containing `:closed', `:deadline', `:scheduled', `:begin', `:end'
2107 and `:post-blank' keywords."
2109 (let* ((case-fold-search nil
)
2111 (post-blank (let ((before-blank (progn (forward-line) (point))))
2112 (skip-chars-forward " \r\t\n" limit
)
2113 (skip-chars-backward " \t")
2114 (unless (bolp) (end-of-line))
2115 (count-lines before-blank
(point))))
2117 closed deadline scheduled
)
2119 (while (re-search-forward org-keyword-time-not-clock-regexp end t
)
2120 (goto-char (match-end 1))
2121 (skip-chars-forward " \t" end
)
2122 (let ((keyword (match-string 1))
2123 (time (org-element-timestamp-parser)))
2124 (cond ((equal keyword org-closed-string
) (setq closed time
))
2125 ((equal keyword org-deadline-string
) (setq deadline time
))
2126 (t (setq scheduled time
)))))
2128 (list :closed closed
2130 :scheduled scheduled
2133 :post-blank post-blank
)))))
2135 (defun org-element-planning-interpreter (planning contents
)
2136 "Interpret PLANNING element as Org syntax.
2141 (list (let ((deadline (org-element-property :deadline planning
)))
2143 (concat org-deadline-string
" "
2144 (org-element-timestamp-interpreter deadline nil
))))
2145 (let ((scheduled (org-element-property :scheduled planning
)))
2147 (concat org-scheduled-string
" "
2148 (org-element-timestamp-interpreter scheduled nil
))))
2149 (let ((closed (org-element-property :closed planning
)))
2151 (concat org-closed-string
" "
2152 (org-element-timestamp-interpreter closed nil
))))))
2158 (defun org-element-quote-section-parser (limit)
2159 "Parse a quote section.
2161 LIMIT bounds the search.
2163 Return a list whose CAR is `quote-section' and CDR is a plist
2164 containing `:begin', `:end', `:value' and `:post-blank' keywords.
2166 Assume point is at beginning of the section."
2168 (let* ((begin (point))
2169 (end (progn (org-with-limited-levels (outline-next-heading))
2171 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
2174 (value (buffer-substring-no-properties begin pos-before-blank
)))
2175 (list 'quote-section
2179 :post-blank
(count-lines pos-before-blank end
))))))
2181 (defun org-element-quote-section-interpreter (quote-section contents
)
2182 "Interpret QUOTE-SECTION element as Org syntax.
2184 (org-element-property :value quote-section
))
2189 (defun org-element-src-block-parser (limit affiliated
)
2192 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2193 the buffer position at the beginning of the first affiliated
2194 keyword and CDR is a plist of affiliated keywords along with
2197 Return a list whose CAR is `src-block' and CDR is a plist
2198 containing `:language', `:switches', `:parameters', `:begin',
2199 `:end', `:hiddenp', `:number-lines', `:retain-labels',
2200 `:use-labels', `:label-fmt', `:preserve-indent', `:value',
2201 `:post-blank' and `:post-affiliated' keywords.
2203 Assume point is at the beginning of the block."
2204 (let ((case-fold-search t
))
2205 (if (not (save-excursion (re-search-forward "^[ \t]*#\\+END_SRC[ \t]*$"
2207 ;; Incomplete block: parse it as a paragraph.
2208 (org-element-paragraph-parser limit affiliated
)
2209 (let ((contents-end (match-beginning 0)))
2211 (let* ((begin (car affiliated
))
2212 (post-affiliated (point))
2213 ;; Get language as a string.
2217 (concat "^[ \t]*#\\+BEGIN_SRC"
2218 "\\(?: +\\(\\S-+\\)\\)?"
2219 "\\(\\(?: +\\(?:-l \".*?\"\\|[-+][A-Za-z]\\)\\)+\\)?"
2221 (org-match-string-no-properties 1)))
2223 (switches (org-match-string-no-properties 2))
2225 (parameters (org-match-string-no-properties 3))
2226 ;; Switches analysis
2227 (number-lines (cond ((not switches
) nil
)
2228 ((string-match "-n\\>" switches
) 'new
)
2229 ((string-match "+n\\>" switches
) 'continued
)))
2230 (preserve-indent (and switches
(string-match "-i\\>" switches
)))
2231 (label-fmt (and switches
2232 (string-match "-l +\"\\([^\"\n]+\\)\"" switches
)
2233 (match-string 1 switches
)))
2234 ;; Should labels be retained in (or stripped from)
2238 (not (string-match "-r\\>" switches
))
2239 (and number-lines
(string-match "-k\\>" switches
))))
2240 ;; What should code-references use - labels or
2244 (and retain-labels
(not (string-match "-k\\>" switches
)))))
2245 ;; Get visibility status.
2246 (hidden (progn (forward-line) (org-invisible-p2)))
2248 (value (org-unescape-code-in-string
2249 (buffer-substring-no-properties (point) contents-end
)))
2250 (pos-before-blank (progn (goto-char contents-end
)
2253 ;; Get position after ending blank lines.
2254 (end (progn (skip-chars-forward " \r\t\n" limit
)
2255 (skip-chars-backward " \t")
2256 (if (bolp) (point) (line-end-position)))))
2259 (list :language language
2260 :switches
(and (org-string-nw-p switches
)
2261 (org-trim switches
))
2262 :parameters
(and (org-string-nw-p parameters
)
2263 (org-trim parameters
))
2266 :number-lines number-lines
2267 :preserve-indent preserve-indent
2268 :retain-labels retain-labels
2269 :use-labels use-labels
2270 :label-fmt label-fmt
2273 :post-blank
(count-lines pos-before-blank end
)
2274 :post-affiliated post-affiliated
)
2275 (cdr affiliated
)))))))))
2277 (defun org-element-src-block-interpreter (src-block contents
)
2278 "Interpret SRC-BLOCK element as Org syntax.
2280 (let ((lang (org-element-property :language src-block
))
2281 (switches (org-element-property :switches src-block
))
2282 (params (org-element-property :parameters src-block
))
2283 (value (let ((val (org-element-property :value src-block
)))
2285 (org-src-preserve-indentation val
)
2286 ((zerop org-edit-src-content-indentation
)
2287 (org-remove-indentation val
))
2289 (let ((ind (make-string
2290 org-edit-src-content-indentation
32)))
2291 (replace-regexp-in-string
2292 "\\(^\\)[ \t]*\\S-" ind
2293 (org-remove-indentation val
) nil nil
1)))))))
2294 (concat (format "#+BEGIN_SRC%s\n"
2295 (concat (and lang
(concat " " lang
))
2296 (and switches
(concat " " switches
))
2297 (and params
(concat " " params
))))
2298 (org-escape-code-in-string value
)
2304 (defun org-element-table-parser (limit affiliated
)
2305 "Parse a table at point.
2307 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2308 the buffer position at the beginning of the first affiliated
2309 keyword and CDR is a plist of affiliated keywords along with
2312 Return a list whose CAR is `table' and CDR is a plist containing
2313 `:begin', `:end', `:tblfm', `:type', `:contents-begin',
2314 `:contents-end', `:value', `:post-blank' and `:post-affiliated'
2317 Assume point is at the beginning of the table."
2319 (let* ((case-fold-search t
)
2320 (table-begin (point))
2321 (type (if (org-at-table.el-p
) 'table.el
'org
))
2322 (begin (car affiliated
))
2324 (if (re-search-forward org-table-any-border-regexp limit
'm
)
2325 (goto-char (match-beginning 0))
2328 (while (looking-at "[ \t]*#\\+TBLFM: +\\(.*\\)[ \t]*$")
2329 (push (org-match-string-no-properties 1) acc
)
2332 (pos-before-blank (point))
2333 (end (progn (skip-chars-forward " \r\t\n" limit
)
2334 (skip-chars-backward " \t")
2335 (if (bolp) (point) (line-end-position)))))
2342 ;; Only `org' tables have contents. `table.el' tables
2343 ;; use a `:value' property to store raw table as
2345 :contents-begin
(and (eq type
'org
) table-begin
)
2346 :contents-end
(and (eq type
'org
) table-end
)
2347 :value
(and (eq type
'table.el
)
2348 (buffer-substring-no-properties
2349 table-begin table-end
))
2350 :post-blank
(count-lines pos-before-blank end
)
2351 :post-affiliated table-begin
)
2352 (cdr affiliated
))))))
2354 (defun org-element-table-interpreter (table contents
)
2355 "Interpret TABLE element as Org syntax.
2357 (if (eq (org-element-property :type table
) 'table.el
)
2358 (org-remove-indentation (org-element-property :value table
))
2359 (concat (with-temp-buffer (insert contents
)
2362 (mapconcat (lambda (fm) (concat "#+TBLFM: " fm
))
2363 (reverse (org-element-property :tblfm table
))
2369 (defun org-element-table-row-parser (limit)
2370 "Parse table row at point.
2372 LIMIT bounds the search.
2374 Return a list whose CAR is `table-row' and CDR is a plist
2375 containing `:begin', `:end', `:contents-begin', `:contents-end',
2376 `:type' and `:post-blank' keywords."
2378 (let* ((type (if (looking-at "^[ \t]*|-") 'rule
'standard
))
2380 ;; A table rule has no contents. In that case, ensure
2381 ;; CONTENTS-BEGIN matches CONTENTS-END.
2382 (contents-begin (and (eq type
'standard
)
2383 (search-forward "|")
2385 (contents-end (and (eq type
'standard
)
2388 (skip-chars-backward " \t")
2390 (end (progn (forward-line) (point))))
2395 :contents-begin contents-begin
2396 :contents-end contents-end
2399 (defun org-element-table-row-interpreter (table-row contents
)
2400 "Interpret TABLE-ROW element as Org syntax.
2401 CONTENTS is the contents of the table row."
2402 (if (eq (org-element-property :type table-row
) 'rule
) "|-"
2403 (concat "| " contents
)))
2408 (defun org-element-verse-block-parser (limit affiliated
)
2409 "Parse a verse block.
2411 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2412 the buffer position at the beginning of the first affiliated
2413 keyword and CDR is a plist of affiliated keywords along with
2416 Return a list whose CAR is `verse-block' and CDR is a plist
2417 containing `:begin', `:end', `:contents-begin', `:contents-end',
2418 `:hiddenp', `:post-blank' and `:post-affiliated' keywords.
2420 Assume point is at beginning of the block."
2421 (let ((case-fold-search t
))
2422 (if (not (save-excursion
2423 (re-search-forward "^[ \t]*#\\+END_VERSE[ \t]*$" limit t
)))
2424 ;; Incomplete block: parse it as a paragraph.
2425 (org-element-paragraph-parser limit affiliated
)
2426 (let ((contents-end (match-beginning 0)))
2428 (let* ((begin (car affiliated
))
2429 (post-affiliated (point))
2430 (hidden (progn (forward-line) (org-invisible-p2)))
2431 (contents-begin (point))
2432 (pos-before-blank (progn (goto-char contents-end
)
2435 (end (progn (skip-chars-forward " \r\t\n" limit
)
2436 (skip-chars-backward " \t")
2437 (if (bolp) (point) (line-end-position)))))
2442 :contents-begin contents-begin
2443 :contents-end contents-end
2445 :post-blank
(count-lines pos-before-blank end
)
2446 :post-affiliated post-affiliated
)
2447 (cdr affiliated
)))))))))
2449 (defun org-element-verse-block-interpreter (verse-block contents
)
2450 "Interpret VERSE-BLOCK element as Org syntax.
2451 CONTENTS is verse block contents."
2452 (format "#+BEGIN_VERSE\n%s#+END_VERSE" contents
))
2458 ;; Unlike to elements, interstices can be found between objects.
2459 ;; That's why, along with the parser, successor functions are provided
2460 ;; for each object. Some objects share the same successor (i.e. `code'
2461 ;; and `verbatim' objects).
2463 ;; A successor must accept a single argument bounding the search. It
2464 ;; will return either a cons cell whose CAR is the object's type, as
2465 ;; a symbol, and CDR the position of its next occurrence, or nil.
2467 ;; Successors follow the naming convention:
2468 ;; org-element-NAME-successor, where NAME is the name of the
2469 ;; successor, as defined in `org-element-all-successors'.
2471 ;; Some object types (i.e. `italic') are recursive. Restrictions on
2472 ;; object types they can contain will be specified in
2473 ;; `org-element-object-restrictions'.
2475 ;; Adding a new type of object is simple. Implement a successor,
2476 ;; a parser, and an interpreter for it, all following the naming
2477 ;; convention. Register type in `org-element-all-objects' and
2478 ;; successor in `org-element-all-successors'. Maybe tweak
2479 ;; restrictions about it, and that's it.
2484 (defun org-element-bold-parser ()
2485 "Parse bold object at point.
2487 Return a list whose CAR is `bold' and CDR is a plist with
2488 `:begin', `:end', `:contents-begin' and `:contents-end' and
2489 `:post-blank' keywords.
2491 Assume point is at the first star marker."
2493 (unless (bolp) (backward-char 1))
2494 (looking-at org-emph-re
)
2495 (let ((begin (match-beginning 2))
2496 (contents-begin (match-beginning 4))
2497 (contents-end (match-end 4))
2498 (post-blank (progn (goto-char (match-end 2))
2499 (skip-chars-forward " \t")))
2504 :contents-begin contents-begin
2505 :contents-end contents-end
2506 :post-blank post-blank
)))))
2508 (defun org-element-bold-interpreter (bold contents
)
2509 "Interpret BOLD object as Org syntax.
2510 CONTENTS is the contents of the object."
2511 (format "*%s*" contents
))
2513 (defun org-element-text-markup-successor (limit)
2514 "Search for the next text-markup object.
2516 LIMIT bounds the search.
2518 Return value is a cons cell whose CAR is a symbol among `bold',
2519 `italic', `underline', `strike-through', `code' and `verbatim'
2520 and CDR is beginning position."
2522 (unless (bolp) (backward-char))
2523 (when (re-search-forward org-emph-re limit t
)
2524 (let ((marker (match-string 3)))
2526 ((equal marker
"*") 'bold
)
2527 ((equal marker
"/") 'italic
)
2528 ((equal marker
"_") 'underline
)
2529 ((equal marker
"+") 'strike-through
)
2530 ((equal marker
"~") 'code
)
2531 ((equal marker
"=") 'verbatim
)
2532 (t (error "Unknown marker at %d" (match-beginning 3))))
2533 (match-beginning 2))))))
2538 (defun org-element-code-parser ()
2539 "Parse code object at point.
2541 Return a list whose CAR is `code' and CDR is a plist with
2542 `:value', `:begin', `:end' and `:post-blank' keywords.
2544 Assume point is at the first tilde marker."
2546 (unless (bolp) (backward-char 1))
2547 (looking-at org-emph-re
)
2548 (let ((begin (match-beginning 2))
2549 (value (org-match-string-no-properties 4))
2550 (post-blank (progn (goto-char (match-end 2))
2551 (skip-chars-forward " \t")))
2557 :post-blank post-blank
)))))
2559 (defun org-element-code-interpreter (code contents
)
2560 "Interpret CODE object as Org syntax.
2562 (format "~%s~" (org-element-property :value code
)))
2567 (defun org-element-entity-parser ()
2568 "Parse entity at point.
2570 Return a list whose CAR is `entity' and CDR a plist with
2571 `:begin', `:end', `:latex', `:latex-math-p', `:html', `:latin1',
2572 `:utf-8', `:ascii', `:use-brackets-p' and `:post-blank' as
2575 Assume point is at the beginning of the entity."
2577 (looking-at "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)")
2578 (let* ((value (org-entity-get (match-string 1)))
2579 (begin (match-beginning 0))
2580 (bracketsp (string= (match-string 2) "{}"))
2581 (post-blank (progn (goto-char (match-end 1))
2582 (when bracketsp
(forward-char 2))
2583 (skip-chars-forward " \t")))
2586 (list :name
(car value
)
2587 :latex
(nth 1 value
)
2588 :latex-math-p
(nth 2 value
)
2590 :ascii
(nth 4 value
)
2591 :latin1
(nth 5 value
)
2592 :utf-8
(nth 6 value
)
2595 :use-brackets-p bracketsp
2596 :post-blank post-blank
)))))
2598 (defun org-element-entity-interpreter (entity contents
)
2599 "Interpret ENTITY object as Org syntax.
2602 (org-element-property :name entity
)
2603 (when (org-element-property :use-brackets-p entity
) "{}")))
2605 (defun org-element-latex-or-entity-successor (limit)
2606 "Search for the next latex-fragment or entity object.
2608 LIMIT bounds the search.
2610 Return value is a cons cell whose CAR is `entity' or
2611 `latex-fragment' and CDR is beginning position."
2613 (unless (bolp) (backward-char))
2615 (remove "begin" (plist-get org-format-latex-options
:matchers
)))
2616 ;; ENTITY-RE matches both LaTeX commands and Org entities.
2618 "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))
2619 (when (re-search-forward
2620 (concat (mapconcat (lambda (e) (nth 1 (assoc e org-latex-regexps
)))
2624 (goto-char (match-beginning 0))
2625 (if (looking-at entity-re
)
2626 ;; Determine if it's a real entity or a LaTeX command.
2627 (cons (if (org-entity-get (match-string 1)) 'entity
'latex-fragment
)
2628 (match-beginning 0))
2629 ;; No entity nor command: point is at a LaTeX fragment.
2630 ;; Determine its type to get the correct beginning position.
2631 (cons 'latex-fragment
2634 (when (looking-at (nth 1 (assoc e org-latex-regexps
)))
2637 (nth 2 (assoc e org-latex-regexps
))))))
2644 (defun org-element-export-snippet-parser ()
2645 "Parse export snippet at point.
2647 Return a list whose CAR is `export-snippet' and CDR a plist with
2648 `:begin', `:end', `:back-end', `:value' and `:post-blank' as
2651 Assume point is at the beginning of the snippet."
2653 (re-search-forward "@@\\([-A-Za-z0-9]+\\):" nil t
)
2654 (let* ((begin (match-beginning 0))
2655 (back-end (org-match-string-no-properties 1))
2656 (value (buffer-substring-no-properties
2658 (progn (re-search-forward "@@" nil t
) (match-beginning 0))))
2659 (post-blank (skip-chars-forward " \t"))
2661 (list 'export-snippet
2662 (list :back-end back-end
2666 :post-blank post-blank
)))))
2668 (defun org-element-export-snippet-interpreter (export-snippet contents
)
2669 "Interpret EXPORT-SNIPPET object as Org syntax.
2672 (org-element-property :back-end export-snippet
)
2673 (org-element-property :value export-snippet
)))
2675 (defun org-element-export-snippet-successor (limit)
2676 "Search for the next export-snippet object.
2678 LIMIT bounds the search.
2680 Return value is a cons cell whose CAR is `export-snippet' and CDR
2681 its beginning position."
2684 (when (and (re-search-forward "@@[-A-Za-z0-9]+:" limit t
)
2685 (setq beg
(match-beginning 0))
2686 (search-forward "@@" limit t
))
2687 (cons 'export-snippet beg
)))))
2690 ;;;; Footnote Reference
2692 (defun org-element-footnote-reference-parser ()
2693 "Parse footnote reference at point.
2695 Return a list whose CAR is `footnote-reference' and CDR a plist
2696 with `:label', `:type', `:inline-definition', `:begin', `:end'
2697 and `:post-blank' as keywords."
2699 (looking-at org-footnote-re
)
2700 (let* ((begin (point))
2701 (label (or (org-match-string-no-properties 2)
2702 (org-match-string-no-properties 3)
2703 (and (match-string 1)
2704 (concat "fn:" (org-match-string-no-properties 1)))))
2705 (type (if (or (not label
) (match-string 1)) 'inline
'standard
))
2706 (inner-begin (match-end 0))
2710 (while (and (> count
0) (re-search-forward "[][]" nil t
))
2711 (if (equal (match-string 0) "[") (incf count
) (decf count
)))
2713 (post-blank (progn (goto-char (1+ inner-end
))
2714 (skip-chars-forward " \t")))
2717 (list 'footnote-reference
2722 :post-blank post-blank
))))
2723 (org-element-put-property
2724 footnote-reference
:inline-definition
2725 (and (eq type
'inline
)
2726 (org-element-parse-secondary-string
2727 (buffer-substring inner-begin inner-end
)
2728 (org-element-restriction 'footnote-reference
)
2729 footnote-reference
))))))
2731 (defun org-element-footnote-reference-interpreter (footnote-reference contents
)
2732 "Interpret FOOTNOTE-REFERENCE object as Org syntax.
2734 (let ((label (or (org-element-property :label footnote-reference
) "fn:"))
2737 (org-element-property :inline-definition footnote-reference
)))
2738 (if (not inline-def
) ""
2739 (concat ":" (org-element-interpret-data inline-def
))))))
2740 (format "[%s]" (concat label def
))))
2742 (defun org-element-footnote-reference-successor (limit)
2743 "Search for the next footnote-reference object.
2745 LIMIT bounds the search.
2747 Return value is a cons cell whose CAR is `footnote-reference' and
2748 CDR is beginning position."
2751 (while (re-search-forward org-footnote-re limit t
)
2753 (let ((beg (match-beginning 0))
2756 (while (re-search-forward "[][]" limit t
)
2757 (if (equal (match-string 0) "[") (incf count
) (decf count
))
2759 (throw 'exit
(cons 'footnote-reference beg
))))))))))
2762 ;;;; Inline Babel Call
2764 (defun org-element-inline-babel-call-parser ()
2765 "Parse inline babel call at point.
2767 Return a list whose CAR is `inline-babel-call' and CDR a plist
2768 with `:begin', `:end', `:info' and `:post-blank' as keywords.
2770 Assume point is at the beginning of the babel call."
2772 (unless (bolp) (backward-char))
2773 (looking-at org-babel-inline-lob-one-liner-regexp
)
2774 (let ((info (save-match-data (org-babel-lob-get-info)))
2775 (begin (match-end 1))
2776 (post-blank (progn (goto-char (match-end 0))
2777 (skip-chars-forward " \t")))
2779 (list 'inline-babel-call
2783 :post-blank post-blank
)))))
2785 (defun org-element-inline-babel-call-interpreter (inline-babel-call contents
)
2786 "Interpret INLINE-BABEL-CALL object as Org syntax.
2788 (let* ((babel-info (org-element-property :info inline-babel-call
))
2789 (main-source (car babel-info
))
2790 (post-options (nth 1 babel-info
)))
2792 (if (string-match "\\[\\(\\[.*?\\]\\)\\]" main-source
)
2793 ;; Remove redundant square brackets.
2795 (match-string 1 main-source
) nil nil main-source
)
2797 (and post-options
(format "[%s]" post-options
)))))
2799 (defun org-element-inline-babel-call-successor (limit)
2800 "Search for the next inline-babel-call object.
2802 LIMIT bounds the search.
2804 Return value is a cons cell whose CAR is `inline-babel-call' and
2805 CDR is beginning position."
2807 ;; Use a simplified version of
2808 ;; `org-babel-inline-lob-one-liner-regexp'.
2809 (when (re-search-forward
2810 "call_\\([^()\n]+?\\)\\(?:\\[.*?\\]\\)?([^\n]*?)\\(\\[.*?\\]\\)?"
2812 (cons 'inline-babel-call
(match-beginning 0)))))
2815 ;;;; Inline Src Block
2817 (defun org-element-inline-src-block-parser ()
2818 "Parse inline source block at point.
2820 LIMIT bounds the search.
2822 Return a list whose CAR is `inline-src-block' and CDR a plist
2823 with `:begin', `:end', `:language', `:value', `:parameters' and
2824 `:post-blank' as keywords.
2826 Assume point is at the beginning of the inline src block."
2828 (unless (bolp) (backward-char))
2829 (looking-at org-babel-inline-src-block-regexp
)
2830 (let ((begin (match-beginning 1))
2831 (language (org-match-string-no-properties 2))
2832 (parameters (org-match-string-no-properties 4))
2833 (value (org-match-string-no-properties 5))
2834 (post-blank (progn (goto-char (match-end 0))
2835 (skip-chars-forward " \t")))
2837 (list 'inline-src-block
2838 (list :language language
2840 :parameters parameters
2843 :post-blank post-blank
)))))
2845 (defun org-element-inline-src-block-interpreter (inline-src-block contents
)
2846 "Interpret INLINE-SRC-BLOCK object as Org syntax.
2848 (let ((language (org-element-property :language inline-src-block
))
2849 (arguments (org-element-property :parameters inline-src-block
))
2850 (body (org-element-property :value inline-src-block
)))
2851 (format "src_%s%s{%s}"
2853 (if arguments
(format "[%s]" arguments
) "")
2856 (defun org-element-inline-src-block-successor (limit)
2857 "Search for the next inline-babel-call element.
2859 LIMIT bounds the search.
2861 Return value is a cons cell whose CAR is `inline-babel-call' and
2862 CDR is beginning position."
2864 (unless (bolp) (backward-char))
2865 (when (re-search-forward org-babel-inline-src-block-regexp limit t
)
2866 (cons 'inline-src-block
(match-beginning 1)))))
2870 (defun org-element-italic-parser ()
2871 "Parse italic object at point.
2873 Return a list whose CAR is `italic' and CDR is a plist with
2874 `:begin', `:end', `:contents-begin' and `:contents-end' and
2875 `:post-blank' keywords.
2877 Assume point is at the first slash marker."
2879 (unless (bolp) (backward-char 1))
2880 (looking-at org-emph-re
)
2881 (let ((begin (match-beginning 2))
2882 (contents-begin (match-beginning 4))
2883 (contents-end (match-end 4))
2884 (post-blank (progn (goto-char (match-end 2))
2885 (skip-chars-forward " \t")))
2890 :contents-begin contents-begin
2891 :contents-end contents-end
2892 :post-blank post-blank
)))))
2894 (defun org-element-italic-interpreter (italic contents
)
2895 "Interpret ITALIC object as Org syntax.
2896 CONTENTS is the contents of the object."
2897 (format "/%s/" contents
))
2902 (defun org-element-latex-fragment-parser ()
2903 "Parse latex fragment at point.
2905 Return a list whose CAR is `latex-fragment' and CDR a plist with
2906 `:value', `:begin', `:end', and `:post-blank' as keywords.
2908 Assume point is at the beginning of the latex fragment."
2910 (let* ((begin (point))
2914 (let ((latex-regexp (nth 1 (assoc e org-latex-regexps
))))
2915 (when (or (looking-at latex-regexp
)
2919 (looking-at latex-regexp
))))
2920 (throw 'exit
(nth 2 (assoc e org-latex-regexps
))))))
2921 (plist-get org-format-latex-options
:matchers
))
2922 ;; None found: it's a macro.
2923 (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")
2925 (value (match-string-no-properties substring-match
))
2926 (post-blank (progn (goto-char (match-end substring-match
))
2927 (skip-chars-forward " \t")))
2929 (list 'latex-fragment
2933 :post-blank post-blank
)))))
2935 (defun org-element-latex-fragment-interpreter (latex-fragment contents
)
2936 "Interpret LATEX-FRAGMENT object as Org syntax.
2938 (org-element-property :value latex-fragment
))
2942 (defun org-element-line-break-parser ()
2943 "Parse line break at point.
2945 Return a list whose CAR is `line-break', and CDR a plist with
2946 `:begin', `:end' and `:post-blank' keywords.
2948 Assume point is at the beginning of the line break."
2949 (list 'line-break
(list :begin
(point) :end
(point-at-eol) :post-blank
0)))
2951 (defun org-element-line-break-interpreter (line-break contents
)
2952 "Interpret LINE-BREAK object as Org syntax.
2956 (defun org-element-line-break-successor (limit)
2957 "Search for the next line-break object.
2959 LIMIT bounds the search.
2961 Return value is a cons cell whose CAR is `line-break' and CDR is
2962 beginning position."
2964 (let ((beg (and (re-search-forward "[^\\\\]\\(\\\\\\\\\\)[ \t]*$" limit t
)
2965 (goto-char (match-beginning 1)))))
2966 ;; A line break can only happen on a non-empty line.
2967 (when (and beg
(re-search-backward "\\S-" (point-at-bol) t
))
2968 (cons 'line-break beg
)))))
2973 (defun org-element-link-parser ()
2974 "Parse link at point.
2976 Return a list whose CAR is `link' and CDR a plist with `:type',
2977 `:path', `:raw-link', `:application', `:search-option', `:begin',
2978 `:end', `:contents-begin', `:contents-end' and `:post-blank' as
2981 Assume point is at the beginning of the link."
2983 (let ((begin (point))
2984 end contents-begin contents-end link-end post-blank path type
2985 raw-link link search-option application
)
2987 ;; Type 1: Text targeted from a radio target.
2988 ((and org-target-link-regexp
(looking-at org-target-link-regexp
))
2990 link-end
(match-end 0)
2991 path
(org-match-string-no-properties 0)))
2992 ;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]]
2993 ((looking-at org-bracket-link-regexp
)
2994 (setq contents-begin
(match-beginning 3)
2995 contents-end
(match-end 3)
2996 link-end
(match-end 0)
2997 ;; RAW-LINK is the original link.
2998 raw-link
(org-match-string-no-properties 1)
2999 link
(org-translate-link
3000 (org-link-expand-abbrev
3001 (org-link-unescape raw-link
))))
3002 ;; Determine TYPE of link and set PATH accordingly.
3005 ((or (file-name-absolute-p link
) (string-match "^\\.\\.?/" link
))
3006 (setq type
"file" path link
))
3007 ;; Explicit type (http, irc, bbdb...). See `org-link-types'.
3008 ((string-match org-link-re-with-space3 link
)
3009 (setq type
(match-string 1 link
) path
(match-string 2 link
)))
3010 ;; Id type: PATH is the id.
3011 ((string-match "^id:\\([-a-f0-9]+\\)" link
)
3012 (setq type
"id" path
(match-string 1 link
)))
3013 ;; Code-ref type: PATH is the name of the reference.
3014 ((string-match "^(\\(.*\\))$" link
)
3015 (setq type
"coderef" path
(match-string 1 link
)))
3016 ;; Custom-id type: PATH is the name of the custom id.
3017 ((= (aref link
0) ?
#)
3018 (setq type
"custom-id" path
(substring link
1)))
3019 ;; Fuzzy type: Internal link either matches a target, an
3020 ;; headline name or nothing. PATH is the target or
3022 (t (setq type
"fuzzy" path link
))))
3023 ;; Type 3: Plain link, i.e. http://orgmode.org
3024 ((looking-at org-plain-link-re
)
3025 (setq raw-link
(org-match-string-no-properties 0)
3026 type
(org-match-string-no-properties 1)
3027 path
(org-match-string-no-properties 2)
3028 link-end
(match-end 0)))
3029 ;; Type 4: Angular link, i.e. <http://orgmode.org>
3030 ((looking-at org-angle-link-re
)
3031 (setq raw-link
(buffer-substring-no-properties
3032 (match-beginning 1) (match-end 2))
3033 type
(org-match-string-no-properties 1)
3034 path
(org-match-string-no-properties 2)
3035 link-end
(match-end 0))))
3036 ;; In any case, deduce end point after trailing white space from
3037 ;; LINK-END variable.
3038 (setq post-blank
(progn (goto-char link-end
) (skip-chars-forward " \t"))
3040 ;; Extract search option and opening application out of
3041 ;; "file"-type links.
3042 (when (member type org-element-link-type-is-file
)
3044 (cond ((string-match "^file\\+\\(.*\\)$" type
)
3045 (setq application
(match-string 1 type
)))
3046 ((not (string-match "^file" type
))
3047 (setq application type
)))
3048 ;; Extract search option from PATH.
3049 (when (string-match "::\\(.*\\)$" path
)
3050 (setq search-option
(match-string 1 path
)
3051 path
(replace-match "" nil nil path
)))
3052 ;; Make sure TYPE always report "file".
3057 :raw-link
(or raw-link path
)
3058 :application application
3059 :search-option search-option
3062 :contents-begin contents-begin
3063 :contents-end contents-end
3064 :post-blank post-blank
)))))
3066 (defun org-element-link-interpreter (link contents
)
3067 "Interpret LINK object as Org syntax.
3068 CONTENTS is the contents of the object, or nil."
3069 (let ((type (org-element-property :type link
))
3070 (raw-link (org-element-property :raw-link link
)))
3071 (if (string= type
"radio") raw-link
3074 (if contents
(format "[%s]" contents
) "")))))
3076 (defun org-element-link-successor (limit)
3077 "Search for the next link object.
3079 LIMIT bounds the search.
3081 Return value is a cons cell whose CAR is `link' and CDR is
3082 beginning position."
3085 (if (not org-target-link-regexp
) org-any-link-re
3086 (concat org-any-link-re
"\\|" org-target-link-regexp
))))
3087 (when (re-search-forward link-regexp limit t
)
3088 (cons 'link
(match-beginning 0))))))
3093 (defun org-element-macro-parser ()
3094 "Parse macro at point.
3096 Return a list whose CAR is `macro' and CDR a plist with `:key',
3097 `:args', `:begin', `:end', `:value' and `:post-blank' as
3100 Assume point is at the macro."
3102 (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
3103 (let ((begin (point))
3104 (key (downcase (org-match-string-no-properties 1)))
3105 (value (org-match-string-no-properties 0))
3106 (post-blank (progn (goto-char (match-end 0))
3107 (skip-chars-forward " \t")))
3109 (args (let ((args (org-match-string-no-properties 3)) args2
)
3111 (setq args
(org-split-string args
","))
3113 (while (string-match "\\\\\\'" (car args
))
3114 ;; Repair bad splits.
3115 (setcar (cdr args
) (concat (substring (car args
) 0 -
1)
3118 (push (pop args
) args2
))
3119 (mapcar 'org-trim
(nreverse args2
))))))
3126 :post-blank post-blank
)))))
3128 (defun org-element-macro-interpreter (macro contents
)
3129 "Interpret MACRO object as Org syntax.
3131 (org-element-property :value macro
))
3133 (defun org-element-macro-successor (limit)
3134 "Search for the next macro object.
3136 LIMIT bounds the search.
3138 Return value is cons cell whose CAR is `macro' and CDR is
3139 beginning position."
3141 (when (re-search-forward
3142 "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
3144 (cons 'macro
(match-beginning 0)))))
3149 (defun org-element-radio-target-parser ()
3150 "Parse radio target at point.
3152 Return a list whose CAR is `radio-target' and CDR a plist with
3153 `:begin', `:end', `:contents-begin', `:contents-end', `:value'
3154 and `:post-blank' as keywords.
3156 Assume point is at the radio target."
3158 (looking-at org-radio-target-regexp
)
3159 (let ((begin (point))
3160 (contents-begin (match-beginning 1))
3161 (contents-end (match-end 1))
3162 (value (org-match-string-no-properties 1))
3163 (post-blank (progn (goto-char (match-end 0))
3164 (skip-chars-forward " \t")))
3169 :contents-begin contents-begin
3170 :contents-end contents-end
3171 :post-blank post-blank
3174 (defun org-element-radio-target-interpreter (target contents
)
3175 "Interpret TARGET object as Org syntax.
3176 CONTENTS is the contents of the object."
3177 (concat "<<<" contents
">>>"))
3179 (defun org-element-radio-target-successor (limit)
3180 "Search for the next radio-target object.
3182 LIMIT bounds the search.
3184 Return value is a cons cell whose CAR is `radio-target' and CDR
3185 is beginning position."
3187 (when (re-search-forward org-radio-target-regexp limit t
)
3188 (cons 'radio-target
(match-beginning 0)))))
3191 ;;;; Statistics Cookie
3193 (defun org-element-statistics-cookie-parser ()
3194 "Parse statistics cookie at point.
3196 Return a list whose CAR is `statistics-cookie', and CDR a plist
3197 with `:begin', `:end', `:value' and `:post-blank' keywords.
3199 Assume point is at the beginning of the statistics-cookie."
3201 (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
3202 (let* ((begin (point))
3203 (value (buffer-substring-no-properties
3204 (match-beginning 0) (match-end 0)))
3205 (post-blank (progn (goto-char (match-end 0))
3206 (skip-chars-forward " \t")))
3208 (list 'statistics-cookie
3212 :post-blank post-blank
)))))
3214 (defun org-element-statistics-cookie-interpreter (statistics-cookie contents
)
3215 "Interpret STATISTICS-COOKIE object as Org syntax.
3217 (org-element-property :value statistics-cookie
))
3219 (defun org-element-statistics-cookie-successor (limit)
3220 "Search for the next statistics cookie object.
3222 LIMIT bounds the search.
3224 Return value is a cons cell whose CAR is `statistics-cookie' and
3225 CDR is beginning position."
3227 (when (re-search-forward "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]" limit t
)
3228 (cons 'statistics-cookie
(match-beginning 0)))))
3233 (defun org-element-strike-through-parser ()
3234 "Parse strike-through object at point.
3236 Return a list whose CAR is `strike-through' and CDR is a plist
3237 with `:begin', `:end', `:contents-begin' and `:contents-end' and
3238 `:post-blank' keywords.
3240 Assume point is at the first plus sign marker."
3242 (unless (bolp) (backward-char 1))
3243 (looking-at org-emph-re
)
3244 (let ((begin (match-beginning 2))
3245 (contents-begin (match-beginning 4))
3246 (contents-end (match-end 4))
3247 (post-blank (progn (goto-char (match-end 2))
3248 (skip-chars-forward " \t")))
3250 (list 'strike-through
3253 :contents-begin contents-begin
3254 :contents-end contents-end
3255 :post-blank post-blank
)))))
3257 (defun org-element-strike-through-interpreter (strike-through contents
)
3258 "Interpret STRIKE-THROUGH object as Org syntax.
3259 CONTENTS is the contents of the object."
3260 (format "+%s+" contents
))
3265 (defun org-element-subscript-parser ()
3266 "Parse subscript at point.
3268 Return a list whose CAR is `subscript' and CDR a plist with
3269 `:begin', `:end', `:contents-begin', `:contents-end',
3270 `:use-brackets-p' and `:post-blank' as keywords.
3272 Assume point is at the underscore."
3274 (unless (bolp) (backward-char))
3275 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp
)
3277 (not (looking-at org-match-substring-regexp
))))
3278 (begin (match-beginning 2))
3279 (contents-begin (or (match-beginning 5)
3280 (match-beginning 3)))
3281 (contents-end (or (match-end 5) (match-end 3)))
3282 (post-blank (progn (goto-char (match-end 0))
3283 (skip-chars-forward " \t")))
3288 :use-brackets-p bracketsp
3289 :contents-begin contents-begin
3290 :contents-end contents-end
3291 :post-blank post-blank
)))))
3293 (defun org-element-subscript-interpreter (subscript contents
)
3294 "Interpret SUBSCRIPT object as Org syntax.
3295 CONTENTS is the contents of the object."
3297 (if (org-element-property :use-brackets-p subscript
) "_{%s}" "_%s")
3300 (defun org-element-sub/superscript-successor
(limit)
3301 "Search for the next sub/superscript object.
3303 LIMIT bounds the search.
3305 Return value is a cons cell whose CAR is either `subscript' or
3306 `superscript' and CDR is beginning position."
3308 (unless (bolp) (backward-char))
3309 (when (re-search-forward org-match-substring-regexp limit t
)
3310 (cons (if (string= (match-string 2) "_") 'subscript
'superscript
)
3311 (match-beginning 2)))))
3316 (defun org-element-superscript-parser ()
3317 "Parse superscript at point.
3319 Return a list whose CAR is `superscript' and CDR a plist with
3320 `:begin', `:end', `:contents-begin', `:contents-end',
3321 `:use-brackets-p' and `:post-blank' as keywords.
3323 Assume point is at the caret."
3325 (unless (bolp) (backward-char))
3326 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp
) t
3327 (not (looking-at org-match-substring-regexp
))))
3328 (begin (match-beginning 2))
3329 (contents-begin (or (match-beginning 5)
3330 (match-beginning 3)))
3331 (contents-end (or (match-end 5) (match-end 3)))
3332 (post-blank (progn (goto-char (match-end 0))
3333 (skip-chars-forward " \t")))
3338 :use-brackets-p bracketsp
3339 :contents-begin contents-begin
3340 :contents-end contents-end
3341 :post-blank post-blank
)))))
3343 (defun org-element-superscript-interpreter (superscript contents
)
3344 "Interpret SUPERSCRIPT object as Org syntax.
3345 CONTENTS is the contents of the object."
3347 (if (org-element-property :use-brackets-p superscript
) "^{%s}" "^%s")
3353 (defun org-element-table-cell-parser ()
3354 "Parse table cell at point.
3356 Return a list whose CAR is `table-cell' and CDR is a plist
3357 containing `:begin', `:end', `:contents-begin', `:contents-end'
3358 and `:post-blank' keywords."
3359 (looking-at "[ \t]*\\(.*?\\)[ \t]*|")
3360 (let* ((begin (match-beginning 0))
3362 (contents-begin (match-beginning 1))
3363 (contents-end (match-end 1)))
3367 :contents-begin contents-begin
3368 :contents-end contents-end
3371 (defun org-element-table-cell-interpreter (table-cell contents
)
3372 "Interpret TABLE-CELL element as Org syntax.
3373 CONTENTS is the contents of the cell, or nil."
3374 (concat " " contents
" |"))
3376 (defun org-element-table-cell-successor (limit)
3377 "Search for the next table-cell object.
3379 LIMIT bounds the search.
3381 Return value is a cons cell whose CAR is `table-cell' and CDR is
3382 beginning position."
3383 (when (looking-at "[ \t]*.*?[ \t]+|") (cons 'table-cell
(point))))
3388 (defun org-element-target-parser ()
3389 "Parse target at point.
3391 Return a list whose CAR is `target' and CDR a plist with
3392 `:begin', `:end', `:value' and `:post-blank' as keywords.
3394 Assume point is at the target."
3396 (looking-at org-target-regexp
)
3397 (let ((begin (point))
3398 (value (org-match-string-no-properties 1))
3399 (post-blank (progn (goto-char (match-end 0))
3400 (skip-chars-forward " \t")))
3406 :post-blank post-blank
)))))
3408 (defun org-element-target-interpreter (target contents
)
3409 "Interpret TARGET object as Org syntax.
3411 (format "<<%s>>" (org-element-property :value target
)))
3413 (defun org-element-target-successor (limit)
3414 "Search for the next target object.
3416 LIMIT bounds the search.
3418 Return value is a cons cell whose CAR is `target' and CDR is
3419 beginning position."
3421 (when (re-search-forward org-target-regexp limit t
)
3422 (cons 'target
(match-beginning 0)))))
3427 (defun org-element-timestamp-parser ()
3428 "Parse time stamp at point.
3430 Return a list whose CAR is `timestamp', and CDR a plist with
3431 `:type', `:begin', `:end', `:value' and `:post-blank' keywords.
3433 Assume point is at the beginning of the timestamp."
3435 (let* ((begin (point))
3436 (activep (eq (char-after) ?
<))
3439 (looking-at "\\([<[]\\(%%\\)?.*?\\)[]>]\\(?:--\\([<[].*?[]>]\\)\\)?")
3440 (match-string-no-properties 0)))
3441 (date-start (match-string-no-properties 1))
3442 (date-end (match-string 3))
3443 (diaryp (match-beginning 2))
3444 (type (cond (diaryp 'diary
)
3445 ((and activep date-end
) 'active-range
)
3447 (date-end 'inactive-range
)
3449 (post-blank (progn (goto-char (match-end 0))
3450 (skip-chars-forward " \t")))
3452 (with-time-p (string-match "[012]?[0-9]:[0-5][0-9]" date-start
))
3455 (string-match "\\([.+]?\\+\\)\\([0-9]+\\)\\([hdwmy]\\)>"
3459 (let ((type (match-string 1 raw-value
)))
3460 (cond ((equal "++" type
) 'catch-up
)
3461 ((equal ".+" type
) 'restart
)
3463 :repeater-value
(string-to-number (match-string 2 raw-value
))
3465 (case (string-to-char (match-string 3 raw-value
))
3466 (?h
'hour
) (?d
'day
) (?w
'week
) (?m
'month
) (t 'year
)))))
3467 time-range year-start month-start day-start hour-start minute-start
3468 year-end month-end day-end hour-end minute-end
)
3469 ;; Extract time range, if any, and remove it from date start.
3473 "[012]?[0-9]:[0-5][0-9]\\(-\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)"
3475 (cons (string-to-number (match-string 2 date-start
))
3476 (string-to-number (match-string 3 date-start
)))))
3478 (setq date-start
(replace-match "" nil nil date-start
1)))
3479 ;; Parse date-start.
3481 (let ((date (org-parse-time-string date-start
)))
3482 (setq year-start
(nth 5 date
)
3483 month-start
(nth 4 date
)
3484 day-start
(nth 3 date
)
3485 hour-start
(and with-time-p
(nth 2 date
))
3486 minute-start
(and with-time-p
(nth 1 date
)))))
3487 ;; Compute date-end. It can be provided directly in time-stamp,
3488 ;; or extracted from time range. Otherwise, it defaults to the
3489 ;; same values as date-start.
3491 (let ((date (and date-end
(org-parse-time-string date-end
))))
3492 (setq year-end
(or (nth 5 date
) year-start
)
3493 month-end
(or (nth 4 date
) month-start
)
3494 day-end
(or (nth 3 date
) day-start
)
3495 hour-end
(or (nth 2 date
) (car time-range
) hour-start
)
3496 minute-end
(or (nth 1 date
) (cdr time-range
) minute-start
))))
3498 (nconc (list :type type
3499 :raw-value raw-value
3500 :year-start year-start
3501 :month-start month-start
3502 :day-start day-start
3503 :hour-start hour-start
3504 :minute-start minute-start
3506 :month-end month-end
3509 :minute-end minute-end
3512 :post-blank post-blank
)
3515 (defun org-element-timestamp-interpreter (timestamp contents
)
3516 "Interpret TIMESTAMP object as Org syntax.
3518 ;; Use `:raw-value' if specified.
3519 (or (org-element-property :raw-value timestamp
)
3520 ;; Otherwise, build timestamp string.
3521 (let ((build-ts-string
3522 ;; Build an Org timestamp string from TIME. ACTIVEP is
3523 ;; non-nil when time stamp is active. If WITH-TIME-P is
3524 ;; non-nil, add a time part. HOUR-END and MINUTE-END
3525 ;; specify a time range in the timestamp. REPEAT-STRING
3526 ;; is the repeater string, if any.
3527 (lambda (time activep
3528 &optional with-time-p hour-end minute-end repeat-string
)
3529 (let ((ts (format-time-string
3530 (funcall (if with-time-p
'cdr
'car
)
3531 org-time-stamp-formats
)
3533 (when (and hour-end minute-end
)
3534 (string-match "[012]?[0-9]:[0-5][0-9]" ts
)
3537 (format "\\&-%02d:%02d" hour-end minute-end
)
3539 (unless activep
(setq ts
(format "[%s]" (substring ts
1 -
1))))
3540 (when (org-string-nw-p repeat-string
)
3541 (setq ts
(concat (substring ts
0 -
1)
3544 (substring ts -
1))))
3547 (type (org-element-property :type timestamp
)))
3550 (let* ((minute-start (org-element-property :minute-start timestamp
))
3551 (minute-end (org-element-property :minute-end timestamp
))
3552 (hour-start (org-element-property :hour-start timestamp
))
3553 (hour-end (org-element-property :hour-end timestamp
))
3554 (time-range-p (and hour-start hour-end minute-start minute-end
3555 (or (/= hour-start hour-end
)
3556 (/= minute-start minute-end
)))))
3562 (org-element-property :day-start timestamp
)
3563 (org-element-property :month-start timestamp
)
3564 (org-element-property :year-start timestamp
))
3566 (and hour-start minute-start
)
3567 (and time-range-p hour-end
)
3568 (and time-range-p minute-end
)
3569 (concat (case (org-element-property :repeater-type timestamp
)
3570 (cumulate "+") (catch-up "++") (restart ".+"))
3571 (org-element-property :repeater-value timestamp
)
3572 (org-element-property :repeater-unit timestamp
)))))
3573 ((active-range inactive-range
)
3574 (let ((minute-start (org-element-property :minute-start timestamp
))
3575 (minute-end (org-element-property :minute-end timestamp
))
3576 (hour-start (org-element-property :hour-start timestamp
))
3577 (hour-end (org-element-property :hour-end timestamp
)))
3580 build-ts-string
(encode-time
3584 (org-element-property :day-start timestamp
)
3585 (org-element-property :month-start timestamp
)
3586 (org-element-property :year-start timestamp
))
3587 (eq type
'active-range
)
3588 (and hour-start minute-start
))
3590 (funcall build-ts-string
3594 (org-element-property :day-end timestamp
)
3595 (org-element-property :month-end timestamp
)
3596 (org-element-property :year-end timestamp
))
3597 (eq type
'active-range
)
3598 (and hour-end minute-end
)))))))))
3600 (defun org-element-timestamp-successor (limit)
3601 "Search for the next timestamp object.
3603 LIMIT bounds the search.
3605 Return value is a cons cell whose CAR is `timestamp' and CDR is
3606 beginning position."
3608 (when (re-search-forward
3609 (concat org-ts-regexp-both
3611 "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
3613 "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
3615 (cons 'timestamp
(match-beginning 0)))))
3620 (defun org-element-underline-parser ()
3621 "Parse underline object at point.
3623 Return a list whose CAR is `underline' and CDR is a plist with
3624 `:begin', `:end', `:contents-begin' and `:contents-end' and
3625 `:post-blank' keywords.
3627 Assume point is at the first underscore marker."
3629 (unless (bolp) (backward-char 1))
3630 (looking-at org-emph-re
)
3631 (let ((begin (match-beginning 2))
3632 (contents-begin (match-beginning 4))
3633 (contents-end (match-end 4))
3634 (post-blank (progn (goto-char (match-end 2))
3635 (skip-chars-forward " \t")))
3640 :contents-begin contents-begin
3641 :contents-end contents-end
3642 :post-blank post-blank
)))))
3644 (defun org-element-underline-interpreter (underline contents
)
3645 "Interpret UNDERLINE object as Org syntax.
3646 CONTENTS is the contents of the object."
3647 (format "_%s_" contents
))
3652 (defun org-element-verbatim-parser ()
3653 "Parse verbatim object at point.
3655 Return a list whose CAR is `verbatim' and CDR is a plist with
3656 `:value', `:begin', `:end' and `:post-blank' keywords.
3658 Assume point is at the first equal sign marker."
3660 (unless (bolp) (backward-char 1))
3661 (looking-at org-emph-re
)
3662 (let ((begin (match-beginning 2))
3663 (value (org-match-string-no-properties 4))
3664 (post-blank (progn (goto-char (match-end 2))
3665 (skip-chars-forward " \t")))
3671 :post-blank post-blank
)))))
3673 (defun org-element-verbatim-interpreter (verbatim contents
)
3674 "Interpret VERBATIM object as Org syntax.
3676 (format "=%s=" (org-element-property :value verbatim
)))
3680 ;;; Parsing Element Starting At Point
3682 ;; `org-element--current-element' is the core function of this section.
3683 ;; It returns the Lisp representation of the element starting at
3686 ;; `org-element--current-element' makes use of special modes. They
3687 ;; are activated for fixed element chaining (i.e. `plain-list' >
3688 ;; `item') or fixed conditional element chaining (i.e. `headline' >
3689 ;; `section'). Special modes are: `first-section', `item',
3690 ;; `node-property', `quote-section', `section' and `table-row'.
3692 (defun org-element--current-element
3693 (limit &optional granularity special structure
)
3694 "Parse the element starting at point.
3696 LIMIT bounds the search.
3698 Return value is a list like (TYPE PROPS) where TYPE is the type
3699 of the element and PROPS a plist of properties associated to the
3702 Possible types are defined in `org-element-all-elements'.
3704 Optional argument GRANULARITY determines the depth of the
3705 recursion. Allowed values are `headline', `greater-element',
3706 `element', `object' or nil. When it is broader than `object' (or
3707 nil), secondary values will not be parsed, since they only
3710 Optional argument SPECIAL, when non-nil, can be either
3711 `first-section', `item', `node-property', `quote-section',
3712 `section', and `table-row'.
3714 If STRUCTURE isn't provided but SPECIAL is set to `item', it will
3717 This function assumes point is always at the beginning of the
3718 element it has to parse."
3720 (let ((case-fold-search t
)
3721 ;; Determine if parsing depth allows for secondary strings
3722 ;; parsing. It only applies to elements referenced in
3723 ;; `org-element-secondary-value-alist'.
3724 (raw-secondary-p (and granularity
(not (eq granularity
'object
)))))
3728 (org-element-item-parser limit structure raw-secondary-p
))
3730 ((eq special
'table-row
) (org-element-table-row-parser limit
))
3732 ((eq special
'node-property
) (org-element-node-property-parser limit
))
3734 ((org-with-limited-levels (org-at-heading-p))
3735 (org-element-headline-parser limit raw-secondary-p
))
3736 ;; Sections (must be checked after headline).
3737 ((eq special
'section
) (org-element-section-parser limit
))
3738 ((eq special
'quote-section
) (org-element-quote-section-parser limit
))
3739 ((eq special
'first-section
)
3740 (org-element-section-parser
3741 (or (save-excursion (org-with-limited-levels (outline-next-heading)))
3743 ;; When not at bol, point is at the beginning of an item or
3744 ;; a footnote definition: next item is always a paragraph.
3745 ((not (bolp)) (org-element-paragraph-parser limit
(list (point))))
3746 ;; Planning and Clock.
3747 ((looking-at org-planning-or-clock-line-re
)
3748 (if (equal (match-string 1) org-clock-string
)
3749 (org-element-clock-parser limit
)
3750 (org-element-planning-parser limit
)))
3753 (org-element-inlinetask-parser limit raw-secondary-p
))
3754 ;; From there, elements can have affiliated keywords.
3755 (t (let ((affiliated (org-element--collect-affiliated-keywords)))
3757 ;; LaTeX Environment.
3758 ((looking-at "[ \t]*\\\\begin{\\([A-Za-z0-9*]+\\)}[ \t]*$")
3759 (org-element-latex-environment-parser limit affiliated
))
3760 ;; Drawer and Property Drawer.
3761 ((looking-at org-drawer-regexp
)
3762 (if (equal (match-string 1) "PROPERTIES")
3763 (org-element-property-drawer-parser limit affiliated
)
3764 (org-element-drawer-parser limit affiliated
)))
3766 ((looking-at "[ \t]*:\\( \\|$\\)")
3767 (org-element-fixed-width-parser limit affiliated
))
3768 ;; Inline Comments, Blocks, Babel Calls, Dynamic Blocks and
3770 ((looking-at "[ \t]*#")
3771 (goto-char (match-end 0))
3772 (cond ((looking-at "\\(?: \\|$\\)")
3774 (org-element-comment-parser limit affiliated
))
3775 ((looking-at "\\+BEGIN_\\(\\S-+\\)")
3777 (let ((parser (assoc (upcase (match-string 1))
3778 org-element-block-name-alist
)))
3779 (if parser
(funcall (cdr parser
) limit affiliated
)
3780 (org-element-special-block-parser limit affiliated
))))
3781 ((looking-at "\\+CALL:")
3783 (org-element-babel-call-parser limit affiliated
))
3784 ((looking-at "\\+BEGIN:? ")
3786 (org-element-dynamic-block-parser limit affiliated
))
3787 ((looking-at "\\+\\S-+:")
3789 (org-element-keyword-parser limit affiliated
))
3792 (org-element-paragraph-parser limit affiliated
))))
3793 ;; Footnote Definition.
3794 ((looking-at org-footnote-definition-re
)
3795 (org-element-footnote-definition-parser limit affiliated
))
3797 ((looking-at "[ \t]*-\\{5,\\}[ \t]*$")
3798 (org-element-horizontal-rule-parser limit affiliated
))
3801 (org-element-diary-sexp-parser limit affiliated
))
3803 ((org-at-table-p t
) (org-element-table-parser limit affiliated
))
3805 ((looking-at (org-item-re))
3806 (org-element-plain-list-parser
3807 limit affiliated
(or structure
(org-list-struct))))
3808 ;; Default element: Paragraph.
3809 (t (org-element-paragraph-parser limit affiliated
)))))))))
3812 ;; Most elements can have affiliated keywords. When looking for an
3813 ;; element beginning, we want to move before them, as they belong to
3814 ;; that element, and, in the meantime, collect information they give
3815 ;; into appropriate properties. Hence the following function.
3817 (defun org-element--collect-affiliated-keywords ()
3818 "Collect affiliated keywords from point.
3820 Return a list whose CAR is the position at the first of them and
3821 CDR a plist of keywords and values and move point to the
3822 beginning of the first line after them.
3824 As a special case, if element doesn't start at the beginning of
3825 the line (i.e. a paragraph starting an item), CAR is current
3826 position of point and CDR is nil."
3827 (if (not (bolp)) (list (point))
3828 (let ((case-fold-search t
)
3830 ;; RESTRICT is the list of objects allowed in parsed
3832 (restrict (org-element-restriction 'keyword
))
3834 (while (and (not (eobp)) (looking-at org-element--affiliated-re
))
3835 (let* ((raw-kwd (upcase (match-string 1)))
3836 ;; Apply translation to RAW-KWD. From there, KWD is
3837 ;; the official keyword.
3838 (kwd (or (cdr (assoc raw-kwd
3839 org-element-keyword-translation-alist
))
3841 ;; Find main value for any keyword.
3845 (buffer-substring-no-properties
3846 (match-end 0) (point-at-eol)))))
3847 ;; PARSEDP is non-nil when keyword should have its
3849 (parsedp (member kwd org-element-parsed-keywords
))
3850 ;; If KWD is a dual keyword, find its secondary
3851 ;; value. Maybe parse it.
3852 (dualp (member kwd org-element-dual-keywords
))
3855 (let ((sec (org-match-string-no-properties 2)))
3856 (if (or (not sec
) (not parsedp
)) sec
3857 (org-element-parse-secondary-string sec restrict
)))))
3858 ;; Attribute a property name to KWD.
3859 (kwd-sym (and kwd
(intern (concat ":" (downcase kwd
))))))
3860 ;; Now set final shape for VALUE.
3862 (setq value
(org-element-parse-secondary-string value restrict
)))
3864 (setq value
(and (or value dual-value
) (cons value dual-value
))))
3865 (when (or (member kwd org-element-multiple-keywords
)
3866 ;; Attributes can always appear on multiple lines.
3867 (string-match "^ATTR_" kwd
))
3868 (setq value
(cons value
(plist-get output kwd-sym
))))
3869 ;; Eventually store the new value in OUTPUT.
3870 (setq output
(plist-put output kwd-sym value
))
3871 ;; Move to next keyword.
3873 ;; If affiliated keywords are orphaned: move back to first one.
3874 ;; They will be parsed as a paragraph.
3875 (when (looking-at "[ \t]*$") (goto-char origin
) (setq output nil
))
3877 (cons origin output
))))
3883 ;; The two major functions here are `org-element-parse-buffer', which
3884 ;; parses Org syntax inside the current buffer, taking into account
3885 ;; region, narrowing, or even visibility if specified, and
3886 ;; `org-element-parse-secondary-string', which parses objects within
3889 ;; The (almost) almighty `org-element-map' allows to apply a function
3890 ;; on elements or objects matching some type, and accumulate the
3891 ;; resulting values. In an export situation, it also skips unneeded
3892 ;; parts of the parse tree.
3894 (defun org-element-parse-buffer (&optional granularity visible-only
)
3895 "Recursively parse the buffer and return structure.
3896 If narrowing is in effect, only parse the visible part of the
3899 Optional argument GRANULARITY determines the depth of the
3900 recursion. It can be set to the following symbols:
3902 `headline' Only parse headlines.
3903 `greater-element' Don't recurse into greater elements excepted
3904 headlines and sections. Thus, elements
3905 parsed are the top-level ones.
3906 `element' Parse everything but objects and plain text.
3907 `object' Parse the complete buffer (default).
3909 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3912 Assume buffer is in Org mode."
3914 (goto-char (point-min))
3915 (org-skip-whitespace)
3916 (org-element--parse-elements
3917 (point-at-bol) (point-max)
3918 ;; Start in `first-section' mode so text before the first
3919 ;; headline belongs to a section.
3920 'first-section nil granularity visible-only
(list 'org-data nil
))))
3922 (defun org-element-parse-secondary-string (string restriction
&optional parent
)
3923 "Recursively parse objects in STRING and return structure.
3925 RESTRICTION is a symbol limiting the object types that will be
3928 Optional argument PARENT, when non-nil, is the element or object
3929 containing the secondary string. It is used to set correctly
3930 `:parent' property within the string."
3933 (let ((secondary (org-element--parse-objects
3934 (point-min) (point-max) nil restriction
)))
3936 (mapc (lambda (obj) (org-element-put-property obj
:parent parent
))
3940 (defun org-element-map
3941 (data types fun
&optional info first-match no-recursion with-affiliated
)
3942 "Map a function on selected elements or objects.
3944 DATA is the parsed tree, as returned by, i.e,
3945 `org-element-parse-buffer'. TYPES is a symbol or list of symbols
3946 of elements or objects types. FUN is the function called on the
3947 matching element or object. It must accept one argument: the
3948 element or object itself.
3950 When optional argument INFO is non-nil, it should be a plist
3951 holding export options. In that case, parts of the parse tree
3952 not exportable according to that property list will be skipped.
3954 When optional argument FIRST-MATCH is non-nil, stop at the first
3955 match for which FUN doesn't return nil, and return that value.
3957 Optional argument NO-RECURSION is a symbol or a list of symbols
3958 representing elements or objects types. `org-element-map' won't
3959 enter any recursive element or object whose type belongs to that
3960 list. Though, FUN can still be applied on them.
3962 When optional argument WITH-AFFILIATED is non-nil, also move into
3963 affiliated keywords to find objects.
3965 Nil values returned from FUN do not appear in the results."
3966 ;; Ensure TYPES and NO-RECURSION are a list, even of one element.
3967 (unless (listp types
) (setq types
(list types
)))
3968 (unless (listp no-recursion
) (setq no-recursion
(list no-recursion
)))
3969 ;; Recursion depth is determined by --CATEGORY.
3972 (let ((category 'greater-elements
))
3973 (mapc (lambda (type)
3974 (cond ((or (memq type org-element-all-objects
)
3975 (eq type
'plain-text
))
3976 ;; If one object is found, the function
3977 ;; has to recurse into every object.
3978 (throw 'found
'objects
))
3979 ((not (memq type org-element-greater-elements
))
3980 ;; If one regular element is found, the
3981 ;; function has to recurse, at least,
3982 ;; into every element it encounters.
3983 (and (not (eq category
'elements
))
3984 (setq category
'elements
)))))
3987 ;; Compute properties for affiliated keywords if necessary.
3989 (and with-affiliated
3990 (mapcar (lambda (kwd)
3991 (cons kwd
(intern (concat ":" (downcase kwd
)))))
3992 org-element-affiliated-keywords
)))
3998 ;; Recursively walk DATA. INFO, if non-nil, is a plist
3999 ;; holding contextual information.
4000 (let ((--type (org-element-type --data
)))
4003 ;; Ignored element in an export context.
4004 ((and info
(memq --data
(plist-get info
:ignore-list
))))
4005 ;; Secondary string: only objects can be found there.
4007 (when (eq --category
'objects
) (mapc --walk-tree --data
)))
4008 ;; Unconditionally enter parse trees.
4009 ((eq --type
'org-data
)
4010 (mapc --walk-tree
(org-element-contents --data
)))
4012 ;; Check if TYPE is matching among TYPES. If so,
4013 ;; apply FUN to --DATA and accumulate return value
4014 ;; into --ACC (or exit if FIRST-MATCH is non-nil).
4015 (when (memq --type types
)
4016 (let ((result (funcall fun --data
)))
4017 (cond ((not result
))
4018 (first-match (throw '--map-first-match result
))
4019 (t (push result --acc
)))))
4020 ;; If --DATA has a secondary string that can contain
4021 ;; objects with their type among TYPES, look into it.
4022 (when (and (eq --category
'objects
) (not (stringp --data
)))
4024 (assq --type org-element-secondary-value-alist
)))
4026 (funcall --walk-tree
4027 (org-element-property (cdr sec-prop
) --data
)))))
4028 ;; If --DATA has any affiliated keywords and
4029 ;; WITH-AFFILIATED is non-nil, look for objects in
4031 (when (and with-affiliated
4032 (eq --category
'objects
)
4033 (memq --type org-element-all-elements
))
4034 (mapc (lambda (kwd-pair)
4035 (let ((kwd (car kwd-pair
))
4036 (value (org-element-property
4037 (cdr kwd-pair
) --data
)))
4038 ;; Pay attention to the type of value.
4039 ;; Preserve order for multiple keywords.
4042 ((and (member kwd org-element-multiple-keywords
)
4043 (member kwd org-element-dual-keywords
))
4044 (mapc (lambda (line)
4045 (funcall --walk-tree
(cdr line
))
4046 (funcall --walk-tree
(car line
)))
4048 ((member kwd org-element-multiple-keywords
)
4049 (mapc (lambda (line) (funcall --walk-tree line
))
4051 ((member kwd org-element-dual-keywords
)
4052 (funcall --walk-tree
(cdr value
))
4053 (funcall --walk-tree
(car value
)))
4054 (t (funcall --walk-tree value
)))))
4055 --affiliated-alist
))
4056 ;; Determine if a recursion into --DATA is possible.
4058 ;; --TYPE is explicitly removed from recursion.
4059 ((memq --type no-recursion
))
4060 ;; --DATA has no contents.
4061 ((not (org-element-contents --data
)))
4062 ;; Looking for greater elements but --DATA is simply
4063 ;; an element or an object.
4064 ((and (eq --category
'greater-elements
)
4065 (not (memq --type org-element-greater-elements
))))
4066 ;; Looking for elements but --DATA is an object.
4067 ((and (eq --category
'elements
)
4068 (memq --type org-element-all-objects
)))
4069 ;; In any other case, map contents.
4070 (t (mapc --walk-tree
(org-element-contents --data
)))))))))))
4071 (catch '--map-first-match
4072 (funcall --walk-tree data
)
4073 ;; Return value in a proper order.
4076 ;; The following functions are internal parts of the parser.
4078 ;; The first one, `org-element--parse-elements' acts at the element's
4081 ;; The second one, `org-element--parse-objects' applies on all objects
4082 ;; of a paragraph or a secondary string. It uses
4083 ;; `org-element--get-next-object-candidates' to optimize the search of
4084 ;; the next object in the buffer.
4086 ;; More precisely, that function looks for every allowed object type
4087 ;; first. Then, it discards failed searches, keeps further matches,
4088 ;; and searches again types matched behind point, for subsequent
4089 ;; calls. Thus, searching for a given type fails only once, and every
4090 ;; object is searched only once at top level (but sometimes more for
4093 (defun org-element--parse-elements
4094 (beg end special structure granularity visible-only acc
)
4095 "Parse elements between BEG and END positions.
4097 SPECIAL prioritize some elements over the others. It can be set
4098 to `first-section', `quote-section', `section' `item' or
4101 When value is `item', STRUCTURE will be used as the current list
4104 GRANULARITY determines the depth of the recursion. See
4105 `org-element-parse-buffer' for more information.
4107 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
4110 Elements are accumulated into ACC."
4113 ;; When parsing only headlines, skip any text before first one.
4114 (when (and (eq granularity
'headline
) (not (org-at-heading-p)))
4115 (org-with-limited-levels (outline-next-heading)))
4117 (while (< (point) end
)
4118 ;; Find current element's type and parse it accordingly to
4120 (let* ((element (org-element--current-element
4121 end granularity special structure
))
4122 (type (org-element-type element
))
4123 (cbeg (org-element-property :contents-begin element
)))
4124 (goto-char (org-element-property :end element
))
4125 ;; Fill ELEMENT contents by side-effect.
4127 ;; If VISIBLE-ONLY is true and element is hidden or if it has
4128 ;; no contents, don't modify it.
4129 ((or (and visible-only
(org-element-property :hiddenp element
))
4131 ;; Greater element: parse it between `contents-begin' and
4132 ;; `contents-end'. Make sure GRANULARITY allows the
4133 ;; recursion, or ELEMENT is an headline, in which case going
4134 ;; inside is mandatory, in order to get sub-level headings.
4135 ((and (memq type org-element-greater-elements
)
4136 (or (memq granularity
'(element object nil
))
4137 (and (eq granularity
'greater-element
)
4139 (eq type
'headline
)))
4140 (org-element--parse-elements
4141 cbeg
(org-element-property :contents-end element
)
4142 ;; Possibly switch to a special mode.
4145 (if (org-element-property :quotedp element
) 'quote-section
4148 (property-drawer 'node-property
)
4150 (org-element-property :structure element
)
4151 granularity visible-only element
))
4152 ;; ELEMENT has contents. Parse objects inside, if
4153 ;; GRANULARITY allows it.
4154 ((memq granularity
'(object nil
))
4155 (org-element--parse-objects
4156 cbeg
(org-element-property :contents-end element
) element
4157 (org-element-restriction type
))))
4158 (org-element-adopt-elements acc element
)))
4162 (defun org-element--parse-objects (beg end acc restriction
)
4163 "Parse objects between BEG and END and return recursive structure.
4165 Objects are accumulated in ACC.
4167 RESTRICTION is a list of object types which are allowed in the
4172 (while (and (< (point) end
)
4173 (setq candidates
(org-element--get-next-object-candidates
4174 end restriction candidates
)))
4176 (let ((pos (apply 'min
(mapcar 'cdr candidates
))))
4179 (funcall (intern (format "org-element-%s-parser"
4180 (car (rassq pos candidates
)))))))))
4181 ;; 1. Text before any object. Untabify it.
4182 (let ((obj-beg (org-element-property :begin next-object
)))
4183 (unless (= (point) obj-beg
)
4185 (org-element-adopt-elements
4187 (replace-regexp-in-string
4188 "\t" (make-string tab-width ?
)
4189 (buffer-substring-no-properties (point) obj-beg
))))))
4191 (let ((obj-end (org-element-property :end next-object
))
4192 (cont-beg (org-element-property :contents-begin next-object
)))
4193 ;; Fill contents of NEXT-OBJECT by side-effect, if it has
4194 ;; a recursive type.
4196 (memq (car next-object
) org-element-recursive-objects
))
4200 (org-element-property :contents-end next-object
))
4201 (org-element--parse-objects
4202 (point-min) (point-max) next-object
4203 (org-element-restriction next-object
))))
4204 (setq acc
(org-element-adopt-elements acc next-object
))
4205 (goto-char obj-end
))))
4206 ;; 3. Text after last object. Untabify it.
4207 (unless (= (point) end
)
4209 (org-element-adopt-elements
4211 (replace-regexp-in-string
4212 "\t" (make-string tab-width ?
)
4213 (buffer-substring-no-properties (point) end
)))))
4217 (defun org-element--get-next-object-candidates (limit restriction objects
)
4218 "Return an alist of candidates for the next object.
4220 LIMIT bounds the search, and RESTRICTION narrows candidates to
4223 Return value is an alist whose CAR is position and CDR the object
4226 OBJECTS is the previous candidates alist."
4227 ;; Filter out any object found but not belonging to RESTRICTION.
4231 (let ((type (car obj
)))
4232 (memq (or (cdr (assq type org-element-object-successor-alist
))
4236 (let (next-candidates types-to-search
)
4237 ;; If no previous result, search every object type in RESTRICTION.
4238 ;; Otherwise, keep potential candidates (old objects located after
4239 ;; point) and ask to search again those which had matched before.
4240 (if (not objects
) (setq types-to-search restriction
)
4242 (if (< (cdr obj
) (point)) (push (car obj
) types-to-search
)
4243 (push obj next-candidates
)))
4245 ;; Call the appropriate successor function for each type to search
4246 ;; and accumulate matches.
4249 (let* ((successor-fun
4251 (format "org-element-%s-successor"
4252 (or (cdr (assq type org-element-object-successor-alist
))
4254 (obj (funcall successor-fun limit
)))
4255 (and obj
(push obj next-candidates
))))
4262 ;;; Towards A Bijective Process
4264 ;; The parse tree obtained with `org-element-parse-buffer' is really
4265 ;; a snapshot of the corresponding Org buffer. Therefore, it can be
4266 ;; interpreted and expanded into a string with canonical Org syntax.
4267 ;; Hence `org-element-interpret-data'.
4269 ;; The function relies internally on
4270 ;; `org-element--interpret-affiliated-keywords'.
4273 (defun org-element-interpret-data (data &optional parent
)
4274 "Interpret DATA as Org syntax.
4276 DATA is a parse tree, an element, an object or a secondary string
4279 Optional argument PARENT is used for recursive calls. It contains
4280 the element or object containing data, or nil.
4282 Return Org syntax as a string."
4283 (let* ((type (org-element-type data
))
4286 ;; Secondary string.
4289 (lambda (obj) (org-element-interpret-data obj parent
))
4291 ;; Full Org document.
4292 ((eq type
'org-data
)
4294 (lambda (obj) (org-element-interpret-data obj parent
))
4295 (org-element-contents data
) ""))
4297 ((stringp data
) data
)
4298 ;; Element/Object without contents.
4299 ((not (org-element-contents data
))
4300 (funcall (intern (format "org-element-%s-interpreter" type
))
4302 ;; Element/Object with contents.
4304 (let* ((greaterp (memq type org-element-greater-elements
))
4305 (objectp (and (not greaterp
)
4306 (memq type org-element-recursive-objects
)))
4309 (lambda (obj) (org-element-interpret-data obj data
))
4310 (org-element-contents
4311 (if (or greaterp objectp
) data
4312 ;; Elements directly containing objects must
4313 ;; have their indentation normalized first.
4314 (org-element-normalize-contents
4316 ;; When normalizing first paragraph of an
4317 ;; item or a footnote-definition, ignore
4318 ;; first line's indentation.
4319 (and (eq type
'paragraph
)
4320 (equal data
(car (org-element-contents parent
)))
4321 (memq (org-element-type parent
)
4322 '(footnote-definition item
))))))
4324 (funcall (intern (format "org-element-%s-interpreter" type
))
4326 (if greaterp
(org-element-normalize-contents contents
)
4328 (if (memq type
'(org-data plain-text nil
)) results
4329 ;; Build white spaces. If no `:post-blank' property is
4330 ;; specified, assume its value is 0.
4331 (let ((post-blank (or (org-element-property :post-blank data
) 0)))
4332 (if (memq type org-element-all-objects
)
4333 (concat results
(make-string post-blank
32))
4335 (org-element--interpret-affiliated-keywords data
)
4336 (org-element-normalize-string results
)
4337 (make-string post-blank
10)))))))
4339 (defun org-element--interpret-affiliated-keywords (element)
4340 "Return ELEMENT's affiliated keywords as Org syntax.
4341 If there is no affiliated keyword, return the empty string."
4342 (let ((keyword-to-org
4346 (when (member key org-element-dual-keywords
)
4347 (setq dual
(cdr value
) value
(car value
)))
4350 (format "[%s]" (org-element-interpret-data dual
)))
4352 (if (member key org-element-parsed-keywords
)
4353 (org-element-interpret-data value
)
4358 (let ((value (org-element-property prop element
))
4359 (keyword (upcase (substring (symbol-name prop
) 1))))
4361 (if (or (member keyword org-element-multiple-keywords
)
4362 ;; All attribute keywords can have multiple lines.
4363 (string-match "^ATTR_" keyword
))
4364 (mapconcat (lambda (line) (funcall keyword-to-org keyword line
))
4367 (funcall keyword-to-org keyword value
)))))
4368 ;; List all ELEMENT's properties matching an attribute line or an
4369 ;; affiliated keyword, but ignore translated keywords since they
4370 ;; cannot belong to the property list.
4371 (loop for prop in
(nth 1 element
) by
'cddr
4372 when
(let ((keyword (upcase (substring (symbol-name prop
) 1))))
4373 (or (string-match "^ATTR_" keyword
)
4375 (member keyword org-element-affiliated-keywords
)
4377 org-element-keyword-translation-alist
)))))
4381 ;; Because interpretation of the parse tree must return the same
4382 ;; number of blank lines between elements and the same number of white
4383 ;; space after objects, some special care must be given to white
4386 ;; The first function, `org-element-normalize-string', ensures any
4387 ;; string different from the empty string will end with a single
4388 ;; newline character.
4390 ;; The second function, `org-element-normalize-contents', removes
4391 ;; global indentation from the contents of the current element.
4393 (defun org-element-normalize-string (s)
4394 "Ensure string S ends with a single newline character.
4396 If S isn't a string return it unchanged. If S is the empty
4397 string, return it. Otherwise, return a new string with a single
4398 newline character at its end."
4400 ((not (stringp s
)) s
)
4402 (t (and (string-match "\\(\n[ \t]*\\)*\\'" s
)
4403 (replace-match "\n" nil nil s
)))))
4405 (defun org-element-normalize-contents (element &optional ignore-first
)
4406 "Normalize plain text in ELEMENT's contents.
4408 ELEMENT must only contain plain text and objects.
4410 If optional argument IGNORE-FIRST is non-nil, ignore first line's
4411 indentation to compute maximal common indentation.
4413 Return the normalized element that is element with global
4414 indentation removed from its contents. The function assumes that
4415 indentation is not done with TAB characters."
4416 (let* (ind-list ; for byte-compiler
4417 collect-inds
; for byte-compiler
4420 ;; Return list of indentations within BLOB. This is done by
4421 ;; walking recursively BLOB and updating IND-LIST along the
4422 ;; way. FIRST-FLAG is non-nil when the first string hasn't
4423 ;; been seen yet. It is required as this string is the only
4424 ;; one whose indentation doesn't happen after a newline
4426 (lambda (blob first-flag
)
4429 (when (and first-flag
(stringp object
))
4430 (setq first-flag nil
)
4431 (string-match "\\`\\( *\\)" object
)
4432 (let ((len (length (match-string 1 object
))))
4433 ;; An indentation of zero means no string will be
4434 ;; modified. Quit the process.
4435 (if (zerop len
) (throw 'zero
(setq ind-list nil
))
4436 (push len ind-list
))))
4440 ;; Avoid matching blank or empty lines.
4441 (while (and (string-match "\n\\( *\\)\\(.\\)" object start
)
4442 (not (equal (match-string 2 object
) " ")))
4443 (setq start
(match-end 0))
4444 (push (length (match-string 1 object
)) ind-list
))))
4445 ((memq (org-element-type object
) org-element-recursive-objects
)
4446 (funcall collect-inds object first-flag
))))
4447 (org-element-contents blob
))))))
4448 ;; Collect indentation list in ELEMENT. Possibly remove first
4449 ;; value if IGNORE-FIRST is non-nil.
4450 (catch 'zero
(funcall collect-inds element
(not ignore-first
)))
4451 (if (not ind-list
) element
4452 ;; Build ELEMENT back, replacing each string with the same
4453 ;; string minus common indentation.
4454 (let* (build ; For byte compiler.
4457 (lambda (blob mci first-flag
)
4458 ;; Return BLOB with all its strings indentation
4459 ;; shortened from MCI white spaces. FIRST-FLAG is
4460 ;; non-nil when the first string hasn't been seen
4465 (when (and first-flag
(stringp object
))
4466 (setq first-flag nil
)
4468 (replace-regexp-in-string
4469 (format "\\` \\{%d\\}" mci
) "" object
)))
4472 (replace-regexp-in-string
4473 (format "\n \\{%d\\}" mci
) "\n" object
))
4474 ((memq (org-element-type object
)
4475 org-element-recursive-objects
)
4476 (funcall build object mci first-flag
))
4478 (org-element-contents blob
)))
4480 (funcall build element
(apply 'min ind-list
) (not ignore-first
))))))
4486 ;; The first move is to implement a way to obtain the smallest element
4487 ;; containing point. This is the job of `org-element-at-point'. It
4488 ;; basically jumps back to the beginning of section containing point
4489 ;; and moves, element after element, with
4490 ;; `org-element--current-element' until the container is found. Note:
4491 ;; When using `org-element-at-point', secondary values are never
4492 ;; parsed since the function focuses on elements, not on objects.
4494 ;; At a deeper level, `org-element-context' lists all elements and
4495 ;; objects containing point.
4497 ;; `org-element-nested-p' and `org-element-swap-A-B' may be used
4498 ;; internally by navigation and manipulation tools.
4501 (defun org-element-at-point (&optional keep-trail
)
4502 "Determine closest element around point.
4504 Return value is a list like (TYPE PROPS) where TYPE is the type
4505 of the element and PROPS a plist of properties associated to the
4508 Possible types are defined in `org-element-all-elements'.
4509 Properties depend on element or object type, but always
4510 include :begin, :end, :parent and :post-blank properties.
4512 As a special case, if point is at the very beginning of a list or
4513 sub-list, returned element will be that list instead of the first
4514 item. In the same way, if point is at the beginning of the first
4515 row of a table, returned element will be the table instead of the
4518 If optional argument KEEP-TRAIL is non-nil, the function returns
4519 a list of of elements leading to element at point. The list's
4520 CAR is always the element at point. Following positions contain
4521 element's siblings, then parents, siblings of parents, until the
4522 first element of current section."
4523 (org-with-wide-buffer
4524 ;; If at an headline, parse it. It is the sole element that
4525 ;; doesn't require to know about context. Be sure to disallow
4526 ;; secondary string parsing, though.
4527 (if (org-with-limited-levels (org-at-heading-p))
4530 (if (not keep-trail
) (org-element-headline-parser (point-max) t
)
4531 (list (org-element-headline-parser (point-max) t
))))
4532 ;; Otherwise move at the beginning of the section containing
4534 (let ((origin (point))
4535 (end (save-excursion
4536 (org-with-limited-levels (outline-next-heading)) (point)))
4537 element type special-flag trail struct prevs parent
)
4538 (org-with-limited-levels
4539 (if (org-with-limited-levels (org-before-first-heading-p))
4540 (goto-char (point-min))
4541 (org-back-to-heading)
4543 (org-skip-whitespace)
4545 ;; Parse successively each element, skipping those ending
4546 ;; before original position.
4550 (org-element--current-element end
'element special-flag struct
)
4552 (org-element-put-property element
:parent parent
)
4553 (when keep-trail
(push element trail
))
4555 ;; 1. Skip any element ending before point. Also skip
4556 ;; element ending at point when we're sure that another
4557 ;; element has started.
4558 ((let ((elem-end (org-element-property :end element
)))
4559 (when (or (< elem-end origin
)
4560 (and (= elem-end origin
) (/= elem-end end
)))
4561 (goto-char elem-end
))))
4562 ;; 2. An element containing point is always the element at
4564 ((not (memq type org-element-greater-elements
))
4565 (throw 'exit
(if keep-trail trail element
)))
4566 ;; 3. At any other greater element type, if point is
4567 ;; within contents, move into it.
4569 (let ((cbeg (org-element-property :contents-begin element
))
4570 (cend (org-element-property :contents-end element
)))
4571 (if (or (not cbeg
) (not cend
) (> cbeg origin
) (< cend origin
)
4572 ;; Create an anchor for tables and plain lists:
4573 ;; when point is at the very beginning of these
4574 ;; elements, ignoring affiliated keywords,
4575 ;; target them instead of their contents.
4576 (and (= cbeg origin
) (memq type
'(plain-list table
)))
4577 ;; When point is at contents end, do not move
4578 ;; into elements with an explicit ending, but
4579 ;; return that element instead.
4580 (and (= cend origin
)
4583 drawer dynamic-block inlinetask item
4584 plain-list property-drawer quote-block
4586 (throw 'exit
(if keep-trail trail element
))
4587 (setq parent element
)
4590 (setq special-flag
'item
4591 struct
(org-element-property :structure element
)))
4592 (property-drawer (setq special-flag
'node-property
))
4593 (table (setq special-flag
'table-row
))
4594 (otherwise (setq special-flag nil
)))
4596 (goto-char cbeg
)))))))))))
4599 (defun org-element-context ()
4600 "Return closest element or object around point.
4602 Return value is a list like (TYPE PROPS) where TYPE is the type
4603 of the element or object and PROPS a plist of properties
4606 Possible types are defined in `org-element-all-elements' and
4607 `org-element-all-objects'. Properties depend on element or
4608 object type, but always include :begin, :end, :parent
4609 and :post-blank properties."
4610 (org-with-wide-buffer
4611 (let* ((origin (point))
4612 (element (org-element-at-point))
4613 (type (car element
))
4615 ;; Check if point is inside an element containing objects or at
4616 ;; a secondary string. In that case, move to beginning of the
4617 ;; element or secondary string and set END to the other side.
4618 (if (not (or (let ((post (org-element-property :post-affiliated element
)))
4619 (and post
(> post origin
)
4620 (< (org-element-property :begin element
) origin
)
4621 (progn (beginning-of-line)
4622 (looking-at org-element--affiliated-re
)
4623 (member (upcase (match-string 1))
4624 org-element-parsed-keywords
))
4625 ;; We're at an affiliated keyword. Change
4626 ;; type to retrieve correct restrictions.
4627 (setq type
'keyword
)
4628 ;; Determine if we're at main or dual value.
4629 (if (and (match-end 2) (<= origin
(match-end 2)))
4630 (progn (goto-char (match-beginning 2))
4631 (setq end
(match-end 2)))
4632 (goto-char (match-end 0))
4633 (setq end
(line-end-position)))))
4634 (and (eq type
'item
)
4635 (let ((tag (org-element-property :tag element
)))
4639 (search-forward tag
(point-at-eol))
4640 (goto-char (match-beginning 0))
4641 (and (>= origin
(point))
4643 ;; `1+' is required so some
4644 ;; successors can match
4645 ;; properly their object.
4646 (setq end
(1+ (match-end 0)))))))))
4647 (and (memq type
'(headline inlinetask
))
4648 (progn (beginning-of-line)
4649 (skip-chars-forward "* ")
4650 (setq end
(point-at-eol))))
4651 (and (memq type
'(paragraph table-row verse-block
))
4652 (let ((cbeg (org-element-property
4653 :contents-begin element
))
4654 (cend (org-element-property
4655 :contents-end element
)))
4656 (and (>= origin cbeg
)
4658 (progn (goto-char cbeg
) (setq end cend
)))))
4659 (and (eq type
'keyword
)
4660 (let ((key (org-element-property :key element
)))
4661 (and (member key org-element-document-properties
)
4662 (progn (beginning-of-line)
4663 (search-forward key
(line-end-position) t
)
4665 (setq end
(line-end-position))))))))
4667 (let ((restriction (org-element-restriction type
))
4671 (while (setq candidates
(org-element--get-next-object-candidates
4672 end restriction candidates
))
4673 (let ((closest-cand (rassq (apply 'min
(mapcar 'cdr candidates
))
4675 ;; If ORIGIN is before next object in element, there's
4676 ;; no point in looking further.
4677 (if (> (cdr closest-cand
) origin
) (throw 'exit parent
)
4679 (progn (goto-char (cdr closest-cand
))
4680 (funcall (intern (format "org-element-%s-parser"
4681 (car closest-cand
))))))
4682 (cbeg (org-element-property :contents-begin object
))
4683 (cend (org-element-property :contents-end object
)))
4685 ;; ORIGIN is after OBJECT, so skip it.
4686 ((< (org-element-property :end object
) origin
)
4687 (goto-char (org-element-property :end object
)))
4688 ;; ORIGIN is within a non-recursive object or at an
4689 ;; object boundaries: Return that object.
4690 ((or (not cbeg
) (> cbeg origin
) (< cend origin
))
4692 (org-element-put-property object
:parent parent
)))
4693 ;; Otherwise, move within current object and restrict
4694 ;; search to the end of its contents.
4696 (org-element-put-property object
:parent parent
)
4698 restriction
(org-element-restriction object
)
4702 (defsubst org-element-nested-p
(elem-A elem-B
)
4703 "Non-nil when elements ELEM-A and ELEM-B are nested."
4704 (let ((beg-A (org-element-property :begin elem-A
))
4705 (beg-B (org-element-property :begin elem-B
))
4706 (end-A (org-element-property :end elem-A
))
4707 (end-B (org-element-property :end elem-B
)))
4708 (or (and (>= beg-A beg-B
) (<= end-A end-B
))
4709 (and (>= beg-B beg-A
) (<= end-B end-A
)))))
4711 (defun org-element-swap-A-B (elem-A elem-B
)
4712 "Swap elements ELEM-A and ELEM-B.
4713 Assume ELEM-B is after ELEM-A in the buffer. Leave point at the
4715 (goto-char (org-element-property :begin elem-A
))
4716 ;; There are two special cases when an element doesn't start at bol:
4717 ;; the first paragraph in an item or in a footnote definition.
4718 (let ((specialp (not (bolp))))
4719 ;; Only a paragraph without any affiliated keyword can be moved at
4720 ;; ELEM-A position in such a situation. Note that the case of
4721 ;; a footnote definition is impossible: it cannot contain two
4722 ;; paragraphs in a row because it cannot contain a blank line.
4724 (or (not (eq (org-element-type elem-B
) 'paragraph
))
4725 (/= (org-element-property :begin elem-B
)
4726 (org-element-property :contents-begin elem-B
))))
4727 (error "Cannot swap elements"))
4728 ;; In a special situation, ELEM-A will have no indentation. We'll
4729 ;; give it ELEM-B's (which will in, in turn, have no indentation).
4730 (let* ((ind-B (when specialp
4731 (goto-char (org-element-property :begin elem-B
))
4732 (org-get-indentation)))
4733 (beg-A (org-element-property :begin elem-A
))
4734 (end-A (save-excursion
4735 (goto-char (org-element-property :end elem-A
))
4736 (skip-chars-backward " \r\t\n")
4738 (beg-B (org-element-property :begin elem-B
))
4739 (end-B (save-excursion
4740 (goto-char (org-element-property :end elem-B
))
4741 (skip-chars-backward " \r\t\n")
4743 ;; Store overlays responsible for visibility status. We
4744 ;; also need to store their boundaries as they will be
4745 ;; removed from buffer.
4748 (mapcar (lambda (ov) (list ov
(overlay-start ov
) (overlay-end ov
)))
4749 (overlays-in beg-A end-A
))
4750 (mapcar (lambda (ov) (list ov
(overlay-start ov
) (overlay-end ov
)))
4751 (overlays-in beg-B end-B
))))
4753 (body-A (buffer-substring beg-A end-A
))
4754 (body-B (delete-and-extract-region beg-B end-B
)))
4757 (setq body-B
(replace-regexp-in-string "\\`[ \t]*" "" body-B
))
4758 (org-indent-to-column ind-B
))
4760 ;; Restore ex ELEM-A overlays.
4761 (let ((offset (- beg-B beg-A
)))
4764 (car ov
) (+ (nth 1 ov
) offset
) (+ (nth 2 ov
) offset
)))
4767 (delete-region beg-A end-A
)
4769 ;; Restore ex ELEM-B overlays.
4772 (car ov
) (- (nth 1 ov
) offset
) (- (nth 2 ov
) offset
)))
4774 (goto-char (org-element-property :end elem-B
)))))
4776 (provide 'org-element
)
4779 ;; generated-autoload-file: "org-loaddefs.el"
4782 ;;; org-element.el ends here