1 ;;; org-element.el --- Parser And Applications for Org syntax
3 ;; Copyright (C) 2012 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; Org syntax can be divided into three categories: "Greater
26 ;; elements", "Elements" and "Objects".
28 ;; Elements are related to the structure of the document. Indeed, all
29 ;; elements are a cover for the document: each position within belongs
30 ;; to at least one element.
32 ;; An element always starts and ends at the beginning of a line. With
33 ;; a few exceptions (`clock', `headline', `inlinetask', `item',
34 ;; `planning', `node-property', `quote-section' `section' and
35 ;; `table-row' types), it can also accept a fixed set of keywords as
36 ;; attributes. Those are called "affiliated keywords" to distinguish
37 ;; them from other keywords, which are full-fledged elements. Almost
38 ;; all affiliated keywords are referenced in
39 ;; `org-element-affiliated-keywords'; the others are export attributes
40 ;; and start with "ATTR_" prefix.
42 ;; Element containing other elements (and only elements) are called
43 ;; greater elements. Concerned types are: `center-block', `drawer',
44 ;; `dynamic-block', `footnote-definition', `headline', `inlinetask',
45 ;; `item', `plain-list', `property-drawer', `quote-block', `section'
46 ;; and `special-block'.
48 ;; Other element types are: `babel-call', `clock', `comment',
49 ;; `comment-block', `example-block', `export-block', `fixed-width',
50 ;; `horizontal-rule', `keyword', `latex-environment', `node-property',
51 ;; `paragraph', `planning', `quote-section', `src-block', `table',
52 ;; `table-row' and `verse-block'. Among them, `paragraph' and
53 ;; `verse-block' types can contain Org objects and plain text.
55 ;; Objects are related to document's contents. Some of them are
56 ;; recursive. Associated types are of the following: `bold', `code',
57 ;; `entity', `export-snippet', `footnote-reference',
58 ;; `inline-babel-call', `inline-src-block', `italic',
59 ;; `latex-fragment', `line-break', `link', `macro', `radio-target',
60 ;; `statistics-cookie', `strike-through', `subscript', `superscript',
61 ;; `table-cell', `target', `timestamp', `underline' and `verbatim'.
63 ;; Some elements also have special properties whose value can hold
64 ;; objects themselves (i.e. an item tag or an headline name). Such
65 ;; values are called "secondary strings". Any object belongs to
66 ;; either an element or a secondary string.
68 ;; Notwithstanding affiliated keywords, each greater element, element
69 ;; and object has a fixed set of properties attached to it. Among
70 ;; them, four are shared by all types: `:begin' and `:end', which
71 ;; refer to the beginning and ending buffer positions of the
72 ;; considered element or object, `:post-blank', which holds the number
73 ;; of blank lines, or white spaces, at its end and `:parent' which
74 ;; refers to the element or object containing it. Greater elements
75 ;; and elements containing objects will also have `:contents-begin'
76 ;; and `:contents-end' properties to delimit contents.
78 ;; At the lowest level, a `:parent' property is also attached to any
79 ;; string, as a text property.
81 ;; Lisp-wise, an element or an object can be represented as a list.
82 ;; It follows the pattern (TYPE PROPERTIES CONTENTS), where:
83 ;; TYPE is a symbol describing the Org element or object.
84 ;; PROPERTIES is the property list attached to it. See docstring of
85 ;; appropriate parsing function to get an exhaustive
87 ;; CONTENTS is a list of elements, objects or raw strings contained
88 ;; in the current element or object, when applicable.
90 ;; An Org buffer is a nested list of such elements and objects, whose
91 ;; type is `org-data' and properties is nil.
93 ;; The first part of this file defines Org syntax, while the second
94 ;; one provide accessors and setters functions.
96 ;; The next part implements a parser and an interpreter for each
97 ;; element and object type in Org syntax.
99 ;; The following part creates a fully recursive buffer parser. It
100 ;; also provides a tool to map a function to elements or objects
101 ;; matching some criteria in the parse tree. Functions of interest
102 ;; are `org-element-parse-buffer', `org-element-map' and, to a lesser
103 ;; extent, `org-element-parse-secondary-string'.
105 ;; The penultimate part is the cradle of an interpreter for the
106 ;; obtained parse tree: `org-element-interpret-data'.
108 ;; The library ends by furnishing `org-element-at-point' function, and
109 ;; a way to give information about document structure around point
110 ;; with `org-element-context'.
115 (eval-when-compile (require 'cl
))
120 ;;; Definitions And Rules
122 ;; Define elements, greater elements and specify recursive objects,
123 ;; along with the affiliated keywords recognized. Also set up
124 ;; restrictions on recursive objects combinations.
126 ;; These variables really act as a control center for the parsing
129 (defconst org-element-paragraph-separate
131 ;; Headlines, inlinetasks.
132 org-outline-regexp
"\\|"
133 ;; Footnote definitions.
134 "\\[\\(?:[0-9]+\\|fn:[-_[:word:]]+\\)\\]" "\\|"
138 ;; Tables (any type).
139 "\\(?:|\\|\\+-[-+]\\)" "\\|"
140 ;; Blocks (any type), Babel calls, drawers (any type),
141 ;; fixed-width areas and keywords. Note: this is only an
142 ;; indication and need some thorough check.
145 "-\\{5,\\}[ \t]*$" "\\|"
146 ;; LaTeX environments.
147 "\\\\begin{\\([A-Za-z0-9]+\\*?\\)}" "\\|"
148 ;; Planning and Clock lines.
149 (regexp-opt (list org-scheduled-string
155 (let ((term (case org-plain-list-ordered-item-terminator
156 (?\
) ")") (?.
"\\.") (otherwise "[.)]")))
157 (alpha (and org-alphabetical-lists
"\\|[A-Za-z]")))
158 (concat "\\(?:[-+*]\\|\\(?:[0-9]+" alpha
"\\)" term
"\\)"
159 "\\(?:[ \t]\\|$\\)"))
161 "Regexp to separate paragraphs in an Org buffer.
162 In the case of lines starting with \"#\" and \":\", this regexp
163 is not sufficient to know if point is at a paragraph ending. See
164 `org-element-paragraph-parser' for more information.")
166 (defconst org-element-all-elements
167 '(babel-call center-block clock comment comment-block drawer dynamic-block
168 example-block export-block fixed-width footnote-definition
169 headline horizontal-rule inlinetask item keyword
170 latex-environment node-property paragraph plain-list planning
171 property-drawer quote-block quote-section section special-block
172 src-block table table-row verse-block
)
173 "Complete list of element types.")
175 (defconst org-element-greater-elements
176 '(center-block drawer dynamic-block footnote-definition headline inlinetask
177 item plain-list property-drawer quote-block section
179 "List of recursive element types aka Greater Elements.")
181 (defconst org-element-all-successors
182 '(export-snippet footnote-reference inline-babel-call inline-src-block
183 latex-or-entity line-break link macro radio-target
184 statistics-cookie sub
/superscript table-cell target
185 text-markup timestamp
)
186 "Complete list of successors.")
188 (defconst org-element-object-successor-alist
189 '((subscript . sub
/superscript
) (superscript . sub
/superscript
)
190 (bold . text-markup
) (code . text-markup
) (italic . text-markup
)
191 (strike-through . text-markup
) (underline . text-markup
)
192 (verbatim . text-markup
) (entity . latex-or-entity
)
193 (latex-fragment . latex-or-entity
))
194 "Alist of translations between object type and successor name.
196 Sharing the same successor comes handy when, for example, the
197 regexp matching one object can also match the other object.")
199 (defconst org-element-all-objects
200 '(bold code entity export-snippet footnote-reference inline-babel-call
201 inline-src-block italic line-break latex-fragment link macro
202 radio-target statistics-cookie strike-through subscript superscript
203 table-cell target timestamp underline verbatim
)
204 "Complete list of object types.")
206 (defconst org-element-recursive-objects
207 '(bold italic link subscript radio-target strike-through superscript
208 table-cell underline
)
209 "List of recursive object types.")
211 (defconst org-element-block-name-alist
212 '(("CENTER" . org-element-center-block-parser
)
213 ("COMMENT" . org-element-comment-block-parser
)
214 ("EXAMPLE" . org-element-example-block-parser
)
215 ("QUOTE" . org-element-quote-block-parser
)
216 ("SRC" . org-element-src-block-parser
)
217 ("VERSE" . org-element-verse-block-parser
))
218 "Alist between block names and the associated parsing function.
219 Names must be uppercase. Any block whose name has no association
220 is parsed with `org-element-special-block-parser'.")
222 (defconst org-element-link-type-is-file
223 '("file" "file+emacs" "file+sys" "docview")
224 "List of link types equivalent to \"file\".
225 Only these types can accept search options and an explicit
226 application to open them.")
228 (defconst org-element-affiliated-keywords
229 '("CAPTION" "DATA" "HEADER" "HEADERS" "LABEL" "NAME" "PLOT" "RESNAME" "RESULT"
230 "RESULTS" "SOURCE" "SRCNAME" "TBLNAME")
231 "List of affiliated keywords as strings.
232 By default, all keywords setting attributes (i.e. \"ATTR_LATEX\")
233 are affiliated keywords and need not to be in this list.")
235 (defconst org-element--affiliated-re
236 (format "[ \t]*#\\+%s:"
237 ;; Regular affiliated keywords.
238 (format "\\(%s\\|ATTR_[-_A-Za-z0-9]+\\)\\(?:\\[\\(.*\\)\\]\\)?"
239 (regexp-opt org-element-affiliated-keywords
)))
240 "Regexp matching any affiliated keyword.
242 Keyword name is put in match group 1. Moreover, if keyword
243 belongs to `org-element-dual-keywords', put the dual value in
246 Don't modify it, set `org-element-affiliated-keywords' instead.")
248 (defconst org-element-keyword-translation-alist
249 '(("DATA" .
"NAME") ("LABEL" .
"NAME") ("RESNAME" .
"NAME")
250 ("SOURCE" .
"NAME") ("SRCNAME" .
"NAME") ("TBLNAME" .
"NAME")
251 ("RESULT" .
"RESULTS") ("HEADERS" .
"HEADER"))
252 "Alist of usual translations for keywords.
253 The key is the old name and the value the new one. The property
254 holding their value will be named after the translated name.")
256 (defconst org-element-multiple-keywords
'("CAPTION" "HEADER")
257 "List of affiliated keywords that can occur more than once in an element.
259 Their value will be consed into a list of strings, which will be
260 returned as the value of the property.
262 This list is checked after translations have been applied. See
263 `org-element-keyword-translation-alist'.
265 By default, all keywords setting attributes (i.e. \"ATTR_LATEX\")
266 allow multiple occurrences and need not to be in this list.")
268 (defconst org-element-parsed-keywords
'("CAPTION")
269 "List of affiliated keywords whose value can be parsed.
271 Their value will be stored as a secondary string: a list of
274 This list is checked after translations have been applied. See
275 `org-element-keyword-translation-alist'.")
277 (defconst org-element-dual-keywords
'("CAPTION" "RESULTS")
278 "List of affiliated keywords which can have a secondary value.
280 In Org syntax, they can be written with optional square brackets
281 before the colons. For example, RESULTS keyword can be
282 associated to a hash value with the following:
284 #+RESULTS[hash-string]: some-source
286 This list is checked after translations have been applied. See
287 `org-element-keyword-translation-alist'.")
289 (defconst org-element-document-properties
'("AUTHOR" "DATE" "TITLE")
290 "List of properties associated to the whole document.
291 Any keyword in this list will have its value parsed and stored as
292 a secondary string.")
294 (defconst org-element-object-restrictions
295 '((bold export-snippet inline-babel-call inline-src-block latex-or-entity link
296 radio-target sub
/superscript target text-markup timestamp
)
297 (footnote-reference export-snippet footnote-reference inline-babel-call
298 inline-src-block latex-or-entity line-break link macro
299 radio-target sub
/superscript target text-markup
301 (headline inline-babel-call inline-src-block latex-or-entity link macro
302 radio-target statistics-cookie sub
/superscript target text-markup
304 (inlinetask inline-babel-call inline-src-block latex-or-entity link macro
305 radio-target sub
/superscript target text-markup timestamp
)
306 (italic export-snippet inline-babel-call inline-src-block latex-or-entity
307 link radio-target sub
/superscript target text-markup timestamp
)
308 (item export-snippet footnote-reference inline-babel-call latex-or-entity
309 link macro radio-target sub
/superscript target text-markup
)
310 (keyword inline-babel-call inline-src-block latex-or-entity link macro
311 sub
/superscript text-markup
)
312 (link export-snippet inline-babel-call inline-src-block latex-or-entity link
313 sub
/superscript text-markup
)
314 (paragraph export-snippet footnote-reference inline-babel-call
315 inline-src-block latex-or-entity line-break link macro
316 radio-target statistics-cookie sub
/superscript target text-markup
318 (radio-target export-snippet latex-or-entity sub
/superscript
)
319 (strike-through export-snippet inline-babel-call inline-src-block
320 latex-or-entity link radio-target sub
/superscript target
321 text-markup timestamp
)
322 (subscript export-snippet inline-babel-call inline-src-block latex-or-entity
323 sub
/superscript target text-markup
)
324 (superscript export-snippet inline-babel-call inline-src-block
325 latex-or-entity sub
/superscript target text-markup
)
326 (table-cell export-snippet latex-or-entity link macro radio-target
327 sub
/superscript target text-markup timestamp
)
328 (table-row table-cell
)
329 (underline export-snippet inline-babel-call inline-src-block latex-or-entity
330 link radio-target sub
/superscript target text-markup timestamp
)
331 (verse-block footnote-reference inline-babel-call inline-src-block
332 latex-or-entity line-break link macro radio-target
333 sub
/superscript target text-markup timestamp
))
334 "Alist of objects restrictions.
336 CAR is an element or object type containing objects and CDR is
337 a list of successors that will be called within an element or
340 For example, in a `radio-target' object, one can only find
341 entities, export snippets, latex-fragments, subscript and
344 This alist also applies to secondary string. For example, an
345 `headline' type element doesn't directly contain objects, but
346 still has an entry since one of its properties (`:title') does.")
348 (defconst org-element-secondary-value-alist
349 '((headline .
:title
)
350 (inlinetask .
:title
)
352 (footnote-reference .
:inline-definition
))
353 "Alist between element types and location of secondary value.")
357 ;;; Accessors and Setters
359 ;; Provide four accessors: `org-element-type', `org-element-property'
360 ;; `org-element-contents' and `org-element-restriction'.
362 ;; Setter functions allow to modify elements by side effect. There is
363 ;; `org-element-put-property', `org-element-set-contents',
364 ;; `org-element-set-element' and `org-element-adopt-element'. Note
365 ;; that `org-element-set-element' and `org-element-adopt-elements' are
366 ;; higher level functions since also update `:parent' property.
368 (defsubst org-element-type
(element)
369 "Return type of ELEMENT.
371 The function returns the type of the element or object provided.
372 It can also return the following special value:
373 `plain-text' for a string
374 `org-data' for a complete document
375 nil in any other case."
377 ((not (consp element
)) (and (stringp element
) 'plain-text
))
378 ((symbolp (car element
)) (car element
))))
380 (defsubst org-element-property
(property element
)
381 "Extract the value from the PROPERTY of an ELEMENT."
382 (if (stringp element
) (get-text-property 0 property element
)
383 (plist-get (nth 1 element
) property
)))
385 (defsubst org-element-contents
(element)
386 "Extract contents from an ELEMENT."
387 (and (consp element
) (nthcdr 2 element
)))
389 (defsubst org-element-restriction
(element)
390 "Return restriction associated to ELEMENT.
391 ELEMENT can be an element, an object or a symbol representing an
392 element or object type."
393 (cdr (assq (if (symbolp element
) element
(org-element-type element
))
394 org-element-object-restrictions
)))
396 (defsubst org-element-put-property
(element property value
)
397 "In ELEMENT set PROPERTY to VALUE.
398 Return modified element."
399 (if (stringp element
) (org-add-props element nil property value
)
400 (setcar (cdr element
) (plist-put (nth 1 element
) property value
))
403 (defsubst org-element-set-contents
(element &rest contents
)
404 "Set ELEMENT contents to CONTENTS.
405 Return modified element."
406 (cond ((not element
) (list contents
))
407 ((cdr element
) (setcdr (cdr element
) contents
))
408 (t (nconc element contents
))))
410 (defsubst org-element-set-element
(old new
)
411 "Replace element or object OLD with element or object NEW.
412 The function takes care of setting `:parent' property for NEW."
413 ;; Since OLD is going to be changed into NEW by side-effect, first
414 ;; make sure that every element or object within NEW has OLD as
416 (mapc (lambda (blob) (org-element-put-property blob
:parent old
))
417 (org-element-contents new
))
418 ;; Transfer contents.
419 (apply 'org-element-set-contents old
(org-element-contents new
))
420 ;; Ensure NEW has same parent as OLD, then overwrite OLD properties
422 (org-element-put-property new
:parent
(org-element-property :parent old
))
423 (setcar (cdr old
) (nth 1 new
))
425 (setcar old
(car new
)))
427 (defsubst org-element-adopt-elements
(parent &rest children
)
428 "Append elements to the contents of another element.
430 PARENT is an element or object. CHILDREN can be elements,
431 objects, or a strings.
433 The function takes care of setting `:parent' property for CHILD.
434 Return parent element."
435 (if (not parent
) children
436 ;; Link every child to PARENT.
437 (mapc (lambda (child) (org-element-put-property child
:parent parent
))
439 ;; Add CHILDREN at the end of PARENT contents.
440 (apply 'org-element-set-contents
442 (nconc (org-element-contents parent
) children
))
443 ;; Return modified PARENT element.
450 ;; For each greater element type, we define a parser and an
453 ;; A parser returns the element or object as the list described above.
454 ;; Most of them accepts no argument. Though, exceptions exist. Hence
455 ;; every element containing a secondary string (see
456 ;; `org-element-secondary-value-alist') will accept an optional
457 ;; argument to toggle parsing of that secondary string. Moreover,
458 ;; `item' parser requires current list's structure as its first
461 ;; An interpreter accepts two arguments: the list representation of
462 ;; the element or object, and its contents. The latter may be nil,
463 ;; depending on the element or object considered. It returns the
464 ;; appropriate Org syntax, as a string.
466 ;; Parsing functions must follow the naming convention:
467 ;; org-element-TYPE-parser, where TYPE is greater element's type, as
468 ;; defined in `org-element-greater-elements'.
470 ;; Similarly, interpreting functions must follow the naming
471 ;; convention: org-element-TYPE-interpreter.
473 ;; With the exception of `headline' and `item' types, greater elements
474 ;; cannot contain other greater elements of their own type.
476 ;; Beside implementing a parser and an interpreter, adding a new
477 ;; greater element requires to tweak `org-element--current-element'.
478 ;; Moreover, the newly defined type must be added to both
479 ;; `org-element-all-elements' and `org-element-greater-elements'.
484 (defun org-element-center-block-parser (limit affiliated
)
485 "Parse a center block.
487 LIMIT bounds the search. AFFILIATED is a list of which CAR is
488 the buffer position at the beginning of the first affiliated
489 keyword and CDR is a plist of affiliated keywords along with
492 Return a list whose CAR is `center-block' and CDR is a plist
493 containing `:begin', `:end', `:hiddenp', `:contents-begin',
494 `:contents-end' and `:post-blank' keywords.
496 Assume point is at the beginning of the block."
497 (let ((case-fold-search t
))
498 (if (not (save-excursion
499 (re-search-forward "^[ \t]*#\\+END_CENTER[ \t]*$" limit t
)))
500 ;; Incomplete block: parse it as a paragraph.
501 (org-element-paragraph-parser limit affiliated
)
502 (let ((block-end-line (match-beginning 0)))
503 (let* ((begin (car affiliated
))
504 ;; Empty blocks have no contents.
505 (contents-begin (progn (forward-line)
506 (and (< (point) block-end-line
)
508 (contents-end (and contents-begin block-end-line
))
509 (hidden (org-invisible-p2))
510 (pos-before-blank (progn (goto-char block-end-line
)
513 (end (save-excursion (skip-chars-forward " \r\t\n" limit
)
514 (skip-chars-backward " \t")
515 (if (bolp) (point) (line-end-position)))))
521 :contents-begin contents-begin
522 :contents-end contents-end
523 :post-blank
(count-lines pos-before-blank end
))
524 (cdr affiliated
))))))))
526 (defun org-element-center-block-interpreter (center-block contents
)
527 "Interpret CENTER-BLOCK element as Org syntax.
528 CONTENTS is the contents of the element."
529 (format "#+BEGIN_CENTER\n%s#+END_CENTER" contents
))
534 (defun org-element-drawer-parser (limit affiliated
)
537 LIMIT bounds the search. AFFILIATED is a list of which CAR is
538 the buffer position at the beginning of the first affiliated
539 keyword and CDR is a plist of affiliated keywords along with
542 Return a list whose CAR is `drawer' and CDR is a plist containing
543 `:drawer-name', `:begin', `:end', `:hiddenp', `:contents-begin',
544 `:contents-end' and `:post-blank' keywords.
546 Assume point is at beginning of drawer."
547 (let ((case-fold-search t
))
548 (if (not (save-excursion (re-search-forward "^[ \t]*:END:[ \t]*$" limit t
)))
549 ;; Incomplete drawer: parse it as a paragraph.
550 (org-element-paragraph-parser limit affiliated
)
552 (let* ((drawer-end-line (match-beginning 0))
553 (name (progn (looking-at org-drawer-regexp
)
554 (org-match-string-no-properties 1)))
555 (begin (car affiliated
))
556 ;; Empty drawers have no contents.
557 (contents-begin (progn (forward-line)
558 (and (< (point) drawer-end-line
)
560 (contents-end (and contents-begin drawer-end-line
))
561 (hidden (org-invisible-p2))
562 (pos-before-blank (progn (goto-char drawer-end-line
)
565 (end (progn (skip-chars-forward " \r\t\n" limit
)
566 (skip-chars-backward " \t")
567 (if (bolp) (point) (line-end-position)))))
574 :contents-begin contents-begin
575 :contents-end contents-end
576 :post-blank
(count-lines pos-before-blank end
))
577 (cdr affiliated
))))))))
579 (defun org-element-drawer-interpreter (drawer contents
)
580 "Interpret DRAWER element as Org syntax.
581 CONTENTS is the contents of the element."
582 (format ":%s:\n%s:END:"
583 (org-element-property :drawer-name drawer
)
589 (defun org-element-dynamic-block-parser (limit affiliated
)
590 "Parse a dynamic block.
592 LIMIT bounds the search. AFFILIATED is a list of which CAR is
593 the buffer position at the beginning of the first affiliated
594 keyword and CDR is a plist of affiliated keywords along with
597 Return a list whose CAR is `dynamic-block' and CDR is a plist
598 containing `:block-name', `:begin', `:end', `:hiddenp',
599 `:contents-begin', `:contents-end', `:arguments' and
600 `:post-blank' keywords.
602 Assume point is at beginning of dynamic block."
603 (let ((case-fold-search t
))
604 (if (not (save-excursion
605 (re-search-forward "^[ \t]*#\\+END:?[ \t]*$" limit t
)))
606 ;; Incomplete block: parse it as a paragraph.
607 (org-element-paragraph-parser limit affiliated
)
608 (let ((block-end-line (match-beginning 0)))
610 (let* ((name (progn (looking-at org-dblock-start-re
)
611 (org-match-string-no-properties 1)))
612 (arguments (org-match-string-no-properties 3))
613 (begin (car affiliated
))
614 ;; Empty blocks have no contents.
615 (contents-begin (progn (forward-line)
616 (and (< (point) block-end-line
)
618 (contents-end (and contents-begin block-end-line
))
619 (hidden (org-invisible-p2))
620 (pos-before-blank (progn (goto-char block-end-line
)
623 (end (progn (skip-chars-forward " \r\t\n" limit
)
624 (skip-chars-backward " \t")
625 (if (bolp) (point) (line-end-position)))))
633 :contents-begin contents-begin
634 :contents-end contents-end
635 :post-blank
(count-lines pos-before-blank end
))
636 (cdr affiliated
)))))))))
638 (defun org-element-dynamic-block-interpreter (dynamic-block contents
)
639 "Interpret DYNAMIC-BLOCK element as Org syntax.
640 CONTENTS is the contents of the element."
641 (format "#+BEGIN: %s%s\n%s#+END:"
642 (org-element-property :block-name dynamic-block
)
643 (let ((args (org-element-property :arguments dynamic-block
)))
644 (and args
(concat " " args
)))
648 ;;;; Footnote Definition
650 (defun org-element-footnote-definition-parser (limit affiliated
)
651 "Parse a footnote definition.
653 LIMIT bounds the search. AFFILIATED is a list of which CAR is
654 the buffer position at the beginning of the first affiliated
655 keyword and CDR is a plist of affiliated keywords along with
658 Return a list whose CAR is `footnote-definition' and CDR is
659 a plist containing `:label', `:begin' `:end', `:contents-begin',
660 `:contents-end' and `:post-blank' keywords.
662 Assume point is at the beginning of the footnote definition."
664 (let* ((label (progn (looking-at org-footnote-definition-re
)
665 (org-match-string-no-properties 1)))
666 (begin (car affiliated
))
667 (ending (save-excursion
671 (concat org-outline-regexp-bol
"\\|"
672 org-footnote-definition-re
"\\|"
673 "^[ \t]*$") limit
'move
))
676 (contents-begin (progn (search-forward "]")
677 (skip-chars-forward " \r\t\n" ending
)
678 (and (/= (point) ending
) (point))))
679 (contents-end (and contents-begin ending
))
680 (end (progn (goto-char ending
)
681 (skip-chars-forward " \r\t\n" limit
)
682 (skip-chars-backward " \t")
683 (if (bolp) (point) (line-end-position)))))
684 (list 'footnote-definition
689 :contents-begin contents-begin
690 :contents-end contents-end
691 :post-blank
(count-lines ending end
))
692 (cdr affiliated
))))))
694 (defun org-element-footnote-definition-interpreter (footnote-definition contents
)
695 "Interpret FOOTNOTE-DEFINITION element as Org syntax.
696 CONTENTS is the contents of the footnote-definition."
697 (concat (format "[%s]" (org-element-property :label footnote-definition
))
704 (defun org-element-headline-parser (limit &optional raw-secondary-p
)
707 Return a list whose CAR is `headline' and CDR is a plist
708 containing `:raw-value', `:title', `:begin', `:end',
709 `:pre-blank', `:hiddenp', `:contents-begin' and `:contents-end',
710 `:level', `:priority', `:tags', `:todo-keyword',`:todo-type',
711 `:scheduled', `:deadline', `:timestamp', `:clock', `:category',
712 `:quotedp', `:archivedp', `:commentedp' and `:footnote-section-p'
715 The plist also contains any property set in the property drawer,
716 with its name in lowercase, the underscores replaced with hyphens
717 and colons at the beginning (i.e. `:custom-id').
719 When RAW-SECONDARY-P is non-nil, headline's title will not be
720 parsed as a secondary string, but as a plain string instead.
722 Assume point is at beginning of the headline."
724 (let* ((components (org-heading-components))
725 (level (nth 1 components
))
726 (todo (nth 2 components
))
728 (and todo
(if (member todo org-done-keywords
) 'done
'todo
)))
729 (tags (let ((raw-tags (nth 5 components
)))
730 (and raw-tags
(org-split-string raw-tags
":"))))
731 (raw-value (or (nth 4 components
) ""))
733 (let ((case-fold-search nil
))
734 (string-match (format "^%s\\( \\|$\\)" org-quote-string
)
737 (let ((case-fold-search nil
))
738 (string-match (format "^%s\\( \\|$\\)" org-comment-string
)
740 (archivedp (member org-archive-tag tags
))
741 (footnote-section-p (and org-footnote-section
742 (string= org-footnote-section raw-value
)))
743 ;; Normalize property names: ":SOME_PROP:" becomes
745 (standard-props (let (plist)
748 (let ((p-name (downcase (car p
))))
749 (while (string-match "_" p-name
)
751 (replace-match "-" nil nil p-name
)))
752 (setq p-name
(intern (concat ":" p-name
)))
754 (plist-put plist p-name
(cdr p
)))))
755 (org-entry-properties nil
'standard
))
757 (time-props (org-entry-properties nil
'special
"CLOCK"))
758 (scheduled (cdr (assoc "SCHEDULED" time-props
)))
759 (deadline (cdr (assoc "DEADLINE" time-props
)))
760 (clock (cdr (assoc "CLOCK" time-props
)))
761 (timestamp (cdr (assoc "TIMESTAMP" time-props
)))
763 (end (save-excursion (goto-char (org-end-of-subtree t t
))))
764 (pos-after-head (progn (forward-line) (point)))
765 (contents-begin (save-excursion
766 (skip-chars-forward " \r\t\n" end
)
767 (and (/= (point) end
) (line-beginning-position))))
768 (hidden (org-invisible-p2))
769 (contents-end (and contents-begin
770 (progn (goto-char end
)
771 (skip-chars-backward " \r\t\n")
774 ;; Clean RAW-VALUE from any quote or comment string.
775 (when (or quotedp commentedp
)
776 (let ((case-fold-search nil
))
778 (replace-regexp-in-string
780 (regexp-opt (list org-quote-string org-comment-string
))
784 ;; Clean TAGS from archive tag, if any.
785 (when archivedp
(setq tags
(delete org-archive-tag tags
)))
789 (list :raw-value raw-value
793 (if (not contents-begin
) 0
794 (count-lines pos-after-head contents-begin
))
796 :contents-begin contents-begin
797 :contents-end contents-end
799 :priority
(nth 3 components
)
807 :post-blank
(count-lines
808 (if (not contents-end
) pos-after-head
809 (goto-char contents-end
)
813 :footnote-section-p footnote-section-p
815 :commentedp commentedp
818 (org-element-put-property
820 (if raw-secondary-p raw-value
821 (org-element-parse-secondary-string
822 raw-value
(org-element-restriction 'headline
) headline
)))))))
824 (defun org-element-headline-interpreter (headline contents
)
825 "Interpret HEADLINE element as Org syntax.
826 CONTENTS is the contents of the element."
827 (let* ((level (org-element-property :level headline
))
828 (todo (org-element-property :todo-keyword headline
))
829 (priority (org-element-property :priority headline
))
830 (title (org-element-interpret-data
831 (org-element-property :title headline
)))
832 (tags (let ((tag-list (if (org-element-property :archivedp headline
)
833 (cons org-archive-tag
834 (org-element-property :tags headline
))
835 (org-element-property :tags headline
))))
837 (format ":%s:" (mapconcat 'identity tag-list
":")))))
838 (commentedp (org-element-property :commentedp headline
))
839 (quotedp (org-element-property :quotedp headline
))
840 (pre-blank (or (org-element-property :pre-blank headline
) 0))
841 (heading (concat (make-string level ?
*)
842 (and todo
(concat " " todo
))
843 (and quotedp
(concat " " org-quote-string
))
844 (and commentedp
(concat " " org-comment-string
))
846 (format " [#%s]" (char-to-string priority
)))
847 (cond ((and org-footnote-section
848 (org-element-property
849 :footnote-section-p headline
))
850 (concat " " org-footnote-section
))
851 (title (concat " " title
))))))
856 ((zerop org-tags-column
) (format " %s" tags
))
857 ((< org-tags-column
0)
860 (max (- (+ org-tags-column
(length heading
) (length tags
))) 1)
865 (make-string (max (- org-tags-column
(length heading
)) 1) ?
)
867 (make-string (1+ pre-blank
) 10)
873 (defun org-element-inlinetask-parser (limit &optional raw-secondary-p
)
874 "Parse an inline task.
876 Return a list whose CAR is `inlinetask' and CDR is a plist
877 containing `:title', `:begin', `:end', `:hiddenp',
878 `:contents-begin' and `:contents-end', `:level', `:priority',
879 `:raw-value', `:tags', `:todo-keyword', `:todo-type',
880 `:scheduled', `:deadline', `:timestamp', `:clock' and
881 `:post-blank' keywords.
883 The plist also contains any property set in the property drawer,
884 with its name in lowercase, the underscores replaced with hyphens
885 and colons at the beginning (i.e. `:custom-id').
887 When optional argument RAW-SECONDARY-P is non-nil, inline-task's
888 title will not be parsed as a secondary string, but as a plain
891 Assume point is at beginning of the inline task."
893 (let* ((keywords (org-element--collect-affiliated-keywords))
894 (begin (car keywords
))
895 (components (org-heading-components))
896 (todo (nth 2 components
))
898 (if (member todo org-done-keywords
) 'done
'todo
)))
899 (tags (let ((raw-tags (nth 5 components
)))
900 (and raw-tags
(org-split-string raw-tags
":"))))
901 (raw-value (or (nth 4 components
) ""))
902 ;; Normalize property names: ":SOME_PROP:" becomes
904 (standard-props (let (plist)
907 (let ((p-name (downcase (car p
))))
908 (while (string-match "_" p-name
)
910 (replace-match "-" nil nil p-name
)))
911 (setq p-name
(intern (concat ":" p-name
)))
913 (plist-put plist p-name
(cdr p
)))))
914 (org-entry-properties nil
'standard
))
916 (time-props (org-entry-properties nil
'special
"CLOCK"))
917 (scheduled (cdr (assoc "SCHEDULED" time-props
)))
918 (deadline (cdr (assoc "DEADLINE" time-props
)))
919 (clock (cdr (assoc "CLOCK" time-props
)))
920 (timestamp (cdr (assoc "TIMESTAMP" time-props
)))
921 (task-end (save-excursion
923 (and (re-search-forward "^\\*+ END" limit t
)
924 (match-beginning 0))))
925 (contents-begin (progn (forward-line)
926 (and task-end
(< (point) task-end
) (point))))
927 (hidden (and contents-begin
(org-invisible-p2)))
928 (contents-end (and contents-begin task-end
))
929 (before-blank (if (not task-end
) (point)
933 (end (progn (skip-chars-forward " \r\t\n" limit
)
934 (skip-chars-backward " \t")
935 (if (bolp) (point) (line-end-position))))
939 (list :raw-value raw-value
943 :contents-begin contents-begin
944 :contents-end contents-end
945 :level
(nth 1 components
)
946 :priority
(nth 3 components
)
954 :post-blank
(count-lines before-blank end
))
957 (org-element-put-property
959 (if raw-secondary-p raw-value
960 (org-element-parse-secondary-string
962 (org-element-restriction 'inlinetask
)
965 (defun org-element-inlinetask-interpreter (inlinetask contents
)
966 "Interpret INLINETASK element as Org syntax.
967 CONTENTS is the contents of inlinetask."
968 (let* ((level (org-element-property :level inlinetask
))
969 (todo (org-element-property :todo-keyword inlinetask
))
970 (priority (org-element-property :priority inlinetask
))
971 (title (org-element-interpret-data
972 (org-element-property :title inlinetask
)))
973 (tags (let ((tag-list (org-element-property :tags inlinetask
)))
975 (format ":%s:" (mapconcat 'identity tag-list
":")))))
976 (task (concat (make-string level ?
*)
977 (and todo
(concat " " todo
))
979 (format " [#%s]" (char-to-string priority
)))
980 (and title
(concat " " title
)))))
985 ((zerop org-tags-column
) (format " %s" tags
))
986 ((< org-tags-column
0)
989 (max (- (+ org-tags-column
(length task
) (length tags
))) 1)
994 (make-string (max (- org-tags-column
(length task
)) 1) ?
)
996 ;; Prefer degenerate inlinetasks when there are no
1001 (make-string level ?
*) " END")))))
1006 (defun org-element-item-parser (limit struct
&optional raw-secondary-p
)
1009 STRUCT is the structure of the plain list.
1011 Return a list whose CAR is `item' and CDR is a plist containing
1012 `:bullet', `:begin', `:end', `:contents-begin', `:contents-end',
1013 `:checkbox', `:counter', `:tag', `:structure', `:hiddenp' and
1014 `:post-blank' keywords.
1016 When optional argument RAW-SECONDARY-P is non-nil, item's tag, if
1017 any, will not be parsed as a secondary string, but as a plain
1020 Assume point is at the beginning of the item."
1023 (looking-at org-list-full-item-re
)
1024 (let* ((begin (point))
1025 (bullet (org-match-string-no-properties 1))
1026 (checkbox (let ((box (org-match-string-no-properties 3)))
1027 (cond ((equal "[ ]" box
) 'off
)
1028 ((equal "[X]" box
) 'on
)
1029 ((equal "[-]" box
) 'trans
))))
1030 (counter (let ((c (org-match-string-no-properties 2)))
1034 ((string-match "[A-Za-z]" c
)
1035 (- (string-to-char (upcase (match-string 0 c
)))
1037 ((string-match "[0-9]+" c
)
1038 (string-to-number (match-string 0 c
)))))))
1039 (end (save-excursion (goto-char (org-list-get-item-end begin struct
))
1040 (unless (bolp) (forward-line))
1044 ;; Ignore tags in un-ordered lists: they are just
1045 ;; a part of item's body.
1046 (if (and (match-beginning 4)
1047 (save-match-data (string-match "[.)]" bullet
)))
1050 (skip-chars-forward " \r\t\n" limit
)
1051 ;; If first line isn't empty, contents really start
1052 ;; at the text after item's meta-data.
1053 (if (= (point-at-bol) begin
) (point) (point-at-bol))))
1054 (hidden (progn (forward-line)
1055 (and (not (= (point) end
)) (org-invisible-p2))))
1056 (contents-end (progn (goto-char end
)
1057 (skip-chars-backward " \r\t\n")
1062 (list :bullet bullet
1065 ;; CONTENTS-BEGIN and CONTENTS-END may be
1066 ;; mixed up in the case of an empty item
1067 ;; separated from the next by a blank line.
1068 ;; Thus ensure the former is always the
1070 :contents-begin
(min contents-begin contents-end
)
1071 :contents-end
(max contents-begin contents-end
)
1076 :post-blank
(count-lines contents-end end
)))))
1077 (org-element-put-property
1079 (let ((raw-tag (org-list-get-tag begin struct
)))
1081 (if raw-secondary-p raw-tag
1082 (org-element-parse-secondary-string
1083 raw-tag
(org-element-restriction 'item
) item
))))))))
1085 (defun org-element-item-interpreter (item contents
)
1086 "Interpret ITEM element as Org syntax.
1087 CONTENTS is the contents of the element."
1088 (let* ((bullet (org-list-bullet-string (org-element-property :bullet item
)))
1089 (checkbox (org-element-property :checkbox item
))
1090 (counter (org-element-property :counter item
))
1091 (tag (let ((tag (org-element-property :tag item
)))
1092 (and tag
(org-element-interpret-data tag
))))
1093 ;; Compute indentation.
1094 (ind (make-string (length bullet
) 32))
1095 (item-starts-with-par-p
1096 (eq (org-element-type (car (org-element-contents item
)))
1101 (and counter
(format "[@%d] " counter
))
1106 (and tag
(format "%s :: " tag
))
1107 (let ((contents (replace-regexp-in-string
1108 "\\(^\\)[ \t]*\\S-" ind contents nil nil
1)))
1109 (if item-starts-with-par-p
(org-trim contents
)
1110 (concat "\n" contents
))))))
1115 (defun org-element-plain-list-parser (limit affiliated structure
)
1116 "Parse a plain list.
1118 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1119 the buffer position at the beginning of the first affiliated
1120 keyword and CDR is a plist of affiliated keywords along with
1121 their value. STRUCTURE is the structure of the plain list being
1124 Return a list whose CAR is `plain-list' and CDR is a plist
1125 containing `:type', `:begin', `:end', `:contents-begin' and
1126 `:contents-end', `:structure' and `:post-blank' keywords.
1128 Assume point is at the beginning of the list."
1130 (let* ((struct (or structure
(org-list-struct)))
1131 (prevs (org-list-prevs-alist struct
))
1132 (parents (org-list-parents-alist struct
))
1133 (type (org-list-get-list-type (point) struct prevs
))
1134 (contents-begin (point))
1135 (begin (car affiliated
))
1137 (progn (goto-char (org-list-get-list-end (point) struct prevs
))
1138 (unless (bolp) (forward-line))
1140 (end (progn (skip-chars-forward " \r\t\n" limit
)
1141 (skip-chars-backward " \t")
1142 (if (bolp) (point) (line-end-position)))))
1149 :contents-begin contents-begin
1150 :contents-end contents-end
1152 :post-blank
(count-lines contents-end end
))
1153 (cdr affiliated
))))))
1155 (defun org-element-plain-list-interpreter (plain-list contents
)
1156 "Interpret PLAIN-LIST element as Org syntax.
1157 CONTENTS is the contents of the element."
1160 (goto-char (point-min))
1165 ;;;; Property Drawer
1167 (defun org-element-property-drawer-parser (limit affiliated
)
1168 "Parse a property drawer.
1170 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1171 the buffer position at the beginning of the first affiliated
1172 keyword and CDR is a plist of affiliated keywords along with
1175 Return a list whose CAR is `property-drawer' and CDR is a plist
1176 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1177 `:contents-end' and `:post-blank' keywords.
1179 Assume point is at the beginning of the property drawer."
1181 (let ((case-fold-search t
))
1182 (if (not (save-excursion
1183 (re-search-forward "^[ \t]*:END:[ \t]*$" limit t
)))
1184 ;; Incomplete drawer: parse it as a paragraph.
1185 (org-element-paragraph-parser limit affiliated
)
1187 (let* ((drawer-end-line (match-beginning 0))
1188 (begin (car affiliated
))
1189 (contents-begin (progn (forward-line)
1190 (and (< (point) drawer-end-line
)
1192 (contents-end (and contents-begin drawer-end-line
))
1193 (hidden (org-invisible-p2))
1194 (pos-before-blank (progn (goto-char drawer-end-line
)
1197 (end (progn (skip-chars-forward " \r\t\n" limit
)
1198 (skip-chars-backward " \t")
1199 (if (bolp) (point) (line-end-position)))))
1200 (list 'property-drawer
1205 :contents-begin contents-begin
1206 :contents-end contents-end
1207 :post-blank
(count-lines pos-before-blank end
))
1208 (cdr affiliated
)))))))))
1210 (defun org-element-property-drawer-interpreter (property-drawer contents
)
1211 "Interpret PROPERTY-DRAWER element as Org syntax.
1212 CONTENTS is the properties within the drawer."
1213 (format ":PROPERTIES:\n%s:END:" contents
))
1218 (defun org-element-quote-block-parser (limit affiliated
)
1219 "Parse a quote block.
1221 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1222 the buffer position at the beginning of the first affiliated
1223 keyword and CDR is a plist of affiliated keywords along with
1226 Return a list whose CAR is `quote-block' and CDR is a plist
1227 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1228 `:contents-end' and `:post-blank' keywords.
1230 Assume point is at the beginning of the block."
1231 (let ((case-fold-search t
))
1232 (if (not (save-excursion
1233 (re-search-forward "^[ \t]*#\\+END_QUOTE[ \t]*$" limit t
)))
1234 ;; Incomplete block: parse it as a paragraph.
1235 (org-element-paragraph-parser limit affiliated
)
1236 (let ((block-end-line (match-beginning 0)))
1238 (let* ((begin (car affiliated
))
1239 ;; Empty blocks have no contents.
1240 (contents-begin (progn (forward-line)
1241 (and (< (point) block-end-line
)
1243 (contents-end (and contents-begin block-end-line
))
1244 (hidden (org-invisible-p2))
1245 (pos-before-blank (progn (goto-char block-end-line
)
1248 (end (progn (skip-chars-forward " \r\t\n" limit
)
1249 (skip-chars-backward " \t")
1250 (if (bolp) (point) (line-end-position)))))
1256 :contents-begin contents-begin
1257 :contents-end contents-end
1258 :post-blank
(count-lines pos-before-blank end
))
1259 (cdr affiliated
)))))))))
1261 (defun org-element-quote-block-interpreter (quote-block contents
)
1262 "Interpret QUOTE-BLOCK element as Org syntax.
1263 CONTENTS is the contents of the element."
1264 (format "#+BEGIN_QUOTE\n%s#+END_QUOTE" contents
))
1269 (defun org-element-section-parser (limit)
1272 LIMIT bounds the search.
1274 Return a list whose CAR is `section' and CDR is a plist
1275 containing `:begin', `:end', `:contents-begin', `contents-end'
1276 and `:post-blank' keywords."
1278 ;; Beginning of section is the beginning of the first non-blank
1279 ;; line after previous headline.
1280 (let ((begin (point))
1281 (end (progn (org-with-limited-levels (outline-next-heading))
1283 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
1289 :contents-begin begin
1290 :contents-end pos-before-blank
1291 :post-blank
(count-lines pos-before-blank end
))))))
1293 (defun org-element-section-interpreter (section contents
)
1294 "Interpret SECTION element as Org syntax.
1295 CONTENTS is the contents of the element."
1301 (defun org-element-special-block-parser (limit affiliated
)
1302 "Parse a special block.
1304 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1305 the buffer position at the beginning of the first affiliated
1306 keyword and CDR is a plist of affiliated keywords along with
1309 Return a list whose CAR is `special-block' and CDR is a plist
1310 containing `:type', `:begin', `:end', `:hiddenp',
1311 `:contents-begin', `:contents-end' and `:post-blank' keywords.
1313 Assume point is at the beginning of the block."
1314 (let* ((case-fold-search t
)
1315 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(S-+\\)")
1316 (upcase (match-string-no-properties 1)))))
1317 (if (not (save-excursion
1319 (format "^[ \t]*#\\+END_%s[ \t]*$" type
) limit t
)))
1320 ;; Incomplete block: parse it as a paragraph.
1321 (org-element-paragraph-parser limit affiliated
)
1322 (let ((block-end-line (match-beginning 0)))
1324 (let* ((begin (car affiliated
))
1325 ;; Empty blocks have no contents.
1326 (contents-begin (progn (forward-line)
1327 (and (< (point) block-end-line
)
1329 (contents-end (and contents-begin block-end-line
))
1330 (hidden (org-invisible-p2))
1331 (pos-before-blank (progn (goto-char block-end-line
)
1334 (end (progn (skip-chars-forward " \r\t\n" limit
)
1335 (skip-chars-backward " \t")
1336 (if (bolp) (point) (line-end-position)))))
1337 (list 'special-block
1343 :contents-begin contents-begin
1344 :contents-end contents-end
1345 :post-blank
(count-lines pos-before-blank end
))
1346 (cdr affiliated
)))))))))
1348 (defun org-element-special-block-interpreter (special-block contents
)
1349 "Interpret SPECIAL-BLOCK element as Org syntax.
1350 CONTENTS is the contents of the element."
1351 (let ((block-type (org-element-property :type special-block
)))
1352 (format "#+BEGIN_%s\n%s#+END_%s" block-type contents block-type
)))
1358 ;; For each element, a parser and an interpreter are also defined.
1359 ;; Both follow the same naming convention used for greater elements.
1361 ;; Also, as for greater elements, adding a new element type is done
1362 ;; through the following steps: implement a parser and an interpreter,
1363 ;; tweak `org-element--current-element' so that it recognizes the new
1364 ;; type and add that new type to `org-element-all-elements'.
1366 ;; As a special case, when the newly defined type is a block type,
1367 ;; `org-element-block-name-alist' has to be modified accordingly.
1372 (defun org-element-babel-call-parser (limit affiliated
)
1373 "Parse a babel call.
1375 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1376 the buffer position at the beginning of the first affiliated
1377 keyword and CDR is a plist of affiliated keywords along with
1380 Return a list whose CAR is `babel-call' and CDR is a plist
1381 containing `:begin', `:end', `:info' and `:post-blank' as
1384 (let ((case-fold-search t
)
1385 (info (progn (looking-at org-babel-block-lob-one-liner-regexp
)
1386 (org-babel-lob-get-info)))
1387 (begin (car affiliated
))
1388 (pos-before-blank (progn (forward-line) (point)))
1389 (end (progn (skip-chars-forward " \r\t\n" limit
)
1390 (skip-chars-backward " \t")
1391 (if (bolp) (point) (line-end-position)))))
1397 :post-blank
(count-lines pos-before-blank end
))
1398 (cdr affiliated
))))))
1400 (defun org-element-babel-call-interpreter (babel-call contents
)
1401 "Interpret BABEL-CALL element as Org syntax.
1403 (let* ((babel-info (org-element-property :info babel-call
))
1404 (main (car babel-info
))
1405 (post-options (nth 1 babel-info
)))
1407 (if (not (string-match "\\[\\(\\[.*?\\]\\)\\]" main
)) main
1408 ;; Remove redundant square brackets.
1409 (replace-match (match-string 1 main
) nil nil main
))
1410 (and post-options
(format "[%s]" post-options
)))))
1415 (defun org-element-clock-parser (limit)
1418 LIMIT bounds the search.
1420 Return a list whose CAR is `clock' and CDR is a plist containing
1421 `:status', `:value', `:time', `:begin', `:end' and `:post-blank'
1424 (let* ((case-fold-search nil
)
1426 (value (progn (search-forward org-clock-string
(line-end-position) t
)
1427 (org-skip-whitespace)
1428 (looking-at "\\[.*\\]")
1429 (org-match-string-no-properties 0)))
1430 (time (and (progn (goto-char (match-end 0))
1431 (looking-at " +=> +\\(\\S-+\\)[ \t]*$"))
1432 (org-match-string-no-properties 1)))
1433 (status (if time
'closed
'running
))
1434 (post-blank (let ((before-blank (progn (forward-line) (point))))
1435 (skip-chars-forward " \r\t\n" limit
)
1436 (skip-chars-backward " \t")
1437 (unless (bolp) (end-of-line))
1438 (count-lines before-blank
(point))))
1441 (list :status status
1446 :post-blank post-blank
)))))
1448 (defun org-element-clock-interpreter (clock contents
)
1449 "Interpret CLOCK element as Org syntax.
1451 (concat org-clock-string
" "
1452 (org-element-property :value clock
)
1453 (let ((time (org-element-property :time clock
)))
1458 (org-split-string time
":")))))))
1463 (defun org-element-comment-parser (limit affiliated
)
1466 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1467 the buffer position at the beginning of the first affiliated
1468 keyword and CDR is a plist of affiliated keywords along with
1471 Return a list whose CAR is `comment' and CDR is a plist
1472 containing `:begin', `:end', `:value' and `:post-blank'
1475 Assume point is at comment beginning."
1477 (let* ((begin (car affiliated
))
1478 (value (prog2 (looking-at "[ \t]*# ?")
1479 (buffer-substring-no-properties
1480 (match-end 0) (line-end-position))
1483 ;; Get comments ending.
1485 (while (and (< (point) limit
) (looking-at "[ \t]*#\\( \\|$\\)"))
1486 ;; Accumulate lines without leading hash and first
1491 (buffer-substring-no-properties
1492 (match-end 0) (line-end-position))))
1495 (end (progn (goto-char com-end
)
1496 (skip-chars-forward " \r\t\n" limit
)
1497 (skip-chars-backward " \t")
1498 (if (bolp) (point) (line-end-position)))))
1504 :post-blank
(count-lines com-end end
))
1505 (cdr affiliated
))))))
1507 (defun org-element-comment-interpreter (comment contents
)
1508 "Interpret COMMENT element as Org syntax.
1510 (replace-regexp-in-string "^" "# " (org-element-property :value comment
)))
1515 (defun org-element-comment-block-parser (limit affiliated
)
1516 "Parse an export block.
1518 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1519 the buffer position at the beginning of the first affiliated
1520 keyword and CDR is a plist of affiliated keywords along with
1523 Return a list whose CAR is `comment-block' and CDR is a plist
1524 containing `:begin', `:end', `:hiddenp', `:value' and
1525 `:post-blank' keywords.
1527 Assume point is at comment block beginning."
1528 (let ((case-fold-search t
))
1529 (if (not (save-excursion
1530 (re-search-forward "^[ \t]*#\\+END_COMMENT[ \t]*$" limit t
)))
1531 ;; Incomplete block: parse it as a paragraph.
1532 (org-element-paragraph-parser limit affiliated
)
1533 (let ((contents-end (match-beginning 0)))
1535 (let* ((begin (car affiliated
))
1536 (contents-begin (progn (forward-line) (point)))
1537 (hidden (org-invisible-p2))
1538 (pos-before-blank (progn (goto-char contents-end
)
1541 (end (progn (skip-chars-forward " \r\t\n" limit
)
1542 (skip-chars-backward " \t")
1543 (if (bolp) (point) (line-end-position))))
1544 (value (buffer-substring-no-properties
1545 contents-begin contents-end
)))
1546 (list 'comment-block
1552 :post-blank
(count-lines pos-before-blank end
))
1553 (cdr affiliated
)))))))))
1555 (defun org-element-comment-block-interpreter (comment-block contents
)
1556 "Interpret COMMENT-BLOCK element as Org syntax.
1558 (format "#+BEGIN_COMMENT\n%s#+END_COMMENT"
1559 (org-remove-indentation (org-element-property :value comment-block
))))
1564 (defun org-element-example-block-parser (limit affiliated
)
1565 "Parse an example block.
1567 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1568 the buffer position at the beginning of the first affiliated
1569 keyword and CDR is a plist of affiliated keywords along with
1572 Return a list whose CAR is `example-block' and CDR is a plist
1573 containing `:begin', `:end', `:number-lines', `:preserve-indent',
1574 `:retain-labels', `:use-labels', `:label-fmt', `:hiddenp',
1575 `:switches', `:value' and `:post-blank' keywords."
1576 (let ((case-fold-search t
))
1577 (if (not (save-excursion
1578 (re-search-forward "^[ \t]*#\\+END_EXAMPLE[ \t]*$" limit t
)))
1579 ;; Incomplete block: parse it as a paragraph.
1580 (org-element-paragraph-parser limit affiliated
)
1581 (let ((contents-end (match-beginning 0)))
1584 (progn (looking-at "^[ \t]*#\\+BEGIN_EXAMPLE\\(?: +\\(.*\\)\\)?")
1585 (org-match-string-no-properties 1)))
1586 ;; Switches analysis
1587 (number-lines (cond ((not switches
) nil
)
1588 ((string-match "-n\\>" switches
) 'new
)
1589 ((string-match "+n\\>" switches
) 'continued
)))
1590 (preserve-indent (and switches
(string-match "-i\\>" switches
)))
1591 ;; Should labels be retained in (or stripped from) example
1595 (not (string-match "-r\\>" switches
))
1596 (and number-lines
(string-match "-k\\>" switches
))))
1597 ;; What should code-references use - labels or
1601 (and retain-labels
(not (string-match "-k\\>" switches
)))))
1602 (label-fmt (and switches
1603 (string-match "-l +\"\\([^\"\n]+\\)\"" switches
)
1604 (match-string 1 switches
)))
1605 ;; Standard block parsing.
1606 (begin (car affiliated
))
1607 (contents-begin (progn (forward-line) (point)))
1608 (hidden (org-invisible-p2))
1609 (value (org-unescape-code-in-string
1610 (buffer-substring-no-properties
1611 contents-begin contents-end
)))
1612 (pos-before-blank (progn (goto-char contents-end
)
1615 (end (progn (skip-chars-forward " \r\t\n" limit
)
1616 (skip-chars-backward " \t")
1617 (if (bolp) (point) (line-end-position)))))
1618 (list 'example-block
1624 :number-lines number-lines
1625 :preserve-indent preserve-indent
1626 :retain-labels retain-labels
1627 :use-labels use-labels
1628 :label-fmt label-fmt
1630 :post-blank
(count-lines pos-before-blank end
))
1631 (cdr affiliated
)))))))))
1633 (defun org-element-example-block-interpreter (example-block contents
)
1634 "Interpret EXAMPLE-BLOCK element as Org syntax.
1636 (let ((switches (org-element-property :switches example-block
)))
1637 (concat "#+BEGIN_EXAMPLE" (and switches
(concat " " switches
)) "\n"
1638 (org-remove-indentation
1639 (org-escape-code-in-string
1640 (org-element-property :value example-block
)))
1646 (defun org-element-export-block-parser (limit affiliated
)
1647 "Parse an export block.
1649 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1650 the buffer position at the beginning of the first affiliated
1651 keyword and CDR is a plist of affiliated keywords along with
1654 Return a list whose CAR is `export-block' and CDR is a plist
1655 containing `:begin', `:end', `:type', `:hiddenp', `:value' and
1656 `:post-blank' keywords.
1658 Assume point is at export-block beginning."
1659 (let* ((case-fold-search t
)
1660 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
1661 (upcase (org-match-string-no-properties 1)))))
1662 (if (not (save-excursion
1664 (format "^[ \t]*#\\+END_%s[ \t]*$" type
) limit t
)))
1665 ;; Incomplete block: parse it as a paragraph.
1666 (org-element-paragraph-parser limit affiliated
)
1667 (let ((contents-end (match-beginning 0)))
1669 (let* ((begin (car affiliated
))
1670 (contents-begin (progn (forward-line) (point)))
1671 (hidden (org-invisible-p2))
1672 (pos-before-blank (progn (goto-char contents-end
)
1675 (end (progn (skip-chars-forward " \r\t\n" limit
)
1676 (skip-chars-backward " \t")
1677 (if (bolp) (point) (line-end-position))))
1678 (value (buffer-substring-no-properties contents-begin
1687 :post-blank
(count-lines pos-before-blank end
))
1688 (cdr affiliated
)))))))))
1690 (defun org-element-export-block-interpreter (export-block contents
)
1691 "Interpret EXPORT-BLOCK element as Org syntax.
1693 (let ((type (org-element-property :type export-block
)))
1694 (concat (format "#+BEGIN_%s\n" type
)
1695 (org-element-property :value export-block
)
1696 (format "#+END_%s" type
))))
1701 (defun org-element-fixed-width-parser (limit affiliated
)
1702 "Parse a fixed-width section.
1704 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1705 the buffer position at the beginning of the first affiliated
1706 keyword and CDR is a plist of affiliated keywords along with
1709 Return a list whose CAR is `fixed-width' and CDR is a plist
1710 containing `:begin', `:end', `:value' and `:post-blank' keywords.
1712 Assume point is at the beginning of the fixed-width area."
1714 (let* ((begin (car affiliated
))
1718 (while (and (< (point) limit
)
1719 (looking-at "[ \t]*:\\( \\|$\\)"))
1720 ;; Accumulate text without starting colons.
1723 (buffer-substring-no-properties
1724 (match-end 0) (point-at-eol))
1728 (end (progn (skip-chars-forward " \r\t\n" limit
)
1729 (skip-chars-backward " \t")
1730 (if (bolp) (point) (line-end-position)))))
1736 :post-blank
(count-lines end-area end
))
1737 (cdr affiliated
))))))
1739 (defun org-element-fixed-width-interpreter (fixed-width contents
)
1740 "Interpret FIXED-WIDTH element as Org syntax.
1742 (replace-regexp-in-string
1743 "^" ": " (substring (org-element-property :value fixed-width
) 0 -
1)))
1746 ;;;; Horizontal Rule
1748 (defun org-element-horizontal-rule-parser (limit affiliated
)
1749 "Parse an horizontal rule.
1751 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1752 the buffer position at the beginning of the first affiliated
1753 keyword and CDR is a plist of affiliated keywords along with
1756 Return a list whose CAR is `horizontal-rule' and CDR is a plist
1757 containing `:begin', `:end' and `:post-blank' keywords."
1759 (let ((begin (car affiliated
))
1760 (post-hr (progn (forward-line) (point)))
1761 (end (progn (skip-chars-forward " \r\t\n" limit
)
1762 (skip-chars-backward " \t")
1763 (if (bolp) (point) (line-end-position)))))
1764 (list 'horizontal-rule
1768 :post-blank
(count-lines post-hr end
))
1769 (cdr affiliated
))))))
1771 (defun org-element-horizontal-rule-interpreter (horizontal-rule contents
)
1772 "Interpret HORIZONTAL-RULE element as Org syntax.
1779 (defun org-element-keyword-parser (limit affiliated
)
1780 "Parse a keyword at point.
1782 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1783 the buffer position at the beginning of the first affiliated
1784 keyword and CDR is a plist of affiliated keywords along with
1787 Return a list whose CAR is `keyword' and CDR is a plist
1788 containing `:key', `:value', `:begin', `:end' and `:post-blank'
1791 (let ((begin (car affiliated
))
1792 (key (progn (looking-at "[ \t]*#\\+\\(\\S-+*\\):")
1793 (upcase (org-match-string-no-properties 1))))
1794 (value (org-trim (buffer-substring-no-properties
1795 (match-end 0) (point-at-eol))))
1796 (pos-before-blank (progn (forward-line) (point)))
1797 (end (progn (skip-chars-forward " \r\t\n" limit
)
1798 (skip-chars-backward " \t")
1799 (if (bolp) (point) (line-end-position)))))
1806 :post-blank
(count-lines pos-before-blank end
))
1807 (cdr affiliated
))))))
1809 (defun org-element-keyword-interpreter (keyword contents
)
1810 "Interpret KEYWORD element as Org syntax.
1813 (org-element-property :key keyword
)
1814 (org-element-property :value keyword
)))
1817 ;;;; Latex Environment
1819 (defun org-element-latex-environment-parser (limit affiliated
)
1820 "Parse a LaTeX environment.
1822 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1823 the buffer position at the beginning of the first affiliated
1824 keyword and CDR is a plist of affiliated keywords along with
1827 Return a list whose CAR is `latex-environment' and CDR is a plist
1828 containing `:begin', `:end', `:value' and `:post-blank'
1831 Assume point is at the beginning of the latex environment."
1833 (let ((case-fold-search t
)
1834 (code-begin (point)))
1835 (looking-at "[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}")
1836 (if (not (re-search-forward (format "^[ \t]*\\\\end{%s}[ \t]*$"
1837 (regexp-quote (match-string 1)))
1839 ;; Incomplete latex environment: parse it as a paragraph.
1840 (org-element-paragraph-parser limit affiliated
)
1841 (let* ((code-end (progn (forward-line) (point)))
1842 (begin (car affiliated
))
1843 (value (buffer-substring-no-properties code-begin code-end
))
1844 (end (progn (skip-chars-forward " \r\t\n" limit
)
1845 (skip-chars-backward " \t")
1846 (if (bolp) (point) (line-end-position)))))
1847 (list 'latex-environment
1852 :post-blank
(count-lines code-end end
))
1853 (cdr affiliated
))))))))
1855 (defun org-element-latex-environment-interpreter (latex-environment contents
)
1856 "Interpret LATEX-ENVIRONMENT element as Org syntax.
1858 (org-element-property :value latex-environment
))
1863 (defun org-element-node-property-parser (limit)
1864 "Parse a node-property at point.
1866 LIMIT bounds the search.
1868 Return a list whose CAR is `node-property' and CDR is a plist
1869 containing `:key', `:value', `:begin', `:end' and `:post-blank'
1872 (let ((case-fold-search t
)
1874 (key (progn (looking-at "[ \t]*:\\(.*?\\):[ \t]+\\(.*?\\)[ \t]*$")
1875 (org-match-string-no-properties 1)))
1876 (value (org-match-string-no-properties 2))
1877 (pos-before-blank (progn (forward-line) (point)))
1878 (end (progn (skip-chars-forward " \r\t\n" limit
)
1879 (if (eobp) (point) (point-at-bol)))))
1880 (list 'node-property
1885 :post-blank
(count-lines pos-before-blank end
))))))
1887 (defun org-element-node-property-interpreter (node-property contents
)
1888 "Interpret NODE-PROPERTY element as Org syntax.
1890 (format org-property-format
1891 (format ":%s:" (org-element-property :key node-property
))
1892 (org-element-property :value node-property
)))
1897 (defun org-element-paragraph-parser (limit affiliated
)
1900 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1901 the buffer position at the beginning of the first affiliated
1902 keyword and CDR is a plist of affiliated keywords along with
1905 Return a list whose CAR is `paragraph' and CDR is a plist
1906 containing `:begin', `:end', `:contents-begin' and
1907 `:contents-end' and `:post-blank' keywords.
1909 Assume point is at the beginning of the paragraph."
1911 (let* ((begin (car affiliated
))
1912 (contents-begin (point))
1914 (let ((case-fold-search t
))
1916 (if (not (re-search-forward
1917 org-element-paragraph-separate limit
'm
))
1919 ;; A matching `org-element-paragraph-separate' is not
1920 ;; necessarily the end of the paragraph. In
1921 ;; particular, lines starting with # or : as a first
1922 ;; non-space character are ambiguous. We have check
1923 ;; if they are valid Org syntax (i.e. not an
1924 ;; incomplete keyword).
1928 ;; There's no ambiguity for other symbols or
1929 ;; empty lines: stop here.
1930 (looking-at "[ \t]*\\(?:[^:#]\\|$\\)")
1931 ;; Stop at valid fixed-width areas.
1932 (looking-at "[ \t]*:\\(?: \\|$\\)")
1934 (and (looking-at org-drawer-regexp
)
1937 "^[ \t]*:END:[ \t]*$" limit t
)))
1938 ;; Stop at valid comments.
1939 (looking-at "[ \t]*#\\(?: \\|$\\)")
1940 ;; Stop at valid dynamic blocks.
1941 (and (looking-at org-dblock-start-re
)
1944 "^[ \t]*#\\+END:?[ \t]*$" limit t
)))
1945 ;; Stop at valid blocks.
1947 "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
1950 (format "^[ \t]*#\\+END_%s[ \t]*$"
1953 ;; Stop at valid latex environments.
1955 "^[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}[ \t]*$")
1958 (format "^[ \t]*\\\\end{%s}[ \t]*$"
1961 ;; Stop at valid keywords.
1962 (looking-at "[ \t]*#\\+\\S-+:")
1963 ;; Skip everything else.
1967 (re-search-forward org-element-paragraph-separate
1969 (beginning-of-line)))
1970 (if (= (point) limit
) limit
1971 (goto-char (line-beginning-position)))))
1972 (contents-end (progn (skip-chars-backward " \r\t\n" contents-begin
)
1975 (end (progn (skip-chars-forward " \r\t\n" limit
)
1976 (skip-chars-backward " \t")
1977 (if (bolp) (point) (line-end-position)))))
1982 :contents-begin contents-begin
1983 :contents-end contents-end
1984 :post-blank
(count-lines before-blank end
))
1985 (cdr affiliated
))))))
1987 (defun org-element-paragraph-interpreter (paragraph contents
)
1988 "Interpret PARAGRAPH element as Org syntax.
1989 CONTENTS is the contents of the element."
1995 (defun org-element-planning-parser (limit)
1998 LIMIT bounds the search.
2000 Return a list whose CAR is `planning' and CDR is a plist
2001 containing `:closed', `:deadline', `:scheduled', `:begin', `:end'
2002 and `:post-blank' keywords."
2004 (let* ((case-fold-search nil
)
2006 (post-blank (let ((before-blank (progn (forward-line) (point))))
2007 (skip-chars-forward " \r\t\n" limit
)
2008 (skip-chars-backward " \t")
2009 (unless (bolp) (end-of-line))
2010 (count-lines before-blank
(point))))
2012 closed deadline scheduled
)
2014 (while (re-search-forward org-keyword-time-not-clock-regexp
2015 (line-end-position) t
)
2016 (goto-char (match-end 1))
2017 (org-skip-whitespace)
2018 (let ((time (buffer-substring-no-properties
2019 (1+ (point)) (1- (match-end 0))))
2020 (keyword (match-string 1)))
2021 (cond ((equal keyword org-closed-string
) (setq closed time
))
2022 ((equal keyword org-deadline-string
) (setq deadline time
))
2023 (t (setq scheduled time
)))))
2025 (list :closed closed
2027 :scheduled scheduled
2030 :post-blank post-blank
)))))
2032 (defun org-element-planning-interpreter (planning contents
)
2033 "Interpret PLANNING element as Org syntax.
2038 (list (let ((closed (org-element-property :closed planning
)))
2039 (when closed
(concat org-closed-string
" [" closed
"]")))
2040 (let ((deadline (org-element-property :deadline planning
)))
2041 (when deadline
(concat org-deadline-string
" <" deadline
">")))
2042 (let ((scheduled (org-element-property :scheduled planning
)))
2044 (concat org-scheduled-string
" <" scheduled
">")))))
2050 (defun org-element-quote-section-parser (limit)
2051 "Parse a quote section.
2053 LIMIT bounds the search.
2055 Return a list whose CAR is `quote-section' and CDR is a plist
2056 containing `:begin', `:end', `:value' and `:post-blank' keywords.
2058 Assume point is at beginning of the section."
2060 (let* ((begin (point))
2061 (end (progn (org-with-limited-levels (outline-next-heading))
2063 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
2066 (value (buffer-substring-no-properties begin pos-before-blank
)))
2067 (list 'quote-section
2071 :post-blank
(count-lines pos-before-blank end
))))))
2073 (defun org-element-quote-section-interpreter (quote-section contents
)
2074 "Interpret QUOTE-SECTION element as Org syntax.
2076 (org-element-property :value quote-section
))
2081 (defun org-element-src-block-parser (limit affiliated
)
2084 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2085 the buffer position at the beginning of the first affiliated
2086 keyword and CDR is a plist of affiliated keywords along with
2089 Return a list whose CAR is `src-block' and CDR is a plist
2090 containing `:language', `:switches', `:parameters', `:begin',
2091 `:end', `:hiddenp', `:number-lines', `:retain-labels',
2092 `:use-labels', `:label-fmt', `:preserve-indent', `:value' and
2093 `:post-blank' keywords.
2095 Assume point is at the beginning of the block."
2096 (let ((case-fold-search t
))
2097 (if (not (save-excursion (re-search-forward "^[ \t]*#\\+END_SRC[ \t]*$"
2099 ;; Incomplete block: parse it as a paragraph.
2100 (org-element-paragraph-parser limit affiliated
)
2101 (let ((contents-end (match-beginning 0)))
2103 (let* ((begin (car affiliated
))
2104 ;; Get language as a string.
2108 (concat "^[ \t]*#\\+BEGIN_SRC"
2109 "\\(?: +\\(\\S-+\\)\\)?"
2110 "\\(\\(?: +\\(?:-l \".*?\"\\|[-+][A-Za-z]\\)\\)+\\)?"
2112 (org-match-string-no-properties 1)))
2114 (switches (org-match-string-no-properties 2))
2116 (parameters (org-match-string-no-properties 3))
2117 ;; Switches analysis
2118 (number-lines (cond ((not switches
) nil
)
2119 ((string-match "-n\\>" switches
) 'new
)
2120 ((string-match "+n\\>" switches
) 'continued
)))
2121 (preserve-indent (and switches
(string-match "-i\\>" switches
)))
2122 (label-fmt (and switches
2123 (string-match "-l +\"\\([^\"\n]+\\)\"" switches
)
2124 (match-string 1 switches
)))
2125 ;; Should labels be retained in (or stripped from)
2129 (not (string-match "-r\\>" switches
))
2130 (and number-lines
(string-match "-k\\>" switches
))))
2131 ;; What should code-references use - labels or
2135 (and retain-labels
(not (string-match "-k\\>" switches
)))))
2136 ;; Get visibility status.
2137 (hidden (progn (forward-line) (org-invisible-p2)))
2139 (value (org-unescape-code-in-string
2140 (buffer-substring-no-properties (point) contents-end
)))
2141 (pos-before-blank (progn (goto-char contents-end
)
2144 ;; Get position after ending blank lines.
2145 (end (progn (skip-chars-forward " \r\t\n" limit
)
2146 (skip-chars-backward " \t")
2147 (if (bolp) (point) (line-end-position)))))
2150 (list :language language
2151 :switches
(and (org-string-nw-p switches
)
2152 (org-trim switches
))
2153 :parameters
(and (org-string-nw-p parameters
)
2154 (org-trim parameters
))
2157 :number-lines number-lines
2158 :preserve-indent preserve-indent
2159 :retain-labels retain-labels
2160 :use-labels use-labels
2161 :label-fmt label-fmt
2164 :post-blank
(count-lines pos-before-blank end
))
2165 (cdr affiliated
)))))))))
2167 (defun org-element-src-block-interpreter (src-block contents
)
2168 "Interpret SRC-BLOCK element as Org syntax.
2170 (let ((lang (org-element-property :language src-block
))
2171 (switches (org-element-property :switches src-block
))
2172 (params (org-element-property :parameters src-block
))
2173 (value (let ((val (org-element-property :value src-block
)))
2175 (org-src-preserve-indentation val
)
2176 ((zerop org-edit-src-content-indentation
)
2177 (org-remove-indentation val
))
2179 (let ((ind (make-string
2180 org-edit-src-content-indentation
32)))
2181 (replace-regexp-in-string
2182 "\\(^\\)[ \t]*\\S-" ind
2183 (org-remove-indentation val
) nil nil
1)))))))
2184 (concat (format "#+BEGIN_SRC%s\n"
2185 (concat (and lang
(concat " " lang
))
2186 (and switches
(concat " " switches
))
2187 (and params
(concat " " params
))))
2188 (org-escape-code-in-string value
)
2194 (defun org-element-table-parser (limit affiliated
)
2195 "Parse a table at point.
2197 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2198 the buffer position at the beginning of the first affiliated
2199 keyword and CDR is a plist of affiliated keywords along with
2202 Return a list whose CAR is `table' and CDR is a plist containing
2203 `:begin', `:end', `:tblfm', `:type', `:contents-begin',
2204 `:contents-end', `:value' and `:post-blank' keywords.
2206 Assume point is at the beginning of the table."
2208 (let* ((case-fold-search t
)
2209 (table-begin (point))
2210 (type (if (org-at-table.el-p
) 'table.el
'org
))
2211 (begin (car affiliated
))
2213 (if (re-search-forward org-table-any-border-regexp limit
'm
)
2214 (goto-char (match-beginning 0))
2217 (while (looking-at "[ \t]*#\\+TBLFM: +\\(.*\\)[ \t]*$")
2218 (push (org-match-string-no-properties 1) acc
)
2221 (pos-before-blank (point))
2222 (end (progn (skip-chars-forward " \r\t\n" limit
)
2223 (skip-chars-backward " \t")
2224 (if (bolp) (point) (line-end-position)))))
2231 ;; Only `org' tables have contents. `table.el' tables
2232 ;; use a `:value' property to store raw table as
2234 :contents-begin
(and (eq type
'org
) table-begin
)
2235 :contents-end
(and (eq type
'org
) table-end
)
2236 :value
(and (eq type
'table.el
)
2237 (buffer-substring-no-properties
2238 table-begin table-end
))
2239 :post-blank
(count-lines pos-before-blank end
))
2240 (cdr affiliated
))))))
2242 (defun org-element-table-interpreter (table contents
)
2243 "Interpret TABLE element as Org syntax.
2245 (if (eq (org-element-property :type table
) 'table.el
)
2246 (org-remove-indentation (org-element-property :value table
))
2247 (concat (with-temp-buffer (insert contents
)
2250 (mapconcat (lambda (fm) (concat "#+TBLFM: " fm
))
2251 (reverse (org-element-property :tblfm table
))
2257 (defun org-element-table-row-parser (limit)
2258 "Parse table row at point.
2260 LIMIT bounds the search.
2262 Return a list whose CAR is `table-row' and CDR is a plist
2263 containing `:begin', `:end', `:contents-begin', `:contents-end',
2264 `:type' and `:post-blank' keywords."
2266 (let* ((type (if (looking-at "^[ \t]*|-") 'rule
'standard
))
2268 ;; A table rule has no contents. In that case, ensure
2269 ;; CONTENTS-BEGIN matches CONTENTS-END.
2270 (contents-begin (and (eq type
'standard
)
2271 (search-forward "|")
2273 (contents-end (and (eq type
'standard
)
2276 (skip-chars-backward " \t")
2278 (end (progn (forward-line) (point))))
2283 :contents-begin contents-begin
2284 :contents-end contents-end
2287 (defun org-element-table-row-interpreter (table-row contents
)
2288 "Interpret TABLE-ROW element as Org syntax.
2289 CONTENTS is the contents of the table row."
2290 (if (eq (org-element-property :type table-row
) 'rule
) "|-"
2291 (concat "| " contents
)))
2296 (defun org-element-verse-block-parser (limit affiliated
)
2297 "Parse a verse block.
2299 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2300 the buffer position at the beginning of the first affiliated
2301 keyword and CDR is a plist of affiliated keywords along with
2304 Return a list whose CAR is `verse-block' and CDR is a plist
2305 containing `:begin', `:end', `:contents-begin', `:contents-end',
2306 `:hiddenp' and `:post-blank' keywords.
2308 Assume point is at beginning of the block."
2309 (let ((case-fold-search t
))
2310 (if (not (save-excursion
2311 (re-search-forward "^[ \t]*#\\+END_VERSE[ \t]*$" limit t
)))
2312 ;; Incomplete block: parse it as a paragraph.
2313 (org-element-paragraph-parser limit affiliated
)
2314 (let ((contents-end (match-beginning 0)))
2316 (let* ((begin (car affiliated
))
2317 (hidden (progn (forward-line) (org-invisible-p2)))
2318 (contents-begin (point))
2319 (pos-before-blank (progn (goto-char contents-end
)
2322 (end (progn (skip-chars-forward " \r\t\n" limit
)
2323 (skip-chars-backward " \t")
2324 (if (bolp) (point) (line-end-position)))))
2329 :contents-begin contents-begin
2330 :contents-end contents-end
2332 :post-blank
(count-lines pos-before-blank end
))
2333 (cdr affiliated
)))))))))
2335 (defun org-element-verse-block-interpreter (verse-block contents
)
2336 "Interpret VERSE-BLOCK element as Org syntax.
2337 CONTENTS is verse block contents."
2338 (format "#+BEGIN_VERSE\n%s#+END_VERSE" contents
))
2344 ;; Unlike to elements, interstices can be found between objects.
2345 ;; That's why, along with the parser, successor functions are provided
2346 ;; for each object. Some objects share the same successor (i.e. `code'
2347 ;; and `verbatim' objects).
2349 ;; A successor must accept a single argument bounding the search. It
2350 ;; will return either a cons cell whose CAR is the object's type, as
2351 ;; a symbol, and CDR the position of its next occurrence, or nil.
2353 ;; Successors follow the naming convention:
2354 ;; org-element-NAME-successor, where NAME is the name of the
2355 ;; successor, as defined in `org-element-all-successors'.
2357 ;; Some object types (i.e. `italic') are recursive. Restrictions on
2358 ;; object types they can contain will be specified in
2359 ;; `org-element-object-restrictions'.
2361 ;; Adding a new type of object is simple. Implement a successor,
2362 ;; a parser, and an interpreter for it, all following the naming
2363 ;; convention. Register type in `org-element-all-objects' and
2364 ;; successor in `org-element-all-successors'. Maybe tweak
2365 ;; restrictions about it, and that's it.
2370 (defun org-element-bold-parser ()
2371 "Parse bold object at point.
2373 Return a list whose CAR is `bold' and CDR is a plist with
2374 `:begin', `:end', `:contents-begin' and `:contents-end' and
2375 `:post-blank' keywords.
2377 Assume point is at the first star marker."
2379 (unless (bolp) (backward-char 1))
2380 (looking-at org-emph-re
)
2381 (let ((begin (match-beginning 2))
2382 (contents-begin (match-beginning 4))
2383 (contents-end (match-end 4))
2384 (post-blank (progn (goto-char (match-end 2))
2385 (skip-chars-forward " \t")))
2390 :contents-begin contents-begin
2391 :contents-end contents-end
2392 :post-blank post-blank
)))))
2394 (defun org-element-bold-interpreter (bold contents
)
2395 "Interpret BOLD object as Org syntax.
2396 CONTENTS is the contents of the object."
2397 (format "*%s*" contents
))
2399 (defun org-element-text-markup-successor (limit)
2400 "Search for the next text-markup object.
2402 LIMIT bounds the search.
2404 Return value is a cons cell whose CAR is a symbol among `bold',
2405 `italic', `underline', `strike-through', `code' and `verbatim'
2406 and CDR is beginning position."
2408 (unless (bolp) (backward-char))
2409 (when (re-search-forward org-emph-re limit t
)
2410 (let ((marker (match-string 3)))
2412 ((equal marker
"*") 'bold
)
2413 ((equal marker
"/") 'italic
)
2414 ((equal marker
"_") 'underline
)
2415 ((equal marker
"+") 'strike-through
)
2416 ((equal marker
"~") 'code
)
2417 ((equal marker
"=") 'verbatim
)
2418 (t (error "Unknown marker at %d" (match-beginning 3))))
2419 (match-beginning 2))))))
2424 (defun org-element-code-parser ()
2425 "Parse code object at point.
2427 Return a list whose CAR is `code' and CDR is a plist with
2428 `:value', `:begin', `:end' and `:post-blank' keywords.
2430 Assume point is at the first tilde marker."
2432 (unless (bolp) (backward-char 1))
2433 (looking-at org-emph-re
)
2434 (let ((begin (match-beginning 2))
2435 (value (org-match-string-no-properties 4))
2436 (post-blank (progn (goto-char (match-end 2))
2437 (skip-chars-forward " \t")))
2443 :post-blank post-blank
)))))
2445 (defun org-element-code-interpreter (code contents
)
2446 "Interpret CODE object as Org syntax.
2448 (format "~%s~" (org-element-property :value code
)))
2453 (defun org-element-entity-parser ()
2454 "Parse entity at point.
2456 Return a list whose CAR is `entity' and CDR a plist with
2457 `:begin', `:end', `:latex', `:latex-math-p', `:html', `:latin1',
2458 `:utf-8', `:ascii', `:use-brackets-p' and `:post-blank' as
2461 Assume point is at the beginning of the entity."
2463 (looking-at "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)")
2464 (let* ((value (org-entity-get (match-string 1)))
2465 (begin (match-beginning 0))
2466 (bracketsp (string= (match-string 2) "{}"))
2467 (post-blank (progn (goto-char (match-end 1))
2468 (when bracketsp
(forward-char 2))
2469 (skip-chars-forward " \t")))
2472 (list :name
(car value
)
2473 :latex
(nth 1 value
)
2474 :latex-math-p
(nth 2 value
)
2476 :ascii
(nth 4 value
)
2477 :latin1
(nth 5 value
)
2478 :utf-8
(nth 6 value
)
2481 :use-brackets-p bracketsp
2482 :post-blank post-blank
)))))
2484 (defun org-element-entity-interpreter (entity contents
)
2485 "Interpret ENTITY object as Org syntax.
2488 (org-element-property :name entity
)
2489 (when (org-element-property :use-brackets-p entity
) "{}")))
2491 (defun org-element-latex-or-entity-successor (limit)
2492 "Search for the next latex-fragment or entity object.
2494 LIMIT bounds the search.
2496 Return value is a cons cell whose CAR is `entity' or
2497 `latex-fragment' and CDR is beginning position."
2499 (unless (bolp) (backward-char))
2501 (remove "begin" (plist-get org-format-latex-options
:matchers
)))
2502 ;; ENTITY-RE matches both LaTeX commands and Org entities.
2504 "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))
2505 (when (re-search-forward
2506 (concat (mapconcat (lambda (e) (nth 1 (assoc e org-latex-regexps
)))
2510 (goto-char (match-beginning 0))
2511 (if (looking-at entity-re
)
2512 ;; Determine if it's a real entity or a LaTeX command.
2513 (cons (if (org-entity-get (match-string 1)) 'entity
'latex-fragment
)
2514 (match-beginning 0))
2515 ;; No entity nor command: point is at a LaTeX fragment.
2516 ;; Determine its type to get the correct beginning position.
2517 (cons 'latex-fragment
2520 (when (looking-at (nth 1 (assoc e org-latex-regexps
)))
2523 (nth 2 (assoc e org-latex-regexps
))))))
2530 (defun org-element-export-snippet-parser ()
2531 "Parse export snippet at point.
2533 Return a list whose CAR is `export-snippet' and CDR a plist with
2534 `:begin', `:end', `:back-end', `:value' and `:post-blank' as
2537 Assume point is at the beginning of the snippet."
2539 (re-search-forward "@@\\([-A-Za-z0-9]+\\):" nil t
)
2540 (let* ((begin (match-beginning 0))
2541 (back-end (org-match-string-no-properties 1))
2542 (value (buffer-substring-no-properties
2544 (progn (re-search-forward "@@" nil t
) (match-beginning 0))))
2545 (post-blank (skip-chars-forward " \t"))
2547 (list 'export-snippet
2548 (list :back-end back-end
2552 :post-blank post-blank
)))))
2554 (defun org-element-export-snippet-interpreter (export-snippet contents
)
2555 "Interpret EXPORT-SNIPPET object as Org syntax.
2558 (org-element-property :back-end export-snippet
)
2559 (org-element-property :value export-snippet
)))
2561 (defun org-element-export-snippet-successor (limit)
2562 "Search for the next export-snippet object.
2564 LIMIT bounds the search.
2566 Return value is a cons cell whose CAR is `export-snippet' and CDR
2567 its beginning position."
2570 (when (and (re-search-forward "@@[-A-Za-z0-9]+:" limit t
)
2571 (setq beg
(match-beginning 0))
2572 (search-forward "@@" limit t
))
2573 (cons 'export-snippet beg
)))))
2576 ;;;; Footnote Reference
2578 (defun org-element-footnote-reference-parser ()
2579 "Parse footnote reference at point.
2581 Return a list whose CAR is `footnote-reference' and CDR a plist
2582 with `:label', `:type', `:inline-definition', `:begin', `:end'
2583 and `:post-blank' as keywords."
2585 (looking-at org-footnote-re
)
2586 (let* ((begin (point))
2587 (label (or (org-match-string-no-properties 2)
2588 (org-match-string-no-properties 3)
2589 (and (match-string 1)
2590 (concat "fn:" (org-match-string-no-properties 1)))))
2591 (type (if (or (not label
) (match-string 1)) 'inline
'standard
))
2592 (inner-begin (match-end 0))
2596 (while (and (> count
0) (re-search-forward "[][]" nil t
))
2597 (if (equal (match-string 0) "[") (incf count
) (decf count
)))
2599 (post-blank (progn (goto-char (1+ inner-end
))
2600 (skip-chars-forward " \t")))
2603 (list 'footnote-reference
2608 :post-blank post-blank
))))
2609 (org-element-put-property
2610 footnote-reference
:inline-definition
2611 (and (eq type
'inline
)
2612 (org-element-parse-secondary-string
2613 (buffer-substring inner-begin inner-end
)
2614 (org-element-restriction 'footnote-reference
)
2615 footnote-reference
))))))
2617 (defun org-element-footnote-reference-interpreter (footnote-reference contents
)
2618 "Interpret FOOTNOTE-REFERENCE object as Org syntax.
2620 (let ((label (or (org-element-property :label footnote-reference
) "fn:"))
2623 (org-element-property :inline-definition footnote-reference
)))
2624 (if (not inline-def
) ""
2625 (concat ":" (org-element-interpret-data inline-def
))))))
2626 (format "[%s]" (concat label def
))))
2628 (defun org-element-footnote-reference-successor (limit)
2629 "Search for the next footnote-reference object.
2631 LIMIT bounds the search.
2633 Return value is a cons cell whose CAR is `footnote-reference' and
2634 CDR is beginning position."
2637 (while (re-search-forward org-footnote-re limit t
)
2639 (let ((beg (match-beginning 0))
2642 (while (re-search-forward "[][]" limit t
)
2643 (if (equal (match-string 0) "[") (incf count
) (decf count
))
2645 (throw 'exit
(cons 'footnote-reference beg
))))))))))
2648 ;;;; Inline Babel Call
2650 (defun org-element-inline-babel-call-parser ()
2651 "Parse inline babel call at point.
2653 Return a list whose CAR is `inline-babel-call' and CDR a plist
2654 with `:begin', `:end', `:info' and `:post-blank' as keywords.
2656 Assume point is at the beginning of the babel call."
2658 (unless (bolp) (backward-char))
2659 (looking-at org-babel-inline-lob-one-liner-regexp
)
2660 (let ((info (save-match-data (org-babel-lob-get-info)))
2661 (begin (match-end 1))
2662 (post-blank (progn (goto-char (match-end 0))
2663 (skip-chars-forward " \t")))
2665 (list 'inline-babel-call
2669 :post-blank post-blank
)))))
2671 (defun org-element-inline-babel-call-interpreter (inline-babel-call contents
)
2672 "Interpret INLINE-BABEL-CALL object as Org syntax.
2674 (let* ((babel-info (org-element-property :info inline-babel-call
))
2675 (main-source (car babel-info
))
2676 (post-options (nth 1 babel-info
)))
2678 (if (string-match "\\[\\(\\[.*?\\]\\)\\]" main-source
)
2679 ;; Remove redundant square brackets.
2681 (match-string 1 main-source
) nil nil main-source
)
2683 (and post-options
(format "[%s]" post-options
)))))
2685 (defun org-element-inline-babel-call-successor (limit)
2686 "Search for the next inline-babel-call object.
2688 LIMIT bounds the search.
2690 Return value is a cons cell whose CAR is `inline-babel-call' and
2691 CDR is beginning position."
2693 ;; Use a simplified version of
2694 ;; `org-babel-inline-lob-one-liner-regexp'.
2695 (when (re-search-forward
2696 "call_\\([^()\n]+?\\)\\(?:\\[.*?\\]\\)?([^\n]*?)\\(\\[.*?\\]\\)?"
2698 (cons 'inline-babel-call
(match-beginning 0)))))
2701 ;;;; Inline Src Block
2703 (defun org-element-inline-src-block-parser ()
2704 "Parse inline source block at point.
2706 LIMIT bounds the search.
2708 Return a list whose CAR is `inline-src-block' and CDR a plist
2709 with `:begin', `:end', `:language', `:value', `:parameters' and
2710 `:post-blank' as keywords.
2712 Assume point is at the beginning of the inline src block."
2714 (unless (bolp) (backward-char))
2715 (looking-at org-babel-inline-src-block-regexp
)
2716 (let ((begin (match-beginning 1))
2717 (language (org-match-string-no-properties 2))
2718 (parameters (org-match-string-no-properties 4))
2719 (value (org-match-string-no-properties 5))
2720 (post-blank (progn (goto-char (match-end 0))
2721 (skip-chars-forward " \t")))
2723 (list 'inline-src-block
2724 (list :language language
2726 :parameters parameters
2729 :post-blank post-blank
)))))
2731 (defun org-element-inline-src-block-interpreter (inline-src-block contents
)
2732 "Interpret INLINE-SRC-BLOCK object as Org syntax.
2734 (let ((language (org-element-property :language inline-src-block
))
2735 (arguments (org-element-property :parameters inline-src-block
))
2736 (body (org-element-property :value inline-src-block
)))
2737 (format "src_%s%s{%s}"
2739 (if arguments
(format "[%s]" arguments
) "")
2742 (defun org-element-inline-src-block-successor (limit)
2743 "Search for the next inline-babel-call element.
2745 LIMIT bounds the search.
2747 Return value is a cons cell whose CAR is `inline-babel-call' and
2748 CDR is beginning position."
2750 (unless (bolp) (backward-char))
2751 (when (re-search-forward org-babel-inline-src-block-regexp limit t
)
2752 (cons 'inline-src-block
(match-beginning 1)))))
2756 (defun org-element-italic-parser ()
2757 "Parse italic object at point.
2759 Return a list whose CAR is `italic' and CDR is a plist with
2760 `:begin', `:end', `:contents-begin' and `:contents-end' and
2761 `:post-blank' keywords.
2763 Assume point is at the first slash marker."
2765 (unless (bolp) (backward-char 1))
2766 (looking-at org-emph-re
)
2767 (let ((begin (match-beginning 2))
2768 (contents-begin (match-beginning 4))
2769 (contents-end (match-end 4))
2770 (post-blank (progn (goto-char (match-end 2))
2771 (skip-chars-forward " \t")))
2776 :contents-begin contents-begin
2777 :contents-end contents-end
2778 :post-blank post-blank
)))))
2780 (defun org-element-italic-interpreter (italic contents
)
2781 "Interpret ITALIC object as Org syntax.
2782 CONTENTS is the contents of the object."
2783 (format "/%s/" contents
))
2788 (defun org-element-latex-fragment-parser ()
2789 "Parse latex fragment at point.
2791 Return a list whose CAR is `latex-fragment' and CDR a plist with
2792 `:value', `:begin', `:end', and `:post-blank' as keywords.
2794 Assume point is at the beginning of the latex fragment."
2796 (let* ((begin (point))
2800 (let ((latex-regexp (nth 1 (assoc e org-latex-regexps
))))
2801 (when (or (looking-at latex-regexp
)
2805 (looking-at latex-regexp
))))
2806 (throw 'exit
(nth 2 (assoc e org-latex-regexps
))))))
2807 (plist-get org-format-latex-options
:matchers
))
2808 ;; None found: it's a macro.
2809 (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")
2811 (value (match-string-no-properties substring-match
))
2812 (post-blank (progn (goto-char (match-end substring-match
))
2813 (skip-chars-forward " \t")))
2815 (list 'latex-fragment
2819 :post-blank post-blank
)))))
2821 (defun org-element-latex-fragment-interpreter (latex-fragment contents
)
2822 "Interpret LATEX-FRAGMENT object as Org syntax.
2824 (org-element-property :value latex-fragment
))
2828 (defun org-element-line-break-parser ()
2829 "Parse line break at point.
2831 Return a list whose CAR is `line-break', and CDR a plist with
2832 `:begin', `:end' and `:post-blank' keywords.
2834 Assume point is at the beginning of the line break."
2835 (list 'line-break
(list :begin
(point) :end
(point-at-eol) :post-blank
0)))
2837 (defun org-element-line-break-interpreter (line-break contents
)
2838 "Interpret LINE-BREAK object as Org syntax.
2842 (defun org-element-line-break-successor (limit)
2843 "Search for the next line-break object.
2845 LIMIT bounds the search.
2847 Return value is a cons cell whose CAR is `line-break' and CDR is
2848 beginning position."
2850 (let ((beg (and (re-search-forward "[^\\\\]\\(\\\\\\\\\\)[ \t]*$" limit t
)
2851 (goto-char (match-beginning 1)))))
2852 ;; A line break can only happen on a non-empty line.
2853 (when (and beg
(re-search-backward "\\S-" (point-at-bol) t
))
2854 (cons 'line-break beg
)))))
2859 (defun org-element-link-parser ()
2860 "Parse link at point.
2862 Return a list whose CAR is `link' and CDR a plist with `:type',
2863 `:path', `:raw-link', `:application', `:search-option', `:begin',
2864 `:end', `:contents-begin', `:contents-end' and `:post-blank' as
2867 Assume point is at the beginning of the link."
2869 (let ((begin (point))
2870 end contents-begin contents-end link-end post-blank path type
2871 raw-link link search-option application
)
2873 ;; Type 1: Text targeted from a radio target.
2874 ((and org-target-link-regexp
(looking-at org-target-link-regexp
))
2876 link-end
(match-end 0)
2877 path
(org-match-string-no-properties 0)))
2878 ;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]]
2879 ((looking-at org-bracket-link-regexp
)
2880 (setq contents-begin
(match-beginning 3)
2881 contents-end
(match-end 3)
2882 link-end
(match-end 0)
2883 ;; RAW-LINK is the original link.
2884 raw-link
(org-match-string-no-properties 1)
2885 link
(org-translate-link
2886 (org-link-expand-abbrev
2887 (org-link-unescape raw-link
))))
2888 ;; Determine TYPE of link and set PATH accordingly.
2891 ((or (file-name-absolute-p link
) (string-match "^\\.\\.?/" link
))
2892 (setq type
"file" path link
))
2893 ;; Explicit type (http, irc, bbdb...). See `org-link-types'.
2894 ((string-match org-link-re-with-space3 link
)
2895 (setq type
(match-string 1 link
) path
(match-string 2 link
)))
2896 ;; Id type: PATH is the id.
2897 ((string-match "^id:\\([-a-f0-9]+\\)" link
)
2898 (setq type
"id" path
(match-string 1 link
)))
2899 ;; Code-ref type: PATH is the name of the reference.
2900 ((string-match "^(\\(.*\\))$" link
)
2901 (setq type
"coderef" path
(match-string 1 link
)))
2902 ;; Custom-id type: PATH is the name of the custom id.
2903 ((= (aref link
0) ?
#)
2904 (setq type
"custom-id" path
(substring link
1)))
2905 ;; Fuzzy type: Internal link either matches a target, an
2906 ;; headline name or nothing. PATH is the target or
2908 (t (setq type
"fuzzy" path link
))))
2909 ;; Type 3: Plain link, i.e. http://orgmode.org
2910 ((looking-at org-plain-link-re
)
2911 (setq raw-link
(org-match-string-no-properties 0)
2912 type
(org-match-string-no-properties 1)
2913 path
(org-match-string-no-properties 2)
2914 link-end
(match-end 0)))
2915 ;; Type 4: Angular link, i.e. <http://orgmode.org>
2916 ((looking-at org-angle-link-re
)
2917 (setq raw-link
(buffer-substring-no-properties
2918 (match-beginning 1) (match-end 2))
2919 type
(org-match-string-no-properties 1)
2920 path
(org-match-string-no-properties 2)
2921 link-end
(match-end 0))))
2922 ;; In any case, deduce end point after trailing white space from
2923 ;; LINK-END variable.
2924 (setq post-blank
(progn (goto-char link-end
) (skip-chars-forward " \t"))
2926 ;; Extract search option and opening application out of
2927 ;; "file"-type links.
2928 (when (member type org-element-link-type-is-file
)
2930 (cond ((string-match "^file\\+\\(.*\\)$" type
)
2931 (setq application
(match-string 1 type
)))
2932 ((not (string-match "^file" type
))
2933 (setq application type
)))
2934 ;; Extract search option from PATH.
2935 (when (string-match "::\\(.*\\)$" path
)
2936 (setq search-option
(match-string 1 path
)
2937 path
(replace-match "" nil nil path
)))
2938 ;; Make sure TYPE always report "file".
2943 :raw-link
(or raw-link path
)
2944 :application application
2945 :search-option search-option
2948 :contents-begin contents-begin
2949 :contents-end contents-end
2950 :post-blank post-blank
)))))
2952 (defun org-element-link-interpreter (link contents
)
2953 "Interpret LINK object as Org syntax.
2954 CONTENTS is the contents of the object, or nil."
2955 (let ((type (org-element-property :type link
))
2956 (raw-link (org-element-property :raw-link link
)))
2957 (if (string= type
"radio") raw-link
2960 (if contents
(format "[%s]" contents
) "")))))
2962 (defun org-element-link-successor (limit)
2963 "Search for the next link object.
2965 LIMIT bounds the search.
2967 Return value is a cons cell whose CAR is `link' and CDR is
2968 beginning position."
2971 (if (not org-target-link-regexp
) org-any-link-re
2972 (concat org-any-link-re
"\\|" org-target-link-regexp
))))
2973 (when (re-search-forward link-regexp limit t
)
2974 (cons 'link
(match-beginning 0))))))
2979 (defun org-element-macro-parser ()
2980 "Parse macro at point.
2982 Return a list whose CAR is `macro' and CDR a plist with `:key',
2983 `:args', `:begin', `:end', `:value' and `:post-blank' as
2986 Assume point is at the macro."
2988 (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
2989 (let ((begin (point))
2990 (key (downcase (org-match-string-no-properties 1)))
2991 (value (org-match-string-no-properties 0))
2992 (post-blank (progn (goto-char (match-end 0))
2993 (skip-chars-forward " \t")))
2995 (args (let ((args (org-match-string-no-properties 3)) args2
)
2997 (setq args
(org-split-string args
","))
2999 (while (string-match "\\\\\\'" (car args
))
3000 ;; Repair bad splits.
3001 (setcar (cdr args
) (concat (substring (car args
) 0 -
1)
3004 (push (pop args
) args2
))
3005 (mapcar 'org-trim
(nreverse args2
))))))
3012 :post-blank post-blank
)))))
3014 (defun org-element-macro-interpreter (macro contents
)
3015 "Interpret MACRO object as Org syntax.
3017 (org-element-property :value macro
))
3019 (defun org-element-macro-successor (limit)
3020 "Search for the next macro object.
3022 LIMIT bounds the search.
3024 Return value is cons cell whose CAR is `macro' and CDR is
3025 beginning position."
3027 (when (re-search-forward
3028 "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
3030 (cons 'macro
(match-beginning 0)))))
3035 (defun org-element-radio-target-parser ()
3036 "Parse radio target at point.
3038 Return a list whose CAR is `radio-target' and CDR a plist with
3039 `:begin', `:end', `:contents-begin', `:contents-end', `:value'
3040 and `:post-blank' as keywords.
3042 Assume point is at the radio target."
3044 (looking-at org-radio-target-regexp
)
3045 (let ((begin (point))
3046 (contents-begin (match-beginning 1))
3047 (contents-end (match-end 1))
3048 (value (org-match-string-no-properties 1))
3049 (post-blank (progn (goto-char (match-end 0))
3050 (skip-chars-forward " \t")))
3055 :contents-begin contents-begin
3056 :contents-end contents-end
3057 :post-blank post-blank
3060 (defun org-element-radio-target-interpreter (target contents
)
3061 "Interpret TARGET object as Org syntax.
3062 CONTENTS is the contents of the object."
3063 (concat "<<<" contents
">>>"))
3065 (defun org-element-radio-target-successor (limit)
3066 "Search for the next radio-target object.
3068 LIMIT bounds the search.
3070 Return value is a cons cell whose CAR is `radio-target' and CDR
3071 is beginning position."
3073 (when (re-search-forward org-radio-target-regexp limit t
)
3074 (cons 'radio-target
(match-beginning 0)))))
3077 ;;;; Statistics Cookie
3079 (defun org-element-statistics-cookie-parser ()
3080 "Parse statistics cookie at point.
3082 Return a list whose CAR is `statistics-cookie', and CDR a plist
3083 with `:begin', `:end', `:value' and `:post-blank' keywords.
3085 Assume point is at the beginning of the statistics-cookie."
3087 (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
3088 (let* ((begin (point))
3089 (value (buffer-substring-no-properties
3090 (match-beginning 0) (match-end 0)))
3091 (post-blank (progn (goto-char (match-end 0))
3092 (skip-chars-forward " \t")))
3094 (list 'statistics-cookie
3098 :post-blank post-blank
)))))
3100 (defun org-element-statistics-cookie-interpreter (statistics-cookie contents
)
3101 "Interpret STATISTICS-COOKIE object as Org syntax.
3103 (org-element-property :value statistics-cookie
))
3105 (defun org-element-statistics-cookie-successor (limit)
3106 "Search for the next statistics cookie object.
3108 LIMIT bounds the search.
3110 Return value is a cons cell whose CAR is `statistics-cookie' and
3111 CDR is beginning position."
3113 (when (re-search-forward "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]" limit t
)
3114 (cons 'statistics-cookie
(match-beginning 0)))))
3119 (defun org-element-strike-through-parser ()
3120 "Parse strike-through object at point.
3122 Return a list whose CAR is `strike-through' and CDR is a plist
3123 with `:begin', `:end', `:contents-begin' and `:contents-end' and
3124 `:post-blank' keywords.
3126 Assume point is at the first plus sign marker."
3128 (unless (bolp) (backward-char 1))
3129 (looking-at org-emph-re
)
3130 (let ((begin (match-beginning 2))
3131 (contents-begin (match-beginning 4))
3132 (contents-end (match-end 4))
3133 (post-blank (progn (goto-char (match-end 2))
3134 (skip-chars-forward " \t")))
3136 (list 'strike-through
3139 :contents-begin contents-begin
3140 :contents-end contents-end
3141 :post-blank post-blank
)))))
3143 (defun org-element-strike-through-interpreter (strike-through contents
)
3144 "Interpret STRIKE-THROUGH object as Org syntax.
3145 CONTENTS is the contents of the object."
3146 (format "+%s+" contents
))
3151 (defun org-element-subscript-parser ()
3152 "Parse subscript at point.
3154 Return a list whose CAR is `subscript' and CDR a plist with
3155 `:begin', `:end', `:contents-begin', `:contents-end',
3156 `:use-brackets-p' and `:post-blank' as keywords.
3158 Assume point is at the underscore."
3160 (unless (bolp) (backward-char))
3161 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp
)
3163 (not (looking-at org-match-substring-regexp
))))
3164 (begin (match-beginning 2))
3165 (contents-begin (or (match-beginning 5)
3166 (match-beginning 3)))
3167 (contents-end (or (match-end 5) (match-end 3)))
3168 (post-blank (progn (goto-char (match-end 0))
3169 (skip-chars-forward " \t")))
3174 :use-brackets-p bracketsp
3175 :contents-begin contents-begin
3176 :contents-end contents-end
3177 :post-blank post-blank
)))))
3179 (defun org-element-subscript-interpreter (subscript contents
)
3180 "Interpret SUBSCRIPT object as Org syntax.
3181 CONTENTS is the contents of the object."
3183 (if (org-element-property :use-brackets-p subscript
) "_{%s}" "_%s")
3186 (defun org-element-sub/superscript-successor
(limit)
3187 "Search for the next sub/superscript object.
3189 LIMIT bounds the search.
3191 Return value is a cons cell whose CAR is either `subscript' or
3192 `superscript' and CDR is beginning position."
3194 (unless (bolp) (backward-char))
3195 (when (re-search-forward org-match-substring-regexp limit t
)
3196 (cons (if (string= (match-string 2) "_") 'subscript
'superscript
)
3197 (match-beginning 2)))))
3202 (defun org-element-superscript-parser ()
3203 "Parse superscript at point.
3205 Return a list whose CAR is `superscript' and CDR a plist with
3206 `:begin', `:end', `:contents-begin', `:contents-end',
3207 `:use-brackets-p' and `:post-blank' as keywords.
3209 Assume point is at the caret."
3211 (unless (bolp) (backward-char))
3212 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp
) t
3213 (not (looking-at org-match-substring-regexp
))))
3214 (begin (match-beginning 2))
3215 (contents-begin (or (match-beginning 5)
3216 (match-beginning 3)))
3217 (contents-end (or (match-end 5) (match-end 3)))
3218 (post-blank (progn (goto-char (match-end 0))
3219 (skip-chars-forward " \t")))
3224 :use-brackets-p bracketsp
3225 :contents-begin contents-begin
3226 :contents-end contents-end
3227 :post-blank post-blank
)))))
3229 (defun org-element-superscript-interpreter (superscript contents
)
3230 "Interpret SUPERSCRIPT object as Org syntax.
3231 CONTENTS is the contents of the object."
3233 (if (org-element-property :use-brackets-p superscript
) "^{%s}" "^%s")
3239 (defun org-element-table-cell-parser ()
3240 "Parse table cell at point.
3242 Return a list whose CAR is `table-cell' and CDR is a plist
3243 containing `:begin', `:end', `:contents-begin', `:contents-end'
3244 and `:post-blank' keywords."
3245 (looking-at "[ \t]*\\(.*?\\)[ \t]*|")
3246 (let* ((begin (match-beginning 0))
3248 (contents-begin (match-beginning 1))
3249 (contents-end (match-end 1)))
3253 :contents-begin contents-begin
3254 :contents-end contents-end
3257 (defun org-element-table-cell-interpreter (table-cell contents
)
3258 "Interpret TABLE-CELL element as Org syntax.
3259 CONTENTS is the contents of the cell, or nil."
3260 (concat " " contents
" |"))
3262 (defun org-element-table-cell-successor (limit)
3263 "Search for the next table-cell object.
3265 LIMIT bounds the search.
3267 Return value is a cons cell whose CAR is `table-cell' and CDR is
3268 beginning position."
3269 (when (looking-at "[ \t]*.*?[ \t]+|") (cons 'table-cell
(point))))
3274 (defun org-element-target-parser ()
3275 "Parse target at point.
3277 Return a list whose CAR is `target' and CDR a plist with
3278 `:begin', `:end', `:value' and `:post-blank' as keywords.
3280 Assume point is at the target."
3282 (looking-at org-target-regexp
)
3283 (let ((begin (point))
3284 (value (org-match-string-no-properties 1))
3285 (post-blank (progn (goto-char (match-end 0))
3286 (skip-chars-forward " \t")))
3292 :post-blank post-blank
)))))
3294 (defun org-element-target-interpreter (target contents
)
3295 "Interpret TARGET object as Org syntax.
3297 (format "<<%s>>" (org-element-property :value target
)))
3299 (defun org-element-target-successor (limit)
3300 "Search for the next target object.
3302 LIMIT bounds the search.
3304 Return value is a cons cell whose CAR is `target' and CDR is
3305 beginning position."
3307 (when (re-search-forward org-target-regexp limit t
)
3308 (cons 'target
(match-beginning 0)))))
3313 (defun org-element-timestamp-parser ()
3314 "Parse time stamp at point.
3316 Return a list whose CAR is `timestamp', and CDR a plist with
3317 `:type', `:begin', `:end', `:value' and `:post-blank' keywords.
3319 Assume point is at the beginning of the timestamp."
3321 (let* ((begin (point))
3322 (activep (eq (char-after) ?
<))
3325 (looking-at "[<[]\\(\\(%%\\)?.*?\\)[]>]\\(?:--[<[]\\(.*?\\)[]>]\\)?")
3326 (match-string-no-properties 1)))
3327 (range-end (match-string-no-properties 3))
3328 (type (cond ((match-string 2) 'diary
)
3329 ((and activep range-end
) 'active-range
)
3331 (range-end 'inactive-range
)
3333 (post-blank (progn (goto-char (match-end 0))
3334 (skip-chars-forward " \t")))
3339 :range-end range-end
3342 :post-blank post-blank
)))))
3344 (defun org-element-timestamp-interpreter (timestamp contents
)
3345 "Interpret TIMESTAMP object as Org syntax.
3347 (let ((type (org-element-property :type timestamp
) ))
3349 (format (if (memq type
'(inactive inactive-range
)) "[%s]" "<%s>")
3350 (org-element-property :value timestamp
))
3351 (let ((range-end (org-element-property :range-end timestamp
)))
3354 (format (if (eq type
'inactive-range
) "[%s]" "<%s>")
3357 (defun org-element-timestamp-successor (limit)
3358 "Search for the next timestamp object.
3360 LIMIT bounds the search.
3362 Return value is a cons cell whose CAR is `timestamp' and CDR is
3363 beginning position."
3365 (when (re-search-forward
3366 (concat org-ts-regexp-both
3368 "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
3370 "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
3372 (cons 'timestamp
(match-beginning 0)))))
3377 (defun org-element-underline-parser ()
3378 "Parse underline object at point.
3380 Return a list whose CAR is `underline' and CDR is a plist with
3381 `:begin', `:end', `:contents-begin' and `:contents-end' and
3382 `:post-blank' keywords.
3384 Assume point is at the first underscore marker."
3386 (unless (bolp) (backward-char 1))
3387 (looking-at org-emph-re
)
3388 (let ((begin (match-beginning 2))
3389 (contents-begin (match-beginning 4))
3390 (contents-end (match-end 4))
3391 (post-blank (progn (goto-char (match-end 2))
3392 (skip-chars-forward " \t")))
3397 :contents-begin contents-begin
3398 :contents-end contents-end
3399 :post-blank post-blank
)))))
3401 (defun org-element-underline-interpreter (underline contents
)
3402 "Interpret UNDERLINE object as Org syntax.
3403 CONTENTS is the contents of the object."
3404 (format "_%s_" contents
))
3409 (defun org-element-verbatim-parser ()
3410 "Parse verbatim object at point.
3412 Return a list whose CAR is `verbatim' and CDR is a plist with
3413 `:value', `:begin', `:end' and `:post-blank' keywords.
3415 Assume point is at the first equal sign marker."
3417 (unless (bolp) (backward-char 1))
3418 (looking-at org-emph-re
)
3419 (let ((begin (match-beginning 2))
3420 (value (org-match-string-no-properties 4))
3421 (post-blank (progn (goto-char (match-end 2))
3422 (skip-chars-forward " \t")))
3428 :post-blank post-blank
)))))
3430 (defun org-element-verbatim-interpreter (verbatim contents
)
3431 "Interpret VERBATIM object as Org syntax.
3433 (format "=%s=" (org-element-property :value verbatim
)))
3437 ;;; Parsing Element Starting At Point
3439 ;; `org-element--current-element' is the core function of this section.
3440 ;; It returns the Lisp representation of the element starting at
3443 ;; `org-element--current-element' makes use of special modes. They
3444 ;; are activated for fixed element chaining (i.e. `plain-list' >
3445 ;; `item') or fixed conditional element chaining (i.e. `headline' >
3446 ;; `section'). Special modes are: `first-section', `item',
3447 ;; `node-property', `quote-section', `section' and `table-row'.
3449 (defun org-element--current-element
3450 (limit &optional granularity special structure
)
3451 "Parse the element starting at point.
3453 LIMIT bounds the search.
3455 Return value is a list like (TYPE PROPS) where TYPE is the type
3456 of the element and PROPS a plist of properties associated to the
3459 Possible types are defined in `org-element-all-elements'.
3461 Optional argument GRANULARITY determines the depth of the
3462 recursion. Allowed values are `headline', `greater-element',
3463 `element', `object' or nil. When it is broader than `object' (or
3464 nil), secondary values will not be parsed, since they only
3467 Optional argument SPECIAL, when non-nil, can be either
3468 `first-section', `item', `node-property', `quote-section',
3469 `section', and `table-row'.
3471 If STRUCTURE isn't provided but SPECIAL is set to `item', it will
3474 This function assumes point is always at the beginning of the
3475 element it has to parse."
3477 (let ((case-fold-search t
)
3478 ;; Determine if parsing depth allows for secondary strings
3479 ;; parsing. It only applies to elements referenced in
3480 ;; `org-element-secondary-value-alist'.
3481 (raw-secondary-p (and granularity
(not (eq granularity
'object
)))))
3485 (org-element-item-parser limit structure raw-secondary-p
))
3487 ((eq special
'table-row
) (org-element-table-row-parser limit
))
3489 ((eq special
'node-property
) (org-element-node-property-parser limit
))
3491 ((org-with-limited-levels (org-at-heading-p))
3492 (org-element-headline-parser limit raw-secondary-p
))
3493 ;; Sections (must be checked after headline).
3494 ((eq special
'section
) (org-element-section-parser limit
))
3495 ((eq special
'quote-section
) (org-element-quote-section-parser limit
))
3496 ((eq special
'first-section
)
3497 (org-element-section-parser
3498 (or (save-excursion (org-with-limited-levels (outline-next-heading)))
3500 ;; When not at bol, point is at the beginning of an item or
3501 ;; a footnote definition: next item is always a paragraph.
3502 ((not (bolp)) (org-element-paragraph-parser limit
(list (point))))
3503 ;; Planning and Clock.
3504 ((and (looking-at org-planning-or-clock-line-re
))
3505 (if (equal (match-string 1) org-clock-string
)
3506 (org-element-clock-parser limit
)
3507 (org-element-planning-parser limit
)))
3510 (org-element-inlinetask-parser limit raw-secondary-p
))
3511 ;; From there, elements can have affiliated keywords.
3512 (t (let ((affiliated (org-element--collect-affiliated-keywords)))
3514 ;; LaTeX Environment.
3515 ((looking-at "[ \t]*\\\\begin{\\([A-Za-z0-9*]+\\)}[ \t]*$")
3516 (org-element-latex-environment-parser limit affiliated
))
3517 ;; Drawer and Property Drawer.
3518 ((looking-at org-drawer-regexp
)
3519 (if (equal (match-string 1) "PROPERTIES")
3520 (org-element-property-drawer-parser limit affiliated
)
3521 (org-element-drawer-parser limit affiliated
)))
3523 ((looking-at "[ \t]*:\\( \\|$\\)")
3524 (org-element-fixed-width-parser limit affiliated
))
3525 ;; Inline Comments, Blocks, Babel Calls, Dynamic Blocks and
3527 ((looking-at "[ \t]*#")
3528 (goto-char (match-end 0))
3529 (cond ((looking-at "\\(?: \\|$\\)")
3531 (org-element-comment-parser limit affiliated
))
3532 ((looking-at "\\+BEGIN_\\(\\S-+\\)")
3534 (let ((parser (assoc (upcase (match-string 1))
3535 org-element-block-name-alist
)))
3536 (if parser
(funcall (cdr parser
) limit affiliated
)
3537 (org-element-special-block-parser limit affiliated
))))
3538 ((looking-at "\\+CALL:")
3540 (org-element-babel-call-parser limit affiliated
))
3541 ((looking-at "\\+BEGIN:? ")
3543 (org-element-dynamic-block-parser limit affiliated
))
3544 ((looking-at "\\+\\S-+:")
3546 (org-element-keyword-parser limit affiliated
))
3549 (org-element-paragraph-parser limit affiliated
))))
3550 ;; Footnote Definition.
3551 ((looking-at org-footnote-definition-re
)
3552 (org-element-footnote-definition-parser limit affiliated
))
3554 ((looking-at "[ \t]*-\\{5,\\}[ \t]*$")
3555 (org-element-horizontal-rule-parser limit affiliated
))
3557 ((org-at-table-p t
) (org-element-table-parser limit affiliated
))
3559 ((looking-at (org-item-re))
3560 (org-element-plain-list-parser
3561 limit affiliated
(or structure
(org-list-struct))))
3562 ;; Default element: Paragraph.
3563 (t (org-element-paragraph-parser limit affiliated
)))))))))
3566 ;; Most elements can have affiliated keywords. When looking for an
3567 ;; element beginning, we want to move before them, as they belong to
3568 ;; that element, and, in the meantime, collect information they give
3569 ;; into appropriate properties. Hence the following function.
3571 (defun org-element--collect-affiliated-keywords ()
3572 "Collect affiliated keywords from point.
3574 Return a list whose CAR is the position at the first of them and
3575 CDR a plist of keywords and values and move point to the
3576 beginning of the first line after them.
3578 As a special case, if element doesn't start at the beginning of
3579 the line (i.e. a paragraph starting an item), CAR is current
3580 position of point and CDR is nil."
3581 (if (not (bolp)) (list (point))
3582 (let ((case-fold-search t
)
3584 ;; RESTRICT is the list of objects allowed in parsed
3586 (restrict (org-element-restriction 'keyword
))
3588 (while (and (not (eobp)) (looking-at org-element--affiliated-re
))
3589 (let* ((raw-kwd (upcase (match-string 1)))
3590 ;; Apply translation to RAW-KWD. From there, KWD is
3591 ;; the official keyword.
3592 (kwd (or (cdr (assoc raw-kwd
3593 org-element-keyword-translation-alist
))
3595 ;; Find main value for any keyword.
3599 (buffer-substring-no-properties
3600 (match-end 0) (point-at-eol)))))
3601 ;; PARSEDP is non-nil when keyword should have its
3603 (parsedp (member kwd org-element-parsed-keywords
))
3604 ;; If KWD is a dual keyword, find its secondary
3605 ;; value. Maybe parse it.
3606 (dualp (member kwd org-element-dual-keywords
))
3609 (let ((sec (org-match-string-no-properties 2)))
3610 (if (or (not sec
) (not parsedp
)) sec
3611 (org-element-parse-secondary-string sec restrict
)))))
3612 ;; Attribute a property name to KWD.
3613 (kwd-sym (and kwd
(intern (concat ":" (downcase kwd
))))))
3614 ;; Now set final shape for VALUE.
3616 (setq value
(org-element-parse-secondary-string value restrict
)))
3617 (when dualp
(setq value
(and value
(cons value dual-value
))))
3618 (when (or (member kwd org-element-multiple-keywords
)
3619 ;; Attributes can always appear on multiple lines.
3620 (string-match "^ATTR_" kwd
))
3621 (setq value
(cons value
(plist-get output kwd-sym
))))
3622 ;; Eventually store the new value in OUTPUT.
3623 (setq output
(plist-put output kwd-sym value
))
3624 ;; Move to next keyword.
3626 ;; If affiliated keywords are orphaned: move back to first one.
3627 ;; They will be parsed as a paragraph.
3628 (when (looking-at "[ \t]*$") (goto-char origin
) (setq output nil
))
3630 (cons origin output
))))
3636 ;; The two major functions here are `org-element-parse-buffer', which
3637 ;; parses Org syntax inside the current buffer, taking into account
3638 ;; region, narrowing, or even visibility if specified, and
3639 ;; `org-element-parse-secondary-string', which parses objects within
3642 ;; The (almost) almighty `org-element-map' allows to apply a function
3643 ;; on elements or objects matching some type, and accumulate the
3644 ;; resulting values. In an export situation, it also skips unneeded
3645 ;; parts of the parse tree.
3647 (defun org-element-parse-buffer (&optional granularity visible-only
)
3648 "Recursively parse the buffer and return structure.
3649 If narrowing is in effect, only parse the visible part of the
3652 Optional argument GRANULARITY determines the depth of the
3653 recursion. It can be set to the following symbols:
3655 `headline' Only parse headlines.
3656 `greater-element' Don't recurse into greater elements excepted
3657 headlines and sections. Thus, elements
3658 parsed are the top-level ones.
3659 `element' Parse everything but objects and plain text.
3660 `object' Parse the complete buffer (default).
3662 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3665 Assume buffer is in Org mode."
3667 (goto-char (point-min))
3668 (org-skip-whitespace)
3669 (org-element--parse-elements
3670 (point-at-bol) (point-max)
3671 ;; Start in `first-section' mode so text before the first
3672 ;; headline belongs to a section.
3673 'first-section nil granularity visible-only
(list 'org-data nil
))))
3675 (defun org-element-parse-secondary-string (string restriction
&optional parent
)
3676 "Recursively parse objects in STRING and return structure.
3678 RESTRICTION is a symbol limiting the object types that will be
3681 Optional argument PARENT, when non-nil, is the element or object
3682 containing the secondary string. It is used to set correctly
3683 `:parent' property within the string."
3686 (let ((secondary (org-element--parse-objects
3687 (point-min) (point-max) nil restriction
)))
3688 (mapc (lambda (obj) (org-element-put-property obj
:parent parent
))
3691 (defun org-element-map
3692 (data types fun
&optional info first-match no-recursion with-affiliated
)
3693 "Map a function on selected elements or objects.
3695 DATA is the parsed tree, as returned by, i.e,
3696 `org-element-parse-buffer'. TYPES is a symbol or list of symbols
3697 of elements or objects types. FUN is the function called on the
3698 matching element or object. It must accept one arguments: the
3699 element or object itself.
3701 When optional argument INFO is non-nil, it should be a plist
3702 holding export options. In that case, parts of the parse tree
3703 not exportable according to that property list will be skipped.
3705 When optional argument FIRST-MATCH is non-nil, stop at the first
3706 match for which FUN doesn't return nil, and return that value.
3708 Optional argument NO-RECURSION is a symbol or a list of symbols
3709 representing elements or objects types. `org-element-map' won't
3710 enter any recursive element or object whose type belongs to that
3711 list. Though, FUN can still be applied on them.
3713 When optional argument WITH-AFFILIATED is non-nil, also move into
3714 affiliated keywords to find objects.
3716 Nil values returned from FUN do not appear in the results."
3717 ;; Ensure TYPES and NO-RECURSION are a list, even of one element.
3718 (unless (listp types
) (setq types
(list types
)))
3719 (unless (listp no-recursion
) (setq no-recursion
(list no-recursion
)))
3720 ;; Recursion depth is determined by --CATEGORY.
3723 (let ((category 'greater-elements
))
3724 (mapc (lambda (type)
3725 (cond ((or (memq type org-element-all-objects
)
3726 (eq type
'plain-text
))
3727 ;; If one object is found, the function
3728 ;; has to recurse into every object.
3729 (throw 'found
'objects
))
3730 ((not (memq type org-element-greater-elements
))
3731 ;; If one regular element is found, the
3732 ;; function has to recurse, at least,
3733 ;; into every element it encounters.
3734 (and (not (eq category
'elements
))
3735 (setq category
'elements
)))))
3738 ;; Compute properties for affiliated keywords if necessary.
3740 (and with-affiliated
3741 (mapcar (lambda (kwd)
3742 (cons kwd
(intern (concat ":" (downcase kwd
)))))
3743 org-element-affiliated-keywords
)))
3749 ;; Recursively walk DATA. INFO, if non-nil, is a plist
3750 ;; holding contextual information.
3751 (let ((--type (org-element-type --data
)))
3754 ;; Ignored element in an export context.
3755 ((and info
(memq --data
(plist-get info
:ignore-list
))))
3756 ;; Secondary string: only objects can be found there.
3758 (when (eq --category
'objects
) (mapc --walk-tree --data
)))
3759 ;; Unconditionally enter parse trees.
3760 ((eq --type
'org-data
)
3761 (mapc --walk-tree
(org-element-contents --data
)))
3763 ;; Check if TYPE is matching among TYPES. If so,
3764 ;; apply FUN to --DATA and accumulate return value
3765 ;; into --ACC (or exit if FIRST-MATCH is non-nil).
3766 (when (memq --type types
)
3767 (let ((result (funcall fun --data
)))
3768 (cond ((not result
))
3769 (first-match (throw '--map-first-match result
))
3770 (t (push result --acc
)))))
3771 ;; If --DATA has a secondary string that can contain
3772 ;; objects with their type among TYPES, look into it.
3773 (when (and (eq --category
'objects
) (not (stringp --data
)))
3775 (assq --type org-element-secondary-value-alist
)))
3777 (funcall --walk-tree
3778 (org-element-property (cdr sec-prop
) --data
)))))
3779 ;; If --DATA has any affiliated keywords and
3780 ;; WITH-AFFILIATED is non-nil, look for objects in
3782 (when (and with-affiliated
3783 (eq --category
'objects
)
3784 (memq --type org-element-all-elements
))
3785 (mapc (lambda (kwd-pair)
3786 (let ((kwd (car kwd-pair
))
3787 (value (org-element-property
3788 (cdr kwd-pair
) --data
)))
3789 ;; Pay attention to the type of value.
3790 ;; Preserve order for multiple keywords.
3793 ((and (member kwd org-element-multiple-keywords
)
3794 (member kwd org-element-dual-keywords
))
3795 (mapc (lambda (line)
3796 (funcall --walk-tree
(cdr line
))
3797 (funcall --walk-tree
(car line
)))
3799 ((member kwd org-element-multiple-keywords
)
3800 (mapc (lambda (line) (funcall --walk-tree line
))
3802 ((member kwd org-element-dual-keywords
)
3803 (funcall --walk-tree
(cdr value
))
3804 (funcall --walk-tree
(car value
)))
3805 (t (funcall --walk-tree value
)))))
3806 --affiliated-alist
))
3807 ;; Determine if a recursion into --DATA is possible.
3809 ;; --TYPE is explicitly removed from recursion.
3810 ((memq --type no-recursion
))
3811 ;; --DATA has no contents.
3812 ((not (org-element-contents --data
)))
3813 ;; Looking for greater elements but --DATA is simply
3814 ;; an element or an object.
3815 ((and (eq --category
'greater-elements
)
3816 (not (memq --type org-element-greater-elements
))))
3817 ;; Looking for elements but --DATA is an object.
3818 ((and (eq --category
'elements
)
3819 (memq --type org-element-all-objects
)))
3820 ;; In any other case, map contents.
3821 (t (mapc --walk-tree
(org-element-contents --data
)))))))))))
3822 (catch '--map-first-match
3823 (funcall --walk-tree data
)
3824 ;; Return value in a proper order.
3827 ;; The following functions are internal parts of the parser.
3829 ;; The first one, `org-element--parse-elements' acts at the element's
3832 ;; The second one, `org-element--parse-objects' applies on all objects
3833 ;; of a paragraph or a secondary string. It uses
3834 ;; `org-element--get-next-object-candidates' to optimize the search of
3835 ;; the next object in the buffer.
3837 ;; More precisely, that function looks for every allowed object type
3838 ;; first. Then, it discards failed searches, keeps further matches,
3839 ;; and searches again types matched behind point, for subsequent
3840 ;; calls. Thus, searching for a given type fails only once, and every
3841 ;; object is searched only once at top level (but sometimes more for
3844 (defun org-element--parse-elements
3845 (beg end special structure granularity visible-only acc
)
3846 "Parse elements between BEG and END positions.
3848 SPECIAL prioritize some elements over the others. It can be set
3849 to `first-section', `quote-section', `section' `item' or
3852 When value is `item', STRUCTURE will be used as the current list
3855 GRANULARITY determines the depth of the recursion. See
3856 `org-element-parse-buffer' for more information.
3858 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3861 Elements are accumulated into ACC."
3864 ;; When parsing only headlines, skip any text before first one.
3865 (when (and (eq granularity
'headline
) (not (org-at-heading-p)))
3866 (org-with-limited-levels (outline-next-heading)))
3868 (while (< (point) end
)
3869 ;; Find current element's type and parse it accordingly to
3871 (let* ((element (org-element--current-element
3872 end granularity special structure
))
3873 (type (org-element-type element
))
3874 (cbeg (org-element-property :contents-begin element
)))
3875 (goto-char (org-element-property :end element
))
3876 ;; Fill ELEMENT contents by side-effect.
3878 ;; If VISIBLE-ONLY is true and element is hidden or if it has
3879 ;; no contents, don't modify it.
3880 ((or (and visible-only
(org-element-property :hiddenp element
))
3882 ;; Greater element: parse it between `contents-begin' and
3883 ;; `contents-end'. Make sure GRANULARITY allows the
3884 ;; recursion, or ELEMENT is an headline, in which case going
3885 ;; inside is mandatory, in order to get sub-level headings.
3886 ((and (memq type org-element-greater-elements
)
3887 (or (memq granularity
'(element object nil
))
3888 (and (eq granularity
'greater-element
)
3890 (eq type
'headline
)))
3891 (org-element--parse-elements
3892 cbeg
(org-element-property :contents-end element
)
3893 ;; Possibly switch to a special mode.
3896 (if (org-element-property :quotedp element
) 'quote-section
3899 (property-drawer 'node-property
)
3901 (org-element-property :structure element
)
3902 granularity visible-only element
))
3903 ;; ELEMENT has contents. Parse objects inside, if
3904 ;; GRANULARITY allows it.
3905 ((memq granularity
'(object nil
))
3906 (org-element--parse-objects
3907 cbeg
(org-element-property :contents-end element
) element
3908 (org-element-restriction type
))))
3909 (org-element-adopt-elements acc element
)))
3913 (defun org-element--parse-objects (beg end acc restriction
)
3914 "Parse objects between BEG and END and return recursive structure.
3916 Objects are accumulated in ACC.
3918 RESTRICTION is a list of object types which are allowed in the
3923 (while (and (< (point) end
)
3924 (setq candidates
(org-element--get-next-object-candidates
3925 end restriction candidates
)))
3927 (let ((pos (apply 'min
(mapcar 'cdr candidates
))))
3930 (funcall (intern (format "org-element-%s-parser"
3931 (car (rassq pos candidates
)))))))))
3932 ;; 1. Text before any object. Untabify it.
3933 (let ((obj-beg (org-element-property :begin next-object
)))
3934 (unless (= (point) obj-beg
)
3936 (org-element-adopt-elements
3938 (replace-regexp-in-string
3939 "\t" (make-string tab-width ?
)
3940 (buffer-substring-no-properties (point) obj-beg
))))))
3942 (let ((obj-end (org-element-property :end next-object
))
3943 (cont-beg (org-element-property :contents-begin next-object
)))
3944 ;; Fill contents of NEXT-OBJECT by side-effect, if it has
3945 ;; a recursive type.
3947 (memq (car next-object
) org-element-recursive-objects
))
3951 (org-element-property :contents-end next-object
))
3952 (org-element--parse-objects
3953 (point-min) (point-max) next-object
3954 (org-element-restriction next-object
))))
3955 (setq acc
(org-element-adopt-elements acc next-object
))
3956 (goto-char obj-end
))))
3957 ;; 3. Text after last object. Untabify it.
3958 (unless (= (point) end
)
3960 (org-element-adopt-elements
3962 (replace-regexp-in-string
3963 "\t" (make-string tab-width ?
)
3964 (buffer-substring-no-properties (point) end
)))))
3968 (defun org-element--get-next-object-candidates (limit restriction objects
)
3969 "Return an alist of candidates for the next object.
3971 LIMIT bounds the search, and RESTRICTION narrows candidates to
3974 Return value is an alist whose CAR is position and CDR the object
3977 OBJECTS is the previous candidates alist."
3978 ;; Filter out any object found but not belonging to RESTRICTION.
3982 (let ((type (car obj
)))
3983 (memq (or (cdr (assq type org-element-object-successor-alist
))
3987 (let (next-candidates types-to-search
)
3988 ;; If no previous result, search every object type in RESTRICTION.
3989 ;; Otherwise, keep potential candidates (old objects located after
3990 ;; point) and ask to search again those which had matched before.
3991 (if (not objects
) (setq types-to-search restriction
)
3993 (if (< (cdr obj
) (point)) (push (car obj
) types-to-search
)
3994 (push obj next-candidates
)))
3996 ;; Call the appropriate successor function for each type to search
3997 ;; and accumulate matches.
4000 (let* ((successor-fun
4002 (format "org-element-%s-successor"
4003 (or (cdr (assq type org-element-object-successor-alist
))
4005 (obj (funcall successor-fun limit
)))
4006 (and obj
(push obj next-candidates
))))
4013 ;;; Towards A Bijective Process
4015 ;; The parse tree obtained with `org-element-parse-buffer' is really
4016 ;; a snapshot of the corresponding Org buffer. Therefore, it can be
4017 ;; interpreted and expanded into a string with canonical Org syntax.
4018 ;; Hence `org-element-interpret-data'.
4020 ;; The function relies internally on
4021 ;; `org-element--interpret-affiliated-keywords'.
4024 (defun org-element-interpret-data (data &optional parent
)
4025 "Interpret DATA as Org syntax.
4027 DATA is a parse tree, an element, an object or a secondary string
4030 Optional argument PARENT is used for recursive calls. It contains
4031 the element or object containing data, or nil.
4033 Return Org syntax as a string."
4034 (let* ((type (org-element-type data
))
4037 ;; Secondary string.
4040 (lambda (obj) (org-element-interpret-data obj parent
))
4042 ;; Full Org document.
4043 ((eq type
'org-data
)
4045 (lambda (obj) (org-element-interpret-data obj parent
))
4046 (org-element-contents data
) ""))
4048 ((stringp data
) data
)
4049 ;; Element/Object without contents.
4050 ((not (org-element-contents data
))
4051 (funcall (intern (format "org-element-%s-interpreter" type
))
4053 ;; Element/Object with contents.
4055 (let* ((greaterp (memq type org-element-greater-elements
))
4056 (objectp (and (not greaterp
)
4057 (memq type org-element-recursive-objects
)))
4060 (lambda (obj) (org-element-interpret-data obj data
))
4061 (org-element-contents
4062 (if (or greaterp objectp
) data
4063 ;; Elements directly containing objects must
4064 ;; have their indentation normalized first.
4065 (org-element-normalize-contents
4067 ;; When normalizing first paragraph of an
4068 ;; item or a footnote-definition, ignore
4069 ;; first line's indentation.
4070 (and (eq type
'paragraph
)
4071 (equal data
(car (org-element-contents parent
)))
4072 (memq (org-element-type parent
)
4073 '(footnote-definition item
))))))
4075 (funcall (intern (format "org-element-%s-interpreter" type
))
4077 (if greaterp
(org-element-normalize-contents contents
)
4079 (if (memq type
'(org-data plain-text nil
)) results
4080 ;; Build white spaces. If no `:post-blank' property is
4081 ;; specified, assume its value is 0.
4082 (let ((post-blank (or (org-element-property :post-blank data
) 0)))
4083 (if (memq type org-element-all-objects
)
4084 (concat results
(make-string post-blank
32))
4086 (org-element--interpret-affiliated-keywords data
)
4087 (org-element-normalize-string results
)
4088 (make-string post-blank
10)))))))
4090 (defun org-element--interpret-affiliated-keywords (element)
4091 "Return ELEMENT's affiliated keywords as Org syntax.
4092 If there is no affiliated keyword, return the empty string."
4093 (let ((keyword-to-org
4097 (when (member key org-element-dual-keywords
)
4098 (setq dual
(cdr value
) value
(car value
)))
4101 (format "[%s]" (org-element-interpret-data dual
)))
4103 (if (member key org-element-parsed-keywords
)
4104 (org-element-interpret-data value
)
4109 (let ((value (org-element-property prop element
))
4110 (keyword (upcase (substring (symbol-name prop
) 1))))
4112 (if (or (member keyword org-element-multiple-keywords
)
4113 ;; All attribute keywords can have multiple lines.
4114 (string-match "^ATTR_" keyword
))
4115 (mapconcat (lambda (line) (funcall keyword-to-org keyword line
))
4118 (funcall keyword-to-org keyword value
)))))
4119 ;; List all ELEMENT's properties matching an attribute line or an
4120 ;; affiliated keyword, but ignore translated keywords since they
4121 ;; cannot belong to the property list.
4122 (loop for prop in
(nth 1 element
) by
'cddr
4123 when
(let ((keyword (upcase (substring (symbol-name prop
) 1))))
4124 (or (string-match "^ATTR_" keyword
)
4126 (member keyword org-element-affiliated-keywords
)
4128 org-element-keyword-translation-alist
)))))
4132 ;; Because interpretation of the parse tree must return the same
4133 ;; number of blank lines between elements and the same number of white
4134 ;; space after objects, some special care must be given to white
4137 ;; The first function, `org-element-normalize-string', ensures any
4138 ;; string different from the empty string will end with a single
4139 ;; newline character.
4141 ;; The second function, `org-element-normalize-contents', removes
4142 ;; global indentation from the contents of the current element.
4144 (defun org-element-normalize-string (s)
4145 "Ensure string S ends with a single newline character.
4147 If S isn't a string return it unchanged. If S is the empty
4148 string, return it. Otherwise, return a new string with a single
4149 newline character at its end."
4151 ((not (stringp s
)) s
)
4153 (t (and (string-match "\\(\n[ \t]*\\)*\\'" s
)
4154 (replace-match "\n" nil nil s
)))))
4156 (defun org-element-normalize-contents (element &optional ignore-first
)
4157 "Normalize plain text in ELEMENT's contents.
4159 ELEMENT must only contain plain text and objects.
4161 If optional argument IGNORE-FIRST is non-nil, ignore first line's
4162 indentation to compute maximal common indentation.
4164 Return the normalized element that is element with global
4165 indentation removed from its contents. The function assumes that
4166 indentation is not done with TAB characters."
4167 (let* (ind-list ; for byte-compiler
4168 collect-inds
; for byte-compiler
4171 ;; Return list of indentations within BLOB. This is done by
4172 ;; walking recursively BLOB and updating IND-LIST along the
4173 ;; way. FIRST-FLAG is non-nil when the first string hasn't
4174 ;; been seen yet. It is required as this string is the only
4175 ;; one whose indentation doesn't happen after a newline
4177 (lambda (blob first-flag
)
4180 (when (and first-flag
(stringp object
))
4181 (setq first-flag nil
)
4182 (string-match "\\`\\( *\\)" object
)
4183 (let ((len (length (match-string 1 object
))))
4184 ;; An indentation of zero means no string will be
4185 ;; modified. Quit the process.
4186 (if (zerop len
) (throw 'zero
(setq ind-list nil
))
4187 (push len ind-list
))))
4191 ;; Avoid matching blank or empty lines.
4192 (while (and (string-match "\n\\( *\\)\\(.\\)" object start
)
4193 (not (equal (match-string 2 object
) " ")))
4194 (setq start
(match-end 0))
4195 (push (length (match-string 1 object
)) ind-list
))))
4196 ((memq (org-element-type object
) org-element-recursive-objects
)
4197 (funcall collect-inds object first-flag
))))
4198 (org-element-contents blob
))))))
4199 ;; Collect indentation list in ELEMENT. Possibly remove first
4200 ;; value if IGNORE-FIRST is non-nil.
4201 (catch 'zero
(funcall collect-inds element
(not ignore-first
)))
4202 (if (not ind-list
) element
4203 ;; Build ELEMENT back, replacing each string with the same
4204 ;; string minus common indentation.
4205 (let* (build ; For byte compiler.
4208 (lambda (blob mci first-flag
)
4209 ;; Return BLOB with all its strings indentation
4210 ;; shortened from MCI white spaces. FIRST-FLAG is
4211 ;; non-nil when the first string hasn't been seen
4216 (when (and first-flag
(stringp object
))
4217 (setq first-flag nil
)
4219 (replace-regexp-in-string
4220 (format "\\` \\{%d\\}" mci
) "" object
)))
4223 (replace-regexp-in-string
4224 (format "\n \\{%d\\}" mci
) "\n" object
))
4225 ((memq (org-element-type object
)
4226 org-element-recursive-objects
)
4227 (funcall build object mci first-flag
))
4229 (org-element-contents blob
)))
4231 (funcall build element
(apply 'min ind-list
) (not ignore-first
))))))
4237 ;; The first move is to implement a way to obtain the smallest element
4238 ;; containing point. This is the job of `org-element-at-point'. It
4239 ;; basically jumps back to the beginning of section containing point
4240 ;; and moves, element after element, with
4241 ;; `org-element--current-element' until the container is found. Note:
4242 ;; When using `org-element-at-point', secondary values are never
4243 ;; parsed since the function focuses on elements, not on objects.
4245 ;; At a deeper level, `org-element-context' lists all elements and
4246 ;; objects containing point.
4248 ;; `org-element-nested-p' and `org-element-swap-A-B' may be used
4249 ;; internally by navigation and manipulation tools.
4252 (defun org-element-at-point (&optional keep-trail
)
4253 "Determine closest element around point.
4255 Return value is a list like (TYPE PROPS) where TYPE is the type
4256 of the element and PROPS a plist of properties associated to the
4259 Possible types are defined in `org-element-all-elements'.
4260 Properties depend on element or object type, but always
4261 include :begin, :end, :parent and :post-blank properties.
4263 As a special case, if point is at the very beginning of a list or
4264 sub-list, returned element will be that list instead of the first
4265 item. In the same way, if point is at the beginning of the first
4266 row of a table, returned element will be the table instead of the
4269 If optional argument KEEP-TRAIL is non-nil, the function returns
4270 a list of of elements leading to element at point. The list's
4271 CAR is always the element at point. Following positions contain
4272 element's siblings, then parents, siblings of parents, until the
4273 first element of current section."
4274 (org-with-wide-buffer
4275 ;; If at an headline, parse it. It is the sole element that
4276 ;; doesn't require to know about context. Be sure to disallow
4277 ;; secondary string parsing, though.
4278 (if (org-with-limited-levels (org-at-heading-p))
4281 (if (not keep-trail
) (org-element-headline-parser (point-max) t
)
4282 (list (org-element-headline-parser (point-max) t
))))
4283 ;; Otherwise move at the beginning of the section containing
4285 (let ((origin (point))
4286 (end (save-excursion
4287 (org-with-limited-levels (outline-next-heading)) (point)))
4288 element type special-flag trail struct prevs parent
)
4289 (org-with-limited-levels
4290 (if (org-with-limited-levels (org-before-first-heading-p))
4291 (goto-char (point-min))
4292 (org-back-to-heading)
4294 (org-skip-whitespace)
4296 ;; Parse successively each element, skipping those ending
4297 ;; before original position.
4301 (org-element--current-element end
'element special-flag struct
)
4303 (org-element-put-property element
:parent parent
)
4304 (when keep-trail
(push element trail
))
4306 ;; 1. Skip any element ending before point. Also skip
4307 ;; element ending at point when we're sure that another
4308 ;; element has started.
4309 ((let ((elem-end (org-element-property :end element
)))
4310 (when (or (< elem-end origin
)
4311 (and (= elem-end origin
) (/= elem-end end
)))
4312 (goto-char elem-end
))))
4313 ;; 2. An element containing point is always the element at
4315 ((not (memq type org-element-greater-elements
))
4316 (throw 'exit
(if keep-trail trail element
)))
4317 ;; 3. At any other greater element type, if point is
4318 ;; within contents, move into it.
4320 (let ((cbeg (org-element-property :contents-begin element
))
4321 (cend (org-element-property :contents-end element
)))
4322 (if (or (not cbeg
) (not cend
) (> cbeg origin
) (< cend origin
)
4323 ;; Create an anchor for tables and plain lists:
4324 ;; when point is at the very beginning of these
4325 ;; elements, ignoring affiliated keywords,
4326 ;; target them instead of their contents.
4327 (and (= cbeg origin
) (memq type
'(plain-list table
)))
4328 ;; When point is at contents end, do not move
4329 ;; into elements with an explicit ending, but
4330 ;; return that element instead.
4331 (and (= cend origin
)
4334 drawer dynamic-block inlinetask item
4335 plain-list property-drawer quote-block
4337 (throw 'exit
(if keep-trail trail element
))
4338 (setq parent element
)
4341 (setq special-flag
'item
4342 struct
(org-element-property :structure element
)))
4343 (property-drawer (setq special-flag
'node-property
))
4344 (table (setq special-flag
'table-row
))
4345 (otherwise (setq special-flag nil
)))
4347 (goto-char cbeg
)))))))))))
4350 (defun org-element-context ()
4351 "Return closest element or object around point.
4353 Return value is a list like (TYPE PROPS) where TYPE is the type
4354 of the element or object and PROPS a plist of properties
4357 Possible types are defined in `org-element-all-elements' and
4358 `org-element-all-objects'. Properties depend on element or
4359 object type, but always include :begin, :end, :parent
4360 and :post-blank properties."
4361 (org-with-wide-buffer
4362 (let* ((origin (point))
4363 (element (org-element-at-point))
4364 (type (car element
))
4366 ;; Check if point is inside an element containing objects or at
4367 ;; a secondary string. In that case, move to beginning of the
4368 ;; element or secondary string and set END to the other side.
4369 (if (not (or (and (eq type
'item
)
4370 (let ((tag (org-element-property :tag element
)))
4374 (search-forward tag
(point-at-eol))
4375 (goto-char (match-beginning 0))
4376 (and (>= origin
(point))
4378 ;; `1+' is required so some
4379 ;; successors can match
4380 ;; properly their object.
4381 (setq end
(1+ (match-end 0)))))))))
4382 (and (memq type
'(headline inlinetask
))
4383 (progn (beginning-of-line)
4384 (skip-chars-forward "* ")
4385 (setq end
(point-at-eol))))
4386 (and (memq type
'(paragraph table-row verse-block
))
4387 (let ((cbeg (org-element-property
4388 :contents-begin element
))
4389 (cend (org-element-property
4390 :contents-end element
)))
4391 (and (>= origin cbeg
)
4393 (progn (goto-char cbeg
) (setq end cend
)))))))
4395 (let ((restriction (org-element-restriction element
))
4399 (while (setq candidates
(org-element--get-next-object-candidates
4400 end restriction candidates
))
4401 (let ((closest-cand (rassq (apply 'min
(mapcar 'cdr candidates
))
4403 ;; If ORIGIN is before next object in element, there's
4404 ;; no point in looking further.
4405 (if (> (cdr closest-cand
) origin
) (throw 'exit parent
)
4407 (progn (goto-char (cdr closest-cand
))
4408 (funcall (intern (format "org-element-%s-parser"
4409 (car closest-cand
))))))
4410 (cbeg (org-element-property :contents-begin object
))
4411 (cend (org-element-property :contents-end object
)))
4413 ;; ORIGIN is after OBJECT, so skip it.
4414 ((< (org-element-property :end object
) origin
)
4415 (goto-char (org-element-property :end object
)))
4416 ;; ORIGIN is within a non-recursive object or at an
4417 ;; object boundaries: Return that object.
4418 ((or (not cbeg
) (> cbeg origin
) (< cend origin
))
4420 (org-element-put-property object
:parent parent
)))
4421 ;; Otherwise, move within current object and restrict
4422 ;; search to the end of its contents.
4424 (org-element-put-property object
:parent parent
)
4426 restriction
(org-element-restriction object
)
4430 (defsubst org-element-nested-p
(elem-A elem-B
)
4431 "Non-nil when elements ELEM-A and ELEM-B are nested."
4432 (let ((beg-A (org-element-property :begin elem-A
))
4433 (beg-B (org-element-property :begin elem-B
))
4434 (end-A (org-element-property :end elem-A
))
4435 (end-B (org-element-property :end elem-B
)))
4436 (or (and (>= beg-A beg-B
) (<= end-A end-B
))
4437 (and (>= beg-B beg-A
) (<= end-B end-A
)))))
4439 (defun org-element-swap-A-B (elem-A elem-B
)
4440 "Swap elements ELEM-A and ELEM-B.
4441 Assume ELEM-B is after ELEM-A in the buffer. Leave point at the
4443 (goto-char (org-element-property :begin elem-A
))
4444 ;; There are two special cases when an element doesn't start at bol:
4445 ;; the first paragraph in an item or in a footnote definition.
4446 (let ((specialp (not (bolp))))
4447 ;; Only a paragraph without any affiliated keyword can be moved at
4448 ;; ELEM-A position in such a situation. Note that the case of
4449 ;; a footnote definition is impossible: it cannot contain two
4450 ;; paragraphs in a row because it cannot contain a blank line.
4452 (or (not (eq (org-element-type elem-B
) 'paragraph
))
4453 (/= (org-element-property :begin elem-B
)
4454 (org-element-property :contents-begin elem-B
))))
4455 (error "Cannot swap elements"))
4456 ;; In a special situation, ELEM-A will have no indentation. We'll
4457 ;; give it ELEM-B's (which will in, in turn, have no indentation).
4458 (let* ((ind-B (when specialp
4459 (goto-char (org-element-property :begin elem-B
))
4460 (org-get-indentation)))
4461 (beg-A (org-element-property :begin elem-A
))
4462 (end-A (save-excursion
4463 (goto-char (org-element-property :end elem-A
))
4464 (skip-chars-backward " \r\t\n")
4466 (beg-B (org-element-property :begin elem-B
))
4467 (end-B (save-excursion
4468 (goto-char (org-element-property :end elem-B
))
4469 (skip-chars-backward " \r\t\n")
4471 ;; Store overlays responsible for visibility status. We
4472 ;; also need to store their boundaries as they will be
4473 ;; removed from buffer.
4476 (mapcar (lambda (ov) (list ov
(overlay-start ov
) (overlay-end ov
)))
4477 (overlays-in beg-A end-A
))
4478 (mapcar (lambda (ov) (list ov
(overlay-start ov
) (overlay-end ov
)))
4479 (overlays-in beg-B end-B
))))
4481 (body-A (buffer-substring beg-A end-A
))
4482 (body-B (delete-and-extract-region beg-B end-B
)))
4485 (setq body-B
(replace-regexp-in-string "\\`[ \t]*" "" body-B
))
4486 (org-indent-to-column ind-B
))
4488 ;; Restore ex ELEM-A overlays.
4489 (let ((offset (- beg-B beg-A
)))
4492 (car ov
) (+ (nth 1 ov
) offset
) (+ (nth 2 ov
) offset
)))
4495 (delete-region beg-A end-A
)
4497 ;; Restore ex ELEM-B overlays.
4500 (car ov
) (- (nth 1 ov
) offset
) (- (nth 2 ov
) offset
)))
4502 (goto-char (org-element-property :end elem-B
)))))
4504 (provide 'org-element
)
4507 ;; generated-autoload-file: "org-loaddefs.el"
4510 ;;; org-element.el ends here