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