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