org-element: Use `org-latex-regexps', not `org-format-latex-options'
[org-mode.git] / lisp / org-element.el
blob873c4bb7684fdd48436afe402093111d1e18592d
1 ;;; org-element.el --- Parser And Applications for Org syntax
3 ;; Copyright (C) 2012-2013 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', `diary-sexp', `example-block', `export-block',
50 ;; `fixed-width', `horizontal-rule', `keyword', `latex-environment',
51 ;; `node-property', `paragraph', `planning', `quote-section',
52 ;; `src-block', `table', `table-row' and `verse-block'. Among them,
53 ;; `paragraph' and `verse-block' types can contain Org objects and
54 ;; plain text.
56 ;; Objects are related to document's contents. Some of them are
57 ;; recursive. Associated types are of the following: `bold', `code',
58 ;; `entity', `export-snippet', `footnote-reference',
59 ;; `inline-babel-call', `inline-src-block', `italic',
60 ;; `latex-fragment', `line-break', `link', `macro', `radio-target',
61 ;; `statistics-cookie', `strike-through', `subscript', `superscript',
62 ;; `table-cell', `target', `timestamp', `underline' and `verbatim'.
64 ;; Some elements also have special properties whose value can hold
65 ;; objects themselves (i.e. an item tag or a headline name). Such
66 ;; values are called "secondary strings". Any object belongs to
67 ;; either an element or a secondary string.
69 ;; Notwithstanding affiliated keywords, each greater element, element
70 ;; and object has a fixed set of properties attached to it. Among
71 ;; them, four are shared by all types: `:begin' and `:end', which
72 ;; refer to the beginning and ending buffer positions of the
73 ;; considered element or object, `:post-blank', which holds the number
74 ;; of blank lines, or white spaces, at its end and `:parent' which
75 ;; refers to the element or object containing it. Greater elements,
76 ;; elements and objects containing objects will also have
77 ;; `:contents-begin' and `:contents-end' properties to delimit
78 ;; contents. Eventually, greater elements and elements accepting
79 ;; affiliated keywords will have a `:post-affiliated' property,
80 ;; referring to the buffer position after all such keywords.
82 ;; At the lowest level, a `:parent' property is also attached to any
83 ;; string, as a text property.
85 ;; Lisp-wise, an element or an object can be represented as a list.
86 ;; It follows the pattern (TYPE PROPERTIES CONTENTS), where:
87 ;; TYPE is a symbol describing the Org element or object.
88 ;; PROPERTIES is the property list attached to it. See docstring of
89 ;; appropriate parsing function to get an exhaustive
90 ;; list.
91 ;; CONTENTS is a list of elements, objects or raw strings contained
92 ;; in the current element or object, when applicable.
94 ;; An Org buffer is a nested list of such elements and objects, whose
95 ;; type is `org-data' and properties is nil.
97 ;; The first part of this file defines Org syntax, while the second
98 ;; one provide accessors and setters functions.
100 ;; The next part implements a parser and an interpreter for each
101 ;; element and object type in Org syntax.
103 ;; The following part creates a fully recursive buffer parser. It
104 ;; also provides a tool to map a function to elements or objects
105 ;; matching some criteria in the parse tree. Functions of interest
106 ;; are `org-element-parse-buffer', `org-element-map' and, to a lesser
107 ;; extent, `org-element-parse-secondary-string'.
109 ;; The penultimate part is the cradle of an interpreter for the
110 ;; obtained parse tree: `org-element-interpret-data'.
112 ;; The library ends by furnishing `org-element-at-point' function, and
113 ;; a way to give information about document structure around point
114 ;; with `org-element-context'.
117 ;;; Code:
119 (eval-when-compile (require 'cl))
120 (require 'org)
124 ;;; Definitions And Rules
126 ;; Define elements, greater elements and specify recursive objects,
127 ;; along with the affiliated keywords recognized. Also set up
128 ;; restrictions on recursive objects combinations.
130 ;; These variables really act as a control center for the parsing
131 ;; process.
133 (defconst org-element-paragraph-separate
134 (concat "^\\(?:"
135 ;; Headlines, inlinetasks.
136 org-outline-regexp "\\|"
137 ;; Footnote definitions.
138 "\\[\\(?:[0-9]+\\|fn:[-_[:word:]]+\\)\\]" "\\|"
139 ;; Diary sexps.
140 "%%(" "\\|"
141 "[ \t]*\\(?:"
142 ;; Empty lines.
143 "$" "\\|"
144 ;; Tables (any type).
145 "\\(?:|\\|\\+-[-+]\\)" "\\|"
146 ;; Blocks (any type), Babel calls, drawers (any type),
147 ;; fixed-width areas and keywords. Note: this is only an
148 ;; indication and need some thorough check.
149 "[#:]" "\\|"
150 ;; Horizontal rules.
151 "-\\{5,\\}[ \t]*$" "\\|"
152 ;; LaTeX environments.
153 "\\\\begin{\\([A-Za-z0-9]+\\*?\\)}" "\\|"
154 ;; Planning and Clock lines.
155 (regexp-opt (list org-scheduled-string
156 org-deadline-string
157 org-closed-string
158 org-clock-string))
159 "\\|"
160 ;; Lists.
161 (let ((term (case org-plain-list-ordered-item-terminator
162 (?\) ")") (?. "\\.") (otherwise "[.)]")))
163 (alpha (and org-list-allow-alphabetical "\\|[A-Za-z]")))
164 (concat "\\(?:[-+*]\\|\\(?:[0-9]+" alpha "\\)" term "\\)"
165 "\\(?:[ \t]\\|$\\)"))
166 "\\)\\)")
167 "Regexp to separate paragraphs in an Org buffer.
168 In the case of lines starting with \"#\" and \":\", this regexp
169 is not sufficient to know if point is at a paragraph ending. See
170 `org-element-paragraph-parser' for more information.")
172 (defconst org-element-all-elements
173 '(babel-call center-block clock comment comment-block diary-sexp drawer
174 dynamic-block example-block export-block fixed-width
175 footnote-definition headline horizontal-rule inlinetask item
176 keyword latex-environment node-property paragraph plain-list
177 planning property-drawer quote-block quote-section section
178 special-block src-block table table-row verse-block)
179 "Complete list of element types.")
181 (defconst org-element-greater-elements
182 '(center-block drawer dynamic-block footnote-definition headline inlinetask
183 item plain-list property-drawer quote-block section
184 special-block table)
185 "List of recursive element types aka Greater Elements.")
187 (defconst org-element-all-successors
188 '(export-snippet footnote-reference inline-babel-call inline-src-block
189 latex-or-entity line-break link macro plain-link radio-target
190 statistics-cookie sub/superscript table-cell target
191 text-markup timestamp)
192 "Complete list of successors.")
194 (defconst org-element-object-successor-alist
195 '((subscript . sub/superscript) (superscript . sub/superscript)
196 (bold . text-markup) (code . text-markup) (italic . text-markup)
197 (strike-through . text-markup) (underline . text-markup)
198 (verbatim . text-markup) (entity . latex-or-entity)
199 (latex-fragment . latex-or-entity))
200 "Alist of translations between object type and successor name.
201 Sharing the same successor comes handy when, for example, the
202 regexp matching one object can also match the other object.")
204 (defconst org-element-all-objects
205 '(bold code entity export-snippet footnote-reference inline-babel-call
206 inline-src-block italic line-break latex-fragment link macro
207 radio-target statistics-cookie strike-through subscript superscript
208 table-cell target timestamp underline verbatim)
209 "Complete list of object types.")
211 (defconst org-element-recursive-objects
212 '(bold italic link subscript radio-target strike-through superscript
213 table-cell underline)
214 "List of recursive object types.")
216 (defvar org-element-block-name-alist
217 '(("CENTER" . org-element-center-block-parser)
218 ("COMMENT" . org-element-comment-block-parser)
219 ("EXAMPLE" . org-element-example-block-parser)
220 ("QUOTE" . org-element-quote-block-parser)
221 ("SRC" . org-element-src-block-parser)
222 ("VERSE" . org-element-verse-block-parser))
223 "Alist between block names and the associated parsing function.
224 Names must be uppercase. Any block whose name has no association
225 is parsed with `org-element-special-block-parser'.")
227 (defconst org-element-link-type-is-file
228 '("file" "file+emacs" "file+sys" "docview")
229 "List of link types equivalent to \"file\".
230 Only these types can accept search options and an explicit
231 application to open them.")
233 (defconst org-element-affiliated-keywords
234 '("CAPTION" "DATA" "HEADER" "HEADERS" "LABEL" "NAME" "PLOT" "RESNAME" "RESULT"
235 "RESULTS" "SOURCE" "SRCNAME" "TBLNAME")
236 "List of affiliated keywords as strings.
237 By default, all keywords setting attributes (i.e. \"ATTR_LATEX\")
238 are affiliated keywords and need not to be in this list.")
240 (defconst org-element--affiliated-re
241 (format "[ \t]*#\\+%s:"
242 ;; Regular affiliated keywords.
243 (format "\\(%s\\|ATTR_[-_A-Za-z0-9]+\\)\\(?:\\[\\(.*\\)\\]\\)?"
244 (regexp-opt org-element-affiliated-keywords)))
245 "Regexp matching any affiliated keyword.
247 Keyword name is put in match group 1. Moreover, if keyword
248 belongs to `org-element-dual-keywords', put the dual value in
249 match group 2.
251 Don't modify it, set `org-element-affiliated-keywords' instead.")
253 (defconst org-element-keyword-translation-alist
254 '(("DATA" . "NAME") ("LABEL" . "NAME") ("RESNAME" . "NAME")
255 ("SOURCE" . "NAME") ("SRCNAME" . "NAME") ("TBLNAME" . "NAME")
256 ("RESULT" . "RESULTS") ("HEADERS" . "HEADER"))
257 "Alist of usual translations for keywords.
258 The key is the old name and the value the new one. The property
259 holding their value will be named after the translated name.")
261 (defconst org-element-multiple-keywords '("CAPTION" "HEADER")
262 "List of affiliated keywords that can occur more than once in an element.
264 Their value will be consed into a list of strings, which will be
265 returned as the value of the property.
267 This list is checked after translations have been applied. See
268 `org-element-keyword-translation-alist'.
270 By default, all keywords setting attributes (i.e. \"ATTR_LATEX\")
271 allow multiple occurrences and need not to be in this list.")
273 (defconst org-element-parsed-keywords '("CAPTION")
274 "List of affiliated keywords whose value can be parsed.
276 Their value will be stored as a secondary string: a list of
277 strings and objects.
279 This list is checked after translations have been applied. See
280 `org-element-keyword-translation-alist'.")
282 (defconst org-element-dual-keywords '("CAPTION" "RESULTS")
283 "List of affiliated keywords which can have a secondary value.
285 In Org syntax, they can be written with optional square brackets
286 before the colons. For example, RESULTS keyword can be
287 associated to a hash value with the following:
289 #+RESULTS[hash-string]: some-source
291 This list is checked after translations have been applied. See
292 `org-element-keyword-translation-alist'.")
294 (defconst org-element-document-properties '("AUTHOR" "DATE" "TITLE")
295 "List of properties associated to the whole document.
296 Any keyword in this list will have its value parsed and stored as
297 a secondary string.")
299 (defconst org-element-object-restrictions
300 (let* ((standard-set
301 (remq 'plain-link (remq 'table-cell org-element-all-successors)))
302 (standard-set-no-line-break (remq 'line-break standard-set)))
303 `((bold ,@standard-set)
304 (footnote-reference ,@standard-set)
305 (headline ,@standard-set-no-line-break)
306 (inlinetask ,@standard-set-no-line-break)
307 (italic ,@standard-set)
308 (item ,@standard-set-no-line-break)
309 (keyword ,@standard-set)
310 ;; Ignore all links excepted plain links in a link description.
311 ;; Also ignore radio-targets and line breaks.
312 (link export-snippet inline-babel-call inline-src-block latex-or-entity
313 macro plain-link statistics-cookie sub/superscript text-markup)
314 (paragraph ,@standard-set)
315 ;; Remove any variable object from radio target as it would
316 ;; prevent it from being properly recognized.
317 (radio-target latex-or-entity sub/superscript)
318 (strike-through ,@standard-set)
319 (subscript ,@standard-set)
320 (superscript ,@standard-set)
321 ;; Ignore inline babel call and inline src block as formulas are
322 ;; possible. Also ignore line breaks and statistics cookies.
323 (table-cell export-snippet footnote-reference latex-or-entity link macro
324 radio-target sub/superscript target text-markup timestamp)
325 (table-row table-cell)
326 (underline ,@standard-set)
327 (verse-block ,@standard-set)))
328 "Alist of objects restrictions.
330 CAR is an element or object type containing objects and CDR is
331 a list of successors that will be called within an element or
332 object of such type.
334 For example, in a `radio-target' object, one can only find
335 entities, latex-fragments, subscript and superscript.
337 This alist also applies to secondary string. For example, an
338 `headline' type element doesn't directly contain objects, but
339 still has an entry since one of its properties (`:title') does.")
341 (defconst org-element-secondary-value-alist
342 '((headline . :title)
343 (inlinetask . :title)
344 (item . :tag)
345 (footnote-reference . :inline-definition))
346 "Alist between element types and location of secondary value.")
348 (defconst org-element-object-variables '(org-link-abbrev-alist-local)
349 "List of buffer-local variables used when parsing objects.
350 These variables are copied to the temporary buffer created by
351 `org-export-secondary-string'.")
355 ;;; Accessors and Setters
357 ;; Provide four accessors: `org-element-type', `org-element-property'
358 ;; `org-element-contents' and `org-element-restriction'.
360 ;; Setter functions allow to modify elements by side effect. There is
361 ;; `org-element-put-property', `org-element-set-contents',
362 ;; `org-element-set-element' and `org-element-adopt-element'. Note
363 ;; that `org-element-set-element' and `org-element-adopt-elements' are
364 ;; higher level functions since also update `:parent' property.
366 (defsubst org-element-type (element)
367 "Return type of ELEMENT.
369 The function returns the type of the element or object provided.
370 It can also return the following special value:
371 `plain-text' for a string
372 `org-data' for a complete document
373 nil in any other case."
374 (cond
375 ((not (consp element)) (and (stringp element) 'plain-text))
376 ((symbolp (car element)) (car element))))
378 (defsubst org-element-property (property element)
379 "Extract the value from the PROPERTY of an ELEMENT."
380 (if (stringp element) (get-text-property 0 property element)
381 (plist-get (nth 1 element) property)))
383 (defsubst org-element-contents (element)
384 "Extract contents from an ELEMENT."
385 (cond ((not (consp element)) nil)
386 ((symbolp (car element)) (nthcdr 2 element))
387 (t element)))
389 (defsubst org-element-restriction (element)
390 "Return restriction associated to ELEMENT.
391 ELEMENT can be an element, an object or a symbol representing an
392 element or object type."
393 (cdr (assq (if (symbolp element) element (org-element-type element))
394 org-element-object-restrictions)))
396 (defsubst org-element-put-property (element property value)
397 "In ELEMENT set PROPERTY to VALUE.
398 Return modified element."
399 (if (stringp element) (org-add-props element nil property value)
400 (setcar (cdr element) (plist-put (nth 1 element) property value))
401 element))
403 (defsubst org-element-set-contents (element &rest contents)
404 "Set ELEMENT contents to CONTENTS.
405 Return modified element."
406 (cond ((not element) (list contents))
407 ((not (symbolp (car element))) contents)
408 ((cdr element) (setcdr (cdr element) contents))
409 (t (nconc element contents))))
411 (defsubst org-element-set-element (old new)
412 "Replace element or object OLD with element or object NEW.
413 The function takes care of setting `:parent' property for NEW."
414 ;; Since OLD is going to be changed into NEW by side-effect, first
415 ;; make sure that every element or object within NEW has OLD as
416 ;; parent.
417 (mapc (lambda (blob) (org-element-put-property blob :parent old))
418 (org-element-contents new))
419 ;; Transfer contents.
420 (apply 'org-element-set-contents old (org-element-contents new))
421 ;; Ensure NEW has same parent as OLD, then overwrite OLD properties
422 ;; with NEW's.
423 (org-element-put-property new :parent (org-element-property :parent old))
424 (setcar (cdr old) (nth 1 new))
425 ;; Transfer type.
426 (setcar old (car new)))
428 (defsubst org-element-adopt-elements (parent &rest children)
429 "Append elements to the contents of another element.
431 PARENT is an element or object. CHILDREN can be elements,
432 objects, or a strings.
434 The function takes care of setting `:parent' property for CHILD.
435 Return parent element."
436 ;; Link every child to PARENT. If PARENT is nil, it is a secondary
437 ;; string: parent is the list itself.
438 (mapc (lambda (child)
439 (org-element-put-property child :parent (or parent children)))
440 children)
441 ;; Add CHILDREN at the end of PARENT contents.
442 (when parent
443 (apply 'org-element-set-contents
444 parent
445 (nconc (org-element-contents parent) children)))
446 ;; Return modified PARENT element.
447 (or parent children))
451 ;;; Greater elements
453 ;; For each greater element type, we define a parser and an
454 ;; interpreter.
456 ;; A parser returns the element or object as the list described above.
457 ;; Most of them accepts no argument. Though, exceptions exist. Hence
458 ;; every element containing a secondary string (see
459 ;; `org-element-secondary-value-alist') will accept an optional
460 ;; argument to toggle parsing of that secondary string. Moreover,
461 ;; `item' parser requires current list's structure as its first
462 ;; element.
464 ;; An interpreter accepts two arguments: the list representation of
465 ;; the element or object, and its contents. The latter may be nil,
466 ;; depending on the element or object considered. It returns the
467 ;; appropriate Org syntax, as a string.
469 ;; Parsing functions must follow the naming convention:
470 ;; org-element-TYPE-parser, where TYPE is greater element's type, as
471 ;; defined in `org-element-greater-elements'.
473 ;; Similarly, interpreting functions must follow the naming
474 ;; convention: org-element-TYPE-interpreter.
476 ;; With the exception of `headline' and `item' types, greater elements
477 ;; cannot contain other greater elements of their own type.
479 ;; Beside implementing a parser and an interpreter, adding a new
480 ;; greater element requires to tweak `org-element--current-element'.
481 ;; Moreover, the newly defined type must be added to both
482 ;; `org-element-all-elements' and `org-element-greater-elements'.
485 ;;;; Center Block
487 (defun org-element-center-block-parser (limit affiliated)
488 "Parse a center block.
490 LIMIT bounds the search. AFFILIATED is a list of which CAR is
491 the buffer position at the beginning of the first affiliated
492 keyword and CDR is a plist of affiliated keywords along with
493 their value.
495 Return a list whose CAR is `center-block' and CDR is a plist
496 containing `:begin', `:end', `:hiddenp', `:contents-begin',
497 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
499 Assume point is at the beginning of the block."
500 (let ((case-fold-search t))
501 (if (not (save-excursion
502 (re-search-forward "^[ \t]*#\\+END_CENTER[ \t]*$" limit t)))
503 ;; Incomplete block: parse it as a paragraph.
504 (org-element-paragraph-parser limit affiliated)
505 (let ((block-end-line (match-beginning 0)))
506 (let* ((begin (car affiliated))
507 (post-affiliated (point))
508 ;; Empty blocks have no contents.
509 (contents-begin (progn (forward-line)
510 (and (< (point) block-end-line)
511 (point))))
512 (contents-end (and contents-begin block-end-line))
513 (hidden (org-invisible-p2))
514 (pos-before-blank (progn (goto-char block-end-line)
515 (forward-line)
516 (point)))
517 (end (save-excursion (skip-chars-forward " \r\t\n" limit)
518 (skip-chars-backward " \t")
519 (if (bolp) (point) (line-end-position)))))
520 (list 'center-block
521 (nconc
522 (list :begin begin
523 :end end
524 :hiddenp hidden
525 :contents-begin contents-begin
526 :contents-end contents-end
527 :post-blank (count-lines pos-before-blank end)
528 :post-affiliated post-affiliated)
529 (cdr affiliated))))))))
531 (defun org-element-center-block-interpreter (center-block contents)
532 "Interpret CENTER-BLOCK element as Org syntax.
533 CONTENTS is the contents of the element."
534 (format "#+BEGIN_CENTER\n%s#+END_CENTER" contents))
537 ;;;; Drawer
539 (defun org-element-drawer-parser (limit affiliated)
540 "Parse a drawer.
542 LIMIT bounds the search. AFFILIATED is a list of which CAR is
543 the buffer position at the beginning of the first affiliated
544 keyword and CDR is a plist of affiliated keywords along with
545 their value.
547 Return a list whose CAR is `drawer' and CDR is a plist containing
548 `:drawer-name', `:begin', `:end', `:hiddenp', `:contents-begin',
549 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
551 Assume point is at beginning of drawer."
552 (let ((case-fold-search t))
553 (if (not (save-excursion (re-search-forward "^[ \t]*:END:[ \t]*$" limit t)))
554 ;; Incomplete drawer: parse it as a paragraph.
555 (org-element-paragraph-parser limit affiliated)
556 (save-excursion
557 (let* ((drawer-end-line (match-beginning 0))
558 (name (progn (looking-at org-drawer-regexp)
559 (org-match-string-no-properties 1)))
560 (begin (car affiliated))
561 (post-affiliated (point))
562 ;; Empty drawers have no contents.
563 (contents-begin (progn (forward-line)
564 (and (< (point) drawer-end-line)
565 (point))))
566 (contents-end (and contents-begin drawer-end-line))
567 (hidden (org-invisible-p2))
568 (pos-before-blank (progn (goto-char drawer-end-line)
569 (forward-line)
570 (point)))
571 (end (progn (skip-chars-forward " \r\t\n" limit)
572 (skip-chars-backward " \t")
573 (if (bolp) (point) (line-end-position)))))
574 (list 'drawer
575 (nconc
576 (list :begin begin
577 :end end
578 :drawer-name name
579 :hiddenp hidden
580 :contents-begin contents-begin
581 :contents-end contents-end
582 :post-blank (count-lines pos-before-blank end)
583 :post-affiliated post-affiliated)
584 (cdr affiliated))))))))
586 (defun org-element-drawer-interpreter (drawer contents)
587 "Interpret DRAWER element as Org syntax.
588 CONTENTS is the contents of the element."
589 (format ":%s:\n%s:END:"
590 (org-element-property :drawer-name drawer)
591 contents))
594 ;;;; Dynamic Block
596 (defun org-element-dynamic-block-parser (limit affiliated)
597 "Parse a dynamic block.
599 LIMIT bounds the search. AFFILIATED is a list of which CAR is
600 the buffer position at the beginning of the first affiliated
601 keyword and CDR is a plist of affiliated keywords along with
602 their value.
604 Return a list whose CAR is `dynamic-block' and CDR is a plist
605 containing `:block-name', `:begin', `:end', `:hiddenp',
606 `:contents-begin', `:contents-end', `:arguments', `:post-blank'
607 and `:post-affiliated' keywords.
609 Assume point is at beginning of dynamic block."
610 (let ((case-fold-search t))
611 (if (not (save-excursion
612 (re-search-forward "^[ \t]*#\\+END:?[ \t]*$" limit t)))
613 ;; Incomplete block: parse it as a paragraph.
614 (org-element-paragraph-parser limit affiliated)
615 (let ((block-end-line (match-beginning 0)))
616 (save-excursion
617 (let* ((name (progn (looking-at org-dblock-start-re)
618 (org-match-string-no-properties 1)))
619 (arguments (org-match-string-no-properties 3))
620 (begin (car affiliated))
621 (post-affiliated (point))
622 ;; Empty blocks have no contents.
623 (contents-begin (progn (forward-line)
624 (and (< (point) block-end-line)
625 (point))))
626 (contents-end (and contents-begin block-end-line))
627 (hidden (org-invisible-p2))
628 (pos-before-blank (progn (goto-char block-end-line)
629 (forward-line)
630 (point)))
631 (end (progn (skip-chars-forward " \r\t\n" limit)
632 (skip-chars-backward " \t")
633 (if (bolp) (point) (line-end-position)))))
634 (list 'dynamic-block
635 (nconc
636 (list :begin begin
637 :end end
638 :block-name name
639 :arguments arguments
640 :hiddenp hidden
641 :contents-begin contents-begin
642 :contents-end contents-end
643 :post-blank (count-lines pos-before-blank end)
644 :post-affiliated post-affiliated)
645 (cdr affiliated)))))))))
647 (defun org-element-dynamic-block-interpreter (dynamic-block contents)
648 "Interpret DYNAMIC-BLOCK element as Org syntax.
649 CONTENTS is the contents of the element."
650 (format "#+BEGIN: %s%s\n%s#+END:"
651 (org-element-property :block-name dynamic-block)
652 (let ((args (org-element-property :arguments dynamic-block)))
653 (and args (concat " " args)))
654 contents))
657 ;;;; Footnote Definition
659 (defun org-element-footnote-definition-parser (limit affiliated)
660 "Parse a footnote definition.
662 LIMIT bounds the search. AFFILIATED is a list of which CAR is
663 the buffer position at the beginning of the first affiliated
664 keyword and CDR is a plist of affiliated keywords along with
665 their value.
667 Return a list whose CAR is `footnote-definition' and CDR is
668 a plist containing `:label', `:begin' `:end', `:contents-begin',
669 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
671 Assume point is at the beginning of the footnote definition."
672 (save-excursion
673 (let* ((label (progn (looking-at org-footnote-definition-re)
674 (org-match-string-no-properties 1)))
675 (begin (car affiliated))
676 (post-affiliated (point))
677 (ending (save-excursion
678 (if (progn
679 (end-of-line)
680 (re-search-forward
681 (concat org-outline-regexp-bol "\\|"
682 org-footnote-definition-re "\\|"
683 "^\\([ \t]*\n\\)\\{2,\\}") limit 'move))
684 (match-beginning 0)
685 (point))))
686 (contents-begin (progn
687 (search-forward "]")
688 (skip-chars-forward " \r\t\n" ending)
689 (cond ((= (point) ending) nil)
690 ((= (line-beginning-position) begin) (point))
691 (t (line-beginning-position)))))
692 (contents-end (and contents-begin ending))
693 (end (progn (goto-char ending)
694 (skip-chars-forward " \r\t\n" limit)
695 (skip-chars-backward " \t")
696 (if (bolp) (point) (line-end-position)))))
697 (list 'footnote-definition
698 (nconc
699 (list :label label
700 :begin begin
701 :end end
702 :contents-begin contents-begin
703 :contents-end contents-end
704 :post-blank (count-lines ending end)
705 :post-affiliated post-affiliated)
706 (cdr affiliated))))))
708 (defun org-element-footnote-definition-interpreter (footnote-definition contents)
709 "Interpret FOOTNOTE-DEFINITION element as Org syntax.
710 CONTENTS is the contents of the footnote-definition."
711 (concat (format "[%s]" (org-element-property :label footnote-definition))
713 contents))
716 ;;;; Headline
718 (defun org-element-headline-parser (limit &optional raw-secondary-p)
719 "Parse a headline.
721 Return a list whose CAR is `headline' and CDR is a plist
722 containing `:raw-value', `:title', `:alt-title', `:begin',
723 `:end', `:pre-blank', `:hiddenp', `:contents-begin' and
724 `:contents-end', `:level', `:priority', `:tags',
725 `:todo-keyword',`:todo-type', `:scheduled', `:deadline',
726 `:closed', `:quotedp', `:archivedp', `:commentedp' and
727 `:footnote-section-p' keywords.
729 The plist also contains any property set in the property drawer,
730 with its name in upper cases and colons added at the
731 beginning (i.e. `:CUSTOM_ID').
733 When RAW-SECONDARY-P is non-nil, headline's title will not be
734 parsed as a secondary string, but as a plain string instead.
736 Assume point is at beginning of the headline."
737 (save-excursion
738 (let* ((components (org-heading-components))
739 (level (nth 1 components))
740 (todo (nth 2 components))
741 (todo-type
742 (and todo (if (member todo org-done-keywords) 'done 'todo)))
743 (tags (let ((raw-tags (nth 5 components)))
744 (and raw-tags (org-split-string raw-tags ":"))))
745 (raw-value (or (nth 4 components) ""))
746 (quotedp
747 (let ((case-fold-search nil))
748 (string-match (format "^%s\\( \\|$\\)" org-quote-string)
749 raw-value)))
750 (commentedp
751 (let ((case-fold-search nil))
752 (string-match (format "^%s\\( \\|$\\)" org-comment-string)
753 raw-value)))
754 (archivedp (member org-archive-tag tags))
755 (footnote-section-p (and org-footnote-section
756 (string= org-footnote-section raw-value)))
757 ;; Upcase property names. It avoids confusion between
758 ;; properties obtained through property drawer and default
759 ;; properties from the parser (e.g. `:end' and :END:)
760 (standard-props
761 (let (plist)
762 (mapc
763 (lambda (p)
764 (setq plist
765 (plist-put plist
766 (intern (concat ":" (upcase (car p))))
767 (cdr p))))
768 (org-entry-properties nil 'standard))
769 plist))
770 (time-props
771 ;; Read time properties on the line below the headline.
772 (save-excursion
773 (when (progn (forward-line)
774 (looking-at org-planning-or-clock-line-re))
775 (let ((end (line-end-position)) plist)
776 (while (re-search-forward
777 org-keyword-time-not-clock-regexp end t)
778 (goto-char (match-end 1))
779 (skip-chars-forward " \t")
780 (let ((keyword (match-string 1))
781 (time (org-element-timestamp-parser)))
782 (cond ((equal keyword org-scheduled-string)
783 (setq plist (plist-put plist :scheduled time)))
784 ((equal keyword org-deadline-string)
785 (setq plist (plist-put plist :deadline time)))
786 (t (setq plist (plist-put plist :closed time))))))
787 plist))))
788 (begin (point))
789 (end (save-excursion (goto-char (org-end-of-subtree t t))))
790 (pos-after-head (progn (forward-line) (point)))
791 (contents-begin (save-excursion
792 (skip-chars-forward " \r\t\n" end)
793 (and (/= (point) end) (line-beginning-position))))
794 (hidden (org-invisible-p2))
795 (contents-end (and contents-begin
796 (progn (goto-char end)
797 (skip-chars-backward " \r\t\n")
798 (forward-line)
799 (point)))))
800 ;; Clean RAW-VALUE from any quote or comment string.
801 (when (or quotedp commentedp)
802 (let ((case-fold-search nil))
803 (setq raw-value
804 (replace-regexp-in-string
805 (concat
806 (regexp-opt (list org-quote-string org-comment-string))
807 "\\(?: \\|$\\)")
809 raw-value))))
810 ;; Clean TAGS from archive tag, if any.
811 (when archivedp (setq tags (delete org-archive-tag tags)))
812 (let ((headline
813 (list 'headline
814 (nconc
815 (list :raw-value raw-value
816 :begin begin
817 :end end
818 :pre-blank
819 (if (not contents-begin) 0
820 (count-lines pos-after-head contents-begin))
821 :hiddenp hidden
822 :contents-begin contents-begin
823 :contents-end contents-end
824 :level level
825 :priority (nth 3 components)
826 :tags tags
827 :todo-keyword todo
828 :todo-type todo-type
829 :post-blank (count-lines
830 (if (not contents-end) pos-after-head
831 (goto-char contents-end)
832 (forward-line)
833 (point))
834 end)
835 :footnote-section-p footnote-section-p
836 :archivedp archivedp
837 :commentedp commentedp
838 :quotedp quotedp)
839 time-props
840 standard-props))))
841 (let ((alt-title (org-element-property :ALT_TITLE headline)))
842 (when alt-title
843 (org-element-put-property
844 headline :alt-title
845 (if raw-secondary-p alt-title
846 (org-element-parse-secondary-string
847 alt-title (org-element-restriction 'headline) headline)))))
848 (org-element-put-property
849 headline :title
850 (if raw-secondary-p raw-value
851 (org-element-parse-secondary-string
852 raw-value (org-element-restriction 'headline) headline)))))))
854 (defun org-element-headline-interpreter (headline contents)
855 "Interpret HEADLINE element as Org syntax.
856 CONTENTS is the contents of the element."
857 (let* ((level (org-element-property :level headline))
858 (todo (org-element-property :todo-keyword headline))
859 (priority (org-element-property :priority headline))
860 (title (org-element-interpret-data
861 (org-element-property :title headline)))
862 (tags (let ((tag-list (if (org-element-property :archivedp headline)
863 (cons org-archive-tag
864 (org-element-property :tags headline))
865 (org-element-property :tags headline))))
866 (and tag-list
867 (format ":%s:" (mapconcat 'identity tag-list ":")))))
868 (commentedp (org-element-property :commentedp headline))
869 (quotedp (org-element-property :quotedp headline))
870 (pre-blank (or (org-element-property :pre-blank headline) 0))
871 (heading (concat (make-string level ?*)
872 (and todo (concat " " todo))
873 (and quotedp (concat " " org-quote-string))
874 (and commentedp (concat " " org-comment-string))
875 (and priority
876 (format " [#%s]" (char-to-string priority)))
877 (cond ((and org-footnote-section
878 (org-element-property
879 :footnote-section-p headline))
880 (concat " " org-footnote-section))
881 (title (concat " " title))))))
882 (concat heading
883 ;; Align tags.
884 (when tags
885 (cond
886 ((zerop org-tags-column) (format " %s" tags))
887 ((< org-tags-column 0)
888 (concat
889 (make-string
890 (max (- (+ org-tags-column (length heading) (length tags))) 1)
892 tags))
894 (concat
895 (make-string (max (- org-tags-column (length heading)) 1) ? )
896 tags))))
897 (make-string (1+ pre-blank) 10)
898 contents)))
901 ;;;; Inlinetask
903 (defun org-element-inlinetask-parser (limit &optional raw-secondary-p)
904 "Parse an inline task.
906 Return a list whose CAR is `inlinetask' and CDR is a plist
907 containing `:title', `:begin', `:end', `:hiddenp',
908 `:contents-begin' and `:contents-end', `:level', `:priority',
909 `:raw-value', `:tags', `:todo-keyword', `:todo-type',
910 `:scheduled', `:deadline', `:closed' and `:post-blank' keywords.
912 The plist also contains any property set in the property drawer,
913 with its name in upper cases and colons added at the
914 beginning (i.e. `:CUSTOM_ID').
916 When optional argument RAW-SECONDARY-P is non-nil, inline-task's
917 title will not be parsed as a secondary string, but as a plain
918 string instead.
920 Assume point is at beginning of the inline task."
921 (save-excursion
922 (let* ((begin (point))
923 (components (org-heading-components))
924 (todo (nth 2 components))
925 (todo-type (and todo
926 (if (member todo org-done-keywords) 'done 'todo)))
927 (tags (let ((raw-tags (nth 5 components)))
928 (and raw-tags (org-split-string raw-tags ":"))))
929 (raw-value (or (nth 4 components) ""))
930 ;; Upcase property names. It avoids confusion between
931 ;; properties obtained through property drawer and default
932 ;; properties from the parser (e.g. `:end' and :END:)
933 (standard-props
934 (let (plist)
935 (mapc
936 (lambda (p)
937 (setq plist
938 (plist-put plist
939 (intern (concat ":" (upcase (car p))))
940 (cdr p))))
941 (org-entry-properties nil 'standard))
942 plist))
943 (time-props
944 ;; Read time properties on the line below the inlinetask
945 ;; opening string.
946 (save-excursion
947 (when (progn (forward-line)
948 (looking-at org-planning-or-clock-line-re))
949 (let ((end (line-end-position)) plist)
950 (while (re-search-forward
951 org-keyword-time-not-clock-regexp end t)
952 (goto-char (match-end 1))
953 (skip-chars-forward " \t")
954 (let ((keyword (match-string 1))
955 (time (org-element-timestamp-parser)))
956 (cond ((equal keyword org-scheduled-string)
957 (setq plist (plist-put plist :scheduled time)))
958 ((equal keyword org-deadline-string)
959 (setq plist (plist-put plist :deadline time)))
960 (t (setq plist (plist-put plist :closed time))))))
961 plist))))
962 (task-end (save-excursion
963 (end-of-line)
964 (and (re-search-forward "^\\*+ END" limit t)
965 (match-beginning 0))))
966 (contents-begin (progn (forward-line)
967 (and task-end (< (point) task-end) (point))))
968 (hidden (and contents-begin (org-invisible-p2)))
969 (contents-end (and contents-begin task-end))
970 (before-blank (if (not task-end) (point)
971 (goto-char task-end)
972 (forward-line)
973 (point)))
974 (end (progn (skip-chars-forward " \r\t\n" limit)
975 (skip-chars-backward " \t")
976 (if (bolp) (point) (line-end-position))))
977 (inlinetask
978 (list 'inlinetask
979 (nconc
980 (list :raw-value raw-value
981 :begin begin
982 :end end
983 :hiddenp hidden
984 :contents-begin contents-begin
985 :contents-end contents-end
986 :level (nth 1 components)
987 :priority (nth 3 components)
988 :tags tags
989 :todo-keyword todo
990 :todo-type todo-type
991 :post-blank (count-lines before-blank end))
992 time-props
993 standard-props))))
994 (org-element-put-property
995 inlinetask :title
996 (if raw-secondary-p raw-value
997 (org-element-parse-secondary-string
998 raw-value
999 (org-element-restriction 'inlinetask)
1000 inlinetask))))))
1002 (defun org-element-inlinetask-interpreter (inlinetask contents)
1003 "Interpret INLINETASK element as Org syntax.
1004 CONTENTS is the contents of inlinetask."
1005 (let* ((level (org-element-property :level inlinetask))
1006 (todo (org-element-property :todo-keyword inlinetask))
1007 (priority (org-element-property :priority inlinetask))
1008 (title (org-element-interpret-data
1009 (org-element-property :title inlinetask)))
1010 (tags (let ((tag-list (org-element-property :tags inlinetask)))
1011 (and tag-list
1012 (format ":%s:" (mapconcat 'identity tag-list ":")))))
1013 (task (concat (make-string level ?*)
1014 (and todo (concat " " todo))
1015 (and priority
1016 (format " [#%s]" (char-to-string priority)))
1017 (and title (concat " " title)))))
1018 (concat task
1019 ;; Align tags.
1020 (when tags
1021 (cond
1022 ((zerop org-tags-column) (format " %s" tags))
1023 ((< org-tags-column 0)
1024 (concat
1025 (make-string
1026 (max (- (+ org-tags-column (length task) (length tags))) 1)
1028 tags))
1030 (concat
1031 (make-string (max (- org-tags-column (length task)) 1) ? )
1032 tags))))
1033 ;; Prefer degenerate inlinetasks when there are no
1034 ;; contents.
1035 (when contents
1036 (concat "\n"
1037 contents
1038 (make-string level ?*) " END")))))
1041 ;;;; Item
1043 (defun org-element-item-parser (limit struct &optional raw-secondary-p)
1044 "Parse an item.
1046 STRUCT is the structure of the plain list.
1048 Return a list whose CAR is `item' and CDR is a plist containing
1049 `:bullet', `:begin', `:end', `:contents-begin', `:contents-end',
1050 `:checkbox', `:counter', `:tag', `:structure', `:hiddenp' and
1051 `:post-blank' keywords.
1053 When optional argument RAW-SECONDARY-P is non-nil, item's tag, if
1054 any, will not be parsed as a secondary string, but as a plain
1055 string instead.
1057 Assume point is at the beginning of the item."
1058 (save-excursion
1059 (beginning-of-line)
1060 (looking-at org-list-full-item-re)
1061 (let* ((begin (point))
1062 (bullet (org-match-string-no-properties 1))
1063 (checkbox (let ((box (org-match-string-no-properties 3)))
1064 (cond ((equal "[ ]" box) 'off)
1065 ((equal "[X]" box) 'on)
1066 ((equal "[-]" box) 'trans))))
1067 (counter (let ((c (org-match-string-no-properties 2)))
1068 (save-match-data
1069 (cond
1070 ((not c) nil)
1071 ((string-match "[A-Za-z]" c)
1072 (- (string-to-char (upcase (match-string 0 c)))
1073 64))
1074 ((string-match "[0-9]+" c)
1075 (string-to-number (match-string 0 c)))))))
1076 (end (save-excursion (goto-char (org-list-get-item-end begin struct))
1077 (unless (bolp) (forward-line))
1078 (point)))
1079 (contents-begin
1080 (progn (goto-char
1081 ;; Ignore tags in un-ordered lists: they are just
1082 ;; a part of item's body.
1083 (if (and (match-beginning 4)
1084 (save-match-data (string-match "[.)]" bullet)))
1085 (match-beginning 4)
1086 (match-end 0)))
1087 (skip-chars-forward " \r\t\n" limit)
1088 ;; If first line isn't empty, contents really start
1089 ;; at the text after item's meta-data.
1090 (if (= (point-at-bol) begin) (point) (point-at-bol))))
1091 (hidden (progn (forward-line)
1092 (and (not (= (point) end)) (org-invisible-p2))))
1093 (contents-end (progn (goto-char end)
1094 (skip-chars-backward " \r\t\n")
1095 (forward-line)
1096 (point)))
1097 (item
1098 (list 'item
1099 (list :bullet bullet
1100 :begin begin
1101 :end end
1102 ;; CONTENTS-BEGIN and CONTENTS-END may be
1103 ;; mixed up in the case of an empty item
1104 ;; separated from the next by a blank line.
1105 ;; Thus ensure the former is always the
1106 ;; smallest.
1107 :contents-begin (min contents-begin contents-end)
1108 :contents-end (max contents-begin contents-end)
1109 :checkbox checkbox
1110 :counter counter
1111 :hiddenp hidden
1112 :structure struct
1113 :post-blank (count-lines contents-end end)))))
1114 (org-element-put-property
1115 item :tag
1116 (let ((raw-tag (org-list-get-tag begin struct)))
1117 (and raw-tag
1118 (if raw-secondary-p raw-tag
1119 (org-element-parse-secondary-string
1120 raw-tag (org-element-restriction 'item) item))))))))
1122 (defun org-element-item-interpreter (item contents)
1123 "Interpret ITEM element as Org syntax.
1124 CONTENTS is the contents of the element."
1125 (let* ((bullet (let ((bullet (org-element-property :bullet item)))
1126 (org-list-bullet-string
1127 (cond ((not (string-match "[0-9a-zA-Z]" bullet)) "- ")
1128 ((eq org-plain-list-ordered-item-terminator ?\)) "1)")
1129 (t "1.")))))
1130 (checkbox (org-element-property :checkbox item))
1131 (counter (org-element-property :counter item))
1132 (tag (let ((tag (org-element-property :tag item)))
1133 (and tag (org-element-interpret-data tag))))
1134 ;; Compute indentation.
1135 (ind (make-string (length bullet) 32))
1136 (item-starts-with-par-p
1137 (eq (org-element-type (car (org-element-contents item)))
1138 'paragraph)))
1139 ;; Indent contents.
1140 (concat
1141 bullet
1142 (and counter (format "[@%d] " counter))
1143 (case checkbox
1144 (on "[X] ")
1145 (off "[ ] ")
1146 (trans "[-] "))
1147 (and tag (format "%s :: " tag))
1148 (when contents
1149 (let ((contents (replace-regexp-in-string
1150 "\\(^\\)[ \t]*\\S-" ind contents nil nil 1)))
1151 (if item-starts-with-par-p (org-trim contents)
1152 (concat "\n" contents)))))))
1155 ;;;; Plain List
1157 (defun org-element--list-struct (limit)
1158 ;; Return structure of list at point. Internal function. See
1159 ;; `org-list-struct' for details.
1160 (let ((case-fold-search t)
1161 (top-ind limit)
1162 (item-re (org-item-re))
1163 (drawers-re (concat ":\\("
1164 (mapconcat 'regexp-quote org-drawers "\\|")
1165 "\\):[ \t]*$"))
1166 (inlinetask-re (and (featurep 'org-inlinetask) "^\\*+ "))
1167 items struct)
1168 (save-excursion
1169 (catch 'exit
1170 (while t
1171 (cond
1172 ;; At limit: end all items.
1173 ((>= (point) limit)
1174 (throw 'exit
1175 (let ((end (progn (skip-chars-backward " \r\t\n")
1176 (forward-line)
1177 (point))))
1178 (dolist (item items (sort (nconc items struct)
1179 'car-less-than-car))
1180 (setcar (nthcdr 6 item) end)))))
1181 ;; At list end: end all items.
1182 ((looking-at org-list-end-re)
1183 (throw 'exit (dolist (item items (sort (nconc items struct)
1184 'car-less-than-car))
1185 (setcar (nthcdr 6 item) (point)))))
1186 ;; At a new item: end previous sibling.
1187 ((looking-at item-re)
1188 (let ((ind (save-excursion (skip-chars-forward " \t")
1189 (current-column))))
1190 (setq top-ind (min top-ind ind))
1191 (while (and items (<= ind (nth 1 (car items))))
1192 (let ((item (pop items)))
1193 (setcar (nthcdr 6 item) (point))
1194 (push item struct)))
1195 (push (progn (looking-at org-list-full-item-re)
1196 (let ((bullet (match-string-no-properties 1)))
1197 (list (point)
1199 bullet
1200 (match-string-no-properties 2) ; counter
1201 (match-string-no-properties 3) ; checkbox
1202 ;; Description tag.
1203 (and (save-match-data
1204 (string-match "[-+*]" bullet))
1205 (match-string-no-properties 4))
1206 ;; Ending position, unknown so far.
1207 nil)))
1208 items))
1209 (forward-line 1))
1210 ;; Skip empty lines.
1211 ((looking-at "^[ \t]*$") (forward-line))
1212 ;; Skip inline tasks and blank lines along the way.
1213 ((and inlinetask-re (looking-at inlinetask-re))
1214 (forward-line)
1215 (let ((origin (point)))
1216 (when (re-search-forward inlinetask-re limit t)
1217 (if (looking-at "^\\*+ END[ \t]*$") (forward-line)
1218 (goto-char origin)))))
1219 ;; At some text line. Check if it ends any previous item.
1221 (let ((ind (progn (skip-chars-forward " \t") (current-column))))
1222 (when (<= ind top-ind)
1223 (skip-chars-backward " \r\t\n")
1224 (forward-line))
1225 (while (<= ind (nth 1 (car items)))
1226 (let ((item (pop items)))
1227 (setcar (nthcdr 6 item) (line-beginning-position))
1228 (push item struct)
1229 (unless items
1230 (throw 'exit (sort struct 'car-less-than-car))))))
1231 ;; Skip blocks (any type) and drawers contents.
1232 (cond
1233 ((and (looking-at "#\\+BEGIN\\(:\\|_\\S-+\\)")
1234 (re-search-forward
1235 (format "^[ \t]*#\\+END%s[ \t]*$"
1236 (org-match-string-no-properties 1))
1237 limit t)))
1238 ((and (looking-at drawers-re)
1239 (re-search-forward "^[ \t]*:END:[ \t]*$" limit t))))
1240 (forward-line))))))))
1242 (defun org-element-plain-list-parser (limit affiliated structure)
1243 "Parse a plain list.
1245 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1246 the buffer position at the beginning of the first affiliated
1247 keyword and CDR is a plist of affiliated keywords along with
1248 their value. STRUCTURE is the structure of the plain list being
1249 parsed.
1251 Return a list whose CAR is `plain-list' and CDR is a plist
1252 containing `:type', `:begin', `:end', `:contents-begin' and
1253 `:contents-end', `:structure', `:post-blank' and
1254 `:post-affiliated' keywords.
1256 Assume point is at the beginning of the list."
1257 (save-excursion
1258 (let* ((struct (or structure (org-element--list-struct limit)))
1259 (prevs (org-list-prevs-alist struct))
1260 (type (org-list-get-list-type (point) struct prevs))
1261 (contents-begin (point))
1262 (begin (car affiliated))
1263 (contents-end
1264 (progn (goto-char (org-list-get-list-end (point) struct prevs))
1265 (unless (bolp) (forward-line))
1266 (point)))
1267 (end (progn (skip-chars-forward " \r\t\n" limit)
1268 (if (= (point) limit) limit (line-beginning-position)))))
1269 ;; Return value.
1270 (list 'plain-list
1271 (nconc
1272 (list :type type
1273 :begin begin
1274 :end end
1275 :contents-begin contents-begin
1276 :contents-end contents-end
1277 :structure struct
1278 :post-blank (count-lines contents-end end)
1279 :post-affiliated contents-begin)
1280 (cdr affiliated))))))
1282 (defun org-element-plain-list-interpreter (plain-list contents)
1283 "Interpret PLAIN-LIST element as Org syntax.
1284 CONTENTS is the contents of the element."
1285 (with-temp-buffer
1286 (insert contents)
1287 (goto-char (point-min))
1288 (org-list-repair)
1289 (buffer-string)))
1292 ;;;; Property Drawer
1294 (defun org-element-property-drawer-parser (limit affiliated)
1295 "Parse a property drawer.
1297 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1298 the buffer position at the beginning of the first affiliated
1299 keyword and CDR is a plist of affiliated keywords along with
1300 their value.
1302 Return a list whose CAR is `property-drawer' and CDR is a plist
1303 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1304 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
1306 Assume point is at the beginning of the property drawer."
1307 (save-excursion
1308 (let ((case-fold-search t))
1309 (if (not (save-excursion
1310 (re-search-forward "^[ \t]*:END:[ \t]*$" limit t)))
1311 ;; Incomplete drawer: parse it as a paragraph.
1312 (org-element-paragraph-parser limit affiliated)
1313 (save-excursion
1314 (let* ((drawer-end-line (match-beginning 0))
1315 (begin (car affiliated))
1316 (post-affiliated (point))
1317 (contents-begin (progn (forward-line)
1318 (and (< (point) drawer-end-line)
1319 (point))))
1320 (contents-end (and contents-begin drawer-end-line))
1321 (hidden (org-invisible-p2))
1322 (pos-before-blank (progn (goto-char drawer-end-line)
1323 (forward-line)
1324 (point)))
1325 (end (progn (skip-chars-forward " \r\t\n" limit)
1326 (skip-chars-backward " \t")
1327 (if (bolp) (point) (line-end-position)))))
1328 (list 'property-drawer
1329 (nconc
1330 (list :begin begin
1331 :end end
1332 :hiddenp hidden
1333 :contents-begin contents-begin
1334 :contents-end contents-end
1335 :post-blank (count-lines pos-before-blank end)
1336 :post-affiliated post-affiliated)
1337 (cdr affiliated)))))))))
1339 (defun org-element-property-drawer-interpreter (property-drawer contents)
1340 "Interpret PROPERTY-DRAWER element as Org syntax.
1341 CONTENTS is the properties within the drawer."
1342 (format ":PROPERTIES:\n%s:END:" contents))
1345 ;;;; Quote Block
1347 (defun org-element-quote-block-parser (limit affiliated)
1348 "Parse a quote block.
1350 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1351 the buffer position at the beginning of the first affiliated
1352 keyword and CDR is a plist of affiliated keywords along with
1353 their value.
1355 Return a list whose CAR is `quote-block' and CDR is a plist
1356 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1357 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
1359 Assume point is at the beginning of the block."
1360 (let ((case-fold-search t))
1361 (if (not (save-excursion
1362 (re-search-forward "^[ \t]*#\\+END_QUOTE[ \t]*$" limit t)))
1363 ;; Incomplete block: parse it as a paragraph.
1364 (org-element-paragraph-parser limit affiliated)
1365 (let ((block-end-line (match-beginning 0)))
1366 (save-excursion
1367 (let* ((begin (car affiliated))
1368 (post-affiliated (point))
1369 ;; Empty blocks have no contents.
1370 (contents-begin (progn (forward-line)
1371 (and (< (point) block-end-line)
1372 (point))))
1373 (contents-end (and contents-begin block-end-line))
1374 (hidden (org-invisible-p2))
1375 (pos-before-blank (progn (goto-char block-end-line)
1376 (forward-line)
1377 (point)))
1378 (end (progn (skip-chars-forward " \r\t\n" limit)
1379 (skip-chars-backward " \t")
1380 (if (bolp) (point) (line-end-position)))))
1381 (list 'quote-block
1382 (nconc
1383 (list :begin begin
1384 :end end
1385 :hiddenp hidden
1386 :contents-begin contents-begin
1387 :contents-end contents-end
1388 :post-blank (count-lines pos-before-blank end)
1389 :post-affiliated post-affiliated)
1390 (cdr affiliated)))))))))
1392 (defun org-element-quote-block-interpreter (quote-block contents)
1393 "Interpret QUOTE-BLOCK element as Org syntax.
1394 CONTENTS is the contents of the element."
1395 (format "#+BEGIN_QUOTE\n%s#+END_QUOTE" contents))
1398 ;;;; Section
1400 (defun org-element-section-parser (limit)
1401 "Parse a section.
1403 LIMIT bounds the search.
1405 Return a list whose CAR is `section' and CDR is a plist
1406 containing `:begin', `:end', `:contents-begin', `contents-end'
1407 and `:post-blank' keywords."
1408 (save-excursion
1409 ;; Beginning of section is the beginning of the first non-blank
1410 ;; line after previous headline.
1411 (let ((begin (point))
1412 (end (progn (org-with-limited-levels (outline-next-heading))
1413 (point)))
1414 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
1415 (forward-line)
1416 (point))))
1417 (list 'section
1418 (list :begin begin
1419 :end end
1420 :contents-begin begin
1421 :contents-end pos-before-blank
1422 :post-blank (count-lines pos-before-blank end))))))
1424 (defun org-element-section-interpreter (section contents)
1425 "Interpret SECTION element as Org syntax.
1426 CONTENTS is the contents of the element."
1427 contents)
1430 ;;;; Special Block
1432 (defun org-element-special-block-parser (limit affiliated)
1433 "Parse a special block.
1435 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1436 the buffer position at the beginning of the first affiliated
1437 keyword and CDR is a plist of affiliated keywords along with
1438 their value.
1440 Return a list whose CAR is `special-block' and CDR is a plist
1441 containing `:type', `:begin', `:end', `:hiddenp',
1442 `:contents-begin', `:contents-end', `:post-blank' and
1443 `:post-affiliated' keywords.
1445 Assume point is at the beginning of the block."
1446 (let* ((case-fold-search t)
1447 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
1448 (upcase (match-string-no-properties 1)))))
1449 (if (not (save-excursion
1450 (re-search-forward
1451 (format "^[ \t]*#\\+END_%s[ \t]*$" (regexp-quote type))
1452 limit t)))
1453 ;; Incomplete block: parse it as a paragraph.
1454 (org-element-paragraph-parser limit affiliated)
1455 (let ((block-end-line (match-beginning 0)))
1456 (save-excursion
1457 (let* ((begin (car affiliated))
1458 (post-affiliated (point))
1459 ;; Empty blocks have no contents.
1460 (contents-begin (progn (forward-line)
1461 (and (< (point) block-end-line)
1462 (point))))
1463 (contents-end (and contents-begin block-end-line))
1464 (hidden (org-invisible-p2))
1465 (pos-before-blank (progn (goto-char block-end-line)
1466 (forward-line)
1467 (point)))
1468 (end (progn (skip-chars-forward " \r\t\n" limit)
1469 (skip-chars-backward " \t")
1470 (if (bolp) (point) (line-end-position)))))
1471 (list 'special-block
1472 (nconc
1473 (list :type type
1474 :begin begin
1475 :end end
1476 :hiddenp hidden
1477 :contents-begin contents-begin
1478 :contents-end contents-end
1479 :post-blank (count-lines pos-before-blank end)
1480 :post-affiliated post-affiliated)
1481 (cdr affiliated)))))))))
1483 (defun org-element-special-block-interpreter (special-block contents)
1484 "Interpret SPECIAL-BLOCK element as Org syntax.
1485 CONTENTS is the contents of the element."
1486 (let ((block-type (org-element-property :type special-block)))
1487 (format "#+BEGIN_%s\n%s#+END_%s" block-type contents block-type)))
1491 ;;; Elements
1493 ;; For each element, a parser and an interpreter are also defined.
1494 ;; Both follow the same naming convention used for greater elements.
1496 ;; Also, as for greater elements, adding a new element type is done
1497 ;; through the following steps: implement a parser and an interpreter,
1498 ;; tweak `org-element--current-element' so that it recognizes the new
1499 ;; type and add that new type to `org-element-all-elements'.
1501 ;; As a special case, when the newly defined type is a block type,
1502 ;; `org-element-block-name-alist' has to be modified accordingly.
1505 ;;;; Babel Call
1507 (defun org-element-babel-call-parser (limit affiliated)
1508 "Parse a babel call.
1510 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1511 the buffer position at the beginning of the first affiliated
1512 keyword and CDR is a plist of affiliated keywords along with
1513 their value.
1515 Return a list whose CAR is `babel-call' and CDR is a plist
1516 containing `:begin', `:end', `:info', `:post-blank' and
1517 `:post-affiliated' as keywords."
1518 (save-excursion
1519 (let ((case-fold-search t)
1520 (info (progn (looking-at org-babel-block-lob-one-liner-regexp)
1521 (org-babel-lob-get-info)))
1522 (begin (car affiliated))
1523 (post-affiliated (point))
1524 (pos-before-blank (progn (forward-line) (point)))
1525 (end (progn (skip-chars-forward " \r\t\n" limit)
1526 (skip-chars-backward " \t")
1527 (if (bolp) (point) (line-end-position)))))
1528 (list 'babel-call
1529 (nconc
1530 (list :begin begin
1531 :end end
1532 :info info
1533 :post-blank (count-lines pos-before-blank end)
1534 :post-affiliated post-affiliated)
1535 (cdr affiliated))))))
1537 (defun org-element-babel-call-interpreter (babel-call contents)
1538 "Interpret BABEL-CALL element as Org syntax.
1539 CONTENTS is nil."
1540 (let* ((babel-info (org-element-property :info babel-call))
1541 (main (car babel-info))
1542 (post-options (nth 1 babel-info)))
1543 (concat "#+CALL: "
1544 (if (not (string-match "\\[\\(\\[.*?\\]\\)\\]" main)) main
1545 ;; Remove redundant square brackets.
1546 (replace-match (match-string 1 main) nil nil main))
1547 (and post-options (format "[%s]" post-options)))))
1550 ;;;; Clock
1552 (defun org-element-clock-parser (limit)
1553 "Parse a clock.
1555 LIMIT bounds the search.
1557 Return a list whose CAR is `clock' and CDR is a plist containing
1558 `:status', `:value', `:time', `:begin', `:end' and `:post-blank'
1559 as keywords."
1560 (save-excursion
1561 (let* ((case-fold-search nil)
1562 (begin (point))
1563 (value (progn (search-forward org-clock-string (line-end-position) t)
1564 (skip-chars-forward " \t")
1565 (org-element-timestamp-parser)))
1566 (duration (and (search-forward " => " (line-end-position) t)
1567 (progn (skip-chars-forward " \t")
1568 (looking-at "\\(\\S-+\\)[ \t]*$"))
1569 (org-match-string-no-properties 1)))
1570 (status (if duration 'closed 'running))
1571 (post-blank (let ((before-blank (progn (forward-line) (point))))
1572 (skip-chars-forward " \r\t\n" limit)
1573 (skip-chars-backward " \t")
1574 (unless (bolp) (end-of-line))
1575 (count-lines before-blank (point))))
1576 (end (point)))
1577 (list 'clock
1578 (list :status status
1579 :value value
1580 :duration duration
1581 :begin begin
1582 :end end
1583 :post-blank post-blank)))))
1585 (defun org-element-clock-interpreter (clock contents)
1586 "Interpret CLOCK element as Org syntax.
1587 CONTENTS is nil."
1588 (concat org-clock-string " "
1589 (org-element-timestamp-interpreter
1590 (org-element-property :value clock) nil)
1591 (let ((duration (org-element-property :duration clock)))
1592 (and duration
1593 (concat " => "
1594 (apply 'format
1595 "%2s:%02s"
1596 (org-split-string duration ":")))))))
1599 ;;;; Comment
1601 (defun org-element-comment-parser (limit affiliated)
1602 "Parse a comment.
1604 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1605 the buffer position at the beginning of the first affiliated
1606 keyword and CDR is a plist of affiliated keywords along with
1607 their value.
1609 Return a list whose CAR is `comment' and CDR is a plist
1610 containing `:begin', `:end', `:value', `:post-blank',
1611 `:post-affiliated' keywords.
1613 Assume point is at comment beginning."
1614 (save-excursion
1615 (let* ((begin (car affiliated))
1616 (post-affiliated (point))
1617 (value (prog2 (looking-at "[ \t]*# ?")
1618 (buffer-substring-no-properties
1619 (match-end 0) (line-end-position))
1620 (forward-line)))
1621 (com-end
1622 ;; Get comments ending.
1623 (progn
1624 (while (and (< (point) limit) (looking-at "[ \t]*#\\( \\|$\\)"))
1625 ;; Accumulate lines without leading hash and first
1626 ;; whitespace.
1627 (setq value
1628 (concat value
1629 "\n"
1630 (buffer-substring-no-properties
1631 (match-end 0) (line-end-position))))
1632 (forward-line))
1633 (point)))
1634 (end (progn (goto-char com-end)
1635 (skip-chars-forward " \r\t\n" limit)
1636 (skip-chars-backward " \t")
1637 (if (bolp) (point) (line-end-position)))))
1638 (list 'comment
1639 (nconc
1640 (list :begin begin
1641 :end end
1642 :value value
1643 :post-blank (count-lines com-end end)
1644 :post-affiliated post-affiliated)
1645 (cdr affiliated))))))
1647 (defun org-element-comment-interpreter (comment contents)
1648 "Interpret COMMENT element as Org syntax.
1649 CONTENTS is nil."
1650 (replace-regexp-in-string "^" "# " (org-element-property :value comment)))
1653 ;;;; Comment Block
1655 (defun org-element-comment-block-parser (limit affiliated)
1656 "Parse an export block.
1658 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1659 the buffer position at the beginning of the first affiliated
1660 keyword and CDR is a plist of affiliated keywords along with
1661 their value.
1663 Return a list whose CAR is `comment-block' and CDR is a plist
1664 containing `:begin', `:end', `:hiddenp', `:value', `:post-blank'
1665 and `:post-affiliated' keywords.
1667 Assume point is at comment block beginning."
1668 (let ((case-fold-search t))
1669 (if (not (save-excursion
1670 (re-search-forward "^[ \t]*#\\+END_COMMENT[ \t]*$" limit t)))
1671 ;; Incomplete block: parse it as a paragraph.
1672 (org-element-paragraph-parser limit affiliated)
1673 (let ((contents-end (match-beginning 0)))
1674 (save-excursion
1675 (let* ((begin (car affiliated))
1676 (post-affiliated (point))
1677 (contents-begin (progn (forward-line) (point)))
1678 (hidden (org-invisible-p2))
1679 (pos-before-blank (progn (goto-char contents-end)
1680 (forward-line)
1681 (point)))
1682 (end (progn (skip-chars-forward " \r\t\n" limit)
1683 (skip-chars-backward " \t")
1684 (if (bolp) (point) (line-end-position))))
1685 (value (buffer-substring-no-properties
1686 contents-begin contents-end)))
1687 (list 'comment-block
1688 (nconc
1689 (list :begin begin
1690 :end end
1691 :value value
1692 :hiddenp hidden
1693 :post-blank (count-lines pos-before-blank end)
1694 :post-affiliated post-affiliated)
1695 (cdr affiliated)))))))))
1697 (defun org-element-comment-block-interpreter (comment-block contents)
1698 "Interpret COMMENT-BLOCK element as Org syntax.
1699 CONTENTS is nil."
1700 (format "#+BEGIN_COMMENT\n%s#+END_COMMENT"
1701 (org-remove-indentation (org-element-property :value comment-block))))
1704 ;;;; Diary Sexp
1706 (defun org-element-diary-sexp-parser (limit affiliated)
1707 "Parse a diary sexp.
1709 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1710 the buffer position at the beginning of the first affiliated
1711 keyword and CDR is a plist of affiliated keywords along with
1712 their value.
1714 Return a list whose CAR is `diary-sexp' and CDR is a plist
1715 containing `:begin', `:end', `:value', `:post-blank' and
1716 `:post-affiliated' keywords."
1717 (save-excursion
1718 (let ((begin (car affiliated))
1719 (post-affiliated (point))
1720 (value (progn (looking-at "\\(%%(.*\\)[ \t]*$")
1721 (org-match-string-no-properties 1)))
1722 (pos-before-blank (progn (forward-line) (point)))
1723 (end (progn (skip-chars-forward " \r\t\n" limit)
1724 (skip-chars-backward " \t")
1725 (if (bolp) (point) (line-end-position)))))
1726 (list 'diary-sexp
1727 (nconc
1728 (list :value value
1729 :begin begin
1730 :end end
1731 :post-blank (count-lines pos-before-blank end)
1732 :post-affiliated post-affiliated)
1733 (cdr affiliated))))))
1735 (defun org-element-diary-sexp-interpreter (diary-sexp contents)
1736 "Interpret DIARY-SEXP as Org syntax.
1737 CONTENTS is nil."
1738 (org-element-property :value diary-sexp))
1741 ;;;; Example Block
1743 (defun org-element--remove-indentation (s &optional n)
1744 "Remove maximum common indentation in string S and return it.
1745 When optional argument N is a positive integer, remove exactly
1746 that much characters from indentation, if possible, or return
1747 S as-is otherwise. Unlike to `org-remove-indentation', this
1748 function doesn't call `untabify' on S."
1749 (catch 'exit
1750 (with-temp-buffer
1751 (insert s)
1752 (goto-char (point-min))
1753 ;; Find maximum common indentation, if not specified.
1754 (setq n (or n
1755 (let ((min-ind (point-max)))
1756 (save-excursion
1757 (while (re-search-forward "^[ \t]*\\S-" nil t)
1758 (let ((ind (1- (current-column))))
1759 (if (zerop ind) (throw 'exit s)
1760 (setq min-ind (min min-ind ind))))))
1761 min-ind)))
1762 (if (zerop n) s
1763 ;; Remove exactly N indentation, but give up if not possible.
1764 (while (not (eobp))
1765 (let ((ind (progn (skip-chars-forward " \t") (current-column))))
1766 (cond ((eolp) (delete-region (line-beginning-position) (point)))
1767 ((< ind n) (throw 'exit s))
1768 (t (org-indent-line-to (- ind n))))
1769 (forward-line)))
1770 (buffer-string)))))
1772 (defun org-element-example-block-parser (limit affiliated)
1773 "Parse an example block.
1775 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1776 the buffer position at the beginning of the first affiliated
1777 keyword and CDR is a plist of affiliated keywords along with
1778 their value.
1780 Return a list whose CAR is `example-block' and CDR is a plist
1781 containing `:begin', `:end', `:number-lines', `:preserve-indent',
1782 `:retain-labels', `:use-labels', `:label-fmt', `:hiddenp',
1783 `:switches', `:value', `:post-blank' and `:post-affiliated'
1784 keywords."
1785 (let ((case-fold-search t))
1786 (if (not (save-excursion
1787 (re-search-forward "^[ \t]*#\\+END_EXAMPLE[ \t]*$" limit t)))
1788 ;; Incomplete block: parse it as a paragraph.
1789 (org-element-paragraph-parser limit affiliated)
1790 (let ((contents-end (match-beginning 0)))
1791 (save-excursion
1792 (let* ((switches
1793 (progn
1794 (looking-at "^[ \t]*#\\+BEGIN_EXAMPLE\\(?: +\\(.*\\)\\)?")
1795 (org-match-string-no-properties 1)))
1796 ;; Switches analysis
1797 (number-lines
1798 (cond ((not switches) nil)
1799 ((string-match "-n\\>" switches) 'new)
1800 ((string-match "+n\\>" switches) 'continued)))
1801 (preserve-indent
1802 (or org-src-preserve-indentation
1803 (and switches (string-match "-i\\>" switches))))
1804 ;; Should labels be retained in (or stripped from) example
1805 ;; blocks?
1806 (retain-labels
1807 (or (not switches)
1808 (not (string-match "-r\\>" switches))
1809 (and number-lines (string-match "-k\\>" switches))))
1810 ;; What should code-references use - labels or
1811 ;; line-numbers?
1812 (use-labels
1813 (or (not switches)
1814 (and retain-labels
1815 (not (string-match "-k\\>" switches)))))
1816 (label-fmt
1817 (and switches
1818 (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
1819 (match-string 1 switches)))
1820 ;; Standard block parsing.
1821 (begin (car affiliated))
1822 (post-affiliated (point))
1823 (block-ind (progn (skip-chars-forward " \t") (current-column)))
1824 (contents-begin (progn (forward-line) (point)))
1825 (hidden (org-invisible-p2))
1826 (value (org-element--remove-indentation
1827 (org-unescape-code-in-string
1828 (buffer-substring-no-properties
1829 contents-begin contents-end))
1830 (and preserve-indent block-ind)))
1831 (pos-before-blank (progn (goto-char contents-end)
1832 (forward-line)
1833 (point)))
1834 (end (progn (skip-chars-forward " \r\t\n" limit)
1835 (skip-chars-backward " \t")
1836 (if (bolp) (point) (line-end-position)))))
1837 (list 'example-block
1838 (nconc
1839 (list :begin begin
1840 :end end
1841 :value value
1842 :switches switches
1843 :number-lines number-lines
1844 :preserve-indent preserve-indent
1845 :retain-labels retain-labels
1846 :use-labels use-labels
1847 :label-fmt label-fmt
1848 :hiddenp hidden
1849 :post-blank (count-lines pos-before-blank end)
1850 :post-affiliated post-affiliated)
1851 (cdr affiliated)))))))))
1853 (defun org-element-example-block-interpreter (example-block contents)
1854 "Interpret EXAMPLE-BLOCK element as Org syntax.
1855 CONTENTS is nil."
1856 (let ((switches (org-element-property :switches example-block)))
1857 (concat "#+BEGIN_EXAMPLE" (and switches (concat " " switches)) "\n"
1858 (org-escape-code-in-string
1859 (org-element-property :value example-block))
1860 "#+END_EXAMPLE")))
1863 ;;;; Export Block
1865 (defun org-element-export-block-parser (limit affiliated)
1866 "Parse an export block.
1868 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1869 the buffer position at the beginning of the first affiliated
1870 keyword and CDR is a plist of affiliated keywords along with
1871 their value.
1873 Return a list whose CAR is `export-block' and CDR is a plist
1874 containing `:begin', `:end', `:type', `:hiddenp', `:value',
1875 `:post-blank' and `:post-affiliated' keywords.
1877 Assume point is at export-block beginning."
1878 (let* ((case-fold-search t)
1879 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
1880 (upcase (org-match-string-no-properties 1)))))
1881 (if (not (save-excursion
1882 (re-search-forward
1883 (format "^[ \t]*#\\+END_%s[ \t]*$" type) limit t)))
1884 ;; Incomplete block: parse it as a paragraph.
1885 (org-element-paragraph-parser limit affiliated)
1886 (let ((contents-end (match-beginning 0)))
1887 (save-excursion
1888 (let* ((begin (car affiliated))
1889 (post-affiliated (point))
1890 (contents-begin (progn (forward-line) (point)))
1891 (hidden (org-invisible-p2))
1892 (pos-before-blank (progn (goto-char contents-end)
1893 (forward-line)
1894 (point)))
1895 (end (progn (skip-chars-forward " \r\t\n" limit)
1896 (skip-chars-backward " \t")
1897 (if (bolp) (point) (line-end-position))))
1898 (value (buffer-substring-no-properties contents-begin
1899 contents-end)))
1900 (list 'export-block
1901 (nconc
1902 (list :begin begin
1903 :end end
1904 :type type
1905 :value value
1906 :hiddenp hidden
1907 :post-blank (count-lines pos-before-blank end)
1908 :post-affiliated post-affiliated)
1909 (cdr affiliated)))))))))
1911 (defun org-element-export-block-interpreter (export-block contents)
1912 "Interpret EXPORT-BLOCK element as Org syntax.
1913 CONTENTS is nil."
1914 (let ((type (org-element-property :type export-block)))
1915 (concat (format "#+BEGIN_%s\n" type)
1916 (org-element-property :value export-block)
1917 (format "#+END_%s" type))))
1920 ;;;; Fixed-width
1922 (defun org-element-fixed-width-parser (limit affiliated)
1923 "Parse a fixed-width section.
1925 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1926 the buffer position at the beginning of the first affiliated
1927 keyword and CDR is a plist of affiliated keywords along with
1928 their value.
1930 Return a list whose CAR is `fixed-width' and CDR is a plist
1931 containing `:begin', `:end', `:value', `:post-blank' and
1932 `:post-affiliated' keywords.
1934 Assume point is at the beginning of the fixed-width area."
1935 (save-excursion
1936 (let* ((begin (car affiliated))
1937 (post-affiliated (point))
1938 value
1939 (end-area
1940 (progn
1941 (while (and (< (point) limit)
1942 (looking-at "[ \t]*:\\( \\|$\\)"))
1943 ;; Accumulate text without starting colons.
1944 (setq value
1945 (concat value
1946 (buffer-substring-no-properties
1947 (match-end 0) (point-at-eol))
1948 "\n"))
1949 (forward-line))
1950 (point)))
1951 (end (progn (skip-chars-forward " \r\t\n" limit)
1952 (skip-chars-backward " \t")
1953 (if (bolp) (point) (line-end-position)))))
1954 (list 'fixed-width
1955 (nconc
1956 (list :begin begin
1957 :end end
1958 :value value
1959 :post-blank (count-lines end-area end)
1960 :post-affiliated post-affiliated)
1961 (cdr affiliated))))))
1963 (defun org-element-fixed-width-interpreter (fixed-width contents)
1964 "Interpret FIXED-WIDTH element as Org syntax.
1965 CONTENTS is nil."
1966 (let ((value (org-element-property :value fixed-width)))
1967 (and value
1968 (replace-regexp-in-string
1969 "^" ": "
1970 (if (string-match "\n\\'" value) (substring value 0 -1) value)))))
1973 ;;;; Horizontal Rule
1975 (defun org-element-horizontal-rule-parser (limit affiliated)
1976 "Parse an horizontal rule.
1978 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1979 the buffer position at the beginning of the first affiliated
1980 keyword and CDR is a plist of affiliated keywords along with
1981 their value.
1983 Return a list whose CAR is `horizontal-rule' and CDR is a plist
1984 containing `:begin', `:end', `:post-blank' and `:post-affiliated'
1985 keywords."
1986 (save-excursion
1987 (let ((begin (car affiliated))
1988 (post-affiliated (point))
1989 (post-hr (progn (forward-line) (point)))
1990 (end (progn (skip-chars-forward " \r\t\n" limit)
1991 (skip-chars-backward " \t")
1992 (if (bolp) (point) (line-end-position)))))
1993 (list 'horizontal-rule
1994 (nconc
1995 (list :begin begin
1996 :end end
1997 :post-blank (count-lines post-hr end)
1998 :post-affiliated post-affiliated)
1999 (cdr affiliated))))))
2001 (defun org-element-horizontal-rule-interpreter (horizontal-rule contents)
2002 "Interpret HORIZONTAL-RULE element as Org syntax.
2003 CONTENTS is nil."
2004 "-----")
2007 ;;;; Keyword
2009 (defun org-element-keyword-parser (limit affiliated)
2010 "Parse a keyword at point.
2012 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2013 the buffer position at the beginning of the first affiliated
2014 keyword and CDR is a plist of affiliated keywords along with
2015 their value.
2017 Return a list whose CAR is `keyword' and CDR is a plist
2018 containing `:key', `:value', `:begin', `:end', `:post-blank' and
2019 `:post-affiliated' keywords."
2020 (save-excursion
2021 (let ((begin (car affiliated))
2022 (post-affiliated (point))
2023 (key (progn (looking-at "[ \t]*#\\+\\(\\S-+*\\):")
2024 (upcase (org-match-string-no-properties 1))))
2025 (value (org-trim (buffer-substring-no-properties
2026 (match-end 0) (point-at-eol))))
2027 (pos-before-blank (progn (forward-line) (point)))
2028 (end (progn (skip-chars-forward " \r\t\n" limit)
2029 (skip-chars-backward " \t")
2030 (if (bolp) (point) (line-end-position)))))
2031 (list 'keyword
2032 (nconc
2033 (list :key key
2034 :value value
2035 :begin begin
2036 :end end
2037 :post-blank (count-lines pos-before-blank end)
2038 :post-affiliated post-affiliated)
2039 (cdr affiliated))))))
2041 (defun org-element-keyword-interpreter (keyword contents)
2042 "Interpret KEYWORD element as Org syntax.
2043 CONTENTS is nil."
2044 (format "#+%s: %s"
2045 (org-element-property :key keyword)
2046 (org-element-property :value keyword)))
2049 ;;;; Latex Environment
2051 (defun org-element-latex-environment-parser (limit affiliated)
2052 "Parse a LaTeX environment.
2054 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2055 the buffer position at the beginning of the first affiliated
2056 keyword and CDR is a plist of affiliated keywords along with
2057 their value.
2059 Return a list whose CAR is `latex-environment' and CDR is a plist
2060 containing `:begin', `:end', `:value', `:post-blank' and
2061 `:post-affiliated' keywords.
2063 Assume point is at the beginning of the latex environment."
2064 (save-excursion
2065 (let ((case-fold-search t)
2066 (code-begin (point)))
2067 (looking-at "[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}")
2068 (if (not (re-search-forward (format "^[ \t]*\\\\end{%s}[ \t]*$"
2069 (regexp-quote (match-string 1)))
2070 limit t))
2071 ;; Incomplete latex environment: parse it as a paragraph.
2072 (org-element-paragraph-parser limit affiliated)
2073 (let* ((code-end (progn (forward-line) (point)))
2074 (begin (car affiliated))
2075 (value (buffer-substring-no-properties code-begin code-end))
2076 (end (progn (skip-chars-forward " \r\t\n" limit)
2077 (skip-chars-backward " \t")
2078 (if (bolp) (point) (line-end-position)))))
2079 (list 'latex-environment
2080 (nconc
2081 (list :begin begin
2082 :end end
2083 :value value
2084 :post-blank (count-lines code-end end)
2085 :post-affiliated code-begin)
2086 (cdr affiliated))))))))
2088 (defun org-element-latex-environment-interpreter (latex-environment contents)
2089 "Interpret LATEX-ENVIRONMENT element as Org syntax.
2090 CONTENTS is nil."
2091 (org-element-property :value latex-environment))
2094 ;;;; Node Property
2096 (defun org-element-node-property-parser (limit)
2097 "Parse a node-property at point.
2099 LIMIT bounds the search.
2101 Return a list whose CAR is `node-property' and CDR is a plist
2102 containing `:key', `:value', `:begin', `:end' and `:post-blank'
2103 keywords."
2104 (save-excursion
2105 (looking-at org-property-re)
2106 (let ((case-fold-search t)
2107 (begin (point))
2108 (key (org-match-string-no-properties 2))
2109 (value (org-match-string-no-properties 3))
2110 (pos-before-blank (progn (forward-line) (point)))
2111 (end (progn (skip-chars-forward " \r\t\n" limit)
2112 (if (eobp) (point) (point-at-bol)))))
2113 (list 'node-property
2114 (list :key key
2115 :value value
2116 :begin begin
2117 :end end
2118 :post-blank (count-lines pos-before-blank end))))))
2120 (defun org-element-node-property-interpreter (node-property contents)
2121 "Interpret NODE-PROPERTY element as Org syntax.
2122 CONTENTS is nil."
2123 (format org-property-format
2124 (format ":%s:" (org-element-property :key node-property))
2125 (org-element-property :value node-property)))
2128 ;;;; Paragraph
2130 (defun org-element-paragraph-parser (limit affiliated)
2131 "Parse a paragraph.
2133 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2134 the buffer position at the beginning of the first affiliated
2135 keyword and CDR is a plist of affiliated keywords along with
2136 their value.
2138 Return a list whose CAR is `paragraph' and CDR is a plist
2139 containing `:begin', `:end', `:contents-begin' and
2140 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
2142 Assume point is at the beginning of the paragraph."
2143 (save-excursion
2144 (let* ((begin (car affiliated))
2145 (contents-begin (point))
2146 (before-blank
2147 (let ((case-fold-search t))
2148 (end-of-line)
2149 (if (not (re-search-forward
2150 org-element-paragraph-separate limit 'm))
2151 limit
2152 ;; A matching `org-element-paragraph-separate' is not
2153 ;; necessarily the end of the paragraph. In
2154 ;; particular, lines starting with # or : as a first
2155 ;; non-space character are ambiguous. We have check
2156 ;; if they are valid Org syntax (i.e. not an
2157 ;; incomplete keyword).
2158 (beginning-of-line)
2159 (while (not
2161 ;; There's no ambiguity for other symbols or
2162 ;; empty lines: stop here.
2163 (looking-at "[ \t]*\\(?:[^:#]\\|$\\)")
2164 ;; Stop at valid fixed-width areas.
2165 (looking-at "[ \t]*:\\(?: \\|$\\)")
2166 ;; Stop at drawers.
2167 (and (looking-at org-drawer-regexp)
2168 (save-excursion
2169 (re-search-forward
2170 "^[ \t]*:END:[ \t]*$" limit t)))
2171 ;; Stop at valid comments.
2172 (looking-at "[ \t]*#\\(?: \\|$\\)")
2173 ;; Stop at valid dynamic blocks.
2174 (and (looking-at org-dblock-start-re)
2175 (save-excursion
2176 (re-search-forward
2177 "^[ \t]*#\\+END:?[ \t]*$" limit t)))
2178 ;; Stop at valid blocks.
2179 (and (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
2180 (save-excursion
2181 (re-search-forward
2182 (format "^[ \t]*#\\+END_%s[ \t]*$"
2183 (regexp-quote
2184 (org-match-string-no-properties 1)))
2185 limit t)))
2186 ;; Stop at valid latex environments.
2187 (and (looking-at
2188 "[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}")
2189 (save-excursion
2190 (re-search-forward
2191 (format "^[ \t]*\\\\end{%s}[ \t]*$"
2192 (regexp-quote
2193 (org-match-string-no-properties 1)))
2194 limit t)))
2195 ;; Stop at valid keywords.
2196 (looking-at "[ \t]*#\\+\\S-+:")
2197 ;; Skip everything else.
2198 (not
2199 (progn
2200 (end-of-line)
2201 (re-search-forward org-element-paragraph-separate
2202 limit 'm)))))
2203 (beginning-of-line)))
2204 (if (= (point) limit) limit
2205 (goto-char (line-beginning-position)))))
2206 (contents-end (progn (skip-chars-backward " \r\t\n" contents-begin)
2207 (forward-line)
2208 (point)))
2209 (end (progn (skip-chars-forward " \r\t\n" limit)
2210 (skip-chars-backward " \t")
2211 (if (bolp) (point) (line-end-position)))))
2212 (list 'paragraph
2213 (nconc
2214 (list :begin begin
2215 :end end
2216 :contents-begin contents-begin
2217 :contents-end contents-end
2218 :post-blank (count-lines before-blank end)
2219 :post-affiliated contents-begin)
2220 (cdr affiliated))))))
2222 (defun org-element-paragraph-interpreter (paragraph contents)
2223 "Interpret PARAGRAPH element as Org syntax.
2224 CONTENTS is the contents of the element."
2225 contents)
2228 ;;;; Planning
2230 (defun org-element-planning-parser (limit)
2231 "Parse a planning.
2233 LIMIT bounds the search.
2235 Return a list whose CAR is `planning' and CDR is a plist
2236 containing `:closed', `:deadline', `:scheduled', `:begin', `:end'
2237 and `:post-blank' keywords."
2238 (save-excursion
2239 (let* ((case-fold-search nil)
2240 (begin (point))
2241 (post-blank (let ((before-blank (progn (forward-line) (point))))
2242 (skip-chars-forward " \r\t\n" limit)
2243 (skip-chars-backward " \t")
2244 (unless (bolp) (end-of-line))
2245 (count-lines before-blank (point))))
2246 (end (point))
2247 closed deadline scheduled)
2248 (goto-char begin)
2249 (while (re-search-forward org-keyword-time-not-clock-regexp end t)
2250 (goto-char (match-end 1))
2251 (skip-chars-forward " \t" end)
2252 (let ((keyword (match-string 1))
2253 (time (org-element-timestamp-parser)))
2254 (cond ((equal keyword org-closed-string) (setq closed time))
2255 ((equal keyword org-deadline-string) (setq deadline time))
2256 (t (setq scheduled time)))))
2257 (list 'planning
2258 (list :closed closed
2259 :deadline deadline
2260 :scheduled scheduled
2261 :begin begin
2262 :end end
2263 :post-blank post-blank)))))
2265 (defun org-element-planning-interpreter (planning contents)
2266 "Interpret PLANNING element as Org syntax.
2267 CONTENTS is nil."
2268 (mapconcat
2269 'identity
2270 (delq nil
2271 (list (let ((deadline (org-element-property :deadline planning)))
2272 (when deadline
2273 (concat org-deadline-string " "
2274 (org-element-timestamp-interpreter deadline nil))))
2275 (let ((scheduled (org-element-property :scheduled planning)))
2276 (when scheduled
2277 (concat org-scheduled-string " "
2278 (org-element-timestamp-interpreter scheduled nil))))
2279 (let ((closed (org-element-property :closed planning)))
2280 (when closed
2281 (concat org-closed-string " "
2282 (org-element-timestamp-interpreter closed nil))))))
2283 " "))
2286 ;;;; Quote Section
2288 (defun org-element-quote-section-parser (limit)
2289 "Parse a quote section.
2291 LIMIT bounds the search.
2293 Return a list whose CAR is `quote-section' and CDR is a plist
2294 containing `:begin', `:end', `:value' and `:post-blank' keywords.
2296 Assume point is at beginning of the section."
2297 (save-excursion
2298 (let* ((begin (point))
2299 (end (progn (org-with-limited-levels (outline-next-heading))
2300 (point)))
2301 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
2302 (forward-line)
2303 (point)))
2304 (value (buffer-substring-no-properties begin pos-before-blank)))
2305 (list 'quote-section
2306 (list :begin begin
2307 :end end
2308 :value value
2309 :post-blank (count-lines pos-before-blank end))))))
2311 (defun org-element-quote-section-interpreter (quote-section contents)
2312 "Interpret QUOTE-SECTION element as Org syntax.
2313 CONTENTS is nil."
2314 (org-element-property :value quote-section))
2317 ;;;; Src Block
2319 (defun org-element-src-block-parser (limit affiliated)
2320 "Parse a src block.
2322 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2323 the buffer position at the beginning of the first affiliated
2324 keyword and CDR is a plist of affiliated keywords along with
2325 their value.
2327 Return a list whose CAR is `src-block' and CDR is a plist
2328 containing `:language', `:switches', `:parameters', `:begin',
2329 `:end', `:hiddenp', `:number-lines', `:retain-labels',
2330 `:use-labels', `:label-fmt', `:preserve-indent', `:value',
2331 `:post-blank' and `:post-affiliated' keywords.
2333 Assume point is at the beginning of the block."
2334 (let ((case-fold-search t))
2335 (if (not (save-excursion (re-search-forward "^[ \t]*#\\+END_SRC[ \t]*$"
2336 limit t)))
2337 ;; Incomplete block: parse it as a paragraph.
2338 (org-element-paragraph-parser limit affiliated)
2339 (let ((contents-end (match-beginning 0)))
2340 (save-excursion
2341 (let* ((begin (car affiliated))
2342 (post-affiliated (point))
2343 ;; Get language as a string.
2344 (language
2345 (progn
2346 (looking-at
2347 (concat "^[ \t]*#\\+BEGIN_SRC"
2348 "\\(?: +\\(\\S-+\\)\\)?"
2349 "\\(\\(?: +\\(?:-l \".*?\"\\|[-+][A-Za-z]\\)\\)+\\)?"
2350 "\\(.*\\)[ \t]*$"))
2351 (org-match-string-no-properties 1)))
2352 ;; Get switches.
2353 (switches (org-match-string-no-properties 2))
2354 ;; Get parameters.
2355 (parameters (org-match-string-no-properties 3))
2356 ;; Switches analysis
2357 (number-lines
2358 (cond ((not switches) nil)
2359 ((string-match "-n\\>" switches) 'new)
2360 ((string-match "+n\\>" switches) 'continued)))
2361 (preserve-indent (or org-src-preserve-indentation
2362 (and switches
2363 (string-match "-i\\>" switches))))
2364 (label-fmt
2365 (and switches
2366 (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
2367 (match-string 1 switches)))
2368 ;; Should labels be retained in (or stripped from)
2369 ;; src blocks?
2370 (retain-labels
2371 (or (not switches)
2372 (not (string-match "-r\\>" switches))
2373 (and number-lines (string-match "-k\\>" switches))))
2374 ;; What should code-references use - labels or
2375 ;; line-numbers?
2376 (use-labels
2377 (or (not switches)
2378 (and retain-labels
2379 (not (string-match "-k\\>" switches)))))
2380 ;; Indentation.
2381 (block-ind (progn (skip-chars-forward " \t") (current-column)))
2382 ;; Get visibility status.
2383 (hidden (progn (forward-line) (org-invisible-p2)))
2384 ;; Retrieve code.
2385 (value (org-element--remove-indentation
2386 (org-unescape-code-in-string
2387 (buffer-substring-no-properties
2388 (point) contents-end))
2389 (and preserve-indent block-ind)))
2390 (pos-before-blank (progn (goto-char contents-end)
2391 (forward-line)
2392 (point)))
2393 ;; Get position after ending blank lines.
2394 (end (progn (skip-chars-forward " \r\t\n" limit)
2395 (skip-chars-backward " \t")
2396 (if (bolp) (point) (line-end-position)))))
2397 (list 'src-block
2398 (nconc
2399 (list :language language
2400 :switches (and (org-string-nw-p switches)
2401 (org-trim switches))
2402 :parameters (and (org-string-nw-p parameters)
2403 (org-trim parameters))
2404 :begin begin
2405 :end end
2406 :number-lines number-lines
2407 :preserve-indent preserve-indent
2408 :retain-labels retain-labels
2409 :use-labels use-labels
2410 :label-fmt label-fmt
2411 :hiddenp hidden
2412 :value value
2413 :post-blank (count-lines pos-before-blank end)
2414 :post-affiliated post-affiliated)
2415 (cdr affiliated)))))))))
2417 (defun org-element-src-block-interpreter (src-block contents)
2418 "Interpret SRC-BLOCK element as Org syntax.
2419 CONTENTS is nil."
2420 (let ((lang (org-element-property :language src-block))
2421 (switches (org-element-property :switches src-block))
2422 (params (org-element-property :parameters src-block))
2423 (value (let ((val (org-element-property :value src-block)))
2424 (cond
2425 ((org-element-property :preserve-indent src-block) val)
2426 ((zerop org-edit-src-content-indentation) val)
2428 (let ((ind (make-string
2429 org-edit-src-content-indentation 32)))
2430 (replace-regexp-in-string
2431 "\\(^\\)[ \t]*\\S-" ind val nil nil 1)))))))
2432 (concat (format "#+BEGIN_SRC%s\n"
2433 (concat (and lang (concat " " lang))
2434 (and switches (concat " " switches))
2435 (and params (concat " " params))))
2436 (org-escape-code-in-string value)
2437 "#+END_SRC")))
2440 ;;;; Table
2442 (defun org-element-table-parser (limit affiliated)
2443 "Parse a table at point.
2445 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2446 the buffer position at the beginning of the first affiliated
2447 keyword and CDR is a plist of affiliated keywords along with
2448 their value.
2450 Return a list whose CAR is `table' and CDR is a plist containing
2451 `:begin', `:end', `:tblfm', `:type', `:contents-begin',
2452 `:contents-end', `:value', `:post-blank' and `:post-affiliated'
2453 keywords.
2455 Assume point is at the beginning of the table."
2456 (save-excursion
2457 (let* ((case-fold-search t)
2458 (table-begin (point))
2459 (type (if (org-at-table.el-p) 'table.el 'org))
2460 (begin (car affiliated))
2461 (table-end
2462 (if (re-search-forward org-table-any-border-regexp limit 'm)
2463 (goto-char (match-beginning 0))
2464 (point)))
2465 (tblfm (let (acc)
2466 (while (looking-at "[ \t]*#\\+TBLFM: +\\(.*\\)[ \t]*$")
2467 (push (org-match-string-no-properties 1) acc)
2468 (forward-line))
2469 acc))
2470 (pos-before-blank (point))
2471 (end (progn (skip-chars-forward " \r\t\n" limit)
2472 (skip-chars-backward " \t")
2473 (if (bolp) (point) (line-end-position)))))
2474 (list 'table
2475 (nconc
2476 (list :begin begin
2477 :end end
2478 :type type
2479 :tblfm tblfm
2480 ;; Only `org' tables have contents. `table.el' tables
2481 ;; use a `:value' property to store raw table as
2482 ;; a string.
2483 :contents-begin (and (eq type 'org) table-begin)
2484 :contents-end (and (eq type 'org) table-end)
2485 :value (and (eq type 'table.el)
2486 (buffer-substring-no-properties
2487 table-begin table-end))
2488 :post-blank (count-lines pos-before-blank end)
2489 :post-affiliated table-begin)
2490 (cdr affiliated))))))
2492 (defun org-element-table-interpreter (table contents)
2493 "Interpret TABLE element as Org syntax.
2494 CONTENTS is nil."
2495 (if (eq (org-element-property :type table) 'table.el)
2496 (org-remove-indentation (org-element-property :value table))
2497 (concat (with-temp-buffer (insert contents)
2498 (org-table-align)
2499 (buffer-string))
2500 (mapconcat (lambda (fm) (concat "#+TBLFM: " fm))
2501 (reverse (org-element-property :tblfm table))
2502 "\n"))))
2505 ;;;; Table Row
2507 (defun org-element-table-row-parser (limit)
2508 "Parse table row at point.
2510 LIMIT bounds the search.
2512 Return a list whose CAR is `table-row' and CDR is a plist
2513 containing `:begin', `:end', `:contents-begin', `:contents-end',
2514 `:type' and `:post-blank' keywords."
2515 (save-excursion
2516 (let* ((type (if (looking-at "^[ \t]*|-") 'rule 'standard))
2517 (begin (point))
2518 ;; A table rule has no contents. In that case, ensure
2519 ;; CONTENTS-BEGIN matches CONTENTS-END.
2520 (contents-begin (and (eq type 'standard)
2521 (search-forward "|")
2522 (point)))
2523 (contents-end (and (eq type 'standard)
2524 (progn
2525 (end-of-line)
2526 (skip-chars-backward " \t")
2527 (point))))
2528 (end (progn (forward-line) (point))))
2529 (list 'table-row
2530 (list :type type
2531 :begin begin
2532 :end end
2533 :contents-begin contents-begin
2534 :contents-end contents-end
2535 :post-blank 0)))))
2537 (defun org-element-table-row-interpreter (table-row contents)
2538 "Interpret TABLE-ROW element as Org syntax.
2539 CONTENTS is the contents of the table row."
2540 (if (eq (org-element-property :type table-row) 'rule) "|-"
2541 (concat "| " contents)))
2544 ;;;; Verse Block
2546 (defun org-element-verse-block-parser (limit affiliated)
2547 "Parse a verse block.
2549 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2550 the buffer position at the beginning of the first affiliated
2551 keyword and CDR is a plist of affiliated keywords along with
2552 their value.
2554 Return a list whose CAR is `verse-block' and CDR is a plist
2555 containing `:begin', `:end', `:contents-begin', `:contents-end',
2556 `:hiddenp', `:post-blank' and `:post-affiliated' keywords.
2558 Assume point is at beginning of the block."
2559 (let ((case-fold-search t))
2560 (if (not (save-excursion
2561 (re-search-forward "^[ \t]*#\\+END_VERSE[ \t]*$" limit t)))
2562 ;; Incomplete block: parse it as a paragraph.
2563 (org-element-paragraph-parser limit affiliated)
2564 (let ((contents-end (match-beginning 0)))
2565 (save-excursion
2566 (let* ((begin (car affiliated))
2567 (post-affiliated (point))
2568 (hidden (progn (forward-line) (org-invisible-p2)))
2569 (contents-begin (point))
2570 (pos-before-blank (progn (goto-char contents-end)
2571 (forward-line)
2572 (point)))
2573 (end (progn (skip-chars-forward " \r\t\n" limit)
2574 (skip-chars-backward " \t")
2575 (if (bolp) (point) (line-end-position)))))
2576 (list 'verse-block
2577 (nconc
2578 (list :begin begin
2579 :end end
2580 :contents-begin contents-begin
2581 :contents-end contents-end
2582 :hiddenp hidden
2583 :post-blank (count-lines pos-before-blank end)
2584 :post-affiliated post-affiliated)
2585 (cdr affiliated)))))))))
2587 (defun org-element-verse-block-interpreter (verse-block contents)
2588 "Interpret VERSE-BLOCK element as Org syntax.
2589 CONTENTS is verse block contents."
2590 (format "#+BEGIN_VERSE\n%s#+END_VERSE" contents))
2594 ;;; Objects
2596 ;; Unlike to elements, interstices can be found between objects.
2597 ;; That's why, along with the parser, successor functions are provided
2598 ;; for each object. Some objects share the same successor (i.e. `code'
2599 ;; and `verbatim' objects).
2601 ;; A successor must accept a single argument bounding the search. It
2602 ;; will return either a cons cell whose CAR is the object's type, as
2603 ;; a symbol, and CDR the position of its next occurrence, or nil.
2605 ;; Successors follow the naming convention:
2606 ;; org-element-NAME-successor, where NAME is the name of the
2607 ;; successor, as defined in `org-element-all-successors'.
2609 ;; Some object types (i.e. `italic') are recursive. Restrictions on
2610 ;; object types they can contain will be specified in
2611 ;; `org-element-object-restrictions'.
2613 ;; Adding a new type of object is simple. Implement a successor,
2614 ;; a parser, and an interpreter for it, all following the naming
2615 ;; convention. Register type in `org-element-all-objects' and
2616 ;; successor in `org-element-all-successors'. Maybe tweak
2617 ;; restrictions about it, and that's it.
2620 ;;;; Bold
2622 (defun org-element-bold-parser ()
2623 "Parse bold object at point.
2625 Return a list whose CAR is `bold' and CDR is a plist with
2626 `:begin', `:end', `:contents-begin' and `:contents-end' and
2627 `:post-blank' keywords.
2629 Assume point is at the first star marker."
2630 (save-excursion
2631 (unless (bolp) (backward-char 1))
2632 (looking-at org-emph-re)
2633 (let ((begin (match-beginning 2))
2634 (contents-begin (match-beginning 4))
2635 (contents-end (match-end 4))
2636 (post-blank (progn (goto-char (match-end 2))
2637 (skip-chars-forward " \t")))
2638 (end (point)))
2639 (list 'bold
2640 (list :begin begin
2641 :end end
2642 :contents-begin contents-begin
2643 :contents-end contents-end
2644 :post-blank post-blank)))))
2646 (defun org-element-bold-interpreter (bold contents)
2647 "Interpret BOLD object as Org syntax.
2648 CONTENTS is the contents of the object."
2649 (format "*%s*" contents))
2651 (defun org-element-text-markup-successor ()
2652 "Search for the next text-markup object.
2654 Return value is a cons cell whose CAR is a symbol among `bold',
2655 `italic', `underline', `strike-through', `code' and `verbatim'
2656 and CDR is beginning position."
2657 (save-excursion
2658 (unless (bolp) (backward-char))
2659 (when (re-search-forward org-emph-re nil t)
2660 (let ((marker (match-string 3)))
2661 (cons (cond
2662 ((equal marker "*") 'bold)
2663 ((equal marker "/") 'italic)
2664 ((equal marker "_") 'underline)
2665 ((equal marker "+") 'strike-through)
2666 ((equal marker "~") 'code)
2667 ((equal marker "=") 'verbatim)
2668 (t (error "Unknown marker at %d" (match-beginning 3))))
2669 (match-beginning 2))))))
2672 ;;;; Code
2674 (defun org-element-code-parser ()
2675 "Parse code object at point.
2677 Return a list whose CAR is `code' and CDR is a plist with
2678 `:value', `:begin', `:end' and `:post-blank' keywords.
2680 Assume point is at the first tilde marker."
2681 (save-excursion
2682 (unless (bolp) (backward-char 1))
2683 (looking-at org-emph-re)
2684 (let ((begin (match-beginning 2))
2685 (value (org-match-string-no-properties 4))
2686 (post-blank (progn (goto-char (match-end 2))
2687 (skip-chars-forward " \t")))
2688 (end (point)))
2689 (list 'code
2690 (list :value value
2691 :begin begin
2692 :end end
2693 :post-blank post-blank)))))
2695 (defun org-element-code-interpreter (code contents)
2696 "Interpret CODE object as Org syntax.
2697 CONTENTS is nil."
2698 (format "~%s~" (org-element-property :value code)))
2701 ;;;; Entity
2703 (defun org-element-entity-parser ()
2704 "Parse entity at point.
2706 Return a list whose CAR is `entity' and CDR a plist with
2707 `:begin', `:end', `:latex', `:latex-math-p', `:html', `:latin1',
2708 `:utf-8', `:ascii', `:use-brackets-p' and `:post-blank' as
2709 keywords.
2711 Assume point is at the beginning of the entity."
2712 (save-excursion
2713 (looking-at "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)")
2714 (let* ((value (org-entity-get (match-string 1)))
2715 (begin (match-beginning 0))
2716 (bracketsp (string= (match-string 2) "{}"))
2717 (post-blank (progn (goto-char (match-end 1))
2718 (when bracketsp (forward-char 2))
2719 (skip-chars-forward " \t")))
2720 (end (point)))
2721 (list 'entity
2722 (list :name (car value)
2723 :latex (nth 1 value)
2724 :latex-math-p (nth 2 value)
2725 :html (nth 3 value)
2726 :ascii (nth 4 value)
2727 :latin1 (nth 5 value)
2728 :utf-8 (nth 6 value)
2729 :begin begin
2730 :end end
2731 :use-brackets-p bracketsp
2732 :post-blank post-blank)))))
2734 (defun org-element-entity-interpreter (entity contents)
2735 "Interpret ENTITY object as Org syntax.
2736 CONTENTS is nil."
2737 (concat "\\"
2738 (org-element-property :name entity)
2739 (when (org-element-property :use-brackets-p entity) "{}")))
2741 (defun org-element-latex-or-entity-successor ()
2742 "Search for the next latex-fragment or entity object.
2744 Return value is a cons cell whose CAR is `entity' or
2745 `latex-fragment' and CDR is beginning position."
2746 (save-excursion
2747 (unless (bolp) (backward-char))
2748 (let ((matchers (cdr org-latex-regexps))
2749 ;; ENTITY-RE matches both LaTeX commands and Org entities.
2750 (entity-re
2751 "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))
2752 (when (re-search-forward
2753 (concat (mapconcat #'cadr matchers "\\|") "\\|" entity-re) nil t)
2754 (goto-char (match-beginning 0))
2755 (if (looking-at entity-re)
2756 ;; Determine if it's a real entity or a LaTeX command.
2757 (cons (if (org-entity-get (match-string 1)) 'entity 'latex-fragment)
2758 (match-beginning 0))
2759 ;; No entity nor command: point is at a LaTeX fragment.
2760 ;; Determine its type to get the correct beginning position.
2761 (cons 'latex-fragment
2762 (catch 'return
2763 (dolist (e matchers)
2764 (when (looking-at (nth 1 e))
2765 (throw 'return (match-beginning (nth 2 e)))))
2766 (point))))))))
2769 ;;;; Export Snippet
2771 (defun org-element-export-snippet-parser ()
2772 "Parse export snippet at point.
2774 Return a list whose CAR is `export-snippet' and CDR a plist with
2775 `:begin', `:end', `:back-end', `:value' and `:post-blank' as
2776 keywords.
2778 Assume point is at the beginning of the snippet."
2779 (save-excursion
2780 (re-search-forward "@@\\([-A-Za-z0-9]+\\):" nil t)
2781 (let* ((begin (match-beginning 0))
2782 (back-end (org-match-string-no-properties 1))
2783 (value (buffer-substring-no-properties
2784 (point)
2785 (progn (re-search-forward "@@" nil t) (match-beginning 0))))
2786 (post-blank (skip-chars-forward " \t"))
2787 (end (point)))
2788 (list 'export-snippet
2789 (list :back-end back-end
2790 :value value
2791 :begin begin
2792 :end end
2793 :post-blank post-blank)))))
2795 (defun org-element-export-snippet-interpreter (export-snippet contents)
2796 "Interpret EXPORT-SNIPPET object as Org syntax.
2797 CONTENTS is nil."
2798 (format "@@%s:%s@@"
2799 (org-element-property :back-end export-snippet)
2800 (org-element-property :value export-snippet)))
2802 (defun org-element-export-snippet-successor ()
2803 "Search for the next export-snippet object.
2805 Return value is a cons cell whose CAR is `export-snippet' and CDR
2806 its beginning position."
2807 (save-excursion
2808 (let (beg)
2809 (when (and (re-search-forward "@@[-A-Za-z0-9]+:" nil t)
2810 (setq beg (match-beginning 0))
2811 (search-forward "@@" nil t))
2812 (cons 'export-snippet beg)))))
2815 ;;;; Footnote Reference
2817 (defun org-element-footnote-reference-parser ()
2818 "Parse footnote reference at point.
2820 Return a list whose CAR is `footnote-reference' and CDR a plist
2821 with `:label', `:type', `:inline-definition', `:begin', `:end'
2822 and `:post-blank' as keywords."
2823 (save-excursion
2824 (looking-at org-footnote-re)
2825 (let* ((begin (point))
2826 (label (or (org-match-string-no-properties 2)
2827 (org-match-string-no-properties 3)
2828 (and (match-string 1)
2829 (concat "fn:" (org-match-string-no-properties 1)))))
2830 (type (if (or (not label) (match-string 1)) 'inline 'standard))
2831 (inner-begin (match-end 0))
2832 (inner-end
2833 (let ((count 1))
2834 (forward-char)
2835 (while (and (> count 0) (re-search-forward "[][]" nil t))
2836 (if (equal (match-string 0) "[") (incf count) (decf count)))
2837 (1- (point))))
2838 (post-blank (progn (goto-char (1+ inner-end))
2839 (skip-chars-forward " \t")))
2840 (end (point))
2841 (footnote-reference
2842 (list 'footnote-reference
2843 (list :label label
2844 :type type
2845 :begin begin
2846 :end end
2847 :post-blank post-blank))))
2848 (org-element-put-property
2849 footnote-reference :inline-definition
2850 (and (eq type 'inline)
2851 (org-element-parse-secondary-string
2852 (buffer-substring inner-begin inner-end)
2853 (org-element-restriction 'footnote-reference)
2854 footnote-reference))))))
2856 (defun org-element-footnote-reference-interpreter (footnote-reference contents)
2857 "Interpret FOOTNOTE-REFERENCE object as Org syntax.
2858 CONTENTS is nil."
2859 (let ((label (or (org-element-property :label footnote-reference) "fn:"))
2860 (def
2861 (let ((inline-def
2862 (org-element-property :inline-definition footnote-reference)))
2863 (if (not inline-def) ""
2864 (concat ":" (org-element-interpret-data inline-def))))))
2865 (format "[%s]" (concat label def))))
2867 (defun org-element-footnote-reference-successor ()
2868 "Search for the next footnote-reference object.
2870 Return value is a cons cell whose CAR is `footnote-reference' and
2871 CDR is beginning position."
2872 (save-excursion
2873 (catch 'exit
2874 (while (re-search-forward org-footnote-re nil t)
2875 (save-excursion
2876 (let ((beg (match-beginning 0))
2877 (count 1))
2878 (backward-char)
2879 (while (re-search-forward "[][]" nil t)
2880 (if (equal (match-string 0) "[") (incf count) (decf count))
2881 (when (zerop count)
2882 (throw 'exit (cons 'footnote-reference beg))))))))))
2885 ;;;; Inline Babel Call
2887 (defun org-element-inline-babel-call-parser ()
2888 "Parse inline babel call at point.
2890 Return a list whose CAR is `inline-babel-call' and CDR a plist
2891 with `:begin', `:end', `:info' and `:post-blank' as keywords.
2893 Assume point is at the beginning of the babel call."
2894 (save-excursion
2895 (unless (bolp) (backward-char))
2896 (looking-at org-babel-inline-lob-one-liner-regexp)
2897 (let ((info (save-match-data (org-babel-lob-get-info)))
2898 (begin (match-end 1))
2899 (post-blank (progn (goto-char (match-end 0))
2900 (skip-chars-forward " \t")))
2901 (end (point)))
2902 (list 'inline-babel-call
2903 (list :begin begin
2904 :end end
2905 :info info
2906 :post-blank post-blank)))))
2908 (defun org-element-inline-babel-call-interpreter (inline-babel-call contents)
2909 "Interpret INLINE-BABEL-CALL object as Org syntax.
2910 CONTENTS is nil."
2911 (let* ((babel-info (org-element-property :info inline-babel-call))
2912 (main-source (car babel-info))
2913 (post-options (nth 1 babel-info)))
2914 (concat "call_"
2915 (if (string-match "\\[\\(\\[.*?\\]\\)\\]" main-source)
2916 ;; Remove redundant square brackets.
2917 (replace-match
2918 (match-string 1 main-source) nil nil main-source)
2919 main-source)
2920 (and post-options (format "[%s]" post-options)))))
2922 (defun org-element-inline-babel-call-successor ()
2923 "Search for the next inline-babel-call object.
2925 Return value is a cons cell whose CAR is `inline-babel-call' and
2926 CDR is beginning position."
2927 (save-excursion
2928 ;; Use a simplified version of
2929 ;; `org-babel-inline-lob-one-liner-regexp'.
2930 (when (re-search-forward
2931 "call_\\([^()\n]+?\\)\\(?:\\[.*?\\]\\)?([^\n]*?)\\(\\[.*?\\]\\)?"
2932 nil t)
2933 (cons 'inline-babel-call (match-beginning 0)))))
2936 ;;;; Inline Src Block
2938 (defun org-element-inline-src-block-parser ()
2939 "Parse inline source block at point.
2941 Return a list whose CAR is `inline-src-block' and CDR a plist
2942 with `:begin', `:end', `:language', `:value', `:parameters' and
2943 `:post-blank' as keywords.
2945 Assume point is at the beginning of the inline src block."
2946 (save-excursion
2947 (unless (bolp) (backward-char))
2948 (looking-at org-babel-inline-src-block-regexp)
2949 (let ((begin (match-beginning 1))
2950 (language (org-match-string-no-properties 2))
2951 (parameters (org-match-string-no-properties 4))
2952 (value (org-match-string-no-properties 5))
2953 (post-blank (progn (goto-char (match-end 0))
2954 (skip-chars-forward " \t")))
2955 (end (point)))
2956 (list 'inline-src-block
2957 (list :language language
2958 :value value
2959 :parameters parameters
2960 :begin begin
2961 :end end
2962 :post-blank post-blank)))))
2964 (defun org-element-inline-src-block-interpreter (inline-src-block contents)
2965 "Interpret INLINE-SRC-BLOCK object as Org syntax.
2966 CONTENTS is nil."
2967 (let ((language (org-element-property :language inline-src-block))
2968 (arguments (org-element-property :parameters inline-src-block))
2969 (body (org-element-property :value inline-src-block)))
2970 (format "src_%s%s{%s}"
2971 language
2972 (if arguments (format "[%s]" arguments) "")
2973 body)))
2975 (defun org-element-inline-src-block-successor ()
2976 "Search for the next inline-babel-call element.
2978 Return value is a cons cell whose CAR is `inline-babel-call' and
2979 CDR is beginning position."
2980 (save-excursion
2981 (unless (bolp) (backward-char))
2982 (when (re-search-forward org-babel-inline-src-block-regexp nil t)
2983 (cons 'inline-src-block (match-beginning 1)))))
2985 ;;;; Italic
2987 (defun org-element-italic-parser ()
2988 "Parse italic object at point.
2990 Return a list whose CAR is `italic' and CDR is a plist with
2991 `:begin', `:end', `:contents-begin' and `:contents-end' and
2992 `:post-blank' keywords.
2994 Assume point is at the first slash marker."
2995 (save-excursion
2996 (unless (bolp) (backward-char 1))
2997 (looking-at org-emph-re)
2998 (let ((begin (match-beginning 2))
2999 (contents-begin (match-beginning 4))
3000 (contents-end (match-end 4))
3001 (post-blank (progn (goto-char (match-end 2))
3002 (skip-chars-forward " \t")))
3003 (end (point)))
3004 (list 'italic
3005 (list :begin begin
3006 :end end
3007 :contents-begin contents-begin
3008 :contents-end contents-end
3009 :post-blank post-blank)))))
3011 (defun org-element-italic-interpreter (italic contents)
3012 "Interpret ITALIC object as Org syntax.
3013 CONTENTS is the contents of the object."
3014 (format "/%s/" contents))
3017 ;;;; Latex Fragment
3019 (defun org-element-latex-fragment-parser ()
3020 "Parse LaTeX fragment at point.
3022 Return a list whose CAR is `latex-fragment' and CDR a plist with
3023 `:value', `:begin', `:end', and `:post-blank' as keywords.
3025 Assume point is at the beginning of the LaTeX fragment."
3026 (save-excursion
3027 (let* ((begin (point))
3028 (substring-match
3029 (catch 'exit
3030 (dolist (e (cdr org-latex-regexps))
3031 (let ((latex-regexp (nth 1 e)))
3032 (when (or (looking-at latex-regexp)
3033 (and (not (bobp))
3034 (save-excursion
3035 (backward-char)
3036 (looking-at latex-regexp))))
3037 (throw 'exit (nth 2 e)))))
3038 ;; None found: it's a macro.
3039 (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")
3041 (value (org-match-string-no-properties substring-match))
3042 (post-blank (progn (goto-char (match-end substring-match))
3043 (skip-chars-forward " \t")))
3044 (end (point)))
3045 (list 'latex-fragment
3046 (list :value value
3047 :begin begin
3048 :end end
3049 :post-blank post-blank)))))
3051 (defun org-element-latex-fragment-interpreter (latex-fragment contents)
3052 "Interpret LATEX-FRAGMENT object as Org syntax.
3053 CONTENTS is nil."
3054 (org-element-property :value latex-fragment))
3056 ;;;; Line Break
3058 (defun org-element-line-break-parser ()
3059 "Parse line break at point.
3061 Return a list whose CAR is `line-break', and CDR a plist with
3062 `:begin', `:end' and `:post-blank' keywords.
3064 Assume point is at the beginning of the line break."
3065 (list 'line-break
3066 (list :begin (point)
3067 :end (progn (forward-line) (point))
3068 :post-blank 0)))
3070 (defun org-element-line-break-interpreter (line-break contents)
3071 "Interpret LINE-BREAK object as Org syntax.
3072 CONTENTS is nil."
3073 "\\\\\n")
3075 (defun org-element-line-break-successor ()
3076 "Search for the next line-break object.
3078 Return value is a cons cell whose CAR is `line-break' and CDR is
3079 beginning position."
3080 (save-excursion
3081 (let ((beg (and (re-search-forward "[^\\\\]\\(\\\\\\\\\\)[ \t]*$" nil t)
3082 (goto-char (match-beginning 1)))))
3083 ;; A line break can only happen on a non-empty line.
3084 (when (and beg (re-search-backward "\\S-" (point-at-bol) t))
3085 (cons 'line-break beg)))))
3088 ;;;; Link
3090 (defun org-element-link-parser ()
3091 "Parse link at point.
3093 Return a list whose CAR is `link' and CDR a plist with `:type',
3094 `:path', `:raw-link', `:application', `:search-option', `:begin',
3095 `:end', `:contents-begin', `:contents-end' and `:post-blank' as
3096 keywords.
3098 Assume point is at the beginning of the link."
3099 (save-excursion
3100 (let ((begin (point))
3101 end contents-begin contents-end link-end post-blank path type
3102 raw-link link search-option application)
3103 (cond
3104 ;; Type 1: Text targeted from a radio target.
3105 ((and org-target-link-regexp (looking-at org-target-link-regexp))
3106 (setq type "radio"
3107 link-end (match-end 0)
3108 path (org-match-string-no-properties 0)))
3109 ;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]]
3110 ((looking-at org-bracket-link-regexp)
3111 (setq contents-begin (match-beginning 3)
3112 contents-end (match-end 3)
3113 link-end (match-end 0)
3114 ;; RAW-LINK is the original link. Expand any
3115 ;; abbreviation in it.
3116 raw-link (org-translate-link
3117 (org-link-expand-abbrev
3118 (org-match-string-no-properties 1))))
3119 ;; Determine TYPE of link and set PATH accordingly.
3120 (cond
3121 ;; File type.
3122 ((or (file-name-absolute-p raw-link)
3123 (string-match "^\\.\\.?/" raw-link))
3124 (setq type "file" path raw-link))
3125 ;; Explicit type (http, irc, bbdb...). See `org-link-types'.
3126 ((string-match org-link-re-with-space3 raw-link)
3127 (setq type (match-string 1 raw-link) path (match-string 2 raw-link)))
3128 ;; Id type: PATH is the id.
3129 ((string-match "^id:\\([-a-f0-9]+\\)" raw-link)
3130 (setq type "id" path (match-string 1 raw-link)))
3131 ;; Code-ref type: PATH is the name of the reference.
3132 ((string-match "^(\\(.*\\))$" raw-link)
3133 (setq type "coderef" path (match-string 1 raw-link)))
3134 ;; Custom-id type: PATH is the name of the custom id.
3135 ((= (aref raw-link 0) ?#)
3136 (setq type "custom-id" path (substring raw-link 1)))
3137 ;; Fuzzy type: Internal link either matches a target, an
3138 ;; headline name or nothing. PATH is the target or
3139 ;; headline's name.
3140 (t (setq type "fuzzy" path raw-link))))
3141 ;; Type 3: Plain link, i.e. http://orgmode.org
3142 ((looking-at org-plain-link-re)
3143 (setq raw-link (org-match-string-no-properties 0)
3144 type (org-match-string-no-properties 1)
3145 link-end (match-end 0)
3146 path (org-match-string-no-properties 2)))
3147 ;; Type 4: Angular link, i.e. <http://orgmode.org>
3148 ((looking-at org-angle-link-re)
3149 (setq raw-link (buffer-substring-no-properties
3150 (match-beginning 1) (match-end 2))
3151 type (org-match-string-no-properties 1)
3152 link-end (match-end 0)
3153 path (org-match-string-no-properties 2))))
3154 ;; In any case, deduce end point after trailing white space from
3155 ;; LINK-END variable.
3156 (setq post-blank (progn (goto-char link-end) (skip-chars-forward " \t"))
3157 end (point))
3158 ;; Extract search option and opening application out of
3159 ;; "file"-type links.
3160 (when (member type org-element-link-type-is-file)
3161 ;; Application.
3162 (cond ((string-match "^file\\+\\(.*\\)$" type)
3163 (setq application (match-string 1 type)))
3164 ((not (string-match "^file" type))
3165 (setq application type)))
3166 ;; Extract search option from PATH.
3167 (when (string-match "::\\(.*\\)$" path)
3168 (setq search-option (match-string 1 path)
3169 path (replace-match "" nil nil path)))
3170 ;; Make sure TYPE always reports "file".
3171 (setq type "file"))
3172 (list 'link
3173 (list :type type
3174 :path path
3175 :raw-link (or raw-link path)
3176 :application application
3177 :search-option search-option
3178 :begin begin
3179 :end end
3180 :contents-begin contents-begin
3181 :contents-end contents-end
3182 :post-blank post-blank)))))
3184 (defun org-element-link-interpreter (link contents)
3185 "Interpret LINK object as Org syntax.
3186 CONTENTS is the contents of the object, or nil."
3187 (let ((type (org-element-property :type link))
3188 (raw-link (org-element-property :raw-link link)))
3189 (if (string= type "radio") raw-link
3190 (format "[[%s]%s]"
3191 raw-link
3192 (if contents (format "[%s]" contents) "")))))
3194 (defun org-element-link-successor ()
3195 "Search for the next link object.
3197 Return value is a cons cell whose CAR is `link' and CDR is
3198 beginning position."
3199 (save-excursion
3200 (let ((link-regexp
3201 (if (not org-target-link-regexp) org-any-link-re
3202 (concat org-any-link-re "\\|" org-target-link-regexp))))
3203 (when (re-search-forward link-regexp nil t)
3204 (cons 'link (match-beginning 0))))))
3206 (defun org-element-plain-link-successor ()
3207 "Search for the next plain link object.
3209 Return value is a cons cell whose CAR is `link' and CDR is
3210 beginning position."
3211 (and (save-excursion (re-search-forward org-plain-link-re nil t))
3212 (cons 'link (match-beginning 0))))
3215 ;;;; Macro
3217 (defun org-element-macro-parser ()
3218 "Parse macro at point.
3220 Return a list whose CAR is `macro' and CDR a plist with `:key',
3221 `:args', `:begin', `:end', `:value' and `:post-blank' as
3222 keywords.
3224 Assume point is at the macro."
3225 (save-excursion
3226 (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
3227 (let ((begin (point))
3228 (key (downcase (org-match-string-no-properties 1)))
3229 (value (org-match-string-no-properties 0))
3230 (post-blank (progn (goto-char (match-end 0))
3231 (skip-chars-forward " \t")))
3232 (end (point))
3233 (args (let ((args (org-match-string-no-properties 3)))
3234 (when args
3235 ;; Do not use `org-split-string' since empty
3236 ;; strings are meaningful here.
3237 (split-string
3238 (replace-regexp-in-string
3239 "\\(\\\\*\\)\\(,\\)"
3240 (lambda (str)
3241 (let ((len (length (match-string 1 str))))
3242 (concat (make-string (/ len 2) ?\\)
3243 (if (zerop (mod len 2)) "\000" ","))))
3244 args nil t)
3245 "\000")))))
3246 (list 'macro
3247 (list :key key
3248 :value value
3249 :args args
3250 :begin begin
3251 :end end
3252 :post-blank post-blank)))))
3254 (defun org-element-macro-interpreter (macro contents)
3255 "Interpret MACRO object as Org syntax.
3256 CONTENTS is nil."
3257 (org-element-property :value macro))
3259 (defun org-element-macro-successor ()
3260 "Search for the next macro object.
3262 Return value is cons cell whose CAR is `macro' and CDR is
3263 beginning position."
3264 (save-excursion
3265 (when (re-search-forward
3266 "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
3267 nil t)
3268 (cons 'macro (match-beginning 0)))))
3271 ;;;; Radio-target
3273 (defun org-element-radio-target-parser ()
3274 "Parse radio target at point.
3276 Return a list whose CAR is `radio-target' and CDR a plist with
3277 `:begin', `:end', `:contents-begin', `:contents-end', `:value'
3278 and `:post-blank' as keywords.
3280 Assume point is at the radio target."
3281 (save-excursion
3282 (looking-at org-radio-target-regexp)
3283 (let ((begin (point))
3284 (contents-begin (match-beginning 1))
3285 (contents-end (match-end 1))
3286 (value (org-match-string-no-properties 1))
3287 (post-blank (progn (goto-char (match-end 0))
3288 (skip-chars-forward " \t")))
3289 (end (point)))
3290 (list 'radio-target
3291 (list :begin begin
3292 :end end
3293 :contents-begin contents-begin
3294 :contents-end contents-end
3295 :post-blank post-blank
3296 :value value)))))
3298 (defun org-element-radio-target-interpreter (target contents)
3299 "Interpret TARGET object as Org syntax.
3300 CONTENTS is the contents of the object."
3301 (concat "<<<" contents ">>>"))
3303 (defun org-element-radio-target-successor ()
3304 "Search for the next radio-target object.
3306 Return value is a cons cell whose CAR is `radio-target' and CDR
3307 is beginning position."
3308 (save-excursion
3309 (when (re-search-forward org-radio-target-regexp nil t)
3310 (cons 'radio-target (match-beginning 0)))))
3313 ;;;; Statistics Cookie
3315 (defun org-element-statistics-cookie-parser ()
3316 "Parse statistics cookie at point.
3318 Return a list whose CAR is `statistics-cookie', and CDR a plist
3319 with `:begin', `:end', `:value' and `:post-blank' keywords.
3321 Assume point is at the beginning of the statistics-cookie."
3322 (save-excursion
3323 (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
3324 (let* ((begin (point))
3325 (value (buffer-substring-no-properties
3326 (match-beginning 0) (match-end 0)))
3327 (post-blank (progn (goto-char (match-end 0))
3328 (skip-chars-forward " \t")))
3329 (end (point)))
3330 (list 'statistics-cookie
3331 (list :begin begin
3332 :end end
3333 :value value
3334 :post-blank post-blank)))))
3336 (defun org-element-statistics-cookie-interpreter (statistics-cookie contents)
3337 "Interpret STATISTICS-COOKIE object as Org syntax.
3338 CONTENTS is nil."
3339 (org-element-property :value statistics-cookie))
3341 (defun org-element-statistics-cookie-successor ()
3342 "Search for the next statistics cookie object.
3344 Return value is a cons cell whose CAR is `statistics-cookie' and
3345 CDR is beginning position."
3346 (save-excursion
3347 (when (re-search-forward "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]" nil t)
3348 (cons 'statistics-cookie (match-beginning 0)))))
3351 ;;;; Strike-Through
3353 (defun org-element-strike-through-parser ()
3354 "Parse strike-through object at point.
3356 Return a list whose CAR is `strike-through' and CDR is a plist
3357 with `:begin', `:end', `:contents-begin' and `:contents-end' and
3358 `:post-blank' keywords.
3360 Assume point is at the first plus sign marker."
3361 (save-excursion
3362 (unless (bolp) (backward-char 1))
3363 (looking-at org-emph-re)
3364 (let ((begin (match-beginning 2))
3365 (contents-begin (match-beginning 4))
3366 (contents-end (match-end 4))
3367 (post-blank (progn (goto-char (match-end 2))
3368 (skip-chars-forward " \t")))
3369 (end (point)))
3370 (list 'strike-through
3371 (list :begin begin
3372 :end end
3373 :contents-begin contents-begin
3374 :contents-end contents-end
3375 :post-blank post-blank)))))
3377 (defun org-element-strike-through-interpreter (strike-through contents)
3378 "Interpret STRIKE-THROUGH object as Org syntax.
3379 CONTENTS is the contents of the object."
3380 (format "+%s+" contents))
3383 ;;;; Subscript
3385 (defun org-element-subscript-parser ()
3386 "Parse subscript at point.
3388 Return a list whose CAR is `subscript' and CDR a plist with
3389 `:begin', `:end', `:contents-begin', `:contents-end',
3390 `:use-brackets-p' and `:post-blank' as keywords.
3392 Assume point is at the underscore."
3393 (save-excursion
3394 (unless (bolp) (backward-char))
3395 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp)
3397 (not (looking-at org-match-substring-regexp))))
3398 (begin (match-beginning 2))
3399 (contents-begin (or (match-beginning 5)
3400 (match-beginning 3)))
3401 (contents-end (or (match-end 5) (match-end 3)))
3402 (post-blank (progn (goto-char (match-end 0))
3403 (skip-chars-forward " \t")))
3404 (end (point)))
3405 (list 'subscript
3406 (list :begin begin
3407 :end end
3408 :use-brackets-p bracketsp
3409 :contents-begin contents-begin
3410 :contents-end contents-end
3411 :post-blank post-blank)))))
3413 (defun org-element-subscript-interpreter (subscript contents)
3414 "Interpret SUBSCRIPT object as Org syntax.
3415 CONTENTS is the contents of the object."
3416 (format
3417 (if (org-element-property :use-brackets-p subscript) "_{%s}" "_%s")
3418 contents))
3420 (defun org-element-sub/superscript-successor ()
3421 "Search for the next sub/superscript object.
3423 Return value is a cons cell whose CAR is either `subscript' or
3424 `superscript' and CDR is beginning position."
3425 (save-excursion
3426 (unless (bolp) (backward-char))
3427 (when (re-search-forward org-match-substring-regexp nil t)
3428 (cons (if (string= (match-string 2) "_") 'subscript 'superscript)
3429 (match-beginning 2)))))
3432 ;;;; Superscript
3434 (defun org-element-superscript-parser ()
3435 "Parse superscript at point.
3437 Return a list whose CAR is `superscript' and CDR a plist with
3438 `:begin', `:end', `:contents-begin', `:contents-end',
3439 `:use-brackets-p' and `:post-blank' as keywords.
3441 Assume point is at the caret."
3442 (save-excursion
3443 (unless (bolp) (backward-char))
3444 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp) t
3445 (not (looking-at org-match-substring-regexp))))
3446 (begin (match-beginning 2))
3447 (contents-begin (or (match-beginning 5)
3448 (match-beginning 3)))
3449 (contents-end (or (match-end 5) (match-end 3)))
3450 (post-blank (progn (goto-char (match-end 0))
3451 (skip-chars-forward " \t")))
3452 (end (point)))
3453 (list 'superscript
3454 (list :begin begin
3455 :end end
3456 :use-brackets-p bracketsp
3457 :contents-begin contents-begin
3458 :contents-end contents-end
3459 :post-blank post-blank)))))
3461 (defun org-element-superscript-interpreter (superscript contents)
3462 "Interpret SUPERSCRIPT object as Org syntax.
3463 CONTENTS is the contents of the object."
3464 (format
3465 (if (org-element-property :use-brackets-p superscript) "^{%s}" "^%s")
3466 contents))
3469 ;;;; Table Cell
3471 (defun org-element-table-cell-parser ()
3472 "Parse table cell at point.
3474 Return a list whose CAR is `table-cell' and CDR is a plist
3475 containing `:begin', `:end', `:contents-begin', `:contents-end'
3476 and `:post-blank' keywords."
3477 (looking-at "[ \t]*\\(.*?\\)[ \t]*|")
3478 (let* ((begin (match-beginning 0))
3479 (end (match-end 0))
3480 (contents-begin (match-beginning 1))
3481 (contents-end (match-end 1)))
3482 (list 'table-cell
3483 (list :begin begin
3484 :end end
3485 :contents-begin contents-begin
3486 :contents-end contents-end
3487 :post-blank 0))))
3489 (defun org-element-table-cell-interpreter (table-cell contents)
3490 "Interpret TABLE-CELL element as Org syntax.
3491 CONTENTS is the contents of the cell, or nil."
3492 (concat " " contents " |"))
3494 (defun org-element-table-cell-successor ()
3495 "Search for the next table-cell object.
3497 Return value is a cons cell whose CAR is `table-cell' and CDR is
3498 beginning position."
3499 (when (looking-at "[ \t]*.*?[ \t]*|") (cons 'table-cell (point))))
3502 ;;;; Target
3504 (defun org-element-target-parser ()
3505 "Parse target at point.
3507 Return a list whose CAR is `target' and CDR a plist with
3508 `:begin', `:end', `:value' and `:post-blank' as keywords.
3510 Assume point is at the target."
3511 (save-excursion
3512 (looking-at org-target-regexp)
3513 (let ((begin (point))
3514 (value (org-match-string-no-properties 1))
3515 (post-blank (progn (goto-char (match-end 0))
3516 (skip-chars-forward " \t")))
3517 (end (point)))
3518 (list 'target
3519 (list :begin begin
3520 :end end
3521 :value value
3522 :post-blank post-blank)))))
3524 (defun org-element-target-interpreter (target contents)
3525 "Interpret TARGET object as Org syntax.
3526 CONTENTS is nil."
3527 (format "<<%s>>" (org-element-property :value target)))
3529 (defun org-element-target-successor ()
3530 "Search for the next target object.
3532 Return value is a cons cell whose CAR is `target' and CDR is
3533 beginning position."
3534 (save-excursion
3535 (when (re-search-forward org-target-regexp nil t)
3536 (cons 'target (match-beginning 0)))))
3539 ;;;; Timestamp
3541 (defun org-element-timestamp-parser ()
3542 "Parse time stamp at point.
3544 Return a list whose CAR is `timestamp', and CDR a plist with
3545 `:type', `:raw-value', `:year-start', `:month-start',
3546 `:day-start', `:hour-start', `:minute-start', `:year-end',
3547 `:month-end', `:day-end', `:hour-end', `:minute-end',
3548 `:repeater-type', `:repeater-value', `:repeater-unit',
3549 `:warning-type', `:warning-value', `:warning-unit', `:begin',
3550 `:end', `:value' and `:post-blank' keywords.
3552 Assume point is at the beginning of the timestamp."
3553 (save-excursion
3554 (let* ((begin (point))
3555 (activep (eq (char-after) ?<))
3556 (raw-value
3557 (progn
3558 (looking-at "\\([<[]\\(%%\\)?.*?\\)[]>]\\(?:--\\([<[].*?[]>]\\)\\)?")
3559 (match-string-no-properties 0)))
3560 (date-start (match-string-no-properties 1))
3561 (date-end (match-string 3))
3562 (diaryp (match-beginning 2))
3563 (post-blank (progn (goto-char (match-end 0))
3564 (skip-chars-forward " \t")))
3565 (end (point))
3566 (time-range
3567 (and (not diaryp)
3568 (string-match
3569 "[012]?[0-9]:[0-5][0-9]\\(-\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)"
3570 date-start)
3571 (cons (string-to-number (match-string 2 date-start))
3572 (string-to-number (match-string 3 date-start)))))
3573 (type (cond (diaryp 'diary)
3574 ((and activep (or date-end time-range)) 'active-range)
3575 (activep 'active)
3576 ((or date-end time-range) 'inactive-range)
3577 (t 'inactive)))
3578 (repeater-props
3579 (and (not diaryp)
3580 (string-match "\\([.+]?\\+\\)\\([0-9]+\\)\\([hdwmy]\\)"
3581 raw-value)
3582 (list
3583 :repeater-type
3584 (let ((type (match-string 1 raw-value)))
3585 (cond ((equal "++" type) 'catch-up)
3586 ((equal ".+" type) 'restart)
3587 (t 'cumulate)))
3588 :repeater-value (string-to-number (match-string 2 raw-value))
3589 :repeater-unit
3590 (case (string-to-char (match-string 3 raw-value))
3591 (?h 'hour) (?d 'day) (?w 'week) (?m 'month) (t 'year)))))
3592 (warning-props
3593 (and (not diaryp)
3594 (string-match "\\(-\\)?-\\([0-9]+\\)\\([hdwmy]\\)" raw-value)
3595 (list
3596 :warning-type (if (match-string 1 raw-value) 'first 'all)
3597 :warning-value (string-to-number (match-string 2 raw-value))
3598 :warning-unit
3599 (case (string-to-char (match-string 3 raw-value))
3600 (?h 'hour) (?d 'day) (?w 'week) (?m 'month) (t 'year)))))
3601 year-start month-start day-start hour-start minute-start year-end
3602 month-end day-end hour-end minute-end)
3603 ;; Parse date-start.
3604 (unless diaryp
3605 (let ((date (org-parse-time-string date-start t)))
3606 (setq year-start (nth 5 date)
3607 month-start (nth 4 date)
3608 day-start (nth 3 date)
3609 hour-start (nth 2 date)
3610 minute-start (nth 1 date))))
3611 ;; Compute date-end. It can be provided directly in time-stamp,
3612 ;; or extracted from time range. Otherwise, it defaults to the
3613 ;; same values as date-start.
3614 (unless diaryp
3615 (let ((date (and date-end (org-parse-time-string date-end t))))
3616 (setq year-end (or (nth 5 date) year-start)
3617 month-end (or (nth 4 date) month-start)
3618 day-end (or (nth 3 date) day-start)
3619 hour-end (or (nth 2 date) (car time-range) hour-start)
3620 minute-end (or (nth 1 date) (cdr time-range) minute-start))))
3621 (list 'timestamp
3622 (nconc (list :type type
3623 :raw-value raw-value
3624 :year-start year-start
3625 :month-start month-start
3626 :day-start day-start
3627 :hour-start hour-start
3628 :minute-start minute-start
3629 :year-end year-end
3630 :month-end month-end
3631 :day-end day-end
3632 :hour-end hour-end
3633 :minute-end minute-end
3634 :begin begin
3635 :end end
3636 :post-blank post-blank)
3637 repeater-props
3638 warning-props)))))
3640 (defun org-element-timestamp-interpreter (timestamp contents)
3641 "Interpret TIMESTAMP object as Org syntax.
3642 CONTENTS is nil."
3643 ;; Use `:raw-value' if specified.
3644 (or (org-element-property :raw-value timestamp)
3645 ;; Otherwise, build timestamp string.
3646 (let* ((repeat-string
3647 (concat
3648 (case (org-element-property :repeater-type timestamp)
3649 (cumulate "+") (catch-up "++") (restart ".+"))
3650 (let ((val (org-element-property :repeater-value timestamp)))
3651 (and val (number-to-string val)))
3652 (case (org-element-property :repeater-unit timestamp)
3653 (hour "h") (day "d") (week "w") (month "m") (year "y"))))
3654 (warning-string
3655 (concat
3656 (case (org-element-property :warning-type timestamp)
3657 (first "--")
3658 (all "-"))
3659 (let ((val (org-element-property :warning-value timestamp)))
3660 (and val (number-to-string val)))
3661 (case (org-element-property :warning-unit timestamp)
3662 (hour "h") (day "d") (week "w") (month "m") (year "y"))))
3663 (build-ts-string
3664 ;; Build an Org timestamp string from TIME. ACTIVEP is
3665 ;; non-nil when time stamp is active. If WITH-TIME-P is
3666 ;; non-nil, add a time part. HOUR-END and MINUTE-END
3667 ;; specify a time range in the timestamp. REPEAT-STRING
3668 ;; is the repeater string, if any.
3669 (lambda (time activep &optional with-time-p hour-end minute-end)
3670 (let ((ts (format-time-string
3671 (funcall (if with-time-p 'cdr 'car)
3672 org-time-stamp-formats)
3673 time)))
3674 (when (and hour-end minute-end)
3675 (string-match "[012]?[0-9]:[0-5][0-9]" ts)
3676 (setq ts
3677 (replace-match
3678 (format "\\&-%02d:%02d" hour-end minute-end)
3679 nil nil ts)))
3680 (unless activep (setq ts (format "[%s]" (substring ts 1 -1))))
3681 (dolist (s (list repeat-string warning-string))
3682 (when (org-string-nw-p s)
3683 (setq ts (concat (substring ts 0 -1)
3686 (substring ts -1)))))
3687 ;; Return value.
3688 ts)))
3689 (type (org-element-property :type timestamp)))
3690 (case type
3691 ((active inactive)
3692 (let* ((minute-start (org-element-property :minute-start timestamp))
3693 (minute-end (org-element-property :minute-end timestamp))
3694 (hour-start (org-element-property :hour-start timestamp))
3695 (hour-end (org-element-property :hour-end timestamp))
3696 (time-range-p (and hour-start hour-end minute-start minute-end
3697 (or (/= hour-start hour-end)
3698 (/= minute-start minute-end)))))
3699 (funcall
3700 build-ts-string
3701 (encode-time 0
3702 (or minute-start 0)
3703 (or hour-start 0)
3704 (org-element-property :day-start timestamp)
3705 (org-element-property :month-start timestamp)
3706 (org-element-property :year-start timestamp))
3707 (eq type 'active)
3708 (and hour-start minute-start)
3709 (and time-range-p hour-end)
3710 (and time-range-p minute-end))))
3711 ((active-range inactive-range)
3712 (let ((minute-start (org-element-property :minute-start timestamp))
3713 (minute-end (org-element-property :minute-end timestamp))
3714 (hour-start (org-element-property :hour-start timestamp))
3715 (hour-end (org-element-property :hour-end timestamp)))
3716 (concat
3717 (funcall
3718 build-ts-string (encode-time
3720 (or minute-start 0)
3721 (or hour-start 0)
3722 (org-element-property :day-start timestamp)
3723 (org-element-property :month-start timestamp)
3724 (org-element-property :year-start timestamp))
3725 (eq type 'active-range)
3726 (and hour-start minute-start))
3727 "--"
3728 (funcall build-ts-string
3729 (encode-time 0
3730 (or minute-end 0)
3731 (or hour-end 0)
3732 (org-element-property :day-end timestamp)
3733 (org-element-property :month-end timestamp)
3734 (org-element-property :year-end timestamp))
3735 (eq type 'active-range)
3736 (and hour-end minute-end)))))))))
3738 (defun org-element-timestamp-successor ()
3739 "Search for the next timestamp object.
3741 Return value is a cons cell whose CAR is `timestamp' and CDR is
3742 beginning position."
3743 (save-excursion
3744 (when (re-search-forward
3745 (concat org-ts-regexp-both
3746 "\\|"
3747 "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
3748 "\\|"
3749 "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
3750 nil t)
3751 (cons 'timestamp (match-beginning 0)))))
3754 ;;;; Underline
3756 (defun org-element-underline-parser ()
3757 "Parse underline object at point.
3759 Return a list whose CAR is `underline' and CDR is a plist with
3760 `:begin', `:end', `:contents-begin' and `:contents-end' and
3761 `:post-blank' keywords.
3763 Assume point is at the first underscore marker."
3764 (save-excursion
3765 (unless (bolp) (backward-char 1))
3766 (looking-at org-emph-re)
3767 (let ((begin (match-beginning 2))
3768 (contents-begin (match-beginning 4))
3769 (contents-end (match-end 4))
3770 (post-blank (progn (goto-char (match-end 2))
3771 (skip-chars-forward " \t")))
3772 (end (point)))
3773 (list 'underline
3774 (list :begin begin
3775 :end end
3776 :contents-begin contents-begin
3777 :contents-end contents-end
3778 :post-blank post-blank)))))
3780 (defun org-element-underline-interpreter (underline contents)
3781 "Interpret UNDERLINE object as Org syntax.
3782 CONTENTS is the contents of the object."
3783 (format "_%s_" contents))
3786 ;;;; Verbatim
3788 (defun org-element-verbatim-parser ()
3789 "Parse verbatim object at point.
3791 Return a list whose CAR is `verbatim' and CDR is a plist with
3792 `:value', `:begin', `:end' and `:post-blank' keywords.
3794 Assume point is at the first equal sign marker."
3795 (save-excursion
3796 (unless (bolp) (backward-char 1))
3797 (looking-at org-emph-re)
3798 (let ((begin (match-beginning 2))
3799 (value (org-match-string-no-properties 4))
3800 (post-blank (progn (goto-char (match-end 2))
3801 (skip-chars-forward " \t")))
3802 (end (point)))
3803 (list 'verbatim
3804 (list :value value
3805 :begin begin
3806 :end end
3807 :post-blank post-blank)))))
3809 (defun org-element-verbatim-interpreter (verbatim contents)
3810 "Interpret VERBATIM object as Org syntax.
3811 CONTENTS is nil."
3812 (format "=%s=" (org-element-property :value verbatim)))
3816 ;;; Parsing Element Starting At Point
3818 ;; `org-element--current-element' is the core function of this section.
3819 ;; It returns the Lisp representation of the element starting at
3820 ;; point.
3822 ;; `org-element--current-element' makes use of special modes. They
3823 ;; are activated for fixed element chaining (i.e. `plain-list' >
3824 ;; `item') or fixed conditional element chaining (i.e. `headline' >
3825 ;; `section'). Special modes are: `first-section', `item',
3826 ;; `node-property', `quote-section', `section' and `table-row'.
3828 (defun org-element--current-element
3829 (limit &optional granularity special structure)
3830 "Parse the element starting at point.
3832 Return value is a list like (TYPE PROPS) where TYPE is the type
3833 of the element and PROPS a plist of properties associated to the
3834 element.
3836 Possible types are defined in `org-element-all-elements'.
3838 LIMIT bounds the search.
3840 Optional argument GRANULARITY determines the depth of the
3841 recursion. Allowed values are `headline', `greater-element',
3842 `element', `object' or nil. When it is broader than `object' (or
3843 nil), secondary values will not be parsed, since they only
3844 contain objects.
3846 Optional argument SPECIAL, when non-nil, can be either
3847 `first-section', `item', `node-property', `quote-section',
3848 `section', and `table-row'.
3850 If STRUCTURE isn't provided but SPECIAL is set to `item', it will
3851 be computed.
3853 This function assumes point is always at the beginning of the
3854 element it has to parse."
3855 (save-excursion
3856 (let ((case-fold-search t)
3857 ;; Determine if parsing depth allows for secondary strings
3858 ;; parsing. It only applies to elements referenced in
3859 ;; `org-element-secondary-value-alist'.
3860 (raw-secondary-p (and granularity (not (eq granularity 'object)))))
3861 (cond
3862 ;; Item.
3863 ((eq special 'item)
3864 (org-element-item-parser limit structure raw-secondary-p))
3865 ;; Table Row.
3866 ((eq special 'table-row) (org-element-table-row-parser limit))
3867 ;; Node Property.
3868 ((eq special 'node-property) (org-element-node-property-parser limit))
3869 ;; Headline.
3870 ((org-with-limited-levels (org-at-heading-p))
3871 (org-element-headline-parser limit raw-secondary-p))
3872 ;; Sections (must be checked after headline).
3873 ((eq special 'section) (org-element-section-parser limit))
3874 ((eq special 'quote-section) (org-element-quote-section-parser limit))
3875 ((eq special 'first-section)
3876 (org-element-section-parser
3877 (or (save-excursion (org-with-limited-levels (outline-next-heading)))
3878 limit)))
3879 ;; When not at bol, point is at the beginning of an item or
3880 ;; a footnote definition: next item is always a paragraph.
3881 ((not (bolp)) (org-element-paragraph-parser limit (list (point))))
3882 ;; Planning and Clock.
3883 ((looking-at org-planning-or-clock-line-re)
3884 (if (equal (match-string 1) org-clock-string)
3885 (org-element-clock-parser limit)
3886 (org-element-planning-parser limit)))
3887 ;; Inlinetask.
3888 ((org-at-heading-p)
3889 (org-element-inlinetask-parser limit raw-secondary-p))
3890 ;; From there, elements can have affiliated keywords.
3891 (t (let ((affiliated (org-element--collect-affiliated-keywords limit)))
3892 (cond
3893 ;; Jumping over affiliated keywords put point off-limits.
3894 ;; Parse them as regular keywords.
3895 ((and (cdr affiliated) (>= (point) limit))
3896 (goto-char (car affiliated))
3897 (org-element-keyword-parser limit nil))
3898 ;; LaTeX Environment.
3899 ((looking-at
3900 "[ \t]*\\\\begin{[A-Za-z0-9*]+}\\(\\[.*?\\]\\|{.*?}\\)*[ \t]*$")
3901 (org-element-latex-environment-parser limit affiliated))
3902 ;; Drawer and Property Drawer.
3903 ((looking-at org-drawer-regexp)
3904 (if (equal (match-string 1) "PROPERTIES")
3905 (org-element-property-drawer-parser limit affiliated)
3906 (org-element-drawer-parser limit affiliated)))
3907 ;; Fixed Width
3908 ((looking-at "[ \t]*:\\( \\|$\\)")
3909 (org-element-fixed-width-parser limit affiliated))
3910 ;; Inline Comments, Blocks, Babel Calls, Dynamic Blocks and
3911 ;; Keywords.
3912 ((looking-at "[ \t]*#")
3913 (goto-char (match-end 0))
3914 (cond ((looking-at "\\(?: \\|$\\)")
3915 (beginning-of-line)
3916 (org-element-comment-parser limit affiliated))
3917 ((looking-at "\\+BEGIN_\\(\\S-+\\)")
3918 (beginning-of-line)
3919 (let ((parser (assoc (upcase (match-string 1))
3920 org-element-block-name-alist)))
3921 (if parser (funcall (cdr parser) limit affiliated)
3922 (org-element-special-block-parser limit affiliated))))
3923 ((looking-at "\\+CALL:")
3924 (beginning-of-line)
3925 (org-element-babel-call-parser limit affiliated))
3926 ((looking-at "\\+BEGIN:? ")
3927 (beginning-of-line)
3928 (org-element-dynamic-block-parser limit affiliated))
3929 ((looking-at "\\+\\S-+:")
3930 (beginning-of-line)
3931 (org-element-keyword-parser limit affiliated))
3933 (beginning-of-line)
3934 (org-element-paragraph-parser limit affiliated))))
3935 ;; Footnote Definition.
3936 ((looking-at org-footnote-definition-re)
3937 (org-element-footnote-definition-parser limit affiliated))
3938 ;; Horizontal Rule.
3939 ((looking-at "[ \t]*-\\{5,\\}[ \t]*$")
3940 (org-element-horizontal-rule-parser limit affiliated))
3941 ;; Diary Sexp.
3942 ((looking-at "%%(")
3943 (org-element-diary-sexp-parser limit affiliated))
3944 ;; Table.
3945 ((org-at-table-p t) (org-element-table-parser limit affiliated))
3946 ;; List.
3947 ((looking-at (org-item-re))
3948 (org-element-plain-list-parser
3949 limit affiliated
3950 (or structure (org-element--list-struct limit))))
3951 ;; Default element: Paragraph.
3952 (t (org-element-paragraph-parser limit affiliated)))))))))
3955 ;; Most elements can have affiliated keywords. When looking for an
3956 ;; element beginning, we want to move before them, as they belong to
3957 ;; that element, and, in the meantime, collect information they give
3958 ;; into appropriate properties. Hence the following function.
3960 (defun org-element--collect-affiliated-keywords (limit)
3961 "Collect affiliated keywords from point down to LIMIT.
3963 Return a list whose CAR is the position at the first of them and
3964 CDR a plist of keywords and values and move point to the
3965 beginning of the first line after them.
3967 As a special case, if element doesn't start at the beginning of
3968 the line (i.e. a paragraph starting an item), CAR is current
3969 position of point and CDR is nil."
3970 (if (not (bolp)) (list (point))
3971 (let ((case-fold-search t)
3972 (origin (point))
3973 ;; RESTRICT is the list of objects allowed in parsed
3974 ;; keywords value.
3975 (restrict (org-element-restriction 'keyword))
3976 output)
3977 (while (and (< (point) limit) (looking-at org-element--affiliated-re))
3978 (let* ((raw-kwd (upcase (match-string 1)))
3979 ;; Apply translation to RAW-KWD. From there, KWD is
3980 ;; the official keyword.
3981 (kwd (or (cdr (assoc raw-kwd
3982 org-element-keyword-translation-alist))
3983 raw-kwd))
3984 ;; Find main value for any keyword.
3985 (value
3986 (save-match-data
3987 (org-trim
3988 (buffer-substring-no-properties
3989 (match-end 0) (point-at-eol)))))
3990 ;; PARSEDP is non-nil when keyword should have its
3991 ;; value parsed.
3992 (parsedp (member kwd org-element-parsed-keywords))
3993 ;; If KWD is a dual keyword, find its secondary
3994 ;; value. Maybe parse it.
3995 (dualp (member kwd org-element-dual-keywords))
3996 (dual-value
3997 (and dualp
3998 (let ((sec (org-match-string-no-properties 2)))
3999 (if (or (not sec) (not parsedp)) sec
4000 (org-element-parse-secondary-string sec restrict)))))
4001 ;; Attribute a property name to KWD.
4002 (kwd-sym (and kwd (intern (concat ":" (downcase kwd))))))
4003 ;; Now set final shape for VALUE.
4004 (when parsedp
4005 (setq value (org-element-parse-secondary-string value restrict)))
4006 (when dualp
4007 (setq value (and (or value dual-value) (cons value dual-value))))
4008 (when (or (member kwd org-element-multiple-keywords)
4009 ;; Attributes can always appear on multiple lines.
4010 (string-match "^ATTR_" kwd))
4011 (setq value (cons value (plist-get output kwd-sym))))
4012 ;; Eventually store the new value in OUTPUT.
4013 (setq output (plist-put output kwd-sym value))
4014 ;; Move to next keyword.
4015 (forward-line)))
4016 ;; If affiliated keywords are orphaned: move back to first one.
4017 ;; They will be parsed as a paragraph.
4018 (when (looking-at "[ \t]*$") (goto-char origin) (setq output nil))
4019 ;; Return value.
4020 (cons origin output))))
4024 ;;; The Org Parser
4026 ;; The two major functions here are `org-element-parse-buffer', which
4027 ;; parses Org syntax inside the current buffer, taking into account
4028 ;; region, narrowing, or even visibility if specified, and
4029 ;; `org-element-parse-secondary-string', which parses objects within
4030 ;; a given string.
4032 ;; The (almost) almighty `org-element-map' allows to apply a function
4033 ;; on elements or objects matching some type, and accumulate the
4034 ;; resulting values. In an export situation, it also skips unneeded
4035 ;; parts of the parse tree.
4037 (defun org-element-parse-buffer (&optional granularity visible-only)
4038 "Recursively parse the buffer and return structure.
4039 If narrowing is in effect, only parse the visible part of the
4040 buffer.
4042 Optional argument GRANULARITY determines the depth of the
4043 recursion. It can be set to the following symbols:
4045 `headline' Only parse headlines.
4046 `greater-element' Don't recurse into greater elements excepted
4047 headlines and sections. Thus, elements
4048 parsed are the top-level ones.
4049 `element' Parse everything but objects and plain text.
4050 `object' Parse the complete buffer (default).
4052 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
4053 elements.
4055 An element or an objects is represented as a list with the
4056 pattern (TYPE PROPERTIES CONTENTS), where :
4058 TYPE is a symbol describing the element or object. See
4059 `org-element-all-elements' and `org-element-all-objects' for an
4060 exhaustive list of such symbols. One can retrieve it with
4061 `org-element-type' function.
4063 PROPERTIES is the list of attributes attached to the element or
4064 object, as a plist. Although most of them are specific to the
4065 element or object type, all types share `:begin', `:end',
4066 `:post-blank' and `:parent' properties, which respectively
4067 refer to buffer position where the element or object starts,
4068 ends, the number of white spaces or blank lines after it, and
4069 the element or object containing it. Properties values can be
4070 obtained by using `org-element-property' function.
4072 CONTENTS is a list of elements, objects or raw strings
4073 contained in the current element or object, when applicable.
4074 One can access them with `org-element-contents' function.
4076 The Org buffer has `org-data' as type and nil as properties.
4077 `org-element-map' function can be used to find specific elements
4078 or objects within the parse tree.
4080 This function assumes that current major mode is `org-mode'."
4081 (save-excursion
4082 (goto-char (point-min))
4083 (org-skip-whitespace)
4084 (org-element--parse-elements
4085 (point-at-bol) (point-max)
4086 ;; Start in `first-section' mode so text before the first
4087 ;; headline belongs to a section.
4088 'first-section nil granularity visible-only (list 'org-data nil))))
4090 (defun org-element-parse-secondary-string (string restriction &optional parent)
4091 "Recursively parse objects in STRING and return structure.
4093 RESTRICTION is a symbol limiting the object types that will be
4094 looked after.
4096 Optional argument PARENT, when non-nil, is the element or object
4097 containing the secondary string. It is used to set correctly
4098 `:parent' property within the string."
4099 ;; Copy buffer-local variables listed in
4100 ;; `org-element-object-variables' into temporary buffer. This is
4101 ;; required since object parsing is dependent on these variables.
4102 (let ((pairs (delq nil (mapcar (lambda (var)
4103 (when (boundp var)
4104 (cons var (symbol-value var))))
4105 org-element-object-variables))))
4106 (with-temp-buffer
4107 (mapc (lambda (pair) (org-set-local (car pair) (cdr pair))) pairs)
4108 (insert string)
4109 (let ((secondary (org-element--parse-objects
4110 (point-min) (point-max) nil restriction)))
4111 (when parent
4112 (mapc (lambda (obj) (org-element-put-property obj :parent parent))
4113 secondary))
4114 secondary))))
4116 (defun org-element-map
4117 (data types fun &optional info first-match no-recursion with-affiliated)
4118 "Map a function on selected elements or objects.
4120 DATA is a parse tree, an element, an object, a string, or a list
4121 of such constructs. TYPES is a symbol or list of symbols of
4122 elements or objects types (see `org-element-all-elements' and
4123 `org-element-all-objects' for a complete list of types). FUN is
4124 the function called on the matching element or object. It has to
4125 accept one argument: the element or object itself.
4127 When optional argument INFO is non-nil, it should be a plist
4128 holding export options. In that case, parts of the parse tree
4129 not exportable according to that property list will be skipped.
4131 When optional argument FIRST-MATCH is non-nil, stop at the first
4132 match for which FUN doesn't return nil, and return that value.
4134 Optional argument NO-RECURSION is a symbol or a list of symbols
4135 representing elements or objects types. `org-element-map' won't
4136 enter any recursive element or object whose type belongs to that
4137 list. Though, FUN can still be applied on them.
4139 When optional argument WITH-AFFILIATED is non-nil, FUN will also
4140 apply to matching objects within parsed affiliated keywords (see
4141 `org-element-parsed-keywords').
4143 Nil values returned from FUN do not appear in the results.
4146 Examples:
4147 ---------
4149 Assuming TREE is a variable containing an Org buffer parse tree,
4150 the following example will return a flat list of all `src-block'
4151 and `example-block' elements in it:
4153 \(org-element-map tree '(example-block src-block) 'identity)
4155 The following snippet will find the first headline with a level
4156 of 1 and a \"phone\" tag, and will return its beginning position:
4158 \(org-element-map tree 'headline
4159 \(lambda (hl)
4160 \(and (= (org-element-property :level hl) 1)
4161 \(member \"phone\" (org-element-property :tags hl))
4162 \(org-element-property :begin hl)))
4163 nil t)
4165 The next example will return a flat list of all `plain-list' type
4166 elements in TREE that are not a sub-list themselves:
4168 \(org-element-map tree 'plain-list 'identity nil nil 'plain-list)
4170 Eventually, this example will return a flat list of all `bold'
4171 type objects containing a `latex-snippet' type object, even
4172 looking into captions:
4174 \(org-element-map tree 'bold
4175 \(lambda (b)
4176 \(and (org-element-map b 'latex-snippet 'identity nil t) b))
4177 nil nil nil t)"
4178 ;; Ensure TYPES and NO-RECURSION are a list, even of one element.
4179 (unless (listp types) (setq types (list types)))
4180 (unless (listp no-recursion) (setq no-recursion (list no-recursion)))
4181 ;; Recursion depth is determined by --CATEGORY.
4182 (let* ((--category
4183 (catch 'found
4184 (let ((category 'greater-elements))
4185 (mapc (lambda (type)
4186 (cond ((or (memq type org-element-all-objects)
4187 (eq type 'plain-text))
4188 ;; If one object is found, the function
4189 ;; has to recurse into every object.
4190 (throw 'found 'objects))
4191 ((not (memq type org-element-greater-elements))
4192 ;; If one regular element is found, the
4193 ;; function has to recurse, at least,
4194 ;; into every element it encounters.
4195 (and (not (eq category 'elements))
4196 (setq category 'elements)))))
4197 types)
4198 category)))
4199 ;; Compute properties for affiliated keywords if necessary.
4200 (--affiliated-alist
4201 (and with-affiliated
4202 (mapcar (lambda (kwd)
4203 (cons kwd (intern (concat ":" (downcase kwd)))))
4204 org-element-affiliated-keywords)))
4205 --acc
4206 --walk-tree
4207 (--walk-tree
4208 (function
4209 (lambda (--data)
4210 ;; Recursively walk DATA. INFO, if non-nil, is a plist
4211 ;; holding contextual information.
4212 (let ((--type (org-element-type --data)))
4213 (cond
4214 ((not --data))
4215 ;; Ignored element in an export context.
4216 ((and info (memq --data (plist-get info :ignore-list))))
4217 ;; List of elements or objects.
4218 ((not --type) (mapc --walk-tree --data))
4219 ;; Unconditionally enter parse trees.
4220 ((eq --type 'org-data)
4221 (mapc --walk-tree (org-element-contents --data)))
4223 ;; Check if TYPE is matching among TYPES. If so,
4224 ;; apply FUN to --DATA and accumulate return value
4225 ;; into --ACC (or exit if FIRST-MATCH is non-nil).
4226 (when (memq --type types)
4227 (let ((result (funcall fun --data)))
4228 (cond ((not result))
4229 (first-match (throw '--map-first-match result))
4230 (t (push result --acc)))))
4231 ;; If --DATA has a secondary string that can contain
4232 ;; objects with their type among TYPES, look into it.
4233 (when (and (eq --category 'objects) (not (stringp --data)))
4234 (let ((sec-prop
4235 (assq --type org-element-secondary-value-alist)))
4236 (when sec-prop
4237 (funcall --walk-tree
4238 (org-element-property (cdr sec-prop) --data)))))
4239 ;; If --DATA has any affiliated keywords and
4240 ;; WITH-AFFILIATED is non-nil, look for objects in
4241 ;; them.
4242 (when (and with-affiliated
4243 (eq --category 'objects)
4244 (memq --type org-element-all-elements))
4245 (mapc (lambda (kwd-pair)
4246 (let ((kwd (car kwd-pair))
4247 (value (org-element-property
4248 (cdr kwd-pair) --data)))
4249 ;; Pay attention to the type of value.
4250 ;; Preserve order for multiple keywords.
4251 (cond
4252 ((not value))
4253 ((and (member kwd org-element-multiple-keywords)
4254 (member kwd org-element-dual-keywords))
4255 (mapc (lambda (line)
4256 (funcall --walk-tree (cdr line))
4257 (funcall --walk-tree (car line)))
4258 (reverse value)))
4259 ((member kwd org-element-multiple-keywords)
4260 (mapc (lambda (line) (funcall --walk-tree line))
4261 (reverse value)))
4262 ((member kwd org-element-dual-keywords)
4263 (funcall --walk-tree (cdr value))
4264 (funcall --walk-tree (car value)))
4265 (t (funcall --walk-tree value)))))
4266 --affiliated-alist))
4267 ;; Determine if a recursion into --DATA is possible.
4268 (cond
4269 ;; --TYPE is explicitly removed from recursion.
4270 ((memq --type no-recursion))
4271 ;; --DATA has no contents.
4272 ((not (org-element-contents --data)))
4273 ;; Looking for greater elements but --DATA is simply
4274 ;; an element or an object.
4275 ((and (eq --category 'greater-elements)
4276 (not (memq --type org-element-greater-elements))))
4277 ;; Looking for elements but --DATA is an object.
4278 ((and (eq --category 'elements)
4279 (memq --type org-element-all-objects)))
4280 ;; In any other case, map contents.
4281 (t (mapc --walk-tree (org-element-contents --data)))))))))))
4282 (catch '--map-first-match
4283 (funcall --walk-tree data)
4284 ;; Return value in a proper order.
4285 (nreverse --acc))))
4286 (put 'org-element-map 'lisp-indent-function 2)
4288 ;; The following functions are internal parts of the parser.
4290 ;; The first one, `org-element--parse-elements' acts at the element's
4291 ;; level.
4293 ;; The second one, `org-element--parse-objects' applies on all objects
4294 ;; of a paragraph or a secondary string. It uses
4295 ;; `org-element--get-next-object-candidates' to optimize the search of
4296 ;; the next object in the buffer.
4298 ;; More precisely, that function looks for every allowed object type
4299 ;; first. Then, it discards failed searches, keeps further matches,
4300 ;; and searches again types matched behind point, for subsequent
4301 ;; calls. Thus, searching for a given type fails only once, and every
4302 ;; object is searched only once at top level (but sometimes more for
4303 ;; nested types).
4305 (defun org-element--parse-elements
4306 (beg end special structure granularity visible-only acc)
4307 "Parse elements between BEG and END positions.
4309 SPECIAL prioritize some elements over the others. It can be set
4310 to `first-section', `quote-section', `section' `item' or
4311 `table-row'.
4313 When value is `item', STRUCTURE will be used as the current list
4314 structure.
4316 GRANULARITY determines the depth of the recursion. See
4317 `org-element-parse-buffer' for more information.
4319 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
4320 elements.
4322 Elements are accumulated into ACC."
4323 (save-excursion
4324 (goto-char beg)
4325 ;; Visible only: skip invisible parts at the beginning of the
4326 ;; element.
4327 (when (and visible-only (org-invisible-p2))
4328 (goto-char (min (1+ (org-find-visible)) end)))
4329 ;; When parsing only headlines, skip any text before first one.
4330 (when (and (eq granularity 'headline) (not (org-at-heading-p)))
4331 (org-with-limited-levels (outline-next-heading)))
4332 ;; Main loop start.
4333 (while (< (point) end)
4334 ;; Find current element's type and parse it accordingly to
4335 ;; its category.
4336 (let* ((element (org-element--current-element
4337 end granularity special structure))
4338 (type (org-element-type element))
4339 (cbeg (org-element-property :contents-begin element)))
4340 (goto-char (org-element-property :end element))
4341 ;; Visible only: skip invisible parts between siblings.
4342 (when (and visible-only (org-invisible-p2))
4343 (goto-char (min (1+ (org-find-visible)) end)))
4344 ;; Fill ELEMENT contents by side-effect.
4345 (cond
4346 ;; If element has no contents, don't modify it.
4347 ((not cbeg))
4348 ;; Greater element: parse it between `contents-begin' and
4349 ;; `contents-end'. Make sure GRANULARITY allows the
4350 ;; recursion, or ELEMENT is a headline, in which case going
4351 ;; inside is mandatory, in order to get sub-level headings.
4352 ((and (memq type org-element-greater-elements)
4353 (or (memq granularity '(element object nil))
4354 (and (eq granularity 'greater-element)
4355 (eq type 'section))
4356 (eq type 'headline)))
4357 (org-element--parse-elements
4358 cbeg (org-element-property :contents-end element)
4359 ;; Possibly switch to a special mode.
4360 (case type
4361 (headline
4362 (if (org-element-property :quotedp element) 'quote-section
4363 'section))
4364 (plain-list 'item)
4365 (property-drawer 'node-property)
4366 (table 'table-row))
4367 (and (memq type '(item plain-list))
4368 (org-element-property :structure element))
4369 granularity visible-only element))
4370 ;; ELEMENT has contents. Parse objects inside, if
4371 ;; GRANULARITY allows it.
4372 ((memq granularity '(object nil))
4373 (org-element--parse-objects
4374 cbeg (org-element-property :contents-end element) element
4375 (org-element-restriction type))))
4376 (org-element-adopt-elements acc element)))
4377 ;; Return result.
4378 acc))
4380 (defun org-element--parse-objects (beg end acc restriction)
4381 "Parse objects between BEG and END and return recursive structure.
4383 Objects are accumulated in ACC.
4385 RESTRICTION is a list of object successors which are allowed in
4386 the current object."
4387 (let ((candidates 'initial))
4388 (save-excursion
4389 (save-restriction
4390 (narrow-to-region beg end)
4391 (goto-char (point-min))
4392 (while (and (not (eobp))
4393 (setq candidates
4394 (org-element--get-next-object-candidates
4395 restriction candidates)))
4396 (let ((next-object
4397 (let ((pos (apply 'min (mapcar 'cdr candidates))))
4398 (save-excursion
4399 (goto-char pos)
4400 (funcall (intern (format "org-element-%s-parser"
4401 (car (rassq pos candidates)))))))))
4402 ;; 1. Text before any object. Untabify it.
4403 (let ((obj-beg (org-element-property :begin next-object)))
4404 (unless (= (point) obj-beg)
4405 (setq acc
4406 (org-element-adopt-elements
4408 (replace-regexp-in-string
4409 "\t" (make-string tab-width ? )
4410 (buffer-substring-no-properties (point) obj-beg))))))
4411 ;; 2. Object...
4412 (let ((obj-end (org-element-property :end next-object))
4413 (cont-beg (org-element-property :contents-begin next-object)))
4414 ;; Fill contents of NEXT-OBJECT by side-effect, if it has
4415 ;; a recursive type.
4416 (when (and cont-beg
4417 (memq (car next-object) org-element-recursive-objects))
4418 (org-element--parse-objects
4419 cont-beg (org-element-property :contents-end next-object)
4420 next-object (org-element-restriction next-object)))
4421 (setq acc (org-element-adopt-elements acc next-object))
4422 (goto-char obj-end))))
4423 ;; 3. Text after last object. Untabify it.
4424 (unless (eobp)
4425 (setq acc
4426 (org-element-adopt-elements
4428 (replace-regexp-in-string
4429 "\t" (make-string tab-width ? )
4430 (buffer-substring-no-properties (point) end)))))
4431 ;; Result.
4432 acc))))
4434 (defun org-element--get-next-object-candidates (restriction objects)
4435 "Return an alist of candidates for the next object.
4437 RESTRICTION is a list of object types, as symbols. Only
4438 candidates with such types are looked after.
4440 OBJECTS is the previous candidates alist. If it is set to
4441 `initial', no search has been done before, and all symbols in
4442 RESTRICTION should be looked after.
4444 Return value is an alist whose CAR is the object type and CDR its
4445 beginning position."
4446 (delq
4448 (if (eq objects 'initial)
4449 ;; When searching for the first time, look for every successor
4450 ;; allowed in RESTRICTION.
4451 (mapcar
4452 (lambda (res)
4453 (funcall (intern (format "org-element-%s-successor" res))))
4454 restriction)
4455 ;; Focus on objects returned during last search. Keep those
4456 ;; still after point. Search again objects before it.
4457 (mapcar
4458 (lambda (obj)
4459 (if (>= (cdr obj) (point)) obj
4460 (let* ((type (car obj))
4461 (succ (or (cdr (assq type org-element-object-successor-alist))
4462 type)))
4463 (and succ
4464 (funcall (intern (format "org-element-%s-successor" succ)))))))
4465 objects))))
4469 ;;; Towards A Bijective Process
4471 ;; The parse tree obtained with `org-element-parse-buffer' is really
4472 ;; a snapshot of the corresponding Org buffer. Therefore, it can be
4473 ;; interpreted and expanded into a string with canonical Org syntax.
4474 ;; Hence `org-element-interpret-data'.
4476 ;; The function relies internally on
4477 ;; `org-element--interpret-affiliated-keywords'.
4479 ;;;###autoload
4480 (defun org-element-interpret-data (data &optional parent)
4481 "Interpret DATA as Org syntax.
4483 DATA is a parse tree, an element, an object or a secondary string
4484 to interpret.
4486 Optional argument PARENT is used for recursive calls. It contains
4487 the element or object containing data, or nil.
4489 Return Org syntax as a string."
4490 (let* ((type (org-element-type data))
4491 (results
4492 (cond
4493 ;; Secondary string.
4494 ((not type)
4495 (mapconcat
4496 (lambda (obj) (org-element-interpret-data obj parent))
4497 data ""))
4498 ;; Full Org document.
4499 ((eq type 'org-data)
4500 (mapconcat
4501 (lambda (obj) (org-element-interpret-data obj parent))
4502 (org-element-contents data) ""))
4503 ;; Plain text: remove `:parent' text property from output.
4504 ((stringp data) (org-no-properties data))
4505 ;; Element/Object without contents.
4506 ((not (org-element-contents data))
4507 (funcall (intern (format "org-element-%s-interpreter" type))
4508 data nil))
4509 ;; Element/Object with contents.
4511 (let* ((greaterp (memq type org-element-greater-elements))
4512 (objectp (and (not greaterp)
4513 (memq type org-element-recursive-objects)))
4514 (contents
4515 (mapconcat
4516 (lambda (obj) (org-element-interpret-data obj data))
4517 (org-element-contents
4518 (if (or greaterp objectp) data
4519 ;; Elements directly containing objects must
4520 ;; have their indentation normalized first.
4521 (org-element-normalize-contents
4522 data
4523 ;; When normalizing first paragraph of an
4524 ;; item or a footnote-definition, ignore
4525 ;; first line's indentation.
4526 (and (eq type 'paragraph)
4527 (equal data (car (org-element-contents parent)))
4528 (memq (org-element-type parent)
4529 '(footnote-definition item))))))
4530 "")))
4531 (funcall (intern (format "org-element-%s-interpreter" type))
4532 data
4533 (if greaterp (org-element-normalize-contents contents)
4534 contents)))))))
4535 (if (memq type '(org-data plain-text nil)) results
4536 ;; Build white spaces. If no `:post-blank' property is
4537 ;; specified, assume its value is 0.
4538 (let ((post-blank (or (org-element-property :post-blank data) 0)))
4539 (if (memq type org-element-all-objects)
4540 (concat results (make-string post-blank 32))
4541 (concat
4542 (org-element--interpret-affiliated-keywords data)
4543 (org-element-normalize-string results)
4544 (make-string post-blank 10)))))))
4546 (defun org-element--interpret-affiliated-keywords (element)
4547 "Return ELEMENT's affiliated keywords as Org syntax.
4548 If there is no affiliated keyword, return the empty string."
4549 (let ((keyword-to-org
4550 (function
4551 (lambda (key value)
4552 (let (dual)
4553 (when (member key org-element-dual-keywords)
4554 (setq dual (cdr value) value (car value)))
4555 (concat "#+" key
4556 (and dual
4557 (format "[%s]" (org-element-interpret-data dual)))
4558 ": "
4559 (if (member key org-element-parsed-keywords)
4560 (org-element-interpret-data value)
4561 value)
4562 "\n"))))))
4563 (mapconcat
4564 (lambda (prop)
4565 (let ((value (org-element-property prop element))
4566 (keyword (upcase (substring (symbol-name prop) 1))))
4567 (when value
4568 (if (or (member keyword org-element-multiple-keywords)
4569 ;; All attribute keywords can have multiple lines.
4570 (string-match "^ATTR_" keyword))
4571 (mapconcat (lambda (line) (funcall keyword-to-org keyword line))
4572 (reverse value)
4574 (funcall keyword-to-org keyword value)))))
4575 ;; List all ELEMENT's properties matching an attribute line or an
4576 ;; affiliated keyword, but ignore translated keywords since they
4577 ;; cannot belong to the property list.
4578 (loop for prop in (nth 1 element) by 'cddr
4579 when (let ((keyword (upcase (substring (symbol-name prop) 1))))
4580 (or (string-match "^ATTR_" keyword)
4581 (and
4582 (member keyword org-element-affiliated-keywords)
4583 (not (assoc keyword
4584 org-element-keyword-translation-alist)))))
4585 collect prop)
4586 "")))
4588 ;; Because interpretation of the parse tree must return the same
4589 ;; number of blank lines between elements and the same number of white
4590 ;; space after objects, some special care must be given to white
4591 ;; spaces.
4593 ;; The first function, `org-element-normalize-string', ensures any
4594 ;; string different from the empty string will end with a single
4595 ;; newline character.
4597 ;; The second function, `org-element-normalize-contents', removes
4598 ;; global indentation from the contents of the current element.
4600 (defun org-element-normalize-string (s)
4601 "Ensure string S ends with a single newline character.
4603 If S isn't a string return it unchanged. If S is the empty
4604 string, return it. Otherwise, return a new string with a single
4605 newline character at its end."
4606 (cond
4607 ((not (stringp s)) s)
4608 ((string= "" s) "")
4609 (t (and (string-match "\\(\n[ \t]*\\)*\\'" s)
4610 (replace-match "\n" nil nil s)))))
4612 (defun org-element-normalize-contents (element &optional ignore-first)
4613 "Normalize plain text in ELEMENT's contents.
4615 ELEMENT must only contain plain text and objects.
4617 If optional argument IGNORE-FIRST is non-nil, ignore first line's
4618 indentation to compute maximal common indentation.
4620 Return the normalized element that is element with global
4621 indentation removed from its contents. The function assumes that
4622 indentation is not done with TAB characters."
4623 (let* (ind-list ; for byte-compiler
4624 collect-inds ; for byte-compiler
4625 (collect-inds
4626 (function
4627 ;; Return list of indentations within BLOB. This is done by
4628 ;; walking recursively BLOB and updating IND-LIST along the
4629 ;; way. FIRST-FLAG is non-nil when the first string hasn't
4630 ;; been seen yet. It is required as this string is the only
4631 ;; one whose indentation doesn't happen after a newline
4632 ;; character.
4633 (lambda (blob first-flag)
4634 (mapc
4635 (lambda (object)
4636 (when (and first-flag (stringp object))
4637 (setq first-flag nil)
4638 (string-match "\\`\\( *\\)" object)
4639 (let ((len (length (match-string 1 object))))
4640 ;; An indentation of zero means no string will be
4641 ;; modified. Quit the process.
4642 (if (zerop len) (throw 'zero (setq ind-list nil))
4643 (push len ind-list))))
4644 (cond
4645 ((stringp object)
4646 (let ((start 0))
4647 ;; Avoid matching blank or empty lines.
4648 (while (and (string-match "\n\\( *\\)\\(.\\)" object start)
4649 (not (equal (match-string 2 object) " ")))
4650 (setq start (match-end 0))
4651 (push (length (match-string 1 object)) ind-list))))
4652 ((memq (org-element-type object) org-element-recursive-objects)
4653 (funcall collect-inds object first-flag))))
4654 (org-element-contents blob))))))
4655 ;; Collect indentation list in ELEMENT. Possibly remove first
4656 ;; value if IGNORE-FIRST is non-nil.
4657 (catch 'zero (funcall collect-inds element (not ignore-first)))
4658 (if (not ind-list) element
4659 ;; Build ELEMENT back, replacing each string with the same
4660 ;; string minus common indentation.
4661 (let* (build ; For byte compiler.
4662 (build
4663 (function
4664 (lambda (blob mci first-flag)
4665 ;; Return BLOB with all its strings indentation
4666 ;; shortened from MCI white spaces. FIRST-FLAG is
4667 ;; non-nil when the first string hasn't been seen
4668 ;; yet.
4669 (setcdr (cdr blob)
4670 (mapcar
4671 (lambda (object)
4672 (when (and first-flag (stringp object))
4673 (setq first-flag nil)
4674 (setq object
4675 (replace-regexp-in-string
4676 (format "\\` \\{%d\\}" mci) "" object)))
4677 (cond
4678 ((stringp object)
4679 (replace-regexp-in-string
4680 (format "\n \\{%d\\}" mci) "\n" object))
4681 ((memq (org-element-type object)
4682 org-element-recursive-objects)
4683 (funcall build object mci first-flag))
4684 (t object)))
4685 (org-element-contents blob)))
4686 blob))))
4687 (funcall build element (apply 'min ind-list) (not ignore-first))))))
4691 ;;; The Toolbox
4693 ;; The first move is to implement a way to obtain the smallest element
4694 ;; containing point. This is the job of `org-element-at-point'. It
4695 ;; basically jumps back to the beginning of section containing point
4696 ;; and moves, element after element, with
4697 ;; `org-element--current-element' until the container is found. Note:
4698 ;; When using `org-element-at-point', secondary values are never
4699 ;; parsed since the function focuses on elements, not on objects.
4701 ;; At a deeper level, `org-element-context' lists all elements and
4702 ;; objects containing point.
4704 ;; `org-element-nested-p' and `org-element-swap-A-B' may be used
4705 ;; internally by navigation and manipulation tools.
4707 ;;;###autoload
4708 (defun org-element-at-point (&optional keep-trail)
4709 "Determine closest element around point.
4711 Return value is a list like (TYPE PROPS) where TYPE is the type
4712 of the element and PROPS a plist of properties associated to the
4713 element.
4715 Possible types are defined in `org-element-all-elements'.
4716 Properties depend on element or object type, but always include
4717 `:begin', `:end', `:parent' and `:post-blank' properties.
4719 As a special case, if point is at the very beginning of a list or
4720 sub-list, returned element will be that list instead of the first
4721 item. In the same way, if point is at the beginning of the first
4722 row of a table, returned element will be the table instead of the
4723 first row.
4725 If optional argument KEEP-TRAIL is non-nil, the function returns
4726 a list of elements leading to element at point. The list's CAR
4727 is always the element at point. The following positions contain
4728 element's siblings, then parents, siblings of parents, until the
4729 first element of current section."
4730 (org-with-wide-buffer
4731 ;; If at a headline, parse it. It is the sole element that
4732 ;; doesn't require to know about context. Be sure to disallow
4733 ;; secondary string parsing, though.
4734 (if (org-with-limited-levels (org-at-heading-p))
4735 (progn
4736 (beginning-of-line)
4737 (if (not keep-trail) (org-element-headline-parser (point-max) t)
4738 (list (org-element-headline-parser (point-max) t))))
4739 ;; Otherwise move at the beginning of the section containing
4740 ;; point.
4741 (catch 'exit
4742 (let ((origin (point))
4743 (end (save-excursion
4744 (org-with-limited-levels (outline-next-heading)) (point)))
4745 element type special-flag trail struct prevs parent)
4746 (org-with-limited-levels
4747 (if (org-before-first-heading-p)
4748 ;; In empty lines at buffer's beginning, return nil.
4749 (progn (goto-char (point-min))
4750 (org-skip-whitespace)
4751 (when (or (eobp) (> (line-beginning-position) origin))
4752 (throw 'exit nil)))
4753 (org-back-to-heading)
4754 (forward-line)
4755 (org-skip-whitespace)
4756 (when (or (eobp) (> (line-beginning-position) origin))
4757 ;; In blank lines just after the headline, point still
4758 ;; belongs to the headline.
4759 (throw 'exit
4760 (progn (skip-chars-backward " \r\t\n")
4761 (beginning-of-line)
4762 (if (not keep-trail)
4763 (org-element-headline-parser (point-max) t)
4764 (list (org-element-headline-parser
4765 (point-max) t))))))))
4766 (beginning-of-line)
4767 ;; Parse successively each element, skipping those ending
4768 ;; before original position.
4769 (while t
4770 (setq element
4771 (org-element--current-element end 'element special-flag struct)
4772 type (car element))
4773 (org-element-put-property element :parent parent)
4774 (when keep-trail (push element trail))
4775 (cond
4776 ;; 1. Skip any element ending before point. Also skip
4777 ;; element ending at point when we're sure that another
4778 ;; element has started.
4779 ((let ((elem-end (org-element-property :end element)))
4780 (when (or (< elem-end origin)
4781 (and (= elem-end origin) (/= elem-end end)))
4782 (goto-char elem-end))))
4783 ;; 2. An element containing point is always the element at
4784 ;; point.
4785 ((not (memq type org-element-greater-elements))
4786 (throw 'exit (if keep-trail trail element)))
4787 ;; 3. At any other greater element type, if point is
4788 ;; within contents, move into it.
4790 (let ((cbeg (org-element-property :contents-begin element))
4791 (cend (org-element-property :contents-end element)))
4792 (if (or (not cbeg) (not cend) (> cbeg origin) (< cend origin)
4793 ;; Create an anchor for tables and plain lists:
4794 ;; when point is at the very beginning of these
4795 ;; elements, ignoring affiliated keywords,
4796 ;; target them instead of their contents.
4797 (and (= cbeg origin) (memq type '(plain-list table)))
4798 ;; When point is at contents end, do not move
4799 ;; into elements with an explicit ending, but
4800 ;; return that element instead.
4801 (and (= cend origin)
4802 (or (memq type
4803 '(center-block
4804 drawer dynamic-block inlinetask
4805 property-drawer quote-block
4806 special-block))
4807 ;; Corner case: if a list ends at the
4808 ;; end of a buffer without a final new
4809 ;; line, return last element in last
4810 ;; item instead.
4811 (and (memq type '(item plain-list))
4812 (progn (goto-char cend)
4813 (or (bolp) (not (eobp))))))))
4814 (throw 'exit (if keep-trail trail element))
4815 (setq parent element)
4816 (case type
4817 (plain-list
4818 (setq special-flag 'item
4819 struct (org-element-property :structure element)))
4820 (item (setq special-flag nil))
4821 (property-drawer
4822 (setq special-flag 'node-property struct nil))
4823 (table (setq special-flag 'table-row struct nil))
4824 (otherwise (setq special-flag nil struct nil)))
4825 (setq end cend)
4826 (goto-char cbeg)))))))))))
4828 ;;;###autoload
4829 (defun org-element-context (&optional element)
4830 "Return closest element or object around point.
4832 Return value is a list like (TYPE PROPS) where TYPE is the type
4833 of the element or object and PROPS a plist of properties
4834 associated to it.
4836 Possible types are defined in `org-element-all-elements' and
4837 `org-element-all-objects'. Properties depend on element or
4838 object type, but always include `:begin', `:end', `:parent' and
4839 `:post-blank'.
4841 Optional argument ELEMENT, when non-nil, is the closest element
4842 containing point, as returned by `org-element-at-point'.
4843 Providing it allows for quicker computation."
4844 (catch 'objects-forbidden
4845 (org-with-wide-buffer
4846 (let* ((origin (point))
4847 (element (or element (org-element-at-point)))
4848 (type (org-element-type element))
4849 context)
4850 ;; Check if point is inside an element containing objects or at
4851 ;; a secondary string. In that case, narrow buffer to the
4852 ;; containing area. Otherwise, return ELEMENT.
4853 (cond
4854 ;; At a parsed affiliated keyword, check if we're inside main
4855 ;; or dual value.
4856 ((let ((post (org-element-property :post-affiliated element)))
4857 (and post (< origin post)))
4858 (beginning-of-line)
4859 (looking-at org-element--affiliated-re)
4860 (cond
4861 ((not (member (upcase (match-string 1)) org-element-parsed-keywords))
4862 (throw 'objects-forbidden element))
4863 ((< (match-end 0) origin)
4864 (narrow-to-region (match-end 0) (line-end-position)))
4865 ((and (match-beginning 2)
4866 (>= origin (match-beginning 2))
4867 (< origin (match-end 2)))
4868 (narrow-to-region (match-beginning 2) (match-end 2)))
4869 (t (throw 'objects-forbidden element)))
4870 ;; Also change type to retrieve correct restrictions.
4871 (setq type 'keyword))
4872 ;; At an item, objects can only be located within tag, if any.
4873 ((eq type 'item)
4874 (let ((tag (org-element-property :tag element)))
4875 (if (not tag) (throw 'objects-forbidden element)
4876 (beginning-of-line)
4877 (search-forward tag (line-end-position))
4878 (goto-char (match-beginning 0))
4879 (if (and (>= origin (point)) (< origin (match-end 0)))
4880 (narrow-to-region (point) (match-end 0))
4881 (throw 'objects-forbidden element)))))
4882 ;; At an headline or inlinetask, objects are located within
4883 ;; their title.
4884 ((memq type '(headline inlinetask))
4885 (goto-char (org-element-property :begin element))
4886 (skip-chars-forward "* ")
4887 (if (and (>= origin (point)) (< origin (line-end-position)))
4888 (narrow-to-region (point) (line-end-position))
4889 (throw 'objects-forbidden element)))
4890 ;; At a paragraph, a table-row or a verse block, objects are
4891 ;; located within their contents.
4892 ((memq type '(paragraph table-row verse-block))
4893 (let ((cbeg (org-element-property :contents-begin element))
4894 (cend (org-element-property :contents-end element)))
4895 ;; CBEG is nil for table rules.
4896 (if (and cbeg cend (>= origin cbeg) (< origin cend))
4897 (narrow-to-region cbeg cend)
4898 (throw 'objects-forbidden element))))
4899 ;; At a parsed keyword, objects are located within value.
4900 ((eq type 'keyword)
4901 (if (not (member (org-element-property :key element)
4902 org-element-document-properties))
4903 (throw 'objects-forbidden element)
4904 (beginning-of-line)
4905 (search-forward ":")
4906 (if (and (>= origin (point)) (< origin (line-end-position)))
4907 (narrow-to-region (point) (line-end-position))
4908 (throw 'objects-forbidden element))))
4909 (t (throw 'objects-forbidden element)))
4910 (goto-char (point-min))
4911 (let ((restriction (org-element-restriction type))
4912 (parent element)
4913 (candidates 'initial))
4914 (catch 'exit
4915 (while (setq candidates
4916 (org-element--get-next-object-candidates
4917 restriction candidates))
4918 (let ((closest-cand (rassq (apply 'min (mapcar 'cdr candidates))
4919 candidates)))
4920 ;; If ORIGIN is before next object in element, there's
4921 ;; no point in looking further.
4922 (if (> (cdr closest-cand) origin) (throw 'exit parent)
4923 (let* ((object
4924 (progn (goto-char (cdr closest-cand))
4925 (funcall (intern (format "org-element-%s-parser"
4926 (car closest-cand))))))
4927 (cbeg (org-element-property :contents-begin object))
4928 (cend (org-element-property :contents-end object))
4929 (obj-end (org-element-property :end object)))
4930 (cond
4931 ;; ORIGIN is after OBJECT, so skip it.
4932 ((<= obj-end origin) (goto-char obj-end))
4933 ;; ORIGIN is within a non-recursive object or at
4934 ;; an object boundaries: Return that object.
4935 ((or (not cbeg) (< origin cbeg) (>= origin cend))
4936 (throw 'exit
4937 (org-element-put-property object :parent parent)))
4938 ;; Otherwise, move within current object and
4939 ;; restrict search to the end of its contents.
4940 (t (goto-char cbeg)
4941 (narrow-to-region (point) cend)
4942 (org-element-put-property object :parent parent)
4943 (setq parent object
4944 restriction (org-element-restriction object)
4945 candidates 'initial)))))))
4946 parent))))))
4948 (defun org-element-nested-p (elem-A elem-B)
4949 "Non-nil when elements ELEM-A and ELEM-B are nested."
4950 (let ((beg-A (org-element-property :begin elem-A))
4951 (beg-B (org-element-property :begin elem-B))
4952 (end-A (org-element-property :end elem-A))
4953 (end-B (org-element-property :end elem-B)))
4954 (or (and (>= beg-A beg-B) (<= end-A end-B))
4955 (and (>= beg-B beg-A) (<= end-B end-A)))))
4957 (defun org-element-swap-A-B (elem-A elem-B)
4958 "Swap elements ELEM-A and ELEM-B.
4959 Assume ELEM-B is after ELEM-A in the buffer. Leave point at the
4960 end of ELEM-A."
4961 (goto-char (org-element-property :begin elem-A))
4962 ;; There are two special cases when an element doesn't start at bol:
4963 ;; the first paragraph in an item or in a footnote definition.
4964 (let ((specialp (not (bolp))))
4965 ;; Only a paragraph without any affiliated keyword can be moved at
4966 ;; ELEM-A position in such a situation. Note that the case of
4967 ;; a footnote definition is impossible: it cannot contain two
4968 ;; paragraphs in a row because it cannot contain a blank line.
4969 (if (and specialp
4970 (or (not (eq (org-element-type elem-B) 'paragraph))
4971 (/= (org-element-property :begin elem-B)
4972 (org-element-property :contents-begin elem-B))))
4973 (error "Cannot swap elements"))
4974 ;; In a special situation, ELEM-A will have no indentation. We'll
4975 ;; give it ELEM-B's (which will in, in turn, have no indentation).
4976 (let* ((ind-B (when specialp
4977 (goto-char (org-element-property :begin elem-B))
4978 (org-get-indentation)))
4979 (beg-A (org-element-property :begin elem-A))
4980 (end-A (save-excursion
4981 (goto-char (org-element-property :end elem-A))
4982 (skip-chars-backward " \r\t\n")
4983 (point-at-eol)))
4984 (beg-B (org-element-property :begin elem-B))
4985 (end-B (save-excursion
4986 (goto-char (org-element-property :end elem-B))
4987 (skip-chars-backward " \r\t\n")
4988 (point-at-eol)))
4989 ;; Store overlays responsible for visibility status. We
4990 ;; also need to store their boundaries as they will be
4991 ;; removed from buffer.
4992 (overlays
4993 (cons
4994 (mapcar (lambda (ov) (list ov (overlay-start ov) (overlay-end ov)))
4995 (overlays-in beg-A end-A))
4996 (mapcar (lambda (ov) (list ov (overlay-start ov) (overlay-end ov)))
4997 (overlays-in beg-B end-B))))
4998 ;; Get contents.
4999 (body-A (buffer-substring beg-A end-A))
5000 (body-B (delete-and-extract-region beg-B end-B)))
5001 (goto-char beg-B)
5002 (when specialp
5003 (setq body-B (replace-regexp-in-string "\\`[ \t]*" "" body-B))
5004 (org-indent-to-column ind-B))
5005 (insert body-A)
5006 ;; Restore ex ELEM-A overlays.
5007 (let ((offset (- beg-B beg-A)))
5008 (mapc (lambda (ov)
5009 (move-overlay
5010 (car ov) (+ (nth 1 ov) offset) (+ (nth 2 ov) offset)))
5011 (car overlays))
5012 (goto-char beg-A)
5013 (delete-region beg-A end-A)
5014 (insert body-B)
5015 ;; Restore ex ELEM-B overlays.
5016 (mapc (lambda (ov)
5017 (move-overlay
5018 (car ov) (- (nth 1 ov) offset) (- (nth 2 ov) offset)))
5019 (cdr overlays)))
5020 (goto-char (org-element-property :end elem-B)))))
5022 (provide 'org-element)
5024 ;; Local variables:
5025 ;; generated-autoload-file: "org-loaddefs.el"
5026 ;; End:
5028 ;;; org-element.el ends here