org-element: Add :parent text property to strings in parse tree
[org-mode.git] / lisp / org-element.el
blobdb6a2db022fda1b064540c4eb16fe6a002fded16
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/>.
23 ;;; Commentary:
25 ;; Org syntax can be divided into three categories: "Greater
26 ;; elements", "Elements" and "Objects".
28 ;; Elements are related to the structure of the document. Indeed, all
29 ;; elements are a cover for the document: each position within belongs
30 ;; to at least one element.
32 ;; An element always starts and ends at the beginning of a line. With
33 ;; a few exceptions (`clock', `headline', `inlinetask', `item',
34 ;; `planning', `node-property', `quote-section' `section' and
35 ;; `table-row' types), it can also accept a fixed set of keywords as
36 ;; attributes. Those are called "affiliated keywords" to distinguish
37 ;; them from other keywords, which are full-fledged elements. Almost
38 ;; all affiliated keywords are referenced in
39 ;; `org-element-affiliated-keywords'; the others are export attributes
40 ;; and start with "ATTR_" prefix.
42 ;; Element containing other elements (and only elements) are called
43 ;; greater elements. Concerned types are: `center-block', `drawer',
44 ;; `dynamic-block', `footnote-definition', `headline', `inlinetask',
45 ;; `item', `plain-list', `property-drawer', `quote-block', `section'
46 ;; and `special-block'.
48 ;; Other element types are: `babel-call', `clock', `comment',
49 ;; `comment-block', `example-block', `export-block', `fixed-width',
50 ;; `horizontal-rule', `keyword', `latex-environment', `node-property',
51 ;; `paragraph', `planning', `quote-section', `src-block', `table',
52 ;; `table-row' and `verse-block'. Among them, `paragraph' and
53 ;; `verse-block' types can contain Org objects and plain text.
55 ;; Objects are related to document's contents. Some of them are
56 ;; recursive. Associated types are of the following: `bold', `code',
57 ;; `entity', `export-snippet', `footnote-reference',
58 ;; `inline-babel-call', `inline-src-block', `italic',
59 ;; `latex-fragment', `line-break', `link', `macro', `radio-target',
60 ;; `statistics-cookie', `strike-through', `subscript', `superscript',
61 ;; `table-cell', `target', `timestamp', `underline' and `verbatim'.
63 ;; Some elements also have special properties whose value can hold
64 ;; objects themselves (i.e. an item tag or an headline name). Such
65 ;; values are called "secondary strings". Any object belongs to
66 ;; either an element or a secondary string.
68 ;; Notwithstanding affiliated keywords, each greater element, element
69 ;; and object has a fixed set of properties attached to it. Among
70 ;; them, four are shared by all types: `:begin' and `:end', which
71 ;; refer to the beginning and ending buffer positions of the
72 ;; considered element or object, `:post-blank', which holds the number
73 ;; of blank lines, or white spaces, at its end and `:parent' which
74 ;; refers to the element or object containing it. Greater elements
75 ;; and elements containing objects will also have `:contents-begin'
76 ;; and `:contents-end' properties to delimit contents.
78 ;; At the lowest level, a `:parent' property is also attached to any
79 ;; string, as a text property.
81 ;; Lisp-wise, an element or an object can be represented as a list.
82 ;; It follows the pattern (TYPE PROPERTIES CONTENTS), where:
83 ;; TYPE is a symbol describing the Org element or object.
84 ;; PROPERTIES is the property list attached to it. See docstring of
85 ;; appropriate parsing function to get an exhaustive
86 ;; list.
87 ;; CONTENTS is a list of elements, objects or raw strings contained
88 ;; in the current element or object, when applicable.
90 ;; An Org buffer is a nested list of such elements and objects, whose
91 ;; type is `org-data' and properties is nil.
93 ;; The first part of this file defines Org syntax, while the second
94 ;; one provide accessors and setters functions.
96 ;; The next part implements a parser and an interpreter for each
97 ;; element and object type in Org syntax.
99 ;; The following part creates a fully recursive buffer parser. It
100 ;; also provides a tool to map a function to elements or objects
101 ;; matching some criteria in the parse tree. Functions of interest
102 ;; are `org-element-parse-buffer', `org-element-map' and, to a lesser
103 ;; extent, `org-element-parse-secondary-string'.
105 ;; The penultimate part is the cradle of an interpreter for the
106 ;; obtained parse tree: `org-element-interpret-data'.
108 ;; The library ends by furnishing `org-element-at-point' function, and
109 ;; a way to give information about document structure around point
110 ;; with `org-element-context'.
113 ;;; Code:
115 (eval-when-compile (require 'cl))
116 (require 'org)
120 ;;; Definitions And Rules
122 ;; Define elements, greater elements and specify recursive objects,
123 ;; along with the affiliated keywords recognized. Also set up
124 ;; restrictions on recursive objects combinations.
126 ;; These variables really act as a control center for the parsing
127 ;; process.
129 (defconst org-element-paragraph-separate
130 (concat "^\\(?:"
131 ;; Headlines, inlinetasks.
132 org-outline-regexp "\\|"
133 ;; Footnote definitions.
134 "\\[\\(?:[0-9]+\\|fn:[-_[:word:]]+\\)\\]" "\\|"
135 "[ \t]*\\(?:"
136 ;; Empty lines.
137 "$" "\\|"
138 ;; Tables (any type).
139 "\\(?:|\\|\\+-[-+]\\)" "\\|"
140 ;; Blocks (any type), Babel calls, drawers (any type),
141 ;; fixed-width areas and keywords. Note: this is only an
142 ;; indication and need some thorough check.
143 "[#:]" "\\|"
144 ;; Horizontal rules.
145 "-\\{5,\\}[ \t]*$" "\\|"
146 ;; LaTeX environments.
147 "\\\\begin{\\([A-Za-z0-9]+\\*?\\)}" "\\|"
148 ;; Planning and Clock lines.
149 (regexp-opt (list org-scheduled-string
150 org-deadline-string
151 org-closed-string
152 org-clock-string))
153 "\\|"
154 ;; Lists.
155 (let ((term (case org-plain-list-ordered-item-terminator
156 (?\) ")") (?. "\\.") (otherwise "[.)]")))
157 (alpha (and org-alphabetical-lists "\\|[A-Za-z]")))
158 (concat "\\(?:[-+*]\\|\\(?:[0-9]+" alpha "\\)" term "\\)"
159 "\\(?:[ \t]\\|$\\)"))
160 "\\)\\)")
161 "Regexp to separate paragraphs in an Org buffer.
162 In the case of lines starting with \"#\" and \":\", this regexp
163 is not sufficient to know if point is at a paragraph ending. See
164 `org-element-paragraph-parser' for more information.")
166 (defconst org-element-all-elements
167 '(babel-call center-block clock comment comment-block drawer dynamic-block
168 example-block export-block fixed-width footnote-definition
169 headline horizontal-rule inlinetask item keyword
170 latex-environment node-property paragraph plain-list planning
171 property-drawer quote-block quote-section section special-block
172 src-block table table-row verse-block)
173 "Complete list of element types.")
175 (defconst org-element-greater-elements
176 '(center-block drawer dynamic-block footnote-definition headline inlinetask
177 item plain-list property-drawer quote-block section
178 special-block table)
179 "List of recursive element types aka Greater Elements.")
181 (defconst org-element-all-successors
182 '(export-snippet footnote-reference inline-babel-call inline-src-block
183 latex-or-entity line-break link macro radio-target
184 statistics-cookie sub/superscript table-cell target
185 text-markup timestamp)
186 "Complete list of successors.")
188 (defconst org-element-object-successor-alist
189 '((subscript . sub/superscript) (superscript . sub/superscript)
190 (bold . text-markup) (code . text-markup) (italic . text-markup)
191 (strike-through . text-markup) (underline . text-markup)
192 (verbatim . text-markup) (entity . latex-or-entity)
193 (latex-fragment . latex-or-entity))
194 "Alist of translations between object type and successor name.
196 Sharing the same successor comes handy when, for example, the
197 regexp matching one object can also match the other object.")
199 (defconst org-element-all-objects
200 '(bold code entity export-snippet footnote-reference inline-babel-call
201 inline-src-block italic line-break latex-fragment link macro
202 radio-target statistics-cookie strike-through subscript superscript
203 table-cell target timestamp underline verbatim)
204 "Complete list of object types.")
206 (defconst org-element-recursive-objects
207 '(bold italic link subscript radio-target strike-through superscript
208 table-cell underline)
209 "List of recursive object types.")
211 (defconst org-element-block-name-alist
212 '(("CENTER" . org-element-center-block-parser)
213 ("COMMENT" . org-element-comment-block-parser)
214 ("EXAMPLE" . org-element-example-block-parser)
215 ("QUOTE" . org-element-quote-block-parser)
216 ("SRC" . org-element-src-block-parser)
217 ("VERSE" . org-element-verse-block-parser))
218 "Alist between block names and the associated parsing function.
219 Names must be uppercase. Any block whose name has no association
220 is parsed with `org-element-special-block-parser'.")
222 (defconst org-element-link-type-is-file
223 '("file" "file+emacs" "file+sys" "docview")
224 "List of link types equivalent to \"file\".
225 Only these types can accept search options and an explicit
226 application to open them.")
228 (defconst org-element-affiliated-keywords
229 '("CAPTION" "DATA" "HEADER" "HEADERS" "LABEL" "NAME" "PLOT" "RESNAME" "RESULT"
230 "RESULTS" "SOURCE" "SRCNAME" "TBLNAME")
231 "List of affiliated keywords as strings.
232 By default, all keywords setting attributes (i.e. \"ATTR_LATEX\")
233 are affiliated keywords and need not to be in this list.")
235 (defconst org-element--affiliated-re
236 (format "[ \t]*#\\+%s:"
237 ;; Regular affiliated keywords.
238 (format "\\(%s\\|ATTR_[-_A-Za-z0-9]+\\)\\(?:\\[\\(.*\\)\\]\\)?"
239 (regexp-opt org-element-affiliated-keywords)))
240 "Regexp matching any affiliated keyword.
242 Keyword name is put in match group 1. Moreover, if keyword
243 belongs to `org-element-dual-keywords', put the dual value in
244 match group 2.
246 Don't modify it, set `org-element-affiliated-keywords' instead.")
248 (defconst org-element-keyword-translation-alist
249 '(("DATA" . "NAME") ("LABEL" . "NAME") ("RESNAME" . "NAME")
250 ("SOURCE" . "NAME") ("SRCNAME" . "NAME") ("TBLNAME" . "NAME")
251 ("RESULT" . "RESULTS") ("HEADERS" . "HEADER"))
252 "Alist of usual translations for keywords.
253 The key is the old name and the value the new one. The property
254 holding their value will be named after the translated name.")
256 (defconst org-element-multiple-keywords '("CAPTION" "HEADER")
257 "List of affiliated keywords that can occur more than once in an element.
259 Their value will be consed into a list of strings, which will be
260 returned as the value of the property.
262 This list is checked after translations have been applied. See
263 `org-element-keyword-translation-alist'.
265 By default, all keywords setting attributes (i.e. \"ATTR_LATEX\")
266 allow multiple occurrences and need not to be in this list.")
268 (defconst org-element-parsed-keywords '("CAPTION")
269 "List of affiliated keywords whose value can be parsed.
271 Their value will be stored as a secondary string: a list of
272 strings and objects.
274 This list is checked after translations have been applied. See
275 `org-element-keyword-translation-alist'.")
277 (defconst org-element-dual-keywords '("CAPTION" "RESULTS")
278 "List of affiliated keywords which can have a secondary value.
280 In Org syntax, they can be written with optional square brackets
281 before the colons. For example, RESULTS keyword can be
282 associated to a hash value with the following:
284 #+RESULTS[hash-string]: some-source
286 This list is checked after translations have been applied. See
287 `org-element-keyword-translation-alist'.")
289 (defconst org-element-document-properties '("AUTHOR" "DATE" "TITLE")
290 "List of properties associated to the whole document.
291 Any keyword in this list will have its value parsed and stored as
292 a secondary string.")
294 (defconst org-element-object-restrictions
295 '((bold export-snippet inline-babel-call inline-src-block latex-or-entity link
296 radio-target sub/superscript target text-markup timestamp)
297 (footnote-reference export-snippet footnote-reference inline-babel-call
298 inline-src-block latex-or-entity line-break link macro
299 radio-target sub/superscript target text-markup
300 timestamp)
301 (headline inline-babel-call inline-src-block latex-or-entity link macro
302 radio-target statistics-cookie sub/superscript target text-markup
303 timestamp)
304 (inlinetask inline-babel-call inline-src-block latex-or-entity link macro
305 radio-target sub/superscript target text-markup timestamp)
306 (italic export-snippet inline-babel-call inline-src-block latex-or-entity
307 link radio-target sub/superscript target text-markup timestamp)
308 (item export-snippet footnote-reference inline-babel-call latex-or-entity
309 link macro radio-target sub/superscript target text-markup)
310 (keyword inline-babel-call inline-src-block latex-or-entity link macro
311 sub/superscript text-markup)
312 (link export-snippet inline-babel-call inline-src-block latex-or-entity link
313 sub/superscript text-markup)
314 (paragraph export-snippet footnote-reference inline-babel-call
315 inline-src-block latex-or-entity line-break link macro
316 radio-target statistics-cookie sub/superscript target text-markup
317 timestamp)
318 (radio-target export-snippet latex-or-entity sub/superscript)
319 (strike-through export-snippet inline-babel-call inline-src-block
320 latex-or-entity link radio-target sub/superscript target
321 text-markup timestamp)
322 (subscript export-snippet inline-babel-call inline-src-block latex-or-entity
323 sub/superscript target text-markup)
324 (superscript export-snippet inline-babel-call inline-src-block
325 latex-or-entity sub/superscript target text-markup)
326 (table-cell export-snippet latex-or-entity link macro radio-target
327 sub/superscript target text-markup timestamp)
328 (table-row table-cell)
329 (underline export-snippet inline-babel-call inline-src-block latex-or-entity
330 link radio-target sub/superscript target text-markup timestamp)
331 (verse-block footnote-reference inline-babel-call inline-src-block
332 latex-or-entity line-break link macro radio-target
333 sub/superscript target text-markup timestamp))
334 "Alist of objects restrictions.
336 CAR is an element or object type containing objects and CDR is
337 a list of successors that will be called within an element or
338 object of such type.
340 For example, in a `radio-target' object, one can only find
341 entities, export snippets, latex-fragments, subscript and
342 superscript.
344 This alist also applies to secondary string. For example, an
345 `headline' type element doesn't directly contain objects, but
346 still has an entry since one of its properties (`:title') does.")
348 (defconst org-element-secondary-value-alist
349 '((headline . :title)
350 (inlinetask . :title)
351 (item . :tag)
352 (footnote-reference . :inline-definition))
353 "Alist between element types and location of secondary value.")
357 ;;; Accessors and Setters
359 ;; Provide four accessors: `org-element-type', `org-element-property'
360 ;; `org-element-contents' and `org-element-restriction'.
362 ;; Setter functions allow to modify elements by side effect. There is
363 ;; `org-element-put-property', `org-element-set-contents',
364 ;; `org-element-set-element' and `org-element-adopt-element'. Note
365 ;; that `org-element-set-element' and `org-element-adopt-elements' are
366 ;; higher level functions since also update `:parent' property.
368 (defsubst org-element-type (element)
369 "Return type of ELEMENT.
371 The function returns the type of the element or object provided.
372 It can also return the following special value:
373 `plain-text' for a string
374 `org-data' for a complete document
375 nil in any other case."
376 (cond
377 ((not (consp element)) (and (stringp element) 'plain-text))
378 ((symbolp (car element)) (car element))))
380 (defsubst org-element-property (property element)
381 "Extract the value from the PROPERTY of an ELEMENT."
382 (if (stringp element) (get-text-property 0 property element)
383 (plist-get (nth 1 element) property)))
385 (defsubst org-element-contents (element)
386 "Extract contents from an ELEMENT."
387 (and (consp element) (nthcdr 2 element)))
389 (defsubst org-element-restriction (element)
390 "Return restriction associated to ELEMENT.
391 ELEMENT can be an element, an object or a symbol representing an
392 element or object type."
393 (cdr (assq (if (symbolp element) element (org-element-type element))
394 org-element-object-restrictions)))
396 (defsubst org-element-put-property (element property value)
397 "In ELEMENT set PROPERTY to VALUE.
398 Return modified element."
399 (if (stringp element) (org-add-props element nil property value)
400 (setcar (cdr element) (plist-put (nth 1 element) property value))
401 element))
403 (defsubst org-element-set-contents (element &rest contents)
404 "Set ELEMENT contents to CONTENTS.
405 Return modified element."
406 (cond ((not element) (list contents))
407 ((cdr element) (setcdr (cdr element) contents))
408 (t (nconc element contents))))
410 (defsubst org-element-set-element (old new)
411 "Replace element or object OLD with element or object NEW.
412 The function takes care of setting `:parent' property for NEW."
413 ;; Since OLD is going to be changed into NEW by side-effect, first
414 ;; make sure that every element or object within NEW has OLD as
415 ;; parent.
416 (mapc (lambda (blob) (org-element-put-property blob :parent old))
417 (org-element-contents new))
418 ;; Transfer contents.
419 (apply 'org-element-set-contents old (org-element-contents new))
420 ;; Ensure NEW has same parent as OLD, then overwrite OLD properties
421 ;; with NEW's.
422 (org-element-put-property new :parent (org-element-property :parent old))
423 (setcar (cdr old) (nth 1 new))
424 ;; Transfer type.
425 (setcar old (car new)))
427 (defsubst org-element-adopt-elements (parent &rest children)
428 "Append elements to the contents of another element.
430 PARENT is an element or object. CHILDREN can be elements,
431 objects, or a strings.
433 The function takes care of setting `:parent' property for CHILD.
434 Return parent element."
435 (if (not parent) children
436 ;; Link every child to PARENT.
437 (mapc (lambda (child) (org-element-put-property child :parent parent))
438 children)
439 ;; Add CHILDREN at the end of PARENT contents.
440 (apply 'org-element-set-contents
441 parent
442 (nconc (org-element-contents parent) children))
443 ;; Return modified PARENT element.
444 parent))
448 ;;; Greater elements
450 ;; For each greater element type, we define a parser and an
451 ;; interpreter.
453 ;; A parser returns the element or object as the list described above.
454 ;; Most of them accepts no argument. Though, exceptions exist. Hence
455 ;; every element containing a secondary string (see
456 ;; `org-element-secondary-value-alist') will accept an optional
457 ;; argument to toggle parsing of that secondary string. Moreover,
458 ;; `item' parser requires current list's structure as its first
459 ;; element.
461 ;; An interpreter accepts two arguments: the list representation of
462 ;; the element or object, and its contents. The latter may be nil,
463 ;; depending on the element or object considered. It returns the
464 ;; appropriate Org syntax, as a string.
466 ;; Parsing functions must follow the naming convention:
467 ;; org-element-TYPE-parser, where TYPE is greater element's type, as
468 ;; defined in `org-element-greater-elements'.
470 ;; Similarly, interpreting functions must follow the naming
471 ;; convention: org-element-TYPE-interpreter.
473 ;; With the exception of `headline' and `item' types, greater elements
474 ;; cannot contain other greater elements of their own type.
476 ;; Beside implementing a parser and an interpreter, adding a new
477 ;; greater element requires to tweak `org-element--current-element'.
478 ;; Moreover, the newly defined type must be added to both
479 ;; `org-element-all-elements' and `org-element-greater-elements'.
482 ;;;; Center Block
484 (defun org-element-center-block-parser (limit affiliated)
485 "Parse a center block.
487 LIMIT bounds the search. AFFILIATED is a list of which CAR is
488 the buffer position at the beginning of the first affiliated
489 keyword and CDR is a plist of affiliated keywords along with
490 their value.
492 Return a list whose CAR is `center-block' and CDR is a plist
493 containing `:begin', `:end', `:hiddenp', `:contents-begin',
494 `:contents-end' and `:post-blank' keywords.
496 Assume point is at the beginning of the block."
497 (let ((case-fold-search t))
498 (if (not (save-excursion
499 (re-search-forward "^[ \t]*#\\+END_CENTER[ \t]*$" limit t)))
500 ;; Incomplete block: parse it as a paragraph.
501 (org-element-paragraph-parser limit affiliated)
502 (let ((block-end-line (match-beginning 0)))
503 (let* ((begin (car affiliated))
504 ;; Empty blocks have no contents.
505 (contents-begin (progn (forward-line)
506 (and (< (point) block-end-line)
507 (point))))
508 (contents-end (and contents-begin block-end-line))
509 (hidden (org-invisible-p2))
510 (pos-before-blank (progn (goto-char block-end-line)
511 (forward-line)
512 (point)))
513 (end (save-excursion (skip-chars-forward " \r\t\n" limit)
514 (skip-chars-backward " \t")
515 (if (bolp) (point) (line-end-position)))))
516 (list 'center-block
517 (nconc
518 (list :begin begin
519 :end end
520 :hiddenp hidden
521 :contents-begin contents-begin
522 :contents-end contents-end
523 :post-blank (count-lines pos-before-blank end))
524 (cdr affiliated))))))))
526 (defun org-element-center-block-interpreter (center-block contents)
527 "Interpret CENTER-BLOCK element as Org syntax.
528 CONTENTS is the contents of the element."
529 (format "#+BEGIN_CENTER\n%s#+END_CENTER" contents))
532 ;;;; Drawer
534 (defun org-element-drawer-parser (limit affiliated)
535 "Parse a drawer.
537 LIMIT bounds the search. AFFILIATED is a list of which CAR is
538 the buffer position at the beginning of the first affiliated
539 keyword and CDR is a plist of affiliated keywords along with
540 their value.
542 Return a list whose CAR is `drawer' and CDR is a plist containing
543 `:drawer-name', `:begin', `:end', `:hiddenp', `:contents-begin',
544 `:contents-end' and `:post-blank' keywords.
546 Assume point is at beginning of drawer."
547 (let ((case-fold-search t))
548 (if (not (save-excursion (re-search-forward "^[ \t]*:END:[ \t]*$" limit t)))
549 ;; Incomplete drawer: parse it as a paragraph.
550 (org-element-paragraph-parser limit affiliated)
551 (save-excursion
552 (let* ((drawer-end-line (match-beginning 0))
553 (name (progn (looking-at org-drawer-regexp)
554 (org-match-string-no-properties 1)))
555 (begin (car affiliated))
556 ;; Empty drawers have no contents.
557 (contents-begin (progn (forward-line)
558 (and (< (point) drawer-end-line)
559 (point))))
560 (contents-end (and contents-begin drawer-end-line))
561 (hidden (org-invisible-p2))
562 (pos-before-blank (progn (goto-char drawer-end-line)
563 (forward-line)
564 (point)))
565 (end (progn (skip-chars-forward " \r\t\n" limit)
566 (skip-chars-backward " \t")
567 (if (bolp) (point) (line-end-position)))))
568 (list 'drawer
569 (nconc
570 (list :begin begin
571 :end end
572 :drawer-name name
573 :hiddenp hidden
574 :contents-begin contents-begin
575 :contents-end contents-end
576 :post-blank (count-lines pos-before-blank end))
577 (cdr affiliated))))))))
579 (defun org-element-drawer-interpreter (drawer contents)
580 "Interpret DRAWER element as Org syntax.
581 CONTENTS is the contents of the element."
582 (format ":%s:\n%s:END:"
583 (org-element-property :drawer-name drawer)
584 contents))
587 ;;;; Dynamic Block
589 (defun org-element-dynamic-block-parser (limit affiliated)
590 "Parse a dynamic block.
592 LIMIT bounds the search. AFFILIATED is a list of which CAR is
593 the buffer position at the beginning of the first affiliated
594 keyword and CDR is a plist of affiliated keywords along with
595 their value.
597 Return a list whose CAR is `dynamic-block' and CDR is a plist
598 containing `:block-name', `:begin', `:end', `:hiddenp',
599 `:contents-begin', `:contents-end', `:arguments' and
600 `:post-blank' keywords.
602 Assume point is at beginning of dynamic block."
603 (let ((case-fold-search t))
604 (if (not (save-excursion
605 (re-search-forward "^[ \t]*#\\+END:?[ \t]*$" limit t)))
606 ;; Incomplete block: parse it as a paragraph.
607 (org-element-paragraph-parser limit affiliated)
608 (let ((block-end-line (match-beginning 0)))
609 (save-excursion
610 (let* ((name (progn (looking-at org-dblock-start-re)
611 (org-match-string-no-properties 1)))
612 (arguments (org-match-string-no-properties 3))
613 (begin (car affiliated))
614 ;; Empty blocks have no contents.
615 (contents-begin (progn (forward-line)
616 (and (< (point) block-end-line)
617 (point))))
618 (contents-end (and contents-begin block-end-line))
619 (hidden (org-invisible-p2))
620 (pos-before-blank (progn (goto-char block-end-line)
621 (forward-line)
622 (point)))
623 (end (progn (skip-chars-forward " \r\t\n" limit)
624 (skip-chars-backward " \t")
625 (if (bolp) (point) (line-end-position)))))
626 (list 'dynamic-block
627 (nconc
628 (list :begin begin
629 :end end
630 :block-name name
631 :arguments arguments
632 :hiddenp hidden
633 :contents-begin contents-begin
634 :contents-end contents-end
635 :post-blank (count-lines pos-before-blank end))
636 (cdr affiliated)))))))))
638 (defun org-element-dynamic-block-interpreter (dynamic-block contents)
639 "Interpret DYNAMIC-BLOCK element as Org syntax.
640 CONTENTS is the contents of the element."
641 (format "#+BEGIN: %s%s\n%s#+END:"
642 (org-element-property :block-name dynamic-block)
643 (let ((args (org-element-property :arguments dynamic-block)))
644 (and args (concat " " args)))
645 contents))
648 ;;;; Footnote Definition
650 (defun org-element-footnote-definition-parser (limit affiliated)
651 "Parse a footnote definition.
653 LIMIT bounds the search. AFFILIATED is a list of which CAR is
654 the buffer position at the beginning of the first affiliated
655 keyword and CDR is a plist of affiliated keywords along with
656 their value.
658 Return a list whose CAR is `footnote-definition' and CDR is
659 a plist containing `:label', `:begin' `:end', `:contents-begin',
660 `:contents-end' and `:post-blank' keywords.
662 Assume point is at the beginning of the footnote definition."
663 (save-excursion
664 (let* ((label (progn (looking-at org-footnote-definition-re)
665 (org-match-string-no-properties 1)))
666 (begin (car affiliated))
667 (ending (save-excursion
668 (if (progn
669 (end-of-line)
670 (re-search-forward
671 (concat org-outline-regexp-bol "\\|"
672 org-footnote-definition-re "\\|"
673 "^[ \t]*$") limit 'move))
674 (match-beginning 0)
675 (point))))
676 (contents-begin (progn (search-forward "]")
677 (skip-chars-forward " \r\t\n" ending)
678 (and (/= (point) ending) (point))))
679 (contents-end (and contents-begin ending))
680 (end (progn (goto-char ending)
681 (skip-chars-forward " \r\t\n" limit)
682 (skip-chars-backward " \t")
683 (if (bolp) (point) (line-end-position)))))
684 (list 'footnote-definition
685 (nconc
686 (list :label label
687 :begin begin
688 :end end
689 :contents-begin contents-begin
690 :contents-end contents-end
691 :post-blank (count-lines ending end))
692 (cdr affiliated))))))
694 (defun org-element-footnote-definition-interpreter (footnote-definition contents)
695 "Interpret FOOTNOTE-DEFINITION element as Org syntax.
696 CONTENTS is the contents of the footnote-definition."
697 (concat (format "[%s]" (org-element-property :label footnote-definition))
699 contents))
702 ;;;; Headline
704 (defun org-element-headline-parser (limit &optional raw-secondary-p)
705 "Parse an headline.
707 Return a list whose CAR is `headline' and CDR is a plist
708 containing `:raw-value', `:title', `:begin', `:end',
709 `:pre-blank', `:hiddenp', `:contents-begin' and `:contents-end',
710 `:level', `:priority', `:tags', `:todo-keyword',`:todo-type',
711 `:scheduled', `:deadline', `:timestamp', `:clock', `:category',
712 `:quotedp', `:archivedp', `:commentedp' and `:footnote-section-p'
713 keywords.
715 The plist also contains any property set in the property drawer,
716 with its name in lowercase, the underscores replaced with hyphens
717 and colons at the beginning (i.e. `:custom-id').
719 When RAW-SECONDARY-P is non-nil, headline's title will not be
720 parsed as a secondary string, but as a plain string instead.
722 Assume point is at beginning of the headline."
723 (save-excursion
724 (let* ((components (org-heading-components))
725 (level (nth 1 components))
726 (todo (nth 2 components))
727 (todo-type
728 (and todo (if (member todo org-done-keywords) 'done 'todo)))
729 (tags (let ((raw-tags (nth 5 components)))
730 (and raw-tags (org-split-string raw-tags ":"))))
731 (raw-value (or (nth 4 components) ""))
732 (quotedp
733 (let ((case-fold-search nil))
734 (string-match (format "^%s\\( \\|$\\)" org-quote-string)
735 raw-value)))
736 (commentedp
737 (let ((case-fold-search nil))
738 (string-match (format "^%s\\( \\|$\\)" org-comment-string)
739 raw-value)))
740 (archivedp (member org-archive-tag tags))
741 (footnote-section-p (and org-footnote-section
742 (string= org-footnote-section raw-value)))
743 ;; Normalize property names: ":SOME_PROP:" becomes
744 ;; ":some-prop".
745 (standard-props (let (plist)
746 (mapc
747 (lambda (p)
748 (let ((p-name (downcase (car p))))
749 (while (string-match "_" p-name)
750 (setq p-name
751 (replace-match "-" nil nil p-name)))
752 (setq p-name (intern (concat ":" p-name)))
753 (setq plist
754 (plist-put plist p-name (cdr p)))))
755 (org-entry-properties nil 'standard))
756 plist))
757 (time-props (org-entry-properties nil 'special "CLOCK"))
758 (scheduled (cdr (assoc "SCHEDULED" time-props)))
759 (deadline (cdr (assoc "DEADLINE" time-props)))
760 (clock (cdr (assoc "CLOCK" time-props)))
761 (timestamp (cdr (assoc "TIMESTAMP" time-props)))
762 (begin (point))
763 (end (save-excursion (goto-char (org-end-of-subtree t t))))
764 (pos-after-head (progn (forward-line) (point)))
765 (contents-begin (save-excursion
766 (skip-chars-forward " \r\t\n" end)
767 (and (/= (point) end) (line-beginning-position))))
768 (hidden (org-invisible-p2))
769 (contents-end (and contents-begin
770 (progn (goto-char end)
771 (skip-chars-backward " \r\t\n")
772 (forward-line)
773 (point)))))
774 ;; Clean RAW-VALUE from any quote or comment string.
775 (when (or quotedp commentedp)
776 (let ((case-fold-search nil))
777 (setq raw-value
778 (replace-regexp-in-string
779 (concat
780 (regexp-opt (list org-quote-string org-comment-string))
781 "\\(?: \\|$\\)")
783 raw-value))))
784 ;; Clean TAGS from archive tag, if any.
785 (when archivedp (setq tags (delete org-archive-tag tags)))
786 (let ((headline
787 (list 'headline
788 (nconc
789 (list :raw-value raw-value
790 :begin begin
791 :end end
792 :pre-blank
793 (if (not contents-begin) 0
794 (count-lines pos-after-head contents-begin))
795 :hiddenp hidden
796 :contents-begin contents-begin
797 :contents-end contents-end
798 :level level
799 :priority (nth 3 components)
800 :tags tags
801 :todo-keyword todo
802 :todo-type todo-type
803 :scheduled scheduled
804 :deadline deadline
805 :timestamp timestamp
806 :clock clock
807 :post-blank (count-lines
808 (if (not contents-end) pos-after-head
809 (goto-char contents-end)
810 (forward-line)
811 (point))
812 end)
813 :footnote-section-p footnote-section-p
814 :archivedp archivedp
815 :commentedp commentedp
816 :quotedp quotedp)
817 standard-props))))
818 (org-element-put-property
819 headline :title
820 (if raw-secondary-p raw-value
821 (org-element-parse-secondary-string
822 raw-value (org-element-restriction 'headline) headline)))))))
824 (defun org-element-headline-interpreter (headline contents)
825 "Interpret HEADLINE element as Org syntax.
826 CONTENTS is the contents of the element."
827 (let* ((level (org-element-property :level headline))
828 (todo (org-element-property :todo-keyword headline))
829 (priority (org-element-property :priority headline))
830 (title (org-element-interpret-data
831 (org-element-property :title headline)))
832 (tags (let ((tag-list (if (org-element-property :archivedp headline)
833 (cons org-archive-tag
834 (org-element-property :tags headline))
835 (org-element-property :tags headline))))
836 (and tag-list
837 (format ":%s:" (mapconcat 'identity tag-list ":")))))
838 (commentedp (org-element-property :commentedp headline))
839 (quotedp (org-element-property :quotedp headline))
840 (pre-blank (or (org-element-property :pre-blank headline) 0))
841 (heading (concat (make-string level ?*)
842 (and todo (concat " " todo))
843 (and quotedp (concat " " org-quote-string))
844 (and commentedp (concat " " org-comment-string))
845 (and priority
846 (format " [#%s]" (char-to-string priority)))
847 (cond ((and org-footnote-section
848 (org-element-property
849 :footnote-section-p headline))
850 (concat " " org-footnote-section))
851 (title (concat " " title))))))
852 (concat heading
853 ;; Align tags.
854 (when tags
855 (cond
856 ((zerop org-tags-column) (format " %s" tags))
857 ((< org-tags-column 0)
858 (concat
859 (make-string
860 (max (- (+ org-tags-column (length heading) (length tags))) 1)
862 tags))
864 (concat
865 (make-string (max (- org-tags-column (length heading)) 1) ? )
866 tags))))
867 (make-string (1+ pre-blank) 10)
868 contents)))
871 ;;;; Inlinetask
873 (defun org-element-inlinetask-parser (limit &optional raw-secondary-p)
874 "Parse an inline task.
876 Return a list whose CAR is `inlinetask' and CDR is a plist
877 containing `:title', `:begin', `:end', `:hiddenp',
878 `:contents-begin' and `:contents-end', `:level', `:priority',
879 `:raw-value', `:tags', `:todo-keyword', `:todo-type',
880 `:scheduled', `:deadline', `:timestamp', `:clock' and
881 `:post-blank' keywords.
883 The plist also contains any property set in the property drawer,
884 with its name in lowercase, the underscores replaced with hyphens
885 and colons at the beginning (i.e. `:custom-id').
887 When optional argument RAW-SECONDARY-P is non-nil, inline-task's
888 title will not be parsed as a secondary string, but as a plain
889 string instead.
891 Assume point is at beginning of the inline task."
892 (save-excursion
893 (let* ((keywords (org-element--collect-affiliated-keywords))
894 (begin (car keywords))
895 (components (org-heading-components))
896 (todo (nth 2 components))
897 (todo-type (and todo
898 (if (member todo org-done-keywords) 'done 'todo)))
899 (tags (let ((raw-tags (nth 5 components)))
900 (and raw-tags (org-split-string raw-tags ":"))))
901 (raw-value (or (nth 4 components) ""))
902 ;; Normalize property names: ":SOME_PROP:" becomes
903 ;; ":some-prop".
904 (standard-props (let (plist)
905 (mapc
906 (lambda (p)
907 (let ((p-name (downcase (car p))))
908 (while (string-match "_" p-name)
909 (setq p-name
910 (replace-match "-" nil nil p-name)))
911 (setq p-name (intern (concat ":" p-name)))
912 (setq plist
913 (plist-put plist p-name (cdr p)))))
914 (org-entry-properties nil 'standard))
915 plist))
916 (time-props (org-entry-properties nil 'special "CLOCK"))
917 (scheduled (cdr (assoc "SCHEDULED" time-props)))
918 (deadline (cdr (assoc "DEADLINE" time-props)))
919 (clock (cdr (assoc "CLOCK" time-props)))
920 (timestamp (cdr (assoc "TIMESTAMP" time-props)))
921 (task-end (save-excursion
922 (end-of-line)
923 (and (re-search-forward "^\\*+ END" limit t)
924 (match-beginning 0))))
925 (contents-begin (progn (forward-line)
926 (and task-end (< (point) task-end) (point))))
927 (hidden (and contents-begin (org-invisible-p2)))
928 (contents-end (and contents-begin task-end))
929 (before-blank (if (not task-end) (point)
930 (goto-char task-end)
931 (forward-line)
932 (point)))
933 (end (progn (skip-chars-forward " \r\t\n" limit)
934 (skip-chars-backward " \t")
935 (if (bolp) (point) (line-end-position))))
936 (inlinetask
937 (list 'inlinetask
938 (nconc
939 (list :raw-value raw-value
940 :begin begin
941 :end end
942 :hiddenp hidden
943 :contents-begin contents-begin
944 :contents-end contents-end
945 :level (nth 1 components)
946 :priority (nth 3 components)
947 :tags tags
948 :todo-keyword todo
949 :todo-type todo-type
950 :scheduled scheduled
951 :deadline deadline
952 :timestamp timestamp
953 :clock clock
954 :post-blank (count-lines before-blank end))
955 standard-props
956 (cadr keywords)))))
957 (org-element-put-property
958 inlinetask :title
959 (if raw-secondary-p raw-value
960 (org-element-parse-secondary-string
961 raw-value
962 (org-element-restriction 'inlinetask)
963 inlinetask))))))
965 (defun org-element-inlinetask-interpreter (inlinetask contents)
966 "Interpret INLINETASK element as Org syntax.
967 CONTENTS is the contents of inlinetask."
968 (let* ((level (org-element-property :level inlinetask))
969 (todo (org-element-property :todo-keyword inlinetask))
970 (priority (org-element-property :priority inlinetask))
971 (title (org-element-interpret-data
972 (org-element-property :title inlinetask)))
973 (tags (let ((tag-list (org-element-property :tags inlinetask)))
974 (and tag-list
975 (format ":%s:" (mapconcat 'identity tag-list ":")))))
976 (task (concat (make-string level ?*)
977 (and todo (concat " " todo))
978 (and priority
979 (format " [#%s]" (char-to-string priority)))
980 (and title (concat " " title)))))
981 (concat task
982 ;; Align tags.
983 (when tags
984 (cond
985 ((zerop org-tags-column) (format " %s" tags))
986 ((< org-tags-column 0)
987 (concat
988 (make-string
989 (max (- (+ org-tags-column (length task) (length tags))) 1)
991 tags))
993 (concat
994 (make-string (max (- org-tags-column (length task)) 1) ? )
995 tags))))
996 ;; Prefer degenerate inlinetasks when there are no
997 ;; contents.
998 (when contents
999 (concat "\n"
1000 contents
1001 (make-string level ?*) " END")))))
1004 ;;;; Item
1006 (defun org-element-item-parser (limit struct &optional raw-secondary-p)
1007 "Parse an item.
1009 STRUCT is the structure of the plain list.
1011 Return a list whose CAR is `item' and CDR is a plist containing
1012 `:bullet', `:begin', `:end', `:contents-begin', `:contents-end',
1013 `:checkbox', `:counter', `:tag', `:structure', `:hiddenp' and
1014 `:post-blank' keywords.
1016 When optional argument RAW-SECONDARY-P is non-nil, item's tag, if
1017 any, will not be parsed as a secondary string, but as a plain
1018 string instead.
1020 Assume point is at the beginning of the item."
1021 (save-excursion
1022 (beginning-of-line)
1023 (looking-at org-list-full-item-re)
1024 (let* ((begin (point))
1025 (bullet (org-match-string-no-properties 1))
1026 (checkbox (let ((box (org-match-string-no-properties 3)))
1027 (cond ((equal "[ ]" box) 'off)
1028 ((equal "[X]" box) 'on)
1029 ((equal "[-]" box) 'trans))))
1030 (counter (let ((c (org-match-string-no-properties 2)))
1031 (save-match-data
1032 (cond
1033 ((not c) nil)
1034 ((string-match "[A-Za-z]" c)
1035 (- (string-to-char (upcase (match-string 0 c)))
1036 64))
1037 ((string-match "[0-9]+" c)
1038 (string-to-number (match-string 0 c)))))))
1039 (end (save-excursion (goto-char (org-list-get-item-end begin struct))
1040 (unless (bolp) (forward-line))
1041 (point)))
1042 (contents-begin
1043 (progn (goto-char
1044 ;; Ignore tags in un-ordered lists: they are just
1045 ;; a part of item's body.
1046 (if (and (match-beginning 4)
1047 (save-match-data (string-match "[.)]" bullet)))
1048 (match-beginning 4)
1049 (match-end 0)))
1050 (skip-chars-forward " \r\t\n" limit)
1051 ;; If first line isn't empty, contents really start
1052 ;; at the text after item's meta-data.
1053 (if (= (point-at-bol) begin) (point) (point-at-bol))))
1054 (hidden (progn (forward-line)
1055 (and (not (= (point) end)) (org-invisible-p2))))
1056 (contents-end (progn (goto-char end)
1057 (skip-chars-backward " \r\t\n")
1058 (forward-line)
1059 (point)))
1060 (item
1061 (list 'item
1062 (list :bullet bullet
1063 :begin begin
1064 :end end
1065 ;; CONTENTS-BEGIN and CONTENTS-END may be
1066 ;; mixed up in the case of an empty item
1067 ;; separated from the next by a blank line.
1068 ;; Thus ensure the former is always the
1069 ;; smallest.
1070 :contents-begin (min contents-begin contents-end)
1071 :contents-end (max contents-begin contents-end)
1072 :checkbox checkbox
1073 :counter counter
1074 :hiddenp hidden
1075 :structure struct
1076 :post-blank (count-lines contents-end end)))))
1077 (org-element-put-property
1078 item :tag
1079 (let ((raw-tag (org-list-get-tag begin struct)))
1080 (and raw-tag
1081 (if raw-secondary-p raw-tag
1082 (org-element-parse-secondary-string
1083 raw-tag (org-element-restriction 'item) item))))))))
1085 (defun org-element-item-interpreter (item contents)
1086 "Interpret ITEM element as Org syntax.
1087 CONTENTS is the contents of the element."
1088 (let* ((bullet (org-list-bullet-string (org-element-property :bullet item)))
1089 (checkbox (org-element-property :checkbox item))
1090 (counter (org-element-property :counter item))
1091 (tag (let ((tag (org-element-property :tag item)))
1092 (and tag (org-element-interpret-data tag))))
1093 ;; Compute indentation.
1094 (ind (make-string (length bullet) 32))
1095 (item-starts-with-par-p
1096 (eq (org-element-type (car (org-element-contents item)))
1097 'paragraph)))
1098 ;; Indent contents.
1099 (concat
1100 bullet
1101 (and counter (format "[@%d] " counter))
1102 (case checkbox
1103 (on "[X] ")
1104 (off "[ ] ")
1105 (trans "[-] "))
1106 (and tag (format "%s :: " tag))
1107 (let ((contents (replace-regexp-in-string
1108 "\\(^\\)[ \t]*\\S-" ind contents nil nil 1)))
1109 (if item-starts-with-par-p (org-trim contents)
1110 (concat "\n" contents))))))
1113 ;;;; Plain List
1115 (defun org-element-plain-list-parser (limit affiliated structure)
1116 "Parse a plain list.
1118 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1119 the buffer position at the beginning of the first affiliated
1120 keyword and CDR is a plist of affiliated keywords along with
1121 their value. STRUCTURE is the structure of the plain list being
1122 parsed.
1124 Return a list whose CAR is `plain-list' and CDR is a plist
1125 containing `:type', `:begin', `:end', `:contents-begin' and
1126 `:contents-end', `:structure' and `:post-blank' keywords.
1128 Assume point is at the beginning of the list."
1129 (save-excursion
1130 (let* ((struct (or structure (org-list-struct)))
1131 (prevs (org-list-prevs-alist struct))
1132 (parents (org-list-parents-alist struct))
1133 (type (org-list-get-list-type (point) struct prevs))
1134 (contents-begin (point))
1135 (begin (car affiliated))
1136 (contents-end
1137 (progn (goto-char (org-list-get-list-end (point) struct prevs))
1138 (unless (bolp) (forward-line))
1139 (point)))
1140 (end (progn (skip-chars-forward " \r\t\n" limit)
1141 (skip-chars-backward " \t")
1142 (if (bolp) (point) (line-end-position)))))
1143 ;; Return value.
1144 (list 'plain-list
1145 (nconc
1146 (list :type type
1147 :begin begin
1148 :end end
1149 :contents-begin contents-begin
1150 :contents-end contents-end
1151 :structure struct
1152 :post-blank (count-lines contents-end end))
1153 (cdr affiliated))))))
1155 (defun org-element-plain-list-interpreter (plain-list contents)
1156 "Interpret PLAIN-LIST element as Org syntax.
1157 CONTENTS is the contents of the element."
1158 (with-temp-buffer
1159 (insert contents)
1160 (goto-char (point-min))
1161 (org-list-repair)
1162 (buffer-string)))
1165 ;;;; Property Drawer
1167 (defun org-element-property-drawer-parser (limit affiliated)
1168 "Parse a property drawer.
1170 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1171 the buffer position at the beginning of the first affiliated
1172 keyword and CDR is a plist of affiliated keywords along with
1173 their value.
1175 Return a list whose CAR is `property-drawer' and CDR is a plist
1176 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1177 `:contents-end' and `:post-blank' keywords.
1179 Assume point is at the beginning of the property drawer."
1180 (save-excursion
1181 (let ((case-fold-search t))
1182 (if (not (save-excursion
1183 (re-search-forward "^[ \t]*:END:[ \t]*$" limit t)))
1184 ;; Incomplete drawer: parse it as a paragraph.
1185 (org-element-paragraph-parser limit affiliated)
1186 (save-excursion
1187 (let* ((drawer-end-line (match-beginning 0))
1188 (begin (car affiliated))
1189 (contents-begin (progn (forward-line)
1190 (and (< (point) drawer-end-line)
1191 (point))))
1192 (contents-end (and contents-begin drawer-end-line))
1193 (hidden (org-invisible-p2))
1194 (pos-before-blank (progn (goto-char drawer-end-line)
1195 (forward-line)
1196 (point)))
1197 (end (progn (skip-chars-forward " \r\t\n" limit)
1198 (skip-chars-backward " \t")
1199 (if (bolp) (point) (line-end-position)))))
1200 (list 'property-drawer
1201 (nconc
1202 (list :begin begin
1203 :end end
1204 :hiddenp hidden
1205 :contents-begin contents-begin
1206 :contents-end contents-end
1207 :post-blank (count-lines pos-before-blank end))
1208 (cdr affiliated)))))))))
1210 (defun org-element-property-drawer-interpreter (property-drawer contents)
1211 "Interpret PROPERTY-DRAWER element as Org syntax.
1212 CONTENTS is the properties within the drawer."
1213 (format ":PROPERTIES:\n%s:END:" contents))
1216 ;;;; Quote Block
1218 (defun org-element-quote-block-parser (limit affiliated)
1219 "Parse a quote block.
1221 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1222 the buffer position at the beginning of the first affiliated
1223 keyword and CDR is a plist of affiliated keywords along with
1224 their value.
1226 Return a list whose CAR is `quote-block' and CDR is a plist
1227 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1228 `:contents-end' and `:post-blank' keywords.
1230 Assume point is at the beginning of the block."
1231 (let ((case-fold-search t))
1232 (if (not (save-excursion
1233 (re-search-forward "^[ \t]*#\\+END_QUOTE[ \t]*$" limit t)))
1234 ;; Incomplete block: parse it as a paragraph.
1235 (org-element-paragraph-parser limit affiliated)
1236 (let ((block-end-line (match-beginning 0)))
1237 (save-excursion
1238 (let* ((begin (car affiliated))
1239 ;; Empty blocks have no contents.
1240 (contents-begin (progn (forward-line)
1241 (and (< (point) block-end-line)
1242 (point))))
1243 (contents-end (and contents-begin block-end-line))
1244 (hidden (org-invisible-p2))
1245 (pos-before-blank (progn (goto-char block-end-line)
1246 (forward-line)
1247 (point)))
1248 (end (progn (skip-chars-forward " \r\t\n" limit)
1249 (skip-chars-backward " \t")
1250 (if (bolp) (point) (line-end-position)))))
1251 (list 'quote-block
1252 (nconc
1253 (list :begin begin
1254 :end end
1255 :hiddenp hidden
1256 :contents-begin contents-begin
1257 :contents-end contents-end
1258 :post-blank (count-lines pos-before-blank end))
1259 (cdr affiliated)))))))))
1261 (defun org-element-quote-block-interpreter (quote-block contents)
1262 "Interpret QUOTE-BLOCK element as Org syntax.
1263 CONTENTS is the contents of the element."
1264 (format "#+BEGIN_QUOTE\n%s#+END_QUOTE" contents))
1267 ;;;; Section
1269 (defun org-element-section-parser (limit)
1270 "Parse a section.
1272 LIMIT bounds the search.
1274 Return a list whose CAR is `section' and CDR is a plist
1275 containing `:begin', `:end', `:contents-begin', `contents-end'
1276 and `:post-blank' keywords."
1277 (save-excursion
1278 ;; Beginning of section is the beginning of the first non-blank
1279 ;; line after previous headline.
1280 (let ((begin (point))
1281 (end (progn (org-with-limited-levels (outline-next-heading))
1282 (point)))
1283 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
1284 (forward-line)
1285 (point))))
1286 (list 'section
1287 (list :begin begin
1288 :end end
1289 :contents-begin begin
1290 :contents-end pos-before-blank
1291 :post-blank (count-lines pos-before-blank end))))))
1293 (defun org-element-section-interpreter (section contents)
1294 "Interpret SECTION element as Org syntax.
1295 CONTENTS is the contents of the element."
1296 contents)
1299 ;;;; Special Block
1301 (defun org-element-special-block-parser (limit affiliated)
1302 "Parse a special block.
1304 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1305 the buffer position at the beginning of the first affiliated
1306 keyword and CDR is a plist of affiliated keywords along with
1307 their value.
1309 Return a list whose CAR is `special-block' and CDR is a plist
1310 containing `:type', `:begin', `:end', `:hiddenp',
1311 `:contents-begin', `:contents-end' and `:post-blank' keywords.
1313 Assume point is at the beginning of the block."
1314 (let* ((case-fold-search t)
1315 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(S-+\\)")
1316 (upcase (match-string-no-properties 1)))))
1317 (if (not (save-excursion
1318 (re-search-forward
1319 (format "^[ \t]*#\\+END_%s[ \t]*$" type) limit t)))
1320 ;; Incomplete block: parse it as a paragraph.
1321 (org-element-paragraph-parser limit affiliated)
1322 (let ((block-end-line (match-beginning 0)))
1323 (save-excursion
1324 (let* ((begin (car affiliated))
1325 ;; Empty blocks have no contents.
1326 (contents-begin (progn (forward-line)
1327 (and (< (point) block-end-line)
1328 (point))))
1329 (contents-end (and contents-begin block-end-line))
1330 (hidden (org-invisible-p2))
1331 (pos-before-blank (progn (goto-char block-end-line)
1332 (forward-line)
1333 (point)))
1334 (end (progn (skip-chars-forward " \r\t\n" limit)
1335 (skip-chars-backward " \t")
1336 (if (bolp) (point) (line-end-position)))))
1337 (list 'special-block
1338 (nconc
1339 (list :type type
1340 :begin begin
1341 :end end
1342 :hiddenp hidden
1343 :contents-begin contents-begin
1344 :contents-end contents-end
1345 :post-blank (count-lines pos-before-blank end))
1346 (cdr affiliated)))))))))
1348 (defun org-element-special-block-interpreter (special-block contents)
1349 "Interpret SPECIAL-BLOCK element as Org syntax.
1350 CONTENTS is the contents of the element."
1351 (let ((block-type (org-element-property :type special-block)))
1352 (format "#+BEGIN_%s\n%s#+END_%s" block-type contents block-type)))
1356 ;;; Elements
1358 ;; For each element, a parser and an interpreter are also defined.
1359 ;; Both follow the same naming convention used for greater elements.
1361 ;; Also, as for greater elements, adding a new element type is done
1362 ;; through the following steps: implement a parser and an interpreter,
1363 ;; tweak `org-element--current-element' so that it recognizes the new
1364 ;; type and add that new type to `org-element-all-elements'.
1366 ;; As a special case, when the newly defined type is a block type,
1367 ;; `org-element-block-name-alist' has to be modified accordingly.
1370 ;;;; Babel Call
1372 (defun org-element-babel-call-parser (limit affiliated)
1373 "Parse a babel call.
1375 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1376 the buffer position at the beginning of the first affiliated
1377 keyword and CDR is a plist of affiliated keywords along with
1378 their value.
1380 Return a list whose CAR is `babel-call' and CDR is a plist
1381 containing `:begin', `:end', `:info' and `:post-blank' as
1382 keywords."
1383 (save-excursion
1384 (let ((case-fold-search t)
1385 (info (progn (looking-at org-babel-block-lob-one-liner-regexp)
1386 (org-babel-lob-get-info)))
1387 (begin (car affiliated))
1388 (pos-before-blank (progn (forward-line) (point)))
1389 (end (progn (skip-chars-forward " \r\t\n" limit)
1390 (skip-chars-backward " \t")
1391 (if (bolp) (point) (line-end-position)))))
1392 (list 'babel-call
1393 (nconc
1394 (list :begin begin
1395 :end end
1396 :info info
1397 :post-blank (count-lines pos-before-blank end))
1398 (cdr affiliated))))))
1400 (defun org-element-babel-call-interpreter (babel-call contents)
1401 "Interpret BABEL-CALL element as Org syntax.
1402 CONTENTS is nil."
1403 (let* ((babel-info (org-element-property :info babel-call))
1404 (main (car babel-info))
1405 (post-options (nth 1 babel-info)))
1406 (concat "#+CALL: "
1407 (if (not (string-match "\\[\\(\\[.*?\\]\\)\\]" main)) main
1408 ;; Remove redundant square brackets.
1409 (replace-match (match-string 1 main) nil nil main))
1410 (and post-options (format "[%s]" post-options)))))
1413 ;;;; Clock
1415 (defun org-element-clock-parser (limit)
1416 "Parse a clock.
1418 LIMIT bounds the search.
1420 Return a list whose CAR is `clock' and CDR is a plist containing
1421 `:status', `:value', `:time', `:begin', `:end' and `:post-blank'
1422 as keywords."
1423 (save-excursion
1424 (let* ((case-fold-search nil)
1425 (begin (point))
1426 (value (progn (search-forward org-clock-string (line-end-position) t)
1427 (org-skip-whitespace)
1428 (looking-at "\\[.*\\]")
1429 (org-match-string-no-properties 0)))
1430 (time (and (progn (goto-char (match-end 0))
1431 (looking-at " +=> +\\(\\S-+\\)[ \t]*$"))
1432 (org-match-string-no-properties 1)))
1433 (status (if time 'closed 'running))
1434 (post-blank (let ((before-blank (progn (forward-line) (point))))
1435 (skip-chars-forward " \r\t\n" limit)
1436 (skip-chars-backward " \t")
1437 (unless (bolp) (end-of-line))
1438 (count-lines before-blank (point))))
1439 (end (point)))
1440 (list 'clock
1441 (list :status status
1442 :value value
1443 :time time
1444 :begin begin
1445 :end end
1446 :post-blank post-blank)))))
1448 (defun org-element-clock-interpreter (clock contents)
1449 "Interpret CLOCK element as Org syntax.
1450 CONTENTS is nil."
1451 (concat org-clock-string " "
1452 (org-element-property :value clock)
1453 (let ((time (org-element-property :time clock)))
1454 (and time
1455 (concat " => "
1456 (apply 'format
1457 "%2s:%02s"
1458 (org-split-string time ":")))))))
1461 ;;;; Comment
1463 (defun org-element-comment-parser (limit affiliated)
1464 "Parse a comment.
1466 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1467 the buffer position at the beginning of the first affiliated
1468 keyword and CDR is a plist of affiliated keywords along with
1469 their value.
1471 Return a list whose CAR is `comment' and CDR is a plist
1472 containing `:begin', `:end', `:value' and `:post-blank'
1473 keywords.
1475 Assume point is at comment beginning."
1476 (save-excursion
1477 (let* ((begin (car affiliated))
1478 (value (prog2 (looking-at "[ \t]*# ?")
1479 (buffer-substring-no-properties
1480 (match-end 0) (line-end-position))
1481 (forward-line)))
1482 (com-end
1483 ;; Get comments ending.
1484 (progn
1485 (while (and (< (point) limit) (looking-at "[ \t]*#\\( \\|$\\)"))
1486 ;; Accumulate lines without leading hash and first
1487 ;; whitespace.
1488 (setq value
1489 (concat value
1490 "\n"
1491 (buffer-substring-no-properties
1492 (match-end 0) (line-end-position))))
1493 (forward-line))
1494 (point)))
1495 (end (progn (goto-char com-end)
1496 (skip-chars-forward " \r\t\n" limit)
1497 (skip-chars-backward " \t")
1498 (if (bolp) (point) (line-end-position)))))
1499 (list 'comment
1500 (nconc
1501 (list :begin begin
1502 :end end
1503 :value value
1504 :post-blank (count-lines com-end end))
1505 (cdr affiliated))))))
1507 (defun org-element-comment-interpreter (comment contents)
1508 "Interpret COMMENT element as Org syntax.
1509 CONTENTS is nil."
1510 (replace-regexp-in-string "^" "# " (org-element-property :value comment)))
1513 ;;;; Comment Block
1515 (defun org-element-comment-block-parser (limit affiliated)
1516 "Parse an export block.
1518 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1519 the buffer position at the beginning of the first affiliated
1520 keyword and CDR is a plist of affiliated keywords along with
1521 their value.
1523 Return a list whose CAR is `comment-block' and CDR is a plist
1524 containing `:begin', `:end', `:hiddenp', `:value' and
1525 `:post-blank' keywords.
1527 Assume point is at comment block beginning."
1528 (let ((case-fold-search t))
1529 (if (not (save-excursion
1530 (re-search-forward "^[ \t]*#\\+END_COMMENT[ \t]*$" limit t)))
1531 ;; Incomplete block: parse it as a paragraph.
1532 (org-element-paragraph-parser limit affiliated)
1533 (let ((contents-end (match-beginning 0)))
1534 (save-excursion
1535 (let* ((begin (car affiliated))
1536 (contents-begin (progn (forward-line) (point)))
1537 (hidden (org-invisible-p2))
1538 (pos-before-blank (progn (goto-char contents-end)
1539 (forward-line)
1540 (point)))
1541 (end (progn (skip-chars-forward " \r\t\n" limit)
1542 (skip-chars-backward " \t")
1543 (if (bolp) (point) (line-end-position))))
1544 (value (buffer-substring-no-properties
1545 contents-begin contents-end)))
1546 (list 'comment-block
1547 (nconc
1548 (list :begin begin
1549 :end end
1550 :value value
1551 :hiddenp hidden
1552 :post-blank (count-lines pos-before-blank end))
1553 (cdr affiliated)))))))))
1555 (defun org-element-comment-block-interpreter (comment-block contents)
1556 "Interpret COMMENT-BLOCK element as Org syntax.
1557 CONTENTS is nil."
1558 (format "#+BEGIN_COMMENT\n%s#+END_COMMENT"
1559 (org-remove-indentation (org-element-property :value comment-block))))
1562 ;;;; Example Block
1564 (defun org-element-example-block-parser (limit affiliated)
1565 "Parse an example block.
1567 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1568 the buffer position at the beginning of the first affiliated
1569 keyword and CDR is a plist of affiliated keywords along with
1570 their value.
1572 Return a list whose CAR is `example-block' and CDR is a plist
1573 containing `:begin', `:end', `:number-lines', `:preserve-indent',
1574 `:retain-labels', `:use-labels', `:label-fmt', `:hiddenp',
1575 `:switches', `:value' and `:post-blank' keywords."
1576 (let ((case-fold-search t))
1577 (if (not (save-excursion
1578 (re-search-forward "^[ \t]*#\\+END_EXAMPLE[ \t]*$" limit t)))
1579 ;; Incomplete block: parse it as a paragraph.
1580 (org-element-paragraph-parser limit affiliated)
1581 (let ((contents-end (match-beginning 0)))
1582 (save-excursion
1583 (let* ((switches
1584 (progn (looking-at "^[ \t]*#\\+BEGIN_EXAMPLE\\(?: +\\(.*\\)\\)?")
1585 (org-match-string-no-properties 1)))
1586 ;; Switches analysis
1587 (number-lines (cond ((not switches) nil)
1588 ((string-match "-n\\>" switches) 'new)
1589 ((string-match "+n\\>" switches) 'continued)))
1590 (preserve-indent (and switches (string-match "-i\\>" switches)))
1591 ;; Should labels be retained in (or stripped from) example
1592 ;; blocks?
1593 (retain-labels
1594 (or (not switches)
1595 (not (string-match "-r\\>" switches))
1596 (and number-lines (string-match "-k\\>" switches))))
1597 ;; What should code-references use - labels or
1598 ;; line-numbers?
1599 (use-labels
1600 (or (not switches)
1601 (and retain-labels (not (string-match "-k\\>" switches)))))
1602 (label-fmt (and switches
1603 (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
1604 (match-string 1 switches)))
1605 ;; Standard block parsing.
1606 (begin (car affiliated))
1607 (contents-begin (progn (forward-line) (point)))
1608 (hidden (org-invisible-p2))
1609 (value (org-unescape-code-in-string
1610 (buffer-substring-no-properties
1611 contents-begin contents-end)))
1612 (pos-before-blank (progn (goto-char contents-end)
1613 (forward-line)
1614 (point)))
1615 (end (progn (skip-chars-forward " \r\t\n" limit)
1616 (skip-chars-backward " \t")
1617 (if (bolp) (point) (line-end-position)))))
1618 (list 'example-block
1619 (nconc
1620 (list :begin begin
1621 :end end
1622 :value value
1623 :switches switches
1624 :number-lines number-lines
1625 :preserve-indent preserve-indent
1626 :retain-labels retain-labels
1627 :use-labels use-labels
1628 :label-fmt label-fmt
1629 :hiddenp hidden
1630 :post-blank (count-lines pos-before-blank end))
1631 (cdr affiliated)))))))))
1633 (defun org-element-example-block-interpreter (example-block contents)
1634 "Interpret EXAMPLE-BLOCK element as Org syntax.
1635 CONTENTS is nil."
1636 (let ((switches (org-element-property :switches example-block)))
1637 (concat "#+BEGIN_EXAMPLE" (and switches (concat " " switches)) "\n"
1638 (org-remove-indentation
1639 (org-escape-code-in-string
1640 (org-element-property :value example-block)))
1641 "#+END_EXAMPLE")))
1644 ;;;; Export Block
1646 (defun org-element-export-block-parser (limit affiliated)
1647 "Parse an export block.
1649 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1650 the buffer position at the beginning of the first affiliated
1651 keyword and CDR is a plist of affiliated keywords along with
1652 their value.
1654 Return a list whose CAR is `export-block' and CDR is a plist
1655 containing `:begin', `:end', `:type', `:hiddenp', `:value' and
1656 `:post-blank' keywords.
1658 Assume point is at export-block beginning."
1659 (let* ((case-fold-search t)
1660 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
1661 (upcase (org-match-string-no-properties 1)))))
1662 (if (not (save-excursion
1663 (re-search-forward
1664 (format "^[ \t]*#\\+END_%s[ \t]*$" type) limit t)))
1665 ;; Incomplete block: parse it as a paragraph.
1666 (org-element-paragraph-parser limit affiliated)
1667 (let ((contents-end (match-beginning 0)))
1668 (save-excursion
1669 (let* ((begin (car affiliated))
1670 (contents-begin (progn (forward-line) (point)))
1671 (hidden (org-invisible-p2))
1672 (pos-before-blank (progn (goto-char contents-end)
1673 (forward-line)
1674 (point)))
1675 (end (progn (skip-chars-forward " \r\t\n" limit)
1676 (skip-chars-backward " \t")
1677 (if (bolp) (point) (line-end-position))))
1678 (value (buffer-substring-no-properties contents-begin
1679 contents-end)))
1680 (list 'export-block
1681 (nconc
1682 (list :begin begin
1683 :end end
1684 :type type
1685 :value value
1686 :hiddenp hidden
1687 :post-blank (count-lines pos-before-blank end))
1688 (cdr affiliated)))))))))
1690 (defun org-element-export-block-interpreter (export-block contents)
1691 "Interpret EXPORT-BLOCK element as Org syntax.
1692 CONTENTS is nil."
1693 (let ((type (org-element-property :type export-block)))
1694 (concat (format "#+BEGIN_%s\n" type)
1695 (org-element-property :value export-block)
1696 (format "#+END_%s" type))))
1699 ;;;; Fixed-width
1701 (defun org-element-fixed-width-parser (limit affiliated)
1702 "Parse a fixed-width section.
1704 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1705 the buffer position at the beginning of the first affiliated
1706 keyword and CDR is a plist of affiliated keywords along with
1707 their value.
1709 Return a list whose CAR is `fixed-width' and CDR is a plist
1710 containing `:begin', `:end', `:value' and `:post-blank' keywords.
1712 Assume point is at the beginning of the fixed-width area."
1713 (save-excursion
1714 (let* ((begin (car affiliated))
1715 value
1716 (end-area
1717 (progn
1718 (while (and (< (point) limit)
1719 (looking-at "[ \t]*:\\( \\|$\\)"))
1720 ;; Accumulate text without starting colons.
1721 (setq value
1722 (concat value
1723 (buffer-substring-no-properties
1724 (match-end 0) (point-at-eol))
1725 "\n"))
1726 (forward-line))
1727 (point)))
1728 (end (progn (skip-chars-forward " \r\t\n" limit)
1729 (skip-chars-backward " \t")
1730 (if (bolp) (point) (line-end-position)))))
1731 (list 'fixed-width
1732 (nconc
1733 (list :begin begin
1734 :end end
1735 :value value
1736 :post-blank (count-lines end-area end))
1737 (cdr affiliated))))))
1739 (defun org-element-fixed-width-interpreter (fixed-width contents)
1740 "Interpret FIXED-WIDTH element as Org syntax.
1741 CONTENTS is nil."
1742 (replace-regexp-in-string
1743 "^" ": " (substring (org-element-property :value fixed-width) 0 -1)))
1746 ;;;; Horizontal Rule
1748 (defun org-element-horizontal-rule-parser (limit affiliated)
1749 "Parse an horizontal rule.
1751 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1752 the buffer position at the beginning of the first affiliated
1753 keyword and CDR is a plist of affiliated keywords along with
1754 their value.
1756 Return a list whose CAR is `horizontal-rule' and CDR is a plist
1757 containing `:begin', `:end' and `:post-blank' keywords."
1758 (save-excursion
1759 (let ((begin (car affiliated))
1760 (post-hr (progn (forward-line) (point)))
1761 (end (progn (skip-chars-forward " \r\t\n" limit)
1762 (skip-chars-backward " \t")
1763 (if (bolp) (point) (line-end-position)))))
1764 (list 'horizontal-rule
1765 (nconc
1766 (list :begin begin
1767 :end end
1768 :post-blank (count-lines post-hr end))
1769 (cdr affiliated))))))
1771 (defun org-element-horizontal-rule-interpreter (horizontal-rule contents)
1772 "Interpret HORIZONTAL-RULE element as Org syntax.
1773 CONTENTS is nil."
1774 "-----")
1777 ;;;; Keyword
1779 (defun org-element-keyword-parser (limit affiliated)
1780 "Parse a keyword at point.
1782 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1783 the buffer position at the beginning of the first affiliated
1784 keyword and CDR is a plist of affiliated keywords along with
1785 their value.
1787 Return a list whose CAR is `keyword' and CDR is a plist
1788 containing `:key', `:value', `:begin', `:end' and `:post-blank'
1789 keywords."
1790 (save-excursion
1791 (let ((case-fold-search t)
1792 (begin (car affiliated))
1793 (key (progn (looking-at "[ \t]*#\\+\\(\\S-+*\\):")
1794 (upcase (org-match-string-no-properties 1))))
1795 (value (org-trim (buffer-substring-no-properties
1796 (match-end 0) (point-at-eol))))
1797 (pos-before-blank (progn (forward-line) (point)))
1798 (end (progn (skip-chars-forward " \r\t\n" limit)
1799 (skip-chars-backward " \t")
1800 (if (bolp) (point) (line-end-position)))))
1801 (list 'keyword
1802 (nconc
1803 (list :key key
1804 :value value
1805 :begin begin
1806 :end end
1807 :post-blank (count-lines pos-before-blank end))
1808 (cdr affiliated))))))
1810 (defun org-element-keyword-interpreter (keyword contents)
1811 "Interpret KEYWORD element as Org syntax.
1812 CONTENTS is nil."
1813 (format "#+%s: %s"
1814 (org-element-property :key keyword)
1815 (org-element-property :value keyword)))
1818 ;;;; Latex Environment
1820 (defun org-element-latex-environment-parser (limit affiliated)
1821 "Parse a LaTeX environment.
1823 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1824 the buffer position at the beginning of the first affiliated
1825 keyword and CDR is a plist of affiliated keywords along with
1826 their value.
1828 Return a list whose CAR is `latex-environment' and CDR is a plist
1829 containing `:begin', `:end', `:value' and `:post-blank'
1830 keywords.
1832 Assume point is at the beginning of the latex environment."
1833 (save-excursion
1834 (let* ((case-fold-search t)
1835 (code-begin (point))
1836 (begin (car affiliated))
1837 (env (progn (looking-at "^[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}")
1838 (regexp-quote (match-string 1))))
1839 (code-end
1840 (progn (re-search-forward
1841 (format "^[ \t]*\\\\end{%s}[ \t]*$" env) limit t)
1842 (forward-line)
1843 (point)))
1844 (value (buffer-substring-no-properties code-begin code-end))
1845 (end (progn (skip-chars-forward " \r\t\n" limit)
1846 (skip-chars-backward " \t")
1847 (if (bolp) (point) (line-end-position)))))
1848 (list 'latex-environment
1849 (nconc
1850 (list :begin begin
1851 :end end
1852 :value value
1853 :post-blank (count-lines code-end end))
1854 (cdr affiliated))))))
1856 (defun org-element-latex-environment-interpreter (latex-environment contents)
1857 "Interpret LATEX-ENVIRONMENT element as Org syntax.
1858 CONTENTS is nil."
1859 (org-element-property :value latex-environment))
1862 ;;;; Node Property
1864 (defun org-element-node-property-parser (limit)
1865 "Parse a node-property at point.
1867 LIMIT bounds the search.
1869 Return a list whose CAR is `node-property' and CDR is a plist
1870 containing `:key', `:value', `:begin', `:end' and `:post-blank'
1871 keywords."
1872 (save-excursion
1873 (let ((case-fold-search t)
1874 (begin (point))
1875 (key (progn (looking-at "[ \t]*:\\(.*?\\):[ \t]+\\(.*?\\)[ \t]*$")
1876 (org-match-string-no-properties 1)))
1877 (value (org-match-string-no-properties 2))
1878 (pos-before-blank (progn (forward-line) (point)))
1879 (end (progn (skip-chars-forward " \r\t\n" limit)
1880 (if (eobp) (point) (point-at-bol)))))
1881 (list 'node-property
1882 (list :key key
1883 :value value
1884 :begin begin
1885 :end end
1886 :post-blank (count-lines pos-before-blank end))))))
1888 (defun org-element-node-property-interpreter (node-property contents)
1889 "Interpret NODE-PROPERTY element as Org syntax.
1890 CONTENTS is nil."
1891 (format org-property-format
1892 (format ":%s:" (org-element-property :key node-property))
1893 (org-element-property :value node-property)))
1896 ;;;; Paragraph
1898 (defun org-element-paragraph-parser (limit affiliated)
1899 "Parse a paragraph.
1901 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1902 the buffer position at the beginning of the first affiliated
1903 keyword and CDR is a plist of affiliated keywords along with
1904 their value.
1906 Return a list whose CAR is `paragraph' and CDR is a plist
1907 containing `:begin', `:end', `:contents-begin' and
1908 `:contents-end' and `:post-blank' keywords.
1910 Assume point is at the beginning of the paragraph."
1911 (save-excursion
1912 (let* ((begin (car affiliated))
1913 (contents-begin (point))
1914 (before-blank
1915 (let ((case-fold-search t))
1916 (end-of-line)
1917 (if (not (re-search-forward
1918 org-element-paragraph-separate limit 'm))
1919 limit
1920 ;; A matching `org-element-paragraph-separate' is not
1921 ;; necessarily the end of the paragraph. In
1922 ;; particular, lines starting with # or : as a first
1923 ;; non-space character are ambiguous. We have check
1924 ;; if they are valid Org syntax (i.e. not an
1925 ;; incomplete keyword).
1926 (beginning-of-line)
1927 (while (not
1929 ;; There's no ambiguity for other symbols or
1930 ;; empty lines: stop here.
1931 (looking-at "[ \t]*\\(?:[^:#]\\|$\\)")
1932 ;; Stop at valid fixed-width areas.
1933 (looking-at "[ \t]*:\\(?: \\|$\\)")
1934 ;; Stop at drawers.
1935 (and (looking-at org-drawer-regexp)
1936 (save-excursion
1937 (re-search-forward
1938 "^[ \t]*:END:[ \t]*$" limit t)))
1939 ;; Stop at valid comments.
1940 (looking-at "[ \t]*#\\(?: \\|$\\)")
1941 ;; Stop at valid dynamic blocks.
1942 (and (looking-at org-dblock-start-re)
1943 (save-excursion
1944 (re-search-forward
1945 "^[ \t]*#\\+END:?[ \t]*$" limit t)))
1946 ;; Stop at valid blocks.
1947 (and (looking-at
1948 "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
1949 (save-excursion
1950 (re-search-forward
1951 (format "^[ \t]*#\\+END_%s[ \t]*$"
1952 (match-string 1))
1953 limit t)))
1954 ;; Stop at valid latex environments.
1955 (and (looking-at
1956 "^[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}[ \t]*$")
1957 (save-excursion
1958 (re-search-forward
1959 (format "^[ \t]*\\\\end{%s}[ \t]*$"
1960 (match-string 1))
1961 limit t)))
1962 ;; Stop at valid keywords.
1963 (looking-at "[ \t]*#\\+\\S-+:")
1964 ;; Skip everything else.
1965 (not
1966 (progn
1967 (end-of-line)
1968 (re-search-forward org-element-paragraph-separate
1969 limit 'm)))))
1970 (beginning-of-line)))
1971 (if (= (point) limit) limit
1972 (goto-char (line-beginning-position)))))
1973 (contents-end (progn (skip-chars-backward " \r\t\n" contents-begin)
1974 (forward-line)
1975 (point)))
1976 (end (progn (skip-chars-forward " \r\t\n" limit)
1977 (skip-chars-backward " \t")
1978 (if (bolp) (point) (line-end-position)))))
1979 (list 'paragraph
1980 (nconc
1981 (list :begin begin
1982 :end end
1983 :contents-begin contents-begin
1984 :contents-end contents-end
1985 :post-blank (count-lines before-blank end))
1986 (cdr affiliated))))))
1988 (defun org-element-paragraph-interpreter (paragraph contents)
1989 "Interpret PARAGRAPH element as Org syntax.
1990 CONTENTS is the contents of the element."
1991 contents)
1994 ;;;; Planning
1996 (defun org-element-planning-parser (limit)
1997 "Parse a planning.
1999 LIMIT bounds the search.
2001 Return a list whose CAR is `planning' and CDR is a plist
2002 containing `:closed', `:deadline', `:scheduled', `:begin', `:end'
2003 and `:post-blank' keywords."
2004 (save-excursion
2005 (let* ((case-fold-search nil)
2006 (begin (point))
2007 (post-blank (let ((before-blank (progn (forward-line) (point))))
2008 (skip-chars-forward " \r\t\n" limit)
2009 (skip-chars-backward " \t")
2010 (unless (bolp) (end-of-line))
2011 (count-lines before-blank (point))))
2012 (end (point))
2013 closed deadline scheduled)
2014 (goto-char begin)
2015 (while (re-search-forward org-keyword-time-not-clock-regexp
2016 (line-end-position) t)
2017 (goto-char (match-end 1))
2018 (org-skip-whitespace)
2019 (let ((time (buffer-substring-no-properties
2020 (1+ (point)) (1- (match-end 0))))
2021 (keyword (match-string 1)))
2022 (cond ((equal keyword org-closed-string) (setq closed time))
2023 ((equal keyword org-deadline-string) (setq deadline time))
2024 (t (setq scheduled time)))))
2025 (list 'planning
2026 (list :closed closed
2027 :deadline deadline
2028 :scheduled scheduled
2029 :begin begin
2030 :end end
2031 :post-blank post-blank)))))
2033 (defun org-element-planning-interpreter (planning contents)
2034 "Interpret PLANNING element as Org syntax.
2035 CONTENTS is nil."
2036 (mapconcat
2037 'identity
2038 (delq nil
2039 (list (let ((closed (org-element-property :closed planning)))
2040 (when closed (concat org-closed-string " [" closed "]")))
2041 (let ((deadline (org-element-property :deadline planning)))
2042 (when deadline (concat org-deadline-string " <" deadline ">")))
2043 (let ((scheduled (org-element-property :scheduled planning)))
2044 (when scheduled
2045 (concat org-scheduled-string " <" scheduled ">")))))
2046 " "))
2049 ;;;; Quote Section
2051 (defun org-element-quote-section-parser (limit)
2052 "Parse a quote section.
2054 LIMIT bounds the search.
2056 Return a list whose CAR is `quote-section' and CDR is a plist
2057 containing `:begin', `:end', `:value' and `:post-blank' keywords.
2059 Assume point is at beginning of the section."
2060 (save-excursion
2061 (let* ((begin (point))
2062 (end (progn (org-with-limited-levels (outline-next-heading))
2063 (point)))
2064 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
2065 (forward-line)
2066 (point)))
2067 (value (buffer-substring-no-properties begin pos-before-blank)))
2068 (list 'quote-section
2069 (list :begin begin
2070 :end end
2071 :value value
2072 :post-blank (count-lines pos-before-blank end))))))
2074 (defun org-element-quote-section-interpreter (quote-section contents)
2075 "Interpret QUOTE-SECTION element as Org syntax.
2076 CONTENTS is nil."
2077 (org-element-property :value quote-section))
2080 ;;;; Src Block
2082 (defun org-element-src-block-parser (limit affiliated)
2083 "Parse a src block.
2085 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2086 the buffer position at the beginning of the first affiliated
2087 keyword and CDR is a plist of affiliated keywords along with
2088 their value.
2090 Return a list whose CAR is `src-block' and CDR is a plist
2091 containing `:language', `:switches', `:parameters', `:begin',
2092 `:end', `:hiddenp', `:number-lines', `:retain-labels',
2093 `:use-labels', `:label-fmt', `:preserve-indent', `:value' and
2094 `:post-blank' keywords.
2096 Assume point is at the beginning of the block."
2097 (let ((case-fold-search t))
2098 (if (not (save-excursion (re-search-forward "^[ \t]*#\\+END_SRC[ \t]*$"
2099 limit t)))
2100 ;; Incomplete block: parse it as a paragraph.
2101 (org-element-paragraph-parser limit affiliated)
2102 (let ((contents-end (match-beginning 0)))
2103 (save-excursion
2104 (let* ((begin (car affiliated))
2105 ;; Get language as a string.
2106 (language
2107 (progn
2108 (looking-at
2109 (concat "^[ \t]*#\\+BEGIN_SRC"
2110 "\\(?: +\\(\\S-+\\)\\)?"
2111 "\\(\\(?: +\\(?:-l \".*?\"\\|[-+][A-Za-z]\\)\\)+\\)?"
2112 "\\(.*\\)[ \t]*$"))
2113 (org-match-string-no-properties 1)))
2114 ;; Get switches.
2115 (switches (org-match-string-no-properties 2))
2116 ;; Get parameters.
2117 (parameters (org-match-string-no-properties 3))
2118 ;; Switches analysis
2119 (number-lines (cond ((not switches) nil)
2120 ((string-match "-n\\>" switches) 'new)
2121 ((string-match "+n\\>" switches) 'continued)))
2122 (preserve-indent (and switches (string-match "-i\\>" switches)))
2123 (label-fmt (and switches
2124 (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
2125 (match-string 1 switches)))
2126 ;; Should labels be retained in (or stripped from)
2127 ;; src blocks?
2128 (retain-labels
2129 (or (not switches)
2130 (not (string-match "-r\\>" switches))
2131 (and number-lines (string-match "-k\\>" switches))))
2132 ;; What should code-references use - labels or
2133 ;; line-numbers?
2134 (use-labels
2135 (or (not switches)
2136 (and retain-labels (not (string-match "-k\\>" switches)))))
2137 ;; Get visibility status.
2138 (hidden (progn (forward-line) (org-invisible-p2)))
2139 ;; Retrieve code.
2140 (value (org-unescape-code-in-string
2141 (buffer-substring-no-properties (point) contents-end)))
2142 (pos-before-blank (progn (goto-char contents-end)
2143 (forward-line)
2144 (point)))
2145 ;; Get position after ending blank lines.
2146 (end (progn (skip-chars-forward " \r\t\n" limit)
2147 (skip-chars-backward " \t")
2148 (if (bolp) (point) (line-end-position)))))
2149 (list 'src-block
2150 (nconc
2151 (list :language language
2152 :switches (and (org-string-nw-p switches)
2153 (org-trim switches))
2154 :parameters (and (org-string-nw-p parameters)
2155 (org-trim parameters))
2156 :begin begin
2157 :end end
2158 :number-lines number-lines
2159 :preserve-indent preserve-indent
2160 :retain-labels retain-labels
2161 :use-labels use-labels
2162 :label-fmt label-fmt
2163 :hiddenp hidden
2164 :value value
2165 :post-blank (count-lines pos-before-blank end))
2166 (cdr affiliated)))))))))
2168 (defun org-element-src-block-interpreter (src-block contents)
2169 "Interpret SRC-BLOCK element as Org syntax.
2170 CONTENTS is nil."
2171 (let ((lang (org-element-property :language src-block))
2172 (switches (org-element-property :switches src-block))
2173 (params (org-element-property :parameters src-block))
2174 (value (let ((val (org-element-property :value src-block)))
2175 (cond
2176 (org-src-preserve-indentation val)
2177 ((zerop org-edit-src-content-indentation)
2178 (org-remove-indentation val))
2180 (let ((ind (make-string
2181 org-edit-src-content-indentation 32)))
2182 (replace-regexp-in-string
2183 "\\(^\\)[ \t]*\\S-" ind
2184 (org-remove-indentation val) nil nil 1)))))))
2185 (concat (format "#+BEGIN_SRC%s\n"
2186 (concat (and lang (concat " " lang))
2187 (and switches (concat " " switches))
2188 (and params (concat " " params))))
2189 (org-escape-code-in-string value)
2190 "#+END_SRC")))
2193 ;;;; Table
2195 (defun org-element-table-parser (limit affiliated)
2196 "Parse a table at point.
2198 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2199 the buffer position at the beginning of the first affiliated
2200 keyword and CDR is a plist of affiliated keywords along with
2201 their value.
2203 Return a list whose CAR is `table' and CDR is a plist containing
2204 `:begin', `:end', `:tblfm', `:type', `:contents-begin',
2205 `:contents-end', `:value' and `:post-blank' keywords.
2207 Assume point is at the beginning of the table."
2208 (save-excursion
2209 (let* ((case-fold-search t)
2210 (table-begin (point))
2211 (type (if (org-at-table.el-p) 'table.el 'org))
2212 (begin (car affiliated))
2213 (table-end
2214 (if (re-search-forward org-table-any-border-regexp limit 'm)
2215 (goto-char (match-beginning 0))
2216 (point)))
2217 (tblfm (let (acc)
2218 (while (looking-at "[ \t]*#\\+TBLFM: +\\(.*\\)[ \t]*$")
2219 (push (org-match-string-no-properties 1) acc)
2220 (forward-line))
2221 acc))
2222 (pos-before-blank (point))
2223 (end (progn (skip-chars-forward " \r\t\n" limit)
2224 (skip-chars-backward " \t")
2225 (if (bolp) (point) (line-end-position)))))
2226 (list 'table
2227 (nconc
2228 (list :begin begin
2229 :end end
2230 :type type
2231 :tblfm tblfm
2232 ;; Only `org' tables have contents. `table.el' tables
2233 ;; use a `:value' property to store raw table as
2234 ;; a string.
2235 :contents-begin (and (eq type 'org) table-begin)
2236 :contents-end (and (eq type 'org) table-end)
2237 :value (and (eq type 'table.el)
2238 (buffer-substring-no-properties
2239 table-begin table-end))
2240 :post-blank (count-lines pos-before-blank end))
2241 (cdr affiliated))))))
2243 (defun org-element-table-interpreter (table contents)
2244 "Interpret TABLE element as Org syntax.
2245 CONTENTS is nil."
2246 (if (eq (org-element-property :type table) 'table.el)
2247 (org-remove-indentation (org-element-property :value table))
2248 (concat (with-temp-buffer (insert contents)
2249 (org-table-align)
2250 (buffer-string))
2251 (mapconcat (lambda (fm) (concat "#+TBLFM: " fm))
2252 (reverse (org-element-property :tblfm table))
2253 "\n"))))
2256 ;;;; Table Row
2258 (defun org-element-table-row-parser (limit)
2259 "Parse table row at point.
2261 LIMIT bounds the search.
2263 Return a list whose CAR is `table-row' and CDR is a plist
2264 containing `:begin', `:end', `:contents-begin', `:contents-end',
2265 `:type' and `:post-blank' keywords."
2266 (save-excursion
2267 (let* ((type (if (looking-at "^[ \t]*|-") 'rule 'standard))
2268 (begin (point))
2269 ;; A table rule has no contents. In that case, ensure
2270 ;; CONTENTS-BEGIN matches CONTENTS-END.
2271 (contents-begin (and (eq type 'standard)
2272 (search-forward "|")
2273 (point)))
2274 (contents-end (and (eq type 'standard)
2275 (progn
2276 (end-of-line)
2277 (skip-chars-backward " \t")
2278 (point))))
2279 (end (progn (forward-line) (point))))
2280 (list 'table-row
2281 (list :type type
2282 :begin begin
2283 :end end
2284 :contents-begin contents-begin
2285 :contents-end contents-end
2286 :post-blank 0)))))
2288 (defun org-element-table-row-interpreter (table-row contents)
2289 "Interpret TABLE-ROW element as Org syntax.
2290 CONTENTS is the contents of the table row."
2291 (if (eq (org-element-property :type table-row) 'rule) "|-"
2292 (concat "| " contents)))
2295 ;;;; Verse Block
2297 (defun org-element-verse-block-parser (limit affiliated)
2298 "Parse a verse block.
2300 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2301 the buffer position at the beginning of the first affiliated
2302 keyword and CDR is a plist of affiliated keywords along with
2303 their value.
2305 Return a list whose CAR is `verse-block' and CDR is a plist
2306 containing `:begin', `:end', `:contents-begin', `:contents-end',
2307 `:hiddenp' and `:post-blank' keywords.
2309 Assume point is at beginning of the block."
2310 (let ((case-fold-search t))
2311 (if (not (save-excursion
2312 (re-search-forward "^[ \t]*#\\+END_VERSE[ \t]*$" limit t)))
2313 ;; Incomplete block: parse it as a paragraph.
2314 (org-element-paragraph-parser limit affiliated)
2315 (let ((contents-end (match-beginning 0)))
2316 (save-excursion
2317 (let* ((begin (car affiliated))
2318 (hidden (progn (forward-line) (org-invisible-p2)))
2319 (contents-begin (point))
2320 (pos-before-blank (progn (goto-char contents-end)
2321 (forward-line)
2322 (point)))
2323 (end (progn (skip-chars-forward " \r\t\n" limit)
2324 (skip-chars-backward " \t")
2325 (if (bolp) (point) (line-end-position)))))
2326 (list 'verse-block
2327 (nconc
2328 (list :begin begin
2329 :end end
2330 :contents-begin contents-begin
2331 :contents-end contents-end
2332 :hiddenp hidden
2333 :post-blank (count-lines pos-before-blank end))
2334 (cdr affiliated)))))))))
2336 (defun org-element-verse-block-interpreter (verse-block contents)
2337 "Interpret VERSE-BLOCK element as Org syntax.
2338 CONTENTS is verse block contents."
2339 (format "#+BEGIN_VERSE\n%s#+END_VERSE" contents))
2343 ;;; Objects
2345 ;; Unlike to elements, interstices can be found between objects.
2346 ;; That's why, along with the parser, successor functions are provided
2347 ;; for each object. Some objects share the same successor (i.e. `code'
2348 ;; and `verbatim' objects).
2350 ;; A successor must accept a single argument bounding the search. It
2351 ;; will return either a cons cell whose CAR is the object's type, as
2352 ;; a symbol, and CDR the position of its next occurrence, or nil.
2354 ;; Successors follow the naming convention:
2355 ;; org-element-NAME-successor, where NAME is the name of the
2356 ;; successor, as defined in `org-element-all-successors'.
2358 ;; Some object types (i.e. `italic') are recursive. Restrictions on
2359 ;; object types they can contain will be specified in
2360 ;; `org-element-object-restrictions'.
2362 ;; Adding a new type of object is simple. Implement a successor,
2363 ;; a parser, and an interpreter for it, all following the naming
2364 ;; convention. Register type in `org-element-all-objects' and
2365 ;; successor in `org-element-all-successors'. Maybe tweak
2366 ;; restrictions about it, and that's it.
2369 ;;;; Bold
2371 (defun org-element-bold-parser ()
2372 "Parse bold object at point.
2374 Return a list whose CAR is `bold' and CDR is a plist with
2375 `:begin', `:end', `:contents-begin' and `:contents-end' and
2376 `:post-blank' keywords.
2378 Assume point is at the first star marker."
2379 (save-excursion
2380 (unless (bolp) (backward-char 1))
2381 (looking-at org-emph-re)
2382 (let ((begin (match-beginning 2))
2383 (contents-begin (match-beginning 4))
2384 (contents-end (match-end 4))
2385 (post-blank (progn (goto-char (match-end 2))
2386 (skip-chars-forward " \t")))
2387 (end (point)))
2388 (list 'bold
2389 (list :begin begin
2390 :end end
2391 :contents-begin contents-begin
2392 :contents-end contents-end
2393 :post-blank post-blank)))))
2395 (defun org-element-bold-interpreter (bold contents)
2396 "Interpret BOLD object as Org syntax.
2397 CONTENTS is the contents of the object."
2398 (format "*%s*" contents))
2400 (defun org-element-text-markup-successor (limit)
2401 "Search for the next text-markup object.
2403 LIMIT bounds the search.
2405 Return value is a cons cell whose CAR is a symbol among `bold',
2406 `italic', `underline', `strike-through', `code' and `verbatim'
2407 and CDR is beginning position."
2408 (save-excursion
2409 (unless (bolp) (backward-char))
2410 (when (re-search-forward org-emph-re limit t)
2411 (let ((marker (match-string 3)))
2412 (cons (cond
2413 ((equal marker "*") 'bold)
2414 ((equal marker "/") 'italic)
2415 ((equal marker "_") 'underline)
2416 ((equal marker "+") 'strike-through)
2417 ((equal marker "~") 'code)
2418 ((equal marker "=") 'verbatim)
2419 (t (error "Unknown marker at %d" (match-beginning 3))))
2420 (match-beginning 2))))))
2423 ;;;; Code
2425 (defun org-element-code-parser ()
2426 "Parse code object at point.
2428 Return a list whose CAR is `code' and CDR is a plist with
2429 `:value', `:begin', `:end' and `:post-blank' keywords.
2431 Assume point is at the first tilde marker."
2432 (save-excursion
2433 (unless (bolp) (backward-char 1))
2434 (looking-at org-emph-re)
2435 (let ((begin (match-beginning 2))
2436 (value (org-match-string-no-properties 4))
2437 (post-blank (progn (goto-char (match-end 2))
2438 (skip-chars-forward " \t")))
2439 (end (point)))
2440 (list 'code
2441 (list :value value
2442 :begin begin
2443 :end end
2444 :post-blank post-blank)))))
2446 (defun org-element-code-interpreter (code contents)
2447 "Interpret CODE object as Org syntax.
2448 CONTENTS is nil."
2449 (format "~%s~" (org-element-property :value code)))
2452 ;;;; Entity
2454 (defun org-element-entity-parser ()
2455 "Parse entity at point.
2457 Return a list whose CAR is `entity' and CDR a plist with
2458 `:begin', `:end', `:latex', `:latex-math-p', `:html', `:latin1',
2459 `:utf-8', `:ascii', `:use-brackets-p' and `:post-blank' as
2460 keywords.
2462 Assume point is at the beginning of the entity."
2463 (save-excursion
2464 (looking-at "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)")
2465 (let* ((value (org-entity-get (match-string 1)))
2466 (begin (match-beginning 0))
2467 (bracketsp (string= (match-string 2) "{}"))
2468 (post-blank (progn (goto-char (match-end 1))
2469 (when bracketsp (forward-char 2))
2470 (skip-chars-forward " \t")))
2471 (end (point)))
2472 (list 'entity
2473 (list :name (car value)
2474 :latex (nth 1 value)
2475 :latex-math-p (nth 2 value)
2476 :html (nth 3 value)
2477 :ascii (nth 4 value)
2478 :latin1 (nth 5 value)
2479 :utf-8 (nth 6 value)
2480 :begin begin
2481 :end end
2482 :use-brackets-p bracketsp
2483 :post-blank post-blank)))))
2485 (defun org-element-entity-interpreter (entity contents)
2486 "Interpret ENTITY object as Org syntax.
2487 CONTENTS is nil."
2488 (concat "\\"
2489 (org-element-property :name entity)
2490 (when (org-element-property :use-brackets-p entity) "{}")))
2492 (defun org-element-latex-or-entity-successor (limit)
2493 "Search for the next latex-fragment or entity object.
2495 LIMIT bounds the search.
2497 Return value is a cons cell whose CAR is `entity' or
2498 `latex-fragment' and CDR is beginning position."
2499 (save-excursion
2500 (unless (bolp) (backward-char))
2501 (let ((matchers
2502 (remove "begin" (plist-get org-format-latex-options :matchers)))
2503 ;; ENTITY-RE matches both LaTeX commands and Org entities.
2504 (entity-re
2505 "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))
2506 (when (re-search-forward
2507 (concat (mapconcat (lambda (e) (nth 1 (assoc e org-latex-regexps)))
2508 matchers "\\|")
2509 "\\|" entity-re)
2510 limit t)
2511 (goto-char (match-beginning 0))
2512 (if (looking-at entity-re)
2513 ;; Determine if it's a real entity or a LaTeX command.
2514 (cons (if (org-entity-get (match-string 1)) 'entity 'latex-fragment)
2515 (match-beginning 0))
2516 ;; No entity nor command: point is at a LaTeX fragment.
2517 ;; Determine its type to get the correct beginning position.
2518 (cons 'latex-fragment
2519 (catch 'return
2520 (mapc (lambda (e)
2521 (when (looking-at (nth 1 (assoc e org-latex-regexps)))
2522 (throw 'return
2523 (match-beginning
2524 (nth 2 (assoc e org-latex-regexps))))))
2525 matchers)
2526 (point))))))))
2529 ;;;; Export Snippet
2531 (defun org-element-export-snippet-parser ()
2532 "Parse export snippet at point.
2534 Return a list whose CAR is `export-snippet' and CDR a plist with
2535 `:begin', `:end', `:back-end', `:value' and `:post-blank' as
2536 keywords.
2538 Assume point is at the beginning of the snippet."
2539 (save-excursion
2540 (re-search-forward "@@\\([-A-Za-z0-9]+\\):" nil t)
2541 (let* ((begin (match-beginning 0))
2542 (back-end (org-match-string-no-properties 1))
2543 (value (buffer-substring-no-properties
2544 (point)
2545 (progn (re-search-forward "@@" nil t) (match-beginning 0))))
2546 (post-blank (skip-chars-forward " \t"))
2547 (end (point)))
2548 (list 'export-snippet
2549 (list :back-end back-end
2550 :value value
2551 :begin begin
2552 :end end
2553 :post-blank post-blank)))))
2555 (defun org-element-export-snippet-interpreter (export-snippet contents)
2556 "Interpret EXPORT-SNIPPET object as Org syntax.
2557 CONTENTS is nil."
2558 (format "@@%s:%s@@"
2559 (org-element-property :back-end export-snippet)
2560 (org-element-property :value export-snippet)))
2562 (defun org-element-export-snippet-successor (limit)
2563 "Search for the next export-snippet object.
2565 LIMIT bounds the search.
2567 Return value is a cons cell whose CAR is `export-snippet' and CDR
2568 its beginning position."
2569 (save-excursion
2570 (let (beg)
2571 (when (and (re-search-forward "@@[-A-Za-z0-9]+:" limit t)
2572 (setq beg (match-beginning 0))
2573 (search-forward "@@" limit t))
2574 (cons 'export-snippet beg)))))
2577 ;;;; Footnote Reference
2579 (defun org-element-footnote-reference-parser ()
2580 "Parse footnote reference at point.
2582 Return a list whose CAR is `footnote-reference' and CDR a plist
2583 with `:label', `:type', `:inline-definition', `:begin', `:end'
2584 and `:post-blank' as keywords."
2585 (save-excursion
2586 (looking-at org-footnote-re)
2587 (let* ((begin (point))
2588 (label (or (org-match-string-no-properties 2)
2589 (org-match-string-no-properties 3)
2590 (and (match-string 1)
2591 (concat "fn:" (org-match-string-no-properties 1)))))
2592 (type (if (or (not label) (match-string 1)) 'inline 'standard))
2593 (inner-begin (match-end 0))
2594 (inner-end
2595 (let ((count 1))
2596 (forward-char)
2597 (while (and (> count 0) (re-search-forward "[][]" nil t))
2598 (if (equal (match-string 0) "[") (incf count) (decf count)))
2599 (1- (point))))
2600 (post-blank (progn (goto-char (1+ inner-end))
2601 (skip-chars-forward " \t")))
2602 (end (point))
2603 (footnote-reference
2604 (list 'footnote-reference
2605 (list :label label
2606 :type type
2607 :begin begin
2608 :end end
2609 :post-blank post-blank))))
2610 (org-element-put-property
2611 footnote-reference :inline-definition
2612 (and (eq type 'inline)
2613 (org-element-parse-secondary-string
2614 (buffer-substring inner-begin inner-end)
2615 (org-element-restriction 'footnote-reference)
2616 footnote-reference))))))
2618 (defun org-element-footnote-reference-interpreter (footnote-reference contents)
2619 "Interpret FOOTNOTE-REFERENCE object as Org syntax.
2620 CONTENTS is nil."
2621 (let ((label (or (org-element-property :label footnote-reference) "fn:"))
2622 (def
2623 (let ((inline-def
2624 (org-element-property :inline-definition footnote-reference)))
2625 (if (not inline-def) ""
2626 (concat ":" (org-element-interpret-data inline-def))))))
2627 (format "[%s]" (concat label def))))
2629 (defun org-element-footnote-reference-successor (limit)
2630 "Search for the next footnote-reference object.
2632 LIMIT bounds the search.
2634 Return value is a cons cell whose CAR is `footnote-reference' and
2635 CDR is beginning position."
2636 (save-excursion
2637 (catch 'exit
2638 (while (re-search-forward org-footnote-re limit t)
2639 (save-excursion
2640 (let ((beg (match-beginning 0))
2641 (count 1))
2642 (backward-char)
2643 (while (re-search-forward "[][]" limit t)
2644 (if (equal (match-string 0) "[") (incf count) (decf count))
2645 (when (zerop count)
2646 (throw 'exit (cons 'footnote-reference beg))))))))))
2649 ;;;; Inline Babel Call
2651 (defun org-element-inline-babel-call-parser ()
2652 "Parse inline babel call at point.
2654 Return a list whose CAR is `inline-babel-call' and CDR a plist
2655 with `:begin', `:end', `:info' and `:post-blank' as keywords.
2657 Assume point is at the beginning of the babel call."
2658 (save-excursion
2659 (unless (bolp) (backward-char))
2660 (looking-at org-babel-inline-lob-one-liner-regexp)
2661 (let ((info (save-match-data (org-babel-lob-get-info)))
2662 (begin (match-end 1))
2663 (post-blank (progn (goto-char (match-end 0))
2664 (skip-chars-forward " \t")))
2665 (end (point)))
2666 (list 'inline-babel-call
2667 (list :begin begin
2668 :end end
2669 :info info
2670 :post-blank post-blank)))))
2672 (defun org-element-inline-babel-call-interpreter (inline-babel-call contents)
2673 "Interpret INLINE-BABEL-CALL object as Org syntax.
2674 CONTENTS is nil."
2675 (let* ((babel-info (org-element-property :info inline-babel-call))
2676 (main-source (car babel-info))
2677 (post-options (nth 1 babel-info)))
2678 (concat "call_"
2679 (if (string-match "\\[\\(\\[.*?\\]\\)\\]" main-source)
2680 ;; Remove redundant square brackets.
2681 (replace-match
2682 (match-string 1 main-source) nil nil main-source)
2683 main-source)
2684 (and post-options (format "[%s]" post-options)))))
2686 (defun org-element-inline-babel-call-successor (limit)
2687 "Search for the next inline-babel-call object.
2689 LIMIT bounds the search.
2691 Return value is a cons cell whose CAR is `inline-babel-call' and
2692 CDR is beginning position."
2693 (save-excursion
2694 ;; Use a simplified version of
2695 ;; `org-babel-inline-lob-one-liner-regexp'.
2696 (when (re-search-forward
2697 "call_\\([^()\n]+?\\)\\(?:\\[.*?\\]\\)?([^\n]*?)\\(\\[.*?\\]\\)?"
2698 limit t)
2699 (cons 'inline-babel-call (match-beginning 0)))))
2702 ;;;; Inline Src Block
2704 (defun org-element-inline-src-block-parser ()
2705 "Parse inline source block at point.
2707 LIMIT bounds the search.
2709 Return a list whose CAR is `inline-src-block' and CDR a plist
2710 with `:begin', `:end', `:language', `:value', `:parameters' and
2711 `:post-blank' as keywords.
2713 Assume point is at the beginning of the inline src block."
2714 (save-excursion
2715 (unless (bolp) (backward-char))
2716 (looking-at org-babel-inline-src-block-regexp)
2717 (let ((begin (match-beginning 1))
2718 (language (org-match-string-no-properties 2))
2719 (parameters (org-match-string-no-properties 4))
2720 (value (org-match-string-no-properties 5))
2721 (post-blank (progn (goto-char (match-end 0))
2722 (skip-chars-forward " \t")))
2723 (end (point)))
2724 (list 'inline-src-block
2725 (list :language language
2726 :value value
2727 :parameters parameters
2728 :begin begin
2729 :end end
2730 :post-blank post-blank)))))
2732 (defun org-element-inline-src-block-interpreter (inline-src-block contents)
2733 "Interpret INLINE-SRC-BLOCK object as Org syntax.
2734 CONTENTS is nil."
2735 (let ((language (org-element-property :language inline-src-block))
2736 (arguments (org-element-property :parameters inline-src-block))
2737 (body (org-element-property :value inline-src-block)))
2738 (format "src_%s%s{%s}"
2739 language
2740 (if arguments (format "[%s]" arguments) "")
2741 body)))
2743 (defun org-element-inline-src-block-successor (limit)
2744 "Search for the next inline-babel-call element.
2746 LIMIT bounds the search.
2748 Return value is a cons cell whose CAR is `inline-babel-call' and
2749 CDR is beginning position."
2750 (save-excursion
2751 (unless (bolp) (backward-char))
2752 (when (re-search-forward org-babel-inline-src-block-regexp limit t)
2753 (cons 'inline-src-block (match-beginning 1)))))
2755 ;;;; Italic
2757 (defun org-element-italic-parser ()
2758 "Parse italic object at point.
2760 Return a list whose CAR is `italic' and CDR is a plist with
2761 `:begin', `:end', `:contents-begin' and `:contents-end' and
2762 `:post-blank' keywords.
2764 Assume point is at the first slash marker."
2765 (save-excursion
2766 (unless (bolp) (backward-char 1))
2767 (looking-at org-emph-re)
2768 (let ((begin (match-beginning 2))
2769 (contents-begin (match-beginning 4))
2770 (contents-end (match-end 4))
2771 (post-blank (progn (goto-char (match-end 2))
2772 (skip-chars-forward " \t")))
2773 (end (point)))
2774 (list 'italic
2775 (list :begin begin
2776 :end end
2777 :contents-begin contents-begin
2778 :contents-end contents-end
2779 :post-blank post-blank)))))
2781 (defun org-element-italic-interpreter (italic contents)
2782 "Interpret ITALIC object as Org syntax.
2783 CONTENTS is the contents of the object."
2784 (format "/%s/" contents))
2787 ;;;; Latex Fragment
2789 (defun org-element-latex-fragment-parser ()
2790 "Parse latex fragment at point.
2792 Return a list whose CAR is `latex-fragment' and CDR a plist with
2793 `:value', `:begin', `:end', and `:post-blank' as keywords.
2795 Assume point is at the beginning of the latex fragment."
2796 (save-excursion
2797 (let* ((begin (point))
2798 (substring-match
2799 (catch 'exit
2800 (mapc (lambda (e)
2801 (let ((latex-regexp (nth 1 (assoc e org-latex-regexps))))
2802 (when (or (looking-at latex-regexp)
2803 (and (not (bobp))
2804 (save-excursion
2805 (backward-char)
2806 (looking-at latex-regexp))))
2807 (throw 'exit (nth 2 (assoc e org-latex-regexps))))))
2808 (plist-get org-format-latex-options :matchers))
2809 ;; None found: it's a macro.
2810 (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")
2812 (value (match-string-no-properties substring-match))
2813 (post-blank (progn (goto-char (match-end substring-match))
2814 (skip-chars-forward " \t")))
2815 (end (point)))
2816 (list 'latex-fragment
2817 (list :value value
2818 :begin begin
2819 :end end
2820 :post-blank post-blank)))))
2822 (defun org-element-latex-fragment-interpreter (latex-fragment contents)
2823 "Interpret LATEX-FRAGMENT object as Org syntax.
2824 CONTENTS is nil."
2825 (org-element-property :value latex-fragment))
2827 ;;;; Line Break
2829 (defun org-element-line-break-parser ()
2830 "Parse line break at point.
2832 Return a list whose CAR is `line-break', and CDR a plist with
2833 `:begin', `:end' and `:post-blank' keywords.
2835 Assume point is at the beginning of the line break."
2836 (list 'line-break (list :begin (point) :end (point-at-eol) :post-blank 0)))
2838 (defun org-element-line-break-interpreter (line-break contents)
2839 "Interpret LINE-BREAK object as Org syntax.
2840 CONTENTS is nil."
2841 "\\\\")
2843 (defun org-element-line-break-successor (limit)
2844 "Search for the next line-break object.
2846 LIMIT bounds the search.
2848 Return value is a cons cell whose CAR is `line-break' and CDR is
2849 beginning position."
2850 (save-excursion
2851 (let ((beg (and (re-search-forward "[^\\\\]\\(\\\\\\\\\\)[ \t]*$" limit t)
2852 (goto-char (match-beginning 1)))))
2853 ;; A line break can only happen on a non-empty line.
2854 (when (and beg (re-search-backward "\\S-" (point-at-bol) t))
2855 (cons 'line-break beg)))))
2858 ;;;; Link
2860 (defun org-element-link-parser ()
2861 "Parse link at point.
2863 Return a list whose CAR is `link' and CDR a plist with `:type',
2864 `:path', `:raw-link', `:application', `:search-option', `:begin',
2865 `:end', `:contents-begin', `:contents-end' and `:post-blank' as
2866 keywords.
2868 Assume point is at the beginning of the link."
2869 (save-excursion
2870 (let ((begin (point))
2871 end contents-begin contents-end link-end post-blank path type
2872 raw-link link search-option application)
2873 (cond
2874 ;; Type 1: Text targeted from a radio target.
2875 ((and org-target-link-regexp (looking-at org-target-link-regexp))
2876 (setq type "radio"
2877 link-end (match-end 0)
2878 path (org-match-string-no-properties 0)))
2879 ;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]]
2880 ((looking-at org-bracket-link-regexp)
2881 (setq contents-begin (match-beginning 3)
2882 contents-end (match-end 3)
2883 link-end (match-end 0)
2884 ;; RAW-LINK is the original link.
2885 raw-link (org-match-string-no-properties 1)
2886 link (org-translate-link
2887 (org-link-expand-abbrev
2888 (org-link-unescape raw-link))))
2889 ;; Determine TYPE of link and set PATH accordingly.
2890 (cond
2891 ;; File type.
2892 ((or (file-name-absolute-p link) (string-match "^\\.\\.?/" link))
2893 (setq type "file" path link))
2894 ;; Explicit type (http, irc, bbdb...). See `org-link-types'.
2895 ((string-match org-link-re-with-space3 link)
2896 (setq type (match-string 1 link) path (match-string 2 link)))
2897 ;; Id type: PATH is the id.
2898 ((string-match "^id:\\([-a-f0-9]+\\)" link)
2899 (setq type "id" path (match-string 1 link)))
2900 ;; Code-ref type: PATH is the name of the reference.
2901 ((string-match "^(\\(.*\\))$" link)
2902 (setq type "coderef" path (match-string 1 link)))
2903 ;; Custom-id type: PATH is the name of the custom id.
2904 ((= (aref link 0) ?#)
2905 (setq type "custom-id" path (substring link 1)))
2906 ;; Fuzzy type: Internal link either matches a target, an
2907 ;; headline name or nothing. PATH is the target or
2908 ;; headline's name.
2909 (t (setq type "fuzzy" path link))))
2910 ;; Type 3: Plain link, i.e. http://orgmode.org
2911 ((looking-at org-plain-link-re)
2912 (setq raw-link (org-match-string-no-properties 0)
2913 type (org-match-string-no-properties 1)
2914 path (org-match-string-no-properties 2)
2915 link-end (match-end 0)))
2916 ;; Type 4: Angular link, i.e. <http://orgmode.org>
2917 ((looking-at org-angle-link-re)
2918 (setq raw-link (buffer-substring-no-properties
2919 (match-beginning 1) (match-end 2))
2920 type (org-match-string-no-properties 1)
2921 path (org-match-string-no-properties 2)
2922 link-end (match-end 0))))
2923 ;; In any case, deduce end point after trailing white space from
2924 ;; LINK-END variable.
2925 (setq post-blank (progn (goto-char link-end) (skip-chars-forward " \t"))
2926 end (point))
2927 ;; Extract search option and opening application out of
2928 ;; "file"-type links.
2929 (when (member type org-element-link-type-is-file)
2930 ;; Application.
2931 (cond ((string-match "^file\\+\\(.*\\)$" type)
2932 (setq application (match-string 1 type)))
2933 ((not (string-match "^file" type))
2934 (setq application type)))
2935 ;; Extract search option from PATH.
2936 (when (string-match "::\\(.*\\)$" path)
2937 (setq search-option (match-string 1 path)
2938 path (replace-match "" nil nil path)))
2939 ;; Make sure TYPE always report "file".
2940 (setq type "file"))
2941 (list 'link
2942 (list :type type
2943 :path path
2944 :raw-link (or raw-link path)
2945 :application application
2946 :search-option search-option
2947 :begin begin
2948 :end end
2949 :contents-begin contents-begin
2950 :contents-end contents-end
2951 :post-blank post-blank)))))
2953 (defun org-element-link-interpreter (link contents)
2954 "Interpret LINK object as Org syntax.
2955 CONTENTS is the contents of the object, or nil."
2956 (let ((type (org-element-property :type link))
2957 (raw-link (org-element-property :raw-link link)))
2958 (if (string= type "radio") raw-link
2959 (format "[[%s]%s]"
2960 raw-link
2961 (if contents (format "[%s]" contents) "")))))
2963 (defun org-element-link-successor (limit)
2964 "Search for the next link object.
2966 LIMIT bounds the search.
2968 Return value is a cons cell whose CAR is `link' and CDR is
2969 beginning position."
2970 (save-excursion
2971 (let ((link-regexp
2972 (if (not org-target-link-regexp) org-any-link-re
2973 (concat org-any-link-re "\\|" org-target-link-regexp))))
2974 (when (re-search-forward link-regexp limit t)
2975 (cons 'link (match-beginning 0))))))
2978 ;;;; Macro
2980 (defun org-element-macro-parser ()
2981 "Parse macro at point.
2983 Return a list whose CAR is `macro' and CDR a plist with `:key',
2984 `:args', `:begin', `:end', `:value' and `:post-blank' as
2985 keywords.
2987 Assume point is at the macro."
2988 (save-excursion
2989 (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
2990 (let ((begin (point))
2991 (key (downcase (org-match-string-no-properties 1)))
2992 (value (org-match-string-no-properties 0))
2993 (post-blank (progn (goto-char (match-end 0))
2994 (skip-chars-forward " \t")))
2995 (end (point))
2996 (args (let ((args (org-match-string-no-properties 3)) args2)
2997 (when args
2998 (setq args (org-split-string args ","))
2999 (while args
3000 (while (string-match "\\\\\\'" (car args))
3001 ;; Repair bad splits.
3002 (setcar (cdr args) (concat (substring (car args) 0 -1)
3003 "," (nth 1 args)))
3004 (pop args))
3005 (push (pop args) args2))
3006 (mapcar 'org-trim (nreverse args2))))))
3007 (list 'macro
3008 (list :key key
3009 :value value
3010 :args args
3011 :begin begin
3012 :end end
3013 :post-blank post-blank)))))
3015 (defun org-element-macro-interpreter (macro contents)
3016 "Interpret MACRO object as Org syntax.
3017 CONTENTS is nil."
3018 (org-element-property :value macro))
3020 (defun org-element-macro-successor (limit)
3021 "Search for the next macro object.
3023 LIMIT bounds the search.
3025 Return value is cons cell whose CAR is `macro' and CDR is
3026 beginning position."
3027 (save-excursion
3028 (when (re-search-forward
3029 "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
3030 limit t)
3031 (cons 'macro (match-beginning 0)))))
3034 ;;;; Radio-target
3036 (defun org-element-radio-target-parser ()
3037 "Parse radio target at point.
3039 Return a list whose CAR is `radio-target' and CDR a plist with
3040 `:begin', `:end', `:contents-begin', `:contents-end', `:value'
3041 and `:post-blank' as keywords.
3043 Assume point is at the radio target."
3044 (save-excursion
3045 (looking-at org-radio-target-regexp)
3046 (let ((begin (point))
3047 (contents-begin (match-beginning 1))
3048 (contents-end (match-end 1))
3049 (value (org-match-string-no-properties 1))
3050 (post-blank (progn (goto-char (match-end 0))
3051 (skip-chars-forward " \t")))
3052 (end (point)))
3053 (list 'radio-target
3054 (list :begin begin
3055 :end end
3056 :contents-begin contents-begin
3057 :contents-end contents-end
3058 :post-blank post-blank
3059 :value value)))))
3061 (defun org-element-radio-target-interpreter (target contents)
3062 "Interpret TARGET object as Org syntax.
3063 CONTENTS is the contents of the object."
3064 (concat "<<<" contents ">>>"))
3066 (defun org-element-radio-target-successor (limit)
3067 "Search for the next radio-target object.
3069 LIMIT bounds the search.
3071 Return value is a cons cell whose CAR is `radio-target' and CDR
3072 is beginning position."
3073 (save-excursion
3074 (when (re-search-forward org-radio-target-regexp limit t)
3075 (cons 'radio-target (match-beginning 0)))))
3078 ;;;; Statistics Cookie
3080 (defun org-element-statistics-cookie-parser ()
3081 "Parse statistics cookie at point.
3083 Return a list whose CAR is `statistics-cookie', and CDR a plist
3084 with `:begin', `:end', `:value' and `:post-blank' keywords.
3086 Assume point is at the beginning of the statistics-cookie."
3087 (save-excursion
3088 (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
3089 (let* ((begin (point))
3090 (value (buffer-substring-no-properties
3091 (match-beginning 0) (match-end 0)))
3092 (post-blank (progn (goto-char (match-end 0))
3093 (skip-chars-forward " \t")))
3094 (end (point)))
3095 (list 'statistics-cookie
3096 (list :begin begin
3097 :end end
3098 :value value
3099 :post-blank post-blank)))))
3101 (defun org-element-statistics-cookie-interpreter (statistics-cookie contents)
3102 "Interpret STATISTICS-COOKIE object as Org syntax.
3103 CONTENTS is nil."
3104 (org-element-property :value statistics-cookie))
3106 (defun org-element-statistics-cookie-successor (limit)
3107 "Search for the next statistics cookie object.
3109 LIMIT bounds the search.
3111 Return value is a cons cell whose CAR is `statistics-cookie' and
3112 CDR is beginning position."
3113 (save-excursion
3114 (when (re-search-forward "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]" limit t)
3115 (cons 'statistics-cookie (match-beginning 0)))))
3118 ;;;; Strike-Through
3120 (defun org-element-strike-through-parser ()
3121 "Parse strike-through object at point.
3123 Return a list whose CAR is `strike-through' and CDR is a plist
3124 with `:begin', `:end', `:contents-begin' and `:contents-end' and
3125 `:post-blank' keywords.
3127 Assume point is at the first plus sign marker."
3128 (save-excursion
3129 (unless (bolp) (backward-char 1))
3130 (looking-at org-emph-re)
3131 (let ((begin (match-beginning 2))
3132 (contents-begin (match-beginning 4))
3133 (contents-end (match-end 4))
3134 (post-blank (progn (goto-char (match-end 2))
3135 (skip-chars-forward " \t")))
3136 (end (point)))
3137 (list 'strike-through
3138 (list :begin begin
3139 :end end
3140 :contents-begin contents-begin
3141 :contents-end contents-end
3142 :post-blank post-blank)))))
3144 (defun org-element-strike-through-interpreter (strike-through contents)
3145 "Interpret STRIKE-THROUGH object as Org syntax.
3146 CONTENTS is the contents of the object."
3147 (format "+%s+" contents))
3150 ;;;; Subscript
3152 (defun org-element-subscript-parser ()
3153 "Parse subscript at point.
3155 Return a list whose CAR is `subscript' and CDR a plist with
3156 `:begin', `:end', `:contents-begin', `:contents-end',
3157 `:use-brackets-p' and `:post-blank' as keywords.
3159 Assume point is at the underscore."
3160 (save-excursion
3161 (unless (bolp) (backward-char))
3162 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp)
3164 (not (looking-at org-match-substring-regexp))))
3165 (begin (match-beginning 2))
3166 (contents-begin (or (match-beginning 5)
3167 (match-beginning 3)))
3168 (contents-end (or (match-end 5) (match-end 3)))
3169 (post-blank (progn (goto-char (match-end 0))
3170 (skip-chars-forward " \t")))
3171 (end (point)))
3172 (list 'subscript
3173 (list :begin begin
3174 :end end
3175 :use-brackets-p bracketsp
3176 :contents-begin contents-begin
3177 :contents-end contents-end
3178 :post-blank post-blank)))))
3180 (defun org-element-subscript-interpreter (subscript contents)
3181 "Interpret SUBSCRIPT object as Org syntax.
3182 CONTENTS is the contents of the object."
3183 (format
3184 (if (org-element-property :use-brackets-p subscript) "_{%s}" "_%s")
3185 contents))
3187 (defun org-element-sub/superscript-successor (limit)
3188 "Search for the next sub/superscript object.
3190 LIMIT bounds the search.
3192 Return value is a cons cell whose CAR is either `subscript' or
3193 `superscript' and CDR is beginning position."
3194 (save-excursion
3195 (unless (bolp) (backward-char))
3196 (when (re-search-forward org-match-substring-regexp limit t)
3197 (cons (if (string= (match-string 2) "_") 'subscript 'superscript)
3198 (match-beginning 2)))))
3201 ;;;; Superscript
3203 (defun org-element-superscript-parser ()
3204 "Parse superscript at point.
3206 Return a list whose CAR is `superscript' and CDR a plist with
3207 `:begin', `:end', `:contents-begin', `:contents-end',
3208 `:use-brackets-p' and `:post-blank' as keywords.
3210 Assume point is at the caret."
3211 (save-excursion
3212 (unless (bolp) (backward-char))
3213 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp) t
3214 (not (looking-at org-match-substring-regexp))))
3215 (begin (match-beginning 2))
3216 (contents-begin (or (match-beginning 5)
3217 (match-beginning 3)))
3218 (contents-end (or (match-end 5) (match-end 3)))
3219 (post-blank (progn (goto-char (match-end 0))
3220 (skip-chars-forward " \t")))
3221 (end (point)))
3222 (list 'superscript
3223 (list :begin begin
3224 :end end
3225 :use-brackets-p bracketsp
3226 :contents-begin contents-begin
3227 :contents-end contents-end
3228 :post-blank post-blank)))))
3230 (defun org-element-superscript-interpreter (superscript contents)
3231 "Interpret SUPERSCRIPT object as Org syntax.
3232 CONTENTS is the contents of the object."
3233 (format
3234 (if (org-element-property :use-brackets-p superscript) "^{%s}" "^%s")
3235 contents))
3238 ;;;; Table Cell
3240 (defun org-element-table-cell-parser ()
3241 "Parse table cell at point.
3243 Return a list whose CAR is `table-cell' and CDR is a plist
3244 containing `:begin', `:end', `:contents-begin', `:contents-end'
3245 and `:post-blank' keywords."
3246 (looking-at "[ \t]*\\(.*?\\)[ \t]*|")
3247 (let* ((begin (match-beginning 0))
3248 (end (match-end 0))
3249 (contents-begin (match-beginning 1))
3250 (contents-end (match-end 1)))
3251 (list 'table-cell
3252 (list :begin begin
3253 :end end
3254 :contents-begin contents-begin
3255 :contents-end contents-end
3256 :post-blank 0))))
3258 (defun org-element-table-cell-interpreter (table-cell contents)
3259 "Interpret TABLE-CELL element as Org syntax.
3260 CONTENTS is the contents of the cell, or nil."
3261 (concat " " contents " |"))
3263 (defun org-element-table-cell-successor (limit)
3264 "Search for the next table-cell object.
3266 LIMIT bounds the search.
3268 Return value is a cons cell whose CAR is `table-cell' and CDR is
3269 beginning position."
3270 (when (looking-at "[ \t]*.*?[ \t]+|") (cons 'table-cell (point))))
3273 ;;;; Target
3275 (defun org-element-target-parser ()
3276 "Parse target at point.
3278 Return a list whose CAR is `target' and CDR a plist with
3279 `:begin', `:end', `:value' and `:post-blank' as keywords.
3281 Assume point is at the target."
3282 (save-excursion
3283 (looking-at org-target-regexp)
3284 (let ((begin (point))
3285 (value (org-match-string-no-properties 1))
3286 (post-blank (progn (goto-char (match-end 0))
3287 (skip-chars-forward " \t")))
3288 (end (point)))
3289 (list 'target
3290 (list :begin begin
3291 :end end
3292 :value value
3293 :post-blank post-blank)))))
3295 (defun org-element-target-interpreter (target contents)
3296 "Interpret TARGET object as Org syntax.
3297 CONTENTS is nil."
3298 (format "<<%s>>" (org-element-property :value target)))
3300 (defun org-element-target-successor (limit)
3301 "Search for the next target object.
3303 LIMIT bounds the search.
3305 Return value is a cons cell whose CAR is `target' and CDR is
3306 beginning position."
3307 (save-excursion
3308 (when (re-search-forward org-target-regexp limit t)
3309 (cons 'target (match-beginning 0)))))
3312 ;;;; Timestamp
3314 (defun org-element-timestamp-parser ()
3315 "Parse time stamp at point.
3317 Return a list whose CAR is `timestamp', and CDR a plist with
3318 `:type', `:begin', `:end', `:value' and `:post-blank' keywords.
3320 Assume point is at the beginning of the timestamp."
3321 (save-excursion
3322 (let* ((begin (point))
3323 (activep (eq (char-after) ?<))
3324 (main-value
3325 (progn
3326 (looking-at "[<[]\\(\\(%%\\)?.*?\\)[]>]\\(?:--[<[]\\(.*?\\)[]>]\\)?")
3327 (match-string-no-properties 1)))
3328 (range-end (match-string-no-properties 3))
3329 (type (cond ((match-string 2) 'diary)
3330 ((and activep range-end) 'active-range)
3331 (activep 'active)
3332 (range-end 'inactive-range)
3333 (t 'inactive)))
3334 (post-blank (progn (goto-char (match-end 0))
3335 (skip-chars-forward " \t")))
3336 (end (point)))
3337 (list 'timestamp
3338 (list :type type
3339 :value main-value
3340 :range-end range-end
3341 :begin begin
3342 :end end
3343 :post-blank post-blank)))))
3345 (defun org-element-timestamp-interpreter (timestamp contents)
3346 "Interpret TIMESTAMP object as Org syntax.
3347 CONTENTS is nil."
3348 (let ((type (org-element-property :type timestamp) ))
3349 (concat
3350 (format (if (memq type '(inactive inactive-range)) "[%s]" "<%s>")
3351 (org-element-property :value timestamp))
3352 (let ((range-end (org-element-property :range-end timestamp)))
3353 (when range-end
3354 (concat "--"
3355 (format (if (eq type 'inactive-range) "[%s]" "<%s>")
3356 range-end)))))))
3358 (defun org-element-timestamp-successor (limit)
3359 "Search for the next timestamp object.
3361 LIMIT bounds the search.
3363 Return value is a cons cell whose CAR is `timestamp' and CDR is
3364 beginning position."
3365 (save-excursion
3366 (when (re-search-forward
3367 (concat org-ts-regexp-both
3368 "\\|"
3369 "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
3370 "\\|"
3371 "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
3372 limit t)
3373 (cons 'timestamp (match-beginning 0)))))
3376 ;;;; Underline
3378 (defun org-element-underline-parser ()
3379 "Parse underline object at point.
3381 Return a list whose CAR is `underline' and CDR is a plist with
3382 `:begin', `:end', `:contents-begin' and `:contents-end' and
3383 `:post-blank' keywords.
3385 Assume point is at the first underscore marker."
3386 (save-excursion
3387 (unless (bolp) (backward-char 1))
3388 (looking-at org-emph-re)
3389 (let ((begin (match-beginning 2))
3390 (contents-begin (match-beginning 4))
3391 (contents-end (match-end 4))
3392 (post-blank (progn (goto-char (match-end 2))
3393 (skip-chars-forward " \t")))
3394 (end (point)))
3395 (list 'underline
3396 (list :begin begin
3397 :end end
3398 :contents-begin contents-begin
3399 :contents-end contents-end
3400 :post-blank post-blank)))))
3402 (defun org-element-underline-interpreter (underline contents)
3403 "Interpret UNDERLINE object as Org syntax.
3404 CONTENTS is the contents of the object."
3405 (format "_%s_" contents))
3408 ;;;; Verbatim
3410 (defun org-element-verbatim-parser ()
3411 "Parse verbatim object at point.
3413 Return a list whose CAR is `verbatim' and CDR is a plist with
3414 `:value', `:begin', `:end' and `:post-blank' keywords.
3416 Assume point is at the first equal sign marker."
3417 (save-excursion
3418 (unless (bolp) (backward-char 1))
3419 (looking-at org-emph-re)
3420 (let ((begin (match-beginning 2))
3421 (value (org-match-string-no-properties 4))
3422 (post-blank (progn (goto-char (match-end 2))
3423 (skip-chars-forward " \t")))
3424 (end (point)))
3425 (list 'verbatim
3426 (list :value value
3427 :begin begin
3428 :end end
3429 :post-blank post-blank)))))
3431 (defun org-element-verbatim-interpreter (verbatim contents)
3432 "Interpret VERBATIM object as Org syntax.
3433 CONTENTS is nil."
3434 (format "=%s=" (org-element-property :value verbatim)))
3438 ;;; Parsing Element Starting At Point
3440 ;; `org-element--current-element' is the core function of this section.
3441 ;; It returns the Lisp representation of the element starting at
3442 ;; point.
3444 ;; `org-element--current-element' makes use of special modes. They
3445 ;; are activated for fixed element chaining (i.e. `plain-list' >
3446 ;; `item') or fixed conditional element chaining (i.e. `headline' >
3447 ;; `section'). Special modes are: `first-section', `item',
3448 ;; `node-property', `quote-section', `section' and `table-row'.
3450 (defun org-element--current-element
3451 (limit &optional granularity special structure)
3452 "Parse the element starting at point.
3454 LIMIT bounds the search.
3456 Return value is a list like (TYPE PROPS) where TYPE is the type
3457 of the element and PROPS a plist of properties associated to the
3458 element.
3460 Possible types are defined in `org-element-all-elements'.
3462 Optional argument GRANULARITY determines the depth of the
3463 recursion. Allowed values are `headline', `greater-element',
3464 `element', `object' or nil. When it is broader than `object' (or
3465 nil), secondary values will not be parsed, since they only
3466 contain objects.
3468 Optional argument SPECIAL, when non-nil, can be either
3469 `first-section', `item', `node-property', `quote-section',
3470 `section', and `table-row'.
3472 If STRUCTURE isn't provided but SPECIAL is set to `item', it will
3473 be computed.
3475 This function assumes point is always at the beginning of the
3476 element it has to parse."
3477 (save-excursion
3478 (let ((case-fold-search t)
3479 ;; Determine if parsing depth allows for secondary strings
3480 ;; parsing. It only applies to elements referenced in
3481 ;; `org-element-secondary-value-alist'.
3482 (raw-secondary-p (and granularity (not (eq granularity 'object)))))
3483 (cond
3484 ;; Item.
3485 ((eq special 'item)
3486 (org-element-item-parser limit structure raw-secondary-p))
3487 ;; Table Row.
3488 ((eq special 'table-row) (org-element-table-row-parser limit))
3489 ;; Node Property.
3490 ((eq special 'node-property) (org-element-node-property-parser limit))
3491 ;; Headline.
3492 ((org-with-limited-levels (org-at-heading-p))
3493 (org-element-headline-parser limit raw-secondary-p))
3494 ;; Sections (must be checked after headline).
3495 ((eq special 'section) (org-element-section-parser limit))
3496 ((eq special 'quote-section) (org-element-quote-section-parser limit))
3497 ((eq special 'first-section)
3498 (org-element-section-parser
3499 (or (save-excursion (org-with-limited-levels (outline-next-heading)))
3500 limit)))
3501 ;; When not at bol, point is at the beginning of an item or
3502 ;; a footnote definition: next item is always a paragraph.
3503 ((not (bolp)) (org-element-paragraph-parser limit (list (point))))
3504 ;; Planning and Clock.
3505 ((and (looking-at org-planning-or-clock-line-re))
3506 (if (equal (match-string 1) org-clock-string)
3507 (org-element-clock-parser limit)
3508 (org-element-planning-parser limit)))
3509 ;; Inlinetask.
3510 ((org-at-heading-p)
3511 (org-element-inlinetask-parser limit raw-secondary-p))
3512 ;; From there, elements can have affiliated keywords.
3513 (t (let ((affiliated (org-element--collect-affiliated-keywords)))
3514 (cond
3515 ;; LaTeX Environment.
3516 ((looking-at "[ \t]*\\\\begin{\\([A-Za-z0-9*]+\\)}[ \t]*$")
3517 (if (save-excursion
3518 (re-search-forward
3519 (format "^[ \t]*\\\\end{%s}[ \t]*$"
3520 (regexp-quote (match-string 1)))
3521 nil t))
3522 (org-element-latex-environment-parser limit affiliated)
3523 (org-element-paragraph-parser limit affiliated)))
3524 ;; Drawer and Property Drawer.
3525 ((looking-at org-drawer-regexp)
3526 (let ((name (match-string 1)))
3527 (cond
3528 ((not (save-excursion
3529 (re-search-forward "^[ \t]*:END:[ \t]*$" nil t)))
3530 (org-element-paragraph-parser limit affiliated))
3531 ((equal "PROPERTIES" name)
3532 (org-element-property-drawer-parser limit affiliated))
3533 (t (org-element-drawer-parser limit affiliated)))))
3534 ;; Fixed Width
3535 ((looking-at "[ \t]*:\\( \\|$\\)")
3536 (org-element-fixed-width-parser limit affiliated))
3537 ;; Inline Comments, Blocks, Babel Calls, Dynamic Blocks and
3538 ;; Keywords.
3539 ((looking-at "[ \t]*#")
3540 (goto-char (match-end 0))
3541 (cond ((looking-at "\\(?: \\|$\\)")
3542 (beginning-of-line)
3543 (org-element-comment-parser limit affiliated))
3544 ((looking-at "\\+BEGIN_\\(\\S-+\\)")
3545 (beginning-of-line)
3546 (let ((parser (assoc (upcase (match-string 1))
3547 org-element-block-name-alist)))
3548 (if parser (funcall (cdr parser) limit affiliated)
3549 (org-element-special-block-parser limit affiliated))))
3550 ((looking-at "\\+CALL:")
3551 (beginning-of-line)
3552 (org-element-babel-call-parser limit affiliated))
3553 ((looking-at "\\+BEGIN:? ")
3554 (beginning-of-line)
3555 (org-element-dynamic-block-parser limit affiliated))
3556 ((looking-at "\\+\\S-+:")
3557 (beginning-of-line)
3558 (org-element-keyword-parser limit affiliated))
3560 (beginning-of-line)
3561 (org-element-paragraph-parser limit affiliated))))
3562 ;; Footnote Definition.
3563 ((looking-at org-footnote-definition-re)
3564 (org-element-footnote-definition-parser limit affiliated))
3565 ;; Horizontal Rule.
3566 ((looking-at "[ \t]*-\\{5,\\}[ \t]*$")
3567 (org-element-horizontal-rule-parser limit affiliated))
3568 ;; Table.
3569 ((org-at-table-p t) (org-element-table-parser limit affiliated))
3570 ;; List.
3571 ((looking-at (org-item-re))
3572 (org-element-plain-list-parser
3573 limit affiliated (or structure (org-list-struct))))
3574 ;; Default element: Paragraph.
3575 (t (org-element-paragraph-parser limit affiliated)))))))))
3578 ;; Most elements can have affiliated keywords. When looking for an
3579 ;; element beginning, we want to move before them, as they belong to
3580 ;; that element, and, in the meantime, collect information they give
3581 ;; into appropriate properties. Hence the following function.
3583 (defun org-element--collect-affiliated-keywords ()
3584 "Collect affiliated keywords from point.
3586 Return a list whose CAR is the position at the first of them and
3587 CDR a plist of keywords and values and move point to the
3588 beginning of the first line after them.
3590 As a special case, if element doesn't start at the beginning of
3591 the line (i.e. a paragraph starting an item), CAR is current
3592 position of point and CDR is nil."
3593 (if (not (bolp)) (list (point))
3594 (let ((case-fold-search t)
3595 (origin (point))
3596 ;; RESTRICT is the list of objects allowed in parsed
3597 ;; keywords value.
3598 (restrict (org-element-restriction 'keyword))
3599 output)
3600 (while (and (not (eobp)) (looking-at org-element--affiliated-re))
3601 (let* ((raw-kwd (upcase (match-string 1)))
3602 ;; Apply translation to RAW-KWD. From there, KWD is
3603 ;; the official keyword.
3604 (kwd (or (cdr (assoc raw-kwd
3605 org-element-keyword-translation-alist))
3606 raw-kwd))
3607 ;; Find main value for any keyword.
3608 (value
3609 (save-match-data
3610 (org-trim
3611 (buffer-substring-no-properties
3612 (match-end 0) (point-at-eol)))))
3613 ;; PARSEDP is non-nil when keyword should have its
3614 ;; value parsed.
3615 (parsedp (member kwd org-element-parsed-keywords))
3616 ;; If KWD is a dual keyword, find its secondary
3617 ;; value. Maybe parse it.
3618 (dualp (member kwd org-element-dual-keywords))
3619 (dual-value
3620 (and dualp
3621 (let ((sec (org-match-string-no-properties 2)))
3622 (if (or (not sec) (not parsedp)) sec
3623 (org-element-parse-secondary-string sec restrict)))))
3624 ;; Attribute a property name to KWD.
3625 (kwd-sym (and kwd (intern (concat ":" (downcase kwd))))))
3626 ;; Now set final shape for VALUE.
3627 (when parsedp
3628 (setq value (org-element-parse-secondary-string value restrict)))
3629 (when dualp (setq value (and value (cons value dual-value))))
3630 (when (or (member kwd org-element-multiple-keywords)
3631 ;; Attributes can always appear on multiple lines.
3632 (string-match "^ATTR_" kwd))
3633 (setq value (cons value (plist-get output kwd-sym))))
3634 ;; Eventually store the new value in OUTPUT.
3635 (setq output (plist-put output kwd-sym value))
3636 ;; Move to next keyword.
3637 (forward-line)))
3638 ;; If affiliated keywords are orphaned: move back to first one.
3639 ;; They will be parsed as a paragraph.
3640 (when (looking-at "[ \t]*$") (goto-char origin) (setq output nil))
3641 ;; Return value.
3642 (cons origin output))))
3646 ;;; The Org Parser
3648 ;; The two major functions here are `org-element-parse-buffer', which
3649 ;; parses Org syntax inside the current buffer, taking into account
3650 ;; region, narrowing, or even visibility if specified, and
3651 ;; `org-element-parse-secondary-string', which parses objects within
3652 ;; a given string.
3654 ;; The (almost) almighty `org-element-map' allows to apply a function
3655 ;; on elements or objects matching some type, and accumulate the
3656 ;; resulting values. In an export situation, it also skips unneeded
3657 ;; parts of the parse tree.
3659 (defun org-element-parse-buffer (&optional granularity visible-only)
3660 "Recursively parse the buffer and return structure.
3661 If narrowing is in effect, only parse the visible part of the
3662 buffer.
3664 Optional argument GRANULARITY determines the depth of the
3665 recursion. It can be set to the following symbols:
3667 `headline' Only parse headlines.
3668 `greater-element' Don't recurse into greater elements excepted
3669 headlines and sections. Thus, elements
3670 parsed are the top-level ones.
3671 `element' Parse everything but objects and plain text.
3672 `object' Parse the complete buffer (default).
3674 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3675 elements.
3677 Assume buffer is in Org mode."
3678 (save-excursion
3679 (goto-char (point-min))
3680 (org-skip-whitespace)
3681 (org-element--parse-elements
3682 (point-at-bol) (point-max)
3683 ;; Start in `first-section' mode so text before the first
3684 ;; headline belongs to a section.
3685 'first-section nil granularity visible-only (list 'org-data nil))))
3687 (defun org-element-parse-secondary-string (string restriction &optional parent)
3688 "Recursively parse objects in STRING and return structure.
3690 RESTRICTION is a symbol limiting the object types that will be
3691 looked after.
3693 Optional argument PARENT, when non-nil, is the element or object
3694 containing the secondary string. It is used to set correctly
3695 `:parent' property within the string."
3696 (with-temp-buffer
3697 (insert string)
3698 (let ((secondary (org-element--parse-objects
3699 (point-min) (point-max) nil restriction)))
3700 (mapc (lambda (obj) (org-element-put-property obj :parent parent))
3701 secondary))))
3703 (defun org-element-map
3704 (data types fun &optional info first-match no-recursion with-affiliated)
3705 "Map a function on selected elements or objects.
3707 DATA is the parsed tree, as returned by, i.e,
3708 `org-element-parse-buffer'. TYPES is a symbol or list of symbols
3709 of elements or objects types. FUN is the function called on the
3710 matching element or object. It must accept one arguments: the
3711 element or object itself.
3713 When optional argument INFO is non-nil, it should be a plist
3714 holding export options. In that case, parts of the parse tree
3715 not exportable according to that property list will be skipped.
3717 When optional argument FIRST-MATCH is non-nil, stop at the first
3718 match for which FUN doesn't return nil, and return that value.
3720 Optional argument NO-RECURSION is a symbol or a list of symbols
3721 representing elements or objects types. `org-element-map' won't
3722 enter any recursive element or object whose type belongs to that
3723 list. Though, FUN can still be applied on them.
3725 When optional argument WITH-AFFILIATED is non-nil, also move into
3726 affiliated keywords to find objects.
3728 Nil values returned from FUN do not appear in the results."
3729 ;; Ensure TYPES and NO-RECURSION are a list, even of one element.
3730 (unless (listp types) (setq types (list types)))
3731 (unless (listp no-recursion) (setq no-recursion (list no-recursion)))
3732 ;; Recursion depth is determined by --CATEGORY.
3733 (let* ((--category
3734 (catch 'found
3735 (let ((category 'greater-elements))
3736 (mapc (lambda (type)
3737 (cond ((or (memq type org-element-all-objects)
3738 (eq type 'plain-text))
3739 ;; If one object is found, the function
3740 ;; has to recurse into every object.
3741 (throw 'found 'objects))
3742 ((not (memq type org-element-greater-elements))
3743 ;; If one regular element is found, the
3744 ;; function has to recurse, at least,
3745 ;; into every element it encounters.
3746 (and (not (eq category 'elements))
3747 (setq category 'elements)))))
3748 types)
3749 category)))
3750 ;; Compute properties for affiliated keywords if necessary.
3751 (--affiliated-alist
3752 (and with-affiliated
3753 (mapcar (lambda (kwd)
3754 (cons kwd (intern (concat ":" (downcase kwd)))))
3755 org-element-affiliated-keywords)))
3756 --acc
3757 --walk-tree
3758 (--walk-tree
3759 (function
3760 (lambda (--data)
3761 ;; Recursively walk DATA. INFO, if non-nil, is a plist
3762 ;; holding contextual information.
3763 (let ((--type (org-element-type --data)))
3764 (cond
3765 ((not --data))
3766 ;; Ignored element in an export context.
3767 ((and info (memq --data (plist-get info :ignore-list))))
3768 ;; Secondary string: only objects can be found there.
3769 ((not --type)
3770 (when (eq --category 'objects) (mapc --walk-tree --data)))
3771 ;; Unconditionally enter parse trees.
3772 ((eq --type 'org-data)
3773 (mapc --walk-tree (org-element-contents --data)))
3775 ;; Check if TYPE is matching among TYPES. If so,
3776 ;; apply FUN to --DATA and accumulate return value
3777 ;; into --ACC (or exit if FIRST-MATCH is non-nil).
3778 (when (memq --type types)
3779 (let ((result (funcall fun --data)))
3780 (cond ((not result))
3781 (first-match (throw '--map-first-match result))
3782 (t (push result --acc)))))
3783 ;; If --DATA has a secondary string that can contain
3784 ;; objects with their type among TYPES, look into it.
3785 (when (and (eq --category 'objects) (not (stringp --data)))
3786 (let ((sec-prop
3787 (assq --type org-element-secondary-value-alist)))
3788 (when sec-prop
3789 (funcall --walk-tree
3790 (org-element-property (cdr sec-prop) --data)))))
3791 ;; If --DATA has any affiliated keywords and
3792 ;; WITH-AFFILIATED is non-nil, look for objects in
3793 ;; them.
3794 (when (and with-affiliated
3795 (eq --category 'objects)
3796 (memq --type org-element-all-elements))
3797 (mapc (lambda (kwd-pair)
3798 (let ((kwd (car kwd-pair))
3799 (value (org-element-property
3800 (cdr kwd-pair) --data)))
3801 ;; Pay attention to the type of value.
3802 ;; Preserve order for multiple keywords.
3803 (cond
3804 ((not value))
3805 ((and (member kwd org-element-multiple-keywords)
3806 (member kwd org-element-dual-keywords))
3807 (mapc (lambda (line)
3808 (funcall --walk-tree (cdr line))
3809 (funcall --walk-tree (car line)))
3810 (reverse value)))
3811 ((member kwd org-element-multiple-keywords)
3812 (mapc (lambda (line) (funcall --walk-tree line))
3813 (reverse value)))
3814 ((member kwd org-element-dual-keywords)
3815 (funcall --walk-tree (cdr value))
3816 (funcall --walk-tree (car value)))
3817 (t (funcall --walk-tree value)))))
3818 --affiliated-alist))
3819 ;; Determine if a recursion into --DATA is possible.
3820 (cond
3821 ;; --TYPE is explicitly removed from recursion.
3822 ((memq --type no-recursion))
3823 ;; --DATA has no contents.
3824 ((not (org-element-contents --data)))
3825 ;; Looking for greater elements but --DATA is simply
3826 ;; an element or an object.
3827 ((and (eq --category 'greater-elements)
3828 (not (memq --type org-element-greater-elements))))
3829 ;; Looking for elements but --DATA is an object.
3830 ((and (eq --category 'elements)
3831 (memq --type org-element-all-objects)))
3832 ;; In any other case, map contents.
3833 (t (mapc --walk-tree (org-element-contents --data)))))))))))
3834 (catch '--map-first-match
3835 (funcall --walk-tree data)
3836 ;; Return value in a proper order.
3837 (nreverse --acc))))
3839 ;; The following functions are internal parts of the parser.
3841 ;; The first one, `org-element--parse-elements' acts at the element's
3842 ;; level.
3844 ;; The second one, `org-element--parse-objects' applies on all objects
3845 ;; of a paragraph or a secondary string. It uses
3846 ;; `org-element--get-next-object-candidates' to optimize the search of
3847 ;; the next object in the buffer.
3849 ;; More precisely, that function looks for every allowed object type
3850 ;; first. Then, it discards failed searches, keeps further matches,
3851 ;; and searches again types matched behind point, for subsequent
3852 ;; calls. Thus, searching for a given type fails only once, and every
3853 ;; object is searched only once at top level (but sometimes more for
3854 ;; nested types).
3856 (defun org-element--parse-elements
3857 (beg end special structure granularity visible-only acc)
3858 "Parse elements between BEG and END positions.
3860 SPECIAL prioritize some elements over the others. It can be set
3861 to `first-section', `quote-section', `section' `item' or
3862 `table-row'.
3864 When value is `item', STRUCTURE will be used as the current list
3865 structure.
3867 GRANULARITY determines the depth of the recursion. See
3868 `org-element-parse-buffer' for more information.
3870 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3871 elements.
3873 Elements are accumulated into ACC."
3874 (save-excursion
3875 (goto-char beg)
3876 ;; When parsing only headlines, skip any text before first one.
3877 (when (and (eq granularity 'headline) (not (org-at-heading-p)))
3878 (org-with-limited-levels (outline-next-heading)))
3879 ;; Main loop start.
3880 (while (< (point) end)
3881 ;; Find current element's type and parse it accordingly to
3882 ;; its category.
3883 (let* ((element (org-element--current-element
3884 end granularity special structure))
3885 (type (org-element-type element))
3886 (cbeg (org-element-property :contents-begin element)))
3887 (goto-char (org-element-property :end element))
3888 ;; Fill ELEMENT contents by side-effect.
3889 (cond
3890 ;; If VISIBLE-ONLY is true and element is hidden or if it has
3891 ;; no contents, don't modify it.
3892 ((or (and visible-only (org-element-property :hiddenp element))
3893 (not cbeg)))
3894 ;; Greater element: parse it between `contents-begin' and
3895 ;; `contents-end'. Make sure GRANULARITY allows the
3896 ;; recursion, or ELEMENT is an headline, in which case going
3897 ;; inside is mandatory, in order to get sub-level headings.
3898 ((and (memq type org-element-greater-elements)
3899 (or (memq granularity '(element object nil))
3900 (and (eq granularity 'greater-element)
3901 (eq type 'section))
3902 (eq type 'headline)))
3903 (org-element--parse-elements
3904 cbeg (org-element-property :contents-end element)
3905 ;; Possibly switch to a special mode.
3906 (case type
3907 (headline
3908 (if (org-element-property :quotedp element) 'quote-section
3909 'section))
3910 (plain-list 'item)
3911 (property-drawer 'node-property)
3912 (table 'table-row))
3913 (org-element-property :structure element)
3914 granularity visible-only element))
3915 ;; ELEMENT has contents. Parse objects inside, if
3916 ;; GRANULARITY allows it.
3917 ((memq granularity '(object nil))
3918 (org-element--parse-objects
3919 cbeg (org-element-property :contents-end element) element
3920 (org-element-restriction type))))
3921 (org-element-adopt-elements acc element)))
3922 ;; Return result.
3923 acc))
3925 (defun org-element--parse-objects (beg end acc restriction)
3926 "Parse objects between BEG and END and return recursive structure.
3928 Objects are accumulated in ACC.
3930 RESTRICTION is a list of object types which are allowed in the
3931 current object."
3932 (let (candidates)
3933 (save-excursion
3934 (goto-char beg)
3935 (while (and (< (point) end)
3936 (setq candidates (org-element--get-next-object-candidates
3937 end restriction candidates)))
3938 (let ((next-object
3939 (let ((pos (apply 'min (mapcar 'cdr candidates))))
3940 (save-excursion
3941 (goto-char pos)
3942 (funcall (intern (format "org-element-%s-parser"
3943 (car (rassq pos candidates)))))))))
3944 ;; 1. Text before any object. Untabify it.
3945 (let ((obj-beg (org-element-property :begin next-object)))
3946 (unless (= (point) obj-beg)
3947 (setq acc
3948 (org-element-adopt-elements
3950 (replace-regexp-in-string
3951 "\t" (make-string tab-width ? )
3952 (buffer-substring-no-properties (point) obj-beg))))))
3953 ;; 2. Object...
3954 (let ((obj-end (org-element-property :end next-object))
3955 (cont-beg (org-element-property :contents-begin next-object)))
3956 ;; Fill contents of NEXT-OBJECT by side-effect, if it has
3957 ;; a recursive type.
3958 (when (and cont-beg
3959 (memq (car next-object) org-element-recursive-objects))
3960 (save-restriction
3961 (narrow-to-region
3962 cont-beg
3963 (org-element-property :contents-end next-object))
3964 (org-element--parse-objects
3965 (point-min) (point-max) next-object
3966 (org-element-restriction next-object))))
3967 (setq acc (org-element-adopt-elements acc next-object))
3968 (goto-char obj-end))))
3969 ;; 3. Text after last object. Untabify it.
3970 (unless (= (point) end)
3971 (setq acc
3972 (org-element-adopt-elements
3974 (replace-regexp-in-string
3975 "\t" (make-string tab-width ? )
3976 (buffer-substring-no-properties (point) end)))))
3977 ;; Result.
3978 acc)))
3980 (defun org-element--get-next-object-candidates (limit restriction objects)
3981 "Return an alist of candidates for the next object.
3983 LIMIT bounds the search, and RESTRICTION narrows candidates to
3984 some object types.
3986 Return value is an alist whose CAR is position and CDR the object
3987 type, as a symbol.
3989 OBJECTS is the previous candidates alist."
3990 ;; Filter out any object found but not belonging to RESTRICTION.
3991 (setq objects
3992 (org-remove-if-not
3993 (lambda (obj)
3994 (let ((type (car obj)))
3995 (memq (or (cdr (assq type org-element-object-successor-alist))
3996 type)
3997 restriction)))
3998 objects))
3999 (let (next-candidates types-to-search)
4000 ;; If no previous result, search every object type in RESTRICTION.
4001 ;; Otherwise, keep potential candidates (old objects located after
4002 ;; point) and ask to search again those which had matched before.
4003 (if (not objects) (setq types-to-search restriction)
4004 (mapc (lambda (obj)
4005 (if (< (cdr obj) (point)) (push (car obj) types-to-search)
4006 (push obj next-candidates)))
4007 objects))
4008 ;; Call the appropriate successor function for each type to search
4009 ;; and accumulate matches.
4010 (mapc
4011 (lambda (type)
4012 (let* ((successor-fun
4013 (intern
4014 (format "org-element-%s-successor"
4015 (or (cdr (assq type org-element-object-successor-alist))
4016 type))))
4017 (obj (funcall successor-fun limit)))
4018 (and obj (push obj next-candidates))))
4019 types-to-search)
4020 ;; Return alist.
4021 next-candidates))
4025 ;;; Towards A Bijective Process
4027 ;; The parse tree obtained with `org-element-parse-buffer' is really
4028 ;; a snapshot of the corresponding Org buffer. Therefore, it can be
4029 ;; interpreted and expanded into a string with canonical Org syntax.
4030 ;; Hence `org-element-interpret-data'.
4032 ;; The function relies internally on
4033 ;; `org-element--interpret-affiliated-keywords'.
4035 ;;;###autoload
4036 (defun org-element-interpret-data (data &optional parent)
4037 "Interpret DATA as Org syntax.
4039 DATA is a parse tree, an element, an object or a secondary string
4040 to interpret.
4042 Optional argument PARENT is used for recursive calls. It contains
4043 the element or object containing data, or nil.
4045 Return Org syntax as a string."
4046 (let* ((type (org-element-type data))
4047 (results
4048 (cond
4049 ;; Secondary string.
4050 ((not type)
4051 (mapconcat
4052 (lambda (obj) (org-element-interpret-data obj parent))
4053 data ""))
4054 ;; Full Org document.
4055 ((eq type 'org-data)
4056 (mapconcat
4057 (lambda (obj) (org-element-interpret-data obj parent))
4058 (org-element-contents data) ""))
4059 ;; Plain text.
4060 ((stringp data) data)
4061 ;; Element/Object without contents.
4062 ((not (org-element-contents data))
4063 (funcall (intern (format "org-element-%s-interpreter" type))
4064 data nil))
4065 ;; Element/Object with contents.
4067 (let* ((greaterp (memq type org-element-greater-elements))
4068 (objectp (and (not greaterp)
4069 (memq type org-element-recursive-objects)))
4070 (contents
4071 (mapconcat
4072 (lambda (obj) (org-element-interpret-data obj data))
4073 (org-element-contents
4074 (if (or greaterp objectp) data
4075 ;; Elements directly containing objects must
4076 ;; have their indentation normalized first.
4077 (org-element-normalize-contents
4078 data
4079 ;; When normalizing first paragraph of an
4080 ;; item or a footnote-definition, ignore
4081 ;; first line's indentation.
4082 (and (eq type 'paragraph)
4083 (equal data (car (org-element-contents parent)))
4084 (memq (org-element-type parent)
4085 '(footnote-definition item))))))
4086 "")))
4087 (funcall (intern (format "org-element-%s-interpreter" type))
4088 data
4089 (if greaterp (org-element-normalize-contents contents)
4090 contents)))))))
4091 (if (memq type '(org-data plain-text nil)) results
4092 ;; Build white spaces. If no `:post-blank' property is
4093 ;; specified, assume its value is 0.
4094 (let ((post-blank (or (org-element-property :post-blank data) 0)))
4095 (if (memq type org-element-all-objects)
4096 (concat results (make-string post-blank 32))
4097 (concat
4098 (org-element--interpret-affiliated-keywords data)
4099 (org-element-normalize-string results)
4100 (make-string post-blank 10)))))))
4102 (defun org-element--interpret-affiliated-keywords (element)
4103 "Return ELEMENT's affiliated keywords as Org syntax.
4104 If there is no affiliated keyword, return the empty string."
4105 (let ((keyword-to-org
4106 (function
4107 (lambda (key value)
4108 (let (dual)
4109 (when (member key org-element-dual-keywords)
4110 (setq dual (cdr value) value (car value)))
4111 (concat "#+" key
4112 (and dual
4113 (format "[%s]" (org-element-interpret-data dual)))
4114 ": "
4115 (if (member key org-element-parsed-keywords)
4116 (org-element-interpret-data value)
4117 value)
4118 "\n"))))))
4119 (mapconcat
4120 (lambda (prop)
4121 (let ((value (org-element-property prop element))
4122 (keyword (upcase (substring (symbol-name prop) 1))))
4123 (when value
4124 (if (or (member keyword org-element-multiple-keywords)
4125 ;; All attribute keywords can have multiple lines.
4126 (string-match "^ATTR_" keyword))
4127 (mapconcat (lambda (line) (funcall keyword-to-org keyword line))
4128 (reverse value)
4130 (funcall keyword-to-org keyword value)))))
4131 ;; List all ELEMENT's properties matching an attribute line or an
4132 ;; affiliated keyword, but ignore translated keywords since they
4133 ;; cannot belong to the property list.
4134 (loop for prop in (nth 1 element) by 'cddr
4135 when (let ((keyword (upcase (substring (symbol-name prop) 1))))
4136 (or (string-match "^ATTR_" keyword)
4137 (and
4138 (member keyword org-element-affiliated-keywords)
4139 (not (assoc keyword
4140 org-element-keyword-translation-alist)))))
4141 collect prop)
4142 "")))
4144 ;; Because interpretation of the parse tree must return the same
4145 ;; number of blank lines between elements and the same number of white
4146 ;; space after objects, some special care must be given to white
4147 ;; spaces.
4149 ;; The first function, `org-element-normalize-string', ensures any
4150 ;; string different from the empty string will end with a single
4151 ;; newline character.
4153 ;; The second function, `org-element-normalize-contents', removes
4154 ;; global indentation from the contents of the current element.
4156 (defun org-element-normalize-string (s)
4157 "Ensure string S ends with a single newline character.
4159 If S isn't a string return it unchanged. If S is the empty
4160 string, return it. Otherwise, return a new string with a single
4161 newline character at its end."
4162 (cond
4163 ((not (stringp s)) s)
4164 ((string= "" s) "")
4165 (t (and (string-match "\\(\n[ \t]*\\)*\\'" s)
4166 (replace-match "\n" nil nil s)))))
4168 (defun org-element-normalize-contents (element &optional ignore-first)
4169 "Normalize plain text in ELEMENT's contents.
4171 ELEMENT must only contain plain text and objects.
4173 If optional argument IGNORE-FIRST is non-nil, ignore first line's
4174 indentation to compute maximal common indentation.
4176 Return the normalized element that is element with global
4177 indentation removed from its contents. The function assumes that
4178 indentation is not done with TAB characters."
4179 (let* (ind-list ; for byte-compiler
4180 collect-inds ; for byte-compiler
4181 (collect-inds
4182 (function
4183 ;; Return list of indentations within BLOB. This is done by
4184 ;; walking recursively BLOB and updating IND-LIST along the
4185 ;; way. FIRST-FLAG is non-nil when the first string hasn't
4186 ;; been seen yet. It is required as this string is the only
4187 ;; one whose indentation doesn't happen after a newline
4188 ;; character.
4189 (lambda (blob first-flag)
4190 (mapc
4191 (lambda (object)
4192 (when (and first-flag (stringp object))
4193 (setq first-flag nil)
4194 (string-match "\\`\\( *\\)" object)
4195 (let ((len (length (match-string 1 object))))
4196 ;; An indentation of zero means no string will be
4197 ;; modified. Quit the process.
4198 (if (zerop len) (throw 'zero (setq ind-list nil))
4199 (push len ind-list))))
4200 (cond
4201 ((stringp object)
4202 (let ((start 0))
4203 ;; Avoid matching blank or empty lines.
4204 (while (and (string-match "\n\\( *\\)\\(.\\)" object start)
4205 (not (equal (match-string 2 object) " ")))
4206 (setq start (match-end 0))
4207 (push (length (match-string 1 object)) ind-list))))
4208 ((memq (org-element-type object) org-element-recursive-objects)
4209 (funcall collect-inds object first-flag))))
4210 (org-element-contents blob))))))
4211 ;; Collect indentation list in ELEMENT. Possibly remove first
4212 ;; value if IGNORE-FIRST is non-nil.
4213 (catch 'zero (funcall collect-inds element (not ignore-first)))
4214 (if (not ind-list) element
4215 ;; Build ELEMENT back, replacing each string with the same
4216 ;; string minus common indentation.
4217 (let* (build ; For byte compiler.
4218 (build
4219 (function
4220 (lambda (blob mci first-flag)
4221 ;; Return BLOB with all its strings indentation
4222 ;; shortened from MCI white spaces. FIRST-FLAG is
4223 ;; non-nil when the first string hasn't been seen
4224 ;; yet.
4225 (setcdr (cdr blob)
4226 (mapcar
4227 (lambda (object)
4228 (when (and first-flag (stringp object))
4229 (setq first-flag nil)
4230 (setq object
4231 (replace-regexp-in-string
4232 (format "\\` \\{%d\\}" mci) "" object)))
4233 (cond
4234 ((stringp object)
4235 (replace-regexp-in-string
4236 (format "\n \\{%d\\}" mci) "\n" object))
4237 ((memq (org-element-type object)
4238 org-element-recursive-objects)
4239 (funcall build object mci first-flag))
4240 (t object)))
4241 (org-element-contents blob)))
4242 blob))))
4243 (funcall build element (apply 'min ind-list) (not ignore-first))))))
4247 ;;; The Toolbox
4249 ;; The first move is to implement a way to obtain the smallest element
4250 ;; containing point. This is the job of `org-element-at-point'. It
4251 ;; basically jumps back to the beginning of section containing point
4252 ;; and moves, element after element, with
4253 ;; `org-element--current-element' until the container is found. Note:
4254 ;; When using `org-element-at-point', secondary values are never
4255 ;; parsed since the function focuses on elements, not on objects.
4257 ;; At a deeper level, `org-element-context' lists all elements and
4258 ;; objects containing point.
4260 ;; `org-element-nested-p' and `org-element-swap-A-B' may be used
4261 ;; internally by navigation and manipulation tools.
4263 ;;;###autoload
4264 (defun org-element-at-point (&optional keep-trail)
4265 "Determine closest element around point.
4267 Return value is a list like (TYPE PROPS) where TYPE is the type
4268 of the element and PROPS a plist of properties associated to the
4269 element.
4271 Possible types are defined in `org-element-all-elements'.
4272 Properties depend on element or object type, but always
4273 include :begin, :end, :parent and :post-blank properties.
4275 As a special case, if point is at the very beginning of a list or
4276 sub-list, returned element will be that list instead of the first
4277 item. In the same way, if point is at the beginning of the first
4278 row of a table, returned element will be the table instead of the
4279 first row.
4281 If optional argument KEEP-TRAIL is non-nil, the function returns
4282 a list of of elements leading to element at point. The list's
4283 CAR is always the element at point. Following positions contain
4284 element's siblings, then parents, siblings of parents, until the
4285 first element of current section."
4286 (org-with-wide-buffer
4287 ;; If at an headline, parse it. It is the sole element that
4288 ;; doesn't require to know about context. Be sure to disallow
4289 ;; secondary string parsing, though.
4290 (if (org-with-limited-levels (org-at-heading-p))
4291 (progn
4292 (beginning-of-line)
4293 (if (not keep-trail) (org-element-headline-parser (point-max) t)
4294 (list (org-element-headline-parser (point-max) t))))
4295 ;; Otherwise move at the beginning of the section containing
4296 ;; point.
4297 (let ((origin (point))
4298 (end (save-excursion
4299 (org-with-limited-levels (outline-next-heading)) (point)))
4300 element type special-flag trail struct prevs parent)
4301 (org-with-limited-levels
4302 (if (org-with-limited-levels (org-before-first-heading-p))
4303 (goto-char (point-min))
4304 (org-back-to-heading)
4305 (forward-line)))
4306 (org-skip-whitespace)
4307 (beginning-of-line)
4308 ;; Parse successively each element, skipping those ending
4309 ;; before original position.
4310 (catch 'exit
4311 (while t
4312 (setq element
4313 (org-element--current-element end 'element special-flag struct)
4314 type (car element))
4315 (org-element-put-property element :parent parent)
4316 (when keep-trail (push element trail))
4317 (cond
4318 ;; 1. Skip any element ending before point. Also skip
4319 ;; element ending at point when we're sure that another
4320 ;; element has started.
4321 ((let ((elem-end (org-element-property :end element)))
4322 (when (or (< elem-end origin)
4323 (and (= elem-end origin) (/= elem-end end)))
4324 (goto-char elem-end))))
4325 ;; 2. An element containing point is always the element at
4326 ;; point.
4327 ((not (memq type org-element-greater-elements))
4328 (throw 'exit (if keep-trail trail element)))
4329 ;; 3. At any other greater element type, if point is
4330 ;; within contents, move into it.
4332 (let ((cbeg (org-element-property :contents-begin element))
4333 (cend (org-element-property :contents-end element)))
4334 (if (or (not cbeg) (not cend) (> cbeg origin) (< cend origin)
4335 ;; Create an anchor for tables and plain lists:
4336 ;; when point is at the very beginning of these
4337 ;; elements, ignoring affiliated keywords,
4338 ;; target them instead of their contents.
4339 (and (= cbeg origin) (memq type '(plain-list table)))
4340 ;; When point is at contents end, do not move
4341 ;; into elements with an explicit ending, but
4342 ;; return that element instead.
4343 (and (= cend origin)
4344 (memq type
4345 '(center-block
4346 drawer dynamic-block inlinetask item
4347 plain-list property-drawer quote-block
4348 special-block))))
4349 (throw 'exit (if keep-trail trail element))
4350 (setq parent element)
4351 (case type
4352 (plain-list
4353 (setq special-flag 'item
4354 struct (org-element-property :structure element)))
4355 (property-drawer (setq special-flag 'node-property))
4356 (table (setq special-flag 'table-row))
4357 (otherwise (setq special-flag nil)))
4358 (setq end cend)
4359 (goto-char cbeg)))))))))))
4361 ;;;###autoload
4362 (defun org-element-context ()
4363 "Return closest element or object around point.
4365 Return value is a list like (TYPE PROPS) where TYPE is the type
4366 of the element or object and PROPS a plist of properties
4367 associated to it.
4369 Possible types are defined in `org-element-all-elements' and
4370 `org-element-all-objects'. Properties depend on element or
4371 object type, but always include :begin, :end, :parent
4372 and :post-blank properties."
4373 (org-with-wide-buffer
4374 (let* ((origin (point))
4375 (element (org-element-at-point))
4376 (type (car element))
4377 end)
4378 ;; Check if point is inside an element containing objects or at
4379 ;; a secondary string. In that case, move to beginning of the
4380 ;; element or secondary string and set END to the other side.
4381 (if (not (or (and (eq type 'item)
4382 (let ((tag (org-element-property :tag element)))
4383 (and tag
4384 (progn
4385 (beginning-of-line)
4386 (search-forward tag (point-at-eol))
4387 (goto-char (match-beginning 0))
4388 (and (>= origin (point))
4389 (<= origin
4390 ;; `1+' is required so some
4391 ;; successors can match
4392 ;; properly their object.
4393 (setq end (1+ (match-end 0)))))))))
4394 (and (memq type '(headline inlinetask))
4395 (progn (beginning-of-line)
4396 (skip-chars-forward "* ")
4397 (setq end (point-at-eol))))
4398 (and (memq type '(paragraph table-row verse-block))
4399 (let ((cbeg (org-element-property
4400 :contents-begin element))
4401 (cend (org-element-property
4402 :contents-end element)))
4403 (and (>= origin cbeg)
4404 (<= origin cend)
4405 (progn (goto-char cbeg) (setq end cend)))))))
4406 element
4407 (let ((restriction (org-element-restriction element))
4408 (parent element)
4409 candidates)
4410 (catch 'exit
4411 (while (setq candidates (org-element--get-next-object-candidates
4412 end restriction candidates))
4413 (let ((closest-cand (rassq (apply 'min (mapcar 'cdr candidates))
4414 candidates)))
4415 ;; If ORIGIN is before next object in element, there's
4416 ;; no point in looking further.
4417 (if (> (cdr closest-cand) origin) (throw 'exit parent)
4418 (let* ((object
4419 (progn (goto-char (cdr closest-cand))
4420 (funcall (intern (format "org-element-%s-parser"
4421 (car closest-cand))))))
4422 (cbeg (org-element-property :contents-begin object))
4423 (cend (org-element-property :contents-end object)))
4424 (cond
4425 ;; ORIGIN is after OBJECT, so skip it.
4426 ((< (org-element-property :end object) origin)
4427 (goto-char (org-element-property :end object)))
4428 ;; ORIGIN is within a non-recursive object or at an
4429 ;; object boundaries: Return that object.
4430 ((or (not cbeg) (> cbeg origin) (< cend origin))
4431 (throw 'exit
4432 (org-element-put-property object :parent parent)))
4433 ;; Otherwise, move within current object and restrict
4434 ;; search to the end of its contents.
4435 (t (goto-char cbeg)
4436 (org-element-put-property object :parent parent)
4437 (setq parent object
4438 restriction (org-element-restriction object)
4439 end cend)))))))
4440 parent))))))
4442 (defsubst org-element-nested-p (elem-A elem-B)
4443 "Non-nil when elements ELEM-A and ELEM-B are nested."
4444 (let ((beg-A (org-element-property :begin elem-A))
4445 (beg-B (org-element-property :begin elem-B))
4446 (end-A (org-element-property :end elem-A))
4447 (end-B (org-element-property :end elem-B)))
4448 (or (and (>= beg-A beg-B) (<= end-A end-B))
4449 (and (>= beg-B beg-A) (<= end-B end-A)))))
4451 (defun org-element-swap-A-B (elem-A elem-B)
4452 "Swap elements ELEM-A and ELEM-B.
4453 Assume ELEM-B is after ELEM-A in the buffer. Leave point at the
4454 end of ELEM-A."
4455 (goto-char (org-element-property :begin elem-A))
4456 ;; There are two special cases when an element doesn't start at bol:
4457 ;; the first paragraph in an item or in a footnote definition.
4458 (let ((specialp (not (bolp))))
4459 ;; Only a paragraph without any affiliated keyword can be moved at
4460 ;; ELEM-A position in such a situation. Note that the case of
4461 ;; a footnote definition is impossible: it cannot contain two
4462 ;; paragraphs in a row because it cannot contain a blank line.
4463 (if (and specialp
4464 (or (not (eq (org-element-type elem-B) 'paragraph))
4465 (/= (org-element-property :begin elem-B)
4466 (org-element-property :contents-begin elem-B))))
4467 (error "Cannot swap elements"))
4468 ;; In a special situation, ELEM-A will have no indentation. We'll
4469 ;; give it ELEM-B's (which will in, in turn, have no indentation).
4470 (let* ((ind-B (when specialp
4471 (goto-char (org-element-property :begin elem-B))
4472 (org-get-indentation)))
4473 (beg-A (org-element-property :begin elem-A))
4474 (end-A (save-excursion
4475 (goto-char (org-element-property :end elem-A))
4476 (skip-chars-backward " \r\t\n")
4477 (point-at-eol)))
4478 (beg-B (org-element-property :begin elem-B))
4479 (end-B (save-excursion
4480 (goto-char (org-element-property :end elem-B))
4481 (skip-chars-backward " \r\t\n")
4482 (point-at-eol)))
4483 ;; Store overlays responsible for visibility status. We
4484 ;; also need to store their boundaries as they will be
4485 ;; removed from buffer.
4486 (overlays
4487 (cons
4488 (mapcar (lambda (ov) (list ov (overlay-start ov) (overlay-end ov)))
4489 (overlays-in beg-A end-A))
4490 (mapcar (lambda (ov) (list ov (overlay-start ov) (overlay-end ov)))
4491 (overlays-in beg-B end-B))))
4492 ;; Get contents.
4493 (body-A (buffer-substring beg-A end-A))
4494 (body-B (delete-and-extract-region beg-B end-B)))
4495 (goto-char beg-B)
4496 (when specialp
4497 (setq body-B (replace-regexp-in-string "\\`[ \t]*" "" body-B))
4498 (org-indent-to-column ind-B))
4499 (insert body-A)
4500 ;; Restore ex ELEM-A overlays.
4501 (let ((offset (- beg-B beg-A)))
4502 (mapc (lambda (ov)
4503 (move-overlay
4504 (car ov) (+ (nth 1 ov) offset) (+ (nth 2 ov) offset)))
4505 (car overlays))
4506 (goto-char beg-A)
4507 (delete-region beg-A end-A)
4508 (insert body-B)
4509 ;; Restore ex ELEM-B overlays.
4510 (mapc (lambda (ov)
4511 (move-overlay
4512 (car ov) (- (nth 1 ov) offset) (- (nth 2 ov) offset)))
4513 (cdr overlays)))
4514 (goto-char (org-element-property :end elem-B)))))
4516 (provide 'org-element)
4518 ;; Local variables:
4519 ;; generated-autoload-file: "org-loaddefs.el"
4520 ;; End:
4522 ;;; org-element.el ends here