4a73e2692546acaed99a598303b335c369e6992e
[org-mode.git] / lisp / org-element.el
blob4a73e2692546acaed99a598303b335c369e6992e
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 an 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-alphabetical-lists "\\|[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 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.
202 Sharing the same successor comes handy when, for example, the
203 regexp matching one object can also match the other object.")
205 (defconst org-element-all-objects
206 '(bold code entity export-snippet footnote-reference inline-babel-call
207 inline-src-block italic line-break latex-fragment link macro
208 radio-target statistics-cookie strike-through subscript superscript
209 table-cell target timestamp underline verbatim)
210 "Complete list of object types.")
212 (defconst org-element-recursive-objects
213 '(bold italic link subscript radio-target strike-through superscript
214 table-cell underline)
215 "List of recursive object types.")
217 (defvar org-element-block-name-alist
218 '(("CENTER" . org-element-center-block-parser)
219 ("COMMENT" . org-element-comment-block-parser)
220 ("EXAMPLE" . org-element-example-block-parser)
221 ("QUOTE" . org-element-quote-block-parser)
222 ("SRC" . org-element-src-block-parser)
223 ("VERSE" . org-element-verse-block-parser))
224 "Alist between block names and the associated parsing function.
225 Names must be uppercase. Any block whose name has no association
226 is parsed with `org-element-special-block-parser'.")
228 (defconst org-element-link-type-is-file
229 '("file" "file+emacs" "file+sys" "docview")
230 "List of link types equivalent to \"file\".
231 Only these types can accept search options and an explicit
232 application to open them.")
234 (defconst org-element-affiliated-keywords
235 '("CAPTION" "DATA" "HEADER" "HEADERS" "LABEL" "NAME" "PLOT" "RESNAME" "RESULT"
236 "RESULTS" "SOURCE" "SRCNAME" "TBLNAME")
237 "List of affiliated keywords as strings.
238 By default, all keywords setting attributes (i.e. \"ATTR_LATEX\")
239 are affiliated keywords and need not to be in this list.")
241 (defconst org-element--affiliated-re
242 (format "[ \t]*#\\+%s:"
243 ;; Regular affiliated keywords.
244 (format "\\(%s\\|ATTR_[-_A-Za-z0-9]+\\)\\(?:\\[\\(.*\\)\\]\\)?"
245 (regexp-opt org-element-affiliated-keywords)))
246 "Regexp matching any affiliated keyword.
248 Keyword name is put in match group 1. Moreover, if keyword
249 belongs to `org-element-dual-keywords', put the dual value in
250 match group 2.
252 Don't modify it, set `org-element-affiliated-keywords' instead.")
254 (defconst org-element-keyword-translation-alist
255 '(("DATA" . "NAME") ("LABEL" . "NAME") ("RESNAME" . "NAME")
256 ("SOURCE" . "NAME") ("SRCNAME" . "NAME") ("TBLNAME" . "NAME")
257 ("RESULT" . "RESULTS") ("HEADERS" . "HEADER"))
258 "Alist of usual translations for keywords.
259 The key is the old name and the value the new one. The property
260 holding their value will be named after the translated name.")
262 (defconst org-element-multiple-keywords '("CAPTION" "HEADER")
263 "List of affiliated keywords that can occur more than once in an element.
265 Their value will be consed into a list of strings, which will be
266 returned as the value of the property.
268 This list is checked after translations have been applied. See
269 `org-element-keyword-translation-alist'.
271 By default, all keywords setting attributes (i.e. \"ATTR_LATEX\")
272 allow multiple occurrences and need not to be in this list.")
274 (defconst org-element-parsed-keywords '("CAPTION")
275 "List of affiliated keywords whose value can be parsed.
277 Their value will be stored as a secondary string: a list of
278 strings and objects.
280 This list is checked after translations have been applied. See
281 `org-element-keyword-translation-alist'.")
283 (defconst org-element-dual-keywords '("CAPTION" "RESULTS")
284 "List of affiliated keywords which can have a secondary value.
286 In Org syntax, they can be written with optional square brackets
287 before the colons. For example, RESULTS keyword can be
288 associated to a hash value with the following:
290 #+RESULTS[hash-string]: some-source
292 This list is checked after translations have been applied. See
293 `org-element-keyword-translation-alist'.")
295 (defconst org-element-document-properties '("AUTHOR" "DATE" "TITLE")
296 "List of properties associated to the whole document.
297 Any keyword in this list will have its value parsed and stored as
298 a secondary string.")
300 (defconst org-element-object-restrictions
301 '((bold export-snippet inline-babel-call inline-src-block latex-or-entity link
302 radio-target sub/superscript target text-markup timestamp)
303 (footnote-reference export-snippet footnote-reference inline-babel-call
304 inline-src-block latex-or-entity line-break link macro
305 radio-target sub/superscript target text-markup
306 timestamp)
307 (headline inline-babel-call inline-src-block latex-or-entity link macro
308 radio-target statistics-cookie sub/superscript target text-markup
309 timestamp)
310 (inlinetask inline-babel-call inline-src-block latex-or-entity link macro
311 radio-target sub/superscript target text-markup timestamp)
312 (italic export-snippet inline-babel-call inline-src-block latex-or-entity
313 link radio-target sub/superscript target text-markup timestamp)
314 (item export-snippet footnote-reference inline-babel-call latex-or-entity
315 link macro radio-target sub/superscript target text-markup)
316 (keyword inline-babel-call inline-src-block latex-or-entity link macro
317 sub/superscript text-markup timestamp)
318 (link export-snippet inline-babel-call inline-src-block latex-or-entity link
319 sub/superscript text-markup)
320 (paragraph export-snippet footnote-reference inline-babel-call
321 inline-src-block latex-or-entity line-break link macro
322 radio-target statistics-cookie sub/superscript target text-markup
323 timestamp)
324 (radio-target export-snippet latex-or-entity sub/superscript)
325 (strike-through export-snippet inline-babel-call inline-src-block
326 latex-or-entity link radio-target sub/superscript target
327 text-markup timestamp)
328 (subscript export-snippet inline-babel-call inline-src-block latex-or-entity
329 sub/superscript target text-markup)
330 (superscript export-snippet inline-babel-call inline-src-block
331 latex-or-entity sub/superscript target text-markup)
332 (table-cell export-snippet footnote-reference latex-or-entity link macro
333 radio-target sub/superscript target text-markup timestamp)
334 (table-row table-cell)
335 (underline export-snippet inline-babel-call inline-src-block latex-or-entity
336 link radio-target sub/superscript target text-markup timestamp)
337 (verse-block footnote-reference inline-babel-call inline-src-block
338 latex-or-entity line-break link macro radio-target
339 sub/superscript target text-markup timestamp))
340 "Alist of objects restrictions.
342 CAR is an element or object type containing objects and CDR is
343 a list of successors that will be called within an element or
344 object of such type.
346 For example, in a `radio-target' object, one can only find
347 entities, export snippets, latex-fragments, subscript and
348 superscript.
350 This alist also applies to secondary string. For example, an
351 `headline' type element doesn't directly contain objects, but
352 still has an entry since one of its properties (`:title') does.")
354 (defconst org-element-secondary-value-alist
355 '((headline . :title)
356 (inlinetask . :title)
357 (item . :tag)
358 (footnote-reference . :inline-definition))
359 "Alist between element types and location of secondary value.")
361 (defconst org-element-object-variables '(org-link-abbrev-alist-local)
362 "List of buffer-local variables used when parsing objects.
363 These variables are copied to the temporary buffer created by
364 `org-export-secondary-string'.")
368 ;;; Accessors and Setters
370 ;; Provide four accessors: `org-element-type', `org-element-property'
371 ;; `org-element-contents' and `org-element-restriction'.
373 ;; Setter functions allow to modify elements by side effect. There is
374 ;; `org-element-put-property', `org-element-set-contents',
375 ;; `org-element-set-element' and `org-element-adopt-element'. Note
376 ;; that `org-element-set-element' and `org-element-adopt-elements' are
377 ;; higher level functions since also update `:parent' property.
379 (defsubst org-element-type (element)
380 "Return type of ELEMENT.
382 The function returns the type of the element or object provided.
383 It can also return the following special value:
384 `plain-text' for a string
385 `org-data' for a complete document
386 nil in any other case."
387 (cond
388 ((not (consp element)) (and (stringp element) 'plain-text))
389 ((symbolp (car element)) (car element))))
391 (defsubst org-element-property (property element)
392 "Extract the value from the PROPERTY of an ELEMENT."
393 (if (stringp element) (get-text-property 0 property element)
394 (plist-get (nth 1 element) property)))
396 (defsubst org-element-contents (element)
397 "Extract contents from an ELEMENT."
398 (cond ((not (consp element)) nil)
399 ((symbolp (car element)) (nthcdr 2 element))
400 (t element)))
402 (defsubst org-element-restriction (element)
403 "Return restriction associated to ELEMENT.
404 ELEMENT can be an element, an object or a symbol representing an
405 element or object type."
406 (cdr (assq (if (symbolp element) element (org-element-type element))
407 org-element-object-restrictions)))
409 (defsubst org-element-put-property (element property value)
410 "In ELEMENT set PROPERTY to VALUE.
411 Return modified element."
412 (if (stringp element) (org-add-props element nil property value)
413 (setcar (cdr element) (plist-put (nth 1 element) property value))
414 element))
416 (defsubst org-element-set-contents (element &rest contents)
417 "Set ELEMENT contents to CONTENTS.
418 Return modified element."
419 (cond ((not element) (list contents))
420 ((not (symbolp (car element))) contents)
421 ((cdr element) (setcdr (cdr element) contents))
422 (t (nconc element contents))))
424 (defsubst org-element-set-element (old new)
425 "Replace element or object OLD with element or object NEW.
426 The function takes care of setting `:parent' property for NEW."
427 ;; Since OLD is going to be changed into NEW by side-effect, first
428 ;; make sure that every element or object within NEW has OLD as
429 ;; parent.
430 (mapc (lambda (blob) (org-element-put-property blob :parent old))
431 (org-element-contents new))
432 ;; Transfer contents.
433 (apply 'org-element-set-contents old (org-element-contents new))
434 ;; Ensure NEW has same parent as OLD, then overwrite OLD properties
435 ;; with NEW's.
436 (org-element-put-property new :parent (org-element-property :parent old))
437 (setcar (cdr old) (nth 1 new))
438 ;; Transfer type.
439 (setcar old (car new)))
441 (defsubst org-element-adopt-elements (parent &rest children)
442 "Append elements to the contents of another element.
444 PARENT is an element or object. CHILDREN can be elements,
445 objects, or a strings.
447 The function takes care of setting `:parent' property for CHILD.
448 Return parent element."
449 ;; Link every child to PARENT. If PARENT is nil, it is a secondary
450 ;; string: parent is the list itself.
451 (mapc (lambda (child)
452 (org-element-put-property child :parent (or parent children)))
453 children)
454 ;; Add CHILDREN at the end of PARENT contents.
455 (when parent
456 (apply 'org-element-set-contents
457 parent
458 (nconc (org-element-contents parent) children)))
459 ;; Return modified PARENT element.
460 (or parent children))
464 ;;; Greater elements
466 ;; For each greater element type, we define a parser and an
467 ;; interpreter.
469 ;; A parser returns the element or object as the list described above.
470 ;; Most of them accepts no argument. Though, exceptions exist. Hence
471 ;; every element containing a secondary string (see
472 ;; `org-element-secondary-value-alist') will accept an optional
473 ;; argument to toggle parsing of that secondary string. Moreover,
474 ;; `item' parser requires current list's structure as its first
475 ;; element.
477 ;; An interpreter accepts two arguments: the list representation of
478 ;; the element or object, and its contents. The latter may be nil,
479 ;; depending on the element or object considered. It returns the
480 ;; appropriate Org syntax, as a string.
482 ;; Parsing functions must follow the naming convention:
483 ;; org-element-TYPE-parser, where TYPE is greater element's type, as
484 ;; defined in `org-element-greater-elements'.
486 ;; Similarly, interpreting functions must follow the naming
487 ;; convention: org-element-TYPE-interpreter.
489 ;; With the exception of `headline' and `item' types, greater elements
490 ;; cannot contain other greater elements of their own type.
492 ;; Beside implementing a parser and an interpreter, adding a new
493 ;; greater element requires to tweak `org-element--current-element'.
494 ;; Moreover, the newly defined type must be added to both
495 ;; `org-element-all-elements' and `org-element-greater-elements'.
498 ;;;; Center Block
500 (defun org-element-center-block-parser (limit affiliated)
501 "Parse a center block.
503 LIMIT bounds the search. AFFILIATED is a list of which CAR is
504 the buffer position at the beginning of the first affiliated
505 keyword and CDR is a plist of affiliated keywords along with
506 their value.
508 Return a list whose CAR is `center-block' and CDR is a plist
509 containing `:begin', `:end', `:hiddenp', `:contents-begin',
510 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
512 Assume point is at the beginning of the block."
513 (let ((case-fold-search t))
514 (if (not (save-excursion
515 (re-search-forward "^[ \t]*#\\+END_CENTER[ \t]*$" limit t)))
516 ;; Incomplete block: parse it as a paragraph.
517 (org-element-paragraph-parser limit affiliated)
518 (let ((block-end-line (match-beginning 0)))
519 (let* ((begin (car affiliated))
520 (post-affiliated (point))
521 ;; Empty blocks have no contents.
522 (contents-begin (progn (forward-line)
523 (and (< (point) block-end-line)
524 (point))))
525 (contents-end (and contents-begin block-end-line))
526 (hidden (org-invisible-p2))
527 (pos-before-blank (progn (goto-char block-end-line)
528 (forward-line)
529 (point)))
530 (end (save-excursion (skip-chars-forward " \r\t\n" limit)
531 (skip-chars-backward " \t")
532 (if (bolp) (point) (line-end-position)))))
533 (list 'center-block
534 (nconc
535 (list :begin begin
536 :end end
537 :hiddenp hidden
538 :contents-begin contents-begin
539 :contents-end contents-end
540 :post-blank (count-lines pos-before-blank end)
541 :post-affiliated post-affiliated)
542 (cdr affiliated))))))))
544 (defun org-element-center-block-interpreter (center-block contents)
545 "Interpret CENTER-BLOCK element as Org syntax.
546 CONTENTS is the contents of the element."
547 (format "#+BEGIN_CENTER\n%s#+END_CENTER" contents))
550 ;;;; Drawer
552 (defun org-element-drawer-parser (limit affiliated)
553 "Parse a drawer.
555 LIMIT bounds the search. AFFILIATED is a list of which CAR is
556 the buffer position at the beginning of the first affiliated
557 keyword and CDR is a plist of affiliated keywords along with
558 their value.
560 Return a list whose CAR is `drawer' and CDR is a plist containing
561 `:drawer-name', `:begin', `:end', `:hiddenp', `:contents-begin',
562 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
564 Assume point is at beginning of drawer."
565 (let ((case-fold-search t))
566 (if (not (save-excursion (re-search-forward "^[ \t]*:END:[ \t]*$" limit t)))
567 ;; Incomplete drawer: parse it as a paragraph.
568 (org-element-paragraph-parser limit affiliated)
569 (save-excursion
570 (let* ((drawer-end-line (match-beginning 0))
571 (name (progn (looking-at org-drawer-regexp)
572 (org-match-string-no-properties 1)))
573 (begin (car affiliated))
574 (post-affiliated (point))
575 ;; Empty drawers have no contents.
576 (contents-begin (progn (forward-line)
577 (and (< (point) drawer-end-line)
578 (point))))
579 (contents-end (and contents-begin drawer-end-line))
580 (hidden (org-invisible-p2))
581 (pos-before-blank (progn (goto-char drawer-end-line)
582 (forward-line)
583 (point)))
584 (end (progn (skip-chars-forward " \r\t\n" limit)
585 (skip-chars-backward " \t")
586 (if (bolp) (point) (line-end-position)))))
587 (list 'drawer
588 (nconc
589 (list :begin begin
590 :end end
591 :drawer-name name
592 :hiddenp hidden
593 :contents-begin contents-begin
594 :contents-end contents-end
595 :post-blank (count-lines pos-before-blank end)
596 :post-affiliated post-affiliated)
597 (cdr affiliated))))))))
599 (defun org-element-drawer-interpreter (drawer contents)
600 "Interpret DRAWER element as Org syntax.
601 CONTENTS is the contents of the element."
602 (format ":%s:\n%s:END:"
603 (org-element-property :drawer-name drawer)
604 contents))
607 ;;;; Dynamic Block
609 (defun org-element-dynamic-block-parser (limit affiliated)
610 "Parse a dynamic block.
612 LIMIT bounds the search. AFFILIATED is a list of which CAR is
613 the buffer position at the beginning of the first affiliated
614 keyword and CDR is a plist of affiliated keywords along with
615 their value.
617 Return a list whose CAR is `dynamic-block' and CDR is a plist
618 containing `:block-name', `:begin', `:end', `:hiddenp',
619 `:contents-begin', `:contents-end', `:arguments', `:post-blank'
620 and `:post-affiliated' keywords.
622 Assume point is at beginning of dynamic block."
623 (let ((case-fold-search t))
624 (if (not (save-excursion
625 (re-search-forward "^[ \t]*#\\+END:?[ \t]*$" limit t)))
626 ;; Incomplete block: parse it as a paragraph.
627 (org-element-paragraph-parser limit affiliated)
628 (let ((block-end-line (match-beginning 0)))
629 (save-excursion
630 (let* ((name (progn (looking-at org-dblock-start-re)
631 (org-match-string-no-properties 1)))
632 (arguments (org-match-string-no-properties 3))
633 (begin (car affiliated))
634 (post-affiliated (point))
635 ;; Empty blocks have no contents.
636 (contents-begin (progn (forward-line)
637 (and (< (point) block-end-line)
638 (point))))
639 (contents-end (and contents-begin block-end-line))
640 (hidden (org-invisible-p2))
641 (pos-before-blank (progn (goto-char block-end-line)
642 (forward-line)
643 (point)))
644 (end (progn (skip-chars-forward " \r\t\n" limit)
645 (skip-chars-backward " \t")
646 (if (bolp) (point) (line-end-position)))))
647 (list 'dynamic-block
648 (nconc
649 (list :begin begin
650 :end end
651 :block-name name
652 :arguments arguments
653 :hiddenp hidden
654 :contents-begin contents-begin
655 :contents-end contents-end
656 :post-blank (count-lines pos-before-blank end)
657 :post-affiliated post-affiliated)
658 (cdr affiliated)))))))))
660 (defun org-element-dynamic-block-interpreter (dynamic-block contents)
661 "Interpret DYNAMIC-BLOCK element as Org syntax.
662 CONTENTS is the contents of the element."
663 (format "#+BEGIN: %s%s\n%s#+END:"
664 (org-element-property :block-name dynamic-block)
665 (let ((args (org-element-property :arguments dynamic-block)))
666 (and args (concat " " args)))
667 contents))
670 ;;;; Footnote Definition
672 (defun org-element-footnote-definition-parser (limit affiliated)
673 "Parse a footnote definition.
675 LIMIT bounds the search. AFFILIATED is a list of which CAR is
676 the buffer position at the beginning of the first affiliated
677 keyword and CDR is a plist of affiliated keywords along with
678 their value.
680 Return a list whose CAR is `footnote-definition' and CDR is
681 a plist containing `:label', `:begin' `:end', `:contents-begin',
682 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
684 Assume point is at the beginning of the footnote definition."
685 (save-excursion
686 (let* ((label (progn (looking-at org-footnote-definition-re)
687 (org-match-string-no-properties 1)))
688 (begin (car affiliated))
689 (post-affiliated (point))
690 (ending (save-excursion
691 (if (progn
692 (end-of-line)
693 (re-search-forward
694 (concat org-outline-regexp-bol "\\|"
695 org-footnote-definition-re "\\|"
696 "^[ \t]*$") limit 'move))
697 (match-beginning 0)
698 (point))))
699 (contents-begin (progn (search-forward "]")
700 (skip-chars-forward " \r\t\n" ending)
701 (and (/= (point) ending) (point))))
702 (contents-end (and contents-begin ending))
703 (end (progn (goto-char ending)
704 (skip-chars-forward " \r\t\n" limit)
705 (skip-chars-backward " \t")
706 (if (bolp) (point) (line-end-position)))))
707 (list 'footnote-definition
708 (nconc
709 (list :label label
710 :begin begin
711 :end end
712 :contents-begin contents-begin
713 :contents-end contents-end
714 :post-blank (count-lines ending end)
715 :post-affiliated post-affiliated)
716 (cdr affiliated))))))
718 (defun org-element-footnote-definition-interpreter (footnote-definition contents)
719 "Interpret FOOTNOTE-DEFINITION element as Org syntax.
720 CONTENTS is the contents of the footnote-definition."
721 (concat (format "[%s]" (org-element-property :label footnote-definition))
723 contents))
726 ;;;; Headline
728 (defun org-element-headline-parser (limit &optional raw-secondary-p)
729 "Parse an headline.
731 Return a list whose CAR is `headline' and CDR is a plist
732 containing `:raw-value', `:title', `:begin', `:end',
733 `:pre-blank', `:hiddenp', `:contents-begin' and `:contents-end',
734 `:level', `:priority', `:tags', `:todo-keyword',`:todo-type',
735 `:scheduled', `:deadline', `:closed', `:quotedp', `:archivedp',
736 `:commentedp' and `:footnote-section-p' keywords.
738 The plist also contains any property set in the property drawer,
739 with its name in lowercase, the underscores replaced with hyphens
740 and colons at the beginning (i.e. `:custom-id').
742 When RAW-SECONDARY-P is non-nil, headline's title will not be
743 parsed as a secondary string, but as a plain string instead.
745 Assume point is at beginning of the headline."
746 (save-excursion
747 (let* ((components (org-heading-components))
748 (level (nth 1 components))
749 (todo (nth 2 components))
750 (todo-type
751 (and todo (if (member todo org-done-keywords) 'done 'todo)))
752 (tags (let ((raw-tags (nth 5 components)))
753 (and raw-tags (org-split-string raw-tags ":"))))
754 (raw-value (or (nth 4 components) ""))
755 (quotedp
756 (let ((case-fold-search nil))
757 (string-match (format "^%s\\( \\|$\\)" org-quote-string)
758 raw-value)))
759 (commentedp
760 (let ((case-fold-search nil))
761 (string-match (format "^%s\\( \\|$\\)" org-comment-string)
762 raw-value)))
763 (archivedp (member org-archive-tag tags))
764 (footnote-section-p (and org-footnote-section
765 (string= org-footnote-section raw-value)))
766 ;; Normalize property names: ":SOME_PROP:" becomes
767 ;; ":some-prop".
768 (standard-props
769 (let (plist)
770 (mapc
771 (lambda (p)
772 (setq plist
773 (plist-put plist
774 (intern (concat ":"
775 (replace-regexp-in-string
776 "_" "-" (downcase (car p)))))
777 (cdr p))))
778 (org-entry-properties nil 'standard))
779 plist))
780 (time-props
781 ;; Read time properties on the line below the headline.
782 (save-excursion
783 (when (progn (forward-line)
784 (looking-at org-planning-or-clock-line-re))
785 (let ((end (line-end-position)) plist)
786 (while (re-search-forward
787 org-keyword-time-not-clock-regexp end t)
788 (goto-char (match-end 1))
789 (skip-chars-forward " \t")
790 (let ((keyword (match-string 1))
791 (time (org-element-timestamp-parser)))
792 (cond ((equal keyword org-scheduled-string)
793 (setq plist (plist-put plist :scheduled time)))
794 ((equal keyword org-deadline-string)
795 (setq plist (plist-put plist :deadline time)))
796 (t (setq plist (plist-put plist :closed time))))))
797 plist))))
798 (begin (point))
799 (end (save-excursion (goto-char (org-end-of-subtree t t))))
800 (pos-after-head (progn (forward-line) (point)))
801 (contents-begin (save-excursion
802 (skip-chars-forward " \r\t\n" end)
803 (and (/= (point) end) (line-beginning-position))))
804 (hidden (org-invisible-p2))
805 (contents-end (and contents-begin
806 (progn (goto-char end)
807 (skip-chars-backward " \r\t\n")
808 (forward-line)
809 (point)))))
810 ;; Clean RAW-VALUE from any quote or comment string.
811 (when (or quotedp commentedp)
812 (let ((case-fold-search nil))
813 (setq raw-value
814 (replace-regexp-in-string
815 (concat
816 (regexp-opt (list org-quote-string org-comment-string))
817 "\\(?: \\|$\\)")
819 raw-value))))
820 ;; Clean TAGS from archive tag, if any.
821 (when archivedp (setq tags (delete org-archive-tag tags)))
822 (let ((headline
823 (list 'headline
824 (nconc
825 (list :raw-value raw-value
826 :begin begin
827 :end end
828 :pre-blank
829 (if (not contents-begin) 0
830 (count-lines pos-after-head contents-begin))
831 :hiddenp hidden
832 :contents-begin contents-begin
833 :contents-end contents-end
834 :level level
835 :priority (nth 3 components)
836 :tags tags
837 :todo-keyword todo
838 :todo-type todo-type
839 :post-blank (count-lines
840 (if (not contents-end) pos-after-head
841 (goto-char contents-end)
842 (forward-line)
843 (point))
844 end)
845 :footnote-section-p footnote-section-p
846 :archivedp archivedp
847 :commentedp commentedp
848 :quotedp quotedp)
849 time-props
850 standard-props))))
851 (org-element-put-property
852 headline :title
853 (if raw-secondary-p raw-value
854 (org-element-parse-secondary-string
855 raw-value (org-element-restriction 'headline) headline)))))))
857 (defun org-element-headline-interpreter (headline contents)
858 "Interpret HEADLINE element as Org syntax.
859 CONTENTS is the contents of the element."
860 (let* ((level (org-element-property :level headline))
861 (todo (org-element-property :todo-keyword headline))
862 (priority (org-element-property :priority headline))
863 (title (org-element-interpret-data
864 (org-element-property :title headline)))
865 (tags (let ((tag-list (if (org-element-property :archivedp headline)
866 (cons org-archive-tag
867 (org-element-property :tags headline))
868 (org-element-property :tags headline))))
869 (and tag-list
870 (format ":%s:" (mapconcat 'identity tag-list ":")))))
871 (commentedp (org-element-property :commentedp headline))
872 (quotedp (org-element-property :quotedp headline))
873 (pre-blank (or (org-element-property :pre-blank headline) 0))
874 (heading (concat (make-string level ?*)
875 (and todo (concat " " todo))
876 (and quotedp (concat " " org-quote-string))
877 (and commentedp (concat " " org-comment-string))
878 (and priority
879 (format " [#%s]" (char-to-string priority)))
880 (cond ((and org-footnote-section
881 (org-element-property
882 :footnote-section-p headline))
883 (concat " " org-footnote-section))
884 (title (concat " " title))))))
885 (concat heading
886 ;; Align tags.
887 (when tags
888 (cond
889 ((zerop org-tags-column) (format " %s" tags))
890 ((< org-tags-column 0)
891 (concat
892 (make-string
893 (max (- (+ org-tags-column (length heading) (length tags))) 1)
895 tags))
897 (concat
898 (make-string (max (- org-tags-column (length heading)) 1) ? )
899 tags))))
900 (make-string (1+ pre-blank) 10)
901 contents)))
904 ;;;; Inlinetask
906 (defun org-element-inlinetask-parser (limit &optional raw-secondary-p)
907 "Parse an inline task.
909 Return a list whose CAR is `inlinetask' and CDR is a plist
910 containing `:title', `:begin', `:end', `:hiddenp',
911 `:contents-begin' and `:contents-end', `:level', `:priority',
912 `:raw-value', `:tags', `:todo-keyword', `:todo-type',
913 `:scheduled', `:deadline', `:closed' and `:post-blank' keywords.
915 The plist also contains any property set in the property drawer,
916 with its name in lowercase, the underscores replaced with hyphens
917 and colons at the beginning (i.e. `:custom-id').
919 When optional argument RAW-SECONDARY-P is non-nil, inline-task's
920 title will not be parsed as a secondary string, but as a plain
921 string instead.
923 Assume point is at beginning of the inline task."
924 (save-excursion
925 (let* ((begin (point))
926 (components (org-heading-components))
927 (todo (nth 2 components))
928 (todo-type (and todo
929 (if (member todo org-done-keywords) 'done 'todo)))
930 (tags (let ((raw-tags (nth 5 components)))
931 (and raw-tags (org-split-string raw-tags ":"))))
932 (raw-value (or (nth 4 components) ""))
933 ;; Normalize property names: ":SOME_PROP:" becomes
934 ;; ":some-prop".
935 (standard-props
936 (let (plist)
937 (mapc
938 (lambda (p)
939 (setq plist
940 (plist-put plist
941 (intern (concat ":"
942 (replace-regexp-in-string
943 "_" "-" (downcase (car p)))))
944 (cdr p))))
945 (org-entry-properties nil 'standard))
946 plist))
947 (time-props
948 ;; Read time properties on the line below the inlinetask
949 ;; opening string.
950 (save-excursion
951 (when (progn (forward-line)
952 (looking-at org-planning-or-clock-line-re))
953 (let ((end (line-end-position)) plist)
954 (while (re-search-forward
955 org-keyword-time-not-clock-regexp end t)
956 (goto-char (match-end 1))
957 (skip-chars-forward " \t")
958 (let ((keyword (match-string 1))
959 (time (org-element-timestamp-parser)))
960 (cond ((equal keyword org-scheduled-string)
961 (setq plist (plist-put plist :scheduled time)))
962 ((equal keyword org-deadline-string)
963 (setq plist (plist-put plist :deadline time)))
964 (t (setq plist (plist-put plist :closed time))))))
965 plist))))
966 (task-end (save-excursion
967 (end-of-line)
968 (and (re-search-forward "^\\*+ END" limit t)
969 (match-beginning 0))))
970 (contents-begin (progn (forward-line)
971 (and task-end (< (point) task-end) (point))))
972 (hidden (and contents-begin (org-invisible-p2)))
973 (contents-end (and contents-begin task-end))
974 (before-blank (if (not task-end) (point)
975 (goto-char task-end)
976 (forward-line)
977 (point)))
978 (end (progn (skip-chars-forward " \r\t\n" limit)
979 (skip-chars-backward " \t")
980 (if (bolp) (point) (line-end-position))))
981 (inlinetask
982 (list 'inlinetask
983 (nconc
984 (list :raw-value raw-value
985 :begin begin
986 :end end
987 :hiddenp hidden
988 :contents-begin contents-begin
989 :contents-end contents-end
990 :level (nth 1 components)
991 :priority (nth 3 components)
992 :tags tags
993 :todo-keyword todo
994 :todo-type todo-type
995 :post-blank (count-lines before-blank end))
996 time-props
997 standard-props))))
998 (org-element-put-property
999 inlinetask :title
1000 (if raw-secondary-p raw-value
1001 (org-element-parse-secondary-string
1002 raw-value
1003 (org-element-restriction 'inlinetask)
1004 inlinetask))))))
1006 (defun org-element-inlinetask-interpreter (inlinetask contents)
1007 "Interpret INLINETASK element as Org syntax.
1008 CONTENTS is the contents of inlinetask."
1009 (let* ((level (org-element-property :level inlinetask))
1010 (todo (org-element-property :todo-keyword inlinetask))
1011 (priority (org-element-property :priority inlinetask))
1012 (title (org-element-interpret-data
1013 (org-element-property :title inlinetask)))
1014 (tags (let ((tag-list (org-element-property :tags inlinetask)))
1015 (and tag-list
1016 (format ":%s:" (mapconcat 'identity tag-list ":")))))
1017 (task (concat (make-string level ?*)
1018 (and todo (concat " " todo))
1019 (and priority
1020 (format " [#%s]" (char-to-string priority)))
1021 (and title (concat " " title)))))
1022 (concat task
1023 ;; Align tags.
1024 (when tags
1025 (cond
1026 ((zerop org-tags-column) (format " %s" tags))
1027 ((< org-tags-column 0)
1028 (concat
1029 (make-string
1030 (max (- (+ org-tags-column (length task) (length tags))) 1)
1032 tags))
1034 (concat
1035 (make-string (max (- org-tags-column (length task)) 1) ? )
1036 tags))))
1037 ;; Prefer degenerate inlinetasks when there are no
1038 ;; contents.
1039 (when contents
1040 (concat "\n"
1041 contents
1042 (make-string level ?*) " END")))))
1045 ;;;; Item
1047 (defun org-element-item-parser (limit struct &optional raw-secondary-p)
1048 "Parse an item.
1050 STRUCT is the structure of the plain list.
1052 Return a list whose CAR is `item' and CDR is a plist containing
1053 `:bullet', `:begin', `:end', `:contents-begin', `:contents-end',
1054 `:checkbox', `:counter', `:tag', `:structure', `:hiddenp' and
1055 `:post-blank' keywords.
1057 When optional argument RAW-SECONDARY-P is non-nil, item's tag, if
1058 any, will not be parsed as a secondary string, but as a plain
1059 string instead.
1061 Assume point is at the beginning of the item."
1062 (save-excursion
1063 (beginning-of-line)
1064 (looking-at org-list-full-item-re)
1065 (let* ((begin (point))
1066 (bullet (org-match-string-no-properties 1))
1067 (checkbox (let ((box (org-match-string-no-properties 3)))
1068 (cond ((equal "[ ]" box) 'off)
1069 ((equal "[X]" box) 'on)
1070 ((equal "[-]" box) 'trans))))
1071 (counter (let ((c (org-match-string-no-properties 2)))
1072 (save-match-data
1073 (cond
1074 ((not c) nil)
1075 ((string-match "[A-Za-z]" c)
1076 (- (string-to-char (upcase (match-string 0 c)))
1077 64))
1078 ((string-match "[0-9]+" c)
1079 (string-to-number (match-string 0 c)))))))
1080 (end (save-excursion (goto-char (org-list-get-item-end begin struct))
1081 (unless (bolp) (forward-line))
1082 (point)))
1083 (contents-begin
1084 (progn (goto-char
1085 ;; Ignore tags in un-ordered lists: they are just
1086 ;; a part of item's body.
1087 (if (and (match-beginning 4)
1088 (save-match-data (string-match "[.)]" bullet)))
1089 (match-beginning 4)
1090 (match-end 0)))
1091 (skip-chars-forward " \r\t\n" limit)
1092 ;; If first line isn't empty, contents really start
1093 ;; at the text after item's meta-data.
1094 (if (= (point-at-bol) begin) (point) (point-at-bol))))
1095 (hidden (progn (forward-line)
1096 (and (not (= (point) end)) (org-invisible-p2))))
1097 (contents-end (progn (goto-char end)
1098 (skip-chars-backward " \r\t\n")
1099 (forward-line)
1100 (point)))
1101 (item
1102 (list 'item
1103 (list :bullet bullet
1104 :begin begin
1105 :end end
1106 ;; CONTENTS-BEGIN and CONTENTS-END may be
1107 ;; mixed up in the case of an empty item
1108 ;; separated from the next by a blank line.
1109 ;; Thus ensure the former is always the
1110 ;; smallest.
1111 :contents-begin (min contents-begin contents-end)
1112 :contents-end (max contents-begin contents-end)
1113 :checkbox checkbox
1114 :counter counter
1115 :hiddenp hidden
1116 :structure struct
1117 :post-blank (count-lines contents-end end)))))
1118 (org-element-put-property
1119 item :tag
1120 (let ((raw-tag (org-list-get-tag begin struct)))
1121 (and raw-tag
1122 (if raw-secondary-p raw-tag
1123 (org-element-parse-secondary-string
1124 raw-tag (org-element-restriction 'item) item))))))))
1126 (defun org-element-item-interpreter (item contents)
1127 "Interpret ITEM element as Org syntax.
1128 CONTENTS is the contents of the element."
1129 (let* ((bullet (org-list-bullet-string (org-element-property :bullet item)))
1130 (checkbox (org-element-property :checkbox item))
1131 (counter (org-element-property :counter item))
1132 (tag (let ((tag (org-element-property :tag item)))
1133 (and tag (org-element-interpret-data tag))))
1134 ;; Compute indentation.
1135 (ind (make-string (length bullet) 32))
1136 (item-starts-with-par-p
1137 (eq (org-element-type (car (org-element-contents item)))
1138 'paragraph)))
1139 ;; Indent contents.
1140 (concat
1141 bullet
1142 (and counter (format "[@%d] " counter))
1143 (case checkbox
1144 (on "[X] ")
1145 (off "[ ] ")
1146 (trans "[-] "))
1147 (and tag (format "%s :: " tag))
1148 (let ((contents (replace-regexp-in-string
1149 "\\(^\\)[ \t]*\\S-" ind contents nil nil 1)))
1150 (if item-starts-with-par-p (org-trim contents)
1151 (concat "\n" contents))))))
1154 ;;;; Plain List
1156 (defun org-element-plain-list-parser (limit affiliated structure)
1157 "Parse a plain list.
1159 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1160 the buffer position at the beginning of the first affiliated
1161 keyword and CDR is a plist of affiliated keywords along with
1162 their value. STRUCTURE is the structure of the plain list being
1163 parsed.
1165 Return a list whose CAR is `plain-list' and CDR is a plist
1166 containing `:type', `:begin', `:end', `:contents-begin' and
1167 `:contents-end', `:structure', `:post-blank' and
1168 `:post-affiliated' keywords.
1170 Assume point is at the beginning of the list."
1171 (save-excursion
1172 (let* ((struct (or structure (org-list-struct)))
1173 (prevs (org-list-prevs-alist struct))
1174 (parents (org-list-parents-alist struct))
1175 (type (org-list-get-list-type (point) struct prevs))
1176 (contents-begin (point))
1177 (begin (car affiliated))
1178 (contents-end
1179 (progn (goto-char (org-list-get-list-end (point) struct prevs))
1180 (unless (bolp) (forward-line))
1181 (point)))
1182 (end (progn (skip-chars-forward " \r\t\n" limit)
1183 (skip-chars-backward " \t")
1184 (if (bolp) (point) (line-end-position)))))
1185 ;; Return value.
1186 (list 'plain-list
1187 (nconc
1188 (list :type type
1189 :begin begin
1190 :end end
1191 :contents-begin contents-begin
1192 :contents-end contents-end
1193 :structure struct
1194 :post-blank (count-lines contents-end end)
1195 :post-affiliated contents-begin)
1196 (cdr affiliated))))))
1198 (defun org-element-plain-list-interpreter (plain-list contents)
1199 "Interpret PLAIN-LIST element as Org syntax.
1200 CONTENTS is the contents of the element."
1201 (with-temp-buffer
1202 (insert contents)
1203 (goto-char (point-min))
1204 (org-list-repair)
1205 (buffer-string)))
1208 ;;;; Property Drawer
1210 (defun org-element-property-drawer-parser (limit affiliated)
1211 "Parse a property drawer.
1213 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1214 the buffer position at the beginning of the first affiliated
1215 keyword and CDR is a plist of affiliated keywords along with
1216 their value.
1218 Return a list whose CAR is `property-drawer' and CDR is a plist
1219 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1220 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
1222 Assume point is at the beginning of the property drawer."
1223 (save-excursion
1224 (let ((case-fold-search t))
1225 (if (not (save-excursion
1226 (re-search-forward "^[ \t]*:END:[ \t]*$" limit t)))
1227 ;; Incomplete drawer: parse it as a paragraph.
1228 (org-element-paragraph-parser limit affiliated)
1229 (save-excursion
1230 (let* ((drawer-end-line (match-beginning 0))
1231 (begin (car affiliated))
1232 (post-affiliated (point))
1233 (contents-begin (progn (forward-line)
1234 (and (< (point) drawer-end-line)
1235 (point))))
1236 (contents-end (and contents-begin drawer-end-line))
1237 (hidden (org-invisible-p2))
1238 (pos-before-blank (progn (goto-char drawer-end-line)
1239 (forward-line)
1240 (point)))
1241 (end (progn (skip-chars-forward " \r\t\n" limit)
1242 (skip-chars-backward " \t")
1243 (if (bolp) (point) (line-end-position)))))
1244 (list 'property-drawer
1245 (nconc
1246 (list :begin begin
1247 :end end
1248 :hiddenp hidden
1249 :contents-begin contents-begin
1250 :contents-end contents-end
1251 :post-blank (count-lines pos-before-blank end)
1252 :post-affiliated post-affiliated)
1253 (cdr affiliated)))))))))
1255 (defun org-element-property-drawer-interpreter (property-drawer contents)
1256 "Interpret PROPERTY-DRAWER element as Org syntax.
1257 CONTENTS is the properties within the drawer."
1258 (format ":PROPERTIES:\n%s:END:" contents))
1261 ;;;; Quote Block
1263 (defun org-element-quote-block-parser (limit affiliated)
1264 "Parse a quote block.
1266 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1267 the buffer position at the beginning of the first affiliated
1268 keyword and CDR is a plist of affiliated keywords along with
1269 their value.
1271 Return a list whose CAR is `quote-block' and CDR is a plist
1272 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1273 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
1275 Assume point is at the beginning of the block."
1276 (let ((case-fold-search t))
1277 (if (not (save-excursion
1278 (re-search-forward "^[ \t]*#\\+END_QUOTE[ \t]*$" limit t)))
1279 ;; Incomplete block: parse it as a paragraph.
1280 (org-element-paragraph-parser limit affiliated)
1281 (let ((block-end-line (match-beginning 0)))
1282 (save-excursion
1283 (let* ((begin (car affiliated))
1284 (post-affiliated (point))
1285 ;; Empty blocks have no contents.
1286 (contents-begin (progn (forward-line)
1287 (and (< (point) block-end-line)
1288 (point))))
1289 (contents-end (and contents-begin block-end-line))
1290 (hidden (org-invisible-p2))
1291 (pos-before-blank (progn (goto-char block-end-line)
1292 (forward-line)
1293 (point)))
1294 (end (progn (skip-chars-forward " \r\t\n" limit)
1295 (skip-chars-backward " \t")
1296 (if (bolp) (point) (line-end-position)))))
1297 (list 'quote-block
1298 (nconc
1299 (list :begin begin
1300 :end end
1301 :hiddenp hidden
1302 :contents-begin contents-begin
1303 :contents-end contents-end
1304 :post-blank (count-lines pos-before-blank end)
1305 :post-affiliated post-affiliated)
1306 (cdr affiliated)))))))))
1308 (defun org-element-quote-block-interpreter (quote-block contents)
1309 "Interpret QUOTE-BLOCK element as Org syntax.
1310 CONTENTS is the contents of the element."
1311 (format "#+BEGIN_QUOTE\n%s#+END_QUOTE" contents))
1314 ;;;; Section
1316 (defun org-element-section-parser (limit)
1317 "Parse a section.
1319 LIMIT bounds the search.
1321 Return a list whose CAR is `section' and CDR is a plist
1322 containing `:begin', `:end', `:contents-begin', `contents-end'
1323 and `:post-blank' keywords."
1324 (save-excursion
1325 ;; Beginning of section is the beginning of the first non-blank
1326 ;; line after previous headline.
1327 (let ((begin (point))
1328 (end (progn (org-with-limited-levels (outline-next-heading))
1329 (point)))
1330 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
1331 (forward-line)
1332 (point))))
1333 (list 'section
1334 (list :begin begin
1335 :end end
1336 :contents-begin begin
1337 :contents-end pos-before-blank
1338 :post-blank (count-lines pos-before-blank end))))))
1340 (defun org-element-section-interpreter (section contents)
1341 "Interpret SECTION element as Org syntax.
1342 CONTENTS is the contents of the element."
1343 contents)
1346 ;;;; Special Block
1348 (defun org-element-special-block-parser (limit affiliated)
1349 "Parse a special block.
1351 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1352 the buffer position at the beginning of the first affiliated
1353 keyword and CDR is a plist of affiliated keywords along with
1354 their value.
1356 Return a list whose CAR is `special-block' and CDR is a plist
1357 containing `:type', `:begin', `:end', `:hiddenp',
1358 `:contents-begin', `:contents-end', `:post-blank' and
1359 `:post-affiliated' keywords.
1361 Assume point is at the beginning of the block."
1362 (let* ((case-fold-search t)
1363 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(S-+\\)")
1364 (upcase (match-string-no-properties 1)))))
1365 (if (not (save-excursion
1366 (re-search-forward
1367 (format "^[ \t]*#\\+END_%s[ \t]*$" type) limit t)))
1368 ;; Incomplete block: parse it as a paragraph.
1369 (org-element-paragraph-parser limit affiliated)
1370 (let ((block-end-line (match-beginning 0)))
1371 (save-excursion
1372 (let* ((begin (car affiliated))
1373 (post-affiliated (point))
1374 ;; Empty blocks have no contents.
1375 (contents-begin (progn (forward-line)
1376 (and (< (point) block-end-line)
1377 (point))))
1378 (contents-end (and contents-begin block-end-line))
1379 (hidden (org-invisible-p2))
1380 (pos-before-blank (progn (goto-char block-end-line)
1381 (forward-line)
1382 (point)))
1383 (end (progn (skip-chars-forward " \r\t\n" limit)
1384 (skip-chars-backward " \t")
1385 (if (bolp) (point) (line-end-position)))))
1386 (list 'special-block
1387 (nconc
1388 (list :type type
1389 :begin begin
1390 :end end
1391 :hiddenp hidden
1392 :contents-begin contents-begin
1393 :contents-end contents-end
1394 :post-blank (count-lines pos-before-blank end)
1395 :post-affiliated post-affiliated)
1396 (cdr affiliated)))))))))
1398 (defun org-element-special-block-interpreter (special-block contents)
1399 "Interpret SPECIAL-BLOCK element as Org syntax.
1400 CONTENTS is the contents of the element."
1401 (let ((block-type (org-element-property :type special-block)))
1402 (format "#+BEGIN_%s\n%s#+END_%s" block-type contents block-type)))
1406 ;;; Elements
1408 ;; For each element, a parser and an interpreter are also defined.
1409 ;; Both follow the same naming convention used for greater elements.
1411 ;; Also, as for greater elements, adding a new element type is done
1412 ;; through the following steps: implement a parser and an interpreter,
1413 ;; tweak `org-element--current-element' so that it recognizes the new
1414 ;; type and add that new type to `org-element-all-elements'.
1416 ;; As a special case, when the newly defined type is a block type,
1417 ;; `org-element-block-name-alist' has to be modified accordingly.
1420 ;;;; Babel Call
1422 (defun org-element-babel-call-parser (limit affiliated)
1423 "Parse a babel call.
1425 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1426 the buffer position at the beginning of the first affiliated
1427 keyword and CDR is a plist of affiliated keywords along with
1428 their value.
1430 Return a list whose CAR is `babel-call' and CDR is a plist
1431 containing `:begin', `:end', `:info', `:post-blank' and
1432 `:post-affiliated' as keywords."
1433 (save-excursion
1434 (let ((case-fold-search t)
1435 (info (progn (looking-at org-babel-block-lob-one-liner-regexp)
1436 (org-babel-lob-get-info)))
1437 (begin (car affiliated))
1438 (post-affiliated (point))
1439 (pos-before-blank (progn (forward-line) (point)))
1440 (end (progn (skip-chars-forward " \r\t\n" limit)
1441 (skip-chars-backward " \t")
1442 (if (bolp) (point) (line-end-position)))))
1443 (list 'babel-call
1444 (nconc
1445 (list :begin begin
1446 :end end
1447 :info info
1448 :post-blank (count-lines pos-before-blank end)
1449 :post-affiliated post-affiliated)
1450 (cdr affiliated))))))
1452 (defun org-element-babel-call-interpreter (babel-call contents)
1453 "Interpret BABEL-CALL element as Org syntax.
1454 CONTENTS is nil."
1455 (let* ((babel-info (org-element-property :info babel-call))
1456 (main (car babel-info))
1457 (post-options (nth 1 babel-info)))
1458 (concat "#+CALL: "
1459 (if (not (string-match "\\[\\(\\[.*?\\]\\)\\]" main)) main
1460 ;; Remove redundant square brackets.
1461 (replace-match (match-string 1 main) nil nil main))
1462 (and post-options (format "[%s]" post-options)))))
1465 ;;;; Clock
1467 (defun org-element-clock-parser (limit)
1468 "Parse a clock.
1470 LIMIT bounds the search.
1472 Return a list whose CAR is `clock' and CDR is a plist containing
1473 `:status', `:value', `:time', `:begin', `:end' and `:post-blank'
1474 as keywords."
1475 (save-excursion
1476 (let* ((case-fold-search nil)
1477 (begin (point))
1478 (value (progn (search-forward org-clock-string (line-end-position) t)
1479 (skip-chars-forward " \t")
1480 (org-element-timestamp-parser)))
1481 (duration (and (search-forward " => " (line-end-position) t)
1482 (progn (skip-chars-forward " \t")
1483 (looking-at "\\(\\S-+\\)[ \t]*$"))
1484 (org-match-string-no-properties 1)))
1485 (status (if duration 'closed 'running))
1486 (post-blank (let ((before-blank (progn (forward-line) (point))))
1487 (skip-chars-forward " \r\t\n" limit)
1488 (skip-chars-backward " \t")
1489 (unless (bolp) (end-of-line))
1490 (count-lines before-blank (point))))
1491 (end (point)))
1492 (list 'clock
1493 (list :status status
1494 :value value
1495 :duration duration
1496 :begin begin
1497 :end end
1498 :post-blank post-blank)))))
1500 (defun org-element-clock-interpreter (clock contents)
1501 "Interpret CLOCK element as Org syntax.
1502 CONTENTS is nil."
1503 (concat org-clock-string " "
1504 (org-element-timestamp-interpreter
1505 (org-element-property :value clock) nil)
1506 (let ((duration (org-element-property :duration clock)))
1507 (and duration
1508 (concat " => "
1509 (apply 'format
1510 "%2s:%02s"
1511 (org-split-string duration ":")))))))
1514 ;;;; Comment
1516 (defun org-element-comment-parser (limit affiliated)
1517 "Parse a comment.
1519 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1520 the buffer position at the beginning of the first affiliated
1521 keyword and CDR is a plist of affiliated keywords along with
1522 their value.
1524 Return a list whose CAR is `comment' and CDR is a plist
1525 containing `:begin', `:end', `:value', `:post-blank',
1526 `:post-affiliated' keywords.
1528 Assume point is at comment beginning."
1529 (save-excursion
1530 (let* ((begin (car affiliated))
1531 (post-affiliated (point))
1532 (value (prog2 (looking-at "[ \t]*# ?")
1533 (buffer-substring-no-properties
1534 (match-end 0) (line-end-position))
1535 (forward-line)))
1536 (com-end
1537 ;; Get comments ending.
1538 (progn
1539 (while (and (< (point) limit) (looking-at "[ \t]*#\\( \\|$\\)"))
1540 ;; Accumulate lines without leading hash and first
1541 ;; whitespace.
1542 (setq value
1543 (concat value
1544 "\n"
1545 (buffer-substring-no-properties
1546 (match-end 0) (line-end-position))))
1547 (forward-line))
1548 (point)))
1549 (end (progn (goto-char com-end)
1550 (skip-chars-forward " \r\t\n" limit)
1551 (skip-chars-backward " \t")
1552 (if (bolp) (point) (line-end-position)))))
1553 (list 'comment
1554 (nconc
1555 (list :begin begin
1556 :end end
1557 :value value
1558 :post-blank (count-lines com-end end)
1559 :post-affiliated post-affiliated)
1560 (cdr affiliated))))))
1562 (defun org-element-comment-interpreter (comment contents)
1563 "Interpret COMMENT element as Org syntax.
1564 CONTENTS is nil."
1565 (replace-regexp-in-string "^" "# " (org-element-property :value comment)))
1568 ;;;; Comment Block
1570 (defun org-element-comment-block-parser (limit affiliated)
1571 "Parse an export block.
1573 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1574 the buffer position at the beginning of the first affiliated
1575 keyword and CDR is a plist of affiliated keywords along with
1576 their value.
1578 Return a list whose CAR is `comment-block' and CDR is a plist
1579 containing `:begin', `:end', `:hiddenp', `:value', `:post-blank'
1580 and `:post-affiliated' keywords.
1582 Assume point is at comment block beginning."
1583 (let ((case-fold-search t))
1584 (if (not (save-excursion
1585 (re-search-forward "^[ \t]*#\\+END_COMMENT[ \t]*$" limit t)))
1586 ;; Incomplete block: parse it as a paragraph.
1587 (org-element-paragraph-parser limit affiliated)
1588 (let ((contents-end (match-beginning 0)))
1589 (save-excursion
1590 (let* ((begin (car affiliated))
1591 (post-affiliated (point))
1592 (contents-begin (progn (forward-line) (point)))
1593 (hidden (org-invisible-p2))
1594 (pos-before-blank (progn (goto-char contents-end)
1595 (forward-line)
1596 (point)))
1597 (end (progn (skip-chars-forward " \r\t\n" limit)
1598 (skip-chars-backward " \t")
1599 (if (bolp) (point) (line-end-position))))
1600 (value (buffer-substring-no-properties
1601 contents-begin contents-end)))
1602 (list 'comment-block
1603 (nconc
1604 (list :begin begin
1605 :end end
1606 :value value
1607 :hiddenp hidden
1608 :post-blank (count-lines pos-before-blank end)
1609 :post-affiliated post-affiliated)
1610 (cdr affiliated)))))))))
1612 (defun org-element-comment-block-interpreter (comment-block contents)
1613 "Interpret COMMENT-BLOCK element as Org syntax.
1614 CONTENTS is nil."
1615 (format "#+BEGIN_COMMENT\n%s#+END_COMMENT"
1616 (org-remove-indentation (org-element-property :value comment-block))))
1619 ;;;; Diary Sexp
1621 (defun org-element-diary-sexp-parser (limit affiliated)
1622 "Parse a diary sexp.
1624 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1625 the buffer position at the beginning of the first affiliated
1626 keyword and CDR is a plist of affiliated keywords along with
1627 their value.
1629 Return a list whose CAR is `diary-sexp' and CDR is a plist
1630 containing `:begin', `:end', `:value', `:post-blank' and
1631 `:post-affiliated' keywords."
1632 (save-excursion
1633 (let ((begin (car affiliated))
1634 (post-affiliated (point))
1635 (value (progn (looking-at "\\(%%(.*\\)[ \t]*$")
1636 (org-match-string-no-properties 1)))
1637 (pos-before-blank (progn (forward-line) (point)))
1638 (end (progn (skip-chars-forward " \r\t\n" limit)
1639 (skip-chars-backward " \t")
1640 (if (bolp) (point) (line-end-position)))))
1641 (list 'diary-sexp
1642 (nconc
1643 (list :value value
1644 :begin begin
1645 :end end
1646 :post-blank (count-lines pos-before-blank end)
1647 :post-affiliated post-affiliated)
1648 (cdr affiliated))))))
1650 (defun org-element-diary-sexp-interpreter (diary-sexp contents)
1651 "Interpret DIARY-SEXP as Org syntax.
1652 CONTENTS is nil."
1653 (org-element-property :value diary-sexp))
1656 ;;;; Example Block
1658 (defun org-element-example-block-parser (limit affiliated)
1659 "Parse an example block.
1661 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1662 the buffer position at the beginning of the first affiliated
1663 keyword and CDR is a plist of affiliated keywords along with
1664 their value.
1666 Return a list whose CAR is `example-block' and CDR is a plist
1667 containing `:begin', `:end', `:number-lines', `:preserve-indent',
1668 `:retain-labels', `:use-labels', `:label-fmt', `:hiddenp',
1669 `:switches', `:value', `:post-blank' and `:post-affiliated'
1670 keywords."
1671 (let ((case-fold-search t))
1672 (if (not (save-excursion
1673 (re-search-forward "^[ \t]*#\\+END_EXAMPLE[ \t]*$" limit t)))
1674 ;; Incomplete block: parse it as a paragraph.
1675 (org-element-paragraph-parser limit affiliated)
1676 (let ((contents-end (match-beginning 0)))
1677 (save-excursion
1678 (let* ((switches
1679 (progn (looking-at "^[ \t]*#\\+BEGIN_EXAMPLE\\(?: +\\(.*\\)\\)?")
1680 (org-match-string-no-properties 1)))
1681 ;; Switches analysis
1682 (number-lines (cond ((not switches) nil)
1683 ((string-match "-n\\>" switches) 'new)
1684 ((string-match "+n\\>" switches) 'continued)))
1685 (preserve-indent (and switches (string-match "-i\\>" switches)))
1686 ;; Should labels be retained in (or stripped from) example
1687 ;; blocks?
1688 (retain-labels
1689 (or (not switches)
1690 (not (string-match "-r\\>" switches))
1691 (and number-lines (string-match "-k\\>" switches))))
1692 ;; What should code-references use - labels or
1693 ;; line-numbers?
1694 (use-labels
1695 (or (not switches)
1696 (and retain-labels (not (string-match "-k\\>" switches)))))
1697 (label-fmt (and switches
1698 (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
1699 (match-string 1 switches)))
1700 ;; Standard block parsing.
1701 (begin (car affiliated))
1702 (post-affiliated (point))
1703 (contents-begin (progn (forward-line) (point)))
1704 (hidden (org-invisible-p2))
1705 (value (org-unescape-code-in-string
1706 (buffer-substring-no-properties
1707 contents-begin contents-end)))
1708 (pos-before-blank (progn (goto-char contents-end)
1709 (forward-line)
1710 (point)))
1711 (end (progn (skip-chars-forward " \r\t\n" limit)
1712 (skip-chars-backward " \t")
1713 (if (bolp) (point) (line-end-position)))))
1714 (list 'example-block
1715 (nconc
1716 (list :begin begin
1717 :end end
1718 :value value
1719 :switches switches
1720 :number-lines number-lines
1721 :preserve-indent preserve-indent
1722 :retain-labels retain-labels
1723 :use-labels use-labels
1724 :label-fmt label-fmt
1725 :hiddenp hidden
1726 :post-blank (count-lines pos-before-blank end)
1727 :post-affiliated post-affiliated)
1728 (cdr affiliated)))))))))
1730 (defun org-element-example-block-interpreter (example-block contents)
1731 "Interpret EXAMPLE-BLOCK element as Org syntax.
1732 CONTENTS is nil."
1733 (let ((switches (org-element-property :switches example-block)))
1734 (concat "#+BEGIN_EXAMPLE" (and switches (concat " " switches)) "\n"
1735 (org-remove-indentation
1736 (org-escape-code-in-string
1737 (org-element-property :value example-block)))
1738 "#+END_EXAMPLE")))
1741 ;;;; Export Block
1743 (defun org-element-export-block-parser (limit affiliated)
1744 "Parse an export block.
1746 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1747 the buffer position at the beginning of the first affiliated
1748 keyword and CDR is a plist of affiliated keywords along with
1749 their value.
1751 Return a list whose CAR is `export-block' and CDR is a plist
1752 containing `:begin', `:end', `:type', `:hiddenp', `:value',
1753 `:post-blank' and `:post-affiliated' keywords.
1755 Assume point is at export-block beginning."
1756 (let* ((case-fold-search t)
1757 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
1758 (upcase (org-match-string-no-properties 1)))))
1759 (if (not (save-excursion
1760 (re-search-forward
1761 (format "^[ \t]*#\\+END_%s[ \t]*$" type) limit t)))
1762 ;; Incomplete block: parse it as a paragraph.
1763 (org-element-paragraph-parser limit affiliated)
1764 (let ((contents-end (match-beginning 0)))
1765 (save-excursion
1766 (let* ((begin (car affiliated))
1767 (post-affiliated (point))
1768 (contents-begin (progn (forward-line) (point)))
1769 (hidden (org-invisible-p2))
1770 (pos-before-blank (progn (goto-char contents-end)
1771 (forward-line)
1772 (point)))
1773 (end (progn (skip-chars-forward " \r\t\n" limit)
1774 (skip-chars-backward " \t")
1775 (if (bolp) (point) (line-end-position))))
1776 (value (buffer-substring-no-properties contents-begin
1777 contents-end)))
1778 (list 'export-block
1779 (nconc
1780 (list :begin begin
1781 :end end
1782 :type type
1783 :value value
1784 :hiddenp hidden
1785 :post-blank (count-lines pos-before-blank end)
1786 :post-affiliated post-affiliated)
1787 (cdr affiliated)))))))))
1789 (defun org-element-export-block-interpreter (export-block contents)
1790 "Interpret EXPORT-BLOCK element as Org syntax.
1791 CONTENTS is nil."
1792 (let ((type (org-element-property :type export-block)))
1793 (concat (format "#+BEGIN_%s\n" type)
1794 (org-element-property :value export-block)
1795 (format "#+END_%s" type))))
1798 ;;;; Fixed-width
1800 (defun org-element-fixed-width-parser (limit affiliated)
1801 "Parse a fixed-width section.
1803 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1804 the buffer position at the beginning of the first affiliated
1805 keyword and CDR is a plist of affiliated keywords along with
1806 their value.
1808 Return a list whose CAR is `fixed-width' and CDR is a plist
1809 containing `:begin', `:end', `:value', `:post-blank' and
1810 `:post-affiliated' keywords.
1812 Assume point is at the beginning of the fixed-width area."
1813 (save-excursion
1814 (let* ((begin (car affiliated))
1815 (post-affiliated (point))
1816 value
1817 (end-area
1818 (progn
1819 (while (and (< (point) limit)
1820 (looking-at "[ \t]*:\\( \\|$\\)"))
1821 ;; Accumulate text without starting colons.
1822 (setq value
1823 (concat value
1824 (buffer-substring-no-properties
1825 (match-end 0) (point-at-eol))
1826 "\n"))
1827 (forward-line))
1828 (point)))
1829 (end (progn (skip-chars-forward " \r\t\n" limit)
1830 (skip-chars-backward " \t")
1831 (if (bolp) (point) (line-end-position)))))
1832 (list 'fixed-width
1833 (nconc
1834 (list :begin begin
1835 :end end
1836 :value value
1837 :post-blank (count-lines end-area end)
1838 :post-affiliated post-affiliated)
1839 (cdr affiliated))))))
1841 (defun org-element-fixed-width-interpreter (fixed-width contents)
1842 "Interpret FIXED-WIDTH element as Org syntax.
1843 CONTENTS is nil."
1844 (replace-regexp-in-string
1845 "^" ": " (substring (org-element-property :value fixed-width) 0 -1)))
1848 ;;;; Horizontal Rule
1850 (defun org-element-horizontal-rule-parser (limit affiliated)
1851 "Parse an horizontal rule.
1853 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1854 the buffer position at the beginning of the first affiliated
1855 keyword and CDR is a plist of affiliated keywords along with
1856 their value.
1858 Return a list whose CAR is `horizontal-rule' and CDR is a plist
1859 containing `:begin', `:end', `:post-blank' and `:post-affiliated'
1860 keywords."
1861 (save-excursion
1862 (let ((begin (car affiliated))
1863 (post-affiliated (point))
1864 (post-hr (progn (forward-line) (point)))
1865 (end (progn (skip-chars-forward " \r\t\n" limit)
1866 (skip-chars-backward " \t")
1867 (if (bolp) (point) (line-end-position)))))
1868 (list 'horizontal-rule
1869 (nconc
1870 (list :begin begin
1871 :end end
1872 :post-blank (count-lines post-hr end)
1873 :post-affiliated post-affiliated)
1874 (cdr affiliated))))))
1876 (defun org-element-horizontal-rule-interpreter (horizontal-rule contents)
1877 "Interpret HORIZONTAL-RULE element as Org syntax.
1878 CONTENTS is nil."
1879 "-----")
1882 ;;;; Keyword
1884 (defun org-element-keyword-parser (limit affiliated)
1885 "Parse a keyword at point.
1887 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1888 the buffer position at the beginning of the first affiliated
1889 keyword and CDR is a plist of affiliated keywords along with
1890 their value.
1892 Return a list whose CAR is `keyword' and CDR is a plist
1893 containing `:key', `:value', `:begin', `:end', `:post-blank' and
1894 `:post-affiliated' keywords."
1895 (save-excursion
1896 (let ((begin (car affiliated))
1897 (post-affiliated (point))
1898 (key (progn (looking-at "[ \t]*#\\+\\(\\S-+*\\):")
1899 (upcase (org-match-string-no-properties 1))))
1900 (value (org-trim (buffer-substring-no-properties
1901 (match-end 0) (point-at-eol))))
1902 (pos-before-blank (progn (forward-line) (point)))
1903 (end (progn (skip-chars-forward " \r\t\n" limit)
1904 (skip-chars-backward " \t")
1905 (if (bolp) (point) (line-end-position)))))
1906 (list 'keyword
1907 (nconc
1908 (list :key key
1909 :value value
1910 :begin begin
1911 :end end
1912 :post-blank (count-lines pos-before-blank end)
1913 :post-affiliated post-affiliated)
1914 (cdr affiliated))))))
1916 (defun org-element-keyword-interpreter (keyword contents)
1917 "Interpret KEYWORD element as Org syntax.
1918 CONTENTS is nil."
1919 (format "#+%s: %s"
1920 (org-element-property :key keyword)
1921 (org-element-property :value keyword)))
1924 ;;;; Latex Environment
1926 (defun org-element-latex-environment-parser (limit affiliated)
1927 "Parse a LaTeX environment.
1929 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1930 the buffer position at the beginning of the first affiliated
1931 keyword and CDR is a plist of affiliated keywords along with
1932 their value.
1934 Return a list whose CAR is `latex-environment' and CDR is a plist
1935 containing `:begin', `:end', `:value', `:post-blank' and
1936 `:post-affiliated' keywords.
1938 Assume point is at the beginning of the latex environment."
1939 (save-excursion
1940 (let ((case-fold-search t)
1941 (code-begin (point)))
1942 (looking-at "[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}")
1943 (if (not (re-search-forward (format "^[ \t]*\\\\end{%s}[ \t]*$"
1944 (regexp-quote (match-string 1)))
1945 limit t))
1946 ;; Incomplete latex environment: parse it as a paragraph.
1947 (org-element-paragraph-parser limit affiliated)
1948 (let* ((code-end (progn (forward-line) (point)))
1949 (begin (car affiliated))
1950 (post-affiliated (point))
1951 (value (buffer-substring-no-properties code-begin code-end))
1952 (end (progn (skip-chars-forward " \r\t\n" limit)
1953 (skip-chars-backward " \t")
1954 (if (bolp) (point) (line-end-position)))))
1955 (list 'latex-environment
1956 (nconc
1957 (list :begin begin
1958 :end end
1959 :value value
1960 :post-blank (count-lines code-end end)
1961 :post-affiliated post-affiliated)
1962 (cdr affiliated))))))))
1964 (defun org-element-latex-environment-interpreter (latex-environment contents)
1965 "Interpret LATEX-ENVIRONMENT element as Org syntax.
1966 CONTENTS is nil."
1967 (org-element-property :value latex-environment))
1970 ;;;; Node Property
1972 (defun org-element-node-property-parser (limit)
1973 "Parse a node-property at point.
1975 LIMIT bounds the search.
1977 Return a list whose CAR is `node-property' and CDR is a plist
1978 containing `:key', `:value', `:begin', `:end' and `:post-blank'
1979 keywords."
1980 (save-excursion
1981 (let ((case-fold-search t)
1982 (begin (point))
1983 (key (progn (looking-at "[ \t]*:\\(.*?\\):[ \t]+\\(.*?\\)[ \t]*$")
1984 (org-match-string-no-properties 1)))
1985 (value (org-match-string-no-properties 2))
1986 (pos-before-blank (progn (forward-line) (point)))
1987 (end (progn (skip-chars-forward " \r\t\n" limit)
1988 (if (eobp) (point) (point-at-bol)))))
1989 (list 'node-property
1990 (list :key key
1991 :value value
1992 :begin begin
1993 :end end
1994 :post-blank (count-lines pos-before-blank end))))))
1996 (defun org-element-node-property-interpreter (node-property contents)
1997 "Interpret NODE-PROPERTY element as Org syntax.
1998 CONTENTS is nil."
1999 (format org-property-format
2000 (format ":%s:" (org-element-property :key node-property))
2001 (org-element-property :value node-property)))
2004 ;;;; Paragraph
2006 (defun org-element-paragraph-parser (limit affiliated)
2007 "Parse a paragraph.
2009 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2010 the buffer position at the beginning of the first affiliated
2011 keyword and CDR is a plist of affiliated keywords along with
2012 their value.
2014 Return a list whose CAR is `paragraph' and CDR is a plist
2015 containing `:begin', `:end', `:contents-begin' and
2016 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
2018 Assume point is at the beginning of the paragraph."
2019 (save-excursion
2020 (let* ((begin (car affiliated))
2021 (contents-begin (point))
2022 (before-blank
2023 (let ((case-fold-search t))
2024 (end-of-line)
2025 (if (not (re-search-forward
2026 org-element-paragraph-separate limit 'm))
2027 limit
2028 ;; A matching `org-element-paragraph-separate' is not
2029 ;; necessarily the end of the paragraph. In
2030 ;; particular, lines starting with # or : as a first
2031 ;; non-space character are ambiguous. We have check
2032 ;; if they are valid Org syntax (i.e. not an
2033 ;; incomplete keyword).
2034 (beginning-of-line)
2035 (while (not
2037 ;; There's no ambiguity for other symbols or
2038 ;; empty lines: stop here.
2039 (looking-at "[ \t]*\\(?:[^:#]\\|$\\)")
2040 ;; Stop at valid fixed-width areas.
2041 (looking-at "[ \t]*:\\(?: \\|$\\)")
2042 ;; Stop at drawers.
2043 (and (looking-at org-drawer-regexp)
2044 (save-excursion
2045 (re-search-forward
2046 "^[ \t]*:END:[ \t]*$" limit t)))
2047 ;; Stop at valid comments.
2048 (looking-at "[ \t]*#\\(?: \\|$\\)")
2049 ;; Stop at valid dynamic blocks.
2050 (and (looking-at org-dblock-start-re)
2051 (save-excursion
2052 (re-search-forward
2053 "^[ \t]*#\\+END:?[ \t]*$" limit t)))
2054 ;; Stop at valid blocks.
2055 (and (looking-at
2056 "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
2057 (save-excursion
2058 (re-search-forward
2059 (format "^[ \t]*#\\+END_%s[ \t]*$"
2060 (match-string 1))
2061 limit t)))
2062 ;; Stop at valid latex environments.
2063 (and (looking-at
2064 "^[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}[ \t]*$")
2065 (save-excursion
2066 (re-search-forward
2067 (format "^[ \t]*\\\\end{%s}[ \t]*$"
2068 (match-string 1))
2069 limit t)))
2070 ;; Stop at valid keywords.
2071 (looking-at "[ \t]*#\\+\\S-+:")
2072 ;; Skip everything else.
2073 (not
2074 (progn
2075 (end-of-line)
2076 (re-search-forward org-element-paragraph-separate
2077 limit 'm)))))
2078 (beginning-of-line)))
2079 (if (= (point) limit) limit
2080 (goto-char (line-beginning-position)))))
2081 (contents-end (progn (skip-chars-backward " \r\t\n" contents-begin)
2082 (forward-line)
2083 (point)))
2084 (end (progn (skip-chars-forward " \r\t\n" limit)
2085 (skip-chars-backward " \t")
2086 (if (bolp) (point) (line-end-position)))))
2087 (list 'paragraph
2088 (nconc
2089 (list :begin begin
2090 :end end
2091 :contents-begin contents-begin
2092 :contents-end contents-end
2093 :post-blank (count-lines before-blank end)
2094 :post-affiliated contents-begin)
2095 (cdr affiliated))))))
2097 (defun org-element-paragraph-interpreter (paragraph contents)
2098 "Interpret PARAGRAPH element as Org syntax.
2099 CONTENTS is the contents of the element."
2100 contents)
2103 ;;;; Planning
2105 (defun org-element-planning-parser (limit)
2106 "Parse a planning.
2108 LIMIT bounds the search.
2110 Return a list whose CAR is `planning' and CDR is a plist
2111 containing `:closed', `:deadline', `:scheduled', `:begin', `:end'
2112 and `:post-blank' keywords."
2113 (save-excursion
2114 (let* ((case-fold-search nil)
2115 (begin (point))
2116 (post-blank (let ((before-blank (progn (forward-line) (point))))
2117 (skip-chars-forward " \r\t\n" limit)
2118 (skip-chars-backward " \t")
2119 (unless (bolp) (end-of-line))
2120 (count-lines before-blank (point))))
2121 (end (point))
2122 closed deadline scheduled)
2123 (goto-char begin)
2124 (while (re-search-forward org-keyword-time-not-clock-regexp end t)
2125 (goto-char (match-end 1))
2126 (skip-chars-forward " \t" end)
2127 (let ((keyword (match-string 1))
2128 (time (org-element-timestamp-parser)))
2129 (cond ((equal keyword org-closed-string) (setq closed time))
2130 ((equal keyword org-deadline-string) (setq deadline time))
2131 (t (setq scheduled time)))))
2132 (list 'planning
2133 (list :closed closed
2134 :deadline deadline
2135 :scheduled scheduled
2136 :begin begin
2137 :end end
2138 :post-blank post-blank)))))
2140 (defun org-element-planning-interpreter (planning contents)
2141 "Interpret PLANNING element as Org syntax.
2142 CONTENTS is nil."
2143 (mapconcat
2144 'identity
2145 (delq nil
2146 (list (let ((deadline (org-element-property :deadline planning)))
2147 (when deadline
2148 (concat org-deadline-string " "
2149 (org-element-timestamp-interpreter deadline nil))))
2150 (let ((scheduled (org-element-property :scheduled planning)))
2151 (when scheduled
2152 (concat org-scheduled-string " "
2153 (org-element-timestamp-interpreter scheduled nil))))
2154 (let ((closed (org-element-property :closed planning)))
2155 (when closed
2156 (concat org-closed-string " "
2157 (org-element-timestamp-interpreter closed nil))))))
2158 " "))
2161 ;;;; Quote Section
2163 (defun org-element-quote-section-parser (limit)
2164 "Parse a quote section.
2166 LIMIT bounds the search.
2168 Return a list whose CAR is `quote-section' and CDR is a plist
2169 containing `:begin', `:end', `:value' and `:post-blank' keywords.
2171 Assume point is at beginning of the section."
2172 (save-excursion
2173 (let* ((begin (point))
2174 (end (progn (org-with-limited-levels (outline-next-heading))
2175 (point)))
2176 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
2177 (forward-line)
2178 (point)))
2179 (value (buffer-substring-no-properties begin pos-before-blank)))
2180 (list 'quote-section
2181 (list :begin begin
2182 :end end
2183 :value value
2184 :post-blank (count-lines pos-before-blank end))))))
2186 (defun org-element-quote-section-interpreter (quote-section contents)
2187 "Interpret QUOTE-SECTION element as Org syntax.
2188 CONTENTS is nil."
2189 (org-element-property :value quote-section))
2192 ;;;; Src Block
2194 (defun org-element-src-block-parser (limit affiliated)
2195 "Parse a src block.
2197 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2198 the buffer position at the beginning of the first affiliated
2199 keyword and CDR is a plist of affiliated keywords along with
2200 their value.
2202 Return a list whose CAR is `src-block' and CDR is a plist
2203 containing `:language', `:switches', `:parameters', `:begin',
2204 `:end', `:hiddenp', `:number-lines', `:retain-labels',
2205 `:use-labels', `:label-fmt', `:preserve-indent', `:value',
2206 `:post-blank' and `:post-affiliated' keywords.
2208 Assume point is at the beginning of the block."
2209 (let ((case-fold-search t))
2210 (if (not (save-excursion (re-search-forward "^[ \t]*#\\+END_SRC[ \t]*$"
2211 limit t)))
2212 ;; Incomplete block: parse it as a paragraph.
2213 (org-element-paragraph-parser limit affiliated)
2214 (let ((contents-end (match-beginning 0)))
2215 (save-excursion
2216 (let* ((begin (car affiliated))
2217 (post-affiliated (point))
2218 ;; Get language as a string.
2219 (language
2220 (progn
2221 (looking-at
2222 (concat "^[ \t]*#\\+BEGIN_SRC"
2223 "\\(?: +\\(\\S-+\\)\\)?"
2224 "\\(\\(?: +\\(?:-l \".*?\"\\|[-+][A-Za-z]\\)\\)+\\)?"
2225 "\\(.*\\)[ \t]*$"))
2226 (org-match-string-no-properties 1)))
2227 ;; Get switches.
2228 (switches (org-match-string-no-properties 2))
2229 ;; Get parameters.
2230 (parameters (org-match-string-no-properties 3))
2231 ;; Switches analysis
2232 (number-lines (cond ((not switches) nil)
2233 ((string-match "-n\\>" switches) 'new)
2234 ((string-match "+n\\>" switches) 'continued)))
2235 (preserve-indent (and switches (string-match "-i\\>" switches)))
2236 (label-fmt (and switches
2237 (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
2238 (match-string 1 switches)))
2239 ;; Should labels be retained in (or stripped from)
2240 ;; src blocks?
2241 (retain-labels
2242 (or (not switches)
2243 (not (string-match "-r\\>" switches))
2244 (and number-lines (string-match "-k\\>" switches))))
2245 ;; What should code-references use - labels or
2246 ;; line-numbers?
2247 (use-labels
2248 (or (not switches)
2249 (and retain-labels (not (string-match "-k\\>" switches)))))
2250 ;; Get visibility status.
2251 (hidden (progn (forward-line) (org-invisible-p2)))
2252 ;; Retrieve code.
2253 (value (org-unescape-code-in-string
2254 (buffer-substring-no-properties (point) contents-end)))
2255 (pos-before-blank (progn (goto-char contents-end)
2256 (forward-line)
2257 (point)))
2258 ;; Get position after ending blank lines.
2259 (end (progn (skip-chars-forward " \r\t\n" limit)
2260 (skip-chars-backward " \t")
2261 (if (bolp) (point) (line-end-position)))))
2262 (list 'src-block
2263 (nconc
2264 (list :language language
2265 :switches (and (org-string-nw-p switches)
2266 (org-trim switches))
2267 :parameters (and (org-string-nw-p parameters)
2268 (org-trim parameters))
2269 :begin begin
2270 :end end
2271 :number-lines number-lines
2272 :preserve-indent preserve-indent
2273 :retain-labels retain-labels
2274 :use-labels use-labels
2275 :label-fmt label-fmt
2276 :hiddenp hidden
2277 :value value
2278 :post-blank (count-lines pos-before-blank end)
2279 :post-affiliated post-affiliated)
2280 (cdr affiliated)))))))))
2282 (defun org-element-src-block-interpreter (src-block contents)
2283 "Interpret SRC-BLOCK element as Org syntax.
2284 CONTENTS is nil."
2285 (let ((lang (org-element-property :language src-block))
2286 (switches (org-element-property :switches src-block))
2287 (params (org-element-property :parameters src-block))
2288 (value (let ((val (org-element-property :value src-block)))
2289 (cond
2290 (org-src-preserve-indentation val)
2291 ((zerop org-edit-src-content-indentation)
2292 (org-remove-indentation val))
2294 (let ((ind (make-string
2295 org-edit-src-content-indentation 32)))
2296 (replace-regexp-in-string
2297 "\\(^\\)[ \t]*\\S-" ind
2298 (org-remove-indentation val) nil nil 1)))))))
2299 (concat (format "#+BEGIN_SRC%s\n"
2300 (concat (and lang (concat " " lang))
2301 (and switches (concat " " switches))
2302 (and params (concat " " params))))
2303 (org-escape-code-in-string value)
2304 "#+END_SRC")))
2307 ;;;; Table
2309 (defun org-element-table-parser (limit affiliated)
2310 "Parse a table at point.
2312 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2313 the buffer position at the beginning of the first affiliated
2314 keyword and CDR is a plist of affiliated keywords along with
2315 their value.
2317 Return a list whose CAR is `table' and CDR is a plist containing
2318 `:begin', `:end', `:tblfm', `:type', `:contents-begin',
2319 `:contents-end', `:value', `:post-blank' and `:post-affiliated'
2320 keywords.
2322 Assume point is at the beginning of the table."
2323 (save-excursion
2324 (let* ((case-fold-search t)
2325 (table-begin (point))
2326 (type (if (org-at-table.el-p) 'table.el 'org))
2327 (begin (car affiliated))
2328 (table-end
2329 (if (re-search-forward org-table-any-border-regexp limit 'm)
2330 (goto-char (match-beginning 0))
2331 (point)))
2332 (tblfm (let (acc)
2333 (while (looking-at "[ \t]*#\\+TBLFM: +\\(.*\\)[ \t]*$")
2334 (push (org-match-string-no-properties 1) acc)
2335 (forward-line))
2336 acc))
2337 (pos-before-blank (point))
2338 (end (progn (skip-chars-forward " \r\t\n" limit)
2339 (skip-chars-backward " \t")
2340 (if (bolp) (point) (line-end-position)))))
2341 (list 'table
2342 (nconc
2343 (list :begin begin
2344 :end end
2345 :type type
2346 :tblfm tblfm
2347 ;; Only `org' tables have contents. `table.el' tables
2348 ;; use a `:value' property to store raw table as
2349 ;; a string.
2350 :contents-begin (and (eq type 'org) table-begin)
2351 :contents-end (and (eq type 'org) table-end)
2352 :value (and (eq type 'table.el)
2353 (buffer-substring-no-properties
2354 table-begin table-end))
2355 :post-blank (count-lines pos-before-blank end)
2356 :post-affiliated table-begin)
2357 (cdr affiliated))))))
2359 (defun org-element-table-interpreter (table contents)
2360 "Interpret TABLE element as Org syntax.
2361 CONTENTS is nil."
2362 (if (eq (org-element-property :type table) 'table.el)
2363 (org-remove-indentation (org-element-property :value table))
2364 (concat (with-temp-buffer (insert contents)
2365 (org-table-align)
2366 (buffer-string))
2367 (mapconcat (lambda (fm) (concat "#+TBLFM: " fm))
2368 (reverse (org-element-property :tblfm table))
2369 "\n"))))
2372 ;;;; Table Row
2374 (defun org-element-table-row-parser (limit)
2375 "Parse table row at point.
2377 LIMIT bounds the search.
2379 Return a list whose CAR is `table-row' and CDR is a plist
2380 containing `:begin', `:end', `:contents-begin', `:contents-end',
2381 `:type' and `:post-blank' keywords."
2382 (save-excursion
2383 (let* ((type (if (looking-at "^[ \t]*|-") 'rule 'standard))
2384 (begin (point))
2385 ;; A table rule has no contents. In that case, ensure
2386 ;; CONTENTS-BEGIN matches CONTENTS-END.
2387 (contents-begin (and (eq type 'standard)
2388 (search-forward "|")
2389 (point)))
2390 (contents-end (and (eq type 'standard)
2391 (progn
2392 (end-of-line)
2393 (skip-chars-backward " \t")
2394 (point))))
2395 (end (progn (forward-line) (point))))
2396 (list 'table-row
2397 (list :type type
2398 :begin begin
2399 :end end
2400 :contents-begin contents-begin
2401 :contents-end contents-end
2402 :post-blank 0)))))
2404 (defun org-element-table-row-interpreter (table-row contents)
2405 "Interpret TABLE-ROW element as Org syntax.
2406 CONTENTS is the contents of the table row."
2407 (if (eq (org-element-property :type table-row) 'rule) "|-"
2408 (concat "| " contents)))
2411 ;;;; Verse Block
2413 (defun org-element-verse-block-parser (limit affiliated)
2414 "Parse a verse block.
2416 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2417 the buffer position at the beginning of the first affiliated
2418 keyword and CDR is a plist of affiliated keywords along with
2419 their value.
2421 Return a list whose CAR is `verse-block' and CDR is a plist
2422 containing `:begin', `:end', `:contents-begin', `:contents-end',
2423 `:hiddenp', `:post-blank' and `:post-affiliated' keywords.
2425 Assume point is at beginning of the block."
2426 (let ((case-fold-search t))
2427 (if (not (save-excursion
2428 (re-search-forward "^[ \t]*#\\+END_VERSE[ \t]*$" limit t)))
2429 ;; Incomplete block: parse it as a paragraph.
2430 (org-element-paragraph-parser limit affiliated)
2431 (let ((contents-end (match-beginning 0)))
2432 (save-excursion
2433 (let* ((begin (car affiliated))
2434 (post-affiliated (point))
2435 (hidden (progn (forward-line) (org-invisible-p2)))
2436 (contents-begin (point))
2437 (pos-before-blank (progn (goto-char contents-end)
2438 (forward-line)
2439 (point)))
2440 (end (progn (skip-chars-forward " \r\t\n" limit)
2441 (skip-chars-backward " \t")
2442 (if (bolp) (point) (line-end-position)))))
2443 (list 'verse-block
2444 (nconc
2445 (list :begin begin
2446 :end end
2447 :contents-begin contents-begin
2448 :contents-end contents-end
2449 :hiddenp hidden
2450 :post-blank (count-lines pos-before-blank end)
2451 :post-affiliated post-affiliated)
2452 (cdr affiliated)))))))))
2454 (defun org-element-verse-block-interpreter (verse-block contents)
2455 "Interpret VERSE-BLOCK element as Org syntax.
2456 CONTENTS is verse block contents."
2457 (format "#+BEGIN_VERSE\n%s#+END_VERSE" contents))
2461 ;;; Objects
2463 ;; Unlike to elements, interstices can be found between objects.
2464 ;; That's why, along with the parser, successor functions are provided
2465 ;; for each object. Some objects share the same successor (i.e. `code'
2466 ;; and `verbatim' objects).
2468 ;; A successor must accept a single argument bounding the search. It
2469 ;; will return either a cons cell whose CAR is the object's type, as
2470 ;; a symbol, and CDR the position of its next occurrence, or nil.
2472 ;; Successors follow the naming convention:
2473 ;; org-element-NAME-successor, where NAME is the name of the
2474 ;; successor, as defined in `org-element-all-successors'.
2476 ;; Some object types (i.e. `italic') are recursive. Restrictions on
2477 ;; object types they can contain will be specified in
2478 ;; `org-element-object-restrictions'.
2480 ;; Adding a new type of object is simple. Implement a successor,
2481 ;; a parser, and an interpreter for it, all following the naming
2482 ;; convention. Register type in `org-element-all-objects' and
2483 ;; successor in `org-element-all-successors'. Maybe tweak
2484 ;; restrictions about it, and that's it.
2487 ;;;; Bold
2489 (defun org-element-bold-parser ()
2490 "Parse bold object at point.
2492 Return a list whose CAR is `bold' and CDR is a plist with
2493 `:begin', `:end', `:contents-begin' and `:contents-end' and
2494 `:post-blank' keywords.
2496 Assume point is at the first star marker."
2497 (save-excursion
2498 (unless (bolp) (backward-char 1))
2499 (looking-at org-emph-re)
2500 (let ((begin (match-beginning 2))
2501 (contents-begin (match-beginning 4))
2502 (contents-end (match-end 4))
2503 (post-blank (progn (goto-char (match-end 2))
2504 (skip-chars-forward " \t")))
2505 (end (point)))
2506 (list 'bold
2507 (list :begin begin
2508 :end end
2509 :contents-begin contents-begin
2510 :contents-end contents-end
2511 :post-blank post-blank)))))
2513 (defun org-element-bold-interpreter (bold contents)
2514 "Interpret BOLD object as Org syntax.
2515 CONTENTS is the contents of the object."
2516 (format "*%s*" contents))
2518 (defun org-element-text-markup-successor (limit)
2519 "Search for the next text-markup object.
2521 LIMIT bounds the search.
2523 Return value is a cons cell whose CAR is a symbol among `bold',
2524 `italic', `underline', `strike-through', `code' and `verbatim'
2525 and CDR is beginning position."
2526 (save-excursion
2527 (unless (bolp) (backward-char))
2528 (when (re-search-forward org-emph-re limit t)
2529 (let ((marker (match-string 3)))
2530 (cons (cond
2531 ((equal marker "*") 'bold)
2532 ((equal marker "/") 'italic)
2533 ((equal marker "_") 'underline)
2534 ((equal marker "+") 'strike-through)
2535 ((equal marker "~") 'code)
2536 ((equal marker "=") 'verbatim)
2537 (t (error "Unknown marker at %d" (match-beginning 3))))
2538 (match-beginning 2))))))
2541 ;;;; Code
2543 (defun org-element-code-parser ()
2544 "Parse code object at point.
2546 Return a list whose CAR is `code' and CDR is a plist with
2547 `:value', `:begin', `:end' and `:post-blank' keywords.
2549 Assume point is at the first tilde marker."
2550 (save-excursion
2551 (unless (bolp) (backward-char 1))
2552 (looking-at org-emph-re)
2553 (let ((begin (match-beginning 2))
2554 (value (org-match-string-no-properties 4))
2555 (post-blank (progn (goto-char (match-end 2))
2556 (skip-chars-forward " \t")))
2557 (end (point)))
2558 (list 'code
2559 (list :value value
2560 :begin begin
2561 :end end
2562 :post-blank post-blank)))))
2564 (defun org-element-code-interpreter (code contents)
2565 "Interpret CODE object as Org syntax.
2566 CONTENTS is nil."
2567 (format "~%s~" (org-element-property :value code)))
2570 ;;;; Entity
2572 (defun org-element-entity-parser ()
2573 "Parse entity at point.
2575 Return a list whose CAR is `entity' and CDR a plist with
2576 `:begin', `:end', `:latex', `:latex-math-p', `:html', `:latin1',
2577 `:utf-8', `:ascii', `:use-brackets-p' and `:post-blank' as
2578 keywords.
2580 Assume point is at the beginning of the entity."
2581 (save-excursion
2582 (looking-at "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)")
2583 (let* ((value (org-entity-get (match-string 1)))
2584 (begin (match-beginning 0))
2585 (bracketsp (string= (match-string 2) "{}"))
2586 (post-blank (progn (goto-char (match-end 1))
2587 (when bracketsp (forward-char 2))
2588 (skip-chars-forward " \t")))
2589 (end (point)))
2590 (list 'entity
2591 (list :name (car value)
2592 :latex (nth 1 value)
2593 :latex-math-p (nth 2 value)
2594 :html (nth 3 value)
2595 :ascii (nth 4 value)
2596 :latin1 (nth 5 value)
2597 :utf-8 (nth 6 value)
2598 :begin begin
2599 :end end
2600 :use-brackets-p bracketsp
2601 :post-blank post-blank)))))
2603 (defun org-element-entity-interpreter (entity contents)
2604 "Interpret ENTITY object as Org syntax.
2605 CONTENTS is nil."
2606 (concat "\\"
2607 (org-element-property :name entity)
2608 (when (org-element-property :use-brackets-p entity) "{}")))
2610 (defun org-element-latex-or-entity-successor (limit)
2611 "Search for the next latex-fragment or entity object.
2613 LIMIT bounds the search.
2615 Return value is a cons cell whose CAR is `entity' or
2616 `latex-fragment' and CDR is beginning position."
2617 (save-excursion
2618 (unless (bolp) (backward-char))
2619 (let ((matchers
2620 (remove "begin" (plist-get org-format-latex-options :matchers)))
2621 ;; ENTITY-RE matches both LaTeX commands and Org entities.
2622 (entity-re
2623 "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))
2624 (when (re-search-forward
2625 (concat (mapconcat (lambda (e) (nth 1 (assoc e org-latex-regexps)))
2626 matchers "\\|")
2627 "\\|" entity-re)
2628 limit t)
2629 (goto-char (match-beginning 0))
2630 (if (looking-at entity-re)
2631 ;; Determine if it's a real entity or a LaTeX command.
2632 (cons (if (org-entity-get (match-string 1)) 'entity 'latex-fragment)
2633 (match-beginning 0))
2634 ;; No entity nor command: point is at a LaTeX fragment.
2635 ;; Determine its type to get the correct beginning position.
2636 (cons 'latex-fragment
2637 (catch 'return
2638 (mapc (lambda (e)
2639 (when (looking-at (nth 1 (assoc e org-latex-regexps)))
2640 (throw 'return
2641 (match-beginning
2642 (nth 2 (assoc e org-latex-regexps))))))
2643 matchers)
2644 (point))))))))
2647 ;;;; Export Snippet
2649 (defun org-element-export-snippet-parser ()
2650 "Parse export snippet at point.
2652 Return a list whose CAR is `export-snippet' and CDR a plist with
2653 `:begin', `:end', `:back-end', `:value' and `:post-blank' as
2654 keywords.
2656 Assume point is at the beginning of the snippet."
2657 (save-excursion
2658 (re-search-forward "@@\\([-A-Za-z0-9]+\\):" nil t)
2659 (let* ((begin (match-beginning 0))
2660 (back-end (org-match-string-no-properties 1))
2661 (value (buffer-substring-no-properties
2662 (point)
2663 (progn (re-search-forward "@@" nil t) (match-beginning 0))))
2664 (post-blank (skip-chars-forward " \t"))
2665 (end (point)))
2666 (list 'export-snippet
2667 (list :back-end back-end
2668 :value value
2669 :begin begin
2670 :end end
2671 :post-blank post-blank)))))
2673 (defun org-element-export-snippet-interpreter (export-snippet contents)
2674 "Interpret EXPORT-SNIPPET object as Org syntax.
2675 CONTENTS is nil."
2676 (format "@@%s:%s@@"
2677 (org-element-property :back-end export-snippet)
2678 (org-element-property :value export-snippet)))
2680 (defun org-element-export-snippet-successor (limit)
2681 "Search for the next export-snippet object.
2683 LIMIT bounds the search.
2685 Return value is a cons cell whose CAR is `export-snippet' and CDR
2686 its beginning position."
2687 (save-excursion
2688 (let (beg)
2689 (when (and (re-search-forward "@@[-A-Za-z0-9]+:" limit t)
2690 (setq beg (match-beginning 0))
2691 (search-forward "@@" limit t))
2692 (cons 'export-snippet beg)))))
2695 ;;;; Footnote Reference
2697 (defun org-element-footnote-reference-parser ()
2698 "Parse footnote reference at point.
2700 Return a list whose CAR is `footnote-reference' and CDR a plist
2701 with `:label', `:type', `:inline-definition', `:begin', `:end'
2702 and `:post-blank' as keywords."
2703 (save-excursion
2704 (looking-at org-footnote-re)
2705 (let* ((begin (point))
2706 (label (or (org-match-string-no-properties 2)
2707 (org-match-string-no-properties 3)
2708 (and (match-string 1)
2709 (concat "fn:" (org-match-string-no-properties 1)))))
2710 (type (if (or (not label) (match-string 1)) 'inline 'standard))
2711 (inner-begin (match-end 0))
2712 (inner-end
2713 (let ((count 1))
2714 (forward-char)
2715 (while (and (> count 0) (re-search-forward "[][]" nil t))
2716 (if (equal (match-string 0) "[") (incf count) (decf count)))
2717 (1- (point))))
2718 (post-blank (progn (goto-char (1+ inner-end))
2719 (skip-chars-forward " \t")))
2720 (end (point))
2721 (footnote-reference
2722 (list 'footnote-reference
2723 (list :label label
2724 :type type
2725 :begin begin
2726 :end end
2727 :post-blank post-blank))))
2728 (org-element-put-property
2729 footnote-reference :inline-definition
2730 (and (eq type 'inline)
2731 (org-element-parse-secondary-string
2732 (buffer-substring inner-begin inner-end)
2733 (org-element-restriction 'footnote-reference)
2734 footnote-reference))))))
2736 (defun org-element-footnote-reference-interpreter (footnote-reference contents)
2737 "Interpret FOOTNOTE-REFERENCE object as Org syntax.
2738 CONTENTS is nil."
2739 (let ((label (or (org-element-property :label footnote-reference) "fn:"))
2740 (def
2741 (let ((inline-def
2742 (org-element-property :inline-definition footnote-reference)))
2743 (if (not inline-def) ""
2744 (concat ":" (org-element-interpret-data inline-def))))))
2745 (format "[%s]" (concat label def))))
2747 (defun org-element-footnote-reference-successor (limit)
2748 "Search for the next footnote-reference object.
2750 LIMIT bounds the search.
2752 Return value is a cons cell whose CAR is `footnote-reference' and
2753 CDR is beginning position."
2754 (save-excursion
2755 (catch 'exit
2756 (while (re-search-forward org-footnote-re limit t)
2757 (save-excursion
2758 (let ((beg (match-beginning 0))
2759 (count 1))
2760 (backward-char)
2761 (while (re-search-forward "[][]" limit t)
2762 (if (equal (match-string 0) "[") (incf count) (decf count))
2763 (when (zerop count)
2764 (throw 'exit (cons 'footnote-reference beg))))))))))
2767 ;;;; Inline Babel Call
2769 (defun org-element-inline-babel-call-parser ()
2770 "Parse inline babel call at point.
2772 Return a list whose CAR is `inline-babel-call' and CDR a plist
2773 with `:begin', `:end', `:info' and `:post-blank' as keywords.
2775 Assume point is at the beginning of the babel call."
2776 (save-excursion
2777 (unless (bolp) (backward-char))
2778 (looking-at org-babel-inline-lob-one-liner-regexp)
2779 (let ((info (save-match-data (org-babel-lob-get-info)))
2780 (begin (match-end 1))
2781 (post-blank (progn (goto-char (match-end 0))
2782 (skip-chars-forward " \t")))
2783 (end (point)))
2784 (list 'inline-babel-call
2785 (list :begin begin
2786 :end end
2787 :info info
2788 :post-blank post-blank)))))
2790 (defun org-element-inline-babel-call-interpreter (inline-babel-call contents)
2791 "Interpret INLINE-BABEL-CALL object as Org syntax.
2792 CONTENTS is nil."
2793 (let* ((babel-info (org-element-property :info inline-babel-call))
2794 (main-source (car babel-info))
2795 (post-options (nth 1 babel-info)))
2796 (concat "call_"
2797 (if (string-match "\\[\\(\\[.*?\\]\\)\\]" main-source)
2798 ;; Remove redundant square brackets.
2799 (replace-match
2800 (match-string 1 main-source) nil nil main-source)
2801 main-source)
2802 (and post-options (format "[%s]" post-options)))))
2804 (defun org-element-inline-babel-call-successor (limit)
2805 "Search for the next inline-babel-call object.
2807 LIMIT bounds the search.
2809 Return value is a cons cell whose CAR is `inline-babel-call' and
2810 CDR is beginning position."
2811 (save-excursion
2812 ;; Use a simplified version of
2813 ;; `org-babel-inline-lob-one-liner-regexp'.
2814 (when (re-search-forward
2815 "call_\\([^()\n]+?\\)\\(?:\\[.*?\\]\\)?([^\n]*?)\\(\\[.*?\\]\\)?"
2816 limit t)
2817 (cons 'inline-babel-call (match-beginning 0)))))
2820 ;;;; Inline Src Block
2822 (defun org-element-inline-src-block-parser ()
2823 "Parse inline source block at point.
2825 LIMIT bounds the search.
2827 Return a list whose CAR is `inline-src-block' and CDR a plist
2828 with `:begin', `:end', `:language', `:value', `:parameters' and
2829 `:post-blank' as keywords.
2831 Assume point is at the beginning of the inline src block."
2832 (save-excursion
2833 (unless (bolp) (backward-char))
2834 (looking-at org-babel-inline-src-block-regexp)
2835 (let ((begin (match-beginning 1))
2836 (language (org-match-string-no-properties 2))
2837 (parameters (org-match-string-no-properties 4))
2838 (value (org-match-string-no-properties 5))
2839 (post-blank (progn (goto-char (match-end 0))
2840 (skip-chars-forward " \t")))
2841 (end (point)))
2842 (list 'inline-src-block
2843 (list :language language
2844 :value value
2845 :parameters parameters
2846 :begin begin
2847 :end end
2848 :post-blank post-blank)))))
2850 (defun org-element-inline-src-block-interpreter (inline-src-block contents)
2851 "Interpret INLINE-SRC-BLOCK object as Org syntax.
2852 CONTENTS is nil."
2853 (let ((language (org-element-property :language inline-src-block))
2854 (arguments (org-element-property :parameters inline-src-block))
2855 (body (org-element-property :value inline-src-block)))
2856 (format "src_%s%s{%s}"
2857 language
2858 (if arguments (format "[%s]" arguments) "")
2859 body)))
2861 (defun org-element-inline-src-block-successor (limit)
2862 "Search for the next inline-babel-call element.
2864 LIMIT bounds the search.
2866 Return value is a cons cell whose CAR is `inline-babel-call' and
2867 CDR is beginning position."
2868 (save-excursion
2869 (unless (bolp) (backward-char))
2870 (when (re-search-forward org-babel-inline-src-block-regexp limit t)
2871 (cons 'inline-src-block (match-beginning 1)))))
2873 ;;;; Italic
2875 (defun org-element-italic-parser ()
2876 "Parse italic object at point.
2878 Return a list whose CAR is `italic' and CDR is a plist with
2879 `:begin', `:end', `:contents-begin' and `:contents-end' and
2880 `:post-blank' keywords.
2882 Assume point is at the first slash marker."
2883 (save-excursion
2884 (unless (bolp) (backward-char 1))
2885 (looking-at org-emph-re)
2886 (let ((begin (match-beginning 2))
2887 (contents-begin (match-beginning 4))
2888 (contents-end (match-end 4))
2889 (post-blank (progn (goto-char (match-end 2))
2890 (skip-chars-forward " \t")))
2891 (end (point)))
2892 (list 'italic
2893 (list :begin begin
2894 :end end
2895 :contents-begin contents-begin
2896 :contents-end contents-end
2897 :post-blank post-blank)))))
2899 (defun org-element-italic-interpreter (italic contents)
2900 "Interpret ITALIC object as Org syntax.
2901 CONTENTS is the contents of the object."
2902 (format "/%s/" contents))
2905 ;;;; Latex Fragment
2907 (defun org-element-latex-fragment-parser ()
2908 "Parse latex fragment at point.
2910 Return a list whose CAR is `latex-fragment' and CDR a plist with
2911 `:value', `:begin', `:end', and `:post-blank' as keywords.
2913 Assume point is at the beginning of the latex fragment."
2914 (save-excursion
2915 (let* ((begin (point))
2916 (substring-match
2917 (catch 'exit
2918 (mapc (lambda (e)
2919 (let ((latex-regexp (nth 1 (assoc e org-latex-regexps))))
2920 (when (or (looking-at latex-regexp)
2921 (and (not (bobp))
2922 (save-excursion
2923 (backward-char)
2924 (looking-at latex-regexp))))
2925 (throw 'exit (nth 2 (assoc e org-latex-regexps))))))
2926 (plist-get org-format-latex-options :matchers))
2927 ;; None found: it's a macro.
2928 (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")
2930 (value (match-string-no-properties substring-match))
2931 (post-blank (progn (goto-char (match-end substring-match))
2932 (skip-chars-forward " \t")))
2933 (end (point)))
2934 (list 'latex-fragment
2935 (list :value value
2936 :begin begin
2937 :end end
2938 :post-blank post-blank)))))
2940 (defun org-element-latex-fragment-interpreter (latex-fragment contents)
2941 "Interpret LATEX-FRAGMENT object as Org syntax.
2942 CONTENTS is nil."
2943 (org-element-property :value latex-fragment))
2945 ;;;; Line Break
2947 (defun org-element-line-break-parser ()
2948 "Parse line break at point.
2950 Return a list whose CAR is `line-break', and CDR a plist with
2951 `:begin', `:end' and `:post-blank' keywords.
2953 Assume point is at the beginning of the line break."
2954 (list 'line-break
2955 (list :begin (point)
2956 :end (progn (forward-line) (point))
2957 :post-blank 0)))
2959 (defun org-element-line-break-interpreter (line-break contents)
2960 "Interpret LINE-BREAK object as Org syntax.
2961 CONTENTS is nil."
2962 "\\\\\n")
2964 (defun org-element-line-break-successor (limit)
2965 "Search for the next line-break object.
2967 LIMIT bounds the search.
2969 Return value is a cons cell whose CAR is `line-break' and CDR is
2970 beginning position."
2971 (save-excursion
2972 (let ((beg (and (re-search-forward "[^\\\\]\\(\\\\\\\\\\)[ \t]*$" limit t)
2973 (goto-char (match-beginning 1)))))
2974 ;; A line break can only happen on a non-empty line.
2975 (when (and beg (re-search-backward "\\S-" (point-at-bol) t))
2976 (cons 'line-break beg)))))
2979 ;;;; Link
2981 (defun org-element-link-parser ()
2982 "Parse link at point.
2984 Return a list whose CAR is `link' and CDR a plist with `:type',
2985 `:path', `:raw-link', `:application', `:search-option', `:begin',
2986 `:end', `:contents-begin', `:contents-end' and `:post-blank' as
2987 keywords.
2989 Assume point is at the beginning of the link."
2990 (save-excursion
2991 (let ((begin (point))
2992 end contents-begin contents-end link-end post-blank path type
2993 raw-link link search-option application)
2994 (cond
2995 ;; Type 1: Text targeted from a radio target.
2996 ((and org-target-link-regexp (looking-at org-target-link-regexp))
2997 (setq type "radio"
2998 link-end (match-end 0)
2999 path (org-match-string-no-properties 0)))
3000 ;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]]
3001 ((looking-at org-bracket-link-regexp)
3002 (setq contents-begin (match-beginning 3)
3003 contents-end (match-end 3)
3004 link-end (match-end 0)
3005 ;; RAW-LINK is the original link.
3006 raw-link (org-translate-link
3007 (org-link-expand-abbrev
3008 (org-match-string-no-properties 1)))
3009 link (org-link-unescape raw-link))
3010 ;; Determine TYPE of link and set PATH accordingly.
3011 (cond
3012 ;; File type.
3013 ((or (file-name-absolute-p link) (string-match "^\\.\\.?/" link))
3014 (setq type "file" path link))
3015 ;; Explicit type (http, irc, bbdb...). See `org-link-types'.
3016 ((string-match org-link-re-with-space3 link)
3017 (setq type (match-string 1 link) path (match-string 2 link)))
3018 ;; Id type: PATH is the id.
3019 ((string-match "^id:\\([-a-f0-9]+\\)" link)
3020 (setq type "id" path (match-string 1 link)))
3021 ;; Code-ref type: PATH is the name of the reference.
3022 ((string-match "^(\\(.*\\))$" link)
3023 (setq type "coderef" path (match-string 1 link)))
3024 ;; Custom-id type: PATH is the name of the custom id.
3025 ((= (aref link 0) ?#)
3026 (setq type "custom-id" path (substring link 1)))
3027 ;; Fuzzy type: Internal link either matches a target, an
3028 ;; headline name or nothing. PATH is the target or
3029 ;; headline's name.
3030 (t (setq type "fuzzy" path link))))
3031 ;; Type 3: Plain link, i.e. http://orgmode.org
3032 ((looking-at org-plain-link-re)
3033 (setq raw-link (org-match-string-no-properties 0)
3034 type (org-match-string-no-properties 1)
3035 path (org-match-string-no-properties 2)
3036 link-end (match-end 0)))
3037 ;; Type 4: Angular link, i.e. <http://orgmode.org>
3038 ((looking-at org-angle-link-re)
3039 (setq raw-link (buffer-substring-no-properties
3040 (match-beginning 1) (match-end 2))
3041 type (org-match-string-no-properties 1)
3042 path (org-match-string-no-properties 2)
3043 link-end (match-end 0))))
3044 ;; In any case, deduce end point after trailing white space from
3045 ;; LINK-END variable.
3046 (setq post-blank (progn (goto-char link-end) (skip-chars-forward " \t"))
3047 end (point))
3048 ;; Extract search option and opening application out of
3049 ;; "file"-type links.
3050 (when (member type org-element-link-type-is-file)
3051 ;; Application.
3052 (cond ((string-match "^file\\+\\(.*\\)$" type)
3053 (setq application (match-string 1 type)))
3054 ((not (string-match "^file" type))
3055 (setq application type)))
3056 ;; Extract search option from PATH.
3057 (when (string-match "::\\(.*\\)$" path)
3058 (setq search-option (match-string 1 path)
3059 path (replace-match "" nil nil path)))
3060 ;; Make sure TYPE always report "file".
3061 (setq type "file"))
3062 (list 'link
3063 (list :type type
3064 :path path
3065 :raw-link (or raw-link path)
3066 :application application
3067 :search-option search-option
3068 :begin begin
3069 :end end
3070 :contents-begin contents-begin
3071 :contents-end contents-end
3072 :post-blank post-blank)))))
3074 (defun org-element-link-interpreter (link contents)
3075 "Interpret LINK object as Org syntax.
3076 CONTENTS is the contents of the object, or nil."
3077 (let ((type (org-element-property :type link))
3078 (raw-link (org-element-property :raw-link link)))
3079 (if (string= type "radio") raw-link
3080 (format "[[%s]%s]"
3081 raw-link
3082 (if contents (format "[%s]" contents) "")))))
3084 (defun org-element-link-successor (limit)
3085 "Search for the next link object.
3087 LIMIT bounds the search.
3089 Return value is a cons cell whose CAR is `link' and CDR is
3090 beginning position."
3091 (save-excursion
3092 (let ((link-regexp
3093 (if (not org-target-link-regexp) org-any-link-re
3094 (concat org-any-link-re "\\|" org-target-link-regexp))))
3095 (when (re-search-forward link-regexp limit t)
3096 (cons 'link (match-beginning 0))))))
3099 ;;;; Macro
3101 (defun org-element-macro-parser ()
3102 "Parse macro at point.
3104 Return a list whose CAR is `macro' and CDR a plist with `:key',
3105 `:args', `:begin', `:end', `:value' and `:post-blank' as
3106 keywords.
3108 Assume point is at the macro."
3109 (save-excursion
3110 (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
3111 (let ((begin (point))
3112 (key (downcase (org-match-string-no-properties 1)))
3113 (value (org-match-string-no-properties 0))
3114 (post-blank (progn (goto-char (match-end 0))
3115 (skip-chars-forward " \t")))
3116 (end (point))
3117 (args (let ((args (org-match-string-no-properties 3)) args2)
3118 (when args
3119 ;; Do not use `org-split-string' since empty
3120 ;; strings are meaningful here.
3121 (setq args (split-string args ","))
3122 (while args
3123 (while (string-match "\\\\\\'" (car args))
3124 ;; Repair bad splits, when comma is protected,
3125 ;; and thus not a real separator.
3126 (setcar (cdr args) (concat (substring (car args) 0 -1)
3127 "," (nth 1 args)))
3128 (pop args))
3129 (push (pop args) args2))
3130 (mapcar 'org-trim (nreverse args2))))))
3131 (list 'macro
3132 (list :key key
3133 :value value
3134 :args args
3135 :begin begin
3136 :end end
3137 :post-blank post-blank)))))
3139 (defun org-element-macro-interpreter (macro contents)
3140 "Interpret MACRO object as Org syntax.
3141 CONTENTS is nil."
3142 (org-element-property :value macro))
3144 (defun org-element-macro-successor (limit)
3145 "Search for the next macro object.
3147 LIMIT bounds the search.
3149 Return value is cons cell whose CAR is `macro' and CDR is
3150 beginning position."
3151 (save-excursion
3152 (when (re-search-forward
3153 "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
3154 limit t)
3155 (cons 'macro (match-beginning 0)))))
3158 ;;;; Radio-target
3160 (defun org-element-radio-target-parser ()
3161 "Parse radio target at point.
3163 Return a list whose CAR is `radio-target' and CDR a plist with
3164 `:begin', `:end', `:contents-begin', `:contents-end', `:value'
3165 and `:post-blank' as keywords.
3167 Assume point is at the radio target."
3168 (save-excursion
3169 (looking-at org-radio-target-regexp)
3170 (let ((begin (point))
3171 (contents-begin (match-beginning 1))
3172 (contents-end (match-end 1))
3173 (value (org-match-string-no-properties 1))
3174 (post-blank (progn (goto-char (match-end 0))
3175 (skip-chars-forward " \t")))
3176 (end (point)))
3177 (list 'radio-target
3178 (list :begin begin
3179 :end end
3180 :contents-begin contents-begin
3181 :contents-end contents-end
3182 :post-blank post-blank
3183 :value value)))))
3185 (defun org-element-radio-target-interpreter (target contents)
3186 "Interpret TARGET object as Org syntax.
3187 CONTENTS is the contents of the object."
3188 (concat "<<<" contents ">>>"))
3190 (defun org-element-radio-target-successor (limit)
3191 "Search for the next radio-target object.
3193 LIMIT bounds the search.
3195 Return value is a cons cell whose CAR is `radio-target' and CDR
3196 is beginning position."
3197 (save-excursion
3198 (when (re-search-forward org-radio-target-regexp limit t)
3199 (cons 'radio-target (match-beginning 0)))))
3202 ;;;; Statistics Cookie
3204 (defun org-element-statistics-cookie-parser ()
3205 "Parse statistics cookie at point.
3207 Return a list whose CAR is `statistics-cookie', and CDR a plist
3208 with `:begin', `:end', `:value' and `:post-blank' keywords.
3210 Assume point is at the beginning of the statistics-cookie."
3211 (save-excursion
3212 (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
3213 (let* ((begin (point))
3214 (value (buffer-substring-no-properties
3215 (match-beginning 0) (match-end 0)))
3216 (post-blank (progn (goto-char (match-end 0))
3217 (skip-chars-forward " \t")))
3218 (end (point)))
3219 (list 'statistics-cookie
3220 (list :begin begin
3221 :end end
3222 :value value
3223 :post-blank post-blank)))))
3225 (defun org-element-statistics-cookie-interpreter (statistics-cookie contents)
3226 "Interpret STATISTICS-COOKIE object as Org syntax.
3227 CONTENTS is nil."
3228 (org-element-property :value statistics-cookie))
3230 (defun org-element-statistics-cookie-successor (limit)
3231 "Search for the next statistics cookie object.
3233 LIMIT bounds the search.
3235 Return value is a cons cell whose CAR is `statistics-cookie' and
3236 CDR is beginning position."
3237 (save-excursion
3238 (when (re-search-forward "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]" limit t)
3239 (cons 'statistics-cookie (match-beginning 0)))))
3242 ;;;; Strike-Through
3244 (defun org-element-strike-through-parser ()
3245 "Parse strike-through object at point.
3247 Return a list whose CAR is `strike-through' and CDR is a plist
3248 with `:begin', `:end', `:contents-begin' and `:contents-end' and
3249 `:post-blank' keywords.
3251 Assume point is at the first plus sign marker."
3252 (save-excursion
3253 (unless (bolp) (backward-char 1))
3254 (looking-at org-emph-re)
3255 (let ((begin (match-beginning 2))
3256 (contents-begin (match-beginning 4))
3257 (contents-end (match-end 4))
3258 (post-blank (progn (goto-char (match-end 2))
3259 (skip-chars-forward " \t")))
3260 (end (point)))
3261 (list 'strike-through
3262 (list :begin begin
3263 :end end
3264 :contents-begin contents-begin
3265 :contents-end contents-end
3266 :post-blank post-blank)))))
3268 (defun org-element-strike-through-interpreter (strike-through contents)
3269 "Interpret STRIKE-THROUGH object as Org syntax.
3270 CONTENTS is the contents of the object."
3271 (format "+%s+" contents))
3274 ;;;; Subscript
3276 (defun org-element-subscript-parser ()
3277 "Parse subscript at point.
3279 Return a list whose CAR is `subscript' and CDR a plist with
3280 `:begin', `:end', `:contents-begin', `:contents-end',
3281 `:use-brackets-p' and `:post-blank' as keywords.
3283 Assume point is at the underscore."
3284 (save-excursion
3285 (unless (bolp) (backward-char))
3286 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp)
3288 (not (looking-at org-match-substring-regexp))))
3289 (begin (match-beginning 2))
3290 (contents-begin (or (match-beginning 5)
3291 (match-beginning 3)))
3292 (contents-end (or (match-end 5) (match-end 3)))
3293 (post-blank (progn (goto-char (match-end 0))
3294 (skip-chars-forward " \t")))
3295 (end (point)))
3296 (list 'subscript
3297 (list :begin begin
3298 :end end
3299 :use-brackets-p bracketsp
3300 :contents-begin contents-begin
3301 :contents-end contents-end
3302 :post-blank post-blank)))))
3304 (defun org-element-subscript-interpreter (subscript contents)
3305 "Interpret SUBSCRIPT object as Org syntax.
3306 CONTENTS is the contents of the object."
3307 (format
3308 (if (org-element-property :use-brackets-p subscript) "_{%s}" "_%s")
3309 contents))
3311 (defun org-element-sub/superscript-successor (limit)
3312 "Search for the next sub/superscript object.
3314 LIMIT bounds the search.
3316 Return value is a cons cell whose CAR is either `subscript' or
3317 `superscript' and CDR is beginning position."
3318 (save-excursion
3319 (unless (bolp) (backward-char))
3320 (when (re-search-forward org-match-substring-regexp limit t)
3321 (cons (if (string= (match-string 2) "_") 'subscript 'superscript)
3322 (match-beginning 2)))))
3325 ;;;; Superscript
3327 (defun org-element-superscript-parser ()
3328 "Parse superscript at point.
3330 Return a list whose CAR is `superscript' and CDR a plist with
3331 `:begin', `:end', `:contents-begin', `:contents-end',
3332 `:use-brackets-p' and `:post-blank' as keywords.
3334 Assume point is at the caret."
3335 (save-excursion
3336 (unless (bolp) (backward-char))
3337 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp) t
3338 (not (looking-at org-match-substring-regexp))))
3339 (begin (match-beginning 2))
3340 (contents-begin (or (match-beginning 5)
3341 (match-beginning 3)))
3342 (contents-end (or (match-end 5) (match-end 3)))
3343 (post-blank (progn (goto-char (match-end 0))
3344 (skip-chars-forward " \t")))
3345 (end (point)))
3346 (list 'superscript
3347 (list :begin begin
3348 :end end
3349 :use-brackets-p bracketsp
3350 :contents-begin contents-begin
3351 :contents-end contents-end
3352 :post-blank post-blank)))))
3354 (defun org-element-superscript-interpreter (superscript contents)
3355 "Interpret SUPERSCRIPT object as Org syntax.
3356 CONTENTS is the contents of the object."
3357 (format
3358 (if (org-element-property :use-brackets-p superscript) "^{%s}" "^%s")
3359 contents))
3362 ;;;; Table Cell
3364 (defun org-element-table-cell-parser ()
3365 "Parse table cell at point.
3367 Return a list whose CAR is `table-cell' and CDR is a plist
3368 containing `:begin', `:end', `:contents-begin', `:contents-end'
3369 and `:post-blank' keywords."
3370 (looking-at "[ \t]*\\(.*?\\)[ \t]*|")
3371 (let* ((begin (match-beginning 0))
3372 (end (match-end 0))
3373 (contents-begin (match-beginning 1))
3374 (contents-end (match-end 1)))
3375 (list 'table-cell
3376 (list :begin begin
3377 :end end
3378 :contents-begin contents-begin
3379 :contents-end contents-end
3380 :post-blank 0))))
3382 (defun org-element-table-cell-interpreter (table-cell contents)
3383 "Interpret TABLE-CELL element as Org syntax.
3384 CONTENTS is the contents of the cell, or nil."
3385 (concat " " contents " |"))
3387 (defun org-element-table-cell-successor (limit)
3388 "Search for the next table-cell object.
3390 LIMIT bounds the search.
3392 Return value is a cons cell whose CAR is `table-cell' and CDR is
3393 beginning position."
3394 (when (looking-at "[ \t]*.*?[ \t]+|") (cons 'table-cell (point))))
3397 ;;;; Target
3399 (defun org-element-target-parser ()
3400 "Parse target at point.
3402 Return a list whose CAR is `target' and CDR a plist with
3403 `:begin', `:end', `:value' and `:post-blank' as keywords.
3405 Assume point is at the target."
3406 (save-excursion
3407 (looking-at org-target-regexp)
3408 (let ((begin (point))
3409 (value (org-match-string-no-properties 1))
3410 (post-blank (progn (goto-char (match-end 0))
3411 (skip-chars-forward " \t")))
3412 (end (point)))
3413 (list 'target
3414 (list :begin begin
3415 :end end
3416 :value value
3417 :post-blank post-blank)))))
3419 (defun org-element-target-interpreter (target contents)
3420 "Interpret TARGET object as Org syntax.
3421 CONTENTS is nil."
3422 (format "<<%s>>" (org-element-property :value target)))
3424 (defun org-element-target-successor (limit)
3425 "Search for the next target object.
3427 LIMIT bounds the search.
3429 Return value is a cons cell whose CAR is `target' and CDR is
3430 beginning position."
3431 (save-excursion
3432 (when (re-search-forward org-target-regexp limit t)
3433 (cons 'target (match-beginning 0)))))
3436 ;;;; Timestamp
3438 (defun org-element-timestamp-parser ()
3439 "Parse time stamp at point.
3441 Return a list whose CAR is `timestamp', and CDR a plist with
3442 `:type', `:begin', `:end', `:value' and `:post-blank' keywords.
3444 Assume point is at the beginning of the timestamp."
3445 (save-excursion
3446 (let* ((begin (point))
3447 (activep (eq (char-after) ?<))
3448 (raw-value
3449 (progn
3450 (looking-at "\\([<[]\\(%%\\)?.*?\\)[]>]\\(?:--\\([<[].*?[]>]\\)\\)?")
3451 (match-string-no-properties 0)))
3452 (date-start (match-string-no-properties 1))
3453 (date-end (match-string 3))
3454 (diaryp (match-beginning 2))
3455 (post-blank (progn (goto-char (match-end 0))
3456 (skip-chars-forward " \t")))
3457 (end (point))
3458 (time-range
3459 (and (not diaryp)
3460 (string-match
3461 "[012]?[0-9]:[0-5][0-9]\\(-\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)"
3462 date-start)
3463 (cons (string-to-number (match-string 2 date-start))
3464 (string-to-number (match-string 3 date-start)))))
3465 (type (cond (diaryp 'diary)
3466 ((and activep (or date-end time-range)) 'active-range)
3467 (activep 'active)
3468 ((or date-end time-range) 'inactive-range)
3469 (t 'inactive)))
3470 (repeater-props
3471 (and (not diaryp)
3472 (string-match "\\([.+]?\\+\\)\\([0-9]+\\)\\([hdwmy]\\)>"
3473 raw-value)
3474 (list
3475 :repeater-type
3476 (let ((type (match-string 1 raw-value)))
3477 (cond ((equal "++" type) 'catch-up)
3478 ((equal ".+" type) 'restart)
3479 (t 'cumulate)))
3480 :repeater-value (string-to-number (match-string 2 raw-value))
3481 :repeater-unit
3482 (case (string-to-char (match-string 3 raw-value))
3483 (?h 'hour) (?d 'day) (?w 'week) (?m 'month) (t 'year)))))
3484 year-start month-start day-start hour-start minute-start year-end
3485 month-end day-end hour-end minute-end)
3486 ;; Parse date-start.
3487 (unless diaryp
3488 (let ((date (org-parse-time-string date-start t)))
3489 (setq year-start (nth 5 date)
3490 month-start (nth 4 date)
3491 day-start (nth 3 date)
3492 hour-start (nth 2 date)
3493 minute-start (nth 1 date))))
3494 ;; Compute date-end. It can be provided directly in time-stamp,
3495 ;; or extracted from time range. Otherwise, it defaults to the
3496 ;; same values as date-start.
3497 (unless diaryp
3498 (let ((date (and date-end (org-parse-time-string date-end t))))
3499 (setq year-end (or (nth 5 date) year-start)
3500 month-end (or (nth 4 date) month-start)
3501 day-end (or (nth 3 date) day-start)
3502 hour-end (or (nth 2 date) (car time-range) hour-start)
3503 minute-end (or (nth 1 date) (cdr time-range) minute-start))))
3504 (list 'timestamp
3505 (nconc (list :type type
3506 :raw-value raw-value
3507 :year-start year-start
3508 :month-start month-start
3509 :day-start day-start
3510 :hour-start hour-start
3511 :minute-start minute-start
3512 :year-end year-end
3513 :month-end month-end
3514 :day-end day-end
3515 :hour-end hour-end
3516 :minute-end minute-end
3517 :begin begin
3518 :end end
3519 :post-blank post-blank)
3520 repeater-props)))))
3522 (defun org-element-timestamp-interpreter (timestamp contents)
3523 "Interpret TIMESTAMP object as Org syntax.
3524 CONTENTS is nil."
3525 ;; Use `:raw-value' if specified.
3526 (or (org-element-property :raw-value timestamp)
3527 ;; Otherwise, build timestamp string.
3528 (let* ((repeat-string
3529 (concat
3530 (case (org-element-property :repeater-type timestamp)
3531 (cumulate "+") (catch-up "++") (restart ".+"))
3532 (let ((val (org-element-property :repeater-value timestamp)))
3533 (and val (number-to-string val)))
3534 (case (org-element-property :repeater-unit timestamp)
3535 (hour "h") (day "d") (week "w") (month "m") (year "y"))))
3536 (build-ts-string
3537 ;; Build an Org timestamp string from TIME. ACTIVEP is
3538 ;; non-nil when time stamp is active. If WITH-TIME-P is
3539 ;; non-nil, add a time part. HOUR-END and MINUTE-END
3540 ;; specify a time range in the timestamp. REPEAT-STRING
3541 ;; is the repeater string, if any.
3542 (lambda (time activep &optional with-time-p hour-end minute-end)
3543 (let ((ts (format-time-string
3544 (funcall (if with-time-p 'cdr 'car)
3545 org-time-stamp-formats)
3546 time)))
3547 (when (and hour-end minute-end)
3548 (string-match "[012]?[0-9]:[0-5][0-9]" ts)
3549 (setq ts
3550 (replace-match
3551 (format "\\&-%02d:%02d" hour-end minute-end)
3552 nil nil ts)))
3553 (unless activep (setq ts (format "[%s]" (substring ts 1 -1))))
3554 (when (org-string-nw-p repeat-string)
3555 (setq ts (concat (substring ts 0 -1)
3557 repeat-string
3558 (substring ts -1))))
3559 ;; Return value.
3560 ts)))
3561 (type (org-element-property :type timestamp)))
3562 (case type
3563 ((active inactive)
3564 (let* ((minute-start (org-element-property :minute-start timestamp))
3565 (minute-end (org-element-property :minute-end timestamp))
3566 (hour-start (org-element-property :hour-start timestamp))
3567 (hour-end (org-element-property :hour-end timestamp))
3568 (time-range-p (and hour-start hour-end minute-start minute-end
3569 (or (/= hour-start hour-end)
3570 (/= minute-start minute-end)))))
3571 (funcall
3572 build-ts-string
3573 (encode-time 0
3574 (or minute-start 0)
3575 (or hour-start 0)
3576 (org-element-property :day-start timestamp)
3577 (org-element-property :month-start timestamp)
3578 (org-element-property :year-start timestamp))
3579 (eq type 'active)
3580 (and hour-start minute-start)
3581 (and time-range-p hour-end)
3582 (and time-range-p minute-end))))
3583 ((active-range inactive-range)
3584 (let ((minute-start (org-element-property :minute-start timestamp))
3585 (minute-end (org-element-property :minute-end timestamp))
3586 (hour-start (org-element-property :hour-start timestamp))
3587 (hour-end (org-element-property :hour-end timestamp)))
3588 (concat
3589 (funcall
3590 build-ts-string (encode-time
3592 (or minute-start 0)
3593 (or hour-start 0)
3594 (org-element-property :day-start timestamp)
3595 (org-element-property :month-start timestamp)
3596 (org-element-property :year-start timestamp))
3597 (eq type 'active-range)
3598 (and hour-start minute-start))
3599 "--"
3600 (funcall build-ts-string
3601 (encode-time 0
3602 (or minute-end 0)
3603 (or hour-end 0)
3604 (org-element-property :day-end timestamp)
3605 (org-element-property :month-end timestamp)
3606 (org-element-property :year-end timestamp))
3607 (eq type 'active-range)
3608 (and hour-end minute-end)))))))))
3610 (defun org-element-timestamp-successor (limit)
3611 "Search for the next timestamp object.
3613 LIMIT bounds the search.
3615 Return value is a cons cell whose CAR is `timestamp' and CDR is
3616 beginning position."
3617 (save-excursion
3618 (when (re-search-forward
3619 (concat org-ts-regexp-both
3620 "\\|"
3621 "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
3622 "\\|"
3623 "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
3624 limit t)
3625 (cons 'timestamp (match-beginning 0)))))
3628 ;;;; Underline
3630 (defun org-element-underline-parser ()
3631 "Parse underline object at point.
3633 Return a list whose CAR is `underline' and CDR is a plist with
3634 `:begin', `:end', `:contents-begin' and `:contents-end' and
3635 `:post-blank' keywords.
3637 Assume point is at the first underscore marker."
3638 (save-excursion
3639 (unless (bolp) (backward-char 1))
3640 (looking-at org-emph-re)
3641 (let ((begin (match-beginning 2))
3642 (contents-begin (match-beginning 4))
3643 (contents-end (match-end 4))
3644 (post-blank (progn (goto-char (match-end 2))
3645 (skip-chars-forward " \t")))
3646 (end (point)))
3647 (list 'underline
3648 (list :begin begin
3649 :end end
3650 :contents-begin contents-begin
3651 :contents-end contents-end
3652 :post-blank post-blank)))))
3654 (defun org-element-underline-interpreter (underline contents)
3655 "Interpret UNDERLINE object as Org syntax.
3656 CONTENTS is the contents of the object."
3657 (format "_%s_" contents))
3660 ;;;; Verbatim
3662 (defun org-element-verbatim-parser ()
3663 "Parse verbatim object at point.
3665 Return a list whose CAR is `verbatim' and CDR is a plist with
3666 `:value', `:begin', `:end' and `:post-blank' keywords.
3668 Assume point is at the first equal sign marker."
3669 (save-excursion
3670 (unless (bolp) (backward-char 1))
3671 (looking-at org-emph-re)
3672 (let ((begin (match-beginning 2))
3673 (value (org-match-string-no-properties 4))
3674 (post-blank (progn (goto-char (match-end 2))
3675 (skip-chars-forward " \t")))
3676 (end (point)))
3677 (list 'verbatim
3678 (list :value value
3679 :begin begin
3680 :end end
3681 :post-blank post-blank)))))
3683 (defun org-element-verbatim-interpreter (verbatim contents)
3684 "Interpret VERBATIM object as Org syntax.
3685 CONTENTS is nil."
3686 (format "=%s=" (org-element-property :value verbatim)))
3690 ;;; Parsing Element Starting At Point
3692 ;; `org-element--current-element' is the core function of this section.
3693 ;; It returns the Lisp representation of the element starting at
3694 ;; point.
3696 ;; `org-element--current-element' makes use of special modes. They
3697 ;; are activated for fixed element chaining (i.e. `plain-list' >
3698 ;; `item') or fixed conditional element chaining (i.e. `headline' >
3699 ;; `section'). Special modes are: `first-section', `item',
3700 ;; `node-property', `quote-section', `section' and `table-row'.
3702 (defun org-element--current-element
3703 (limit &optional granularity special structure)
3704 "Parse the element starting at point.
3706 LIMIT bounds the search.
3708 Return value is a list like (TYPE PROPS) where TYPE is the type
3709 of the element and PROPS a plist of properties associated to the
3710 element.
3712 Possible types are defined in `org-element-all-elements'.
3714 Optional argument GRANULARITY determines the depth of the
3715 recursion. Allowed values are `headline', `greater-element',
3716 `element', `object' or nil. When it is broader than `object' (or
3717 nil), secondary values will not be parsed, since they only
3718 contain objects.
3720 Optional argument SPECIAL, when non-nil, can be either
3721 `first-section', `item', `node-property', `quote-section',
3722 `section', and `table-row'.
3724 If STRUCTURE isn't provided but SPECIAL is set to `item', it will
3725 be computed.
3727 This function assumes point is always at the beginning of the
3728 element it has to parse."
3729 (save-excursion
3730 (let ((case-fold-search t)
3731 ;; Determine if parsing depth allows for secondary strings
3732 ;; parsing. It only applies to elements referenced in
3733 ;; `org-element-secondary-value-alist'.
3734 (raw-secondary-p (and granularity (not (eq granularity 'object)))))
3735 (cond
3736 ;; Item.
3737 ((eq special 'item)
3738 (org-element-item-parser limit structure raw-secondary-p))
3739 ;; Table Row.
3740 ((eq special 'table-row) (org-element-table-row-parser limit))
3741 ;; Node Property.
3742 ((eq special 'node-property) (org-element-node-property-parser limit))
3743 ;; Headline.
3744 ((org-with-limited-levels (org-at-heading-p))
3745 (org-element-headline-parser limit raw-secondary-p))
3746 ;; Sections (must be checked after headline).
3747 ((eq special 'section) (org-element-section-parser limit))
3748 ((eq special 'quote-section) (org-element-quote-section-parser limit))
3749 ((eq special 'first-section)
3750 (org-element-section-parser
3751 (or (save-excursion (org-with-limited-levels (outline-next-heading)))
3752 limit)))
3753 ;; When not at bol, point is at the beginning of an item or
3754 ;; a footnote definition: next item is always a paragraph.
3755 ((not (bolp)) (org-element-paragraph-parser limit (list (point))))
3756 ;; Planning and Clock.
3757 ((looking-at org-planning-or-clock-line-re)
3758 (if (equal (match-string 1) org-clock-string)
3759 (org-element-clock-parser limit)
3760 (org-element-planning-parser limit)))
3761 ;; Inlinetask.
3762 ((org-at-heading-p)
3763 (org-element-inlinetask-parser limit raw-secondary-p))
3764 ;; From there, elements can have affiliated keywords.
3765 (t (let ((affiliated (org-element--collect-affiliated-keywords)))
3766 (cond
3767 ;; LaTeX Environment.
3768 ((looking-at "[ \t]*\\\\begin{\\([A-Za-z0-9*]+\\)}[ \t]*$")
3769 (org-element-latex-environment-parser limit affiliated))
3770 ;; Drawer and Property Drawer.
3771 ((looking-at org-drawer-regexp)
3772 (if (equal (match-string 1) "PROPERTIES")
3773 (org-element-property-drawer-parser limit affiliated)
3774 (org-element-drawer-parser limit affiliated)))
3775 ;; Fixed Width
3776 ((looking-at "[ \t]*:\\( \\|$\\)")
3777 (org-element-fixed-width-parser limit affiliated))
3778 ;; Inline Comments, Blocks, Babel Calls, Dynamic Blocks and
3779 ;; Keywords.
3780 ((looking-at "[ \t]*#")
3781 (goto-char (match-end 0))
3782 (cond ((looking-at "\\(?: \\|$\\)")
3783 (beginning-of-line)
3784 (org-element-comment-parser limit affiliated))
3785 ((looking-at "\\+BEGIN_\\(\\S-+\\)")
3786 (beginning-of-line)
3787 (let ((parser (assoc (upcase (match-string 1))
3788 org-element-block-name-alist)))
3789 (if parser (funcall (cdr parser) limit affiliated)
3790 (org-element-special-block-parser limit affiliated))))
3791 ((looking-at "\\+CALL:")
3792 (beginning-of-line)
3793 (org-element-babel-call-parser limit affiliated))
3794 ((looking-at "\\+BEGIN:? ")
3795 (beginning-of-line)
3796 (org-element-dynamic-block-parser limit affiliated))
3797 ((looking-at "\\+\\S-+:")
3798 (beginning-of-line)
3799 (org-element-keyword-parser limit affiliated))
3801 (beginning-of-line)
3802 (org-element-paragraph-parser limit affiliated))))
3803 ;; Footnote Definition.
3804 ((looking-at org-footnote-definition-re)
3805 (org-element-footnote-definition-parser limit affiliated))
3806 ;; Horizontal Rule.
3807 ((looking-at "[ \t]*-\\{5,\\}[ \t]*$")
3808 (org-element-horizontal-rule-parser limit affiliated))
3809 ;; Diary Sexp.
3810 ((looking-at "%%(")
3811 (org-element-diary-sexp-parser limit affiliated))
3812 ;; Table.
3813 ((org-at-table-p t) (org-element-table-parser limit affiliated))
3814 ;; List.
3815 ((looking-at (org-item-re))
3816 (org-element-plain-list-parser
3817 limit affiliated (or structure (org-list-struct))))
3818 ;; Default element: Paragraph.
3819 (t (org-element-paragraph-parser limit affiliated)))))))))
3822 ;; Most elements can have affiliated keywords. When looking for an
3823 ;; element beginning, we want to move before them, as they belong to
3824 ;; that element, and, in the meantime, collect information they give
3825 ;; into appropriate properties. Hence the following function.
3827 (defun org-element--collect-affiliated-keywords ()
3828 "Collect affiliated keywords from point.
3830 Return a list whose CAR is the position at the first of them and
3831 CDR a plist of keywords and values and move point to the
3832 beginning of the first line after them.
3834 As a special case, if element doesn't start at the beginning of
3835 the line (i.e. a paragraph starting an item), CAR is current
3836 position of point and CDR is nil."
3837 (if (not (bolp)) (list (point))
3838 (let ((case-fold-search t)
3839 (origin (point))
3840 ;; RESTRICT is the list of objects allowed in parsed
3841 ;; keywords value.
3842 (restrict (org-element-restriction 'keyword))
3843 output)
3844 (while (and (not (eobp)) (looking-at org-element--affiliated-re))
3845 (let* ((raw-kwd (upcase (match-string 1)))
3846 ;; Apply translation to RAW-KWD. From there, KWD is
3847 ;; the official keyword.
3848 (kwd (or (cdr (assoc raw-kwd
3849 org-element-keyword-translation-alist))
3850 raw-kwd))
3851 ;; Find main value for any keyword.
3852 (value
3853 (save-match-data
3854 (org-trim
3855 (buffer-substring-no-properties
3856 (match-end 0) (point-at-eol)))))
3857 ;; PARSEDP is non-nil when keyword should have its
3858 ;; value parsed.
3859 (parsedp (member kwd org-element-parsed-keywords))
3860 ;; If KWD is a dual keyword, find its secondary
3861 ;; value. Maybe parse it.
3862 (dualp (member kwd org-element-dual-keywords))
3863 (dual-value
3864 (and dualp
3865 (let ((sec (org-match-string-no-properties 2)))
3866 (if (or (not sec) (not parsedp)) sec
3867 (org-element-parse-secondary-string sec restrict)))))
3868 ;; Attribute a property name to KWD.
3869 (kwd-sym (and kwd (intern (concat ":" (downcase kwd))))))
3870 ;; Now set final shape for VALUE.
3871 (when parsedp
3872 (setq value (org-element-parse-secondary-string value restrict)))
3873 (when dualp
3874 (setq value (and (or value dual-value) (cons value dual-value))))
3875 (when (or (member kwd org-element-multiple-keywords)
3876 ;; Attributes can always appear on multiple lines.
3877 (string-match "^ATTR_" kwd))
3878 (setq value (cons value (plist-get output kwd-sym))))
3879 ;; Eventually store the new value in OUTPUT.
3880 (setq output (plist-put output kwd-sym value))
3881 ;; Move to next keyword.
3882 (forward-line)))
3883 ;; If affiliated keywords are orphaned: move back to first one.
3884 ;; They will be parsed as a paragraph.
3885 (when (looking-at "[ \t]*$") (goto-char origin) (setq output nil))
3886 ;; Return value.
3887 (cons origin output))))
3891 ;;; The Org Parser
3893 ;; The two major functions here are `org-element-parse-buffer', which
3894 ;; parses Org syntax inside the current buffer, taking into account
3895 ;; region, narrowing, or even visibility if specified, and
3896 ;; `org-element-parse-secondary-string', which parses objects within
3897 ;; a given string.
3899 ;; The (almost) almighty `org-element-map' allows to apply a function
3900 ;; on elements or objects matching some type, and accumulate the
3901 ;; resulting values. In an export situation, it also skips unneeded
3902 ;; parts of the parse tree.
3904 (defun org-element-parse-buffer (&optional granularity visible-only)
3905 "Recursively parse the buffer and return structure.
3906 If narrowing is in effect, only parse the visible part of the
3907 buffer.
3909 Optional argument GRANULARITY determines the depth of the
3910 recursion. It can be set to the following symbols:
3912 `headline' Only parse headlines.
3913 `greater-element' Don't recurse into greater elements excepted
3914 headlines and sections. Thus, elements
3915 parsed are the top-level ones.
3916 `element' Parse everything but objects and plain text.
3917 `object' Parse the complete buffer (default).
3919 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3920 elements.
3922 An element or an objects is represented as a list with the
3923 pattern (TYPE PROPERTIES CONTENTS), where :
3925 TYPE is a symbol describing the element or object. See
3926 `org-element-all-elements' and `org-element-all-objects' for an
3927 exhaustive list of such symbols. One can retrieve it with
3928 `org-element-type' function.
3930 PROPERTIES is the list of attributes attached to the element or
3931 object, as a plist. Although most of them are specific to the
3932 element or object type, all types share `:begin', `:end',
3933 `:post-blank' and `:parent' properties, which respectively
3934 refer to buffer position where the element or object starts,
3935 ends, the number of white spaces or blank lines after it, and
3936 the element or object containing it. Properties values can be
3937 obtained by using `org-element-property' function.
3939 CONTENTS is a list of elements, objects or raw strings
3940 contained in the current element or object, when applicable.
3941 One can access them with `org-element-contents' function.
3943 The Org buffer has `org-data' as type and nil as properties.
3944 `org-element-map' function can be used to find specific elements
3945 or objects within the parse tree.
3947 This function assumes that current major mode is `org-mode'."
3948 (save-excursion
3949 (goto-char (point-min))
3950 (org-skip-whitespace)
3951 (org-element--parse-elements
3952 (point-at-bol) (point-max)
3953 ;; Start in `first-section' mode so text before the first
3954 ;; headline belongs to a section.
3955 'first-section nil granularity visible-only (list 'org-data nil))))
3957 (defun org-element-parse-secondary-string (string restriction &optional parent)
3958 "Recursively parse objects in STRING and return structure.
3960 RESTRICTION is a symbol limiting the object types that will be
3961 looked after.
3963 Optional argument PARENT, when non-nil, is the element or object
3964 containing the secondary string. It is used to set correctly
3965 `:parent' property within the string."
3966 ;; Copy buffer-local variables listed in
3967 ;; `org-element-object-variables' into temporary buffer. This is
3968 ;; required since object parsing is dependent on these variables.
3969 (let ((pairs (delq nil (mapcar (lambda (var)
3970 (when (boundp var)
3971 (cons var (symbol-value var))))
3972 org-element-object-variables))))
3973 (with-temp-buffer
3974 (mapc (lambda (pair) (org-set-local (car pair) (cdr pair))) pairs)
3975 (insert string)
3976 (let ((secondary (org-element--parse-objects
3977 (point-min) (point-max) nil restriction)))
3978 (when parent
3979 (mapc (lambda (obj) (org-element-put-property obj :parent parent))
3980 secondary))
3981 secondary))))
3983 (defun org-element-map
3984 (data types fun &optional info first-match no-recursion with-affiliated)
3985 "Map a function on selected elements or objects.
3987 DATA is an Org buffer parse tree, as returned by, i.e,
3988 `org-element-parse-buffer'. TYPES is a symbol or list of symbols
3989 of elements or objects types (see `org-element-all-elements' and
3990 `org-element-all-objects' for a complete list of types). FUN is
3991 the function called on the matching element or object. It has to
3992 accept one argument: the element or object itself.
3994 When optional argument INFO is non-nil, it should be a plist
3995 holding export options. In that case, parts of the parse tree
3996 not exportable according to that property list will be skipped.
3998 When optional argument FIRST-MATCH is non-nil, stop at the first
3999 match for which FUN doesn't return nil, and return that value.
4001 Optional argument NO-RECURSION is a symbol or a list of symbols
4002 representing elements or objects types. `org-element-map' won't
4003 enter any recursive element or object whose type belongs to that
4004 list. Though, FUN can still be applied on them.
4006 When optional argument WITH-AFFILIATED is non-nil, FUN will also
4007 apply to matching objects within parsed affiliated keywords (see
4008 `org-element-parsed-keywords').
4010 Nil values returned from FUN do not appear in the results.
4013 Examples:
4014 --------
4016 Assuming TREE is a variable containing an Org buffer parse tree,
4017 the following example will return a flat list of all `src-block'
4018 and `example-block' elements in it:
4020 \(org-element-map tree '(example-block src-block) 'identity)
4022 The following snippet will find the first headline with a level
4023 of 1 and a \"phone\" tag, and will return its beginning position:
4025 \(org-element-map tree 'headline
4026 \(lambda (hl)
4027 \(and (= (org-element-property :level hl) 1)
4028 \(member \"phone\" (org-element-property :tags hl))
4029 \(org-element-property :begin hl)))
4030 nil t)
4032 The next example will return a flat list of all `plain-list' type
4033 elements in TREE that are not a sub-list themselves:
4035 \(org-element-map tree 'plain-list 'identity nil nil 'plain-list)
4037 Eventually, this example will return a flat list of all `bold'
4038 type objects containing a `latex-snippet' type object, even
4039 looking into captions:
4041 \(org-element-map tree 'bold
4042 \(lambda (b)
4043 \(and (org-element-map b 'latex-snippet 'identity nil t) b))
4044 nil nil nil t)"
4045 ;; Ensure TYPES and NO-RECURSION are a list, even of one element.
4046 (unless (listp types) (setq types (list types)))
4047 (unless (listp no-recursion) (setq no-recursion (list no-recursion)))
4048 ;; Recursion depth is determined by --CATEGORY.
4049 (let* ((--category
4050 (catch 'found
4051 (let ((category 'greater-elements))
4052 (mapc (lambda (type)
4053 (cond ((or (memq type org-element-all-objects)
4054 (eq type 'plain-text))
4055 ;; If one object is found, the function
4056 ;; has to recurse into every object.
4057 (throw 'found 'objects))
4058 ((not (memq type org-element-greater-elements))
4059 ;; If one regular element is found, the
4060 ;; function has to recurse, at least,
4061 ;; into every element it encounters.
4062 (and (not (eq category 'elements))
4063 (setq category 'elements)))))
4064 types)
4065 category)))
4066 ;; Compute properties for affiliated keywords if necessary.
4067 (--affiliated-alist
4068 (and with-affiliated
4069 (mapcar (lambda (kwd)
4070 (cons kwd (intern (concat ":" (downcase kwd)))))
4071 org-element-affiliated-keywords)))
4072 --acc
4073 --walk-tree
4074 (--walk-tree
4075 (function
4076 (lambda (--data)
4077 ;; Recursively walk DATA. INFO, if non-nil, is a plist
4078 ;; holding contextual information.
4079 (let ((--type (org-element-type --data)))
4080 (cond
4081 ((not --data))
4082 ;; Ignored element in an export context.
4083 ((and info (memq --data (plist-get info :ignore-list))))
4084 ;; Secondary string: only objects can be found there.
4085 ((not --type)
4086 (when (eq --category 'objects) (mapc --walk-tree --data)))
4087 ;; Unconditionally enter parse trees.
4088 ((eq --type 'org-data)
4089 (mapc --walk-tree (org-element-contents --data)))
4091 ;; Check if TYPE is matching among TYPES. If so,
4092 ;; apply FUN to --DATA and accumulate return value
4093 ;; into --ACC (or exit if FIRST-MATCH is non-nil).
4094 (when (memq --type types)
4095 (let ((result (funcall fun --data)))
4096 (cond ((not result))
4097 (first-match (throw '--map-first-match result))
4098 (t (push result --acc)))))
4099 ;; If --DATA has a secondary string that can contain
4100 ;; objects with their type among TYPES, look into it.
4101 (when (and (eq --category 'objects) (not (stringp --data)))
4102 (let ((sec-prop
4103 (assq --type org-element-secondary-value-alist)))
4104 (when sec-prop
4105 (funcall --walk-tree
4106 (org-element-property (cdr sec-prop) --data)))))
4107 ;; If --DATA has any affiliated keywords and
4108 ;; WITH-AFFILIATED is non-nil, look for objects in
4109 ;; them.
4110 (when (and with-affiliated
4111 (eq --category 'objects)
4112 (memq --type org-element-all-elements))
4113 (mapc (lambda (kwd-pair)
4114 (let ((kwd (car kwd-pair))
4115 (value (org-element-property
4116 (cdr kwd-pair) --data)))
4117 ;; Pay attention to the type of value.
4118 ;; Preserve order for multiple keywords.
4119 (cond
4120 ((not value))
4121 ((and (member kwd org-element-multiple-keywords)
4122 (member kwd org-element-dual-keywords))
4123 (mapc (lambda (line)
4124 (funcall --walk-tree (cdr line))
4125 (funcall --walk-tree (car line)))
4126 (reverse value)))
4127 ((member kwd org-element-multiple-keywords)
4128 (mapc (lambda (line) (funcall --walk-tree line))
4129 (reverse value)))
4130 ((member kwd org-element-dual-keywords)
4131 (funcall --walk-tree (cdr value))
4132 (funcall --walk-tree (car value)))
4133 (t (funcall --walk-tree value)))))
4134 --affiliated-alist))
4135 ;; Determine if a recursion into --DATA is possible.
4136 (cond
4137 ;; --TYPE is explicitly removed from recursion.
4138 ((memq --type no-recursion))
4139 ;; --DATA has no contents.
4140 ((not (org-element-contents --data)))
4141 ;; Looking for greater elements but --DATA is simply
4142 ;; an element or an object.
4143 ((and (eq --category 'greater-elements)
4144 (not (memq --type org-element-greater-elements))))
4145 ;; Looking for elements but --DATA is an object.
4146 ((and (eq --category 'elements)
4147 (memq --type org-element-all-objects)))
4148 ;; In any other case, map contents.
4149 (t (mapc --walk-tree (org-element-contents --data)))))))))))
4150 (catch '--map-first-match
4151 (funcall --walk-tree data)
4152 ;; Return value in a proper order.
4153 (nreverse --acc))))
4154 (put 'org-element-map 'lisp-indent-function 2)
4156 ;; The following functions are internal parts of the parser.
4158 ;; The first one, `org-element--parse-elements' acts at the element's
4159 ;; level.
4161 ;; The second one, `org-element--parse-objects' applies on all objects
4162 ;; of a paragraph or a secondary string. It uses
4163 ;; `org-element--get-next-object-candidates' to optimize the search of
4164 ;; the next object in the buffer.
4166 ;; More precisely, that function looks for every allowed object type
4167 ;; first. Then, it discards failed searches, keeps further matches,
4168 ;; and searches again types matched behind point, for subsequent
4169 ;; calls. Thus, searching for a given type fails only once, and every
4170 ;; object is searched only once at top level (but sometimes more for
4171 ;; nested types).
4173 (defun org-element--parse-elements
4174 (beg end special structure granularity visible-only acc)
4175 "Parse elements between BEG and END positions.
4177 SPECIAL prioritize some elements over the others. It can be set
4178 to `first-section', `quote-section', `section' `item' or
4179 `table-row'.
4181 When value is `item', STRUCTURE will be used as the current list
4182 structure.
4184 GRANULARITY determines the depth of the recursion. See
4185 `org-element-parse-buffer' for more information.
4187 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
4188 elements.
4190 Elements are accumulated into ACC."
4191 (save-excursion
4192 (goto-char beg)
4193 ;; When parsing only headlines, skip any text before first one.
4194 (when (and (eq granularity 'headline) (not (org-at-heading-p)))
4195 (org-with-limited-levels (outline-next-heading)))
4196 ;; Main loop start.
4197 (while (< (point) end)
4198 ;; Find current element's type and parse it accordingly to
4199 ;; its category.
4200 (let* ((element (org-element--current-element
4201 end granularity special structure))
4202 (type (org-element-type element))
4203 (cbeg (org-element-property :contents-begin element)))
4204 (goto-char (org-element-property :end element))
4205 ;; Fill ELEMENT contents by side-effect.
4206 (cond
4207 ;; If VISIBLE-ONLY is true and element is hidden or if it has
4208 ;; no contents, don't modify it.
4209 ((or (and visible-only (org-element-property :hiddenp element))
4210 (not cbeg)))
4211 ;; Greater element: parse it between `contents-begin' and
4212 ;; `contents-end'. Make sure GRANULARITY allows the
4213 ;; recursion, or ELEMENT is an headline, in which case going
4214 ;; inside is mandatory, in order to get sub-level headings.
4215 ((and (memq type org-element-greater-elements)
4216 (or (memq granularity '(element object nil))
4217 (and (eq granularity 'greater-element)
4218 (eq type 'section))
4219 (eq type 'headline)))
4220 (org-element--parse-elements
4221 cbeg (org-element-property :contents-end element)
4222 ;; Possibly switch to a special mode.
4223 (case type
4224 (headline
4225 (if (org-element-property :quotedp element) 'quote-section
4226 'section))
4227 (plain-list 'item)
4228 (property-drawer 'node-property)
4229 (table 'table-row))
4230 (and (memq type '(item plain-list))
4231 (org-element-property :structure element))
4232 granularity visible-only element))
4233 ;; ELEMENT has contents. Parse objects inside, if
4234 ;; GRANULARITY allows it.
4235 ((memq granularity '(object nil))
4236 (org-element--parse-objects
4237 cbeg (org-element-property :contents-end element) element
4238 (org-element-restriction type))))
4239 (org-element-adopt-elements acc element)))
4240 ;; Return result.
4241 acc))
4243 (defun org-element--parse-objects (beg end acc restriction)
4244 "Parse objects between BEG and END and return recursive structure.
4246 Objects are accumulated in ACC.
4248 RESTRICTION is a list of object types which are allowed in the
4249 current object."
4250 (let (candidates)
4251 (save-excursion
4252 (goto-char beg)
4253 (while (and (< (point) end)
4254 (setq candidates (org-element--get-next-object-candidates
4255 end restriction candidates)))
4256 (let ((next-object
4257 (let ((pos (apply 'min (mapcar 'cdr candidates))))
4258 (save-excursion
4259 (goto-char pos)
4260 (funcall (intern (format "org-element-%s-parser"
4261 (car (rassq pos candidates)))))))))
4262 ;; 1. Text before any object. Untabify it.
4263 (let ((obj-beg (org-element-property :begin next-object)))
4264 (unless (= (point) obj-beg)
4265 (setq acc
4266 (org-element-adopt-elements
4268 (replace-regexp-in-string
4269 "\t" (make-string tab-width ? )
4270 (buffer-substring-no-properties (point) obj-beg))))))
4271 ;; 2. Object...
4272 (let ((obj-end (org-element-property :end next-object))
4273 (cont-beg (org-element-property :contents-begin next-object)))
4274 ;; Fill contents of NEXT-OBJECT by side-effect, if it has
4275 ;; a recursive type.
4276 (when (and cont-beg
4277 (memq (car next-object) org-element-recursive-objects))
4278 (save-restriction
4279 (narrow-to-region
4280 cont-beg
4281 (org-element-property :contents-end next-object))
4282 (org-element--parse-objects
4283 (point-min) (point-max) next-object
4284 (org-element-restriction next-object))))
4285 (setq acc (org-element-adopt-elements acc next-object))
4286 (goto-char obj-end))))
4287 ;; 3. Text after last object. Untabify it.
4288 (unless (= (point) end)
4289 (setq acc
4290 (org-element-adopt-elements
4292 (replace-regexp-in-string
4293 "\t" (make-string tab-width ? )
4294 (buffer-substring-no-properties (point) end)))))
4295 ;; Result.
4296 acc)))
4298 (defun org-element--get-next-object-candidates (limit restriction objects)
4299 "Return an alist of candidates for the next object.
4301 LIMIT bounds the search, and RESTRICTION narrows candidates to
4302 some object types.
4304 Return value is an alist whose CAR is position and CDR the object
4305 type, as a symbol.
4307 OBJECTS is the previous candidates alist."
4308 ;; Filter out any object found but not belonging to RESTRICTION.
4309 (setq objects
4310 (org-remove-if-not
4311 (lambda (obj)
4312 (let ((type (car obj)))
4313 (memq (or (cdr (assq type org-element-object-successor-alist))
4314 type)
4315 restriction)))
4316 objects))
4317 (let (next-candidates types-to-search)
4318 ;; If no previous result, search every object type in RESTRICTION.
4319 ;; Otherwise, keep potential candidates (old objects located after
4320 ;; point) and ask to search again those which had matched before.
4321 (if (not objects) (setq types-to-search restriction)
4322 (mapc (lambda (obj)
4323 (if (< (cdr obj) (point)) (push (car obj) types-to-search)
4324 (push obj next-candidates)))
4325 objects))
4326 ;; Call the appropriate successor function for each type to search
4327 ;; and accumulate matches.
4328 (mapc
4329 (lambda (type)
4330 (let* ((successor-fun
4331 (intern
4332 (format "org-element-%s-successor"
4333 (or (cdr (assq type org-element-object-successor-alist))
4334 type))))
4335 (obj (funcall successor-fun limit)))
4336 (and obj (push obj next-candidates))))
4337 types-to-search)
4338 ;; Return alist.
4339 next-candidates))
4343 ;;; Towards A Bijective Process
4345 ;; The parse tree obtained with `org-element-parse-buffer' is really
4346 ;; a snapshot of the corresponding Org buffer. Therefore, it can be
4347 ;; interpreted and expanded into a string with canonical Org syntax.
4348 ;; Hence `org-element-interpret-data'.
4350 ;; The function relies internally on
4351 ;; `org-element--interpret-affiliated-keywords'.
4353 ;;;###autoload
4354 (defun org-element-interpret-data (data &optional parent)
4355 "Interpret DATA as Org syntax.
4357 DATA is a parse tree, an element, an object or a secondary string
4358 to interpret.
4360 Optional argument PARENT is used for recursive calls. It contains
4361 the element or object containing data, or nil.
4363 Return Org syntax as a string."
4364 (let* ((type (org-element-type data))
4365 (results
4366 (cond
4367 ;; Secondary string.
4368 ((not type)
4369 (mapconcat
4370 (lambda (obj) (org-element-interpret-data obj parent))
4371 data ""))
4372 ;; Full Org document.
4373 ((eq type 'org-data)
4374 (mapconcat
4375 (lambda (obj) (org-element-interpret-data obj parent))
4376 (org-element-contents data) ""))
4377 ;; Plain text: remove `:parent' text property from output.
4378 ((stringp data) (org-no-properties data))
4379 ;; Element/Object without contents.
4380 ((not (org-element-contents data))
4381 (funcall (intern (format "org-element-%s-interpreter" type))
4382 data nil))
4383 ;; Element/Object with contents.
4385 (let* ((greaterp (memq type org-element-greater-elements))
4386 (objectp (and (not greaterp)
4387 (memq type org-element-recursive-objects)))
4388 (contents
4389 (mapconcat
4390 (lambda (obj) (org-element-interpret-data obj data))
4391 (org-element-contents
4392 (if (or greaterp objectp) data
4393 ;; Elements directly containing objects must
4394 ;; have their indentation normalized first.
4395 (org-element-normalize-contents
4396 data
4397 ;; When normalizing first paragraph of an
4398 ;; item or a footnote-definition, ignore
4399 ;; first line's indentation.
4400 (and (eq type 'paragraph)
4401 (equal data (car (org-element-contents parent)))
4402 (memq (org-element-type parent)
4403 '(footnote-definition item))))))
4404 "")))
4405 (funcall (intern (format "org-element-%s-interpreter" type))
4406 data
4407 (if greaterp (org-element-normalize-contents contents)
4408 contents)))))))
4409 (if (memq type '(org-data plain-text nil)) results
4410 ;; Build white spaces. If no `:post-blank' property is
4411 ;; specified, assume its value is 0.
4412 (let ((post-blank (or (org-element-property :post-blank data) 0)))
4413 (if (memq type org-element-all-objects)
4414 (concat results (make-string post-blank 32))
4415 (concat
4416 (org-element--interpret-affiliated-keywords data)
4417 (org-element-normalize-string results)
4418 (make-string post-blank 10)))))))
4420 (defun org-element--interpret-affiliated-keywords (element)
4421 "Return ELEMENT's affiliated keywords as Org syntax.
4422 If there is no affiliated keyword, return the empty string."
4423 (let ((keyword-to-org
4424 (function
4425 (lambda (key value)
4426 (let (dual)
4427 (when (member key org-element-dual-keywords)
4428 (setq dual (cdr value) value (car value)))
4429 (concat "#+" key
4430 (and dual
4431 (format "[%s]" (org-element-interpret-data dual)))
4432 ": "
4433 (if (member key org-element-parsed-keywords)
4434 (org-element-interpret-data value)
4435 value)
4436 "\n"))))))
4437 (mapconcat
4438 (lambda (prop)
4439 (let ((value (org-element-property prop element))
4440 (keyword (upcase (substring (symbol-name prop) 1))))
4441 (when value
4442 (if (or (member keyword org-element-multiple-keywords)
4443 ;; All attribute keywords can have multiple lines.
4444 (string-match "^ATTR_" keyword))
4445 (mapconcat (lambda (line) (funcall keyword-to-org keyword line))
4446 (reverse value)
4448 (funcall keyword-to-org keyword value)))))
4449 ;; List all ELEMENT's properties matching an attribute line or an
4450 ;; affiliated keyword, but ignore translated keywords since they
4451 ;; cannot belong to the property list.
4452 (loop for prop in (nth 1 element) by 'cddr
4453 when (let ((keyword (upcase (substring (symbol-name prop) 1))))
4454 (or (string-match "^ATTR_" keyword)
4455 (and
4456 (member keyword org-element-affiliated-keywords)
4457 (not (assoc keyword
4458 org-element-keyword-translation-alist)))))
4459 collect prop)
4460 "")))
4462 ;; Because interpretation of the parse tree must return the same
4463 ;; number of blank lines between elements and the same number of white
4464 ;; space after objects, some special care must be given to white
4465 ;; spaces.
4467 ;; The first function, `org-element-normalize-string', ensures any
4468 ;; string different from the empty string will end with a single
4469 ;; newline character.
4471 ;; The second function, `org-element-normalize-contents', removes
4472 ;; global indentation from the contents of the current element.
4474 (defun org-element-normalize-string (s)
4475 "Ensure string S ends with a single newline character.
4477 If S isn't a string return it unchanged. If S is the empty
4478 string, return it. Otherwise, return a new string with a single
4479 newline character at its end."
4480 (cond
4481 ((not (stringp s)) s)
4482 ((string= "" s) "")
4483 (t (and (string-match "\\(\n[ \t]*\\)*\\'" s)
4484 (replace-match "\n" nil nil s)))))
4486 (defun org-element-normalize-contents (element &optional ignore-first)
4487 "Normalize plain text in ELEMENT's contents.
4489 ELEMENT must only contain plain text and objects.
4491 If optional argument IGNORE-FIRST is non-nil, ignore first line's
4492 indentation to compute maximal common indentation.
4494 Return the normalized element that is element with global
4495 indentation removed from its contents. The function assumes that
4496 indentation is not done with TAB characters."
4497 (let* (ind-list ; for byte-compiler
4498 collect-inds ; for byte-compiler
4499 (collect-inds
4500 (function
4501 ;; Return list of indentations within BLOB. This is done by
4502 ;; walking recursively BLOB and updating IND-LIST along the
4503 ;; way. FIRST-FLAG is non-nil when the first string hasn't
4504 ;; been seen yet. It is required as this string is the only
4505 ;; one whose indentation doesn't happen after a newline
4506 ;; character.
4507 (lambda (blob first-flag)
4508 (mapc
4509 (lambda (object)
4510 (when (and first-flag (stringp object))
4511 (setq first-flag nil)
4512 (string-match "\\`\\( *\\)" object)
4513 (let ((len (length (match-string 1 object))))
4514 ;; An indentation of zero means no string will be
4515 ;; modified. Quit the process.
4516 (if (zerop len) (throw 'zero (setq ind-list nil))
4517 (push len ind-list))))
4518 (cond
4519 ((stringp object)
4520 (let ((start 0))
4521 ;; Avoid matching blank or empty lines.
4522 (while (and (string-match "\n\\( *\\)\\(.\\)" object start)
4523 (not (equal (match-string 2 object) " ")))
4524 (setq start (match-end 0))
4525 (push (length (match-string 1 object)) ind-list))))
4526 ((memq (org-element-type object) org-element-recursive-objects)
4527 (funcall collect-inds object first-flag))))
4528 (org-element-contents blob))))))
4529 ;; Collect indentation list in ELEMENT. Possibly remove first
4530 ;; value if IGNORE-FIRST is non-nil.
4531 (catch 'zero (funcall collect-inds element (not ignore-first)))
4532 (if (not ind-list) element
4533 ;; Build ELEMENT back, replacing each string with the same
4534 ;; string minus common indentation.
4535 (let* (build ; For byte compiler.
4536 (build
4537 (function
4538 (lambda (blob mci first-flag)
4539 ;; Return BLOB with all its strings indentation
4540 ;; shortened from MCI white spaces. FIRST-FLAG is
4541 ;; non-nil when the first string hasn't been seen
4542 ;; yet.
4543 (setcdr (cdr blob)
4544 (mapcar
4545 (lambda (object)
4546 (when (and first-flag (stringp object))
4547 (setq first-flag nil)
4548 (setq object
4549 (replace-regexp-in-string
4550 (format "\\` \\{%d\\}" mci) "" object)))
4551 (cond
4552 ((stringp object)
4553 (replace-regexp-in-string
4554 (format "\n \\{%d\\}" mci) "\n" object))
4555 ((memq (org-element-type object)
4556 org-element-recursive-objects)
4557 (funcall build object mci first-flag))
4558 (t object)))
4559 (org-element-contents blob)))
4560 blob))))
4561 (funcall build element (apply 'min ind-list) (not ignore-first))))))
4565 ;;; The Toolbox
4567 ;; The first move is to implement a way to obtain the smallest element
4568 ;; containing point. This is the job of `org-element-at-point'. It
4569 ;; basically jumps back to the beginning of section containing point
4570 ;; and moves, element after element, with
4571 ;; `org-element--current-element' until the container is found. Note:
4572 ;; When using `org-element-at-point', secondary values are never
4573 ;; parsed since the function focuses on elements, not on objects.
4575 ;; At a deeper level, `org-element-context' lists all elements and
4576 ;; objects containing point.
4578 ;; `org-element-nested-p' and `org-element-swap-A-B' may be used
4579 ;; internally by navigation and manipulation tools.
4581 ;;;###autoload
4582 (defun org-element-at-point (&optional keep-trail)
4583 "Determine closest element around point.
4585 Return value is a list like (TYPE PROPS) where TYPE is the type
4586 of the element and PROPS a plist of properties associated to the
4587 element.
4589 Possible types are defined in `org-element-all-elements'.
4590 Properties depend on element or object type, but always include
4591 `:begin', `:end', `:parent' and `:post-blank' properties.
4593 As a special case, if point is at the very beginning of a list or
4594 sub-list, returned element will be that list instead of the first
4595 item. In the same way, if point is at the beginning of the first
4596 row of a table, returned element will be the table instead of the
4597 first row.
4599 If optional argument KEEP-TRAIL is non-nil, the function returns
4600 a list of elements leading to element at point. The list's CAR
4601 is always the element at point. The following positions contain
4602 element's siblings, then parents, siblings of parents, until the
4603 first element of current section."
4604 (org-with-wide-buffer
4605 ;; If at an headline, parse it. It is the sole element that
4606 ;; doesn't require to know about context. Be sure to disallow
4607 ;; secondary string parsing, though.
4608 (if (org-with-limited-levels (org-at-heading-p))
4609 (progn
4610 (beginning-of-line)
4611 (if (not keep-trail) (org-element-headline-parser (point-max) t)
4612 (list (org-element-headline-parser (point-max) t))))
4613 ;; Otherwise move at the beginning of the section containing
4614 ;; point.
4615 (let ((origin (point))
4616 (end (save-excursion
4617 (org-with-limited-levels (outline-next-heading)) (point)))
4618 element type special-flag trail struct prevs parent)
4619 (org-with-limited-levels
4620 (if (org-before-first-heading-p) (goto-char (point-min))
4621 (org-back-to-heading)
4622 (forward-line)))
4623 (org-skip-whitespace)
4624 (beginning-of-line)
4625 ;; Parse successively each element, skipping those ending
4626 ;; before original position.
4627 (catch 'exit
4628 (while t
4629 (setq element
4630 (org-element--current-element end 'element special-flag struct)
4631 type (car element))
4632 (org-element-put-property element :parent parent)
4633 (when keep-trail (push element trail))
4634 (cond
4635 ;; 1. Skip any element ending before point. Also skip
4636 ;; element ending at point when we're sure that another
4637 ;; element has started.
4638 ((let ((elem-end (org-element-property :end element)))
4639 (when (or (< elem-end origin)
4640 (and (= elem-end origin) (/= elem-end end)))
4641 (goto-char elem-end))))
4642 ;; 2. An element containing point is always the element at
4643 ;; point.
4644 ((not (memq type org-element-greater-elements))
4645 (throw 'exit (if keep-trail trail element)))
4646 ;; 3. At any other greater element type, if point is
4647 ;; within contents, move into it.
4649 (let ((cbeg (org-element-property :contents-begin element))
4650 (cend (org-element-property :contents-end element)))
4651 (if (or (not cbeg) (not cend) (> cbeg origin) (< cend origin)
4652 ;; Create an anchor for tables and plain lists:
4653 ;; when point is at the very beginning of these
4654 ;; elements, ignoring affiliated keywords,
4655 ;; target them instead of their contents.
4656 (and (= cbeg origin) (memq type '(plain-list table)))
4657 ;; When point is at contents end, do not move
4658 ;; into elements with an explicit ending, but
4659 ;; return that element instead.
4660 (and (= cend origin)
4661 (memq type
4662 '(center-block
4663 drawer dynamic-block inlinetask item
4664 plain-list property-drawer quote-block
4665 special-block))))
4666 (throw 'exit (if keep-trail trail element))
4667 (setq parent element)
4668 (case type
4669 (plain-list
4670 (setq special-flag 'item
4671 struct (org-element-property :structure element)))
4672 (item (setq special-flag nil))
4673 (property-drawer
4674 (setq special-flag 'node-property struct nil))
4675 (table (setq special-flag 'table-row struct nil))
4676 (otherwise (setq special-flag nil struct nil)))
4677 (setq end cend)
4678 (goto-char cbeg)))))))))))
4680 ;;;###autoload
4681 (defun org-element-context (&optional element)
4682 "Return closest element or object around point.
4684 Return value is a list like (TYPE PROPS) where TYPE is the type
4685 of the element or object and PROPS a plist of properties
4686 associated to it.
4688 Possible types are defined in `org-element-all-elements' and
4689 `org-element-all-objects'. Properties depend on element or
4690 object type, but always include `:begin', `:end', `:parent' and
4691 `:post-blank'.
4693 Optional argument ELEMENT, when non-nil, is the closest element
4694 containing point, as returned by `org-element-at-point'.
4695 Providing it allows for quicker computation."
4696 (org-with-wide-buffer
4697 (let* ((origin (point))
4698 (element (or element (org-element-at-point)))
4699 (type (org-element-type element))
4700 end)
4701 ;; Check if point is inside an element containing objects or at
4702 ;; a secondary string. In that case, move to beginning of the
4703 ;; element or secondary string and set END to the other side.
4704 (if (not (or (let ((post (org-element-property :post-affiliated element)))
4705 (and post (> post origin)
4706 (< (org-element-property :begin element) origin)
4707 (progn (beginning-of-line)
4708 (looking-at org-element--affiliated-re)
4709 (member (upcase (match-string 1))
4710 org-element-parsed-keywords))
4711 ;; We're at an affiliated keyword. Change
4712 ;; type to retrieve correct restrictions.
4713 (setq type 'keyword)
4714 ;; Determine if we're at main or dual value.
4715 (if (and (match-end 2) (<= origin (match-end 2)))
4716 (progn (goto-char (match-beginning 2))
4717 (setq end (match-end 2)))
4718 (goto-char (match-end 0))
4719 (setq end (line-end-position)))))
4720 (and (eq type 'item)
4721 (let ((tag (org-element-property :tag element)))
4722 (and tag
4723 (progn
4724 (beginning-of-line)
4725 (search-forward tag (point-at-eol))
4726 (goto-char (match-beginning 0))
4727 (and (>= origin (point))
4728 (<= origin
4729 ;; `1+' is required so some
4730 ;; successors can match
4731 ;; properly their object.
4732 (setq end (1+ (match-end 0)))))))))
4733 (and (memq type '(headline inlinetask))
4734 (progn (beginning-of-line)
4735 (skip-chars-forward "* ")
4736 (setq end (point-at-eol))))
4737 (and (memq type '(paragraph table-row verse-block))
4738 (let ((cbeg (org-element-property
4739 :contents-begin element))
4740 (cend (org-element-property
4741 :contents-end element)))
4742 (and (>= origin cbeg)
4743 (<= origin cend)
4744 (progn (goto-char cbeg) (setq end cend)))))
4745 (and (eq type 'keyword)
4746 (let ((key (org-element-property :key element)))
4747 (and (member key org-element-document-properties)
4748 (progn (beginning-of-line)
4749 (search-forward key (line-end-position) t)
4750 (forward-char)
4751 (setq end (line-end-position))))))))
4752 element
4753 (let ((restriction (org-element-restriction type))
4754 (parent element)
4755 candidates)
4756 (catch 'exit
4757 (while (setq candidates (org-element--get-next-object-candidates
4758 end restriction candidates))
4759 (let ((closest-cand (rassq (apply 'min (mapcar 'cdr candidates))
4760 candidates)))
4761 ;; If ORIGIN is before next object in element, there's
4762 ;; no point in looking further.
4763 (if (> (cdr closest-cand) origin) (throw 'exit parent)
4764 (let* ((object
4765 (progn (goto-char (cdr closest-cand))
4766 (funcall (intern (format "org-element-%s-parser"
4767 (car closest-cand))))))
4768 (cbeg (org-element-property :contents-begin object))
4769 (cend (org-element-property :contents-end object))
4770 (obj-end (org-element-property :end object)))
4771 (cond
4772 ;; ORIGIN is after OBJECT, so skip it.
4773 ((<= obj-end origin)
4774 (if (/= obj-end end) (goto-char obj-end)
4775 (throw 'exit
4776 (org-element-put-property
4777 object :parent parent))))
4778 ;; ORIGIN is within a non-recursive object or at
4779 ;; an object boundaries: Return that object.
4780 ((or (not cbeg) (> cbeg origin) (< cend origin))
4781 (throw 'exit
4782 (org-element-put-property object :parent parent)))
4783 ;; Otherwise, move within current object and
4784 ;; restrict search to the end of its contents.
4785 (t (goto-char cbeg)
4786 (org-element-put-property object :parent parent)
4787 (setq parent object
4788 restriction (org-element-restriction object)
4789 end cend)))))))
4790 parent))))))
4792 (defun org-element-nested-p (elem-A elem-B)
4793 "Non-nil when elements ELEM-A and ELEM-B are nested."
4794 (let ((beg-A (org-element-property :begin elem-A))
4795 (beg-B (org-element-property :begin elem-B))
4796 (end-A (org-element-property :end elem-A))
4797 (end-B (org-element-property :end elem-B)))
4798 (or (and (>= beg-A beg-B) (<= end-A end-B))
4799 (and (>= beg-B beg-A) (<= end-B end-A)))))
4801 (defun org-element-swap-A-B (elem-A elem-B)
4802 "Swap elements ELEM-A and ELEM-B.
4803 Assume ELEM-B is after ELEM-A in the buffer. Leave point at the
4804 end of ELEM-A."
4805 (goto-char (org-element-property :begin elem-A))
4806 ;; There are two special cases when an element doesn't start at bol:
4807 ;; the first paragraph in an item or in a footnote definition.
4808 (let ((specialp (not (bolp))))
4809 ;; Only a paragraph without any affiliated keyword can be moved at
4810 ;; ELEM-A position in such a situation. Note that the case of
4811 ;; a footnote definition is impossible: it cannot contain two
4812 ;; paragraphs in a row because it cannot contain a blank line.
4813 (if (and specialp
4814 (or (not (eq (org-element-type elem-B) 'paragraph))
4815 (/= (org-element-property :begin elem-B)
4816 (org-element-property :contents-begin elem-B))))
4817 (error "Cannot swap elements"))
4818 ;; In a special situation, ELEM-A will have no indentation. We'll
4819 ;; give it ELEM-B's (which will in, in turn, have no indentation).
4820 (let* ((ind-B (when specialp
4821 (goto-char (org-element-property :begin elem-B))
4822 (org-get-indentation)))
4823 (beg-A (org-element-property :begin elem-A))
4824 (end-A (save-excursion
4825 (goto-char (org-element-property :end elem-A))
4826 (skip-chars-backward " \r\t\n")
4827 (point-at-eol)))
4828 (beg-B (org-element-property :begin elem-B))
4829 (end-B (save-excursion
4830 (goto-char (org-element-property :end elem-B))
4831 (skip-chars-backward " \r\t\n")
4832 (point-at-eol)))
4833 ;; Store overlays responsible for visibility status. We
4834 ;; also need to store their boundaries as they will be
4835 ;; removed from buffer.
4836 (overlays
4837 (cons
4838 (mapcar (lambda (ov) (list ov (overlay-start ov) (overlay-end ov)))
4839 (overlays-in beg-A end-A))
4840 (mapcar (lambda (ov) (list ov (overlay-start ov) (overlay-end ov)))
4841 (overlays-in beg-B end-B))))
4842 ;; Get contents.
4843 (body-A (buffer-substring beg-A end-A))
4844 (body-B (delete-and-extract-region beg-B end-B)))
4845 (goto-char beg-B)
4846 (when specialp
4847 (setq body-B (replace-regexp-in-string "\\`[ \t]*" "" body-B))
4848 (org-indent-to-column ind-B))
4849 (insert body-A)
4850 ;; Restore ex ELEM-A overlays.
4851 (let ((offset (- beg-B beg-A)))
4852 (mapc (lambda (ov)
4853 (move-overlay
4854 (car ov) (+ (nth 1 ov) offset) (+ (nth 2 ov) offset)))
4855 (car overlays))
4856 (goto-char beg-A)
4857 (delete-region beg-A end-A)
4858 (insert body-B)
4859 ;; Restore ex ELEM-B overlays.
4860 (mapc (lambda (ov)
4861 (move-overlay
4862 (car ov) (- (nth 1 ov) offset) (- (nth 2 ov) offset)))
4863 (cdr overlays)))
4864 (goto-char (org-element-property :end elem-B)))))
4866 (provide 'org-element)
4868 ;; Local variables:
4869 ;; generated-autoload-file: "org-loaddefs.el"
4870 ;; End:
4872 ;;; org-element.el ends here