1 ;;; org-element.el --- Parser And Applications for Org syntax
3 ;; Copyright (C) 2012-2014 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; Org syntax can be divided into three categories: "Greater
26 ;; elements", "Elements" and "Objects".
28 ;; Elements are related to the structure of the document. Indeed, all
29 ;; elements are a cover for the document: each position within belongs
30 ;; to at least one element.
32 ;; An element always starts and ends at the beginning of a line. With
33 ;; a few exceptions (`clock', `headline', `inlinetask', `item',
34 ;; `planning', `node-property', `quote-section' `section' and
35 ;; `table-row' types), it can also accept a fixed set of keywords as
36 ;; attributes. Those are called "affiliated keywords" to distinguish
37 ;; them from other keywords, which are full-fledged elements. Almost
38 ;; all affiliated keywords are referenced in
39 ;; `org-element-affiliated-keywords'; the others are export attributes
40 ;; and start with "ATTR_" prefix.
42 ;; Element containing other elements (and only elements) are called
43 ;; greater elements. Concerned types are: `center-block', `drawer',
44 ;; `dynamic-block', `footnote-definition', `headline', `inlinetask',
45 ;; `item', `plain-list', `property-drawer', `quote-block', `section'
46 ;; and `special-block'.
48 ;; Other element types are: `babel-call', `clock', `comment',
49 ;; `comment-block', `diary-sexp', `example-block', `export-block',
50 ;; `fixed-width', `horizontal-rule', `keyword', `latex-environment',
51 ;; `node-property', `paragraph', `planning', `quote-section',
52 ;; `src-block', `table', `table-row' and `verse-block'. Among them,
53 ;; `paragraph' and `verse-block' types can contain Org objects and
56 ;; Objects are related to document's contents. Some of them are
57 ;; recursive. Associated types are of the following: `bold', `code',
58 ;; `entity', `export-snippet', `footnote-reference',
59 ;; `inline-babel-call', `inline-src-block', `italic',
60 ;; `latex-fragment', `line-break', `link', `macro', `radio-target',
61 ;; `statistics-cookie', `strike-through', `subscript', `superscript',
62 ;; `table-cell', `target', `timestamp', `underline' and `verbatim'.
64 ;; Some elements also have special properties whose value can hold
65 ;; objects themselves (e.g. an item tag or a headline name). Such
66 ;; values are called "secondary strings". Any object belongs to
67 ;; either an element or a secondary string.
69 ;; Notwithstanding affiliated keywords, each greater element, element
70 ;; and object has a fixed set of properties attached to it. Among
71 ;; them, four are shared by all types: `:begin' and `:end', which
72 ;; refer to the beginning and ending buffer positions of the
73 ;; considered element or object, `:post-blank', which holds the number
74 ;; of blank lines, or white spaces, at its end and `:parent' which
75 ;; refers to the element or object containing it. Greater elements,
76 ;; elements and objects containing objects will also have
77 ;; `:contents-begin' and `:contents-end' properties to delimit
78 ;; contents. Eventually, greater elements and elements accepting
79 ;; affiliated keywords will have a `:post-affiliated' property,
80 ;; referring to the buffer position after all such keywords.
82 ;; At the lowest level, a `:parent' property is also attached to any
83 ;; string, as a text property.
85 ;; Lisp-wise, an element or an object can be represented as a list.
86 ;; It follows the pattern (TYPE PROPERTIES CONTENTS), where:
87 ;; TYPE is a symbol describing the Org element or object.
88 ;; PROPERTIES is the property list attached to it. See docstring of
89 ;; appropriate parsing function to get an exhaustive
91 ;; CONTENTS is a list of elements, objects or raw strings contained
92 ;; in the current element or object, when applicable.
94 ;; An Org buffer is a nested list of such elements and objects, whose
95 ;; type is `org-data' and properties is nil.
97 ;; The first part of this file defines Org syntax, while the second
98 ;; one provide accessors and setters functions.
100 ;; The next part implements a parser and an interpreter for each
101 ;; element and object type in Org syntax.
103 ;; The following part creates a fully recursive buffer parser. It
104 ;; also provides a tool to map a function to elements or objects
105 ;; matching some criteria in the parse tree. Functions of interest
106 ;; are `org-element-parse-buffer', `org-element-map' and, to a lesser
107 ;; extent, `org-element-parse-secondary-string'.
109 ;; The penultimate part is the cradle of an interpreter for the
110 ;; obtained parse tree: `org-element-interpret-data'.
112 ;; The library ends by furnishing `org-element-at-point' function, and
113 ;; a way to give information about document structure around point
114 ;; with `org-element-context'.
119 (eval-when-compile (require 'cl
))
124 ;;; Definitions And Rules
126 ;; Define elements, greater elements and specify recursive objects,
127 ;; along with the affiliated keywords recognized. Also set up
128 ;; restrictions on recursive objects combinations.
130 ;; These variables really act as a control center for the parsing
133 (defconst org-element-paragraph-separate
135 ;; Headlines, inlinetasks.
136 org-outline-regexp
"\\|"
137 ;; Footnote definitions.
138 "\\[\\(?:[0-9]+\\|fn:[-_[:word:]]+\\)\\]" "\\|"
144 ;; Tables (any type).
145 "\\(?:|\\|\\+-[-+]\\)" "\\|"
146 ;; Blocks (any type), Babel calls and keywords. Note: this
147 ;; is only an indication and need some thorough check.
148 "#\\(?:[+ ]\\|$\\)" "\\|"
149 ;; Drawers (any type) and fixed-width areas. This is also
150 ;; only an indication.
153 "-\\{5,\\}[ \t]*$" "\\|"
154 ;; LaTeX environments.
155 "\\\\begin{\\([A-Za-z0-9]+\\*?\\)}" "\\|"
156 ;; Planning and Clock lines.
157 (regexp-opt (list org-scheduled-string
163 (let ((term (case org-plain-list-ordered-item-terminator
164 (?\
) ")") (?.
"\\.") (otherwise "[.)]")))
165 (alpha (and org-list-allow-alphabetical
"\\|[A-Za-z]")))
166 (concat "\\(?:[-+*]\\|\\(?:[0-9]+" alpha
"\\)" term
"\\)"
167 "\\(?:[ \t]\\|$\\)"))
169 "Regexp to separate paragraphs in an Org buffer.
170 In the case of lines starting with \"#\" and \":\", this regexp
171 is not sufficient to know if point is at a paragraph ending. See
172 `org-element-paragraph-parser' for more information.")
174 (defconst org-element-all-elements
175 '(babel-call center-block clock comment comment-block diary-sexp drawer
176 dynamic-block example-block export-block fixed-width
177 footnote-definition headline horizontal-rule inlinetask item
178 keyword latex-environment node-property paragraph plain-list
179 planning property-drawer quote-block quote-section section
180 special-block src-block table table-row verse-block
)
181 "Complete list of element types.")
183 (defconst org-element-greater-elements
184 '(center-block drawer dynamic-block footnote-definition headline inlinetask
185 item plain-list property-drawer quote-block section
187 "List of recursive element types aka Greater Elements.")
189 (defconst org-element-all-successors
190 '(link export-snippet footnote-reference inline-babel-call
191 inline-src-block latex-or-entity line-break macro plain-link
192 radio-target statistics-cookie sub
/superscript table-cell target
193 text-markup timestamp
)
194 "Complete list of successors.")
196 (defconst org-element-object-successor-alist
197 '((subscript . sub
/superscript
) (superscript . sub
/superscript
)
198 (bold . text-markup
) (code . text-markup
) (italic . text-markup
)
199 (strike-through . text-markup
) (underline . text-markup
)
200 (verbatim . text-markup
) (entity . latex-or-entity
)
201 (latex-fragment . latex-or-entity
))
202 "Alist of translations between object type and successor name.
203 Sharing the same successor comes handy when, for example, the
204 regexp matching one object can also match the other object.")
206 (defconst org-element-all-objects
207 '(bold code entity export-snippet footnote-reference inline-babel-call
208 inline-src-block italic line-break latex-fragment link macro
209 radio-target statistics-cookie strike-through subscript superscript
210 table-cell target timestamp underline verbatim
)
211 "Complete list of object types.")
213 (defconst org-element-recursive-objects
214 '(bold italic link subscript radio-target strike-through superscript
215 table-cell underline
)
216 "List of recursive object types.")
218 (defvar org-element-block-name-alist
219 '(("CENTER" . org-element-center-block-parser
)
220 ("COMMENT" . org-element-comment-block-parser
)
221 ("EXAMPLE" . org-element-example-block-parser
)
222 ("QUOTE" . org-element-quote-block-parser
)
223 ("SRC" . org-element-src-block-parser
)
224 ("VERSE" . org-element-verse-block-parser
))
225 "Alist between block names and the associated parsing function.
226 Names must be uppercase. Any block whose name has no association
227 is parsed with `org-element-special-block-parser'.")
229 (defconst org-element-link-type-is-file
230 '("file" "file+emacs" "file+sys" "docview")
231 "List of link types equivalent to \"file\".
232 Only these types can accept search options and an explicit
233 application to open them.")
235 (defconst org-element-affiliated-keywords
236 '("CAPTION" "DATA" "HEADER" "HEADERS" "LABEL" "NAME" "PLOT" "RESNAME" "RESULT"
237 "RESULTS" "SOURCE" "SRCNAME" "TBLNAME")
238 "List of affiliated keywords as strings.
239 By default, all keywords setting attributes (e.g., \"ATTR_LATEX\")
240 are affiliated keywords and need not to be in this list.")
242 (defconst org-element-keyword-translation-alist
243 '(("DATA" .
"NAME") ("LABEL" .
"NAME") ("RESNAME" .
"NAME")
244 ("SOURCE" .
"NAME") ("SRCNAME" .
"NAME") ("TBLNAME" .
"NAME")
245 ("RESULT" .
"RESULTS") ("HEADERS" .
"HEADER"))
246 "Alist of usual translations for keywords.
247 The key is the old name and the value the new one. The property
248 holding their value will be named after the translated name.")
250 (defconst org-element-multiple-keywords
'("CAPTION" "HEADER")
251 "List of affiliated keywords that can occur more than once in an element.
253 Their value will be consed into a list of strings, which will be
254 returned as the value of the property.
256 This list is checked after translations have been applied. See
257 `org-element-keyword-translation-alist'.
259 By default, all keywords setting attributes (e.g., \"ATTR_LATEX\")
260 allow multiple occurrences and need not to be in this list.")
262 (defconst org-element-parsed-keywords
'("CAPTION")
263 "List of affiliated keywords whose value can be parsed.
265 Their value will be stored as a secondary string: a list of
268 This list is checked after translations have been applied. See
269 `org-element-keyword-translation-alist'.")
271 (defconst org-element-dual-keywords
'("CAPTION" "RESULTS")
272 "List of affiliated keywords which can have a secondary value.
274 In Org syntax, they can be written with optional square brackets
275 before the colons. For example, RESULTS keyword can be
276 associated to a hash value with the following:
278 #+RESULTS[hash-string]: some-source
280 This list is checked after translations have been applied. See
281 `org-element-keyword-translation-alist'.")
283 (defconst org-element-document-properties
'("AUTHOR" "DATE" "TITLE")
284 "List of properties associated to the whole document.
285 Any keyword in this list will have its value parsed and stored as
286 a secondary string.")
288 (defconst org-element--affiliated-re
289 (format "[ \t]*#\\+\\(?:%s\\):\\(?: \\|$\\)"
291 ;; Dual affiliated keywords.
292 (format "\\(?1:%s\\)\\(?:\\[\\(.*\\)\\]\\)?"
293 (regexp-opt org-element-dual-keywords
))
295 ;; Regular affiliated keywords.
296 (format "\\(?1:%s\\)"
300 (member keyword org-element-dual-keywords
))
301 org-element-affiliated-keywords
)))
303 ;; Export attributes.
304 "\\(?1:ATTR_[-_A-Za-z0-9]+\\)"))
305 "Regexp matching any affiliated keyword.
307 Keyword name is put in match group 1. Moreover, if keyword
308 belongs to `org-element-dual-keywords', put the dual value in
311 Don't modify it, set `org-element-affiliated-keywords' instead.")
313 (defconst org-element-object-restrictions
315 (remq 'plain-link
(remq 'table-cell org-element-all-successors
)))
316 (standard-set-no-line-break (remq 'line-break standard-set
)))
317 `((bold ,@standard-set
)
318 (footnote-reference ,@standard-set
)
319 (headline ,@standard-set-no-line-break
)
320 (inlinetask ,@standard-set-no-line-break
)
321 (italic ,@standard-set
)
322 (item ,@standard-set-no-line-break
)
323 (keyword ,@standard-set
)
324 ;; Ignore all links excepted plain links in a link description.
325 ;; Also ignore radio-targets and line breaks.
326 (link export-snippet inline-babel-call inline-src-block latex-or-entity
327 macro plain-link statistics-cookie sub
/superscript text-markup
)
328 (paragraph ,@standard-set
)
329 ;; Remove any variable object from radio target as it would
330 ;; prevent it from being properly recognized.
331 (radio-target latex-or-entity sub
/superscript text-markup
)
332 (strike-through ,@standard-set
)
333 (subscript ,@standard-set
)
334 (superscript ,@standard-set
)
335 ;; Ignore inline babel call and inline src block as formulas are
336 ;; possible. Also ignore line breaks and statistics cookies.
337 (table-cell link export-snippet footnote-reference latex-or-entity macro
338 radio-target sub
/superscript target text-markup timestamp
)
339 (table-row table-cell
)
340 (underline ,@standard-set
)
341 (verse-block ,@standard-set
)))
342 "Alist of objects restrictions.
344 CAR is an element or object type containing objects and CDR is
345 a list of successors that will be called within an element or
348 For example, in a `radio-target' object, one can only find
349 entities, latex-fragments, subscript, superscript and text
352 This alist also applies to secondary string. For example, an
353 `headline' type element doesn't directly contain objects, but
354 still has an entry since one of its properties (`:title') does.")
356 (defconst org-element-secondary-value-alist
357 '((headline .
:title
)
358 (inlinetask .
:title
)
360 (footnote-reference .
:inline-definition
))
361 "Alist between element types and location of secondary value.")
365 ;;; Accessors and Setters
367 ;; Provide four accessors: `org-element-type', `org-element-property'
368 ;; `org-element-contents' and `org-element-restriction'.
370 ;; Setter functions allow to modify elements by side effect. There is
371 ;; `org-element-put-property', `org-element-set-contents',
372 ;; `org-element-set-element' and `org-element-adopt-element'. Note
373 ;; that `org-element-set-element' and `org-element-adopt-elements' are
374 ;; higher level functions since also update `:parent' property.
376 (defsubst org-element-type
(element)
377 "Return type of ELEMENT.
379 The function returns the type of the element or object provided.
380 It can also return the following special value:
381 `plain-text' for a string
382 `org-data' for a complete document
383 nil in any other case."
385 ((not (consp element
)) (and (stringp element
) 'plain-text
))
386 ((symbolp (car element
)) (car element
))))
388 (defsubst org-element-property
(property element
)
389 "Extract the value from the PROPERTY of an ELEMENT."
390 (if (stringp element
) (get-text-property 0 property element
)
391 (plist-get (nth 1 element
) property
)))
393 (defsubst org-element-contents
(element)
394 "Extract contents from an ELEMENT."
395 (cond ((not (consp element
)) nil
)
396 ((symbolp (car element
)) (nthcdr 2 element
))
399 (defsubst org-element-restriction
(element)
400 "Return restriction associated to ELEMENT.
401 ELEMENT can be an element, an object or a symbol representing an
402 element or object type."
403 (cdr (assq (if (symbolp element
) element
(org-element-type element
))
404 org-element-object-restrictions
)))
406 (defsubst org-element-put-property
(element property value
)
407 "In ELEMENT set PROPERTY to VALUE.
408 Return modified element."
409 (if (stringp element
) (org-add-props element nil property value
)
410 (setcar (cdr element
) (plist-put (nth 1 element
) property value
))
413 (defsubst org-element-set-contents
(element &rest contents
)
414 "Set ELEMENT contents to CONTENTS.
415 Return modified element."
416 (cond ((not element
) (list contents
))
417 ((not (symbolp (car element
))) contents
)
418 ((cdr element
) (setcdr (cdr element
) contents
))
419 (t (nconc element contents
))))
421 (defsubst org-element-set-element
(old new
)
422 "Replace element or object OLD with element or object NEW.
423 The function takes care of setting `:parent' property for NEW."
424 ;; Since OLD is going to be changed into NEW by side-effect, first
425 ;; make sure that every element or object within NEW has OLD as
427 (mapc (lambda (blob) (org-element-put-property blob
:parent old
))
428 (org-element-contents new
))
429 ;; Transfer contents.
430 (apply 'org-element-set-contents old
(org-element-contents new
))
431 ;; Ensure NEW has same parent as OLD, then overwrite OLD properties
433 (org-element-put-property new
:parent
(org-element-property :parent old
))
434 (setcar (cdr old
) (nth 1 new
))
436 (setcar old
(car new
)))
438 (defsubst org-element-adopt-elements
(parent &rest children
)
439 "Append elements to the contents of another element.
441 PARENT is an element or object. CHILDREN can be elements,
442 objects, or a strings.
444 The function takes care of setting `:parent' property for CHILD.
445 Return parent element."
446 ;; Link every child to PARENT. If PARENT is nil, it is a secondary
447 ;; string: parent is the list itself.
448 (mapc (lambda (child)
449 (org-element-put-property child
:parent
(or parent children
)))
451 ;; Add CHILDREN at the end of PARENT contents.
453 (apply 'org-element-set-contents
455 (nconc (org-element-contents parent
) children
)))
456 ;; Return modified PARENT element.
457 (or parent children
))
463 ;; For each greater element type, we define a parser and an
466 ;; A parser returns the element or object as the list described above.
467 ;; Most of them accepts no argument. Though, exceptions exist. Hence
468 ;; every element containing a secondary string (see
469 ;; `org-element-secondary-value-alist') will accept an optional
470 ;; argument to toggle parsing of that secondary string. Moreover,
471 ;; `item' parser requires current list's structure as its first
474 ;; An interpreter accepts two arguments: the list representation of
475 ;; the element or object, and its contents. The latter may be nil,
476 ;; depending on the element or object considered. It returns the
477 ;; appropriate Org syntax, as a string.
479 ;; Parsing functions must follow the naming convention:
480 ;; org-element-TYPE-parser, where TYPE is greater element's type, as
481 ;; defined in `org-element-greater-elements'.
483 ;; Similarly, interpreting functions must follow the naming
484 ;; convention: org-element-TYPE-interpreter.
486 ;; With the exception of `headline' and `item' types, greater elements
487 ;; cannot contain other greater elements of their own type.
489 ;; Beside implementing a parser and an interpreter, adding a new
490 ;; greater element requires to tweak `org-element--current-element'.
491 ;; Moreover, the newly defined type must be added to both
492 ;; `org-element-all-elements' and `org-element-greater-elements'.
497 (defun org-element-center-block-parser (limit affiliated
)
498 "Parse a center block.
500 LIMIT bounds the search. AFFILIATED is a list of which CAR is
501 the buffer position at the beginning of the first affiliated
502 keyword and CDR is a plist of affiliated keywords along with
505 Return a list whose CAR is `center-block' and CDR is a plist
506 containing `:begin', `:end', `:hiddenp', `:contents-begin',
507 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
509 Assume point is at the beginning of the block."
510 (let ((case-fold-search t
))
511 (if (not (save-excursion
512 (re-search-forward "^[ \t]*#\\+END_CENTER[ \t]*$" limit t
)))
513 ;; Incomplete block: parse it as a paragraph.
514 (org-element-paragraph-parser limit affiliated
)
515 (let ((block-end-line (match-beginning 0)))
516 (let* ((begin (car affiliated
))
517 (post-affiliated (point))
518 ;; Empty blocks have no contents.
519 (contents-begin (progn (forward-line)
520 (and (< (point) block-end-line
)
522 (contents-end (and contents-begin block-end-line
))
523 (hidden (org-invisible-p2))
524 (pos-before-blank (progn (goto-char block-end-line
)
528 (skip-chars-forward " \r\t\n" limit
)
529 (if (eobp) (point) (line-beginning-position)))))
535 :contents-begin contents-begin
536 :contents-end contents-end
537 :post-blank
(count-lines pos-before-blank end
)
538 :post-affiliated post-affiliated
)
539 (cdr affiliated
))))))))
541 (defun org-element-center-block-interpreter (center-block contents
)
542 "Interpret CENTER-BLOCK element as Org syntax.
543 CONTENTS is the contents of the element."
544 (format "#+BEGIN_CENTER\n%s#+END_CENTER" contents
))
549 (defun org-element-drawer-parser (limit affiliated
)
552 LIMIT bounds the search. AFFILIATED is a list of which CAR is
553 the buffer position at the beginning of the first affiliated
554 keyword and CDR is a plist of affiliated keywords along with
557 Return a list whose CAR is `drawer' and CDR is a plist containing
558 `:drawer-name', `:begin', `:end', `:hiddenp', `:contents-begin',
559 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
561 Assume point is at beginning of drawer."
562 (let ((case-fold-search t
))
563 (if (not (save-excursion (re-search-forward "^[ \t]*:END:[ \t]*$" limit t
)))
564 ;; Incomplete drawer: parse it as a paragraph.
565 (org-element-paragraph-parser limit affiliated
)
567 (let* ((drawer-end-line (match-beginning 0))
568 (name (progn (looking-at org-drawer-regexp
)
569 (org-match-string-no-properties 1)))
570 (begin (car affiliated
))
571 (post-affiliated (point))
572 ;; Empty drawers have no contents.
573 (contents-begin (progn (forward-line)
574 (and (< (point) drawer-end-line
)
576 (contents-end (and contents-begin drawer-end-line
))
577 (hidden (org-invisible-p2))
578 (pos-before-blank (progn (goto-char drawer-end-line
)
581 (end (progn (skip-chars-forward " \r\t\n" limit
)
582 (if (eobp) (point) (line-beginning-position)))))
589 :contents-begin contents-begin
590 :contents-end contents-end
591 :post-blank
(count-lines pos-before-blank end
)
592 :post-affiliated post-affiliated
)
593 (cdr affiliated
))))))))
595 (defun org-element-drawer-interpreter (drawer contents
)
596 "Interpret DRAWER element as Org syntax.
597 CONTENTS is the contents of the element."
598 (format ":%s:\n%s:END:"
599 (org-element-property :drawer-name drawer
)
605 (defun org-element-dynamic-block-parser (limit affiliated
)
606 "Parse a dynamic block.
608 LIMIT bounds the search. AFFILIATED is a list of which CAR is
609 the buffer position at the beginning of the first affiliated
610 keyword and CDR is a plist of affiliated keywords along with
613 Return a list whose CAR is `dynamic-block' and CDR is a plist
614 containing `:block-name', `:begin', `:end', `:hiddenp',
615 `:contents-begin', `:contents-end', `:arguments', `:post-blank'
616 and `:post-affiliated' keywords.
618 Assume point is at beginning of dynamic block."
619 (let ((case-fold-search t
))
620 (if (not (save-excursion
621 (re-search-forward "^[ \t]*#\\+END:?[ \t]*$" limit t
)))
622 ;; Incomplete block: parse it as a paragraph.
623 (org-element-paragraph-parser limit affiliated
)
624 (let ((block-end-line (match-beginning 0)))
626 (let* ((name (progn (looking-at org-dblock-start-re
)
627 (org-match-string-no-properties 1)))
628 (arguments (org-match-string-no-properties 3))
629 (begin (car affiliated
))
630 (post-affiliated (point))
631 ;; Empty blocks have no contents.
632 (contents-begin (progn (forward-line)
633 (and (< (point) block-end-line
)
635 (contents-end (and contents-begin block-end-line
))
636 (hidden (org-invisible-p2))
637 (pos-before-blank (progn (goto-char block-end-line
)
640 (end (progn (skip-chars-forward " \r\t\n" limit
)
641 (if (eobp) (point) (line-beginning-position)))))
649 :contents-begin contents-begin
650 :contents-end contents-end
651 :post-blank
(count-lines pos-before-blank end
)
652 :post-affiliated post-affiliated
)
653 (cdr affiliated
)))))))))
655 (defun org-element-dynamic-block-interpreter (dynamic-block contents
)
656 "Interpret DYNAMIC-BLOCK element as Org syntax.
657 CONTENTS is the contents of the element."
658 (format "#+BEGIN: %s%s\n%s#+END:"
659 (org-element-property :block-name dynamic-block
)
660 (let ((args (org-element-property :arguments dynamic-block
)))
661 (and args
(concat " " args
)))
665 ;;;; Footnote Definition
667 (defun org-element-footnote-definition-parser (limit affiliated
)
668 "Parse a footnote definition.
670 LIMIT bounds the search. AFFILIATED is a list of which CAR is
671 the buffer position at the beginning of the first affiliated
672 keyword and CDR is a plist of affiliated keywords along with
675 Return a list whose CAR is `footnote-definition' and CDR is
676 a plist containing `:label', `:begin' `:end', `:contents-begin',
677 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
679 Assume point is at the beginning of the footnote definition."
681 (let* ((label (progn (looking-at org-footnote-definition-re
)
682 (org-match-string-no-properties 1)))
683 (begin (car affiliated
))
684 (post-affiliated (point))
685 (ending (save-excursion
689 (concat org-outline-regexp-bol
"\\|"
690 org-footnote-definition-re
"\\|"
691 "^\\([ \t]*\n\\)\\{2,\\}") limit
'move
))
694 (contents-begin (progn
696 (skip-chars-forward " \r\t\n" ending
)
697 (cond ((= (point) ending
) nil
)
698 ((= (line-beginning-position) begin
) (point))
699 (t (line-beginning-position)))))
700 (contents-end (and contents-begin ending
))
701 (end (progn (goto-char ending
)
702 (skip-chars-forward " \r\t\n" limit
)
703 (if (eobp) (point) (line-beginning-position)))))
704 (list 'footnote-definition
709 :contents-begin contents-begin
710 :contents-end contents-end
711 :post-blank
(count-lines ending end
)
712 :post-affiliated post-affiliated
)
713 (cdr affiliated
))))))
715 (defun org-element-footnote-definition-interpreter (footnote-definition contents
)
716 "Interpret FOOTNOTE-DEFINITION element as Org syntax.
717 CONTENTS is the contents of the footnote-definition."
718 (concat (format "[%s]" (org-element-property :label footnote-definition
))
725 (defun org-element-headline-parser (limit &optional raw-secondary-p
)
728 Return a list whose CAR is `headline' and CDR is a plist
729 containing `:raw-value', `:title', `:alt-title', `:begin',
730 `:end', `:pre-blank', `:hiddenp', `:contents-begin',
731 `:contents-end', `:level', `:priority', `:tags',
732 `:todo-keyword',`:todo-type', `:scheduled', `:deadline',
733 `:closed', `:quotedp', `:archivedp', `:commentedp',
734 `:footnote-section-p' and `:post-blank' keywords.
736 The plist also contains any property set in the property drawer,
737 with its name in upper cases and colons added at the
738 beginning (e.g., `:CUSTOM_ID').
740 LIMIT is a buffer position bounding the search.
742 When RAW-SECONDARY-P is non-nil, headline's title will not be
743 parsed as a secondary string, but as a plain string instead.
745 Assume point is at beginning of the headline."
747 (let* ((components (org-heading-components))
748 (level (nth 1 components
))
749 (todo (nth 2 components
))
751 (and todo
(if (member todo org-done-keywords
) 'done
'todo
)))
752 (tags (let ((raw-tags (nth 5 components
)))
753 (and raw-tags
(org-split-string raw-tags
":"))))
754 (raw-value (or (nth 4 components
) ""))
756 (let ((case-fold-search nil
))
757 (string-match (format "^%s\\( \\|$\\)" org-quote-string
)
760 (let ((case-fold-search nil
))
761 (string-match (format "^%s\\( \\|$\\)" org-comment-string
)
763 (archivedp (member org-archive-tag tags
))
764 (footnote-section-p (and org-footnote-section
765 (string= org-footnote-section raw-value
)))
766 ;; Upcase property names. It avoids confusion between
767 ;; properties obtained through property drawer and default
768 ;; properties from the parser (e.g. `:end' and :END:)
775 (intern (concat ":" (upcase (car p
))))
777 (org-entry-properties nil
'standard
))
780 ;; Read time properties on the line below the headline.
782 (when (progn (forward-line)
783 (looking-at org-planning-or-clock-line-re
))
784 (let ((end (line-end-position)) plist
)
785 (while (re-search-forward
786 org-keyword-time-not-clock-regexp end t
)
787 (goto-char (match-end 1))
788 (skip-chars-forward " \t")
789 (let ((keyword (match-string 1))
790 (time (org-element-timestamp-parser)))
791 (cond ((equal keyword org-scheduled-string
)
792 (setq plist
(plist-put plist
:scheduled time
)))
793 ((equal keyword org-deadline-string
)
794 (setq plist
(plist-put plist
:deadline time
)))
795 (t (setq plist
(plist-put plist
:closed time
))))))
798 (end (min (save-excursion (org-end-of-subtree t t
)) limit
))
799 (pos-after-head (progn (forward-line) (point)))
800 (contents-begin (save-excursion
801 (skip-chars-forward " \r\t\n" end
)
802 (and (/= (point) end
) (line-beginning-position))))
803 (hidden (org-invisible-p2))
804 (contents-end (and contents-begin
805 (progn (goto-char end
)
806 (skip-chars-backward " \r\t\n")
809 ;; Clean RAW-VALUE from any quote or comment string.
810 (when (or quotedp commentedp
)
811 (let ((case-fold-search nil
))
813 (replace-regexp-in-string
815 (regexp-opt (list org-quote-string org-comment-string
))
819 ;; Clean TAGS from archive tag, if any.
820 (when archivedp
(setq tags
(delete org-archive-tag tags
)))
824 (list :raw-value raw-value
828 (if (not contents-begin
) 0
829 (count-lines pos-after-head contents-begin
))
831 :contents-begin contents-begin
832 :contents-end contents-end
834 :priority
(nth 3 components
)
838 :post-blank
(count-lines
839 (or contents-end pos-after-head
)
841 :footnote-section-p footnote-section-p
843 :commentedp commentedp
847 (let ((alt-title (org-element-property :ALT_TITLE headline
)))
849 (org-element-put-property
851 (if raw-secondary-p alt-title
852 (org-element-parse-secondary-string
853 alt-title
(org-element-restriction 'headline
) headline
)))))
854 (org-element-put-property
856 (if raw-secondary-p raw-value
857 (org-element-parse-secondary-string
858 raw-value
(org-element-restriction 'headline
) headline
)))))))
860 (defun org-element-headline-interpreter (headline contents
)
861 "Interpret HEADLINE element as Org syntax.
862 CONTENTS is the contents of the element."
863 (let* ((level (org-element-property :level headline
))
864 (todo (org-element-property :todo-keyword headline
))
865 (priority (org-element-property :priority headline
))
866 (title (org-element-interpret-data
867 (org-element-property :title headline
)))
868 (tags (let ((tag-list (if (org-element-property :archivedp headline
)
869 (cons org-archive-tag
870 (org-element-property :tags headline
))
871 (org-element-property :tags headline
))))
873 (format ":%s:" (mapconcat 'identity tag-list
":")))))
874 (commentedp (org-element-property :commentedp headline
))
875 (quotedp (org-element-property :quotedp headline
))
876 (pre-blank (or (org-element-property :pre-blank headline
) 0))
877 (heading (concat (make-string (org-reduced-level level
) ?
*)
878 (and todo
(concat " " todo
))
879 (and quotedp
(concat " " org-quote-string
))
880 (and commentedp
(concat " " org-comment-string
))
882 (format " [#%s]" (char-to-string priority
)))
883 (cond ((and org-footnote-section
884 (org-element-property
885 :footnote-section-p headline
))
886 (concat " " org-footnote-section
))
887 (title (concat " " title
))))))
892 ((zerop org-tags-column
) (format " %s" tags
))
893 ((< org-tags-column
0)
896 (max (- (+ org-tags-column
(length heading
) (length tags
))) 1)
901 (make-string (max (- org-tags-column
(length heading
)) 1) ?
)
903 (make-string (1+ pre-blank
) 10)
909 (defun org-element-inlinetask-parser (limit &optional raw-secondary-p
)
910 "Parse an inline task.
912 Return a list whose CAR is `inlinetask' and CDR is a plist
913 containing `:title', `:begin', `:end', `:hiddenp',
914 `:contents-begin' and `:contents-end', `:level', `:priority',
915 `:raw-value', `:tags', `:todo-keyword', `:todo-type',
916 `:scheduled', `:deadline', `:closed' and `:post-blank' keywords.
918 The plist also contains any property set in the property drawer,
919 with its name in upper cases and colons added at the
920 beginning (e.g., `:CUSTOM_ID').
922 When optional argument RAW-SECONDARY-P is non-nil, inline-task's
923 title will not be parsed as a secondary string, but as a plain
926 Assume point is at beginning of the inline task."
928 (let* ((begin (point))
929 (components (org-heading-components))
930 (todo (nth 2 components
))
932 (if (member todo org-done-keywords
) 'done
'todo
)))
933 (tags (let ((raw-tags (nth 5 components
)))
934 (and raw-tags
(org-split-string raw-tags
":"))))
935 (raw-value (or (nth 4 components
) ""))
936 ;; Upcase property names. It avoids confusion between
937 ;; properties obtained through property drawer and default
938 ;; properties from the parser (e.g. `:end' and :END:)
945 (intern (concat ":" (upcase (car p
))))
947 (org-entry-properties nil
'standard
))
950 ;; Read time properties on the line below the inlinetask
953 (when (progn (forward-line)
954 (looking-at org-planning-or-clock-line-re
))
955 (let ((end (line-end-position)) plist
)
956 (while (re-search-forward
957 org-keyword-time-not-clock-regexp end t
)
958 (goto-char (match-end 1))
959 (skip-chars-forward " \t")
960 (let ((keyword (match-string 1))
961 (time (org-element-timestamp-parser)))
962 (cond ((equal keyword org-scheduled-string
)
963 (setq plist
(plist-put plist
:scheduled time
)))
964 ((equal keyword org-deadline-string
)
965 (setq plist
(plist-put plist
:deadline time
)))
966 (t (setq plist
(plist-put plist
:closed time
))))))
968 (task-end (save-excursion
970 (and (re-search-forward org-outline-regexp-bol limit t
)
971 (org-looking-at-p "END[ \t]*$")
972 (line-beginning-position))))
973 (contents-begin (progn (forward-line)
974 (and task-end
(< (point) task-end
) (point))))
975 (hidden (and contents-begin
(org-invisible-p2)))
976 (contents-end (and contents-begin task-end
))
977 (before-blank (if (not task-end
) (point)
981 (end (progn (skip-chars-forward " \r\t\n" limit
)
982 (if (eobp) (point) (line-beginning-position))))
986 (list :raw-value raw-value
990 :contents-begin contents-begin
991 :contents-end contents-end
992 :level
(nth 1 components
)
993 :priority
(nth 3 components
)
997 :post-blank
(count-lines before-blank end
))
1000 (org-element-put-property
1002 (if raw-secondary-p raw-value
1003 (org-element-parse-secondary-string
1005 (org-element-restriction 'inlinetask
)
1008 (defun org-element-inlinetask-interpreter (inlinetask contents
)
1009 "Interpret INLINETASK element as Org syntax.
1010 CONTENTS is the contents of inlinetask."
1011 (let* ((level (org-element-property :level inlinetask
))
1012 (todo (org-element-property :todo-keyword inlinetask
))
1013 (priority (org-element-property :priority inlinetask
))
1014 (title (org-element-interpret-data
1015 (org-element-property :title inlinetask
)))
1016 (tags (let ((tag-list (org-element-property :tags inlinetask
)))
1018 (format ":%s:" (mapconcat 'identity tag-list
":")))))
1019 (task (concat (make-string level ?
*)
1020 (and todo
(concat " " todo
))
1022 (format " [#%s]" (char-to-string priority
)))
1023 (and title
(concat " " title
)))))
1028 ((zerop org-tags-column
) (format " %s" tags
))
1029 ((< org-tags-column
0)
1032 (max (- (+ org-tags-column
(length task
) (length tags
))) 1)
1037 (make-string (max (- org-tags-column
(length task
)) 1) ?
)
1039 ;; Prefer degenerate inlinetasks when there are no
1044 (make-string level ?
*) " END")))))
1049 (defun org-element-item-parser (limit struct
&optional raw-secondary-p
)
1052 STRUCT is the structure of the plain list.
1054 Return a list whose CAR is `item' and CDR is a plist containing
1055 `:bullet', `:begin', `:end', `:contents-begin', `:contents-end',
1056 `:checkbox', `:counter', `:tag', `:structure', `:hiddenp' and
1057 `:post-blank' keywords.
1059 When optional argument RAW-SECONDARY-P is non-nil, item's tag, if
1060 any, will not be parsed as a secondary string, but as a plain
1063 Assume point is at the beginning of the item."
1066 (looking-at org-list-full-item-re
)
1067 (let* ((begin (point))
1068 (bullet (org-match-string-no-properties 1))
1069 (checkbox (let ((box (org-match-string-no-properties 3)))
1070 (cond ((equal "[ ]" box
) 'off
)
1071 ((equal "[X]" box
) 'on
)
1072 ((equal "[-]" box
) 'trans
))))
1073 (counter (let ((c (org-match-string-no-properties 2)))
1077 ((string-match "[A-Za-z]" c
)
1078 (- (string-to-char (upcase (match-string 0 c
)))
1080 ((string-match "[0-9]+" c
)
1081 (string-to-number (match-string 0 c
)))))))
1082 (end (save-excursion (goto-char (org-list-get-item-end begin struct
))
1083 (unless (bolp) (forward-line))
1087 ;; Ignore tags in un-ordered lists: they are just
1088 ;; a part of item's body.
1089 (if (and (match-beginning 4)
1090 (save-match-data (string-match "[.)]" bullet
)))
1093 (skip-chars-forward " \r\t\n" limit
)
1094 ;; If first line isn't empty, contents really start
1095 ;; at the text after item's meta-data.
1096 (if (= (point-at-bol) begin
) (point) (point-at-bol))))
1097 (hidden (progn (forward-line)
1098 (and (not (= (point) end
)) (org-invisible-p2))))
1099 (contents-end (progn (goto-char end
)
1100 (skip-chars-backward " \r\t\n")
1105 (list :bullet bullet
1108 ;; CONTENTS-BEGIN and CONTENTS-END may be
1109 ;; mixed up in the case of an empty item
1110 ;; separated from the next by a blank line.
1111 ;; Thus ensure the former is always the
1113 :contents-begin
(min contents-begin contents-end
)
1114 :contents-end
(max contents-begin contents-end
)
1119 :post-blank
(count-lines contents-end end
)))))
1120 (org-element-put-property
1122 (let ((raw-tag (org-list-get-tag begin struct
)))
1124 (if raw-secondary-p raw-tag
1125 (org-element-parse-secondary-string
1126 raw-tag
(org-element-restriction 'item
) item
))))))))
1128 (defun org-element-item-interpreter (item contents
)
1129 "Interpret ITEM element as Org syntax.
1130 CONTENTS is the contents of the element."
1131 (let* ((bullet (let ((bullet (org-element-property :bullet item
)))
1132 (org-list-bullet-string
1133 (cond ((not (string-match "[0-9a-zA-Z]" bullet
)) "- ")
1134 ((eq org-plain-list-ordered-item-terminator ?\
)) "1)")
1136 (checkbox (org-element-property :checkbox item
))
1137 (counter (org-element-property :counter item
))
1138 (tag (let ((tag (org-element-property :tag item
)))
1139 (and tag
(org-element-interpret-data tag
))))
1140 ;; Compute indentation.
1141 (ind (make-string (length bullet
) 32))
1142 (item-starts-with-par-p
1143 (eq (org-element-type (car (org-element-contents item
)))
1148 (and counter
(format "[@%d] " counter
))
1153 (and tag
(format "%s :: " tag
))
1155 (let ((contents (replace-regexp-in-string
1156 "\\(^\\)[ \t]*\\S-" ind contents nil nil
1)))
1157 (if item-starts-with-par-p
(org-trim contents
)
1158 (concat "\n" contents
)))))))
1163 (defun org-element--list-struct (limit)
1164 ;; Return structure of list at point. Internal function. See
1165 ;; `org-list-struct' for details.
1166 (let ((case-fold-search t
)
1168 (item-re (org-item-re))
1169 (drawers-re (concat ":\\("
1170 (mapconcat 'regexp-quote org-drawers
"\\|")
1172 (inlinetask-re (and (featurep 'org-inlinetask
) "^\\*+ "))
1178 ;; At limit: end all items.
1181 (let ((end (progn (skip-chars-backward " \r\t\n")
1184 (dolist (item items
(sort (nconc items struct
)
1185 'car-less-than-car
))
1186 (setcar (nthcdr 6 item
) end
)))))
1187 ;; At list end: end all items.
1188 ((looking-at org-list-end-re
)
1189 (throw 'exit
(dolist (item items
(sort (nconc items struct
)
1190 'car-less-than-car
))
1191 (setcar (nthcdr 6 item
) (point)))))
1192 ;; At a new item: end previous sibling.
1193 ((looking-at item-re
)
1194 (let ((ind (save-excursion (skip-chars-forward " \t")
1196 (setq top-ind
(min top-ind ind
))
1197 (while (and items
(<= ind
(nth 1 (car items
))))
1198 (let ((item (pop items
)))
1199 (setcar (nthcdr 6 item
) (point))
1200 (push item struct
)))
1201 (push (progn (looking-at org-list-full-item-re
)
1202 (let ((bullet (match-string-no-properties 1)))
1206 (match-string-no-properties 2) ; counter
1207 (match-string-no-properties 3) ; checkbox
1209 (and (save-match-data
1210 (string-match "[-+*]" bullet
))
1211 (match-string-no-properties 4))
1212 ;; Ending position, unknown so far.
1216 ;; Skip empty lines.
1217 ((looking-at "^[ \t]*$") (forward-line))
1218 ;; Skip inline tasks and blank lines along the way.
1219 ((and inlinetask-re
(looking-at inlinetask-re
))
1221 (let ((origin (point)))
1222 (when (re-search-forward inlinetask-re limit t
)
1223 (if (org-looking-at-p "END[ \t]*$") (forward-line)
1224 (goto-char origin
)))))
1225 ;; At some text line. Check if it ends any previous item.
1227 (let ((ind (progn (skip-chars-forward " \t") (current-column))))
1228 (when (<= ind top-ind
)
1229 (skip-chars-backward " \r\t\n")
1231 (while (<= ind
(nth 1 (car items
)))
1232 (let ((item (pop items
)))
1233 (setcar (nthcdr 6 item
) (line-beginning-position))
1236 (throw 'exit
(sort struct
'car-less-than-car
))))))
1237 ;; Skip blocks (any type) and drawers contents.
1239 ((and (looking-at "#\\+BEGIN\\(:\\|_\\S-+\\)")
1241 (format "^[ \t]*#\\+END%s[ \t]*$"
1242 (org-match-string-no-properties 1))
1244 ((and (looking-at drawers-re
)
1245 (re-search-forward "^[ \t]*:END:[ \t]*$" limit t
))))
1246 (forward-line))))))))
1248 (defun org-element-plain-list-parser (limit affiliated structure
)
1249 "Parse a plain list.
1251 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1252 the buffer position at the beginning of the first affiliated
1253 keyword and CDR is a plist of affiliated keywords along with
1254 their value. STRUCTURE is the structure of the plain list being
1257 Return a list whose CAR is `plain-list' and CDR is a plist
1258 containing `:type', `:begin', `:end', `:contents-begin' and
1259 `:contents-end', `:structure', `:post-blank' and
1260 `:post-affiliated' keywords.
1262 Assume point is at the beginning of the list."
1264 (let* ((struct (or structure
(org-element--list-struct limit
)))
1265 (prevs (org-list-prevs-alist struct
))
1266 (type (org-list-get-list-type (point) struct prevs
))
1267 (contents-begin (point))
1268 (begin (car affiliated
))
1270 (progn (goto-char (org-list-get-list-end (point) struct prevs
))
1271 (unless (bolp) (forward-line))
1273 (end (progn (skip-chars-forward " \r\t\n" limit
)
1274 (if (= (point) limit
) limit
(line-beginning-position)))))
1281 :contents-begin contents-begin
1282 :contents-end contents-end
1284 :post-blank
(count-lines contents-end end
)
1285 :post-affiliated contents-begin
)
1286 (cdr affiliated
))))))
1288 (defun org-element-plain-list-interpreter (plain-list contents
)
1289 "Interpret PLAIN-LIST element as Org syntax.
1290 CONTENTS is the contents of the element."
1293 (goto-char (point-min))
1298 ;;;; Property Drawer
1300 (defun org-element-property-drawer-parser (limit affiliated
)
1301 "Parse a property drawer.
1303 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1304 the buffer position at the beginning of the first affiliated
1305 keyword and CDR is a plist of affiliated keywords along with
1308 Return a list whose CAR is `property-drawer' and CDR is a plist
1309 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1310 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
1312 Assume point is at the beginning of the property drawer."
1314 (let ((case-fold-search t
))
1315 (if (not (save-excursion
1316 (re-search-forward "^[ \t]*:END:[ \t]*$" limit t
)))
1317 ;; Incomplete drawer: parse it as a paragraph.
1318 (org-element-paragraph-parser limit affiliated
)
1320 (let* ((drawer-end-line (match-beginning 0))
1321 (begin (car affiliated
))
1322 (post-affiliated (point))
1323 (contents-begin (progn (forward-line)
1324 (and (< (point) drawer-end-line
)
1326 (contents-end (and contents-begin drawer-end-line
))
1327 (hidden (org-invisible-p2))
1328 (pos-before-blank (progn (goto-char drawer-end-line
)
1331 (end (progn (skip-chars-forward " \r\t\n" limit
)
1332 (if (eobp) (point) (line-beginning-position)))))
1333 (list 'property-drawer
1338 :contents-begin contents-begin
1339 :contents-end contents-end
1340 :post-blank
(count-lines pos-before-blank end
)
1341 :post-affiliated post-affiliated
)
1342 (cdr affiliated
)))))))))
1344 (defun org-element-property-drawer-interpreter (property-drawer contents
)
1345 "Interpret PROPERTY-DRAWER element as Org syntax.
1346 CONTENTS is the properties within the drawer."
1347 (format ":PROPERTIES:\n%s:END:" contents
))
1352 (defun org-element-quote-block-parser (limit affiliated
)
1353 "Parse a quote block.
1355 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1356 the buffer position at the beginning of the first affiliated
1357 keyword and CDR is a plist of affiliated keywords along with
1360 Return a list whose CAR is `quote-block' and CDR is a plist
1361 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1362 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
1364 Assume point is at the beginning of the block."
1365 (let ((case-fold-search t
))
1366 (if (not (save-excursion
1367 (re-search-forward "^[ \t]*#\\+END_QUOTE[ \t]*$" limit t
)))
1368 ;; Incomplete block: parse it as a paragraph.
1369 (org-element-paragraph-parser limit affiliated
)
1370 (let ((block-end-line (match-beginning 0)))
1372 (let* ((begin (car affiliated
))
1373 (post-affiliated (point))
1374 ;; Empty blocks have no contents.
1375 (contents-begin (progn (forward-line)
1376 (and (< (point) block-end-line
)
1378 (contents-end (and contents-begin block-end-line
))
1379 (hidden (org-invisible-p2))
1380 (pos-before-blank (progn (goto-char block-end-line
)
1383 (end (progn (skip-chars-forward " \r\t\n" limit
)
1384 (if (eobp) (point) (line-beginning-position)))))
1390 :contents-begin contents-begin
1391 :contents-end contents-end
1392 :post-blank
(count-lines pos-before-blank end
)
1393 :post-affiliated post-affiliated
)
1394 (cdr affiliated
)))))))))
1396 (defun org-element-quote-block-interpreter (quote-block contents
)
1397 "Interpret QUOTE-BLOCK element as Org syntax.
1398 CONTENTS is the contents of the element."
1399 (format "#+BEGIN_QUOTE\n%s#+END_QUOTE" contents
))
1404 (defun org-element-section-parser (limit)
1407 LIMIT bounds the search.
1409 Return a list whose CAR is `section' and CDR is a plist
1410 containing `:begin', `:end', `:contents-begin', `contents-end'
1411 and `:post-blank' keywords."
1413 ;; Beginning of section is the beginning of the first non-blank
1414 ;; line after previous headline.
1415 (let ((begin (point))
1416 (end (progn (org-with-limited-levels (outline-next-heading))
1418 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
1424 :contents-begin begin
1425 :contents-end pos-before-blank
1426 :post-blank
(count-lines pos-before-blank end
))))))
1428 (defun org-element-section-interpreter (section contents
)
1429 "Interpret SECTION element as Org syntax.
1430 CONTENTS is the contents of the element."
1436 (defun org-element-special-block-parser (limit affiliated
)
1437 "Parse a special block.
1439 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1440 the buffer position at the beginning of the first affiliated
1441 keyword and CDR is a plist of affiliated keywords along with
1444 Return a list whose CAR is `special-block' and CDR is a plist
1445 containing `:type', `:begin', `:end', `:hiddenp',
1446 `:contents-begin', `:contents-end', `:post-blank' and
1447 `:post-affiliated' keywords.
1449 Assume point is at the beginning of the block."
1450 (let* ((case-fold-search t
)
1451 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
1452 (upcase (match-string-no-properties 1)))))
1453 (if (not (save-excursion
1455 (format "^[ \t]*#\\+END_%s[ \t]*$" (regexp-quote type
))
1457 ;; Incomplete block: parse it as a paragraph.
1458 (org-element-paragraph-parser limit affiliated
)
1459 (let ((block-end-line (match-beginning 0)))
1461 (let* ((begin (car affiliated
))
1462 (post-affiliated (point))
1463 ;; Empty blocks have no contents.
1464 (contents-begin (progn (forward-line)
1465 (and (< (point) block-end-line
)
1467 (contents-end (and contents-begin block-end-line
))
1468 (hidden (org-invisible-p2))
1469 (pos-before-blank (progn (goto-char block-end-line
)
1472 (end (progn (skip-chars-forward " \r\t\n" limit
)
1473 (if (eobp) (point) (line-beginning-position)))))
1474 (list 'special-block
1480 :contents-begin contents-begin
1481 :contents-end contents-end
1482 :post-blank
(count-lines pos-before-blank end
)
1483 :post-affiliated post-affiliated
)
1484 (cdr affiliated
)))))))))
1486 (defun org-element-special-block-interpreter (special-block contents
)
1487 "Interpret SPECIAL-BLOCK element as Org syntax.
1488 CONTENTS is the contents of the element."
1489 (let ((block-type (org-element-property :type special-block
)))
1490 (format "#+BEGIN_%s\n%s#+END_%s" block-type contents block-type
)))
1496 ;; For each element, a parser and an interpreter are also defined.
1497 ;; Both follow the same naming convention used for greater elements.
1499 ;; Also, as for greater elements, adding a new element type is done
1500 ;; through the following steps: implement a parser and an interpreter,
1501 ;; tweak `org-element--current-element' so that it recognizes the new
1502 ;; type and add that new type to `org-element-all-elements'.
1504 ;; As a special case, when the newly defined type is a block type,
1505 ;; `org-element-block-name-alist' has to be modified accordingly.
1510 (defun org-element-babel-call-parser (limit affiliated
)
1511 "Parse a babel call.
1513 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1514 the buffer position at the beginning of the first affiliated
1515 keyword and CDR is a plist of affiliated keywords along with
1518 Return a list whose CAR is `babel-call' and CDR is a plist
1519 containing `:begin', `:end', `:info', `:post-blank' and
1520 `:post-affiliated' as keywords."
1522 (let ((case-fold-search t
)
1523 (info (progn (looking-at org-babel-block-lob-one-liner-regexp
)
1524 (org-babel-lob-get-info)))
1525 (begin (car affiliated
))
1526 (post-affiliated (point))
1527 (pos-before-blank (progn (forward-line) (point)))
1528 (end (progn (skip-chars-forward " \r\t\n" limit
)
1529 (if (eobp) (point) (line-beginning-position)))))
1535 :post-blank
(count-lines pos-before-blank end
)
1536 :post-affiliated post-affiliated
)
1537 (cdr affiliated
))))))
1539 (defun org-element-babel-call-interpreter (babel-call contents
)
1540 "Interpret BABEL-CALL element as Org syntax.
1542 (let* ((babel-info (org-element-property :info babel-call
))
1543 (main (car babel-info
))
1544 (post-options (nth 1 babel-info
)))
1546 (if (not (string-match "\\[\\(\\[.*?\\]\\)\\]" main
)) main
1547 ;; Remove redundant square brackets.
1548 (replace-match (match-string 1 main
) nil nil main
))
1549 (and post-options
(format "[%s]" post-options
)))))
1554 (defun org-element-clock-parser (limit)
1557 LIMIT bounds the search.
1559 Return a list whose CAR is `clock' and CDR is a plist containing
1560 `:status', `:value', `:time', `:begin', `:end' and `:post-blank'
1563 (let* ((case-fold-search nil
)
1565 (value (progn (search-forward org-clock-string
(line-end-position) t
)
1566 (skip-chars-forward " \t")
1567 (org-element-timestamp-parser)))
1568 (duration (and (search-forward " => " (line-end-position) t
)
1569 (progn (skip-chars-forward " \t")
1570 (looking-at "\\(\\S-+\\)[ \t]*$"))
1571 (org-match-string-no-properties 1)))
1572 (status (if duration
'closed
'running
))
1573 (post-blank (let ((before-blank (progn (forward-line) (point))))
1574 (skip-chars-forward " \r\t\n" limit
)
1575 (skip-chars-backward " \t")
1576 (unless (bolp) (end-of-line))
1577 (count-lines before-blank
(point))))
1580 (list :status status
1585 :post-blank post-blank
)))))
1587 (defun org-element-clock-interpreter (clock contents
)
1588 "Interpret CLOCK element as Org syntax.
1590 (concat org-clock-string
" "
1591 (org-element-timestamp-interpreter
1592 (org-element-property :value clock
) nil
)
1593 (let ((duration (org-element-property :duration clock
)))
1598 (org-split-string duration
":")))))))
1603 (defun org-element-comment-parser (limit affiliated
)
1606 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1607 the buffer position at the beginning of the first affiliated
1608 keyword and CDR is a plist of affiliated keywords along with
1611 Return a list whose CAR is `comment' and CDR is a plist
1612 containing `:begin', `:end', `:value', `:post-blank',
1613 `:post-affiliated' keywords.
1615 Assume point is at comment beginning."
1617 (let* ((begin (car affiliated
))
1618 (post-affiliated (point))
1619 (value (prog2 (looking-at "[ \t]*# ?")
1620 (buffer-substring-no-properties
1621 (match-end 0) (line-end-position))
1624 ;; Get comments ending.
1626 (while (and (< (point) limit
) (looking-at "[ \t]*#\\( \\|$\\)"))
1627 ;; Accumulate lines without leading hash and first
1632 (buffer-substring-no-properties
1633 (match-end 0) (line-end-position))))
1636 (end (progn (goto-char com-end
)
1637 (skip-chars-forward " \r\t\n" limit
)
1638 (if (eobp) (point) (line-beginning-position)))))
1644 :post-blank
(count-lines com-end end
)
1645 :post-affiliated post-affiliated
)
1646 (cdr affiliated
))))))
1648 (defun org-element-comment-interpreter (comment contents
)
1649 "Interpret COMMENT element as Org syntax.
1651 (replace-regexp-in-string "^" "# " (org-element-property :value comment
)))
1656 (defun org-element-comment-block-parser (limit affiliated
)
1657 "Parse an export block.
1659 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1660 the buffer position at the beginning of the first affiliated
1661 keyword and CDR is a plist of affiliated keywords along with
1664 Return a list whose CAR is `comment-block' and CDR is a plist
1665 containing `:begin', `:end', `:hiddenp', `:value', `:post-blank'
1666 and `:post-affiliated' keywords.
1668 Assume point is at comment block beginning."
1669 (let ((case-fold-search t
))
1670 (if (not (save-excursion
1671 (re-search-forward "^[ \t]*#\\+END_COMMENT[ \t]*$" limit t
)))
1672 ;; Incomplete block: parse it as a paragraph.
1673 (org-element-paragraph-parser limit affiliated
)
1674 (let ((contents-end (match-beginning 0)))
1676 (let* ((begin (car affiliated
))
1677 (post-affiliated (point))
1678 (contents-begin (progn (forward-line) (point)))
1679 (hidden (org-invisible-p2))
1680 (pos-before-blank (progn (goto-char contents-end
)
1683 (end (progn (skip-chars-forward " \r\t\n" limit
)
1684 (if (eobp) (point) (line-beginning-position))))
1685 (value (buffer-substring-no-properties
1686 contents-begin contents-end
)))
1687 (list 'comment-block
1693 :post-blank
(count-lines pos-before-blank end
)
1694 :post-affiliated post-affiliated
)
1695 (cdr affiliated
)))))))))
1697 (defun org-element-comment-block-interpreter (comment-block contents
)
1698 "Interpret COMMENT-BLOCK element as Org syntax.
1700 (format "#+BEGIN_COMMENT\n%s#+END_COMMENT"
1701 (org-remove-indentation (org-element-property :value comment-block
))))
1706 (defun org-element-diary-sexp-parser (limit affiliated
)
1707 "Parse a diary sexp.
1709 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1710 the buffer position at the beginning of the first affiliated
1711 keyword and CDR is a plist of affiliated keywords along with
1714 Return a list whose CAR is `diary-sexp' and CDR is a plist
1715 containing `:begin', `:end', `:value', `:post-blank' and
1716 `:post-affiliated' keywords."
1718 (let ((begin (car affiliated
))
1719 (post-affiliated (point))
1720 (value (progn (looking-at "\\(%%(.*\\)[ \t]*$")
1721 (org-match-string-no-properties 1)))
1722 (pos-before-blank (progn (forward-line) (point)))
1723 (end (progn (skip-chars-forward " \r\t\n" limit
)
1724 (if (eobp) (point) (line-beginning-position)))))
1730 :post-blank
(count-lines pos-before-blank end
)
1731 :post-affiliated post-affiliated
)
1732 (cdr affiliated
))))))
1734 (defun org-element-diary-sexp-interpreter (diary-sexp contents
)
1735 "Interpret DIARY-SEXP as Org syntax.
1737 (org-element-property :value diary-sexp
))
1742 (defun org-element--remove-indentation (s &optional n
)
1743 "Remove maximum common indentation in string S and return it.
1744 When optional argument N is a positive integer, remove exactly
1745 that much characters from indentation, if possible, or return
1746 S as-is otherwise. Unlike to `org-remove-indentation', this
1747 function doesn't call `untabify' on S."
1751 (goto-char (point-min))
1752 ;; Find maximum common indentation, if not specified.
1754 (let ((min-ind (point-max)))
1756 (while (re-search-forward "^[ \t]*\\S-" nil t
)
1757 (let ((ind (1- (current-column))))
1758 (if (zerop ind
) (throw 'exit s
)
1759 (setq min-ind
(min min-ind ind
))))))
1762 ;; Remove exactly N indentation, but give up if not possible.
1764 (let ((ind (progn (skip-chars-forward " \t") (current-column))))
1765 (cond ((eolp) (delete-region (line-beginning-position) (point)))
1766 ((< ind n
) (throw 'exit s
))
1767 (t (org-indent-line-to (- ind n
))))
1771 (defun org-element-example-block-parser (limit affiliated
)
1772 "Parse an example block.
1774 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1775 the buffer position at the beginning of the first affiliated
1776 keyword and CDR is a plist of affiliated keywords along with
1779 Return a list whose CAR is `example-block' and CDR is a plist
1780 containing `:begin', `:end', `:number-lines', `:preserve-indent',
1781 `:retain-labels', `:use-labels', `:label-fmt', `:hiddenp',
1782 `:switches', `:value', `:post-blank' and `:post-affiliated'
1784 (let ((case-fold-search t
))
1785 (if (not (save-excursion
1786 (re-search-forward "^[ \t]*#\\+END_EXAMPLE[ \t]*$" limit t
)))
1787 ;; Incomplete block: parse it as a paragraph.
1788 (org-element-paragraph-parser limit affiliated
)
1789 (let ((contents-end (match-beginning 0)))
1793 (looking-at "^[ \t]*#\\+BEGIN_EXAMPLE\\(?: +\\(.*\\)\\)?")
1794 (org-match-string-no-properties 1)))
1795 ;; Switches analysis
1797 (cond ((not switches
) nil
)
1798 ((string-match "-n\\>" switches
) 'new
)
1799 ((string-match "+n\\>" switches
) 'continued
)))
1801 (or org-src-preserve-indentation
1802 (and switches
(string-match "-i\\>" switches
))))
1803 ;; Should labels be retained in (or stripped from) example
1807 (not (string-match "-r\\>" switches
))
1808 (and number-lines
(string-match "-k\\>" switches
))))
1809 ;; What should code-references use - labels or
1814 (not (string-match "-k\\>" switches
)))))
1817 (string-match "-l +\"\\([^\"\n]+\\)\"" switches
)
1818 (match-string 1 switches
)))
1819 ;; Standard block parsing.
1820 (begin (car affiliated
))
1821 (post-affiliated (point))
1822 (block-ind (progn (skip-chars-forward " \t") (current-column)))
1823 (contents-begin (progn (forward-line) (point)))
1824 (hidden (org-invisible-p2))
1825 (value (org-element--remove-indentation
1826 (org-unescape-code-in-string
1827 (buffer-substring-no-properties
1828 contents-begin contents-end
))
1829 (and preserve-indent block-ind
)))
1830 (pos-before-blank (progn (goto-char contents-end
)
1833 (end (progn (skip-chars-forward " \r\t\n" limit
)
1834 (if (eobp) (point) (line-beginning-position)))))
1835 (list 'example-block
1841 :number-lines number-lines
1842 :preserve-indent preserve-indent
1843 :retain-labels retain-labels
1844 :use-labels use-labels
1845 :label-fmt label-fmt
1847 :post-blank
(count-lines pos-before-blank end
)
1848 :post-affiliated post-affiliated
)
1849 (cdr affiliated
)))))))))
1851 (defun org-element-example-block-interpreter (example-block contents
)
1852 "Interpret EXAMPLE-BLOCK element as Org syntax.
1854 (let ((switches (org-element-property :switches example-block
)))
1855 (concat "#+BEGIN_EXAMPLE" (and switches
(concat " " switches
)) "\n"
1856 (org-escape-code-in-string
1857 (org-element-property :value example-block
))
1863 (defun org-element-export-block-parser (limit affiliated
)
1864 "Parse an export block.
1866 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1867 the buffer position at the beginning of the first affiliated
1868 keyword and CDR is a plist of affiliated keywords along with
1871 Return a list whose CAR is `export-block' and CDR is a plist
1872 containing `:begin', `:end', `:type', `:hiddenp', `:value',
1873 `:post-blank' and `:post-affiliated' keywords.
1875 Assume point is at export-block beginning."
1876 (let* ((case-fold-search t
)
1877 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
1878 (upcase (org-match-string-no-properties 1)))))
1879 (if (not (save-excursion
1881 (format "^[ \t]*#\\+END_%s[ \t]*$" type
) limit t
)))
1882 ;; Incomplete block: parse it as a paragraph.
1883 (org-element-paragraph-parser limit affiliated
)
1884 (let ((contents-end (match-beginning 0)))
1886 (let* ((begin (car affiliated
))
1887 (post-affiliated (point))
1888 (contents-begin (progn (forward-line) (point)))
1889 (hidden (org-invisible-p2))
1890 (pos-before-blank (progn (goto-char contents-end
)
1893 (end (progn (skip-chars-forward " \r\t\n" limit
)
1894 (if (eobp) (point) (line-beginning-position))))
1895 (value (buffer-substring-no-properties contents-begin
1904 :post-blank
(count-lines pos-before-blank end
)
1905 :post-affiliated post-affiliated
)
1906 (cdr affiliated
)))))))))
1908 (defun org-element-export-block-interpreter (export-block contents
)
1909 "Interpret EXPORT-BLOCK element as Org syntax.
1911 (let ((type (org-element-property :type export-block
)))
1912 (concat (format "#+BEGIN_%s\n" type
)
1913 (org-element-property :value export-block
)
1914 (format "#+END_%s" type
))))
1919 (defun org-element-fixed-width-parser (limit affiliated
)
1920 "Parse a fixed-width section.
1922 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1923 the buffer position at the beginning of the first affiliated
1924 keyword and CDR is a plist of affiliated keywords along with
1927 Return a list whose CAR is `fixed-width' and CDR is a plist
1928 containing `:begin', `:end', `:value', `:post-blank' and
1929 `:post-affiliated' keywords.
1931 Assume point is at the beginning of the fixed-width area."
1933 (let* ((begin (car affiliated
))
1934 (post-affiliated (point))
1938 (while (and (< (point) limit
)
1939 (looking-at "[ \t]*:\\( \\|$\\)"))
1940 ;; Accumulate text without starting colons.
1943 (buffer-substring-no-properties
1944 (match-end 0) (point-at-eol))
1948 (end (progn (skip-chars-forward " \r\t\n" limit
)
1949 (if (eobp) (point) (line-beginning-position)))))
1955 :post-blank
(count-lines end-area end
)
1956 :post-affiliated post-affiliated
)
1957 (cdr affiliated
))))))
1959 (defun org-element-fixed-width-interpreter (fixed-width contents
)
1960 "Interpret FIXED-WIDTH element as Org syntax.
1962 (let ((value (org-element-property :value fixed-width
)))
1964 (replace-regexp-in-string
1966 (if (string-match "\n\\'" value
) (substring value
0 -
1) value
)))))
1969 ;;;; Horizontal Rule
1971 (defun org-element-horizontal-rule-parser (limit affiliated
)
1972 "Parse an horizontal rule.
1974 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1975 the buffer position at the beginning of the first affiliated
1976 keyword and CDR is a plist of affiliated keywords along with
1979 Return a list whose CAR is `horizontal-rule' and CDR is a plist
1980 containing `:begin', `:end', `:post-blank' and `:post-affiliated'
1983 (let ((begin (car affiliated
))
1984 (post-affiliated (point))
1985 (post-hr (progn (forward-line) (point)))
1986 (end (progn (skip-chars-forward " \r\t\n" limit
)
1987 (if (eobp) (point) (line-beginning-position)))))
1988 (list 'horizontal-rule
1992 :post-blank
(count-lines post-hr end
)
1993 :post-affiliated post-affiliated
)
1994 (cdr affiliated
))))))
1996 (defun org-element-horizontal-rule-interpreter (horizontal-rule contents
)
1997 "Interpret HORIZONTAL-RULE element as Org syntax.
2004 (defun org-element-keyword-parser (limit affiliated
)
2005 "Parse a keyword at point.
2007 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2008 the buffer position at the beginning of the first affiliated
2009 keyword and CDR is a plist of affiliated keywords along with
2012 Return a list whose CAR is `keyword' and CDR is a plist
2013 containing `:key', `:value', `:begin', `:end', `:post-blank' and
2014 `:post-affiliated' keywords."
2016 (let ((begin (car affiliated
))
2017 (post-affiliated (point))
2018 (key (progn (looking-at "[ \t]*#\\+\\(\\S-+*\\):")
2019 (upcase (org-match-string-no-properties 1))))
2020 (value (org-trim (buffer-substring-no-properties
2021 (match-end 0) (point-at-eol))))
2022 (pos-before-blank (progn (forward-line) (point)))
2023 (end (progn (skip-chars-forward " \r\t\n" limit
)
2024 (if (eobp) (point) (line-beginning-position)))))
2031 :post-blank
(count-lines pos-before-blank end
)
2032 :post-affiliated post-affiliated
)
2033 (cdr affiliated
))))))
2035 (defun org-element-keyword-interpreter (keyword contents
)
2036 "Interpret KEYWORD element as Org syntax.
2039 (org-element-property :key keyword
)
2040 (org-element-property :value keyword
)))
2043 ;;;; Latex Environment
2045 (defun org-element-latex-environment-parser (limit affiliated
)
2046 "Parse a LaTeX environment.
2048 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2049 the buffer position at the beginning of the first affiliated
2050 keyword and CDR is a plist of affiliated keywords along with
2053 Return a list whose CAR is `latex-environment' and CDR is a plist
2054 containing `:begin', `:end', `:value', `:post-blank' and
2055 `:post-affiliated' keywords.
2057 Assume point is at the beginning of the latex environment."
2059 (let ((case-fold-search t
)
2060 (code-begin (point)))
2061 (looking-at "[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}")
2062 (if (not (re-search-forward (format "^[ \t]*\\\\end{%s}[ \t]*$"
2063 (regexp-quote (match-string 1)))
2065 ;; Incomplete latex environment: parse it as a paragraph.
2066 (org-element-paragraph-parser limit affiliated
)
2067 (let* ((code-end (progn (forward-line) (point)))
2068 (begin (car affiliated
))
2069 (value (buffer-substring-no-properties code-begin code-end
))
2070 (end (progn (skip-chars-forward " \r\t\n" limit
)
2071 (if (eobp) (point) (line-beginning-position)))))
2072 (list 'latex-environment
2077 :post-blank
(count-lines code-end end
)
2078 :post-affiliated code-begin
)
2079 (cdr affiliated
))))))))
2081 (defun org-element-latex-environment-interpreter (latex-environment contents
)
2082 "Interpret LATEX-ENVIRONMENT element as Org syntax.
2084 (org-element-property :value latex-environment
))
2089 (defun org-element-node-property-parser (limit)
2090 "Parse a node-property at point.
2092 LIMIT bounds the search.
2094 Return a list whose CAR is `node-property' and CDR is a plist
2095 containing `:key', `:value', `:begin', `:end' and `:post-blank'
2098 (looking-at org-property-re
)
2099 (let ((case-fold-search t
)
2101 (key (org-match-string-no-properties 2))
2102 (value (org-match-string-no-properties 3))
2103 (pos-before-blank (progn (forward-line) (point)))
2104 (end (progn (skip-chars-forward " \r\t\n" limit
)
2105 (if (eobp) (point) (point-at-bol)))))
2106 (list 'node-property
2111 :post-blank
(count-lines pos-before-blank end
))))))
2113 (defun org-element-node-property-interpreter (node-property contents
)
2114 "Interpret NODE-PROPERTY element as Org syntax.
2116 (format org-property-format
2117 (format ":%s:" (org-element-property :key node-property
))
2118 (org-element-property :value node-property
)))
2123 (defun org-element-paragraph-parser (limit affiliated
)
2126 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2127 the buffer position at the beginning of the first affiliated
2128 keyword and CDR is a plist of affiliated keywords along with
2131 Return a list whose CAR is `paragraph' and CDR is a plist
2132 containing `:begin', `:end', `:contents-begin' and
2133 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
2135 Assume point is at the beginning of the paragraph."
2137 (let* ((begin (car affiliated
))
2138 (contents-begin (point))
2140 (let ((case-fold-search t
))
2142 (if (not (re-search-forward
2143 org-element-paragraph-separate limit
'm
))
2145 ;; A matching `org-element-paragraph-separate' is not
2146 ;; necessarily the end of the paragraph. In
2147 ;; particular, lines starting with # or : as a first
2148 ;; non-space character are ambiguous. We have to
2149 ;; check if they are valid Org syntax (e.g., not an
2150 ;; incomplete keyword).
2154 ;; There's no ambiguity for other symbols or
2155 ;; empty lines: stop here.
2156 (looking-at "[ \t]*\\(?:[^:#]\\|$\\)")
2157 ;; Stop at valid fixed-width areas.
2158 (looking-at "[ \t]*:\\(?: \\|$\\)")
2160 (and (looking-at org-drawer-regexp
)
2163 "^[ \t]*:END:[ \t]*$" limit t
)))
2164 ;; Stop at valid comments.
2165 (looking-at "[ \t]*#\\(?: \\|$\\)")
2166 ;; Stop at valid dynamic blocks.
2167 (and (looking-at org-dblock-start-re
)
2170 "^[ \t]*#\\+END:?[ \t]*$" limit t
)))
2171 ;; Stop at valid blocks.
2172 (and (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
2175 (format "^[ \t]*#\\+END_%s[ \t]*$"
2177 (org-match-string-no-properties 1)))
2179 ;; Stop at valid latex environments.
2181 "[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}")
2184 (format "^[ \t]*\\\\end{%s}[ \t]*$"
2186 (org-match-string-no-properties 1)))
2188 ;; Stop at valid keywords.
2189 (looking-at "[ \t]*#\\+\\S-+:")
2190 ;; Skip everything else.
2194 (re-search-forward org-element-paragraph-separate
2196 (beginning-of-line)))
2197 (if (= (point) limit
) limit
2198 (goto-char (line-beginning-position)))))
2199 (contents-end (progn (skip-chars-backward " \r\t\n" contents-begin
)
2202 (end (progn (skip-chars-forward " \r\t\n" limit
)
2203 (if (eobp) (point) (line-beginning-position)))))
2208 :contents-begin contents-begin
2209 :contents-end contents-end
2210 :post-blank
(count-lines before-blank end
)
2211 :post-affiliated contents-begin
)
2212 (cdr affiliated
))))))
2214 (defun org-element-paragraph-interpreter (paragraph contents
)
2215 "Interpret PARAGRAPH element as Org syntax.
2216 CONTENTS is the contents of the element."
2222 (defun org-element-planning-parser (limit)
2225 LIMIT bounds the search.
2227 Return a list whose CAR is `planning' and CDR is a plist
2228 containing `:closed', `:deadline', `:scheduled', `:begin', `:end'
2229 and `:post-blank' keywords."
2231 (let* ((case-fold-search nil
)
2233 (post-blank (let ((before-blank (progn (forward-line) (point))))
2234 (skip-chars-forward " \r\t\n" limit
)
2235 (skip-chars-backward " \t")
2236 (unless (bolp) (end-of-line))
2237 (count-lines before-blank
(point))))
2239 closed deadline scheduled
)
2241 (while (re-search-forward org-keyword-time-not-clock-regexp end t
)
2242 (goto-char (match-end 1))
2243 (skip-chars-forward " \t" end
)
2244 (let ((keyword (match-string 1))
2245 (time (org-element-timestamp-parser)))
2246 (cond ((equal keyword org-closed-string
) (setq closed time
))
2247 ((equal keyword org-deadline-string
) (setq deadline time
))
2248 (t (setq scheduled time
)))))
2250 (list :closed closed
2252 :scheduled scheduled
2255 :post-blank post-blank
)))))
2257 (defun org-element-planning-interpreter (planning contents
)
2258 "Interpret PLANNING element as Org syntax.
2263 (list (let ((deadline (org-element-property :deadline planning
)))
2265 (concat org-deadline-string
" "
2266 (org-element-timestamp-interpreter deadline nil
))))
2267 (let ((scheduled (org-element-property :scheduled planning
)))
2269 (concat org-scheduled-string
" "
2270 (org-element-timestamp-interpreter scheduled nil
))))
2271 (let ((closed (org-element-property :closed planning
)))
2273 (concat org-closed-string
" "
2274 (org-element-timestamp-interpreter closed nil
))))))
2280 (defun org-element-quote-section-parser (limit)
2281 "Parse a quote section.
2283 LIMIT bounds the search.
2285 Return a list whose CAR is `quote-section' and CDR is a plist
2286 containing `:begin', `:end', `:value' and `:post-blank' keywords.
2288 Assume point is at beginning of the section."
2290 (let* ((begin (point))
2291 (end (progn (org-with-limited-levels (outline-next-heading))
2293 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
2296 (value (buffer-substring-no-properties begin pos-before-blank
)))
2297 (list 'quote-section
2301 :post-blank
(count-lines pos-before-blank end
))))))
2303 (defun org-element-quote-section-interpreter (quote-section contents
)
2304 "Interpret QUOTE-SECTION element as Org syntax.
2306 (org-element-property :value quote-section
))
2311 (defun org-element-src-block-parser (limit affiliated
)
2314 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2315 the buffer position at the beginning of the first affiliated
2316 keyword and CDR is a plist of affiliated keywords along with
2319 Return a list whose CAR is `src-block' and CDR is a plist
2320 containing `:language', `:switches', `:parameters', `:begin',
2321 `:end', `:hiddenp', `:number-lines', `:retain-labels',
2322 `:use-labels', `:label-fmt', `:preserve-indent', `:value',
2323 `:post-blank' and `:post-affiliated' keywords.
2325 Assume point is at the beginning of the block."
2326 (let ((case-fold-search t
))
2327 (if (not (save-excursion (re-search-forward "^[ \t]*#\\+END_SRC[ \t]*$"
2329 ;; Incomplete block: parse it as a paragraph.
2330 (org-element-paragraph-parser limit affiliated
)
2331 (let ((contents-end (match-beginning 0)))
2333 (let* ((begin (car affiliated
))
2334 (post-affiliated (point))
2335 ;; Get language as a string.
2339 (concat "^[ \t]*#\\+BEGIN_SRC"
2340 "\\(?: +\\(\\S-+\\)\\)?"
2341 "\\(\\(?: +\\(?:-l \".*?\"\\|[-+][A-Za-z]\\)\\)+\\)?"
2343 (org-match-string-no-properties 1)))
2345 (switches (org-match-string-no-properties 2))
2347 (parameters (org-match-string-no-properties 3))
2348 ;; Switches analysis
2350 (cond ((not switches
) nil
)
2351 ((string-match "-n\\>" switches
) 'new
)
2352 ((string-match "+n\\>" switches
) 'continued
)))
2353 (preserve-indent (or org-src-preserve-indentation
2355 (string-match "-i\\>" switches
))))
2358 (string-match "-l +\"\\([^\"\n]+\\)\"" switches
)
2359 (match-string 1 switches
)))
2360 ;; Should labels be retained in (or stripped from)
2364 (not (string-match "-r\\>" switches
))
2365 (and number-lines
(string-match "-k\\>" switches
))))
2366 ;; What should code-references use - labels or
2371 (not (string-match "-k\\>" switches
)))))
2373 (block-ind (progn (skip-chars-forward " \t") (current-column)))
2374 ;; Get visibility status.
2375 (hidden (progn (forward-line) (org-invisible-p2)))
2377 (value (org-element--remove-indentation
2378 (org-unescape-code-in-string
2379 (buffer-substring-no-properties
2380 (point) contents-end
))
2381 (and preserve-indent block-ind
)))
2382 (pos-before-blank (progn (goto-char contents-end
)
2385 ;; Get position after ending blank lines.
2386 (end (progn (skip-chars-forward " \r\t\n" limit
)
2387 (if (eobp) (point) (line-beginning-position)))))
2390 (list :language language
2391 :switches
(and (org-string-nw-p switches
)
2392 (org-trim switches
))
2393 :parameters
(and (org-string-nw-p parameters
)
2394 (org-trim parameters
))
2397 :number-lines number-lines
2398 :preserve-indent preserve-indent
2399 :retain-labels retain-labels
2400 :use-labels use-labels
2401 :label-fmt label-fmt
2404 :post-blank
(count-lines pos-before-blank end
)
2405 :post-affiliated post-affiliated
)
2406 (cdr affiliated
)))))))))
2408 (defun org-element-src-block-interpreter (src-block contents
)
2409 "Interpret SRC-BLOCK element as Org syntax.
2411 (let ((lang (org-element-property :language src-block
))
2412 (switches (org-element-property :switches src-block
))
2413 (params (org-element-property :parameters src-block
))
2414 (value (let ((val (org-element-property :value src-block
)))
2416 ((org-element-property :preserve-indent src-block
) val
)
2417 ((zerop org-edit-src-content-indentation
) val
)
2419 (let ((ind (make-string
2420 org-edit-src-content-indentation
32)))
2421 (replace-regexp-in-string
2422 "\\(^\\)[ \t]*\\S-" ind val nil nil
1)))))))
2423 (concat (format "#+BEGIN_SRC%s\n"
2424 (concat (and lang
(concat " " lang
))
2425 (and switches
(concat " " switches
))
2426 (and params
(concat " " params
))))
2427 (org-escape-code-in-string value
)
2433 (defun org-element-table-parser (limit affiliated
)
2434 "Parse a table at point.
2436 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2437 the buffer position at the beginning of the first affiliated
2438 keyword and CDR is a plist of affiliated keywords along with
2441 Return a list whose CAR is `table' and CDR is a plist containing
2442 `:begin', `:end', `:tblfm', `:type', `:contents-begin',
2443 `:contents-end', `:value', `:post-blank' and `:post-affiliated'
2446 Assume point is at the beginning of the table."
2448 (let* ((case-fold-search t
)
2449 (table-begin (point))
2450 (type (if (org-at-table.el-p
) 'table.el
'org
))
2451 (begin (car affiliated
))
2453 (if (re-search-forward org-table-any-border-regexp limit
'm
)
2454 (goto-char (match-beginning 0))
2457 (while (looking-at "[ \t]*#\\+TBLFM: +\\(.*\\)[ \t]*$")
2458 (push (org-match-string-no-properties 1) acc
)
2461 (pos-before-blank (point))
2462 (end (progn (skip-chars-forward " \r\t\n" limit
)
2463 (if (eobp) (point) (line-beginning-position)))))
2470 ;; Only `org' tables have contents. `table.el' tables
2471 ;; use a `:value' property to store raw table as
2473 :contents-begin
(and (eq type
'org
) table-begin
)
2474 :contents-end
(and (eq type
'org
) table-end
)
2475 :value
(and (eq type
'table.el
)
2476 (buffer-substring-no-properties
2477 table-begin table-end
))
2478 :post-blank
(count-lines pos-before-blank end
)
2479 :post-affiliated table-begin
)
2480 (cdr affiliated
))))))
2482 (defun org-element-table-interpreter (table contents
)
2483 "Interpret TABLE element as Org syntax.
2485 (if (eq (org-element-property :type table
) 'table.el
)
2486 (org-remove-indentation (org-element-property :value table
))
2487 (concat (with-temp-buffer (insert contents
)
2490 (mapconcat (lambda (fm) (concat "#+TBLFM: " fm
))
2491 (reverse (org-element-property :tblfm table
))
2497 (defun org-element-table-row-parser (limit)
2498 "Parse table row at point.
2500 LIMIT bounds the search.
2502 Return a list whose CAR is `table-row' and CDR is a plist
2503 containing `:begin', `:end', `:contents-begin', `:contents-end',
2504 `:type' and `:post-blank' keywords."
2506 (let* ((type (if (looking-at "^[ \t]*|-") 'rule
'standard
))
2508 ;; A table rule has no contents. In that case, ensure
2509 ;; CONTENTS-BEGIN matches CONTENTS-END.
2510 (contents-begin (and (eq type
'standard
)
2511 (search-forward "|")
2513 (contents-end (and (eq type
'standard
)
2516 (skip-chars-backward " \t")
2518 (end (progn (forward-line) (point))))
2523 :contents-begin contents-begin
2524 :contents-end contents-end
2527 (defun org-element-table-row-interpreter (table-row contents
)
2528 "Interpret TABLE-ROW element as Org syntax.
2529 CONTENTS is the contents of the table row."
2530 (if (eq (org-element-property :type table-row
) 'rule
) "|-"
2531 (concat "| " contents
)))
2536 (defun org-element-verse-block-parser (limit affiliated
)
2537 "Parse a verse block.
2539 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2540 the buffer position at the beginning of the first affiliated
2541 keyword and CDR is a plist of affiliated keywords along with
2544 Return a list whose CAR is `verse-block' and CDR is a plist
2545 containing `:begin', `:end', `:contents-begin', `:contents-end',
2546 `:hiddenp', `:post-blank' and `:post-affiliated' keywords.
2548 Assume point is at beginning of the block."
2549 (let ((case-fold-search t
))
2550 (if (not (save-excursion
2551 (re-search-forward "^[ \t]*#\\+END_VERSE[ \t]*$" limit t
)))
2552 ;; Incomplete block: parse it as a paragraph.
2553 (org-element-paragraph-parser limit affiliated
)
2554 (let ((contents-end (match-beginning 0)))
2556 (let* ((begin (car affiliated
))
2557 (post-affiliated (point))
2558 (hidden (progn (forward-line) (org-invisible-p2)))
2559 (contents-begin (point))
2560 (pos-before-blank (progn (goto-char contents-end
)
2563 (end (progn (skip-chars-forward " \r\t\n" limit
)
2564 (if (eobp) (point) (line-beginning-position)))))
2569 :contents-begin contents-begin
2570 :contents-end contents-end
2572 :post-blank
(count-lines pos-before-blank end
)
2573 :post-affiliated post-affiliated
)
2574 (cdr affiliated
)))))))))
2576 (defun org-element-verse-block-interpreter (verse-block contents
)
2577 "Interpret VERSE-BLOCK element as Org syntax.
2578 CONTENTS is verse block contents."
2579 (format "#+BEGIN_VERSE\n%s#+END_VERSE" contents
))
2585 ;; Unlike to elements, interstices can be found between objects.
2586 ;; That's why, along with the parser, successor functions are provided
2587 ;; for each object. Some objects share the same successor (e.g.,
2588 ;; `code' and `verbatim' objects).
2590 ;; A successor must accept a single argument bounding the search. It
2591 ;; will return either a cons cell whose CAR is the object's type, as
2592 ;; a symbol, and CDR the position of its next occurrence, or nil.
2594 ;; Successors follow the naming convention:
2595 ;; org-element-NAME-successor, where NAME is the name of the
2596 ;; successor, as defined in `org-element-all-successors'.
2598 ;; Some object types (e.g., `italic') are recursive. Restrictions on
2599 ;; object types they can contain will be specified in
2600 ;; `org-element-object-restrictions'.
2602 ;; Adding a new type of object is simple. Implement a successor,
2603 ;; a parser, and an interpreter for it, all following the naming
2604 ;; convention. Register type in `org-element-all-objects' and
2605 ;; successor in `org-element-all-successors'. Maybe tweak
2606 ;; restrictions about it, and that's it.
2611 (defun org-element-bold-parser ()
2612 "Parse bold object at point.
2614 Return a list whose CAR is `bold' and CDR is a plist with
2615 `:begin', `:end', `:contents-begin' and `:contents-end' and
2616 `:post-blank' keywords.
2618 Assume point is at the first star marker."
2620 (unless (bolp) (backward-char 1))
2621 (looking-at org-emph-re
)
2622 (let ((begin (match-beginning 2))
2623 (contents-begin (match-beginning 4))
2624 (contents-end (match-end 4))
2625 (post-blank (progn (goto-char (match-end 2))
2626 (skip-chars-forward " \t")))
2631 :contents-begin contents-begin
2632 :contents-end contents-end
2633 :post-blank post-blank
)))))
2635 (defun org-element-bold-interpreter (bold contents
)
2636 "Interpret BOLD object as Org syntax.
2637 CONTENTS is the contents of the object."
2638 (format "*%s*" contents
))
2640 (defun org-element-text-markup-successor ()
2641 "Search for the next text-markup object.
2643 Return value is a cons cell whose CAR is a symbol among `bold',
2644 `italic', `underline', `strike-through', `code' and `verbatim'
2645 and CDR is beginning position."
2647 (unless (bolp) (backward-char))
2648 (when (re-search-forward org-emph-re nil t
)
2649 (let ((marker (match-string 3)))
2651 ((equal marker
"*") 'bold
)
2652 ((equal marker
"/") 'italic
)
2653 ((equal marker
"_") 'underline
)
2654 ((equal marker
"+") 'strike-through
)
2655 ((equal marker
"~") 'code
)
2656 ((equal marker
"=") 'verbatim
)
2657 (t (error "Unknown marker at %d" (match-beginning 3))))
2658 (match-beginning 2))))))
2663 (defun org-element-code-parser ()
2664 "Parse code object at point.
2666 Return a list whose CAR is `code' and CDR is a plist with
2667 `:value', `:begin', `:end' and `:post-blank' keywords.
2669 Assume point is at the first tilde marker."
2671 (unless (bolp) (backward-char 1))
2672 (looking-at org-emph-re
)
2673 (let ((begin (match-beginning 2))
2674 (value (org-match-string-no-properties 4))
2675 (post-blank (progn (goto-char (match-end 2))
2676 (skip-chars-forward " \t")))
2682 :post-blank post-blank
)))))
2684 (defun org-element-code-interpreter (code contents
)
2685 "Interpret CODE object as Org syntax.
2687 (format "~%s~" (org-element-property :value code
)))
2692 (defun org-element-entity-parser ()
2693 "Parse entity at point.
2695 Return a list whose CAR is `entity' and CDR a plist with
2696 `:begin', `:end', `:latex', `:latex-math-p', `:html', `:latin1',
2697 `:utf-8', `:ascii', `:use-brackets-p' and `:post-blank' as
2700 Assume point is at the beginning of the entity."
2702 (looking-at "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)")
2703 (let* ((value (org-entity-get (match-string 1)))
2704 (begin (match-beginning 0))
2705 (bracketsp (string= (match-string 2) "{}"))
2706 (post-blank (progn (goto-char (match-end 1))
2707 (when bracketsp
(forward-char 2))
2708 (skip-chars-forward " \t")))
2711 (list :name
(car value
)
2712 :latex
(nth 1 value
)
2713 :latex-math-p
(nth 2 value
)
2715 :ascii
(nth 4 value
)
2716 :latin1
(nth 5 value
)
2717 :utf-8
(nth 6 value
)
2720 :use-brackets-p bracketsp
2721 :post-blank post-blank
)))))
2723 (defun org-element-entity-interpreter (entity contents
)
2724 "Interpret ENTITY object as Org syntax.
2727 (org-element-property :name entity
)
2728 (when (org-element-property :use-brackets-p entity
) "{}")))
2730 (defun org-element-latex-or-entity-successor ()
2731 "Search for the next latex-fragment or entity object.
2733 Return value is a cons cell whose CAR is `entity' or
2734 `latex-fragment' and CDR is beginning position."
2736 (unless (bolp) (backward-char))
2737 (let ((matchers (cdr org-latex-regexps
))
2738 ;; ENTITY-RE matches both LaTeX commands and Org entities.
2740 "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))
2741 (when (re-search-forward
2742 (concat (mapconcat #'cadr matchers
"\\|") "\\|" entity-re
) nil t
)
2743 (goto-char (match-beginning 0))
2744 (if (looking-at entity-re
)
2745 ;; Determine if it's a real entity or a LaTeX command.
2746 (cons (if (org-entity-get (match-string 1)) 'entity
'latex-fragment
)
2747 (match-beginning 0))
2748 ;; No entity nor command: point is at a LaTeX fragment.
2749 ;; Determine its type to get the correct beginning position.
2750 (cons 'latex-fragment
2752 (dolist (e matchers
)
2753 (when (looking-at (nth 1 e
))
2754 (throw 'return
(match-beginning (nth 2 e
)))))
2760 (defun org-element-export-snippet-parser ()
2761 "Parse export snippet at point.
2763 Return a list whose CAR is `export-snippet' and CDR a plist with
2764 `:begin', `:end', `:back-end', `:value' and `:post-blank' as
2767 Assume point is at the beginning of the snippet."
2769 (re-search-forward "@@\\([-A-Za-z0-9]+\\):" nil t
)
2770 (let* ((begin (match-beginning 0))
2771 (back-end (org-match-string-no-properties 1))
2772 (value (buffer-substring-no-properties
2774 (progn (re-search-forward "@@" nil t
) (match-beginning 0))))
2775 (post-blank (skip-chars-forward " \t"))
2777 (list 'export-snippet
2778 (list :back-end back-end
2782 :post-blank post-blank
)))))
2784 (defun org-element-export-snippet-interpreter (export-snippet contents
)
2785 "Interpret EXPORT-SNIPPET object as Org syntax.
2788 (org-element-property :back-end export-snippet
)
2789 (org-element-property :value export-snippet
)))
2791 (defun org-element-export-snippet-successor ()
2792 "Search for the next export-snippet object.
2794 Return value is a cons cell whose CAR is `export-snippet' and CDR
2795 its beginning position."
2798 (when (and (re-search-forward "@@[-A-Za-z0-9]+:" nil t
)
2799 (setq beg
(match-beginning 0))
2800 (search-forward "@@" nil t
))
2801 (cons 'export-snippet beg
)))))
2804 ;;;; Footnote Reference
2806 (defun org-element-footnote-reference-parser ()
2807 "Parse footnote reference at point.
2809 Return a list whose CAR is `footnote-reference' and CDR a plist
2810 with `:label', `:type', `:inline-definition', `:begin', `:end'
2811 and `:post-blank' as keywords."
2813 (looking-at org-footnote-re
)
2814 (let* ((begin (point))
2815 (label (or (org-match-string-no-properties 2)
2816 (org-match-string-no-properties 3)
2817 (and (match-string 1)
2818 (concat "fn:" (org-match-string-no-properties 1)))))
2819 (type (if (or (not label
) (match-string 1)) 'inline
'standard
))
2820 (inner-begin (match-end 0))
2824 (while (and (> count
0) (re-search-forward "[][]" nil t
))
2825 (if (equal (match-string 0) "[") (incf count
) (decf count
)))
2827 (post-blank (progn (goto-char (1+ inner-end
))
2828 (skip-chars-forward " \t")))
2831 (list 'footnote-reference
2836 :post-blank post-blank
))))
2837 (org-element-put-property
2838 footnote-reference
:inline-definition
2839 (and (eq type
'inline
)
2840 (org-element-parse-secondary-string
2841 (buffer-substring inner-begin inner-end
)
2842 (org-element-restriction 'footnote-reference
)
2843 footnote-reference
))))))
2845 (defun org-element-footnote-reference-interpreter (footnote-reference contents
)
2846 "Interpret FOOTNOTE-REFERENCE object as Org syntax.
2848 (let ((label (or (org-element-property :label footnote-reference
) "fn:"))
2851 (org-element-property :inline-definition footnote-reference
)))
2852 (if (not inline-def
) ""
2853 (concat ":" (org-element-interpret-data inline-def
))))))
2854 (format "[%s]" (concat label def
))))
2856 (defun org-element-footnote-reference-successor ()
2857 "Search for the next footnote-reference object.
2859 Return value is a cons cell whose CAR is `footnote-reference' and
2860 CDR is beginning position."
2863 (while (re-search-forward org-footnote-re nil t
)
2865 (let ((beg (match-beginning 0))
2868 (while (re-search-forward "[][]" nil t
)
2869 (if (equal (match-string 0) "[") (incf count
) (decf count
))
2871 (throw 'exit
(cons 'footnote-reference beg
))))))))))
2874 ;;;; Inline Babel Call
2876 (defun org-element-inline-babel-call-parser ()
2877 "Parse inline babel call at point.
2879 Return a list whose CAR is `inline-babel-call' and CDR a plist
2880 with `:begin', `:end', `:info' and `:post-blank' as keywords.
2882 Assume point is at the beginning of the babel call."
2884 (unless (bolp) (backward-char))
2885 (looking-at org-babel-inline-lob-one-liner-regexp
)
2886 (let ((info (save-match-data (org-babel-lob-get-info)))
2887 (begin (match-end 1))
2888 (post-blank (progn (goto-char (match-end 0))
2889 (skip-chars-forward " \t")))
2891 (list 'inline-babel-call
2895 :post-blank post-blank
)))))
2897 (defun org-element-inline-babel-call-interpreter (inline-babel-call contents
)
2898 "Interpret INLINE-BABEL-CALL object as Org syntax.
2900 (let* ((babel-info (org-element-property :info inline-babel-call
))
2901 (main-source (car babel-info
))
2902 (post-options (nth 1 babel-info
)))
2904 (if (string-match "\\[\\(\\[.*?\\]\\)\\]" main-source
)
2905 ;; Remove redundant square brackets.
2907 (match-string 1 main-source
) nil nil main-source
)
2909 (and post-options
(format "[%s]" post-options
)))))
2911 (defun org-element-inline-babel-call-successor ()
2912 "Search for the next inline-babel-call object.
2914 Return value is a cons cell whose CAR is `inline-babel-call' and
2915 CDR is beginning position."
2917 (when (re-search-forward org-babel-inline-lob-one-liner-regexp nil t
)
2918 (cons 'inline-babel-call
(match-end 1)))))
2921 ;;;; Inline Src Block
2923 (defun org-element-inline-src-block-parser ()
2924 "Parse inline source block at point.
2926 Return a list whose CAR is `inline-src-block' and CDR a plist
2927 with `:begin', `:end', `:language', `:value', `:parameters' and
2928 `:post-blank' as keywords.
2930 Assume point is at the beginning of the inline src block."
2932 (unless (bolp) (backward-char))
2933 (looking-at org-babel-inline-src-block-regexp
)
2934 (let ((begin (match-beginning 1))
2935 (language (org-match-string-no-properties 2))
2936 (parameters (org-match-string-no-properties 4))
2937 (value (org-match-string-no-properties 5))
2938 (post-blank (progn (goto-char (match-end 0))
2939 (skip-chars-forward " \t")))
2941 (list 'inline-src-block
2942 (list :language language
2944 :parameters parameters
2947 :post-blank post-blank
)))))
2949 (defun org-element-inline-src-block-interpreter (inline-src-block contents
)
2950 "Interpret INLINE-SRC-BLOCK object as Org syntax.
2952 (let ((language (org-element-property :language inline-src-block
))
2953 (arguments (org-element-property :parameters inline-src-block
))
2954 (body (org-element-property :value inline-src-block
)))
2955 (format "src_%s%s{%s}"
2957 (if arguments
(format "[%s]" arguments
) "")
2960 (defun org-element-inline-src-block-successor ()
2961 "Search for the next inline-babel-call element.
2963 Return value is a cons cell whose CAR is `inline-babel-call' and
2964 CDR is beginning position."
2966 (unless (bolp) (backward-char))
2967 (when (re-search-forward org-babel-inline-src-block-regexp nil t
)
2968 (cons 'inline-src-block
(match-beginning 1)))))
2972 (defun org-element-italic-parser ()
2973 "Parse italic object at point.
2975 Return a list whose CAR is `italic' and CDR is a plist with
2976 `:begin', `:end', `:contents-begin' and `:contents-end' and
2977 `:post-blank' keywords.
2979 Assume point is at the first slash marker."
2981 (unless (bolp) (backward-char 1))
2982 (looking-at org-emph-re
)
2983 (let ((begin (match-beginning 2))
2984 (contents-begin (match-beginning 4))
2985 (contents-end (match-end 4))
2986 (post-blank (progn (goto-char (match-end 2))
2987 (skip-chars-forward " \t")))
2992 :contents-begin contents-begin
2993 :contents-end contents-end
2994 :post-blank post-blank
)))))
2996 (defun org-element-italic-interpreter (italic contents
)
2997 "Interpret ITALIC object as Org syntax.
2998 CONTENTS is the contents of the object."
2999 (format "/%s/" contents
))
3004 (defun org-element-latex-fragment-parser ()
3005 "Parse LaTeX fragment at point.
3007 Return a list whose CAR is `latex-fragment' and CDR a plist with
3008 `:value', `:begin', `:end', and `:post-blank' as keywords.
3010 Assume point is at the beginning of the LaTeX fragment."
3012 (let* ((begin (point))
3015 (dolist (e (cdr org-latex-regexps
))
3016 (let ((latex-regexp (nth 1 e
)))
3017 (when (or (looking-at latex-regexp
)
3021 (looking-at latex-regexp
))))
3022 (throw 'exit
(nth 2 e
)))))
3023 ;; None found: it's a macro.
3024 (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")
3026 (value (org-match-string-no-properties substring-match
))
3027 (post-blank (progn (goto-char (match-end substring-match
))
3028 (skip-chars-forward " \t")))
3030 (list 'latex-fragment
3034 :post-blank post-blank
)))))
3036 (defun org-element-latex-fragment-interpreter (latex-fragment contents
)
3037 "Interpret LATEX-FRAGMENT object as Org syntax.
3039 (org-element-property :value latex-fragment
))
3043 (defun org-element-line-break-parser ()
3044 "Parse line break at point.
3046 Return a list whose CAR is `line-break', and CDR a plist with
3047 `:begin', `:end' and `:post-blank' keywords.
3049 Assume point is at the beginning of the line break."
3051 (list :begin
(point)
3052 :end
(progn (forward-line) (point))
3055 (defun org-element-line-break-interpreter (line-break contents
)
3056 "Interpret LINE-BREAK object as Org syntax.
3060 (defun org-element-line-break-successor ()
3061 "Search for the next line-break object.
3063 Return value is a cons cell whose CAR is `line-break' and CDR is
3064 beginning position."
3066 (let ((beg (and (re-search-forward "[^\\\\]\\(\\\\\\\\\\)[ \t]*$" nil t
)
3067 (goto-char (match-beginning 1)))))
3068 ;; A line break can only happen on a non-empty line.
3069 (when (and beg
(re-search-backward "\\S-" (point-at-bol) t
))
3070 (cons 'line-break beg
)))))
3075 (defun org-element-link-parser ()
3076 "Parse link at point.
3078 Return a list whose CAR is `link' and CDR a plist with `:type',
3079 `:path', `:raw-link', `:application', `:search-option', `:begin',
3080 `:end', `:contents-begin', `:contents-end' and `:post-blank' as
3083 Assume point is at the beginning of the link."
3085 (let ((begin (point))
3086 end contents-begin contents-end link-end post-blank path type
3087 raw-link link search-option application
)
3089 ;; Type 1: Text targeted from a radio target.
3090 ((and org-target-link-regexp
(looking-at org-target-link-regexp
))
3092 link-end
(match-end 0)
3093 path
(org-match-string-no-properties 0)
3094 contents-begin
(match-beginning 0)
3095 contents-end
(match-end 0)))
3096 ;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]]
3097 ((looking-at org-bracket-link-regexp
)
3098 (setq contents-begin
(match-beginning 3)
3099 contents-end
(match-end 3)
3100 link-end
(match-end 0)
3101 ;; RAW-LINK is the original link. Expand any
3102 ;; abbreviation in it.
3103 raw-link
(org-translate-link
3104 (org-link-expand-abbrev
3105 (org-match-string-no-properties 1))))
3106 ;; Determine TYPE of link and set PATH accordingly.
3109 ((or (file-name-absolute-p raw-link
)
3110 (string-match "\\`\\.\\.?/" raw-link
))
3111 (setq type
"file" path raw-link
))
3112 ;; Explicit type (http, irc, bbdb...). See `org-link-types'.
3113 ((string-match org-link-types-re raw-link
)
3114 (setq type
(match-string 1 raw-link
)
3115 ;; According to RFC 3986, extra whitespace should be
3116 ;; ignored when a URI is extracted.
3117 path
(replace-regexp-in-string
3118 "[ \t]*\n[ \t]*" "" (substring raw-link
(match-end 0)))))
3119 ;; Id type: PATH is the id.
3120 ((string-match "\\`id:\\([-a-f0-9]+\\)" raw-link
)
3121 (setq type
"id" path
(match-string 1 raw-link
)))
3122 ;; Code-ref type: PATH is the name of the reference.
3123 ((string-match "\\`(\\(.*\\))\\'" raw-link
)
3124 (setq type
"coderef" path
(match-string 1 raw-link
)))
3125 ;; Custom-id type: PATH is the name of the custom id.
3126 ((= (aref raw-link
0) ?
#)
3127 (setq type
"custom-id" path
(substring raw-link
1)))
3128 ;; Fuzzy type: Internal link either matches a target, an
3129 ;; headline name or nothing. PATH is the target or
3131 (t (setq type
"fuzzy" path raw-link
))))
3132 ;; Type 3: Plain link, e.g., http://orgmode.org
3133 ((looking-at org-plain-link-re
)
3134 (setq raw-link
(org-match-string-no-properties 0)
3135 type
(org-match-string-no-properties 1)
3136 link-end
(match-end 0)
3137 path
(org-match-string-no-properties 2)))
3138 ;; Type 4: Angular link, e.g., <http://orgmode.org>
3139 ((looking-at org-angle-link-re
)
3140 (setq raw-link
(buffer-substring-no-properties
3141 (match-beginning 1) (match-end 2))
3142 type
(org-match-string-no-properties 1)
3143 link-end
(match-end 0)
3144 path
(org-match-string-no-properties 2))))
3145 ;; In any case, deduce end point after trailing white space from
3146 ;; LINK-END variable.
3147 (setq post-blank
(progn (goto-char link-end
) (skip-chars-forward " \t"))
3149 ;; Special "file" type link processing.
3150 (when (member type org-element-link-type-is-file
)
3151 ;; Extract opening application and search option.
3152 (cond ((string-match "^file\\+\\(.*\\)$" type
)
3153 (setq application
(match-string 1 type
)))
3154 ((not (string-match "^file" type
))
3155 (setq application type
)))
3156 (when (string-match "::\\(.*\\)\\'" path
)
3157 (setq search-option
(match-string 1 path
)
3158 path
(replace-match "" nil nil path
)))
3160 (when (and (not (org-string-match-p "\\`//" path
))
3161 (file-name-absolute-p path
))
3162 (setq path
(concat "//" (expand-file-name path
))))
3163 ;; Make sure TYPE always reports "file".
3168 :raw-link
(or raw-link path
)
3169 :application application
3170 :search-option search-option
3173 :contents-begin contents-begin
3174 :contents-end contents-end
3175 :post-blank post-blank
)))))
3177 (defun org-element-link-interpreter (link contents
)
3178 "Interpret LINK object as Org syntax.
3179 CONTENTS is the contents of the object, or nil."
3180 (let ((type (org-element-property :type link
))
3181 (raw-link (org-element-property :raw-link link
)))
3182 (if (string= type
"radio") raw-link
3185 (if contents
(format "[%s]" contents
) "")))))
3187 (defun org-element-link-successor ()
3188 "Search for the next link object.
3190 Return value is a cons cell whose CAR is `link' and CDR is
3191 beginning position."
3194 (if (not org-target-link-regexp
) org-any-link-re
3195 (concat org-any-link-re
"\\|" org-target-link-regexp
))))
3196 (when (re-search-forward link-regexp nil t
)
3197 (cons 'link
(match-beginning 0))))))
3199 (defun org-element-plain-link-successor ()
3200 "Search for the next plain link object.
3202 Return value is a cons cell whose CAR is `link' and CDR is
3203 beginning position."
3204 (and (save-excursion (re-search-forward org-plain-link-re nil t
))
3205 (cons 'link
(match-beginning 0))))
3210 (defun org-element-macro-parser ()
3211 "Parse macro at point.
3213 Return a list whose CAR is `macro' and CDR a plist with `:key',
3214 `:args', `:begin', `:end', `:value' and `:post-blank' as
3217 Assume point is at the macro."
3219 (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
3220 (let ((begin (point))
3221 (key (downcase (org-match-string-no-properties 1)))
3222 (value (org-match-string-no-properties 0))
3223 (post-blank (progn (goto-char (match-end 0))
3224 (skip-chars-forward " \t")))
3226 (args (let ((args (org-match-string-no-properties 3)))
3228 ;; Do not use `org-split-string' since empty
3229 ;; strings are meaningful here.
3231 (replace-regexp-in-string
3232 "\\(\\\\*\\)\\(,\\)"
3234 (let ((len (length (match-string 1 str
))))
3235 (concat (make-string (/ len
2) ?
\\)
3236 (if (zerop (mod len
2)) "\000" ","))))
3245 :post-blank post-blank
)))))
3247 (defun org-element-macro-interpreter (macro contents
)
3248 "Interpret MACRO object as Org syntax.
3250 (org-element-property :value macro
))
3252 (defun org-element-macro-successor ()
3253 "Search for the next macro object.
3255 Return value is cons cell whose CAR is `macro' and CDR is
3256 beginning position."
3258 (when (re-search-forward
3259 "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
3261 (cons 'macro
(match-beginning 0)))))
3266 (defun org-element-radio-target-parser ()
3267 "Parse radio target at point.
3269 Return a list whose CAR is `radio-target' and CDR a plist with
3270 `:begin', `:end', `:contents-begin', `:contents-end', `:value'
3271 and `:post-blank' as keywords.
3273 Assume point is at the radio target."
3275 (looking-at org-radio-target-regexp
)
3276 (let ((begin (point))
3277 (contents-begin (match-beginning 1))
3278 (contents-end (match-end 1))
3279 (value (org-match-string-no-properties 1))
3280 (post-blank (progn (goto-char (match-end 0))
3281 (skip-chars-forward " \t")))
3286 :contents-begin contents-begin
3287 :contents-end contents-end
3288 :post-blank post-blank
3291 (defun org-element-radio-target-interpreter (target contents
)
3292 "Interpret TARGET object as Org syntax.
3293 CONTENTS is the contents of the object."
3294 (concat "<<<" contents
">>>"))
3296 (defun org-element-radio-target-successor ()
3297 "Search for the next radio-target object.
3299 Return value is a cons cell whose CAR is `radio-target' and CDR
3300 is beginning position."
3302 (when (re-search-forward org-radio-target-regexp nil t
)
3303 (cons 'radio-target
(match-beginning 0)))))
3306 ;;;; Statistics Cookie
3308 (defun org-element-statistics-cookie-parser ()
3309 "Parse statistics cookie at point.
3311 Return a list whose CAR is `statistics-cookie', and CDR a plist
3312 with `:begin', `:end', `:value' and `:post-blank' keywords.
3314 Assume point is at the beginning of the statistics-cookie."
3316 (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
3317 (let* ((begin (point))
3318 (value (buffer-substring-no-properties
3319 (match-beginning 0) (match-end 0)))
3320 (post-blank (progn (goto-char (match-end 0))
3321 (skip-chars-forward " \t")))
3323 (list 'statistics-cookie
3327 :post-blank post-blank
)))))
3329 (defun org-element-statistics-cookie-interpreter (statistics-cookie contents
)
3330 "Interpret STATISTICS-COOKIE object as Org syntax.
3332 (org-element-property :value statistics-cookie
))
3334 (defun org-element-statistics-cookie-successor ()
3335 "Search for the next statistics cookie object.
3337 Return value is a cons cell whose CAR is `statistics-cookie' and
3338 CDR is beginning position."
3340 (when (re-search-forward "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]" nil t
)
3341 (cons 'statistics-cookie
(match-beginning 0)))))
3346 (defun org-element-strike-through-parser ()
3347 "Parse strike-through object at point.
3349 Return a list whose CAR is `strike-through' and CDR is a plist
3350 with `:begin', `:end', `:contents-begin' and `:contents-end' and
3351 `:post-blank' keywords.
3353 Assume point is at the first plus sign marker."
3355 (unless (bolp) (backward-char 1))
3356 (looking-at org-emph-re
)
3357 (let ((begin (match-beginning 2))
3358 (contents-begin (match-beginning 4))
3359 (contents-end (match-end 4))
3360 (post-blank (progn (goto-char (match-end 2))
3361 (skip-chars-forward " \t")))
3363 (list 'strike-through
3366 :contents-begin contents-begin
3367 :contents-end contents-end
3368 :post-blank post-blank
)))))
3370 (defun org-element-strike-through-interpreter (strike-through contents
)
3371 "Interpret STRIKE-THROUGH object as Org syntax.
3372 CONTENTS is the contents of the object."
3373 (format "+%s+" contents
))
3378 (defun org-element-subscript-parser ()
3379 "Parse subscript at point.
3381 Return a list whose CAR is `subscript' and CDR a plist with
3382 `:begin', `:end', `:contents-begin', `:contents-end',
3383 `:use-brackets-p' and `:post-blank' as keywords.
3385 Assume point is at the underscore."
3387 (unless (bolp) (backward-char))
3388 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp
)
3390 (not (looking-at org-match-substring-regexp
))))
3391 (begin (match-beginning 2))
3392 (contents-begin (or (match-beginning 5)
3393 (match-beginning 3)))
3394 (contents-end (or (match-end 5) (match-end 3)))
3395 (post-blank (progn (goto-char (match-end 0))
3396 (skip-chars-forward " \t")))
3401 :use-brackets-p bracketsp
3402 :contents-begin contents-begin
3403 :contents-end contents-end
3404 :post-blank post-blank
)))))
3406 (defun org-element-subscript-interpreter (subscript contents
)
3407 "Interpret SUBSCRIPT object as Org syntax.
3408 CONTENTS is the contents of the object."
3410 (if (org-element-property :use-brackets-p subscript
) "_{%s}" "_%s")
3413 (defun org-element-sub/superscript-successor
()
3414 "Search for the next sub/superscript object.
3416 Return value is a cons cell whose CAR is either `subscript' or
3417 `superscript' and CDR is beginning position."
3419 (unless (bolp) (backward-char))
3420 (when (re-search-forward org-match-substring-regexp nil t
)
3421 (cons (if (string= (match-string 2) "_") 'subscript
'superscript
)
3422 (match-beginning 2)))))
3427 (defun org-element-superscript-parser ()
3428 "Parse superscript at point.
3430 Return a list whose CAR is `superscript' and CDR a plist with
3431 `:begin', `:end', `:contents-begin', `:contents-end',
3432 `:use-brackets-p' and `:post-blank' as keywords.
3434 Assume point is at the caret."
3436 (unless (bolp) (backward-char))
3437 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp
) t
3438 (not (looking-at org-match-substring-regexp
))))
3439 (begin (match-beginning 2))
3440 (contents-begin (or (match-beginning 5)
3441 (match-beginning 3)))
3442 (contents-end (or (match-end 5) (match-end 3)))
3443 (post-blank (progn (goto-char (match-end 0))
3444 (skip-chars-forward " \t")))
3449 :use-brackets-p bracketsp
3450 :contents-begin contents-begin
3451 :contents-end contents-end
3452 :post-blank post-blank
)))))
3454 (defun org-element-superscript-interpreter (superscript contents
)
3455 "Interpret SUPERSCRIPT object as Org syntax.
3456 CONTENTS is the contents of the object."
3458 (if (org-element-property :use-brackets-p superscript
) "^{%s}" "^%s")
3464 (defun org-element-table-cell-parser ()
3465 "Parse table cell at point.
3467 Return a list whose CAR is `table-cell' and CDR is a plist
3468 containing `:begin', `:end', `:contents-begin', `:contents-end'
3469 and `:post-blank' keywords."
3470 (looking-at "[ \t]*\\(.*?\\)[ \t]*\\(?:|\\|$\\)")
3471 (let* ((begin (match-beginning 0))
3473 (contents-begin (match-beginning 1))
3474 (contents-end (match-end 1)))
3478 :contents-begin contents-begin
3479 :contents-end contents-end
3482 (defun org-element-table-cell-interpreter (table-cell contents
)
3483 "Interpret TABLE-CELL element as Org syntax.
3484 CONTENTS is the contents of the cell, or nil."
3485 (concat " " contents
" |"))
3487 (defun org-element-table-cell-successor ()
3488 "Search for the next table-cell object.
3490 Return value is a cons cell whose CAR is `table-cell' and CDR is
3491 beginning position."
3492 (when (looking-at "[ \t]*.*?[ \t]*\\(|\\|$\\)") (cons 'table-cell
(point))))
3497 (defun org-element-target-parser ()
3498 "Parse target at point.
3500 Return a list whose CAR is `target' and CDR a plist with
3501 `:begin', `:end', `:value' and `:post-blank' as keywords.
3503 Assume point is at the target."
3505 (looking-at org-target-regexp
)
3506 (let ((begin (point))
3507 (value (org-match-string-no-properties 1))
3508 (post-blank (progn (goto-char (match-end 0))
3509 (skip-chars-forward " \t")))
3515 :post-blank post-blank
)))))
3517 (defun org-element-target-interpreter (target contents
)
3518 "Interpret TARGET object as Org syntax.
3520 (format "<<%s>>" (org-element-property :value target
)))
3522 (defun org-element-target-successor ()
3523 "Search for the next target object.
3525 Return value is a cons cell whose CAR is `target' and CDR is
3526 beginning position."
3528 (when (re-search-forward org-target-regexp nil t
)
3529 (cons 'target
(match-beginning 0)))))
3534 (defun org-element-timestamp-parser ()
3535 "Parse time stamp at point.
3537 Return a list whose CAR is `timestamp', and CDR a plist with
3538 `:type', `:raw-value', `:year-start', `:month-start',
3539 `:day-start', `:hour-start', `:minute-start', `:year-end',
3540 `:month-end', `:day-end', `:hour-end', `:minute-end',
3541 `:repeater-type', `:repeater-value', `:repeater-unit',
3542 `:warning-type', `:warning-value', `:warning-unit', `:begin',
3543 `:end' and `:post-blank' keywords.
3545 Assume point is at the beginning of the timestamp."
3547 (let* ((begin (point))
3548 (activep (eq (char-after) ?
<))
3551 (looking-at "\\([<[]\\(%%\\)?.*?\\)[]>]\\(?:--\\([<[].*?[]>]\\)\\)?")
3552 (match-string-no-properties 0)))
3553 (date-start (match-string-no-properties 1))
3554 (date-end (match-string 3))
3555 (diaryp (match-beginning 2))
3556 (post-blank (progn (goto-char (match-end 0))
3557 (skip-chars-forward " \t")))
3562 "[012]?[0-9]:[0-5][0-9]\\(-\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)"
3564 (cons (string-to-number (match-string 2 date-start
))
3565 (string-to-number (match-string 3 date-start
)))))
3566 (type (cond (diaryp 'diary
)
3567 ((and activep
(or date-end time-range
)) 'active-range
)
3569 ((or date-end time-range
) 'inactive-range
)
3573 (string-match "\\([.+]?\\+\\)\\([0-9]+\\)\\([hdwmy]\\)"
3577 (let ((type (match-string 1 raw-value
)))
3578 (cond ((equal "++" type
) 'catch-up
)
3579 ((equal ".+" type
) 'restart
)
3581 :repeater-value
(string-to-number (match-string 2 raw-value
))
3583 (case (string-to-char (match-string 3 raw-value
))
3584 (?h
'hour
) (?d
'day
) (?w
'week
) (?m
'month
) (t 'year
)))))
3587 (string-match "\\(-\\)?-\\([0-9]+\\)\\([hdwmy]\\)" raw-value
)
3589 :warning-type
(if (match-string 1 raw-value
) 'first
'all
)
3590 :warning-value
(string-to-number (match-string 2 raw-value
))
3592 (case (string-to-char (match-string 3 raw-value
))
3593 (?h
'hour
) (?d
'day
) (?w
'week
) (?m
'month
) (t 'year
)))))
3594 year-start month-start day-start hour-start minute-start year-end
3595 month-end day-end hour-end minute-end
)
3596 ;; Parse date-start.
3598 (let ((date (org-parse-time-string date-start t
)))
3599 (setq year-start
(nth 5 date
)
3600 month-start
(nth 4 date
)
3601 day-start
(nth 3 date
)
3602 hour-start
(nth 2 date
)
3603 minute-start
(nth 1 date
))))
3604 ;; Compute date-end. It can be provided directly in time-stamp,
3605 ;; or extracted from time range. Otherwise, it defaults to the
3606 ;; same values as date-start.
3608 (let ((date (and date-end
(org-parse-time-string date-end t
))))
3609 (setq year-end
(or (nth 5 date
) year-start
)
3610 month-end
(or (nth 4 date
) month-start
)
3611 day-end
(or (nth 3 date
) day-start
)
3612 hour-end
(or (nth 2 date
) (car time-range
) hour-start
)
3613 minute-end
(or (nth 1 date
) (cdr time-range
) minute-start
))))
3615 (nconc (list :type type
3616 :raw-value raw-value
3617 :year-start year-start
3618 :month-start month-start
3619 :day-start day-start
3620 :hour-start hour-start
3621 :minute-start minute-start
3623 :month-end month-end
3626 :minute-end minute-end
3629 :post-blank post-blank
)
3633 (defun org-element-timestamp-interpreter (timestamp contents
)
3634 "Interpret TIMESTAMP object as Org syntax.
3636 ;; Use `:raw-value' if specified.
3637 (or (org-element-property :raw-value timestamp
)
3638 ;; Otherwise, build timestamp string.
3639 (let* ((repeat-string
3641 (case (org-element-property :repeater-type timestamp
)
3642 (cumulate "+") (catch-up "++") (restart ".+"))
3643 (let ((val (org-element-property :repeater-value timestamp
)))
3644 (and val
(number-to-string val
)))
3645 (case (org-element-property :repeater-unit timestamp
)
3646 (hour "h") (day "d") (week "w") (month "m") (year "y"))))
3649 (case (org-element-property :warning-type timestamp
)
3652 (let ((val (org-element-property :warning-value timestamp
)))
3653 (and val
(number-to-string val
)))
3654 (case (org-element-property :warning-unit timestamp
)
3655 (hour "h") (day "d") (week "w") (month "m") (year "y"))))
3657 ;; Build an Org timestamp string from TIME. ACTIVEP is
3658 ;; non-nil when time stamp is active. If WITH-TIME-P is
3659 ;; non-nil, add a time part. HOUR-END and MINUTE-END
3660 ;; specify a time range in the timestamp. REPEAT-STRING
3661 ;; is the repeater string, if any.
3662 (lambda (time activep
&optional with-time-p hour-end minute-end
)
3663 (let ((ts (format-time-string
3664 (funcall (if with-time-p
'cdr
'car
)
3665 org-time-stamp-formats
)
3667 (when (and hour-end minute-end
)
3668 (string-match "[012]?[0-9]:[0-5][0-9]" ts
)
3671 (format "\\&-%02d:%02d" hour-end minute-end
)
3673 (unless activep
(setq ts
(format "[%s]" (substring ts
1 -
1))))
3674 (dolist (s (list repeat-string warning-string
))
3675 (when (org-string-nw-p s
)
3676 (setq ts
(concat (substring ts
0 -
1)
3679 (substring ts -
1)))))
3682 (type (org-element-property :type timestamp
)))
3685 (let* ((minute-start (org-element-property :minute-start timestamp
))
3686 (minute-end (org-element-property :minute-end timestamp
))
3687 (hour-start (org-element-property :hour-start timestamp
))
3688 (hour-end (org-element-property :hour-end timestamp
))
3689 (time-range-p (and hour-start hour-end minute-start minute-end
3690 (or (/= hour-start hour-end
)
3691 (/= minute-start minute-end
)))))
3697 (org-element-property :day-start timestamp
)
3698 (org-element-property :month-start timestamp
)
3699 (org-element-property :year-start timestamp
))
3701 (and hour-start minute-start
)
3702 (and time-range-p hour-end
)
3703 (and time-range-p minute-end
))))
3704 ((active-range inactive-range
)
3705 (let ((minute-start (org-element-property :minute-start timestamp
))
3706 (minute-end (org-element-property :minute-end timestamp
))
3707 (hour-start (org-element-property :hour-start timestamp
))
3708 (hour-end (org-element-property :hour-end timestamp
)))
3711 build-ts-string
(encode-time
3715 (org-element-property :day-start timestamp
)
3716 (org-element-property :month-start timestamp
)
3717 (org-element-property :year-start timestamp
))
3718 (eq type
'active-range
)
3719 (and hour-start minute-start
))
3721 (funcall build-ts-string
3725 (org-element-property :day-end timestamp
)
3726 (org-element-property :month-end timestamp
)
3727 (org-element-property :year-end timestamp
))
3728 (eq type
'active-range
)
3729 (and hour-end minute-end
)))))))))
3731 (defun org-element-timestamp-successor ()
3732 "Search for the next timestamp object.
3734 Return value is a cons cell whose CAR is `timestamp' and CDR is
3735 beginning position."
3737 (when (re-search-forward
3738 (concat org-ts-regexp-both
3740 "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
3742 "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
3744 (cons 'timestamp
(match-beginning 0)))))
3749 (defun org-element-underline-parser ()
3750 "Parse underline object at point.
3752 Return a list whose CAR is `underline' and CDR is a plist with
3753 `:begin', `:end', `:contents-begin' and `:contents-end' and
3754 `:post-blank' keywords.
3756 Assume point is at the first underscore marker."
3758 (unless (bolp) (backward-char 1))
3759 (looking-at org-emph-re
)
3760 (let ((begin (match-beginning 2))
3761 (contents-begin (match-beginning 4))
3762 (contents-end (match-end 4))
3763 (post-blank (progn (goto-char (match-end 2))
3764 (skip-chars-forward " \t")))
3769 :contents-begin contents-begin
3770 :contents-end contents-end
3771 :post-blank post-blank
)))))
3773 (defun org-element-underline-interpreter (underline contents
)
3774 "Interpret UNDERLINE object as Org syntax.
3775 CONTENTS is the contents of the object."
3776 (format "_%s_" contents
))
3781 (defun org-element-verbatim-parser ()
3782 "Parse verbatim object at point.
3784 Return a list whose CAR is `verbatim' and CDR is a plist with
3785 `:value', `:begin', `:end' and `:post-blank' keywords.
3787 Assume point is at the first equal sign marker."
3789 (unless (bolp) (backward-char 1))
3790 (looking-at org-emph-re
)
3791 (let ((begin (match-beginning 2))
3792 (value (org-match-string-no-properties 4))
3793 (post-blank (progn (goto-char (match-end 2))
3794 (skip-chars-forward " \t")))
3800 :post-blank post-blank
)))))
3802 (defun org-element-verbatim-interpreter (verbatim contents
)
3803 "Interpret VERBATIM object as Org syntax.
3805 (format "=%s=" (org-element-property :value verbatim
)))
3809 ;;; Parsing Element Starting At Point
3811 ;; `org-element--current-element' is the core function of this section.
3812 ;; It returns the Lisp representation of the element starting at
3815 ;; `org-element--current-element' makes use of special modes. They
3816 ;; are activated for fixed element chaining (e.g., `plain-list' >
3817 ;; `item') or fixed conditional element chaining (e.g., `headline' >
3818 ;; `section'). Special modes are: `first-section', `item',
3819 ;; `node-property', `quote-section', `section' and `table-row'.
3821 (defun org-element--current-element
3822 (limit &optional granularity special structure
)
3823 "Parse the element starting at point.
3825 Return value is a list like (TYPE PROPS) where TYPE is the type
3826 of the element and PROPS a plist of properties associated to the
3829 Possible types are defined in `org-element-all-elements'.
3831 LIMIT bounds the search.
3833 Optional argument GRANULARITY determines the depth of the
3834 recursion. Allowed values are `headline', `greater-element',
3835 `element', `object' or nil. When it is broader than `object' (or
3836 nil), secondary values will not be parsed, since they only
3839 Optional argument SPECIAL, when non-nil, can be either
3840 `first-section', `item', `node-property', `quote-section',
3841 `section', and `table-row'.
3843 If STRUCTURE isn't provided but SPECIAL is set to `item', it will
3846 This function assumes point is always at the beginning of the
3847 element it has to parse."
3849 (let ((case-fold-search t
)
3850 ;; Determine if parsing depth allows for secondary strings
3851 ;; parsing. It only applies to elements referenced in
3852 ;; `org-element-secondary-value-alist'.
3853 (raw-secondary-p (and granularity
(not (eq granularity
'object
)))))
3857 (org-element-item-parser limit structure raw-secondary-p
))
3859 ((eq special
'table-row
) (org-element-table-row-parser limit
))
3861 ((eq special
'node-property
) (org-element-node-property-parser limit
))
3863 ((org-with-limited-levels (org-at-heading-p))
3864 (org-element-headline-parser limit raw-secondary-p
))
3865 ;; Sections (must be checked after headline).
3866 ((eq special
'section
) (org-element-section-parser limit
))
3867 ((eq special
'quote-section
) (org-element-quote-section-parser limit
))
3868 ((eq special
'first-section
)
3869 (org-element-section-parser
3870 (or (save-excursion (org-with-limited-levels (outline-next-heading)))
3872 ;; When not at bol, point is at the beginning of an item or
3873 ;; a footnote definition: next item is always a paragraph.
3874 ((not (bolp)) (org-element-paragraph-parser limit
(list (point))))
3875 ;; Planning and Clock.
3876 ((looking-at org-planning-or-clock-line-re
)
3877 (if (equal (match-string 1) org-clock-string
)
3878 (org-element-clock-parser limit
)
3879 (org-element-planning-parser limit
)))
3882 (org-element-inlinetask-parser limit raw-secondary-p
))
3883 ;; From there, elements can have affiliated keywords.
3884 (t (let ((affiliated (org-element--collect-affiliated-keywords limit
)))
3886 ;; Jumping over affiliated keywords put point off-limits.
3887 ;; Parse them as regular keywords.
3888 ((and (cdr affiliated
) (>= (point) limit
))
3889 (goto-char (car affiliated
))
3890 (org-element-keyword-parser limit nil
))
3891 ;; LaTeX Environment.
3892 ((looking-at "[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}\\(\\[.*?\\]\\|{.*?}\\)*[ \t]*$")
3893 (org-element-latex-environment-parser limit affiliated
))
3894 ;; Drawer and Property Drawer.
3895 ((looking-at org-drawer-regexp
)
3896 (if (equal (match-string 1) "PROPERTIES")
3897 (org-element-property-drawer-parser limit affiliated
)
3898 (org-element-drawer-parser limit affiliated
)))
3900 ((looking-at "[ \t]*:\\( \\|$\\)")
3901 (org-element-fixed-width-parser limit affiliated
))
3902 ;; Inline Comments, Blocks, Babel Calls, Dynamic Blocks and
3904 ((looking-at "[ \t]*#")
3905 (goto-char (match-end 0))
3906 (cond ((looking-at "\\(?: \\|$\\)")
3908 (org-element-comment-parser limit affiliated
))
3909 ((looking-at "\\+BEGIN_\\(\\S-+\\)")
3911 (let ((parser (assoc (upcase (match-string 1))
3912 org-element-block-name-alist
)))
3913 (if parser
(funcall (cdr parser
) limit affiliated
)
3914 (org-element-special-block-parser limit affiliated
))))
3915 ((looking-at "\\+CALL:")
3917 (org-element-babel-call-parser limit affiliated
))
3918 ((looking-at "\\+BEGIN:? ")
3920 (org-element-dynamic-block-parser limit affiliated
))
3921 ((looking-at "\\+\\S-+:")
3923 (org-element-keyword-parser limit affiliated
))
3926 (org-element-paragraph-parser limit affiliated
))))
3927 ;; Footnote Definition.
3928 ((looking-at org-footnote-definition-re
)
3929 (org-element-footnote-definition-parser limit affiliated
))
3931 ((looking-at "[ \t]*-\\{5,\\}[ \t]*$")
3932 (org-element-horizontal-rule-parser limit affiliated
))
3935 (org-element-diary-sexp-parser limit affiliated
))
3937 ((org-at-table-p t
) (org-element-table-parser limit affiliated
))
3939 ((looking-at (org-item-re))
3940 (org-element-plain-list-parser
3942 (or structure
(org-element--list-struct limit
))))
3943 ;; Default element: Paragraph.
3944 (t (org-element-paragraph-parser limit affiliated
)))))))))
3947 ;; Most elements can have affiliated keywords. When looking for an
3948 ;; element beginning, we want to move before them, as they belong to
3949 ;; that element, and, in the meantime, collect information they give
3950 ;; into appropriate properties. Hence the following function.
3952 (defun org-element--collect-affiliated-keywords (limit)
3953 "Collect affiliated keywords from point down to LIMIT.
3955 Return a list whose CAR is the position at the first of them and
3956 CDR a plist of keywords and values and move point to the
3957 beginning of the first line after them.
3959 As a special case, if element doesn't start at the beginning of
3960 the line (e.g., a paragraph starting an item), CAR is current
3961 position of point and CDR is nil."
3962 (if (not (bolp)) (list (point))
3963 (let ((case-fold-search t
)
3965 ;; RESTRICT is the list of objects allowed in parsed
3967 (restrict (org-element-restriction 'keyword
))
3969 (while (and (< (point) limit
) (looking-at org-element--affiliated-re
))
3970 (let* ((raw-kwd (upcase (match-string 1)))
3971 ;; Apply translation to RAW-KWD. From there, KWD is
3972 ;; the official keyword.
3973 (kwd (or (cdr (assoc raw-kwd
3974 org-element-keyword-translation-alist
))
3976 ;; Find main value for any keyword.
3980 (buffer-substring-no-properties
3981 (match-end 0) (point-at-eol)))))
3982 ;; PARSEDP is non-nil when keyword should have its
3984 (parsedp (member kwd org-element-parsed-keywords
))
3985 ;; If KWD is a dual keyword, find its secondary
3986 ;; value. Maybe parse it.
3987 (dualp (member kwd org-element-dual-keywords
))
3990 (let ((sec (org-match-string-no-properties 2)))
3991 (if (or (not sec
) (not parsedp
)) sec
3992 (org-element-parse-secondary-string sec restrict
)))))
3993 ;; Attribute a property name to KWD.
3994 (kwd-sym (and kwd
(intern (concat ":" (downcase kwd
))))))
3995 ;; Now set final shape for VALUE.
3997 (setq value
(org-element-parse-secondary-string value restrict
)))
3999 (setq value
(and (or value dual-value
) (cons value dual-value
))))
4000 (when (or (member kwd org-element-multiple-keywords
)
4001 ;; Attributes can always appear on multiple lines.
4002 (string-match "^ATTR_" kwd
))
4003 (setq value
(cons value
(plist-get output kwd-sym
))))
4004 ;; Eventually store the new value in OUTPUT.
4005 (setq output
(plist-put output kwd-sym value
))
4006 ;; Move to next keyword.
4008 ;; If affiliated keywords are orphaned: move back to first one.
4009 ;; They will be parsed as a paragraph.
4010 (when (looking-at "[ \t]*$") (goto-char origin
) (setq output nil
))
4012 (cons origin output
))))
4018 ;; The two major functions here are `org-element-parse-buffer', which
4019 ;; parses Org syntax inside the current buffer, taking into account
4020 ;; region, narrowing, or even visibility if specified, and
4021 ;; `org-element-parse-secondary-string', which parses objects within
4024 ;; The (almost) almighty `org-element-map' allows to apply a function
4025 ;; on elements or objects matching some type, and accumulate the
4026 ;; resulting values. In an export situation, it also skips unneeded
4027 ;; parts of the parse tree.
4029 (defun org-element-parse-buffer (&optional granularity visible-only
)
4030 "Recursively parse the buffer and return structure.
4031 If narrowing is in effect, only parse the visible part of the
4034 Optional argument GRANULARITY determines the depth of the
4035 recursion. It can be set to the following symbols:
4037 `headline' Only parse headlines.
4038 `greater-element' Don't recurse into greater elements excepted
4039 headlines and sections. Thus, elements
4040 parsed are the top-level ones.
4041 `element' Parse everything but objects and plain text.
4042 `object' Parse the complete buffer (default).
4044 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
4047 An element or an objects is represented as a list with the
4048 pattern (TYPE PROPERTIES CONTENTS), where :
4050 TYPE is a symbol describing the element or object. See
4051 `org-element-all-elements' and `org-element-all-objects' for an
4052 exhaustive list of such symbols. One can retrieve it with
4053 `org-element-type' function.
4055 PROPERTIES is the list of attributes attached to the element or
4056 object, as a plist. Although most of them are specific to the
4057 element or object type, all types share `:begin', `:end',
4058 `:post-blank' and `:parent' properties, which respectively
4059 refer to buffer position where the element or object starts,
4060 ends, the number of white spaces or blank lines after it, and
4061 the element or object containing it. Properties values can be
4062 obtained by using `org-element-property' function.
4064 CONTENTS is a list of elements, objects or raw strings
4065 contained in the current element or object, when applicable.
4066 One can access them with `org-element-contents' function.
4068 The Org buffer has `org-data' as type and nil as properties.
4069 `org-element-map' function can be used to find specific elements
4070 or objects within the parse tree.
4072 This function assumes that current major mode is `org-mode'."
4074 (goto-char (point-min))
4075 (org-skip-whitespace)
4076 (org-element--parse-elements
4077 (point-at-bol) (point-max)
4078 ;; Start in `first-section' mode so text before the first
4079 ;; headline belongs to a section.
4080 'first-section nil granularity visible-only
(list 'org-data nil
))))
4082 (defun org-element-parse-secondary-string (string restriction
&optional parent
)
4083 "Recursively parse objects in STRING and return structure.
4085 RESTRICTION is a symbol limiting the object types that will be
4088 Optional argument PARENT, when non-nil, is the element or object
4089 containing the secondary string. It is used to set correctly
4090 `:parent' property within the string."
4091 (let ((local-variables (buffer-local-variables)))
4093 (dolist (v local-variables
)
4095 (if (symbolp v
) (makunbound v
)
4096 (org-set-local (car v
) (cdr v
)))))
4098 (restore-buffer-modified-p nil
)
4099 (let ((secondary (org-element--parse-objects
4100 (point-min) (point-max) nil restriction
)))
4102 (dolist (o secondary
) (org-element-put-property o
:parent parent
)))
4105 (defun org-element-map
4106 (data types fun
&optional info first-match no-recursion with-affiliated
)
4107 "Map a function on selected elements or objects.
4109 DATA is a parse tree, an element, an object, a string, or a list
4110 of such constructs. TYPES is a symbol or list of symbols of
4111 elements or objects types (see `org-element-all-elements' and
4112 `org-element-all-objects' for a complete list of types). FUN is
4113 the function called on the matching element or object. It has to
4114 accept one argument: the element or object itself.
4116 When optional argument INFO is non-nil, it should be a plist
4117 holding export options. In that case, parts of the parse tree
4118 not exportable according to that property list will be skipped.
4120 When optional argument FIRST-MATCH is non-nil, stop at the first
4121 match for which FUN doesn't return nil, and return that value.
4123 Optional argument NO-RECURSION is a symbol or a list of symbols
4124 representing elements or objects types. `org-element-map' won't
4125 enter any recursive element or object whose type belongs to that
4126 list. Though, FUN can still be applied on them.
4128 When optional argument WITH-AFFILIATED is non-nil, FUN will also
4129 apply to matching objects within parsed affiliated keywords (see
4130 `org-element-parsed-keywords').
4132 Nil values returned from FUN do not appear in the results.
4138 Assuming TREE is a variable containing an Org buffer parse tree,
4139 the following example will return a flat list of all `src-block'
4140 and `example-block' elements in it:
4142 \(org-element-map tree '(example-block src-block) 'identity)
4144 The following snippet will find the first headline with a level
4145 of 1 and a \"phone\" tag, and will return its beginning position:
4147 \(org-element-map tree 'headline
4149 \(and (= (org-element-property :level hl) 1)
4150 \(member \"phone\" (org-element-property :tags hl))
4151 \(org-element-property :begin hl)))
4154 The next example will return a flat list of all `plain-list' type
4155 elements in TREE that are not a sub-list themselves:
4157 \(org-element-map tree 'plain-list 'identity nil nil 'plain-list)
4159 Eventually, this example will return a flat list of all `bold'
4160 type objects containing a `latex-snippet' type object, even
4161 looking into captions:
4163 \(org-element-map tree 'bold
4165 \(and (org-element-map b 'latex-snippet 'identity nil t) b))
4167 ;; Ensure TYPES and NO-RECURSION are a list, even of one element.
4168 (unless (listp types
) (setq types
(list types
)))
4169 (unless (listp no-recursion
) (setq no-recursion
(list no-recursion
)))
4170 ;; Recursion depth is determined by --CATEGORY.
4173 (let ((category 'greater-elements
))
4174 (mapc (lambda (type)
4175 (cond ((or (memq type org-element-all-objects
)
4176 (eq type
'plain-text
))
4177 ;; If one object is found, the function
4178 ;; has to recurse into every object.
4179 (throw 'found
'objects
))
4180 ((not (memq type org-element-greater-elements
))
4181 ;; If one regular element is found, the
4182 ;; function has to recurse, at least,
4183 ;; into every element it encounters.
4184 (and (not (eq category
'elements
))
4185 (setq category
'elements
)))))
4188 ;; Compute properties for affiliated keywords if necessary.
4190 (and with-affiliated
4191 (mapcar (lambda (kwd)
4192 (cons kwd
(intern (concat ":" (downcase kwd
)))))
4193 org-element-affiliated-keywords
)))
4199 ;; Recursively walk DATA. INFO, if non-nil, is a plist
4200 ;; holding contextual information.
4201 (let ((--type (org-element-type --data
)))
4204 ;; Ignored element in an export context.
4205 ((and info
(memq --data
(plist-get info
:ignore-list
))))
4206 ;; List of elements or objects.
4207 ((not --type
) (mapc --walk-tree --data
))
4208 ;; Unconditionally enter parse trees.
4209 ((eq --type
'org-data
)
4210 (mapc --walk-tree
(org-element-contents --data
)))
4212 ;; Check if TYPE is matching among TYPES. If so,
4213 ;; apply FUN to --DATA and accumulate return value
4214 ;; into --ACC (or exit if FIRST-MATCH is non-nil).
4215 (when (memq --type types
)
4216 (let ((result (funcall fun --data
)))
4217 (cond ((not result
))
4218 (first-match (throw '--map-first-match result
))
4219 (t (push result --acc
)))))
4220 ;; If --DATA has a secondary string that can contain
4221 ;; objects with their type among TYPES, look into it.
4222 (when (and (eq --category
'objects
) (not (stringp --data
)))
4224 (assq --type org-element-secondary-value-alist
)))
4226 (funcall --walk-tree
4227 (org-element-property (cdr sec-prop
) --data
)))))
4228 ;; If --DATA has any affiliated keywords and
4229 ;; WITH-AFFILIATED is non-nil, look for objects in
4231 (when (and with-affiliated
4232 (eq --category
'objects
)
4233 (memq --type org-element-all-elements
))
4234 (mapc (lambda (kwd-pair)
4235 (let ((kwd (car kwd-pair
))
4236 (value (org-element-property
4237 (cdr kwd-pair
) --data
)))
4238 ;; Pay attention to the type of value.
4239 ;; Preserve order for multiple keywords.
4242 ((and (member kwd org-element-multiple-keywords
)
4243 (member kwd org-element-dual-keywords
))
4244 (mapc (lambda (line)
4245 (funcall --walk-tree
(cdr line
))
4246 (funcall --walk-tree
(car line
)))
4248 ((member kwd org-element-multiple-keywords
)
4249 (mapc (lambda (line) (funcall --walk-tree line
))
4251 ((member kwd org-element-dual-keywords
)
4252 (funcall --walk-tree
(cdr value
))
4253 (funcall --walk-tree
(car value
)))
4254 (t (funcall --walk-tree value
)))))
4255 --affiliated-alist
))
4256 ;; Determine if a recursion into --DATA is possible.
4258 ;; --TYPE is explicitly removed from recursion.
4259 ((memq --type no-recursion
))
4260 ;; --DATA has no contents.
4261 ((not (org-element-contents --data
)))
4262 ;; Looking for greater elements but --DATA is simply
4263 ;; an element or an object.
4264 ((and (eq --category
'greater-elements
)
4265 (not (memq --type org-element-greater-elements
))))
4266 ;; Looking for elements but --DATA is an object.
4267 ((and (eq --category
'elements
)
4268 (memq --type org-element-all-objects
)))
4269 ;; In any other case, map contents.
4270 (t (mapc --walk-tree
(org-element-contents --data
)))))))))))
4271 (catch '--map-first-match
4272 (funcall --walk-tree data
)
4273 ;; Return value in a proper order.
4275 (put 'org-element-map
'lisp-indent-function
2)
4277 ;; The following functions are internal parts of the parser.
4279 ;; The first one, `org-element--parse-elements' acts at the element's
4282 ;; The second one, `org-element--parse-objects' applies on all objects
4283 ;; of a paragraph or a secondary string. It uses
4284 ;; `org-element--get-next-object-candidates' to optimize the search of
4285 ;; the next object in the buffer.
4287 ;; More precisely, that function looks for every allowed object type
4288 ;; first. Then, it discards failed searches, keeps further matches,
4289 ;; and searches again types matched behind point, for subsequent
4290 ;; calls. Thus, searching for a given type fails only once, and every
4291 ;; object is searched only once at top level (but sometimes more for
4294 (defun org-element--parse-elements
4295 (beg end special structure granularity visible-only acc
)
4296 "Parse elements between BEG and END positions.
4298 SPECIAL prioritize some elements over the others. It can be set
4299 to `first-section', `quote-section', `section' `item' or
4302 When value is `item', STRUCTURE will be used as the current list
4305 GRANULARITY determines the depth of the recursion. See
4306 `org-element-parse-buffer' for more information.
4308 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
4311 Elements are accumulated into ACC."
4314 ;; Visible only: skip invisible parts at the beginning of the
4316 (when (and visible-only
(org-invisible-p2))
4317 (goto-char (min (1+ (org-find-visible)) end
)))
4318 ;; When parsing only headlines, skip any text before first one.
4319 (when (and (eq granularity
'headline
) (not (org-at-heading-p)))
4320 (org-with-limited-levels (outline-next-heading)))
4322 (while (< (point) end
)
4323 ;; Find current element's type and parse it accordingly to
4325 (let* ((element (org-element--current-element
4326 end granularity special structure
))
4327 (type (org-element-type element
))
4328 (cbeg (org-element-property :contents-begin element
)))
4329 (goto-char (org-element-property :end element
))
4330 ;; Visible only: skip invisible parts between siblings.
4331 (when (and visible-only
(org-invisible-p2))
4332 (goto-char (min (1+ (org-find-visible)) end
)))
4333 ;; Fill ELEMENT contents by side-effect.
4335 ;; If element has no contents, don't modify it.
4337 ;; Greater element: parse it between `contents-begin' and
4338 ;; `contents-end'. Make sure GRANULARITY allows the
4339 ;; recursion, or ELEMENT is a headline, in which case going
4340 ;; inside is mandatory, in order to get sub-level headings.
4341 ((and (memq type org-element-greater-elements
)
4342 (or (memq granularity
'(element object nil
))
4343 (and (eq granularity
'greater-element
)
4345 (eq type
'headline
)))
4346 (org-element--parse-elements
4347 cbeg
(org-element-property :contents-end element
)
4348 ;; Possibly switch to a special mode.
4351 (if (org-element-property :quotedp element
) 'quote-section
4354 (property-drawer 'node-property
)
4356 (and (memq type
'(item plain-list
))
4357 (org-element-property :structure element
))
4358 granularity visible-only element
))
4359 ;; ELEMENT has contents. Parse objects inside, if
4360 ;; GRANULARITY allows it.
4361 ((memq granularity
'(object nil
))
4362 (org-element--parse-objects
4363 cbeg
(org-element-property :contents-end element
) element
4364 (org-element-restriction type
))))
4365 (org-element-adopt-elements acc element
)))
4369 (defun org-element--parse-objects (beg end acc restriction
)
4370 "Parse objects between BEG and END and return recursive structure.
4372 Objects are accumulated in ACC.
4374 RESTRICTION is a list of object successors which are allowed in
4375 the current object."
4376 (let ((candidates 'initial
))
4379 (narrow-to-region beg end
)
4380 (goto-char (point-min))
4381 (while (and (not (eobp))
4383 (org-element--get-next-object-candidates
4384 restriction candidates
)))
4386 (let ((pos (apply 'min
(mapcar 'cdr candidates
))))
4389 (funcall (intern (format "org-element-%s-parser"
4390 (car (rassq pos candidates
)))))))))
4391 ;; 1. Text before any object. Untabify it.
4392 (let ((obj-beg (org-element-property :begin next-object
)))
4393 (unless (= (point) obj-beg
)
4395 (org-element-adopt-elements
4397 (replace-regexp-in-string
4398 "\t" (make-string tab-width ?
)
4399 (buffer-substring-no-properties (point) obj-beg
))))))
4401 (let ((obj-end (org-element-property :end next-object
))
4402 (cont-beg (org-element-property :contents-begin next-object
)))
4403 ;; Fill contents of NEXT-OBJECT by side-effect, if it has
4404 ;; a recursive type.
4406 (memq (car next-object
) org-element-recursive-objects
))
4407 (org-element--parse-objects
4408 cont-beg
(org-element-property :contents-end next-object
)
4409 next-object
(org-element-restriction next-object
)))
4410 (setq acc
(org-element-adopt-elements acc next-object
))
4411 (goto-char obj-end
))))
4412 ;; 3. Text after last object. Untabify it.
4415 (org-element-adopt-elements
4417 (replace-regexp-in-string
4418 "\t" (make-string tab-width ?
)
4419 (buffer-substring-no-properties (point) end
)))))
4423 (defun org-element--get-next-object-candidates (restriction objects
)
4424 "Return an alist of candidates for the next object.
4426 RESTRICTION is a list of object types, as symbols. Only
4427 candidates with such types are looked after.
4429 OBJECTS is the previous candidates alist. If it is set to
4430 `initial', no search has been done before, and all symbols in
4431 RESTRICTION should be looked after.
4433 Return value is an alist whose CAR is the object type and CDR its
4434 beginning position."
4437 (if (eq objects
'initial
)
4438 ;; When searching for the first time, look for every successor
4439 ;; allowed in RESTRICTION.
4442 (funcall (intern (format "org-element-%s-successor" res
))))
4444 ;; Focus on objects returned during last search. Keep those
4445 ;; still after point. Search again objects before it.
4448 (if (>= (cdr obj
) (point)) obj
4449 (let* ((type (car obj
))
4450 (succ (or (cdr (assq type org-element-object-successor-alist
))
4453 (funcall (intern (format "org-element-%s-successor" succ
)))))))
4458 ;;; Towards A Bijective Process
4460 ;; The parse tree obtained with `org-element-parse-buffer' is really
4461 ;; a snapshot of the corresponding Org buffer. Therefore, it can be
4462 ;; interpreted and expanded into a string with canonical Org syntax.
4463 ;; Hence `org-element-interpret-data'.
4465 ;; The function relies internally on
4466 ;; `org-element--interpret-affiliated-keywords'.
4469 (defun org-element-interpret-data (data &optional parent
)
4470 "Interpret DATA as Org syntax.
4472 DATA is a parse tree, an element, an object or a secondary string
4475 Optional argument PARENT is used for recursive calls. It contains
4476 the element or object containing data, or nil.
4478 Return Org syntax as a string."
4479 (let* ((type (org-element-type data
))
4482 ;; Secondary string.
4485 (lambda (obj) (org-element-interpret-data obj parent
))
4487 ;; Full Org document.
4488 ((eq type
'org-data
)
4490 (lambda (obj) (org-element-interpret-data obj parent
))
4491 (org-element-contents data
) ""))
4492 ;; Plain text: return it.
4493 ((stringp data
) data
)
4494 ;; Element/Object without contents.
4495 ((not (org-element-contents data
))
4496 (funcall (intern (format "org-element-%s-interpreter" type
))
4498 ;; Element/Object with contents.
4500 (let* ((greaterp (memq type org-element-greater-elements
))
4501 (objectp (and (not greaterp
)
4502 (memq type org-element-recursive-objects
)))
4505 (lambda (obj) (org-element-interpret-data obj data
))
4506 (org-element-contents
4507 (if (or greaterp objectp
) data
4508 ;; Elements directly containing objects must
4509 ;; have their indentation normalized first.
4510 (org-element-normalize-contents
4512 ;; When normalizing first paragraph of an
4513 ;; item or a footnote-definition, ignore
4514 ;; first line's indentation.
4515 (and (eq type
'paragraph
)
4516 (equal data
(car (org-element-contents parent
)))
4517 (memq (org-element-type parent
)
4518 '(footnote-definition item
))))))
4520 (funcall (intern (format "org-element-%s-interpreter" type
))
4522 (if greaterp
(org-element-normalize-contents contents
)
4524 (if (memq type
'(org-data plain-text nil
)) results
4525 ;; Build white spaces. If no `:post-blank' property is
4526 ;; specified, assume its value is 0.
4527 (let ((post-blank (or (org-element-property :post-blank data
) 0)))
4528 (if (memq type org-element-all-objects
)
4529 (concat results
(make-string post-blank
32))
4531 (org-element--interpret-affiliated-keywords data
)
4532 (org-element-normalize-string results
)
4533 (make-string post-blank
10)))))))
4535 (defun org-element--interpret-affiliated-keywords (element)
4536 "Return ELEMENT's affiliated keywords as Org syntax.
4537 If there is no affiliated keyword, return the empty string."
4538 (let ((keyword-to-org
4542 (when (member key org-element-dual-keywords
)
4543 (setq dual
(cdr value
) value
(car value
)))
4546 (format "[%s]" (org-element-interpret-data dual
)))
4548 (if (member key org-element-parsed-keywords
)
4549 (org-element-interpret-data value
)
4554 (let ((value (org-element-property prop element
))
4555 (keyword (upcase (substring (symbol-name prop
) 1))))
4557 (if (or (member keyword org-element-multiple-keywords
)
4558 ;; All attribute keywords can have multiple lines.
4559 (string-match "^ATTR_" keyword
))
4560 (mapconcat (lambda (line) (funcall keyword-to-org keyword line
))
4563 (funcall keyword-to-org keyword value
)))))
4564 ;; List all ELEMENT's properties matching an attribute line or an
4565 ;; affiliated keyword, but ignore translated keywords since they
4566 ;; cannot belong to the property list.
4567 (loop for prop in
(nth 1 element
) by
'cddr
4568 when
(let ((keyword (upcase (substring (symbol-name prop
) 1))))
4569 (or (string-match "^ATTR_" keyword
)
4571 (member keyword org-element-affiliated-keywords
)
4573 org-element-keyword-translation-alist
)))))
4577 ;; Because interpretation of the parse tree must return the same
4578 ;; number of blank lines between elements and the same number of white
4579 ;; space after objects, some special care must be given to white
4582 ;; The first function, `org-element-normalize-string', ensures any
4583 ;; string different from the empty string will end with a single
4584 ;; newline character.
4586 ;; The second function, `org-element-normalize-contents', removes
4587 ;; global indentation from the contents of the current element.
4589 (defun org-element-normalize-string (s)
4590 "Ensure string S ends with a single newline character.
4592 If S isn't a string return it unchanged. If S is the empty
4593 string, return it. Otherwise, return a new string with a single
4594 newline character at its end."
4596 ((not (stringp s
)) s
)
4598 (t (and (string-match "\\(\n[ \t]*\\)*\\'" s
)
4599 (replace-match "\n" nil nil s
)))))
4601 (defun org-element-normalize-contents (element &optional ignore-first
)
4602 "Normalize plain text in ELEMENT's contents.
4604 ELEMENT must only contain plain text and objects.
4606 If optional argument IGNORE-FIRST is non-nil, ignore first line's
4607 indentation to compute maximal common indentation.
4609 Return the normalized element that is element with global
4610 indentation removed from its contents. The function assumes that
4611 indentation is not done with TAB characters."
4612 (let* ((min-ind most-positive-fixnum
)
4613 find-min-ind
; For byte-compiler.
4615 ;; Return minimal common indentation within BLOB. This is
4616 ;; done by walking recursively BLOB and updating MIN-IND
4617 ;; along the way. FIRST-FLAG is non-nil when the first
4618 ;; string hasn't been seen yet. It is required as this
4619 ;; string is the only one whose indentation doesn't happen
4620 ;; after a newline character.
4621 (lambda (blob first-flag
)
4622 (dolist (object (org-element-contents blob
))
4623 (when (and first-flag
(stringp object
))
4624 (setq first-flag nil
)
4625 (string-match "\\` *" object
)
4626 (let ((len (match-end 0)))
4627 ;; An indentation of zero means no string will be
4628 ;; modified. Quit the process.
4629 (if (zerop len
) (throw 'zero
(setq min-ind
0))
4630 (setq min-ind
(min len min-ind
)))))
4633 (dolist (line (cdr (org-split-string object
" *\n")))
4634 (unless (string= line
"")
4635 (setq min-ind
(min (org-get-indentation line
) min-ind
)))))
4636 ((memq (org-element-type object
) org-element-recursive-objects
)
4637 (funcall find-min-ind object first-flag
)))))))
4638 ;; Find minimal indentation in ELEMENT.
4639 (catch 'zero
(funcall find-min-ind element
(not ignore-first
)))
4640 (if (or (zerop min-ind
) (= min-ind most-positive-fixnum
)) element
4641 ;; Build ELEMENT back, replacing each string with the same
4642 ;; string minus common indentation.
4643 (let* (build ; For byte compiler.
4646 (lambda (blob first-flag
)
4647 ;; Return BLOB with all its strings indentation
4648 ;; shortened from MIN-IND white spaces. FIRST-FLAG
4649 ;; is non-nil when the first string hasn't been seen
4654 (when (and first-flag
(stringp object
))
4655 (setq first-flag nil
)
4657 (replace-regexp-in-string
4658 (format "\\` \\{%d\\}" min-ind
)
4662 (replace-regexp-in-string
4663 (format "\n \\{%d\\}" min-ind
) "\n" object
))
4664 ((memq (org-element-type object
)
4665 org-element-recursive-objects
)
4666 (funcall build object first-flag
))
4668 (org-element-contents blob
)))
4670 (funcall build element
(not ignore-first
))))))
4676 ;; The first move is to implement a way to obtain the smallest element
4677 ;; containing point. This is the job of `org-element-at-point'. It
4678 ;; basically jumps back to the beginning of section containing point
4679 ;; and moves, element after element, with
4680 ;; `org-element--current-element' until the container is found. Note:
4681 ;; When using `org-element-at-point', secondary values are never
4682 ;; parsed since the function focuses on elements, not on objects.
4684 ;; At a deeper level, `org-element-context' lists all elements and
4685 ;; objects containing point.
4687 ;; `org-element-nested-p' and `org-element-swap-A-B' may be used
4688 ;; internally by navigation and manipulation tools.
4691 (defun org-element-at-point (&optional keep-trail
)
4692 "Determine closest element around point.
4694 Return value is a list like (TYPE PROPS) where TYPE is the type
4695 of the element and PROPS a plist of properties associated to the
4698 Possible types are defined in `org-element-all-elements'.
4699 Properties depend on element or object type, but always include
4700 `:begin', `:end', `:parent' and `:post-blank' properties.
4702 As a special case, if point is at the very beginning of a list or
4703 sub-list, returned element will be that list instead of the first
4704 item. In the same way, if point is at the beginning of the first
4705 row of a table, returned element will be the table instead of the
4708 If optional argument KEEP-TRAIL is non-nil, the function returns
4709 a list of elements leading to element at point. The list's CAR
4710 is always the element at point. The following positions contain
4711 element's siblings, then parents, siblings of parents, until the
4712 first element of current section."
4713 (org-with-wide-buffer
4714 ;; If at a headline, parse it. It is the sole element that
4715 ;; doesn't require to know about context. Be sure to disallow
4716 ;; secondary string parsing, though.
4717 (if (org-with-limited-levels (org-at-heading-p))
4720 (if (not keep-trail
) (org-element-headline-parser (point-max) t
)
4721 (list (org-element-headline-parser (point-max) t
))))
4722 ;; Otherwise move at the beginning of the section containing
4725 (let ((origin (point))
4726 (end (save-excursion
4727 (org-with-limited-levels (outline-next-heading)) (point)))
4728 element type special-flag trail struct prevs parent
)
4729 (org-with-limited-levels
4730 (if (org-before-first-heading-p)
4731 ;; In empty lines at buffer's beginning, return nil.
4732 (progn (goto-char (point-min))
4733 (org-skip-whitespace)
4734 (when (or (eobp) (> (line-beginning-position) origin
))
4736 (org-back-to-heading)
4738 (org-skip-whitespace)
4739 (when (or (eobp) (> (line-beginning-position) origin
))
4740 ;; In blank lines just after the headline, point still
4741 ;; belongs to the headline.
4743 (progn (skip-chars-backward " \r\t\n")
4745 (if (not keep-trail
)
4746 (org-element-headline-parser (point-max) t
)
4747 (list (org-element-headline-parser
4748 (point-max) t
))))))))
4750 ;; Parse successively each element, skipping those ending
4751 ;; before original position.
4754 (org-element--current-element end
'element special-flag struct
)
4756 (org-element-put-property element
:parent parent
)
4757 (when keep-trail
(push element trail
))
4759 ;; 1. Skip any element ending before point. Also skip
4760 ;; element ending at point when we're sure that another
4761 ;; element has started.
4762 ((let ((elem-end (org-element-property :end element
)))
4763 (when (or (< elem-end origin
)
4764 (and (= elem-end origin
) (/= elem-end end
)))
4765 (goto-char elem-end
))))
4766 ;; 2. An element containing point is always the element at
4768 ((not (memq type org-element-greater-elements
))
4769 (throw 'exit
(if keep-trail trail element
)))
4770 ;; 3. At any other greater element type, if point is
4771 ;; within contents, move into it.
4773 (let ((cbeg (org-element-property :contents-begin element
))
4774 (cend (org-element-property :contents-end element
)))
4775 (if (or (not cbeg
) (not cend
) (> cbeg origin
) (< cend origin
)
4776 ;; Create an anchor for tables and plain lists:
4777 ;; when point is at the very beginning of these
4778 ;; elements, ignoring affiliated keywords,
4779 ;; target them instead of their contents.
4780 (and (= cbeg origin
) (memq type
'(plain-list table
)))
4781 ;; When point is at contents end, do not move
4782 ;; into elements with an explicit ending, but
4783 ;; return that element instead.
4784 (and (= cend origin
)
4787 drawer dynamic-block inlinetask
4788 property-drawer quote-block
4790 ;; Corner case: if a list ends at the
4791 ;; end of a buffer without a final new
4792 ;; line, return last element in last
4794 (and (memq type
'(item plain-list
))
4795 (progn (goto-char cend
)
4796 (or (bolp) (not (eobp))))))))
4797 (throw 'exit
(if keep-trail trail element
))
4798 (setq parent element
)
4801 (setq special-flag
'item
4802 struct
(org-element-property :structure element
)))
4803 (item (setq special-flag nil
))
4805 (setq special-flag
'node-property struct nil
))
4806 (table (setq special-flag
'table-row struct nil
))
4807 (otherwise (setq special-flag nil struct nil
)))
4809 (goto-char cbeg
)))))))))))
4812 (defun org-element-context (&optional element
)
4813 "Return closest element or object around point.
4815 Return value is a list like (TYPE PROPS) where TYPE is the type
4816 of the element or object and PROPS a plist of properties
4819 Possible types are defined in `org-element-all-elements' and
4820 `org-element-all-objects'. Properties depend on element or
4821 object type, but always include `:begin', `:end', `:parent' and
4824 Optional argument ELEMENT, when non-nil, is the closest element
4825 containing point, as returned by `org-element-at-point'.
4826 Providing it allows for quicker computation."
4827 (catch 'objects-forbidden
4828 (org-with-wide-buffer
4829 (let* ((origin (point))
4830 (element (or element
(org-element-at-point)))
4831 (type (org-element-type element
))
4833 ;; Check if point is inside an element containing objects or at
4834 ;; a secondary string. In that case, narrow buffer to the
4835 ;; containing area. Otherwise, return ELEMENT.
4837 ;; At a parsed affiliated keyword, check if we're inside main
4839 ((let ((post (org-element-property :post-affiliated element
)))
4840 (and post
(< origin post
)))
4842 (let ((case-fold-search t
)) (looking-at org-element--affiliated-re
))
4844 ((not (member-ignore-case (match-string 1)
4845 org-element-parsed-keywords
))
4846 (throw 'objects-forbidden element
))
4847 ((< (match-end 0) origin
)
4848 (narrow-to-region (match-end 0) (line-end-position)))
4849 ((and (match-beginning 2)
4850 (>= origin
(match-beginning 2))
4851 (< origin
(match-end 2)))
4852 (narrow-to-region (match-beginning 2) (match-end 2)))
4853 (t (throw 'objects-forbidden element
)))
4854 ;; Also change type to retrieve correct restrictions.
4855 (setq type
'keyword
))
4856 ;; At an item, objects can only be located within tag, if any.
4858 (let ((tag (org-element-property :tag element
)))
4859 (if (not tag
) (throw 'objects-forbidden element
)
4861 (search-forward tag
(line-end-position))
4862 (goto-char (match-beginning 0))
4863 (if (and (>= origin
(point)) (< origin
(match-end 0)))
4864 (narrow-to-region (point) (match-end 0))
4865 (throw 'objects-forbidden element
)))))
4866 ;; At an headline or inlinetask, objects are located within
4868 ((memq type
'(headline inlinetask
))
4869 (goto-char (org-element-property :begin element
))
4870 (skip-chars-forward "*")
4871 (if (and (> origin
(point)) (< origin
(line-end-position)))
4872 (narrow-to-region (point) (line-end-position))
4873 (throw 'objects-forbidden element
)))
4874 ;; At a paragraph, a table-row or a verse block, objects are
4875 ;; located within their contents.
4876 ((memq type
'(paragraph table-row verse-block
))
4877 (let ((cbeg (org-element-property :contents-begin element
))
4878 (cend (org-element-property :contents-end element
)))
4879 ;; CBEG is nil for table rules.
4880 (if (and cbeg cend
(>= origin cbeg
) (< origin cend
))
4881 (narrow-to-region cbeg cend
)
4882 (throw 'objects-forbidden element
))))
4883 ;; At a parsed keyword, objects are located within value.
4885 (if (not (member (org-element-property :key element
)
4886 org-element-document-properties
))
4887 (throw 'objects-forbidden element
)
4889 (search-forward ":")
4890 (if (and (>= origin
(point)) (< origin
(line-end-position)))
4891 (narrow-to-region (point) (line-end-position))
4892 (throw 'objects-forbidden element
))))
4893 ;; At a planning line, if point is at a timestamp, return it,
4894 ;; otherwise, return element.
4895 ((eq type
'planning
)
4896 (dolist (p '(:closed
:deadline
:scheduled
))
4897 (let ((timestamp (org-element-property p element
)))
4898 (when (and timestamp
4899 (<= (org-element-property :begin timestamp
) origin
)
4900 (> (org-element-property :end timestamp
) origin
))
4901 (throw 'objects-forbidden timestamp
))))
4902 (throw 'objects-forbidden element
))
4903 (t (throw 'objects-forbidden element
)))
4904 (goto-char (point-min))
4905 (let ((restriction (org-element-restriction type
))
4907 (candidates 'initial
))
4909 (while (setq candidates
4910 (org-element--get-next-object-candidates
4911 restriction candidates
))
4912 (let ((closest-cand (rassq (apply 'min
(mapcar 'cdr candidates
))
4914 ;; If ORIGIN is before next object in element, there's
4915 ;; no point in looking further.
4916 (if (> (cdr closest-cand
) origin
) (throw 'exit parent
)
4918 (progn (goto-char (cdr closest-cand
))
4919 (funcall (intern (format "org-element-%s-parser"
4920 (car closest-cand
))))))
4921 (cbeg (org-element-property :contents-begin object
))
4922 (cend (org-element-property :contents-end object
))
4923 (obj-end (org-element-property :end object
)))
4925 ;; ORIGIN is after OBJECT, so skip it.
4926 ((<= obj-end origin
) (goto-char obj-end
))
4927 ;; ORIGIN is within a non-recursive object or at
4928 ;; an object boundaries: Return that object.
4929 ((or (not cbeg
) (< origin cbeg
) (>= origin cend
))
4931 (org-element-put-property object
:parent parent
)))
4932 ;; Otherwise, move within current object and
4933 ;; restrict search to the end of its contents.
4935 (narrow-to-region (point) cend
)
4936 (org-element-put-property object
:parent parent
)
4938 restriction
(org-element-restriction object
)
4939 candidates
'initial
)))))))
4942 (defun org-element-nested-p (elem-A elem-B
)
4943 "Non-nil when elements ELEM-A and ELEM-B are nested."
4944 (let ((beg-A (org-element-property :begin elem-A
))
4945 (beg-B (org-element-property :begin elem-B
))
4946 (end-A (org-element-property :end elem-A
))
4947 (end-B (org-element-property :end elem-B
)))
4948 (or (and (>= beg-A beg-B
) (<= end-A end-B
))
4949 (and (>= beg-B beg-A
) (<= end-B end-A
)))))
4951 (defun org-element-swap-A-B (elem-A elem-B
)
4952 "Swap elements ELEM-A and ELEM-B.
4953 Assume ELEM-B is after ELEM-A in the buffer. Leave point at the
4955 (goto-char (org-element-property :begin elem-A
))
4956 ;; There are two special cases when an element doesn't start at bol:
4957 ;; the first paragraph in an item or in a footnote definition.
4958 (let ((specialp (not (bolp))))
4959 ;; Only a paragraph without any affiliated keyword can be moved at
4960 ;; ELEM-A position in such a situation. Note that the case of
4961 ;; a footnote definition is impossible: it cannot contain two
4962 ;; paragraphs in a row because it cannot contain a blank line.
4964 (or (not (eq (org-element-type elem-B
) 'paragraph
))
4965 (/= (org-element-property :begin elem-B
)
4966 (org-element-property :contents-begin elem-B
))))
4967 (error "Cannot swap elements"))
4968 ;; In a special situation, ELEM-A will have no indentation. We'll
4969 ;; give it ELEM-B's (which will in, in turn, have no indentation).
4970 (let* ((ind-B (when specialp
4971 (goto-char (org-element-property :begin elem-B
))
4972 (org-get-indentation)))
4973 (beg-A (org-element-property :begin elem-A
))
4974 (end-A (save-excursion
4975 (goto-char (org-element-property :end elem-A
))
4976 (skip-chars-backward " \r\t\n")
4978 (beg-B (org-element-property :begin elem-B
))
4979 (end-B (save-excursion
4980 (goto-char (org-element-property :end elem-B
))
4981 (skip-chars-backward " \r\t\n")
4983 ;; Store overlays responsible for visibility status. We
4984 ;; also need to store their boundaries as they will be
4985 ;; removed from buffer.
4988 (mapcar (lambda (ov) (list ov
(overlay-start ov
) (overlay-end ov
)))
4989 (overlays-in beg-A end-A
))
4990 (mapcar (lambda (ov) (list ov
(overlay-start ov
) (overlay-end ov
)))
4991 (overlays-in beg-B end-B
))))
4993 (body-A (buffer-substring beg-A end-A
))
4994 (body-B (delete-and-extract-region beg-B end-B
)))
4997 (setq body-B
(replace-regexp-in-string "\\`[ \t]*" "" body-B
))
4998 (org-indent-to-column ind-B
))
5000 ;; Restore ex ELEM-A overlays.
5001 (let ((offset (- beg-B beg-A
)))
5004 (car ov
) (+ (nth 1 ov
) offset
) (+ (nth 2 ov
) offset
)))
5007 (delete-region beg-A end-A
)
5009 ;; Restore ex ELEM-B overlays.
5012 (car ov
) (- (nth 1 ov
) offset
) (- (nth 2 ov
) offset
)))
5014 (goto-char (org-element-property :end elem-B
)))))
5016 (provide 'org-element
)
5019 ;; generated-autoload-file: "org-loaddefs.el"
5022 ;;; org-element.el ends here