1 ;;; org-element.el --- Parser And Applications for Org syntax
3 ;; Copyright (C) 2012-2013 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; Org syntax can be divided into three categories: "Greater
26 ;; elements", "Elements" and "Objects".
28 ;; Elements are related to the structure of the document. Indeed, all
29 ;; elements are a cover for the document: each position within belongs
30 ;; to at least one element.
32 ;; An element always starts and ends at the beginning of a line. With
33 ;; a few exceptions (`clock', `headline', `inlinetask', `item',
34 ;; `planning', `node-property', `quote-section' `section' and
35 ;; `table-row' types), it can also accept a fixed set of keywords as
36 ;; attributes. Those are called "affiliated keywords" to distinguish
37 ;; them from other keywords, which are full-fledged elements. Almost
38 ;; all affiliated keywords are referenced in
39 ;; `org-element-affiliated-keywords'; the others are export attributes
40 ;; and start with "ATTR_" prefix.
42 ;; Element containing other elements (and only elements) are called
43 ;; greater elements. Concerned types are: `center-block', `drawer',
44 ;; `dynamic-block', `footnote-definition', `headline', `inlinetask',
45 ;; `item', `plain-list', `property-drawer', `quote-block', `section'
46 ;; and `special-block'.
48 ;; Other element types are: `babel-call', `clock', `comment',
49 ;; `comment-block', `diary-sexp', `example-block', `export-block',
50 ;; `fixed-width', `horizontal-rule', `keyword', `latex-environment',
51 ;; `node-property', `paragraph', `planning', `quote-section',
52 ;; `src-block', `table', `table-row' and `verse-block'. Among them,
53 ;; `paragraph' and `verse-block' types can contain Org objects and
56 ;; Objects are related to document's contents. Some of them are
57 ;; recursive. Associated types are of the following: `bold', `code',
58 ;; `entity', `export-snippet', `footnote-reference',
59 ;; `inline-babel-call', `inline-src-block', `italic',
60 ;; `latex-fragment', `line-break', `link', `macro', `radio-target',
61 ;; `statistics-cookie', `strike-through', `subscript', `superscript',
62 ;; `table-cell', `target', `timestamp', `underline' and `verbatim'.
64 ;; Some elements also have special properties whose value can hold
65 ;; objects themselves (i.e. an item tag or a headline name). Such
66 ;; values are called "secondary strings". Any object belongs to
67 ;; either an element or a secondary string.
69 ;; Notwithstanding affiliated keywords, each greater element, element
70 ;; and object has a fixed set of properties attached to it. Among
71 ;; them, four are shared by all types: `:begin' and `:end', which
72 ;; refer to the beginning and ending buffer positions of the
73 ;; considered element or object, `:post-blank', which holds the number
74 ;; of blank lines, or white spaces, at its end and `:parent' which
75 ;; refers to the element or object containing it. Greater elements,
76 ;; elements and objects containing objects will also have
77 ;; `:contents-begin' and `:contents-end' properties to delimit
78 ;; contents. Eventually, greater elements and elements accepting
79 ;; affiliated keywords will have a `:post-affiliated' property,
80 ;; referring to the buffer position after all such keywords.
82 ;; At the lowest level, a `:parent' property is also attached to any
83 ;; string, as a text property.
85 ;; Lisp-wise, an element or an object can be represented as a list.
86 ;; It follows the pattern (TYPE PROPERTIES CONTENTS), where:
87 ;; TYPE is a symbol describing the Org element or object.
88 ;; PROPERTIES is the property list attached to it. See docstring of
89 ;; appropriate parsing function to get an exhaustive
91 ;; CONTENTS is a list of elements, objects or raw strings contained
92 ;; in the current element or object, when applicable.
94 ;; An Org buffer is a nested list of such elements and objects, whose
95 ;; type is `org-data' and properties is nil.
97 ;; The first part of this file defines Org syntax, while the second
98 ;; one provide accessors and setters functions.
100 ;; The next part implements a parser and an interpreter for each
101 ;; element and object type in Org syntax.
103 ;; The following part creates a fully recursive buffer parser. It
104 ;; also provides a tool to map a function to elements or objects
105 ;; matching some criteria in the parse tree. Functions of interest
106 ;; are `org-element-parse-buffer', `org-element-map' and, to a lesser
107 ;; extent, `org-element-parse-secondary-string'.
109 ;; The penultimate part is the cradle of an interpreter for the
110 ;; obtained parse tree: `org-element-interpret-data'.
112 ;; The library ends by furnishing `org-element-at-point' function, and
113 ;; a way to give information about document structure around point
114 ;; with `org-element-context'.
119 (eval-when-compile (require 'cl
))
124 ;;; Definitions And Rules
126 ;; Define elements, greater elements and specify recursive objects,
127 ;; along with the affiliated keywords recognized. Also set up
128 ;; restrictions on recursive objects combinations.
130 ;; These variables really act as a control center for the parsing
133 (defconst org-element-paragraph-separate
135 ;; Headlines, inlinetasks.
136 org-outline-regexp
"\\|"
137 ;; Footnote definitions.
138 "\\[\\(?:[0-9]+\\|fn:[-_[:word:]]+\\)\\]" "\\|"
144 ;; Tables (any type).
145 "\\(?:|\\|\\+-[-+]\\)" "\\|"
146 ;; Blocks (any type), Babel calls, drawers (any type),
147 ;; fixed-width areas and keywords. Note: this is only an
148 ;; indication and need some thorough check.
151 "-\\{5,\\}[ \t]*$" "\\|"
152 ;; LaTeX environments.
153 "\\\\begin{\\([A-Za-z0-9]+\\*?\\)}" "\\|"
154 ;; Planning and Clock lines.
155 (regexp-opt (list org-scheduled-string
161 (let ((term (case org-plain-list-ordered-item-terminator
162 (?\
) ")") (?.
"\\.") (otherwise "[.)]")))
163 (alpha (and org-list-allow-alphabetical
"\\|[A-Za-z]")))
164 (concat "\\(?:[-+*]\\|\\(?:[0-9]+" alpha
"\\)" term
"\\)"
165 "\\(?:[ \t]\\|$\\)"))
167 "Regexp to separate paragraphs in an Org buffer.
168 In the case of lines starting with \"#\" and \":\", this regexp
169 is not sufficient to know if point is at a paragraph ending. See
170 `org-element-paragraph-parser' for more information.")
172 (defconst org-element-all-elements
173 '(babel-call center-block clock comment comment-block diary-sexp drawer
174 dynamic-block example-block export-block fixed-width
175 footnote-definition headline horizontal-rule inlinetask item
176 keyword latex-environment node-property paragraph plain-list
177 planning property-drawer quote-block quote-section section
178 special-block src-block table table-row verse-block
)
179 "Complete list of element types.")
181 (defconst org-element-greater-elements
182 '(center-block drawer dynamic-block footnote-definition headline inlinetask
183 item plain-list property-drawer quote-block section
185 "List of recursive element types aka Greater Elements.")
187 (defconst org-element-all-successors
188 '(export-snippet footnote-reference inline-babel-call inline-src-block
189 latex-or-entity line-break link macro plain-link radio-target
190 statistics-cookie sub
/superscript table-cell target
191 text-markup timestamp
)
192 "Complete list of successors.")
194 (defconst org-element-object-successor-alist
195 '((subscript . sub
/superscript
) (superscript . sub
/superscript
)
196 (bold . text-markup
) (code . text-markup
) (italic . text-markup
)
197 (strike-through . text-markup
) (underline . text-markup
)
198 (verbatim . text-markup
) (entity . latex-or-entity
)
199 (latex-fragment . latex-or-entity
))
200 "Alist of translations between object type and successor name.
201 Sharing the same successor comes handy when, for example, the
202 regexp matching one object can also match the other object.")
204 (defconst org-element-all-objects
205 '(bold code entity export-snippet footnote-reference inline-babel-call
206 inline-src-block italic line-break latex-fragment link macro
207 radio-target statistics-cookie strike-through subscript superscript
208 table-cell target timestamp underline verbatim
)
209 "Complete list of object types.")
211 (defconst org-element-recursive-objects
212 '(bold italic link subscript radio-target strike-through superscript
213 table-cell underline
)
214 "List of recursive object types.")
216 (defvar org-element-block-name-alist
217 '(("CENTER" . org-element-center-block-parser
)
218 ("COMMENT" . org-element-comment-block-parser
)
219 ("EXAMPLE" . org-element-example-block-parser
)
220 ("QUOTE" . org-element-quote-block-parser
)
221 ("SRC" . org-element-src-block-parser
)
222 ("VERSE" . org-element-verse-block-parser
))
223 "Alist between block names and the associated parsing function.
224 Names must be uppercase. Any block whose name has no association
225 is parsed with `org-element-special-block-parser'.")
227 (defconst org-element-link-type-is-file
228 '("file" "file+emacs" "file+sys" "docview")
229 "List of link types equivalent to \"file\".
230 Only these types can accept search options and an explicit
231 application to open them.")
233 (defconst org-element-affiliated-keywords
234 '("CAPTION" "DATA" "HEADER" "HEADERS" "LABEL" "NAME" "PLOT" "RESNAME" "RESULT"
235 "RESULTS" "SOURCE" "SRCNAME" "TBLNAME")
236 "List of affiliated keywords as strings.
237 By default, all keywords setting attributes (i.e. \"ATTR_LATEX\")
238 are affiliated keywords and need not to be in this list.")
240 (defconst org-element--affiliated-re
241 (format "[ \t]*#\\+%s:"
242 ;; Regular affiliated keywords.
243 (format "\\(%s\\|ATTR_[-_A-Za-z0-9]+\\)\\(?:\\[\\(.*\\)\\]\\)?"
244 (regexp-opt org-element-affiliated-keywords
)))
245 "Regexp matching any affiliated keyword.
247 Keyword name is put in match group 1. Moreover, if keyword
248 belongs to `org-element-dual-keywords', put the dual value in
251 Don't modify it, set `org-element-affiliated-keywords' instead.")
253 (defconst org-element-keyword-translation-alist
254 '(("DATA" .
"NAME") ("LABEL" .
"NAME") ("RESNAME" .
"NAME")
255 ("SOURCE" .
"NAME") ("SRCNAME" .
"NAME") ("TBLNAME" .
"NAME")
256 ("RESULT" .
"RESULTS") ("HEADERS" .
"HEADER"))
257 "Alist of usual translations for keywords.
258 The key is the old name and the value the new one. The property
259 holding their value will be named after the translated name.")
261 (defconst org-element-multiple-keywords
'("CAPTION" "HEADER")
262 "List of affiliated keywords that can occur more than once in an element.
264 Their value will be consed into a list of strings, which will be
265 returned as the value of the property.
267 This list is checked after translations have been applied. See
268 `org-element-keyword-translation-alist'.
270 By default, all keywords setting attributes (i.e. \"ATTR_LATEX\")
271 allow multiple occurrences and need not to be in this list.")
273 (defconst org-element-parsed-keywords
'("CAPTION")
274 "List of affiliated keywords whose value can be parsed.
276 Their value will be stored as a secondary string: a list of
279 This list is checked after translations have been applied. See
280 `org-element-keyword-translation-alist'.")
282 (defconst org-element-dual-keywords
'("CAPTION" "RESULTS")
283 "List of affiliated keywords which can have a secondary value.
285 In Org syntax, they can be written with optional square brackets
286 before the colons. For example, RESULTS keyword can be
287 associated to a hash value with the following:
289 #+RESULTS[hash-string]: some-source
291 This list is checked after translations have been applied. See
292 `org-element-keyword-translation-alist'.")
294 (defconst org-element-document-properties
'("AUTHOR" "DATE" "TITLE")
295 "List of properties associated to the whole document.
296 Any keyword in this list will have its value parsed and stored as
297 a secondary string.")
299 (defconst org-element-object-restrictions
301 (remq 'plain-link
(remq 'table-cell org-element-all-successors
)))
302 (standard-set-no-line-break (remq 'line-break standard-set
)))
303 `((bold ,@standard-set
)
304 (footnote-reference ,@standard-set
)
305 (headline ,@standard-set-no-line-break
)
306 (inlinetask ,@standard-set-no-line-break
)
307 (italic ,@standard-set
)
308 (item ,@standard-set-no-line-break
)
309 (keyword ,@standard-set
)
310 ;; Ignore all links excepted plain links in a link description.
311 ;; Also ignore radio-targets and line breaks.
312 (link export-snippet inline-babel-call inline-src-block latex-or-entity
313 macro plain-link statistics-cookie sub
/superscript text-markup
)
314 (paragraph ,@standard-set
)
315 ;; Remove any variable object from radio target as it would
316 ;; prevent it from being properly recognized.
317 (radio-target latex-or-entity sub
/superscript
)
318 (strike-through ,@standard-set
)
319 (subscript ,@standard-set
)
320 (superscript ,@standard-set
)
321 ;; Ignore inline babel call and inline src block as formulas are
322 ;; possible. Also ignore line breaks and statistics cookies.
323 (table-cell export-snippet footnote-reference latex-or-entity link macro
324 radio-target sub
/superscript target text-markup timestamp
)
325 (table-row table-cell
)
326 (underline ,@standard-set
)
327 (verse-block ,@standard-set
)))
328 "Alist of objects restrictions.
330 CAR is an element or object type containing objects and CDR is
331 a list of successors that will be called within an element or
334 For example, in a `radio-target' object, one can only find
335 entities, latex-fragments, subscript and superscript.
337 This alist also applies to secondary string. For example, an
338 `headline' type element doesn't directly contain objects, but
339 still has an entry since one of its properties (`:title') does.")
341 (defconst org-element-secondary-value-alist
342 '((headline .
:title
)
343 (inlinetask .
:title
)
345 (footnote-reference .
:inline-definition
))
346 "Alist between element types and location of secondary value.")
348 (defconst org-element-object-variables
'(org-link-abbrev-alist-local)
349 "List of buffer-local variables used when parsing objects.
350 These variables are copied to the temporary buffer created by
351 `org-export-secondary-string'.")
355 ;;; Accessors and Setters
357 ;; Provide four accessors: `org-element-type', `org-element-property'
358 ;; `org-element-contents' and `org-element-restriction'.
360 ;; Setter functions allow to modify elements by side effect. There is
361 ;; `org-element-put-property', `org-element-set-contents',
362 ;; `org-element-set-element' and `org-element-adopt-element'. Note
363 ;; that `org-element-set-element' and `org-element-adopt-elements' are
364 ;; higher level functions since also update `:parent' property.
366 (defsubst org-element-type
(element)
367 "Return type of ELEMENT.
369 The function returns the type of the element or object provided.
370 It can also return the following special value:
371 `plain-text' for a string
372 `org-data' for a complete document
373 nil in any other case."
375 ((not (consp element
)) (and (stringp element
) 'plain-text
))
376 ((symbolp (car element
)) (car element
))))
378 (defsubst org-element-property
(property element
)
379 "Extract the value from the PROPERTY of an ELEMENT."
380 (if (stringp element
) (get-text-property 0 property element
)
381 (plist-get (nth 1 element
) property
)))
383 (defsubst org-element-contents
(element)
384 "Extract contents from an ELEMENT."
385 (cond ((not (consp element
)) nil
)
386 ((symbolp (car element
)) (nthcdr 2 element
))
389 (defsubst org-element-restriction
(element)
390 "Return restriction associated to ELEMENT.
391 ELEMENT can be an element, an object or a symbol representing an
392 element or object type."
393 (cdr (assq (if (symbolp element
) element
(org-element-type element
))
394 org-element-object-restrictions
)))
396 (defsubst org-element-put-property
(element property value
)
397 "In ELEMENT set PROPERTY to VALUE.
398 Return modified element."
399 (if (stringp element
) (org-add-props element nil property value
)
400 (setcar (cdr element
) (plist-put (nth 1 element
) property value
))
403 (defsubst org-element-set-contents
(element &rest contents
)
404 "Set ELEMENT contents to CONTENTS.
405 Return modified element."
406 (cond ((not element
) (list contents
))
407 ((not (symbolp (car element
))) contents
)
408 ((cdr element
) (setcdr (cdr element
) contents
))
409 (t (nconc element contents
))))
411 (defsubst org-element-set-element
(old new
)
412 "Replace element or object OLD with element or object NEW.
413 The function takes care of setting `:parent' property for NEW."
414 ;; Since OLD is going to be changed into NEW by side-effect, first
415 ;; make sure that every element or object within NEW has OLD as
417 (mapc (lambda (blob) (org-element-put-property blob
:parent old
))
418 (org-element-contents new
))
419 ;; Transfer contents.
420 (apply 'org-element-set-contents old
(org-element-contents new
))
421 ;; Ensure NEW has same parent as OLD, then overwrite OLD properties
423 (org-element-put-property new
:parent
(org-element-property :parent old
))
424 (setcar (cdr old
) (nth 1 new
))
426 (setcar old
(car new
)))
428 (defsubst org-element-adopt-elements
(parent &rest children
)
429 "Append elements to the contents of another element.
431 PARENT is an element or object. CHILDREN can be elements,
432 objects, or a strings.
434 The function takes care of setting `:parent' property for CHILD.
435 Return parent element."
436 ;; Link every child to PARENT. If PARENT is nil, it is a secondary
437 ;; string: parent is the list itself.
438 (mapc (lambda (child)
439 (org-element-put-property child
:parent
(or parent children
)))
441 ;; Add CHILDREN at the end of PARENT contents.
443 (apply 'org-element-set-contents
445 (nconc (org-element-contents parent
) children
)))
446 ;; Return modified PARENT element.
447 (or parent children
))
453 ;; For each greater element type, we define a parser and an
456 ;; A parser returns the element or object as the list described above.
457 ;; Most of them accepts no argument. Though, exceptions exist. Hence
458 ;; every element containing a secondary string (see
459 ;; `org-element-secondary-value-alist') will accept an optional
460 ;; argument to toggle parsing of that secondary string. Moreover,
461 ;; `item' parser requires current list's structure as its first
464 ;; An interpreter accepts two arguments: the list representation of
465 ;; the element or object, and its contents. The latter may be nil,
466 ;; depending on the element or object considered. It returns the
467 ;; appropriate Org syntax, as a string.
469 ;; Parsing functions must follow the naming convention:
470 ;; org-element-TYPE-parser, where TYPE is greater element's type, as
471 ;; defined in `org-element-greater-elements'.
473 ;; Similarly, interpreting functions must follow the naming
474 ;; convention: org-element-TYPE-interpreter.
476 ;; With the exception of `headline' and `item' types, greater elements
477 ;; cannot contain other greater elements of their own type.
479 ;; Beside implementing a parser and an interpreter, adding a new
480 ;; greater element requires to tweak `org-element--current-element'.
481 ;; Moreover, the newly defined type must be added to both
482 ;; `org-element-all-elements' and `org-element-greater-elements'.
487 (defun org-element-center-block-parser (limit affiliated
)
488 "Parse a center block.
490 LIMIT bounds the search. AFFILIATED is a list of which CAR is
491 the buffer position at the beginning of the first affiliated
492 keyword and CDR is a plist of affiliated keywords along with
495 Return a list whose CAR is `center-block' and CDR is a plist
496 containing `:begin', `:end', `:hiddenp', `:contents-begin',
497 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
499 Assume point is at the beginning of the block."
500 (let ((case-fold-search t
))
501 (if (not (save-excursion
502 (re-search-forward "^[ \t]*#\\+END_CENTER[ \t]*$" limit t
)))
503 ;; Incomplete block: parse it as a paragraph.
504 (org-element-paragraph-parser limit affiliated
)
505 (let ((block-end-line (match-beginning 0)))
506 (let* ((begin (car affiliated
))
507 (post-affiliated (point))
508 ;; Empty blocks have no contents.
509 (contents-begin (progn (forward-line)
510 (and (< (point) block-end-line
)
512 (contents-end (and contents-begin block-end-line
))
513 (hidden (org-invisible-p2))
514 (pos-before-blank (progn (goto-char block-end-line
)
517 (end (save-excursion (skip-chars-forward " \r\t\n" limit
)
518 (skip-chars-backward " \t")
519 (if (bolp) (point) (line-end-position)))))
525 :contents-begin contents-begin
526 :contents-end contents-end
527 :post-blank
(count-lines pos-before-blank end
)
528 :post-affiliated post-affiliated
)
529 (cdr affiliated
))))))))
531 (defun org-element-center-block-interpreter (center-block contents
)
532 "Interpret CENTER-BLOCK element as Org syntax.
533 CONTENTS is the contents of the element."
534 (format "#+BEGIN_CENTER\n%s#+END_CENTER" contents
))
539 (defun org-element-drawer-parser (limit affiliated
)
542 LIMIT bounds the search. AFFILIATED is a list of which CAR is
543 the buffer position at the beginning of the first affiliated
544 keyword and CDR is a plist of affiliated keywords along with
547 Return a list whose CAR is `drawer' and CDR is a plist containing
548 `:drawer-name', `:begin', `:end', `:hiddenp', `:contents-begin',
549 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
551 Assume point is at beginning of drawer."
552 (let ((case-fold-search t
))
553 (if (not (save-excursion (re-search-forward "^[ \t]*:END:[ \t]*$" limit t
)))
554 ;; Incomplete drawer: parse it as a paragraph.
555 (org-element-paragraph-parser limit affiliated
)
557 (let* ((drawer-end-line (match-beginning 0))
558 (name (progn (looking-at org-drawer-regexp
)
559 (org-match-string-no-properties 1)))
560 (begin (car affiliated
))
561 (post-affiliated (point))
562 ;; Empty drawers have no contents.
563 (contents-begin (progn (forward-line)
564 (and (< (point) drawer-end-line
)
566 (contents-end (and contents-begin drawer-end-line
))
567 (hidden (org-invisible-p2))
568 (pos-before-blank (progn (goto-char drawer-end-line
)
571 (end (progn (skip-chars-forward " \r\t\n" limit
)
572 (skip-chars-backward " \t")
573 (if (bolp) (point) (line-end-position)))))
580 :contents-begin contents-begin
581 :contents-end contents-end
582 :post-blank
(count-lines pos-before-blank end
)
583 :post-affiliated post-affiliated
)
584 (cdr affiliated
))))))))
586 (defun org-element-drawer-interpreter (drawer contents
)
587 "Interpret DRAWER element as Org syntax.
588 CONTENTS is the contents of the element."
589 (format ":%s:\n%s:END:"
590 (org-element-property :drawer-name drawer
)
596 (defun org-element-dynamic-block-parser (limit affiliated
)
597 "Parse a dynamic block.
599 LIMIT bounds the search. AFFILIATED is a list of which CAR is
600 the buffer position at the beginning of the first affiliated
601 keyword and CDR is a plist of affiliated keywords along with
604 Return a list whose CAR is `dynamic-block' and CDR is a plist
605 containing `:block-name', `:begin', `:end', `:hiddenp',
606 `:contents-begin', `:contents-end', `:arguments', `:post-blank'
607 and `:post-affiliated' keywords.
609 Assume point is at beginning of dynamic block."
610 (let ((case-fold-search t
))
611 (if (not (save-excursion
612 (re-search-forward "^[ \t]*#\\+END:?[ \t]*$" limit t
)))
613 ;; Incomplete block: parse it as a paragraph.
614 (org-element-paragraph-parser limit affiliated
)
615 (let ((block-end-line (match-beginning 0)))
617 (let* ((name (progn (looking-at org-dblock-start-re
)
618 (org-match-string-no-properties 1)))
619 (arguments (org-match-string-no-properties 3))
620 (begin (car affiliated
))
621 (post-affiliated (point))
622 ;; Empty blocks have no contents.
623 (contents-begin (progn (forward-line)
624 (and (< (point) block-end-line
)
626 (contents-end (and contents-begin block-end-line
))
627 (hidden (org-invisible-p2))
628 (pos-before-blank (progn (goto-char block-end-line
)
631 (end (progn (skip-chars-forward " \r\t\n" limit
)
632 (skip-chars-backward " \t")
633 (if (bolp) (point) (line-end-position)))))
641 :contents-begin contents-begin
642 :contents-end contents-end
643 :post-blank
(count-lines pos-before-blank end
)
644 :post-affiliated post-affiliated
)
645 (cdr affiliated
)))))))))
647 (defun org-element-dynamic-block-interpreter (dynamic-block contents
)
648 "Interpret DYNAMIC-BLOCK element as Org syntax.
649 CONTENTS is the contents of the element."
650 (format "#+BEGIN: %s%s\n%s#+END:"
651 (org-element-property :block-name dynamic-block
)
652 (let ((args (org-element-property :arguments dynamic-block
)))
653 (and args
(concat " " args
)))
657 ;;;; Footnote Definition
659 (defun org-element-footnote-definition-parser (limit affiliated
)
660 "Parse a footnote definition.
662 LIMIT bounds the search. AFFILIATED is a list of which CAR is
663 the buffer position at the beginning of the first affiliated
664 keyword and CDR is a plist of affiliated keywords along with
667 Return a list whose CAR is `footnote-definition' and CDR is
668 a plist containing `:label', `:begin' `:end', `:contents-begin',
669 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
671 Assume point is at the beginning of the footnote definition."
673 (let* ((label (progn (looking-at org-footnote-definition-re
)
674 (org-match-string-no-properties 1)))
675 (begin (car affiliated
))
676 (post-affiliated (point))
677 (ending (save-excursion
681 (concat org-outline-regexp-bol
"\\|"
682 org-footnote-definition-re
"\\|"
683 "^\\([ \t]*\n\\)\\{2,\\}") limit
'move
))
686 (contents-begin (progn (search-forward "]")
687 (skip-chars-forward " \r\t\n" ending
)
688 (and (/= (point) ending
) (point))))
689 (contents-end (and contents-begin ending
))
690 (end (progn (goto-char ending
)
691 (skip-chars-forward " \r\t\n" limit
)
692 (skip-chars-backward " \t")
693 (if (bolp) (point) (line-end-position)))))
694 (list 'footnote-definition
699 :contents-begin contents-begin
700 :contents-end contents-end
701 :post-blank
(count-lines ending end
)
702 :post-affiliated post-affiliated
)
703 (cdr affiliated
))))))
705 (defun org-element-footnote-definition-interpreter (footnote-definition contents
)
706 "Interpret FOOTNOTE-DEFINITION element as Org syntax.
707 CONTENTS is the contents of the footnote-definition."
708 (concat (format "[%s]" (org-element-property :label footnote-definition
))
715 (defun org-element-headline-parser (limit &optional raw-secondary-p
)
718 Return a list whose CAR is `headline' and CDR is a plist
719 containing `:raw-value', `:title', `:alt-title', `:begin',
720 `:end', `:pre-blank', `:hiddenp', `:contents-begin' and
721 `:contents-end', `:level', `:priority', `:tags',
722 `:todo-keyword',`:todo-type', `:scheduled', `:deadline',
723 `:closed', `:quotedp', `:archivedp', `:commentedp' and
724 `:footnote-section-p' keywords.
726 The plist also contains any property set in the property drawer,
727 with its name in upper cases and colons added at the
728 beginning (i.e. `:CUSTOM_ID').
730 When RAW-SECONDARY-P is non-nil, headline's title will not be
731 parsed as a secondary string, but as a plain string instead.
733 Assume point is at beginning of the headline."
735 (let* ((components (org-heading-components))
736 (level (nth 1 components
))
737 (todo (nth 2 components
))
739 (and todo
(if (member todo org-done-keywords
) 'done
'todo
)))
740 (tags (let ((raw-tags (nth 5 components
)))
741 (and raw-tags
(org-split-string raw-tags
":"))))
742 (raw-value (or (nth 4 components
) ""))
744 (let ((case-fold-search nil
))
745 (string-match (format "^%s\\( \\|$\\)" org-quote-string
)
748 (let ((case-fold-search nil
))
749 (string-match (format "^%s\\( \\|$\\)" org-comment-string
)
751 (archivedp (member org-archive-tag tags
))
752 (footnote-section-p (and org-footnote-section
753 (string= org-footnote-section raw-value
)))
754 ;; Upcase property names. It avoids confusion between
755 ;; properties obtained through property drawer and default
756 ;; properties from the parser (e.g. `:end' and :END:)
763 (intern (concat ":" (upcase (car p
))))
765 (org-entry-properties nil
'standard
))
768 ;; Read time properties on the line below the headline.
770 (when (progn (forward-line)
771 (looking-at org-planning-or-clock-line-re
))
772 (let ((end (line-end-position)) plist
)
773 (while (re-search-forward
774 org-keyword-time-not-clock-regexp end t
)
775 (goto-char (match-end 1))
776 (skip-chars-forward " \t")
777 (let ((keyword (match-string 1))
778 (time (org-element-timestamp-parser)))
779 (cond ((equal keyword org-scheduled-string
)
780 (setq plist
(plist-put plist
:scheduled time
)))
781 ((equal keyword org-deadline-string
)
782 (setq plist
(plist-put plist
:deadline time
)))
783 (t (setq plist
(plist-put plist
:closed time
))))))
786 (end (save-excursion (goto-char (org-end-of-subtree t t
))))
787 (pos-after-head (progn (forward-line) (point)))
788 (contents-begin (save-excursion
789 (skip-chars-forward " \r\t\n" end
)
790 (and (/= (point) end
) (line-beginning-position))))
791 (hidden (org-invisible-p2))
792 (contents-end (and contents-begin
793 (progn (goto-char end
)
794 (skip-chars-backward " \r\t\n")
797 ;; Clean RAW-VALUE from any quote or comment string.
798 (when (or quotedp commentedp
)
799 (let ((case-fold-search nil
))
801 (replace-regexp-in-string
803 (regexp-opt (list org-quote-string org-comment-string
))
807 ;; Clean TAGS from archive tag, if any.
808 (when archivedp
(setq tags
(delete org-archive-tag tags
)))
812 (list :raw-value raw-value
816 (if (not contents-begin
) 0
817 (count-lines pos-after-head contents-begin
))
819 :contents-begin contents-begin
820 :contents-end contents-end
822 :priority
(nth 3 components
)
826 :post-blank
(count-lines
827 (if (not contents-end
) pos-after-head
828 (goto-char contents-end
)
832 :footnote-section-p footnote-section-p
834 :commentedp commentedp
838 (let ((alt-title (org-element-property :ALT_TITLE headline
)))
840 (org-element-put-property
842 (if raw-secondary-p alt-title
843 (org-element-parse-secondary-string
844 alt-title
(org-element-restriction 'headline
) headline
)))))
845 (org-element-put-property
847 (if raw-secondary-p raw-value
848 (org-element-parse-secondary-string
849 raw-value
(org-element-restriction 'headline
) headline
)))))))
851 (defun org-element-headline-interpreter (headline contents
)
852 "Interpret HEADLINE element as Org syntax.
853 CONTENTS is the contents of the element."
854 (let* ((level (org-element-property :level headline
))
855 (todo (org-element-property :todo-keyword headline
))
856 (priority (org-element-property :priority headline
))
857 (title (org-element-interpret-data
858 (org-element-property :title headline
)))
859 (tags (let ((tag-list (if (org-element-property :archivedp headline
)
860 (cons org-archive-tag
861 (org-element-property :tags headline
))
862 (org-element-property :tags headline
))))
864 (format ":%s:" (mapconcat 'identity tag-list
":")))))
865 (commentedp (org-element-property :commentedp headline
))
866 (quotedp (org-element-property :quotedp headline
))
867 (pre-blank (or (org-element-property :pre-blank headline
) 0))
868 (heading (concat (make-string level ?
*)
869 (and todo
(concat " " todo
))
870 (and quotedp
(concat " " org-quote-string
))
871 (and commentedp
(concat " " org-comment-string
))
873 (format " [#%s]" (char-to-string priority
)))
874 (cond ((and org-footnote-section
875 (org-element-property
876 :footnote-section-p headline
))
877 (concat " " org-footnote-section
))
878 (title (concat " " title
))))))
883 ((zerop org-tags-column
) (format " %s" tags
))
884 ((< org-tags-column
0)
887 (max (- (+ org-tags-column
(length heading
) (length tags
))) 1)
892 (make-string (max (- org-tags-column
(length heading
)) 1) ?
)
894 (make-string (1+ pre-blank
) 10)
900 (defun org-element-inlinetask-parser (limit &optional raw-secondary-p
)
901 "Parse an inline task.
903 Return a list whose CAR is `inlinetask' and CDR is a plist
904 containing `:title', `:begin', `:end', `:hiddenp',
905 `:contents-begin' and `:contents-end', `:level', `:priority',
906 `:raw-value', `:tags', `:todo-keyword', `:todo-type',
907 `:scheduled', `:deadline', `:closed' and `:post-blank' keywords.
909 The plist also contains any property set in the property drawer,
910 with its name in upper cases and colons added at the
911 beginning (i.e. `:CUSTOM_ID').
913 When optional argument RAW-SECONDARY-P is non-nil, inline-task's
914 title will not be parsed as a secondary string, but as a plain
917 Assume point is at beginning of the inline task."
919 (let* ((begin (point))
920 (components (org-heading-components))
921 (todo (nth 2 components
))
923 (if (member todo org-done-keywords
) 'done
'todo
)))
924 (tags (let ((raw-tags (nth 5 components
)))
925 (and raw-tags
(org-split-string raw-tags
":"))))
926 (raw-value (or (nth 4 components
) ""))
927 ;; Upcase property names. It avoids confusion between
928 ;; properties obtained through property drawer and default
929 ;; properties from the parser (e.g. `:end' and :END:)
936 (intern (concat ":" (upcase (car p
))))
938 (org-entry-properties nil
'standard
))
941 ;; Read time properties on the line below the inlinetask
944 (when (progn (forward-line)
945 (looking-at org-planning-or-clock-line-re
))
946 (let ((end (line-end-position)) plist
)
947 (while (re-search-forward
948 org-keyword-time-not-clock-regexp end t
)
949 (goto-char (match-end 1))
950 (skip-chars-forward " \t")
951 (let ((keyword (match-string 1))
952 (time (org-element-timestamp-parser)))
953 (cond ((equal keyword org-scheduled-string
)
954 (setq plist
(plist-put plist
:scheduled time
)))
955 ((equal keyword org-deadline-string
)
956 (setq plist
(plist-put plist
:deadline time
)))
957 (t (setq plist
(plist-put plist
:closed time
))))))
959 (task-end (save-excursion
961 (and (re-search-forward "^\\*+ END" limit t
)
962 (match-beginning 0))))
963 (contents-begin (progn (forward-line)
964 (and task-end
(< (point) task-end
) (point))))
965 (hidden (and contents-begin
(org-invisible-p2)))
966 (contents-end (and contents-begin task-end
))
967 (before-blank (if (not task-end
) (point)
971 (end (progn (skip-chars-forward " \r\t\n" limit
)
972 (skip-chars-backward " \t")
973 (if (bolp) (point) (line-end-position))))
977 (list :raw-value raw-value
981 :contents-begin contents-begin
982 :contents-end contents-end
983 :level
(nth 1 components
)
984 :priority
(nth 3 components
)
988 :post-blank
(count-lines before-blank end
))
991 (org-element-put-property
993 (if raw-secondary-p raw-value
994 (org-element-parse-secondary-string
996 (org-element-restriction 'inlinetask
)
999 (defun org-element-inlinetask-interpreter (inlinetask contents
)
1000 "Interpret INLINETASK element as Org syntax.
1001 CONTENTS is the contents of inlinetask."
1002 (let* ((level (org-element-property :level inlinetask
))
1003 (todo (org-element-property :todo-keyword inlinetask
))
1004 (priority (org-element-property :priority inlinetask
))
1005 (title (org-element-interpret-data
1006 (org-element-property :title inlinetask
)))
1007 (tags (let ((tag-list (org-element-property :tags inlinetask
)))
1009 (format ":%s:" (mapconcat 'identity tag-list
":")))))
1010 (task (concat (make-string level ?
*)
1011 (and todo
(concat " " todo
))
1013 (format " [#%s]" (char-to-string priority
)))
1014 (and title
(concat " " title
)))))
1019 ((zerop org-tags-column
) (format " %s" tags
))
1020 ((< org-tags-column
0)
1023 (max (- (+ org-tags-column
(length task
) (length tags
))) 1)
1028 (make-string (max (- org-tags-column
(length task
)) 1) ?
)
1030 ;; Prefer degenerate inlinetasks when there are no
1035 (make-string level ?
*) " END")))))
1040 (defun org-element-item-parser (limit struct
&optional raw-secondary-p
)
1043 STRUCT is the structure of the plain list.
1045 Return a list whose CAR is `item' and CDR is a plist containing
1046 `:bullet', `:begin', `:end', `:contents-begin', `:contents-end',
1047 `:checkbox', `:counter', `:tag', `:structure', `:hiddenp' and
1048 `:post-blank' keywords.
1050 When optional argument RAW-SECONDARY-P is non-nil, item's tag, if
1051 any, will not be parsed as a secondary string, but as a plain
1054 Assume point is at the beginning of the item."
1057 (looking-at org-list-full-item-re
)
1058 (let* ((begin (point))
1059 (bullet (org-match-string-no-properties 1))
1060 (checkbox (let ((box (org-match-string-no-properties 3)))
1061 (cond ((equal "[ ]" box
) 'off
)
1062 ((equal "[X]" box
) 'on
)
1063 ((equal "[-]" box
) 'trans
))))
1064 (counter (let ((c (org-match-string-no-properties 2)))
1068 ((string-match "[A-Za-z]" c
)
1069 (- (string-to-char (upcase (match-string 0 c
)))
1071 ((string-match "[0-9]+" c
)
1072 (string-to-number (match-string 0 c
)))))))
1073 (end (save-excursion (goto-char (org-list-get-item-end begin struct
))
1074 (unless (bolp) (forward-line))
1078 ;; Ignore tags in un-ordered lists: they are just
1079 ;; a part of item's body.
1080 (if (and (match-beginning 4)
1081 (save-match-data (string-match "[.)]" bullet
)))
1084 (skip-chars-forward " \r\t\n" limit
)
1085 ;; If first line isn't empty, contents really start
1086 ;; at the text after item's meta-data.
1087 (if (= (point-at-bol) begin
) (point) (point-at-bol))))
1088 (hidden (progn (forward-line)
1089 (and (not (= (point) end
)) (org-invisible-p2))))
1090 (contents-end (progn (goto-char end
)
1091 (skip-chars-backward " \r\t\n")
1096 (list :bullet bullet
1099 ;; CONTENTS-BEGIN and CONTENTS-END may be
1100 ;; mixed up in the case of an empty item
1101 ;; separated from the next by a blank line.
1102 ;; Thus ensure the former is always the
1104 :contents-begin
(min contents-begin contents-end
)
1105 :contents-end
(max contents-begin contents-end
)
1110 :post-blank
(count-lines contents-end end
)))))
1111 (org-element-put-property
1113 (let ((raw-tag (org-list-get-tag begin struct
)))
1115 (if raw-secondary-p raw-tag
1116 (org-element-parse-secondary-string
1117 raw-tag
(org-element-restriction 'item
) item
))))))))
1119 (defun org-element-item-interpreter (item contents
)
1120 "Interpret ITEM element as Org syntax.
1121 CONTENTS is the contents of the element."
1122 (let* ((bullet (org-list-bullet-string (org-element-property :bullet item
)))
1123 (checkbox (org-element-property :checkbox item
))
1124 (counter (org-element-property :counter item
))
1125 (tag (let ((tag (org-element-property :tag item
)))
1126 (and tag
(org-element-interpret-data tag
))))
1127 ;; Compute indentation.
1128 (ind (make-string (length bullet
) 32))
1129 (item-starts-with-par-p
1130 (eq (org-element-type (car (org-element-contents item
)))
1135 (and counter
(format "[@%d] " counter
))
1140 (and tag
(format "%s :: " tag
))
1141 (let ((contents (replace-regexp-in-string
1142 "\\(^\\)[ \t]*\\S-" ind contents nil nil
1)))
1143 (if item-starts-with-par-p
(org-trim contents
)
1144 (concat "\n" contents
))))))
1149 (defun org-element-plain-list-parser (limit affiliated structure
)
1150 "Parse a plain list.
1152 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1153 the buffer position at the beginning of the first affiliated
1154 keyword and CDR is a plist of affiliated keywords along with
1155 their value. STRUCTURE is the structure of the plain list being
1158 Return a list whose CAR is `plain-list' and CDR is a plist
1159 containing `:type', `:begin', `:end', `:contents-begin' and
1160 `:contents-end', `:structure', `:post-blank' and
1161 `:post-affiliated' keywords.
1163 Assume point is at the beginning of the list."
1165 (let* ((struct (or structure
(org-list-struct)))
1166 (prevs (org-list-prevs-alist struct
))
1167 (parents (org-list-parents-alist struct
))
1168 (type (org-list-get-list-type (point) struct prevs
))
1169 (contents-begin (point))
1170 (begin (car affiliated
))
1172 (progn (goto-char (org-list-get-list-end (point) struct prevs
))
1173 (unless (bolp) (forward-line))
1175 (end (progn (skip-chars-forward " \r\t\n" limit
)
1176 (skip-chars-backward " \t")
1177 (if (bolp) (point) (line-end-position)))))
1184 :contents-begin contents-begin
1185 :contents-end contents-end
1187 :post-blank
(count-lines contents-end end
)
1188 :post-affiliated contents-begin
)
1189 (cdr affiliated
))))))
1191 (defun org-element-plain-list-interpreter (plain-list contents
)
1192 "Interpret PLAIN-LIST element as Org syntax.
1193 CONTENTS is the contents of the element."
1196 (goto-char (point-min))
1201 ;;;; Property Drawer
1203 (defun org-element-property-drawer-parser (limit affiliated
)
1204 "Parse a property drawer.
1206 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1207 the buffer position at the beginning of the first affiliated
1208 keyword and CDR is a plist of affiliated keywords along with
1211 Return a list whose CAR is `property-drawer' and CDR is a plist
1212 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1213 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
1215 Assume point is at the beginning of the property drawer."
1217 (let ((case-fold-search t
))
1218 (if (not (save-excursion
1219 (re-search-forward "^[ \t]*:END:[ \t]*$" limit t
)))
1220 ;; Incomplete drawer: parse it as a paragraph.
1221 (org-element-paragraph-parser limit affiliated
)
1223 (let* ((drawer-end-line (match-beginning 0))
1224 (begin (car affiliated
))
1225 (post-affiliated (point))
1226 (contents-begin (progn (forward-line)
1227 (and (< (point) drawer-end-line
)
1229 (contents-end (and contents-begin drawer-end-line
))
1230 (hidden (org-invisible-p2))
1231 (pos-before-blank (progn (goto-char drawer-end-line
)
1234 (end (progn (skip-chars-forward " \r\t\n" limit
)
1235 (skip-chars-backward " \t")
1236 (if (bolp) (point) (line-end-position)))))
1237 (list 'property-drawer
1242 :contents-begin contents-begin
1243 :contents-end contents-end
1244 :post-blank
(count-lines pos-before-blank end
)
1245 :post-affiliated post-affiliated
)
1246 (cdr affiliated
)))))))))
1248 (defun org-element-property-drawer-interpreter (property-drawer contents
)
1249 "Interpret PROPERTY-DRAWER element as Org syntax.
1250 CONTENTS is the properties within the drawer."
1251 (format ":PROPERTIES:\n%s:END:" contents
))
1256 (defun org-element-quote-block-parser (limit affiliated
)
1257 "Parse a quote block.
1259 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1260 the buffer position at the beginning of the first affiliated
1261 keyword and CDR is a plist of affiliated keywords along with
1264 Return a list whose CAR is `quote-block' and CDR is a plist
1265 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1266 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
1268 Assume point is at the beginning of the block."
1269 (let ((case-fold-search t
))
1270 (if (not (save-excursion
1271 (re-search-forward "^[ \t]*#\\+END_QUOTE[ \t]*$" limit t
)))
1272 ;; Incomplete block: parse it as a paragraph.
1273 (org-element-paragraph-parser limit affiliated
)
1274 (let ((block-end-line (match-beginning 0)))
1276 (let* ((begin (car affiliated
))
1277 (post-affiliated (point))
1278 ;; Empty blocks have no contents.
1279 (contents-begin (progn (forward-line)
1280 (and (< (point) block-end-line
)
1282 (contents-end (and contents-begin block-end-line
))
1283 (hidden (org-invisible-p2))
1284 (pos-before-blank (progn (goto-char block-end-line
)
1287 (end (progn (skip-chars-forward " \r\t\n" limit
)
1288 (skip-chars-backward " \t")
1289 (if (bolp) (point) (line-end-position)))))
1295 :contents-begin contents-begin
1296 :contents-end contents-end
1297 :post-blank
(count-lines pos-before-blank end
)
1298 :post-affiliated post-affiliated
)
1299 (cdr affiliated
)))))))))
1301 (defun org-element-quote-block-interpreter (quote-block contents
)
1302 "Interpret QUOTE-BLOCK element as Org syntax.
1303 CONTENTS is the contents of the element."
1304 (format "#+BEGIN_QUOTE\n%s#+END_QUOTE" contents
))
1309 (defun org-element-section-parser (limit)
1312 LIMIT bounds the search.
1314 Return a list whose CAR is `section' and CDR is a plist
1315 containing `:begin', `:end', `:contents-begin', `contents-end'
1316 and `:post-blank' keywords."
1318 ;; Beginning of section is the beginning of the first non-blank
1319 ;; line after previous headline.
1320 (let ((begin (point))
1321 (end (progn (org-with-limited-levels (outline-next-heading))
1323 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
1329 :contents-begin begin
1330 :contents-end pos-before-blank
1331 :post-blank
(count-lines pos-before-blank end
))))))
1333 (defun org-element-section-interpreter (section contents
)
1334 "Interpret SECTION element as Org syntax.
1335 CONTENTS is the contents of the element."
1341 (defun org-element-special-block-parser (limit affiliated
)
1342 "Parse a special block.
1344 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1345 the buffer position at the beginning of the first affiliated
1346 keyword and CDR is a plist of affiliated keywords along with
1349 Return a list whose CAR is `special-block' and CDR is a plist
1350 containing `:type', `:begin', `:end', `:hiddenp',
1351 `:contents-begin', `:contents-end', `:post-blank' and
1352 `:post-affiliated' keywords.
1354 Assume point is at the beginning of the block."
1355 (let* ((case-fold-search t
)
1356 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
1357 (upcase (match-string-no-properties 1)))))
1358 (if (not (save-excursion
1360 (format "^[ \t]*#\\+END_%s[ \t]*$" (regexp-quote type
))
1362 ;; Incomplete block: parse it as a paragraph.
1363 (org-element-paragraph-parser limit affiliated
)
1364 (let ((block-end-line (match-beginning 0)))
1366 (let* ((begin (car affiliated
))
1367 (post-affiliated (point))
1368 ;; Empty blocks have no contents.
1369 (contents-begin (progn (forward-line)
1370 (and (< (point) block-end-line
)
1372 (contents-end (and contents-begin block-end-line
))
1373 (hidden (org-invisible-p2))
1374 (pos-before-blank (progn (goto-char block-end-line
)
1377 (end (progn (skip-chars-forward " \r\t\n" limit
)
1378 (skip-chars-backward " \t")
1379 (if (bolp) (point) (line-end-position)))))
1380 (list 'special-block
1386 :contents-begin contents-begin
1387 :contents-end contents-end
1388 :post-blank
(count-lines pos-before-blank end
)
1389 :post-affiliated post-affiliated
)
1390 (cdr affiliated
)))))))))
1392 (defun org-element-special-block-interpreter (special-block contents
)
1393 "Interpret SPECIAL-BLOCK element as Org syntax.
1394 CONTENTS is the contents of the element."
1395 (let ((block-type (org-element-property :type special-block
)))
1396 (format "#+BEGIN_%s\n%s#+END_%s" block-type contents block-type
)))
1402 ;; For each element, a parser and an interpreter are also defined.
1403 ;; Both follow the same naming convention used for greater elements.
1405 ;; Also, as for greater elements, adding a new element type is done
1406 ;; through the following steps: implement a parser and an interpreter,
1407 ;; tweak `org-element--current-element' so that it recognizes the new
1408 ;; type and add that new type to `org-element-all-elements'.
1410 ;; As a special case, when the newly defined type is a block type,
1411 ;; `org-element-block-name-alist' has to be modified accordingly.
1416 (defun org-element-babel-call-parser (limit affiliated
)
1417 "Parse a babel call.
1419 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1420 the buffer position at the beginning of the first affiliated
1421 keyword and CDR is a plist of affiliated keywords along with
1424 Return a list whose CAR is `babel-call' and CDR is a plist
1425 containing `:begin', `:end', `:info', `:post-blank' and
1426 `:post-affiliated' as keywords."
1428 (let ((case-fold-search t
)
1429 (info (progn (looking-at org-babel-block-lob-one-liner-regexp
)
1430 (org-babel-lob-get-info)))
1431 (begin (car affiliated
))
1432 (post-affiliated (point))
1433 (pos-before-blank (progn (forward-line) (point)))
1434 (end (progn (skip-chars-forward " \r\t\n" limit
)
1435 (skip-chars-backward " \t")
1436 (if (bolp) (point) (line-end-position)))))
1442 :post-blank
(count-lines pos-before-blank end
)
1443 :post-affiliated post-affiliated
)
1444 (cdr affiliated
))))))
1446 (defun org-element-babel-call-interpreter (babel-call contents
)
1447 "Interpret BABEL-CALL element as Org syntax.
1449 (let* ((babel-info (org-element-property :info babel-call
))
1450 (main (car babel-info
))
1451 (post-options (nth 1 babel-info
)))
1453 (if (not (string-match "\\[\\(\\[.*?\\]\\)\\]" main
)) main
1454 ;; Remove redundant square brackets.
1455 (replace-match (match-string 1 main
) nil nil main
))
1456 (and post-options
(format "[%s]" post-options
)))))
1461 (defun org-element-clock-parser (limit)
1464 LIMIT bounds the search.
1466 Return a list whose CAR is `clock' and CDR is a plist containing
1467 `:status', `:value', `:time', `:begin', `:end' and `:post-blank'
1470 (let* ((case-fold-search nil
)
1472 (value (progn (search-forward org-clock-string
(line-end-position) t
)
1473 (skip-chars-forward " \t")
1474 (org-element-timestamp-parser)))
1475 (duration (and (search-forward " => " (line-end-position) t
)
1476 (progn (skip-chars-forward " \t")
1477 (looking-at "\\(\\S-+\\)[ \t]*$"))
1478 (org-match-string-no-properties 1)))
1479 (status (if duration
'closed
'running
))
1480 (post-blank (let ((before-blank (progn (forward-line) (point))))
1481 (skip-chars-forward " \r\t\n" limit
)
1482 (skip-chars-backward " \t")
1483 (unless (bolp) (end-of-line))
1484 (count-lines before-blank
(point))))
1487 (list :status status
1492 :post-blank post-blank
)))))
1494 (defun org-element-clock-interpreter (clock contents
)
1495 "Interpret CLOCK element as Org syntax.
1497 (concat org-clock-string
" "
1498 (org-element-timestamp-interpreter
1499 (org-element-property :value clock
) nil
)
1500 (let ((duration (org-element-property :duration clock
)))
1505 (org-split-string duration
":")))))))
1510 (defun org-element-comment-parser (limit affiliated
)
1513 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1514 the buffer position at the beginning of the first affiliated
1515 keyword and CDR is a plist of affiliated keywords along with
1518 Return a list whose CAR is `comment' and CDR is a plist
1519 containing `:begin', `:end', `:value', `:post-blank',
1520 `:post-affiliated' keywords.
1522 Assume point is at comment beginning."
1524 (let* ((begin (car affiliated
))
1525 (post-affiliated (point))
1526 (value (prog2 (looking-at "[ \t]*# ?")
1527 (buffer-substring-no-properties
1528 (match-end 0) (line-end-position))
1531 ;; Get comments ending.
1533 (while (and (< (point) limit
) (looking-at "[ \t]*#\\( \\|$\\)"))
1534 ;; Accumulate lines without leading hash and first
1539 (buffer-substring-no-properties
1540 (match-end 0) (line-end-position))))
1543 (end (progn (goto-char com-end
)
1544 (skip-chars-forward " \r\t\n" limit
)
1545 (skip-chars-backward " \t")
1546 (if (bolp) (point) (line-end-position)))))
1552 :post-blank
(count-lines com-end end
)
1553 :post-affiliated post-affiliated
)
1554 (cdr affiliated
))))))
1556 (defun org-element-comment-interpreter (comment contents
)
1557 "Interpret COMMENT element as Org syntax.
1559 (replace-regexp-in-string "^" "# " (org-element-property :value comment
)))
1564 (defun org-element-comment-block-parser (limit affiliated
)
1565 "Parse an export block.
1567 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1568 the buffer position at the beginning of the first affiliated
1569 keyword and CDR is a plist of affiliated keywords along with
1572 Return a list whose CAR is `comment-block' and CDR is a plist
1573 containing `:begin', `:end', `:hiddenp', `:value', `:post-blank'
1574 and `:post-affiliated' keywords.
1576 Assume point is at comment block beginning."
1577 (let ((case-fold-search t
))
1578 (if (not (save-excursion
1579 (re-search-forward "^[ \t]*#\\+END_COMMENT[ \t]*$" limit t
)))
1580 ;; Incomplete block: parse it as a paragraph.
1581 (org-element-paragraph-parser limit affiliated
)
1582 (let ((contents-end (match-beginning 0)))
1584 (let* ((begin (car affiliated
))
1585 (post-affiliated (point))
1586 (contents-begin (progn (forward-line) (point)))
1587 (hidden (org-invisible-p2))
1588 (pos-before-blank (progn (goto-char contents-end
)
1591 (end (progn (skip-chars-forward " \r\t\n" limit
)
1592 (skip-chars-backward " \t")
1593 (if (bolp) (point) (line-end-position))))
1594 (value (buffer-substring-no-properties
1595 contents-begin contents-end
)))
1596 (list 'comment-block
1602 :post-blank
(count-lines pos-before-blank end
)
1603 :post-affiliated post-affiliated
)
1604 (cdr affiliated
)))))))))
1606 (defun org-element-comment-block-interpreter (comment-block contents
)
1607 "Interpret COMMENT-BLOCK element as Org syntax.
1609 (format "#+BEGIN_COMMENT\n%s#+END_COMMENT"
1610 (org-remove-indentation (org-element-property :value comment-block
))))
1615 (defun org-element-diary-sexp-parser (limit affiliated
)
1616 "Parse a diary sexp.
1618 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1619 the buffer position at the beginning of the first affiliated
1620 keyword and CDR is a plist of affiliated keywords along with
1623 Return a list whose CAR is `diary-sexp' and CDR is a plist
1624 containing `:begin', `:end', `:value', `:post-blank' and
1625 `:post-affiliated' keywords."
1627 (let ((begin (car affiliated
))
1628 (post-affiliated (point))
1629 (value (progn (looking-at "\\(%%(.*\\)[ \t]*$")
1630 (org-match-string-no-properties 1)))
1631 (pos-before-blank (progn (forward-line) (point)))
1632 (end (progn (skip-chars-forward " \r\t\n" limit
)
1633 (skip-chars-backward " \t")
1634 (if (bolp) (point) (line-end-position)))))
1640 :post-blank
(count-lines pos-before-blank end
)
1641 :post-affiliated post-affiliated
)
1642 (cdr affiliated
))))))
1644 (defun org-element-diary-sexp-interpreter (diary-sexp contents
)
1645 "Interpret DIARY-SEXP as Org syntax.
1647 (org-element-property :value diary-sexp
))
1652 (defun org-element--remove-indentation (s &optional n
)
1653 "Remove maximum common indentation in string S and return it.
1654 When optional argument N is a positive integer, remove exactly
1655 that much characters from indentation, if possible, or return
1656 S as-is otherwise. Unlike to `org-remove-indentation', this
1657 function doesn't call `untabify' on S."
1661 (goto-char (point-min))
1662 ;; Find maximum common indentation, if not specified.
1664 (let ((min-ind (point-max)))
1666 (while (re-search-forward "^[ \t]*\\S-" nil t
)
1667 (let ((ind (1- (current-column))))
1668 (if (zerop ind
) (throw 'exit s
)
1669 (setq min-ind
(min min-ind ind
))))))
1672 ;; Remove exactly N indentation, but give up if not possible.
1674 (let ((ind (progn (skip-chars-forward " \t") (current-column))))
1675 (cond ((eolp) (delete-region (line-beginning-position) (point)))
1676 ((< ind n
) (throw 'exit s
))
1677 (t (org-indent-line-to (- ind n
))))
1681 (defun org-element-example-block-parser (limit affiliated
)
1682 "Parse an example block.
1684 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1685 the buffer position at the beginning of the first affiliated
1686 keyword and CDR is a plist of affiliated keywords along with
1689 Return a list whose CAR is `example-block' and CDR is a plist
1690 containing `:begin', `:end', `:number-lines', `:preserve-indent',
1691 `:retain-labels', `:use-labels', `:label-fmt', `:hiddenp',
1692 `:switches', `:value', `:post-blank' and `:post-affiliated'
1694 (let ((case-fold-search t
))
1695 (if (not (save-excursion
1696 (re-search-forward "^[ \t]*#\\+END_EXAMPLE[ \t]*$" limit t
)))
1697 ;; Incomplete block: parse it as a paragraph.
1698 (org-element-paragraph-parser limit affiliated
)
1699 (let ((contents-end (match-beginning 0)))
1703 (looking-at "^[ \t]*#\\+BEGIN_EXAMPLE\\(?: +\\(.*\\)\\)?")
1704 (org-match-string-no-properties 1)))
1705 ;; Switches analysis
1707 (cond ((not switches
) nil
)
1708 ((string-match "-n\\>" switches
) 'new
)
1709 ((string-match "+n\\>" switches
) 'continued
)))
1711 (or org-src-preserve-indentation
1712 (and switches
(string-match "-i\\>" switches
))))
1713 ;; Should labels be retained in (or stripped from) example
1717 (not (string-match "-r\\>" switches
))
1718 (and number-lines
(string-match "-k\\>" switches
))))
1719 ;; What should code-references use - labels or
1724 (not (string-match "-k\\>" switches
)))))
1727 (string-match "-l +\"\\([^\"\n]+\\)\"" switches
)
1728 (match-string 1 switches
)))
1729 ;; Standard block parsing.
1730 (begin (car affiliated
))
1731 (post-affiliated (point))
1732 (block-ind (progn (skip-chars-forward " \t") (current-column)))
1733 (contents-begin (progn (forward-line) (point)))
1734 (hidden (org-invisible-p2))
1735 (value (org-element--remove-indentation
1736 (org-unescape-code-in-string
1737 (buffer-substring-no-properties
1738 contents-begin contents-end
))
1739 (and preserve-indent block-ind
)))
1740 (pos-before-blank (progn (goto-char contents-end
)
1743 (end (progn (skip-chars-forward " \r\t\n" limit
)
1744 (skip-chars-backward " \t")
1745 (if (bolp) (point) (line-end-position)))))
1746 (list 'example-block
1752 :number-lines number-lines
1753 :preserve-indent preserve-indent
1754 :retain-labels retain-labels
1755 :use-labels use-labels
1756 :label-fmt label-fmt
1758 :post-blank
(count-lines pos-before-blank end
)
1759 :post-affiliated post-affiliated
)
1760 (cdr affiliated
)))))))))
1762 (defun org-element-example-block-interpreter (example-block contents
)
1763 "Interpret EXAMPLE-BLOCK element as Org syntax.
1765 (let ((switches (org-element-property :switches example-block
)))
1766 (concat "#+BEGIN_EXAMPLE" (and switches
(concat " " switches
)) "\n"
1767 (org-escape-code-in-string
1768 (org-element-property :value example-block
))
1774 (defun org-element-export-block-parser (limit affiliated
)
1775 "Parse an export block.
1777 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1778 the buffer position at the beginning of the first affiliated
1779 keyword and CDR is a plist of affiliated keywords along with
1782 Return a list whose CAR is `export-block' and CDR is a plist
1783 containing `:begin', `:end', `:type', `:hiddenp', `:value',
1784 `:post-blank' and `:post-affiliated' keywords.
1786 Assume point is at export-block beginning."
1787 (let* ((case-fold-search t
)
1788 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
1789 (upcase (org-match-string-no-properties 1)))))
1790 (if (not (save-excursion
1792 (format "^[ \t]*#\\+END_%s[ \t]*$" type
) limit t
)))
1793 ;; Incomplete block: parse it as a paragraph.
1794 (org-element-paragraph-parser limit affiliated
)
1795 (let ((contents-end (match-beginning 0)))
1797 (let* ((begin (car affiliated
))
1798 (post-affiliated (point))
1799 (contents-begin (progn (forward-line) (point)))
1800 (hidden (org-invisible-p2))
1801 (pos-before-blank (progn (goto-char contents-end
)
1804 (end (progn (skip-chars-forward " \r\t\n" limit
)
1805 (skip-chars-backward " \t")
1806 (if (bolp) (point) (line-end-position))))
1807 (value (buffer-substring-no-properties contents-begin
1816 :post-blank
(count-lines pos-before-blank end
)
1817 :post-affiliated post-affiliated
)
1818 (cdr affiliated
)))))))))
1820 (defun org-element-export-block-interpreter (export-block contents
)
1821 "Interpret EXPORT-BLOCK element as Org syntax.
1823 (let ((type (org-element-property :type export-block
)))
1824 (concat (format "#+BEGIN_%s\n" type
)
1825 (org-element-property :value export-block
)
1826 (format "#+END_%s" type
))))
1831 (defun org-element-fixed-width-parser (limit affiliated
)
1832 "Parse a fixed-width section.
1834 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1835 the buffer position at the beginning of the first affiliated
1836 keyword and CDR is a plist of affiliated keywords along with
1839 Return a list whose CAR is `fixed-width' and CDR is a plist
1840 containing `:begin', `:end', `:value', `:post-blank' and
1841 `:post-affiliated' keywords.
1843 Assume point is at the beginning of the fixed-width area."
1845 (let* ((begin (car affiliated
))
1846 (post-affiliated (point))
1850 (while (and (< (point) limit
)
1851 (looking-at "[ \t]*:\\( \\|$\\)"))
1852 ;; Accumulate text without starting colons.
1855 (buffer-substring-no-properties
1856 (match-end 0) (point-at-eol))
1860 (end (progn (skip-chars-forward " \r\t\n" limit
)
1861 (skip-chars-backward " \t")
1862 (if (bolp) (point) (line-end-position)))))
1868 :post-blank
(count-lines end-area end
)
1869 :post-affiliated post-affiliated
)
1870 (cdr affiliated
))))))
1872 (defun org-element-fixed-width-interpreter (fixed-width contents
)
1873 "Interpret FIXED-WIDTH element as Org syntax.
1875 (let ((value (org-element-property :value fixed-width
)))
1877 (replace-regexp-in-string
1879 (if (string-match "\n\\'" value
) (substring value
0 -
1) value
)))))
1882 ;;;; Horizontal Rule
1884 (defun org-element-horizontal-rule-parser (limit affiliated
)
1885 "Parse an horizontal rule.
1887 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1888 the buffer position at the beginning of the first affiliated
1889 keyword and CDR is a plist of affiliated keywords along with
1892 Return a list whose CAR is `horizontal-rule' and CDR is a plist
1893 containing `:begin', `:end', `:post-blank' and `:post-affiliated'
1896 (let ((begin (car affiliated
))
1897 (post-affiliated (point))
1898 (post-hr (progn (forward-line) (point)))
1899 (end (progn (skip-chars-forward " \r\t\n" limit
)
1900 (skip-chars-backward " \t")
1901 (if (bolp) (point) (line-end-position)))))
1902 (list 'horizontal-rule
1906 :post-blank
(count-lines post-hr end
)
1907 :post-affiliated post-affiliated
)
1908 (cdr affiliated
))))))
1910 (defun org-element-horizontal-rule-interpreter (horizontal-rule contents
)
1911 "Interpret HORIZONTAL-RULE element as Org syntax.
1918 (defun org-element-keyword-parser (limit affiliated
)
1919 "Parse a keyword at point.
1921 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1922 the buffer position at the beginning of the first affiliated
1923 keyword and CDR is a plist of affiliated keywords along with
1926 Return a list whose CAR is `keyword' and CDR is a plist
1927 containing `:key', `:value', `:begin', `:end', `:post-blank' and
1928 `:post-affiliated' keywords."
1930 (let ((begin (car affiliated
))
1931 (post-affiliated (point))
1932 (key (progn (looking-at "[ \t]*#\\+\\(\\S-+*\\):")
1933 (upcase (org-match-string-no-properties 1))))
1934 (value (org-trim (buffer-substring-no-properties
1935 (match-end 0) (point-at-eol))))
1936 (pos-before-blank (progn (forward-line) (point)))
1937 (end (progn (skip-chars-forward " \r\t\n" limit
)
1938 (skip-chars-backward " \t")
1939 (if (bolp) (point) (line-end-position)))))
1946 :post-blank
(count-lines pos-before-blank end
)
1947 :post-affiliated post-affiliated
)
1948 (cdr affiliated
))))))
1950 (defun org-element-keyword-interpreter (keyword contents
)
1951 "Interpret KEYWORD element as Org syntax.
1954 (org-element-property :key keyword
)
1955 (org-element-property :value keyword
)))
1958 ;;;; Latex Environment
1960 (defun org-element-latex-environment-parser (limit affiliated
)
1961 "Parse a LaTeX environment.
1963 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1964 the buffer position at the beginning of the first affiliated
1965 keyword and CDR is a plist of affiliated keywords along with
1968 Return a list whose CAR is `latex-environment' and CDR is a plist
1969 containing `:begin', `:end', `:value', `:post-blank' and
1970 `:post-affiliated' keywords.
1972 Assume point is at the beginning of the latex environment."
1974 (let ((case-fold-search t
)
1975 (code-begin (point)))
1976 (looking-at "[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}")
1977 (if (not (re-search-forward (format "^[ \t]*\\\\end{%s}[ \t]*$"
1978 (regexp-quote (match-string 1)))
1980 ;; Incomplete latex environment: parse it as a paragraph.
1981 (org-element-paragraph-parser limit affiliated
)
1982 (let* ((code-end (progn (forward-line) (point)))
1983 (begin (car affiliated
))
1984 (value (buffer-substring-no-properties code-begin code-end
))
1985 (end (progn (skip-chars-forward " \r\t\n" limit
)
1986 (skip-chars-backward " \t")
1987 (if (bolp) (point) (line-end-position)))))
1988 (list 'latex-environment
1993 :post-blank
(count-lines code-end end
)
1994 :post-affiliated code-begin
)
1995 (cdr affiliated
))))))))
1997 (defun org-element-latex-environment-interpreter (latex-environment contents
)
1998 "Interpret LATEX-ENVIRONMENT element as Org syntax.
2000 (org-element-property :value latex-environment
))
2005 (defun org-element-node-property-parser (limit)
2006 "Parse a node-property at point.
2008 LIMIT bounds the search.
2010 Return a list whose CAR is `node-property' and CDR is a plist
2011 containing `:key', `:value', `:begin', `:end' and `:post-blank'
2014 (let ((case-fold-search t
)
2016 (key (progn (looking-at "[ \t]*:\\(.*?\\):[ \t]+\\(.*?\\)[ \t]*$")
2017 (org-match-string-no-properties 1)))
2018 (value (org-match-string-no-properties 2))
2019 (pos-before-blank (progn (forward-line) (point)))
2020 (end (progn (skip-chars-forward " \r\t\n" limit
)
2021 (if (eobp) (point) (point-at-bol)))))
2022 (list 'node-property
2027 :post-blank
(count-lines pos-before-blank end
))))))
2029 (defun org-element-node-property-interpreter (node-property contents
)
2030 "Interpret NODE-PROPERTY element as Org syntax.
2032 (format org-property-format
2033 (format ":%s:" (org-element-property :key node-property
))
2034 (org-element-property :value node-property
)))
2039 (defun org-element-paragraph-parser (limit affiliated
)
2042 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2043 the buffer position at the beginning of the first affiliated
2044 keyword and CDR is a plist of affiliated keywords along with
2047 Return a list whose CAR is `paragraph' and CDR is a plist
2048 containing `:begin', `:end', `:contents-begin' and
2049 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
2051 Assume point is at the beginning of the paragraph."
2053 (let* ((begin (car affiliated
))
2054 (contents-begin (point))
2056 (let ((case-fold-search t
))
2058 (if (not (re-search-forward
2059 org-element-paragraph-separate limit
'm
))
2061 ;; A matching `org-element-paragraph-separate' is not
2062 ;; necessarily the end of the paragraph. In
2063 ;; particular, lines starting with # or : as a first
2064 ;; non-space character are ambiguous. We have check
2065 ;; if they are valid Org syntax (i.e. not an
2066 ;; incomplete keyword).
2070 ;; There's no ambiguity for other symbols or
2071 ;; empty lines: stop here.
2072 (looking-at "[ \t]*\\(?:[^:#]\\|$\\)")
2073 ;; Stop at valid fixed-width areas.
2074 (looking-at "[ \t]*:\\(?: \\|$\\)")
2076 (and (looking-at org-drawer-regexp
)
2079 "^[ \t]*:END:[ \t]*$" limit t
)))
2080 ;; Stop at valid comments.
2081 (looking-at "[ \t]*#\\(?: \\|$\\)")
2082 ;; Stop at valid dynamic blocks.
2083 (and (looking-at org-dblock-start-re
)
2086 "^[ \t]*#\\+END:?[ \t]*$" limit t
)))
2087 ;; Stop at valid blocks.
2089 "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
2092 (format "^[ \t]*#\\+END_%s[ \t]*$"
2095 ;; Stop at valid latex environments.
2097 "^[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}[ \t]*$")
2100 (format "^[ \t]*\\\\end{%s}[ \t]*$"
2103 ;; Stop at valid keywords.
2104 (looking-at "[ \t]*#\\+\\S-+:")
2105 ;; Skip everything else.
2109 (re-search-forward org-element-paragraph-separate
2111 (beginning-of-line)))
2112 (if (= (point) limit
) limit
2113 (goto-char (line-beginning-position)))))
2114 (contents-end (progn (skip-chars-backward " \r\t\n" contents-begin
)
2117 (end (progn (skip-chars-forward " \r\t\n" limit
)
2118 (skip-chars-backward " \t")
2119 (if (bolp) (point) (line-end-position)))))
2124 :contents-begin contents-begin
2125 :contents-end contents-end
2126 :post-blank
(count-lines before-blank end
)
2127 :post-affiliated contents-begin
)
2128 (cdr affiliated
))))))
2130 (defun org-element-paragraph-interpreter (paragraph contents
)
2131 "Interpret PARAGRAPH element as Org syntax.
2132 CONTENTS is the contents of the element."
2138 (defun org-element-planning-parser (limit)
2141 LIMIT bounds the search.
2143 Return a list whose CAR is `planning' and CDR is a plist
2144 containing `:closed', `:deadline', `:scheduled', `:begin', `:end'
2145 and `:post-blank' keywords."
2147 (let* ((case-fold-search nil
)
2149 (post-blank (let ((before-blank (progn (forward-line) (point))))
2150 (skip-chars-forward " \r\t\n" limit
)
2151 (skip-chars-backward " \t")
2152 (unless (bolp) (end-of-line))
2153 (count-lines before-blank
(point))))
2155 closed deadline scheduled
)
2157 (while (re-search-forward org-keyword-time-not-clock-regexp end t
)
2158 (goto-char (match-end 1))
2159 (skip-chars-forward " \t" end
)
2160 (let ((keyword (match-string 1))
2161 (time (org-element-timestamp-parser)))
2162 (cond ((equal keyword org-closed-string
) (setq closed time
))
2163 ((equal keyword org-deadline-string
) (setq deadline time
))
2164 (t (setq scheduled time
)))))
2166 (list :closed closed
2168 :scheduled scheduled
2171 :post-blank post-blank
)))))
2173 (defun org-element-planning-interpreter (planning contents
)
2174 "Interpret PLANNING element as Org syntax.
2179 (list (let ((deadline (org-element-property :deadline planning
)))
2181 (concat org-deadline-string
" "
2182 (org-element-timestamp-interpreter deadline nil
))))
2183 (let ((scheduled (org-element-property :scheduled planning
)))
2185 (concat org-scheduled-string
" "
2186 (org-element-timestamp-interpreter scheduled nil
))))
2187 (let ((closed (org-element-property :closed planning
)))
2189 (concat org-closed-string
" "
2190 (org-element-timestamp-interpreter closed nil
))))))
2196 (defun org-element-quote-section-parser (limit)
2197 "Parse a quote section.
2199 LIMIT bounds the search.
2201 Return a list whose CAR is `quote-section' and CDR is a plist
2202 containing `:begin', `:end', `:value' and `:post-blank' keywords.
2204 Assume point is at beginning of the section."
2206 (let* ((begin (point))
2207 (end (progn (org-with-limited-levels (outline-next-heading))
2209 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
2212 (value (buffer-substring-no-properties begin pos-before-blank
)))
2213 (list 'quote-section
2217 :post-blank
(count-lines pos-before-blank end
))))))
2219 (defun org-element-quote-section-interpreter (quote-section contents
)
2220 "Interpret QUOTE-SECTION element as Org syntax.
2222 (org-element-property :value quote-section
))
2227 (defun org-element-src-block-parser (limit affiliated
)
2230 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2231 the buffer position at the beginning of the first affiliated
2232 keyword and CDR is a plist of affiliated keywords along with
2235 Return a list whose CAR is `src-block' and CDR is a plist
2236 containing `:language', `:switches', `:parameters', `:begin',
2237 `:end', `:hiddenp', `:number-lines', `:retain-labels',
2238 `:use-labels', `:label-fmt', `:preserve-indent', `:value',
2239 `:post-blank' and `:post-affiliated' keywords.
2241 Assume point is at the beginning of the block."
2242 (let ((case-fold-search t
))
2243 (if (not (save-excursion (re-search-forward "^[ \t]*#\\+END_SRC[ \t]*$"
2245 ;; Incomplete block: parse it as a paragraph.
2246 (org-element-paragraph-parser limit affiliated
)
2247 (let ((contents-end (match-beginning 0)))
2249 (let* ((begin (car affiliated
))
2250 (post-affiliated (point))
2251 ;; Get language as a string.
2255 (concat "^[ \t]*#\\+BEGIN_SRC"
2256 "\\(?: +\\(\\S-+\\)\\)?"
2257 "\\(\\(?: +\\(?:-l \".*?\"\\|[-+][A-Za-z]\\)\\)+\\)?"
2259 (org-match-string-no-properties 1)))
2261 (switches (org-match-string-no-properties 2))
2263 (parameters (org-match-string-no-properties 3))
2264 ;; Switches analysis
2266 (cond ((not switches
) nil
)
2267 ((string-match "-n\\>" switches
) 'new
)
2268 ((string-match "+n\\>" switches
) 'continued
)))
2269 (preserve-indent (or org-src-preserve-indentation
2271 (string-match "-i\\>" switches
))))
2274 (string-match "-l +\"\\([^\"\n]+\\)\"" switches
)
2275 (match-string 1 switches
)))
2276 ;; Should labels be retained in (or stripped from)
2280 (not (string-match "-r\\>" switches
))
2281 (and number-lines
(string-match "-k\\>" switches
))))
2282 ;; What should code-references use - labels or
2287 (not (string-match "-k\\>" switches
)))))
2289 (block-ind (progn (skip-chars-forward " \t") (current-column)))
2290 ;; Get visibility status.
2291 (hidden (progn (forward-line) (org-invisible-p2)))
2293 (value (org-element--remove-indentation
2294 (org-unescape-code-in-string
2295 (buffer-substring-no-properties
2296 (point) contents-end
))
2297 (and preserve-indent block-ind
)))
2298 (pos-before-blank (progn (goto-char contents-end
)
2301 ;; Get position after ending blank lines.
2302 (end (progn (skip-chars-forward " \r\t\n" limit
)
2303 (skip-chars-backward " \t")
2304 (if (bolp) (point) (line-end-position)))))
2307 (list :language language
2308 :switches
(and (org-string-nw-p switches
)
2309 (org-trim switches
))
2310 :parameters
(and (org-string-nw-p parameters
)
2311 (org-trim parameters
))
2314 :number-lines number-lines
2315 :preserve-indent preserve-indent
2316 :retain-labels retain-labels
2317 :use-labels use-labels
2318 :label-fmt label-fmt
2321 :post-blank
(count-lines pos-before-blank end
)
2322 :post-affiliated post-affiliated
)
2323 (cdr affiliated
)))))))))
2325 (defun org-element-src-block-interpreter (src-block contents
)
2326 "Interpret SRC-BLOCK element as Org syntax.
2328 (let ((lang (org-element-property :language src-block
))
2329 (switches (org-element-property :switches src-block
))
2330 (params (org-element-property :parameters src-block
))
2331 (value (let ((val (org-element-property :value src-block
)))
2333 ((org-element-property :preserve-indent src-block
) val
)
2334 ((zerop org-edit-src-content-indentation
) val
)
2336 (let ((ind (make-string
2337 org-edit-src-content-indentation
32)))
2338 (replace-regexp-in-string
2339 "\\(^\\)[ \t]*\\S-" ind val nil nil
1)))))))
2340 (concat (format "#+BEGIN_SRC%s\n"
2341 (concat (and lang
(concat " " lang
))
2342 (and switches
(concat " " switches
))
2343 (and params
(concat " " params
))))
2344 (org-escape-code-in-string value
)
2350 (defun org-element-table-parser (limit affiliated
)
2351 "Parse a table at point.
2353 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2354 the buffer position at the beginning of the first affiliated
2355 keyword and CDR is a plist of affiliated keywords along with
2358 Return a list whose CAR is `table' and CDR is a plist containing
2359 `:begin', `:end', `:tblfm', `:type', `:contents-begin',
2360 `:contents-end', `:value', `:post-blank' and `:post-affiliated'
2363 Assume point is at the beginning of the table."
2365 (let* ((case-fold-search t
)
2366 (table-begin (point))
2367 (type (if (org-at-table.el-p
) 'table.el
'org
))
2368 (begin (car affiliated
))
2370 (if (re-search-forward org-table-any-border-regexp limit
'm
)
2371 (goto-char (match-beginning 0))
2374 (while (looking-at "[ \t]*#\\+TBLFM: +\\(.*\\)[ \t]*$")
2375 (push (org-match-string-no-properties 1) acc
)
2378 (pos-before-blank (point))
2379 (end (progn (skip-chars-forward " \r\t\n" limit
)
2380 (skip-chars-backward " \t")
2381 (if (bolp) (point) (line-end-position)))))
2388 ;; Only `org' tables have contents. `table.el' tables
2389 ;; use a `:value' property to store raw table as
2391 :contents-begin
(and (eq type
'org
) table-begin
)
2392 :contents-end
(and (eq type
'org
) table-end
)
2393 :value
(and (eq type
'table.el
)
2394 (buffer-substring-no-properties
2395 table-begin table-end
))
2396 :post-blank
(count-lines pos-before-blank end
)
2397 :post-affiliated table-begin
)
2398 (cdr affiliated
))))))
2400 (defun org-element-table-interpreter (table contents
)
2401 "Interpret TABLE element as Org syntax.
2403 (if (eq (org-element-property :type table
) 'table.el
)
2404 (org-remove-indentation (org-element-property :value table
))
2405 (concat (with-temp-buffer (insert contents
)
2408 (mapconcat (lambda (fm) (concat "#+TBLFM: " fm
))
2409 (reverse (org-element-property :tblfm table
))
2415 (defun org-element-table-row-parser (limit)
2416 "Parse table row at point.
2418 LIMIT bounds the search.
2420 Return a list whose CAR is `table-row' and CDR is a plist
2421 containing `:begin', `:end', `:contents-begin', `:contents-end',
2422 `:type' and `:post-blank' keywords."
2424 (let* ((type (if (looking-at "^[ \t]*|-") 'rule
'standard
))
2426 ;; A table rule has no contents. In that case, ensure
2427 ;; CONTENTS-BEGIN matches CONTENTS-END.
2428 (contents-begin (and (eq type
'standard
)
2429 (search-forward "|")
2431 (contents-end (and (eq type
'standard
)
2434 (skip-chars-backward " \t")
2436 (end (progn (forward-line) (point))))
2441 :contents-begin contents-begin
2442 :contents-end contents-end
2445 (defun org-element-table-row-interpreter (table-row contents
)
2446 "Interpret TABLE-ROW element as Org syntax.
2447 CONTENTS is the contents of the table row."
2448 (if (eq (org-element-property :type table-row
) 'rule
) "|-"
2449 (concat "| " contents
)))
2454 (defun org-element-verse-block-parser (limit affiliated
)
2455 "Parse a verse block.
2457 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2458 the buffer position at the beginning of the first affiliated
2459 keyword and CDR is a plist of affiliated keywords along with
2462 Return a list whose CAR is `verse-block' and CDR is a plist
2463 containing `:begin', `:end', `:contents-begin', `:contents-end',
2464 `:hiddenp', `:post-blank' and `:post-affiliated' keywords.
2466 Assume point is at beginning of the block."
2467 (let ((case-fold-search t
))
2468 (if (not (save-excursion
2469 (re-search-forward "^[ \t]*#\\+END_VERSE[ \t]*$" limit t
)))
2470 ;; Incomplete block: parse it as a paragraph.
2471 (org-element-paragraph-parser limit affiliated
)
2472 (let ((contents-end (match-beginning 0)))
2474 (let* ((begin (car affiliated
))
2475 (post-affiliated (point))
2476 (hidden (progn (forward-line) (org-invisible-p2)))
2477 (contents-begin (point))
2478 (pos-before-blank (progn (goto-char contents-end
)
2481 (end (progn (skip-chars-forward " \r\t\n" limit
)
2482 (skip-chars-backward " \t")
2483 (if (bolp) (point) (line-end-position)))))
2488 :contents-begin contents-begin
2489 :contents-end contents-end
2491 :post-blank
(count-lines pos-before-blank end
)
2492 :post-affiliated post-affiliated
)
2493 (cdr affiliated
)))))))))
2495 (defun org-element-verse-block-interpreter (verse-block contents
)
2496 "Interpret VERSE-BLOCK element as Org syntax.
2497 CONTENTS is verse block contents."
2498 (format "#+BEGIN_VERSE\n%s#+END_VERSE" contents
))
2504 ;; Unlike to elements, interstices can be found between objects.
2505 ;; That's why, along with the parser, successor functions are provided
2506 ;; for each object. Some objects share the same successor (i.e. `code'
2507 ;; and `verbatim' objects).
2509 ;; A successor must accept a single argument bounding the search. It
2510 ;; will return either a cons cell whose CAR is the object's type, as
2511 ;; a symbol, and CDR the position of its next occurrence, or nil.
2513 ;; Successors follow the naming convention:
2514 ;; org-element-NAME-successor, where NAME is the name of the
2515 ;; successor, as defined in `org-element-all-successors'.
2517 ;; Some object types (i.e. `italic') are recursive. Restrictions on
2518 ;; object types they can contain will be specified in
2519 ;; `org-element-object-restrictions'.
2521 ;; Adding a new type of object is simple. Implement a successor,
2522 ;; a parser, and an interpreter for it, all following the naming
2523 ;; convention. Register type in `org-element-all-objects' and
2524 ;; successor in `org-element-all-successors'. Maybe tweak
2525 ;; restrictions about it, and that's it.
2530 (defun org-element-bold-parser ()
2531 "Parse bold object at point.
2533 Return a list whose CAR is `bold' and CDR is a plist with
2534 `:begin', `:end', `:contents-begin' and `:contents-end' and
2535 `:post-blank' keywords.
2537 Assume point is at the first star marker."
2539 (unless (bolp) (backward-char 1))
2540 (looking-at org-emph-re
)
2541 (let ((begin (match-beginning 2))
2542 (contents-begin (match-beginning 4))
2543 (contents-end (match-end 4))
2544 (post-blank (progn (goto-char (match-end 2))
2545 (skip-chars-forward " \t")))
2550 :contents-begin contents-begin
2551 :contents-end contents-end
2552 :post-blank post-blank
)))))
2554 (defun org-element-bold-interpreter (bold contents
)
2555 "Interpret BOLD object as Org syntax.
2556 CONTENTS is the contents of the object."
2557 (format "*%s*" contents
))
2559 (defun org-element-text-markup-successor (limit)
2560 "Search for the next text-markup object.
2562 LIMIT bounds the search.
2564 Return value is a cons cell whose CAR is a symbol among `bold',
2565 `italic', `underline', `strike-through', `code' and `verbatim'
2566 and CDR is beginning position."
2568 (unless (bolp) (backward-char))
2569 (when (re-search-forward org-emph-re limit t
)
2570 (let ((marker (match-string 3)))
2572 ((equal marker
"*") 'bold
)
2573 ((equal marker
"/") 'italic
)
2574 ((equal marker
"_") 'underline
)
2575 ((equal marker
"+") 'strike-through
)
2576 ((equal marker
"~") 'code
)
2577 ((equal marker
"=") 'verbatim
)
2578 (t (error "Unknown marker at %d" (match-beginning 3))))
2579 (match-beginning 2))))))
2584 (defun org-element-code-parser ()
2585 "Parse code object at point.
2587 Return a list whose CAR is `code' and CDR is a plist with
2588 `:value', `:begin', `:end' and `:post-blank' keywords.
2590 Assume point is at the first tilde marker."
2592 (unless (bolp) (backward-char 1))
2593 (looking-at org-emph-re
)
2594 (let ((begin (match-beginning 2))
2595 (value (org-match-string-no-properties 4))
2596 (post-blank (progn (goto-char (match-end 2))
2597 (skip-chars-forward " \t")))
2603 :post-blank post-blank
)))))
2605 (defun org-element-code-interpreter (code contents
)
2606 "Interpret CODE object as Org syntax.
2608 (format "~%s~" (org-element-property :value code
)))
2613 (defun org-element-entity-parser ()
2614 "Parse entity at point.
2616 Return a list whose CAR is `entity' and CDR a plist with
2617 `:begin', `:end', `:latex', `:latex-math-p', `:html', `:latin1',
2618 `:utf-8', `:ascii', `:use-brackets-p' and `:post-blank' as
2621 Assume point is at the beginning of the entity."
2623 (looking-at "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)")
2624 (let* ((value (org-entity-get (match-string 1)))
2625 (begin (match-beginning 0))
2626 (bracketsp (string= (match-string 2) "{}"))
2627 (post-blank (progn (goto-char (match-end 1))
2628 (when bracketsp
(forward-char 2))
2629 (skip-chars-forward " \t")))
2632 (list :name
(car value
)
2633 :latex
(nth 1 value
)
2634 :latex-math-p
(nth 2 value
)
2636 :ascii
(nth 4 value
)
2637 :latin1
(nth 5 value
)
2638 :utf-8
(nth 6 value
)
2641 :use-brackets-p bracketsp
2642 :post-blank post-blank
)))))
2644 (defun org-element-entity-interpreter (entity contents
)
2645 "Interpret ENTITY object as Org syntax.
2648 (org-element-property :name entity
)
2649 (when (org-element-property :use-brackets-p entity
) "{}")))
2651 (defun org-element-latex-or-entity-successor (limit)
2652 "Search for the next latex-fragment or entity object.
2654 LIMIT bounds the search.
2656 Return value is a cons cell whose CAR is `entity' or
2657 `latex-fragment' and CDR is beginning position."
2659 (unless (bolp) (backward-char))
2661 (remove "begin" (plist-get org-format-latex-options
:matchers
)))
2662 ;; ENTITY-RE matches both LaTeX commands and Org entities.
2664 "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))
2665 (when (re-search-forward
2666 (concat (mapconcat (lambda (e) (nth 1 (assoc e org-latex-regexps
)))
2670 (goto-char (match-beginning 0))
2671 (if (looking-at entity-re
)
2672 ;; Determine if it's a real entity or a LaTeX command.
2673 (cons (if (org-entity-get (match-string 1)) 'entity
'latex-fragment
)
2674 (match-beginning 0))
2675 ;; No entity nor command: point is at a LaTeX fragment.
2676 ;; Determine its type to get the correct beginning position.
2677 (cons 'latex-fragment
2680 (when (looking-at (nth 1 (assoc e org-latex-regexps
)))
2683 (nth 2 (assoc e org-latex-regexps
))))))
2690 (defun org-element-export-snippet-parser ()
2691 "Parse export snippet at point.
2693 Return a list whose CAR is `export-snippet' and CDR a plist with
2694 `:begin', `:end', `:back-end', `:value' and `:post-blank' as
2697 Assume point is at the beginning of the snippet."
2699 (re-search-forward "@@\\([-A-Za-z0-9]+\\):" nil t
)
2700 (let* ((begin (match-beginning 0))
2701 (back-end (org-match-string-no-properties 1))
2702 (value (buffer-substring-no-properties
2704 (progn (re-search-forward "@@" nil t
) (match-beginning 0))))
2705 (post-blank (skip-chars-forward " \t"))
2707 (list 'export-snippet
2708 (list :back-end back-end
2712 :post-blank post-blank
)))))
2714 (defun org-element-export-snippet-interpreter (export-snippet contents
)
2715 "Interpret EXPORT-SNIPPET object as Org syntax.
2718 (org-element-property :back-end export-snippet
)
2719 (org-element-property :value export-snippet
)))
2721 (defun org-element-export-snippet-successor (limit)
2722 "Search for the next export-snippet object.
2724 LIMIT bounds the search.
2726 Return value is a cons cell whose CAR is `export-snippet' and CDR
2727 its beginning position."
2730 (when (and (re-search-forward "@@[-A-Za-z0-9]+:" limit t
)
2731 (setq beg
(match-beginning 0))
2732 (search-forward "@@" limit t
))
2733 (cons 'export-snippet beg
)))))
2736 ;;;; Footnote Reference
2738 (defun org-element-footnote-reference-parser ()
2739 "Parse footnote reference at point.
2741 Return a list whose CAR is `footnote-reference' and CDR a plist
2742 with `:label', `:type', `:inline-definition', `:begin', `:end'
2743 and `:post-blank' as keywords."
2745 (looking-at org-footnote-re
)
2746 (let* ((begin (point))
2747 (label (or (org-match-string-no-properties 2)
2748 (org-match-string-no-properties 3)
2749 (and (match-string 1)
2750 (concat "fn:" (org-match-string-no-properties 1)))))
2751 (type (if (or (not label
) (match-string 1)) 'inline
'standard
))
2752 (inner-begin (match-end 0))
2756 (while (and (> count
0) (re-search-forward "[][]" nil t
))
2757 (if (equal (match-string 0) "[") (incf count
) (decf count
)))
2759 (post-blank (progn (goto-char (1+ inner-end
))
2760 (skip-chars-forward " \t")))
2763 (list 'footnote-reference
2768 :post-blank post-blank
))))
2769 (org-element-put-property
2770 footnote-reference
:inline-definition
2771 (and (eq type
'inline
)
2772 (org-element-parse-secondary-string
2773 (buffer-substring inner-begin inner-end
)
2774 (org-element-restriction 'footnote-reference
)
2775 footnote-reference
))))))
2777 (defun org-element-footnote-reference-interpreter (footnote-reference contents
)
2778 "Interpret FOOTNOTE-REFERENCE object as Org syntax.
2780 (let ((label (or (org-element-property :label footnote-reference
) "fn:"))
2783 (org-element-property :inline-definition footnote-reference
)))
2784 (if (not inline-def
) ""
2785 (concat ":" (org-element-interpret-data inline-def
))))))
2786 (format "[%s]" (concat label def
))))
2788 (defun org-element-footnote-reference-successor (limit)
2789 "Search for the next footnote-reference object.
2791 LIMIT bounds the search.
2793 Return value is a cons cell whose CAR is `footnote-reference' and
2794 CDR is beginning position."
2797 (while (re-search-forward org-footnote-re limit t
)
2799 (let ((beg (match-beginning 0))
2802 (while (re-search-forward "[][]" limit t
)
2803 (if (equal (match-string 0) "[") (incf count
) (decf count
))
2805 (throw 'exit
(cons 'footnote-reference beg
))))))))))
2808 ;;;; Inline Babel Call
2810 (defun org-element-inline-babel-call-parser ()
2811 "Parse inline babel call at point.
2813 Return a list whose CAR is `inline-babel-call' and CDR a plist
2814 with `:begin', `:end', `:info' and `:post-blank' as keywords.
2816 Assume point is at the beginning of the babel call."
2818 (unless (bolp) (backward-char))
2819 (looking-at org-babel-inline-lob-one-liner-regexp
)
2820 (let ((info (save-match-data (org-babel-lob-get-info)))
2821 (begin (match-end 1))
2822 (post-blank (progn (goto-char (match-end 0))
2823 (skip-chars-forward " \t")))
2825 (list 'inline-babel-call
2829 :post-blank post-blank
)))))
2831 (defun org-element-inline-babel-call-interpreter (inline-babel-call contents
)
2832 "Interpret INLINE-BABEL-CALL object as Org syntax.
2834 (let* ((babel-info (org-element-property :info inline-babel-call
))
2835 (main-source (car babel-info
))
2836 (post-options (nth 1 babel-info
)))
2838 (if (string-match "\\[\\(\\[.*?\\]\\)\\]" main-source
)
2839 ;; Remove redundant square brackets.
2841 (match-string 1 main-source
) nil nil main-source
)
2843 (and post-options
(format "[%s]" post-options
)))))
2845 (defun org-element-inline-babel-call-successor (limit)
2846 "Search for the next inline-babel-call object.
2848 LIMIT bounds the search.
2850 Return value is a cons cell whose CAR is `inline-babel-call' and
2851 CDR is beginning position."
2853 ;; Use a simplified version of
2854 ;; `org-babel-inline-lob-one-liner-regexp'.
2855 (when (re-search-forward
2856 "call_\\([^()\n]+?\\)\\(?:\\[.*?\\]\\)?([^\n]*?)\\(\\[.*?\\]\\)?"
2858 (cons 'inline-babel-call
(match-beginning 0)))))
2861 ;;;; Inline Src Block
2863 (defun org-element-inline-src-block-parser ()
2864 "Parse inline source block at point.
2866 LIMIT bounds the search.
2868 Return a list whose CAR is `inline-src-block' and CDR a plist
2869 with `:begin', `:end', `:language', `:value', `:parameters' and
2870 `:post-blank' as keywords.
2872 Assume point is at the beginning of the inline src block."
2874 (unless (bolp) (backward-char))
2875 (looking-at org-babel-inline-src-block-regexp
)
2876 (let ((begin (match-beginning 1))
2877 (language (org-match-string-no-properties 2))
2878 (parameters (org-match-string-no-properties 4))
2879 (value (org-match-string-no-properties 5))
2880 (post-blank (progn (goto-char (match-end 0))
2881 (skip-chars-forward " \t")))
2883 (list 'inline-src-block
2884 (list :language language
2886 :parameters parameters
2889 :post-blank post-blank
)))))
2891 (defun org-element-inline-src-block-interpreter (inline-src-block contents
)
2892 "Interpret INLINE-SRC-BLOCK object as Org syntax.
2894 (let ((language (org-element-property :language inline-src-block
))
2895 (arguments (org-element-property :parameters inline-src-block
))
2896 (body (org-element-property :value inline-src-block
)))
2897 (format "src_%s%s{%s}"
2899 (if arguments
(format "[%s]" arguments
) "")
2902 (defun org-element-inline-src-block-successor (limit)
2903 "Search for the next inline-babel-call element.
2905 LIMIT bounds the search.
2907 Return value is a cons cell whose CAR is `inline-babel-call' and
2908 CDR is beginning position."
2910 (unless (bolp) (backward-char))
2911 (when (re-search-forward org-babel-inline-src-block-regexp limit t
)
2912 (cons 'inline-src-block
(match-beginning 1)))))
2916 (defun org-element-italic-parser ()
2917 "Parse italic object at point.
2919 Return a list whose CAR is `italic' and CDR is a plist with
2920 `:begin', `:end', `:contents-begin' and `:contents-end' and
2921 `:post-blank' keywords.
2923 Assume point is at the first slash marker."
2925 (unless (bolp) (backward-char 1))
2926 (looking-at org-emph-re
)
2927 (let ((begin (match-beginning 2))
2928 (contents-begin (match-beginning 4))
2929 (contents-end (match-end 4))
2930 (post-blank (progn (goto-char (match-end 2))
2931 (skip-chars-forward " \t")))
2936 :contents-begin contents-begin
2937 :contents-end contents-end
2938 :post-blank post-blank
)))))
2940 (defun org-element-italic-interpreter (italic contents
)
2941 "Interpret ITALIC object as Org syntax.
2942 CONTENTS is the contents of the object."
2943 (format "/%s/" contents
))
2948 (defun org-element-latex-fragment-parser ()
2949 "Parse latex fragment at point.
2951 Return a list whose CAR is `latex-fragment' and CDR a plist with
2952 `:value', `:begin', `:end', and `:post-blank' as keywords.
2954 Assume point is at the beginning of the latex fragment."
2956 (let* ((begin (point))
2960 (let ((latex-regexp (nth 1 (assoc e org-latex-regexps
))))
2961 (when (or (looking-at latex-regexp
)
2965 (looking-at latex-regexp
))))
2966 (throw 'exit
(nth 2 (assoc e org-latex-regexps
))))))
2967 (plist-get org-format-latex-options
:matchers
))
2968 ;; None found: it's a macro.
2969 (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")
2971 (value (match-string-no-properties substring-match
))
2972 (post-blank (progn (goto-char (match-end substring-match
))
2973 (skip-chars-forward " \t")))
2975 (list 'latex-fragment
2979 :post-blank post-blank
)))))
2981 (defun org-element-latex-fragment-interpreter (latex-fragment contents
)
2982 "Interpret LATEX-FRAGMENT object as Org syntax.
2984 (org-element-property :value latex-fragment
))
2988 (defun org-element-line-break-parser ()
2989 "Parse line break at point.
2991 Return a list whose CAR is `line-break', and CDR a plist with
2992 `:begin', `:end' and `:post-blank' keywords.
2994 Assume point is at the beginning of the line break."
2996 (list :begin
(point)
2997 :end
(progn (forward-line) (point))
3000 (defun org-element-line-break-interpreter (line-break contents
)
3001 "Interpret LINE-BREAK object as Org syntax.
3005 (defun org-element-line-break-successor (limit)
3006 "Search for the next line-break object.
3008 LIMIT bounds the search.
3010 Return value is a cons cell whose CAR is `line-break' and CDR is
3011 beginning position."
3013 (let ((beg (and (re-search-forward "[^\\\\]\\(\\\\\\\\\\)[ \t]*$" limit t
)
3014 (goto-char (match-beginning 1)))))
3015 ;; A line break can only happen on a non-empty line.
3016 (when (and beg
(re-search-backward "\\S-" (point-at-bol) t
))
3017 (cons 'line-break beg
)))))
3022 (defun org-element-link-parser ()
3023 "Parse link at point.
3025 Return a list whose CAR is `link' and CDR a plist with `:type',
3026 `:path', `:raw-link', `:application', `:search-option', `:begin',
3027 `:end', `:contents-begin', `:contents-end' and `:post-blank' as
3030 Assume point is at the beginning of the link."
3032 (let ((begin (point))
3033 end contents-begin contents-end link-end post-blank path type
3034 raw-link link search-option application
)
3036 ;; Type 1: Text targeted from a radio target.
3037 ((and org-target-link-regexp
(looking-at org-target-link-regexp
))
3039 link-end
(match-end 0)
3040 path
(org-match-string-no-properties 0)))
3041 ;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]]
3042 ((looking-at org-bracket-link-regexp
)
3043 (setq contents-begin
(match-beginning 3)
3044 contents-end
(match-end 3)
3045 link-end
(match-end 0)
3046 ;; RAW-LINK is the original link. Expand any
3047 ;; abbreviation in it.
3048 raw-link
(org-translate-link
3049 (org-link-expand-abbrev
3050 (org-match-string-no-properties 1)))
3051 link
(org-link-unescape raw-link
))
3052 ;; Determine TYPE of link and set PATH accordingly.
3055 ((or (file-name-absolute-p link
) (string-match "^\\.\\.?/" link
))
3056 (setq type
"file" path link
))
3057 ;; Explicit type (http, irc, bbdb...). See `org-link-types'.
3058 ((string-match org-link-re-with-space3 link
)
3059 (setq type
(match-string 1 link
) path
(match-string 2 link
)))
3060 ;; Id type: PATH is the id.
3061 ((string-match "^id:\\([-a-f0-9]+\\)" link
)
3062 (setq type
"id" path
(match-string 1 link
)))
3063 ;; Code-ref type: PATH is the name of the reference.
3064 ((string-match "^(\\(.*\\))$" link
)
3065 (setq type
"coderef" path
(match-string 1 link
)))
3066 ;; Custom-id type: PATH is the name of the custom id.
3067 ((= (aref link
0) ?
#)
3068 (setq type
"custom-id" path
(substring link
1)))
3069 ;; Fuzzy type: Internal link either matches a target, an
3070 ;; headline name or nothing. PATH is the target or
3072 (t (setq type
"fuzzy" path link
))))
3073 ;; Type 3: Plain link, i.e. http://orgmode.org
3074 ((looking-at org-plain-link-re
)
3075 (setq raw-link
(org-match-string-no-properties 0)
3076 type
(org-match-string-no-properties 1)
3077 path
(org-match-string-no-properties 2)
3078 link-end
(match-end 0)))
3079 ;; Type 4: Angular link, i.e. <http://orgmode.org>
3080 ((looking-at org-angle-link-re
)
3081 (setq raw-link
(buffer-substring-no-properties
3082 (match-beginning 1) (match-end 2))
3083 type
(org-match-string-no-properties 1)
3084 path
(org-match-string-no-properties 2)
3085 link-end
(match-end 0))))
3086 ;; In any case, deduce end point after trailing white space from
3087 ;; LINK-END variable.
3088 (setq post-blank
(progn (goto-char link-end
) (skip-chars-forward " \t"))
3090 ;; Extract search option and opening application out of
3091 ;; "file"-type links.
3092 (when (member type org-element-link-type-is-file
)
3094 (cond ((string-match "^file\\+\\(.*\\)$" type
)
3095 (setq application
(match-string 1 type
)))
3096 ((not (string-match "^file" type
))
3097 (setq application type
)))
3098 ;; Extract search option from PATH.
3099 (when (string-match "::\\(.*\\)$" path
)
3100 (setq search-option
(match-string 1 path
)
3101 path
(replace-match "" nil nil path
)))
3102 ;; Make sure TYPE always report "file".
3107 :raw-link
(or raw-link path
)
3108 :application application
3109 :search-option search-option
3112 :contents-begin contents-begin
3113 :contents-end contents-end
3114 :post-blank post-blank
)))))
3116 (defun org-element-link-interpreter (link contents
)
3117 "Interpret LINK object as Org syntax.
3118 CONTENTS is the contents of the object, or nil."
3119 (let ((type (org-element-property :type link
))
3120 (raw-link (org-element-property :raw-link link
)))
3121 (if (string= type
"radio") raw-link
3124 (if contents
(format "[%s]" contents
) "")))))
3126 (defun org-element-link-successor (limit)
3127 "Search for the next link object.
3129 LIMIT bounds the search.
3131 Return value is a cons cell whose CAR is `link' and CDR is
3132 beginning position."
3135 (if (not org-target-link-regexp
) org-any-link-re
3136 (concat org-any-link-re
"\\|" org-target-link-regexp
))))
3137 (when (re-search-forward link-regexp limit t
)
3138 (cons 'link
(match-beginning 0))))))
3140 (defun org-element-plain-link-successor (limit)
3141 "Search for the next plain link object.
3143 LIMIT bounds the search.
3145 Return value is a cons cell whose CAR is `link' and CDR is
3146 beginning position."
3147 (and (save-excursion (re-search-forward org-plain-link-re limit t
))
3148 (cons 'link
(match-beginning 0))))
3153 (defun org-element-macro-parser ()
3154 "Parse macro at point.
3156 Return a list whose CAR is `macro' and CDR a plist with `:key',
3157 `:args', `:begin', `:end', `:value' and `:post-blank' as
3160 Assume point is at the macro."
3162 (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
3163 (let ((begin (point))
3164 (key (downcase (org-match-string-no-properties 1)))
3165 (value (org-match-string-no-properties 0))
3166 (post-blank (progn (goto-char (match-end 0))
3167 (skip-chars-forward " \t")))
3169 (args (let ((args (org-match-string-no-properties 3)))
3171 ;; Do not use `org-split-string' since empty
3172 ;; strings are meaningful here.
3174 (replace-regexp-in-string
3175 "\\(\\\\*\\)\\(,\\)"
3177 (let ((len (length (match-string 1 str
))))
3178 (concat (make-string (/ len
2) ?
\\)
3179 (if (zerop (mod len
2)) "\000" ","))))
3188 :post-blank post-blank
)))))
3190 (defun org-element-macro-interpreter (macro contents
)
3191 "Interpret MACRO object as Org syntax.
3193 (org-element-property :value macro
))
3195 (defun org-element-macro-successor (limit)
3196 "Search for the next macro object.
3198 LIMIT bounds the search.
3200 Return value is cons cell whose CAR is `macro' and CDR is
3201 beginning position."
3203 (when (re-search-forward
3204 "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
3206 (cons 'macro
(match-beginning 0)))))
3211 (defun org-element-radio-target-parser ()
3212 "Parse radio target at point.
3214 Return a list whose CAR is `radio-target' and CDR a plist with
3215 `:begin', `:end', `:contents-begin', `:contents-end', `:value'
3216 and `:post-blank' as keywords.
3218 Assume point is at the radio target."
3220 (looking-at org-radio-target-regexp
)
3221 (let ((begin (point))
3222 (contents-begin (match-beginning 1))
3223 (contents-end (match-end 1))
3224 (value (org-match-string-no-properties 1))
3225 (post-blank (progn (goto-char (match-end 0))
3226 (skip-chars-forward " \t")))
3231 :contents-begin contents-begin
3232 :contents-end contents-end
3233 :post-blank post-blank
3236 (defun org-element-radio-target-interpreter (target contents
)
3237 "Interpret TARGET object as Org syntax.
3238 CONTENTS is the contents of the object."
3239 (concat "<<<" contents
">>>"))
3241 (defun org-element-radio-target-successor (limit)
3242 "Search for the next radio-target object.
3244 LIMIT bounds the search.
3246 Return value is a cons cell whose CAR is `radio-target' and CDR
3247 is beginning position."
3249 (when (re-search-forward org-radio-target-regexp limit t
)
3250 (cons 'radio-target
(match-beginning 0)))))
3253 ;;;; Statistics Cookie
3255 (defun org-element-statistics-cookie-parser ()
3256 "Parse statistics cookie at point.
3258 Return a list whose CAR is `statistics-cookie', and CDR a plist
3259 with `:begin', `:end', `:value' and `:post-blank' keywords.
3261 Assume point is at the beginning of the statistics-cookie."
3263 (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
3264 (let* ((begin (point))
3265 (value (buffer-substring-no-properties
3266 (match-beginning 0) (match-end 0)))
3267 (post-blank (progn (goto-char (match-end 0))
3268 (skip-chars-forward " \t")))
3270 (list 'statistics-cookie
3274 :post-blank post-blank
)))))
3276 (defun org-element-statistics-cookie-interpreter (statistics-cookie contents
)
3277 "Interpret STATISTICS-COOKIE object as Org syntax.
3279 (org-element-property :value statistics-cookie
))
3281 (defun org-element-statistics-cookie-successor (limit)
3282 "Search for the next statistics cookie object.
3284 LIMIT bounds the search.
3286 Return value is a cons cell whose CAR is `statistics-cookie' and
3287 CDR is beginning position."
3289 (when (re-search-forward "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]" limit t
)
3290 (cons 'statistics-cookie
(match-beginning 0)))))
3295 (defun org-element-strike-through-parser ()
3296 "Parse strike-through object at point.
3298 Return a list whose CAR is `strike-through' and CDR is a plist
3299 with `:begin', `:end', `:contents-begin' and `:contents-end' and
3300 `:post-blank' keywords.
3302 Assume point is at the first plus sign marker."
3304 (unless (bolp) (backward-char 1))
3305 (looking-at org-emph-re
)
3306 (let ((begin (match-beginning 2))
3307 (contents-begin (match-beginning 4))
3308 (contents-end (match-end 4))
3309 (post-blank (progn (goto-char (match-end 2))
3310 (skip-chars-forward " \t")))
3312 (list 'strike-through
3315 :contents-begin contents-begin
3316 :contents-end contents-end
3317 :post-blank post-blank
)))))
3319 (defun org-element-strike-through-interpreter (strike-through contents
)
3320 "Interpret STRIKE-THROUGH object as Org syntax.
3321 CONTENTS is the contents of the object."
3322 (format "+%s+" contents
))
3327 (defun org-element-subscript-parser ()
3328 "Parse subscript at point.
3330 Return a list whose CAR is `subscript' and CDR a plist with
3331 `:begin', `:end', `:contents-begin', `:contents-end',
3332 `:use-brackets-p' and `:post-blank' as keywords.
3334 Assume point is at the underscore."
3336 (unless (bolp) (backward-char))
3337 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp
)
3339 (not (looking-at org-match-substring-regexp
))))
3340 (begin (match-beginning 2))
3341 (contents-begin (or (match-beginning 5)
3342 (match-beginning 3)))
3343 (contents-end (or (match-end 5) (match-end 3)))
3344 (post-blank (progn (goto-char (match-end 0))
3345 (skip-chars-forward " \t")))
3350 :use-brackets-p bracketsp
3351 :contents-begin contents-begin
3352 :contents-end contents-end
3353 :post-blank post-blank
)))))
3355 (defun org-element-subscript-interpreter (subscript contents
)
3356 "Interpret SUBSCRIPT object as Org syntax.
3357 CONTENTS is the contents of the object."
3359 (if (org-element-property :use-brackets-p subscript
) "_{%s}" "_%s")
3362 (defun org-element-sub/superscript-successor
(limit)
3363 "Search for the next sub/superscript object.
3365 LIMIT bounds the search.
3367 Return value is a cons cell whose CAR is either `subscript' or
3368 `superscript' and CDR is beginning position."
3370 (unless (bolp) (backward-char))
3371 (when (re-search-forward org-match-substring-regexp limit t
)
3372 (cons (if (string= (match-string 2) "_") 'subscript
'superscript
)
3373 (match-beginning 2)))))
3378 (defun org-element-superscript-parser ()
3379 "Parse superscript at point.
3381 Return a list whose CAR is `superscript' and CDR a plist with
3382 `:begin', `:end', `:contents-begin', `:contents-end',
3383 `:use-brackets-p' and `:post-blank' as keywords.
3385 Assume point is at the caret."
3387 (unless (bolp) (backward-char))
3388 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp
) t
3389 (not (looking-at org-match-substring-regexp
))))
3390 (begin (match-beginning 2))
3391 (contents-begin (or (match-beginning 5)
3392 (match-beginning 3)))
3393 (contents-end (or (match-end 5) (match-end 3)))
3394 (post-blank (progn (goto-char (match-end 0))
3395 (skip-chars-forward " \t")))
3400 :use-brackets-p bracketsp
3401 :contents-begin contents-begin
3402 :contents-end contents-end
3403 :post-blank post-blank
)))))
3405 (defun org-element-superscript-interpreter (superscript contents
)
3406 "Interpret SUPERSCRIPT object as Org syntax.
3407 CONTENTS is the contents of the object."
3409 (if (org-element-property :use-brackets-p superscript
) "^{%s}" "^%s")
3415 (defun org-element-table-cell-parser ()
3416 "Parse table cell at point.
3418 Return a list whose CAR is `table-cell' and CDR is a plist
3419 containing `:begin', `:end', `:contents-begin', `:contents-end'
3420 and `:post-blank' keywords."
3421 (looking-at "[ \t]*\\(.*?\\)[ \t]*|")
3422 (let* ((begin (match-beginning 0))
3424 (contents-begin (match-beginning 1))
3425 (contents-end (match-end 1)))
3429 :contents-begin contents-begin
3430 :contents-end contents-end
3433 (defun org-element-table-cell-interpreter (table-cell contents
)
3434 "Interpret TABLE-CELL element as Org syntax.
3435 CONTENTS is the contents of the cell, or nil."
3436 (concat " " contents
" |"))
3438 (defun org-element-table-cell-successor (limit)
3439 "Search for the next table-cell object.
3441 LIMIT bounds the search.
3443 Return value is a cons cell whose CAR is `table-cell' and CDR is
3444 beginning position."
3445 (when (looking-at "[ \t]*.*?[ \t]*|") (cons 'table-cell
(point))))
3450 (defun org-element-target-parser ()
3451 "Parse target at point.
3453 Return a list whose CAR is `target' and CDR a plist with
3454 `:begin', `:end', `:value' and `:post-blank' as keywords.
3456 Assume point is at the target."
3458 (looking-at org-target-regexp
)
3459 (let ((begin (point))
3460 (value (org-match-string-no-properties 1))
3461 (post-blank (progn (goto-char (match-end 0))
3462 (skip-chars-forward " \t")))
3468 :post-blank post-blank
)))))
3470 (defun org-element-target-interpreter (target contents
)
3471 "Interpret TARGET object as Org syntax.
3473 (format "<<%s>>" (org-element-property :value target
)))
3475 (defun org-element-target-successor (limit)
3476 "Search for the next target object.
3478 LIMIT bounds the search.
3480 Return value is a cons cell whose CAR is `target' and CDR is
3481 beginning position."
3483 (when (re-search-forward org-target-regexp limit t
)
3484 (cons 'target
(match-beginning 0)))))
3489 (defun org-element-timestamp-parser ()
3490 "Parse time stamp at point.
3492 Return a list whose CAR is `timestamp', and CDR a plist with
3493 `:type', `:begin', `:end', `:value' and `:post-blank' keywords.
3495 Assume point is at the beginning of the timestamp."
3497 (let* ((begin (point))
3498 (activep (eq (char-after) ?
<))
3501 (looking-at "\\([<[]\\(%%\\)?.*?\\)[]>]\\(?:--\\([<[].*?[]>]\\)\\)?")
3502 (match-string-no-properties 0)))
3503 (date-start (match-string-no-properties 1))
3504 (date-end (match-string 3))
3505 (diaryp (match-beginning 2))
3506 (post-blank (progn (goto-char (match-end 0))
3507 (skip-chars-forward " \t")))
3512 "[012]?[0-9]:[0-5][0-9]\\(-\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)"
3514 (cons (string-to-number (match-string 2 date-start
))
3515 (string-to-number (match-string 3 date-start
)))))
3516 (type (cond (diaryp 'diary
)
3517 ((and activep
(or date-end time-range
)) 'active-range
)
3519 ((or date-end time-range
) 'inactive-range
)
3523 (string-match "\\([.+]?\\+\\)\\([0-9]+\\)\\([hdwmy]\\)>"
3527 (let ((type (match-string 1 raw-value
)))
3528 (cond ((equal "++" type
) 'catch-up
)
3529 ((equal ".+" type
) 'restart
)
3531 :repeater-value
(string-to-number (match-string 2 raw-value
))
3533 (case (string-to-char (match-string 3 raw-value
))
3534 (?h
'hour
) (?d
'day
) (?w
'week
) (?m
'month
) (t 'year
)))))
3535 year-start month-start day-start hour-start minute-start year-end
3536 month-end day-end hour-end minute-end
)
3537 ;; Parse date-start.
3539 (let ((date (org-parse-time-string date-start t
)))
3540 (setq year-start
(nth 5 date
)
3541 month-start
(nth 4 date
)
3542 day-start
(nth 3 date
)
3543 hour-start
(nth 2 date
)
3544 minute-start
(nth 1 date
))))
3545 ;; Compute date-end. It can be provided directly in time-stamp,
3546 ;; or extracted from time range. Otherwise, it defaults to the
3547 ;; same values as date-start.
3549 (let ((date (and date-end
(org-parse-time-string date-end t
))))
3550 (setq year-end
(or (nth 5 date
) year-start
)
3551 month-end
(or (nth 4 date
) month-start
)
3552 day-end
(or (nth 3 date
) day-start
)
3553 hour-end
(or (nth 2 date
) (car time-range
) hour-start
)
3554 minute-end
(or (nth 1 date
) (cdr time-range
) minute-start
))))
3556 (nconc (list :type type
3557 :raw-value raw-value
3558 :year-start year-start
3559 :month-start month-start
3560 :day-start day-start
3561 :hour-start hour-start
3562 :minute-start minute-start
3564 :month-end month-end
3567 :minute-end minute-end
3570 :post-blank post-blank
)
3573 (defun org-element-timestamp-interpreter (timestamp contents
)
3574 "Interpret TIMESTAMP object as Org syntax.
3576 ;; Use `:raw-value' if specified.
3577 (or (org-element-property :raw-value timestamp
)
3578 ;; Otherwise, build timestamp string.
3579 (let* ((repeat-string
3581 (case (org-element-property :repeater-type timestamp
)
3582 (cumulate "+") (catch-up "++") (restart ".+"))
3583 (let ((val (org-element-property :repeater-value timestamp
)))
3584 (and val
(number-to-string val
)))
3585 (case (org-element-property :repeater-unit timestamp
)
3586 (hour "h") (day "d") (week "w") (month "m") (year "y"))))
3588 ;; Build an Org timestamp string from TIME. ACTIVEP is
3589 ;; non-nil when time stamp is active. If WITH-TIME-P is
3590 ;; non-nil, add a time part. HOUR-END and MINUTE-END
3591 ;; specify a time range in the timestamp. REPEAT-STRING
3592 ;; is the repeater string, if any.
3593 (lambda (time activep
&optional with-time-p hour-end minute-end
)
3594 (let ((ts (format-time-string
3595 (funcall (if with-time-p
'cdr
'car
)
3596 org-time-stamp-formats
)
3598 (when (and hour-end minute-end
)
3599 (string-match "[012]?[0-9]:[0-5][0-9]" ts
)
3602 (format "\\&-%02d:%02d" hour-end minute-end
)
3604 (unless activep
(setq ts
(format "[%s]" (substring ts
1 -
1))))
3605 (when (org-string-nw-p repeat-string
)
3606 (setq ts
(concat (substring ts
0 -
1)
3609 (substring ts -
1))))
3612 (type (org-element-property :type timestamp
)))
3615 (let* ((minute-start (org-element-property :minute-start timestamp
))
3616 (minute-end (org-element-property :minute-end timestamp
))
3617 (hour-start (org-element-property :hour-start timestamp
))
3618 (hour-end (org-element-property :hour-end timestamp
))
3619 (time-range-p (and hour-start hour-end minute-start minute-end
3620 (or (/= hour-start hour-end
)
3621 (/= minute-start minute-end
)))))
3627 (org-element-property :day-start timestamp
)
3628 (org-element-property :month-start timestamp
)
3629 (org-element-property :year-start timestamp
))
3631 (and hour-start minute-start
)
3632 (and time-range-p hour-end
)
3633 (and time-range-p minute-end
))))
3634 ((active-range inactive-range
)
3635 (let ((minute-start (org-element-property :minute-start timestamp
))
3636 (minute-end (org-element-property :minute-end timestamp
))
3637 (hour-start (org-element-property :hour-start timestamp
))
3638 (hour-end (org-element-property :hour-end timestamp
)))
3641 build-ts-string
(encode-time
3645 (org-element-property :day-start timestamp
)
3646 (org-element-property :month-start timestamp
)
3647 (org-element-property :year-start timestamp
))
3648 (eq type
'active-range
)
3649 (and hour-start minute-start
))
3651 (funcall build-ts-string
3655 (org-element-property :day-end timestamp
)
3656 (org-element-property :month-end timestamp
)
3657 (org-element-property :year-end timestamp
))
3658 (eq type
'active-range
)
3659 (and hour-end minute-end
)))))))))
3661 (defun org-element-timestamp-successor (limit)
3662 "Search for the next timestamp object.
3664 LIMIT bounds the search.
3666 Return value is a cons cell whose CAR is `timestamp' and CDR is
3667 beginning position."
3669 (when (re-search-forward
3670 (concat org-ts-regexp-both
3672 "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
3674 "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
3676 (cons 'timestamp
(match-beginning 0)))))
3681 (defun org-element-underline-parser ()
3682 "Parse underline object at point.
3684 Return a list whose CAR is `underline' and CDR is a plist with
3685 `:begin', `:end', `:contents-begin' and `:contents-end' and
3686 `:post-blank' keywords.
3688 Assume point is at the first underscore marker."
3690 (unless (bolp) (backward-char 1))
3691 (looking-at org-emph-re
)
3692 (let ((begin (match-beginning 2))
3693 (contents-begin (match-beginning 4))
3694 (contents-end (match-end 4))
3695 (post-blank (progn (goto-char (match-end 2))
3696 (skip-chars-forward " \t")))
3701 :contents-begin contents-begin
3702 :contents-end contents-end
3703 :post-blank post-blank
)))))
3705 (defun org-element-underline-interpreter (underline contents
)
3706 "Interpret UNDERLINE object as Org syntax.
3707 CONTENTS is the contents of the object."
3708 (format "_%s_" contents
))
3713 (defun org-element-verbatim-parser ()
3714 "Parse verbatim object at point.
3716 Return a list whose CAR is `verbatim' and CDR is a plist with
3717 `:value', `:begin', `:end' and `:post-blank' keywords.
3719 Assume point is at the first equal sign marker."
3721 (unless (bolp) (backward-char 1))
3722 (looking-at org-emph-re
)
3723 (let ((begin (match-beginning 2))
3724 (value (org-match-string-no-properties 4))
3725 (post-blank (progn (goto-char (match-end 2))
3726 (skip-chars-forward " \t")))
3732 :post-blank post-blank
)))))
3734 (defun org-element-verbatim-interpreter (verbatim contents
)
3735 "Interpret VERBATIM object as Org syntax.
3737 (format "=%s=" (org-element-property :value verbatim
)))
3741 ;;; Parsing Element Starting At Point
3743 ;; `org-element--current-element' is the core function of this section.
3744 ;; It returns the Lisp representation of the element starting at
3747 ;; `org-element--current-element' makes use of special modes. They
3748 ;; are activated for fixed element chaining (i.e. `plain-list' >
3749 ;; `item') or fixed conditional element chaining (i.e. `headline' >
3750 ;; `section'). Special modes are: `first-section', `item',
3751 ;; `node-property', `quote-section', `section' and `table-row'.
3753 (defun org-element--current-element
3754 (limit &optional granularity special structure
)
3755 "Parse the element starting at point.
3757 LIMIT bounds the search.
3759 Return value is a list like (TYPE PROPS) where TYPE is the type
3760 of the element and PROPS a plist of properties associated to the
3763 Possible types are defined in `org-element-all-elements'.
3765 Optional argument GRANULARITY determines the depth of the
3766 recursion. Allowed values are `headline', `greater-element',
3767 `element', `object' or nil. When it is broader than `object' (or
3768 nil), secondary values will not be parsed, since they only
3771 Optional argument SPECIAL, when non-nil, can be either
3772 `first-section', `item', `node-property', `quote-section',
3773 `section', and `table-row'.
3775 If STRUCTURE isn't provided but SPECIAL is set to `item', it will
3778 This function assumes point is always at the beginning of the
3779 element it has to parse."
3781 (let ((case-fold-search t
)
3782 ;; Determine if parsing depth allows for secondary strings
3783 ;; parsing. It only applies to elements referenced in
3784 ;; `org-element-secondary-value-alist'.
3785 (raw-secondary-p (and granularity
(not (eq granularity
'object
)))))
3789 (org-element-item-parser limit structure raw-secondary-p
))
3791 ((eq special
'table-row
) (org-element-table-row-parser limit
))
3793 ((eq special
'node-property
) (org-element-node-property-parser limit
))
3795 ((org-with-limited-levels (org-at-heading-p))
3796 (org-element-headline-parser limit raw-secondary-p
))
3797 ;; Sections (must be checked after headline).
3798 ((eq special
'section
) (org-element-section-parser limit
))
3799 ((eq special
'quote-section
) (org-element-quote-section-parser limit
))
3800 ((eq special
'first-section
)
3801 (org-element-section-parser
3802 (or (save-excursion (org-with-limited-levels (outline-next-heading)))
3804 ;; When not at bol, point is at the beginning of an item or
3805 ;; a footnote definition: next item is always a paragraph.
3806 ((not (bolp)) (org-element-paragraph-parser limit
(list (point))))
3807 ;; Planning and Clock.
3808 ((looking-at org-planning-or-clock-line-re
)
3809 (if (equal (match-string 1) org-clock-string
)
3810 (org-element-clock-parser limit
)
3811 (org-element-planning-parser limit
)))
3814 (org-element-inlinetask-parser limit raw-secondary-p
))
3815 ;; From there, elements can have affiliated keywords.
3816 (t (let ((affiliated (org-element--collect-affiliated-keywords limit
)))
3818 ;; Jumping over affiliated keywords put point off-limits.
3819 ;; Parse them as regular keywords.
3820 ((and (cdr affiliated
) (>= (point) limit
))
3821 (goto-char (car affiliated
))
3822 (org-element-keyword-parser limit nil
))
3823 ;; LaTeX Environment.
3825 "[ \t]*\\\\begin{[A-Za-z0-9*]+}\\(\\[.*?\\]\\|{.*?}\\)*[ \t]*$")
3826 (org-element-latex-environment-parser limit affiliated
))
3827 ;; Drawer and Property Drawer.
3828 ((looking-at org-drawer-regexp
)
3829 (if (equal (match-string 1) "PROPERTIES")
3830 (org-element-property-drawer-parser limit affiliated
)
3831 (org-element-drawer-parser limit affiliated
)))
3833 ((looking-at "[ \t]*:\\( \\|$\\)")
3834 (org-element-fixed-width-parser limit affiliated
))
3835 ;; Inline Comments, Blocks, Babel Calls, Dynamic Blocks and
3837 ((looking-at "[ \t]*#")
3838 (goto-char (match-end 0))
3839 (cond ((looking-at "\\(?: \\|$\\)")
3841 (org-element-comment-parser limit affiliated
))
3842 ((looking-at "\\+BEGIN_\\(\\S-+\\)")
3844 (let ((parser (assoc (upcase (match-string 1))
3845 org-element-block-name-alist
)))
3846 (if parser
(funcall (cdr parser
) limit affiliated
)
3847 (org-element-special-block-parser limit affiliated
))))
3848 ((looking-at "\\+CALL:")
3850 (org-element-babel-call-parser limit affiliated
))
3851 ((looking-at "\\+BEGIN:? ")
3853 (org-element-dynamic-block-parser limit affiliated
))
3854 ((looking-at "\\+\\S-+:")
3856 (org-element-keyword-parser limit affiliated
))
3859 (org-element-paragraph-parser limit affiliated
))))
3860 ;; Footnote Definition.
3861 ((looking-at org-footnote-definition-re
)
3862 (org-element-footnote-definition-parser limit affiliated
))
3864 ((looking-at "[ \t]*-\\{5,\\}[ \t]*$")
3865 (org-element-horizontal-rule-parser limit affiliated
))
3868 (org-element-diary-sexp-parser limit affiliated
))
3870 ((org-at-table-p t
) (org-element-table-parser limit affiliated
))
3872 ((looking-at (org-item-re))
3873 (org-element-plain-list-parser
3874 limit affiliated
(or structure
(org-list-struct))))
3875 ;; Default element: Paragraph.
3876 (t (org-element-paragraph-parser limit affiliated
)))))))))
3879 ;; Most elements can have affiliated keywords. When looking for an
3880 ;; element beginning, we want to move before them, as they belong to
3881 ;; that element, and, in the meantime, collect information they give
3882 ;; into appropriate properties. Hence the following function.
3884 (defun org-element--collect-affiliated-keywords (limit)
3885 "Collect affiliated keywords from point down to LIMIT.
3887 Return a list whose CAR is the position at the first of them and
3888 CDR a plist of keywords and values and move point to the
3889 beginning of the first line after them.
3891 As a special case, if element doesn't start at the beginning of
3892 the line (i.e. a paragraph starting an item), CAR is current
3893 position of point and CDR is nil."
3894 (if (not (bolp)) (list (point))
3895 (let ((case-fold-search t
)
3897 ;; RESTRICT is the list of objects allowed in parsed
3899 (restrict (org-element-restriction 'keyword
))
3901 (while (and (< (point) limit
) (looking-at org-element--affiliated-re
))
3902 (let* ((raw-kwd (upcase (match-string 1)))
3903 ;; Apply translation to RAW-KWD. From there, KWD is
3904 ;; the official keyword.
3905 (kwd (or (cdr (assoc raw-kwd
3906 org-element-keyword-translation-alist
))
3908 ;; Find main value for any keyword.
3912 (buffer-substring-no-properties
3913 (match-end 0) (point-at-eol)))))
3914 ;; PARSEDP is non-nil when keyword should have its
3916 (parsedp (member kwd org-element-parsed-keywords
))
3917 ;; If KWD is a dual keyword, find its secondary
3918 ;; value. Maybe parse it.
3919 (dualp (member kwd org-element-dual-keywords
))
3922 (let ((sec (org-match-string-no-properties 2)))
3923 (if (or (not sec
) (not parsedp
)) sec
3924 (org-element-parse-secondary-string sec restrict
)))))
3925 ;; Attribute a property name to KWD.
3926 (kwd-sym (and kwd
(intern (concat ":" (downcase kwd
))))))
3927 ;; Now set final shape for VALUE.
3929 (setq value
(org-element-parse-secondary-string value restrict
)))
3931 (setq value
(and (or value dual-value
) (cons value dual-value
))))
3932 (when (or (member kwd org-element-multiple-keywords
)
3933 ;; Attributes can always appear on multiple lines.
3934 (string-match "^ATTR_" kwd
))
3935 (setq value
(cons value
(plist-get output kwd-sym
))))
3936 ;; Eventually store the new value in OUTPUT.
3937 (setq output
(plist-put output kwd-sym value
))
3938 ;; Move to next keyword.
3940 ;; If affiliated keywords are orphaned: move back to first one.
3941 ;; They will be parsed as a paragraph.
3942 (when (looking-at "[ \t]*$") (goto-char origin
) (setq output nil
))
3944 (cons origin output
))))
3950 ;; The two major functions here are `org-element-parse-buffer', which
3951 ;; parses Org syntax inside the current buffer, taking into account
3952 ;; region, narrowing, or even visibility if specified, and
3953 ;; `org-element-parse-secondary-string', which parses objects within
3956 ;; The (almost) almighty `org-element-map' allows to apply a function
3957 ;; on elements or objects matching some type, and accumulate the
3958 ;; resulting values. In an export situation, it also skips unneeded
3959 ;; parts of the parse tree.
3961 (defun org-element-parse-buffer (&optional granularity visible-only
)
3962 "Recursively parse the buffer and return structure.
3963 If narrowing is in effect, only parse the visible part of the
3966 Optional argument GRANULARITY determines the depth of the
3967 recursion. It can be set to the following symbols:
3969 `headline' Only parse headlines.
3970 `greater-element' Don't recurse into greater elements excepted
3971 headlines and sections. Thus, elements
3972 parsed are the top-level ones.
3973 `element' Parse everything but objects and plain text.
3974 `object' Parse the complete buffer (default).
3976 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3979 An element or an objects is represented as a list with the
3980 pattern (TYPE PROPERTIES CONTENTS), where :
3982 TYPE is a symbol describing the element or object. See
3983 `org-element-all-elements' and `org-element-all-objects' for an
3984 exhaustive list of such symbols. One can retrieve it with
3985 `org-element-type' function.
3987 PROPERTIES is the list of attributes attached to the element or
3988 object, as a plist. Although most of them are specific to the
3989 element or object type, all types share `:begin', `:end',
3990 `:post-blank' and `:parent' properties, which respectively
3991 refer to buffer position where the element or object starts,
3992 ends, the number of white spaces or blank lines after it, and
3993 the element or object containing it. Properties values can be
3994 obtained by using `org-element-property' function.
3996 CONTENTS is a list of elements, objects or raw strings
3997 contained in the current element or object, when applicable.
3998 One can access them with `org-element-contents' function.
4000 The Org buffer has `org-data' as type and nil as properties.
4001 `org-element-map' function can be used to find specific elements
4002 or objects within the parse tree.
4004 This function assumes that current major mode is `org-mode'."
4006 (goto-char (point-min))
4007 (org-skip-whitespace)
4008 (org-element--parse-elements
4009 (point-at-bol) (point-max)
4010 ;; Start in `first-section' mode so text before the first
4011 ;; headline belongs to a section.
4012 'first-section nil granularity visible-only
(list 'org-data nil
))))
4014 (defun org-element-parse-secondary-string (string restriction
&optional parent
)
4015 "Recursively parse objects in STRING and return structure.
4017 RESTRICTION is a symbol limiting the object types that will be
4020 Optional argument PARENT, when non-nil, is the element or object
4021 containing the secondary string. It is used to set correctly
4022 `:parent' property within the string."
4023 ;; Copy buffer-local variables listed in
4024 ;; `org-element-object-variables' into temporary buffer. This is
4025 ;; required since object parsing is dependent on these variables.
4026 (let ((pairs (delq nil
(mapcar (lambda (var)
4028 (cons var
(symbol-value var
))))
4029 org-element-object-variables
))))
4031 (mapc (lambda (pair) (org-set-local (car pair
) (cdr pair
))) pairs
)
4033 (let ((secondary (org-element--parse-objects
4034 (point-min) (point-max) nil restriction
)))
4036 (mapc (lambda (obj) (org-element-put-property obj
:parent parent
))
4040 (defun org-element-map
4041 (data types fun
&optional info first-match no-recursion with-affiliated
)
4042 "Map a function on selected elements or objects.
4044 DATA is a parse tree, an element, an object, a string, or a list
4045 of such constructs. TYPES is a symbol or list of symbols of
4046 elements or objects types (see `org-element-all-elements' and
4047 `org-element-all-objects' for a complete list of types). FUN is
4048 the function called on the matching element or object. It has to
4049 accept one argument: the element or object itself.
4051 When optional argument INFO is non-nil, it should be a plist
4052 holding export options. In that case, parts of the parse tree
4053 not exportable according to that property list will be skipped.
4055 When optional argument FIRST-MATCH is non-nil, stop at the first
4056 match for which FUN doesn't return nil, and return that value.
4058 Optional argument NO-RECURSION is a symbol or a list of symbols
4059 representing elements or objects types. `org-element-map' won't
4060 enter any recursive element or object whose type belongs to that
4061 list. Though, FUN can still be applied on them.
4063 When optional argument WITH-AFFILIATED is non-nil, FUN will also
4064 apply to matching objects within parsed affiliated keywords (see
4065 `org-element-parsed-keywords').
4067 Nil values returned from FUN do not appear in the results.
4073 Assuming TREE is a variable containing an Org buffer parse tree,
4074 the following example will return a flat list of all `src-block'
4075 and `example-block' elements in it:
4077 \(org-element-map tree '(example-block src-block) 'identity)
4079 The following snippet will find the first headline with a level
4080 of 1 and a \"phone\" tag, and will return its beginning position:
4082 \(org-element-map tree 'headline
4084 \(and (= (org-element-property :level hl) 1)
4085 \(member \"phone\" (org-element-property :tags hl))
4086 \(org-element-property :begin hl)))
4089 The next example will return a flat list of all `plain-list' type
4090 elements in TREE that are not a sub-list themselves:
4092 \(org-element-map tree 'plain-list 'identity nil nil 'plain-list)
4094 Eventually, this example will return a flat list of all `bold'
4095 type objects containing a `latex-snippet' type object, even
4096 looking into captions:
4098 \(org-element-map tree 'bold
4100 \(and (org-element-map b 'latex-snippet 'identity nil t) b))
4102 ;; Ensure TYPES and NO-RECURSION are a list, even of one element.
4103 (unless (listp types
) (setq types
(list types
)))
4104 (unless (listp no-recursion
) (setq no-recursion
(list no-recursion
)))
4105 ;; Recursion depth is determined by --CATEGORY.
4108 (let ((category 'greater-elements
))
4109 (mapc (lambda (type)
4110 (cond ((or (memq type org-element-all-objects
)
4111 (eq type
'plain-text
))
4112 ;; If one object is found, the function
4113 ;; has to recurse into every object.
4114 (throw 'found
'objects
))
4115 ((not (memq type org-element-greater-elements
))
4116 ;; If one regular element is found, the
4117 ;; function has to recurse, at least,
4118 ;; into every element it encounters.
4119 (and (not (eq category
'elements
))
4120 (setq category
'elements
)))))
4123 ;; Compute properties for affiliated keywords if necessary.
4125 (and with-affiliated
4126 (mapcar (lambda (kwd)
4127 (cons kwd
(intern (concat ":" (downcase kwd
)))))
4128 org-element-affiliated-keywords
)))
4134 ;; Recursively walk DATA. INFO, if non-nil, is a plist
4135 ;; holding contextual information.
4136 (let ((--type (org-element-type --data
)))
4139 ;; Ignored element in an export context.
4140 ((and info
(memq --data
(plist-get info
:ignore-list
))))
4141 ;; List of elements or objects.
4142 ((not --type
) (mapc --walk-tree --data
))
4143 ;; Unconditionally enter parse trees.
4144 ((eq --type
'org-data
)
4145 (mapc --walk-tree
(org-element-contents --data
)))
4147 ;; Check if TYPE is matching among TYPES. If so,
4148 ;; apply FUN to --DATA and accumulate return value
4149 ;; into --ACC (or exit if FIRST-MATCH is non-nil).
4150 (when (memq --type types
)
4151 (let ((result (funcall fun --data
)))
4152 (cond ((not result
))
4153 (first-match (throw '--map-first-match result
))
4154 (t (push result --acc
)))))
4155 ;; If --DATA has a secondary string that can contain
4156 ;; objects with their type among TYPES, look into it.
4157 (when (and (eq --category
'objects
) (not (stringp --data
)))
4159 (assq --type org-element-secondary-value-alist
)))
4161 (funcall --walk-tree
4162 (org-element-property (cdr sec-prop
) --data
)))))
4163 ;; If --DATA has any affiliated keywords and
4164 ;; WITH-AFFILIATED is non-nil, look for objects in
4166 (when (and with-affiliated
4167 (eq --category
'objects
)
4168 (memq --type org-element-all-elements
))
4169 (mapc (lambda (kwd-pair)
4170 (let ((kwd (car kwd-pair
))
4171 (value (org-element-property
4172 (cdr kwd-pair
) --data
)))
4173 ;; Pay attention to the type of value.
4174 ;; Preserve order for multiple keywords.
4177 ((and (member kwd org-element-multiple-keywords
)
4178 (member kwd org-element-dual-keywords
))
4179 (mapc (lambda (line)
4180 (funcall --walk-tree
(cdr line
))
4181 (funcall --walk-tree
(car line
)))
4183 ((member kwd org-element-multiple-keywords
)
4184 (mapc (lambda (line) (funcall --walk-tree line
))
4186 ((member kwd org-element-dual-keywords
)
4187 (funcall --walk-tree
(cdr value
))
4188 (funcall --walk-tree
(car value
)))
4189 (t (funcall --walk-tree value
)))))
4190 --affiliated-alist
))
4191 ;; Determine if a recursion into --DATA is possible.
4193 ;; --TYPE is explicitly removed from recursion.
4194 ((memq --type no-recursion
))
4195 ;; --DATA has no contents.
4196 ((not (org-element-contents --data
)))
4197 ;; Looking for greater elements but --DATA is simply
4198 ;; an element or an object.
4199 ((and (eq --category
'greater-elements
)
4200 (not (memq --type org-element-greater-elements
))))
4201 ;; Looking for elements but --DATA is an object.
4202 ((and (eq --category
'elements
)
4203 (memq --type org-element-all-objects
)))
4204 ;; In any other case, map contents.
4205 (t (mapc --walk-tree
(org-element-contents --data
)))))))))))
4206 (catch '--map-first-match
4207 (funcall --walk-tree data
)
4208 ;; Return value in a proper order.
4210 (put 'org-element-map
'lisp-indent-function
2)
4212 ;; The following functions are internal parts of the parser.
4214 ;; The first one, `org-element--parse-elements' acts at the element's
4217 ;; The second one, `org-element--parse-objects' applies on all objects
4218 ;; of a paragraph or a secondary string. It uses
4219 ;; `org-element--get-next-object-candidates' to optimize the search of
4220 ;; the next object in the buffer.
4222 ;; More precisely, that function looks for every allowed object type
4223 ;; first. Then, it discards failed searches, keeps further matches,
4224 ;; and searches again types matched behind point, for subsequent
4225 ;; calls. Thus, searching for a given type fails only once, and every
4226 ;; object is searched only once at top level (but sometimes more for
4229 (defun org-element--parse-elements
4230 (beg end special structure granularity visible-only acc
)
4231 "Parse elements between BEG and END positions.
4233 SPECIAL prioritize some elements over the others. It can be set
4234 to `first-section', `quote-section', `section' `item' or
4237 When value is `item', STRUCTURE will be used as the current list
4240 GRANULARITY determines the depth of the recursion. See
4241 `org-element-parse-buffer' for more information.
4243 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
4246 Elements are accumulated into ACC."
4249 ;; Visible only: skip invisible parts at the beginning of the
4251 (when (and visible-only
(org-invisible-p2))
4252 (goto-char (min (1+ (org-find-visible)) end
)))
4253 ;; When parsing only headlines, skip any text before first one.
4254 (when (and (eq granularity
'headline
) (not (org-at-heading-p)))
4255 (org-with-limited-levels (outline-next-heading)))
4257 (while (< (point) end
)
4258 ;; Find current element's type and parse it accordingly to
4260 (let* ((element (org-element--current-element
4261 end granularity special structure
))
4262 (type (org-element-type element
))
4263 (cbeg (org-element-property :contents-begin element
)))
4264 (goto-char (org-element-property :end element
))
4265 ;; Visible only: skip invisible parts between siblings.
4266 (when (and visible-only
(org-invisible-p2))
4267 (goto-char (min (1+ (org-find-visible)) end
)))
4268 ;; Fill ELEMENT contents by side-effect.
4270 ;; If element has no contents, don't modify it.
4272 ;; Greater element: parse it between `contents-begin' and
4273 ;; `contents-end'. Make sure GRANULARITY allows the
4274 ;; recursion, or ELEMENT is a headline, in which case going
4275 ;; inside is mandatory, in order to get sub-level headings.
4276 ((and (memq type org-element-greater-elements
)
4277 (or (memq granularity
'(element object nil
))
4278 (and (eq granularity
'greater-element
)
4280 (eq type
'headline
)))
4281 (org-element--parse-elements
4282 cbeg
(org-element-property :contents-end element
)
4283 ;; Possibly switch to a special mode.
4286 (if (org-element-property :quotedp element
) 'quote-section
4289 (property-drawer 'node-property
)
4291 (and (memq type
'(item plain-list
))
4292 (org-element-property :structure element
))
4293 granularity visible-only element
))
4294 ;; ELEMENT has contents. Parse objects inside, if
4295 ;; GRANULARITY allows it.
4296 ((memq granularity
'(object nil
))
4297 (org-element--parse-objects
4298 cbeg
(org-element-property :contents-end element
) element
4299 (org-element-restriction type
))))
4300 (org-element-adopt-elements acc element
)))
4304 (defun org-element--parse-objects (beg end acc restriction
)
4305 "Parse objects between BEG and END and return recursive structure.
4307 Objects are accumulated in ACC.
4309 RESTRICTION is a list of object successors which are allowed in
4310 the current object."
4311 (let ((candidates 'initial
))
4314 (while (and (< (point) end
)
4315 (setq candidates
(org-element--get-next-object-candidates
4316 end restriction candidates
)))
4318 (let ((pos (apply 'min
(mapcar 'cdr candidates
))))
4321 (funcall (intern (format "org-element-%s-parser"
4322 (car (rassq pos candidates
)))))))))
4323 ;; 1. Text before any object. Untabify it.
4324 (let ((obj-beg (org-element-property :begin next-object
)))
4325 (unless (= (point) obj-beg
)
4327 (org-element-adopt-elements
4329 (replace-regexp-in-string
4330 "\t" (make-string tab-width ?
)
4331 (buffer-substring-no-properties (point) obj-beg
))))))
4333 (let ((obj-end (org-element-property :end next-object
))
4334 (cont-beg (org-element-property :contents-begin next-object
)))
4335 ;; Fill contents of NEXT-OBJECT by side-effect, if it has
4336 ;; a recursive type.
4338 (memq (car next-object
) org-element-recursive-objects
))
4342 (org-element-property :contents-end next-object
))
4343 (org-element--parse-objects
4344 (point-min) (point-max) next-object
4345 (org-element-restriction next-object
))))
4346 (setq acc
(org-element-adopt-elements acc next-object
))
4347 (goto-char obj-end
))))
4348 ;; 3. Text after last object. Untabify it.
4349 (unless (= (point) end
)
4351 (org-element-adopt-elements
4353 (replace-regexp-in-string
4354 "\t" (make-string tab-width ?
)
4355 (buffer-substring-no-properties (point) end
)))))
4359 (defun org-element--get-next-object-candidates (limit restriction objects
)
4360 "Return an alist of candidates for the next object.
4362 LIMIT bounds the search, and RESTRICTION narrows candidates to
4363 some object successors.
4365 OBJECTS is the previous candidates alist. If it is set to
4366 `initial', no search has been done before, and all symbols in
4367 RESTRICTION should be looked after.
4369 Return value is an alist whose CAR is the object type and CDR its
4370 beginning position."
4373 (if (eq objects
'initial
)
4374 ;; When searching for the first time, look for every successor
4375 ;; allowed in RESTRICTION.
4378 (funcall (intern (format "org-element-%s-successor" res
)) limit
))
4380 ;; Focus on objects returned during last search. Keep those
4381 ;; still after point. Search again objects before it.
4384 (if (>= (cdr obj
) (point)) obj
4385 (let* ((type (car obj
))
4386 (succ (or (cdr (assq type org-element-object-successor-alist
))
4389 (funcall (intern (format "org-element-%s-successor" succ
))
4395 ;;; Towards A Bijective Process
4397 ;; The parse tree obtained with `org-element-parse-buffer' is really
4398 ;; a snapshot of the corresponding Org buffer. Therefore, it can be
4399 ;; interpreted and expanded into a string with canonical Org syntax.
4400 ;; Hence `org-element-interpret-data'.
4402 ;; The function relies internally on
4403 ;; `org-element--interpret-affiliated-keywords'.
4406 (defun org-element-interpret-data (data &optional parent
)
4407 "Interpret DATA as Org syntax.
4409 DATA is a parse tree, an element, an object or a secondary string
4412 Optional argument PARENT is used for recursive calls. It contains
4413 the element or object containing data, or nil.
4415 Return Org syntax as a string."
4416 (let* ((type (org-element-type data
))
4419 ;; Secondary string.
4422 (lambda (obj) (org-element-interpret-data obj parent
))
4424 ;; Full Org document.
4425 ((eq type
'org-data
)
4427 (lambda (obj) (org-element-interpret-data obj parent
))
4428 (org-element-contents data
) ""))
4429 ;; Plain text: remove `:parent' text property from output.
4430 ((stringp data
) (org-no-properties data
))
4431 ;; Element/Object without contents.
4432 ((not (org-element-contents data
))
4433 (funcall (intern (format "org-element-%s-interpreter" type
))
4435 ;; Element/Object with contents.
4437 (let* ((greaterp (memq type org-element-greater-elements
))
4438 (objectp (and (not greaterp
)
4439 (memq type org-element-recursive-objects
)))
4442 (lambda (obj) (org-element-interpret-data obj data
))
4443 (org-element-contents
4444 (if (or greaterp objectp
) data
4445 ;; Elements directly containing objects must
4446 ;; have their indentation normalized first.
4447 (org-element-normalize-contents
4449 ;; When normalizing first paragraph of an
4450 ;; item or a footnote-definition, ignore
4451 ;; first line's indentation.
4452 (and (eq type
'paragraph
)
4453 (equal data
(car (org-element-contents parent
)))
4454 (memq (org-element-type parent
)
4455 '(footnote-definition item
))))))
4457 (funcall (intern (format "org-element-%s-interpreter" type
))
4459 (if greaterp
(org-element-normalize-contents contents
)
4461 (if (memq type
'(org-data plain-text nil
)) results
4462 ;; Build white spaces. If no `:post-blank' property is
4463 ;; specified, assume its value is 0.
4464 (let ((post-blank (or (org-element-property :post-blank data
) 0)))
4465 (if (memq type org-element-all-objects
)
4466 (concat results
(make-string post-blank
32))
4468 (org-element--interpret-affiliated-keywords data
)
4469 (org-element-normalize-string results
)
4470 (make-string post-blank
10)))))))
4472 (defun org-element--interpret-affiliated-keywords (element)
4473 "Return ELEMENT's affiliated keywords as Org syntax.
4474 If there is no affiliated keyword, return the empty string."
4475 (let ((keyword-to-org
4479 (when (member key org-element-dual-keywords
)
4480 (setq dual
(cdr value
) value
(car value
)))
4483 (format "[%s]" (org-element-interpret-data dual
)))
4485 (if (member key org-element-parsed-keywords
)
4486 (org-element-interpret-data value
)
4491 (let ((value (org-element-property prop element
))
4492 (keyword (upcase (substring (symbol-name prop
) 1))))
4494 (if (or (member keyword org-element-multiple-keywords
)
4495 ;; All attribute keywords can have multiple lines.
4496 (string-match "^ATTR_" keyword
))
4497 (mapconcat (lambda (line) (funcall keyword-to-org keyword line
))
4500 (funcall keyword-to-org keyword value
)))))
4501 ;; List all ELEMENT's properties matching an attribute line or an
4502 ;; affiliated keyword, but ignore translated keywords since they
4503 ;; cannot belong to the property list.
4504 (loop for prop in
(nth 1 element
) by
'cddr
4505 when
(let ((keyword (upcase (substring (symbol-name prop
) 1))))
4506 (or (string-match "^ATTR_" keyword
)
4508 (member keyword org-element-affiliated-keywords
)
4510 org-element-keyword-translation-alist
)))))
4514 ;; Because interpretation of the parse tree must return the same
4515 ;; number of blank lines between elements and the same number of white
4516 ;; space after objects, some special care must be given to white
4519 ;; The first function, `org-element-normalize-string', ensures any
4520 ;; string different from the empty string will end with a single
4521 ;; newline character.
4523 ;; The second function, `org-element-normalize-contents', removes
4524 ;; global indentation from the contents of the current element.
4526 (defun org-element-normalize-string (s)
4527 "Ensure string S ends with a single newline character.
4529 If S isn't a string return it unchanged. If S is the empty
4530 string, return it. Otherwise, return a new string with a single
4531 newline character at its end."
4533 ((not (stringp s
)) s
)
4535 (t (and (string-match "\\(\n[ \t]*\\)*\\'" s
)
4536 (replace-match "\n" nil nil s
)))))
4538 (defun org-element-normalize-contents (element &optional ignore-first
)
4539 "Normalize plain text in ELEMENT's contents.
4541 ELEMENT must only contain plain text and objects.
4543 If optional argument IGNORE-FIRST is non-nil, ignore first line's
4544 indentation to compute maximal common indentation.
4546 Return the normalized element that is element with global
4547 indentation removed from its contents. The function assumes that
4548 indentation is not done with TAB characters."
4549 (let* (ind-list ; for byte-compiler
4550 collect-inds
; for byte-compiler
4553 ;; Return list of indentations within BLOB. This is done by
4554 ;; walking recursively BLOB and updating IND-LIST along the
4555 ;; way. FIRST-FLAG is non-nil when the first string hasn't
4556 ;; been seen yet. It is required as this string is the only
4557 ;; one whose indentation doesn't happen after a newline
4559 (lambda (blob first-flag
)
4562 (when (and first-flag
(stringp object
))
4563 (setq first-flag nil
)
4564 (string-match "\\`\\( *\\)" object
)
4565 (let ((len (length (match-string 1 object
))))
4566 ;; An indentation of zero means no string will be
4567 ;; modified. Quit the process.
4568 (if (zerop len
) (throw 'zero
(setq ind-list nil
))
4569 (push len ind-list
))))
4573 ;; Avoid matching blank or empty lines.
4574 (while (and (string-match "\n\\( *\\)\\(.\\)" object start
)
4575 (not (equal (match-string 2 object
) " ")))
4576 (setq start
(match-end 0))
4577 (push (length (match-string 1 object
)) ind-list
))))
4578 ((memq (org-element-type object
) org-element-recursive-objects
)
4579 (funcall collect-inds object first-flag
))))
4580 (org-element-contents blob
))))))
4581 ;; Collect indentation list in ELEMENT. Possibly remove first
4582 ;; value if IGNORE-FIRST is non-nil.
4583 (catch 'zero
(funcall collect-inds element
(not ignore-first
)))
4584 (if (not ind-list
) element
4585 ;; Build ELEMENT back, replacing each string with the same
4586 ;; string minus common indentation.
4587 (let* (build ; For byte compiler.
4590 (lambda (blob mci first-flag
)
4591 ;; Return BLOB with all its strings indentation
4592 ;; shortened from MCI white spaces. FIRST-FLAG is
4593 ;; non-nil when the first string hasn't been seen
4598 (when (and first-flag
(stringp object
))
4599 (setq first-flag nil
)
4601 (replace-regexp-in-string
4602 (format "\\` \\{%d\\}" mci
) "" object
)))
4605 (replace-regexp-in-string
4606 (format "\n \\{%d\\}" mci
) "\n" object
))
4607 ((memq (org-element-type object
)
4608 org-element-recursive-objects
)
4609 (funcall build object mci first-flag
))
4611 (org-element-contents blob
)))
4613 (funcall build element
(apply 'min ind-list
) (not ignore-first
))))))
4619 ;; The first move is to implement a way to obtain the smallest element
4620 ;; containing point. This is the job of `org-element-at-point'. It
4621 ;; basically jumps back to the beginning of section containing point
4622 ;; and moves, element after element, with
4623 ;; `org-element--current-element' until the container is found. Note:
4624 ;; When using `org-element-at-point', secondary values are never
4625 ;; parsed since the function focuses on elements, not on objects.
4627 ;; At a deeper level, `org-element-context' lists all elements and
4628 ;; objects containing point.
4630 ;; `org-element-nested-p' and `org-element-swap-A-B' may be used
4631 ;; internally by navigation and manipulation tools.
4634 (defun org-element-at-point (&optional keep-trail
)
4635 "Determine closest element around point.
4637 Return value is a list like (TYPE PROPS) where TYPE is the type
4638 of the element and PROPS a plist of properties associated to the
4641 Possible types are defined in `org-element-all-elements'.
4642 Properties depend on element or object type, but always include
4643 `:begin', `:end', `:parent' and `:post-blank' properties.
4645 As a special case, if point is at the very beginning of a list or
4646 sub-list, returned element will be that list instead of the first
4647 item. In the same way, if point is at the beginning of the first
4648 row of a table, returned element will be the table instead of the
4651 If optional argument KEEP-TRAIL is non-nil, the function returns
4652 a list of elements leading to element at point. The list's CAR
4653 is always the element at point. The following positions contain
4654 element's siblings, then parents, siblings of parents, until the
4655 first element of current section."
4656 (org-with-wide-buffer
4657 ;; If at a headline, parse it. It is the sole element that
4658 ;; doesn't require to know about context. Be sure to disallow
4659 ;; secondary string parsing, though.
4660 (if (org-with-limited-levels (org-at-heading-p))
4663 (if (not keep-trail
) (org-element-headline-parser (point-max) t
)
4664 (list (org-element-headline-parser (point-max) t
))))
4665 ;; Otherwise move at the beginning of the section containing
4668 (let ((origin (point))
4669 (end (save-excursion
4670 (org-with-limited-levels (outline-next-heading)) (point)))
4671 element type special-flag trail struct prevs parent
)
4672 (org-with-limited-levels
4673 (if (org-before-first-heading-p)
4674 ;; In empty lines at buffer's beginning, return nil.
4675 (progn (goto-char (point-min))
4676 (org-skip-whitespace)
4677 (when (or (eobp) (> (line-beginning-position) origin
))
4679 (org-back-to-heading)
4681 (org-skip-whitespace)
4682 (when (> (line-beginning-position) origin
)
4683 ;; In blank lines just after the headline, point still
4684 ;; belongs to the headline.
4686 (progn (org-back-to-heading)
4687 (if (not keep-trail
)
4688 (org-element-headline-parser (point-max) t
)
4689 (list (org-element-headline-parser
4690 (point-max) t
))))))))
4692 ;; Parse successively each element, skipping those ending
4693 ;; before original position.
4696 (org-element--current-element end
'element special-flag struct
)
4698 (org-element-put-property element
:parent parent
)
4699 (when keep-trail
(push element trail
))
4701 ;; 1. Skip any element ending before point. Also skip
4702 ;; element ending at point when we're sure that another
4703 ;; element has started.
4704 ((let ((elem-end (org-element-property :end element
)))
4705 (when (or (< elem-end origin
)
4706 (and (= elem-end origin
) (/= elem-end end
)))
4707 (goto-char elem-end
))))
4708 ;; 2. An element containing point is always the element at
4710 ((not (memq type org-element-greater-elements
))
4711 (throw 'exit
(if keep-trail trail element
)))
4712 ;; 3. At any other greater element type, if point is
4713 ;; within contents, move into it.
4715 (let ((cbeg (org-element-property :contents-begin element
))
4716 (cend (org-element-property :contents-end element
)))
4717 (if (or (not cbeg
) (not cend
) (> cbeg origin
) (< cend origin
)
4718 ;; Create an anchor for tables and plain lists:
4719 ;; when point is at the very beginning of these
4720 ;; elements, ignoring affiliated keywords,
4721 ;; target them instead of their contents.
4722 (and (= cbeg origin
) (memq type
'(plain-list table
)))
4723 ;; When point is at contents end, do not move
4724 ;; into elements with an explicit ending, but
4725 ;; return that element instead.
4726 (and (= cend origin
)
4729 drawer dynamic-block inlinetask item
4730 plain-list property-drawer quote-block
4732 (throw 'exit
(if keep-trail trail element
))
4733 (setq parent element
)
4736 (setq special-flag
'item
4737 struct
(org-element-property :structure element
)))
4738 (item (setq special-flag nil
))
4740 (setq special-flag
'node-property struct nil
))
4741 (table (setq special-flag
'table-row struct nil
))
4742 (otherwise (setq special-flag nil struct nil
)))
4744 (goto-char cbeg
)))))))))))
4747 (defun org-element-context (&optional element
)
4748 "Return closest element or object around point.
4750 Return value is a list like (TYPE PROPS) where TYPE is the type
4751 of the element or object and PROPS a plist of properties
4754 Possible types are defined in `org-element-all-elements' and
4755 `org-element-all-objects'. Properties depend on element or
4756 object type, but always include `:begin', `:end', `:parent' and
4759 Optional argument ELEMENT, when non-nil, is the closest element
4760 containing point, as returned by `org-element-at-point'.
4761 Providing it allows for quicker computation."
4762 (org-with-wide-buffer
4763 (let* ((origin (point))
4764 (element (or element
(org-element-at-point)))
4765 (type (org-element-type element
))
4767 ;; Check if point is inside an element containing objects or at
4768 ;; a secondary string. In that case, move to beginning of the
4769 ;; element or secondary string and set END to the other side.
4770 (if (not (or (let ((post (org-element-property :post-affiliated element
)))
4771 (and post
(> post origin
)
4772 (< (org-element-property :begin element
) origin
)
4773 (progn (beginning-of-line)
4774 (looking-at org-element--affiliated-re
)
4775 (member (upcase (match-string 1))
4776 org-element-parsed-keywords
))
4777 ;; We're at an affiliated keyword. Change
4778 ;; type to retrieve correct restrictions.
4779 (setq type
'keyword
)
4780 ;; Determine if we're at main or dual value.
4781 (if (and (match-end 2) (<= origin
(match-end 2)))
4782 (progn (goto-char (match-beginning 2))
4783 (setq end
(match-end 2)))
4784 (goto-char (match-end 0))
4785 (setq end
(line-end-position)))))
4786 (and (eq type
'item
)
4787 (let ((tag (org-element-property :tag element
)))
4791 (search-forward tag
(point-at-eol))
4792 (goto-char (match-beginning 0))
4793 (and (>= origin
(point))
4795 ;; `1+' is required so some
4796 ;; successors can match
4797 ;; properly their object.
4798 (setq end
(1+ (match-end 0)))))))))
4799 (and (memq type
'(headline inlinetask
))
4800 (progn (beginning-of-line)
4801 (skip-chars-forward "* ")
4802 (setq end
(point-at-eol))))
4803 (and (memq type
'(paragraph table-row verse-block
))
4804 (let ((cbeg (org-element-property
4805 :contents-begin element
))
4806 (cend (org-element-property
4807 :contents-end element
)))
4808 (and cbeg cend
; cbeg is nil for table rules
4811 (progn (goto-char cbeg
) (setq end cend
)))))
4812 (and (eq type
'keyword
)
4813 (let ((key (org-element-property :key element
)))
4814 (and (member key org-element-document-properties
)
4815 (progn (beginning-of-line)
4816 (search-forward key
(line-end-position) t
)
4818 (setq end
(line-end-position))))))))
4820 (let ((restriction (org-element-restriction type
))
4822 (candidates 'initial
))
4824 (while (setq candidates
(org-element--get-next-object-candidates
4825 end restriction candidates
))
4826 (let ((closest-cand (rassq (apply 'min
(mapcar 'cdr candidates
))
4828 ;; If ORIGIN is before next object in element, there's
4829 ;; no point in looking further.
4830 (if (> (cdr closest-cand
) origin
) (throw 'exit parent
)
4832 (progn (goto-char (cdr closest-cand
))
4833 (funcall (intern (format "org-element-%s-parser"
4834 (car closest-cand
))))))
4835 (cbeg (org-element-property :contents-begin object
))
4836 (cend (org-element-property :contents-end object
))
4837 (obj-end (org-element-property :end object
)))
4839 ;; ORIGIN is after OBJECT, so skip it.
4840 ((<= obj-end origin
)
4841 (if (/= obj-end end
) (goto-char obj-end
)
4843 (org-element-put-property
4844 object
:parent parent
))))
4845 ;; ORIGIN is within a non-recursive object or at
4846 ;; an object boundaries: Return that object.
4847 ((or (not cbeg
) (> cbeg origin
) (< cend origin
))
4849 (org-element-put-property object
:parent parent
)))
4850 ;; Otherwise, move within current object and
4851 ;; restrict search to the end of its contents.
4853 (org-element-put-property object
:parent parent
)
4855 restriction
(org-element-restriction object
)
4860 (defun org-element-nested-p (elem-A elem-B
)
4861 "Non-nil when elements ELEM-A and ELEM-B are nested."
4862 (let ((beg-A (org-element-property :begin elem-A
))
4863 (beg-B (org-element-property :begin elem-B
))
4864 (end-A (org-element-property :end elem-A
))
4865 (end-B (org-element-property :end elem-B
)))
4866 (or (and (>= beg-A beg-B
) (<= end-A end-B
))
4867 (and (>= beg-B beg-A
) (<= end-B end-A
)))))
4869 (defun org-element-swap-A-B (elem-A elem-B
)
4870 "Swap elements ELEM-A and ELEM-B.
4871 Assume ELEM-B is after ELEM-A in the buffer. Leave point at the
4873 (goto-char (org-element-property :begin elem-A
))
4874 ;; There are two special cases when an element doesn't start at bol:
4875 ;; the first paragraph in an item or in a footnote definition.
4876 (let ((specialp (not (bolp))))
4877 ;; Only a paragraph without any affiliated keyword can be moved at
4878 ;; ELEM-A position in such a situation. Note that the case of
4879 ;; a footnote definition is impossible: it cannot contain two
4880 ;; paragraphs in a row because it cannot contain a blank line.
4882 (or (not (eq (org-element-type elem-B
) 'paragraph
))
4883 (/= (org-element-property :begin elem-B
)
4884 (org-element-property :contents-begin elem-B
))))
4885 (error "Cannot swap elements"))
4886 ;; In a special situation, ELEM-A will have no indentation. We'll
4887 ;; give it ELEM-B's (which will in, in turn, have no indentation).
4888 (let* ((ind-B (when specialp
4889 (goto-char (org-element-property :begin elem-B
))
4890 (org-get-indentation)))
4891 (beg-A (org-element-property :begin elem-A
))
4892 (end-A (save-excursion
4893 (goto-char (org-element-property :end elem-A
))
4894 (skip-chars-backward " \r\t\n")
4896 (beg-B (org-element-property :begin elem-B
))
4897 (end-B (save-excursion
4898 (goto-char (org-element-property :end elem-B
))
4899 (skip-chars-backward " \r\t\n")
4901 ;; Store overlays responsible for visibility status. We
4902 ;; also need to store their boundaries as they will be
4903 ;; removed from buffer.
4906 (mapcar (lambda (ov) (list ov
(overlay-start ov
) (overlay-end ov
)))
4907 (overlays-in beg-A end-A
))
4908 (mapcar (lambda (ov) (list ov
(overlay-start ov
) (overlay-end ov
)))
4909 (overlays-in beg-B end-B
))))
4911 (body-A (buffer-substring beg-A end-A
))
4912 (body-B (delete-and-extract-region beg-B end-B
)))
4915 (setq body-B
(replace-regexp-in-string "\\`[ \t]*" "" body-B
))
4916 (org-indent-to-column ind-B
))
4918 ;; Restore ex ELEM-A overlays.
4919 (let ((offset (- beg-B beg-A
)))
4922 (car ov
) (+ (nth 1 ov
) offset
) (+ (nth 2 ov
) offset
)))
4925 (delete-region beg-A end-A
)
4927 ;; Restore ex ELEM-B overlays.
4930 (car ov
) (- (nth 1 ov
) offset
) (- (nth 2 ov
) offset
)))
4932 (goto-char (org-element-property :end elem-B
)))))
4934 (provide 'org-element
)
4937 ;; generated-autoload-file: "org-loaddefs.el"
4940 ;;; org-element.el ends here