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 ((case-fold-search t
)
1792 (begin (car affiliated
))
1793 (key (progn (looking-at "[ \t]*#\\+\\(\\S-+*\\):")
1794 (upcase (org-match-string-no-properties 1))))
1795 (value (org-trim (buffer-substring-no-properties
1796 (match-end 0) (point-at-eol))))
1797 (pos-before-blank (progn (forward-line) (point)))
1798 (end (progn (skip-chars-forward " \r\t\n" limit
)
1799 (skip-chars-backward " \t")
1800 (if (bolp) (point) (line-end-position)))))
1807 :post-blank
(count-lines pos-before-blank end
))
1808 (cdr affiliated
))))))
1810 (defun org-element-keyword-interpreter (keyword contents
)
1811 "Interpret KEYWORD element as Org syntax.
1814 (org-element-property :key keyword
)
1815 (org-element-property :value keyword
)))
1818 ;;;; Latex Environment
1820 (defun org-element-latex-environment-parser (limit affiliated
)
1821 "Parse a LaTeX environment.
1823 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1824 the buffer position at the beginning of the first affiliated
1825 keyword and CDR is a plist of affiliated keywords along with
1828 Return a list whose CAR is `latex-environment' and CDR is a plist
1829 containing `:begin', `:end', `:value' and `:post-blank'
1832 Assume point is at the beginning of the latex environment."
1834 (let* ((case-fold-search t
)
1835 (code-begin (point))
1836 (begin (car affiliated
))
1837 (env (progn (looking-at "^[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}")
1838 (regexp-quote (match-string 1))))
1840 (progn (re-search-forward
1841 (format "^[ \t]*\\\\end{%s}[ \t]*$" env
) limit t
)
1844 (value (buffer-substring-no-properties code-begin code-end
))
1845 (end (progn (skip-chars-forward " \r\t\n" limit
)
1846 (skip-chars-backward " \t")
1847 (if (bolp) (point) (line-end-position)))))
1848 (list 'latex-environment
1853 :post-blank
(count-lines code-end end
))
1854 (cdr affiliated
))))))
1856 (defun org-element-latex-environment-interpreter (latex-environment contents
)
1857 "Interpret LATEX-ENVIRONMENT element as Org syntax.
1859 (org-element-property :value latex-environment
))
1864 (defun org-element-node-property-parser (limit)
1865 "Parse a node-property at point.
1867 LIMIT bounds the search.
1869 Return a list whose CAR is `node-property' and CDR is a plist
1870 containing `:key', `:value', `:begin', `:end' and `:post-blank'
1873 (let ((case-fold-search t
)
1875 (key (progn (looking-at "[ \t]*:\\(.*?\\):[ \t]+\\(.*?\\)[ \t]*$")
1876 (org-match-string-no-properties 1)))
1877 (value (org-match-string-no-properties 2))
1878 (pos-before-blank (progn (forward-line) (point)))
1879 (end (progn (skip-chars-forward " \r\t\n" limit
)
1880 (if (eobp) (point) (point-at-bol)))))
1881 (list 'node-property
1886 :post-blank
(count-lines pos-before-blank end
))))))
1888 (defun org-element-node-property-interpreter (node-property contents
)
1889 "Interpret NODE-PROPERTY element as Org syntax.
1891 (format org-property-format
1892 (format ":%s:" (org-element-property :key node-property
))
1893 (org-element-property :value node-property
)))
1898 (defun org-element-paragraph-parser (limit affiliated
)
1901 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1902 the buffer position at the beginning of the first affiliated
1903 keyword and CDR is a plist of affiliated keywords along with
1906 Return a list whose CAR is `paragraph' and CDR is a plist
1907 containing `:begin', `:end', `:contents-begin' and
1908 `:contents-end' and `:post-blank' keywords.
1910 Assume point is at the beginning of the paragraph."
1912 (let* ((begin (car affiliated
))
1913 (contents-begin (point))
1915 (let ((case-fold-search t
))
1917 (if (not (re-search-forward
1918 org-element-paragraph-separate limit
'm
))
1920 ;; A matching `org-element-paragraph-separate' is not
1921 ;; necessarily the end of the paragraph. In
1922 ;; particular, lines starting with # or : as a first
1923 ;; non-space character are ambiguous. We have check
1924 ;; if they are valid Org syntax (i.e. not an
1925 ;; incomplete keyword).
1929 ;; There's no ambiguity for other symbols or
1930 ;; empty lines: stop here.
1931 (looking-at "[ \t]*\\(?:[^:#]\\|$\\)")
1932 ;; Stop at valid fixed-width areas.
1933 (looking-at "[ \t]*:\\(?: \\|$\\)")
1935 (and (looking-at org-drawer-regexp
)
1938 "^[ \t]*:END:[ \t]*$" limit t
)))
1939 ;; Stop at valid comments.
1940 (looking-at "[ \t]*#\\(?: \\|$\\)")
1941 ;; Stop at valid dynamic blocks.
1942 (and (looking-at org-dblock-start-re
)
1945 "^[ \t]*#\\+END:?[ \t]*$" limit t
)))
1946 ;; Stop at valid blocks.
1948 "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
1951 (format "^[ \t]*#\\+END_%s[ \t]*$"
1954 ;; Stop at valid latex environments.
1956 "^[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}[ \t]*$")
1959 (format "^[ \t]*\\\\end{%s}[ \t]*$"
1962 ;; Stop at valid keywords.
1963 (looking-at "[ \t]*#\\+\\S-+:")
1964 ;; Skip everything else.
1968 (re-search-forward org-element-paragraph-separate
1970 (beginning-of-line)))
1971 (if (= (point) limit
) limit
1972 (goto-char (line-beginning-position)))))
1973 (contents-end (progn (skip-chars-backward " \r\t\n" contents-begin
)
1976 (end (progn (skip-chars-forward " \r\t\n" limit
)
1977 (skip-chars-backward " \t")
1978 (if (bolp) (point) (line-end-position)))))
1983 :contents-begin contents-begin
1984 :contents-end contents-end
1985 :post-blank
(count-lines before-blank end
))
1986 (cdr affiliated
))))))
1988 (defun org-element-paragraph-interpreter (paragraph contents
)
1989 "Interpret PARAGRAPH element as Org syntax.
1990 CONTENTS is the contents of the element."
1996 (defun org-element-planning-parser (limit)
1999 LIMIT bounds the search.
2001 Return a list whose CAR is `planning' and CDR is a plist
2002 containing `:closed', `:deadline', `:scheduled', `:begin', `:end'
2003 and `:post-blank' keywords."
2005 (let* ((case-fold-search nil
)
2007 (post-blank (let ((before-blank (progn (forward-line) (point))))
2008 (skip-chars-forward " \r\t\n" limit
)
2009 (skip-chars-backward " \t")
2010 (unless (bolp) (end-of-line))
2011 (count-lines before-blank
(point))))
2013 closed deadline scheduled
)
2015 (while (re-search-forward org-keyword-time-not-clock-regexp
2016 (line-end-position) t
)
2017 (goto-char (match-end 1))
2018 (org-skip-whitespace)
2019 (let ((time (buffer-substring-no-properties
2020 (1+ (point)) (1- (match-end 0))))
2021 (keyword (match-string 1)))
2022 (cond ((equal keyword org-closed-string
) (setq closed time
))
2023 ((equal keyword org-deadline-string
) (setq deadline time
))
2024 (t (setq scheduled time
)))))
2026 (list :closed closed
2028 :scheduled scheduled
2031 :post-blank post-blank
)))))
2033 (defun org-element-planning-interpreter (planning contents
)
2034 "Interpret PLANNING element as Org syntax.
2039 (list (let ((closed (org-element-property :closed planning
)))
2040 (when closed
(concat org-closed-string
" [" closed
"]")))
2041 (let ((deadline (org-element-property :deadline planning
)))
2042 (when deadline
(concat org-deadline-string
" <" deadline
">")))
2043 (let ((scheduled (org-element-property :scheduled planning
)))
2045 (concat org-scheduled-string
" <" scheduled
">")))))
2051 (defun org-element-quote-section-parser (limit)
2052 "Parse a quote section.
2054 LIMIT bounds the search.
2056 Return a list whose CAR is `quote-section' and CDR is a plist
2057 containing `:begin', `:end', `:value' and `:post-blank' keywords.
2059 Assume point is at beginning of the section."
2061 (let* ((begin (point))
2062 (end (progn (org-with-limited-levels (outline-next-heading))
2064 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
2067 (value (buffer-substring-no-properties begin pos-before-blank
)))
2068 (list 'quote-section
2072 :post-blank
(count-lines pos-before-blank end
))))))
2074 (defun org-element-quote-section-interpreter (quote-section contents
)
2075 "Interpret QUOTE-SECTION element as Org syntax.
2077 (org-element-property :value quote-section
))
2082 (defun org-element-src-block-parser (limit affiliated
)
2085 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2086 the buffer position at the beginning of the first affiliated
2087 keyword and CDR is a plist of affiliated keywords along with
2090 Return a list whose CAR is `src-block' and CDR is a plist
2091 containing `:language', `:switches', `:parameters', `:begin',
2092 `:end', `:hiddenp', `:number-lines', `:retain-labels',
2093 `:use-labels', `:label-fmt', `:preserve-indent', `:value' and
2094 `:post-blank' keywords.
2096 Assume point is at the beginning of the block."
2097 (let ((case-fold-search t
))
2098 (if (not (save-excursion (re-search-forward "^[ \t]*#\\+END_SRC[ \t]*$"
2100 ;; Incomplete block: parse it as a paragraph.
2101 (org-element-paragraph-parser limit affiliated
)
2102 (let ((contents-end (match-beginning 0)))
2104 (let* ((begin (car affiliated
))
2105 ;; Get language as a string.
2109 (concat "^[ \t]*#\\+BEGIN_SRC"
2110 "\\(?: +\\(\\S-+\\)\\)?"
2111 "\\(\\(?: +\\(?:-l \".*?\"\\|[-+][A-Za-z]\\)\\)+\\)?"
2113 (org-match-string-no-properties 1)))
2115 (switches (org-match-string-no-properties 2))
2117 (parameters (org-match-string-no-properties 3))
2118 ;; Switches analysis
2119 (number-lines (cond ((not switches
) nil
)
2120 ((string-match "-n\\>" switches
) 'new
)
2121 ((string-match "+n\\>" switches
) 'continued
)))
2122 (preserve-indent (and switches
(string-match "-i\\>" switches
)))
2123 (label-fmt (and switches
2124 (string-match "-l +\"\\([^\"\n]+\\)\"" switches
)
2125 (match-string 1 switches
)))
2126 ;; Should labels be retained in (or stripped from)
2130 (not (string-match "-r\\>" switches
))
2131 (and number-lines
(string-match "-k\\>" switches
))))
2132 ;; What should code-references use - labels or
2136 (and retain-labels
(not (string-match "-k\\>" switches
)))))
2137 ;; Get visibility status.
2138 (hidden (progn (forward-line) (org-invisible-p2)))
2140 (value (org-unescape-code-in-string
2141 (buffer-substring-no-properties (point) contents-end
)))
2142 (pos-before-blank (progn (goto-char contents-end
)
2145 ;; Get position after ending blank lines.
2146 (end (progn (skip-chars-forward " \r\t\n" limit
)
2147 (skip-chars-backward " \t")
2148 (if (bolp) (point) (line-end-position)))))
2151 (list :language language
2152 :switches
(and (org-string-nw-p switches
)
2153 (org-trim switches
))
2154 :parameters
(and (org-string-nw-p parameters
)
2155 (org-trim parameters
))
2158 :number-lines number-lines
2159 :preserve-indent preserve-indent
2160 :retain-labels retain-labels
2161 :use-labels use-labels
2162 :label-fmt label-fmt
2165 :post-blank
(count-lines pos-before-blank end
))
2166 (cdr affiliated
)))))))))
2168 (defun org-element-src-block-interpreter (src-block contents
)
2169 "Interpret SRC-BLOCK element as Org syntax.
2171 (let ((lang (org-element-property :language src-block
))
2172 (switches (org-element-property :switches src-block
))
2173 (params (org-element-property :parameters src-block
))
2174 (value (let ((val (org-element-property :value src-block
)))
2176 (org-src-preserve-indentation val
)
2177 ((zerop org-edit-src-content-indentation
)
2178 (org-remove-indentation val
))
2180 (let ((ind (make-string
2181 org-edit-src-content-indentation
32)))
2182 (replace-regexp-in-string
2183 "\\(^\\)[ \t]*\\S-" ind
2184 (org-remove-indentation val
) nil nil
1)))))))
2185 (concat (format "#+BEGIN_SRC%s\n"
2186 (concat (and lang
(concat " " lang
))
2187 (and switches
(concat " " switches
))
2188 (and params
(concat " " params
))))
2189 (org-escape-code-in-string value
)
2195 (defun org-element-table-parser (limit affiliated
)
2196 "Parse a table at point.
2198 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2199 the buffer position at the beginning of the first affiliated
2200 keyword and CDR is a plist of affiliated keywords along with
2203 Return a list whose CAR is `table' and CDR is a plist containing
2204 `:begin', `:end', `:tblfm', `:type', `:contents-begin',
2205 `:contents-end', `:value' and `:post-blank' keywords.
2207 Assume point is at the beginning of the table."
2209 (let* ((case-fold-search t
)
2210 (table-begin (point))
2211 (type (if (org-at-table.el-p
) 'table.el
'org
))
2212 (begin (car affiliated
))
2214 (if (re-search-forward org-table-any-border-regexp limit
'm
)
2215 (goto-char (match-beginning 0))
2218 (while (looking-at "[ \t]*#\\+TBLFM: +\\(.*\\)[ \t]*$")
2219 (push (org-match-string-no-properties 1) acc
)
2222 (pos-before-blank (point))
2223 (end (progn (skip-chars-forward " \r\t\n" limit
)
2224 (skip-chars-backward " \t")
2225 (if (bolp) (point) (line-end-position)))))
2232 ;; Only `org' tables have contents. `table.el' tables
2233 ;; use a `:value' property to store raw table as
2235 :contents-begin
(and (eq type
'org
) table-begin
)
2236 :contents-end
(and (eq type
'org
) table-end
)
2237 :value
(and (eq type
'table.el
)
2238 (buffer-substring-no-properties
2239 table-begin table-end
))
2240 :post-blank
(count-lines pos-before-blank end
))
2241 (cdr affiliated
))))))
2243 (defun org-element-table-interpreter (table contents
)
2244 "Interpret TABLE element as Org syntax.
2246 (if (eq (org-element-property :type table
) 'table.el
)
2247 (org-remove-indentation (org-element-property :value table
))
2248 (concat (with-temp-buffer (insert contents
)
2251 (mapconcat (lambda (fm) (concat "#+TBLFM: " fm
))
2252 (reverse (org-element-property :tblfm table
))
2258 (defun org-element-table-row-parser (limit)
2259 "Parse table row at point.
2261 LIMIT bounds the search.
2263 Return a list whose CAR is `table-row' and CDR is a plist
2264 containing `:begin', `:end', `:contents-begin', `:contents-end',
2265 `:type' and `:post-blank' keywords."
2267 (let* ((type (if (looking-at "^[ \t]*|-") 'rule
'standard
))
2269 ;; A table rule has no contents. In that case, ensure
2270 ;; CONTENTS-BEGIN matches CONTENTS-END.
2271 (contents-begin (and (eq type
'standard
)
2272 (search-forward "|")
2274 (contents-end (and (eq type
'standard
)
2277 (skip-chars-backward " \t")
2279 (end (progn (forward-line) (point))))
2284 :contents-begin contents-begin
2285 :contents-end contents-end
2288 (defun org-element-table-row-interpreter (table-row contents
)
2289 "Interpret TABLE-ROW element as Org syntax.
2290 CONTENTS is the contents of the table row."
2291 (if (eq (org-element-property :type table-row
) 'rule
) "|-"
2292 (concat "| " contents
)))
2297 (defun org-element-verse-block-parser (limit affiliated
)
2298 "Parse a verse block.
2300 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2301 the buffer position at the beginning of the first affiliated
2302 keyword and CDR is a plist of affiliated keywords along with
2305 Return a list whose CAR is `verse-block' and CDR is a plist
2306 containing `:begin', `:end', `:contents-begin', `:contents-end',
2307 `:hiddenp' and `:post-blank' keywords.
2309 Assume point is at beginning of the block."
2310 (let ((case-fold-search t
))
2311 (if (not (save-excursion
2312 (re-search-forward "^[ \t]*#\\+END_VERSE[ \t]*$" limit t
)))
2313 ;; Incomplete block: parse it as a paragraph.
2314 (org-element-paragraph-parser limit affiliated
)
2315 (let ((contents-end (match-beginning 0)))
2317 (let* ((begin (car affiliated
))
2318 (hidden (progn (forward-line) (org-invisible-p2)))
2319 (contents-begin (point))
2320 (pos-before-blank (progn (goto-char contents-end
)
2323 (end (progn (skip-chars-forward " \r\t\n" limit
)
2324 (skip-chars-backward " \t")
2325 (if (bolp) (point) (line-end-position)))))
2330 :contents-begin contents-begin
2331 :contents-end contents-end
2333 :post-blank
(count-lines pos-before-blank end
))
2334 (cdr affiliated
)))))))))
2336 (defun org-element-verse-block-interpreter (verse-block contents
)
2337 "Interpret VERSE-BLOCK element as Org syntax.
2338 CONTENTS is verse block contents."
2339 (format "#+BEGIN_VERSE\n%s#+END_VERSE" contents
))
2345 ;; Unlike to elements, interstices can be found between objects.
2346 ;; That's why, along with the parser, successor functions are provided
2347 ;; for each object. Some objects share the same successor (i.e. `code'
2348 ;; and `verbatim' objects).
2350 ;; A successor must accept a single argument bounding the search. It
2351 ;; will return either a cons cell whose CAR is the object's type, as
2352 ;; a symbol, and CDR the position of its next occurrence, or nil.
2354 ;; Successors follow the naming convention:
2355 ;; org-element-NAME-successor, where NAME is the name of the
2356 ;; successor, as defined in `org-element-all-successors'.
2358 ;; Some object types (i.e. `italic') are recursive. Restrictions on
2359 ;; object types they can contain will be specified in
2360 ;; `org-element-object-restrictions'.
2362 ;; Adding a new type of object is simple. Implement a successor,
2363 ;; a parser, and an interpreter for it, all following the naming
2364 ;; convention. Register type in `org-element-all-objects' and
2365 ;; successor in `org-element-all-successors'. Maybe tweak
2366 ;; restrictions about it, and that's it.
2371 (defun org-element-bold-parser ()
2372 "Parse bold object at point.
2374 Return a list whose CAR is `bold' and CDR is a plist with
2375 `:begin', `:end', `:contents-begin' and `:contents-end' and
2376 `:post-blank' keywords.
2378 Assume point is at the first star marker."
2380 (unless (bolp) (backward-char 1))
2381 (looking-at org-emph-re
)
2382 (let ((begin (match-beginning 2))
2383 (contents-begin (match-beginning 4))
2384 (contents-end (match-end 4))
2385 (post-blank (progn (goto-char (match-end 2))
2386 (skip-chars-forward " \t")))
2391 :contents-begin contents-begin
2392 :contents-end contents-end
2393 :post-blank post-blank
)))))
2395 (defun org-element-bold-interpreter (bold contents
)
2396 "Interpret BOLD object as Org syntax.
2397 CONTENTS is the contents of the object."
2398 (format "*%s*" contents
))
2400 (defun org-element-text-markup-successor (limit)
2401 "Search for the next text-markup object.
2403 LIMIT bounds the search.
2405 Return value is a cons cell whose CAR is a symbol among `bold',
2406 `italic', `underline', `strike-through', `code' and `verbatim'
2407 and CDR is beginning position."
2409 (unless (bolp) (backward-char))
2410 (when (re-search-forward org-emph-re limit t
)
2411 (let ((marker (match-string 3)))
2413 ((equal marker
"*") 'bold
)
2414 ((equal marker
"/") 'italic
)
2415 ((equal marker
"_") 'underline
)
2416 ((equal marker
"+") 'strike-through
)
2417 ((equal marker
"~") 'code
)
2418 ((equal marker
"=") 'verbatim
)
2419 (t (error "Unknown marker at %d" (match-beginning 3))))
2420 (match-beginning 2))))))
2425 (defun org-element-code-parser ()
2426 "Parse code object at point.
2428 Return a list whose CAR is `code' and CDR is a plist with
2429 `:value', `:begin', `:end' and `:post-blank' keywords.
2431 Assume point is at the first tilde marker."
2433 (unless (bolp) (backward-char 1))
2434 (looking-at org-emph-re
)
2435 (let ((begin (match-beginning 2))
2436 (value (org-match-string-no-properties 4))
2437 (post-blank (progn (goto-char (match-end 2))
2438 (skip-chars-forward " \t")))
2444 :post-blank post-blank
)))))
2446 (defun org-element-code-interpreter (code contents
)
2447 "Interpret CODE object as Org syntax.
2449 (format "~%s~" (org-element-property :value code
)))
2454 (defun org-element-entity-parser ()
2455 "Parse entity at point.
2457 Return a list whose CAR is `entity' and CDR a plist with
2458 `:begin', `:end', `:latex', `:latex-math-p', `:html', `:latin1',
2459 `:utf-8', `:ascii', `:use-brackets-p' and `:post-blank' as
2462 Assume point is at the beginning of the entity."
2464 (looking-at "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)")
2465 (let* ((value (org-entity-get (match-string 1)))
2466 (begin (match-beginning 0))
2467 (bracketsp (string= (match-string 2) "{}"))
2468 (post-blank (progn (goto-char (match-end 1))
2469 (when bracketsp
(forward-char 2))
2470 (skip-chars-forward " \t")))
2473 (list :name
(car value
)
2474 :latex
(nth 1 value
)
2475 :latex-math-p
(nth 2 value
)
2477 :ascii
(nth 4 value
)
2478 :latin1
(nth 5 value
)
2479 :utf-8
(nth 6 value
)
2482 :use-brackets-p bracketsp
2483 :post-blank post-blank
)))))
2485 (defun org-element-entity-interpreter (entity contents
)
2486 "Interpret ENTITY object as Org syntax.
2489 (org-element-property :name entity
)
2490 (when (org-element-property :use-brackets-p entity
) "{}")))
2492 (defun org-element-latex-or-entity-successor (limit)
2493 "Search for the next latex-fragment or entity object.
2495 LIMIT bounds the search.
2497 Return value is a cons cell whose CAR is `entity' or
2498 `latex-fragment' and CDR is beginning position."
2500 (unless (bolp) (backward-char))
2502 (remove "begin" (plist-get org-format-latex-options
:matchers
)))
2503 ;; ENTITY-RE matches both LaTeX commands and Org entities.
2505 "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))
2506 (when (re-search-forward
2507 (concat (mapconcat (lambda (e) (nth 1 (assoc e org-latex-regexps
)))
2511 (goto-char (match-beginning 0))
2512 (if (looking-at entity-re
)
2513 ;; Determine if it's a real entity or a LaTeX command.
2514 (cons (if (org-entity-get (match-string 1)) 'entity
'latex-fragment
)
2515 (match-beginning 0))
2516 ;; No entity nor command: point is at a LaTeX fragment.
2517 ;; Determine its type to get the correct beginning position.
2518 (cons 'latex-fragment
2521 (when (looking-at (nth 1 (assoc e org-latex-regexps
)))
2524 (nth 2 (assoc e org-latex-regexps
))))))
2531 (defun org-element-export-snippet-parser ()
2532 "Parse export snippet at point.
2534 Return a list whose CAR is `export-snippet' and CDR a plist with
2535 `:begin', `:end', `:back-end', `:value' and `:post-blank' as
2538 Assume point is at the beginning of the snippet."
2540 (re-search-forward "@@\\([-A-Za-z0-9]+\\):" nil t
)
2541 (let* ((begin (match-beginning 0))
2542 (back-end (org-match-string-no-properties 1))
2543 (value (buffer-substring-no-properties
2545 (progn (re-search-forward "@@" nil t
) (match-beginning 0))))
2546 (post-blank (skip-chars-forward " \t"))
2548 (list 'export-snippet
2549 (list :back-end back-end
2553 :post-blank post-blank
)))))
2555 (defun org-element-export-snippet-interpreter (export-snippet contents
)
2556 "Interpret EXPORT-SNIPPET object as Org syntax.
2559 (org-element-property :back-end export-snippet
)
2560 (org-element-property :value export-snippet
)))
2562 (defun org-element-export-snippet-successor (limit)
2563 "Search for the next export-snippet object.
2565 LIMIT bounds the search.
2567 Return value is a cons cell whose CAR is `export-snippet' and CDR
2568 its beginning position."
2571 (when (and (re-search-forward "@@[-A-Za-z0-9]+:" limit t
)
2572 (setq beg
(match-beginning 0))
2573 (search-forward "@@" limit t
))
2574 (cons 'export-snippet beg
)))))
2577 ;;;; Footnote Reference
2579 (defun org-element-footnote-reference-parser ()
2580 "Parse footnote reference at point.
2582 Return a list whose CAR is `footnote-reference' and CDR a plist
2583 with `:label', `:type', `:inline-definition', `:begin', `:end'
2584 and `:post-blank' as keywords."
2586 (looking-at org-footnote-re
)
2587 (let* ((begin (point))
2588 (label (or (org-match-string-no-properties 2)
2589 (org-match-string-no-properties 3)
2590 (and (match-string 1)
2591 (concat "fn:" (org-match-string-no-properties 1)))))
2592 (type (if (or (not label
) (match-string 1)) 'inline
'standard
))
2593 (inner-begin (match-end 0))
2597 (while (and (> count
0) (re-search-forward "[][]" nil t
))
2598 (if (equal (match-string 0) "[") (incf count
) (decf count
)))
2600 (post-blank (progn (goto-char (1+ inner-end
))
2601 (skip-chars-forward " \t")))
2604 (list 'footnote-reference
2609 :post-blank post-blank
))))
2610 (org-element-put-property
2611 footnote-reference
:inline-definition
2612 (and (eq type
'inline
)
2613 (org-element-parse-secondary-string
2614 (buffer-substring inner-begin inner-end
)
2615 (org-element-restriction 'footnote-reference
)
2616 footnote-reference
))))))
2618 (defun org-element-footnote-reference-interpreter (footnote-reference contents
)
2619 "Interpret FOOTNOTE-REFERENCE object as Org syntax.
2621 (let ((label (or (org-element-property :label footnote-reference
) "fn:"))
2624 (org-element-property :inline-definition footnote-reference
)))
2625 (if (not inline-def
) ""
2626 (concat ":" (org-element-interpret-data inline-def
))))))
2627 (format "[%s]" (concat label def
))))
2629 (defun org-element-footnote-reference-successor (limit)
2630 "Search for the next footnote-reference object.
2632 LIMIT bounds the search.
2634 Return value is a cons cell whose CAR is `footnote-reference' and
2635 CDR is beginning position."
2638 (while (re-search-forward org-footnote-re limit t
)
2640 (let ((beg (match-beginning 0))
2643 (while (re-search-forward "[][]" limit t
)
2644 (if (equal (match-string 0) "[") (incf count
) (decf count
))
2646 (throw 'exit
(cons 'footnote-reference beg
))))))))))
2649 ;;;; Inline Babel Call
2651 (defun org-element-inline-babel-call-parser ()
2652 "Parse inline babel call at point.
2654 Return a list whose CAR is `inline-babel-call' and CDR a plist
2655 with `:begin', `:end', `:info' and `:post-blank' as keywords.
2657 Assume point is at the beginning of the babel call."
2659 (unless (bolp) (backward-char))
2660 (looking-at org-babel-inline-lob-one-liner-regexp
)
2661 (let ((info (save-match-data (org-babel-lob-get-info)))
2662 (begin (match-end 1))
2663 (post-blank (progn (goto-char (match-end 0))
2664 (skip-chars-forward " \t")))
2666 (list 'inline-babel-call
2670 :post-blank post-blank
)))))
2672 (defun org-element-inline-babel-call-interpreter (inline-babel-call contents
)
2673 "Interpret INLINE-BABEL-CALL object as Org syntax.
2675 (let* ((babel-info (org-element-property :info inline-babel-call
))
2676 (main-source (car babel-info
))
2677 (post-options (nth 1 babel-info
)))
2679 (if (string-match "\\[\\(\\[.*?\\]\\)\\]" main-source
)
2680 ;; Remove redundant square brackets.
2682 (match-string 1 main-source
) nil nil main-source
)
2684 (and post-options
(format "[%s]" post-options
)))))
2686 (defun org-element-inline-babel-call-successor (limit)
2687 "Search for the next inline-babel-call object.
2689 LIMIT bounds the search.
2691 Return value is a cons cell whose CAR is `inline-babel-call' and
2692 CDR is beginning position."
2694 ;; Use a simplified version of
2695 ;; `org-babel-inline-lob-one-liner-regexp'.
2696 (when (re-search-forward
2697 "call_\\([^()\n]+?\\)\\(?:\\[.*?\\]\\)?([^\n]*?)\\(\\[.*?\\]\\)?"
2699 (cons 'inline-babel-call
(match-beginning 0)))))
2702 ;;;; Inline Src Block
2704 (defun org-element-inline-src-block-parser ()
2705 "Parse inline source block at point.
2707 LIMIT bounds the search.
2709 Return a list whose CAR is `inline-src-block' and CDR a plist
2710 with `:begin', `:end', `:language', `:value', `:parameters' and
2711 `:post-blank' as keywords.
2713 Assume point is at the beginning of the inline src block."
2715 (unless (bolp) (backward-char))
2716 (looking-at org-babel-inline-src-block-regexp
)
2717 (let ((begin (match-beginning 1))
2718 (language (org-match-string-no-properties 2))
2719 (parameters (org-match-string-no-properties 4))
2720 (value (org-match-string-no-properties 5))
2721 (post-blank (progn (goto-char (match-end 0))
2722 (skip-chars-forward " \t")))
2724 (list 'inline-src-block
2725 (list :language language
2727 :parameters parameters
2730 :post-blank post-blank
)))))
2732 (defun org-element-inline-src-block-interpreter (inline-src-block contents
)
2733 "Interpret INLINE-SRC-BLOCK object as Org syntax.
2735 (let ((language (org-element-property :language inline-src-block
))
2736 (arguments (org-element-property :parameters inline-src-block
))
2737 (body (org-element-property :value inline-src-block
)))
2738 (format "src_%s%s{%s}"
2740 (if arguments
(format "[%s]" arguments
) "")
2743 (defun org-element-inline-src-block-successor (limit)
2744 "Search for the next inline-babel-call element.
2746 LIMIT bounds the search.
2748 Return value is a cons cell whose CAR is `inline-babel-call' and
2749 CDR is beginning position."
2751 (unless (bolp) (backward-char))
2752 (when (re-search-forward org-babel-inline-src-block-regexp limit t
)
2753 (cons 'inline-src-block
(match-beginning 1)))))
2757 (defun org-element-italic-parser ()
2758 "Parse italic object at point.
2760 Return a list whose CAR is `italic' and CDR is a plist with
2761 `:begin', `:end', `:contents-begin' and `:contents-end' and
2762 `:post-blank' keywords.
2764 Assume point is at the first slash marker."
2766 (unless (bolp) (backward-char 1))
2767 (looking-at org-emph-re
)
2768 (let ((begin (match-beginning 2))
2769 (contents-begin (match-beginning 4))
2770 (contents-end (match-end 4))
2771 (post-blank (progn (goto-char (match-end 2))
2772 (skip-chars-forward " \t")))
2777 :contents-begin contents-begin
2778 :contents-end contents-end
2779 :post-blank post-blank
)))))
2781 (defun org-element-italic-interpreter (italic contents
)
2782 "Interpret ITALIC object as Org syntax.
2783 CONTENTS is the contents of the object."
2784 (format "/%s/" contents
))
2789 (defun org-element-latex-fragment-parser ()
2790 "Parse latex fragment at point.
2792 Return a list whose CAR is `latex-fragment' and CDR a plist with
2793 `:value', `:begin', `:end', and `:post-blank' as keywords.
2795 Assume point is at the beginning of the latex fragment."
2797 (let* ((begin (point))
2801 (let ((latex-regexp (nth 1 (assoc e org-latex-regexps
))))
2802 (when (or (looking-at latex-regexp
)
2806 (looking-at latex-regexp
))))
2807 (throw 'exit
(nth 2 (assoc e org-latex-regexps
))))))
2808 (plist-get org-format-latex-options
:matchers
))
2809 ;; None found: it's a macro.
2810 (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")
2812 (value (match-string-no-properties substring-match
))
2813 (post-blank (progn (goto-char (match-end substring-match
))
2814 (skip-chars-forward " \t")))
2816 (list 'latex-fragment
2820 :post-blank post-blank
)))))
2822 (defun org-element-latex-fragment-interpreter (latex-fragment contents
)
2823 "Interpret LATEX-FRAGMENT object as Org syntax.
2825 (org-element-property :value latex-fragment
))
2829 (defun org-element-line-break-parser ()
2830 "Parse line break at point.
2832 Return a list whose CAR is `line-break', and CDR a plist with
2833 `:begin', `:end' and `:post-blank' keywords.
2835 Assume point is at the beginning of the line break."
2836 (list 'line-break
(list :begin
(point) :end
(point-at-eol) :post-blank
0)))
2838 (defun org-element-line-break-interpreter (line-break contents
)
2839 "Interpret LINE-BREAK object as Org syntax.
2843 (defun org-element-line-break-successor (limit)
2844 "Search for the next line-break object.
2846 LIMIT bounds the search.
2848 Return value is a cons cell whose CAR is `line-break' and CDR is
2849 beginning position."
2851 (let ((beg (and (re-search-forward "[^\\\\]\\(\\\\\\\\\\)[ \t]*$" limit t
)
2852 (goto-char (match-beginning 1)))))
2853 ;; A line break can only happen on a non-empty line.
2854 (when (and beg
(re-search-backward "\\S-" (point-at-bol) t
))
2855 (cons 'line-break beg
)))))
2860 (defun org-element-link-parser ()
2861 "Parse link at point.
2863 Return a list whose CAR is `link' and CDR a plist with `:type',
2864 `:path', `:raw-link', `:application', `:search-option', `:begin',
2865 `:end', `:contents-begin', `:contents-end' and `:post-blank' as
2868 Assume point is at the beginning of the link."
2870 (let ((begin (point))
2871 end contents-begin contents-end link-end post-blank path type
2872 raw-link link search-option application
)
2874 ;; Type 1: Text targeted from a radio target.
2875 ((and org-target-link-regexp
(looking-at org-target-link-regexp
))
2877 link-end
(match-end 0)
2878 path
(org-match-string-no-properties 0)))
2879 ;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]]
2880 ((looking-at org-bracket-link-regexp
)
2881 (setq contents-begin
(match-beginning 3)
2882 contents-end
(match-end 3)
2883 link-end
(match-end 0)
2884 ;; RAW-LINK is the original link.
2885 raw-link
(org-match-string-no-properties 1)
2886 link
(org-translate-link
2887 (org-link-expand-abbrev
2888 (org-link-unescape raw-link
))))
2889 ;; Determine TYPE of link and set PATH accordingly.
2892 ((or (file-name-absolute-p link
) (string-match "^\\.\\.?/" link
))
2893 (setq type
"file" path link
))
2894 ;; Explicit type (http, irc, bbdb...). See `org-link-types'.
2895 ((string-match org-link-re-with-space3 link
)
2896 (setq type
(match-string 1 link
) path
(match-string 2 link
)))
2897 ;; Id type: PATH is the id.
2898 ((string-match "^id:\\([-a-f0-9]+\\)" link
)
2899 (setq type
"id" path
(match-string 1 link
)))
2900 ;; Code-ref type: PATH is the name of the reference.
2901 ((string-match "^(\\(.*\\))$" link
)
2902 (setq type
"coderef" path
(match-string 1 link
)))
2903 ;; Custom-id type: PATH is the name of the custom id.
2904 ((= (aref link
0) ?
#)
2905 (setq type
"custom-id" path
(substring link
1)))
2906 ;; Fuzzy type: Internal link either matches a target, an
2907 ;; headline name or nothing. PATH is the target or
2909 (t (setq type
"fuzzy" path link
))))
2910 ;; Type 3: Plain link, i.e. http://orgmode.org
2911 ((looking-at org-plain-link-re
)
2912 (setq raw-link
(org-match-string-no-properties 0)
2913 type
(org-match-string-no-properties 1)
2914 path
(org-match-string-no-properties 2)
2915 link-end
(match-end 0)))
2916 ;; Type 4: Angular link, i.e. <http://orgmode.org>
2917 ((looking-at org-angle-link-re
)
2918 (setq raw-link
(buffer-substring-no-properties
2919 (match-beginning 1) (match-end 2))
2920 type
(org-match-string-no-properties 1)
2921 path
(org-match-string-no-properties 2)
2922 link-end
(match-end 0))))
2923 ;; In any case, deduce end point after trailing white space from
2924 ;; LINK-END variable.
2925 (setq post-blank
(progn (goto-char link-end
) (skip-chars-forward " \t"))
2927 ;; Extract search option and opening application out of
2928 ;; "file"-type links.
2929 (when (member type org-element-link-type-is-file
)
2931 (cond ((string-match "^file\\+\\(.*\\)$" type
)
2932 (setq application
(match-string 1 type
)))
2933 ((not (string-match "^file" type
))
2934 (setq application type
)))
2935 ;; Extract search option from PATH.
2936 (when (string-match "::\\(.*\\)$" path
)
2937 (setq search-option
(match-string 1 path
)
2938 path
(replace-match "" nil nil path
)))
2939 ;; Make sure TYPE always report "file".
2944 :raw-link
(or raw-link path
)
2945 :application application
2946 :search-option search-option
2949 :contents-begin contents-begin
2950 :contents-end contents-end
2951 :post-blank post-blank
)))))
2953 (defun org-element-link-interpreter (link contents
)
2954 "Interpret LINK object as Org syntax.
2955 CONTENTS is the contents of the object, or nil."
2956 (let ((type (org-element-property :type link
))
2957 (raw-link (org-element-property :raw-link link
)))
2958 (if (string= type
"radio") raw-link
2961 (if contents
(format "[%s]" contents
) "")))))
2963 (defun org-element-link-successor (limit)
2964 "Search for the next link object.
2966 LIMIT bounds the search.
2968 Return value is a cons cell whose CAR is `link' and CDR is
2969 beginning position."
2972 (if (not org-target-link-regexp
) org-any-link-re
2973 (concat org-any-link-re
"\\|" org-target-link-regexp
))))
2974 (when (re-search-forward link-regexp limit t
)
2975 (cons 'link
(match-beginning 0))))))
2980 (defun org-element-macro-parser ()
2981 "Parse macro at point.
2983 Return a list whose CAR is `macro' and CDR a plist with `:key',
2984 `:args', `:begin', `:end', `:value' and `:post-blank' as
2987 Assume point is at the macro."
2989 (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
2990 (let ((begin (point))
2991 (key (downcase (org-match-string-no-properties 1)))
2992 (value (org-match-string-no-properties 0))
2993 (post-blank (progn (goto-char (match-end 0))
2994 (skip-chars-forward " \t")))
2996 (args (let ((args (org-match-string-no-properties 3)) args2
)
2998 (setq args
(org-split-string args
","))
3000 (while (string-match "\\\\\\'" (car args
))
3001 ;; Repair bad splits.
3002 (setcar (cdr args
) (concat (substring (car args
) 0 -
1)
3005 (push (pop args
) args2
))
3006 (mapcar 'org-trim
(nreverse args2
))))))
3013 :post-blank post-blank
)))))
3015 (defun org-element-macro-interpreter (macro contents
)
3016 "Interpret MACRO object as Org syntax.
3018 (org-element-property :value macro
))
3020 (defun org-element-macro-successor (limit)
3021 "Search for the next macro object.
3023 LIMIT bounds the search.
3025 Return value is cons cell whose CAR is `macro' and CDR is
3026 beginning position."
3028 (when (re-search-forward
3029 "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
3031 (cons 'macro
(match-beginning 0)))))
3036 (defun org-element-radio-target-parser ()
3037 "Parse radio target at point.
3039 Return a list whose CAR is `radio-target' and CDR a plist with
3040 `:begin', `:end', `:contents-begin', `:contents-end', `:value'
3041 and `:post-blank' as keywords.
3043 Assume point is at the radio target."
3045 (looking-at org-radio-target-regexp
)
3046 (let ((begin (point))
3047 (contents-begin (match-beginning 1))
3048 (contents-end (match-end 1))
3049 (value (org-match-string-no-properties 1))
3050 (post-blank (progn (goto-char (match-end 0))
3051 (skip-chars-forward " \t")))
3056 :contents-begin contents-begin
3057 :contents-end contents-end
3058 :post-blank post-blank
3061 (defun org-element-radio-target-interpreter (target contents
)
3062 "Interpret TARGET object as Org syntax.
3063 CONTENTS is the contents of the object."
3064 (concat "<<<" contents
">>>"))
3066 (defun org-element-radio-target-successor (limit)
3067 "Search for the next radio-target object.
3069 LIMIT bounds the search.
3071 Return value is a cons cell whose CAR is `radio-target' and CDR
3072 is beginning position."
3074 (when (re-search-forward org-radio-target-regexp limit t
)
3075 (cons 'radio-target
(match-beginning 0)))))
3078 ;;;; Statistics Cookie
3080 (defun org-element-statistics-cookie-parser ()
3081 "Parse statistics cookie at point.
3083 Return a list whose CAR is `statistics-cookie', and CDR a plist
3084 with `:begin', `:end', `:value' and `:post-blank' keywords.
3086 Assume point is at the beginning of the statistics-cookie."
3088 (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
3089 (let* ((begin (point))
3090 (value (buffer-substring-no-properties
3091 (match-beginning 0) (match-end 0)))
3092 (post-blank (progn (goto-char (match-end 0))
3093 (skip-chars-forward " \t")))
3095 (list 'statistics-cookie
3099 :post-blank post-blank
)))))
3101 (defun org-element-statistics-cookie-interpreter (statistics-cookie contents
)
3102 "Interpret STATISTICS-COOKIE object as Org syntax.
3104 (org-element-property :value statistics-cookie
))
3106 (defun org-element-statistics-cookie-successor (limit)
3107 "Search for the next statistics cookie object.
3109 LIMIT bounds the search.
3111 Return value is a cons cell whose CAR is `statistics-cookie' and
3112 CDR is beginning position."
3114 (when (re-search-forward "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]" limit t
)
3115 (cons 'statistics-cookie
(match-beginning 0)))))
3120 (defun org-element-strike-through-parser ()
3121 "Parse strike-through object at point.
3123 Return a list whose CAR is `strike-through' and CDR is a plist
3124 with `:begin', `:end', `:contents-begin' and `:contents-end' and
3125 `:post-blank' keywords.
3127 Assume point is at the first plus sign marker."
3129 (unless (bolp) (backward-char 1))
3130 (looking-at org-emph-re
)
3131 (let ((begin (match-beginning 2))
3132 (contents-begin (match-beginning 4))
3133 (contents-end (match-end 4))
3134 (post-blank (progn (goto-char (match-end 2))
3135 (skip-chars-forward " \t")))
3137 (list 'strike-through
3140 :contents-begin contents-begin
3141 :contents-end contents-end
3142 :post-blank post-blank
)))))
3144 (defun org-element-strike-through-interpreter (strike-through contents
)
3145 "Interpret STRIKE-THROUGH object as Org syntax.
3146 CONTENTS is the contents of the object."
3147 (format "+%s+" contents
))
3152 (defun org-element-subscript-parser ()
3153 "Parse subscript at point.
3155 Return a list whose CAR is `subscript' and CDR a plist with
3156 `:begin', `:end', `:contents-begin', `:contents-end',
3157 `:use-brackets-p' and `:post-blank' as keywords.
3159 Assume point is at the underscore."
3161 (unless (bolp) (backward-char))
3162 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp
)
3164 (not (looking-at org-match-substring-regexp
))))
3165 (begin (match-beginning 2))
3166 (contents-begin (or (match-beginning 5)
3167 (match-beginning 3)))
3168 (contents-end (or (match-end 5) (match-end 3)))
3169 (post-blank (progn (goto-char (match-end 0))
3170 (skip-chars-forward " \t")))
3175 :use-brackets-p bracketsp
3176 :contents-begin contents-begin
3177 :contents-end contents-end
3178 :post-blank post-blank
)))))
3180 (defun org-element-subscript-interpreter (subscript contents
)
3181 "Interpret SUBSCRIPT object as Org syntax.
3182 CONTENTS is the contents of the object."
3184 (if (org-element-property :use-brackets-p subscript
) "_{%s}" "_%s")
3187 (defun org-element-sub/superscript-successor
(limit)
3188 "Search for the next sub/superscript object.
3190 LIMIT bounds the search.
3192 Return value is a cons cell whose CAR is either `subscript' or
3193 `superscript' and CDR is beginning position."
3195 (unless (bolp) (backward-char))
3196 (when (re-search-forward org-match-substring-regexp limit t
)
3197 (cons (if (string= (match-string 2) "_") 'subscript
'superscript
)
3198 (match-beginning 2)))))
3203 (defun org-element-superscript-parser ()
3204 "Parse superscript at point.
3206 Return a list whose CAR is `superscript' and CDR a plist with
3207 `:begin', `:end', `:contents-begin', `:contents-end',
3208 `:use-brackets-p' and `:post-blank' as keywords.
3210 Assume point is at the caret."
3212 (unless (bolp) (backward-char))
3213 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp
) t
3214 (not (looking-at org-match-substring-regexp
))))
3215 (begin (match-beginning 2))
3216 (contents-begin (or (match-beginning 5)
3217 (match-beginning 3)))
3218 (contents-end (or (match-end 5) (match-end 3)))
3219 (post-blank (progn (goto-char (match-end 0))
3220 (skip-chars-forward " \t")))
3225 :use-brackets-p bracketsp
3226 :contents-begin contents-begin
3227 :contents-end contents-end
3228 :post-blank post-blank
)))))
3230 (defun org-element-superscript-interpreter (superscript contents
)
3231 "Interpret SUPERSCRIPT object as Org syntax.
3232 CONTENTS is the contents of the object."
3234 (if (org-element-property :use-brackets-p superscript
) "^{%s}" "^%s")
3240 (defun org-element-table-cell-parser ()
3241 "Parse table cell at point.
3243 Return a list whose CAR is `table-cell' and CDR is a plist
3244 containing `:begin', `:end', `:contents-begin', `:contents-end'
3245 and `:post-blank' keywords."
3246 (looking-at "[ \t]*\\(.*?\\)[ \t]*|")
3247 (let* ((begin (match-beginning 0))
3249 (contents-begin (match-beginning 1))
3250 (contents-end (match-end 1)))
3254 :contents-begin contents-begin
3255 :contents-end contents-end
3258 (defun org-element-table-cell-interpreter (table-cell contents
)
3259 "Interpret TABLE-CELL element as Org syntax.
3260 CONTENTS is the contents of the cell, or nil."
3261 (concat " " contents
" |"))
3263 (defun org-element-table-cell-successor (limit)
3264 "Search for the next table-cell object.
3266 LIMIT bounds the search.
3268 Return value is a cons cell whose CAR is `table-cell' and CDR is
3269 beginning position."
3270 (when (looking-at "[ \t]*.*?[ \t]+|") (cons 'table-cell
(point))))
3275 (defun org-element-target-parser ()
3276 "Parse target at point.
3278 Return a list whose CAR is `target' and CDR a plist with
3279 `:begin', `:end', `:value' and `:post-blank' as keywords.
3281 Assume point is at the target."
3283 (looking-at org-target-regexp
)
3284 (let ((begin (point))
3285 (value (org-match-string-no-properties 1))
3286 (post-blank (progn (goto-char (match-end 0))
3287 (skip-chars-forward " \t")))
3293 :post-blank post-blank
)))))
3295 (defun org-element-target-interpreter (target contents
)
3296 "Interpret TARGET object as Org syntax.
3298 (format "<<%s>>" (org-element-property :value target
)))
3300 (defun org-element-target-successor (limit)
3301 "Search for the next target object.
3303 LIMIT bounds the search.
3305 Return value is a cons cell whose CAR is `target' and CDR is
3306 beginning position."
3308 (when (re-search-forward org-target-regexp limit t
)
3309 (cons 'target
(match-beginning 0)))))
3314 (defun org-element-timestamp-parser ()
3315 "Parse time stamp at point.
3317 Return a list whose CAR is `timestamp', and CDR a plist with
3318 `:type', `:begin', `:end', `:value' and `:post-blank' keywords.
3320 Assume point is at the beginning of the timestamp."
3322 (let* ((begin (point))
3323 (activep (eq (char-after) ?
<))
3326 (looking-at "[<[]\\(\\(%%\\)?.*?\\)[]>]\\(?:--[<[]\\(.*?\\)[]>]\\)?")
3327 (match-string-no-properties 1)))
3328 (range-end (match-string-no-properties 3))
3329 (type (cond ((match-string 2) 'diary
)
3330 ((and activep range-end
) 'active-range
)
3332 (range-end 'inactive-range
)
3334 (post-blank (progn (goto-char (match-end 0))
3335 (skip-chars-forward " \t")))
3340 :range-end range-end
3343 :post-blank post-blank
)))))
3345 (defun org-element-timestamp-interpreter (timestamp contents
)
3346 "Interpret TIMESTAMP object as Org syntax.
3348 (let ((type (org-element-property :type timestamp
) ))
3350 (format (if (memq type
'(inactive inactive-range
)) "[%s]" "<%s>")
3351 (org-element-property :value timestamp
))
3352 (let ((range-end (org-element-property :range-end timestamp
)))
3355 (format (if (eq type
'inactive-range
) "[%s]" "<%s>")
3358 (defun org-element-timestamp-successor (limit)
3359 "Search for the next timestamp object.
3361 LIMIT bounds the search.
3363 Return value is a cons cell whose CAR is `timestamp' and CDR is
3364 beginning position."
3366 (when (re-search-forward
3367 (concat org-ts-regexp-both
3369 "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
3371 "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
3373 (cons 'timestamp
(match-beginning 0)))))
3378 (defun org-element-underline-parser ()
3379 "Parse underline object at point.
3381 Return a list whose CAR is `underline' and CDR is a plist with
3382 `:begin', `:end', `:contents-begin' and `:contents-end' and
3383 `:post-blank' keywords.
3385 Assume point is at the first underscore marker."
3387 (unless (bolp) (backward-char 1))
3388 (looking-at org-emph-re
)
3389 (let ((begin (match-beginning 2))
3390 (contents-begin (match-beginning 4))
3391 (contents-end (match-end 4))
3392 (post-blank (progn (goto-char (match-end 2))
3393 (skip-chars-forward " \t")))
3398 :contents-begin contents-begin
3399 :contents-end contents-end
3400 :post-blank post-blank
)))))
3402 (defun org-element-underline-interpreter (underline contents
)
3403 "Interpret UNDERLINE object as Org syntax.
3404 CONTENTS is the contents of the object."
3405 (format "_%s_" contents
))
3410 (defun org-element-verbatim-parser ()
3411 "Parse verbatim object at point.
3413 Return a list whose CAR is `verbatim' and CDR is a plist with
3414 `:value', `:begin', `:end' and `:post-blank' keywords.
3416 Assume point is at the first equal sign marker."
3418 (unless (bolp) (backward-char 1))
3419 (looking-at org-emph-re
)
3420 (let ((begin (match-beginning 2))
3421 (value (org-match-string-no-properties 4))
3422 (post-blank (progn (goto-char (match-end 2))
3423 (skip-chars-forward " \t")))
3429 :post-blank post-blank
)))))
3431 (defun org-element-verbatim-interpreter (verbatim contents
)
3432 "Interpret VERBATIM object as Org syntax.
3434 (format "=%s=" (org-element-property :value verbatim
)))
3438 ;;; Parsing Element Starting At Point
3440 ;; `org-element--current-element' is the core function of this section.
3441 ;; It returns the Lisp representation of the element starting at
3444 ;; `org-element--current-element' makes use of special modes. They
3445 ;; are activated for fixed element chaining (i.e. `plain-list' >
3446 ;; `item') or fixed conditional element chaining (i.e. `headline' >
3447 ;; `section'). Special modes are: `first-section', `item',
3448 ;; `node-property', `quote-section', `section' and `table-row'.
3450 (defun org-element--current-element
3451 (limit &optional granularity special structure
)
3452 "Parse the element starting at point.
3454 LIMIT bounds the search.
3456 Return value is a list like (TYPE PROPS) where TYPE is the type
3457 of the element and PROPS a plist of properties associated to the
3460 Possible types are defined in `org-element-all-elements'.
3462 Optional argument GRANULARITY determines the depth of the
3463 recursion. Allowed values are `headline', `greater-element',
3464 `element', `object' or nil. When it is broader than `object' (or
3465 nil), secondary values will not be parsed, since they only
3468 Optional argument SPECIAL, when non-nil, can be either
3469 `first-section', `item', `node-property', `quote-section',
3470 `section', and `table-row'.
3472 If STRUCTURE isn't provided but SPECIAL is set to `item', it will
3475 This function assumes point is always at the beginning of the
3476 element it has to parse."
3478 (let ((case-fold-search t
)
3479 ;; Determine if parsing depth allows for secondary strings
3480 ;; parsing. It only applies to elements referenced in
3481 ;; `org-element-secondary-value-alist'.
3482 (raw-secondary-p (and granularity
(not (eq granularity
'object
)))))
3486 (org-element-item-parser limit structure raw-secondary-p
))
3488 ((eq special
'table-row
) (org-element-table-row-parser limit
))
3490 ((eq special
'node-property
) (org-element-node-property-parser limit
))
3492 ((org-with-limited-levels (org-at-heading-p))
3493 (org-element-headline-parser limit raw-secondary-p
))
3494 ;; Sections (must be checked after headline).
3495 ((eq special
'section
) (org-element-section-parser limit
))
3496 ((eq special
'quote-section
) (org-element-quote-section-parser limit
))
3497 ((eq special
'first-section
)
3498 (org-element-section-parser
3499 (or (save-excursion (org-with-limited-levels (outline-next-heading)))
3501 ;; When not at bol, point is at the beginning of an item or
3502 ;; a footnote definition: next item is always a paragraph.
3503 ((not (bolp)) (org-element-paragraph-parser limit
(list (point))))
3504 ;; Planning and Clock.
3505 ((and (looking-at org-planning-or-clock-line-re
))
3506 (if (equal (match-string 1) org-clock-string
)
3507 (org-element-clock-parser limit
)
3508 (org-element-planning-parser limit
)))
3511 (org-element-inlinetask-parser limit raw-secondary-p
))
3512 ;; From there, elements can have affiliated keywords.
3513 (t (let ((affiliated (org-element--collect-affiliated-keywords)))
3515 ;; LaTeX Environment.
3516 ((looking-at "[ \t]*\\\\begin{\\([A-Za-z0-9*]+\\)}[ \t]*$")
3519 (format "^[ \t]*\\\\end{%s}[ \t]*$"
3520 (regexp-quote (match-string 1)))
3522 (org-element-latex-environment-parser limit affiliated
)
3523 (org-element-paragraph-parser limit affiliated
)))
3524 ;; Drawer and Property Drawer.
3525 ((looking-at org-drawer-regexp
)
3526 (let ((name (match-string 1)))
3528 ((not (save-excursion
3529 (re-search-forward "^[ \t]*:END:[ \t]*$" nil t
)))
3530 (org-element-paragraph-parser limit affiliated
))
3531 ((equal "PROPERTIES" name
)
3532 (org-element-property-drawer-parser limit affiliated
))
3533 (t (org-element-drawer-parser limit affiliated
)))))
3535 ((looking-at "[ \t]*:\\( \\|$\\)")
3536 (org-element-fixed-width-parser limit affiliated
))
3537 ;; Inline Comments, Blocks, Babel Calls, Dynamic Blocks and
3539 ((looking-at "[ \t]*#")
3540 (goto-char (match-end 0))
3541 (cond ((looking-at "\\(?: \\|$\\)")
3543 (org-element-comment-parser limit affiliated
))
3544 ((looking-at "\\+BEGIN_\\(\\S-+\\)")
3546 (let ((parser (assoc (upcase (match-string 1))
3547 org-element-block-name-alist
)))
3548 (if parser
(funcall (cdr parser
) limit affiliated
)
3549 (org-element-special-block-parser limit affiliated
))))
3550 ((looking-at "\\+CALL:")
3552 (org-element-babel-call-parser limit affiliated
))
3553 ((looking-at "\\+BEGIN:? ")
3555 (org-element-dynamic-block-parser limit affiliated
))
3556 ((looking-at "\\+\\S-+:")
3558 (org-element-keyword-parser limit affiliated
))
3561 (org-element-paragraph-parser limit affiliated
))))
3562 ;; Footnote Definition.
3563 ((looking-at org-footnote-definition-re
)
3564 (org-element-footnote-definition-parser limit affiliated
))
3566 ((looking-at "[ \t]*-\\{5,\\}[ \t]*$")
3567 (org-element-horizontal-rule-parser limit affiliated
))
3569 ((org-at-table-p t
) (org-element-table-parser limit affiliated
))
3571 ((looking-at (org-item-re))
3572 (org-element-plain-list-parser
3573 limit affiliated
(or structure
(org-list-struct))))
3574 ;; Default element: Paragraph.
3575 (t (org-element-paragraph-parser limit affiliated
)))))))))
3578 ;; Most elements can have affiliated keywords. When looking for an
3579 ;; element beginning, we want to move before them, as they belong to
3580 ;; that element, and, in the meantime, collect information they give
3581 ;; into appropriate properties. Hence the following function.
3583 (defun org-element--collect-affiliated-keywords ()
3584 "Collect affiliated keywords from point.
3586 Return a list whose CAR is the position at the first of them and
3587 CDR a plist of keywords and values and move point to the
3588 beginning of the first line after them.
3590 As a special case, if element doesn't start at the beginning of
3591 the line (i.e. a paragraph starting an item), CAR is current
3592 position of point and CDR is nil."
3593 (if (not (bolp)) (list (point))
3594 (let ((case-fold-search t
)
3596 ;; RESTRICT is the list of objects allowed in parsed
3598 (restrict (org-element-restriction 'keyword
))
3600 (while (and (not (eobp)) (looking-at org-element--affiliated-re
))
3601 (let* ((raw-kwd (upcase (match-string 1)))
3602 ;; Apply translation to RAW-KWD. From there, KWD is
3603 ;; the official keyword.
3604 (kwd (or (cdr (assoc raw-kwd
3605 org-element-keyword-translation-alist
))
3607 ;; Find main value for any keyword.
3611 (buffer-substring-no-properties
3612 (match-end 0) (point-at-eol)))))
3613 ;; PARSEDP is non-nil when keyword should have its
3615 (parsedp (member kwd org-element-parsed-keywords
))
3616 ;; If KWD is a dual keyword, find its secondary
3617 ;; value. Maybe parse it.
3618 (dualp (member kwd org-element-dual-keywords
))
3621 (let ((sec (org-match-string-no-properties 2)))
3622 (if (or (not sec
) (not parsedp
)) sec
3623 (org-element-parse-secondary-string sec restrict
)))))
3624 ;; Attribute a property name to KWD.
3625 (kwd-sym (and kwd
(intern (concat ":" (downcase kwd
))))))
3626 ;; Now set final shape for VALUE.
3628 (setq value
(org-element-parse-secondary-string value restrict
)))
3629 (when dualp
(setq value
(and value
(cons value dual-value
))))
3630 (when (or (member kwd org-element-multiple-keywords
)
3631 ;; Attributes can always appear on multiple lines.
3632 (string-match "^ATTR_" kwd
))
3633 (setq value
(cons value
(plist-get output kwd-sym
))))
3634 ;; Eventually store the new value in OUTPUT.
3635 (setq output
(plist-put output kwd-sym value
))
3636 ;; Move to next keyword.
3638 ;; If affiliated keywords are orphaned: move back to first one.
3639 ;; They will be parsed as a paragraph.
3640 (when (looking-at "[ \t]*$") (goto-char origin
) (setq output nil
))
3642 (cons origin output
))))
3648 ;; The two major functions here are `org-element-parse-buffer', which
3649 ;; parses Org syntax inside the current buffer, taking into account
3650 ;; region, narrowing, or even visibility if specified, and
3651 ;; `org-element-parse-secondary-string', which parses objects within
3654 ;; The (almost) almighty `org-element-map' allows to apply a function
3655 ;; on elements or objects matching some type, and accumulate the
3656 ;; resulting values. In an export situation, it also skips unneeded
3657 ;; parts of the parse tree.
3659 (defun org-element-parse-buffer (&optional granularity visible-only
)
3660 "Recursively parse the buffer and return structure.
3661 If narrowing is in effect, only parse the visible part of the
3664 Optional argument GRANULARITY determines the depth of the
3665 recursion. It can be set to the following symbols:
3667 `headline' Only parse headlines.
3668 `greater-element' Don't recurse into greater elements excepted
3669 headlines and sections. Thus, elements
3670 parsed are the top-level ones.
3671 `element' Parse everything but objects and plain text.
3672 `object' Parse the complete buffer (default).
3674 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3677 Assume buffer is in Org mode."
3679 (goto-char (point-min))
3680 (org-skip-whitespace)
3681 (org-element--parse-elements
3682 (point-at-bol) (point-max)
3683 ;; Start in `first-section' mode so text before the first
3684 ;; headline belongs to a section.
3685 'first-section nil granularity visible-only
(list 'org-data nil
))))
3687 (defun org-element-parse-secondary-string (string restriction
&optional parent
)
3688 "Recursively parse objects in STRING and return structure.
3690 RESTRICTION is a symbol limiting the object types that will be
3693 Optional argument PARENT, when non-nil, is the element or object
3694 containing the secondary string. It is used to set correctly
3695 `:parent' property within the string."
3698 (let ((secondary (org-element--parse-objects
3699 (point-min) (point-max) nil restriction
)))
3700 (mapc (lambda (obj) (org-element-put-property obj
:parent parent
))
3703 (defun org-element-map
3704 (data types fun
&optional info first-match no-recursion with-affiliated
)
3705 "Map a function on selected elements or objects.
3707 DATA is the parsed tree, as returned by, i.e,
3708 `org-element-parse-buffer'. TYPES is a symbol or list of symbols
3709 of elements or objects types. FUN is the function called on the
3710 matching element or object. It must accept one arguments: the
3711 element or object itself.
3713 When optional argument INFO is non-nil, it should be a plist
3714 holding export options. In that case, parts of the parse tree
3715 not exportable according to that property list will be skipped.
3717 When optional argument FIRST-MATCH is non-nil, stop at the first
3718 match for which FUN doesn't return nil, and return that value.
3720 Optional argument NO-RECURSION is a symbol or a list of symbols
3721 representing elements or objects types. `org-element-map' won't
3722 enter any recursive element or object whose type belongs to that
3723 list. Though, FUN can still be applied on them.
3725 When optional argument WITH-AFFILIATED is non-nil, also move into
3726 affiliated keywords to find objects.
3728 Nil values returned from FUN do not appear in the results."
3729 ;; Ensure TYPES and NO-RECURSION are a list, even of one element.
3730 (unless (listp types
) (setq types
(list types
)))
3731 (unless (listp no-recursion
) (setq no-recursion
(list no-recursion
)))
3732 ;; Recursion depth is determined by --CATEGORY.
3735 (let ((category 'greater-elements
))
3736 (mapc (lambda (type)
3737 (cond ((or (memq type org-element-all-objects
)
3738 (eq type
'plain-text
))
3739 ;; If one object is found, the function
3740 ;; has to recurse into every object.
3741 (throw 'found
'objects
))
3742 ((not (memq type org-element-greater-elements
))
3743 ;; If one regular element is found, the
3744 ;; function has to recurse, at least,
3745 ;; into every element it encounters.
3746 (and (not (eq category
'elements
))
3747 (setq category
'elements
)))))
3750 ;; Compute properties for affiliated keywords if necessary.
3752 (and with-affiliated
3753 (mapcar (lambda (kwd)
3754 (cons kwd
(intern (concat ":" (downcase kwd
)))))
3755 org-element-affiliated-keywords
)))
3761 ;; Recursively walk DATA. INFO, if non-nil, is a plist
3762 ;; holding contextual information.
3763 (let ((--type (org-element-type --data
)))
3766 ;; Ignored element in an export context.
3767 ((and info
(memq --data
(plist-get info
:ignore-list
))))
3768 ;; Secondary string: only objects can be found there.
3770 (when (eq --category
'objects
) (mapc --walk-tree --data
)))
3771 ;; Unconditionally enter parse trees.
3772 ((eq --type
'org-data
)
3773 (mapc --walk-tree
(org-element-contents --data
)))
3775 ;; Check if TYPE is matching among TYPES. If so,
3776 ;; apply FUN to --DATA and accumulate return value
3777 ;; into --ACC (or exit if FIRST-MATCH is non-nil).
3778 (when (memq --type types
)
3779 (let ((result (funcall fun --data
)))
3780 (cond ((not result
))
3781 (first-match (throw '--map-first-match result
))
3782 (t (push result --acc
)))))
3783 ;; If --DATA has a secondary string that can contain
3784 ;; objects with their type among TYPES, look into it.
3785 (when (and (eq --category
'objects
) (not (stringp --data
)))
3787 (assq --type org-element-secondary-value-alist
)))
3789 (funcall --walk-tree
3790 (org-element-property (cdr sec-prop
) --data
)))))
3791 ;; If --DATA has any affiliated keywords and
3792 ;; WITH-AFFILIATED is non-nil, look for objects in
3794 (when (and with-affiliated
3795 (eq --category
'objects
)
3796 (memq --type org-element-all-elements
))
3797 (mapc (lambda (kwd-pair)
3798 (let ((kwd (car kwd-pair
))
3799 (value (org-element-property
3800 (cdr kwd-pair
) --data
)))
3801 ;; Pay attention to the type of value.
3802 ;; Preserve order for multiple keywords.
3805 ((and (member kwd org-element-multiple-keywords
)
3806 (member kwd org-element-dual-keywords
))
3807 (mapc (lambda (line)
3808 (funcall --walk-tree
(cdr line
))
3809 (funcall --walk-tree
(car line
)))
3811 ((member kwd org-element-multiple-keywords
)
3812 (mapc (lambda (line) (funcall --walk-tree line
))
3814 ((member kwd org-element-dual-keywords
)
3815 (funcall --walk-tree
(cdr value
))
3816 (funcall --walk-tree
(car value
)))
3817 (t (funcall --walk-tree value
)))))
3818 --affiliated-alist
))
3819 ;; Determine if a recursion into --DATA is possible.
3821 ;; --TYPE is explicitly removed from recursion.
3822 ((memq --type no-recursion
))
3823 ;; --DATA has no contents.
3824 ((not (org-element-contents --data
)))
3825 ;; Looking for greater elements but --DATA is simply
3826 ;; an element or an object.
3827 ((and (eq --category
'greater-elements
)
3828 (not (memq --type org-element-greater-elements
))))
3829 ;; Looking for elements but --DATA is an object.
3830 ((and (eq --category
'elements
)
3831 (memq --type org-element-all-objects
)))
3832 ;; In any other case, map contents.
3833 (t (mapc --walk-tree
(org-element-contents --data
)))))))))))
3834 (catch '--map-first-match
3835 (funcall --walk-tree data
)
3836 ;; Return value in a proper order.
3839 ;; The following functions are internal parts of the parser.
3841 ;; The first one, `org-element--parse-elements' acts at the element's
3844 ;; The second one, `org-element--parse-objects' applies on all objects
3845 ;; of a paragraph or a secondary string. It uses
3846 ;; `org-element--get-next-object-candidates' to optimize the search of
3847 ;; the next object in the buffer.
3849 ;; More precisely, that function looks for every allowed object type
3850 ;; first. Then, it discards failed searches, keeps further matches,
3851 ;; and searches again types matched behind point, for subsequent
3852 ;; calls. Thus, searching for a given type fails only once, and every
3853 ;; object is searched only once at top level (but sometimes more for
3856 (defun org-element--parse-elements
3857 (beg end special structure granularity visible-only acc
)
3858 "Parse elements between BEG and END positions.
3860 SPECIAL prioritize some elements over the others. It can be set
3861 to `first-section', `quote-section', `section' `item' or
3864 When value is `item', STRUCTURE will be used as the current list
3867 GRANULARITY determines the depth of the recursion. See
3868 `org-element-parse-buffer' for more information.
3870 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3873 Elements are accumulated into ACC."
3876 ;; When parsing only headlines, skip any text before first one.
3877 (when (and (eq granularity
'headline
) (not (org-at-heading-p)))
3878 (org-with-limited-levels (outline-next-heading)))
3880 (while (< (point) end
)
3881 ;; Find current element's type and parse it accordingly to
3883 (let* ((element (org-element--current-element
3884 end granularity special structure
))
3885 (type (org-element-type element
))
3886 (cbeg (org-element-property :contents-begin element
)))
3887 (goto-char (org-element-property :end element
))
3888 ;; Fill ELEMENT contents by side-effect.
3890 ;; If VISIBLE-ONLY is true and element is hidden or if it has
3891 ;; no contents, don't modify it.
3892 ((or (and visible-only
(org-element-property :hiddenp element
))
3894 ;; Greater element: parse it between `contents-begin' and
3895 ;; `contents-end'. Make sure GRANULARITY allows the
3896 ;; recursion, or ELEMENT is an headline, in which case going
3897 ;; inside is mandatory, in order to get sub-level headings.
3898 ((and (memq type org-element-greater-elements
)
3899 (or (memq granularity
'(element object nil
))
3900 (and (eq granularity
'greater-element
)
3902 (eq type
'headline
)))
3903 (org-element--parse-elements
3904 cbeg
(org-element-property :contents-end element
)
3905 ;; Possibly switch to a special mode.
3908 (if (org-element-property :quotedp element
) 'quote-section
3911 (property-drawer 'node-property
)
3913 (org-element-property :structure element
)
3914 granularity visible-only element
))
3915 ;; ELEMENT has contents. Parse objects inside, if
3916 ;; GRANULARITY allows it.
3917 ((memq granularity
'(object nil
))
3918 (org-element--parse-objects
3919 cbeg
(org-element-property :contents-end element
) element
3920 (org-element-restriction type
))))
3921 (org-element-adopt-elements acc element
)))
3925 (defun org-element--parse-objects (beg end acc restriction
)
3926 "Parse objects between BEG and END and return recursive structure.
3928 Objects are accumulated in ACC.
3930 RESTRICTION is a list of object types which are allowed in the
3935 (while (and (< (point) end
)
3936 (setq candidates
(org-element--get-next-object-candidates
3937 end restriction candidates
)))
3939 (let ((pos (apply 'min
(mapcar 'cdr candidates
))))
3942 (funcall (intern (format "org-element-%s-parser"
3943 (car (rassq pos candidates
)))))))))
3944 ;; 1. Text before any object. Untabify it.
3945 (let ((obj-beg (org-element-property :begin next-object
)))
3946 (unless (= (point) obj-beg
)
3948 (org-element-adopt-elements
3950 (replace-regexp-in-string
3951 "\t" (make-string tab-width ?
)
3952 (buffer-substring-no-properties (point) obj-beg
))))))
3954 (let ((obj-end (org-element-property :end next-object
))
3955 (cont-beg (org-element-property :contents-begin next-object
)))
3956 ;; Fill contents of NEXT-OBJECT by side-effect, if it has
3957 ;; a recursive type.
3959 (memq (car next-object
) org-element-recursive-objects
))
3963 (org-element-property :contents-end next-object
))
3964 (org-element--parse-objects
3965 (point-min) (point-max) next-object
3966 (org-element-restriction next-object
))))
3967 (setq acc
(org-element-adopt-elements acc next-object
))
3968 (goto-char obj-end
))))
3969 ;; 3. Text after last object. Untabify it.
3970 (unless (= (point) end
)
3972 (org-element-adopt-elements
3974 (replace-regexp-in-string
3975 "\t" (make-string tab-width ?
)
3976 (buffer-substring-no-properties (point) end
)))))
3980 (defun org-element--get-next-object-candidates (limit restriction objects
)
3981 "Return an alist of candidates for the next object.
3983 LIMIT bounds the search, and RESTRICTION narrows candidates to
3986 Return value is an alist whose CAR is position and CDR the object
3989 OBJECTS is the previous candidates alist."
3990 ;; Filter out any object found but not belonging to RESTRICTION.
3994 (let ((type (car obj
)))
3995 (memq (or (cdr (assq type org-element-object-successor-alist
))
3999 (let (next-candidates types-to-search
)
4000 ;; If no previous result, search every object type in RESTRICTION.
4001 ;; Otherwise, keep potential candidates (old objects located after
4002 ;; point) and ask to search again those which had matched before.
4003 (if (not objects
) (setq types-to-search restriction
)
4005 (if (< (cdr obj
) (point)) (push (car obj
) types-to-search
)
4006 (push obj next-candidates
)))
4008 ;; Call the appropriate successor function for each type to search
4009 ;; and accumulate matches.
4012 (let* ((successor-fun
4014 (format "org-element-%s-successor"
4015 (or (cdr (assq type org-element-object-successor-alist
))
4017 (obj (funcall successor-fun limit
)))
4018 (and obj
(push obj next-candidates
))))
4025 ;;; Towards A Bijective Process
4027 ;; The parse tree obtained with `org-element-parse-buffer' is really
4028 ;; a snapshot of the corresponding Org buffer. Therefore, it can be
4029 ;; interpreted and expanded into a string with canonical Org syntax.
4030 ;; Hence `org-element-interpret-data'.
4032 ;; The function relies internally on
4033 ;; `org-element--interpret-affiliated-keywords'.
4036 (defun org-element-interpret-data (data &optional parent
)
4037 "Interpret DATA as Org syntax.
4039 DATA is a parse tree, an element, an object or a secondary string
4042 Optional argument PARENT is used for recursive calls. It contains
4043 the element or object containing data, or nil.
4045 Return Org syntax as a string."
4046 (let* ((type (org-element-type data
))
4049 ;; Secondary string.
4052 (lambda (obj) (org-element-interpret-data obj parent
))
4054 ;; Full Org document.
4055 ((eq type
'org-data
)
4057 (lambda (obj) (org-element-interpret-data obj parent
))
4058 (org-element-contents data
) ""))
4060 ((stringp data
) data
)
4061 ;; Element/Object without contents.
4062 ((not (org-element-contents data
))
4063 (funcall (intern (format "org-element-%s-interpreter" type
))
4065 ;; Element/Object with contents.
4067 (let* ((greaterp (memq type org-element-greater-elements
))
4068 (objectp (and (not greaterp
)
4069 (memq type org-element-recursive-objects
)))
4072 (lambda (obj) (org-element-interpret-data obj data
))
4073 (org-element-contents
4074 (if (or greaterp objectp
) data
4075 ;; Elements directly containing objects must
4076 ;; have their indentation normalized first.
4077 (org-element-normalize-contents
4079 ;; When normalizing first paragraph of an
4080 ;; item or a footnote-definition, ignore
4081 ;; first line's indentation.
4082 (and (eq type
'paragraph
)
4083 (equal data
(car (org-element-contents parent
)))
4084 (memq (org-element-type parent
)
4085 '(footnote-definition item
))))))
4087 (funcall (intern (format "org-element-%s-interpreter" type
))
4089 (if greaterp
(org-element-normalize-contents contents
)
4091 (if (memq type
'(org-data plain-text nil
)) results
4092 ;; Build white spaces. If no `:post-blank' property is
4093 ;; specified, assume its value is 0.
4094 (let ((post-blank (or (org-element-property :post-blank data
) 0)))
4095 (if (memq type org-element-all-objects
)
4096 (concat results
(make-string post-blank
32))
4098 (org-element--interpret-affiliated-keywords data
)
4099 (org-element-normalize-string results
)
4100 (make-string post-blank
10)))))))
4102 (defun org-element--interpret-affiliated-keywords (element)
4103 "Return ELEMENT's affiliated keywords as Org syntax.
4104 If there is no affiliated keyword, return the empty string."
4105 (let ((keyword-to-org
4109 (when (member key org-element-dual-keywords
)
4110 (setq dual
(cdr value
) value
(car value
)))
4113 (format "[%s]" (org-element-interpret-data dual
)))
4115 (if (member key org-element-parsed-keywords
)
4116 (org-element-interpret-data value
)
4121 (let ((value (org-element-property prop element
))
4122 (keyword (upcase (substring (symbol-name prop
) 1))))
4124 (if (or (member keyword org-element-multiple-keywords
)
4125 ;; All attribute keywords can have multiple lines.
4126 (string-match "^ATTR_" keyword
))
4127 (mapconcat (lambda (line) (funcall keyword-to-org keyword line
))
4130 (funcall keyword-to-org keyword value
)))))
4131 ;; List all ELEMENT's properties matching an attribute line or an
4132 ;; affiliated keyword, but ignore translated keywords since they
4133 ;; cannot belong to the property list.
4134 (loop for prop in
(nth 1 element
) by
'cddr
4135 when
(let ((keyword (upcase (substring (symbol-name prop
) 1))))
4136 (or (string-match "^ATTR_" keyword
)
4138 (member keyword org-element-affiliated-keywords
)
4140 org-element-keyword-translation-alist
)))))
4144 ;; Because interpretation of the parse tree must return the same
4145 ;; number of blank lines between elements and the same number of white
4146 ;; space after objects, some special care must be given to white
4149 ;; The first function, `org-element-normalize-string', ensures any
4150 ;; string different from the empty string will end with a single
4151 ;; newline character.
4153 ;; The second function, `org-element-normalize-contents', removes
4154 ;; global indentation from the contents of the current element.
4156 (defun org-element-normalize-string (s)
4157 "Ensure string S ends with a single newline character.
4159 If S isn't a string return it unchanged. If S is the empty
4160 string, return it. Otherwise, return a new string with a single
4161 newline character at its end."
4163 ((not (stringp s
)) s
)
4165 (t (and (string-match "\\(\n[ \t]*\\)*\\'" s
)
4166 (replace-match "\n" nil nil s
)))))
4168 (defun org-element-normalize-contents (element &optional ignore-first
)
4169 "Normalize plain text in ELEMENT's contents.
4171 ELEMENT must only contain plain text and objects.
4173 If optional argument IGNORE-FIRST is non-nil, ignore first line's
4174 indentation to compute maximal common indentation.
4176 Return the normalized element that is element with global
4177 indentation removed from its contents. The function assumes that
4178 indentation is not done with TAB characters."
4179 (let* (ind-list ; for byte-compiler
4180 collect-inds
; for byte-compiler
4183 ;; Return list of indentations within BLOB. This is done by
4184 ;; walking recursively BLOB and updating IND-LIST along the
4185 ;; way. FIRST-FLAG is non-nil when the first string hasn't
4186 ;; been seen yet. It is required as this string is the only
4187 ;; one whose indentation doesn't happen after a newline
4189 (lambda (blob first-flag
)
4192 (when (and first-flag
(stringp object
))
4193 (setq first-flag nil
)
4194 (string-match "\\`\\( *\\)" object
)
4195 (let ((len (length (match-string 1 object
))))
4196 ;; An indentation of zero means no string will be
4197 ;; modified. Quit the process.
4198 (if (zerop len
) (throw 'zero
(setq ind-list nil
))
4199 (push len ind-list
))))
4203 ;; Avoid matching blank or empty lines.
4204 (while (and (string-match "\n\\( *\\)\\(.\\)" object start
)
4205 (not (equal (match-string 2 object
) " ")))
4206 (setq start
(match-end 0))
4207 (push (length (match-string 1 object
)) ind-list
))))
4208 ((memq (org-element-type object
) org-element-recursive-objects
)
4209 (funcall collect-inds object first-flag
))))
4210 (org-element-contents blob
))))))
4211 ;; Collect indentation list in ELEMENT. Possibly remove first
4212 ;; value if IGNORE-FIRST is non-nil.
4213 (catch 'zero
(funcall collect-inds element
(not ignore-first
)))
4214 (if (not ind-list
) element
4215 ;; Build ELEMENT back, replacing each string with the same
4216 ;; string minus common indentation.
4217 (let* (build ; For byte compiler.
4220 (lambda (blob mci first-flag
)
4221 ;; Return BLOB with all its strings indentation
4222 ;; shortened from MCI white spaces. FIRST-FLAG is
4223 ;; non-nil when the first string hasn't been seen
4228 (when (and first-flag
(stringp object
))
4229 (setq first-flag nil
)
4231 (replace-regexp-in-string
4232 (format "\\` \\{%d\\}" mci
) "" object
)))
4235 (replace-regexp-in-string
4236 (format "\n \\{%d\\}" mci
) "\n" object
))
4237 ((memq (org-element-type object
)
4238 org-element-recursive-objects
)
4239 (funcall build object mci first-flag
))
4241 (org-element-contents blob
)))
4243 (funcall build element
(apply 'min ind-list
) (not ignore-first
))))))
4249 ;; The first move is to implement a way to obtain the smallest element
4250 ;; containing point. This is the job of `org-element-at-point'. It
4251 ;; basically jumps back to the beginning of section containing point
4252 ;; and moves, element after element, with
4253 ;; `org-element--current-element' until the container is found. Note:
4254 ;; When using `org-element-at-point', secondary values are never
4255 ;; parsed since the function focuses on elements, not on objects.
4257 ;; At a deeper level, `org-element-context' lists all elements and
4258 ;; objects containing point.
4260 ;; `org-element-nested-p' and `org-element-swap-A-B' may be used
4261 ;; internally by navigation and manipulation tools.
4264 (defun org-element-at-point (&optional keep-trail
)
4265 "Determine closest element around point.
4267 Return value is a list like (TYPE PROPS) where TYPE is the type
4268 of the element and PROPS a plist of properties associated to the
4271 Possible types are defined in `org-element-all-elements'.
4272 Properties depend on element or object type, but always
4273 include :begin, :end, :parent and :post-blank properties.
4275 As a special case, if point is at the very beginning of a list or
4276 sub-list, returned element will be that list instead of the first
4277 item. In the same way, if point is at the beginning of the first
4278 row of a table, returned element will be the table instead of the
4281 If optional argument KEEP-TRAIL is non-nil, the function returns
4282 a list of of elements leading to element at point. The list's
4283 CAR is always the element at point. Following positions contain
4284 element's siblings, then parents, siblings of parents, until the
4285 first element of current section."
4286 (org-with-wide-buffer
4287 ;; If at an headline, parse it. It is the sole element that
4288 ;; doesn't require to know about context. Be sure to disallow
4289 ;; secondary string parsing, though.
4290 (if (org-with-limited-levels (org-at-heading-p))
4293 (if (not keep-trail
) (org-element-headline-parser (point-max) t
)
4294 (list (org-element-headline-parser (point-max) t
))))
4295 ;; Otherwise move at the beginning of the section containing
4297 (let ((origin (point))
4298 (end (save-excursion
4299 (org-with-limited-levels (outline-next-heading)) (point)))
4300 element type special-flag trail struct prevs parent
)
4301 (org-with-limited-levels
4302 (if (org-with-limited-levels (org-before-first-heading-p))
4303 (goto-char (point-min))
4304 (org-back-to-heading)
4306 (org-skip-whitespace)
4308 ;; Parse successively each element, skipping those ending
4309 ;; before original position.
4313 (org-element--current-element end
'element special-flag struct
)
4315 (org-element-put-property element
:parent parent
)
4316 (when keep-trail
(push element trail
))
4318 ;; 1. Skip any element ending before point. Also skip
4319 ;; element ending at point when we're sure that another
4320 ;; element has started.
4321 ((let ((elem-end (org-element-property :end element
)))
4322 (when (or (< elem-end origin
)
4323 (and (= elem-end origin
) (/= elem-end end
)))
4324 (goto-char elem-end
))))
4325 ;; 2. An element containing point is always the element at
4327 ((not (memq type org-element-greater-elements
))
4328 (throw 'exit
(if keep-trail trail element
)))
4329 ;; 3. At any other greater element type, if point is
4330 ;; within contents, move into it.
4332 (let ((cbeg (org-element-property :contents-begin element
))
4333 (cend (org-element-property :contents-end element
)))
4334 (if (or (not cbeg
) (not cend
) (> cbeg origin
) (< cend origin
)
4335 ;; Create an anchor for tables and plain lists:
4336 ;; when point is at the very beginning of these
4337 ;; elements, ignoring affiliated keywords,
4338 ;; target them instead of their contents.
4339 (and (= cbeg origin
) (memq type
'(plain-list table
)))
4340 ;; When point is at contents end, do not move
4341 ;; into elements with an explicit ending, but
4342 ;; return that element instead.
4343 (and (= cend origin
)
4346 drawer dynamic-block inlinetask item
4347 plain-list property-drawer quote-block
4349 (throw 'exit
(if keep-trail trail element
))
4350 (setq parent element
)
4353 (setq special-flag
'item
4354 struct
(org-element-property :structure element
)))
4355 (property-drawer (setq special-flag
'node-property
))
4356 (table (setq special-flag
'table-row
))
4357 (otherwise (setq special-flag nil
)))
4359 (goto-char cbeg
)))))))))))
4362 (defun org-element-context ()
4363 "Return closest element or object around point.
4365 Return value is a list like (TYPE PROPS) where TYPE is the type
4366 of the element or object and PROPS a plist of properties
4369 Possible types are defined in `org-element-all-elements' and
4370 `org-element-all-objects'. Properties depend on element or
4371 object type, but always include :begin, :end, :parent
4372 and :post-blank properties."
4373 (org-with-wide-buffer
4374 (let* ((origin (point))
4375 (element (org-element-at-point))
4376 (type (car element
))
4378 ;; Check if point is inside an element containing objects or at
4379 ;; a secondary string. In that case, move to beginning of the
4380 ;; element or secondary string and set END to the other side.
4381 (if (not (or (and (eq type
'item
)
4382 (let ((tag (org-element-property :tag element
)))
4386 (search-forward tag
(point-at-eol))
4387 (goto-char (match-beginning 0))
4388 (and (>= origin
(point))
4390 ;; `1+' is required so some
4391 ;; successors can match
4392 ;; properly their object.
4393 (setq end
(1+ (match-end 0)))))))))
4394 (and (memq type
'(headline inlinetask
))
4395 (progn (beginning-of-line)
4396 (skip-chars-forward "* ")
4397 (setq end
(point-at-eol))))
4398 (and (memq type
'(paragraph table-row verse-block
))
4399 (let ((cbeg (org-element-property
4400 :contents-begin element
))
4401 (cend (org-element-property
4402 :contents-end element
)))
4403 (and (>= origin cbeg
)
4405 (progn (goto-char cbeg
) (setq end cend
)))))))
4407 (let ((restriction (org-element-restriction element
))
4411 (while (setq candidates
(org-element--get-next-object-candidates
4412 end restriction candidates
))
4413 (let ((closest-cand (rassq (apply 'min
(mapcar 'cdr candidates
))
4415 ;; If ORIGIN is before next object in element, there's
4416 ;; no point in looking further.
4417 (if (> (cdr closest-cand
) origin
) (throw 'exit parent
)
4419 (progn (goto-char (cdr closest-cand
))
4420 (funcall (intern (format "org-element-%s-parser"
4421 (car closest-cand
))))))
4422 (cbeg (org-element-property :contents-begin object
))
4423 (cend (org-element-property :contents-end object
)))
4425 ;; ORIGIN is after OBJECT, so skip it.
4426 ((< (org-element-property :end object
) origin
)
4427 (goto-char (org-element-property :end object
)))
4428 ;; ORIGIN is within a non-recursive object or at an
4429 ;; object boundaries: Return that object.
4430 ((or (not cbeg
) (> cbeg origin
) (< cend origin
))
4432 (org-element-put-property object
:parent parent
)))
4433 ;; Otherwise, move within current object and restrict
4434 ;; search to the end of its contents.
4436 (org-element-put-property object
:parent parent
)
4438 restriction
(org-element-restriction object
)
4442 (defsubst org-element-nested-p
(elem-A elem-B
)
4443 "Non-nil when elements ELEM-A and ELEM-B are nested."
4444 (let ((beg-A (org-element-property :begin elem-A
))
4445 (beg-B (org-element-property :begin elem-B
))
4446 (end-A (org-element-property :end elem-A
))
4447 (end-B (org-element-property :end elem-B
)))
4448 (or (and (>= beg-A beg-B
) (<= end-A end-B
))
4449 (and (>= beg-B beg-A
) (<= end-B end-A
)))))
4451 (defun org-element-swap-A-B (elem-A elem-B
)
4452 "Swap elements ELEM-A and ELEM-B.
4453 Assume ELEM-B is after ELEM-A in the buffer. Leave point at the
4455 (goto-char (org-element-property :begin elem-A
))
4456 ;; There are two special cases when an element doesn't start at bol:
4457 ;; the first paragraph in an item or in a footnote definition.
4458 (let ((specialp (not (bolp))))
4459 ;; Only a paragraph without any affiliated keyword can be moved at
4460 ;; ELEM-A position in such a situation. Note that the case of
4461 ;; a footnote definition is impossible: it cannot contain two
4462 ;; paragraphs in a row because it cannot contain a blank line.
4464 (or (not (eq (org-element-type elem-B
) 'paragraph
))
4465 (/= (org-element-property :begin elem-B
)
4466 (org-element-property :contents-begin elem-B
))))
4467 (error "Cannot swap elements"))
4468 ;; In a special situation, ELEM-A will have no indentation. We'll
4469 ;; give it ELEM-B's (which will in, in turn, have no indentation).
4470 (let* ((ind-B (when specialp
4471 (goto-char (org-element-property :begin elem-B
))
4472 (org-get-indentation)))
4473 (beg-A (org-element-property :begin elem-A
))
4474 (end-A (save-excursion
4475 (goto-char (org-element-property :end elem-A
))
4476 (skip-chars-backward " \r\t\n")
4478 (beg-B (org-element-property :begin elem-B
))
4479 (end-B (save-excursion
4480 (goto-char (org-element-property :end elem-B
))
4481 (skip-chars-backward " \r\t\n")
4483 ;; Store overlays responsible for visibility status. We
4484 ;; also need to store their boundaries as they will be
4485 ;; removed from buffer.
4488 (mapcar (lambda (ov) (list ov
(overlay-start ov
) (overlay-end ov
)))
4489 (overlays-in beg-A end-A
))
4490 (mapcar (lambda (ov) (list ov
(overlay-start ov
) (overlay-end ov
)))
4491 (overlays-in beg-B end-B
))))
4493 (body-A (buffer-substring beg-A end-A
))
4494 (body-B (delete-and-extract-region beg-B end-B
)))
4497 (setq body-B
(replace-regexp-in-string "\\`[ \t]*" "" body-B
))
4498 (org-indent-to-column ind-B
))
4500 ;; Restore ex ELEM-A overlays.
4501 (let ((offset (- beg-B beg-A
)))
4504 (car ov
) (+ (nth 1 ov
) offset
) (+ (nth 2 ov
) offset
)))
4507 (delete-region beg-A end-A
)
4509 ;; Restore ex ELEM-B overlays.
4512 (car ov
) (- (nth 1 ov
) offset
) (- (nth 2 ov
) offset
)))
4514 (goto-char (org-element-property :end elem-B
)))))
4516 (provide 'org-element
)
4519 ;; generated-autoload-file: "org-loaddefs.el"
4522 ;;; org-element.el ends here