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