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 ;; Lisp-wise, an element or an object can be represented as a list.
79 ;; It follows the pattern (TYPE PROPERTIES CONTENTS), where:
80 ;; TYPE is a symbol describing the Org element or object.
81 ;; PROPERTIES is the property list attached to it. See docstring of
82 ;; appropriate parsing function to get an exhaustive
84 ;; CONTENTS is a list of elements, objects or raw strings contained
85 ;; in the current element or object, when applicable.
87 ;; An Org buffer is a nested list of such elements and objects, whose
88 ;; type is `org-data' and properties is nil.
90 ;; The first part of this file defines Org syntax, while the second
91 ;; one provide accessors and setters functions.
93 ;; The next part implements a parser and an interpreter for each
94 ;; element and object type in Org syntax.
96 ;; The following part creates a fully recursive buffer parser. It
97 ;; also provides a tool to map a function to elements or objects
98 ;; matching some criteria in the parse tree. Functions of interest
99 ;; are `org-element-parse-buffer', `org-element-map' and, to a lesser
100 ;; extent, `org-element-parse-secondary-string'.
102 ;; The penultimate part is the cradle of an interpreter for the
103 ;; obtained parse tree: `org-element-interpret-data'.
105 ;; The library ends by furnishing `org-element-at-point' function, and
106 ;; a way to give information about document structure around point
107 ;; with `org-element-context'.
112 (eval-when-compile (require 'cl
))
117 ;;; Definitions And Rules
119 ;; Define elements, greater elements and specify recursive objects,
120 ;; along with the affiliated keywords recognized. Also set up
121 ;; restrictions on recursive objects combinations.
123 ;; These variables really act as a control center for the parsing
126 (defconst org-element-paragraph-separate
128 ;; Headlines, inlinetasks.
129 org-outline-regexp
"\\|"
130 ;; Footnote definitions.
131 "\\[\\(?:[0-9]+\\|fn:[-_[:word:]]+\\)\\]" "\\|"
135 ;; Tables (any type).
136 "\\(?:|\\|\\+-[-+]\\)" "\\|"
137 ;; Blocks (any type), Babel calls, drawers (any type),
138 ;; fixed-width areas and keywords. Note: this is only an
139 ;; indication and need some thorough check.
142 "-\\{5,\\}[ \t]*$" "\\|"
143 ;; LaTeX environments.
144 "\\\\begin{\\([A-Za-z0-9]+\\*?\\)}" "\\|"
145 ;; Planning and Clock lines.
146 (regexp-opt (list org-scheduled-string
152 (let ((term (case org-plain-list-ordered-item-terminator
153 (?\
) ")") (?.
"\\.") (otherwise "[.)]")))
154 (alpha (and org-alphabetical-lists
"\\|[A-Za-z]")))
155 (concat "\\(?:[-+*]\\|\\(?:[0-9]+" alpha
"\\)" term
"\\)"
156 "\\(?:[ \t]\\|$\\)"))
158 "Regexp to separate paragraphs in an Org buffer.
159 In the case of lines starting with \"#\" and \":\", this regexp
160 is not sufficient to know if point is at a paragraph ending. See
161 `org-element-paragraph-parser' for more information.")
163 (defconst org-element-all-elements
164 '(babel-call center-block clock comment comment-block drawer dynamic-block
165 example-block export-block fixed-width footnote-definition
166 headline horizontal-rule inlinetask item keyword
167 latex-environment node-property paragraph plain-list planning
168 property-drawer quote-block quote-section section special-block
169 src-block table table-row verse-block
)
170 "Complete list of element types.")
172 (defconst org-element-greater-elements
173 '(center-block drawer dynamic-block footnote-definition headline inlinetask
174 item plain-list property-drawer quote-block section
176 "List of recursive element types aka Greater Elements.")
178 (defconst org-element-all-successors
179 '(export-snippet footnote-reference inline-babel-call inline-src-block
180 latex-or-entity line-break link macro radio-target
181 statistics-cookie sub
/superscript table-cell target
182 text-markup timestamp
)
183 "Complete list of successors.")
185 (defconst org-element-object-successor-alist
186 '((subscript . sub
/superscript
) (superscript . sub
/superscript
)
187 (bold . text-markup
) (code . text-markup
) (italic . text-markup
)
188 (strike-through . text-markup
) (underline . text-markup
)
189 (verbatim . text-markup
) (entity . latex-or-entity
)
190 (latex-fragment . latex-or-entity
))
191 "Alist of translations between object type and successor name.
193 Sharing the same successor comes handy when, for example, the
194 regexp matching one object can also match the other object.")
196 (defconst org-element-all-objects
197 '(bold code entity export-snippet footnote-reference inline-babel-call
198 inline-src-block italic line-break latex-fragment link macro
199 radio-target statistics-cookie strike-through subscript superscript
200 table-cell target timestamp underline verbatim
)
201 "Complete list of object types.")
203 (defconst org-element-recursive-objects
204 '(bold italic link subscript radio-target strike-through superscript
205 table-cell underline
)
206 "List of recursive object types.")
208 (defconst org-element-block-name-alist
209 '(("CENTER" . org-element-center-block-parser
)
210 ("COMMENT" . org-element-comment-block-parser
)
211 ("EXAMPLE" . org-element-example-block-parser
)
212 ("QUOTE" . org-element-quote-block-parser
)
213 ("SRC" . org-element-src-block-parser
)
214 ("VERSE" . org-element-verse-block-parser
))
215 "Alist between block names and the associated parsing function.
216 Names must be uppercase. Any block whose name has no association
217 is parsed with `org-element-special-block-parser'.")
219 (defconst org-element-link-type-is-file
220 '("file" "file+emacs" "file+sys" "docview")
221 "List of link types equivalent to \"file\".
222 Only these types can accept search options and an explicit
223 application to open them.")
225 (defconst org-element-affiliated-keywords
226 '("CAPTION" "DATA" "HEADER" "HEADERS" "LABEL" "NAME" "PLOT" "RESNAME" "RESULT"
227 "RESULTS" "SOURCE" "SRCNAME" "TBLNAME")
228 "List of affiliated keywords as strings.
229 By default, all keywords setting attributes (i.e. \"ATTR_LATEX\")
230 are affiliated keywords and need not to be in this list.")
232 (defconst org-element--affiliated-re
233 (format "[ \t]*#\\+%s:"
234 ;; Regular affiliated keywords.
235 (format "\\(%s\\|ATTR_[-_A-Za-z0-9]+\\)\\(?:\\[\\(.*\\)\\]\\)?"
236 (regexp-opt org-element-affiliated-keywords
)))
237 "Regexp matching any affiliated keyword.
239 Keyword name is put in match group 1. Moreover, if keyword
240 belongs to `org-element-dual-keywords', put the dual value in
243 Don't modify it, set `org-element-affiliated-keywords' instead.")
245 (defconst org-element-keyword-translation-alist
246 '(("DATA" .
"NAME") ("LABEL" .
"NAME") ("RESNAME" .
"NAME")
247 ("SOURCE" .
"NAME") ("SRCNAME" .
"NAME") ("TBLNAME" .
"NAME")
248 ("RESULT" .
"RESULTS") ("HEADERS" .
"HEADER"))
249 "Alist of usual translations for keywords.
250 The key is the old name and the value the new one. The property
251 holding their value will be named after the translated name.")
253 (defconst org-element-multiple-keywords
'("CAPTION" "HEADER")
254 "List of affiliated keywords that can occur more than once in an element.
256 Their value will be consed into a list of strings, which will be
257 returned as the value of the property.
259 This list is checked after translations have been applied. See
260 `org-element-keyword-translation-alist'.
262 By default, all keywords setting attributes (i.e. \"ATTR_LATEX\")
263 allow multiple occurrences and need not to be in this list.")
265 (defconst org-element-parsed-keywords
'("CAPTION")
266 "List of affiliated keywords whose value can be parsed.
268 Their value will be stored as a secondary string: a list of
271 This list is checked after translations have been applied. See
272 `org-element-keyword-translation-alist'.")
274 (defconst org-element-dual-keywords
'("CAPTION" "RESULTS")
275 "List of affiliated keywords which can have a secondary value.
277 In Org syntax, they can be written with optional square brackets
278 before the colons. For example, RESULTS keyword can be
279 associated to a hash value with the following:
281 #+RESULTS[hash-string]: some-source
283 This list is checked after translations have been applied. See
284 `org-element-keyword-translation-alist'.")
286 (defconst org-element-document-properties
'("AUTHOR" "DATE" "TITLE")
287 "List of properties associated to the whole document.
288 Any keyword in this list will have its value parsed and stored as
289 a secondary string.")
291 (defconst org-element-object-restrictions
292 '((bold export-snippet inline-babel-call inline-src-block latex-or-entity link
293 radio-target sub
/superscript target text-markup timestamp
)
294 (footnote-reference export-snippet footnote-reference inline-babel-call
295 inline-src-block latex-or-entity line-break link macro
296 radio-target sub
/superscript target text-markup
298 (headline inline-babel-call inline-src-block latex-or-entity link macro
299 radio-target statistics-cookie sub
/superscript target text-markup
301 (inlinetask inline-babel-call inline-src-block latex-or-entity link macro
302 radio-target sub
/superscript target text-markup timestamp
)
303 (italic export-snippet inline-babel-call inline-src-block latex-or-entity
304 link radio-target sub
/superscript target text-markup timestamp
)
305 (item export-snippet footnote-reference inline-babel-call latex-or-entity
306 link macro radio-target sub
/superscript target text-markup
)
307 (keyword inline-babel-call inline-src-block latex-or-entity link macro
308 sub
/superscript text-markup
)
309 (link export-snippet inline-babel-call inline-src-block latex-or-entity link
310 sub
/superscript text-markup
)
311 (paragraph export-snippet footnote-reference inline-babel-call
312 inline-src-block latex-or-entity line-break link macro
313 radio-target statistics-cookie sub
/superscript target text-markup
315 (radio-target export-snippet latex-or-entity sub
/superscript
)
316 (strike-through export-snippet inline-babel-call inline-src-block
317 latex-or-entity link radio-target sub
/superscript target
318 text-markup timestamp
)
319 (subscript export-snippet inline-babel-call inline-src-block latex-or-entity
320 sub
/superscript target text-markup
)
321 (superscript export-snippet inline-babel-call inline-src-block
322 latex-or-entity sub
/superscript target text-markup
)
323 (table-cell export-snippet latex-or-entity link macro radio-target
324 sub
/superscript target text-markup timestamp
)
325 (table-row table-cell
)
326 (underline export-snippet inline-babel-call inline-src-block latex-or-entity
327 link radio-target sub
/superscript target text-markup timestamp
)
328 (verse-block footnote-reference inline-babel-call inline-src-block
329 latex-or-entity line-break link macro radio-target
330 sub
/superscript target text-markup timestamp
))
331 "Alist of objects restrictions.
333 CAR is an element or object type containing objects and CDR is
334 a list of successors that will be called within an element or
337 For example, in a `radio-target' object, one can only find
338 entities, export snippets, latex-fragments, subscript and
341 This alist also applies to secondary string. For example, an
342 `headline' type element doesn't directly contain objects, but
343 still has an entry since one of its properties (`:title') does.")
345 (defconst org-element-secondary-value-alist
346 '((headline .
:title
)
347 (inlinetask .
:title
)
349 (footnote-reference .
:inline-definition
))
350 "Alist between element types and location of secondary value.")
354 ;;; Accessors and Setters
356 ;; Provide four accessors: `org-element-type', `org-element-property'
357 ;; `org-element-contents' and `org-element-restriction'.
359 ;; Setter functions allow to modify elements by side effect. There is
360 ;; `org-element-put-property', `org-element-set-contents',
361 ;; `org-element-set-element' and `org-element-adopt-element'. Note
362 ;; that `org-element-set-element' and `org-element-adopt-elements' are
363 ;; higher level functions since also update `:parent' property.
365 (defsubst org-element-type
(element)
366 "Return type of ELEMENT.
368 The function returns the type of the element or object provided.
369 It can also return the following special value:
370 `plain-text' for a string
371 `org-data' for a complete document
372 nil in any other case."
374 ((not (consp element
)) (and (stringp element
) 'plain-text
))
375 ((symbolp (car element
)) (car element
))))
377 (defsubst org-element-property
(property element
)
378 "Extract the value from the PROPERTY of an ELEMENT."
379 (plist-get (nth 1 element
) property
))
381 (defsubst org-element-contents
(element)
382 "Extract contents from an ELEMENT."
383 (and (consp element
) (nthcdr 2 element
)))
385 (defsubst org-element-restriction
(element)
386 "Return restriction associated to ELEMENT.
387 ELEMENT can be an element, an object or a symbol representing an
388 element or object type."
389 (cdr (assq (if (symbolp element
) element
(org-element-type element
))
390 org-element-object-restrictions
)))
392 (defsubst org-element-put-property
(element property value
)
393 "In ELEMENT set PROPERTY to VALUE.
394 Return modified element."
395 (when (consp element
)
396 (setcar (cdr element
) (plist-put (nth 1 element
) property value
)))
399 (defsubst org-element-set-contents
(element &rest contents
)
400 "Set ELEMENT contents to CONTENTS.
401 Return modified element."
402 (cond ((not element
) (list contents
))
403 ((cdr element
) (setcdr (cdr element
) contents
))
404 (t (nconc element contents
))))
406 (defsubst org-element-set-element
(old new
)
407 "Replace element or object OLD with element or object NEW.
408 The function takes care of setting `:parent' property for NEW."
409 ;; Since OLD is going to be changed into NEW by side-effect, first
410 ;; make sure that every element or object within NEW has OLD as
412 (mapc (lambda (blob) (org-element-put-property blob
:parent old
))
413 (org-element-contents new
))
414 ;; Transfer contents.
415 (apply 'org-element-set-contents old
(org-element-contents new
))
416 ;; Ensure NEW has same parent as OLD, then overwrite OLD properties
418 (org-element-put-property new
:parent
(org-element-property :parent old
))
419 (setcar (cdr old
) (nth 1 new
))
421 (setcar old
(car new
)))
423 (defsubst org-element-adopt-elements
(parent &rest children
)
424 "Append elements to the contents of another element.
426 PARENT is an element or object. CHILDREN can be elements,
427 objects, or a strings.
429 The function takes care of setting `:parent' property for CHILD.
430 Return parent element."
431 (if (not parent
) children
432 ;; Link every child to PARENT.
433 (mapc (lambda (child)
434 (unless (stringp child
)
435 (org-element-put-property child
:parent parent
)))
437 ;; Add CHILDREN at the end of PARENT contents.
438 (apply 'org-element-set-contents
440 (nconc (org-element-contents parent
) children
))
441 ;; Return modified PARENT element.
448 ;; For each greater element type, we define a parser and an
451 ;; A parser returns the element or object as the list described above.
452 ;; Most of them accepts no argument. Though, exceptions exist. Hence
453 ;; every element containing a secondary string (see
454 ;; `org-element-secondary-value-alist') will accept an optional
455 ;; argument to toggle parsing of that secondary string. Moreover,
456 ;; `item' parser requires current list's structure as its first
459 ;; An interpreter accepts two arguments: the list representation of
460 ;; the element or object, and its contents. The latter may be nil,
461 ;; depending on the element or object considered. It returns the
462 ;; appropriate Org syntax, as a string.
464 ;; Parsing functions must follow the naming convention:
465 ;; org-element-TYPE-parser, where TYPE is greater element's type, as
466 ;; defined in `org-element-greater-elements'.
468 ;; Similarly, interpreting functions must follow the naming
469 ;; convention: org-element-TYPE-interpreter.
471 ;; With the exception of `headline' and `item' types, greater elements
472 ;; cannot contain other greater elements of their own type.
474 ;; Beside implementing a parser and an interpreter, adding a new
475 ;; greater element requires to tweak `org-element--current-element'.
476 ;; Moreover, the newly defined type must be added to both
477 ;; `org-element-all-elements' and `org-element-greater-elements'.
482 (defun org-element-center-block-parser (limit affiliated
)
483 "Parse a center block.
485 LIMIT bounds the search. AFFILIATED is a list of which CAR is
486 the buffer position at the beginning of the first affiliated
487 keyword and CDR is a plist of affiliated keywords along with
490 Return a list whose CAR is `center-block' and CDR is a plist
491 containing `:begin', `:end', `:hiddenp', `:contents-begin',
492 `:contents-end' and `:post-blank' keywords.
494 Assume point is at the beginning of the block."
495 (let ((case-fold-search t
))
496 (if (not (save-excursion
497 (re-search-forward "^[ \t]*#\\+END_CENTER[ \t]*$" limit t
)))
498 ;; Incomplete block: parse it as a paragraph.
499 (org-element-paragraph-parser limit affiliated
)
500 (let ((block-end-line (match-beginning 0)))
501 (let* ((begin (car affiliated
))
502 ;; Empty blocks have no contents.
503 (contents-begin (progn (forward-line)
504 (and (< (point) block-end-line
)
506 (contents-end (and contents-begin block-end-line
))
507 (hidden (org-invisible-p2))
508 (pos-before-blank (progn (goto-char block-end-line
)
511 (end (save-excursion (skip-chars-forward " \r\t\n" limit
)
512 (skip-chars-backward " \t")
513 (if (bolp) (point) (line-end-position)))))
519 :contents-begin contents-begin
520 :contents-end contents-end
521 :post-blank
(count-lines pos-before-blank end
))
522 (cdr affiliated
))))))))
524 (defun org-element-center-block-interpreter (center-block contents
)
525 "Interpret CENTER-BLOCK element as Org syntax.
526 CONTENTS is the contents of the element."
527 (format "#+BEGIN_CENTER\n%s#+END_CENTER" contents
))
532 (defun org-element-drawer-parser (limit affiliated
)
535 LIMIT bounds the search. AFFILIATED is a list of which CAR is
536 the buffer position at the beginning of the first affiliated
537 keyword and CDR is a plist of affiliated keywords along with
540 Return a list whose CAR is `drawer' and CDR is a plist containing
541 `:drawer-name', `:begin', `:end', `:hiddenp', `:contents-begin',
542 `:contents-end' and `:post-blank' keywords.
544 Assume point is at beginning of drawer."
545 (let ((case-fold-search t
))
546 (if (not (save-excursion (re-search-forward "^[ \t]*:END:[ \t]*$" limit t
)))
547 ;; Incomplete drawer: parse it as a paragraph.
548 (org-element-paragraph-parser limit affiliated
)
550 (let* ((drawer-end-line (match-beginning 0))
551 (name (progn (looking-at org-drawer-regexp
)
552 (org-match-string-no-properties 1)))
553 (begin (car affiliated
))
554 ;; Empty drawers have no contents.
555 (contents-begin (progn (forward-line)
556 (and (< (point) drawer-end-line
)
558 (contents-end (and contents-begin drawer-end-line
))
559 (hidden (org-invisible-p2))
560 (pos-before-blank (progn (goto-char drawer-end-line
)
563 (end (progn (skip-chars-forward " \r\t\n" limit
)
564 (skip-chars-backward " \t")
565 (if (bolp) (point) (line-end-position)))))
572 :contents-begin contents-begin
573 :contents-end contents-end
574 :post-blank
(count-lines pos-before-blank end
))
575 (cdr affiliated
))))))))
577 (defun org-element-drawer-interpreter (drawer contents
)
578 "Interpret DRAWER element as Org syntax.
579 CONTENTS is the contents of the element."
580 (format ":%s:\n%s:END:"
581 (org-element-property :drawer-name drawer
)
587 (defun org-element-dynamic-block-parser (limit affiliated
)
588 "Parse a dynamic block.
590 LIMIT bounds the search. AFFILIATED is a list of which CAR is
591 the buffer position at the beginning of the first affiliated
592 keyword and CDR is a plist of affiliated keywords along with
595 Return a list whose CAR is `dynamic-block' and CDR is a plist
596 containing `:block-name', `:begin', `:end', `:hiddenp',
597 `:contents-begin', `:contents-end', `:arguments' and
598 `:post-blank' keywords.
600 Assume point is at beginning of dynamic block."
601 (let ((case-fold-search t
))
602 (if (not (save-excursion
603 (re-search-forward "^[ \t]*#\\+END:?[ \t]*$" limit t
)))
604 ;; Incomplete block: parse it as a paragraph.
605 (org-element-paragraph-parser limit affiliated
)
606 (let ((block-end-line (match-beginning 0)))
608 (let* ((name (progn (looking-at org-dblock-start-re
)
609 (org-match-string-no-properties 1)))
610 (arguments (org-match-string-no-properties 3))
611 (begin (car affiliated
))
612 ;; Empty blocks have no contents.
613 (contents-begin (progn (forward-line)
614 (and (< (point) block-end-line
)
616 (contents-end (and contents-begin block-end-line
))
617 (hidden (org-invisible-p2))
618 (pos-before-blank (progn (goto-char block-end-line
)
621 (end (progn (skip-chars-forward " \r\t\n" limit
)
622 (skip-chars-backward " \t")
623 (if (bolp) (point) (line-end-position)))))
631 :contents-begin contents-begin
632 :contents-end contents-end
633 :post-blank
(count-lines pos-before-blank end
))
634 (cdr affiliated
)))))))))
636 (defun org-element-dynamic-block-interpreter (dynamic-block contents
)
637 "Interpret DYNAMIC-BLOCK element as Org syntax.
638 CONTENTS is the contents of the element."
639 (format "#+BEGIN: %s%s\n%s#+END:"
640 (org-element-property :block-name dynamic-block
)
641 (let ((args (org-element-property :arguments dynamic-block
)))
642 (and args
(concat " " args
)))
646 ;;;; Footnote Definition
648 (defun org-element-footnote-definition-parser (limit affiliated
)
649 "Parse a footnote definition.
651 LIMIT bounds the search. AFFILIATED is a list of which CAR is
652 the buffer position at the beginning of the first affiliated
653 keyword and CDR is a plist of affiliated keywords along with
656 Return a list whose CAR is `footnote-definition' and CDR is
657 a plist containing `:label', `:begin' `:end', `:contents-begin',
658 `:contents-end' and `:post-blank' keywords.
660 Assume point is at the beginning of the footnote definition."
662 (let* ((label (progn (looking-at org-footnote-definition-re
)
663 (org-match-string-no-properties 1)))
664 (begin (car affiliated
))
665 (ending (save-excursion
669 (concat org-outline-regexp-bol
"\\|"
670 org-footnote-definition-re
"\\|"
671 "^[ \t]*$") limit
'move
))
674 (contents-begin (progn (search-forward "]")
675 (skip-chars-forward " \r\t\n" ending
)
676 (and (/= (point) ending
) (point))))
677 (contents-end (and contents-begin ending
))
678 (end (progn (goto-char ending
)
679 (skip-chars-forward " \r\t\n" limit
)
680 (skip-chars-backward " \t")
681 (if (bolp) (point) (line-end-position)))))
682 (list 'footnote-definition
687 :contents-begin contents-begin
688 :contents-end contents-end
689 :post-blank
(count-lines ending end
))
690 (cdr affiliated
))))))
692 (defun org-element-footnote-definition-interpreter (footnote-definition contents
)
693 "Interpret FOOTNOTE-DEFINITION element as Org syntax.
694 CONTENTS is the contents of the footnote-definition."
695 (concat (format "[%s]" (org-element-property :label footnote-definition
))
702 (defun org-element-headline-parser (limit &optional raw-secondary-p
)
705 Return a list whose CAR is `headline' and CDR is a plist
706 containing `:raw-value', `:title', `:begin', `:end',
707 `:pre-blank', `:hiddenp', `:contents-begin' and `:contents-end',
708 `:level', `:priority', `:tags', `:todo-keyword',`:todo-type',
709 `:scheduled', `:deadline', `:timestamp', `:clock', `:category',
710 `:quotedp', `:archivedp', `:commentedp' and `:footnote-section-p'
713 The plist also contains any property set in the property drawer,
714 with its name in lowercase, the underscores replaced with hyphens
715 and colons at the beginning (i.e. `:custom-id').
717 When RAW-SECONDARY-P is non-nil, headline's title will not be
718 parsed as a secondary string, but as a plain string instead.
720 Assume point is at beginning of the headline."
722 (let* ((components (org-heading-components))
723 (level (nth 1 components
))
724 (todo (nth 2 components
))
726 (and todo
(if (member todo org-done-keywords
) 'done
'todo
)))
727 (tags (let ((raw-tags (nth 5 components
)))
728 (and raw-tags
(org-split-string raw-tags
":"))))
729 (raw-value (or (nth 4 components
) ""))
731 (let ((case-fold-search nil
))
732 (string-match (format "^%s\\( \\|$\\)" org-quote-string
)
735 (let ((case-fold-search nil
))
736 (string-match (format "^%s\\( \\|$\\)" org-comment-string
)
738 (archivedp (member org-archive-tag tags
))
739 (footnote-section-p (and org-footnote-section
740 (string= org-footnote-section raw-value
)))
741 ;; Normalize property names: ":SOME_PROP:" becomes
743 (standard-props (let (plist)
746 (let ((p-name (downcase (car p
))))
747 (while (string-match "_" p-name
)
749 (replace-match "-" nil nil p-name
)))
750 (setq p-name
(intern (concat ":" p-name
)))
752 (plist-put plist p-name
(cdr p
)))))
753 (org-entry-properties nil
'standard
))
755 (time-props (org-entry-properties nil
'special
"CLOCK"))
756 (scheduled (cdr (assoc "SCHEDULED" time-props
)))
757 (deadline (cdr (assoc "DEADLINE" time-props
)))
758 (clock (cdr (assoc "CLOCK" time-props
)))
759 (timestamp (cdr (assoc "TIMESTAMP" time-props
)))
761 (end (save-excursion (goto-char (org-end-of-subtree t t
))))
762 (pos-after-head (progn (forward-line) (point)))
763 (contents-begin (save-excursion
764 (skip-chars-forward " \r\t\n" end
)
765 (and (/= (point) end
) (line-beginning-position))))
766 (hidden (org-invisible-p2))
767 (contents-end (and contents-begin
768 (progn (goto-char end
)
769 (skip-chars-backward " \r\t\n")
772 ;; Clean RAW-VALUE from any quote or comment string.
773 (when (or quotedp commentedp
)
774 (let ((case-fold-search nil
))
776 (replace-regexp-in-string
778 (regexp-opt (list org-quote-string org-comment-string
))
782 ;; Clean TAGS from archive tag, if any.
783 (when archivedp
(setq tags
(delete org-archive-tag tags
)))
787 (list :raw-value raw-value
791 (if (not contents-begin
) 0
792 (count-lines pos-after-head contents-begin
))
794 :contents-begin contents-begin
795 :contents-end contents-end
797 :priority
(nth 3 components
)
805 :post-blank
(count-lines
806 (if (not contents-end
) pos-after-head
807 (goto-char contents-end
)
811 :footnote-section-p footnote-section-p
813 :commentedp commentedp
816 (org-element-put-property
818 (if raw-secondary-p raw-value
819 (org-element-parse-secondary-string
820 raw-value
(org-element-restriction 'headline
) headline
)))))))
822 (defun org-element-headline-interpreter (headline contents
)
823 "Interpret HEADLINE element as Org syntax.
824 CONTENTS is the contents of the element."
825 (let* ((level (org-element-property :level headline
))
826 (todo (org-element-property :todo-keyword headline
))
827 (priority (org-element-property :priority headline
))
828 (title (org-element-interpret-data
829 (org-element-property :title headline
)))
830 (tags (let ((tag-list (if (org-element-property :archivedp headline
)
831 (cons org-archive-tag
832 (org-element-property :tags headline
))
833 (org-element-property :tags headline
))))
835 (format ":%s:" (mapconcat 'identity tag-list
":")))))
836 (commentedp (org-element-property :commentedp headline
))
837 (quotedp (org-element-property :quotedp headline
))
838 (pre-blank (or (org-element-property :pre-blank headline
) 0))
839 (heading (concat (make-string level ?
*)
840 (and todo
(concat " " todo
))
841 (and quotedp
(concat " " org-quote-string
))
842 (and commentedp
(concat " " org-comment-string
))
844 (format " [#%s]" (char-to-string priority
)))
845 (cond ((and org-footnote-section
846 (org-element-property
847 :footnote-section-p headline
))
848 (concat " " org-footnote-section
))
849 (title (concat " " title
))))))
854 ((zerop org-tags-column
) (format " %s" tags
))
855 ((< org-tags-column
0)
858 (max (- (+ org-tags-column
(length heading
) (length tags
))) 1)
863 (make-string (max (- org-tags-column
(length heading
)) 1) ?
)
865 (make-string (1+ pre-blank
) 10)
871 (defun org-element-inlinetask-parser (limit &optional raw-secondary-p
)
872 "Parse an inline task.
874 Return a list whose CAR is `inlinetask' and CDR is a plist
875 containing `:title', `:begin', `:end', `:hiddenp',
876 `:contents-begin' and `:contents-end', `:level', `:priority',
877 `:raw-value', `:tags', `:todo-keyword', `:todo-type',
878 `:scheduled', `:deadline', `:timestamp', `:clock' and
879 `:post-blank' keywords.
881 The plist also contains any property set in the property drawer,
882 with its name in lowercase, the underscores replaced with hyphens
883 and colons at the beginning (i.e. `:custom-id').
885 When optional argument RAW-SECONDARY-P is non-nil, inline-task's
886 title will not be parsed as a secondary string, but as a plain
889 Assume point is at beginning of the inline task."
891 (let* ((keywords (org-element--collect-affiliated-keywords))
892 (begin (car keywords
))
893 (components (org-heading-components))
894 (todo (nth 2 components
))
896 (if (member todo org-done-keywords
) 'done
'todo
)))
897 (tags (let ((raw-tags (nth 5 components
)))
898 (and raw-tags
(org-split-string raw-tags
":"))))
899 (raw-value (or (nth 4 components
) ""))
900 ;; Normalize property names: ":SOME_PROP:" becomes
902 (standard-props (let (plist)
905 (let ((p-name (downcase (car p
))))
906 (while (string-match "_" p-name
)
908 (replace-match "-" nil nil p-name
)))
909 (setq p-name
(intern (concat ":" p-name
)))
911 (plist-put plist p-name
(cdr p
)))))
912 (org-entry-properties nil
'standard
))
914 (time-props (org-entry-properties nil
'special
"CLOCK"))
915 (scheduled (cdr (assoc "SCHEDULED" time-props
)))
916 (deadline (cdr (assoc "DEADLINE" time-props
)))
917 (clock (cdr (assoc "CLOCK" time-props
)))
918 (timestamp (cdr (assoc "TIMESTAMP" time-props
)))
919 (task-end (save-excursion
921 (and (re-search-forward "^\\*+ END" limit t
)
922 (match-beginning 0))))
923 (contents-begin (progn (forward-line)
924 (and task-end
(< (point) task-end
) (point))))
925 (hidden (and contents-begin
(org-invisible-p2)))
926 (contents-end (and contents-begin task-end
))
927 (before-blank (if (not task-end
) (point)
931 (end (progn (skip-chars-forward " \r\t\n" limit
)
932 (skip-chars-backward " \t")
933 (if (bolp) (point) (line-end-position))))
937 (list :raw-value raw-value
941 :contents-begin contents-begin
942 :contents-end contents-end
943 :level
(nth 1 components
)
944 :priority
(nth 3 components
)
952 :post-blank
(count-lines before-blank end
))
955 (org-element-put-property
957 (if raw-secondary-p raw-value
958 (org-element-parse-secondary-string
960 (org-element-restriction 'inlinetask
)
963 (defun org-element-inlinetask-interpreter (inlinetask contents
)
964 "Interpret INLINETASK element as Org syntax.
965 CONTENTS is the contents of inlinetask."
966 (let* ((level (org-element-property :level inlinetask
))
967 (todo (org-element-property :todo-keyword inlinetask
))
968 (priority (org-element-property :priority inlinetask
))
969 (title (org-element-interpret-data
970 (org-element-property :title inlinetask
)))
971 (tags (let ((tag-list (org-element-property :tags inlinetask
)))
973 (format ":%s:" (mapconcat 'identity tag-list
":")))))
974 (task (concat (make-string level ?
*)
975 (and todo
(concat " " todo
))
977 (format " [#%s]" (char-to-string priority
)))
978 (and title
(concat " " title
)))))
983 ((zerop org-tags-column
) (format " %s" tags
))
984 ((< org-tags-column
0)
987 (max (- (+ org-tags-column
(length task
) (length tags
))) 1)
992 (make-string (max (- org-tags-column
(length task
)) 1) ?
)
994 ;; Prefer degenerate inlinetasks when there are no
999 (make-string level ?
*) " END")))))
1004 (defun org-element-item-parser (limit struct
&optional raw-secondary-p
)
1007 STRUCT is the structure of the plain list.
1009 Return a list whose CAR is `item' and CDR is a plist containing
1010 `:bullet', `:begin', `:end', `:contents-begin', `:contents-end',
1011 `:checkbox', `:counter', `:tag', `:structure', `:hiddenp' and
1012 `:post-blank' keywords.
1014 When optional argument RAW-SECONDARY-P is non-nil, item's tag, if
1015 any, will not be parsed as a secondary string, but as a plain
1018 Assume point is at the beginning of the item."
1021 (looking-at org-list-full-item-re
)
1022 (let* ((begin (point))
1023 (bullet (org-match-string-no-properties 1))
1024 (checkbox (let ((box (org-match-string-no-properties 3)))
1025 (cond ((equal "[ ]" box
) 'off
)
1026 ((equal "[X]" box
) 'on
)
1027 ((equal "[-]" box
) 'trans
))))
1028 (counter (let ((c (org-match-string-no-properties 2)))
1032 ((string-match "[A-Za-z]" c
)
1033 (- (string-to-char (upcase (match-string 0 c
)))
1035 ((string-match "[0-9]+" c
)
1036 (string-to-number (match-string 0 c
)))))))
1037 (end (save-excursion (goto-char (org-list-get-item-end begin struct
))
1038 (unless (bolp) (forward-line))
1042 ;; Ignore tags in un-ordered lists: they are just
1043 ;; a part of item's body.
1044 (if (and (match-beginning 4)
1045 (save-match-data (string-match "[.)]" bullet
)))
1048 (skip-chars-forward " \r\t\n" limit
)
1049 ;; If first line isn't empty, contents really start
1050 ;; at the text after item's meta-data.
1051 (if (= (point-at-bol) begin
) (point) (point-at-bol))))
1052 (hidden (progn (forward-line)
1053 (and (not (= (point) end
)) (org-invisible-p2))))
1054 (contents-end (progn (goto-char end
)
1055 (skip-chars-backward " \r\t\n")
1060 (list :bullet bullet
1063 ;; CONTENTS-BEGIN and CONTENTS-END may be
1064 ;; mixed up in the case of an empty item
1065 ;; separated from the next by a blank line.
1066 ;; Thus ensure the former is always the
1068 :contents-begin
(min contents-begin contents-end
)
1069 :contents-end
(max contents-begin contents-end
)
1074 :post-blank
(count-lines contents-end end
)))))
1075 (org-element-put-property
1077 (let ((raw-tag (org-list-get-tag begin struct
)))
1079 (if raw-secondary-p raw-tag
1080 (org-element-parse-secondary-string
1081 raw-tag
(org-element-restriction 'item
) item
))))))))
1083 (defun org-element-item-interpreter (item contents
)
1084 "Interpret ITEM element as Org syntax.
1085 CONTENTS is the contents of the element."
1086 (let* ((bullet (org-list-bullet-string (org-element-property :bullet item
)))
1087 (checkbox (org-element-property :checkbox item
))
1088 (counter (org-element-property :counter item
))
1089 (tag (let ((tag (org-element-property :tag item
)))
1090 (and tag
(org-element-interpret-data tag
))))
1091 ;; Compute indentation.
1092 (ind (make-string (length bullet
) 32))
1093 (item-starts-with-par-p
1094 (eq (org-element-type (car (org-element-contents item
)))
1099 (and counter
(format "[@%d] " counter
))
1104 (and tag
(format "%s :: " tag
))
1105 (let ((contents (replace-regexp-in-string
1106 "\\(^\\)[ \t]*\\S-" ind contents nil nil
1)))
1107 (if item-starts-with-par-p
(org-trim contents
)
1108 (concat "\n" contents
))))))
1113 (defun org-element-plain-list-parser (limit affiliated structure
)
1114 "Parse a plain list.
1116 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1117 the buffer position at the beginning of the first affiliated
1118 keyword and CDR is a plist of affiliated keywords along with
1119 their value. STRUCTURE is the structure of the plain list being
1122 Return a list whose CAR is `plain-list' and CDR is a plist
1123 containing `:type', `:begin', `:end', `:contents-begin' and
1124 `:contents-end', `:structure' and `:post-blank' keywords.
1126 Assume point is at the beginning of the list."
1128 (let* ((struct (or structure
(org-list-struct)))
1129 (prevs (org-list-prevs-alist struct
))
1130 (parents (org-list-parents-alist struct
))
1131 (type (org-list-get-list-type (point) struct prevs
))
1132 (contents-begin (point))
1133 (begin (car affiliated
))
1135 (progn (goto-char (org-list-get-list-end (point) struct prevs
))
1136 (unless (bolp) (forward-line))
1138 (end (progn (skip-chars-forward " \r\t\n" limit
)
1139 (skip-chars-backward " \t")
1140 (if (bolp) (point) (line-end-position)))))
1147 :contents-begin contents-begin
1148 :contents-end contents-end
1150 :post-blank
(count-lines contents-end end
))
1151 (cdr affiliated
))))))
1153 (defun org-element-plain-list-interpreter (plain-list contents
)
1154 "Interpret PLAIN-LIST element as Org syntax.
1155 CONTENTS is the contents of the element."
1158 (goto-char (point-min))
1163 ;;;; Property Drawer
1165 (defun org-element-property-drawer-parser (limit affiliated
)
1166 "Parse a property drawer.
1168 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1169 the buffer position at the beginning of the first affiliated
1170 keyword and CDR is a plist of affiliated keywords along with
1173 Return a list whose CAR is `property-drawer' and CDR is a plist
1174 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1175 `:contents-end' and `:post-blank' keywords.
1177 Assume point is at the beginning of the property drawer."
1179 (let ((case-fold-search t
))
1180 (if (not (save-excursion
1181 (re-search-forward "^[ \t]*:END:[ \t]*$" limit t
)))
1182 ;; Incomplete drawer: parse it as a paragraph.
1183 (org-element-paragraph-parser limit affiliated
)
1185 (let* ((drawer-end-line (match-beginning 0))
1186 (begin (car affiliated
))
1187 (contents-begin (progn (forward-line)
1188 (and (< (point) drawer-end-line
)
1190 (contents-end (and contents-begin drawer-end-line
))
1191 (hidden (org-invisible-p2))
1192 (pos-before-blank (progn (goto-char drawer-end-line
)
1195 (end (progn (skip-chars-forward " \r\t\n" limit
)
1196 (skip-chars-backward " \t")
1197 (if (bolp) (point) (line-end-position)))))
1198 (list 'property-drawer
1203 :contents-begin contents-begin
1204 :contents-end contents-end
1205 :post-blank
(count-lines pos-before-blank end
))
1206 (cdr affiliated
)))))))))
1208 (defun org-element-property-drawer-interpreter (property-drawer contents
)
1209 "Interpret PROPERTY-DRAWER element as Org syntax.
1210 CONTENTS is the properties within the drawer."
1211 (format ":PROPERTIES:\n%s:END:" contents
))
1216 (defun org-element-quote-block-parser (limit affiliated
)
1217 "Parse a quote block.
1219 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1220 the buffer position at the beginning of the first affiliated
1221 keyword and CDR is a plist of affiliated keywords along with
1224 Return a list whose CAR is `quote-block' and CDR is a plist
1225 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1226 `:contents-end' and `:post-blank' keywords.
1228 Assume point is at the beginning of the block."
1229 (let ((case-fold-search t
))
1230 (if (not (save-excursion
1231 (re-search-forward "^[ \t]*#\\+END_QUOTE[ \t]*$" limit t
)))
1232 ;; Incomplete block: parse it as a paragraph.
1233 (org-element-paragraph-parser limit affiliated
)
1234 (let ((block-end-line (match-beginning 0)))
1236 (let* ((begin (car affiliated
))
1237 ;; Empty blocks have no contents.
1238 (contents-begin (progn (forward-line)
1239 (and (< (point) block-end-line
)
1241 (contents-end (and contents-begin block-end-line
))
1242 (hidden (org-invisible-p2))
1243 (pos-before-blank (progn (goto-char block-end-line
)
1246 (end (progn (skip-chars-forward " \r\t\n" limit
)
1247 (skip-chars-backward " \t")
1248 (if (bolp) (point) (line-end-position)))))
1254 :contents-begin contents-begin
1255 :contents-end contents-end
1256 :post-blank
(count-lines pos-before-blank end
))
1257 (cdr affiliated
)))))))))
1259 (defun org-element-quote-block-interpreter (quote-block contents
)
1260 "Interpret QUOTE-BLOCK element as Org syntax.
1261 CONTENTS is the contents of the element."
1262 (format "#+BEGIN_QUOTE\n%s#+END_QUOTE" contents
))
1267 (defun org-element-section-parser (limit)
1270 LIMIT bounds the search.
1272 Return a list whose CAR is `section' and CDR is a plist
1273 containing `:begin', `:end', `:contents-begin', `contents-end'
1274 and `:post-blank' keywords."
1276 ;; Beginning of section is the beginning of the first non-blank
1277 ;; line after previous headline.
1278 (let ((begin (point))
1279 (end (progn (org-with-limited-levels (outline-next-heading))
1281 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
1287 :contents-begin begin
1288 :contents-end pos-before-blank
1289 :post-blank
(count-lines pos-before-blank end
))))))
1291 (defun org-element-section-interpreter (section contents
)
1292 "Interpret SECTION element as Org syntax.
1293 CONTENTS is the contents of the element."
1299 (defun org-element-special-block-parser (limit affiliated
)
1300 "Parse a special block.
1302 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1303 the buffer position at the beginning of the first affiliated
1304 keyword and CDR is a plist of affiliated keywords along with
1307 Return a list whose CAR is `special-block' and CDR is a plist
1308 containing `:type', `:begin', `:end', `:hiddenp',
1309 `:contents-begin', `:contents-end' and `:post-blank' keywords.
1311 Assume point is at the beginning of the block."
1312 (let* ((case-fold-search t
)
1313 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(S-+\\)")
1314 (upcase (match-string-no-properties 1)))))
1315 (if (not (save-excursion
1317 (format "^[ \t]*#\\+END_%s[ \t]*$" type
) limit t
)))
1318 ;; Incomplete block: parse it as a paragraph.
1319 (org-element-paragraph-parser limit affiliated
)
1320 (let ((block-end-line (match-beginning 0)))
1322 (let* ((begin (car affiliated
))
1323 ;; Empty blocks have no contents.
1324 (contents-begin (progn (forward-line)
1325 (and (< (point) block-end-line
)
1327 (contents-end (and contents-begin block-end-line
))
1328 (hidden (org-invisible-p2))
1329 (pos-before-blank (progn (goto-char block-end-line
)
1332 (end (progn (skip-chars-forward " \r\t\n" limit
)
1333 (skip-chars-backward " \t")
1334 (if (bolp) (point) (line-end-position)))))
1335 (list 'special-block
1341 :contents-begin contents-begin
1342 :contents-end contents-end
1343 :post-blank
(count-lines pos-before-blank end
))
1344 (cdr affiliated
)))))))))
1346 (defun org-element-special-block-interpreter (special-block contents
)
1347 "Interpret SPECIAL-BLOCK element as Org syntax.
1348 CONTENTS is the contents of the element."
1349 (let ((block-type (org-element-property :type special-block
)))
1350 (format "#+BEGIN_%s\n%s#+END_%s" block-type contents block-type
)))
1356 ;; For each element, a parser and an interpreter are also defined.
1357 ;; Both follow the same naming convention used for greater elements.
1359 ;; Also, as for greater elements, adding a new element type is done
1360 ;; through the following steps: implement a parser and an interpreter,
1361 ;; tweak `org-element--current-element' so that it recognizes the new
1362 ;; type and add that new type to `org-element-all-elements'.
1364 ;; As a special case, when the newly defined type is a block type,
1365 ;; `org-element-block-name-alist' has to be modified accordingly.
1370 (defun org-element-babel-call-parser (limit affiliated
)
1371 "Parse a babel call.
1373 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1374 the buffer position at the beginning of the first affiliated
1375 keyword and CDR is a plist of affiliated keywords along with
1378 Return a list whose CAR is `babel-call' and CDR is a plist
1379 containing `:begin', `:end', `:info' and `:post-blank' as
1382 (let ((case-fold-search t
)
1383 (info (progn (looking-at org-babel-block-lob-one-liner-regexp
)
1384 (org-babel-lob-get-info)))
1385 (begin (car affiliated
))
1386 (pos-before-blank (progn (forward-line) (point)))
1387 (end (progn (skip-chars-forward " \r\t\n" limit
)
1388 (skip-chars-backward " \t")
1389 (if (bolp) (point) (line-end-position)))))
1395 :post-blank
(count-lines pos-before-blank end
))
1396 (cdr affiliated
))))))
1398 (defun org-element-babel-call-interpreter (babel-call contents
)
1399 "Interpret BABEL-CALL element as Org syntax.
1401 (let* ((babel-info (org-element-property :info babel-call
))
1402 (main (car babel-info
))
1403 (post-options (nth 1 babel-info
)))
1405 (if (not (string-match "\\[\\(\\[.*?\\]\\)\\]" main
)) main
1406 ;; Remove redundant square brackets.
1407 (replace-match (match-string 1 main
) nil nil main
))
1408 (and post-options
(format "[%s]" post-options
)))))
1413 (defun org-element-clock-parser (limit)
1416 LIMIT bounds the search.
1418 Return a list whose CAR is `clock' and CDR is a plist containing
1419 `:status', `:value', `:time', `:begin', `:end' and `:post-blank'
1422 (let* ((case-fold-search nil
)
1424 (value (progn (search-forward org-clock-string
(line-end-position) t
)
1425 (org-skip-whitespace)
1426 (looking-at "\\[.*\\]")
1427 (org-match-string-no-properties 0)))
1428 (time (and (progn (goto-char (match-end 0))
1429 (looking-at " +=> +\\(\\S-+\\)[ \t]*$"))
1430 (org-match-string-no-properties 1)))
1431 (status (if time
'closed
'running
))
1432 (post-blank (let ((before-blank (progn (forward-line) (point))))
1433 (skip-chars-forward " \r\t\n" limit
)
1434 (skip-chars-backward " \t")
1435 (unless (bolp) (end-of-line))
1436 (count-lines before-blank
(point))))
1439 (list :status status
1444 :post-blank post-blank
)))))
1446 (defun org-element-clock-interpreter (clock contents
)
1447 "Interpret CLOCK element as Org syntax.
1449 (concat org-clock-string
" "
1450 (org-element-property :value clock
)
1451 (let ((time (org-element-property :time clock
)))
1456 (org-split-string time
":")))))))
1461 (defun org-element-comment-parser (limit affiliated
)
1464 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1465 the buffer position at the beginning of the first affiliated
1466 keyword and CDR is a plist of affiliated keywords along with
1469 Return a list whose CAR is `comment' and CDR is a plist
1470 containing `:begin', `:end', `:value' and `:post-blank'
1473 Assume point is at comment beginning."
1475 (let* ((begin (car affiliated
))
1476 (value (prog2 (looking-at "[ \t]*# ?")
1477 (buffer-substring-no-properties
1478 (match-end 0) (line-end-position))
1481 ;; Get comments ending.
1483 (while (and (< (point) limit
) (looking-at "[ \t]*#\\( \\|$\\)"))
1484 ;; Accumulate lines without leading hash and first
1489 (buffer-substring-no-properties
1490 (match-end 0) (line-end-position))))
1493 (end (progn (goto-char com-end
)
1494 (skip-chars-forward " \r\t\n" limit
)
1495 (skip-chars-backward " \t")
1496 (if (bolp) (point) (line-end-position)))))
1502 :post-blank
(count-lines com-end end
))
1503 (cdr affiliated
))))))
1505 (defun org-element-comment-interpreter (comment contents
)
1506 "Interpret COMMENT element as Org syntax.
1508 (replace-regexp-in-string "^" "# " (org-element-property :value comment
)))
1513 (defun org-element-comment-block-parser (limit affiliated
)
1514 "Parse an export block.
1516 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1517 the buffer position at the beginning of the first affiliated
1518 keyword and CDR is a plist of affiliated keywords along with
1521 Return a list whose CAR is `comment-block' and CDR is a plist
1522 containing `:begin', `:end', `:hiddenp', `:value' and
1523 `:post-blank' keywords.
1525 Assume point is at comment block beginning."
1526 (let ((case-fold-search t
))
1527 (if (not (save-excursion
1528 (re-search-forward "^[ \t]*#\\+END_COMMENT[ \t]*$" limit t
)))
1529 ;; Incomplete block: parse it as a paragraph.
1530 (org-element-paragraph-parser limit affiliated
)
1531 (let ((contents-end (match-beginning 0)))
1533 (let* ((begin (car affiliated
))
1534 (contents-begin (progn (forward-line) (point)))
1535 (hidden (org-invisible-p2))
1536 (pos-before-blank (progn (goto-char contents-end
)
1539 (end (progn (skip-chars-forward " \r\t\n" limit
)
1540 (skip-chars-backward " \t")
1541 (if (bolp) (point) (line-end-position))))
1542 (value (buffer-substring-no-properties
1543 contents-begin contents-end
)))
1544 (list 'comment-block
1550 :post-blank
(count-lines pos-before-blank end
))
1551 (cdr affiliated
)))))))))
1553 (defun org-element-comment-block-interpreter (comment-block contents
)
1554 "Interpret COMMENT-BLOCK element as Org syntax.
1556 (format "#+BEGIN_COMMENT\n%s#+END_COMMENT"
1557 (org-remove-indentation (org-element-property :value comment-block
))))
1562 (defun org-element-example-block-parser (limit affiliated
)
1563 "Parse an example block.
1565 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1566 the buffer position at the beginning of the first affiliated
1567 keyword and CDR is a plist of affiliated keywords along with
1570 Return a list whose CAR is `example-block' and CDR is a plist
1571 containing `:begin', `:end', `:number-lines', `:preserve-indent',
1572 `:retain-labels', `:use-labels', `:label-fmt', `:hiddenp',
1573 `:switches', `:value' and `:post-blank' keywords."
1574 (let ((case-fold-search t
))
1575 (if (not (save-excursion
1576 (re-search-forward "^[ \t]*#\\+END_EXAMPLE[ \t]*$" limit t
)))
1577 ;; Incomplete block: parse it as a paragraph.
1578 (org-element-paragraph-parser limit affiliated
)
1579 (let ((contents-end (match-beginning 0)))
1582 (progn (looking-at "^[ \t]*#\\+BEGIN_EXAMPLE\\(?: +\\(.*\\)\\)?")
1583 (org-match-string-no-properties 1)))
1584 ;; Switches analysis
1585 (number-lines (cond ((not switches
) nil
)
1586 ((string-match "-n\\>" switches
) 'new
)
1587 ((string-match "+n\\>" switches
) 'continued
)))
1588 (preserve-indent (and switches
(string-match "-i\\>" switches
)))
1589 ;; Should labels be retained in (or stripped from) example
1593 (not (string-match "-r\\>" switches
))
1594 (and number-lines
(string-match "-k\\>" switches
))))
1595 ;; What should code-references use - labels or
1599 (and retain-labels
(not (string-match "-k\\>" switches
)))))
1600 (label-fmt (and switches
1601 (string-match "-l +\"\\([^\"\n]+\\)\"" switches
)
1602 (match-string 1 switches
)))
1603 ;; Standard block parsing.
1604 (begin (car affiliated
))
1605 (contents-begin (progn (forward-line) (point)))
1606 (hidden (org-invisible-p2))
1607 (value (org-unescape-code-in-string
1608 (buffer-substring-no-properties
1609 contents-begin contents-end
)))
1610 (pos-before-blank (progn (goto-char contents-end
)
1613 (end (progn (skip-chars-forward " \r\t\n" limit
)
1614 (skip-chars-backward " \t")
1615 (if (bolp) (point) (line-end-position)))))
1616 (list 'example-block
1622 :number-lines number-lines
1623 :preserve-indent preserve-indent
1624 :retain-labels retain-labels
1625 :use-labels use-labels
1626 :label-fmt label-fmt
1628 :post-blank
(count-lines pos-before-blank end
))
1629 (cdr affiliated
)))))))))
1631 (defun org-element-example-block-interpreter (example-block contents
)
1632 "Interpret EXAMPLE-BLOCK element as Org syntax.
1634 (let ((switches (org-element-property :switches example-block
)))
1635 (concat "#+BEGIN_EXAMPLE" (and switches
(concat " " switches
)) "\n"
1636 (org-remove-indentation
1637 (org-escape-code-in-string
1638 (org-element-property :value example-block
)))
1644 (defun org-element-export-block-parser (limit affiliated
)
1645 "Parse an export block.
1647 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1648 the buffer position at the beginning of the first affiliated
1649 keyword and CDR is a plist of affiliated keywords along with
1652 Return a list whose CAR is `export-block' and CDR is a plist
1653 containing `:begin', `:end', `:type', `:hiddenp', `:value' and
1654 `:post-blank' keywords.
1656 Assume point is at export-block beginning."
1657 (let* ((case-fold-search t
)
1658 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
1659 (upcase (org-match-string-no-properties 1)))))
1660 (if (not (save-excursion
1662 (format "^[ \t]*#\\+END_%s[ \t]*$" type
) limit t
)))
1663 ;; Incomplete block: parse it as a paragraph.
1664 (org-element-paragraph-parser limit affiliated
)
1665 (let ((contents-end (match-beginning 0)))
1667 (let* ((begin (car affiliated
))
1668 (contents-begin (progn (forward-line) (point)))
1669 (hidden (org-invisible-p2))
1670 (pos-before-blank (progn (goto-char contents-end
)
1673 (end (progn (skip-chars-forward " \r\t\n" limit
)
1674 (skip-chars-backward " \t")
1675 (if (bolp) (point) (line-end-position))))
1676 (value (buffer-substring-no-properties contents-begin
1685 :post-blank
(count-lines pos-before-blank end
))
1686 (cdr affiliated
)))))))))
1688 (defun org-element-export-block-interpreter (export-block contents
)
1689 "Interpret EXPORT-BLOCK element as Org syntax.
1691 (let ((type (org-element-property :type export-block
)))
1692 (concat (format "#+BEGIN_%s\n" type
)
1693 (org-element-property :value export-block
)
1694 (format "#+END_%s" type
))))
1699 (defun org-element-fixed-width-parser (limit affiliated
)
1700 "Parse a fixed-width section.
1702 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1703 the buffer position at the beginning of the first affiliated
1704 keyword and CDR is a plist of affiliated keywords along with
1707 Return a list whose CAR is `fixed-width' and CDR is a plist
1708 containing `:begin', `:end', `:value' and `:post-blank' keywords.
1710 Assume point is at the beginning of the fixed-width area."
1712 (let* ((begin (car affiliated
))
1716 (while (and (< (point) limit
)
1717 (looking-at "[ \t]*:\\( \\|$\\)"))
1718 ;; Accumulate text without starting colons.
1721 (buffer-substring-no-properties
1722 (match-end 0) (point-at-eol))
1726 (end (progn (skip-chars-forward " \r\t\n" limit
)
1727 (skip-chars-backward " \t")
1728 (if (bolp) (point) (line-end-position)))))
1734 :post-blank
(count-lines end-area end
))
1735 (cdr affiliated
))))))
1737 (defun org-element-fixed-width-interpreter (fixed-width contents
)
1738 "Interpret FIXED-WIDTH element as Org syntax.
1740 (replace-regexp-in-string
1741 "^" ": " (substring (org-element-property :value fixed-width
) 0 -
1)))
1744 ;;;; Horizontal Rule
1746 (defun org-element-horizontal-rule-parser (limit affiliated
)
1747 "Parse an horizontal rule.
1749 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1750 the buffer position at the beginning of the first affiliated
1751 keyword and CDR is a plist of affiliated keywords along with
1754 Return a list whose CAR is `horizontal-rule' and CDR is a plist
1755 containing `:begin', `:end' and `:post-blank' keywords."
1757 (let ((begin (car affiliated
))
1758 (post-hr (progn (forward-line) (point)))
1759 (end (progn (skip-chars-forward " \r\t\n" limit
)
1760 (skip-chars-backward " \t")
1761 (if (bolp) (point) (line-end-position)))))
1762 (list 'horizontal-rule
1766 :post-blank
(count-lines post-hr end
))
1767 (cdr affiliated
))))))
1769 (defun org-element-horizontal-rule-interpreter (horizontal-rule contents
)
1770 "Interpret HORIZONTAL-RULE element as Org syntax.
1777 (defun org-element-keyword-parser (limit affiliated
)
1778 "Parse a keyword at point.
1780 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1781 the buffer position at the beginning of the first affiliated
1782 keyword and CDR is a plist of affiliated keywords along with
1785 Return a list whose CAR is `keyword' and CDR is a plist
1786 containing `:key', `:value', `:begin', `:end' and `:post-blank'
1789 (let ((case-fold-search t
)
1790 (begin (car affiliated
))
1791 (key (progn (looking-at "[ \t]*#\\+\\(\\S-+*\\):")
1792 (upcase (org-match-string-no-properties 1))))
1793 (value (org-trim (buffer-substring-no-properties
1794 (match-end 0) (point-at-eol))))
1795 (pos-before-blank (progn (forward-line) (point)))
1796 (end (progn (skip-chars-forward " \r\t\n" limit
)
1797 (skip-chars-backward " \t")
1798 (if (bolp) (point) (line-end-position)))))
1805 :post-blank
(count-lines pos-before-blank end
))
1806 (cdr affiliated
))))))
1808 (defun org-element-keyword-interpreter (keyword contents
)
1809 "Interpret KEYWORD element as Org syntax.
1812 (org-element-property :key keyword
)
1813 (org-element-property :value keyword
)))
1816 ;;;; Latex Environment
1818 (defun org-element-latex-environment-parser (limit affiliated
)
1819 "Parse a LaTeX environment.
1821 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1822 the buffer position at the beginning of the first affiliated
1823 keyword and CDR is a plist of affiliated keywords along with
1826 Return a list whose CAR is `latex-environment' and CDR is a plist
1827 containing `:begin', `:end', `:value' and `:post-blank'
1830 Assume point is at the beginning of the latex environment."
1832 (let* ((case-fold-search t
)
1833 (code-begin (point))
1834 (begin (car affiliated
))
1835 (env (progn (looking-at "^[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}")
1836 (regexp-quote (match-string 1))))
1838 (progn (re-search-forward
1839 (format "^[ \t]*\\\\end{%s}[ \t]*$" env
) limit t
)
1842 (value (buffer-substring-no-properties code-begin code-end
))
1843 (end (progn (skip-chars-forward " \r\t\n" limit
)
1844 (skip-chars-backward " \t")
1845 (if (bolp) (point) (line-end-position)))))
1846 (list 'latex-environment
1851 :post-blank
(count-lines code-end end
))
1852 (cdr affiliated
))))))
1854 (defun org-element-latex-environment-interpreter (latex-environment contents
)
1855 "Interpret LATEX-ENVIRONMENT element as Org syntax.
1857 (org-element-property :value latex-environment
))
1862 (defun org-element-node-property-parser (limit)
1863 "Parse a node-property at point.
1865 LIMIT bounds the search.
1867 Return a list whose CAR is `node-property' and CDR is a plist
1868 containing `:key', `:value', `:begin', `:end' and `:post-blank'
1871 (let ((case-fold-search t
)
1873 (key (progn (looking-at "[ \t]*:\\(.*?\\):[ \t]+\\(.*?\\)[ \t]*$")
1874 (org-match-string-no-properties 1)))
1875 (value (org-match-string-no-properties 2))
1876 (pos-before-blank (progn (forward-line) (point)))
1877 (end (progn (skip-chars-forward " \r\t\n" limit
)
1878 (if (eobp) (point) (point-at-bol)))))
1879 (list 'node-property
1884 :post-blank
(count-lines pos-before-blank end
))))))
1886 (defun org-element-node-property-interpreter (node-property contents
)
1887 "Interpret NODE-PROPERTY element as Org syntax.
1889 (format org-property-format
1890 (format ":%s:" (org-element-property :key node-property
))
1891 (org-element-property :value node-property
)))
1896 (defun org-element-paragraph-parser (limit affiliated
)
1899 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1900 the buffer position at the beginning of the first affiliated
1901 keyword and CDR is a plist of affiliated keywords along with
1904 Return a list whose CAR is `paragraph' and CDR is a plist
1905 containing `:begin', `:end', `:contents-begin' and
1906 `:contents-end' and `:post-blank' keywords.
1908 Assume point is at the beginning of the paragraph."
1910 (let* ((begin (car affiliated
))
1911 (contents-begin (point))
1913 (let ((case-fold-search t
))
1915 (if (not (re-search-forward
1916 org-element-paragraph-separate limit
'm
))
1918 ;; A matching `org-element-paragraph-separate' is not
1919 ;; necessarily the end of the paragraph. In
1920 ;; particular, lines starting with # or : as a first
1921 ;; non-space character are ambiguous. We have check
1922 ;; if they are valid Org syntax (i.e. not an
1923 ;; incomplete keyword).
1927 ;; There's no ambiguity for other symbols or
1928 ;; empty lines: stop here.
1929 (looking-at "[ \t]*\\(?:[^:#]\\|$\\)")
1930 ;; Stop at valid fixed-width areas.
1931 (looking-at "[ \t]*:\\(?: \\|$\\)")
1933 (and (looking-at org-drawer-regexp
)
1936 "^[ \t]*:END:[ \t]*$" limit t
)))
1937 ;; Stop at valid comments.
1938 (looking-at "[ \t]*#\\(?: \\|$\\)")
1939 ;; Stop at valid dynamic blocks.
1940 (and (looking-at org-dblock-start-re
)
1943 "^[ \t]*#\\+END:?[ \t]*$" limit t
)))
1944 ;; Stop at valid blocks.
1946 "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
1949 (format "^[ \t]*#\\+END_%s[ \t]*$"
1952 ;; Stop at valid latex environments.
1954 "^[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}[ \t]*$")
1957 (format "^[ \t]*\\\\end{%s}[ \t]*$"
1960 ;; Stop at valid keywords.
1961 (looking-at "[ \t]*#\\+\\S-+:")
1962 ;; Skip everything else.
1966 (re-search-forward org-element-paragraph-separate
1968 (beginning-of-line)))
1969 (if (= (point) limit
) limit
1970 (goto-char (line-beginning-position)))))
1971 (contents-end (progn (skip-chars-backward " \r\t\n" contents-begin
)
1974 (end (progn (skip-chars-forward " \r\t\n" limit
)
1975 (skip-chars-backward " \t")
1976 (if (bolp) (point) (line-end-position)))))
1981 :contents-begin contents-begin
1982 :contents-end contents-end
1983 :post-blank
(count-lines before-blank end
))
1984 (cdr affiliated
))))))
1986 (defun org-element-paragraph-interpreter (paragraph contents
)
1987 "Interpret PARAGRAPH element as Org syntax.
1988 CONTENTS is the contents of the element."
1994 (defun org-element-planning-parser (limit)
1997 LIMIT bounds the search.
1999 Return a list whose CAR is `planning' and CDR is a plist
2000 containing `:closed', `:deadline', `:scheduled', `:begin', `:end'
2001 and `:post-blank' keywords."
2003 (let* ((case-fold-search nil
)
2005 (post-blank (let ((before-blank (progn (forward-line) (point))))
2006 (skip-chars-forward " \r\t\n" limit
)
2007 (skip-chars-backward " \t")
2008 (unless (bolp) (end-of-line))
2009 (count-lines before-blank
(point))))
2011 closed deadline scheduled
)
2013 (while (re-search-forward org-keyword-time-not-clock-regexp
2014 (line-end-position) t
)
2015 (goto-char (match-end 1))
2016 (org-skip-whitespace)
2017 (let ((time (buffer-substring-no-properties
2018 (1+ (point)) (1- (match-end 0))))
2019 (keyword (match-string 1)))
2020 (cond ((equal keyword org-closed-string
) (setq closed time
))
2021 ((equal keyword org-deadline-string
) (setq deadline time
))
2022 (t (setq scheduled time
)))))
2024 (list :closed closed
2026 :scheduled scheduled
2029 :post-blank post-blank
)))))
2031 (defun org-element-planning-interpreter (planning contents
)
2032 "Interpret PLANNING element as Org syntax.
2037 (list (let ((closed (org-element-property :closed planning
)))
2038 (when closed
(concat org-closed-string
" [" closed
"]")))
2039 (let ((deadline (org-element-property :deadline planning
)))
2040 (when deadline
(concat org-deadline-string
" <" deadline
">")))
2041 (let ((scheduled (org-element-property :scheduled planning
)))
2043 (concat org-scheduled-string
" <" scheduled
">")))))
2049 (defun org-element-quote-section-parser (limit)
2050 "Parse a quote section.
2052 LIMIT bounds the search.
2054 Return a list whose CAR is `quote-section' and CDR is a plist
2055 containing `:begin', `:end', `:value' and `:post-blank' keywords.
2057 Assume point is at beginning of the section."
2059 (let* ((begin (point))
2060 (end (progn (org-with-limited-levels (outline-next-heading))
2062 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
2065 (value (buffer-substring-no-properties begin pos-before-blank
)))
2066 (list 'quote-section
2070 :post-blank
(count-lines pos-before-blank end
))))))
2072 (defun org-element-quote-section-interpreter (quote-section contents
)
2073 "Interpret QUOTE-SECTION element as Org syntax.
2075 (org-element-property :value quote-section
))
2080 (defun org-element-src-block-parser (limit affiliated
)
2083 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2084 the buffer position at the beginning of the first affiliated
2085 keyword and CDR is a plist of affiliated keywords along with
2088 Return a list whose CAR is `src-block' and CDR is a plist
2089 containing `:language', `:switches', `:parameters', `:begin',
2090 `:end', `:hiddenp', `:number-lines', `:retain-labels',
2091 `:use-labels', `:label-fmt', `:preserve-indent', `:value' and
2092 `:post-blank' keywords.
2094 Assume point is at the beginning of the block."
2095 (let ((case-fold-search t
))
2096 (if (not (save-excursion (re-search-forward "^[ \t]*#\\+END_SRC[ \t]*$"
2098 ;; Incomplete block: parse it as a paragraph.
2099 (org-element-paragraph-parser limit affiliated
)
2100 (let ((contents-end (match-beginning 0)))
2102 (let* ((begin (car affiliated
))
2103 ;; Get language as a string.
2107 (concat "^[ \t]*#\\+BEGIN_SRC"
2108 "\\(?: +\\(\\S-+\\)\\)?"
2109 "\\(\\(?: +\\(?:-l \".*?\"\\|[-+][A-Za-z]\\)\\)+\\)?"
2111 (org-match-string-no-properties 1)))
2113 (switches (org-match-string-no-properties 2))
2115 (parameters (org-match-string-no-properties 3))
2116 ;; Switches analysis
2117 (number-lines (cond ((not switches
) nil
)
2118 ((string-match "-n\\>" switches
) 'new
)
2119 ((string-match "+n\\>" switches
) 'continued
)))
2120 (preserve-indent (and switches
(string-match "-i\\>" switches
)))
2121 (label-fmt (and switches
2122 (string-match "-l +\"\\([^\"\n]+\\)\"" switches
)
2123 (match-string 1 switches
)))
2124 ;; Should labels be retained in (or stripped from)
2128 (not (string-match "-r\\>" switches
))
2129 (and number-lines
(string-match "-k\\>" switches
))))
2130 ;; What should code-references use - labels or
2134 (and retain-labels
(not (string-match "-k\\>" switches
)))))
2135 ;; Get visibility status.
2136 (hidden (progn (forward-line) (org-invisible-p2)))
2138 (value (org-unescape-code-in-string
2139 (buffer-substring-no-properties (point) contents-end
)))
2140 (pos-before-blank (progn (goto-char contents-end
)
2143 ;; Get position after ending blank lines.
2144 (end (progn (skip-chars-forward " \r\t\n" limit
)
2145 (skip-chars-backward " \t")
2146 (if (bolp) (point) (line-end-position)))))
2149 (list :language language
2150 :switches
(and (org-string-nw-p switches
)
2151 (org-trim switches
))
2152 :parameters
(and (org-string-nw-p parameters
)
2153 (org-trim parameters
))
2156 :number-lines number-lines
2157 :preserve-indent preserve-indent
2158 :retain-labels retain-labels
2159 :use-labels use-labels
2160 :label-fmt label-fmt
2163 :post-blank
(count-lines pos-before-blank end
))
2164 (cdr affiliated
)))))))))
2166 (defun org-element-src-block-interpreter (src-block contents
)
2167 "Interpret SRC-BLOCK element as Org syntax.
2169 (let ((lang (org-element-property :language src-block
))
2170 (switches (org-element-property :switches src-block
))
2171 (params (org-element-property :parameters src-block
))
2172 (value (let ((val (org-element-property :value src-block
)))
2174 (org-src-preserve-indentation val
)
2175 ((zerop org-edit-src-content-indentation
)
2176 (org-remove-indentation val
))
2178 (let ((ind (make-string
2179 org-edit-src-content-indentation
32)))
2180 (replace-regexp-in-string
2181 "\\(^\\)[ \t]*\\S-" ind
2182 (org-remove-indentation val
) nil nil
1)))))))
2183 (concat (format "#+BEGIN_SRC%s\n"
2184 (concat (and lang
(concat " " lang
))
2185 (and switches
(concat " " switches
))
2186 (and params
(concat " " params
))))
2187 (org-escape-code-in-string value
)
2193 (defun org-element-table-parser (limit affiliated
)
2194 "Parse a table at point.
2196 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2197 the buffer position at the beginning of the first affiliated
2198 keyword and CDR is a plist of affiliated keywords along with
2201 Return a list whose CAR is `table' and CDR is a plist containing
2202 `:begin', `:end', `:tblfm', `:type', `:contents-begin',
2203 `:contents-end', `:value' and `:post-blank' keywords.
2205 Assume point is at the beginning of the table."
2207 (let* ((case-fold-search t
)
2208 (table-begin (point))
2209 (type (if (org-at-table.el-p
) 'table.el
'org
))
2210 (begin (car affiliated
))
2212 (if (re-search-forward org-table-any-border-regexp limit
'm
)
2213 (goto-char (match-beginning 0))
2216 (while (looking-at "[ \t]*#\\+TBLFM: +\\(.*\\)[ \t]*$")
2217 (push (org-match-string-no-properties 1) acc
)
2220 (pos-before-blank (point))
2221 (end (progn (skip-chars-forward " \r\t\n" limit
)
2222 (skip-chars-backward " \t")
2223 (if (bolp) (point) (line-end-position)))))
2230 ;; Only `org' tables have contents. `table.el' tables
2231 ;; use a `:value' property to store raw table as
2233 :contents-begin
(and (eq type
'org
) table-begin
)
2234 :contents-end
(and (eq type
'org
) table-end
)
2235 :value
(and (eq type
'table.el
)
2236 (buffer-substring-no-properties
2237 table-begin table-end
))
2238 :post-blank
(count-lines pos-before-blank end
))
2239 (cdr affiliated
))))))
2241 (defun org-element-table-interpreter (table contents
)
2242 "Interpret TABLE element as Org syntax.
2244 (if (eq (org-element-property :type table
) 'table.el
)
2245 (org-remove-indentation (org-element-property :value table
))
2246 (concat (with-temp-buffer (insert contents
)
2249 (mapconcat (lambda (fm) (concat "#+TBLFM: " fm
))
2250 (reverse (org-element-property :tblfm table
))
2256 (defun org-element-table-row-parser (limit)
2257 "Parse table row at point.
2259 LIMIT bounds the search.
2261 Return a list whose CAR is `table-row' and CDR is a plist
2262 containing `:begin', `:end', `:contents-begin', `:contents-end',
2263 `:type' and `:post-blank' keywords."
2265 (let* ((type (if (looking-at "^[ \t]*|-") 'rule
'standard
))
2267 ;; A table rule has no contents. In that case, ensure
2268 ;; CONTENTS-BEGIN matches CONTENTS-END.
2269 (contents-begin (and (eq type
'standard
)
2270 (search-forward "|")
2272 (contents-end (and (eq type
'standard
)
2275 (skip-chars-backward " \t")
2277 (end (progn (forward-line) (point))))
2282 :contents-begin contents-begin
2283 :contents-end contents-end
2286 (defun org-element-table-row-interpreter (table-row contents
)
2287 "Interpret TABLE-ROW element as Org syntax.
2288 CONTENTS is the contents of the table row."
2289 (if (eq (org-element-property :type table-row
) 'rule
) "|-"
2290 (concat "| " contents
)))
2295 (defun org-element-verse-block-parser (limit affiliated
)
2296 "Parse a verse block.
2298 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2299 the buffer position at the beginning of the first affiliated
2300 keyword and CDR is a plist of affiliated keywords along with
2303 Return a list whose CAR is `verse-block' and CDR is a plist
2304 containing `:begin', `:end', `:contents-begin', `:contents-end',
2305 `:hiddenp' and `:post-blank' keywords.
2307 Assume point is at beginning of the block."
2308 (let ((case-fold-search t
))
2309 (if (not (save-excursion
2310 (re-search-forward "^[ \t]*#\\+END_VERSE[ \t]*$" limit t
)))
2311 ;; Incomplete block: parse it as a paragraph.
2312 (org-element-paragraph-parser limit affiliated
)
2313 (let ((contents-end (match-beginning 0)))
2315 (let* ((begin (car affiliated
))
2316 (hidden (progn (forward-line) (org-invisible-p2)))
2317 (contents-begin (point))
2318 (pos-before-blank (progn (goto-char contents-end
)
2321 (end (progn (skip-chars-forward " \r\t\n" limit
)
2322 (skip-chars-backward " \t")
2323 (if (bolp) (point) (line-end-position)))))
2328 :contents-begin contents-begin
2329 :contents-end contents-end
2331 :post-blank
(count-lines pos-before-blank end
))
2332 (cdr affiliated
)))))))))
2334 (defun org-element-verse-block-interpreter (verse-block contents
)
2335 "Interpret VERSE-BLOCK element as Org syntax.
2336 CONTENTS is verse block contents."
2337 (format "#+BEGIN_VERSE\n%s#+END_VERSE" contents
))
2343 ;; Unlike to elements, interstices can be found between objects.
2344 ;; That's why, along with the parser, successor functions are provided
2345 ;; for each object. Some objects share the same successor (i.e. `code'
2346 ;; and `verbatim' objects).
2348 ;; A successor must accept a single argument bounding the search. It
2349 ;; will return either a cons cell whose CAR is the object's type, as
2350 ;; a symbol, and CDR the position of its next occurrence, or nil.
2352 ;; Successors follow the naming convention:
2353 ;; org-element-NAME-successor, where NAME is the name of the
2354 ;; successor, as defined in `org-element-all-successors'.
2356 ;; Some object types (i.e. `italic') are recursive. Restrictions on
2357 ;; object types they can contain will be specified in
2358 ;; `org-element-object-restrictions'.
2360 ;; Adding a new type of object is simple. Implement a successor,
2361 ;; a parser, and an interpreter for it, all following the naming
2362 ;; convention. Register type in `org-element-all-objects' and
2363 ;; successor in `org-element-all-successors'. Maybe tweak
2364 ;; restrictions about it, and that's it.
2369 (defun org-element-bold-parser ()
2370 "Parse bold object at point.
2372 Return a list whose CAR is `bold' and CDR is a plist with
2373 `:begin', `:end', `:contents-begin' and `:contents-end' and
2374 `:post-blank' keywords.
2376 Assume point is at the first star marker."
2378 (unless (bolp) (backward-char 1))
2379 (looking-at org-emph-re
)
2380 (let ((begin (match-beginning 2))
2381 (contents-begin (match-beginning 4))
2382 (contents-end (match-end 4))
2383 (post-blank (progn (goto-char (match-end 2))
2384 (skip-chars-forward " \t")))
2389 :contents-begin contents-begin
2390 :contents-end contents-end
2391 :post-blank post-blank
)))))
2393 (defun org-element-bold-interpreter (bold contents
)
2394 "Interpret BOLD object as Org syntax.
2395 CONTENTS is the contents of the object."
2396 (format "*%s*" contents
))
2398 (defun org-element-text-markup-successor (limit)
2399 "Search for the next text-markup object.
2401 LIMIT bounds the search.
2403 Return value is a cons cell whose CAR is a symbol among `bold',
2404 `italic', `underline', `strike-through', `code' and `verbatim'
2405 and CDR is beginning position."
2407 (unless (bolp) (backward-char))
2408 (when (re-search-forward org-emph-re limit t
)
2409 (let ((marker (match-string 3)))
2411 ((equal marker
"*") 'bold
)
2412 ((equal marker
"/") 'italic
)
2413 ((equal marker
"_") 'underline
)
2414 ((equal marker
"+") 'strike-through
)
2415 ((equal marker
"~") 'code
)
2416 ((equal marker
"=") 'verbatim
)
2417 (t (error "Unknown marker at %d" (match-beginning 3))))
2418 (match-beginning 2))))))
2423 (defun org-element-code-parser ()
2424 "Parse code object at point.
2426 Return a list whose CAR is `code' and CDR is a plist with
2427 `:value', `:begin', `:end' and `:post-blank' keywords.
2429 Assume point is at the first tilde marker."
2431 (unless (bolp) (backward-char 1))
2432 (looking-at org-emph-re
)
2433 (let ((begin (match-beginning 2))
2434 (value (org-match-string-no-properties 4))
2435 (post-blank (progn (goto-char (match-end 2))
2436 (skip-chars-forward " \t")))
2442 :post-blank post-blank
)))))
2444 (defun org-element-code-interpreter (code contents
)
2445 "Interpret CODE object as Org syntax.
2447 (format "~%s~" (org-element-property :value code
)))
2452 (defun org-element-entity-parser ()
2453 "Parse entity at point.
2455 Return a list whose CAR is `entity' and CDR a plist with
2456 `:begin', `:end', `:latex', `:latex-math-p', `:html', `:latin1',
2457 `:utf-8', `:ascii', `:use-brackets-p' and `:post-blank' as
2460 Assume point is at the beginning of the entity."
2462 (looking-at "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)")
2463 (let* ((value (org-entity-get (match-string 1)))
2464 (begin (match-beginning 0))
2465 (bracketsp (string= (match-string 2) "{}"))
2466 (post-blank (progn (goto-char (match-end 1))
2467 (when bracketsp
(forward-char 2))
2468 (skip-chars-forward " \t")))
2471 (list :name
(car value
)
2472 :latex
(nth 1 value
)
2473 :latex-math-p
(nth 2 value
)
2475 :ascii
(nth 4 value
)
2476 :latin1
(nth 5 value
)
2477 :utf-8
(nth 6 value
)
2480 :use-brackets-p bracketsp
2481 :post-blank post-blank
)))))
2483 (defun org-element-entity-interpreter (entity contents
)
2484 "Interpret ENTITY object as Org syntax.
2487 (org-element-property :name entity
)
2488 (when (org-element-property :use-brackets-p entity
) "{}")))
2490 (defun org-element-latex-or-entity-successor (limit)
2491 "Search for the next latex-fragment or entity object.
2493 LIMIT bounds the search.
2495 Return value is a cons cell whose CAR is `entity' or
2496 `latex-fragment' and CDR is beginning position."
2499 (remove "begin" (plist-get org-format-latex-options
:matchers
)))
2500 ;; ENTITY-RE matches both LaTeX commands and Org entities.
2502 "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))
2503 (when (re-search-forward
2504 (concat (mapconcat (lambda (e) (nth 1 (assoc e org-latex-regexps
)))
2508 (goto-char (match-beginning 0))
2509 (if (looking-at entity-re
)
2510 ;; Determine if it's a real entity or a LaTeX command.
2511 (cons (if (org-entity-get (match-string 1)) 'entity
'latex-fragment
)
2512 (match-beginning 0))
2513 ;; No entity nor command: point is at a LaTeX fragment.
2514 ;; Determine its type to get the correct beginning position.
2515 (cons 'latex-fragment
2518 (when (looking-at (nth 1 (assoc e org-latex-regexps
)))
2521 (nth 2 (assoc e org-latex-regexps
))))))
2528 (defun org-element-export-snippet-parser ()
2529 "Parse export snippet at point.
2531 Return a list whose CAR is `export-snippet' and CDR a plist with
2532 `:begin', `:end', `:back-end', `:value' and `:post-blank' as
2535 Assume point is at the beginning of the snippet."
2537 (re-search-forward "@@\\([-A-Za-z0-9]+\\):" nil t
)
2538 (let* ((begin (match-beginning 0))
2539 (back-end (org-match-string-no-properties 1))
2540 (value (buffer-substring-no-properties
2542 (progn (re-search-forward "@@" nil t
) (match-beginning 0))))
2543 (post-blank (skip-chars-forward " \t"))
2545 (list 'export-snippet
2546 (list :back-end back-end
2550 :post-blank post-blank
)))))
2552 (defun org-element-export-snippet-interpreter (export-snippet contents
)
2553 "Interpret EXPORT-SNIPPET object as Org syntax.
2556 (org-element-property :back-end export-snippet
)
2557 (org-element-property :value export-snippet
)))
2559 (defun org-element-export-snippet-successor (limit)
2560 "Search for the next export-snippet object.
2562 LIMIT bounds the search.
2564 Return value is a cons cell whose CAR is `export-snippet' and CDR
2565 its beginning position."
2568 (when (and (re-search-forward "@@[-A-Za-z0-9]+:" limit t
)
2569 (setq beg
(match-beginning 0))
2570 (search-forward "@@" limit t
))
2571 (cons 'export-snippet beg
)))))
2574 ;;;; Footnote Reference
2576 (defun org-element-footnote-reference-parser ()
2577 "Parse footnote reference at point.
2579 Return a list whose CAR is `footnote-reference' and CDR a plist
2580 with `:label', `:type', `:inline-definition', `:begin', `:end'
2581 and `:post-blank' as keywords."
2583 (looking-at org-footnote-re
)
2584 (let* ((begin (point))
2585 (label (or (org-match-string-no-properties 2)
2586 (org-match-string-no-properties 3)
2587 (and (match-string 1)
2588 (concat "fn:" (org-match-string-no-properties 1)))))
2589 (type (if (or (not label
) (match-string 1)) 'inline
'standard
))
2590 (inner-begin (match-end 0))
2594 (while (and (> count
0) (re-search-forward "[][]" nil t
))
2595 (if (equal (match-string 0) "[") (incf count
) (decf count
)))
2597 (post-blank (progn (goto-char (1+ inner-end
))
2598 (skip-chars-forward " \t")))
2601 (list 'footnote-reference
2606 :post-blank post-blank
))))
2607 (org-element-put-property
2608 footnote-reference
:inline-definition
2609 (and (eq type
'inline
)
2610 (org-element-parse-secondary-string
2611 (buffer-substring inner-begin inner-end
)
2612 (org-element-restriction 'footnote-reference
)
2613 footnote-reference
))))))
2615 (defun org-element-footnote-reference-interpreter (footnote-reference contents
)
2616 "Interpret FOOTNOTE-REFERENCE object as Org syntax.
2618 (let ((label (or (org-element-property :label footnote-reference
) "fn:"))
2621 (org-element-property :inline-definition footnote-reference
)))
2622 (if (not inline-def
) ""
2623 (concat ":" (org-element-interpret-data inline-def
))))))
2624 (format "[%s]" (concat label def
))))
2626 (defun org-element-footnote-reference-successor (limit)
2627 "Search for the next footnote-reference object.
2629 LIMIT bounds the search.
2631 Return value is a cons cell whose CAR is `footnote-reference' and
2632 CDR is beginning position."
2635 (while (re-search-forward org-footnote-re limit t
)
2637 (let ((beg (match-beginning 0))
2640 (while (re-search-forward "[][]" limit t
)
2641 (if (equal (match-string 0) "[") (incf count
) (decf count
))
2643 (throw 'exit
(cons 'footnote-reference beg
))))))))))
2646 ;;;; Inline Babel Call
2648 (defun org-element-inline-babel-call-parser ()
2649 "Parse inline babel call at point.
2651 Return a list whose CAR is `inline-babel-call' and CDR a plist
2652 with `:begin', `:end', `:info' and `:post-blank' as keywords.
2654 Assume point is at the beginning of the babel call."
2656 (unless (bolp) (backward-char))
2657 (looking-at org-babel-inline-lob-one-liner-regexp
)
2658 (let ((info (save-match-data (org-babel-lob-get-info)))
2659 (begin (match-end 1))
2660 (post-blank (progn (goto-char (match-end 0))
2661 (skip-chars-forward " \t")))
2663 (list 'inline-babel-call
2667 :post-blank post-blank
)))))
2669 (defun org-element-inline-babel-call-interpreter (inline-babel-call contents
)
2670 "Interpret INLINE-BABEL-CALL object as Org syntax.
2672 (let* ((babel-info (org-element-property :info inline-babel-call
))
2673 (main-source (car babel-info
))
2674 (post-options (nth 1 babel-info
)))
2676 (if (string-match "\\[\\(\\[.*?\\]\\)\\]" main-source
)
2677 ;; Remove redundant square brackets.
2679 (match-string 1 main-source
) nil nil main-source
)
2681 (and post-options
(format "[%s]" post-options
)))))
2683 (defun org-element-inline-babel-call-successor (limit)
2684 "Search for the next inline-babel-call object.
2686 LIMIT bounds the search.
2688 Return value is a cons cell whose CAR is `inline-babel-call' and
2689 CDR is beginning position."
2691 ;; Use a simplified version of
2692 ;; `org-babel-inline-lob-one-liner-regexp'.
2693 (when (re-search-forward
2694 "call_\\([^()\n]+?\\)\\(?:\\[.*?\\]\\)?([^\n]*?)\\(\\[.*?\\]\\)?"
2696 (cons 'inline-babel-call
(match-beginning 0)))))
2699 ;;;; Inline Src Block
2701 (defun org-element-inline-src-block-parser ()
2702 "Parse inline source block at point.
2704 LIMIT bounds the search.
2706 Return a list whose CAR is `inline-src-block' and CDR a plist
2707 with `:begin', `:end', `:language', `:value', `:parameters' and
2708 `:post-blank' as keywords.
2710 Assume point is at the beginning of the inline src block."
2712 (unless (bolp) (backward-char))
2713 (looking-at org-babel-inline-src-block-regexp
)
2714 (let ((begin (match-beginning 1))
2715 (language (org-match-string-no-properties 2))
2716 (parameters (org-match-string-no-properties 4))
2717 (value (org-match-string-no-properties 5))
2718 (post-blank (progn (goto-char (match-end 0))
2719 (skip-chars-forward " \t")))
2721 (list 'inline-src-block
2722 (list :language language
2724 :parameters parameters
2727 :post-blank post-blank
)))))
2729 (defun org-element-inline-src-block-interpreter (inline-src-block contents
)
2730 "Interpret INLINE-SRC-BLOCK object as Org syntax.
2732 (let ((language (org-element-property :language inline-src-block
))
2733 (arguments (org-element-property :parameters inline-src-block
))
2734 (body (org-element-property :value inline-src-block
)))
2735 (format "src_%s%s{%s}"
2737 (if arguments
(format "[%s]" arguments
) "")
2740 (defun org-element-inline-src-block-successor (limit)
2741 "Search for the next inline-babel-call element.
2743 LIMIT bounds the search.
2745 Return value is a cons cell whose CAR is `inline-babel-call' and
2746 CDR is beginning position."
2748 (unless (bolp) (backward-char))
2749 (when (re-search-forward org-babel-inline-src-block-regexp limit t
)
2750 (cons 'inline-src-block
(match-beginning 1)))))
2754 (defun org-element-italic-parser ()
2755 "Parse italic object at point.
2757 Return a list whose CAR is `italic' and CDR is a plist with
2758 `:begin', `:end', `:contents-begin' and `:contents-end' and
2759 `:post-blank' keywords.
2761 Assume point is at the first slash marker."
2763 (unless (bolp) (backward-char 1))
2764 (looking-at org-emph-re
)
2765 (let ((begin (match-beginning 2))
2766 (contents-begin (match-beginning 4))
2767 (contents-end (match-end 4))
2768 (post-blank (progn (goto-char (match-end 2))
2769 (skip-chars-forward " \t")))
2774 :contents-begin contents-begin
2775 :contents-end contents-end
2776 :post-blank post-blank
)))))
2778 (defun org-element-italic-interpreter (italic contents
)
2779 "Interpret ITALIC object as Org syntax.
2780 CONTENTS is the contents of the object."
2781 (format "/%s/" contents
))
2786 (defun org-element-latex-fragment-parser ()
2787 "Parse latex fragment at point.
2789 Return a list whose CAR is `latex-fragment' and CDR a plist with
2790 `:value', `:begin', `:end', and `:post-blank' as keywords.
2792 Assume point is at the beginning of the latex fragment."
2794 (let* ((begin (point))
2798 (let ((latex-regexp (nth 1 (assoc e org-latex-regexps
))))
2799 (when (or (looking-at latex-regexp
)
2803 (looking-at latex-regexp
))))
2804 (throw 'exit
(nth 2 (assoc e org-latex-regexps
))))))
2805 (plist-get org-format-latex-options
:matchers
))
2806 ;; None found: it's a macro.
2807 (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")
2809 (value (match-string-no-properties substring-match
))
2810 (post-blank (progn (goto-char (match-end substring-match
))
2811 (skip-chars-forward " \t")))
2813 (list 'latex-fragment
2817 :post-blank post-blank
)))))
2819 (defun org-element-latex-fragment-interpreter (latex-fragment contents
)
2820 "Interpret LATEX-FRAGMENT object as Org syntax.
2822 (org-element-property :value latex-fragment
))
2826 (defun org-element-line-break-parser ()
2827 "Parse line break at point.
2829 Return a list whose CAR is `line-break', and CDR a plist with
2830 `:begin', `:end' and `:post-blank' keywords.
2832 Assume point is at the beginning of the line break."
2833 (list 'line-break
(list :begin
(point) :end
(point-at-eol) :post-blank
0)))
2835 (defun org-element-line-break-interpreter (line-break contents
)
2836 "Interpret LINE-BREAK object as Org syntax.
2840 (defun org-element-line-break-successor (limit)
2841 "Search for the next line-break object.
2843 LIMIT bounds the search.
2845 Return value is a cons cell whose CAR is `line-break' and CDR is
2846 beginning position."
2848 (let ((beg (and (re-search-forward "[^\\\\]\\(\\\\\\\\\\)[ \t]*$" limit t
)
2849 (goto-char (match-beginning 1)))))
2850 ;; A line break can only happen on a non-empty line.
2851 (when (and beg
(re-search-backward "\\S-" (point-at-bol) t
))
2852 (cons 'line-break beg
)))))
2857 (defun org-element-link-parser ()
2858 "Parse link at point.
2860 Return a list whose CAR is `link' and CDR a plist with `:type',
2861 `:path', `:raw-link', `:application', `:search-option', `:begin',
2862 `:end', `:contents-begin', `:contents-end' and `:post-blank' as
2865 Assume point is at the beginning of the link."
2867 (let ((begin (point))
2868 end contents-begin contents-end link-end post-blank path type
2869 raw-link link search-option application
)
2871 ;; Type 1: Text targeted from a radio target.
2872 ((and org-target-link-regexp
(looking-at org-target-link-regexp
))
2874 link-end
(match-end 0)
2875 path
(org-match-string-no-properties 0)))
2876 ;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]]
2877 ((looking-at org-bracket-link-regexp
)
2878 (setq contents-begin
(match-beginning 3)
2879 contents-end
(match-end 3)
2880 link-end
(match-end 0)
2881 ;; RAW-LINK is the original link.
2882 raw-link
(org-match-string-no-properties 1)
2883 link
(org-translate-link
2884 (org-link-expand-abbrev
2885 (org-link-unescape raw-link
))))
2886 ;; Determine TYPE of link and set PATH accordingly.
2889 ((or (file-name-absolute-p link
) (string-match "^\\.\\.?/" link
))
2890 (setq type
"file" path link
))
2891 ;; Explicit type (http, irc, bbdb...). See `org-link-types'.
2892 ((string-match org-link-re-with-space3 link
)
2893 (setq type
(match-string 1 link
) path
(match-string 2 link
)))
2894 ;; Id type: PATH is the id.
2895 ((string-match "^id:\\([-a-f0-9]+\\)" link
)
2896 (setq type
"id" path
(match-string 1 link
)))
2897 ;; Code-ref type: PATH is the name of the reference.
2898 ((string-match "^(\\(.*\\))$" link
)
2899 (setq type
"coderef" path
(match-string 1 link
)))
2900 ;; Custom-id type: PATH is the name of the custom id.
2901 ((= (aref link
0) ?
#)
2902 (setq type
"custom-id" path
(substring link
1)))
2903 ;; Fuzzy type: Internal link either matches a target, an
2904 ;; headline name or nothing. PATH is the target or
2906 (t (setq type
"fuzzy" path link
))))
2907 ;; Type 3: Plain link, i.e. http://orgmode.org
2908 ((looking-at org-plain-link-re
)
2909 (setq raw-link
(org-match-string-no-properties 0)
2910 type
(org-match-string-no-properties 1)
2911 path
(org-match-string-no-properties 2)
2912 link-end
(match-end 0)))
2913 ;; Type 4: Angular link, i.e. <http://orgmode.org>
2914 ((looking-at org-angle-link-re
)
2915 (setq raw-link
(buffer-substring-no-properties
2916 (match-beginning 1) (match-end 2))
2917 type
(org-match-string-no-properties 1)
2918 path
(org-match-string-no-properties 2)
2919 link-end
(match-end 0))))
2920 ;; In any case, deduce end point after trailing white space from
2921 ;; LINK-END variable.
2922 (setq post-blank
(progn (goto-char link-end
) (skip-chars-forward " \t"))
2924 ;; Extract search option and opening application out of
2925 ;; "file"-type links.
2926 (when (member type org-element-link-type-is-file
)
2928 (cond ((string-match "^file\\+\\(.*\\)$" type
)
2929 (setq application
(match-string 1 type
)))
2930 ((not (string-match "^file" type
))
2931 (setq application type
)))
2932 ;; Extract search option from PATH.
2933 (when (string-match "::\\(.*\\)$" path
)
2934 (setq search-option
(match-string 1 path
)
2935 path
(replace-match "" nil nil path
)))
2936 ;; Make sure TYPE always report "file".
2941 :raw-link
(or raw-link path
)
2942 :application application
2943 :search-option search-option
2946 :contents-begin contents-begin
2947 :contents-end contents-end
2948 :post-blank post-blank
)))))
2950 (defun org-element-link-interpreter (link contents
)
2951 "Interpret LINK object as Org syntax.
2952 CONTENTS is the contents of the object, or nil."
2953 (let ((type (org-element-property :type link
))
2954 (raw-link (org-element-property :raw-link link
)))
2955 (if (string= type
"radio") raw-link
2958 (if contents
(format "[%s]" contents
) "")))))
2960 (defun org-element-link-successor (limit)
2961 "Search for the next link object.
2963 LIMIT bounds the search.
2965 Return value is a cons cell whose CAR is `link' and CDR is
2966 beginning position."
2969 (if (not org-target-link-regexp
) org-any-link-re
2970 (concat org-any-link-re
"\\|" org-target-link-regexp
))))
2971 (when (re-search-forward link-regexp limit t
)
2972 (cons 'link
(match-beginning 0))))))
2977 (defun org-element-macro-parser ()
2978 "Parse macro at point.
2980 Return a list whose CAR is `macro' and CDR a plist with `:key',
2981 `:args', `:begin', `:end', `:value' and `:post-blank' as
2984 Assume point is at the macro."
2986 (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
2987 (let ((begin (point))
2988 (key (downcase (org-match-string-no-properties 1)))
2989 (value (org-match-string-no-properties 0))
2990 (post-blank (progn (goto-char (match-end 0))
2991 (skip-chars-forward " \t")))
2993 (args (let ((args (org-match-string-no-properties 3)) args2
)
2995 (setq args
(org-split-string args
","))
2997 (while (string-match "\\\\\\'" (car args
))
2998 ;; Repair bad splits.
2999 (setcar (cdr args
) (concat (substring (car args
) 0 -
1)
3002 (push (pop args
) args2
))
3003 (mapcar 'org-trim
(nreverse args2
))))))
3010 :post-blank post-blank
)))))
3012 (defun org-element-macro-interpreter (macro contents
)
3013 "Interpret MACRO object as Org syntax.
3015 (org-element-property :value macro
))
3017 (defun org-element-macro-successor (limit)
3018 "Search for the next macro object.
3020 LIMIT bounds the search.
3022 Return value is cons cell whose CAR is `macro' and CDR is
3023 beginning position."
3025 (when (re-search-forward
3026 "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
3028 (cons 'macro
(match-beginning 0)))))
3033 (defun org-element-radio-target-parser ()
3034 "Parse radio target at point.
3036 Return a list whose CAR is `radio-target' and CDR a plist with
3037 `:begin', `:end', `:contents-begin', `:contents-end', `:value'
3038 and `:post-blank' as keywords.
3040 Assume point is at the radio target."
3042 (looking-at org-radio-target-regexp
)
3043 (let ((begin (point))
3044 (contents-begin (match-beginning 1))
3045 (contents-end (match-end 1))
3046 (value (org-match-string-no-properties 1))
3047 (post-blank (progn (goto-char (match-end 0))
3048 (skip-chars-forward " \t")))
3053 :contents-begin contents-begin
3054 :contents-end contents-end
3055 :post-blank post-blank
3058 (defun org-element-radio-target-interpreter (target contents
)
3059 "Interpret TARGET object as Org syntax.
3060 CONTENTS is the contents of the object."
3061 (concat "<<<" contents
">>>"))
3063 (defun org-element-radio-target-successor (limit)
3064 "Search for the next radio-target object.
3066 LIMIT bounds the search.
3068 Return value is a cons cell whose CAR is `radio-target' and CDR
3069 is beginning position."
3071 (when (re-search-forward org-radio-target-regexp limit t
)
3072 (cons 'radio-target
(match-beginning 0)))))
3075 ;;;; Statistics Cookie
3077 (defun org-element-statistics-cookie-parser ()
3078 "Parse statistics cookie at point.
3080 Return a list whose CAR is `statistics-cookie', and CDR a plist
3081 with `:begin', `:end', `:value' and `:post-blank' keywords.
3083 Assume point is at the beginning of the statistics-cookie."
3085 (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
3086 (let* ((begin (point))
3087 (value (buffer-substring-no-properties
3088 (match-beginning 0) (match-end 0)))
3089 (post-blank (progn (goto-char (match-end 0))
3090 (skip-chars-forward " \t")))
3092 (list 'statistics-cookie
3096 :post-blank post-blank
)))))
3098 (defun org-element-statistics-cookie-interpreter (statistics-cookie contents
)
3099 "Interpret STATISTICS-COOKIE object as Org syntax.
3101 (org-element-property :value statistics-cookie
))
3103 (defun org-element-statistics-cookie-successor (limit)
3104 "Search for the next statistics cookie object.
3106 LIMIT bounds the search.
3108 Return value is a cons cell whose CAR is `statistics-cookie' and
3109 CDR is beginning position."
3111 (when (re-search-forward "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]" limit t
)
3112 (cons 'statistics-cookie
(match-beginning 0)))))
3117 (defun org-element-strike-through-parser ()
3118 "Parse strike-through object at point.
3120 Return a list whose CAR is `strike-through' and CDR is a plist
3121 with `:begin', `:end', `:contents-begin' and `:contents-end' and
3122 `:post-blank' keywords.
3124 Assume point is at the first plus sign marker."
3126 (unless (bolp) (backward-char 1))
3127 (looking-at org-emph-re
)
3128 (let ((begin (match-beginning 2))
3129 (contents-begin (match-beginning 4))
3130 (contents-end (match-end 4))
3131 (post-blank (progn (goto-char (match-end 2))
3132 (skip-chars-forward " \t")))
3134 (list 'strike-through
3137 :contents-begin contents-begin
3138 :contents-end contents-end
3139 :post-blank post-blank
)))))
3141 (defun org-element-strike-through-interpreter (strike-through contents
)
3142 "Interpret STRIKE-THROUGH object as Org syntax.
3143 CONTENTS is the contents of the object."
3144 (format "+%s+" contents
))
3149 (defun org-element-subscript-parser ()
3150 "Parse subscript at point.
3152 Return a list whose CAR is `subscript' and CDR a plist with
3153 `:begin', `:end', `:contents-begin', `:contents-end',
3154 `:use-brackets-p' and `:post-blank' as keywords.
3156 Assume point is at the underscore."
3158 (unless (bolp) (backward-char))
3159 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp
)
3161 (not (looking-at org-match-substring-regexp
))))
3162 (begin (match-beginning 2))
3163 (contents-begin (or (match-beginning 5)
3164 (match-beginning 3)))
3165 (contents-end (or (match-end 5) (match-end 3)))
3166 (post-blank (progn (goto-char (match-end 0))
3167 (skip-chars-forward " \t")))
3172 :use-brackets-p bracketsp
3173 :contents-begin contents-begin
3174 :contents-end contents-end
3175 :post-blank post-blank
)))))
3177 (defun org-element-subscript-interpreter (subscript contents
)
3178 "Interpret SUBSCRIPT object as Org syntax.
3179 CONTENTS is the contents of the object."
3181 (if (org-element-property :use-brackets-p subscript
) "_{%s}" "_%s")
3184 (defun org-element-sub/superscript-successor
(limit)
3185 "Search for the next sub/superscript object.
3187 LIMIT bounds the search.
3189 Return value is a cons cell whose CAR is either `subscript' or
3190 `superscript' and CDR is beginning position."
3192 (when (re-search-forward org-match-substring-regexp limit t
)
3193 (cons (if (string= (match-string 2) "_") 'subscript
'superscript
)
3194 (match-beginning 2)))))
3199 (defun org-element-superscript-parser ()
3200 "Parse superscript at point.
3202 Return a list whose CAR is `superscript' and CDR a plist with
3203 `:begin', `:end', `:contents-begin', `:contents-end',
3204 `:use-brackets-p' and `:post-blank' as keywords.
3206 Assume point is at the caret."
3208 (unless (bolp) (backward-char))
3209 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp
) t
3210 (not (looking-at org-match-substring-regexp
))))
3211 (begin (match-beginning 2))
3212 (contents-begin (or (match-beginning 5)
3213 (match-beginning 3)))
3214 (contents-end (or (match-end 5) (match-end 3)))
3215 (post-blank (progn (goto-char (match-end 0))
3216 (skip-chars-forward " \t")))
3221 :use-brackets-p bracketsp
3222 :contents-begin contents-begin
3223 :contents-end contents-end
3224 :post-blank post-blank
)))))
3226 (defun org-element-superscript-interpreter (superscript contents
)
3227 "Interpret SUPERSCRIPT object as Org syntax.
3228 CONTENTS is the contents of the object."
3230 (if (org-element-property :use-brackets-p superscript
) "^{%s}" "^%s")
3236 (defun org-element-table-cell-parser ()
3237 "Parse table cell at point.
3239 Return a list whose CAR is `table-cell' and CDR is a plist
3240 containing `:begin', `:end', `:contents-begin', `:contents-end'
3241 and `:post-blank' keywords."
3242 (looking-at "[ \t]*\\(.*?\\)[ \t]*|")
3243 (let* ((begin (match-beginning 0))
3245 (contents-begin (match-beginning 1))
3246 (contents-end (match-end 1)))
3250 :contents-begin contents-begin
3251 :contents-end contents-end
3254 (defun org-element-table-cell-interpreter (table-cell contents
)
3255 "Interpret TABLE-CELL element as Org syntax.
3256 CONTENTS is the contents of the cell, or nil."
3257 (concat " " contents
" |"))
3259 (defun org-element-table-cell-successor (limit)
3260 "Search for the next table-cell object.
3262 LIMIT bounds the search.
3264 Return value is a cons cell whose CAR is `table-cell' and CDR is
3265 beginning position."
3266 (when (looking-at "[ \t]*.*?[ \t]+|") (cons 'table-cell
(point))))
3271 (defun org-element-target-parser ()
3272 "Parse target at point.
3274 Return a list whose CAR is `target' and CDR a plist with
3275 `:begin', `:end', `:value' and `:post-blank' as keywords.
3277 Assume point is at the target."
3279 (looking-at org-target-regexp
)
3280 (let ((begin (point))
3281 (value (org-match-string-no-properties 1))
3282 (post-blank (progn (goto-char (match-end 0))
3283 (skip-chars-forward " \t")))
3289 :post-blank post-blank
)))))
3291 (defun org-element-target-interpreter (target contents
)
3292 "Interpret TARGET object as Org syntax.
3294 (format "<<%s>>" (org-element-property :value target
)))
3296 (defun org-element-target-successor (limit)
3297 "Search for the next target object.
3299 LIMIT bounds the search.
3301 Return value is a cons cell whose CAR is `target' and CDR is
3302 beginning position."
3304 (when (re-search-forward org-target-regexp limit t
)
3305 (cons 'target
(match-beginning 0)))))
3310 (defun org-element-timestamp-parser ()
3311 "Parse time stamp at point.
3313 Return a list whose CAR is `timestamp', and CDR a plist with
3314 `:type', `:begin', `:end', `:value' and `:post-blank' keywords.
3316 Assume point is at the beginning of the timestamp."
3318 (let* ((begin (point))
3319 (activep (eq (char-after) ?
<))
3322 (looking-at "[<[]\\(\\(%%\\)?.*?\\)[]>]\\(?:--[<[]\\(.*?\\)[]>]\\)?")
3323 (match-string-no-properties 1)))
3324 (range-end (match-string-no-properties 3))
3325 (type (cond ((match-string 2) 'diary
)
3326 ((and activep range-end
) 'active-range
)
3328 (range-end 'inactive-range
)
3330 (post-blank (progn (goto-char (match-end 0))
3331 (skip-chars-forward " \t")))
3336 :range-end range-end
3339 :post-blank post-blank
)))))
3341 (defun org-element-timestamp-interpreter (timestamp contents
)
3342 "Interpret TIMESTAMP object as Org syntax.
3344 (let ((type (org-element-property :type timestamp
) ))
3346 (format (if (memq type
'(inactive inactive-range
)) "[%s]" "<%s>")
3347 (org-element-property :value timestamp
))
3348 (let ((range-end (org-element-property :range-end timestamp
)))
3351 (format (if (eq type
'inactive-range
) "[%s]" "<%s>")
3354 (defun org-element-timestamp-successor (limit)
3355 "Search for the next timestamp object.
3357 LIMIT bounds the search.
3359 Return value is a cons cell whose CAR is `timestamp' and CDR is
3360 beginning position."
3362 (when (re-search-forward
3363 (concat org-ts-regexp-both
3365 "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
3367 "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
3369 (cons 'timestamp
(match-beginning 0)))))
3374 (defun org-element-underline-parser ()
3375 "Parse underline object at point.
3377 Return a list whose CAR is `underline' and CDR is a plist with
3378 `:begin', `:end', `:contents-begin' and `:contents-end' and
3379 `:post-blank' keywords.
3381 Assume point is at the first underscore marker."
3383 (unless (bolp) (backward-char 1))
3384 (looking-at org-emph-re
)
3385 (let ((begin (match-beginning 2))
3386 (contents-begin (match-beginning 4))
3387 (contents-end (match-end 4))
3388 (post-blank (progn (goto-char (match-end 2))
3389 (skip-chars-forward " \t")))
3394 :contents-begin contents-begin
3395 :contents-end contents-end
3396 :post-blank post-blank
)))))
3398 (defun org-element-underline-interpreter (underline contents
)
3399 "Interpret UNDERLINE object as Org syntax.
3400 CONTENTS is the contents of the object."
3401 (format "_%s_" contents
))
3406 (defun org-element-verbatim-parser ()
3407 "Parse verbatim object at point.
3409 Return a list whose CAR is `verbatim' and CDR is a plist with
3410 `:value', `:begin', `:end' and `:post-blank' keywords.
3412 Assume point is at the first equal sign marker."
3414 (unless (bolp) (backward-char 1))
3415 (looking-at org-emph-re
)
3416 (let ((begin (match-beginning 2))
3417 (value (org-match-string-no-properties 4))
3418 (post-blank (progn (goto-char (match-end 2))
3419 (skip-chars-forward " \t")))
3425 :post-blank post-blank
)))))
3427 (defun org-element-verbatim-interpreter (verbatim contents
)
3428 "Interpret VERBATIM object as Org syntax.
3430 (format "=%s=" (org-element-property :value verbatim
)))
3434 ;;; Parsing Element Starting At Point
3436 ;; `org-element--current-element' is the core function of this section.
3437 ;; It returns the Lisp representation of the element starting at
3440 ;; `org-element--current-element' makes use of special modes. They
3441 ;; are activated for fixed element chaining (i.e. `plain-list' >
3442 ;; `item') or fixed conditional element chaining (i.e. `headline' >
3443 ;; `section'). Special modes are: `first-section', `item',
3444 ;; `node-property', `quote-section', `section' and `table-row'.
3446 (defun org-element--current-element
3447 (limit &optional granularity special structure
)
3448 "Parse the element starting at point.
3450 LIMIT bounds the search.
3452 Return value is a list like (TYPE PROPS) where TYPE is the type
3453 of the element and PROPS a plist of properties associated to the
3456 Possible types are defined in `org-element-all-elements'.
3458 Optional argument GRANULARITY determines the depth of the
3459 recursion. Allowed values are `headline', `greater-element',
3460 `element', `object' or nil. When it is broader than `object' (or
3461 nil), secondary values will not be parsed, since they only
3464 Optional argument SPECIAL, when non-nil, can be either
3465 `first-section', `item', `node-property', `quote-section',
3466 `section', and `table-row'.
3468 If STRUCTURE isn't provided but SPECIAL is set to `item', it will
3471 This function assumes point is always at the beginning of the
3472 element it has to parse."
3474 (let ((case-fold-search t
)
3475 ;; Determine if parsing depth allows for secondary strings
3476 ;; parsing. It only applies to elements referenced in
3477 ;; `org-element-secondary-value-alist'.
3478 (raw-secondary-p (and granularity
(not (eq granularity
'object
)))))
3482 (org-element-item-parser limit structure raw-secondary-p
))
3484 ((eq special
'table-row
) (org-element-table-row-parser limit
))
3486 ((eq special
'node-property
) (org-element-node-property-parser limit
))
3488 ((org-with-limited-levels (org-at-heading-p))
3489 (org-element-headline-parser limit raw-secondary-p
))
3490 ;; Sections (must be checked after headline).
3491 ((eq special
'section
) (org-element-section-parser limit
))
3492 ((eq special
'quote-section
) (org-element-quote-section-parser limit
))
3493 ((eq special
'first-section
)
3494 (org-element-section-parser
3495 (or (save-excursion (org-with-limited-levels (outline-next-heading)))
3497 ;; When not at bol, point is at the beginning of an item or
3498 ;; a footnote definition: next item is always a paragraph.
3499 ((not (bolp)) (org-element-paragraph-parser limit
(list (point))))
3500 ;; Planning and Clock.
3501 ((and (looking-at org-planning-or-clock-line-re
))
3502 (if (equal (match-string 1) org-clock-string
)
3503 (org-element-clock-parser limit
)
3504 (org-element-planning-parser limit
)))
3507 (org-element-inlinetask-parser limit raw-secondary-p
))
3508 ;; From there, elements can have affiliated keywords.
3509 (t (let ((affiliated (org-element--collect-affiliated-keywords)))
3511 ;; LaTeX Environment.
3512 ((looking-at "[ \t]*\\\\begin{\\([A-Za-z0-9*]+\\)}[ \t]*$")
3515 (format "^[ \t]*\\\\end{%s}[ \t]*$"
3516 (regexp-quote (match-string 1)))
3518 (org-element-latex-environment-parser limit affiliated
)
3519 (org-element-paragraph-parser limit affiliated
)))
3520 ;; Drawer and Property Drawer.
3521 ((looking-at org-drawer-regexp
)
3522 (let ((name (match-string 1)))
3524 ((not (save-excursion
3525 (re-search-forward "^[ \t]*:END:[ \t]*$" nil t
)))
3526 (org-element-paragraph-parser limit affiliated
))
3527 ((equal "PROPERTIES" name
)
3528 (org-element-property-drawer-parser limit affiliated
))
3529 (t (org-element-drawer-parser limit affiliated
)))))
3531 ((looking-at "[ \t]*:\\( \\|$\\)")
3532 (org-element-fixed-width-parser limit affiliated
))
3533 ;; Inline Comments, Blocks, Babel Calls, Dynamic Blocks and
3535 ((looking-at "[ \t]*#")
3536 (goto-char (match-end 0))
3537 (cond ((looking-at "\\(?: \\|$\\)")
3539 (org-element-comment-parser limit affiliated
))
3540 ((looking-at "\\+BEGIN_\\(\\S-+\\)")
3542 (let ((parser (assoc (upcase (match-string 1))
3543 org-element-block-name-alist
)))
3544 (if parser
(funcall (cdr parser
) limit affiliated
)
3545 (org-element-special-block-parser limit affiliated
))))
3546 ((looking-at "\\+CALL:")
3548 (org-element-babel-call-parser limit affiliated
))
3549 ((looking-at "\\+BEGIN:? ")
3551 (org-element-dynamic-block-parser limit affiliated
))
3552 ((looking-at "\\+\\S-+:")
3554 (org-element-keyword-parser limit affiliated
))
3557 (org-element-paragraph-parser limit affiliated
))))
3558 ;; Footnote Definition.
3559 ((looking-at org-footnote-definition-re
)
3560 (org-element-footnote-definition-parser limit affiliated
))
3562 ((looking-at "[ \t]*-\\{5,\\}[ \t]*$")
3563 (org-element-horizontal-rule-parser limit affiliated
))
3565 ((org-at-table-p t
) (org-element-table-parser limit affiliated
))
3567 ((looking-at (org-item-re))
3568 (org-element-plain-list-parser
3569 limit affiliated
(or structure
(org-list-struct))))
3570 ;; Default element: Paragraph.
3571 (t (org-element-paragraph-parser limit affiliated
)))))))))
3574 ;; Most elements can have affiliated keywords. When looking for an
3575 ;; element beginning, we want to move before them, as they belong to
3576 ;; that element, and, in the meantime, collect information they give
3577 ;; into appropriate properties. Hence the following function.
3579 (defun org-element--collect-affiliated-keywords ()
3580 "Collect affiliated keywords from point.
3582 Return a list whose CAR is the position at the first of them and
3583 CDR a plist of keywords and values and move point to the
3584 beginning of the first line after them.
3586 As a special case, if element doesn't start at the beginning of
3587 the line (i.e. a paragraph starting an item), CAR is current
3588 position of point and CDR is nil."
3589 (if (not (bolp)) (list (point))
3590 (let ((case-fold-search t
)
3592 ;; RESTRICT is the list of objects allowed in parsed
3594 (restrict (org-element-restriction 'keyword
))
3596 (while (and (not (eobp)) (looking-at org-element--affiliated-re
))
3597 (let* ((raw-kwd (upcase (match-string 1)))
3598 ;; Apply translation to RAW-KWD. From there, KWD is
3599 ;; the official keyword.
3600 (kwd (or (cdr (assoc raw-kwd
3601 org-element-keyword-translation-alist
))
3603 ;; Find main value for any keyword.
3607 (buffer-substring-no-properties
3608 (match-end 0) (point-at-eol)))))
3609 ;; PARSEDP is non-nil when keyword should have its
3611 (parsedp (member kwd org-element-parsed-keywords
))
3612 ;; If KWD is a dual keyword, find its secondary
3613 ;; value. Maybe parse it.
3614 (dualp (member kwd org-element-dual-keywords
))
3617 (let ((sec (org-match-string-no-properties 2)))
3618 (if (or (not sec
) (not parsedp
)) sec
3619 (org-element-parse-secondary-string sec restrict
)))))
3620 ;; Attribute a property name to KWD.
3621 (kwd-sym (and kwd
(intern (concat ":" (downcase kwd
))))))
3622 ;; Now set final shape for VALUE.
3624 (setq value
(org-element-parse-secondary-string value restrict
)))
3625 (when dualp
(setq value
(and value
(cons value dual-value
))))
3626 (when (or (member kwd org-element-multiple-keywords
)
3627 ;; Attributes can always appear on multiple lines.
3628 (string-match "^ATTR_" kwd
))
3629 (setq value
(cons value
(plist-get output kwd-sym
))))
3630 ;; Eventually store the new value in OUTPUT.
3631 (setq output
(plist-put output kwd-sym value
))
3632 ;; Move to next keyword.
3634 ;; If affiliated keywords are orphaned: move back to first one.
3635 ;; They will be parsed as a paragraph.
3636 (when (looking-at "[ \t]*$") (goto-char origin
) (setq output nil
))
3638 (cons origin output
))))
3644 ;; The two major functions here are `org-element-parse-buffer', which
3645 ;; parses Org syntax inside the current buffer, taking into account
3646 ;; region, narrowing, or even visibility if specified, and
3647 ;; `org-element-parse-secondary-string', which parses objects within
3650 ;; The (almost) almighty `org-element-map' allows to apply a function
3651 ;; on elements or objects matching some type, and accumulate the
3652 ;; resulting values. In an export situation, it also skips unneeded
3653 ;; parts of the parse tree.
3655 (defun org-element-parse-buffer (&optional granularity visible-only
)
3656 "Recursively parse the buffer and return structure.
3657 If narrowing is in effect, only parse the visible part of the
3660 Optional argument GRANULARITY determines the depth of the
3661 recursion. It can be set to the following symbols:
3663 `headline' Only parse headlines.
3664 `greater-element' Don't recurse into greater elements excepted
3665 headlines and sections. Thus, elements
3666 parsed are the top-level ones.
3667 `element' Parse everything but objects and plain text.
3668 `object' Parse the complete buffer (default).
3670 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3673 Assume buffer is in Org mode."
3675 (goto-char (point-min))
3676 (org-skip-whitespace)
3677 (org-element--parse-elements
3678 (point-at-bol) (point-max)
3679 ;; Start in `first-section' mode so text before the first
3680 ;; headline belongs to a section.
3681 'first-section nil granularity visible-only
(list 'org-data nil
))))
3683 (defun org-element-parse-secondary-string (string restriction
&optional parent
)
3684 "Recursively parse objects in STRING and return structure.
3686 RESTRICTION is a symbol limiting the object types that will be
3689 Optional argument PARENT, when non-nil, is the element or object
3690 containing the secondary string. It is used to set correctly
3691 `:parent' property within the string."
3694 (let ((secondary (org-element--parse-objects
3695 (point-min) (point-max) nil restriction
)))
3696 (mapc (lambda (obj) (org-element-put-property obj
:parent parent
))
3699 (defun org-element-map
3700 (data types fun
&optional info first-match no-recursion with-affiliated
)
3701 "Map a function on selected elements or objects.
3703 DATA is the parsed tree, as returned by, i.e,
3704 `org-element-parse-buffer'. TYPES is a symbol or list of symbols
3705 of elements or objects types. FUN is the function called on the
3706 matching element or object. It must accept one arguments: the
3707 element or object itself.
3709 When optional argument INFO is non-nil, it should be a plist
3710 holding export options. In that case, parts of the parse tree
3711 not exportable according to that property list will be skipped.
3713 When optional argument FIRST-MATCH is non-nil, stop at the first
3714 match for which FUN doesn't return nil, and return that value.
3716 Optional argument NO-RECURSION is a symbol or a list of symbols
3717 representing elements or objects types. `org-element-map' won't
3718 enter any recursive element or object whose type belongs to that
3719 list. Though, FUN can still be applied on them.
3721 When optional argument WITH-AFFILIATED is non-nil, also move into
3722 affiliated keywords to find objects.
3724 Nil values returned from FUN do not appear in the results."
3725 ;; Ensure TYPES and NO-RECURSION are a list, even of one element.
3726 (unless (listp types
) (setq types
(list types
)))
3727 (unless (listp no-recursion
) (setq no-recursion
(list no-recursion
)))
3728 ;; Recursion depth is determined by --CATEGORY.
3731 (let ((category 'greater-elements
))
3732 (mapc (lambda (type)
3733 (cond ((or (memq type org-element-all-objects
)
3734 (eq type
'plain-text
))
3735 ;; If one object is found, the function
3736 ;; has to recurse into every object.
3737 (throw 'found
'objects
))
3738 ((not (memq type org-element-greater-elements
))
3739 ;; If one regular element is found, the
3740 ;; function has to recurse, at least,
3741 ;; into every element it encounters.
3742 (and (not (eq category
'elements
))
3743 (setq category
'elements
)))))
3746 ;; Compute properties for affiliated keywords if necessary.
3748 (and with-affiliated
3749 (mapcar (lambda (kwd)
3750 (cons kwd
(intern (concat ":" (downcase kwd
)))))
3751 org-element-affiliated-keywords
)))
3757 ;; Recursively walk DATA. INFO, if non-nil, is a plist
3758 ;; holding contextual information.
3759 (let ((--type (org-element-type --data
)))
3762 ;; Ignored element in an export context.
3763 ((and info
(memq --data
(plist-get info
:ignore-list
))))
3764 ;; Secondary string: only objects can be found there.
3766 (when (eq --category
'objects
) (mapc --walk-tree --data
)))
3767 ;; Unconditionally enter parse trees.
3768 ((eq --type
'org-data
)
3769 (mapc --walk-tree
(org-element-contents --data
)))
3771 ;; Check if TYPE is matching among TYPES. If so,
3772 ;; apply FUN to --DATA and accumulate return value
3773 ;; into --ACC (or exit if FIRST-MATCH is non-nil).
3774 (when (memq --type types
)
3775 (let ((result (funcall fun --data
)))
3776 (cond ((not result
))
3777 (first-match (throw '--map-first-match result
))
3778 (t (push result --acc
)))))
3779 ;; If --DATA has a secondary string that can contain
3780 ;; objects with their type among TYPES, look into it.
3781 (when (and (eq --category
'objects
) (not (stringp --data
)))
3783 (assq --type org-element-secondary-value-alist
)))
3785 (funcall --walk-tree
3786 (org-element-property (cdr sec-prop
) --data
)))))
3787 ;; If --DATA has any affiliated keywords and
3788 ;; WITH-AFFILIATED is non-nil, look for objects in
3790 (when (and with-affiliated
3791 (eq --category
'objects
)
3792 (memq --type org-element-all-elements
))
3793 (mapc (lambda (kwd-pair)
3794 (let ((kwd (car kwd-pair
))
3795 (value (org-element-property
3796 (cdr kwd-pair
) --data
)))
3797 ;; Pay attention to the type of value.
3798 ;; Preserve order for multiple keywords.
3801 ((and (member kwd org-element-multiple-keywords
)
3802 (member kwd org-element-dual-keywords
))
3803 (mapc (lambda (line)
3804 (funcall --walk-tree
(cdr line
))
3805 (funcall --walk-tree
(car line
)))
3807 ((member kwd org-element-multiple-keywords
)
3808 (mapc (lambda (line) (funcall --walk-tree line
))
3810 ((member kwd org-element-dual-keywords
)
3811 (funcall --walk-tree
(cdr value
))
3812 (funcall --walk-tree
(car value
)))
3813 (t (funcall --walk-tree value
)))))
3814 --affiliated-alist
))
3815 ;; Determine if a recursion into --DATA is possible.
3817 ;; --TYPE is explicitly removed from recursion.
3818 ((memq --type no-recursion
))
3819 ;; --DATA has no contents.
3820 ((not (org-element-contents --data
)))
3821 ;; Looking for greater elements but --DATA is simply
3822 ;; an element or an object.
3823 ((and (eq --category
'greater-elements
)
3824 (not (memq --type org-element-greater-elements
))))
3825 ;; Looking for elements but --DATA is an object.
3826 ((and (eq --category
'elements
)
3827 (memq --type org-element-all-objects
)))
3828 ;; In any other case, map contents.
3829 (t (mapc --walk-tree
(org-element-contents --data
)))))))))))
3830 (catch '--map-first-match
3831 (funcall --walk-tree data
)
3832 ;; Return value in a proper order.
3835 ;; The following functions are internal parts of the parser.
3837 ;; The first one, `org-element--parse-elements' acts at the element's
3840 ;; The second one, `org-element--parse-objects' applies on all objects
3841 ;; of a paragraph or a secondary string. It uses
3842 ;; `org-element--get-next-object-candidates' to optimize the search of
3843 ;; the next object in the buffer.
3845 ;; More precisely, that function looks for every allowed object type
3846 ;; first. Then, it discards failed searches, keeps further matches,
3847 ;; and searches again types matched behind point, for subsequent
3848 ;; calls. Thus, searching for a given type fails only once, and every
3849 ;; object is searched only once at top level (but sometimes more for
3852 (defun org-element--parse-elements
3853 (beg end special structure granularity visible-only acc
)
3854 "Parse elements between BEG and END positions.
3856 SPECIAL prioritize some elements over the others. It can be set
3857 to `first-section', `quote-section', `section' `item' or
3860 When value is `item', STRUCTURE will be used as the current list
3863 GRANULARITY determines the depth of the recursion. See
3864 `org-element-parse-buffer' for more information.
3866 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3869 Elements are accumulated into ACC."
3872 ;; When parsing only headlines, skip any text before first one.
3873 (when (and (eq granularity
'headline
) (not (org-at-heading-p)))
3874 (org-with-limited-levels (outline-next-heading)))
3876 (while (< (point) end
)
3877 ;; Find current element's type and parse it accordingly to
3879 (let* ((element (org-element--current-element
3880 end granularity special structure
))
3881 (type (org-element-type element
))
3882 (cbeg (org-element-property :contents-begin element
)))
3883 (goto-char (org-element-property :end element
))
3884 ;; Fill ELEMENT contents by side-effect.
3886 ;; If VISIBLE-ONLY is true and element is hidden or if it has
3887 ;; no contents, don't modify it.
3888 ((or (and visible-only
(org-element-property :hiddenp element
))
3890 ;; Greater element: parse it between `contents-begin' and
3891 ;; `contents-end'. Make sure GRANULARITY allows the
3892 ;; recursion, or ELEMENT is an headline, in which case going
3893 ;; inside is mandatory, in order to get sub-level headings.
3894 ((and (memq type org-element-greater-elements
)
3895 (or (memq granularity
'(element object nil
))
3896 (and (eq granularity
'greater-element
)
3898 (eq type
'headline
)))
3899 (org-element--parse-elements
3900 cbeg
(org-element-property :contents-end element
)
3901 ;; Possibly switch to a special mode.
3904 (if (org-element-property :quotedp element
) 'quote-section
3907 (property-drawer 'node-property
)
3909 (org-element-property :structure element
)
3910 granularity visible-only element
))
3911 ;; ELEMENT has contents. Parse objects inside, if
3912 ;; GRANULARITY allows it.
3913 ((memq granularity
'(object nil
))
3914 (org-element--parse-objects
3915 cbeg
(org-element-property :contents-end element
) element
3916 (org-element-restriction type
))))
3917 (org-element-adopt-elements acc element
)))
3921 (defun org-element--parse-objects (beg end acc restriction
)
3922 "Parse objects between BEG and END and return recursive structure.
3924 Objects are accumulated in ACC.
3926 RESTRICTION is a list of object types which are allowed in the
3931 (while (and (< (point) end
)
3932 (setq candidates
(org-element--get-next-object-candidates
3933 end restriction candidates
)))
3935 (let ((pos (apply 'min
(mapcar 'cdr candidates
))))
3938 (funcall (intern (format "org-element-%s-parser"
3939 (car (rassq pos candidates
)))))))))
3940 ;; 1. Text before any object. Untabify it.
3941 (let ((obj-beg (org-element-property :begin next-object
)))
3942 (unless (= (point) obj-beg
)
3944 (org-element-adopt-elements
3946 (replace-regexp-in-string
3947 "\t" (make-string tab-width ?
)
3948 (buffer-substring-no-properties (point) obj-beg
))))))
3950 (let ((obj-end (org-element-property :end next-object
))
3951 (cont-beg (org-element-property :contents-begin next-object
)))
3952 ;; Fill contents of NEXT-OBJECT by side-effect, if it has
3953 ;; a recursive type.
3955 (memq (car next-object
) org-element-recursive-objects
))
3959 (org-element-property :contents-end next-object
))
3960 (org-element--parse-objects
3961 (point-min) (point-max) next-object
3962 (org-element-restriction next-object
))))
3963 (setq acc
(org-element-adopt-elements acc next-object
))
3964 (goto-char obj-end
))))
3965 ;; 3. Text after last object. Untabify it.
3966 (unless (= (point) end
)
3968 (org-element-adopt-elements
3970 (replace-regexp-in-string
3971 "\t" (make-string tab-width ?
)
3972 (buffer-substring-no-properties (point) end
)))))
3976 (defun org-element--get-next-object-candidates (limit restriction objects
)
3977 "Return an alist of candidates for the next object.
3979 LIMIT bounds the search, and RESTRICTION narrows candidates to
3982 Return value is an alist whose CAR is position and CDR the object
3985 OBJECTS is the previous candidates alist."
3986 ;; Filter out any object found but not belonging to RESTRICTION.
3987 (setq objects
(org-remove-if-not (lambda (obj) (memq (car obj
) restriction
))
3989 (let (next-candidates types-to-search
)
3990 ;; If no previous result, search every object type in RESTRICTION.
3991 ;; Otherwise, keep potential candidates (old objects located after
3992 ;; point) and ask to search again those which had matched before.
3993 (if (not objects
) (setq types-to-search restriction
)
3995 (if (< (cdr obj
) (point)) (push (car obj
) types-to-search
)
3996 (push obj next-candidates
)))
3998 ;; Call the appropriate successor function for each type to search
3999 ;; and accumulate matches.
4002 (let* ((successor-fun
4004 (format "org-element-%s-successor"
4005 (or (cdr (assq type org-element-object-successor-alist
))
4007 (obj (funcall successor-fun limit
)))
4008 (and obj
(push obj next-candidates
))))
4015 ;;; Towards A Bijective Process
4017 ;; The parse tree obtained with `org-element-parse-buffer' is really
4018 ;; a snapshot of the corresponding Org buffer. Therefore, it can be
4019 ;; interpreted and expanded into a string with canonical Org syntax.
4020 ;; Hence `org-element-interpret-data'.
4022 ;; The function relies internally on
4023 ;; `org-element--interpret-affiliated-keywords'.
4026 (defun org-element-interpret-data (data &optional parent
)
4027 "Interpret DATA as Org syntax.
4029 DATA is a parse tree, an element, an object or a secondary string
4032 Optional argument PARENT is used for recursive calls. It contains
4033 the element or object containing data, or nil.
4035 Return Org syntax as a string."
4036 (let* ((type (org-element-type data
))
4039 ;; Secondary string.
4042 (lambda (obj) (org-element-interpret-data obj parent
))
4044 ;; Full Org document.
4045 ((eq type
'org-data
)
4047 (lambda (obj) (org-element-interpret-data obj parent
))
4048 (org-element-contents data
) ""))
4050 ((stringp data
) data
)
4051 ;; Element/Object without contents.
4052 ((not (org-element-contents data
))
4053 (funcall (intern (format "org-element-%s-interpreter" type
))
4055 ;; Element/Object with contents.
4057 (let* ((greaterp (memq type org-element-greater-elements
))
4058 (objectp (and (not greaterp
)
4059 (memq type org-element-recursive-objects
)))
4062 (lambda (obj) (org-element-interpret-data obj data
))
4063 (org-element-contents
4064 (if (or greaterp objectp
) data
4065 ;; Elements directly containing objects must
4066 ;; have their indentation normalized first.
4067 (org-element-normalize-contents
4069 ;; When normalizing first paragraph of an
4070 ;; item or a footnote-definition, ignore
4071 ;; first line's indentation.
4072 (and (eq type
'paragraph
)
4073 (equal data
(car (org-element-contents parent
)))
4074 (memq (org-element-type parent
)
4075 '(footnote-definiton item
))))))
4077 (funcall (intern (format "org-element-%s-interpreter" type
))
4079 (if greaterp
(org-element-normalize-contents contents
)
4081 (if (memq type
'(org-data plain-text nil
)) results
4082 ;; Build white spaces. If no `:post-blank' property is
4083 ;; specified, assume its value is 0.
4084 (let ((post-blank (or (org-element-property :post-blank data
) 0)))
4085 (if (memq type org-element-all-objects
)
4086 (concat results
(make-string post-blank
32))
4088 (org-element--interpret-affiliated-keywords data
)
4089 (org-element-normalize-string results
)
4090 (make-string post-blank
10)))))))
4092 (defun org-element--interpret-affiliated-keywords (element)
4093 "Return ELEMENT's affiliated keywords as Org syntax.
4094 If there is no affiliated keyword, return the empty string."
4095 (let ((keyword-to-org
4099 (when (member key org-element-dual-keywords
)
4100 (setq dual
(cdr value
) value
(car value
)))
4103 (format "[%s]" (org-element-interpret-data dual
)))
4105 (if (member key org-element-parsed-keywords
)
4106 (org-element-interpret-data value
)
4111 (let ((value (org-element-property prop element
))
4112 (keyword (upcase (substring (symbol-name prop
) 1))))
4114 (if (or (member keyword org-element-multiple-keywords
)
4115 ;; All attribute keywords can have multiple lines.
4116 (string-match "^ATTR_" keyword
))
4117 (mapconcat (lambda (line) (funcall keyword-to-org keyword line
))
4120 (funcall keyword-to-org keyword value
)))))
4121 ;; List all ELEMENT's properties matching an attribute line or an
4122 ;; affiliated keyword, but ignore translated keywords since they
4123 ;; cannot belong to the property list.
4124 (loop for prop in
(nth 1 element
) by
'cddr
4125 when
(let ((keyword (upcase (substring (symbol-name prop
) 1))))
4126 (or (string-match "^ATTR_" keyword
)
4128 (member keyword org-element-affiliated-keywords
)
4130 org-element-keyword-translation-alist
)))))
4134 ;; Because interpretation of the parse tree must return the same
4135 ;; number of blank lines between elements and the same number of white
4136 ;; space after objects, some special care must be given to white
4139 ;; The first function, `org-element-normalize-string', ensures any
4140 ;; string different from the empty string will end with a single
4141 ;; newline character.
4143 ;; The second function, `org-element-normalize-contents', removes
4144 ;; global indentation from the contents of the current element.
4146 (defun org-element-normalize-string (s)
4147 "Ensure string S ends with a single newline character.
4149 If S isn't a string return it unchanged. If S is the empty
4150 string, return it. Otherwise, return a new string with a single
4151 newline character at its end."
4153 ((not (stringp s
)) s
)
4155 (t (and (string-match "\\(\n[ \t]*\\)*\\'" s
)
4156 (replace-match "\n" nil nil s
)))))
4158 (defun org-element-normalize-contents (element &optional ignore-first
)
4159 "Normalize plain text in ELEMENT's contents.
4161 ELEMENT must only contain plain text and objects.
4163 If optional argument IGNORE-FIRST is non-nil, ignore first line's
4164 indentation to compute maximal common indentation.
4166 Return the normalized element that is element with global
4167 indentation removed from its contents. The function assumes that
4168 indentation is not done with TAB characters."
4169 (let* (ind-list ; for byte-compiler
4170 collect-inds
; for byte-compiler
4173 ;; Return list of indentations within BLOB. This is done by
4174 ;; walking recursively BLOB and updating IND-LIST along the
4175 ;; way. FIRST-FLAG is non-nil when the first string hasn't
4176 ;; been seen yet. It is required as this string is the only
4177 ;; one whose indentation doesn't happen after a newline
4179 (lambda (blob first-flag
)
4182 (when (and first-flag
(stringp object
))
4183 (setq first-flag nil
)
4184 (string-match "\\`\\( *\\)" object
)
4185 (let ((len (length (match-string 1 object
))))
4186 ;; An indentation of zero means no string will be
4187 ;; modified. Quit the process.
4188 (if (zerop len
) (throw 'zero
(setq ind-list nil
))
4189 (push len ind-list
))))
4193 ;; Avoid matching blank or empty lines.
4194 (while (and (string-match "\n\\( *\\)\\(.\\)" object start
)
4195 (not (equal (match-string 2 object
) " ")))
4196 (setq start
(match-end 0))
4197 (push (length (match-string 1 object
)) ind-list
))))
4198 ((memq (org-element-type object
) org-element-recursive-objects
)
4199 (funcall collect-inds object first-flag
))))
4200 (org-element-contents blob
))))))
4201 ;; Collect indentation list in ELEMENT. Possibly remove first
4202 ;; value if IGNORE-FIRST is non-nil.
4203 (catch 'zero
(funcall collect-inds element
(not ignore-first
)))
4204 (if (not ind-list
) element
4205 ;; Build ELEMENT back, replacing each string with the same
4206 ;; string minus common indentation.
4207 (let* (build ; For byte compiler.
4210 (lambda (blob mci first-flag
)
4211 ;; Return BLOB with all its strings indentation
4212 ;; shortened from MCI white spaces. FIRST-FLAG is
4213 ;; non-nil when the first string hasn't been seen
4218 (when (and first-flag
(stringp object
))
4219 (setq first-flag nil
)
4221 (replace-regexp-in-string
4222 (format "\\` \\{%d\\}" mci
) "" object
)))
4225 (replace-regexp-in-string
4226 (format "\n \\{%d\\}" mci
) "\n" object
))
4227 ((memq (org-element-type object
)
4228 org-element-recursive-objects
)
4229 (funcall build object mci first-flag
))
4231 (org-element-contents blob
)))
4233 (funcall build element
(apply 'min ind-list
) (not ignore-first
))))))
4239 ;; The first move is to implement a way to obtain the smallest element
4240 ;; containing point. This is the job of `org-element-at-point'. It
4241 ;; basically jumps back to the beginning of section containing point
4242 ;; and moves, element after element, with
4243 ;; `org-element--current-element' until the container is found. Note:
4244 ;; When using `org-element-at-point', secondary values are never
4245 ;; parsed since the function focuses on elements, not on objects.
4247 ;; At a deeper level, `org-element-context' lists all elements and
4248 ;; objects containing point.
4250 ;; `org-element-nested-p' and `org-element-swap-A-B' may be used
4251 ;; internally by navigation and manipulation tools.
4254 (defun org-element-at-point (&optional keep-trail
)
4255 "Determine closest element around point.
4257 Return value is a list like (TYPE PROPS) where TYPE is the type
4258 of the element and PROPS a plist of properties associated to the
4261 Possible types are defined in `org-element-all-elements'.
4262 Properties depend on element or object type, but always
4263 include :begin, :end, :parent and :post-blank properties.
4265 As a special case, if point is at the very beginning of a list or
4266 sub-list, returned element will be that list instead of the first
4267 item. In the same way, if point is at the beginning of the first
4268 row of a table, returned element will be the table instead of the
4271 If optional argument KEEP-TRAIL is non-nil, the function returns
4272 a list of of elements leading to element at point. The list's
4273 CAR is always the element at point. Following positions contain
4274 element's siblings, then parents, siblings of parents, until the
4275 first element of current section."
4276 (org-with-wide-buffer
4277 ;; If at an headline, parse it. It is the sole element that
4278 ;; doesn't require to know about context. Be sure to disallow
4279 ;; secondary string parsing, though.
4280 (if (org-with-limited-levels (org-at-heading-p))
4283 (if (not keep-trail
) (org-element-headline-parser (point-max) t
)
4284 (list (org-element-headline-parser (point-max) t
))))
4285 ;; Otherwise move at the beginning of the section containing
4287 (let ((origin (point))
4288 (end (save-excursion
4289 (org-with-limited-levels (outline-next-heading)) (point)))
4290 element type special-flag trail struct prevs parent
)
4291 (org-with-limited-levels
4292 (if (org-with-limited-levels (org-before-first-heading-p))
4293 (goto-char (point-min))
4294 (org-back-to-heading)
4296 (org-skip-whitespace)
4298 ;; Parse successively each element, skipping those ending
4299 ;; before original position.
4303 (org-element--current-element end
'element special-flag struct
)
4305 (org-element-put-property element
:parent parent
)
4306 (when keep-trail
(push element trail
))
4308 ;; 1. Skip any element ending before point. Also skip
4309 ;; element ending at point when we're sure that another
4310 ;; element has started.
4311 ((let ((elem-end (org-element-property :end element
)))
4312 (when (or (< elem-end origin
)
4313 (and (= elem-end origin
) (/= elem-end end
)))
4314 (goto-char elem-end
))))
4315 ;; 2. An element containing point is always the element at
4317 ((not (memq type org-element-greater-elements
))
4318 (throw 'exit
(if keep-trail trail element
)))
4319 ;; 3. At any other greater element type, if point is
4320 ;; within contents, move into it.
4322 (let ((cbeg (org-element-property :contents-begin element
))
4323 (cend (org-element-property :contents-end element
)))
4324 (if (or (not cbeg
) (not cend
) (> cbeg origin
) (< cend origin
)
4325 ;; Create an anchor for tables and plain lists:
4326 ;; when point is at the very beginning of these
4327 ;; elements, ignoring affiliated keywords,
4328 ;; target them instead of their contents.
4329 (and (= cbeg origin
) (memq type
'(plain-list table
)))
4330 ;; When point is at contents end, do not move
4331 ;; into elements with an explicit ending, but
4332 ;; return that element instead.
4333 (and (= cend origin
)
4336 drawer dynamic-block inlinetask item
4337 plain-list property-drawer quote-block
4339 (throw 'exit
(if keep-trail trail element
))
4340 (setq parent element
)
4343 (setq special-flag
'item
4344 struct
(org-element-property :structure element
)))
4345 (property-drawer (setq special-flag
'node-property
))
4346 (table (setq special-flag
'table-row
))
4347 (otherwise (setq special-flag nil
)))
4349 (goto-char cbeg
)))))))))))
4352 (defun org-element-context ()
4353 "Return closest element or object around point.
4355 Return value is a list like (TYPE PROPS) where TYPE is the type
4356 of the element or object and PROPS a plist of properties
4359 Possible types are defined in `org-element-all-elements' and
4360 `org-element-all-objects'. Properties depend on element or
4361 object type, but always include :begin, :end, :parent
4362 and :post-blank properties."
4363 (org-with-wide-buffer
4364 (let* ((origin (point))
4365 (element (org-element-at-point))
4366 (type (car element
))
4368 ;; Check if point is inside an element containing objects or at
4369 ;; a secondary string. In that case, move to beginning of the
4370 ;; element or secondary string and set END to the other side.
4371 (if (not (or (and (eq type
'item
)
4372 (let ((tag (org-element-property :tag element
)))
4376 (search-forward tag
(point-at-eol))
4377 (goto-char (match-beginning 0))
4378 (and (>= origin
(point))
4380 ;; `1+' is required so some
4381 ;; successors can match
4382 ;; properly their object.
4383 (setq end
(1+ (match-end 0)))))))))
4384 (and (memq type
'(headline inlinetask
))
4385 (progn (beginning-of-line)
4386 (skip-chars-forward "* ")
4387 (setq end
(point-at-eol))))
4388 (and (memq type
'(paragraph table-row verse-block
))
4389 (let ((cbeg (org-element-property
4390 :contents-begin element
))
4391 (cend (org-element-property
4392 :contents-end element
)))
4393 (and (>= origin cbeg
)
4395 (progn (goto-char cbeg
) (setq end cend
)))))))
4397 (let ((restriction (org-element-restriction element
))
4401 (while (setq candidates
(org-element--get-next-object-candidates
4402 end restriction candidates
))
4403 (let ((closest-cand (rassq (apply 'min
(mapcar 'cdr candidates
))
4405 ;; If ORIGIN is before next object in element, there's
4406 ;; no point in looking further.
4407 (if (> (cdr closest-cand
) origin
) (throw 'exit parent
)
4409 (progn (goto-char (cdr closest-cand
))
4410 (funcall (intern (format "org-element-%s-parser"
4411 (car closest-cand
))))))
4412 (cbeg (org-element-property :contents-begin object
))
4413 (cend (org-element-property :contents-end object
)))
4415 ;; ORIGIN is after OBJECT, so skip it.
4416 ((< (org-element-property :end object
) origin
)
4417 (goto-char (org-element-property :end object
)))
4418 ;; ORIGIN is within a non-recursive object or at an
4419 ;; object boundaries: Return that object.
4420 ((or (not cbeg
) (> cbeg origin
) (< cend origin
))
4422 (org-element-put-property object
:parent parent
)))
4423 ;; Otherwise, move within current object and restrict
4424 ;; search to the end of its contents.
4426 (org-element-put-property object
:parent parent
)
4428 restriction
(org-element-restriction object
)
4432 (defsubst org-element-nested-p
(elem-A elem-B
)
4433 "Non-nil when elements ELEM-A and ELEM-B are nested."
4434 (let ((beg-A (org-element-property :begin elem-A
))
4435 (beg-B (org-element-property :begin elem-B
))
4436 (end-A (org-element-property :end elem-A
))
4437 (end-B (org-element-property :end elem-B
)))
4438 (or (and (>= beg-A beg-B
) (<= end-A end-B
))
4439 (and (>= beg-B beg-A
) (<= end-B end-A
)))))
4441 (defun org-element-swap-A-B (elem-A elem-B
)
4442 "Swap elements ELEM-A and ELEM-B.
4443 Assume ELEM-B is after ELEM-A in the buffer. Leave point at the
4445 (goto-char (org-element-property :begin elem-A
))
4446 ;; There are two special cases when an element doesn't start at bol:
4447 ;; the first paragraph in an item or in a footnote definition.
4448 (let ((specialp (not (bolp))))
4449 ;; Only a paragraph without any affiliated keyword can be moved at
4450 ;; ELEM-A position in such a situation. Note that the case of
4451 ;; a footnote definition is impossible: it cannot contain two
4452 ;; paragraphs in a row because it cannot contain a blank line.
4454 (or (not (eq (org-element-type elem-B
) 'paragraph
))
4455 (/= (org-element-property :begin elem-B
)
4456 (org-element-property :contents-begin elem-B
))))
4457 (error "Cannot swap elements"))
4458 ;; In a special situation, ELEM-A will have no indentation. We'll
4459 ;; give it ELEM-B's (which will in, in turn, have no indentation).
4460 (let* ((ind-B (when specialp
4461 (goto-char (org-element-property :begin elem-B
))
4462 (org-get-indentation)))
4463 (beg-A (org-element-property :begin elem-A
))
4464 (end-A (save-excursion
4465 (goto-char (org-element-property :end elem-A
))
4466 (skip-chars-backward " \r\t\n")
4468 (beg-B (org-element-property :begin elem-B
))
4469 (end-B (save-excursion
4470 (goto-char (org-element-property :end elem-B
))
4471 (skip-chars-backward " \r\t\n")
4473 ;; Store overlays responsible for visibility status. We
4474 ;; also need to store their boundaries as they will be
4475 ;; removed from buffer.
4478 (mapcar (lambda (ov) (list ov
(overlay-start ov
) (overlay-end ov
)))
4479 (overlays-in beg-A end-A
))
4480 (mapcar (lambda (ov) (list ov
(overlay-start ov
) (overlay-end ov
)))
4481 (overlays-in beg-B end-B
))))
4483 (body-A (buffer-substring beg-A end-A
))
4484 (body-B (delete-and-extract-region beg-B end-B
)))
4487 (setq body-B
(replace-regexp-in-string "\\`[ \t]*" "" body-B
))
4488 (org-indent-to-column ind-B
))
4490 ;; Restore ex ELEM-A overlays.
4491 (let ((offset (- beg-B beg-A
)))
4494 (car ov
) (+ (nth 1 ov
) offset
) (+ (nth 2 ov
) offset
)))
4497 (delete-region beg-A end-A
)
4499 ;; Restore ex ELEM-B overlays.
4502 (car ov
) (- (nth 1 ov
) offset
) (- (nth 2 ov
) offset
)))
4504 (goto-char (org-element-property :end elem-B
)))))
4506 (provide 'org-element
)
4509 ;; generated-autoload-file: "org-loaddefs.el"
4512 ;;; org-element.el ends here