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