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