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