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