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