ox-publish: Small refactoring
[org-mode.git] / lisp / org-element.el
blobc701de0e24377ecafe2e9cf5bd130238f2ac7ca4
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. Expand any
3006 ;; abbreviation in it.
3007 raw-link (org-translate-link
3008 (org-link-expand-abbrev
3009 (org-match-string-no-properties 1)))
3010 link (org-link-unescape raw-link))
3011 ;; Determine TYPE of link and set PATH accordingly.
3012 (cond
3013 ;; File type.
3014 ((or (file-name-absolute-p link) (string-match "^\\.\\.?/" link))
3015 (setq type "file" path link))
3016 ;; Explicit type (http, irc, bbdb...). See `org-link-types'.
3017 ((string-match org-link-re-with-space3 link)
3018 (setq type (match-string 1 link) path (match-string 2 link)))
3019 ;; Id type: PATH is the id.
3020 ((string-match "^id:\\([-a-f0-9]+\\)" link)
3021 (setq type "id" path (match-string 1 link)))
3022 ;; Code-ref type: PATH is the name of the reference.
3023 ((string-match "^(\\(.*\\))$" link)
3024 (setq type "coderef" path (match-string 1 link)))
3025 ;; Custom-id type: PATH is the name of the custom id.
3026 ((= (aref link 0) ?#)
3027 (setq type "custom-id" path (substring link 1)))
3028 ;; Fuzzy type: Internal link either matches a target, an
3029 ;; headline name or nothing. PATH is the target or
3030 ;; headline's name.
3031 (t (setq type "fuzzy" path link))))
3032 ;; Type 3: Plain link, i.e. http://orgmode.org
3033 ((looking-at org-plain-link-re)
3034 (setq raw-link (org-match-string-no-properties 0)
3035 type (org-match-string-no-properties 1)
3036 path (org-match-string-no-properties 2)
3037 link-end (match-end 0)))
3038 ;; Type 4: Angular link, i.e. <http://orgmode.org>
3039 ((looking-at org-angle-link-re)
3040 (setq raw-link (buffer-substring-no-properties
3041 (match-beginning 1) (match-end 2))
3042 type (org-match-string-no-properties 1)
3043 path (org-match-string-no-properties 2)
3044 link-end (match-end 0))))
3045 ;; In any case, deduce end point after trailing white space from
3046 ;; LINK-END variable.
3047 (setq post-blank (progn (goto-char link-end) (skip-chars-forward " \t"))
3048 end (point))
3049 ;; Extract search option and opening application out of
3050 ;; "file"-type links.
3051 (when (member type org-element-link-type-is-file)
3052 ;; Application.
3053 (cond ((string-match "^file\\+\\(.*\\)$" type)
3054 (setq application (match-string 1 type)))
3055 ((not (string-match "^file" type))
3056 (setq application type)))
3057 ;; Extract search option from PATH.
3058 (when (string-match "::\\(.*\\)$" path)
3059 (setq search-option (match-string 1 path)
3060 path (replace-match "" nil nil path)))
3061 ;; Make sure TYPE always report "file".
3062 (setq type "file"))
3063 (list 'link
3064 (list :type type
3065 :path path
3066 :raw-link (or raw-link path)
3067 :application application
3068 :search-option search-option
3069 :begin begin
3070 :end end
3071 :contents-begin contents-begin
3072 :contents-end contents-end
3073 :post-blank post-blank)))))
3075 (defun org-element-link-interpreter (link contents)
3076 "Interpret LINK object as Org syntax.
3077 CONTENTS is the contents of the object, or nil."
3078 (let ((type (org-element-property :type link))
3079 (raw-link (org-element-property :raw-link link)))
3080 (if (string= type "radio") raw-link
3081 (format "[[%s]%s]"
3082 raw-link
3083 (if contents (format "[%s]" contents) "")))))
3085 (defun org-element-link-successor (limit)
3086 "Search for the next link object.
3088 LIMIT bounds the search.
3090 Return value is a cons cell whose CAR is `link' and CDR is
3091 beginning position."
3092 (save-excursion
3093 (let ((link-regexp
3094 (if (not org-target-link-regexp) org-any-link-re
3095 (concat org-any-link-re "\\|" org-target-link-regexp))))
3096 (when (re-search-forward link-regexp limit t)
3097 (cons 'link (match-beginning 0))))))
3100 ;;;; Macro
3102 (defun org-element-macro-parser ()
3103 "Parse macro at point.
3105 Return a list whose CAR is `macro' and CDR a plist with `:key',
3106 `:args', `:begin', `:end', `:value' and `:post-blank' as
3107 keywords.
3109 Assume point is at the macro."
3110 (save-excursion
3111 (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
3112 (let ((begin (point))
3113 (key (downcase (org-match-string-no-properties 1)))
3114 (value (org-match-string-no-properties 0))
3115 (post-blank (progn (goto-char (match-end 0))
3116 (skip-chars-forward " \t")))
3117 (end (point))
3118 (args (let ((args (org-match-string-no-properties 3)) args2)
3119 (when args
3120 ;; Do not use `org-split-string' since empty
3121 ;; strings are meaningful here.
3122 (setq args (split-string args ","))
3123 (while args
3124 (while (string-match "\\\\\\'" (car args))
3125 ;; Repair bad splits, when comma is protected,
3126 ;; and thus not a real separator.
3127 (setcar (cdr args) (concat (substring (car args) 0 -1)
3128 "," (nth 1 args)))
3129 (pop args))
3130 (push (pop args) args2))
3131 (mapcar 'org-trim (nreverse args2))))))
3132 (list 'macro
3133 (list :key key
3134 :value value
3135 :args args
3136 :begin begin
3137 :end end
3138 :post-blank post-blank)))))
3140 (defun org-element-macro-interpreter (macro contents)
3141 "Interpret MACRO object as Org syntax.
3142 CONTENTS is nil."
3143 (org-element-property :value macro))
3145 (defun org-element-macro-successor (limit)
3146 "Search for the next macro object.
3148 LIMIT bounds the search.
3150 Return value is cons cell whose CAR is `macro' and CDR is
3151 beginning position."
3152 (save-excursion
3153 (when (re-search-forward
3154 "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
3155 limit t)
3156 (cons 'macro (match-beginning 0)))))
3159 ;;;; Radio-target
3161 (defun org-element-radio-target-parser ()
3162 "Parse radio target at point.
3164 Return a list whose CAR is `radio-target' and CDR a plist with
3165 `:begin', `:end', `:contents-begin', `:contents-end', `:value'
3166 and `:post-blank' as keywords.
3168 Assume point is at the radio target."
3169 (save-excursion
3170 (looking-at org-radio-target-regexp)
3171 (let ((begin (point))
3172 (contents-begin (match-beginning 1))
3173 (contents-end (match-end 1))
3174 (value (org-match-string-no-properties 1))
3175 (post-blank (progn (goto-char (match-end 0))
3176 (skip-chars-forward " \t")))
3177 (end (point)))
3178 (list 'radio-target
3179 (list :begin begin
3180 :end end
3181 :contents-begin contents-begin
3182 :contents-end contents-end
3183 :post-blank post-blank
3184 :value value)))))
3186 (defun org-element-radio-target-interpreter (target contents)
3187 "Interpret TARGET object as Org syntax.
3188 CONTENTS is the contents of the object."
3189 (concat "<<<" contents ">>>"))
3191 (defun org-element-radio-target-successor (limit)
3192 "Search for the next radio-target object.
3194 LIMIT bounds the search.
3196 Return value is a cons cell whose CAR is `radio-target' and CDR
3197 is beginning position."
3198 (save-excursion
3199 (when (re-search-forward org-radio-target-regexp limit t)
3200 (cons 'radio-target (match-beginning 0)))))
3203 ;;;; Statistics Cookie
3205 (defun org-element-statistics-cookie-parser ()
3206 "Parse statistics cookie at point.
3208 Return a list whose CAR is `statistics-cookie', and CDR a plist
3209 with `:begin', `:end', `:value' and `:post-blank' keywords.
3211 Assume point is at the beginning of the statistics-cookie."
3212 (save-excursion
3213 (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
3214 (let* ((begin (point))
3215 (value (buffer-substring-no-properties
3216 (match-beginning 0) (match-end 0)))
3217 (post-blank (progn (goto-char (match-end 0))
3218 (skip-chars-forward " \t")))
3219 (end (point)))
3220 (list 'statistics-cookie
3221 (list :begin begin
3222 :end end
3223 :value value
3224 :post-blank post-blank)))))
3226 (defun org-element-statistics-cookie-interpreter (statistics-cookie contents)
3227 "Interpret STATISTICS-COOKIE object as Org syntax.
3228 CONTENTS is nil."
3229 (org-element-property :value statistics-cookie))
3231 (defun org-element-statistics-cookie-successor (limit)
3232 "Search for the next statistics cookie object.
3234 LIMIT bounds the search.
3236 Return value is a cons cell whose CAR is `statistics-cookie' and
3237 CDR is beginning position."
3238 (save-excursion
3239 (when (re-search-forward "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]" limit t)
3240 (cons 'statistics-cookie (match-beginning 0)))))
3243 ;;;; Strike-Through
3245 (defun org-element-strike-through-parser ()
3246 "Parse strike-through object at point.
3248 Return a list whose CAR is `strike-through' and CDR is a plist
3249 with `:begin', `:end', `:contents-begin' and `:contents-end' and
3250 `:post-blank' keywords.
3252 Assume point is at the first plus sign marker."
3253 (save-excursion
3254 (unless (bolp) (backward-char 1))
3255 (looking-at org-emph-re)
3256 (let ((begin (match-beginning 2))
3257 (contents-begin (match-beginning 4))
3258 (contents-end (match-end 4))
3259 (post-blank (progn (goto-char (match-end 2))
3260 (skip-chars-forward " \t")))
3261 (end (point)))
3262 (list 'strike-through
3263 (list :begin begin
3264 :end end
3265 :contents-begin contents-begin
3266 :contents-end contents-end
3267 :post-blank post-blank)))))
3269 (defun org-element-strike-through-interpreter (strike-through contents)
3270 "Interpret STRIKE-THROUGH object as Org syntax.
3271 CONTENTS is the contents of the object."
3272 (format "+%s+" contents))
3275 ;;;; Subscript
3277 (defun org-element-subscript-parser ()
3278 "Parse subscript at point.
3280 Return a list whose CAR is `subscript' and CDR a plist with
3281 `:begin', `:end', `:contents-begin', `:contents-end',
3282 `:use-brackets-p' and `:post-blank' as keywords.
3284 Assume point is at the underscore."
3285 (save-excursion
3286 (unless (bolp) (backward-char))
3287 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp)
3289 (not (looking-at org-match-substring-regexp))))
3290 (begin (match-beginning 2))
3291 (contents-begin (or (match-beginning 5)
3292 (match-beginning 3)))
3293 (contents-end (or (match-end 5) (match-end 3)))
3294 (post-blank (progn (goto-char (match-end 0))
3295 (skip-chars-forward " \t")))
3296 (end (point)))
3297 (list 'subscript
3298 (list :begin begin
3299 :end end
3300 :use-brackets-p bracketsp
3301 :contents-begin contents-begin
3302 :contents-end contents-end
3303 :post-blank post-blank)))))
3305 (defun org-element-subscript-interpreter (subscript contents)
3306 "Interpret SUBSCRIPT object as Org syntax.
3307 CONTENTS is the contents of the object."
3308 (format
3309 (if (org-element-property :use-brackets-p subscript) "_{%s}" "_%s")
3310 contents))
3312 (defun org-element-sub/superscript-successor (limit)
3313 "Search for the next sub/superscript object.
3315 LIMIT bounds the search.
3317 Return value is a cons cell whose CAR is either `subscript' or
3318 `superscript' and CDR is beginning position."
3319 (save-excursion
3320 (unless (bolp) (backward-char))
3321 (when (re-search-forward org-match-substring-regexp limit t)
3322 (cons (if (string= (match-string 2) "_") 'subscript 'superscript)
3323 (match-beginning 2)))))
3326 ;;;; Superscript
3328 (defun org-element-superscript-parser ()
3329 "Parse superscript at point.
3331 Return a list whose CAR is `superscript' and CDR a plist with
3332 `:begin', `:end', `:contents-begin', `:contents-end',
3333 `:use-brackets-p' and `:post-blank' as keywords.
3335 Assume point is at the caret."
3336 (save-excursion
3337 (unless (bolp) (backward-char))
3338 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp) t
3339 (not (looking-at org-match-substring-regexp))))
3340 (begin (match-beginning 2))
3341 (contents-begin (or (match-beginning 5)
3342 (match-beginning 3)))
3343 (contents-end (or (match-end 5) (match-end 3)))
3344 (post-blank (progn (goto-char (match-end 0))
3345 (skip-chars-forward " \t")))
3346 (end (point)))
3347 (list 'superscript
3348 (list :begin begin
3349 :end end
3350 :use-brackets-p bracketsp
3351 :contents-begin contents-begin
3352 :contents-end contents-end
3353 :post-blank post-blank)))))
3355 (defun org-element-superscript-interpreter (superscript contents)
3356 "Interpret SUPERSCRIPT object as Org syntax.
3357 CONTENTS is the contents of the object."
3358 (format
3359 (if (org-element-property :use-brackets-p superscript) "^{%s}" "^%s")
3360 contents))
3363 ;;;; Table Cell
3365 (defun org-element-table-cell-parser ()
3366 "Parse table cell at point.
3368 Return a list whose CAR is `table-cell' and CDR is a plist
3369 containing `:begin', `:end', `:contents-begin', `:contents-end'
3370 and `:post-blank' keywords."
3371 (looking-at "[ \t]*\\(.*?\\)[ \t]*|")
3372 (let* ((begin (match-beginning 0))
3373 (end (match-end 0))
3374 (contents-begin (match-beginning 1))
3375 (contents-end (match-end 1)))
3376 (list 'table-cell
3377 (list :begin begin
3378 :end end
3379 :contents-begin contents-begin
3380 :contents-end contents-end
3381 :post-blank 0))))
3383 (defun org-element-table-cell-interpreter (table-cell contents)
3384 "Interpret TABLE-CELL element as Org syntax.
3385 CONTENTS is the contents of the cell, or nil."
3386 (concat " " contents " |"))
3388 (defun org-element-table-cell-successor (limit)
3389 "Search for the next table-cell object.
3391 LIMIT bounds the search.
3393 Return value is a cons cell whose CAR is `table-cell' and CDR is
3394 beginning position."
3395 (when (looking-at "[ \t]*.*?[ \t]+|") (cons 'table-cell (point))))
3398 ;;;; Target
3400 (defun org-element-target-parser ()
3401 "Parse target at point.
3403 Return a list whose CAR is `target' and CDR a plist with
3404 `:begin', `:end', `:value' and `:post-blank' as keywords.
3406 Assume point is at the target."
3407 (save-excursion
3408 (looking-at org-target-regexp)
3409 (let ((begin (point))
3410 (value (org-match-string-no-properties 1))
3411 (post-blank (progn (goto-char (match-end 0))
3412 (skip-chars-forward " \t")))
3413 (end (point)))
3414 (list 'target
3415 (list :begin begin
3416 :end end
3417 :value value
3418 :post-blank post-blank)))))
3420 (defun org-element-target-interpreter (target contents)
3421 "Interpret TARGET object as Org syntax.
3422 CONTENTS is nil."
3423 (format "<<%s>>" (org-element-property :value target)))
3425 (defun org-element-target-successor (limit)
3426 "Search for the next target object.
3428 LIMIT bounds the search.
3430 Return value is a cons cell whose CAR is `target' and CDR is
3431 beginning position."
3432 (save-excursion
3433 (when (re-search-forward org-target-regexp limit t)
3434 (cons 'target (match-beginning 0)))))
3437 ;;;; Timestamp
3439 (defun org-element-timestamp-parser ()
3440 "Parse time stamp at point.
3442 Return a list whose CAR is `timestamp', and CDR a plist with
3443 `:type', `:begin', `:end', `:value' and `:post-blank' keywords.
3445 Assume point is at the beginning of the timestamp."
3446 (save-excursion
3447 (let* ((begin (point))
3448 (activep (eq (char-after) ?<))
3449 (raw-value
3450 (progn
3451 (looking-at "\\([<[]\\(%%\\)?.*?\\)[]>]\\(?:--\\([<[].*?[]>]\\)\\)?")
3452 (match-string-no-properties 0)))
3453 (date-start (match-string-no-properties 1))
3454 (date-end (match-string 3))
3455 (diaryp (match-beginning 2))
3456 (post-blank (progn (goto-char (match-end 0))
3457 (skip-chars-forward " \t")))
3458 (end (point))
3459 (time-range
3460 (and (not diaryp)
3461 (string-match
3462 "[012]?[0-9]:[0-5][0-9]\\(-\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)"
3463 date-start)
3464 (cons (string-to-number (match-string 2 date-start))
3465 (string-to-number (match-string 3 date-start)))))
3466 (type (cond (diaryp 'diary)
3467 ((and activep (or date-end time-range)) 'active-range)
3468 (activep 'active)
3469 ((or date-end time-range) 'inactive-range)
3470 (t 'inactive)))
3471 (repeater-props
3472 (and (not diaryp)
3473 (string-match "\\([.+]?\\+\\)\\([0-9]+\\)\\([hdwmy]\\)>"
3474 raw-value)
3475 (list
3476 :repeater-type
3477 (let ((type (match-string 1 raw-value)))
3478 (cond ((equal "++" type) 'catch-up)
3479 ((equal ".+" type) 'restart)
3480 (t 'cumulate)))
3481 :repeater-value (string-to-number (match-string 2 raw-value))
3482 :repeater-unit
3483 (case (string-to-char (match-string 3 raw-value))
3484 (?h 'hour) (?d 'day) (?w 'week) (?m 'month) (t 'year)))))
3485 year-start month-start day-start hour-start minute-start year-end
3486 month-end day-end hour-end minute-end)
3487 ;; Parse date-start.
3488 (unless diaryp
3489 (let ((date (org-parse-time-string date-start t)))
3490 (setq year-start (nth 5 date)
3491 month-start (nth 4 date)
3492 day-start (nth 3 date)
3493 hour-start (nth 2 date)
3494 minute-start (nth 1 date))))
3495 ;; Compute date-end. It can be provided directly in time-stamp,
3496 ;; or extracted from time range. Otherwise, it defaults to the
3497 ;; same values as date-start.
3498 (unless diaryp
3499 (let ((date (and date-end (org-parse-time-string date-end t))))
3500 (setq year-end (or (nth 5 date) year-start)
3501 month-end (or (nth 4 date) month-start)
3502 day-end (or (nth 3 date) day-start)
3503 hour-end (or (nth 2 date) (car time-range) hour-start)
3504 minute-end (or (nth 1 date) (cdr time-range) minute-start))))
3505 (list 'timestamp
3506 (nconc (list :type type
3507 :raw-value raw-value
3508 :year-start year-start
3509 :month-start month-start
3510 :day-start day-start
3511 :hour-start hour-start
3512 :minute-start minute-start
3513 :year-end year-end
3514 :month-end month-end
3515 :day-end day-end
3516 :hour-end hour-end
3517 :minute-end minute-end
3518 :begin begin
3519 :end end
3520 :post-blank post-blank)
3521 repeater-props)))))
3523 (defun org-element-timestamp-interpreter (timestamp contents)
3524 "Interpret TIMESTAMP object as Org syntax.
3525 CONTENTS is nil."
3526 ;; Use `:raw-value' if specified.
3527 (or (org-element-property :raw-value timestamp)
3528 ;; Otherwise, build timestamp string.
3529 (let* ((repeat-string
3530 (concat
3531 (case (org-element-property :repeater-type timestamp)
3532 (cumulate "+") (catch-up "++") (restart ".+"))
3533 (let ((val (org-element-property :repeater-value timestamp)))
3534 (and val (number-to-string val)))
3535 (case (org-element-property :repeater-unit timestamp)
3536 (hour "h") (day "d") (week "w") (month "m") (year "y"))))
3537 (build-ts-string
3538 ;; Build an Org timestamp string from TIME. ACTIVEP is
3539 ;; non-nil when time stamp is active. If WITH-TIME-P is
3540 ;; non-nil, add a time part. HOUR-END and MINUTE-END
3541 ;; specify a time range in the timestamp. REPEAT-STRING
3542 ;; is the repeater string, if any.
3543 (lambda (time activep &optional with-time-p hour-end minute-end)
3544 (let ((ts (format-time-string
3545 (funcall (if with-time-p 'cdr 'car)
3546 org-time-stamp-formats)
3547 time)))
3548 (when (and hour-end minute-end)
3549 (string-match "[012]?[0-9]:[0-5][0-9]" ts)
3550 (setq ts
3551 (replace-match
3552 (format "\\&-%02d:%02d" hour-end minute-end)
3553 nil nil ts)))
3554 (unless activep (setq ts (format "[%s]" (substring ts 1 -1))))
3555 (when (org-string-nw-p repeat-string)
3556 (setq ts (concat (substring ts 0 -1)
3558 repeat-string
3559 (substring ts -1))))
3560 ;; Return value.
3561 ts)))
3562 (type (org-element-property :type timestamp)))
3563 (case type
3564 ((active inactive)
3565 (let* ((minute-start (org-element-property :minute-start timestamp))
3566 (minute-end (org-element-property :minute-end timestamp))
3567 (hour-start (org-element-property :hour-start timestamp))
3568 (hour-end (org-element-property :hour-end timestamp))
3569 (time-range-p (and hour-start hour-end minute-start minute-end
3570 (or (/= hour-start hour-end)
3571 (/= minute-start minute-end)))))
3572 (funcall
3573 build-ts-string
3574 (encode-time 0
3575 (or minute-start 0)
3576 (or hour-start 0)
3577 (org-element-property :day-start timestamp)
3578 (org-element-property :month-start timestamp)
3579 (org-element-property :year-start timestamp))
3580 (eq type 'active)
3581 (and hour-start minute-start)
3582 (and time-range-p hour-end)
3583 (and time-range-p minute-end))))
3584 ((active-range inactive-range)
3585 (let ((minute-start (org-element-property :minute-start timestamp))
3586 (minute-end (org-element-property :minute-end timestamp))
3587 (hour-start (org-element-property :hour-start timestamp))
3588 (hour-end (org-element-property :hour-end timestamp)))
3589 (concat
3590 (funcall
3591 build-ts-string (encode-time
3593 (or minute-start 0)
3594 (or hour-start 0)
3595 (org-element-property :day-start timestamp)
3596 (org-element-property :month-start timestamp)
3597 (org-element-property :year-start timestamp))
3598 (eq type 'active-range)
3599 (and hour-start minute-start))
3600 "--"
3601 (funcall build-ts-string
3602 (encode-time 0
3603 (or minute-end 0)
3604 (or hour-end 0)
3605 (org-element-property :day-end timestamp)
3606 (org-element-property :month-end timestamp)
3607 (org-element-property :year-end timestamp))
3608 (eq type 'active-range)
3609 (and hour-end minute-end)))))))))
3611 (defun org-element-timestamp-successor (limit)
3612 "Search for the next timestamp object.
3614 LIMIT bounds the search.
3616 Return value is a cons cell whose CAR is `timestamp' and CDR is
3617 beginning position."
3618 (save-excursion
3619 (when (re-search-forward
3620 (concat org-ts-regexp-both
3621 "\\|"
3622 "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
3623 "\\|"
3624 "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
3625 limit t)
3626 (cons 'timestamp (match-beginning 0)))))
3629 ;;;; Underline
3631 (defun org-element-underline-parser ()
3632 "Parse underline object at point.
3634 Return a list whose CAR is `underline' and CDR is a plist with
3635 `:begin', `:end', `:contents-begin' and `:contents-end' and
3636 `:post-blank' keywords.
3638 Assume point is at the first underscore marker."
3639 (save-excursion
3640 (unless (bolp) (backward-char 1))
3641 (looking-at org-emph-re)
3642 (let ((begin (match-beginning 2))
3643 (contents-begin (match-beginning 4))
3644 (contents-end (match-end 4))
3645 (post-blank (progn (goto-char (match-end 2))
3646 (skip-chars-forward " \t")))
3647 (end (point)))
3648 (list 'underline
3649 (list :begin begin
3650 :end end
3651 :contents-begin contents-begin
3652 :contents-end contents-end
3653 :post-blank post-blank)))))
3655 (defun org-element-underline-interpreter (underline contents)
3656 "Interpret UNDERLINE object as Org syntax.
3657 CONTENTS is the contents of the object."
3658 (format "_%s_" contents))
3661 ;;;; Verbatim
3663 (defun org-element-verbatim-parser ()
3664 "Parse verbatim object at point.
3666 Return a list whose CAR is `verbatim' and CDR is a plist with
3667 `:value', `:begin', `:end' and `:post-blank' keywords.
3669 Assume point is at the first equal sign marker."
3670 (save-excursion
3671 (unless (bolp) (backward-char 1))
3672 (looking-at org-emph-re)
3673 (let ((begin (match-beginning 2))
3674 (value (org-match-string-no-properties 4))
3675 (post-blank (progn (goto-char (match-end 2))
3676 (skip-chars-forward " \t")))
3677 (end (point)))
3678 (list 'verbatim
3679 (list :value value
3680 :begin begin
3681 :end end
3682 :post-blank post-blank)))))
3684 (defun org-element-verbatim-interpreter (verbatim contents)
3685 "Interpret VERBATIM object as Org syntax.
3686 CONTENTS is nil."
3687 (format "=%s=" (org-element-property :value verbatim)))
3691 ;;; Parsing Element Starting At Point
3693 ;; `org-element--current-element' is the core function of this section.
3694 ;; It returns the Lisp representation of the element starting at
3695 ;; point.
3697 ;; `org-element--current-element' makes use of special modes. They
3698 ;; are activated for fixed element chaining (i.e. `plain-list' >
3699 ;; `item') or fixed conditional element chaining (i.e. `headline' >
3700 ;; `section'). Special modes are: `first-section', `item',
3701 ;; `node-property', `quote-section', `section' and `table-row'.
3703 (defun org-element--current-element
3704 (limit &optional granularity special structure)
3705 "Parse the element starting at point.
3707 LIMIT bounds the search.
3709 Return value is a list like (TYPE PROPS) where TYPE is the type
3710 of the element and PROPS a plist of properties associated to the
3711 element.
3713 Possible types are defined in `org-element-all-elements'.
3715 Optional argument GRANULARITY determines the depth of the
3716 recursion. Allowed values are `headline', `greater-element',
3717 `element', `object' or nil. When it is broader than `object' (or
3718 nil), secondary values will not be parsed, since they only
3719 contain objects.
3721 Optional argument SPECIAL, when non-nil, can be either
3722 `first-section', `item', `node-property', `quote-section',
3723 `section', and `table-row'.
3725 If STRUCTURE isn't provided but SPECIAL is set to `item', it will
3726 be computed.
3728 This function assumes point is always at the beginning of the
3729 element it has to parse."
3730 (save-excursion
3731 (let ((case-fold-search t)
3732 ;; Determine if parsing depth allows for secondary strings
3733 ;; parsing. It only applies to elements referenced in
3734 ;; `org-element-secondary-value-alist'.
3735 (raw-secondary-p (and granularity (not (eq granularity 'object)))))
3736 (cond
3737 ;; Item.
3738 ((eq special 'item)
3739 (org-element-item-parser limit structure raw-secondary-p))
3740 ;; Table Row.
3741 ((eq special 'table-row) (org-element-table-row-parser limit))
3742 ;; Node Property.
3743 ((eq special 'node-property) (org-element-node-property-parser limit))
3744 ;; Headline.
3745 ((org-with-limited-levels (org-at-heading-p))
3746 (org-element-headline-parser limit raw-secondary-p))
3747 ;; Sections (must be checked after headline).
3748 ((eq special 'section) (org-element-section-parser limit))
3749 ((eq special 'quote-section) (org-element-quote-section-parser limit))
3750 ((eq special 'first-section)
3751 (org-element-section-parser
3752 (or (save-excursion (org-with-limited-levels (outline-next-heading)))
3753 limit)))
3754 ;; When not at bol, point is at the beginning of an item or
3755 ;; a footnote definition: next item is always a paragraph.
3756 ((not (bolp)) (org-element-paragraph-parser limit (list (point))))
3757 ;; Planning and Clock.
3758 ((looking-at org-planning-or-clock-line-re)
3759 (if (equal (match-string 1) org-clock-string)
3760 (org-element-clock-parser limit)
3761 (org-element-planning-parser limit)))
3762 ;; Inlinetask.
3763 ((org-at-heading-p)
3764 (org-element-inlinetask-parser limit raw-secondary-p))
3765 ;; From there, elements can have affiliated keywords.
3766 (t (let ((affiliated (org-element--collect-affiliated-keywords limit)))
3767 (cond
3768 ;; Jumping over affiliated keywords put point off-limits.
3769 ;; Parse them as regular keywords.
3770 ((>= (point) limit)
3771 (goto-char (car affiliated))
3772 (org-element-keyword-parser limit nil))
3773 ;; LaTeX Environment.
3774 ((looking-at "[ \t]*\\\\begin{\\([A-Za-z0-9*]+\\)}[ \t]*$")
3775 (org-element-latex-environment-parser limit affiliated))
3776 ;; Drawer and Property Drawer.
3777 ((looking-at org-drawer-regexp)
3778 (if (equal (match-string 1) "PROPERTIES")
3779 (org-element-property-drawer-parser limit affiliated)
3780 (org-element-drawer-parser limit affiliated)))
3781 ;; Fixed Width
3782 ((looking-at "[ \t]*:\\( \\|$\\)")
3783 (org-element-fixed-width-parser limit affiliated))
3784 ;; Inline Comments, Blocks, Babel Calls, Dynamic Blocks and
3785 ;; Keywords.
3786 ((looking-at "[ \t]*#")
3787 (goto-char (match-end 0))
3788 (cond ((looking-at "\\(?: \\|$\\)")
3789 (beginning-of-line)
3790 (org-element-comment-parser limit affiliated))
3791 ((looking-at "\\+BEGIN_\\(\\S-+\\)")
3792 (beginning-of-line)
3793 (let ((parser (assoc (upcase (match-string 1))
3794 org-element-block-name-alist)))
3795 (if parser (funcall (cdr parser) limit affiliated)
3796 (org-element-special-block-parser limit affiliated))))
3797 ((looking-at "\\+CALL:")
3798 (beginning-of-line)
3799 (org-element-babel-call-parser limit affiliated))
3800 ((looking-at "\\+BEGIN:? ")
3801 (beginning-of-line)
3802 (org-element-dynamic-block-parser limit affiliated))
3803 ((looking-at "\\+\\S-+:")
3804 (beginning-of-line)
3805 (org-element-keyword-parser limit affiliated))
3807 (beginning-of-line)
3808 (org-element-paragraph-parser limit affiliated))))
3809 ;; Footnote Definition.
3810 ((looking-at org-footnote-definition-re)
3811 (org-element-footnote-definition-parser limit affiliated))
3812 ;; Horizontal Rule.
3813 ((looking-at "[ \t]*-\\{5,\\}[ \t]*$")
3814 (org-element-horizontal-rule-parser limit affiliated))
3815 ;; Diary Sexp.
3816 ((looking-at "%%(")
3817 (org-element-diary-sexp-parser limit affiliated))
3818 ;; Table.
3819 ((org-at-table-p t) (org-element-table-parser limit affiliated))
3820 ;; List.
3821 ((looking-at (org-item-re))
3822 (org-element-plain-list-parser
3823 limit affiliated (or structure (org-list-struct))))
3824 ;; Default element: Paragraph.
3825 (t (org-element-paragraph-parser limit affiliated)))))))))
3828 ;; Most elements can have affiliated keywords. When looking for an
3829 ;; element beginning, we want to move before them, as they belong to
3830 ;; that element, and, in the meantime, collect information they give
3831 ;; into appropriate properties. Hence the following function.
3833 (defun org-element--collect-affiliated-keywords (limit)
3834 "Collect affiliated keywords from point down to LIMIT.
3836 Return a list whose CAR is the position at the first of them and
3837 CDR a plist of keywords and values and move point to the
3838 beginning of the first line after them.
3840 As a special case, if element doesn't start at the beginning of
3841 the line (i.e. a paragraph starting an item), CAR is current
3842 position of point and CDR is nil."
3843 (if (not (bolp)) (list (point))
3844 (let ((case-fold-search t)
3845 (origin (point))
3846 ;; RESTRICT is the list of objects allowed in parsed
3847 ;; keywords value.
3848 (restrict (org-element-restriction 'keyword))
3849 output)
3850 (while (and (< (point) limit) (looking-at org-element--affiliated-re))
3851 (let* ((raw-kwd (upcase (match-string 1)))
3852 ;; Apply translation to RAW-KWD. From there, KWD is
3853 ;; the official keyword.
3854 (kwd (or (cdr (assoc raw-kwd
3855 org-element-keyword-translation-alist))
3856 raw-kwd))
3857 ;; Find main value for any keyword.
3858 (value
3859 (save-match-data
3860 (org-trim
3861 (buffer-substring-no-properties
3862 (match-end 0) (point-at-eol)))))
3863 ;; PARSEDP is non-nil when keyword should have its
3864 ;; value parsed.
3865 (parsedp (member kwd org-element-parsed-keywords))
3866 ;; If KWD is a dual keyword, find its secondary
3867 ;; value. Maybe parse it.
3868 (dualp (member kwd org-element-dual-keywords))
3869 (dual-value
3870 (and dualp
3871 (let ((sec (org-match-string-no-properties 2)))
3872 (if (or (not sec) (not parsedp)) sec
3873 (org-element-parse-secondary-string sec restrict)))))
3874 ;; Attribute a property name to KWD.
3875 (kwd-sym (and kwd (intern (concat ":" (downcase kwd))))))
3876 ;; Now set final shape for VALUE.
3877 (when parsedp
3878 (setq value (org-element-parse-secondary-string value restrict)))
3879 (when dualp
3880 (setq value (and (or value dual-value) (cons value dual-value))))
3881 (when (or (member kwd org-element-multiple-keywords)
3882 ;; Attributes can always appear on multiple lines.
3883 (string-match "^ATTR_" kwd))
3884 (setq value (cons value (plist-get output kwd-sym))))
3885 ;; Eventually store the new value in OUTPUT.
3886 (setq output (plist-put output kwd-sym value))
3887 ;; Move to next keyword.
3888 (forward-line)))
3889 ;; If affiliated keywords are orphaned: move back to first one.
3890 ;; They will be parsed as a paragraph.
3891 (when (looking-at "[ \t]*$") (goto-char origin) (setq output nil))
3892 ;; Return value.
3893 (cons origin output))))
3897 ;;; The Org Parser
3899 ;; The two major functions here are `org-element-parse-buffer', which
3900 ;; parses Org syntax inside the current buffer, taking into account
3901 ;; region, narrowing, or even visibility if specified, and
3902 ;; `org-element-parse-secondary-string', which parses objects within
3903 ;; a given string.
3905 ;; The (almost) almighty `org-element-map' allows to apply a function
3906 ;; on elements or objects matching some type, and accumulate the
3907 ;; resulting values. In an export situation, it also skips unneeded
3908 ;; parts of the parse tree.
3910 (defun org-element-parse-buffer (&optional granularity visible-only)
3911 "Recursively parse the buffer and return structure.
3912 If narrowing is in effect, only parse the visible part of the
3913 buffer.
3915 Optional argument GRANULARITY determines the depth of the
3916 recursion. It can be set to the following symbols:
3918 `headline' Only parse headlines.
3919 `greater-element' Don't recurse into greater elements excepted
3920 headlines and sections. Thus, elements
3921 parsed are the top-level ones.
3922 `element' Parse everything but objects and plain text.
3923 `object' Parse the complete buffer (default).
3925 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3926 elements.
3928 An element or an objects is represented as a list with the
3929 pattern (TYPE PROPERTIES CONTENTS), where :
3931 TYPE is a symbol describing the element or object. See
3932 `org-element-all-elements' and `org-element-all-objects' for an
3933 exhaustive list of such symbols. One can retrieve it with
3934 `org-element-type' function.
3936 PROPERTIES is the list of attributes attached to the element or
3937 object, as a plist. Although most of them are specific to the
3938 element or object type, all types share `:begin', `:end',
3939 `:post-blank' and `:parent' properties, which respectively
3940 refer to buffer position where the element or object starts,
3941 ends, the number of white spaces or blank lines after it, and
3942 the element or object containing it. Properties values can be
3943 obtained by using `org-element-property' function.
3945 CONTENTS is a list of elements, objects or raw strings
3946 contained in the current element or object, when applicable.
3947 One can access them with `org-element-contents' function.
3949 The Org buffer has `org-data' as type and nil as properties.
3950 `org-element-map' function can be used to find specific elements
3951 or objects within the parse tree.
3953 This function assumes that current major mode is `org-mode'."
3954 (save-excursion
3955 (goto-char (point-min))
3956 (org-skip-whitespace)
3957 (org-element--parse-elements
3958 (point-at-bol) (point-max)
3959 ;; Start in `first-section' mode so text before the first
3960 ;; headline belongs to a section.
3961 'first-section nil granularity visible-only (list 'org-data nil))))
3963 (defun org-element-parse-secondary-string (string restriction &optional parent)
3964 "Recursively parse objects in STRING and return structure.
3966 RESTRICTION is a symbol limiting the object types that will be
3967 looked after.
3969 Optional argument PARENT, when non-nil, is the element or object
3970 containing the secondary string. It is used to set correctly
3971 `:parent' property within the string."
3972 ;; Copy buffer-local variables listed in
3973 ;; `org-element-object-variables' into temporary buffer. This is
3974 ;; required since object parsing is dependent on these variables.
3975 (let ((pairs (delq nil (mapcar (lambda (var)
3976 (when (boundp var)
3977 (cons var (symbol-value var))))
3978 org-element-object-variables))))
3979 (with-temp-buffer
3980 (mapc (lambda (pair) (org-set-local (car pair) (cdr pair))) pairs)
3981 (insert string)
3982 (let ((secondary (org-element--parse-objects
3983 (point-min) (point-max) nil restriction)))
3984 (when parent
3985 (mapc (lambda (obj) (org-element-put-property obj :parent parent))
3986 secondary))
3987 secondary))))
3989 (defun org-element-map
3990 (data types fun &optional info first-match no-recursion with-affiliated)
3991 "Map a function on selected elements or objects.
3993 DATA is an Org buffer parse tree, as returned by, i.e.,
3994 `org-element-parse-buffer'. TYPES is a symbol or list of symbols
3995 of elements or objects types (see `org-element-all-elements' and
3996 `org-element-all-objects' for a complete list of types). FUN is
3997 the function called on the matching element or object. It has to
3998 accept one argument: the element or object itself.
4000 When optional argument INFO is non-nil, it should be a plist
4001 holding export options. In that case, parts of the parse tree
4002 not exportable according to that property list will be skipped.
4004 When optional argument FIRST-MATCH is non-nil, stop at the first
4005 match for which FUN doesn't return nil, and return that value.
4007 Optional argument NO-RECURSION is a symbol or a list of symbols
4008 representing elements or objects types. `org-element-map' won't
4009 enter any recursive element or object whose type belongs to that
4010 list. Though, FUN can still be applied on them.
4012 When optional argument WITH-AFFILIATED is non-nil, FUN will also
4013 apply to matching objects within parsed affiliated keywords (see
4014 `org-element-parsed-keywords').
4016 Nil values returned from FUN do not appear in the results.
4019 Examples:
4020 --------
4022 Assuming TREE is a variable containing an Org buffer parse tree,
4023 the following example will return a flat list of all `src-block'
4024 and `example-block' elements in it:
4026 \(org-element-map tree '(example-block src-block) 'identity)
4028 The following snippet will find the first headline with a level
4029 of 1 and a \"phone\" tag, and will return its beginning position:
4031 \(org-element-map tree 'headline
4032 \(lambda (hl)
4033 \(and (= (org-element-property :level hl) 1)
4034 \(member \"phone\" (org-element-property :tags hl))
4035 \(org-element-property :begin hl)))
4036 nil t)
4038 The next example will return a flat list of all `plain-list' type
4039 elements in TREE that are not a sub-list themselves:
4041 \(org-element-map tree 'plain-list 'identity nil nil 'plain-list)
4043 Eventually, this example will return a flat list of all `bold'
4044 type objects containing a `latex-snippet' type object, even
4045 looking into captions:
4047 \(org-element-map tree 'bold
4048 \(lambda (b)
4049 \(and (org-element-map b 'latex-snippet 'identity nil t) b))
4050 nil nil nil t)"
4051 ;; Ensure TYPES and NO-RECURSION are a list, even of one element.
4052 (unless (listp types) (setq types (list types)))
4053 (unless (listp no-recursion) (setq no-recursion (list no-recursion)))
4054 ;; Recursion depth is determined by --CATEGORY.
4055 (let* ((--category
4056 (catch 'found
4057 (let ((category 'greater-elements))
4058 (mapc (lambda (type)
4059 (cond ((or (memq type org-element-all-objects)
4060 (eq type 'plain-text))
4061 ;; If one object is found, the function
4062 ;; has to recurse into every object.
4063 (throw 'found 'objects))
4064 ((not (memq type org-element-greater-elements))
4065 ;; If one regular element is found, the
4066 ;; function has to recurse, at least,
4067 ;; into every element it encounters.
4068 (and (not (eq category 'elements))
4069 (setq category 'elements)))))
4070 types)
4071 category)))
4072 ;; Compute properties for affiliated keywords if necessary.
4073 (--affiliated-alist
4074 (and with-affiliated
4075 (mapcar (lambda (kwd)
4076 (cons kwd (intern (concat ":" (downcase kwd)))))
4077 org-element-affiliated-keywords)))
4078 --acc
4079 --walk-tree
4080 (--walk-tree
4081 (function
4082 (lambda (--data)
4083 ;; Recursively walk DATA. INFO, if non-nil, is a plist
4084 ;; holding contextual information.
4085 (let ((--type (org-element-type --data)))
4086 (cond
4087 ((not --data))
4088 ;; Ignored element in an export context.
4089 ((and info (memq --data (plist-get info :ignore-list))))
4090 ;; Secondary string: only objects can be found there.
4091 ((not --type)
4092 (when (eq --category 'objects) (mapc --walk-tree --data)))
4093 ;; Unconditionally enter parse trees.
4094 ((eq --type 'org-data)
4095 (mapc --walk-tree (org-element-contents --data)))
4097 ;; Check if TYPE is matching among TYPES. If so,
4098 ;; apply FUN to --DATA and accumulate return value
4099 ;; into --ACC (or exit if FIRST-MATCH is non-nil).
4100 (when (memq --type types)
4101 (let ((result (funcall fun --data)))
4102 (cond ((not result))
4103 (first-match (throw '--map-first-match result))
4104 (t (push result --acc)))))
4105 ;; If --DATA has a secondary string that can contain
4106 ;; objects with their type among TYPES, look into it.
4107 (when (and (eq --category 'objects) (not (stringp --data)))
4108 (let ((sec-prop
4109 (assq --type org-element-secondary-value-alist)))
4110 (when sec-prop
4111 (funcall --walk-tree
4112 (org-element-property (cdr sec-prop) --data)))))
4113 ;; If --DATA has any affiliated keywords and
4114 ;; WITH-AFFILIATED is non-nil, look for objects in
4115 ;; them.
4116 (when (and with-affiliated
4117 (eq --category 'objects)
4118 (memq --type org-element-all-elements))
4119 (mapc (lambda (kwd-pair)
4120 (let ((kwd (car kwd-pair))
4121 (value (org-element-property
4122 (cdr kwd-pair) --data)))
4123 ;; Pay attention to the type of value.
4124 ;; Preserve order for multiple keywords.
4125 (cond
4126 ((not value))
4127 ((and (member kwd org-element-multiple-keywords)
4128 (member kwd org-element-dual-keywords))
4129 (mapc (lambda (line)
4130 (funcall --walk-tree (cdr line))
4131 (funcall --walk-tree (car line)))
4132 (reverse value)))
4133 ((member kwd org-element-multiple-keywords)
4134 (mapc (lambda (line) (funcall --walk-tree line))
4135 (reverse value)))
4136 ((member kwd org-element-dual-keywords)
4137 (funcall --walk-tree (cdr value))
4138 (funcall --walk-tree (car value)))
4139 (t (funcall --walk-tree value)))))
4140 --affiliated-alist))
4141 ;; Determine if a recursion into --DATA is possible.
4142 (cond
4143 ;; --TYPE is explicitly removed from recursion.
4144 ((memq --type no-recursion))
4145 ;; --DATA has no contents.
4146 ((not (org-element-contents --data)))
4147 ;; Looking for greater elements but --DATA is simply
4148 ;; an element or an object.
4149 ((and (eq --category 'greater-elements)
4150 (not (memq --type org-element-greater-elements))))
4151 ;; Looking for elements but --DATA is an object.
4152 ((and (eq --category 'elements)
4153 (memq --type org-element-all-objects)))
4154 ;; In any other case, map contents.
4155 (t (mapc --walk-tree (org-element-contents --data)))))))))))
4156 (catch '--map-first-match
4157 (funcall --walk-tree data)
4158 ;; Return value in a proper order.
4159 (nreverse --acc))))
4160 (put 'org-element-map 'lisp-indent-function 2)
4162 ;; The following functions are internal parts of the parser.
4164 ;; The first one, `org-element--parse-elements' acts at the element's
4165 ;; level.
4167 ;; The second one, `org-element--parse-objects' applies on all objects
4168 ;; of a paragraph or a secondary string. It uses
4169 ;; `org-element--get-next-object-candidates' to optimize the search of
4170 ;; the next object in the buffer.
4172 ;; More precisely, that function looks for every allowed object type
4173 ;; first. Then, it discards failed searches, keeps further matches,
4174 ;; and searches again types matched behind point, for subsequent
4175 ;; calls. Thus, searching for a given type fails only once, and every
4176 ;; object is searched only once at top level (but sometimes more for
4177 ;; nested types).
4179 (defun org-element--parse-elements
4180 (beg end special structure granularity visible-only acc)
4181 "Parse elements between BEG and END positions.
4183 SPECIAL prioritize some elements over the others. It can be set
4184 to `first-section', `quote-section', `section' `item' or
4185 `table-row'.
4187 When value is `item', STRUCTURE will be used as the current list
4188 structure.
4190 GRANULARITY determines the depth of the recursion. See
4191 `org-element-parse-buffer' for more information.
4193 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
4194 elements.
4196 Elements are accumulated into ACC."
4197 (save-excursion
4198 (goto-char beg)
4199 ;; When parsing only headlines, skip any text before first one.
4200 (when (and (eq granularity 'headline) (not (org-at-heading-p)))
4201 (org-with-limited-levels (outline-next-heading)))
4202 ;; Main loop start.
4203 (while (< (point) end)
4204 ;; Find current element's type and parse it accordingly to
4205 ;; its category.
4206 (let* ((element (org-element--current-element
4207 end granularity special structure))
4208 (type (org-element-type element))
4209 (cbeg (org-element-property :contents-begin element)))
4210 (goto-char (org-element-property :end element))
4211 ;; Fill ELEMENT contents by side-effect.
4212 (cond
4213 ;; If VISIBLE-ONLY is true and element is hidden or if it has
4214 ;; no contents, don't modify it.
4215 ((or (and visible-only (org-element-property :hiddenp element))
4216 (not cbeg)))
4217 ;; Greater element: parse it between `contents-begin' and
4218 ;; `contents-end'. Make sure GRANULARITY allows the
4219 ;; recursion, or ELEMENT is an headline, in which case going
4220 ;; inside is mandatory, in order to get sub-level headings.
4221 ((and (memq type org-element-greater-elements)
4222 (or (memq granularity '(element object nil))
4223 (and (eq granularity 'greater-element)
4224 (eq type 'section))
4225 (eq type 'headline)))
4226 (org-element--parse-elements
4227 cbeg (org-element-property :contents-end element)
4228 ;; Possibly switch to a special mode.
4229 (case type
4230 (headline
4231 (if (org-element-property :quotedp element) 'quote-section
4232 'section))
4233 (plain-list 'item)
4234 (property-drawer 'node-property)
4235 (table 'table-row))
4236 (and (memq type '(item plain-list))
4237 (org-element-property :structure element))
4238 granularity visible-only element))
4239 ;; ELEMENT has contents. Parse objects inside, if
4240 ;; GRANULARITY allows it.
4241 ((memq granularity '(object nil))
4242 (org-element--parse-objects
4243 cbeg (org-element-property :contents-end element) element
4244 (org-element-restriction type))))
4245 (org-element-adopt-elements acc element)))
4246 ;; Return result.
4247 acc))
4249 (defun org-element--parse-objects (beg end acc restriction)
4250 "Parse objects between BEG and END and return recursive structure.
4252 Objects are accumulated in ACC.
4254 RESTRICTION is a list of object types which are allowed in the
4255 current object."
4256 (let (candidates)
4257 (save-excursion
4258 (goto-char beg)
4259 (while (and (< (point) end)
4260 (setq candidates (org-element--get-next-object-candidates
4261 end restriction candidates)))
4262 (let ((next-object
4263 (let ((pos (apply 'min (mapcar 'cdr candidates))))
4264 (save-excursion
4265 (goto-char pos)
4266 (funcall (intern (format "org-element-%s-parser"
4267 (car (rassq pos candidates)))))))))
4268 ;; 1. Text before any object. Untabify it.
4269 (let ((obj-beg (org-element-property :begin next-object)))
4270 (unless (= (point) obj-beg)
4271 (setq acc
4272 (org-element-adopt-elements
4274 (replace-regexp-in-string
4275 "\t" (make-string tab-width ? )
4276 (buffer-substring-no-properties (point) obj-beg))))))
4277 ;; 2. Object...
4278 (let ((obj-end (org-element-property :end next-object))
4279 (cont-beg (org-element-property :contents-begin next-object)))
4280 ;; Fill contents of NEXT-OBJECT by side-effect, if it has
4281 ;; a recursive type.
4282 (when (and cont-beg
4283 (memq (car next-object) org-element-recursive-objects))
4284 (save-restriction
4285 (narrow-to-region
4286 cont-beg
4287 (org-element-property :contents-end next-object))
4288 (org-element--parse-objects
4289 (point-min) (point-max) next-object
4290 (org-element-restriction next-object))))
4291 (setq acc (org-element-adopt-elements acc next-object))
4292 (goto-char obj-end))))
4293 ;; 3. Text after last object. Untabify it.
4294 (unless (= (point) end)
4295 (setq acc
4296 (org-element-adopt-elements
4298 (replace-regexp-in-string
4299 "\t" (make-string tab-width ? )
4300 (buffer-substring-no-properties (point) end)))))
4301 ;; Result.
4302 acc)))
4304 (defun org-element--get-next-object-candidates (limit restriction objects)
4305 "Return an alist of candidates for the next object.
4307 LIMIT bounds the search, and RESTRICTION narrows candidates to
4308 some object types.
4310 Return value is an alist whose CAR is position and CDR the object
4311 type, as a symbol.
4313 OBJECTS is the previous candidates alist."
4314 ;; Filter out any object found but not belonging to RESTRICTION.
4315 (setq objects
4316 (org-remove-if-not
4317 (lambda (obj)
4318 (let ((type (car obj)))
4319 (memq (or (cdr (assq type org-element-object-successor-alist))
4320 type)
4321 restriction)))
4322 objects))
4323 (let (next-candidates types-to-search)
4324 ;; If no previous result, search every object type in RESTRICTION.
4325 ;; Otherwise, keep potential candidates (old objects located after
4326 ;; point) and ask to search again those which had matched before.
4327 (if (not objects) (setq types-to-search restriction)
4328 (mapc (lambda (obj)
4329 (if (< (cdr obj) (point)) (push (car obj) types-to-search)
4330 (push obj next-candidates)))
4331 objects))
4332 ;; Call the appropriate successor function for each type to search
4333 ;; and accumulate matches.
4334 (mapc
4335 (lambda (type)
4336 (let* ((successor-fun
4337 (intern
4338 (format "org-element-%s-successor"
4339 (or (cdr (assq type org-element-object-successor-alist))
4340 type))))
4341 (obj (funcall successor-fun limit)))
4342 (and obj (push obj next-candidates))))
4343 types-to-search)
4344 ;; Return alist.
4345 next-candidates))
4349 ;;; Towards A Bijective Process
4351 ;; The parse tree obtained with `org-element-parse-buffer' is really
4352 ;; a snapshot of the corresponding Org buffer. Therefore, it can be
4353 ;; interpreted and expanded into a string with canonical Org syntax.
4354 ;; Hence `org-element-interpret-data'.
4356 ;; The function relies internally on
4357 ;; `org-element--interpret-affiliated-keywords'.
4359 ;;;###autoload
4360 (defun org-element-interpret-data (data &optional parent)
4361 "Interpret DATA as Org syntax.
4363 DATA is a parse tree, an element, an object or a secondary string
4364 to interpret.
4366 Optional argument PARENT is used for recursive calls. It contains
4367 the element or object containing data, or nil.
4369 Return Org syntax as a string."
4370 (let* ((type (org-element-type data))
4371 (results
4372 (cond
4373 ;; Secondary string.
4374 ((not type)
4375 (mapconcat
4376 (lambda (obj) (org-element-interpret-data obj parent))
4377 data ""))
4378 ;; Full Org document.
4379 ((eq type 'org-data)
4380 (mapconcat
4381 (lambda (obj) (org-element-interpret-data obj parent))
4382 (org-element-contents data) ""))
4383 ;; Plain text: remove `:parent' text property from output.
4384 ((stringp data) (org-no-properties data))
4385 ;; Element/Object without contents.
4386 ((not (org-element-contents data))
4387 (funcall (intern (format "org-element-%s-interpreter" type))
4388 data nil))
4389 ;; Element/Object with contents.
4391 (let* ((greaterp (memq type org-element-greater-elements))
4392 (objectp (and (not greaterp)
4393 (memq type org-element-recursive-objects)))
4394 (contents
4395 (mapconcat
4396 (lambda (obj) (org-element-interpret-data obj data))
4397 (org-element-contents
4398 (if (or greaterp objectp) data
4399 ;; Elements directly containing objects must
4400 ;; have their indentation normalized first.
4401 (org-element-normalize-contents
4402 data
4403 ;; When normalizing first paragraph of an
4404 ;; item or a footnote-definition, ignore
4405 ;; first line's indentation.
4406 (and (eq type 'paragraph)
4407 (equal data (car (org-element-contents parent)))
4408 (memq (org-element-type parent)
4409 '(footnote-definition item))))))
4410 "")))
4411 (funcall (intern (format "org-element-%s-interpreter" type))
4412 data
4413 (if greaterp (org-element-normalize-contents contents)
4414 contents)))))))
4415 (if (memq type '(org-data plain-text nil)) results
4416 ;; Build white spaces. If no `:post-blank' property is
4417 ;; specified, assume its value is 0.
4418 (let ((post-blank (or (org-element-property :post-blank data) 0)))
4419 (if (memq type org-element-all-objects)
4420 (concat results (make-string post-blank 32))
4421 (concat
4422 (org-element--interpret-affiliated-keywords data)
4423 (org-element-normalize-string results)
4424 (make-string post-blank 10)))))))
4426 (defun org-element--interpret-affiliated-keywords (element)
4427 "Return ELEMENT's affiliated keywords as Org syntax.
4428 If there is no affiliated keyword, return the empty string."
4429 (let ((keyword-to-org
4430 (function
4431 (lambda (key value)
4432 (let (dual)
4433 (when (member key org-element-dual-keywords)
4434 (setq dual (cdr value) value (car value)))
4435 (concat "#+" key
4436 (and dual
4437 (format "[%s]" (org-element-interpret-data dual)))
4438 ": "
4439 (if (member key org-element-parsed-keywords)
4440 (org-element-interpret-data value)
4441 value)
4442 "\n"))))))
4443 (mapconcat
4444 (lambda (prop)
4445 (let ((value (org-element-property prop element))
4446 (keyword (upcase (substring (symbol-name prop) 1))))
4447 (when value
4448 (if (or (member keyword org-element-multiple-keywords)
4449 ;; All attribute keywords can have multiple lines.
4450 (string-match "^ATTR_" keyword))
4451 (mapconcat (lambda (line) (funcall keyword-to-org keyword line))
4452 (reverse value)
4454 (funcall keyword-to-org keyword value)))))
4455 ;; List all ELEMENT's properties matching an attribute line or an
4456 ;; affiliated keyword, but ignore translated keywords since they
4457 ;; cannot belong to the property list.
4458 (loop for prop in (nth 1 element) by 'cddr
4459 when (let ((keyword (upcase (substring (symbol-name prop) 1))))
4460 (or (string-match "^ATTR_" keyword)
4461 (and
4462 (member keyword org-element-affiliated-keywords)
4463 (not (assoc keyword
4464 org-element-keyword-translation-alist)))))
4465 collect prop)
4466 "")))
4468 ;; Because interpretation of the parse tree must return the same
4469 ;; number of blank lines between elements and the same number of white
4470 ;; space after objects, some special care must be given to white
4471 ;; spaces.
4473 ;; The first function, `org-element-normalize-string', ensures any
4474 ;; string different from the empty string will end with a single
4475 ;; newline character.
4477 ;; The second function, `org-element-normalize-contents', removes
4478 ;; global indentation from the contents of the current element.
4480 (defun org-element-normalize-string (s)
4481 "Ensure string S ends with a single newline character.
4483 If S isn't a string return it unchanged. If S is the empty
4484 string, return it. Otherwise, return a new string with a single
4485 newline character at its end."
4486 (cond
4487 ((not (stringp s)) s)
4488 ((string= "" s) "")
4489 (t (and (string-match "\\(\n[ \t]*\\)*\\'" s)
4490 (replace-match "\n" nil nil s)))))
4492 (defun org-element-normalize-contents (element &optional ignore-first)
4493 "Normalize plain text in ELEMENT's contents.
4495 ELEMENT must only contain plain text and objects.
4497 If optional argument IGNORE-FIRST is non-nil, ignore first line's
4498 indentation to compute maximal common indentation.
4500 Return the normalized element that is element with global
4501 indentation removed from its contents. The function assumes that
4502 indentation is not done with TAB characters."
4503 (let* (ind-list ; for byte-compiler
4504 collect-inds ; for byte-compiler
4505 (collect-inds
4506 (function
4507 ;; Return list of indentations within BLOB. This is done by
4508 ;; walking recursively BLOB and updating IND-LIST along the
4509 ;; way. FIRST-FLAG is non-nil when the first string hasn't
4510 ;; been seen yet. It is required as this string is the only
4511 ;; one whose indentation doesn't happen after a newline
4512 ;; character.
4513 (lambda (blob first-flag)
4514 (mapc
4515 (lambda (object)
4516 (when (and first-flag (stringp object))
4517 (setq first-flag nil)
4518 (string-match "\\`\\( *\\)" object)
4519 (let ((len (length (match-string 1 object))))
4520 ;; An indentation of zero means no string will be
4521 ;; modified. Quit the process.
4522 (if (zerop len) (throw 'zero (setq ind-list nil))
4523 (push len ind-list))))
4524 (cond
4525 ((stringp object)
4526 (let ((start 0))
4527 ;; Avoid matching blank or empty lines.
4528 (while (and (string-match "\n\\( *\\)\\(.\\)" object start)
4529 (not (equal (match-string 2 object) " ")))
4530 (setq start (match-end 0))
4531 (push (length (match-string 1 object)) ind-list))))
4532 ((memq (org-element-type object) org-element-recursive-objects)
4533 (funcall collect-inds object first-flag))))
4534 (org-element-contents blob))))))
4535 ;; Collect indentation list in ELEMENT. Possibly remove first
4536 ;; value if IGNORE-FIRST is non-nil.
4537 (catch 'zero (funcall collect-inds element (not ignore-first)))
4538 (if (not ind-list) element
4539 ;; Build ELEMENT back, replacing each string with the same
4540 ;; string minus common indentation.
4541 (let* (build ; For byte compiler.
4542 (build
4543 (function
4544 (lambda (blob mci first-flag)
4545 ;; Return BLOB with all its strings indentation
4546 ;; shortened from MCI white spaces. FIRST-FLAG is
4547 ;; non-nil when the first string hasn't been seen
4548 ;; yet.
4549 (setcdr (cdr blob)
4550 (mapcar
4551 (lambda (object)
4552 (when (and first-flag (stringp object))
4553 (setq first-flag nil)
4554 (setq object
4555 (replace-regexp-in-string
4556 (format "\\` \\{%d\\}" mci) "" object)))
4557 (cond
4558 ((stringp object)
4559 (replace-regexp-in-string
4560 (format "\n \\{%d\\}" mci) "\n" object))
4561 ((memq (org-element-type object)
4562 org-element-recursive-objects)
4563 (funcall build object mci first-flag))
4564 (t object)))
4565 (org-element-contents blob)))
4566 blob))))
4567 (funcall build element (apply 'min ind-list) (not ignore-first))))))
4571 ;;; The Toolbox
4573 ;; The first move is to implement a way to obtain the smallest element
4574 ;; containing point. This is the job of `org-element-at-point'. It
4575 ;; basically jumps back to the beginning of section containing point
4576 ;; and moves, element after element, with
4577 ;; `org-element--current-element' until the container is found. Note:
4578 ;; When using `org-element-at-point', secondary values are never
4579 ;; parsed since the function focuses on elements, not on objects.
4581 ;; At a deeper level, `org-element-context' lists all elements and
4582 ;; objects containing point.
4584 ;; `org-element-nested-p' and `org-element-swap-A-B' may be used
4585 ;; internally by navigation and manipulation tools.
4587 ;;;###autoload
4588 (defun org-element-at-point (&optional keep-trail)
4589 "Determine closest element around point.
4591 Return value is a list like (TYPE PROPS) where TYPE is the type
4592 of the element and PROPS a plist of properties associated to the
4593 element.
4595 Possible types are defined in `org-element-all-elements'.
4596 Properties depend on element or object type, but always include
4597 `:begin', `:end', `:parent' and `:post-blank' properties.
4599 As a special case, if point is at the very beginning of a list or
4600 sub-list, returned element will be that list instead of the first
4601 item. In the same way, if point is at the beginning of the first
4602 row of a table, returned element will be the table instead of the
4603 first row.
4605 If optional argument KEEP-TRAIL is non-nil, the function returns
4606 a list of elements leading to element at point. The list's CAR
4607 is always the element at point. The following positions contain
4608 element's siblings, then parents, siblings of parents, until the
4609 first element of current section."
4610 (org-with-wide-buffer
4611 ;; If at an headline, parse it. It is the sole element that
4612 ;; doesn't require to know about context. Be sure to disallow
4613 ;; secondary string parsing, though.
4614 (if (org-with-limited-levels (org-at-heading-p))
4615 (progn
4616 (beginning-of-line)
4617 (if (not keep-trail) (org-element-headline-parser (point-max) t)
4618 (list (org-element-headline-parser (point-max) t))))
4619 ;; Otherwise move at the beginning of the section containing
4620 ;; point.
4621 (catch 'exit
4622 (let ((origin (point))
4623 (end (save-excursion
4624 (org-with-limited-levels (outline-next-heading)) (point)))
4625 element type special-flag trail struct prevs parent)
4626 (org-with-limited-levels
4627 (if (org-before-first-heading-p)
4628 ;; In empty lines at buffer's beginning, return nil.
4629 (progn (goto-char (point-min))
4630 (org-skip-whitespace)
4631 (when (or (eobp) (> (line-beginning-position) origin))
4632 (throw 'exit nil)))
4633 (org-back-to-heading)
4634 (forward-line)
4635 (org-skip-whitespace)
4636 (when (> (line-beginning-position) origin)
4637 ;; In blank lines just after the headline, point still
4638 ;; belongs to the headline.
4639 (throw 'exit
4640 (progn (org-back-to-heading)
4641 (if (not keep-trail)
4642 (org-element-headline-parser (point-max) t)
4643 (list (org-element-headline-parser
4644 (point-max) t))))))))
4645 (beginning-of-line)
4646 ;; Parse successively each element, skipping those ending
4647 ;; before original position.
4648 (while t
4649 (setq element
4650 (org-element--current-element end 'element special-flag struct)
4651 type (car element))
4652 (org-element-put-property element :parent parent)
4653 (when keep-trail (push element trail))
4654 (cond
4655 ;; 1. Skip any element ending before point. Also skip
4656 ;; element ending at point when we're sure that another
4657 ;; element has started.
4658 ((let ((elem-end (org-element-property :end element)))
4659 (when (or (< elem-end origin)
4660 (and (= elem-end origin) (/= elem-end end)))
4661 (goto-char elem-end))))
4662 ;; 2. An element containing point is always the element at
4663 ;; point.
4664 ((not (memq type org-element-greater-elements))
4665 (throw 'exit (if keep-trail trail element)))
4666 ;; 3. At any other greater element type, if point is
4667 ;; within contents, move into it.
4669 (let ((cbeg (org-element-property :contents-begin element))
4670 (cend (org-element-property :contents-end element)))
4671 (if (or (not cbeg) (not cend) (> cbeg origin) (< cend origin)
4672 ;; Create an anchor for tables and plain lists:
4673 ;; when point is at the very beginning of these
4674 ;; elements, ignoring affiliated keywords,
4675 ;; target them instead of their contents.
4676 (and (= cbeg origin) (memq type '(plain-list table)))
4677 ;; When point is at contents end, do not move
4678 ;; into elements with an explicit ending, but
4679 ;; return that element instead.
4680 (and (= cend origin)
4681 (memq type
4682 '(center-block
4683 drawer dynamic-block inlinetask item
4684 plain-list property-drawer quote-block
4685 special-block))))
4686 (throw 'exit (if keep-trail trail element))
4687 (setq parent element)
4688 (case type
4689 (plain-list
4690 (setq special-flag 'item
4691 struct (org-element-property :structure element)))
4692 (item (setq special-flag nil))
4693 (property-drawer
4694 (setq special-flag 'node-property struct nil))
4695 (table (setq special-flag 'table-row struct nil))
4696 (otherwise (setq special-flag nil struct nil)))
4697 (setq end cend)
4698 (goto-char cbeg)))))))))))
4700 ;;;###autoload
4701 (defun org-element-context (&optional element)
4702 "Return closest element or object around point.
4704 Return value is a list like (TYPE PROPS) where TYPE is the type
4705 of the element or object and PROPS a plist of properties
4706 associated to it.
4708 Possible types are defined in `org-element-all-elements' and
4709 `org-element-all-objects'. Properties depend on element or
4710 object type, but always include `:begin', `:end', `:parent' and
4711 `:post-blank'.
4713 Optional argument ELEMENT, when non-nil, is the closest element
4714 containing point, as returned by `org-element-at-point'.
4715 Providing it allows for quicker computation."
4716 (org-with-wide-buffer
4717 (let* ((origin (point))
4718 (element (or element (org-element-at-point)))
4719 (type (org-element-type element))
4720 end)
4721 ;; Check if point is inside an element containing objects or at
4722 ;; a secondary string. In that case, move to beginning of the
4723 ;; element or secondary string and set END to the other side.
4724 (if (not (or (let ((post (org-element-property :post-affiliated element)))
4725 (and post (> post origin)
4726 (< (org-element-property :begin element) origin)
4727 (progn (beginning-of-line)
4728 (looking-at org-element--affiliated-re)
4729 (member (upcase (match-string 1))
4730 org-element-parsed-keywords))
4731 ;; We're at an affiliated keyword. Change
4732 ;; type to retrieve correct restrictions.
4733 (setq type 'keyword)
4734 ;; Determine if we're at main or dual value.
4735 (if (and (match-end 2) (<= origin (match-end 2)))
4736 (progn (goto-char (match-beginning 2))
4737 (setq end (match-end 2)))
4738 (goto-char (match-end 0))
4739 (setq end (line-end-position)))))
4740 (and (eq type 'item)
4741 (let ((tag (org-element-property :tag element)))
4742 (and tag
4743 (progn
4744 (beginning-of-line)
4745 (search-forward tag (point-at-eol))
4746 (goto-char (match-beginning 0))
4747 (and (>= origin (point))
4748 (<= origin
4749 ;; `1+' is required so some
4750 ;; successors can match
4751 ;; properly their object.
4752 (setq end (1+ (match-end 0)))))))))
4753 (and (memq type '(headline inlinetask))
4754 (progn (beginning-of-line)
4755 (skip-chars-forward "* ")
4756 (setq end (point-at-eol))))
4757 (and (memq type '(paragraph table-row verse-block))
4758 (let ((cbeg (org-element-property
4759 :contents-begin element))
4760 (cend (org-element-property
4761 :contents-end element)))
4762 (and (>= origin cbeg)
4763 (<= origin cend)
4764 (progn (goto-char cbeg) (setq end cend)))))
4765 (and (eq type 'keyword)
4766 (let ((key (org-element-property :key element)))
4767 (and (member key org-element-document-properties)
4768 (progn (beginning-of-line)
4769 (search-forward key (line-end-position) t)
4770 (forward-char)
4771 (setq end (line-end-position))))))))
4772 element
4773 (let ((restriction (org-element-restriction type))
4774 (parent element)
4775 candidates)
4776 (catch 'exit
4777 (while (setq candidates (org-element--get-next-object-candidates
4778 end restriction candidates))
4779 (let ((closest-cand (rassq (apply 'min (mapcar 'cdr candidates))
4780 candidates)))
4781 ;; If ORIGIN is before next object in element, there's
4782 ;; no point in looking further.
4783 (if (> (cdr closest-cand) origin) (throw 'exit parent)
4784 (let* ((object
4785 (progn (goto-char (cdr closest-cand))
4786 (funcall (intern (format "org-element-%s-parser"
4787 (car closest-cand))))))
4788 (cbeg (org-element-property :contents-begin object))
4789 (cend (org-element-property :contents-end object))
4790 (obj-end (org-element-property :end object)))
4791 (cond
4792 ;; ORIGIN is after OBJECT, so skip it.
4793 ((<= obj-end origin)
4794 (if (/= obj-end end) (goto-char obj-end)
4795 (throw 'exit
4796 (org-element-put-property
4797 object :parent parent))))
4798 ;; ORIGIN is within a non-recursive object or at
4799 ;; an object boundaries: Return that object.
4800 ((or (not cbeg) (> cbeg origin) (< cend origin))
4801 (throw 'exit
4802 (org-element-put-property object :parent parent)))
4803 ;; Otherwise, move within current object and
4804 ;; restrict search to the end of its contents.
4805 (t (goto-char cbeg)
4806 (org-element-put-property object :parent parent)
4807 (setq parent object
4808 restriction (org-element-restriction object)
4809 end cend)))))))
4810 parent))))))
4812 (defun org-element-nested-p (elem-A elem-B)
4813 "Non-nil when elements ELEM-A and ELEM-B are nested."
4814 (let ((beg-A (org-element-property :begin elem-A))
4815 (beg-B (org-element-property :begin elem-B))
4816 (end-A (org-element-property :end elem-A))
4817 (end-B (org-element-property :end elem-B)))
4818 (or (and (>= beg-A beg-B) (<= end-A end-B))
4819 (and (>= beg-B beg-A) (<= end-B end-A)))))
4821 (defun org-element-swap-A-B (elem-A elem-B)
4822 "Swap elements ELEM-A and ELEM-B.
4823 Assume ELEM-B is after ELEM-A in the buffer. Leave point at the
4824 end of ELEM-A."
4825 (goto-char (org-element-property :begin elem-A))
4826 ;; There are two special cases when an element doesn't start at bol:
4827 ;; the first paragraph in an item or in a footnote definition.
4828 (let ((specialp (not (bolp))))
4829 ;; Only a paragraph without any affiliated keyword can be moved at
4830 ;; ELEM-A position in such a situation. Note that the case of
4831 ;; a footnote definition is impossible: it cannot contain two
4832 ;; paragraphs in a row because it cannot contain a blank line.
4833 (if (and specialp
4834 (or (not (eq (org-element-type elem-B) 'paragraph))
4835 (/= (org-element-property :begin elem-B)
4836 (org-element-property :contents-begin elem-B))))
4837 (error "Cannot swap elements"))
4838 ;; In a special situation, ELEM-A will have no indentation. We'll
4839 ;; give it ELEM-B's (which will in, in turn, have no indentation).
4840 (let* ((ind-B (when specialp
4841 (goto-char (org-element-property :begin elem-B))
4842 (org-get-indentation)))
4843 (beg-A (org-element-property :begin elem-A))
4844 (end-A (save-excursion
4845 (goto-char (org-element-property :end elem-A))
4846 (skip-chars-backward " \r\t\n")
4847 (point-at-eol)))
4848 (beg-B (org-element-property :begin elem-B))
4849 (end-B (save-excursion
4850 (goto-char (org-element-property :end elem-B))
4851 (skip-chars-backward " \r\t\n")
4852 (point-at-eol)))
4853 ;; Store overlays responsible for visibility status. We
4854 ;; also need to store their boundaries as they will be
4855 ;; removed from buffer.
4856 (overlays
4857 (cons
4858 (mapcar (lambda (ov) (list ov (overlay-start ov) (overlay-end ov)))
4859 (overlays-in beg-A end-A))
4860 (mapcar (lambda (ov) (list ov (overlay-start ov) (overlay-end ov)))
4861 (overlays-in beg-B end-B))))
4862 ;; Get contents.
4863 (body-A (buffer-substring beg-A end-A))
4864 (body-B (delete-and-extract-region beg-B end-B)))
4865 (goto-char beg-B)
4866 (when specialp
4867 (setq body-B (replace-regexp-in-string "\\`[ \t]*" "" body-B))
4868 (org-indent-to-column ind-B))
4869 (insert body-A)
4870 ;; Restore ex ELEM-A overlays.
4871 (let ((offset (- beg-B beg-A)))
4872 (mapc (lambda (ov)
4873 (move-overlay
4874 (car ov) (+ (nth 1 ov) offset) (+ (nth 2 ov) offset)))
4875 (car overlays))
4876 (goto-char beg-A)
4877 (delete-region beg-A end-A)
4878 (insert body-B)
4879 ;; Restore ex ELEM-B overlays.
4880 (mapc (lambda (ov)
4881 (move-overlay
4882 (car ov) (- (nth 1 ov) offset) (- (nth 2 ov) offset)))
4883 (cdr overlays)))
4884 (goto-char (org-element-property :end elem-B)))))
4886 (provide 'org-element)
4888 ;; Local variables:
4889 ;; generated-autoload-file: "org-loaddefs.el"
4890 ;; End:
4892 ;;; org-element.el ends here