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