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
))
122 (declare-function org-clocking-buffer
"org-clock" ())
126 ;;; Definitions And Rules
128 ;; Define elements, greater elements and specify recursive objects,
129 ;; along with the affiliated keywords recognized. Also set up
130 ;; restrictions on recursive objects combinations.
132 ;; These variables really act as a control center for the parsing
135 (defconst org-element-paragraph-separate
137 ;; Headlines, inlinetasks.
138 org-outline-regexp
"\\|"
139 ;; Footnote definitions.
140 "\\[\\(?:[0-9]+\\|fn:[-_[:word:]]+\\)\\]" "\\|"
146 ;; Tables (any type).
147 "\\(?:|\\|\\+-[-+]\\)" "\\|"
148 ;; Blocks (any type), Babel calls, drawers (any type),
149 ;; fixed-width areas and keywords. Note: this is only an
150 ;; indication and need some thorough check.
153 "-\\{5,\\}[ \t]*$" "\\|"
154 ;; LaTeX environments.
155 "\\\\begin{\\([A-Za-z0-9]+\\*?\\)}" "\\|"
156 ;; Planning and Clock lines.
157 (regexp-opt (list org-scheduled-string
163 (let ((term (case org-plain-list-ordered-item-terminator
164 (?\
) ")") (?.
"\\.") (otherwise "[.)]")))
165 (alpha (and org-alphabetical-lists
"\\|[A-Za-z]")))
166 (concat "\\(?:[-+*]\\|\\(?:[0-9]+" alpha
"\\)" term
"\\)"
167 "\\(?:[ \t]\\|$\\)"))
169 "Regexp to separate paragraphs in an Org buffer.
170 In the case of lines starting with \"#\" and \":\", this regexp
171 is not sufficient to know if point is at a paragraph ending. See
172 `org-element-paragraph-parser' for more information.")
174 (defconst org-element-all-elements
175 '(babel-call center-block clock comment comment-block diary-sexp drawer
176 dynamic-block example-block export-block fixed-width
177 footnote-definition headline horizontal-rule inlinetask item
178 keyword latex-environment node-property paragraph plain-list
179 planning property-drawer quote-block quote-section section
180 special-block src-block table table-row verse-block
)
181 "Complete list of element types.")
183 (defconst org-element-greater-elements
184 '(center-block drawer dynamic-block footnote-definition headline inlinetask
185 item plain-list property-drawer quote-block section
187 "List of recursive element types aka Greater Elements.")
189 (defconst org-element-all-successors
190 '(export-snippet footnote-reference inline-babel-call inline-src-block
191 latex-or-entity line-break link macro radio-target
192 statistics-cookie sub
/superscript table-cell target
193 text-markup timestamp
)
194 "Complete list of successors.")
196 (defconst org-element-object-successor-alist
197 '((subscript . sub
/superscript
) (superscript . sub
/superscript
)
198 (bold . text-markup
) (code . text-markup
) (italic . text-markup
)
199 (strike-through . text-markup
) (underline . text-markup
)
200 (verbatim . text-markup
) (entity . latex-or-entity
)
201 (latex-fragment . latex-or-entity
))
202 "Alist of translations between object type and successor name.
204 Sharing the same successor comes handy when, for example, the
205 regexp matching one object can also match the other object.")
207 (defconst org-element-all-objects
208 '(bold code entity export-snippet footnote-reference inline-babel-call
209 inline-src-block italic line-break latex-fragment link macro
210 radio-target statistics-cookie strike-through subscript superscript
211 table-cell target timestamp underline verbatim
)
212 "Complete list of object types.")
214 (defconst org-element-recursive-objects
215 '(bold italic link subscript radio-target strike-through superscript
216 table-cell underline
)
217 "List of recursive object types.")
219 (defconst org-element-block-name-alist
220 '(("CENTER" . org-element-center-block-parser
)
221 ("COMMENT" . org-element-comment-block-parser
)
222 ("EXAMPLE" . org-element-example-block-parser
)
223 ("QUOTE" . org-element-quote-block-parser
)
224 ("SRC" . org-element-src-block-parser
)
225 ("VERSE" . org-element-verse-block-parser
))
226 "Alist between block names and the associated parsing function.
227 Names must be uppercase. Any block whose name has no association
228 is parsed with `org-element-special-block-parser'.")
230 (defconst org-element-link-type-is-file
231 '("file" "file+emacs" "file+sys" "docview")
232 "List of link types equivalent to \"file\".
233 Only these types can accept search options and an explicit
234 application to open them.")
236 (defconst org-element-affiliated-keywords
237 '("CAPTION" "DATA" "HEADER" "HEADERS" "LABEL" "NAME" "PLOT" "RESNAME" "RESULT"
238 "RESULTS" "SOURCE" "SRCNAME" "TBLNAME")
239 "List of affiliated keywords as strings.
240 By default, all keywords setting attributes (i.e. \"ATTR_LATEX\")
241 are affiliated keywords and need not to be in this list.")
243 (defconst org-element--affiliated-re
244 (format "[ \t]*#\\+%s:"
245 ;; Regular affiliated keywords.
246 (format "\\(%s\\|ATTR_[-_A-Za-z0-9]+\\)\\(?:\\[\\(.*\\)\\]\\)?"
247 (regexp-opt org-element-affiliated-keywords
)))
248 "Regexp matching any affiliated keyword.
250 Keyword name is put in match group 1. Moreover, if keyword
251 belongs to `org-element-dual-keywords', put the dual value in
254 Don't modify it, set `org-element-affiliated-keywords' instead.")
256 (defconst org-element-keyword-translation-alist
257 '(("DATA" .
"NAME") ("LABEL" .
"NAME") ("RESNAME" .
"NAME")
258 ("SOURCE" .
"NAME") ("SRCNAME" .
"NAME") ("TBLNAME" .
"NAME")
259 ("RESULT" .
"RESULTS") ("HEADERS" .
"HEADER"))
260 "Alist of usual translations for keywords.
261 The key is the old name and the value the new one. The property
262 holding their value will be named after the translated name.")
264 (defconst org-element-multiple-keywords
'("CAPTION" "HEADER")
265 "List of affiliated keywords that can occur more than once in an element.
267 Their value will be consed into a list of strings, which will be
268 returned as the value of the property.
270 This list is checked after translations have been applied. See
271 `org-element-keyword-translation-alist'.
273 By default, all keywords setting attributes (i.e. \"ATTR_LATEX\")
274 allow multiple occurrences and need not to be in this list.")
276 (defconst org-element-parsed-keywords
'("CAPTION")
277 "List of affiliated keywords whose value can be parsed.
279 Their value will be stored as a secondary string: a list of
282 This list is checked after translations have been applied. See
283 `org-element-keyword-translation-alist'.")
285 (defconst org-element-dual-keywords
'("CAPTION" "RESULTS")
286 "List of affiliated keywords which can have a secondary value.
288 In Org syntax, they can be written with optional square brackets
289 before the colons. For example, RESULTS keyword can be
290 associated to a hash value with the following:
292 #+RESULTS[hash-string]: some-source
294 This list is checked after translations have been applied. See
295 `org-element-keyword-translation-alist'.")
297 (defconst org-element-document-properties
'("AUTHOR" "DATE" "TITLE")
298 "List of properties associated to the whole document.
299 Any keyword in this list will have its value parsed and stored as
300 a secondary string.")
302 (defconst org-element-object-restrictions
303 '((bold export-snippet inline-babel-call inline-src-block latex-or-entity link
304 radio-target sub
/superscript target text-markup timestamp
)
305 (footnote-reference export-snippet footnote-reference inline-babel-call
306 inline-src-block latex-or-entity line-break link macro
307 radio-target sub
/superscript target text-markup
309 (headline inline-babel-call inline-src-block latex-or-entity link macro
310 radio-target statistics-cookie sub
/superscript target text-markup
312 (inlinetask inline-babel-call inline-src-block latex-or-entity link macro
313 radio-target sub
/superscript target text-markup timestamp
)
314 (italic export-snippet inline-babel-call inline-src-block latex-or-entity
315 link radio-target sub
/superscript target text-markup timestamp
)
316 (item export-snippet footnote-reference inline-babel-call latex-or-entity
317 link macro radio-target sub
/superscript target text-markup
)
318 (keyword inline-babel-call inline-src-block latex-or-entity link macro
319 sub
/superscript text-markup
)
320 (link export-snippet inline-babel-call inline-src-block latex-or-entity link
321 sub
/superscript text-markup
)
322 (paragraph export-snippet footnote-reference inline-babel-call
323 inline-src-block latex-or-entity line-break link macro
324 radio-target statistics-cookie sub
/superscript target text-markup
326 (radio-target export-snippet latex-or-entity sub
/superscript
)
327 (strike-through export-snippet inline-babel-call inline-src-block
328 latex-or-entity link radio-target sub
/superscript target
329 text-markup timestamp
)
330 (subscript export-snippet inline-babel-call inline-src-block latex-or-entity
331 sub
/superscript target text-markup
)
332 (superscript export-snippet inline-babel-call inline-src-block
333 latex-or-entity sub
/superscript target text-markup
)
334 (table-cell export-snippet latex-or-entity link macro radio-target
335 sub
/superscript target text-markup timestamp
)
336 (table-row table-cell
)
337 (underline export-snippet inline-babel-call inline-src-block latex-or-entity
338 link radio-target sub
/superscript target text-markup timestamp
)
339 (verse-block footnote-reference inline-babel-call inline-src-block
340 latex-or-entity line-break link macro radio-target
341 sub
/superscript target text-markup timestamp
))
342 "Alist of objects restrictions.
344 CAR is an element or object type containing objects and CDR is
345 a list of successors that will be called within an element or
348 For example, in a `radio-target' object, one can only find
349 entities, export snippets, latex-fragments, subscript and
352 This alist also applies to secondary string. For example, an
353 `headline' type element doesn't directly contain objects, but
354 still has an entry since one of its properties (`:title') does.")
356 (defconst org-element-secondary-value-alist
357 '((headline .
:title
)
358 (inlinetask .
:title
)
360 (footnote-reference .
:inline-definition
))
361 "Alist between element types and location of secondary value.")
365 ;;; Accessors and Setters
367 ;; Provide four accessors: `org-element-type', `org-element-property'
368 ;; `org-element-contents' and `org-element-restriction'.
370 ;; Setter functions allow to modify elements by side effect. There is
371 ;; `org-element-put-property', `org-element-set-contents',
372 ;; `org-element-set-element' and `org-element-adopt-element'. Note
373 ;; that `org-element-set-element' and `org-element-adopt-elements' are
374 ;; higher level functions since also update `:parent' property.
376 (defsubst org-element-type
(element)
377 "Return type of ELEMENT.
379 The function returns the type of the element or object provided.
380 It can also return the following special value:
381 `plain-text' for a string
382 `org-data' for a complete document
383 nil in any other case."
385 ((not (consp element
)) (and (stringp element
) 'plain-text
))
386 ((symbolp (car element
)) (car element
))))
388 (defsubst org-element-property
(property element
)
389 "Extract the value from the PROPERTY of an ELEMENT."
390 (if (stringp element
) (get-text-property 0 property element
)
391 (plist-get (nth 1 element
) property
)))
393 (defsubst org-element-contents
(element)
394 "Extract contents from an ELEMENT."
395 (cond ((not (consp element
)) nil
)
396 ((symbolp (car element
)) (nthcdr 2 element
))
399 (defsubst org-element-restriction
(element)
400 "Return restriction associated to ELEMENT.
401 ELEMENT can be an element, an object or a symbol representing an
402 element or object type."
403 (cdr (assq (if (symbolp element
) element
(org-element-type element
))
404 org-element-object-restrictions
)))
406 (defsubst org-element-put-property
(element property value
)
407 "In ELEMENT set PROPERTY to VALUE.
408 Return modified element."
409 (if (stringp element
) (org-add-props element nil property value
)
410 (setcar (cdr element
) (plist-put (nth 1 element
) property value
))
413 (defsubst org-element-set-contents
(element &rest contents
)
414 "Set ELEMENT contents to CONTENTS.
415 Return modified element."
416 (cond ((not element
) (list contents
))
417 ((not (symbolp (car element
))) contents
)
418 ((cdr element
) (setcdr (cdr element
) contents
))
419 (t (nconc element contents
))))
421 (defsubst org-element-set-element
(old new
)
422 "Replace element or object OLD with element or object NEW.
423 The function takes care of setting `:parent' property for NEW."
424 ;; Since OLD is going to be changed into NEW by side-effect, first
425 ;; make sure that every element or object within NEW has OLD as
427 (mapc (lambda (blob) (org-element-put-property blob
:parent old
))
428 (org-element-contents new
))
429 ;; Transfer contents.
430 (apply 'org-element-set-contents old
(org-element-contents new
))
431 ;; Ensure NEW has same parent as OLD, then overwrite OLD properties
433 (org-element-put-property new
:parent
(org-element-property :parent old
))
434 (setcar (cdr old
) (nth 1 new
))
436 (setcar old
(car new
)))
438 (defsubst org-element-adopt-elements
(parent &rest children
)
439 "Append elements to the contents of another element.
441 PARENT is an element or object. CHILDREN can be elements,
442 objects, or a strings.
444 The function takes care of setting `:parent' property for CHILD.
445 Return parent element."
446 ;; Link every child to PARENT. If PARENT is nil, it is a secondary
447 ;; string: parent is the list itself.
448 (mapc (lambda (child)
449 (org-element-put-property child
:parent
(or parent children
)))
451 ;; Add CHILDREN at the end of PARENT contents.
453 (apply 'org-element-set-contents
455 (nconc (org-element-contents parent
) children
)))
456 ;; Return modified PARENT element.
457 (or parent children
))
463 ;; For each greater element type, we define a parser and an
466 ;; A parser returns the element or object as the list described above.
467 ;; Most of them accepts no argument. Though, exceptions exist. Hence
468 ;; every element containing a secondary string (see
469 ;; `org-element-secondary-value-alist') will accept an optional
470 ;; argument to toggle parsing of that secondary string. Moreover,
471 ;; `item' parser requires current list's structure as its first
474 ;; An interpreter accepts two arguments: the list representation of
475 ;; the element or object, and its contents. The latter may be nil,
476 ;; depending on the element or object considered. It returns the
477 ;; appropriate Org syntax, as a string.
479 ;; Parsing functions must follow the naming convention:
480 ;; org-element-TYPE-parser, where TYPE is greater element's type, as
481 ;; defined in `org-element-greater-elements'.
483 ;; Similarly, interpreting functions must follow the naming
484 ;; convention: org-element-TYPE-interpreter.
486 ;; With the exception of `headline' and `item' types, greater elements
487 ;; cannot contain other greater elements of their own type.
489 ;; Beside implementing a parser and an interpreter, adding a new
490 ;; greater element requires to tweak `org-element--current-element'.
491 ;; Moreover, the newly defined type must be added to both
492 ;; `org-element-all-elements' and `org-element-greater-elements'.
497 (defun org-element-center-block-parser (limit affiliated
)
498 "Parse a center block.
500 LIMIT bounds the search. AFFILIATED is a list of which CAR is
501 the buffer position at the beginning of the first affiliated
502 keyword and CDR is a plist of affiliated keywords along with
505 Return a list whose CAR is `center-block' and CDR is a plist
506 containing `:begin', `:end', `:hiddenp', `:contents-begin',
507 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
509 Assume point is at the beginning of the block."
510 (let ((case-fold-search t
))
511 (if (not (save-excursion
512 (re-search-forward "^[ \t]*#\\+END_CENTER[ \t]*$" limit t
)))
513 ;; Incomplete block: parse it as a paragraph.
514 (org-element-paragraph-parser limit affiliated
)
515 (let ((block-end-line (match-beginning 0)))
516 (let* ((begin (car affiliated
))
517 (post-affiliated (point))
518 ;; Empty blocks have no contents.
519 (contents-begin (progn (forward-line)
520 (and (< (point) block-end-line
)
522 (contents-end (and contents-begin block-end-line
))
523 (hidden (org-invisible-p2))
524 (pos-before-blank (progn (goto-char block-end-line
)
527 (end (save-excursion (skip-chars-forward " \r\t\n" limit
)
528 (skip-chars-backward " \t")
529 (if (bolp) (point) (line-end-position)))))
535 :contents-begin contents-begin
536 :contents-end contents-end
537 :post-blank
(count-lines pos-before-blank end
)
538 :post-affiliated post-affiliated
)
539 (cdr affiliated
))))))))
541 (defun org-element-center-block-interpreter (center-block contents
)
542 "Interpret CENTER-BLOCK element as Org syntax.
543 CONTENTS is the contents of the element."
544 (format "#+BEGIN_CENTER\n%s#+END_CENTER" contents
))
549 (defun org-element-drawer-parser (limit affiliated
)
552 LIMIT bounds the search. AFFILIATED is a list of which CAR is
553 the buffer position at the beginning of the first affiliated
554 keyword and CDR is a plist of affiliated keywords along with
557 Return a list whose CAR is `drawer' and CDR is a plist containing
558 `:drawer-name', `:begin', `:end', `:hiddenp', `:contents-begin',
559 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
561 Assume point is at beginning of drawer."
562 (let ((case-fold-search t
))
563 (if (not (save-excursion (re-search-forward "^[ \t]*:END:[ \t]*$" limit t
)))
564 ;; Incomplete drawer: parse it as a paragraph.
565 (org-element-paragraph-parser limit affiliated
)
567 (let* ((drawer-end-line (match-beginning 0))
568 (name (progn (looking-at org-drawer-regexp
)
569 (org-match-string-no-properties 1)))
570 (begin (car affiliated
))
571 (post-affiliated (point))
572 ;; Empty drawers have no contents.
573 (contents-begin (progn (forward-line)
574 (and (< (point) drawer-end-line
)
576 (contents-end (and contents-begin drawer-end-line
))
577 (hidden (org-invisible-p2))
578 (pos-before-blank (progn (goto-char drawer-end-line
)
581 (end (progn (skip-chars-forward " \r\t\n" limit
)
582 (skip-chars-backward " \t")
583 (if (bolp) (point) (line-end-position)))))
590 :contents-begin contents-begin
591 :contents-end contents-end
592 :post-blank
(count-lines pos-before-blank end
)
593 :post-affiliated post-affiliated
)
594 (cdr affiliated
))))))))
596 (defun org-element-drawer-interpreter (drawer contents
)
597 "Interpret DRAWER element as Org syntax.
598 CONTENTS is the contents of the element."
599 (format ":%s:\n%s:END:"
600 (org-element-property :drawer-name drawer
)
606 (defun org-element-dynamic-block-parser (limit affiliated
)
607 "Parse a dynamic block.
609 LIMIT bounds the search. AFFILIATED is a list of which CAR is
610 the buffer position at the beginning of the first affiliated
611 keyword and CDR is a plist of affiliated keywords along with
614 Return a list whose CAR is `dynamic-block' and CDR is a plist
615 containing `:block-name', `:begin', `:end', `:hiddenp',
616 `:contents-begin', `:contents-end', `:arguments', `:post-blank'
617 and `:post-affiliated' keywords.
619 Assume point is at beginning of dynamic block."
620 (let ((case-fold-search t
))
621 (if (not (save-excursion
622 (re-search-forward "^[ \t]*#\\+END:?[ \t]*$" limit t
)))
623 ;; Incomplete block: parse it as a paragraph.
624 (org-element-paragraph-parser limit affiliated
)
625 (let ((block-end-line (match-beginning 0)))
627 (let* ((name (progn (looking-at org-dblock-start-re
)
628 (org-match-string-no-properties 1)))
629 (arguments (org-match-string-no-properties 3))
630 (begin (car affiliated
))
631 (post-affiliated (point))
632 ;; Empty blocks have no contents.
633 (contents-begin (progn (forward-line)
634 (and (< (point) block-end-line
)
636 (contents-end (and contents-begin block-end-line
))
637 (hidden (org-invisible-p2))
638 (pos-before-blank (progn (goto-char block-end-line
)
641 (end (progn (skip-chars-forward " \r\t\n" limit
)
642 (skip-chars-backward " \t")
643 (if (bolp) (point) (line-end-position)))))
651 :contents-begin contents-begin
652 :contents-end contents-end
653 :post-blank
(count-lines pos-before-blank end
)
654 :post-affiliated post-affiliated
)
655 (cdr affiliated
)))))))))
657 (defun org-element-dynamic-block-interpreter (dynamic-block contents
)
658 "Interpret DYNAMIC-BLOCK element as Org syntax.
659 CONTENTS is the contents of the element."
660 (format "#+BEGIN: %s%s\n%s#+END:"
661 (org-element-property :block-name dynamic-block
)
662 (let ((args (org-element-property :arguments dynamic-block
)))
663 (and args
(concat " " args
)))
667 ;;;; Footnote Definition
669 (defun org-element-footnote-definition-parser (limit affiliated
)
670 "Parse a footnote definition.
672 LIMIT bounds the search. AFFILIATED is a list of which CAR is
673 the buffer position at the beginning of the first affiliated
674 keyword and CDR is a plist of affiliated keywords along with
677 Return a list whose CAR is `footnote-definition' and CDR is
678 a plist containing `:label', `:begin' `:end', `:contents-begin',
679 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
681 Assume point is at the beginning of the footnote definition."
683 (let* ((label (progn (looking-at org-footnote-definition-re
)
684 (org-match-string-no-properties 1)))
685 (begin (car affiliated
))
686 (post-affiliated (point))
687 (ending (save-excursion
691 (concat org-outline-regexp-bol
"\\|"
692 org-footnote-definition-re
"\\|"
693 "^[ \t]*$") limit
'move
))
696 (contents-begin (progn (search-forward "]")
697 (skip-chars-forward " \r\t\n" ending
)
698 (and (/= (point) ending
) (point))))
699 (contents-end (and contents-begin ending
))
700 (end (progn (goto-char ending
)
701 (skip-chars-forward " \r\t\n" limit
)
702 (skip-chars-backward " \t")
703 (if (bolp) (point) (line-end-position)))))
704 (list 'footnote-definition
709 :contents-begin contents-begin
710 :contents-end contents-end
711 :post-blank
(count-lines ending end
)
712 :post-affiliated post-affiliated
)
713 (cdr affiliated
))))))
715 (defun org-element-footnote-definition-interpreter (footnote-definition contents
)
716 "Interpret FOOTNOTE-DEFINITION element as Org syntax.
717 CONTENTS is the contents of the footnote-definition."
718 (concat (format "[%s]" (org-element-property :label footnote-definition
))
725 (defun org-element-headline-parser (limit &optional raw-secondary-p
)
728 Return a list whose CAR is `headline' and CDR is a plist
729 containing `:raw-value', `:title', `:begin', `:end',
730 `:pre-blank', `:hiddenp', `:contents-begin' and `:contents-end',
731 `:level', `:priority', `:tags', `:todo-keyword',`:todo-type',
732 `:scheduled', `:deadline', `:closed', `:clockedp', `:quotedp',
733 `:archivedp', `:commentedp' and `:footnote-section-p' keywords.
735 The plist also contains any property set in the property drawer,
736 with its name in lowercase, the underscores replaced with hyphens
737 and colons at the beginning (i.e. `:custom-id').
739 When RAW-SECONDARY-P is non-nil, headline's title will not be
740 parsed as a secondary string, but as a plain string instead.
742 Assume point is at beginning of the headline."
744 (let* ((components (org-heading-components))
745 (level (nth 1 components
))
746 (todo (nth 2 components
))
748 (and todo
(if (member todo org-done-keywords
) 'done
'todo
)))
749 (tags (let ((raw-tags (nth 5 components
)))
750 (and raw-tags
(org-split-string raw-tags
":"))))
751 (raw-value (or (nth 4 components
) ""))
753 (let ((case-fold-search nil
))
754 (string-match (format "^%s\\( \\|$\\)" org-quote-string
)
757 (let ((case-fold-search nil
))
758 (string-match (format "^%s\\( \\|$\\)" org-comment-string
)
760 (archivedp (member org-archive-tag tags
))
761 (footnote-section-p (and org-footnote-section
762 (string= org-footnote-section raw-value
)))
763 ;; Normalize property names: ":SOME_PROP:" becomes
772 (replace-regexp-in-string
773 "_" "-" (downcase (car p
)))))
775 (org-entry-properties nil
'standard
))
778 ;; Read time properties on the line below the headline.
780 (when (progn (forward-line)
781 (looking-at org-planning-or-clock-line-re
))
782 (let ((end (line-end-position)) plist
)
783 (while (re-search-forward
784 org-keyword-time-not-clock-regexp end t
)
785 (goto-char (match-end 1))
786 (skip-chars-forward " \t")
787 (let ((keyword (match-string 1))
788 (time (org-element-timestamp-parser)))
789 (cond ((equal keyword org-scheduled-string
)
790 (setq plist
(plist-put plist
:scheduled time
)))
791 ((equal keyword org-deadline-string
)
792 (setq plist
(plist-put plist
:deadline time
)))
793 (t (setq plist
(plist-put plist
:closed time
))))))
796 (end (save-excursion (goto-char (org-end-of-subtree t t
))))
797 (pos-after-head (progn (forward-line) (point)))
798 (contents-begin (save-excursion
799 (skip-chars-forward " \r\t\n" end
)
800 (and (/= (point) end
) (line-beginning-position))))
801 (hidden (org-invisible-p2))
802 (contents-end (and contents-begin
803 (progn (goto-char end
)
804 (skip-chars-backward " \r\t\n")
807 (clockedp (and (featurep 'org-clock
)
808 (eq (org-clocking-buffer)
809 (or (buffer-base-buffer) (current-buffer)))
811 (goto-char (marker-position org-clock-marker
))
812 (org-back-to-heading t
)
813 (= (point) begin
)))))
814 ;; Clean RAW-VALUE from any quote or comment string.
815 (when (or quotedp commentedp
)
816 (let ((case-fold-search nil
))
818 (replace-regexp-in-string
820 (regexp-opt (list org-quote-string org-comment-string
))
824 ;; Clean TAGS from archive tag, if any.
825 (when archivedp
(setq tags
(delete org-archive-tag tags
)))
829 (list :raw-value raw-value
833 (if (not contents-begin
) 0
834 (count-lines pos-after-head contents-begin
))
836 :contents-begin contents-begin
837 :contents-end contents-end
839 :priority
(nth 3 components
)
843 :post-blank
(count-lines
844 (if (not contents-end
) pos-after-head
845 (goto-char contents-end
)
849 :footnote-section-p footnote-section-p
852 :commentedp commentedp
856 (org-element-put-property
858 (if raw-secondary-p raw-value
859 (org-element-parse-secondary-string
860 raw-value
(org-element-restriction 'headline
) headline
)))))))
862 (defun org-element-headline-interpreter (headline contents
)
863 "Interpret HEADLINE element as Org syntax.
864 CONTENTS is the contents of the element."
865 (let* ((level (org-element-property :level headline
))
866 (todo (org-element-property :todo-keyword headline
))
867 (priority (org-element-property :priority headline
))
868 (title (org-element-interpret-data
869 (org-element-property :title headline
)))
870 (tags (let ((tag-list (if (org-element-property :archivedp headline
)
871 (cons org-archive-tag
872 (org-element-property :tags headline
))
873 (org-element-property :tags headline
))))
875 (format ":%s:" (mapconcat 'identity tag-list
":")))))
876 (commentedp (org-element-property :commentedp headline
))
877 (quotedp (org-element-property :quotedp headline
))
878 (pre-blank (or (org-element-property :pre-blank headline
) 0))
879 (heading (concat (make-string level ?
*)
880 (and todo
(concat " " todo
))
881 (and quotedp
(concat " " org-quote-string
))
882 (and commentedp
(concat " " org-comment-string
))
884 (format " [#%s]" (char-to-string priority
)))
885 (cond ((and org-footnote-section
886 (org-element-property
887 :footnote-section-p headline
))
888 (concat " " org-footnote-section
))
889 (title (concat " " title
))))))
894 ((zerop org-tags-column
) (format " %s" tags
))
895 ((< org-tags-column
0)
898 (max (- (+ org-tags-column
(length heading
) (length tags
))) 1)
903 (make-string (max (- org-tags-column
(length heading
)) 1) ?
)
905 (make-string (1+ pre-blank
) 10)
911 (defun org-element-inlinetask-parser (limit &optional raw-secondary-p
)
912 "Parse an inline task.
914 Return a list whose CAR is `inlinetask' and CDR is a plist
915 containing `:title', `:begin', `:end', `:hiddenp',
916 `:contents-begin' and `:contents-end', `:level', `:priority',
917 `:raw-value', `:tags', `:todo-keyword', `:todo-type',
918 `:scheduled', `:deadline', `:clockedp', `:closed' and
919 `:post-blank' keywords.
921 The plist also contains any property set in the property drawer,
922 with its name in lowercase, the underscores replaced with hyphens
923 and colons at the beginning (i.e. `:custom-id').
925 When optional argument RAW-SECONDARY-P is non-nil, inline-task's
926 title will not be parsed as a secondary string, but as a plain
929 Assume point is at beginning of the inline task."
931 (let* ((begin (point))
932 (components (org-heading-components))
933 (todo (nth 2 components
))
935 (if (member todo org-done-keywords
) 'done
'todo
)))
936 (tags (let ((raw-tags (nth 5 components
)))
937 (and raw-tags
(org-split-string raw-tags
":"))))
938 (raw-value (or (nth 4 components
) ""))
939 ;; Normalize property names: ":SOME_PROP:" becomes
948 (replace-regexp-in-string
949 "_" "-" (downcase (car p
)))))
951 (org-entry-properties nil
'standard
))
954 ;; Read time properties on the line below the inlinetask
957 (when (progn (forward-line)
958 (looking-at org-planning-or-clock-line-re
))
959 (let ((end (line-end-position)) plist
)
960 (while (re-search-forward
961 org-keyword-time-not-clock-regexp end t
)
962 (goto-char (match-end 1))
963 (skip-chars-forward " \t")
964 (let ((keyword (match-string 1))
965 (time (org-element-timestamp-parser)))
966 (cond ((equal keyword org-scheduled-string
)
967 (setq plist
(plist-put plist
:scheduled time
)))
968 ((equal keyword org-deadline-string
)
969 (setq plist
(plist-put plist
:deadline time
)))
970 (t (setq plist
(plist-put plist
:closed time
))))))
972 (task-end (save-excursion
974 (and (re-search-forward "^\\*+ END" limit t
)
975 (match-beginning 0))))
976 (clockedp (and (featurep 'org-clock
)
977 (eq (org-clocking-buffer)
978 (or (buffer-base-buffer) (current-buffer)))
979 (let ((clock (marker-position org-clock-marker
)))
980 (and (> clock begin
) (< clock task-end
)))))
981 (contents-begin (progn (forward-line)
982 (and task-end
(< (point) task-end
) (point))))
983 (hidden (and contents-begin
(org-invisible-p2)))
984 (contents-end (and contents-begin task-end
))
985 (before-blank (if (not task-end
) (point)
989 (end (progn (skip-chars-forward " \r\t\n" limit
)
990 (skip-chars-backward " \t")
991 (if (bolp) (point) (line-end-position))))
995 (list :raw-value raw-value
999 :contents-begin contents-begin
1000 :contents-end contents-end
1001 :level
(nth 1 components
)
1002 :priority
(nth 3 components
)
1005 :todo-type todo-type
1007 :post-blank
(count-lines before-blank end
))
1010 (org-element-put-property
1012 (if raw-secondary-p raw-value
1013 (org-element-parse-secondary-string
1015 (org-element-restriction 'inlinetask
)
1018 (defun org-element-inlinetask-interpreter (inlinetask contents
)
1019 "Interpret INLINETASK element as Org syntax.
1020 CONTENTS is the contents of inlinetask."
1021 (let* ((level (org-element-property :level inlinetask
))
1022 (todo (org-element-property :todo-keyword inlinetask
))
1023 (priority (org-element-property :priority inlinetask
))
1024 (title (org-element-interpret-data
1025 (org-element-property :title inlinetask
)))
1026 (tags (let ((tag-list (org-element-property :tags inlinetask
)))
1028 (format ":%s:" (mapconcat 'identity tag-list
":")))))
1029 (task (concat (make-string level ?
*)
1030 (and todo
(concat " " todo
))
1032 (format " [#%s]" (char-to-string priority
)))
1033 (and title
(concat " " title
)))))
1038 ((zerop org-tags-column
) (format " %s" tags
))
1039 ((< org-tags-column
0)
1042 (max (- (+ org-tags-column
(length task
) (length tags
))) 1)
1047 (make-string (max (- org-tags-column
(length task
)) 1) ?
)
1049 ;; Prefer degenerate inlinetasks when there are no
1054 (make-string level ?
*) " END")))))
1059 (defun org-element-item-parser (limit struct
&optional raw-secondary-p
)
1062 STRUCT is the structure of the plain list.
1064 Return a list whose CAR is `item' and CDR is a plist containing
1065 `:bullet', `:begin', `:end', `:contents-begin', `:contents-end',
1066 `:checkbox', `:counter', `:tag', `:structure', `:hiddenp' and
1067 `:post-blank' keywords.
1069 When optional argument RAW-SECONDARY-P is non-nil, item's tag, if
1070 any, will not be parsed as a secondary string, but as a plain
1073 Assume point is at the beginning of the item."
1076 (looking-at org-list-full-item-re
)
1077 (let* ((begin (point))
1078 (bullet (org-match-string-no-properties 1))
1079 (checkbox (let ((box (org-match-string-no-properties 3)))
1080 (cond ((equal "[ ]" box
) 'off
)
1081 ((equal "[X]" box
) 'on
)
1082 ((equal "[-]" box
) 'trans
))))
1083 (counter (let ((c (org-match-string-no-properties 2)))
1087 ((string-match "[A-Za-z]" c
)
1088 (- (string-to-char (upcase (match-string 0 c
)))
1090 ((string-match "[0-9]+" c
)
1091 (string-to-number (match-string 0 c
)))))))
1092 (end (save-excursion (goto-char (org-list-get-item-end begin struct
))
1093 (unless (bolp) (forward-line))
1097 ;; Ignore tags in un-ordered lists: they are just
1098 ;; a part of item's body.
1099 (if (and (match-beginning 4)
1100 (save-match-data (string-match "[.)]" bullet
)))
1103 (skip-chars-forward " \r\t\n" limit
)
1104 ;; If first line isn't empty, contents really start
1105 ;; at the text after item's meta-data.
1106 (if (= (point-at-bol) begin
) (point) (point-at-bol))))
1107 (hidden (progn (forward-line)
1108 (and (not (= (point) end
)) (org-invisible-p2))))
1109 (contents-end (progn (goto-char end
)
1110 (skip-chars-backward " \r\t\n")
1115 (list :bullet bullet
1118 ;; CONTENTS-BEGIN and CONTENTS-END may be
1119 ;; mixed up in the case of an empty item
1120 ;; separated from the next by a blank line.
1121 ;; Thus ensure the former is always the
1123 :contents-begin
(min contents-begin contents-end
)
1124 :contents-end
(max contents-begin contents-end
)
1129 :post-blank
(count-lines contents-end end
)))))
1130 (org-element-put-property
1132 (let ((raw-tag (org-list-get-tag begin struct
)))
1134 (if raw-secondary-p raw-tag
1135 (org-element-parse-secondary-string
1136 raw-tag
(org-element-restriction 'item
) item
))))))))
1138 (defun org-element-item-interpreter (item contents
)
1139 "Interpret ITEM element as Org syntax.
1140 CONTENTS is the contents of the element."
1141 (let* ((bullet (org-list-bullet-string (org-element-property :bullet item
)))
1142 (checkbox (org-element-property :checkbox item
))
1143 (counter (org-element-property :counter item
))
1144 (tag (let ((tag (org-element-property :tag item
)))
1145 (and tag
(org-element-interpret-data tag
))))
1146 ;; Compute indentation.
1147 (ind (make-string (length bullet
) 32))
1148 (item-starts-with-par-p
1149 (eq (org-element-type (car (org-element-contents item
)))
1154 (and counter
(format "[@%d] " counter
))
1159 (and tag
(format "%s :: " tag
))
1160 (let ((contents (replace-regexp-in-string
1161 "\\(^\\)[ \t]*\\S-" ind contents nil nil
1)))
1162 (if item-starts-with-par-p
(org-trim contents
)
1163 (concat "\n" contents
))))))
1168 (defun org-element-plain-list-parser (limit affiliated structure
)
1169 "Parse a plain list.
1171 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1172 the buffer position at the beginning of the first affiliated
1173 keyword and CDR is a plist of affiliated keywords along with
1174 their value. STRUCTURE is the structure of the plain list being
1177 Return a list whose CAR is `plain-list' and CDR is a plist
1178 containing `:type', `:begin', `:end', `:contents-begin' and
1179 `:contents-end', `:structure', `:post-blank' and
1180 `:post-affiliated' keywords.
1182 Assume point is at the beginning of the list."
1184 (let* ((struct (or structure
(org-list-struct)))
1185 (prevs (org-list-prevs-alist struct
))
1186 (parents (org-list-parents-alist struct
))
1187 (type (org-list-get-list-type (point) struct prevs
))
1188 (contents-begin (point))
1189 (begin (car affiliated
))
1191 (progn (goto-char (org-list-get-list-end (point) struct prevs
))
1192 (unless (bolp) (forward-line))
1194 (end (progn (skip-chars-forward " \r\t\n" limit
)
1195 (skip-chars-backward " \t")
1196 (if (bolp) (point) (line-end-position)))))
1203 :contents-begin contents-begin
1204 :contents-end contents-end
1206 :post-blank
(count-lines contents-end end
)
1207 :post-affiliated contents-begin
)
1208 (cdr affiliated
))))))
1210 (defun org-element-plain-list-interpreter (plain-list contents
)
1211 "Interpret PLAIN-LIST element as Org syntax.
1212 CONTENTS is the contents of the element."
1215 (goto-char (point-min))
1220 ;;;; Property Drawer
1222 (defun org-element-property-drawer-parser (limit affiliated
)
1223 "Parse a property drawer.
1225 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1226 the buffer position at the beginning of the first affiliated
1227 keyword and CDR is a plist of affiliated keywords along with
1230 Return a list whose CAR is `property-drawer' and CDR is a plist
1231 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1232 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
1234 Assume point is at the beginning of the property drawer."
1236 (let ((case-fold-search t
))
1237 (if (not (save-excursion
1238 (re-search-forward "^[ \t]*:END:[ \t]*$" limit t
)))
1239 ;; Incomplete drawer: parse it as a paragraph.
1240 (org-element-paragraph-parser limit affiliated
)
1242 (let* ((drawer-end-line (match-beginning 0))
1243 (begin (car affiliated
))
1244 (post-affiliated (point))
1245 (contents-begin (progn (forward-line)
1246 (and (< (point) drawer-end-line
)
1248 (contents-end (and contents-begin drawer-end-line
))
1249 (hidden (org-invisible-p2))
1250 (pos-before-blank (progn (goto-char drawer-end-line
)
1253 (end (progn (skip-chars-forward " \r\t\n" limit
)
1254 (skip-chars-backward " \t")
1255 (if (bolp) (point) (line-end-position)))))
1256 (list 'property-drawer
1261 :contents-begin contents-begin
1262 :contents-end contents-end
1263 :post-blank
(count-lines pos-before-blank end
)
1264 :post-affiliated post-affiliated
)
1265 (cdr affiliated
)))))))))
1267 (defun org-element-property-drawer-interpreter (property-drawer contents
)
1268 "Interpret PROPERTY-DRAWER element as Org syntax.
1269 CONTENTS is the properties within the drawer."
1270 (format ":PROPERTIES:\n%s:END:" contents
))
1275 (defun org-element-quote-block-parser (limit affiliated
)
1276 "Parse a quote block.
1278 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1279 the buffer position at the beginning of the first affiliated
1280 keyword and CDR is a plist of affiliated keywords along with
1283 Return a list whose CAR is `quote-block' and CDR is a plist
1284 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1285 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
1287 Assume point is at the beginning of the block."
1288 (let ((case-fold-search t
))
1289 (if (not (save-excursion
1290 (re-search-forward "^[ \t]*#\\+END_QUOTE[ \t]*$" limit t
)))
1291 ;; Incomplete block: parse it as a paragraph.
1292 (org-element-paragraph-parser limit affiliated
)
1293 (let ((block-end-line (match-beginning 0)))
1295 (let* ((begin (car affiliated
))
1296 (post-affiliated (point))
1297 ;; Empty blocks have no contents.
1298 (contents-begin (progn (forward-line)
1299 (and (< (point) block-end-line
)
1301 (contents-end (and contents-begin block-end-line
))
1302 (hidden (org-invisible-p2))
1303 (pos-before-blank (progn (goto-char block-end-line
)
1306 (end (progn (skip-chars-forward " \r\t\n" limit
)
1307 (skip-chars-backward " \t")
1308 (if (bolp) (point) (line-end-position)))))
1314 :contents-begin contents-begin
1315 :contents-end contents-end
1316 :post-blank
(count-lines pos-before-blank end
)
1317 :post-affiliated post-affiliated
)
1318 (cdr affiliated
)))))))))
1320 (defun org-element-quote-block-interpreter (quote-block contents
)
1321 "Interpret QUOTE-BLOCK element as Org syntax.
1322 CONTENTS is the contents of the element."
1323 (format "#+BEGIN_QUOTE\n%s#+END_QUOTE" contents
))
1328 (defun org-element-section-parser (limit)
1331 LIMIT bounds the search.
1333 Return a list whose CAR is `section' and CDR is a plist
1334 containing `:begin', `:end', `:contents-begin', `contents-end'
1335 and `:post-blank' keywords."
1337 ;; Beginning of section is the beginning of the first non-blank
1338 ;; line after previous headline.
1339 (let ((begin (point))
1340 (end (progn (org-with-limited-levels (outline-next-heading))
1342 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
1348 :contents-begin begin
1349 :contents-end pos-before-blank
1350 :post-blank
(count-lines pos-before-blank end
))))))
1352 (defun org-element-section-interpreter (section contents
)
1353 "Interpret SECTION element as Org syntax.
1354 CONTENTS is the contents of the element."
1360 (defun org-element-special-block-parser (limit affiliated
)
1361 "Parse a special block.
1363 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1364 the buffer position at the beginning of the first affiliated
1365 keyword and CDR is a plist of affiliated keywords along with
1368 Return a list whose CAR is `special-block' and CDR is a plist
1369 containing `:type', `:begin', `:end', `:hiddenp',
1370 `:contents-begin', `:contents-end', `:post-blank' and
1371 `:post-affiliated' keywords.
1373 Assume point is at the beginning of the block."
1374 (let* ((case-fold-search t
)
1375 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(S-+\\)")
1376 (upcase (match-string-no-properties 1)))))
1377 (if (not (save-excursion
1379 (format "^[ \t]*#\\+END_%s[ \t]*$" type
) limit t
)))
1380 ;; Incomplete block: parse it as a paragraph.
1381 (org-element-paragraph-parser limit affiliated
)
1382 (let ((block-end-line (match-beginning 0)))
1384 (let* ((begin (car affiliated
))
1385 (post-affiliated (point))
1386 ;; Empty blocks have no contents.
1387 (contents-begin (progn (forward-line)
1388 (and (< (point) block-end-line
)
1390 (contents-end (and contents-begin block-end-line
))
1391 (hidden (org-invisible-p2))
1392 (pos-before-blank (progn (goto-char block-end-line
)
1395 (end (progn (skip-chars-forward " \r\t\n" limit
)
1396 (skip-chars-backward " \t")
1397 (if (bolp) (point) (line-end-position)))))
1398 (list 'special-block
1404 :contents-begin contents-begin
1405 :contents-end contents-end
1406 :post-blank
(count-lines pos-before-blank end
)
1407 :post-affiliated post-affiliated
)
1408 (cdr affiliated
)))))))))
1410 (defun org-element-special-block-interpreter (special-block contents
)
1411 "Interpret SPECIAL-BLOCK element as Org syntax.
1412 CONTENTS is the contents of the element."
1413 (let ((block-type (org-element-property :type special-block
)))
1414 (format "#+BEGIN_%s\n%s#+END_%s" block-type contents block-type
)))
1420 ;; For each element, a parser and an interpreter are also defined.
1421 ;; Both follow the same naming convention used for greater elements.
1423 ;; Also, as for greater elements, adding a new element type is done
1424 ;; through the following steps: implement a parser and an interpreter,
1425 ;; tweak `org-element--current-element' so that it recognizes the new
1426 ;; type and add that new type to `org-element-all-elements'.
1428 ;; As a special case, when the newly defined type is a block type,
1429 ;; `org-element-block-name-alist' has to be modified accordingly.
1434 (defun org-element-babel-call-parser (limit affiliated
)
1435 "Parse a babel call.
1437 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1438 the buffer position at the beginning of the first affiliated
1439 keyword and CDR is a plist of affiliated keywords along with
1442 Return a list whose CAR is `babel-call' and CDR is a plist
1443 containing `:begin', `:end', `:info', `:post-blank' and
1444 `:post-affiliated' as keywords."
1446 (let ((case-fold-search t
)
1447 (info (progn (looking-at org-babel-block-lob-one-liner-regexp
)
1448 (org-babel-lob-get-info)))
1449 (begin (car affiliated
))
1450 (post-affiliated (point))
1451 (pos-before-blank (progn (forward-line) (point)))
1452 (end (progn (skip-chars-forward " \r\t\n" limit
)
1453 (skip-chars-backward " \t")
1454 (if (bolp) (point) (line-end-position)))))
1460 :post-blank
(count-lines pos-before-blank end
)
1461 :post-affiliated post-affiliated
)
1462 (cdr affiliated
))))))
1464 (defun org-element-babel-call-interpreter (babel-call contents
)
1465 "Interpret BABEL-CALL element as Org syntax.
1467 (let* ((babel-info (org-element-property :info babel-call
))
1468 (main (car babel-info
))
1469 (post-options (nth 1 babel-info
)))
1471 (if (not (string-match "\\[\\(\\[.*?\\]\\)\\]" main
)) main
1472 ;; Remove redundant square brackets.
1473 (replace-match (match-string 1 main
) nil nil main
))
1474 (and post-options
(format "[%s]" post-options
)))))
1479 (defun org-element-clock-parser (limit)
1482 LIMIT bounds the search.
1484 Return a list whose CAR is `clock' and CDR is a plist containing
1485 `:status', `:value', `:time', `:begin', `:end' and `:post-blank'
1488 (let* ((case-fold-search nil
)
1490 (value (progn (search-forward org-clock-string
(line-end-position) t
)
1491 (skip-chars-forward " \t")
1492 (org-element-timestamp-parser)))
1493 (duration (and (search-forward " => " (line-end-position) t
)
1494 (progn (skip-chars-forward " \t")
1495 (looking-at "\\(\\S-+\\)[ \t]*$"))
1496 (org-match-string-no-properties 1)))
1497 (status (if duration
'closed
'running
))
1498 (post-blank (let ((before-blank (progn (forward-line) (point))))
1499 (skip-chars-forward " \r\t\n" limit
)
1500 (skip-chars-backward " \t")
1501 (unless (bolp) (end-of-line))
1502 (count-lines before-blank
(point))))
1505 (list :status status
1510 :post-blank post-blank
)))))
1512 (defun org-element-clock-interpreter (clock contents
)
1513 "Interpret CLOCK element as Org syntax.
1515 (concat org-clock-string
" "
1516 (org-element-timestamp-interpreter
1517 (org-element-property :value clock
) nil
)
1518 (let ((duration (org-element-property :duration clock
)))
1523 (org-split-string duration
":")))))))
1528 (defun org-element-comment-parser (limit affiliated
)
1531 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1532 the buffer position at the beginning of the first affiliated
1533 keyword and CDR is a plist of affiliated keywords along with
1536 Return a list whose CAR is `comment' and CDR is a plist
1537 containing `:begin', `:end', `:value', `:post-blank',
1538 `:post-affiliated' keywords.
1540 Assume point is at comment beginning."
1542 (let* ((begin (car affiliated
))
1543 (post-affiliated (point))
1544 (value (prog2 (looking-at "[ \t]*# ?")
1545 (buffer-substring-no-properties
1546 (match-end 0) (line-end-position))
1549 ;; Get comments ending.
1551 (while (and (< (point) limit
) (looking-at "[ \t]*#\\( \\|$\\)"))
1552 ;; Accumulate lines without leading hash and first
1557 (buffer-substring-no-properties
1558 (match-end 0) (line-end-position))))
1561 (end (progn (goto-char com-end
)
1562 (skip-chars-forward " \r\t\n" limit
)
1563 (skip-chars-backward " \t")
1564 (if (bolp) (point) (line-end-position)))))
1570 :post-blank
(count-lines com-end end
)
1571 :post-affiliated post-affiliated
)
1572 (cdr affiliated
))))))
1574 (defun org-element-comment-interpreter (comment contents
)
1575 "Interpret COMMENT element as Org syntax.
1577 (replace-regexp-in-string "^" "# " (org-element-property :value comment
)))
1582 (defun org-element-comment-block-parser (limit affiliated
)
1583 "Parse an export block.
1585 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1586 the buffer position at the beginning of the first affiliated
1587 keyword and CDR is a plist of affiliated keywords along with
1590 Return a list whose CAR is `comment-block' and CDR is a plist
1591 containing `:begin', `:end', `:hiddenp', `:value', `:post-blank'
1592 and `:post-affiliated' keywords.
1594 Assume point is at comment block beginning."
1595 (let ((case-fold-search t
))
1596 (if (not (save-excursion
1597 (re-search-forward "^[ \t]*#\\+END_COMMENT[ \t]*$" limit t
)))
1598 ;; Incomplete block: parse it as a paragraph.
1599 (org-element-paragraph-parser limit affiliated
)
1600 (let ((contents-end (match-beginning 0)))
1602 (let* ((begin (car affiliated
))
1603 (post-affiliated (point))
1604 (contents-begin (progn (forward-line) (point)))
1605 (hidden (org-invisible-p2))
1606 (pos-before-blank (progn (goto-char contents-end
)
1609 (end (progn (skip-chars-forward " \r\t\n" limit
)
1610 (skip-chars-backward " \t")
1611 (if (bolp) (point) (line-end-position))))
1612 (value (buffer-substring-no-properties
1613 contents-begin contents-end
)))
1614 (list 'comment-block
1620 :post-blank
(count-lines pos-before-blank end
)
1621 :post-affiliated post-affiliated
)
1622 (cdr affiliated
)))))))))
1624 (defun org-element-comment-block-interpreter (comment-block contents
)
1625 "Interpret COMMENT-BLOCK element as Org syntax.
1627 (format "#+BEGIN_COMMENT\n%s#+END_COMMENT"
1628 (org-remove-indentation (org-element-property :value comment-block
))))
1633 (defun org-element-diary-sexp-parser (limit affiliated
)
1634 "Parse a diary sexp.
1636 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1637 the buffer position at the beginning of the first affiliated
1638 keyword and CDR is a plist of affiliated keywords along with
1641 Return a list whose CAR is `diary-sexp' and CDR is a plist
1642 containing `:begin', `:end', `:value', `:post-blank' and
1643 `:post-affiliated' keywords."
1645 (let ((begin (car affiliated
))
1646 (post-affiliated (point))
1647 (value (progn (looking-at "\\(%%(.*\\)[ \t]*$")
1648 (org-match-string-no-properties 1)))
1649 (pos-before-blank (progn (forward-line) (point)))
1650 (end (progn (skip-chars-forward " \r\t\n" limit
)
1651 (skip-chars-backward " \t")
1652 (if (bolp) (point) (line-end-position)))))
1658 :post-blank
(count-lines pos-before-blank end
)
1659 :post-affiliated post-affiliated
)
1660 (cdr affiliated
))))))
1662 (defun org-element-diary-sexp-interpreter (diary-sexp contents
)
1663 "Interpret DIARY-SEXP as Org syntax.
1665 (org-element-property :value diary-sexp
))
1670 (defun org-element-example-block-parser (limit affiliated
)
1671 "Parse an example block.
1673 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1674 the buffer position at the beginning of the first affiliated
1675 keyword and CDR is a plist of affiliated keywords along with
1678 Return a list whose CAR is `example-block' and CDR is a plist
1679 containing `:begin', `:end', `:number-lines', `:preserve-indent',
1680 `:retain-labels', `:use-labels', `:label-fmt', `:hiddenp',
1681 `:switches', `:value', `:post-blank' and `:post-affiliated'
1683 (let ((case-fold-search t
))
1684 (if (not (save-excursion
1685 (re-search-forward "^[ \t]*#\\+END_EXAMPLE[ \t]*$" limit t
)))
1686 ;; Incomplete block: parse it as a paragraph.
1687 (org-element-paragraph-parser limit affiliated
)
1688 (let ((contents-end (match-beginning 0)))
1691 (progn (looking-at "^[ \t]*#\\+BEGIN_EXAMPLE\\(?: +\\(.*\\)\\)?")
1692 (org-match-string-no-properties 1)))
1693 ;; Switches analysis
1694 (number-lines (cond ((not switches
) nil
)
1695 ((string-match "-n\\>" switches
) 'new
)
1696 ((string-match "+n\\>" switches
) 'continued
)))
1697 (preserve-indent (and switches
(string-match "-i\\>" switches
)))
1698 ;; Should labels be retained in (or stripped from) example
1702 (not (string-match "-r\\>" switches
))
1703 (and number-lines
(string-match "-k\\>" switches
))))
1704 ;; What should code-references use - labels or
1708 (and retain-labels
(not (string-match "-k\\>" switches
)))))
1709 (label-fmt (and switches
1710 (string-match "-l +\"\\([^\"\n]+\\)\"" switches
)
1711 (match-string 1 switches
)))
1712 ;; Standard block parsing.
1713 (begin (car affiliated
))
1714 (post-affiliated (point))
1715 (contents-begin (progn (forward-line) (point)))
1716 (hidden (org-invisible-p2))
1717 (value (org-unescape-code-in-string
1718 (buffer-substring-no-properties
1719 contents-begin contents-end
)))
1720 (pos-before-blank (progn (goto-char contents-end
)
1723 (end (progn (skip-chars-forward " \r\t\n" limit
)
1724 (skip-chars-backward " \t")
1725 (if (bolp) (point) (line-end-position)))))
1726 (list 'example-block
1732 :number-lines number-lines
1733 :preserve-indent preserve-indent
1734 :retain-labels retain-labels
1735 :use-labels use-labels
1736 :label-fmt label-fmt
1738 :post-blank
(count-lines pos-before-blank end
)
1739 :post-affiliated post-affiliated
)
1740 (cdr affiliated
)))))))))
1742 (defun org-element-example-block-interpreter (example-block contents
)
1743 "Interpret EXAMPLE-BLOCK element as Org syntax.
1745 (let ((switches (org-element-property :switches example-block
)))
1746 (concat "#+BEGIN_EXAMPLE" (and switches
(concat " " switches
)) "\n"
1747 (org-remove-indentation
1748 (org-escape-code-in-string
1749 (org-element-property :value example-block
)))
1755 (defun org-element-export-block-parser (limit affiliated
)
1756 "Parse an export block.
1758 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1759 the buffer position at the beginning of the first affiliated
1760 keyword and CDR is a plist of affiliated keywords along with
1763 Return a list whose CAR is `export-block' and CDR is a plist
1764 containing `:begin', `:end', `:type', `:hiddenp', `:value',
1765 `:post-blank' and `:post-affiliated' keywords.
1767 Assume point is at export-block beginning."
1768 (let* ((case-fold-search t
)
1769 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
1770 (upcase (org-match-string-no-properties 1)))))
1771 (if (not (save-excursion
1773 (format "^[ \t]*#\\+END_%s[ \t]*$" type
) limit t
)))
1774 ;; Incomplete block: parse it as a paragraph.
1775 (org-element-paragraph-parser limit affiliated
)
1776 (let ((contents-end (match-beginning 0)))
1778 (let* ((begin (car affiliated
))
1779 (post-affiliated (point))
1780 (contents-begin (progn (forward-line) (point)))
1781 (hidden (org-invisible-p2))
1782 (pos-before-blank (progn (goto-char contents-end
)
1785 (end (progn (skip-chars-forward " \r\t\n" limit
)
1786 (skip-chars-backward " \t")
1787 (if (bolp) (point) (line-end-position))))
1788 (value (buffer-substring-no-properties contents-begin
1797 :post-blank
(count-lines pos-before-blank end
)
1798 :post-affiliated post-affiliated
)
1799 (cdr affiliated
)))))))))
1801 (defun org-element-export-block-interpreter (export-block contents
)
1802 "Interpret EXPORT-BLOCK element as Org syntax.
1804 (let ((type (org-element-property :type export-block
)))
1805 (concat (format "#+BEGIN_%s\n" type
)
1806 (org-element-property :value export-block
)
1807 (format "#+END_%s" type
))))
1812 (defun org-element-fixed-width-parser (limit affiliated
)
1813 "Parse a fixed-width section.
1815 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1816 the buffer position at the beginning of the first affiliated
1817 keyword and CDR is a plist of affiliated keywords along with
1820 Return a list whose CAR is `fixed-width' and CDR is a plist
1821 containing `:begin', `:end', `:value', `:post-blank' and
1822 `:post-affiliated' keywords.
1824 Assume point is at the beginning of the fixed-width area."
1826 (let* ((begin (car affiliated
))
1827 (post-affiliated (point))
1831 (while (and (< (point) limit
)
1832 (looking-at "[ \t]*:\\( \\|$\\)"))
1833 ;; Accumulate text without starting colons.
1836 (buffer-substring-no-properties
1837 (match-end 0) (point-at-eol))
1841 (end (progn (skip-chars-forward " \r\t\n" limit
)
1842 (skip-chars-backward " \t")
1843 (if (bolp) (point) (line-end-position)))))
1849 :post-blank
(count-lines end-area end
)
1850 :post-affiliated post-affiliated
)
1851 (cdr affiliated
))))))
1853 (defun org-element-fixed-width-interpreter (fixed-width contents
)
1854 "Interpret FIXED-WIDTH element as Org syntax.
1856 (replace-regexp-in-string
1857 "^" ": " (substring (org-element-property :value fixed-width
) 0 -
1)))
1860 ;;;; Horizontal Rule
1862 (defun org-element-horizontal-rule-parser (limit affiliated
)
1863 "Parse an horizontal rule.
1865 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1866 the buffer position at the beginning of the first affiliated
1867 keyword and CDR is a plist of affiliated keywords along with
1870 Return a list whose CAR is `horizontal-rule' and CDR is a plist
1871 containing `:begin', `:end', `:post-blank' and `:post-affiliated'
1874 (let ((begin (car affiliated
))
1875 (post-affiliated (point))
1876 (post-hr (progn (forward-line) (point)))
1877 (end (progn (skip-chars-forward " \r\t\n" limit
)
1878 (skip-chars-backward " \t")
1879 (if (bolp) (point) (line-end-position)))))
1880 (list 'horizontal-rule
1884 :post-blank
(count-lines post-hr end
)
1885 :post-affiliated post-affiliated
)
1886 (cdr affiliated
))))))
1888 (defun org-element-horizontal-rule-interpreter (horizontal-rule contents
)
1889 "Interpret HORIZONTAL-RULE element as Org syntax.
1896 (defun org-element-keyword-parser (limit affiliated
)
1897 "Parse a keyword at point.
1899 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1900 the buffer position at the beginning of the first affiliated
1901 keyword and CDR is a plist of affiliated keywords along with
1904 Return a list whose CAR is `keyword' and CDR is a plist
1905 containing `:key', `:value', `:begin', `:end', `:post-blank' and
1906 `:post-affiliated' keywords."
1908 (let ((begin (car affiliated
))
1909 (post-affiliated (point))
1910 (key (progn (looking-at "[ \t]*#\\+\\(\\S-+*\\):")
1911 (upcase (org-match-string-no-properties 1))))
1912 (value (org-trim (buffer-substring-no-properties
1913 (match-end 0) (point-at-eol))))
1914 (pos-before-blank (progn (forward-line) (point)))
1915 (end (progn (skip-chars-forward " \r\t\n" limit
)
1916 (skip-chars-backward " \t")
1917 (if (bolp) (point) (line-end-position)))))
1924 :post-blank
(count-lines pos-before-blank end
)
1925 :post-affiliated post-affiliated
)
1926 (cdr affiliated
))))))
1928 (defun org-element-keyword-interpreter (keyword contents
)
1929 "Interpret KEYWORD element as Org syntax.
1932 (org-element-property :key keyword
)
1933 (org-element-property :value keyword
)))
1936 ;;;; Latex Environment
1938 (defun org-element-latex-environment-parser (limit affiliated
)
1939 "Parse a LaTeX environment.
1941 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1942 the buffer position at the beginning of the first affiliated
1943 keyword and CDR is a plist of affiliated keywords along with
1946 Return a list whose CAR is `latex-environment' and CDR is a plist
1947 containing `:begin', `:end', `:value', `:post-blank' and
1948 `:post-affiliated' keywords.
1950 Assume point is at the beginning of the latex environment."
1952 (let ((case-fold-search t
)
1953 (code-begin (point)))
1954 (looking-at "[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}")
1955 (if (not (re-search-forward (format "^[ \t]*\\\\end{%s}[ \t]*$"
1956 (regexp-quote (match-string 1)))
1958 ;; Incomplete latex environment: parse it as a paragraph.
1959 (org-element-paragraph-parser limit affiliated
)
1960 (let* ((code-end (progn (forward-line) (point)))
1961 (begin (car affiliated
))
1962 (post-affiliated (point))
1963 (value (buffer-substring-no-properties code-begin code-end
))
1964 (end (progn (skip-chars-forward " \r\t\n" limit
)
1965 (skip-chars-backward " \t")
1966 (if (bolp) (point) (line-end-position)))))
1967 (list 'latex-environment
1972 :post-blank
(count-lines code-end end
)
1973 :post-affiliated post-affiliated
)
1974 (cdr affiliated
))))))))
1976 (defun org-element-latex-environment-interpreter (latex-environment contents
)
1977 "Interpret LATEX-ENVIRONMENT element as Org syntax.
1979 (org-element-property :value latex-environment
))
1984 (defun org-element-node-property-parser (limit)
1985 "Parse a node-property at point.
1987 LIMIT bounds the search.
1989 Return a list whose CAR is `node-property' and CDR is a plist
1990 containing `:key', `:value', `:begin', `:end' and `:post-blank'
1993 (let ((case-fold-search t
)
1995 (key (progn (looking-at "[ \t]*:\\(.*?\\):[ \t]+\\(.*?\\)[ \t]*$")
1996 (org-match-string-no-properties 1)))
1997 (value (org-match-string-no-properties 2))
1998 (pos-before-blank (progn (forward-line) (point)))
1999 (end (progn (skip-chars-forward " \r\t\n" limit
)
2000 (if (eobp) (point) (point-at-bol)))))
2001 (list 'node-property
2006 :post-blank
(count-lines pos-before-blank end
))))))
2008 (defun org-element-node-property-interpreter (node-property contents
)
2009 "Interpret NODE-PROPERTY element as Org syntax.
2011 (format org-property-format
2012 (format ":%s:" (org-element-property :key node-property
))
2013 (org-element-property :value node-property
)))
2018 (defun org-element-paragraph-parser (limit affiliated
)
2021 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2022 the buffer position at the beginning of the first affiliated
2023 keyword and CDR is a plist of affiliated keywords along with
2026 Return a list whose CAR is `paragraph' and CDR is a plist
2027 containing `:begin', `:end', `:contents-begin' and
2028 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
2030 Assume point is at the beginning of the paragraph."
2032 (let* ((begin (car affiliated
))
2033 (contents-begin (point))
2035 (let ((case-fold-search t
))
2037 (if (not (re-search-forward
2038 org-element-paragraph-separate limit
'm
))
2040 ;; A matching `org-element-paragraph-separate' is not
2041 ;; necessarily the end of the paragraph. In
2042 ;; particular, lines starting with # or : as a first
2043 ;; non-space character are ambiguous. We have check
2044 ;; if they are valid Org syntax (i.e. not an
2045 ;; incomplete keyword).
2049 ;; There's no ambiguity for other symbols or
2050 ;; empty lines: stop here.
2051 (looking-at "[ \t]*\\(?:[^:#]\\|$\\)")
2052 ;; Stop at valid fixed-width areas.
2053 (looking-at "[ \t]*:\\(?: \\|$\\)")
2055 (and (looking-at org-drawer-regexp
)
2058 "^[ \t]*:END:[ \t]*$" limit t
)))
2059 ;; Stop at valid comments.
2060 (looking-at "[ \t]*#\\(?: \\|$\\)")
2061 ;; Stop at valid dynamic blocks.
2062 (and (looking-at org-dblock-start-re
)
2065 "^[ \t]*#\\+END:?[ \t]*$" limit t
)))
2066 ;; Stop at valid blocks.
2068 "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
2071 (format "^[ \t]*#\\+END_%s[ \t]*$"
2074 ;; Stop at valid latex environments.
2076 "^[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}[ \t]*$")
2079 (format "^[ \t]*\\\\end{%s}[ \t]*$"
2082 ;; Stop at valid keywords.
2083 (looking-at "[ \t]*#\\+\\S-+:")
2084 ;; Skip everything else.
2088 (re-search-forward org-element-paragraph-separate
2090 (beginning-of-line)))
2091 (if (= (point) limit
) limit
2092 (goto-char (line-beginning-position)))))
2093 (contents-end (progn (skip-chars-backward " \r\t\n" contents-begin
)
2096 (end (progn (skip-chars-forward " \r\t\n" limit
)
2097 (skip-chars-backward " \t")
2098 (if (bolp) (point) (line-end-position)))))
2103 :contents-begin contents-begin
2104 :contents-end contents-end
2105 :post-blank
(count-lines before-blank end
)
2106 :post-affiliated contents-begin
)
2107 (cdr affiliated
))))))
2109 (defun org-element-paragraph-interpreter (paragraph contents
)
2110 "Interpret PARAGRAPH element as Org syntax.
2111 CONTENTS is the contents of the element."
2117 (defun org-element-planning-parser (limit)
2120 LIMIT bounds the search.
2122 Return a list whose CAR is `planning' and CDR is a plist
2123 containing `:closed', `:deadline', `:scheduled', `:begin', `:end'
2124 and `:post-blank' keywords."
2126 (let* ((case-fold-search nil
)
2128 (post-blank (let ((before-blank (progn (forward-line) (point))))
2129 (skip-chars-forward " \r\t\n" limit
)
2130 (skip-chars-backward " \t")
2131 (unless (bolp) (end-of-line))
2132 (count-lines before-blank
(point))))
2134 closed deadline scheduled
)
2136 (while (re-search-forward org-keyword-time-not-clock-regexp end t
)
2137 (goto-char (match-end 1))
2138 (skip-chars-forward " \t" end
)
2139 (let ((keyword (match-string 1))
2140 (time (org-element-timestamp-parser)))
2141 (cond ((equal keyword org-closed-string
) (setq closed time
))
2142 ((equal keyword org-deadline-string
) (setq deadline time
))
2143 (t (setq scheduled time
)))))
2145 (list :closed closed
2147 :scheduled scheduled
2150 :post-blank post-blank
)))))
2152 (defun org-element-planning-interpreter (planning contents
)
2153 "Interpret PLANNING element as Org syntax.
2158 (list (let ((deadline (org-element-property :deadline planning
)))
2160 (concat org-deadline-string
" "
2161 (org-element-timestamp-interpreter deadline nil
))))
2162 (let ((scheduled (org-element-property :scheduled planning
)))
2164 (concat org-scheduled-string
" "
2165 (org-element-timestamp-interpreter scheduled nil
))))
2166 (let ((closed (org-element-property :closed planning
)))
2168 (concat org-closed-string
" "
2169 (org-element-timestamp-interpreter closed nil
))))))
2175 (defun org-element-quote-section-parser (limit)
2176 "Parse a quote section.
2178 LIMIT bounds the search.
2180 Return a list whose CAR is `quote-section' and CDR is a plist
2181 containing `:begin', `:end', `:value' and `:post-blank' keywords.
2183 Assume point is at beginning of the section."
2185 (let* ((begin (point))
2186 (end (progn (org-with-limited-levels (outline-next-heading))
2188 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
2191 (value (buffer-substring-no-properties begin pos-before-blank
)))
2192 (list 'quote-section
2196 :post-blank
(count-lines pos-before-blank end
))))))
2198 (defun org-element-quote-section-interpreter (quote-section contents
)
2199 "Interpret QUOTE-SECTION element as Org syntax.
2201 (org-element-property :value quote-section
))
2206 (defun org-element-src-block-parser (limit affiliated
)
2209 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2210 the buffer position at the beginning of the first affiliated
2211 keyword and CDR is a plist of affiliated keywords along with
2214 Return a list whose CAR is `src-block' and CDR is a plist
2215 containing `:language', `:switches', `:parameters', `:begin',
2216 `:end', `:hiddenp', `:number-lines', `:retain-labels',
2217 `:use-labels', `:label-fmt', `:preserve-indent', `:value',
2218 `:post-blank' and `:post-affiliated' keywords.
2220 Assume point is at the beginning of the block."
2221 (let ((case-fold-search t
))
2222 (if (not (save-excursion (re-search-forward "^[ \t]*#\\+END_SRC[ \t]*$"
2224 ;; Incomplete block: parse it as a paragraph.
2225 (org-element-paragraph-parser limit affiliated
)
2226 (let ((contents-end (match-beginning 0)))
2228 (let* ((begin (car affiliated
))
2229 (post-affiliated (point))
2230 ;; Get language as a string.
2234 (concat "^[ \t]*#\\+BEGIN_SRC"
2235 "\\(?: +\\(\\S-+\\)\\)?"
2236 "\\(\\(?: +\\(?:-l \".*?\"\\|[-+][A-Za-z]\\)\\)+\\)?"
2238 (org-match-string-no-properties 1)))
2240 (switches (org-match-string-no-properties 2))
2242 (parameters (org-match-string-no-properties 3))
2243 ;; Switches analysis
2244 (number-lines (cond ((not switches
) nil
)
2245 ((string-match "-n\\>" switches
) 'new
)
2246 ((string-match "+n\\>" switches
) 'continued
)))
2247 (preserve-indent (and switches
(string-match "-i\\>" switches
)))
2248 (label-fmt (and switches
2249 (string-match "-l +\"\\([^\"\n]+\\)\"" switches
)
2250 (match-string 1 switches
)))
2251 ;; Should labels be retained in (or stripped from)
2255 (not (string-match "-r\\>" switches
))
2256 (and number-lines
(string-match "-k\\>" switches
))))
2257 ;; What should code-references use - labels or
2261 (and retain-labels
(not (string-match "-k\\>" switches
)))))
2262 ;; Get visibility status.
2263 (hidden (progn (forward-line) (org-invisible-p2)))
2265 (value (org-unescape-code-in-string
2266 (buffer-substring-no-properties (point) contents-end
)))
2267 (pos-before-blank (progn (goto-char contents-end
)
2270 ;; Get position after ending blank lines.
2271 (end (progn (skip-chars-forward " \r\t\n" limit
)
2272 (skip-chars-backward " \t")
2273 (if (bolp) (point) (line-end-position)))))
2276 (list :language language
2277 :switches
(and (org-string-nw-p switches
)
2278 (org-trim switches
))
2279 :parameters
(and (org-string-nw-p parameters
)
2280 (org-trim parameters
))
2283 :number-lines number-lines
2284 :preserve-indent preserve-indent
2285 :retain-labels retain-labels
2286 :use-labels use-labels
2287 :label-fmt label-fmt
2290 :post-blank
(count-lines pos-before-blank end
)
2291 :post-affiliated post-affiliated
)
2292 (cdr affiliated
)))))))))
2294 (defun org-element-src-block-interpreter (src-block contents
)
2295 "Interpret SRC-BLOCK element as Org syntax.
2297 (let ((lang (org-element-property :language src-block
))
2298 (switches (org-element-property :switches src-block
))
2299 (params (org-element-property :parameters src-block
))
2300 (value (let ((val (org-element-property :value src-block
)))
2302 (org-src-preserve-indentation val
)
2303 ((zerop org-edit-src-content-indentation
)
2304 (org-remove-indentation val
))
2306 (let ((ind (make-string
2307 org-edit-src-content-indentation
32)))
2308 (replace-regexp-in-string
2309 "\\(^\\)[ \t]*\\S-" ind
2310 (org-remove-indentation val
) nil nil
1)))))))
2311 (concat (format "#+BEGIN_SRC%s\n"
2312 (concat (and lang
(concat " " lang
))
2313 (and switches
(concat " " switches
))
2314 (and params
(concat " " params
))))
2315 (org-escape-code-in-string value
)
2321 (defun org-element-table-parser (limit affiliated
)
2322 "Parse a table at point.
2324 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2325 the buffer position at the beginning of the first affiliated
2326 keyword and CDR is a plist of affiliated keywords along with
2329 Return a list whose CAR is `table' and CDR is a plist containing
2330 `:begin', `:end', `:tblfm', `:type', `:contents-begin',
2331 `:contents-end', `:value', `:post-blank' and `:post-affiliated'
2334 Assume point is at the beginning of the table."
2336 (let* ((case-fold-search t
)
2337 (table-begin (point))
2338 (type (if (org-at-table.el-p
) 'table.el
'org
))
2339 (begin (car affiliated
))
2341 (if (re-search-forward org-table-any-border-regexp limit
'm
)
2342 (goto-char (match-beginning 0))
2345 (while (looking-at "[ \t]*#\\+TBLFM: +\\(.*\\)[ \t]*$")
2346 (push (org-match-string-no-properties 1) acc
)
2349 (pos-before-blank (point))
2350 (end (progn (skip-chars-forward " \r\t\n" limit
)
2351 (skip-chars-backward " \t")
2352 (if (bolp) (point) (line-end-position)))))
2359 ;; Only `org' tables have contents. `table.el' tables
2360 ;; use a `:value' property to store raw table as
2362 :contents-begin
(and (eq type
'org
) table-begin
)
2363 :contents-end
(and (eq type
'org
) table-end
)
2364 :value
(and (eq type
'table.el
)
2365 (buffer-substring-no-properties
2366 table-begin table-end
))
2367 :post-blank
(count-lines pos-before-blank end
)
2368 :post-affiliated table-begin
)
2369 (cdr affiliated
))))))
2371 (defun org-element-table-interpreter (table contents
)
2372 "Interpret TABLE element as Org syntax.
2374 (if (eq (org-element-property :type table
) 'table.el
)
2375 (org-remove-indentation (org-element-property :value table
))
2376 (concat (with-temp-buffer (insert contents
)
2379 (mapconcat (lambda (fm) (concat "#+TBLFM: " fm
))
2380 (reverse (org-element-property :tblfm table
))
2386 (defun org-element-table-row-parser (limit)
2387 "Parse table row at point.
2389 LIMIT bounds the search.
2391 Return a list whose CAR is `table-row' and CDR is a plist
2392 containing `:begin', `:end', `:contents-begin', `:contents-end',
2393 `:type' and `:post-blank' keywords."
2395 (let* ((type (if (looking-at "^[ \t]*|-") 'rule
'standard
))
2397 ;; A table rule has no contents. In that case, ensure
2398 ;; CONTENTS-BEGIN matches CONTENTS-END.
2399 (contents-begin (and (eq type
'standard
)
2400 (search-forward "|")
2402 (contents-end (and (eq type
'standard
)
2405 (skip-chars-backward " \t")
2407 (end (progn (forward-line) (point))))
2412 :contents-begin contents-begin
2413 :contents-end contents-end
2416 (defun org-element-table-row-interpreter (table-row contents
)
2417 "Interpret TABLE-ROW element as Org syntax.
2418 CONTENTS is the contents of the table row."
2419 (if (eq (org-element-property :type table-row
) 'rule
) "|-"
2420 (concat "| " contents
)))
2425 (defun org-element-verse-block-parser (limit affiliated
)
2426 "Parse a verse block.
2428 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2429 the buffer position at the beginning of the first affiliated
2430 keyword and CDR is a plist of affiliated keywords along with
2433 Return a list whose CAR is `verse-block' and CDR is a plist
2434 containing `:begin', `:end', `:contents-begin', `:contents-end',
2435 `:hiddenp', `:post-blank' and `:post-affiliated' keywords.
2437 Assume point is at beginning of the block."
2438 (let ((case-fold-search t
))
2439 (if (not (save-excursion
2440 (re-search-forward "^[ \t]*#\\+END_VERSE[ \t]*$" limit t
)))
2441 ;; Incomplete block: parse it as a paragraph.
2442 (org-element-paragraph-parser limit affiliated
)
2443 (let ((contents-end (match-beginning 0)))
2445 (let* ((begin (car affiliated
))
2446 (post-affiliated (point))
2447 (hidden (progn (forward-line) (org-invisible-p2)))
2448 (contents-begin (point))
2449 (pos-before-blank (progn (goto-char contents-end
)
2452 (end (progn (skip-chars-forward " \r\t\n" limit
)
2453 (skip-chars-backward " \t")
2454 (if (bolp) (point) (line-end-position)))))
2459 :contents-begin contents-begin
2460 :contents-end contents-end
2462 :post-blank
(count-lines pos-before-blank end
)
2463 :post-affiliated post-affiliated
)
2464 (cdr affiliated
)))))))))
2466 (defun org-element-verse-block-interpreter (verse-block contents
)
2467 "Interpret VERSE-BLOCK element as Org syntax.
2468 CONTENTS is verse block contents."
2469 (format "#+BEGIN_VERSE\n%s#+END_VERSE" contents
))
2475 ;; Unlike to elements, interstices can be found between objects.
2476 ;; That's why, along with the parser, successor functions are provided
2477 ;; for each object. Some objects share the same successor (i.e. `code'
2478 ;; and `verbatim' objects).
2480 ;; A successor must accept a single argument bounding the search. It
2481 ;; will return either a cons cell whose CAR is the object's type, as
2482 ;; a symbol, and CDR the position of its next occurrence, or nil.
2484 ;; Successors follow the naming convention:
2485 ;; org-element-NAME-successor, where NAME is the name of the
2486 ;; successor, as defined in `org-element-all-successors'.
2488 ;; Some object types (i.e. `italic') are recursive. Restrictions on
2489 ;; object types they can contain will be specified in
2490 ;; `org-element-object-restrictions'.
2492 ;; Adding a new type of object is simple. Implement a successor,
2493 ;; a parser, and an interpreter for it, all following the naming
2494 ;; convention. Register type in `org-element-all-objects' and
2495 ;; successor in `org-element-all-successors'. Maybe tweak
2496 ;; restrictions about it, and that's it.
2501 (defun org-element-bold-parser ()
2502 "Parse bold object at point.
2504 Return a list whose CAR is `bold' and CDR is a plist with
2505 `:begin', `:end', `:contents-begin' and `:contents-end' and
2506 `:post-blank' keywords.
2508 Assume point is at the first star marker."
2510 (unless (bolp) (backward-char 1))
2511 (looking-at org-emph-re
)
2512 (let ((begin (match-beginning 2))
2513 (contents-begin (match-beginning 4))
2514 (contents-end (match-end 4))
2515 (post-blank (progn (goto-char (match-end 2))
2516 (skip-chars-forward " \t")))
2521 :contents-begin contents-begin
2522 :contents-end contents-end
2523 :post-blank post-blank
)))))
2525 (defun org-element-bold-interpreter (bold contents
)
2526 "Interpret BOLD object as Org syntax.
2527 CONTENTS is the contents of the object."
2528 (format "*%s*" contents
))
2530 (defun org-element-text-markup-successor (limit)
2531 "Search for the next text-markup object.
2533 LIMIT bounds the search.
2535 Return value is a cons cell whose CAR is a symbol among `bold',
2536 `italic', `underline', `strike-through', `code' and `verbatim'
2537 and CDR is beginning position."
2539 (unless (bolp) (backward-char))
2540 (when (re-search-forward org-emph-re limit t
)
2541 (let ((marker (match-string 3)))
2543 ((equal marker
"*") 'bold
)
2544 ((equal marker
"/") 'italic
)
2545 ((equal marker
"_") 'underline
)
2546 ((equal marker
"+") 'strike-through
)
2547 ((equal marker
"~") 'code
)
2548 ((equal marker
"=") 'verbatim
)
2549 (t (error "Unknown marker at %d" (match-beginning 3))))
2550 (match-beginning 2))))))
2555 (defun org-element-code-parser ()
2556 "Parse code object at point.
2558 Return a list whose CAR is `code' and CDR is a plist with
2559 `:value', `:begin', `:end' and `:post-blank' keywords.
2561 Assume point is at the first tilde marker."
2563 (unless (bolp) (backward-char 1))
2564 (looking-at org-emph-re
)
2565 (let ((begin (match-beginning 2))
2566 (value (org-match-string-no-properties 4))
2567 (post-blank (progn (goto-char (match-end 2))
2568 (skip-chars-forward " \t")))
2574 :post-blank post-blank
)))))
2576 (defun org-element-code-interpreter (code contents
)
2577 "Interpret CODE object as Org syntax.
2579 (format "~%s~" (org-element-property :value code
)))
2584 (defun org-element-entity-parser ()
2585 "Parse entity at point.
2587 Return a list whose CAR is `entity' and CDR a plist with
2588 `:begin', `:end', `:latex', `:latex-math-p', `:html', `:latin1',
2589 `:utf-8', `:ascii', `:use-brackets-p' and `:post-blank' as
2592 Assume point is at the beginning of the entity."
2594 (looking-at "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)")
2595 (let* ((value (org-entity-get (match-string 1)))
2596 (begin (match-beginning 0))
2597 (bracketsp (string= (match-string 2) "{}"))
2598 (post-blank (progn (goto-char (match-end 1))
2599 (when bracketsp
(forward-char 2))
2600 (skip-chars-forward " \t")))
2603 (list :name
(car value
)
2604 :latex
(nth 1 value
)
2605 :latex-math-p
(nth 2 value
)
2607 :ascii
(nth 4 value
)
2608 :latin1
(nth 5 value
)
2609 :utf-8
(nth 6 value
)
2612 :use-brackets-p bracketsp
2613 :post-blank post-blank
)))))
2615 (defun org-element-entity-interpreter (entity contents
)
2616 "Interpret ENTITY object as Org syntax.
2619 (org-element-property :name entity
)
2620 (when (org-element-property :use-brackets-p entity
) "{}")))
2622 (defun org-element-latex-or-entity-successor (limit)
2623 "Search for the next latex-fragment or entity object.
2625 LIMIT bounds the search.
2627 Return value is a cons cell whose CAR is `entity' or
2628 `latex-fragment' and CDR is beginning position."
2630 (unless (bolp) (backward-char))
2632 (remove "begin" (plist-get org-format-latex-options
:matchers
)))
2633 ;; ENTITY-RE matches both LaTeX commands and Org entities.
2635 "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))
2636 (when (re-search-forward
2637 (concat (mapconcat (lambda (e) (nth 1 (assoc e org-latex-regexps
)))
2641 (goto-char (match-beginning 0))
2642 (if (looking-at entity-re
)
2643 ;; Determine if it's a real entity or a LaTeX command.
2644 (cons (if (org-entity-get (match-string 1)) 'entity
'latex-fragment
)
2645 (match-beginning 0))
2646 ;; No entity nor command: point is at a LaTeX fragment.
2647 ;; Determine its type to get the correct beginning position.
2648 (cons 'latex-fragment
2651 (when (looking-at (nth 1 (assoc e org-latex-regexps
)))
2654 (nth 2 (assoc e org-latex-regexps
))))))
2661 (defun org-element-export-snippet-parser ()
2662 "Parse export snippet at point.
2664 Return a list whose CAR is `export-snippet' and CDR a plist with
2665 `:begin', `:end', `:back-end', `:value' and `:post-blank' as
2668 Assume point is at the beginning of the snippet."
2670 (re-search-forward "@@\\([-A-Za-z0-9]+\\):" nil t
)
2671 (let* ((begin (match-beginning 0))
2672 (back-end (org-match-string-no-properties 1))
2673 (value (buffer-substring-no-properties
2675 (progn (re-search-forward "@@" nil t
) (match-beginning 0))))
2676 (post-blank (skip-chars-forward " \t"))
2678 (list 'export-snippet
2679 (list :back-end back-end
2683 :post-blank post-blank
)))))
2685 (defun org-element-export-snippet-interpreter (export-snippet contents
)
2686 "Interpret EXPORT-SNIPPET object as Org syntax.
2689 (org-element-property :back-end export-snippet
)
2690 (org-element-property :value export-snippet
)))
2692 (defun org-element-export-snippet-successor (limit)
2693 "Search for the next export-snippet object.
2695 LIMIT bounds the search.
2697 Return value is a cons cell whose CAR is `export-snippet' and CDR
2698 its beginning position."
2701 (when (and (re-search-forward "@@[-A-Za-z0-9]+:" limit t
)
2702 (setq beg
(match-beginning 0))
2703 (search-forward "@@" limit t
))
2704 (cons 'export-snippet beg
)))))
2707 ;;;; Footnote Reference
2709 (defun org-element-footnote-reference-parser ()
2710 "Parse footnote reference at point.
2712 Return a list whose CAR is `footnote-reference' and CDR a plist
2713 with `:label', `:type', `:inline-definition', `:begin', `:end'
2714 and `:post-blank' as keywords."
2716 (looking-at org-footnote-re
)
2717 (let* ((begin (point))
2718 (label (or (org-match-string-no-properties 2)
2719 (org-match-string-no-properties 3)
2720 (and (match-string 1)
2721 (concat "fn:" (org-match-string-no-properties 1)))))
2722 (type (if (or (not label
) (match-string 1)) 'inline
'standard
))
2723 (inner-begin (match-end 0))
2727 (while (and (> count
0) (re-search-forward "[][]" nil t
))
2728 (if (equal (match-string 0) "[") (incf count
) (decf count
)))
2730 (post-blank (progn (goto-char (1+ inner-end
))
2731 (skip-chars-forward " \t")))
2734 (list 'footnote-reference
2739 :post-blank post-blank
))))
2740 (org-element-put-property
2741 footnote-reference
:inline-definition
2742 (and (eq type
'inline
)
2743 (org-element-parse-secondary-string
2744 (buffer-substring inner-begin inner-end
)
2745 (org-element-restriction 'footnote-reference
)
2746 footnote-reference
))))))
2748 (defun org-element-footnote-reference-interpreter (footnote-reference contents
)
2749 "Interpret FOOTNOTE-REFERENCE object as Org syntax.
2751 (let ((label (or (org-element-property :label footnote-reference
) "fn:"))
2754 (org-element-property :inline-definition footnote-reference
)))
2755 (if (not inline-def
) ""
2756 (concat ":" (org-element-interpret-data inline-def
))))))
2757 (format "[%s]" (concat label def
))))
2759 (defun org-element-footnote-reference-successor (limit)
2760 "Search for the next footnote-reference object.
2762 LIMIT bounds the search.
2764 Return value is a cons cell whose CAR is `footnote-reference' and
2765 CDR is beginning position."
2768 (while (re-search-forward org-footnote-re limit t
)
2770 (let ((beg (match-beginning 0))
2773 (while (re-search-forward "[][]" limit t
)
2774 (if (equal (match-string 0) "[") (incf count
) (decf count
))
2776 (throw 'exit
(cons 'footnote-reference beg
))))))))))
2779 ;;;; Inline Babel Call
2781 (defun org-element-inline-babel-call-parser ()
2782 "Parse inline babel call at point.
2784 Return a list whose CAR is `inline-babel-call' and CDR a plist
2785 with `:begin', `:end', `:info' and `:post-blank' as keywords.
2787 Assume point is at the beginning of the babel call."
2789 (unless (bolp) (backward-char))
2790 (looking-at org-babel-inline-lob-one-liner-regexp
)
2791 (let ((info (save-match-data (org-babel-lob-get-info)))
2792 (begin (match-end 1))
2793 (post-blank (progn (goto-char (match-end 0))
2794 (skip-chars-forward " \t")))
2796 (list 'inline-babel-call
2800 :post-blank post-blank
)))))
2802 (defun org-element-inline-babel-call-interpreter (inline-babel-call contents
)
2803 "Interpret INLINE-BABEL-CALL object as Org syntax.
2805 (let* ((babel-info (org-element-property :info inline-babel-call
))
2806 (main-source (car babel-info
))
2807 (post-options (nth 1 babel-info
)))
2809 (if (string-match "\\[\\(\\[.*?\\]\\)\\]" main-source
)
2810 ;; Remove redundant square brackets.
2812 (match-string 1 main-source
) nil nil main-source
)
2814 (and post-options
(format "[%s]" post-options
)))))
2816 (defun org-element-inline-babel-call-successor (limit)
2817 "Search for the next inline-babel-call object.
2819 LIMIT bounds the search.
2821 Return value is a cons cell whose CAR is `inline-babel-call' and
2822 CDR is beginning position."
2824 ;; Use a simplified version of
2825 ;; `org-babel-inline-lob-one-liner-regexp'.
2826 (when (re-search-forward
2827 "call_\\([^()\n]+?\\)\\(?:\\[.*?\\]\\)?([^\n]*?)\\(\\[.*?\\]\\)?"
2829 (cons 'inline-babel-call
(match-beginning 0)))))
2832 ;;;; Inline Src Block
2834 (defun org-element-inline-src-block-parser ()
2835 "Parse inline source block at point.
2837 LIMIT bounds the search.
2839 Return a list whose CAR is `inline-src-block' and CDR a plist
2840 with `:begin', `:end', `:language', `:value', `:parameters' and
2841 `:post-blank' as keywords.
2843 Assume point is at the beginning of the inline src block."
2845 (unless (bolp) (backward-char))
2846 (looking-at org-babel-inline-src-block-regexp
)
2847 (let ((begin (match-beginning 1))
2848 (language (org-match-string-no-properties 2))
2849 (parameters (org-match-string-no-properties 4))
2850 (value (org-match-string-no-properties 5))
2851 (post-blank (progn (goto-char (match-end 0))
2852 (skip-chars-forward " \t")))
2854 (list 'inline-src-block
2855 (list :language language
2857 :parameters parameters
2860 :post-blank post-blank
)))))
2862 (defun org-element-inline-src-block-interpreter (inline-src-block contents
)
2863 "Interpret INLINE-SRC-BLOCK object as Org syntax.
2865 (let ((language (org-element-property :language inline-src-block
))
2866 (arguments (org-element-property :parameters inline-src-block
))
2867 (body (org-element-property :value inline-src-block
)))
2868 (format "src_%s%s{%s}"
2870 (if arguments
(format "[%s]" arguments
) "")
2873 (defun org-element-inline-src-block-successor (limit)
2874 "Search for the next inline-babel-call element.
2876 LIMIT bounds the search.
2878 Return value is a cons cell whose CAR is `inline-babel-call' and
2879 CDR is beginning position."
2881 (unless (bolp) (backward-char))
2882 (when (re-search-forward org-babel-inline-src-block-regexp limit t
)
2883 (cons 'inline-src-block
(match-beginning 1)))))
2887 (defun org-element-italic-parser ()
2888 "Parse italic object at point.
2890 Return a list whose CAR is `italic' and CDR is a plist with
2891 `:begin', `:end', `:contents-begin' and `:contents-end' and
2892 `:post-blank' keywords.
2894 Assume point is at the first slash marker."
2896 (unless (bolp) (backward-char 1))
2897 (looking-at org-emph-re
)
2898 (let ((begin (match-beginning 2))
2899 (contents-begin (match-beginning 4))
2900 (contents-end (match-end 4))
2901 (post-blank (progn (goto-char (match-end 2))
2902 (skip-chars-forward " \t")))
2907 :contents-begin contents-begin
2908 :contents-end contents-end
2909 :post-blank post-blank
)))))
2911 (defun org-element-italic-interpreter (italic contents
)
2912 "Interpret ITALIC object as Org syntax.
2913 CONTENTS is the contents of the object."
2914 (format "/%s/" contents
))
2919 (defun org-element-latex-fragment-parser ()
2920 "Parse latex fragment at point.
2922 Return a list whose CAR is `latex-fragment' and CDR a plist with
2923 `:value', `:begin', `:end', and `:post-blank' as keywords.
2925 Assume point is at the beginning of the latex fragment."
2927 (let* ((begin (point))
2931 (let ((latex-regexp (nth 1 (assoc e org-latex-regexps
))))
2932 (when (or (looking-at latex-regexp
)
2936 (looking-at latex-regexp
))))
2937 (throw 'exit
(nth 2 (assoc e org-latex-regexps
))))))
2938 (plist-get org-format-latex-options
:matchers
))
2939 ;; None found: it's a macro.
2940 (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")
2942 (value (match-string-no-properties substring-match
))
2943 (post-blank (progn (goto-char (match-end substring-match
))
2944 (skip-chars-forward " \t")))
2946 (list 'latex-fragment
2950 :post-blank post-blank
)))))
2952 (defun org-element-latex-fragment-interpreter (latex-fragment contents
)
2953 "Interpret LATEX-FRAGMENT object as Org syntax.
2955 (org-element-property :value latex-fragment
))
2959 (defun org-element-line-break-parser ()
2960 "Parse line break at point.
2962 Return a list whose CAR is `line-break', and CDR a plist with
2963 `:begin', `:end' and `:post-blank' keywords.
2965 Assume point is at the beginning of the line break."
2966 (list 'line-break
(list :begin
(point) :end
(point-at-eol) :post-blank
0)))
2968 (defun org-element-line-break-interpreter (line-break contents
)
2969 "Interpret LINE-BREAK object as Org syntax.
2973 (defun org-element-line-break-successor (limit)
2974 "Search for the next line-break object.
2976 LIMIT bounds the search.
2978 Return value is a cons cell whose CAR is `line-break' and CDR is
2979 beginning position."
2981 (let ((beg (and (re-search-forward "[^\\\\]\\(\\\\\\\\\\)[ \t]*$" limit t
)
2982 (goto-char (match-beginning 1)))))
2983 ;; A line break can only happen on a non-empty line.
2984 (when (and beg
(re-search-backward "\\S-" (point-at-bol) t
))
2985 (cons 'line-break beg
)))))
2990 (defun org-element-link-parser ()
2991 "Parse link at point.
2993 Return a list whose CAR is `link' and CDR a plist with `:type',
2994 `:path', `:raw-link', `:application', `:search-option', `:begin',
2995 `:end', `:contents-begin', `:contents-end' and `:post-blank' as
2998 Assume point is at the beginning of the link."
3000 (let ((begin (point))
3001 end contents-begin contents-end link-end post-blank path type
3002 raw-link link search-option application
)
3004 ;; Type 1: Text targeted from a radio target.
3005 ((and org-target-link-regexp
(looking-at org-target-link-regexp
))
3007 link-end
(match-end 0)
3008 path
(org-match-string-no-properties 0)))
3009 ;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]]
3010 ((looking-at org-bracket-link-regexp
)
3011 (setq contents-begin
(match-beginning 3)
3012 contents-end
(match-end 3)
3013 link-end
(match-end 0)
3014 ;; RAW-LINK is the original link.
3015 raw-link
(org-match-string-no-properties 1)
3016 link
(org-translate-link
3017 (org-link-expand-abbrev
3018 (org-link-unescape raw-link
))))
3019 ;; Determine TYPE of link and set PATH accordingly.
3022 ((or (file-name-absolute-p link
) (string-match "^\\.\\.?/" link
))
3023 (setq type
"file" path link
))
3024 ;; Explicit type (http, irc, bbdb...). See `org-link-types'.
3025 ((string-match org-link-re-with-space3 link
)
3026 (setq type
(match-string 1 link
) path
(match-string 2 link
)))
3027 ;; Id type: PATH is the id.
3028 ((string-match "^id:\\([-a-f0-9]+\\)" link
)
3029 (setq type
"id" path
(match-string 1 link
)))
3030 ;; Code-ref type: PATH is the name of the reference.
3031 ((string-match "^(\\(.*\\))$" link
)
3032 (setq type
"coderef" path
(match-string 1 link
)))
3033 ;; Custom-id type: PATH is the name of the custom id.
3034 ((= (aref link
0) ?
#)
3035 (setq type
"custom-id" path
(substring link
1)))
3036 ;; Fuzzy type: Internal link either matches a target, an
3037 ;; headline name or nothing. PATH is the target or
3039 (t (setq type
"fuzzy" path link
))))
3040 ;; Type 3: Plain link, i.e. http://orgmode.org
3041 ((looking-at org-plain-link-re
)
3042 (setq raw-link
(org-match-string-no-properties 0)
3043 type
(org-match-string-no-properties 1)
3044 path
(org-match-string-no-properties 2)
3045 link-end
(match-end 0)))
3046 ;; Type 4: Angular link, i.e. <http://orgmode.org>
3047 ((looking-at org-angle-link-re
)
3048 (setq raw-link
(buffer-substring-no-properties
3049 (match-beginning 1) (match-end 2))
3050 type
(org-match-string-no-properties 1)
3051 path
(org-match-string-no-properties 2)
3052 link-end
(match-end 0))))
3053 ;; In any case, deduce end point after trailing white space from
3054 ;; LINK-END variable.
3055 (setq post-blank
(progn (goto-char link-end
) (skip-chars-forward " \t"))
3057 ;; Extract search option and opening application out of
3058 ;; "file"-type links.
3059 (when (member type org-element-link-type-is-file
)
3061 (cond ((string-match "^file\\+\\(.*\\)$" type
)
3062 (setq application
(match-string 1 type
)))
3063 ((not (string-match "^file" type
))
3064 (setq application type
)))
3065 ;; Extract search option from PATH.
3066 (when (string-match "::\\(.*\\)$" path
)
3067 (setq search-option
(match-string 1 path
)
3068 path
(replace-match "" nil nil path
)))
3069 ;; Make sure TYPE always report "file".
3074 :raw-link
(or raw-link path
)
3075 :application application
3076 :search-option search-option
3079 :contents-begin contents-begin
3080 :contents-end contents-end
3081 :post-blank post-blank
)))))
3083 (defun org-element-link-interpreter (link contents
)
3084 "Interpret LINK object as Org syntax.
3085 CONTENTS is the contents of the object, or nil."
3086 (let ((type (org-element-property :type link
))
3087 (raw-link (org-element-property :raw-link link
)))
3088 (if (string= type
"radio") raw-link
3091 (if contents
(format "[%s]" contents
) "")))))
3093 (defun org-element-link-successor (limit)
3094 "Search for the next link object.
3096 LIMIT bounds the search.
3098 Return value is a cons cell whose CAR is `link' and CDR is
3099 beginning position."
3102 (if (not org-target-link-regexp
) org-any-link-re
3103 (concat org-any-link-re
"\\|" org-target-link-regexp
))))
3104 (when (re-search-forward link-regexp limit t
)
3105 (cons 'link
(match-beginning 0))))))
3110 (defun org-element-macro-parser ()
3111 "Parse macro at point.
3113 Return a list whose CAR is `macro' and CDR a plist with `:key',
3114 `:args', `:begin', `:end', `:value' and `:post-blank' as
3117 Assume point is at the macro."
3119 (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
3120 (let ((begin (point))
3121 (key (downcase (org-match-string-no-properties 1)))
3122 (value (org-match-string-no-properties 0))
3123 (post-blank (progn (goto-char (match-end 0))
3124 (skip-chars-forward " \t")))
3126 (args (let ((args (org-match-string-no-properties 3)) args2
)
3128 (setq args
(org-split-string args
","))
3130 (while (string-match "\\\\\\'" (car args
))
3131 ;; Repair bad splits.
3132 (setcar (cdr args
) (concat (substring (car args
) 0 -
1)
3135 (push (pop args
) args2
))
3136 (mapcar 'org-trim
(nreverse args2
))))))
3143 :post-blank post-blank
)))))
3145 (defun org-element-macro-interpreter (macro contents
)
3146 "Interpret MACRO object as Org syntax.
3148 (org-element-property :value macro
))
3150 (defun org-element-macro-successor (limit)
3151 "Search for the next macro object.
3153 LIMIT bounds the search.
3155 Return value is cons cell whose CAR is `macro' and CDR is
3156 beginning position."
3158 (when (re-search-forward
3159 "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
3161 (cons 'macro
(match-beginning 0)))))
3166 (defun org-element-radio-target-parser ()
3167 "Parse radio target at point.
3169 Return a list whose CAR is `radio-target' and CDR a plist with
3170 `:begin', `:end', `:contents-begin', `:contents-end', `:value'
3171 and `:post-blank' as keywords.
3173 Assume point is at the radio target."
3175 (looking-at org-radio-target-regexp
)
3176 (let ((begin (point))
3177 (contents-begin (match-beginning 1))
3178 (contents-end (match-end 1))
3179 (value (org-match-string-no-properties 1))
3180 (post-blank (progn (goto-char (match-end 0))
3181 (skip-chars-forward " \t")))
3186 :contents-begin contents-begin
3187 :contents-end contents-end
3188 :post-blank post-blank
3191 (defun org-element-radio-target-interpreter (target contents
)
3192 "Interpret TARGET object as Org syntax.
3193 CONTENTS is the contents of the object."
3194 (concat "<<<" contents
">>>"))
3196 (defun org-element-radio-target-successor (limit)
3197 "Search for the next radio-target object.
3199 LIMIT bounds the search.
3201 Return value is a cons cell whose CAR is `radio-target' and CDR
3202 is beginning position."
3204 (when (re-search-forward org-radio-target-regexp limit t
)
3205 (cons 'radio-target
(match-beginning 0)))))
3208 ;;;; Statistics Cookie
3210 (defun org-element-statistics-cookie-parser ()
3211 "Parse statistics cookie at point.
3213 Return a list whose CAR is `statistics-cookie', and CDR a plist
3214 with `:begin', `:end', `:value' and `:post-blank' keywords.
3216 Assume point is at the beginning of the statistics-cookie."
3218 (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
3219 (let* ((begin (point))
3220 (value (buffer-substring-no-properties
3221 (match-beginning 0) (match-end 0)))
3222 (post-blank (progn (goto-char (match-end 0))
3223 (skip-chars-forward " \t")))
3225 (list 'statistics-cookie
3229 :post-blank post-blank
)))))
3231 (defun org-element-statistics-cookie-interpreter (statistics-cookie contents
)
3232 "Interpret STATISTICS-COOKIE object as Org syntax.
3234 (org-element-property :value statistics-cookie
))
3236 (defun org-element-statistics-cookie-successor (limit)
3237 "Search for the next statistics cookie object.
3239 LIMIT bounds the search.
3241 Return value is a cons cell whose CAR is `statistics-cookie' and
3242 CDR is beginning position."
3244 (when (re-search-forward "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]" limit t
)
3245 (cons 'statistics-cookie
(match-beginning 0)))))
3250 (defun org-element-strike-through-parser ()
3251 "Parse strike-through object at point.
3253 Return a list whose CAR is `strike-through' and CDR is a plist
3254 with `:begin', `:end', `:contents-begin' and `:contents-end' and
3255 `:post-blank' keywords.
3257 Assume point is at the first plus sign marker."
3259 (unless (bolp) (backward-char 1))
3260 (looking-at org-emph-re
)
3261 (let ((begin (match-beginning 2))
3262 (contents-begin (match-beginning 4))
3263 (contents-end (match-end 4))
3264 (post-blank (progn (goto-char (match-end 2))
3265 (skip-chars-forward " \t")))
3267 (list 'strike-through
3270 :contents-begin contents-begin
3271 :contents-end contents-end
3272 :post-blank post-blank
)))))
3274 (defun org-element-strike-through-interpreter (strike-through contents
)
3275 "Interpret STRIKE-THROUGH object as Org syntax.
3276 CONTENTS is the contents of the object."
3277 (format "+%s+" contents
))
3282 (defun org-element-subscript-parser ()
3283 "Parse subscript at point.
3285 Return a list whose CAR is `subscript' and CDR a plist with
3286 `:begin', `:end', `:contents-begin', `:contents-end',
3287 `:use-brackets-p' and `:post-blank' as keywords.
3289 Assume point is at the underscore."
3291 (unless (bolp) (backward-char))
3292 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp
)
3294 (not (looking-at org-match-substring-regexp
))))
3295 (begin (match-beginning 2))
3296 (contents-begin (or (match-beginning 5)
3297 (match-beginning 3)))
3298 (contents-end (or (match-end 5) (match-end 3)))
3299 (post-blank (progn (goto-char (match-end 0))
3300 (skip-chars-forward " \t")))
3305 :use-brackets-p bracketsp
3306 :contents-begin contents-begin
3307 :contents-end contents-end
3308 :post-blank post-blank
)))))
3310 (defun org-element-subscript-interpreter (subscript contents
)
3311 "Interpret SUBSCRIPT object as Org syntax.
3312 CONTENTS is the contents of the object."
3314 (if (org-element-property :use-brackets-p subscript
) "_{%s}" "_%s")
3317 (defun org-element-sub/superscript-successor
(limit)
3318 "Search for the next sub/superscript object.
3320 LIMIT bounds the search.
3322 Return value is a cons cell whose CAR is either `subscript' or
3323 `superscript' and CDR is beginning position."
3325 (unless (bolp) (backward-char))
3326 (when (re-search-forward org-match-substring-regexp limit t
)
3327 (cons (if (string= (match-string 2) "_") 'subscript
'superscript
)
3328 (match-beginning 2)))))
3333 (defun org-element-superscript-parser ()
3334 "Parse superscript at point.
3336 Return a list whose CAR is `superscript' and CDR a plist with
3337 `:begin', `:end', `:contents-begin', `:contents-end',
3338 `:use-brackets-p' and `:post-blank' as keywords.
3340 Assume point is at the caret."
3342 (unless (bolp) (backward-char))
3343 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp
) t
3344 (not (looking-at org-match-substring-regexp
))))
3345 (begin (match-beginning 2))
3346 (contents-begin (or (match-beginning 5)
3347 (match-beginning 3)))
3348 (contents-end (or (match-end 5) (match-end 3)))
3349 (post-blank (progn (goto-char (match-end 0))
3350 (skip-chars-forward " \t")))
3355 :use-brackets-p bracketsp
3356 :contents-begin contents-begin
3357 :contents-end contents-end
3358 :post-blank post-blank
)))))
3360 (defun org-element-superscript-interpreter (superscript contents
)
3361 "Interpret SUPERSCRIPT object as Org syntax.
3362 CONTENTS is the contents of the object."
3364 (if (org-element-property :use-brackets-p superscript
) "^{%s}" "^%s")
3370 (defun org-element-table-cell-parser ()
3371 "Parse table cell at point.
3373 Return a list whose CAR is `table-cell' and CDR is a plist
3374 containing `:begin', `:end', `:contents-begin', `:contents-end'
3375 and `:post-blank' keywords."
3376 (looking-at "[ \t]*\\(.*?\\)[ \t]*|")
3377 (let* ((begin (match-beginning 0))
3379 (contents-begin (match-beginning 1))
3380 (contents-end (match-end 1)))
3384 :contents-begin contents-begin
3385 :contents-end contents-end
3388 (defun org-element-table-cell-interpreter (table-cell contents
)
3389 "Interpret TABLE-CELL element as Org syntax.
3390 CONTENTS is the contents of the cell, or nil."
3391 (concat " " contents
" |"))
3393 (defun org-element-table-cell-successor (limit)
3394 "Search for the next table-cell object.
3396 LIMIT bounds the search.
3398 Return value is a cons cell whose CAR is `table-cell' and CDR is
3399 beginning position."
3400 (when (looking-at "[ \t]*.*?[ \t]+|") (cons 'table-cell
(point))))
3405 (defun org-element-target-parser ()
3406 "Parse target at point.
3408 Return a list whose CAR is `target' and CDR a plist with
3409 `:begin', `:end', `:value' and `:post-blank' as keywords.
3411 Assume point is at the target."
3413 (looking-at org-target-regexp
)
3414 (let ((begin (point))
3415 (value (org-match-string-no-properties 1))
3416 (post-blank (progn (goto-char (match-end 0))
3417 (skip-chars-forward " \t")))
3423 :post-blank post-blank
)))))
3425 (defun org-element-target-interpreter (target contents
)
3426 "Interpret TARGET object as Org syntax.
3428 (format "<<%s>>" (org-element-property :value target
)))
3430 (defun org-element-target-successor (limit)
3431 "Search for the next target object.
3433 LIMIT bounds the search.
3435 Return value is a cons cell whose CAR is `target' and CDR is
3436 beginning position."
3438 (when (re-search-forward org-target-regexp limit t
)
3439 (cons 'target
(match-beginning 0)))))
3444 (defun org-element-timestamp-parser ()
3445 "Parse time stamp at point.
3447 Return a list whose CAR is `timestamp', and CDR a plist with
3448 `:type', `:begin', `:end', `:value' and `:post-blank' keywords.
3450 Assume point is at the beginning of the timestamp."
3452 (let* ((begin (point))
3453 (activep (eq (char-after) ?
<))
3456 (looking-at "\\([<[]\\(%%\\)?.*?\\)[]>]\\(?:--\\([<[].*?[]>]\\)\\)?")
3457 (match-string-no-properties 0)))
3458 (date-start (match-string-no-properties 1))
3459 (date-end (match-string 3))
3460 (diaryp (match-beginning 2))
3461 (type (cond (diaryp 'diary
)
3462 ((and activep date-end
) 'active-range
)
3464 (date-end 'inactive-range
)
3466 (post-blank (progn (goto-char (match-end 0))
3467 (skip-chars-forward " \t")))
3469 (with-time-p (string-match "[012]?[0-9]:[0-5][0-9]" date-start
))
3472 (string-match "\\([.+]?\\+\\)\\([0-9]+\\)\\([hdwmy]\\)>"
3476 (let ((type (match-string 1 raw-value
)))
3477 (cond ((equal "++" type
) 'catch-up
)
3478 ((equal ".+" type
) 'restart
)
3480 :repeater-value
(string-to-number (match-string 2 raw-value
))
3482 (case (string-to-char (match-string 3 raw-value
))
3483 (?h
'hour
) (?d
'day
) (?w
'week
) (?m
'month
) (t 'year
)))))
3484 time-range year-start month-start day-start hour-start minute-start
3485 year-end month-end day-end hour-end minute-end
)
3486 ;; Extract time range, if any, and remove it from date start.
3490 "[012]?[0-9]:[0-5][0-9]\\(-\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)"
3492 (cons (string-to-number (match-string 2 date-start
))
3493 (string-to-number (match-string 3 date-start
)))))
3495 (setq date-start
(replace-match "" nil nil date-start
1)))
3496 ;; Parse date-start.
3498 (let ((date (org-parse-time-string date-start
)))
3499 (setq year-start
(nth 5 date
)
3500 month-start
(nth 4 date
)
3501 day-start
(nth 3 date
)
3502 hour-start
(and with-time-p
(nth 2 date
))
3503 minute-start
(and with-time-p
(nth 1 date
)))))
3504 ;; Compute date-end. It can be provided directly in time-stamp,
3505 ;; or extracted from time range. Otherwise, it defaults to the
3506 ;; same values as date-start.
3508 (let ((date (and date-end
(org-parse-time-string date-end
))))
3509 (setq year-end
(or (nth 5 date
) year-start
)
3510 month-end
(or (nth 4 date
) month-start
)
3511 day-end
(or (nth 3 date
) day-start
)
3512 hour-end
(or (nth 2 date
) (car time-range
) hour-start
)
3513 minute-end
(or (nth 1 date
) (cdr time-range
) minute-start
))))
3515 (nconc (list :type type
3516 :raw-value raw-value
3517 :year-start year-start
3518 :month-start month-start
3519 :day-start day-start
3520 :hour-start hour-start
3521 :minute-start minute-start
3523 :month-end month-end
3526 :minute-end minute-end
3529 :post-blank post-blank
)
3532 (defun org-element-timestamp-interpreter (timestamp contents
)
3533 "Interpret TIMESTAMP object as Org syntax.
3535 ;; Use `:raw-value' if specified.
3536 (or (org-element-property :raw-value timestamp
)
3537 ;; Otherwise, build timestamp string.
3538 (let ((build-ts-string
3539 ;; Build an Org timestamp string from TIME. ACTIVEP is
3540 ;; non-nil when time stamp is active. If WITH-TIME-P is
3541 ;; non-nil, add a time part. HOUR-END and MINUTE-END
3542 ;; specify a time range in the timestamp. REPEAT-STRING
3543 ;; is the repeater string, if any.
3544 (lambda (time activep
3545 &optional with-time-p hour-end minute-end repeat-string
)
3546 (let ((ts (format-time-string
3547 (funcall (if with-time-p
'cdr
'car
)
3548 org-time-stamp-formats
)
3550 (when (and hour-end minute-end
)
3551 (string-match "[012]?[0-9]:[0-5][0-9]" ts
)
3554 (format "\\&-%02d:%02d" hour-end minute-end
)
3556 (unless activep
(setq ts
(format "[%s]" (substring ts
1 -
1))))
3557 (when (org-string-nw-p repeat-string
)
3558 (setq ts
(concat (substring ts
0 -
1)
3561 (substring ts -
1))))
3564 (type (org-element-property :type timestamp
)))
3567 (let* ((minute-start (org-element-property :minute-start timestamp
))
3568 (minute-end (org-element-property :minute-end timestamp
))
3569 (hour-start (org-element-property :hour-start timestamp
))
3570 (hour-end (org-element-property :hour-end timestamp
))
3571 (time-range-p (and hour-start hour-end minute-start minute-end
3572 (or (/= hour-start hour-end
)
3573 (/= minute-start minute-end
)))))
3579 (org-element-property :day-start timestamp
)
3580 (org-element-property :month-start timestamp
)
3581 (org-element-property :year-start timestamp
))
3583 (and hour-start minute-start
)
3584 (and time-range-p hour-end
)
3585 (and time-range-p minute-end
)
3586 (concat (case (org-element-property :repeater-type timestamp
)
3587 (cumulate "+") (catch-up "++") (restart ".+"))
3588 (org-element-property :repeater-value timestamp
)
3589 (org-element-property :repeater-unit timestamp
)))))
3590 ((active-range inactive-range
)
3591 (let ((minute-start (org-element-property :minute-start timestamp
))
3592 (minute-end (org-element-property :minute-end timestamp
))
3593 (hour-start (org-element-property :hour-start timestamp
))
3594 (hour-end (org-element-property :hour-end timestamp
)))
3597 build-ts-string
(encode-time
3601 (org-element-property :day-start timestamp
)
3602 (org-element-property :month-start timestamp
)
3603 (org-element-property :year-start timestamp
))
3604 (eq type
'active-range
)
3605 (and hour-start minute-start
))
3607 (funcall build-ts-string
3611 (org-element-property :day-end timestamp
)
3612 (org-element-property :month-end timestamp
)
3613 (org-element-property :year-end timestamp
))
3614 (eq type
'active-range
)
3615 (and hour-end minute-end
)))))))))
3617 (defun org-element-timestamp-successor (limit)
3618 "Search for the next timestamp object.
3620 LIMIT bounds the search.
3622 Return value is a cons cell whose CAR is `timestamp' and CDR is
3623 beginning position."
3625 (when (re-search-forward
3626 (concat org-ts-regexp-both
3628 "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
3630 "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
3632 (cons 'timestamp
(match-beginning 0)))))
3637 (defun org-element-underline-parser ()
3638 "Parse underline object at point.
3640 Return a list whose CAR is `underline' and CDR is a plist with
3641 `:begin', `:end', `:contents-begin' and `:contents-end' and
3642 `:post-blank' keywords.
3644 Assume point is at the first underscore marker."
3646 (unless (bolp) (backward-char 1))
3647 (looking-at org-emph-re
)
3648 (let ((begin (match-beginning 2))
3649 (contents-begin (match-beginning 4))
3650 (contents-end (match-end 4))
3651 (post-blank (progn (goto-char (match-end 2))
3652 (skip-chars-forward " \t")))
3657 :contents-begin contents-begin
3658 :contents-end contents-end
3659 :post-blank post-blank
)))))
3661 (defun org-element-underline-interpreter (underline contents
)
3662 "Interpret UNDERLINE object as Org syntax.
3663 CONTENTS is the contents of the object."
3664 (format "_%s_" contents
))
3669 (defun org-element-verbatim-parser ()
3670 "Parse verbatim object at point.
3672 Return a list whose CAR is `verbatim' and CDR is a plist with
3673 `:value', `:begin', `:end' and `:post-blank' keywords.
3675 Assume point is at the first equal sign marker."
3677 (unless (bolp) (backward-char 1))
3678 (looking-at org-emph-re
)
3679 (let ((begin (match-beginning 2))
3680 (value (org-match-string-no-properties 4))
3681 (post-blank (progn (goto-char (match-end 2))
3682 (skip-chars-forward " \t")))
3688 :post-blank post-blank
)))))
3690 (defun org-element-verbatim-interpreter (verbatim contents
)
3691 "Interpret VERBATIM object as Org syntax.
3693 (format "=%s=" (org-element-property :value verbatim
)))
3697 ;;; Parsing Element Starting At Point
3699 ;; `org-element--current-element' is the core function of this section.
3700 ;; It returns the Lisp representation of the element starting at
3703 ;; `org-element--current-element' makes use of special modes. They
3704 ;; are activated for fixed element chaining (i.e. `plain-list' >
3705 ;; `item') or fixed conditional element chaining (i.e. `headline' >
3706 ;; `section'). Special modes are: `first-section', `item',
3707 ;; `node-property', `quote-section', `section' and `table-row'.
3709 (defun org-element--current-element
3710 (limit &optional granularity special structure
)
3711 "Parse the element starting at point.
3713 LIMIT bounds the search.
3715 Return value is a list like (TYPE PROPS) where TYPE is the type
3716 of the element and PROPS a plist of properties associated to the
3719 Possible types are defined in `org-element-all-elements'.
3721 Optional argument GRANULARITY determines the depth of the
3722 recursion. Allowed values are `headline', `greater-element',
3723 `element', `object' or nil. When it is broader than `object' (or
3724 nil), secondary values will not be parsed, since they only
3727 Optional argument SPECIAL, when non-nil, can be either
3728 `first-section', `item', `node-property', `quote-section',
3729 `section', and `table-row'.
3731 If STRUCTURE isn't provided but SPECIAL is set to `item', it will
3734 This function assumes point is always at the beginning of the
3735 element it has to parse."
3737 (let ((case-fold-search t
)
3738 ;; Determine if parsing depth allows for secondary strings
3739 ;; parsing. It only applies to elements referenced in
3740 ;; `org-element-secondary-value-alist'.
3741 (raw-secondary-p (and granularity
(not (eq granularity
'object
)))))
3745 (org-element-item-parser limit structure raw-secondary-p
))
3747 ((eq special
'table-row
) (org-element-table-row-parser limit
))
3749 ((eq special
'node-property
) (org-element-node-property-parser limit
))
3751 ((org-with-limited-levels (org-at-heading-p))
3752 (org-element-headline-parser limit raw-secondary-p
))
3753 ;; Sections (must be checked after headline).
3754 ((eq special
'section
) (org-element-section-parser limit
))
3755 ((eq special
'quote-section
) (org-element-quote-section-parser limit
))
3756 ((eq special
'first-section
)
3757 (org-element-section-parser
3758 (or (save-excursion (org-with-limited-levels (outline-next-heading)))
3760 ;; When not at bol, point is at the beginning of an item or
3761 ;; a footnote definition: next item is always a paragraph.
3762 ((not (bolp)) (org-element-paragraph-parser limit
(list (point))))
3763 ;; Planning and Clock.
3764 ((looking-at org-planning-or-clock-line-re
)
3765 (if (equal (match-string 1) org-clock-string
)
3766 (org-element-clock-parser limit
)
3767 (org-element-planning-parser limit
)))
3770 (org-element-inlinetask-parser limit raw-secondary-p
))
3771 ;; From there, elements can have affiliated keywords.
3772 (t (let ((affiliated (org-element--collect-affiliated-keywords)))
3774 ;; LaTeX Environment.
3775 ((looking-at "[ \t]*\\\\begin{\\([A-Za-z0-9*]+\\)}[ \t]*$")
3776 (org-element-latex-environment-parser limit affiliated
))
3777 ;; Drawer and Property Drawer.
3778 ((looking-at org-drawer-regexp
)
3779 (if (equal (match-string 1) "PROPERTIES")
3780 (org-element-property-drawer-parser limit affiliated
)
3781 (org-element-drawer-parser limit affiliated
)))
3783 ((looking-at "[ \t]*:\\( \\|$\\)")
3784 (org-element-fixed-width-parser limit affiliated
))
3785 ;; Inline Comments, Blocks, Babel Calls, Dynamic Blocks and
3787 ((looking-at "[ \t]*#")
3788 (goto-char (match-end 0))
3789 (cond ((looking-at "\\(?: \\|$\\)")
3791 (org-element-comment-parser limit affiliated
))
3792 ((looking-at "\\+BEGIN_\\(\\S-+\\)")
3794 (let ((parser (assoc (upcase (match-string 1))
3795 org-element-block-name-alist
)))
3796 (if parser
(funcall (cdr parser
) limit affiliated
)
3797 (org-element-special-block-parser limit affiliated
))))
3798 ((looking-at "\\+CALL:")
3800 (org-element-babel-call-parser limit affiliated
))
3801 ((looking-at "\\+BEGIN:? ")
3803 (org-element-dynamic-block-parser limit affiliated
))
3804 ((looking-at "\\+\\S-+:")
3806 (org-element-keyword-parser limit affiliated
))
3809 (org-element-paragraph-parser limit affiliated
))))
3810 ;; Footnote Definition.
3811 ((looking-at org-footnote-definition-re
)
3812 (org-element-footnote-definition-parser limit affiliated
))
3814 ((looking-at "[ \t]*-\\{5,\\}[ \t]*$")
3815 (org-element-horizontal-rule-parser limit affiliated
))
3818 (org-element-diary-sexp-parser limit affiliated
))
3820 ((org-at-table-p t
) (org-element-table-parser limit affiliated
))
3822 ((looking-at (org-item-re))
3823 (org-element-plain-list-parser
3824 limit affiliated
(or structure
(org-list-struct))))
3825 ;; Default element: Paragraph.
3826 (t (org-element-paragraph-parser limit affiliated
)))))))))
3829 ;; Most elements can have affiliated keywords. When looking for an
3830 ;; element beginning, we want to move before them, as they belong to
3831 ;; that element, and, in the meantime, collect information they give
3832 ;; into appropriate properties. Hence the following function.
3834 (defun org-element--collect-affiliated-keywords ()
3835 "Collect affiliated keywords from point.
3837 Return a list whose CAR is the position at the first of them and
3838 CDR a plist of keywords and values and move point to the
3839 beginning of the first line after them.
3841 As a special case, if element doesn't start at the beginning of
3842 the line (i.e. a paragraph starting an item), CAR is current
3843 position of point and CDR is nil."
3844 (if (not (bolp)) (list (point))
3845 (let ((case-fold-search t
)
3847 ;; RESTRICT is the list of objects allowed in parsed
3849 (restrict (org-element-restriction 'keyword
))
3851 (while (and (not (eobp)) (looking-at org-element--affiliated-re
))
3852 (let* ((raw-kwd (upcase (match-string 1)))
3853 ;; Apply translation to RAW-KWD. From there, KWD is
3854 ;; the official keyword.
3855 (kwd (or (cdr (assoc raw-kwd
3856 org-element-keyword-translation-alist
))
3858 ;; Find main value for any keyword.
3862 (buffer-substring-no-properties
3863 (match-end 0) (point-at-eol)))))
3864 ;; PARSEDP is non-nil when keyword should have its
3866 (parsedp (member kwd org-element-parsed-keywords
))
3867 ;; If KWD is a dual keyword, find its secondary
3868 ;; value. Maybe parse it.
3869 (dualp (member kwd org-element-dual-keywords
))
3872 (let ((sec (org-match-string-no-properties 2)))
3873 (if (or (not sec
) (not parsedp
)) sec
3874 (org-element-parse-secondary-string sec restrict
)))))
3875 ;; Attribute a property name to KWD.
3876 (kwd-sym (and kwd
(intern (concat ":" (downcase kwd
))))))
3877 ;; Now set final shape for VALUE.
3879 (setq value
(org-element-parse-secondary-string value restrict
)))
3881 (setq value
(and (or value dual-value
) (cons value dual-value
))))
3882 (when (or (member kwd org-element-multiple-keywords
)
3883 ;; Attributes can always appear on multiple lines.
3884 (string-match "^ATTR_" kwd
))
3885 (setq value
(cons value
(plist-get output kwd-sym
))))
3886 ;; Eventually store the new value in OUTPUT.
3887 (setq output
(plist-put output kwd-sym value
))
3888 ;; Move to next keyword.
3890 ;; If affiliated keywords are orphaned: move back to first one.
3891 ;; They will be parsed as a paragraph.
3892 (when (looking-at "[ \t]*$") (goto-char origin
) (setq output nil
))
3894 (cons origin output
))))
3900 ;; The two major functions here are `org-element-parse-buffer', which
3901 ;; parses Org syntax inside the current buffer, taking into account
3902 ;; region, narrowing, or even visibility if specified, and
3903 ;; `org-element-parse-secondary-string', which parses objects within
3906 ;; The (almost) almighty `org-element-map' allows to apply a function
3907 ;; on elements or objects matching some type, and accumulate the
3908 ;; resulting values. In an export situation, it also skips unneeded
3909 ;; parts of the parse tree.
3911 (defun org-element-parse-buffer (&optional granularity visible-only
)
3912 "Recursively parse the buffer and return structure.
3913 If narrowing is in effect, only parse the visible part of the
3916 Optional argument GRANULARITY determines the depth of the
3917 recursion. It can be set to the following symbols:
3919 `headline' Only parse headlines.
3920 `greater-element' Don't recurse into greater elements excepted
3921 headlines and sections. Thus, elements
3922 parsed are the top-level ones.
3923 `element' Parse everything but objects and plain text.
3924 `object' Parse the complete buffer (default).
3926 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3929 Assume buffer is in Org mode."
3931 (goto-char (point-min))
3932 (org-skip-whitespace)
3933 (org-element--parse-elements
3934 (point-at-bol) (point-max)
3935 ;; Start in `first-section' mode so text before the first
3936 ;; headline belongs to a section.
3937 'first-section nil granularity visible-only
(list 'org-data nil
))))
3939 (defun org-element-parse-secondary-string (string restriction
&optional parent
)
3940 "Recursively parse objects in STRING and return structure.
3942 RESTRICTION is a symbol limiting the object types that will be
3945 Optional argument PARENT, when non-nil, is the element or object
3946 containing the secondary string. It is used to set correctly
3947 `:parent' property within the string."
3950 (let ((secondary (org-element--parse-objects
3951 (point-min) (point-max) nil restriction
)))
3953 (mapc (lambda (obj) (org-element-put-property obj
:parent parent
))
3957 (defun org-element-map
3958 (data types fun
&optional info first-match no-recursion with-affiliated
)
3959 "Map a function on selected elements or objects.
3961 DATA is the parsed tree, as returned by, i.e,
3962 `org-element-parse-buffer'. TYPES is a symbol or list of symbols
3963 of elements or objects types. FUN is the function called on the
3964 matching element or object. It must accept one argument: the
3965 element or object itself.
3967 When optional argument INFO is non-nil, it should be a plist
3968 holding export options. In that case, parts of the parse tree
3969 not exportable according to that property list will be skipped.
3971 When optional argument FIRST-MATCH is non-nil, stop at the first
3972 match for which FUN doesn't return nil, and return that value.
3974 Optional argument NO-RECURSION is a symbol or a list of symbols
3975 representing elements or objects types. `org-element-map' won't
3976 enter any recursive element or object whose type belongs to that
3977 list. Though, FUN can still be applied on them.
3979 When optional argument WITH-AFFILIATED is non-nil, also move into
3980 affiliated keywords to find objects.
3982 Nil values returned from FUN do not appear in the results."
3983 ;; Ensure TYPES and NO-RECURSION are a list, even of one element.
3984 (unless (listp types
) (setq types
(list types
)))
3985 (unless (listp no-recursion
) (setq no-recursion
(list no-recursion
)))
3986 ;; Recursion depth is determined by --CATEGORY.
3989 (let ((category 'greater-elements
))
3990 (mapc (lambda (type)
3991 (cond ((or (memq type org-element-all-objects
)
3992 (eq type
'plain-text
))
3993 ;; If one object is found, the function
3994 ;; has to recurse into every object.
3995 (throw 'found
'objects
))
3996 ((not (memq type org-element-greater-elements
))
3997 ;; If one regular element is found, the
3998 ;; function has to recurse, at least,
3999 ;; into every element it encounters.
4000 (and (not (eq category
'elements
))
4001 (setq category
'elements
)))))
4004 ;; Compute properties for affiliated keywords if necessary.
4006 (and with-affiliated
4007 (mapcar (lambda (kwd)
4008 (cons kwd
(intern (concat ":" (downcase kwd
)))))
4009 org-element-affiliated-keywords
)))
4015 ;; Recursively walk DATA. INFO, if non-nil, is a plist
4016 ;; holding contextual information.
4017 (let ((--type (org-element-type --data
)))
4020 ;; Ignored element in an export context.
4021 ((and info
(memq --data
(plist-get info
:ignore-list
))))
4022 ;; Secondary string: only objects can be found there.
4024 (when (eq --category
'objects
) (mapc --walk-tree --data
)))
4025 ;; Unconditionally enter parse trees.
4026 ((eq --type
'org-data
)
4027 (mapc --walk-tree
(org-element-contents --data
)))
4029 ;; Check if TYPE is matching among TYPES. If so,
4030 ;; apply FUN to --DATA and accumulate return value
4031 ;; into --ACC (or exit if FIRST-MATCH is non-nil).
4032 (when (memq --type types
)
4033 (let ((result (funcall fun --data
)))
4034 (cond ((not result
))
4035 (first-match (throw '--map-first-match result
))
4036 (t (push result --acc
)))))
4037 ;; If --DATA has a secondary string that can contain
4038 ;; objects with their type among TYPES, look into it.
4039 (when (and (eq --category
'objects
) (not (stringp --data
)))
4041 (assq --type org-element-secondary-value-alist
)))
4043 (funcall --walk-tree
4044 (org-element-property (cdr sec-prop
) --data
)))))
4045 ;; If --DATA has any affiliated keywords and
4046 ;; WITH-AFFILIATED is non-nil, look for objects in
4048 (when (and with-affiliated
4049 (eq --category
'objects
)
4050 (memq --type org-element-all-elements
))
4051 (mapc (lambda (kwd-pair)
4052 (let ((kwd (car kwd-pair
))
4053 (value (org-element-property
4054 (cdr kwd-pair
) --data
)))
4055 ;; Pay attention to the type of value.
4056 ;; Preserve order for multiple keywords.
4059 ((and (member kwd org-element-multiple-keywords
)
4060 (member kwd org-element-dual-keywords
))
4061 (mapc (lambda (line)
4062 (funcall --walk-tree
(cdr line
))
4063 (funcall --walk-tree
(car line
)))
4065 ((member kwd org-element-multiple-keywords
)
4066 (mapc (lambda (line) (funcall --walk-tree line
))
4068 ((member kwd org-element-dual-keywords
)
4069 (funcall --walk-tree
(cdr value
))
4070 (funcall --walk-tree
(car value
)))
4071 (t (funcall --walk-tree value
)))))
4072 --affiliated-alist
))
4073 ;; Determine if a recursion into --DATA is possible.
4075 ;; --TYPE is explicitly removed from recursion.
4076 ((memq --type no-recursion
))
4077 ;; --DATA has no contents.
4078 ((not (org-element-contents --data
)))
4079 ;; Looking for greater elements but --DATA is simply
4080 ;; an element or an object.
4081 ((and (eq --category
'greater-elements
)
4082 (not (memq --type org-element-greater-elements
))))
4083 ;; Looking for elements but --DATA is an object.
4084 ((and (eq --category
'elements
)
4085 (memq --type org-element-all-objects
)))
4086 ;; In any other case, map contents.
4087 (t (mapc --walk-tree
(org-element-contents --data
)))))))))))
4088 (catch '--map-first-match
4089 (funcall --walk-tree data
)
4090 ;; Return value in a proper order.
4093 ;; The following functions are internal parts of the parser.
4095 ;; The first one, `org-element--parse-elements' acts at the element's
4098 ;; The second one, `org-element--parse-objects' applies on all objects
4099 ;; of a paragraph or a secondary string. It uses
4100 ;; `org-element--get-next-object-candidates' to optimize the search of
4101 ;; the next object in the buffer.
4103 ;; More precisely, that function looks for every allowed object type
4104 ;; first. Then, it discards failed searches, keeps further matches,
4105 ;; and searches again types matched behind point, for subsequent
4106 ;; calls. Thus, searching for a given type fails only once, and every
4107 ;; object is searched only once at top level (but sometimes more for
4110 (defun org-element--parse-elements
4111 (beg end special structure granularity visible-only acc
)
4112 "Parse elements between BEG and END positions.
4114 SPECIAL prioritize some elements over the others. It can be set
4115 to `first-section', `quote-section', `section' `item' or
4118 When value is `item', STRUCTURE will be used as the current list
4121 GRANULARITY determines the depth of the recursion. See
4122 `org-element-parse-buffer' for more information.
4124 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
4127 Elements are accumulated into ACC."
4130 ;; When parsing only headlines, skip any text before first one.
4131 (when (and (eq granularity
'headline
) (not (org-at-heading-p)))
4132 (org-with-limited-levels (outline-next-heading)))
4134 (while (< (point) end
)
4135 ;; Find current element's type and parse it accordingly to
4137 (let* ((element (org-element--current-element
4138 end granularity special structure
))
4139 (type (org-element-type element
))
4140 (cbeg (org-element-property :contents-begin element
)))
4141 (goto-char (org-element-property :end element
))
4142 ;; Fill ELEMENT contents by side-effect.
4144 ;; If VISIBLE-ONLY is true and element is hidden or if it has
4145 ;; no contents, don't modify it.
4146 ((or (and visible-only
(org-element-property :hiddenp element
))
4148 ;; Greater element: parse it between `contents-begin' and
4149 ;; `contents-end'. Make sure GRANULARITY allows the
4150 ;; recursion, or ELEMENT is an headline, in which case going
4151 ;; inside is mandatory, in order to get sub-level headings.
4152 ((and (memq type org-element-greater-elements
)
4153 (or (memq granularity
'(element object nil
))
4154 (and (eq granularity
'greater-element
)
4156 (eq type
'headline
)))
4157 (org-element--parse-elements
4158 cbeg
(org-element-property :contents-end element
)
4159 ;; Possibly switch to a special mode.
4162 (if (org-element-property :quotedp element
) 'quote-section
4165 (property-drawer 'node-property
)
4167 (org-element-property :structure element
)
4168 granularity visible-only element
))
4169 ;; ELEMENT has contents. Parse objects inside, if
4170 ;; GRANULARITY allows it.
4171 ((memq granularity
'(object nil
))
4172 (org-element--parse-objects
4173 cbeg
(org-element-property :contents-end element
) element
4174 (org-element-restriction type
))))
4175 (org-element-adopt-elements acc element
)))
4179 (defun org-element--parse-objects (beg end acc restriction
)
4180 "Parse objects between BEG and END and return recursive structure.
4182 Objects are accumulated in ACC.
4184 RESTRICTION is a list of object types which are allowed in the
4189 (while (and (< (point) end
)
4190 (setq candidates
(org-element--get-next-object-candidates
4191 end restriction candidates
)))
4193 (let ((pos (apply 'min
(mapcar 'cdr candidates
))))
4196 (funcall (intern (format "org-element-%s-parser"
4197 (car (rassq pos candidates
)))))))))
4198 ;; 1. Text before any object. Untabify it.
4199 (let ((obj-beg (org-element-property :begin next-object
)))
4200 (unless (= (point) obj-beg
)
4202 (org-element-adopt-elements
4204 (replace-regexp-in-string
4205 "\t" (make-string tab-width ?
)
4206 (buffer-substring-no-properties (point) obj-beg
))))))
4208 (let ((obj-end (org-element-property :end next-object
))
4209 (cont-beg (org-element-property :contents-begin next-object
)))
4210 ;; Fill contents of NEXT-OBJECT by side-effect, if it has
4211 ;; a recursive type.
4213 (memq (car next-object
) org-element-recursive-objects
))
4217 (org-element-property :contents-end next-object
))
4218 (org-element--parse-objects
4219 (point-min) (point-max) next-object
4220 (org-element-restriction next-object
))))
4221 (setq acc
(org-element-adopt-elements acc next-object
))
4222 (goto-char obj-end
))))
4223 ;; 3. Text after last object. Untabify it.
4224 (unless (= (point) end
)
4226 (org-element-adopt-elements
4228 (replace-regexp-in-string
4229 "\t" (make-string tab-width ?
)
4230 (buffer-substring-no-properties (point) end
)))))
4234 (defun org-element--get-next-object-candidates (limit restriction objects
)
4235 "Return an alist of candidates for the next object.
4237 LIMIT bounds the search, and RESTRICTION narrows candidates to
4240 Return value is an alist whose CAR is position and CDR the object
4243 OBJECTS is the previous candidates alist."
4244 ;; Filter out any object found but not belonging to RESTRICTION.
4248 (let ((type (car obj
)))
4249 (memq (or (cdr (assq type org-element-object-successor-alist
))
4253 (let (next-candidates types-to-search
)
4254 ;; If no previous result, search every object type in RESTRICTION.
4255 ;; Otherwise, keep potential candidates (old objects located after
4256 ;; point) and ask to search again those which had matched before.
4257 (if (not objects
) (setq types-to-search restriction
)
4259 (if (< (cdr obj
) (point)) (push (car obj
) types-to-search
)
4260 (push obj next-candidates
)))
4262 ;; Call the appropriate successor function for each type to search
4263 ;; and accumulate matches.
4266 (let* ((successor-fun
4268 (format "org-element-%s-successor"
4269 (or (cdr (assq type org-element-object-successor-alist
))
4271 (obj (funcall successor-fun limit
)))
4272 (and obj
(push obj next-candidates
))))
4279 ;;; Towards A Bijective Process
4281 ;; The parse tree obtained with `org-element-parse-buffer' is really
4282 ;; a snapshot of the corresponding Org buffer. Therefore, it can be
4283 ;; interpreted and expanded into a string with canonical Org syntax.
4284 ;; Hence `org-element-interpret-data'.
4286 ;; The function relies internally on
4287 ;; `org-element--interpret-affiliated-keywords'.
4290 (defun org-element-interpret-data (data &optional parent
)
4291 "Interpret DATA as Org syntax.
4293 DATA is a parse tree, an element, an object or a secondary string
4296 Optional argument PARENT is used for recursive calls. It contains
4297 the element or object containing data, or nil.
4299 Return Org syntax as a string."
4300 (let* ((type (org-element-type data
))
4303 ;; Secondary string.
4306 (lambda (obj) (org-element-interpret-data obj parent
))
4308 ;; Full Org document.
4309 ((eq type
'org-data
)
4311 (lambda (obj) (org-element-interpret-data obj parent
))
4312 (org-element-contents data
) ""))
4314 ((stringp data
) data
)
4315 ;; Element/Object without contents.
4316 ((not (org-element-contents data
))
4317 (funcall (intern (format "org-element-%s-interpreter" type
))
4319 ;; Element/Object with contents.
4321 (let* ((greaterp (memq type org-element-greater-elements
))
4322 (objectp (and (not greaterp
)
4323 (memq type org-element-recursive-objects
)))
4326 (lambda (obj) (org-element-interpret-data obj data
))
4327 (org-element-contents
4328 (if (or greaterp objectp
) data
4329 ;; Elements directly containing objects must
4330 ;; have their indentation normalized first.
4331 (org-element-normalize-contents
4333 ;; When normalizing first paragraph of an
4334 ;; item or a footnote-definition, ignore
4335 ;; first line's indentation.
4336 (and (eq type
'paragraph
)
4337 (equal data
(car (org-element-contents parent
)))
4338 (memq (org-element-type parent
)
4339 '(footnote-definition item
))))))
4341 (funcall (intern (format "org-element-%s-interpreter" type
))
4343 (if greaterp
(org-element-normalize-contents contents
)
4345 (if (memq type
'(org-data plain-text nil
)) results
4346 ;; Build white spaces. If no `:post-blank' property is
4347 ;; specified, assume its value is 0.
4348 (let ((post-blank (or (org-element-property :post-blank data
) 0)))
4349 (if (memq type org-element-all-objects
)
4350 (concat results
(make-string post-blank
32))
4352 (org-element--interpret-affiliated-keywords data
)
4353 (org-element-normalize-string results
)
4354 (make-string post-blank
10)))))))
4356 (defun org-element--interpret-affiliated-keywords (element)
4357 "Return ELEMENT's affiliated keywords as Org syntax.
4358 If there is no affiliated keyword, return the empty string."
4359 (let ((keyword-to-org
4363 (when (member key org-element-dual-keywords
)
4364 (setq dual
(cdr value
) value
(car value
)))
4367 (format "[%s]" (org-element-interpret-data dual
)))
4369 (if (member key org-element-parsed-keywords
)
4370 (org-element-interpret-data value
)
4375 (let ((value (org-element-property prop element
))
4376 (keyword (upcase (substring (symbol-name prop
) 1))))
4378 (if (or (member keyword org-element-multiple-keywords
)
4379 ;; All attribute keywords can have multiple lines.
4380 (string-match "^ATTR_" keyword
))
4381 (mapconcat (lambda (line) (funcall keyword-to-org keyword line
))
4384 (funcall keyword-to-org keyword value
)))))
4385 ;; List all ELEMENT's properties matching an attribute line or an
4386 ;; affiliated keyword, but ignore translated keywords since they
4387 ;; cannot belong to the property list.
4388 (loop for prop in
(nth 1 element
) by
'cddr
4389 when
(let ((keyword (upcase (substring (symbol-name prop
) 1))))
4390 (or (string-match "^ATTR_" keyword
)
4392 (member keyword org-element-affiliated-keywords
)
4394 org-element-keyword-translation-alist
)))))
4398 ;; Because interpretation of the parse tree must return the same
4399 ;; number of blank lines between elements and the same number of white
4400 ;; space after objects, some special care must be given to white
4403 ;; The first function, `org-element-normalize-string', ensures any
4404 ;; string different from the empty string will end with a single
4405 ;; newline character.
4407 ;; The second function, `org-element-normalize-contents', removes
4408 ;; global indentation from the contents of the current element.
4410 (defun org-element-normalize-string (s)
4411 "Ensure string S ends with a single newline character.
4413 If S isn't a string return it unchanged. If S is the empty
4414 string, return it. Otherwise, return a new string with a single
4415 newline character at its end."
4417 ((not (stringp s
)) s
)
4419 (t (and (string-match "\\(\n[ \t]*\\)*\\'" s
)
4420 (replace-match "\n" nil nil s
)))))
4422 (defun org-element-normalize-contents (element &optional ignore-first
)
4423 "Normalize plain text in ELEMENT's contents.
4425 ELEMENT must only contain plain text and objects.
4427 If optional argument IGNORE-FIRST is non-nil, ignore first line's
4428 indentation to compute maximal common indentation.
4430 Return the normalized element that is element with global
4431 indentation removed from its contents. The function assumes that
4432 indentation is not done with TAB characters."
4433 (let* (ind-list ; for byte-compiler
4434 collect-inds
; for byte-compiler
4437 ;; Return list of indentations within BLOB. This is done by
4438 ;; walking recursively BLOB and updating IND-LIST along the
4439 ;; way. FIRST-FLAG is non-nil when the first string hasn't
4440 ;; been seen yet. It is required as this string is the only
4441 ;; one whose indentation doesn't happen after a newline
4443 (lambda (blob first-flag
)
4446 (when (and first-flag
(stringp object
))
4447 (setq first-flag nil
)
4448 (string-match "\\`\\( *\\)" object
)
4449 (let ((len (length (match-string 1 object
))))
4450 ;; An indentation of zero means no string will be
4451 ;; modified. Quit the process.
4452 (if (zerop len
) (throw 'zero
(setq ind-list nil
))
4453 (push len ind-list
))))
4457 ;; Avoid matching blank or empty lines.
4458 (while (and (string-match "\n\\( *\\)\\(.\\)" object start
)
4459 (not (equal (match-string 2 object
) " ")))
4460 (setq start
(match-end 0))
4461 (push (length (match-string 1 object
)) ind-list
))))
4462 ((memq (org-element-type object
) org-element-recursive-objects
)
4463 (funcall collect-inds object first-flag
))))
4464 (org-element-contents blob
))))))
4465 ;; Collect indentation list in ELEMENT. Possibly remove first
4466 ;; value if IGNORE-FIRST is non-nil.
4467 (catch 'zero
(funcall collect-inds element
(not ignore-first
)))
4468 (if (not ind-list
) element
4469 ;; Build ELEMENT back, replacing each string with the same
4470 ;; string minus common indentation.
4471 (let* (build ; For byte compiler.
4474 (lambda (blob mci first-flag
)
4475 ;; Return BLOB with all its strings indentation
4476 ;; shortened from MCI white spaces. FIRST-FLAG is
4477 ;; non-nil when the first string hasn't been seen
4482 (when (and first-flag
(stringp object
))
4483 (setq first-flag nil
)
4485 (replace-regexp-in-string
4486 (format "\\` \\{%d\\}" mci
) "" object
)))
4489 (replace-regexp-in-string
4490 (format "\n \\{%d\\}" mci
) "\n" object
))
4491 ((memq (org-element-type object
)
4492 org-element-recursive-objects
)
4493 (funcall build object mci first-flag
))
4495 (org-element-contents blob
)))
4497 (funcall build element
(apply 'min ind-list
) (not ignore-first
))))))
4503 ;; The first move is to implement a way to obtain the smallest element
4504 ;; containing point. This is the job of `org-element-at-point'. It
4505 ;; basically jumps back to the beginning of section containing point
4506 ;; and moves, element after element, with
4507 ;; `org-element--current-element' until the container is found. Note:
4508 ;; When using `org-element-at-point', secondary values are never
4509 ;; parsed since the function focuses on elements, not on objects.
4511 ;; At a deeper level, `org-element-context' lists all elements and
4512 ;; objects containing point.
4514 ;; `org-element-nested-p' and `org-element-swap-A-B' may be used
4515 ;; internally by navigation and manipulation tools.
4518 (defun org-element-at-point (&optional keep-trail
)
4519 "Determine closest element around point.
4521 Return value is a list like (TYPE PROPS) where TYPE is the type
4522 of the element and PROPS a plist of properties associated to the
4525 Possible types are defined in `org-element-all-elements'.
4526 Properties depend on element or object type, but always
4527 include :begin, :end, :parent and :post-blank properties.
4529 As a special case, if point is at the very beginning of a list or
4530 sub-list, returned element will be that list instead of the first
4531 item. In the same way, if point is at the beginning of the first
4532 row of a table, returned element will be the table instead of the
4535 If optional argument KEEP-TRAIL is non-nil, the function returns
4536 a list of of elements leading to element at point. The list's
4537 CAR is always the element at point. Following positions contain
4538 element's siblings, then parents, siblings of parents, until the
4539 first element of current section."
4540 (org-with-wide-buffer
4541 ;; If at an headline, parse it. It is the sole element that
4542 ;; doesn't require to know about context. Be sure to disallow
4543 ;; secondary string parsing, though.
4544 (if (org-with-limited-levels (org-at-heading-p))
4547 (if (not keep-trail
) (org-element-headline-parser (point-max) t
)
4548 (list (org-element-headline-parser (point-max) t
))))
4549 ;; Otherwise move at the beginning of the section containing
4551 (let ((origin (point))
4552 (end (save-excursion
4553 (org-with-limited-levels (outline-next-heading)) (point)))
4554 element type special-flag trail struct prevs parent
)
4555 (org-with-limited-levels
4556 (if (org-with-limited-levels (org-before-first-heading-p))
4557 (goto-char (point-min))
4558 (org-back-to-heading)
4560 (org-skip-whitespace)
4562 ;; Parse successively each element, skipping those ending
4563 ;; before original position.
4567 (org-element--current-element end
'element special-flag struct
)
4569 (org-element-put-property element
:parent parent
)
4570 (when keep-trail
(push element trail
))
4572 ;; 1. Skip any element ending before point. Also skip
4573 ;; element ending at point when we're sure that another
4574 ;; element has started.
4575 ((let ((elem-end (org-element-property :end element
)))
4576 (when (or (< elem-end origin
)
4577 (and (= elem-end origin
) (/= elem-end end
)))
4578 (goto-char elem-end
))))
4579 ;; 2. An element containing point is always the element at
4581 ((not (memq type org-element-greater-elements
))
4582 (throw 'exit
(if keep-trail trail element
)))
4583 ;; 3. At any other greater element type, if point is
4584 ;; within contents, move into it.
4586 (let ((cbeg (org-element-property :contents-begin element
))
4587 (cend (org-element-property :contents-end element
)))
4588 (if (or (not cbeg
) (not cend
) (> cbeg origin
) (< cend origin
)
4589 ;; Create an anchor for tables and plain lists:
4590 ;; when point is at the very beginning of these
4591 ;; elements, ignoring affiliated keywords,
4592 ;; target them instead of their contents.
4593 (and (= cbeg origin
) (memq type
'(plain-list table
)))
4594 ;; When point is at contents end, do not move
4595 ;; into elements with an explicit ending, but
4596 ;; return that element instead.
4597 (and (= cend origin
)
4600 drawer dynamic-block inlinetask item
4601 plain-list property-drawer quote-block
4603 (throw 'exit
(if keep-trail trail element
))
4604 (setq parent element
)
4607 (setq special-flag
'item
4608 struct
(org-element-property :structure element
)))
4609 (property-drawer (setq special-flag
'node-property
))
4610 (table (setq special-flag
'table-row
))
4611 (otherwise (setq special-flag nil
)))
4613 (goto-char cbeg
)))))))))))
4616 (defun org-element-context ()
4617 "Return closest element or object around point.
4619 Return value is a list like (TYPE PROPS) where TYPE is the type
4620 of the element or object and PROPS a plist of properties
4623 Possible types are defined in `org-element-all-elements' and
4624 `org-element-all-objects'. Properties depend on element or
4625 object type, but always include :begin, :end, :parent
4626 and :post-blank properties."
4627 (org-with-wide-buffer
4628 (let* ((origin (point))
4629 (element (org-element-at-point))
4630 (type (car element
))
4632 ;; Check if point is inside an element containing objects or at
4633 ;; a secondary string. In that case, move to beginning of the
4634 ;; element or secondary string and set END to the other side.
4635 (if (not (or (let ((post (org-element-property :post-affiliated element
)))
4636 (and post
(> post origin
)
4637 (< (org-element-property :begin element
) origin
)
4638 (progn (beginning-of-line)
4639 (looking-at org-element--affiliated-re
)
4640 (member (upcase (match-string 1))
4641 org-element-parsed-keywords
))
4642 ;; We're at an affiliated keyword. Change
4643 ;; type to retrieve correct restrictions.
4644 (setq type
'keyword
)
4645 ;; Determine if we're at main or dual value.
4646 (if (and (match-end 2) (<= origin
(match-end 2)))
4647 (progn (goto-char (match-beginning 2))
4648 (setq end
(match-end 2)))
4649 (goto-char (match-end 0))
4650 (setq end
(line-end-position)))))
4651 (and (eq type
'item
)
4652 (let ((tag (org-element-property :tag element
)))
4656 (search-forward tag
(point-at-eol))
4657 (goto-char (match-beginning 0))
4658 (and (>= origin
(point))
4660 ;; `1+' is required so some
4661 ;; successors can match
4662 ;; properly their object.
4663 (setq end
(1+ (match-end 0)))))))))
4664 (and (memq type
'(headline inlinetask
))
4665 (progn (beginning-of-line)
4666 (skip-chars-forward "* ")
4667 (setq end
(point-at-eol))))
4668 (and (memq type
'(paragraph table-row verse-block
))
4669 (let ((cbeg (org-element-property
4670 :contents-begin element
))
4671 (cend (org-element-property
4672 :contents-end element
)))
4673 (and (>= origin cbeg
)
4675 (progn (goto-char cbeg
) (setq end cend
)))))
4676 (and (eq type
'keyword
)
4677 (let ((key (org-element-property :key element
)))
4678 (and (member key org-element-document-properties
)
4679 (progn (beginning-of-line)
4680 (search-forward key
(line-end-position) t
)
4682 (setq end
(line-end-position))))))))
4684 (let ((restriction (org-element-restriction type
))
4688 (while (setq candidates
(org-element--get-next-object-candidates
4689 end restriction candidates
))
4690 (let ((closest-cand (rassq (apply 'min
(mapcar 'cdr candidates
))
4692 ;; If ORIGIN is before next object in element, there's
4693 ;; no point in looking further.
4694 (if (> (cdr closest-cand
) origin
) (throw 'exit parent
)
4696 (progn (goto-char (cdr closest-cand
))
4697 (funcall (intern (format "org-element-%s-parser"
4698 (car closest-cand
))))))
4699 (cbeg (org-element-property :contents-begin object
))
4700 (cend (org-element-property :contents-end object
)))
4702 ;; ORIGIN is after OBJECT, so skip it.
4703 ((< (org-element-property :end object
) origin
)
4704 (goto-char (org-element-property :end object
)))
4705 ;; ORIGIN is within a non-recursive object or at an
4706 ;; object boundaries: Return that object.
4707 ((or (not cbeg
) (> cbeg origin
) (< cend origin
))
4709 (org-element-put-property object
:parent parent
)))
4710 ;; Otherwise, move within current object and restrict
4711 ;; search to the end of its contents.
4713 (org-element-put-property object
:parent parent
)
4715 restriction
(org-element-restriction object
)
4719 (defsubst org-element-nested-p
(elem-A elem-B
)
4720 "Non-nil when elements ELEM-A and ELEM-B are nested."
4721 (let ((beg-A (org-element-property :begin elem-A
))
4722 (beg-B (org-element-property :begin elem-B
))
4723 (end-A (org-element-property :end elem-A
))
4724 (end-B (org-element-property :end elem-B
)))
4725 (or (and (>= beg-A beg-B
) (<= end-A end-B
))
4726 (and (>= beg-B beg-A
) (<= end-B end-A
)))))
4728 (defun org-element-swap-A-B (elem-A elem-B
)
4729 "Swap elements ELEM-A and ELEM-B.
4730 Assume ELEM-B is after ELEM-A in the buffer. Leave point at the
4732 (goto-char (org-element-property :begin elem-A
))
4733 ;; There are two special cases when an element doesn't start at bol:
4734 ;; the first paragraph in an item or in a footnote definition.
4735 (let ((specialp (not (bolp))))
4736 ;; Only a paragraph without any affiliated keyword can be moved at
4737 ;; ELEM-A position in such a situation. Note that the case of
4738 ;; a footnote definition is impossible: it cannot contain two
4739 ;; paragraphs in a row because it cannot contain a blank line.
4741 (or (not (eq (org-element-type elem-B
) 'paragraph
))
4742 (/= (org-element-property :begin elem-B
)
4743 (org-element-property :contents-begin elem-B
))))
4744 (error "Cannot swap elements"))
4745 ;; In a special situation, ELEM-A will have no indentation. We'll
4746 ;; give it ELEM-B's (which will in, in turn, have no indentation).
4747 (let* ((ind-B (when specialp
4748 (goto-char (org-element-property :begin elem-B
))
4749 (org-get-indentation)))
4750 (beg-A (org-element-property :begin elem-A
))
4751 (end-A (save-excursion
4752 (goto-char (org-element-property :end elem-A
))
4753 (skip-chars-backward " \r\t\n")
4755 (beg-B (org-element-property :begin elem-B
))
4756 (end-B (save-excursion
4757 (goto-char (org-element-property :end elem-B
))
4758 (skip-chars-backward " \r\t\n")
4760 ;; Store overlays responsible for visibility status. We
4761 ;; also need to store their boundaries as they will be
4762 ;; removed from buffer.
4765 (mapcar (lambda (ov) (list ov
(overlay-start ov
) (overlay-end ov
)))
4766 (overlays-in beg-A end-A
))
4767 (mapcar (lambda (ov) (list ov
(overlay-start ov
) (overlay-end ov
)))
4768 (overlays-in beg-B end-B
))))
4770 (body-A (buffer-substring beg-A end-A
))
4771 (body-B (delete-and-extract-region beg-B end-B
)))
4774 (setq body-B
(replace-regexp-in-string "\\`[ \t]*" "" body-B
))
4775 (org-indent-to-column ind-B
))
4777 ;; Restore ex ELEM-A overlays.
4778 (let ((offset (- beg-B beg-A
)))
4781 (car ov
) (+ (nth 1 ov
) offset
) (+ (nth 2 ov
) offset
)))
4784 (delete-region beg-A end-A
)
4786 ;; Restore ex ELEM-B overlays.
4789 (car ov
) (- (nth 1 ov
) offset
) (- (nth 2 ov
) offset
)))
4791 (goto-char (org-element-property :end elem-B
)))))
4793 (provide 'org-element
)
4796 ;; generated-autoload-file: "org-loaddefs.el"
4799 ;;; org-element.el ends here