1 ;;; org-element.el --- Parser And Applications for Org syntax
3 ;; Copyright (C) 2012 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; This file is not part of GNU Emacs.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with this program. 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 (namely `babel-call', `clock', `headline', `item',
34 ;; `keyword', `planning', `property-drawer' and `section' types), it
35 ;; can also accept a fixed set of keywords as attributes. Those are
36 ;; called "affiliated keywords" to distinguish them from other
37 ;; keywords, which are full-fledged elements. Almost all affiliated
38 ;; keywords are referenced in `org-element-affiliated-keywords'; the
39 ;; others are export attributes and start with "ATTR_" prefix.
41 ;; Element containing other elements (and only elements) are called
42 ;; greater elements. Concerned types are: `center-block', `drawer',
43 ;; `dynamic-block', `footnote-definition', `headline', `inlinetask',
44 ;; `item', `plain-list', `quote-block', `section' and `special-block'.
46 ;; Other element types are: `babel-call', `clock', `comment',
47 ;; `comment-block', `example-block', `export-block', `fixed-width',
48 ;; `horizontal-rule', `keyword', `latex-environment', `paragraph',
49 ;; `planning', `property-drawer', `quote-section', `src-block',
50 ;; `table', `table-row' and `verse-block'. Among them, `paragraph'
51 ;; and `verse-block' types can contain Org objects and plain text.
53 ;; Objects are related to document's contents. Some of them are
54 ;; recursive. Associated types are of the following: `bold', `code',
55 ;; `entity', `export-snippet', `footnote-reference',
56 ;; `inline-babel-call', `inline-src-block', `italic',
57 ;; `latex-fragment', `line-break', `link', `macro', `radio-target',
58 ;; `statistics-cookie', `strike-through', `subscript', `superscript',
59 ;; `table-cell', `target', `timestamp', `underline' and `verbatim'.
61 ;; Some elements also have special properties whose value can hold
62 ;; objects themselves (i.e. an item tag or an headline name). Such
63 ;; values are called "secondary strings". Any object belongs to
64 ;; either an element or a secondary string.
66 ;; Notwithstanding affiliated keywords, each greater element, element
67 ;; and object has a fixed set of properties attached to it. Among
68 ;; them, four are shared by all types: `:begin' and `:end', which
69 ;; refer to the beginning and ending buffer positions of the
70 ;; considered element or object, `:post-blank', which holds the number
71 ;; of blank lines, or white spaces, at its end and `:parent' which
72 ;; refers to the element or object containing it. Greater elements
73 ;; and elements containing objects will also have `:contents-begin'
74 ;; and `:contents-end' properties to delimit contents.
76 ;; Lisp-wise, an element or an object can be represented as a list.
77 ;; It follows the pattern (TYPE PROPERTIES CONTENTS), where:
78 ;; TYPE is a symbol describing the Org element or object.
79 ;; PROPERTIES is the property list attached to it. See docstring of
80 ;; appropriate parsing function to get an exhaustive
82 ;; CONTENTS is a list of elements, objects or raw strings contained
83 ;; in the current element or object, when applicable.
85 ;; An Org buffer is a nested list of such elements and objects, whose
86 ;; type is `org-data' and properties is nil.
88 ;; The first part of this file defines Org syntax, while the second
89 ;; one provide accessors and setters functions.
91 ;; The next part implements a parser and an interpreter for each
92 ;; element and object type in Org syntax.
94 ;; The following part creates a fully recursive buffer parser. It
95 ;; also provides a tool to map a function to elements or objects
96 ;; matching some criteria in the parse tree. Functions of interest
97 ;; are `org-element-parse-buffer', `org-element-map' and, to a lesser
98 ;; extent, `org-element-parse-secondary-string'.
100 ;; The penultimate part is the cradle of an interpreter for the
101 ;; obtained parse tree: `org-element-interpret-data'.
103 ;; The library ends by furnishing `org-element-at-point' function, and
104 ;; a way to give information about document structure around point
105 ;; with `org-element-context'.
116 ;;; Definitions And Rules
118 ;; Define elements, greater elements and specify recursive objects,
119 ;; along with the affiliated keywords recognized. Also set up
120 ;; restrictions on recursive objects combinations.
122 ;; These variables really act as a control center for the parsing
125 (defconst org-element-paragraph-separate
126 (concat "^[ \t]*$" "\\|"
127 ;; Headlines and inlinetasks.
128 org-outline-regexp-bol
"\\|"
129 ;; Comments, blocks (any type), keywords and babel calls.
130 "^[ \t]*#\\+" "\\|" "^#\\(?: \\|$\\)" "\\|"
132 (org-item-beginning-re) "\\|"
133 ;; Fixed-width, drawers (any type) and tables.
135 ;; Footnote definitions.
136 org-footnote-definition-re
"\\|"
138 "^[ \t]*-\\{5,\\}[ \t]*$" "\\|"
139 ;; LaTeX environments.
140 "^[ \t]*\\\\\\(begin\\|end\\)" "\\|"
141 ;; Planning and Clock lines.
142 org-planning-or-clock-line-re
)
143 "Regexp to separate paragraphs in an Org buffer.")
145 (defconst org-element-all-elements
146 '(center-block clock comment comment-block drawer dynamic-block example-block
147 export-block fixed-width footnote-definition headline
148 horizontal-rule inlinetask item keyword latex-environment
149 babel-call paragraph plain-list planning property-drawer
150 quote-block quote-section section special-block src-block table
151 table-row verse-block
)
152 "Complete list of element types.")
154 (defconst org-element-greater-elements
155 '(center-block drawer dynamic-block footnote-definition headline inlinetask
156 item plain-list quote-block section special-block table
)
157 "List of recursive element types aka Greater Elements.")
159 (defconst org-element-all-successors
160 '(export-snippet footnote-reference inline-babel-call inline-src-block
161 latex-or-entity line-break link macro radio-target
162 statistics-cookie sub
/superscript table-cell target
163 text-markup timestamp
)
164 "Complete list of successors.")
166 (defconst org-element-object-successor-alist
167 '((subscript . sub
/superscript
) (superscript . sub
/superscript
)
168 (bold . text-markup
) (code . text-markup
) (italic . text-markup
)
169 (strike-through . text-markup
) (underline . text-markup
)
170 (verbatim . text-markup
) (entity . latex-or-entity
)
171 (latex-fragment . latex-or-entity
))
172 "Alist of translations between object type and successor name.
174 Sharing the same successor comes handy when, for example, the
175 regexp matching one object can also match the other object.")
177 (defconst org-element-all-objects
178 '(bold code entity export-snippet footnote-reference inline-babel-call
179 inline-src-block italic line-break latex-fragment link macro
180 radio-target statistics-cookie strike-through subscript superscript
181 table-cell target timestamp underline verbatim
)
182 "Complete list of object types.")
184 (defconst org-element-recursive-objects
185 '(bold italic link macro subscript radio-target strike-through superscript
186 table-cell underline
)
187 "List of recursive object types.")
189 (defconst org-element-block-name-alist
190 '(("CENTER" . org-element-center-block-parser
)
191 ("COMMENT" . org-element-comment-block-parser
)
192 ("EXAMPLE" . org-element-example-block-parser
)
193 ("QUOTE" . org-element-quote-block-parser
)
194 ("SRC" . org-element-src-block-parser
)
195 ("VERSE" . org-element-verse-block-parser
))
196 "Alist between block names and the associated parsing function.
197 Names must be uppercase. Any block whose name has no association
198 is parsed with `org-element-special-block-parser'.")
200 (defconst org-element-affiliated-keywords
201 '("CAPTION" "DATA" "HEADER" "HEADERS" "LABEL" "NAME" "PLOT" "RESNAME" "RESULT"
202 "RESULTS" "SOURCE" "SRCNAME" "TBLNAME")
203 "List of affiliated keywords as strings.
204 By default, all keywords setting attributes (i.e. \"ATTR_LATEX\")
205 are affiliated keywords and need not to be in this list.")
207 (defconst org-element--affiliated-re
208 (format "[ \t]*#\\+%s:"
209 ;; Regular affiliated keywords.
210 (format "\\(%s\\|ATTR_[-_A-Za-z0-9]+\\)\\(?:\\[\\(.*\\)\\]\\)?"
211 (regexp-opt org-element-affiliated-keywords
)))
212 "Regexp matching any affiliated keyword.
214 Keyword name is put in match group 1. Moreover, if keyword
215 belongs to `org-element-dual-keywords', put the dual value in
218 Don't modify it, set `org-element-affiliated-keywords' instead.")
220 (defconst org-element-keyword-translation-alist
221 '(("DATA" .
"NAME") ("LABEL" .
"NAME") ("RESNAME" .
"NAME")
222 ("SOURCE" .
"NAME") ("SRCNAME" .
"NAME") ("TBLNAME" .
"NAME")
223 ("RESULT" .
"RESULTS") ("HEADERS" .
"HEADER"))
224 "Alist of usual translations for keywords.
225 The key is the old name and the value the new one. The property
226 holding their value will be named after the translated name.")
228 (defconst org-element-multiple-keywords
'("HEADER")
229 "List of affiliated keywords that can occur more that once in an element.
231 Their value will be consed into a list of strings, which will be
232 returned as the value of the property.
234 This list is checked after translations have been applied. See
235 `org-element-keyword-translation-alist'.
237 By default, all keywords setting attributes (i.e. \"ATTR_LATEX\")
238 allow multiple occurrences and need not to be in this list.")
240 (defconst org-element-parsed-keywords
'("AUTHOR" "CAPTION" "DATE" "TITLE")
241 "List of keywords whose value can be parsed.
243 Their value will be stored as a secondary string: a list of
246 This list is checked after translations have been applied. See
247 `org-element-keyword-translation-alist'.")
249 (defconst org-element-dual-keywords
'("CAPTION" "RESULTS")
250 "List of keywords which can have a secondary value.
252 In Org syntax, they can be written with optional square brackets
253 before the colons. For example, results keyword can be
254 associated to a hash value with the following:
256 #+RESULTS[hash-string]: some-source
258 This list is checked after translations have been applied. See
259 `org-element-keyword-translation-alist'.")
261 (defconst org-element-object-restrictions
262 '((bold export-snippet inline-babel-call inline-src-block latex-or-entity link
263 radio-target sub
/superscript target text-markup timestamp
)
264 (footnote-reference export-snippet footnote-reference inline-babel-call
265 inline-src-block latex-or-entity line-break link macro
266 radio-target sub
/superscript target text-markup
268 (headline inline-babel-call inline-src-block latex-or-entity link macro
269 radio-target statistics-cookie sub
/superscript target text-markup
271 (inlinetask inline-babel-call inline-src-block latex-or-entity link macro
272 radio-target sub
/superscript target text-markup timestamp
)
273 (italic export-snippet inline-babel-call inline-src-block latex-or-entity
274 link radio-target sub
/superscript target text-markup timestamp
)
275 (item export-snippet footnote-reference inline-babel-call latex-or-entity
276 link macro radio-target sub
/superscript target text-markup
)
277 (keyword latex-or-entity macro sub
/superscript text-markup
)
278 (link export-snippet inline-babel-call inline-src-block latex-or-entity link
279 sub
/superscript text-markup
)
281 (paragraph export-snippet footnote-reference inline-babel-call
282 inline-src-block latex-or-entity line-break link macro
283 radio-target statistics-cookie sub
/superscript target text-markup
285 (radio-target export-snippet latex-or-entity sub
/superscript
)
286 (strike-through export-snippet inline-babel-call inline-src-block
287 latex-or-entity link radio-target sub
/superscript target
288 text-markup timestamp
)
289 (subscript export-snippet inline-babel-call inline-src-block latex-or-entity
290 sub
/superscript target text-markup
)
291 (superscript export-snippet inline-babel-call inline-src-block
292 latex-or-entity sub
/superscript target text-markup
)
293 (table-cell export-snippet latex-or-entity link macro radio-target
294 sub
/superscript target text-markup timestamp
)
295 (table-row table-cell
)
296 (underline export-snippet inline-babel-call inline-src-block latex-or-entity
297 link radio-target sub
/superscript target text-markup timestamp
)
298 (verse-block footnote-reference inline-babel-call inline-src-block
299 latex-or-entity line-break link macro radio-target
300 sub
/superscript target text-markup timestamp
))
301 "Alist of objects restrictions.
303 CAR is an element or object type containing objects and CDR is
304 a list of successors that will be called within an element or
307 For example, in a `radio-target' object, one can only find
308 entities, export snippets, latex-fragments, subscript and
311 This alist also applies to secondary string. For example, an
312 `headline' type element doesn't directly contain objects, but
313 still has an entry since one of its properties (`:title') does.")
315 (defconst org-element-secondary-value-alist
316 '((headline .
:title
)
317 (inlinetask .
:title
)
319 (footnote-reference .
:inline-definition
))
320 "Alist between element types and location of secondary value.")
324 ;;; Accessors and Setters
326 ;; Provide four accessors: `org-element-type', `org-element-property'
327 ;; `org-element-contents' and `org-element-restriction'.
329 ;; Setter functions allow to modify elements by side effect. There is
330 ;; `org-element-put-property', `org-element-set-contents',
331 ;; `org-element-set-element' and `org-element-adopt-element'. Note
332 ;; that `org-element-set-element' and `org-element-adopt-element' are
333 ;; higher level functions since also update `:parent' property.
335 (defsubst org-element-type
(element)
336 "Return type of ELEMENT.
338 The function returns the type of the element or object provided.
339 It can also return the following special value:
340 `plain-text' for a string
341 `org-data' for a complete document
342 nil in any other case."
344 ((not (consp element
)) (and (stringp element
) 'plain-text
))
345 ((symbolp (car element
)) (car element
))))
347 (defsubst org-element-property
(property element
)
348 "Extract the value from the PROPERTY of an ELEMENT."
349 (plist-get (nth 1 element
) property
))
351 (defsubst org-element-contents
(element)
352 "Extract contents from an ELEMENT."
353 (and (consp element
) (nthcdr 2 element
)))
355 (defsubst org-element-restriction
(element)
356 "Return restriction associated to ELEMENT.
357 ELEMENT can be an element, an object or a symbol representing an
358 element or object type."
359 (cdr (assq (if (symbolp element
) element
(org-element-type element
))
360 org-element-object-restrictions
)))
362 (defsubst org-element-put-property
(element property value
)
363 "In ELEMENT set PROPERTY to VALUE.
364 Return modified element."
365 (when (consp element
)
366 (setcar (cdr element
) (plist-put (nth 1 element
) property value
)))
369 (defsubst org-element-set-contents
(element &rest contents
)
370 "Set ELEMENT contents to CONTENTS.
371 Return modified element."
372 (cond ((not element
) (list contents
))
373 ((cdr element
) (setcdr (cdr element
) contents
))
374 (t (nconc element contents
))))
376 (defsubst org-element-set-element
(old new
)
377 "Replace element or object OLD with element or object NEW.
378 The function takes care of setting `:parent' property for NEW."
379 ;; OLD can belong to the contents of PARENT or to its secondary
381 (let* ((parent (org-element-property :parent old
))
382 (sec-loc (cdr (assq (org-element-type parent
)
383 org-element-secondary-value-alist
)))
384 (sec-value (and sec-loc
(org-element-property sec-loc parent
)))
385 (place (or (memq old sec-value
) (memq old parent
))))
386 ;; Make sure NEW has correct `:parent' property.
387 (org-element-put-property new
:parent parent
)
388 ;; Replace OLD with NEW in PARENT.
391 (defsubst org-element-adopt-element
(parent child
&optional append
)
392 "Add an element to the contents of another element.
394 PARENT is an element or object. CHILD is an element, an object,
397 CHILD is added at the beginning of PARENT contents, unless the
398 optional argument APPEND is non-nil, in which case CHILD is added
401 The function takes care of setting `:parent' property for CHILD.
402 Return parent element."
403 (if (not parent
) (list child
)
404 (let ((contents (org-element-contents parent
)))
405 (apply 'org-element-set-contents
407 (if append
(append contents
(list child
)) (cons child contents
))))
408 ;; Link the CHILD element with PARENT.
409 (when (consp child
) (org-element-put-property child
:parent parent
))
410 ;; Return the parent element.
417 ;; For each greater element type, we define a parser and an
420 ;; A parser returns the element or object as the list described above.
421 ;; Most of them accepts no argument. Though, exceptions exist. Hence
422 ;; every element containing a secondary string (see
423 ;; `org-element-secondary-value-alist') will accept an optional
424 ;; argument to toggle parsing of that secondary string. Moreover,
425 ;; `item' parser requires current list's structure as its first
428 ;; An interpreter accepts two arguments: the list representation of
429 ;; the element or object, and its contents. The latter may be nil,
430 ;; depending on the element or object considered. It returns the
431 ;; appropriate Org syntax, as a string.
433 ;; Parsing functions must follow the naming convention:
434 ;; org-element-TYPE-parser, where TYPE is greater element's type, as
435 ;; defined in `org-element-greater-elements'.
437 ;; Similarly, interpreting functions must follow the naming
438 ;; convention: org-element-TYPE-interpreter.
440 ;; With the exception of `headline' and `item' types, greater elements
441 ;; cannot contain other greater elements of their own type.
443 ;; Beside implementing a parser and an interpreter, adding a new
444 ;; greater element requires to tweak `org-element--current-element'.
445 ;; Moreover, the newly defined type must be added to both
446 ;; `org-element-all-elements' and `org-element-greater-elements'.
451 (defun org-element-center-block-parser (limit)
452 "Parse a center block.
454 LIMIT bounds the search.
456 Return a list whose CAR is `center-block' and CDR is a plist
457 containing `:begin', `:end', `:hiddenp', `:contents-begin',
458 `:contents-end' and `:post-blank' keywords.
460 Assume point is at the beginning of the block."
461 (let ((case-fold-search t
))
462 (if (not (save-excursion
463 (re-search-forward "^[ \t]*#\\+END_CENTER" limit t
)))
464 ;; Incomplete block: parse it as a comment.
465 (org-element-comment-parser limit
)
466 (let ((block-end-line (match-beginning 0)))
467 (let* ((keywords (org-element--collect-affiliated-keywords))
468 (begin (car keywords
))
469 ;; Empty blocks have no contents.
470 (contents-begin (progn (forward-line)
471 (and (< (point) block-end-line
)
473 (contents-end (and contents-begin block-end-line
))
474 (hidden (org-invisible-p2))
475 (pos-before-blank (progn (goto-char block-end-line
)
478 (end (save-excursion (skip-chars-forward " \r\t\n" limit
)
479 (if (eobp) (point) (point-at-bol)))))
485 :contents-begin contents-begin
486 :contents-end contents-end
487 :post-blank
(count-lines pos-before-blank end
))
488 (cadr keywords
))))))))
490 (defun org-element-center-block-interpreter (center-block contents
)
491 "Interpret CENTER-BLOCK element as Org syntax.
492 CONTENTS is the contents of the element."
493 (format "#+BEGIN_CENTER\n%s#+END_CENTER" contents
))
498 (defun org-element-drawer-parser (limit)
501 LIMIT bounds the search.
503 Return a list whose CAR is `drawer' and CDR is a plist containing
504 `:drawer-name', `:begin', `:end', `:hiddenp', `:contents-begin',
505 `:contents-end' and `:post-blank' keywords.
507 Assume point is at beginning of drawer."
508 (let ((case-fold-search t
))
509 (if (not (save-excursion (re-search-forward "^[ \t]*:END:" limit t
)))
510 ;; Incomplete drawer: parse it as a paragraph.
511 (org-element-paragraph-parser limit
)
512 (let ((drawer-end-line (match-beginning 0)))
514 (let* ((case-fold-search t
)
515 (name (progn (looking-at org-drawer-regexp
)
516 (org-match-string-no-properties 1)))
517 (keywords (org-element--collect-affiliated-keywords))
518 (begin (car keywords
))
519 ;; Empty drawers have no contents.
520 (contents-begin (progn (forward-line)
521 (and (< (point) drawer-end-line
)
523 (contents-end (and contents-begin drawer-end-line
))
524 (hidden (org-invisible-p2))
525 (pos-before-blank (progn (goto-char drawer-end-line
)
528 (end (progn (skip-chars-forward " \r\t\n" limit
)
529 (if (eobp) (point) (point-at-bol)))))
536 :contents-begin contents-begin
537 :contents-end contents-end
538 :post-blank
(count-lines pos-before-blank end
))
539 (cadr keywords
)))))))))
541 (defun org-element-drawer-interpreter (drawer contents
)
542 "Interpret DRAWER element as Org syntax.
543 CONTENTS is the contents of the element."
544 (format ":%s:\n%s:END:"
545 (org-element-property :drawer-name drawer
)
551 (defun org-element-dynamic-block-parser (limit)
552 "Parse a dynamic block.
554 LIMIT bounds the search.
556 Return a list whose CAR is `dynamic-block' and CDR is a plist
557 containing `:block-name', `:begin', `:end', `:hiddenp',
558 `:contents-begin', `:contents-end', `:arguments' and
559 `:post-blank' keywords.
561 Assume point is at beginning of dynamic block."
562 (let ((case-fold-search t
))
563 (if (not (save-excursion (re-search-forward org-dblock-end-re limit t
)))
564 ;; Incomplete block: parse it as a comment.
565 (org-element-comment-parser limit
)
566 (let ((block-end-line (match-beginning 0)))
568 (let* ((name (progn (looking-at org-dblock-start-re
)
569 (org-match-string-no-properties 1)))
570 (arguments (org-match-string-no-properties 3))
571 (keywords (org-element--collect-affiliated-keywords))
572 (begin (car keywords
))
573 ;; Empty blocks have no contents.
574 (contents-begin (progn (forward-line)
575 (and (< (point) block-end-line
)
577 (contents-end (and contents-begin block-end-line
))
578 (hidden (org-invisible-p2))
579 (pos-before-blank (progn (goto-char block-end-line
)
582 (end (progn (skip-chars-forward " \r\t\n" limit
)
583 (if (eobp) (point) (point-at-bol)))))
591 :contents-begin contents-begin
592 :contents-end contents-end
593 :post-blank
(count-lines pos-before-blank end
))
594 (cadr keywords
)))))))))
596 (defun org-element-dynamic-block-interpreter (dynamic-block contents
)
597 "Interpret DYNAMIC-BLOCK element as Org syntax.
598 CONTENTS is the contents of the element."
599 (format "#+BEGIN: %s%s\n%s#+END:"
600 (org-element-property :block-name dynamic-block
)
601 (let ((args (org-element-property :arguments dynamic-block
)))
602 (and args
(concat " " args
)))
606 ;;;; Footnote Definition
608 (defun org-element-footnote-definition-parser (limit)
609 "Parse a footnote definition.
611 LIMIT bounds the search.
613 Return a list whose CAR is `footnote-definition' and CDR is
614 a plist containing `:label', `:begin' `:end', `:contents-begin',
615 `:contents-end' and `:post-blank' keywords.
617 Assume point is at the beginning of the footnote definition."
619 (let* ((label (progn (looking-at org-footnote-definition-re
)
620 (org-match-string-no-properties 1)))
621 (keywords (org-element--collect-affiliated-keywords))
622 (begin (car keywords
))
623 (ending (save-excursion
627 (concat org-outline-regexp-bol
"\\|"
628 org-footnote-definition-re
"\\|"
629 "^[ \t]*$") limit
'move
))
632 (contents-begin (progn (search-forward "]")
633 (skip-chars-forward " \r\t\n" ending
)
634 (and (/= (point) ending
) (point))))
635 (contents-end (and contents-begin ending
))
636 (end (progn (goto-char ending
)
637 (skip-chars-forward " \r\t\n" limit
)
638 (if (eobp) (point) (point-at-bol)))))
639 (list 'footnote-definition
644 :contents-begin contents-begin
645 :contents-end contents-end
646 :post-blank
(count-lines ending end
))
649 (defun org-element-footnote-definition-interpreter (footnote-definition contents
)
650 "Interpret FOOTNOTE-DEFINITION element as Org syntax.
651 CONTENTS is the contents of the footnote-definition."
652 (concat (format "[%s]" (org-element-property :label footnote-definition
))
659 (defun org-element-headline-parser (limit &optional raw-secondary-p
)
662 Return a list whose CAR is `headline' and CDR is a plist
663 containing `:raw-value', `:title', `:begin', `:end',
664 `:pre-blank', `:hiddenp', `:contents-begin' and `:contents-end',
665 `:level', `:priority', `:tags', `:todo-keyword',`:todo-type',
666 `:scheduled', `:deadline', `:timestamp', `:clock', `:category',
667 `:quotedp', `:archivedp', `:commentedp' and `:footnote-section-p'
670 The plist also contains any property set in the property drawer,
671 with its name in lowercase, the underscores replaced with hyphens
672 and colons at the beginning (i.e. `:custom-id').
674 When RAW-SECONDARY-P is non-nil, headline's title will not be
675 parsed as a secondary string, but as a plain string instead.
677 Assume point is at beginning of the headline."
679 (let* ((components (org-heading-components))
680 (level (nth 1 components
))
681 (todo (nth 2 components
))
683 (and todo
(if (member todo org-done-keywords
) 'done
'todo
)))
684 (tags (let ((raw-tags (nth 5 components
)))
685 (and raw-tags
(org-split-string raw-tags
":"))))
686 (raw-value (nth 4 components
))
688 (let ((case-fold-search nil
))
689 (string-match (format "^%s +" org-quote-string
) raw-value
)))
691 (let ((case-fold-search nil
))
692 (string-match (format "^%s +" org-comment-string
) raw-value
)))
693 (archivedp (member org-archive-tag tags
))
694 (footnote-section-p (and org-footnote-section
695 (string= org-footnote-section raw-value
)))
696 ;; Normalize property names: ":SOME_PROP:" becomes
698 (standard-props (let (plist)
701 (let ((p-name (downcase (car p
))))
702 (while (string-match "_" p-name
)
704 (replace-match "-" nil nil p-name
)))
705 (setq p-name
(intern (concat ":" p-name
)))
707 (plist-put plist p-name
(cdr p
)))))
708 (org-entry-properties nil
'standard
))
710 (time-props (org-entry-properties nil
'special
"CLOCK"))
711 (scheduled (cdr (assoc "SCHEDULED" time-props
)))
712 (deadline (cdr (assoc "DEADLINE" time-props
)))
713 (clock (cdr (assoc "CLOCK" time-props
)))
714 (timestamp (cdr (assoc "TIMESTAMP" time-props
)))
716 (end (save-excursion (goto-char (org-end-of-subtree t t
))))
717 (pos-after-head (progn (forward-line) (point)))
718 (contents-begin (save-excursion
719 (skip-chars-forward " \r\t\n" end
)
720 (and (/= (point) end
) (line-beginning-position))))
721 (hidden (org-invisible-p2))
722 (contents-end (and contents-begin
723 (progn (goto-char end
)
724 (skip-chars-backward " \r\t\n")
727 ;; Clean RAW-VALUE from any quote or comment string.
728 (when (or quotedp commentedp
)
730 (replace-regexp-in-string
731 (concat "\\(" org-quote-string
"\\|" org-comment-string
"\\) +")
734 ;; Clean TAGS from archive tag, if any.
735 (when archivedp
(setq tags
(delete org-archive-tag tags
)))
739 (list :raw-value raw-value
743 (if (not contents-begin
) 0
744 (count-lines pos-after-head contents-begin
))
746 :contents-begin contents-begin
747 :contents-end contents-end
749 :priority
(nth 3 components
)
757 :post-blank
(count-lines
758 (if (not contents-end
) pos-after-head
759 (goto-char contents-end
)
763 :footnote-section-p footnote-section-p
765 :commentedp commentedp
768 (org-element-put-property
770 (if raw-secondary-p raw-value
771 (org-element-parse-secondary-string
772 raw-value
(org-element-restriction 'headline
) headline
)))))))
774 (defun org-element-headline-interpreter (headline contents
)
775 "Interpret HEADLINE element as Org syntax.
776 CONTENTS is the contents of the element."
777 (let* ((level (org-element-property :level headline
))
778 (todo (org-element-property :todo-keyword headline
))
779 (priority (org-element-property :priority headline
))
780 (title (org-element-interpret-data
781 (org-element-property :title headline
)))
782 (tags (let ((tag-list (if (org-element-property :archivedp headline
)
783 (cons org-archive-tag
784 (org-element-property :tags headline
))
785 (org-element-property :tags headline
))))
787 (format ":%s:" (mapconcat 'identity tag-list
":")))))
788 (commentedp (org-element-property :commentedp headline
))
789 (quotedp (org-element-property :quotedp headline
))
790 (pre-blank (or (org-element-property :pre-blank headline
) 0))
791 (heading (concat (make-string level ?
*)
792 (and todo
(concat " " todo
))
793 (and quotedp
(concat " " org-quote-string
))
794 (and commentedp
(concat " " org-comment-string
))
796 (format " [#%s]" (char-to-string priority
)))
797 (cond ((and org-footnote-section
798 (org-element-property
799 :footnote-section-p headline
))
800 (concat " " org-footnote-section
))
801 (title (concat " " title
))))))
806 ((zerop org-tags-column
) (format " %s" tags
))
807 ((< org-tags-column
0)
810 (max (- (+ org-tags-column
(length heading
) (length tags
))) 1)
815 (make-string (max (- org-tags-column
(length heading
)) 1) ?
)
817 (make-string (1+ pre-blank
) 10)
823 (defun org-element-inlinetask-parser (limit &optional raw-secondary-p
)
824 "Parse an inline task.
826 Return a list whose CAR is `inlinetask' and CDR is a plist
827 containing `:title', `:begin', `:end', `:hiddenp',
828 `:contents-begin' and `:contents-end', `:level', `:priority',
829 `:tags', `:todo-keyword', `:todo-type', `:scheduled',
830 `:deadline', `:timestamp', `:clock' and `:post-blank' keywords.
832 The plist also contains any property set in the property drawer,
833 with its name in lowercase, the underscores replaced with hyphens
834 and colons at the beginning (i.e. `:custom-id').
836 When optional argument RAW-SECONDARY-P is non-nil, inline-task's
837 title will not be parsed as a secondary string, but as a plain
840 Assume point is at beginning of the inline task."
842 (let* ((keywords (org-element--collect-affiliated-keywords))
843 (begin (car keywords
))
844 (components (org-heading-components))
845 (todo (nth 2 components
))
847 (if (member todo org-done-keywords
) 'done
'todo
)))
848 (tags (let ((raw-tags (nth 5 components
)))
849 (and raw-tags
(org-split-string raw-tags
":"))))
850 ;; Normalize property names: ":SOME_PROP:" becomes
852 (standard-props (let (plist)
855 (let ((p-name (downcase (car p
))))
856 (while (string-match "_" p-name
)
858 (replace-match "-" nil nil p-name
)))
859 (setq p-name
(intern (concat ":" p-name
)))
861 (plist-put plist p-name
(cdr p
)))))
862 (org-entry-properties nil
'standard
))
864 (time-props (org-entry-properties nil
'special
"CLOCK"))
865 (scheduled (cdr (assoc "SCHEDULED" time-props
)))
866 (deadline (cdr (assoc "DEADLINE" time-props
)))
867 (clock (cdr (assoc "CLOCK" time-props
)))
868 (timestamp (cdr (assoc "TIMESTAMP" time-props
)))
869 (task-end (save-excursion
871 (and (re-search-forward "^\\*+ END" limit t
)
872 (match-beginning 0))))
873 (contents-begin (progn (forward-line)
874 (and task-end
(< (point) task-end
) (point))))
875 (hidden (and contents-begin
(org-invisible-p2)))
876 (contents-end (and contents-begin task-end
))
877 (before-blank (if (not task-end
) (point)
881 (end (progn (skip-chars-forward " \r\t\n" limit
)
882 (if (eobp) (point) (point-at-bol))))
889 :contents-begin contents-begin
890 :contents-end contents-end
891 :level
(nth 1 components
)
892 :priority
(nth 3 components
)
900 :post-blank
(count-lines before-blank end
))
903 (org-element-put-property
905 (if raw-secondary-p
(nth 4 components
)
906 (org-element-parse-secondary-string
908 (org-element-restriction 'inlinetask
)
911 (defun org-element-inlinetask-interpreter (inlinetask contents
)
912 "Interpret INLINETASK element as Org syntax.
913 CONTENTS is the contents of inlinetask."
914 (let* ((level (org-element-property :level inlinetask
))
915 (todo (org-element-property :todo-keyword inlinetask
))
916 (priority (org-element-property :priority inlinetask
))
917 (title (org-element-interpret-data
918 (org-element-property :title inlinetask
)))
919 (tags (let ((tag-list (org-element-property :tags inlinetask
)))
921 (format ":%s:" (mapconcat 'identity tag-list
":")))))
922 (task (concat (make-string level ?
*)
923 (and todo
(concat " " todo
))
925 (format " [#%s]" (char-to-string priority
)))
926 (and title
(concat " " title
)))))
931 ((zerop org-tags-column
) (format " %s" tags
))
932 ((< org-tags-column
0)
935 (max (- (+ org-tags-column
(length task
) (length tags
))) 1)
940 (make-string (max (- org-tags-column
(length task
)) 1) ?
)
942 ;; Prefer degenerate inlinetasks when there are no
947 (make-string level ?
*) " END")))))
952 (defun org-element-item-parser (limit struct
&optional raw-secondary-p
)
955 STRUCT is the structure of the plain list.
957 Return a list whose CAR is `item' and CDR is a plist containing
958 `:bullet', `:begin', `:end', `:contents-begin', `:contents-end',
959 `:checkbox', `:counter', `:tag', `:structure', `:hiddenp' and
960 `:post-blank' keywords.
962 When optional argument RAW-SECONDARY-P is non-nil, item's tag, if
963 any, will not be parsed as a secondary string, but as a plain
966 Assume point is at the beginning of the item."
969 (let* ((begin (point))
970 (bullet (org-list-get-bullet (point) struct
))
971 (checkbox (let ((box (org-list-get-checkbox begin struct
)))
972 (cond ((equal "[ ]" box
) 'off
)
973 ((equal "[X]" box
) 'on
)
974 ((equal "[-]" box
) 'trans
))))
975 (counter (let ((c (org-list-get-counter begin struct
)))
978 ((string-match "[A-Za-z]" c
)
979 (- (string-to-char (upcase (match-string 0 c
)))
981 ((string-match "[0-9]+" c
)
982 (string-to-number (match-string 0 c
))))))
983 (end (save-excursion (goto-char (org-list-get-item-end begin struct
))
984 (unless (bolp) (forward-line))
986 (contents-begin (progn (looking-at org-list-full-item-re
)
987 (goto-char (match-end 0))
988 (skip-chars-forward " \r\t\n" limit
)
989 ;; If first line isn't empty,
990 ;; contents really start at the text
991 ;; after item's meta-data.
992 (if (= (point-at-bol) begin
) (point)
994 (hidden (progn (forward-line)
995 (and (not (= (point) end
))
996 (org-invisible-p2))))
997 (contents-end (progn (goto-char end
)
998 (skip-chars-backward " \r\t\n")
1003 (list :bullet bullet
1006 ;; CONTENTS-BEGIN and CONTENTS-END may be
1007 ;; mixed up in the case of an empty item
1008 ;; separated from the next by a blank line.
1009 ;; Thus ensure the former is always the
1011 :contents-begin
(min contents-begin contents-end
)
1012 :contents-end
(max contents-begin contents-end
)
1017 :post-blank
(count-lines contents-end end
)))))
1018 (org-element-put-property
1020 (let ((raw-tag (org-list-get-tag begin struct
)))
1022 (if raw-secondary-p raw-tag
1023 (org-element-parse-secondary-string
1024 raw-tag
(org-element-restriction 'item
) item
))))))))
1026 (defun org-element-item-interpreter (item contents
)
1027 "Interpret ITEM element as Org syntax.
1028 CONTENTS is the contents of the element."
1029 (let* ((bullet (org-list-bullet-string (org-element-property :bullet item
)))
1030 (checkbox (org-element-property :checkbox item
))
1031 (counter (org-element-property :counter item
))
1032 (tag (let ((tag (org-element-property :tag item
)))
1033 (and tag
(org-element-interpret-data tag
))))
1034 ;; Compute indentation.
1035 (ind (make-string (length bullet
) 32))
1036 (item-starts-with-par-p
1037 (eq (org-element-type (car (org-element-contents item
)))
1042 (and counter
(format "[@%d] " counter
))
1047 (and tag
(format "%s :: " tag
))
1048 (let ((contents (replace-regexp-in-string
1049 "\\(^\\)[ \t]*\\S-" ind contents nil nil
1)))
1050 (if item-starts-with-par-p
(org-trim contents
)
1051 (concat "\n" contents
))))))
1056 (defun org-element-plain-list-parser (limit &optional structure
)
1057 "Parse a plain list.
1059 Optional argument STRUCTURE, when non-nil, is the structure of
1060 the plain list being parsed.
1062 Return a list whose CAR is `plain-list' and CDR is a plist
1063 containing `:type', `:begin', `:end', `:contents-begin' and
1064 `:contents-end', `:structure' and `:post-blank' keywords.
1066 Assume point is at the beginning of the list."
1068 (let* ((struct (or structure
(org-list-struct)))
1069 (prevs (org-list-prevs-alist struct
))
1070 (parents (org-list-parents-alist struct
))
1071 (type (org-list-get-list-type (point) struct prevs
))
1072 (contents-begin (point))
1073 (keywords (org-element--collect-affiliated-keywords))
1074 (begin (car keywords
))
1076 (progn (goto-char (org-list-get-list-end (point) struct prevs
))
1077 (unless (bolp) (forward-line))
1079 (end (progn (skip-chars-forward " \r\t\n" limit
)
1080 (if (eobp) (point) (point-at-bol)))))
1087 :contents-begin contents-begin
1088 :contents-end contents-end
1090 :post-blank
(count-lines contents-end end
))
1091 (cadr keywords
))))))
1093 (defun org-element-plain-list-interpreter (plain-list contents
)
1094 "Interpret PLAIN-LIST element as Org syntax.
1095 CONTENTS is the contents of the element."
1098 (goto-char (point-min))
1105 (defun org-element-quote-block-parser (limit)
1106 "Parse a quote block.
1108 LIMIT bounds the search.
1110 Return a list whose CAR is `quote-block' and CDR is a plist
1111 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1112 `:contents-end' and `:post-blank' keywords.
1114 Assume point is at the beginning of the block."
1115 (let ((case-fold-search t
))
1116 (if (not (save-excursion
1117 (re-search-forward "^[ \t]*#\\+END_QUOTE" limit t
)))
1118 ;; Incomplete block: parse it as a comment.
1119 (org-element-comment-parser limit
)
1120 (let ((block-end-line (match-beginning 0)))
1122 (let* ((keywords (org-element--collect-affiliated-keywords))
1123 (begin (car keywords
))
1124 ;; Empty blocks have no contents.
1125 (contents-begin (progn (forward-line)
1126 (and (< (point) block-end-line
)
1128 (contents-end (and contents-begin block-end-line
))
1129 (hidden (org-invisible-p2))
1130 (pos-before-blank (progn (goto-char block-end-line
)
1133 (end (progn (skip-chars-forward " \r\t\n" limit
)
1134 (if (eobp) (point) (point-at-bol)))))
1140 :contents-begin contents-begin
1141 :contents-end contents-end
1142 :post-blank
(count-lines pos-before-blank end
))
1143 (cadr keywords
)))))))))
1145 (defun org-element-quote-block-interpreter (quote-block contents
)
1146 "Interpret QUOTE-BLOCK element as Org syntax.
1147 CONTENTS is the contents of the element."
1148 (format "#+BEGIN_QUOTE\n%s#+END_QUOTE" contents
))
1153 (defun org-element-section-parser (limit)
1156 LIMIT bounds the search.
1158 Return a list whose CAR is `section' and CDR is a plist
1159 containing `:begin', `:end', `:contents-begin', `contents-end'
1160 and `:post-blank' keywords."
1162 ;; Beginning of section is the beginning of the first non-blank
1163 ;; line after previous headline.
1164 (org-with-limited-levels
1165 (let ((begin (point))
1166 (end (progn (goto-char limit
) (point)))
1167 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
1173 :contents-begin begin
1174 :contents-end pos-before-blank
1175 :post-blank
(count-lines pos-before-blank end
)))))))
1177 (defun org-element-section-interpreter (section contents
)
1178 "Interpret SECTION element as Org syntax.
1179 CONTENTS is the contents of the element."
1185 (defun org-element-special-block-parser (limit)
1186 "Parse a special block.
1188 LIMIT bounds the search.
1190 Return a list whose CAR is `special-block' and CDR is a plist
1191 containing `:type', `:begin', `:end', `:hiddenp',
1192 `:contents-begin', `:contents-end' and `:post-blank' keywords.
1194 Assume point is at the beginning of the block."
1195 (let* ((case-fold-search t
)
1196 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(S-+\\)")
1197 (upcase (match-string-no-properties 1)))))
1198 (if (not (save-excursion
1199 (re-search-forward (concat "^[ \t]*#\\+END_" type
) limit t
)))
1200 ;; Incomplete block: parse it as a comment.
1201 (org-element-comment-parser limit
)
1202 (let ((block-end-line (match-beginning 0)))
1204 (let* ((keywords (org-element--collect-affiliated-keywords))
1205 (begin (car keywords
))
1206 ;; Empty blocks have no contents.
1207 (contents-begin (progn (forward-line)
1208 (and (< (point) block-end-line
)
1210 (contents-end (and contents-begin block-end-line
))
1211 (hidden (org-invisible-p2))
1212 (pos-before-blank (progn (goto-char block-end-line
)
1215 (end (progn (org-skip-whitespace)
1216 (if (eobp) (point) (point-at-bol)))))
1217 (list 'special-block
1223 :contents-begin contents-begin
1224 :contents-end contents-end
1225 :post-blank
(count-lines pos-before-blank end
))
1226 (cadr keywords
)))))))))
1228 (defun org-element-special-block-interpreter (special-block contents
)
1229 "Interpret SPECIAL-BLOCK element as Org syntax.
1230 CONTENTS is the contents of the element."
1231 (let ((block-type (org-element-property :type special-block
)))
1232 (format "#+BEGIN_%s\n%s#+END_%s" block-type contents block-type
)))
1238 ;; For each element, a parser and an interpreter are also defined.
1239 ;; Both follow the same naming convention used for greater elements.
1241 ;; Also, as for greater elements, adding a new element type is done
1242 ;; through the following steps: implement a parser and an interpreter,
1243 ;; tweak `org-element--current-element' so that it recognizes the new
1244 ;; type and add that new type to `org-element-all-elements'.
1246 ;; As a special case, when the newly defined type is a block type,
1247 ;; `org-element-block-name-alist' has to be modified accordingly.
1252 (defun org-element-babel-call-parser (limit)
1253 "Parse a babel call.
1255 LIMIT bounds the search.
1257 Return a list whose CAR is `babel-call' and CDR is a plist
1258 containing `:begin', `:end', `:info' and `:post-blank' as
1261 (let ((case-fold-search t
)
1262 (info (progn (looking-at org-babel-block-lob-one-liner-regexp
)
1263 (org-babel-lob-get-info)))
1264 (begin (point-at-bol))
1265 (pos-before-blank (progn (forward-line) (point)))
1266 (end (progn (skip-chars-forward " \r\t\n" limit
)
1267 (if (eobp) (point) (point-at-bol)))))
1272 :post-blank
(count-lines pos-before-blank end
))))))
1274 (defun org-element-babel-call-interpreter (babel-call contents
)
1275 "Interpret BABEL-CALL element as Org syntax.
1277 (let* ((babel-info (org-element-property :info babel-call
))
1278 (main (car babel-info
))
1279 (post-options (nth 1 babel-info
)))
1281 (if (not (string-match "\\[\\(\\[.*?\\]\\)\\]" main
)) main
1282 ;; Remove redundant square brackets.
1283 (replace-match (match-string 1 main
) nil nil main
))
1284 (and post-options
(format "[%s]" post-options
)))))
1289 (defun org-element-clock-parser (limit)
1292 LIMIT bounds the search.
1294 Return a list whose CAR is `clock' and CDR is a plist containing
1295 `:status', `:value', `:time', `:begin', `:end' and `:post-blank'
1298 (let* ((case-fold-search nil
)
1300 (value (progn (search-forward org-clock-string
(line-end-position) t
)
1301 (org-skip-whitespace)
1302 (looking-at "\\[.*\\]")
1303 (org-match-string-no-properties 0)))
1304 (time (and (progn (goto-char (match-end 0))
1305 (looking-at " +=> +\\(\\S-+\\)[ \t]*$"))
1306 (org-match-string-no-properties 1)))
1307 (status (if time
'closed
'running
))
1308 (post-blank (let ((before-blank (progn (forward-line) (point))))
1309 (skip-chars-forward " \r\t\n" limit
)
1310 (unless (eobp) (beginning-of-line))
1311 (count-lines before-blank
(point))))
1314 (list :status status
1319 :post-blank post-blank
)))))
1321 (defun org-element-clock-interpreter (clock contents
)
1322 "Interpret CLOCK element as Org syntax.
1324 (concat org-clock-string
" "
1325 (org-element-property :value clock
)
1326 (let ((time (org-element-property :time clock
)))
1331 (org-split-string time
":")))))))
1336 (defun org-element-comment-parser (limit)
1339 LIMIT bounds the search.
1341 Return a list whose CAR is `comment' and CDR is a plist
1342 containing `:begin', `:end', `:value' and `:post-blank'
1345 Assume point is at comment beginning."
1347 (let* ((keywords (org-element--collect-affiliated-keywords))
1348 (begin (car keywords
))
1349 ;; Match first line with a loose regexp since it might as
1350 ;; well be an ill-defined keyword.
1351 (value (prog2 (looking-at "[ \t]*# ?")
1352 (buffer-substring-no-properties
1353 (match-end 0) (line-end-position))
1356 ;; Get comments ending.
1358 (while (and (< (point) limit
) (looking-at "[ \t]*#\\( \\|$\\)"))
1359 ;; Accumulate lines without leading hash and first
1364 (buffer-substring-no-properties
1365 (match-end 0) (line-end-position))))
1368 (end (progn (goto-char com-end
)
1369 (skip-chars-forward " \r\t\n" limit
)
1370 (if (eobp) (point) (point-at-bol)))))
1376 :post-blank
(count-lines com-end end
))
1377 (cadr keywords
))))))
1379 (defun org-element-comment-interpreter (comment contents
)
1380 "Interpret COMMENT element as Org syntax.
1382 (replace-regexp-in-string "^" "# " (org-element-property :value comment
)))
1387 (defun org-element-comment-block-parser (limit)
1388 "Parse an export block.
1390 LIMIT bounds the search.
1392 Return a list whose CAR is `comment-block' and CDR is a plist
1393 containing `:begin', `:end', `:hiddenp', `:value' and
1394 `:post-blank' keywords.
1396 Assume point is at comment block beginning."
1397 (let ((case-fold-search t
))
1398 (if (not (save-excursion
1399 (re-search-forward "^[ \t]*#\\+END_COMMENT" limit t
)))
1400 ;; Incomplete block: parse it as a comment.
1401 (org-element-comment-parser limit
)
1402 (let ((contents-end (match-beginning 0)))
1404 (let* ((keywords (org-element--collect-affiliated-keywords))
1405 (begin (car keywords
))
1406 (contents-begin (progn (forward-line) (point)))
1407 (hidden (org-invisible-p2))
1408 (pos-before-blank (progn (goto-char contents-end
)
1411 (end (progn (skip-chars-forward " \r\t\n" limit
)
1412 (if (eobp) (point) (point-at-bol))))
1413 (value (buffer-substring-no-properties
1414 contents-begin contents-end
)))
1415 (list 'comment-block
1421 :post-blank
(count-lines pos-before-blank end
))
1422 (cadr keywords
)))))))))
1424 (defun org-element-comment-block-interpreter (comment-block contents
)
1425 "Interpret COMMENT-BLOCK element as Org syntax.
1427 (format "#+BEGIN_COMMENT\n%s#+END_COMMENT"
1428 (org-remove-indentation (org-element-property :value comment-block
))))
1433 (defun org-element-example-block-parser (limit)
1434 "Parse an example block.
1436 LIMIT bounds the search.
1438 Return a list whose CAR is `example-block' and CDR is a plist
1439 containing `:begin', `:end', `:number-lines', `:preserve-indent',
1440 `:retain-labels', `:use-labels', `:label-fmt', `:hiddenp',
1441 `:switches', `:value' and `:post-blank' keywords."
1442 (let ((case-fold-search t
))
1443 (if (not (save-excursion
1444 (re-search-forward "^[ \t]*#\\+END_EXAMPLE" limit t
)))
1445 ;; Incomplete block: parse it as a comment.
1446 (org-element-comment-parser limit
)
1447 (let ((contents-end (match-beginning 0)))
1450 (progn (looking-at "^[ \t]*#\\+BEGIN_EXAMPLE\\(?: +\\(.*\\)\\)?")
1451 (org-match-string-no-properties 1)))
1452 ;; Switches analysis
1453 (number-lines (cond ((not switches
) nil
)
1454 ((string-match "-n\\>" switches
) 'new
)
1455 ((string-match "+n\\>" switches
) 'continued
)))
1456 (preserve-indent (and switches
(string-match "-i\\>" switches
)))
1457 ;; Should labels be retained in (or stripped from) example
1461 (not (string-match "-r\\>" switches
))
1462 (and number-lines
(string-match "-k\\>" switches
))))
1463 ;; What should code-references use - labels or
1467 (and retain-labels
(not (string-match "-k\\>" switches
)))))
1468 (label-fmt (and switches
1469 (string-match "-l +\"\\([^\"\n]+\\)\"" switches
)
1470 (match-string 1 switches
)))
1471 ;; Standard block parsing.
1472 (keywords (org-element--collect-affiliated-keywords))
1473 (begin (car keywords
))
1474 (contents-begin (progn (forward-line) (point)))
1475 (hidden (org-invisible-p2))
1476 (value (buffer-substring-no-properties contents-begin contents-end
))
1477 (pos-before-blank (progn (goto-char contents-end
)
1480 (end (progn (skip-chars-forward " \r\t\n" limit
)
1481 (if (eobp) (point) (point-at-bol)))))
1482 (list 'example-block
1488 :number-lines number-lines
1489 :preserve-indent preserve-indent
1490 :retain-labels retain-labels
1491 :use-labels use-labels
1492 :label-fmt label-fmt
1494 :post-blank
(count-lines pos-before-blank end
))
1495 (cadr keywords
)))))))))
1497 (defun org-element-example-block-interpreter (example-block contents
)
1498 "Interpret EXAMPLE-BLOCK element as Org syntax.
1500 (let ((switches (org-element-property :switches example-block
)))
1501 (concat "#+BEGIN_EXAMPLE" (and switches
(concat " " switches
)) "\n"
1502 (org-remove-indentation
1503 (org-element-property :value example-block
))
1509 (defun org-element-export-block-parser (limit)
1510 "Parse an export block.
1512 LIMIT bounds the search.
1514 Return a list whose CAR is `export-block' and CDR is a plist
1515 containing `:begin', `:end', `:type', `:hiddenp', `:value' and
1516 `:post-blank' keywords.
1518 Assume point is at export-block beginning."
1519 (let* ((case-fold-search t
)
1520 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
1521 (upcase (org-match-string-no-properties 1)))))
1522 (if (not (save-excursion
1523 (re-search-forward (concat "^[ \t]*#\\+END_" type
) limit t
)))
1524 ;; Incomplete block: parse it as a comment.
1525 (org-element-comment-parser limit
)
1526 (let ((contents-end (match-beginning 0)))
1528 (let* ((keywords (org-element--collect-affiliated-keywords))
1529 (begin (car keywords
))
1530 (contents-begin (progn (forward-line) (point)))
1531 (hidden (org-invisible-p2))
1532 (pos-before-blank (progn (goto-char contents-end
)
1535 (end (progn (skip-chars-forward " \r\t\n" limit
)
1536 (if (eobp) (point) (point-at-bol))))
1537 (value (buffer-substring-no-properties contents-begin
1546 :post-blank
(count-lines pos-before-blank end
))
1547 (cadr keywords
)))))))))
1549 (defun org-element-export-block-interpreter (export-block contents
)
1550 "Interpret EXPORT-BLOCK element as Org syntax.
1552 (let ((type (org-element-property :type export-block
)))
1553 (concat (format "#+BEGIN_%s\n" type
)
1554 (org-element-property :value export-block
)
1555 (format "#+END_%s" type
))))
1560 (defun org-element-fixed-width-parser (limit)
1561 "Parse a fixed-width section.
1563 LIMIT bounds the search.
1565 Return a list whose CAR is `fixed-width' and CDR is a plist
1566 containing `:begin', `:end', `:value' and `:post-blank' keywords.
1568 Assume point is at the beginning of the fixed-width area."
1570 (let* ((keywords (org-element--collect-affiliated-keywords))
1571 (begin (car keywords
))
1575 (while (and (< (point) limit
)
1576 (looking-at "[ \t]*:\\( \\|$\\)"))
1577 ;; Accumulate text without starting colons.
1580 (buffer-substring-no-properties
1581 (match-end 0) (point-at-eol))
1585 (end (progn (skip-chars-forward " \r\t\n" limit
)
1586 (if (eobp) (point) (point-at-bol)))))
1592 :post-blank
(count-lines end-area end
))
1593 (cadr keywords
))))))
1595 (defun org-element-fixed-width-interpreter (fixed-width contents
)
1596 "Interpret FIXED-WIDTH element as Org syntax.
1598 (replace-regexp-in-string
1599 "^" ": " (substring (org-element-property :value fixed-width
) 0 -
1)))
1602 ;;;; Horizontal Rule
1604 (defun org-element-horizontal-rule-parser (limit)
1605 "Parse an horizontal rule.
1607 LIMIT bounds the search.
1609 Return a list whose CAR is `horizontal-rule' and CDR is a plist
1610 containing `:begin', `:end' and `:post-blank' keywords."
1612 (let* ((keywords (org-element--collect-affiliated-keywords))
1613 (begin (car keywords
))
1614 (post-hr (progn (forward-line) (point)))
1615 (end (progn (skip-chars-forward " \r\t\n" limit
)
1616 (if (eobp) (point) (point-at-bol)))))
1617 (list 'horizontal-rule
1621 :post-blank
(count-lines post-hr end
))
1622 (cadr keywords
))))))
1624 (defun org-element-horizontal-rule-interpreter (horizontal-rule contents
)
1625 "Interpret HORIZONTAL-RULE element as Org syntax.
1632 (defun org-element-keyword-parser (limit)
1633 "Parse a keyword at point.
1635 LIMIT bounds the search.
1637 Return a list whose CAR is `keyword' and CDR is a plist
1638 containing `:key', `:value', `:begin', `:end' and `:post-blank'
1641 (let* ((case-fold-search t
)
1643 (key (progn (looking-at "[ \t]*#\\+\\(\\S-+\\):")
1644 (upcase (org-match-string-no-properties 1))))
1645 (value (org-trim (buffer-substring-no-properties
1646 (match-end 0) (point-at-eol))))
1647 (pos-before-blank (progn (forward-line) (point)))
1648 (end (progn (skip-chars-forward " \r\t\n" limit
)
1649 (if (eobp) (point) (point-at-bol)))))
1655 :post-blank
(count-lines pos-before-blank end
))))))
1657 (defun org-element-keyword-interpreter (keyword contents
)
1658 "Interpret KEYWORD element as Org syntax.
1661 (org-element-property :key keyword
)
1662 (org-element-property :value keyword
)))
1665 ;;;; Latex Environment
1667 (defun org-element-latex-environment-parser (limit)
1668 "Parse a LaTeX environment.
1670 LIMIT bounds the search.
1672 Return a list whose CAR is `latex-environment' and CDR is a plist
1673 containing `:begin', `:end', `:value' and `:post-blank'
1676 Assume point is at the beginning of the latex environment."
1678 (let* ((case-fold-search t
)
1679 (code-begin (point))
1680 (keywords (org-element--collect-affiliated-keywords))
1681 (begin (car keywords
))
1682 (env (progn (looking-at "^[ \t]*\\\\begin{\\([A-Za-z0-9*]+\\)}")
1683 (regexp-quote (match-string 1))))
1685 (progn (re-search-forward (format "^[ \t]*\\\\end{%s}" env
) limit t
)
1688 (value (buffer-substring-no-properties code-begin code-end
))
1689 (end (progn (skip-chars-forward " \r\t\n" limit
)
1690 (if (eobp) (point) (point-at-bol)))))
1691 (list 'latex-environment
1696 :post-blank
(count-lines code-end end
))
1697 (cadr keywords
))))))
1699 (defun org-element-latex-environment-interpreter (latex-environment contents
)
1700 "Interpret LATEX-ENVIRONMENT element as Org syntax.
1702 (org-element-property :value latex-environment
))
1707 (defun org-element-paragraph-parser (limit)
1710 LIMIT bounds the search.
1712 Return a list whose CAR is `paragraph' and CDR is a plist
1713 containing `:begin', `:end', `:contents-begin' and
1714 `:contents-end' and `:post-blank' keywords.
1716 Assume point is at the beginning of the paragraph."
1718 (let* ((contents-begin (point))
1719 (keywords (org-element--collect-affiliated-keywords))
1720 (begin (car keywords
))
1722 (progn (end-of-line)
1723 (if (re-search-forward org-element-paragraph-separate
1726 (goto-char (match-beginning 0))
1728 (contents-end (progn (skip-chars-backward " \r\t\n" contents-begin
)
1731 (end (progn (skip-chars-forward " \r\t\n" limit
)
1732 (if (eobp) (point) (point-at-bol)))))
1734 ;; If paragraph has no affiliated keywords, it may not begin
1735 ;; at beginning of line if it starts an item.
1737 (list :begin
(if (cadr keywords
) begin contents-begin
)
1739 :contents-begin contents-begin
1740 :contents-end contents-end
1741 :post-blank
(count-lines before-blank end
))
1742 (cadr keywords
))))))
1744 (defun org-element-paragraph-interpreter (paragraph contents
)
1745 "Interpret PARAGRAPH element as Org syntax.
1746 CONTENTS is the contents of the element."
1752 (defun org-element-planning-parser (limit)
1755 LIMIT bounds the search.
1757 Return a list whose CAR is `planning' and CDR is a plist
1758 containing `:closed', `:deadline', `:scheduled', `:begin', `:end'
1759 and `:post-blank' keywords."
1761 (let* ((case-fold-search nil
)
1763 (post-blank (let ((before-blank (progn (forward-line) (point))))
1764 (skip-chars-forward " \r\t\n" limit
)
1765 (unless (eobp) (beginning-of-line))
1766 (count-lines before-blank
(point))))
1768 closed deadline scheduled
)
1770 (while (re-search-forward org-keyword-time-not-clock-regexp
1771 (line-end-position) t
)
1772 (goto-char (match-end 1))
1773 (org-skip-whitespace)
1774 (let ((time (buffer-substring-no-properties
1775 (1+ (point)) (1- (match-end 0))))
1776 (keyword (match-string 1)))
1777 (cond ((equal keyword org-closed-string
) (setq closed time
))
1778 ((equal keyword org-deadline-string
) (setq deadline time
))
1779 (t (setq scheduled time
)))))
1781 (list :closed closed
1783 :scheduled scheduled
1786 :post-blank post-blank
)))))
1788 (defun org-element-planning-interpreter (planning contents
)
1789 "Interpret PLANNING element as Org syntax.
1794 (list (let ((closed (org-element-property :closed planning
)))
1795 (when closed
(concat org-closed-string
" [" closed
"]")))
1796 (let ((deadline (org-element-property :deadline planning
)))
1797 (when deadline
(concat org-deadline-string
" <" deadline
">")))
1798 (let ((scheduled (org-element-property :scheduled planning
)))
1800 (concat org-scheduled-string
" <" scheduled
">")))))
1804 ;;;; Property Drawer
1806 (defun org-element-property-drawer-parser (limit)
1807 "Parse a property drawer.
1809 LIMIT bounds the search.
1811 Return a list whose CAR is `property-drawer' and CDR is a plist
1812 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1813 `:contents-end', `:properties' and `:post-blank' keywords.
1815 Assume point is at the beginning of the property drawer."
1817 (let ((case-fold-search t
)
1819 (prop-begin (progn (forward-line) (point)))
1820 (hidden (org-invisible-p2))
1823 (while (not (looking-at "^[ \t]*:END:"))
1824 (when (looking-at "[ \t]*:\\([A-Za-z][-_A-Za-z0-9]*\\):")
1825 (push (cons (org-match-string-no-properties 1)
1827 (buffer-substring-no-properties
1828 (match-end 0) (point-at-eol))))
1832 (prop-end (progn (re-search-forward "^[ \t]*:END:" limit t
)
1834 (pos-before-blank (progn (forward-line) (point)))
1835 (end (progn (skip-chars-forward " \r\t\n" limit
)
1836 (if (eobp) (point) (point-at-bol)))))
1837 (list 'property-drawer
1841 :properties properties
1842 :post-blank
(count-lines pos-before-blank end
))))))
1844 (defun org-element-property-drawer-interpreter (property-drawer contents
)
1845 "Interpret PROPERTY-DRAWER element as Org syntax.
1847 (let ((props (org-element-property :properties property-drawer
)))
1850 (mapconcat (lambda (p)
1851 (format org-property-format
(format ":%s:" (car p
)) (cdr p
)))
1852 (nreverse props
) "\n")
1858 (defun org-element-quote-section-parser (limit)
1859 "Parse a quote section.
1861 LIMIT bounds the search.
1863 Return a list whose CAR is `quote-section' and CDR is a plist
1864 containing `:begin', `:end', `:value' and `:post-blank' keywords.
1866 Assume point is at beginning of the section."
1868 (let* ((begin (point))
1869 (end (progn (org-with-limited-levels (outline-next-heading))
1871 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
1874 (value (buffer-substring-no-properties begin pos-before-blank
)))
1875 (list 'quote-section
1879 :post-blank
(count-lines pos-before-blank end
))))))
1881 (defun org-element-quote-section-interpreter (quote-section contents
)
1882 "Interpret QUOTE-SECTION element as Org syntax.
1884 (org-element-property :value quote-section
))
1889 (defun org-element-src-block-parser (limit)
1892 LIMIT bounds the search.
1894 Return a list whose CAR is `src-block' and CDR is a plist
1895 containing `:language', `:switches', `:parameters', `:begin',
1896 `:end', `:hiddenp', `:number-lines', `:retain-labels',
1897 `:use-labels', `:label-fmt', `:preserve-indent', `:value' and
1898 `:post-blank' keywords.
1900 Assume point is at the beginning of the block."
1901 (let ((case-fold-search t
))
1902 (if (not (save-excursion (re-search-forward "^[ \t]*#\\+END_SRC" limit t
)))
1903 ;; Incomplete block: parse it as a comment.
1904 (org-element-comment-parser limit
)
1905 (let ((contents-end (match-beginning 0)))
1907 (let* ((keywords (org-element--collect-affiliated-keywords))
1908 ;; Get beginning position.
1909 (begin (car keywords
))
1910 ;; Get language as a string.
1914 (concat "^[ \t]*#\\+BEGIN_SRC"
1915 "\\(?: +\\(\\S-+\\)\\)?"
1916 "\\(\\(?: +\\(?:-l \".*?\"\\|[-+][A-Za-z]\\)\\)+\\)?"
1918 (org-match-string-no-properties 1)))
1920 (switches (org-match-string-no-properties 2))
1922 (parameters (org-match-string-no-properties 3))
1923 ;; Switches analysis
1924 (number-lines (cond ((not switches
) nil
)
1925 ((string-match "-n\\>" switches
) 'new
)
1926 ((string-match "+n\\>" switches
) 'continued
)))
1927 (preserve-indent (and switches
(string-match "-i\\>" switches
)))
1928 (label-fmt (and switches
1929 (string-match "-l +\"\\([^\"\n]+\\)\"" switches
)
1930 (match-string 1 switches
)))
1931 ;; Should labels be retained in (or stripped from)
1935 (not (string-match "-r\\>" switches
))
1936 (and number-lines
(string-match "-k\\>" switches
))))
1937 ;; What should code-references use - labels or
1941 (and retain-labels
(not (string-match "-k\\>" switches
)))))
1942 ;; Get visibility status.
1943 (hidden (progn (forward-line) (org-invisible-p2)))
1945 (value (buffer-substring-no-properties (point) contents-end
))
1946 (pos-before-blank (progn (goto-char contents-end
)
1949 ;; Get position after ending blank lines.
1950 (end (progn (skip-chars-forward " \r\t\n" limit
)
1951 (if (eobp) (point) (point-at-bol)))))
1954 (list :language language
1955 :switches
(and (org-string-nw-p switches
)
1956 (org-trim switches
))
1957 :parameters
(and (org-string-nw-p parameters
)
1958 (org-trim parameters
))
1961 :number-lines number-lines
1962 :preserve-indent preserve-indent
1963 :retain-labels retain-labels
1964 :use-labels use-labels
1965 :label-fmt label-fmt
1968 :post-blank
(count-lines pos-before-blank end
))
1969 (cadr keywords
)))))))))
1971 (defun org-element-src-block-interpreter (src-block contents
)
1972 "Interpret SRC-BLOCK element as Org syntax.
1974 (let ((lang (org-element-property :language src-block
))
1975 (switches (org-element-property :switches src-block
))
1976 (params (org-element-property :parameters src-block
))
1977 (value (let ((val (org-element-property :value src-block
)))
1980 (org-src-preserve-indentation val
)
1981 ((zerop org-edit-src-content-indentation
)
1982 (org-remove-indentation val
))
1984 (let ((ind (make-string
1985 org-edit-src-content-indentation
32)))
1986 (replace-regexp-in-string
1987 "\\(^\\)[ \t]*\\S-" ind
1988 (org-remove-indentation val
) nil nil
1)))))))
1989 (concat (format "#+BEGIN_SRC%s\n"
1990 (concat (and lang
(concat " " lang
))
1991 (and switches
(concat " " switches
))
1992 (and params
(concat " " params
))))
1999 (defun org-element-table-parser (limit)
2000 "Parse a table at point.
2002 LIMIT bounds the search.
2004 Return a list whose CAR is `table' and CDR is a plist containing
2005 `:begin', `:end', `:tblfm', `:type', `:contents-begin',
2006 `:contents-end', `:value' and `:post-blank' keywords.
2008 Assume point is at the beginning of the table."
2010 (let* ((case-fold-search t
)
2011 (table-begin (point))
2012 (type (if (org-at-table.el-p
) 'table.el
'org
))
2013 (keywords (org-element--collect-affiliated-keywords))
2014 (begin (car keywords
))
2015 (table-end (goto-char (marker-position (org-table-end t
))))
2017 (while (looking-at "[ \t]*#\\+TBLFM: +\\(.*\\)[ \t]*$")
2018 (push (org-match-string-no-properties 1) acc
)
2021 (pos-before-blank (point))
2022 (end (progn (skip-chars-forward " \r\t\n" limit
)
2023 (if (eobp) (point) (point-at-bol)))))
2030 ;; Only `org' tables have contents. `table.el' tables
2031 ;; use a `:value' property to store raw table as
2033 :contents-begin
(and (eq type
'org
) table-begin
)
2034 :contents-end
(and (eq type
'org
) table-end
)
2035 :value
(and (eq type
'table.el
)
2036 (buffer-substring-no-properties
2037 table-begin table-end
))
2038 :post-blank
(count-lines pos-before-blank end
))
2039 (cadr keywords
))))))
2041 (defun org-element-table-interpreter (table contents
)
2042 "Interpret TABLE element as Org syntax.
2044 (if (eq (org-element-property :type table
) 'table.el
)
2045 (org-remove-indentation (org-element-property :value table
))
2046 (concat (with-temp-buffer (insert contents
)
2049 (mapconcat (lambda (fm) (concat "#+TBLFM: " fm
))
2050 (reverse (org-element-property :tblfm table
))
2056 (defun org-element-table-row-parser (limit)
2057 "Parse table row at point.
2059 LIMIT bounds the search.
2061 Return a list whose CAR is `table-row' and CDR is a plist
2062 containing `:begin', `:end', `:contents-begin', `:contents-end',
2063 `:type' and `:post-blank' keywords."
2065 (let* ((type (if (looking-at "^[ \t]*|-") 'rule
'standard
))
2067 ;; A table rule has no contents. In that case, ensure
2068 ;; CONTENTS-BEGIN matches CONTENTS-END.
2069 (contents-begin (and (eq type
'standard
)
2070 (search-forward "|")
2072 (contents-end (and (eq type
'standard
)
2075 (skip-chars-backward " \t")
2077 (end (progn (forward-line) (point))))
2082 :contents-begin contents-begin
2083 :contents-end contents-end
2086 (defun org-element-table-row-interpreter (table-row contents
)
2087 "Interpret TABLE-ROW element as Org syntax.
2088 CONTENTS is the contents of the table row."
2089 (if (eq (org-element-property :type table-row
) 'rule
) "|-"
2090 (concat "| " contents
)))
2095 (defun org-element-verse-block-parser (limit)
2096 "Parse a verse block.
2098 LIMIT bounds the search.
2100 Return a list whose CAR is `verse-block' and CDR is a plist
2101 containing `:begin', `:end', `:contents-begin', `:contents-end',
2102 `:hiddenp' and `:post-blank' keywords.
2104 Assume point is at beginning of the block."
2105 (let ((case-fold-search t
))
2106 (if (not (save-excursion
2107 (re-search-forward "^[ \t]*#\\+END_VERSE" limit t
)))
2108 ;; Incomplete block: parse it as a comment.
2109 (org-element-comment-parser limit
)
2110 (let ((contents-end (match-beginning 0)))
2112 (let* ((keywords (org-element--collect-affiliated-keywords))
2113 (begin (car keywords
))
2114 (hidden (progn (forward-line) (org-invisible-p2)))
2115 (contents-begin (point))
2116 (pos-before-blank (progn (goto-char contents-end
)
2119 (end (progn (skip-chars-forward " \r\t\n" limit
)
2120 (if (eobp) (point) (point-at-bol)))))
2125 :contents-begin contents-begin
2126 :contents-end contents-end
2128 :post-blank
(count-lines pos-before-blank end
))
2129 (cadr keywords
)))))))))
2131 (defun org-element-verse-block-interpreter (verse-block contents
)
2132 "Interpret VERSE-BLOCK element as Org syntax.
2133 CONTENTS is verse block contents."
2134 (format "#+BEGIN_VERSE\n%s#+END_VERSE" contents
))
2140 ;; Unlike to elements, interstices can be found between objects.
2141 ;; That's why, along with the parser, successor functions are provided
2142 ;; for each object. Some objects share the same successor (i.e. `code'
2143 ;; and `verbatim' objects).
2145 ;; A successor must accept a single argument bounding the search. It
2146 ;; will return either a cons cell whose CAR is the object's type, as
2147 ;; a symbol, and CDR the position of its next occurrence, or nil.
2149 ;; Successors follow the naming convention:
2150 ;; org-element-NAME-successor, where NAME is the name of the
2151 ;; successor, as defined in `org-element-all-successors'.
2153 ;; Some object types (i.e. `italic') are recursive. Restrictions on
2154 ;; object types they can contain will be specified in
2155 ;; `org-element-object-restrictions'.
2157 ;; Adding a new type of object is simple. Implement a successor,
2158 ;; a parser, and an interpreter for it, all following the naming
2159 ;; convention. Register type in `org-element-all-objects' and
2160 ;; successor in `org-element-all-successors'. Maybe tweak
2161 ;; restrictions about it, and that's it.
2166 (defun org-element-bold-parser ()
2167 "Parse bold object at point.
2169 Return a list whose CAR is `bold' and CDR is a plist with
2170 `:begin', `:end', `:contents-begin' and `:contents-end' and
2171 `:post-blank' keywords.
2173 Assume point is at the first star marker."
2175 (unless (bolp) (backward-char 1))
2176 (looking-at org-emph-re
)
2177 (let ((begin (match-beginning 2))
2178 (contents-begin (match-beginning 4))
2179 (contents-end (match-end 4))
2180 (post-blank (progn (goto-char (match-end 2))
2181 (skip-chars-forward " \t")))
2186 :contents-begin contents-begin
2187 :contents-end contents-end
2188 :post-blank post-blank
)))))
2190 (defun org-element-bold-interpreter (bold contents
)
2191 "Interpret BOLD object as Org syntax.
2192 CONTENTS is the contents of the object."
2193 (format "*%s*" contents
))
2195 (defun org-element-text-markup-successor (limit)
2196 "Search for the next text-markup object.
2198 LIMIT bounds the search.
2200 LIMIT bounds the search.
2202 Return value is a cons cell whose CAR is a symbol among `bold',
2203 `italic', `underline', `strike-through', `code' and `verbatim'
2204 and CDR is beginning position."
2206 (unless (bolp) (backward-char))
2207 (when (re-search-forward org-emph-re limit t
)
2208 (let ((marker (match-string 3)))
2210 ((equal marker
"*") 'bold
)
2211 ((equal marker
"/") 'italic
)
2212 ((equal marker
"_") 'underline
)
2213 ((equal marker
"+") 'strike-through
)
2214 ((equal marker
"~") 'code
)
2215 ((equal marker
"=") 'verbatim
)
2216 (t (error "Unknown marker at %d" (match-beginning 3))))
2217 (match-beginning 2))))))
2222 (defun org-element-code-parser ()
2223 "Parse code object at point.
2225 Return a list whose CAR is `code' and CDR is a plist with
2226 `:value', `:begin', `:end' and `:post-blank' keywords.
2228 Assume point is at the first tilde marker."
2230 (unless (bolp) (backward-char 1))
2231 (looking-at org-emph-re
)
2232 (let ((begin (match-beginning 2))
2233 (value (org-match-string-no-properties 4))
2234 (post-blank (progn (goto-char (match-end 2))
2235 (skip-chars-forward " \t")))
2241 :post-blank post-blank
)))))
2243 (defun org-element-code-interpreter (code contents
)
2244 "Interpret CODE object as Org syntax.
2246 (format "~%s~" (org-element-property :value code
)))
2251 (defun org-element-entity-parser ()
2252 "Parse entity at point.
2254 Return a list whose CAR is `entity' and CDR a plist with
2255 `:begin', `:end', `:latex', `:latex-math-p', `:html', `:latin1',
2256 `:utf-8', `:ascii', `:use-brackets-p' and `:post-blank' as
2259 Assume point is at the beginning of the entity."
2261 (looking-at "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)")
2262 (let* ((value (org-entity-get (match-string 1)))
2263 (begin (match-beginning 0))
2264 (bracketsp (string= (match-string 2) "{}"))
2265 (post-blank (progn (goto-char (match-end 1))
2266 (when bracketsp
(forward-char 2))
2267 (skip-chars-forward " \t")))
2270 (list :name
(car value
)
2271 :latex
(nth 1 value
)
2272 :latex-math-p
(nth 2 value
)
2274 :ascii
(nth 4 value
)
2275 :latin1
(nth 5 value
)
2276 :utf-8
(nth 6 value
)
2279 :use-brackets-p bracketsp
2280 :post-blank post-blank
)))))
2282 (defun org-element-entity-interpreter (entity contents
)
2283 "Interpret ENTITY object as Org syntax.
2286 (org-element-property :name entity
)
2287 (when (org-element-property :use-brackets-p entity
) "{}")))
2289 (defun org-element-latex-or-entity-successor (limit)
2290 "Search for the next latex-fragment or entity object.
2292 LIMIT bounds the search.
2294 Return value is a cons cell whose CAR is `entity' or
2295 `latex-fragment' and CDR is beginning position."
2297 (let ((matchers (plist-get org-format-latex-options
:matchers
))
2298 ;; ENTITY-RE matches both LaTeX commands and Org entities.
2300 "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))
2301 (when (re-search-forward
2302 (concat (mapconcat (lambda (e) (nth 1 (assoc e org-latex-regexps
)))
2306 (goto-char (match-beginning 0))
2307 (if (looking-at entity-re
)
2308 ;; Determine if it's a real entity or a LaTeX command.
2309 (cons (if (org-entity-get (match-string 1)) 'entity
'latex-fragment
)
2310 (match-beginning 0))
2311 ;; No entity nor command: point is at a LaTeX fragment.
2312 ;; Determine its type to get the correct beginning position.
2313 (cons 'latex-fragment
2316 (when (looking-at (nth 1 (assoc e org-latex-regexps
)))
2319 (nth 2 (assoc e org-latex-regexps
))))))
2326 (defun org-element-export-snippet-parser ()
2327 "Parse export snippet at point.
2329 Return a list whose CAR is `export-snippet' and CDR a plist with
2330 `:begin', `:end', `:back-end', `:value' and `:post-blank' as
2333 Assume point is at the beginning of the snippet."
2335 (re-search-forward "@@\\([-A-Za-z0-9]+\\):" nil t
)
2336 (let* ((begin (match-beginning 0))
2337 (back-end (org-match-string-no-properties 1))
2338 (value (buffer-substring-no-properties
2340 (progn (re-search-forward "@@" nil t
) (match-beginning 0))))
2341 (post-blank (skip-chars-forward " \t"))
2343 (list 'export-snippet
2344 (list :back-end back-end
2348 :post-blank post-blank
)))))
2350 (defun org-element-export-snippet-interpreter (export-snippet contents
)
2351 "Interpret EXPORT-SNIPPET object as Org syntax.
2354 (org-element-property :back-end export-snippet
)
2355 (org-element-property :value export-snippet
)))
2357 (defun org-element-export-snippet-successor (limit)
2358 "Search for the next export-snippet object.
2360 LIMIT bounds the search.
2362 Return value is a cons cell whose CAR is `export-snippet' and CDR
2363 its beginning position."
2366 (when (and (re-search-forward "@@[-A-Za-z0-9]+:" limit t
)
2367 (setq beg
(match-beginning 0))
2368 (re-search-forward "@@" limit t
))
2369 (cons 'export-snippet beg
)))))
2372 ;;;; Footnote Reference
2374 (defun org-element-footnote-reference-parser ()
2375 "Parse footnote reference at point.
2377 Return a list whose CAR is `footnote-reference' and CDR a plist
2378 with `:label', `:type', `:inline-definition', `:begin', `:end'
2379 and `:post-blank' as keywords."
2381 (looking-at org-footnote-re
)
2382 (let* ((begin (point))
2383 (label (or (org-match-string-no-properties 2)
2384 (org-match-string-no-properties 3)
2385 (and (match-string 1)
2386 (concat "fn:" (org-match-string-no-properties 1)))))
2387 (type (if (or (not label
) (match-string 1)) 'inline
'standard
))
2388 (inner-begin (match-end 0))
2392 (while (and (> count
0) (re-search-forward "[][]" nil t
))
2393 (if (equal (match-string 0) "[") (incf count
) (decf count
)))
2395 (post-blank (progn (goto-char (1+ inner-end
))
2396 (skip-chars-forward " \t")))
2399 (list 'footnote-reference
2404 :post-blank post-blank
))))
2405 (org-element-put-property
2406 footnote-reference
:inline-definition
2407 (and (eq type
'inline
)
2408 (org-element-parse-secondary-string
2409 (buffer-substring inner-begin inner-end
)
2410 (org-element-restriction 'footnote-reference
)
2411 footnote-reference
))))))
2413 (defun org-element-footnote-reference-interpreter (footnote-reference contents
)
2414 "Interpret FOOTNOTE-REFERENCE object as Org syntax.
2416 (let ((label (or (org-element-property :label footnote-reference
) "fn:"))
2419 (org-element-property :inline-definition footnote-reference
)))
2420 (if (not inline-def
) ""
2421 (concat ":" (org-element-interpret-data inline-def
))))))
2422 (format "[%s]" (concat label def
))))
2424 (defun org-element-footnote-reference-successor (limit)
2425 "Search for the next footnote-reference object.
2427 LIMIT bounds the search.
2429 Return value is a cons cell whose CAR is `footnote-reference' and
2430 CDR is beginning position."
2433 (while (re-search-forward org-footnote-re limit t
)
2435 (let ((beg (match-beginning 0))
2438 (while (re-search-forward "[][]" limit t
)
2439 (if (equal (match-string 0) "[") (incf count
) (decf count
))
2441 (throw 'exit
(cons 'footnote-reference beg
))))))))))
2444 ;;;; Inline Babel Call
2446 (defun org-element-inline-babel-call-parser ()
2447 "Parse inline babel call at point.
2449 Return a list whose CAR is `inline-babel-call' and CDR a plist
2450 with `:begin', `:end', `:info' and `:post-blank' as keywords.
2452 Assume point is at the beginning of the babel call."
2454 (unless (bolp) (backward-char))
2455 (looking-at org-babel-inline-lob-one-liner-regexp
)
2456 (let ((info (save-match-data (org-babel-lob-get-info)))
2457 (begin (match-end 1))
2458 (post-blank (progn (goto-char (match-end 0))
2459 (skip-chars-forward " \t")))
2461 (list 'inline-babel-call
2465 :post-blank post-blank
)))))
2467 (defun org-element-inline-babel-call-interpreter (inline-babel-call contents
)
2468 "Interpret INLINE-BABEL-CALL object as Org syntax.
2470 (let* ((babel-info (org-element-property :info inline-babel-call
))
2471 (main-source (car babel-info
))
2472 (post-options (nth 1 babel-info
)))
2474 (if (string-match "\\[\\(\\[.*?\\]\\)\\]" main-source
)
2475 ;; Remove redundant square brackets.
2477 (match-string 1 main-source
) nil nil main-source
)
2479 (and post-options
(format "[%s]" post-options
)))))
2481 (defun org-element-inline-babel-call-successor (limit)
2482 "Search for the next inline-babel-call object.
2484 LIMIT bounds the search.
2486 Return value is a cons cell whose CAR is `inline-babel-call' and
2487 CDR is beginning position."
2489 ;; Use a simplified version of
2490 ;; org-babel-inline-lob-one-liner-regexp as regexp for more speed.
2491 (when (re-search-forward
2492 "\\(?:babel\\|call\\)_\\([^()\n]+?\\)\\(\\[\\(.*\\)\\]\\|\\(\\)\\)(\\([^\n]*\\))\\(\\[\\(.*?\\)\\]\\)?"
2494 (cons 'inline-babel-call
(match-beginning 0)))))
2497 ;;;; Inline Src Block
2499 (defun org-element-inline-src-block-parser ()
2500 "Parse inline source block at point.
2502 LIMIT bounds the search.
2504 Return a list whose CAR is `inline-src-block' and CDR a plist
2505 with `:begin', `:end', `:language', `:value', `:parameters' and
2506 `:post-blank' as keywords.
2508 Assume point is at the beginning of the inline src block."
2510 (unless (bolp) (backward-char))
2511 (looking-at org-babel-inline-src-block-regexp
)
2512 (let ((begin (match-beginning 1))
2513 (language (org-match-string-no-properties 2))
2514 (parameters (org-match-string-no-properties 4))
2515 (value (org-match-string-no-properties 5))
2516 (post-blank (progn (goto-char (match-end 0))
2517 (skip-chars-forward " \t")))
2519 (list 'inline-src-block
2520 (list :language language
2522 :parameters parameters
2525 :post-blank post-blank
)))))
2527 (defun org-element-inline-src-block-interpreter (inline-src-block contents
)
2528 "Interpret INLINE-SRC-BLOCK object as Org syntax.
2530 (let ((language (org-element-property :language inline-src-block
))
2531 (arguments (org-element-property :parameters inline-src-block
))
2532 (body (org-element-property :value inline-src-block
)))
2533 (format "src_%s%s{%s}"
2535 (if arguments
(format "[%s]" arguments
) "")
2538 (defun org-element-inline-src-block-successor (limit)
2539 "Search for the next inline-babel-call element.
2541 LIMIT bounds the search.
2543 Return value is a cons cell whose CAR is `inline-babel-call' and
2544 CDR is beginning position."
2546 (when (re-search-forward org-babel-inline-src-block-regexp limit t
)
2547 (cons 'inline-src-block
(match-beginning 1)))))
2551 (defun org-element-italic-parser ()
2552 "Parse italic object at point.
2554 Return a list whose CAR is `italic' and CDR is a plist with
2555 `:begin', `:end', `:contents-begin' and `:contents-end' and
2556 `:post-blank' keywords.
2558 Assume point is at the first slash marker."
2560 (unless (bolp) (backward-char 1))
2561 (looking-at org-emph-re
)
2562 (let ((begin (match-beginning 2))
2563 (contents-begin (match-beginning 4))
2564 (contents-end (match-end 4))
2565 (post-blank (progn (goto-char (match-end 2))
2566 (skip-chars-forward " \t")))
2571 :contents-begin contents-begin
2572 :contents-end contents-end
2573 :post-blank post-blank
)))))
2575 (defun org-element-italic-interpreter (italic contents
)
2576 "Interpret ITALIC object as Org syntax.
2577 CONTENTS is the contents of the object."
2578 (format "/%s/" contents
))
2583 (defun org-element-latex-fragment-parser ()
2584 "Parse latex fragment at point.
2586 Return a list whose CAR is `latex-fragment' and CDR a plist with
2587 `:value', `:begin', `:end', and `:post-blank' as keywords.
2589 Assume point is at the beginning of the latex fragment."
2591 (let* ((begin (point))
2595 (let ((latex-regexp (nth 1 (assoc e org-latex-regexps
))))
2596 (when (or (looking-at latex-regexp
)
2600 (looking-at latex-regexp
))))
2601 (throw 'exit
(nth 2 (assoc e org-latex-regexps
))))))
2602 (plist-get org-format-latex-options
:matchers
))
2603 ;; None found: it's a macro.
2604 (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")
2606 (value (match-string-no-properties substring-match
))
2607 (post-blank (progn (goto-char (match-end substring-match
))
2608 (skip-chars-forward " \t")))
2610 (list 'latex-fragment
2614 :post-blank post-blank
)))))
2616 (defun org-element-latex-fragment-interpreter (latex-fragment contents
)
2617 "Interpret LATEX-FRAGMENT object as Org syntax.
2619 (org-element-property :value latex-fragment
))
2623 (defun org-element-line-break-parser ()
2624 "Parse line break at point.
2626 Return a list whose CAR is `line-break', and CDR a plist with
2627 `:begin', `:end' and `:post-blank' keywords.
2629 Assume point is at the beginning of the line break."
2630 (list 'line-break
(list :begin
(point) :end
(point-at-eol) :post-blank
0)))
2632 (defun org-element-line-break-interpreter (line-break contents
)
2633 "Interpret LINE-BREAK object as Org syntax.
2637 (defun org-element-line-break-successor (limit)
2638 "Search for the next line-break object.
2640 LIMIT bounds the search.
2642 Return value is a cons cell whose CAR is `line-break' and CDR is
2643 beginning position."
2645 (let ((beg (and (re-search-forward "[^\\\\]\\(\\\\\\\\\\)[ \t]*$" limit t
)
2646 (goto-char (match-beginning 1)))))
2647 ;; A line break can only happen on a non-empty line.
2648 (when (and beg
(re-search-backward "\\S-" (point-at-bol) t
))
2649 (cons 'line-break beg
)))))
2654 (defun org-element-link-parser ()
2655 "Parse link at point.
2657 Return a list whose CAR is `link' and CDR a plist with `:type',
2658 `:path', `:raw-link', `:begin', `:end', `:contents-begin',
2659 `:contents-end' and `:post-blank' as keywords.
2661 Assume point is at the beginning of the link."
2663 (let ((begin (point))
2664 end contents-begin contents-end link-end post-blank path type
2667 ;; Type 1: Text targeted from a radio target.
2668 ((and org-target-link-regexp
(looking-at org-target-link-regexp
))
2670 link-end
(match-end 0)
2671 path
(org-match-string-no-properties 0)))
2672 ;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]]
2673 ((looking-at org-bracket-link-regexp
)
2674 (setq contents-begin
(match-beginning 3)
2675 contents-end
(match-end 3)
2676 link-end
(match-end 0)
2677 ;; RAW-LINK is the original link.
2678 raw-link
(org-match-string-no-properties 1)
2679 link
(org-translate-link
2680 (org-link-expand-abbrev
2681 (org-link-unescape raw-link
))))
2682 ;; Determine TYPE of link and set PATH accordingly.
2685 ((or (file-name-absolute-p link
) (string-match "^\\.\\.?/" link
))
2686 (setq type
"file" path link
))
2687 ;; Explicit type (http, irc, bbdb...). See `org-link-types'.
2688 ((string-match org-link-re-with-space3 link
)
2689 (setq type
(match-string 1 link
) path
(match-string 2 link
)))
2690 ;; Id type: PATH is the id.
2691 ((string-match "^id:\\([-a-f0-9]+\\)" link
)
2692 (setq type
"id" path
(match-string 1 link
)))
2693 ;; Code-ref type: PATH is the name of the reference.
2694 ((string-match "^(\\(.*\\))$" link
)
2695 (setq type
"coderef" path
(match-string 1 link
)))
2696 ;; Custom-id type: PATH is the name of the custom id.
2697 ((= (aref link
0) ?
#)
2698 (setq type
"custom-id" path
(substring link
1)))
2699 ;; Fuzzy type: Internal link either matches a target, an
2700 ;; headline name or nothing. PATH is the target or
2702 (t (setq type
"fuzzy" path link
))))
2703 ;; Type 3: Plain link, i.e. http://orgmode.org
2704 ((looking-at org-plain-link-re
)
2705 (setq raw-link
(org-match-string-no-properties 0)
2706 type
(org-match-string-no-properties 1)
2707 path
(org-match-string-no-properties 2)
2708 link-end
(match-end 0)))
2709 ;; Type 4: Angular link, i.e. <http://orgmode.org>
2710 ((looking-at org-angle-link-re
)
2711 (setq raw-link
(buffer-substring-no-properties
2712 (match-beginning 1) (match-end 2))
2713 type
(org-match-string-no-properties 1)
2714 path
(org-match-string-no-properties 2)
2715 link-end
(match-end 0))))
2716 ;; In any case, deduce end point after trailing white space from
2717 ;; LINK-END variable.
2718 (setq post-blank
(progn (goto-char link-end
) (skip-chars-forward " \t"))
2723 :raw-link
(or raw-link path
)
2726 :contents-begin contents-begin
2727 :contents-end contents-end
2728 :post-blank post-blank
)))))
2730 (defun org-element-link-interpreter (link contents
)
2731 "Interpret LINK object as Org syntax.
2732 CONTENTS is the contents of the object, or nil."
2733 (let ((type (org-element-property :type link
))
2734 (raw-link (org-element-property :raw-link link
)))
2735 (if (string= type
"radio") raw-link
2738 (if contents
(format "[%s]" contents
) "")))))
2740 (defun org-element-link-successor (limit)
2741 "Search for the next link object.
2743 LIMIT bounds the search.
2745 Return value is a cons cell whose CAR is `link' and CDR is
2746 beginning position."
2749 (if (not org-target-link-regexp
) org-any-link-re
2750 (concat org-any-link-re
"\\|" org-target-link-regexp
))))
2751 (when (re-search-forward link-regexp limit t
)
2752 (cons 'link
(match-beginning 0))))))
2757 (defun org-element-macro-parser ()
2758 "Parse macro at point.
2760 Return a list whose CAR is `macro' and CDR a plist with `:key',
2761 `:args', `:begin', `:end', `:value' and `:post-blank' as
2764 Assume point is at the macro."
2766 (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
2767 (let ((begin (point))
2768 (key (downcase (org-match-string-no-properties 1)))
2769 (value (org-match-string-no-properties 0))
2770 (post-blank (progn (goto-char (match-end 0))
2771 (skip-chars-forward " \t")))
2773 (args (let ((args (org-match-string-no-properties 3)) args2
)
2775 (setq args
(org-split-string args
","))
2777 (while (string-match "\\\\\\'" (car args
))
2778 ;; Repair bad splits.
2779 (setcar (cdr args
) (concat (substring (car args
) 0 -
1)
2782 (push (pop args
) args2
))
2783 (mapcar 'org-trim
(nreverse args2
))))))
2790 :post-blank post-blank
)))))
2792 (defun org-element-macro-interpreter (macro contents
)
2793 "Interpret MACRO object as Org syntax.
2795 (org-element-property :value macro
))
2797 (defun org-element-macro-successor (limit)
2798 "Search for the next macro object.
2800 LIMIT bounds the search.
2802 Return value is cons cell whose CAR is `macro' and CDR is
2803 beginning position."
2805 (when (re-search-forward
2806 "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
2808 (cons 'macro
(match-beginning 0)))))
2813 (defun org-element-radio-target-parser ()
2814 "Parse radio target at point.
2816 Return a list whose CAR is `radio-target' and CDR a plist with
2817 `:begin', `:end', `:contents-begin', `:contents-end', `:value'
2818 and `:post-blank' as keywords.
2820 Assume point is at the radio target."
2822 (looking-at org-radio-target-regexp
)
2823 (let ((begin (point))
2824 (contents-begin (match-beginning 1))
2825 (contents-end (match-end 1))
2826 (value (org-match-string-no-properties 1))
2827 (post-blank (progn (goto-char (match-end 0))
2828 (skip-chars-forward " \t")))
2833 :contents-begin contents-begin
2834 :contents-end contents-end
2835 :post-blank post-blank
2838 (defun org-element-radio-target-interpreter (target contents
)
2839 "Interpret TARGET object as Org syntax.
2840 CONTENTS is the contents of the object."
2841 (concat "<<<" contents
">>>"))
2843 (defun org-element-radio-target-successor (limit)
2844 "Search for the next radio-target object.
2846 LIMIT bounds the search.
2848 Return value is a cons cell whose CAR is `radio-target' and CDR
2849 is beginning position."
2851 (when (re-search-forward org-radio-target-regexp limit t
)
2852 (cons 'radio-target
(match-beginning 0)))))
2855 ;;;; Statistics Cookie
2857 (defun org-element-statistics-cookie-parser ()
2858 "Parse statistics cookie at point.
2860 Return a list whose CAR is `statistics-cookie', and CDR a plist
2861 with `:begin', `:end', `:value' and `:post-blank' keywords.
2863 Assume point is at the beginning of the statistics-cookie."
2865 (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
2866 (let* ((begin (point))
2867 (value (buffer-substring-no-properties
2868 (match-beginning 0) (match-end 0)))
2869 (post-blank (progn (goto-char (match-end 0))
2870 (skip-chars-forward " \t")))
2872 (list 'statistics-cookie
2876 :post-blank post-blank
)))))
2878 (defun org-element-statistics-cookie-interpreter (statistics-cookie contents
)
2879 "Interpret STATISTICS-COOKIE object as Org syntax.
2881 (org-element-property :value statistics-cookie
))
2883 (defun org-element-statistics-cookie-successor (limit)
2884 "Search for the next statistics cookie object.
2886 LIMIT bounds the search.
2888 Return value is a cons cell whose CAR is `statistics-cookie' and
2889 CDR is beginning position."
2891 (when (re-search-forward "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]" limit t
)
2892 (cons 'statistics-cookie
(match-beginning 0)))))
2897 (defun org-element-strike-through-parser ()
2898 "Parse strike-through object at point.
2900 Return a list whose CAR is `strike-through' and CDR is a plist
2901 with `:begin', `:end', `:contents-begin' and `:contents-end' and
2902 `:post-blank' keywords.
2904 Assume point is at the first plus sign marker."
2906 (unless (bolp) (backward-char 1))
2907 (looking-at org-emph-re
)
2908 (let ((begin (match-beginning 2))
2909 (contents-begin (match-beginning 4))
2910 (contents-end (match-end 4))
2911 (post-blank (progn (goto-char (match-end 2))
2912 (skip-chars-forward " \t")))
2914 (list 'strike-through
2917 :contents-begin contents-begin
2918 :contents-end contents-end
2919 :post-blank post-blank
)))))
2921 (defun org-element-strike-through-interpreter (strike-through contents
)
2922 "Interpret STRIKE-THROUGH object as Org syntax.
2923 CONTENTS is the contents of the object."
2924 (format "+%s+" contents
))
2929 (defun org-element-subscript-parser ()
2930 "Parse subscript at point.
2932 Return a list whose CAR is `subscript' and CDR a plist with
2933 `:begin', `:end', `:contents-begin', `:contents-end',
2934 `:use-brackets-p' and `:post-blank' as keywords.
2936 Assume point is at the underscore."
2938 (unless (bolp) (backward-char))
2939 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp
)
2941 (not (looking-at org-match-substring-regexp
))))
2942 (begin (match-beginning 2))
2943 (contents-begin (or (match-beginning 5)
2944 (match-beginning 3)))
2945 (contents-end (or (match-end 5) (match-end 3)))
2946 (post-blank (progn (goto-char (match-end 0))
2947 (skip-chars-forward " \t")))
2952 :use-brackets-p bracketsp
2953 :contents-begin contents-begin
2954 :contents-end contents-end
2955 :post-blank post-blank
)))))
2957 (defun org-element-subscript-interpreter (subscript contents
)
2958 "Interpret SUBSCRIPT object as Org syntax.
2959 CONTENTS is the contents of the object."
2961 (if (org-element-property :use-brackets-p subscript
) "_{%s}" "_%s")
2964 (defun org-element-sub/superscript-successor
(limit)
2965 "Search for the next sub/superscript object.
2967 LIMIT bounds the search.
2969 Return value is a cons cell whose CAR is either `subscript' or
2970 `superscript' and CDR is beginning position."
2972 (when (re-search-forward org-match-substring-regexp limit t
)
2973 (cons (if (string= (match-string 2) "_") 'subscript
'superscript
)
2974 (match-beginning 2)))))
2979 (defun org-element-superscript-parser ()
2980 "Parse superscript at point.
2982 Return a list whose CAR is `superscript' and CDR a plist with
2983 `:begin', `:end', `:contents-begin', `:contents-end',
2984 `:use-brackets-p' and `:post-blank' as keywords.
2986 Assume point is at the caret."
2988 (unless (bolp) (backward-char))
2989 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp
) t
2990 (not (looking-at org-match-substring-regexp
))))
2991 (begin (match-beginning 2))
2992 (contents-begin (or (match-beginning 5)
2993 (match-beginning 3)))
2994 (contents-end (or (match-end 5) (match-end 3)))
2995 (post-blank (progn (goto-char (match-end 0))
2996 (skip-chars-forward " \t")))
3001 :use-brackets-p bracketsp
3002 :contents-begin contents-begin
3003 :contents-end contents-end
3004 :post-blank post-blank
)))))
3006 (defun org-element-superscript-interpreter (superscript contents
)
3007 "Interpret SUPERSCRIPT object as Org syntax.
3008 CONTENTS is the contents of the object."
3010 (if (org-element-property :use-brackets-p superscript
) "^{%s}" "^%s")
3016 (defun org-element-table-cell-parser ()
3017 "Parse table cell at point.
3019 Return a list whose CAR is `table-cell' and CDR is a plist
3020 containing `:begin', `:end', `:contents-begin', `:contents-end'
3021 and `:post-blank' keywords."
3022 (looking-at "[ \t]*\\(.*?\\)[ \t]*|")
3023 (let* ((begin (match-beginning 0))
3025 (contents-begin (match-beginning 1))
3026 (contents-end (match-end 1)))
3030 :contents-begin contents-begin
3031 :contents-end contents-end
3034 (defun org-element-table-cell-interpreter (table-cell contents
)
3035 "Interpret TABLE-CELL element as Org syntax.
3036 CONTENTS is the contents of the cell, or nil."
3037 (concat " " contents
" |"))
3039 (defun org-element-table-cell-successor (limit)
3040 "Search for the next table-cell object.
3042 LIMIT bounds the search.
3044 Return value is a cons cell whose CAR is `table-cell' and CDR is
3045 beginning position."
3046 (when (looking-at "[ \t]*.*?[ \t]+|") (cons 'table-cell
(point))))
3051 (defun org-element-target-parser ()
3052 "Parse target at point.
3054 Return a list whose CAR is `target' and CDR a plist with
3055 `:begin', `:end', `:value' and `:post-blank' as keywords.
3057 Assume point is at the target."
3059 (looking-at org-target-regexp
)
3060 (let ((begin (point))
3061 (value (org-match-string-no-properties 1))
3062 (post-blank (progn (goto-char (match-end 0))
3063 (skip-chars-forward " \t")))
3069 :post-blank post-blank
)))))
3071 (defun org-element-target-interpreter (target contents
)
3072 "Interpret TARGET object as Org syntax.
3074 (format "<<%s>>" (org-element-property :value target
)))
3076 (defun org-element-target-successor (limit)
3077 "Search for the next target object.
3079 LIMIT bounds the search.
3081 Return value is a cons cell whose CAR is `target' and CDR is
3082 beginning position."
3084 (when (re-search-forward org-target-regexp limit t
)
3085 (cons 'target
(match-beginning 0)))))
3090 (defun org-element-timestamp-parser ()
3091 "Parse time stamp at point.
3093 Return a list whose CAR is `timestamp', and CDR a plist with
3094 `:type', `:begin', `:end', `:value' and `:post-blank' keywords.
3096 Assume point is at the beginning of the timestamp."
3098 (let* ((begin (point))
3099 (activep (eq (char-after) ?
<))
3102 (looking-at "[<[]\\(\\(%%\\)?.*?\\)[]>]\\(?:--[<[]\\(.*?\\)[]>]\\)?")
3103 (match-string-no-properties 1)))
3104 (range-end (match-string-no-properties 3))
3105 (type (cond ((match-string 2) 'diary
)
3106 ((and activep range-end
) 'active-range
)
3108 (range-end 'inactive-range
)
3110 (post-blank (progn (goto-char (match-end 0))
3111 (skip-chars-forward " \t")))
3116 :range-end range-end
3119 :post-blank post-blank
)))))
3121 (defun org-element-timestamp-interpreter (timestamp contents
)
3122 "Interpret TIMESTAMP object as Org syntax.
3124 (let ((type (org-element-property :type timestamp
) ))
3126 (format (if (memq type
'(inactive inactive-range
)) "[%s]" "<%s>")
3127 (org-element-property :value timestamp
))
3128 (let ((range-end (org-element-property :range-end timestamp
)))
3131 (format (if (eq type
'inactive-range
) "[%s]" "<%s>")
3134 (defun org-element-timestamp-successor (limit)
3135 "Search for the next timestamp object.
3137 LIMIT bounds the search.
3139 Return value is a cons cell whose CAR is `timestamp' and CDR is
3140 beginning position."
3142 (when (re-search-forward
3143 (concat org-ts-regexp-both
3145 "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
3147 "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
3149 (cons 'timestamp
(match-beginning 0)))))
3154 (defun org-element-underline-parser ()
3155 "Parse underline object at point.
3157 Return a list whose CAR is `underline' and CDR is a plist with
3158 `:begin', `:end', `:contents-begin' and `:contents-end' and
3159 `:post-blank' keywords.
3161 Assume point is at the first underscore marker."
3163 (unless (bolp) (backward-char 1))
3164 (looking-at org-emph-re
)
3165 (let ((begin (match-beginning 2))
3166 (contents-begin (match-beginning 4))
3167 (contents-end (match-end 4))
3168 (post-blank (progn (goto-char (match-end 2))
3169 (skip-chars-forward " \t")))
3174 :contents-begin contents-begin
3175 :contents-end contents-end
3176 :post-blank post-blank
)))))
3178 (defun org-element-underline-interpreter (underline contents
)
3179 "Interpret UNDERLINE object as Org syntax.
3180 CONTENTS is the contents of the object."
3181 (format "_%s_" contents
))
3186 (defun org-element-verbatim-parser ()
3187 "Parse verbatim object at point.
3189 Return a list whose CAR is `verbatim' and CDR is a plist with
3190 `:value', `:begin', `:end' and `:post-blank' keywords.
3192 Assume point is at the first equal sign marker."
3194 (unless (bolp) (backward-char 1))
3195 (looking-at org-emph-re
)
3196 (let ((begin (match-beginning 2))
3197 (value (org-match-string-no-properties 4))
3198 (post-blank (progn (goto-char (match-end 2))
3199 (skip-chars-forward " \t")))
3205 :post-blank post-blank
)))))
3207 (defun org-element-verbatim-interpreter (verbatim contents
)
3208 "Interpret VERBATIM object as Org syntax.
3210 (format "=%s=" (org-element-property :value verbatim
)))
3214 ;;; Parsing Element Starting At Point
3216 ;; `org-element--current-element' is the core function of this section.
3217 ;; It returns the Lisp representation of the element starting at
3220 ;; `org-element--current-element' makes use of special modes. They
3221 ;; are activated for fixed element chaining (i.e. `plain-list' >
3222 ;; `item') or fixed conditional element chaining (i.e. `headline' >
3223 ;; `section'). Special modes are: `first-section', `section',
3224 ;; `quote-section', `item' and `table-row'.
3226 (defun org-element--current-element
3227 (limit &optional granularity special structure
)
3228 "Parse the element starting at point.
3230 LIMIT bounds the search.
3232 Return value is a list like (TYPE PROPS) where TYPE is the type
3233 of the element and PROPS a plist of properties associated to the
3236 Possible types are defined in `org-element-all-elements'.
3238 Optional argument GRANULARITY determines the depth of the
3239 recursion. Allowed values are `headline', `greater-element',
3240 `element', `object' or nil. When it is broader than `object' (or
3241 nil), secondary values will not be parsed, since they only
3244 Optional argument SPECIAL, when non-nil, can be either
3245 `first-section', `section', `quote-section', `table-row' and
3248 If STRUCTURE isn't provided but SPECIAL is set to `item', it will
3251 This function assumes point is always at the beginning of the
3252 element it has to parse."
3254 ;; If point is at an affiliated keyword, try moving to the
3255 ;; beginning of the associated element. If none is found, the
3256 ;; keyword is orphaned and will be treated as plain text.
3257 (when (looking-at org-element--affiliated-re
)
3258 (let ((opoint (point)))
3259 (while (looking-at org-element--affiliated-re
) (forward-line))
3260 (when (looking-at "[ \t]*$") (goto-char opoint
))))
3261 (let ((case-fold-search t
)
3262 ;; Determine if parsing depth allows for secondary strings
3263 ;; parsing. It only applies to elements referenced in
3264 ;; `org-element-secondary-value-alist'.
3265 (raw-secondary-p (and granularity
(not (eq granularity
'object
)))))
3269 (org-element-item-parser limit structure raw-secondary-p
))
3271 ((eq special
'quote-section
) (org-element-quote-section-parser limit
))
3273 ((eq special
'table-row
) (org-element-table-row-parser limit
))
3275 ((org-with-limited-levels (org-at-heading-p))
3276 (org-element-headline-parser limit raw-secondary-p
))
3277 ;; Section (must be checked after headline).
3278 ((eq special
'section
) (org-element-section-parser limit
))
3279 ((eq special
'first-section
)
3280 (org-element-section-parser
3281 (or (save-excursion (org-with-limited-levels (outline-next-heading)))
3283 ;; Planning and Clock.
3284 ((and (looking-at org-planning-or-clock-line-re
))
3285 (if (equal (match-string 1) org-clock-string
)
3286 (org-element-clock-parser limit
)
3287 (org-element-planning-parser limit
)))
3290 (org-element-inlinetask-parser limit raw-secondary-p
))
3291 ;; LaTeX Environment.
3292 ((looking-at "[ \t]*\\\\begin{\\([A-Za-z0-9*]+\\)}")
3295 (format "[ \t]*\\\\end{%s}[ \t]*"
3296 (regexp-quote (match-string 1)))
3298 (org-element-latex-environment-parser limit
)
3299 (org-element-paragraph-parser limit
)))
3300 ;; Drawer and Property Drawer.
3301 ((looking-at org-drawer-regexp
)
3302 (let ((name (match-string 1)))
3304 ((not (save-excursion
3305 (re-search-forward "^[ \t]*:END:[ \t]*$" nil t
)))
3306 (org-element-paragraph-parser limit
))
3307 ((equal "PROPERTIES" name
)
3308 (org-element-property-drawer-parser limit
))
3309 (t (org-element-drawer-parser limit
)))))
3311 ((looking-at "[ \t]*:\\( \\|$\\)")
3312 (org-element-fixed-width-parser limit
))
3313 ;; Inline Comments, Blocks, Babel Calls, Dynamic Blocks and
3315 ((looking-at "[ \t]*#")
3316 (goto-char (match-end 0))
3317 (cond ((looking-at "\\+BEGIN_\\(\\S-+\\)")
3319 (let ((parser (assoc (upcase (match-string 1))
3320 org-element-block-name-alist
)))
3321 (if parser
(funcall (cdr parser
) limit
)
3322 (org-element-special-block-parser limit
))))
3323 ((looking-at "\\+CALL")
3325 (org-element-babel-call-parser limit
))
3326 ((looking-at "\\+BEGIN:? ")
3328 (org-element-dynamic-block-parser limit
))
3329 ((looking-at "\\+\\S-+:")
3331 (org-element-keyword-parser limit
))
3334 (org-element-comment-parser limit
))))
3335 ;; Footnote Definition.
3336 ((looking-at org-footnote-definition-re
)
3337 (org-element-footnote-definition-parser limit
))
3339 ((looking-at "[ \t]*-\\{5,\\}[ \t]*$")
3340 (org-element-horizontal-rule-parser limit
))
3342 ((org-at-table-p t
) (org-element-table-parser limit
))
3344 ((looking-at (org-item-re))
3345 (org-element-plain-list-parser limit
(or structure
(org-list-struct))))
3346 ;; Default element: Paragraph.
3347 (t (org-element-paragraph-parser limit
))))))
3350 ;; Most elements can have affiliated keywords. When looking for an
3351 ;; element beginning, we want to move before them, as they belong to
3352 ;; that element, and, in the meantime, collect information they give
3353 ;; into appropriate properties. Hence the following function.
3355 ;; Usage of optional arguments may not be obvious at first glance:
3357 ;; - TRANS-LIST is used to polish keywords names that have evolved
3358 ;; during Org history. In example, even though =result= and
3359 ;; =results= coexist, we want to have them under the same =result=
3360 ;; property. It's also true for "srcname" and "name", where the
3361 ;; latter seems to be preferred nowadays (thus the "name" property).
3363 ;; - CONSED allows to regroup multi-lines keywords under the same
3364 ;; property, while preserving their own identity. This is mostly
3365 ;; used for "attr_latex" and al.
3367 ;; - PARSED prepares a keyword value for export. This is useful for
3368 ;; "caption". Objects restrictions for such keywords are defined in
3369 ;; `org-element-object-restrictions'.
3371 ;; - DUALS is used to take care of keywords accepting a main and an
3372 ;; optional secondary values. For example "results" has its
3373 ;; source's name as the main value, and may have an hash string in
3374 ;; optional square brackets as the secondary one.
3376 ;; A keyword may belong to more than one category.
3378 (defun org-element--collect-affiliated-keywords
3379 (&optional key-re trans-list consed parsed duals
)
3380 "Collect affiliated keywords before point.
3382 Optional argument KEY-RE is a regexp matching keywords, which
3383 puts matched keyword in group 1. It defaults to
3384 `org-element--affiliated-re'.
3386 TRANS-LIST is an alist where key is the keyword and value the
3387 property name it should be translated to, without the colons. It
3388 defaults to `org-element-keyword-translation-alist'.
3390 CONSED is a list of strings. Any keyword belonging to that list
3391 will have its value consed. The check is done after keyword
3392 translation. It defaults to `org-element-multiple-keywords'.
3394 PARSED is a list of strings. Any keyword member of this list
3395 will have its value parsed. The check is done after keyword
3396 translation. If a keyword is a member of both CONSED and PARSED,
3397 it's value will be a list of parsed strings. It defaults to
3398 `org-element-parsed-keywords'.
3400 DUALS is a list of strings. Any keyword member of this list can
3401 have two parts: one mandatory and one optional. Its value is
3402 a cons cell whose CAR is the former, and the CDR the latter. If
3403 a keyword is a member of both PARSED and DUALS, both values will
3404 be parsed. It defaults to `org-element-dual-keywords'.
3406 Return a list whose CAR is the position at the first of them and
3407 CDR a plist of keywords and values."
3409 (let ((case-fold-search t
)
3410 (key-re (or key-re org-element--affiliated-re
))
3411 (trans-list (or trans-list org-element-keyword-translation-alist
))
3412 (consed (or consed org-element-multiple-keywords
))
3413 (parsed (or parsed org-element-parsed-keywords
))
3414 (duals (or duals org-element-dual-keywords
))
3415 ;; RESTRICT is the list of objects allowed in parsed
3417 (restrict (org-element-restriction 'keyword
))
3420 (while (and (not (bobp)) (progn (forward-line -
1) (looking-at key-re
)))
3421 (let* ((raw-kwd (upcase (or (match-string 2) (match-string 1))))
3422 ;; Apply translation to RAW-KWD. From there, KWD is
3423 ;; the official keyword.
3424 (kwd (or (cdr (assoc raw-kwd trans-list
)) raw-kwd
))
3425 ;; Find main value for any keyword.
3429 (buffer-substring-no-properties
3430 (match-end 0) (point-at-eol)))))
3431 ;; If KWD is a dual keyword, find its secondary
3432 ;; value. Maybe parse it.
3434 (and (member kwd duals
)
3435 (let ((sec (org-match-string-no-properties 3)))
3436 (if (or (not sec
) (not (member kwd parsed
))) sec
3437 (org-element-parse-secondary-string sec restrict
)))))
3438 ;; Attribute a property name to KWD.
3439 (kwd-sym (and kwd
(intern (concat ":" (downcase kwd
))))))
3440 ;; Now set final shape for VALUE.
3441 (when (member kwd parsed
)
3442 (setq value
(org-element-parse-secondary-string value restrict
)))
3443 (when (member kwd duals
)
3444 ;; VALUE is mandatory. Set it to nil if there is none.
3445 (setq value
(and value
(cons value dual-value
))))
3446 ;; Attributes are always consed.
3447 (when (or (member kwd consed
) (string-match "^ATTR_" kwd
))
3448 (setq value
(cons value
(plist-get output kwd-sym
))))
3449 ;; Eventually store the new value in OUTPUT.
3450 (setq output
(plist-put output kwd-sym value
))))
3451 (unless (looking-at key-re
) (forward-line 1)))
3452 (list (point) output
))))
3458 ;; The two major functions here are `org-element-parse-buffer', which
3459 ;; parses Org syntax inside the current buffer, taking into account
3460 ;; region, narrowing, or even visibility if specified, and
3461 ;; `org-element-parse-secondary-string', which parses objects within
3464 ;; The (almost) almighty `org-element-map' allows to apply a function
3465 ;; on elements or objects matching some type, and accumulate the
3466 ;; resulting values. In an export situation, it also skips unneeded
3467 ;; parts of the parse tree.
3469 (defun org-element-parse-buffer (&optional granularity visible-only
)
3470 "Recursively parse the buffer and return structure.
3471 If narrowing is in effect, only parse the visible part of the
3474 Optional argument GRANULARITY determines the depth of the
3475 recursion. It can be set to the following symbols:
3477 `headline' Only parse headlines.
3478 `greater-element' Don't recurse into greater elements excepted
3479 headlines and sections. Thus, elements
3480 parsed are the top-level ones.
3481 `element' Parse everything but objects and plain text.
3482 `object' Parse the complete buffer (default).
3484 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3487 Assume buffer is in Org mode."
3489 (goto-char (point-min))
3490 (org-skip-whitespace)
3491 (org-element--parse-elements
3492 (point-at-bol) (point-max)
3493 ;; Start in `first-section' mode so text before the first
3494 ;; headline belongs to a section.
3495 'first-section nil granularity visible-only
(list 'org-data nil
))))
3497 (defun org-element-parse-secondary-string (string restriction
&optional parent
)
3498 "Recursively parse objects in STRING and return structure.
3500 RESTRICTION is a symbol limiting the object types that will be
3503 Optional argument PARENT, when non-nil, is the element or object
3504 containing the secondary string. It is used to set correctly
3505 `:parent' property within the string."
3508 (let ((secondary (org-element--parse-objects
3509 (point-min) (point-max) nil restriction
)))
3510 (mapc (lambda (obj) (org-element-put-property obj
:parent parent
))
3513 (defun org-element-map (data types fun
&optional info first-match no-recursion
)
3514 "Map a function on selected elements or objects.
3516 DATA is the parsed tree, as returned by, i.e,
3517 `org-element-parse-buffer'. TYPES is a symbol or list of symbols
3518 of elements or objects types. FUN is the function called on the
3519 matching element or object. It must accept one arguments: the
3520 element or object itself.
3522 When optional argument INFO is non-nil, it should be a plist
3523 holding export options. In that case, parts of the parse tree
3524 not exportable according to that property list will be skipped.
3526 When optional argument FIRST-MATCH is non-nil, stop at the first
3527 match for which FUN doesn't return nil, and return that value.
3529 Optional argument NO-RECURSION is a symbol or a list of symbols
3530 representing elements or objects types. `org-element-map' won't
3531 enter any recursive element or object whose type belongs to that
3532 list. Though, FUN can still be applied on them.
3534 Nil values returned from FUN do not appear in the results."
3535 ;; Ensure TYPES and NO-RECURSION are a list, even of one element.
3536 (unless (listp types
) (setq types
(list types
)))
3537 (unless (listp no-recursion
) (setq no-recursion
(list no-recursion
)))
3538 ;; Recursion depth is determined by --CATEGORY.
3541 (let ((category 'greater-elements
))
3542 (mapc (lambda (type)
3543 (cond ((or (memq type org-element-all-objects
)
3544 (eq type
'plain-text
))
3545 ;; If one object is found, the function
3546 ;; has to recurse into every object.
3547 (throw 'found
'objects
))
3548 ((not (memq type org-element-greater-elements
))
3549 ;; If one regular element is found, the
3550 ;; function has to recurse, at lest, into
3551 ;; every element it encounters.
3552 (and (not (eq category
'elements
))
3553 (setq category
'elements
)))))
3561 ;; Recursively walk DATA. INFO, if non-nil, is a plist
3562 ;; holding contextual information.
3563 (let ((--type (org-element-type --data
)))
3566 ;; Ignored element in an export context.
3567 ((and info
(memq --data
(plist-get info
:ignore-list
))))
3568 ;; Secondary string: only objects can be found there.
3570 (when (eq --category
'objects
) (mapc --walk-tree --data
)))
3571 ;; Unconditionally enter parse trees.
3572 ((eq --type
'org-data
)
3573 (mapc --walk-tree
(org-element-contents --data
)))
3575 ;; Check if TYPE is matching among TYPES. If so,
3576 ;; apply FUN to --DATA and accumulate return value
3577 ;; into --ACC (or exit if FIRST-MATCH is non-nil).
3578 (when (memq --type types
)
3579 (let ((result (funcall fun --data
)))
3580 (cond ((not result
))
3581 (first-match (throw '--map-first-match result
))
3582 (t (push result --acc
)))))
3583 ;; If --DATA has a secondary string that can contain
3584 ;; objects with their type among TYPES, look into it.
3585 (when (eq --category
'objects
)
3587 (assq --type org-element-secondary-value-alist
)))
3589 (funcall --walk-tree
3590 (org-element-property (cdr sec-prop
) --data
)))))
3591 ;; Determine if a recursion into --DATA is possible.
3593 ;; --TYPE is explicitly removed from recursion.
3594 ((memq --type no-recursion
))
3595 ;; --DATA has no contents.
3596 ((not (org-element-contents --data
)))
3597 ;; Looking for greater elements but --DATA is simply
3598 ;; an element or an object.
3599 ((and (eq --category
'greater-elements
)
3600 (not (memq --type org-element-greater-elements
))))
3601 ;; Looking for elements but --DATA is an object.
3602 ((and (eq --category
'elements
)
3603 (memq --type org-element-all-objects
)))
3604 ;; In any other case, map contents.
3605 (t (mapc --walk-tree
(org-element-contents --data
)))))))))))
3606 (catch '--map-first-match
3607 (funcall --walk-tree data
)
3608 ;; Return value in a proper order.
3611 ;; The following functions are internal parts of the parser.
3613 ;; The first one, `org-element--parse-elements' acts at the element's
3616 ;; The second one, `org-element--parse-objects' applies on all objects
3617 ;; of a paragraph or a secondary string. It uses
3618 ;; `org-element--get-next-object-candidates' to optimize the search of
3619 ;; the next object in the buffer.
3621 ;; More precisely, that function looks for every allowed object type
3622 ;; first. Then, it discards failed searches, keeps further matches,
3623 ;; and searches again types matched behind point, for subsequent
3624 ;; calls. Thus, searching for a given type fails only once, and every
3625 ;; object is searched only once at top level (but sometimes more for
3628 (defun org-element--parse-elements
3629 (beg end special structure granularity visible-only acc
)
3630 "Parse elements between BEG and END positions.
3632 SPECIAL prioritize some elements over the others. It can be set
3633 to `first-section', `quote-section', `section' `item' or
3636 When value is `item', STRUCTURE will be used as the current list
3639 GRANULARITY determines the depth of the recursion. See
3640 `org-element-parse-buffer' for more information.
3642 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3645 Elements are accumulated into ACC."
3648 ;; When parsing only headlines, skip any text before first one.
3649 (when (and (eq granularity
'headline
) (not (org-at-heading-p)))
3650 (org-with-limited-levels (outline-next-heading)))
3652 (while (< (point) end
)
3653 ;; Find current element's type and parse it accordingly to
3655 (let* ((element (org-element--current-element
3656 end granularity special structure
))
3657 (type (org-element-type element
))
3658 (cbeg (org-element-property :contents-begin element
)))
3659 (goto-char (org-element-property :end element
))
3660 ;; Fill ELEMENT contents by side-effect.
3662 ;; If VISIBLE-ONLY is true and element is hidden or if it has
3663 ;; no contents, don't modify it.
3664 ((or (and visible-only
(org-element-property :hiddenp element
))
3666 ;; Greater element: parse it between `contents-begin' and
3667 ;; `contents-end'. Make sure GRANULARITY allows the
3668 ;; recursion, or ELEMENT is an headline, in which case going
3669 ;; inside is mandatory, in order to get sub-level headings.
3670 ((and (memq type org-element-greater-elements
)
3671 (or (memq granularity
'(element object nil
))
3672 (and (eq granularity
'greater-element
)
3674 (eq type
'headline
)))
3675 (org-element--parse-elements
3676 cbeg
(org-element-property :contents-end element
)
3677 ;; Possibly switch to a special mode.
3680 (if (org-element-property :quotedp element
) 'quote-section
3684 (org-element-property :structure element
)
3685 granularity visible-only element
))
3686 ;; ELEMENT has contents. Parse objects inside, if
3687 ;; GRANULARITY allows it.
3688 ((memq granularity
'(object nil
))
3689 (org-element--parse-objects
3690 cbeg
(org-element-property :contents-end element
) element
3691 (org-element-restriction type
))))
3692 (org-element-adopt-element acc element t
)))
3696 (defun org-element--parse-objects (beg end acc restriction
)
3697 "Parse objects between BEG and END and return recursive structure.
3699 Objects are accumulated in ACC.
3701 RESTRICTION is a list of object types which are allowed in the
3706 (while (and (< (point) end
)
3707 (setq candidates
(org-element--get-next-object-candidates
3708 end restriction candidates
)))
3710 (let ((pos (apply 'min
(mapcar 'cdr candidates
))))
3713 (funcall (intern (format "org-element-%s-parser"
3714 (car (rassq pos candidates
)))))))))
3715 ;; 1. Text before any object. Untabify it.
3716 (let ((obj-beg (org-element-property :begin next-object
)))
3717 (unless (= (point) obj-beg
)
3719 (org-element-adopt-element
3721 (replace-regexp-in-string
3722 "\t" (make-string tab-width ?
)
3723 (buffer-substring-no-properties (point) obj-beg
)) t
))))
3725 (let ((obj-end (org-element-property :end next-object
))
3726 (cont-beg (org-element-property :contents-begin next-object
)))
3727 ;; Fill contents of NEXT-OBJECT by side-effect, if it has
3728 ;; a recursive type.
3730 (memq (car next-object
) org-element-recursive-objects
))
3734 (org-element-property :contents-end next-object
))
3735 (org-element--parse-objects
3736 (point-min) (point-max) next-object
3737 (org-element-restriction next-object
))))
3738 (setq acc
(org-element-adopt-element acc next-object t
))
3739 (goto-char obj-end
))))
3740 ;; 3. Text after last object. Untabify it.
3741 (unless (= (point) end
)
3743 (org-element-adopt-element
3745 (replace-regexp-in-string
3746 "\t" (make-string tab-width ?
)
3747 (buffer-substring-no-properties (point) end
)) t
)))
3751 (defun org-element--get-next-object-candidates (limit restriction objects
)
3752 "Return an alist of candidates for the next object.
3754 LIMIT bounds the search, and RESTRICTION narrows candidates to
3757 Return value is an alist whose CAR is position and CDR the object
3760 OBJECTS is the previous candidates alist."
3761 (let (next-candidates types-to-search
)
3762 ;; If no previous result, search every object type in RESTRICTION.
3763 ;; Otherwise, keep potential candidates (old objects located after
3764 ;; point) and ask to search again those which had matched before.
3765 (if (not objects
) (setq types-to-search restriction
)
3767 (if (< (cdr obj
) (point)) (push (car obj
) types-to-search
)
3768 (push obj next-candidates
)))
3770 ;; Call the appropriate successor function for each type to search
3771 ;; and accumulate matches.
3774 (let* ((successor-fun
3776 (format "org-element-%s-successor"
3777 (or (cdr (assq type org-element-object-successor-alist
))
3779 (obj (funcall successor-fun limit
)))
3780 (and obj
(push obj next-candidates
))))
3787 ;;; Towards A Bijective Process
3789 ;; The parse tree obtained with `org-element-parse-buffer' is really
3790 ;; a snapshot of the corresponding Org buffer. Therefore, it can be
3791 ;; interpreted and expanded into a string with canonical Org syntax.
3792 ;; Hence `org-element-interpret-data'.
3794 ;; The function relies internally on
3795 ;; `org-element--interpret-affiliated-keywords'.
3797 (defun org-element-interpret-data (data &optional parent
)
3798 "Interpret DATA as Org syntax.
3800 DATA is a parse tree, an element, an object or a secondary string
3803 Optional argument PARENT is used for recursive calls. It contains
3804 the element or object containing data, or nil.
3806 Return Org syntax as a string."
3807 (let* ((type (org-element-type data
))
3810 ;; Secondary string.
3813 (lambda (obj) (org-element-interpret-data obj parent
))
3815 ;; Full Org document.
3816 ((eq type
'org-data
)
3818 (lambda (obj) (org-element-interpret-data obj parent
))
3819 (org-element-contents data
) ""))
3821 ((stringp data
) data
)
3822 ;; Element/Object without contents.
3823 ((not (org-element-contents data
))
3824 (funcall (intern (format "org-element-%s-interpreter" type
))
3826 ;; Element/Object with contents.
3828 (let* ((greaterp (memq type org-element-greater-elements
))
3829 (objectp (and (not greaterp
)
3830 (memq type org-element-recursive-objects
)))
3833 (lambda (obj) (org-element-interpret-data obj data
))
3834 (org-element-contents
3835 (if (or greaterp objectp
) data
3836 ;; Elements directly containing objects must
3837 ;; have their indentation normalized first.
3838 (org-element-normalize-contents
3840 ;; When normalizing first paragraph of an
3841 ;; item or a footnote-definition, ignore
3842 ;; first line's indentation.
3843 (and (eq type
'paragraph
)
3844 (equal data
(car (org-element-contents parent
)))
3845 (memq (org-element-type parent
)
3846 '(footnote-definiton item
))))))
3848 (funcall (intern (format "org-element-%s-interpreter" type
))
3850 (if greaterp
(org-element-normalize-contents contents
)
3852 (if (memq type
'(org-data plain-text nil
)) results
3853 ;; Build white spaces. If no `:post-blank' property is
3854 ;; specified, assume its value is 0.
3855 (let ((post-blank (or (org-element-property :post-blank data
) 0)))
3856 (if (memq type org-element-all-objects
)
3857 (concat results
(make-string post-blank
32))
3859 (org-element--interpret-affiliated-keywords data
)
3860 (org-element-normalize-string results
)
3861 (make-string post-blank
10)))))))
3863 (defun org-element--interpret-affiliated-keywords (element)
3864 "Return ELEMENT's affiliated keywords as Org syntax.
3865 If there is no affiliated keyword, return the empty string."
3866 (let ((keyword-to-org
3870 (when (member key org-element-dual-keywords
)
3871 (setq dual
(cdr value
) value
(car value
)))
3874 (format "[%s]" (org-element-interpret-data dual
)))
3876 (if (member key org-element-parsed-keywords
)
3877 (org-element-interpret-data value
)
3882 (let ((value (org-element-property prop element
))
3883 (keyword (upcase (substring (symbol-name prop
) 1))))
3885 (if (or (member keyword org-element-multiple-keywords
)
3886 ;; All attribute keywords can have multiple lines.
3887 (string-match "^ATTR_" keyword
))
3888 (mapconcat (lambda (line) (funcall keyword-to-org keyword line
))
3891 (funcall keyword-to-org keyword value
)))))
3892 ;; List all ELEMENT's properties matching an attribute line or an
3893 ;; affiliated keyword, but ignore translated keywords since they
3894 ;; cannot belong to the property list.
3895 (loop for prop in
(nth 1 element
) by
'cddr
3896 when
(let ((keyword (upcase (substring (symbol-name prop
) 1))))
3897 (or (string-match "^ATTR_" keyword
)
3899 (member keyword org-element-affiliated-keywords
)
3901 org-element-keyword-translation-alist
)))))
3905 ;; Because interpretation of the parse tree must return the same
3906 ;; number of blank lines between elements and the same number of white
3907 ;; space after objects, some special care must be given to white
3910 ;; The first function, `org-element-normalize-string', ensures any
3911 ;; string different from the empty string will end with a single
3912 ;; newline character.
3914 ;; The second function, `org-element-normalize-contents', removes
3915 ;; global indentation from the contents of the current element.
3917 (defun org-element-normalize-string (s)
3918 "Ensure string S ends with a single newline character.
3920 If S isn't a string return it unchanged. If S is the empty
3921 string, return it. Otherwise, return a new string with a single
3922 newline character at its end."
3924 ((not (stringp s
)) s
)
3926 (t (and (string-match "\\(\n[ \t]*\\)*\\'" s
)
3927 (replace-match "\n" nil nil s
)))))
3929 (defun org-element-normalize-contents (element &optional ignore-first
)
3930 "Normalize plain text in ELEMENT's contents.
3932 ELEMENT must only contain plain text and objects.
3934 If optional argument IGNORE-FIRST is non-nil, ignore first line's
3935 indentation to compute maximal common indentation.
3937 Return the normalized element that is element with global
3938 indentation removed from its contents. The function assumes that
3939 indentation is not done with TAB characters."
3940 (let* (ind-list ; for byte-compiler
3941 collect-inds
; for byte-compiler
3944 ;; Return list of indentations within BLOB. This is done by
3945 ;; walking recursively BLOB and updating IND-LIST along the
3946 ;; way. FIRST-FLAG is non-nil when the first string hasn't
3947 ;; been seen yet. It is required as this string is the only
3948 ;; one whose indentation doesn't happen after a newline
3950 (lambda (blob first-flag
)
3953 (when (and first-flag
(stringp object
))
3954 (setq first-flag nil
)
3955 (string-match "\\`\\( *\\)" object
)
3956 (let ((len (length (match-string 1 object
))))
3957 ;; An indentation of zero means no string will be
3958 ;; modified. Quit the process.
3959 (if (zerop len
) (throw 'zero
(setq ind-list nil
))
3960 (push len ind-list
))))
3964 ;; Avoid matching blank or empty lines.
3965 (while (and (string-match "\n\\( *\\)\\(.\\)" object start
)
3966 (not (equal (match-string 2 object
) " ")))
3967 (setq start
(match-end 0))
3968 (push (length (match-string 1 object
)) ind-list
))))
3969 ((memq (org-element-type object
) org-element-recursive-objects
)
3970 (funcall collect-inds object first-flag
))))
3971 (org-element-contents blob
))))))
3972 ;; Collect indentation list in ELEMENT. Possibly remove first
3973 ;; value if IGNORE-FIRST is non-nil.
3974 (catch 'zero
(funcall collect-inds element
(not ignore-first
)))
3975 (if (not ind-list
) element
3976 ;; Build ELEMENT back, replacing each string with the same
3977 ;; string minus common indentation.
3978 (let* (build ; For byte compiler.
3981 (lambda (blob mci first-flag
)
3982 ;; Return BLOB with all its strings indentation
3983 ;; shortened from MCI white spaces. FIRST-FLAG is
3984 ;; non-nil when the first string hasn't been seen
3989 (when (and first-flag
(stringp object
))
3990 (setq first-flag nil
)
3992 (replace-regexp-in-string
3993 (format "\\` \\{%d\\}" mci
) "" object
)))
3996 (replace-regexp-in-string
3997 (format "\n \\{%d\\}" mci
) "\n" object
))
3998 ((memq (org-element-type object
)
3999 org-element-recursive-objects
)
4000 (funcall build object mci first-flag
))
4002 (org-element-contents blob
)))
4004 (funcall build element
(apply 'min ind-list
) (not ignore-first
))))))
4010 ;; The first move is to implement a way to obtain the smallest element
4011 ;; containing point. This is the job of `org-element-at-point'. It
4012 ;; basically jumps back to the beginning of section containing point
4013 ;; and moves, element after element, with
4014 ;; `org-element--current-element' until the container is found. Note:
4015 ;; When using `org-element-at-point', secondary values are never
4016 ;; parsed since the function focuses on elements, not on objects.
4018 ;; At a deeper level, `org-element-context' lists all elements and
4019 ;; objects containing point.
4021 ;; `org-element-nested-p' and `org-element-swap-A-B' may be used
4022 ;; internally by navigation and manipulation tools.
4025 (defun org-element-at-point (&optional keep-trail
)
4026 "Determine closest element around point.
4028 Return value is a list like (TYPE PROPS) where TYPE is the type
4029 of the element and PROPS a plist of properties associated to the
4032 Possible types are defined in `org-element-all-elements'.
4033 Properties depend on element or object type, but always
4034 include :begin, :end, :parent and :post-blank properties.
4036 As a special case, if point is at the very beginning of a list or
4037 sub-list, returned element will be that list instead of the first
4038 item. In the same way, if point is at the beginning of the first
4039 row of a table, returned element will be the table instead of the
4042 If optional argument KEEP-TRAIL is non-nil, the function returns
4043 a list of of elements leading to element at point. The list's
4044 CAR is always the element at point. Following positions contain
4045 element's siblings, then parents, siblings of parents, until the
4046 first element of current section."
4047 (org-with-wide-buffer
4048 ;; If at an headline, parse it. It is the sole element that
4049 ;; doesn't require to know about context. Be sure to disallow
4050 ;; secondary string parsing, though.
4051 (if (org-with-limited-levels (org-at-heading-p))
4054 (if (not keep-trail
) (org-element-headline-parser (point-max) t
)
4055 (list (org-element-headline-parser (point-max) t
))))
4056 ;; Otherwise move at the beginning of the section containing
4058 (let ((origin (point))
4059 (end (save-excursion
4060 (org-with-limited-levels (outline-next-heading)) (point)))
4061 element type special-flag trail struct prevs parent
)
4062 (org-with-limited-levels
4063 (if (org-with-limited-levels (org-before-first-heading-p))
4064 (goto-char (point-min))
4065 (org-back-to-heading)
4067 (org-skip-whitespace)
4069 ;; Parse successively each element, skipping those ending
4070 ;; before original position.
4074 (org-element--current-element end
'element special-flag struct
)
4076 (org-element-put-property element
:parent parent
)
4077 (when keep-trail
(push element trail
))
4079 ;; 1. Skip any element ending before point. Also skip
4080 ;; element ending at point when we're sure that another
4081 ;; element has started.
4082 ((let ((elem-end (org-element-property :end element
)))
4083 (when (or (< elem-end origin
)
4084 (and (= elem-end origin
) (/= elem-end end
)))
4085 (goto-char elem-end
))))
4086 ;; 2. An element containing point is always the element at
4088 ((not (memq type org-element-greater-elements
))
4089 (throw 'exit
(if keep-trail trail element
)))
4090 ;; 3. At any other greater element type, if point is
4091 ;; within contents, move into it.
4093 (let ((cbeg (org-element-property :contents-begin element
))
4094 (cend (org-element-property :contents-end element
)))
4095 (if (or (not cbeg
) (not cend
) (> cbeg origin
) (< cend origin
)
4096 ;; Create an anchor for tables and plain lists:
4097 ;; when point is at the very beginning of these
4098 ;; elements, ignoring affiliated keywords,
4099 ;; target them instead of their contents.
4100 (and (= cbeg origin
) (memq type
'(plain-list table
)))
4101 ;; When point is at contents end, do not move
4102 ;; into elements with an explicit ending, but
4103 ;; return that element instead.
4104 (and (= cend origin
)
4106 '(center-block drawer dynamic-block inlinetask
4107 quote-block special-block
))))
4108 (throw 'exit
(if keep-trail trail element
))
4109 (setq parent element
)
4112 (setq special-flag
'item
4113 struct
(org-element-property :structure element
)))
4114 (table (setq special-flag
'table-row
))
4115 (otherwise (setq special-flag nil
)))
4117 (goto-char cbeg
)))))))))))
4119 (defun org-element-context ()
4120 "Return closest element or object around point.
4122 Return value is a list like (TYPE PROPS) where TYPE is the type
4123 of the element or object and PROPS a plist of properties
4126 Possible types are defined in `org-element-all-elements' and
4127 `org-element-all-objects'. Properties depend on element or
4128 object type, but always include :begin, :end, :parent
4129 and :post-blank properties."
4130 (org-with-wide-buffer
4131 (let* ((origin (point))
4132 (element (org-element-at-point))
4133 (type (car element
))
4135 ;; Check if point is inside an element containing objects or at
4136 ;; a secondary string. In that case, move to beginning of the
4137 ;; element or secondary string and set END to the other side.
4138 (if (not (or (and (eq type
'item
)
4139 (let ((tag (org-element-property :tag element
)))
4143 (search-forward tag
(point-at-eol))
4144 (goto-char (match-beginning 0))
4145 (and (>= origin
(point))
4147 ;; `1+' is required so some
4148 ;; successors can match
4149 ;; properly their object.
4150 (setq end
(1+ (match-end 0)))))))))
4151 (and (memq type
'(headline inlinetask
))
4152 (progn (beginning-of-line)
4153 (skip-chars-forward "* ")
4154 (setq end
(point-at-eol))))
4155 (and (memq type
'(paragraph table-cell verse-block
))
4156 (let ((cbeg (org-element-property
4157 :contents-begin element
))
4158 (cend (org-element-property
4159 :contents-end element
)))
4160 (and (>= origin cbeg
)
4162 (progn (goto-char cbeg
) (setq end cend
)))))))
4164 (let ((restriction (org-element-restriction element
))
4168 (while (setq candidates
(org-element--get-next-object-candidates
4169 end restriction candidates
))
4170 (let ((closest-cand (rassq (apply 'min
(mapcar 'cdr candidates
))
4172 ;; If ORIGIN is before next object in element, there's
4173 ;; no point in looking further.
4174 (if (> (cdr closest-cand
) origin
) (throw 'exit element
)
4176 (progn (goto-char (cdr closest-cand
))
4177 (funcall (intern (format "org-element-%s-parser"
4178 (car closest-cand
))))))
4179 (cbeg (org-element-property :contents-begin object
))
4180 (cend (org-element-property :contents-end object
)))
4182 ;; ORIGIN is after OBJECT, so skip it.
4183 ((< (org-element-property :end object
) origin
)
4184 (goto-char (org-element-property :end object
)))
4185 ;; ORIGIN is within a non-recursive object or at an
4186 ;; object boundaries: Return that object.
4187 ((or (not cbeg
) (> cbeg origin
) (< cend origin
))
4189 (org-element-put-property object
:parent parent
)))
4190 ;; Otherwise, move within current object and restrict
4191 ;; search to the end of its contents.
4193 (org-element-put-property object
:parent parent
)
4194 (setq parent object end cend
)))))))
4197 (defsubst org-element-nested-p
(elem-A elem-B
)
4198 "Non-nil when elements ELEM-A and ELEM-B are nested."
4199 (let ((beg-A (org-element-property :begin elem-A
))
4200 (beg-B (org-element-property :begin elem-B
))
4201 (end-A (org-element-property :end elem-A
))
4202 (end-B (org-element-property :end elem-B
)))
4203 (or (and (>= beg-A beg-B
) (<= end-A end-B
))
4204 (and (>= beg-B beg-A
) (<= end-B end-A
)))))
4206 (defun org-element-swap-A-B (elem-A elem-B
)
4207 "Swap elements ELEM-A and ELEM-B.
4208 Assume ELEM-B is after ELEM-A in the buffer. Leave point at the
4210 (goto-char (org-element-property :begin elem-A
))
4211 ;; There are two special cases when an element doesn't start at bol:
4212 ;; the first paragraph in an item or in a footnote definition.
4213 (let ((specialp (not (bolp))))
4214 ;; Only a paragraph without any affiliated keyword can be moved at
4215 ;; ELEM-A position in such a situation. Note that the case of
4216 ;; a footnote definition is impossible: it cannot contain two
4217 ;; paragraphs in a row because it cannot contain a blank line.
4219 (or (not (eq (org-element-type elem-B
) 'paragraph
))
4220 (/= (org-element-property :begin elem-B
)
4221 (org-element-property :contents-begin elem-B
))))
4222 (error "Cannot swap elements"))
4223 ;; In a special situation, ELEM-A will have no indentation. We'll
4224 ;; give it ELEM-B's (which will in, in turn, have no indentation).
4225 (let* ((ind-B (when specialp
4226 (goto-char (org-element-property :begin elem-B
))
4227 (org-get-indentation)))
4228 (beg-A (org-element-property :begin elem-A
))
4229 (end-A (save-excursion
4230 (goto-char (org-element-property :end elem-A
))
4231 (skip-chars-backward " \r\t\n")
4233 (beg-B (org-element-property :begin elem-B
))
4234 (end-B (save-excursion
4235 (goto-char (org-element-property :end elem-B
))
4236 (skip-chars-backward " \r\t\n")
4238 ;; Store overlays responsible for visibility status. We
4239 ;; also need to store their boundaries as they will be
4240 ;; removed from buffer.
4243 (mapcar (lambda (ov) (list ov
(overlay-start ov
) (overlay-end ov
)))
4244 (overlays-in beg-A end-A
))
4245 (mapcar (lambda (ov) (list ov
(overlay-start ov
) (overlay-end ov
)))
4246 (overlays-in beg-B end-B
))))
4248 (body-A (buffer-substring beg-A end-A
))
4249 (body-B (delete-and-extract-region beg-B end-B
)))
4252 (setq body-B
(replace-regexp-in-string "\\`[ \t]*" "" body-B
))
4253 (org-indent-to-column ind-B
))
4255 ;; Restore ex ELEM-A overlays.
4259 (+ (nth 1 ov
) (- beg-B beg-A
))
4260 (+ (nth 2 ov
) (- beg-B beg-A
))))
4263 (delete-region beg-A end-A
)
4265 ;; Restore ex ELEM-B overlays.
4267 (move-overlay (car ov
)
4268 (+ (nth 1 ov
) (- beg-A beg-B
))
4269 (+ (nth 2 ov
) (- beg-A beg-B
))))
4271 (goto-char (org-element-property :end elem-B
)))))
4274 (provide 'org-element
)
4275 ;;; org-element.el ends here