org-element: Fix interpretation of empty items
[org-mode.git] / lisp / org-element.el
blobe6f4a46287464413fe6f8a192c63164b5c11a69a
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 (when contents
1146 (let ((contents (replace-regexp-in-string
1147 "\\(^\\)[ \t]*\\S-" ind contents nil nil 1)))
1148 (if item-starts-with-par-p (org-trim contents)
1149 (concat "\n" contents)))))))
1152 ;;;; Plain List
1154 (defun org-element-plain-list-parser (limit affiliated structure)
1155 "Parse a plain list.
1157 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1158 the buffer position at the beginning of the first affiliated
1159 keyword and CDR is a plist of affiliated keywords along with
1160 their value. STRUCTURE is the structure of the plain list being
1161 parsed.
1163 Return a list whose CAR is `plain-list' and CDR is a plist
1164 containing `:type', `:begin', `:end', `:contents-begin' and
1165 `:contents-end', `:structure', `:post-blank' and
1166 `:post-affiliated' keywords.
1168 Assume point is at the beginning of the list."
1169 (save-excursion
1170 (let* ((struct (or structure (org-list-struct)))
1171 (prevs (org-list-prevs-alist struct))
1172 (parents (org-list-parents-alist struct))
1173 (type (org-list-get-list-type (point) struct prevs))
1174 (contents-begin (point))
1175 (begin (car affiliated))
1176 (contents-end
1177 (progn (goto-char (org-list-get-list-end (point) struct prevs))
1178 (unless (bolp) (forward-line))
1179 (point)))
1180 (end (progn (skip-chars-forward " \r\t\n" limit)
1181 (skip-chars-backward " \t")
1182 (if (bolp) (point) (line-end-position)))))
1183 ;; Return value.
1184 (list 'plain-list
1185 (nconc
1186 (list :type type
1187 :begin begin
1188 :end end
1189 :contents-begin contents-begin
1190 :contents-end contents-end
1191 :structure struct
1192 :post-blank (count-lines contents-end end)
1193 :post-affiliated contents-begin)
1194 (cdr affiliated))))))
1196 (defun org-element-plain-list-interpreter (plain-list contents)
1197 "Interpret PLAIN-LIST element as Org syntax.
1198 CONTENTS is the contents of the element."
1199 (with-temp-buffer
1200 (insert contents)
1201 (goto-char (point-min))
1202 (org-list-repair)
1203 (buffer-string)))
1206 ;;;; Property Drawer
1208 (defun org-element-property-drawer-parser (limit affiliated)
1209 "Parse a property drawer.
1211 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1212 the buffer position at the beginning of the first affiliated
1213 keyword and CDR is a plist of affiliated keywords along with
1214 their value.
1216 Return a list whose CAR is `property-drawer' and CDR is a plist
1217 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1218 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
1220 Assume point is at the beginning of the property drawer."
1221 (save-excursion
1222 (let ((case-fold-search t))
1223 (if (not (save-excursion
1224 (re-search-forward "^[ \t]*:END:[ \t]*$" limit t)))
1225 ;; Incomplete drawer: parse it as a paragraph.
1226 (org-element-paragraph-parser limit affiliated)
1227 (save-excursion
1228 (let* ((drawer-end-line (match-beginning 0))
1229 (begin (car affiliated))
1230 (post-affiliated (point))
1231 (contents-begin (progn (forward-line)
1232 (and (< (point) drawer-end-line)
1233 (point))))
1234 (contents-end (and contents-begin drawer-end-line))
1235 (hidden (org-invisible-p2))
1236 (pos-before-blank (progn (goto-char drawer-end-line)
1237 (forward-line)
1238 (point)))
1239 (end (progn (skip-chars-forward " \r\t\n" limit)
1240 (skip-chars-backward " \t")
1241 (if (bolp) (point) (line-end-position)))))
1242 (list 'property-drawer
1243 (nconc
1244 (list :begin begin
1245 :end end
1246 :hiddenp hidden
1247 :contents-begin contents-begin
1248 :contents-end contents-end
1249 :post-blank (count-lines pos-before-blank end)
1250 :post-affiliated post-affiliated)
1251 (cdr affiliated)))))))))
1253 (defun org-element-property-drawer-interpreter (property-drawer contents)
1254 "Interpret PROPERTY-DRAWER element as Org syntax.
1255 CONTENTS is the properties within the drawer."
1256 (format ":PROPERTIES:\n%s:END:" contents))
1259 ;;;; Quote Block
1261 (defun org-element-quote-block-parser (limit affiliated)
1262 "Parse a quote block.
1264 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1265 the buffer position at the beginning of the first affiliated
1266 keyword and CDR is a plist of affiliated keywords along with
1267 their value.
1269 Return a list whose CAR is `quote-block' and CDR is a plist
1270 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1271 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
1273 Assume point is at the beginning of the block."
1274 (let ((case-fold-search t))
1275 (if (not (save-excursion
1276 (re-search-forward "^[ \t]*#\\+END_QUOTE[ \t]*$" limit t)))
1277 ;; Incomplete block: parse it as a paragraph.
1278 (org-element-paragraph-parser limit affiliated)
1279 (let ((block-end-line (match-beginning 0)))
1280 (save-excursion
1281 (let* ((begin (car affiliated))
1282 (post-affiliated (point))
1283 ;; Empty blocks have no contents.
1284 (contents-begin (progn (forward-line)
1285 (and (< (point) block-end-line)
1286 (point))))
1287 (contents-end (and contents-begin block-end-line))
1288 (hidden (org-invisible-p2))
1289 (pos-before-blank (progn (goto-char block-end-line)
1290 (forward-line)
1291 (point)))
1292 (end (progn (skip-chars-forward " \r\t\n" limit)
1293 (skip-chars-backward " \t")
1294 (if (bolp) (point) (line-end-position)))))
1295 (list 'quote-block
1296 (nconc
1297 (list :begin begin
1298 :end end
1299 :hiddenp hidden
1300 :contents-begin contents-begin
1301 :contents-end contents-end
1302 :post-blank (count-lines pos-before-blank end)
1303 :post-affiliated post-affiliated)
1304 (cdr affiliated)))))))))
1306 (defun org-element-quote-block-interpreter (quote-block contents)
1307 "Interpret QUOTE-BLOCK element as Org syntax.
1308 CONTENTS is the contents of the element."
1309 (format "#+BEGIN_QUOTE\n%s#+END_QUOTE" contents))
1312 ;;;; Section
1314 (defun org-element-section-parser (limit)
1315 "Parse a section.
1317 LIMIT bounds the search.
1319 Return a list whose CAR is `section' and CDR is a plist
1320 containing `:begin', `:end', `:contents-begin', `contents-end'
1321 and `:post-blank' keywords."
1322 (save-excursion
1323 ;; Beginning of section is the beginning of the first non-blank
1324 ;; line after previous headline.
1325 (let ((begin (point))
1326 (end (progn (org-with-limited-levels (outline-next-heading))
1327 (point)))
1328 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
1329 (forward-line)
1330 (point))))
1331 (list 'section
1332 (list :begin begin
1333 :end end
1334 :contents-begin begin
1335 :contents-end pos-before-blank
1336 :post-blank (count-lines pos-before-blank end))))))
1338 (defun org-element-section-interpreter (section contents)
1339 "Interpret SECTION element as Org syntax.
1340 CONTENTS is the contents of the element."
1341 contents)
1344 ;;;; Special Block
1346 (defun org-element-special-block-parser (limit affiliated)
1347 "Parse a special block.
1349 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1350 the buffer position at the beginning of the first affiliated
1351 keyword and CDR is a plist of affiliated keywords along with
1352 their value.
1354 Return a list whose CAR is `special-block' and CDR is a plist
1355 containing `:type', `:begin', `:end', `:hiddenp',
1356 `:contents-begin', `:contents-end', `:post-blank' and
1357 `:post-affiliated' keywords.
1359 Assume point is at the beginning of the block."
1360 (let* ((case-fold-search t)
1361 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
1362 (upcase (match-string-no-properties 1)))))
1363 (if (not (save-excursion
1364 (re-search-forward
1365 (format "^[ \t]*#\\+END_%s[ \t]*$" (regexp-quote type))
1366 limit t)))
1367 ;; Incomplete block: parse it as a paragraph.
1368 (org-element-paragraph-parser limit affiliated)
1369 (let ((block-end-line (match-beginning 0)))
1370 (save-excursion
1371 (let* ((begin (car affiliated))
1372 (post-affiliated (point))
1373 ;; Empty blocks have no contents.
1374 (contents-begin (progn (forward-line)
1375 (and (< (point) block-end-line)
1376 (point))))
1377 (contents-end (and contents-begin block-end-line))
1378 (hidden (org-invisible-p2))
1379 (pos-before-blank (progn (goto-char block-end-line)
1380 (forward-line)
1381 (point)))
1382 (end (progn (skip-chars-forward " \r\t\n" limit)
1383 (skip-chars-backward " \t")
1384 (if (bolp) (point) (line-end-position)))))
1385 (list 'special-block
1386 (nconc
1387 (list :type type
1388 :begin begin
1389 :end end
1390 :hiddenp hidden
1391 :contents-begin contents-begin
1392 :contents-end contents-end
1393 :post-blank (count-lines pos-before-blank end)
1394 :post-affiliated post-affiliated)
1395 (cdr affiliated)))))))))
1397 (defun org-element-special-block-interpreter (special-block contents)
1398 "Interpret SPECIAL-BLOCK element as Org syntax.
1399 CONTENTS is the contents of the element."
1400 (let ((block-type (org-element-property :type special-block)))
1401 (format "#+BEGIN_%s\n%s#+END_%s" block-type contents block-type)))
1405 ;;; Elements
1407 ;; For each element, a parser and an interpreter are also defined.
1408 ;; Both follow the same naming convention used for greater elements.
1410 ;; Also, as for greater elements, adding a new element type is done
1411 ;; through the following steps: implement a parser and an interpreter,
1412 ;; tweak `org-element--current-element' so that it recognizes the new
1413 ;; type and add that new type to `org-element-all-elements'.
1415 ;; As a special case, when the newly defined type is a block type,
1416 ;; `org-element-block-name-alist' has to be modified accordingly.
1419 ;;;; Babel Call
1421 (defun org-element-babel-call-parser (limit affiliated)
1422 "Parse a babel call.
1424 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1425 the buffer position at the beginning of the first affiliated
1426 keyword and CDR is a plist of affiliated keywords along with
1427 their value.
1429 Return a list whose CAR is `babel-call' and CDR is a plist
1430 containing `:begin', `:end', `:info', `:post-blank' and
1431 `:post-affiliated' as keywords."
1432 (save-excursion
1433 (let ((case-fold-search t)
1434 (info (progn (looking-at org-babel-block-lob-one-liner-regexp)
1435 (org-babel-lob-get-info)))
1436 (begin (car affiliated))
1437 (post-affiliated (point))
1438 (pos-before-blank (progn (forward-line) (point)))
1439 (end (progn (skip-chars-forward " \r\t\n" limit)
1440 (skip-chars-backward " \t")
1441 (if (bolp) (point) (line-end-position)))))
1442 (list 'babel-call
1443 (nconc
1444 (list :begin begin
1445 :end end
1446 :info info
1447 :post-blank (count-lines pos-before-blank end)
1448 :post-affiliated post-affiliated)
1449 (cdr affiliated))))))
1451 (defun org-element-babel-call-interpreter (babel-call contents)
1452 "Interpret BABEL-CALL element as Org syntax.
1453 CONTENTS is nil."
1454 (let* ((babel-info (org-element-property :info babel-call))
1455 (main (car babel-info))
1456 (post-options (nth 1 babel-info)))
1457 (concat "#+CALL: "
1458 (if (not (string-match "\\[\\(\\[.*?\\]\\)\\]" main)) main
1459 ;; Remove redundant square brackets.
1460 (replace-match (match-string 1 main) nil nil main))
1461 (and post-options (format "[%s]" post-options)))))
1464 ;;;; Clock
1466 (defun org-element-clock-parser (limit)
1467 "Parse a clock.
1469 LIMIT bounds the search.
1471 Return a list whose CAR is `clock' and CDR is a plist containing
1472 `:status', `:value', `:time', `:begin', `:end' and `:post-blank'
1473 as keywords."
1474 (save-excursion
1475 (let* ((case-fold-search nil)
1476 (begin (point))
1477 (value (progn (search-forward org-clock-string (line-end-position) t)
1478 (skip-chars-forward " \t")
1479 (org-element-timestamp-parser)))
1480 (duration (and (search-forward " => " (line-end-position) t)
1481 (progn (skip-chars-forward " \t")
1482 (looking-at "\\(\\S-+\\)[ \t]*$"))
1483 (org-match-string-no-properties 1)))
1484 (status (if duration 'closed 'running))
1485 (post-blank (let ((before-blank (progn (forward-line) (point))))
1486 (skip-chars-forward " \r\t\n" limit)
1487 (skip-chars-backward " \t")
1488 (unless (bolp) (end-of-line))
1489 (count-lines before-blank (point))))
1490 (end (point)))
1491 (list 'clock
1492 (list :status status
1493 :value value
1494 :duration duration
1495 :begin begin
1496 :end end
1497 :post-blank post-blank)))))
1499 (defun org-element-clock-interpreter (clock contents)
1500 "Interpret CLOCK element as Org syntax.
1501 CONTENTS is nil."
1502 (concat org-clock-string " "
1503 (org-element-timestamp-interpreter
1504 (org-element-property :value clock) nil)
1505 (let ((duration (org-element-property :duration clock)))
1506 (and duration
1507 (concat " => "
1508 (apply 'format
1509 "%2s:%02s"
1510 (org-split-string duration ":")))))))
1513 ;;;; Comment
1515 (defun org-element-comment-parser (limit affiliated)
1516 "Parse a comment.
1518 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1519 the buffer position at the beginning of the first affiliated
1520 keyword and CDR is a plist of affiliated keywords along with
1521 their value.
1523 Return a list whose CAR is `comment' and CDR is a plist
1524 containing `:begin', `:end', `:value', `:post-blank',
1525 `:post-affiliated' keywords.
1527 Assume point is at comment beginning."
1528 (save-excursion
1529 (let* ((begin (car affiliated))
1530 (post-affiliated (point))
1531 (value (prog2 (looking-at "[ \t]*# ?")
1532 (buffer-substring-no-properties
1533 (match-end 0) (line-end-position))
1534 (forward-line)))
1535 (com-end
1536 ;; Get comments ending.
1537 (progn
1538 (while (and (< (point) limit) (looking-at "[ \t]*#\\( \\|$\\)"))
1539 ;; Accumulate lines without leading hash and first
1540 ;; whitespace.
1541 (setq value
1542 (concat value
1543 "\n"
1544 (buffer-substring-no-properties
1545 (match-end 0) (line-end-position))))
1546 (forward-line))
1547 (point)))
1548 (end (progn (goto-char com-end)
1549 (skip-chars-forward " \r\t\n" limit)
1550 (skip-chars-backward " \t")
1551 (if (bolp) (point) (line-end-position)))))
1552 (list 'comment
1553 (nconc
1554 (list :begin begin
1555 :end end
1556 :value value
1557 :post-blank (count-lines com-end end)
1558 :post-affiliated post-affiliated)
1559 (cdr affiliated))))))
1561 (defun org-element-comment-interpreter (comment contents)
1562 "Interpret COMMENT element as Org syntax.
1563 CONTENTS is nil."
1564 (replace-regexp-in-string "^" "# " (org-element-property :value comment)))
1567 ;;;; Comment Block
1569 (defun org-element-comment-block-parser (limit affiliated)
1570 "Parse an export block.
1572 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1573 the buffer position at the beginning of the first affiliated
1574 keyword and CDR is a plist of affiliated keywords along with
1575 their value.
1577 Return a list whose CAR is `comment-block' and CDR is a plist
1578 containing `:begin', `:end', `:hiddenp', `:value', `:post-blank'
1579 and `:post-affiliated' keywords.
1581 Assume point is at comment block beginning."
1582 (let ((case-fold-search t))
1583 (if (not (save-excursion
1584 (re-search-forward "^[ \t]*#\\+END_COMMENT[ \t]*$" limit t)))
1585 ;; Incomplete block: parse it as a paragraph.
1586 (org-element-paragraph-parser limit affiliated)
1587 (let ((contents-end (match-beginning 0)))
1588 (save-excursion
1589 (let* ((begin (car affiliated))
1590 (post-affiliated (point))
1591 (contents-begin (progn (forward-line) (point)))
1592 (hidden (org-invisible-p2))
1593 (pos-before-blank (progn (goto-char contents-end)
1594 (forward-line)
1595 (point)))
1596 (end (progn (skip-chars-forward " \r\t\n" limit)
1597 (skip-chars-backward " \t")
1598 (if (bolp) (point) (line-end-position))))
1599 (value (buffer-substring-no-properties
1600 contents-begin contents-end)))
1601 (list 'comment-block
1602 (nconc
1603 (list :begin begin
1604 :end end
1605 :value value
1606 :hiddenp hidden
1607 :post-blank (count-lines pos-before-blank end)
1608 :post-affiliated post-affiliated)
1609 (cdr affiliated)))))))))
1611 (defun org-element-comment-block-interpreter (comment-block contents)
1612 "Interpret COMMENT-BLOCK element as Org syntax.
1613 CONTENTS is nil."
1614 (format "#+BEGIN_COMMENT\n%s#+END_COMMENT"
1615 (org-remove-indentation (org-element-property :value comment-block))))
1618 ;;;; Diary Sexp
1620 (defun org-element-diary-sexp-parser (limit affiliated)
1621 "Parse a diary sexp.
1623 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1624 the buffer position at the beginning of the first affiliated
1625 keyword and CDR is a plist of affiliated keywords along with
1626 their value.
1628 Return a list whose CAR is `diary-sexp' and CDR is a plist
1629 containing `:begin', `:end', `:value', `:post-blank' and
1630 `:post-affiliated' keywords."
1631 (save-excursion
1632 (let ((begin (car affiliated))
1633 (post-affiliated (point))
1634 (value (progn (looking-at "\\(%%(.*\\)[ \t]*$")
1635 (org-match-string-no-properties 1)))
1636 (pos-before-blank (progn (forward-line) (point)))
1637 (end (progn (skip-chars-forward " \r\t\n" limit)
1638 (skip-chars-backward " \t")
1639 (if (bolp) (point) (line-end-position)))))
1640 (list 'diary-sexp
1641 (nconc
1642 (list :value value
1643 :begin begin
1644 :end end
1645 :post-blank (count-lines pos-before-blank end)
1646 :post-affiliated post-affiliated)
1647 (cdr affiliated))))))
1649 (defun org-element-diary-sexp-interpreter (diary-sexp contents)
1650 "Interpret DIARY-SEXP as Org syntax.
1651 CONTENTS is nil."
1652 (org-element-property :value diary-sexp))
1655 ;;;; Example Block
1657 (defun org-element--remove-indentation (s &optional n)
1658 "Remove maximum common indentation in string S and return it.
1659 When optional argument N is a positive integer, remove exactly
1660 that much characters from indentation, if possible, or return
1661 S as-is otherwise. Unlike to `org-remove-indentation', this
1662 function doesn't call `untabify' on S."
1663 (catch 'exit
1664 (with-temp-buffer
1665 (insert s)
1666 (goto-char (point-min))
1667 ;; Find maximum common indentation, if not specified.
1668 (setq n (or n
1669 (let ((min-ind (point-max)))
1670 (save-excursion
1671 (while (re-search-forward "^[ \t]*\\S-" nil t)
1672 (let ((ind (1- (current-column))))
1673 (if (zerop ind) (throw 'exit s)
1674 (setq min-ind (min min-ind ind))))))
1675 min-ind)))
1676 (if (zerop n) s
1677 ;; Remove exactly N indentation, but give up if not possible.
1678 (while (not (eobp))
1679 (let ((ind (progn (skip-chars-forward " \t") (current-column))))
1680 (cond ((eolp) (delete-region (line-beginning-position) (point)))
1681 ((< ind n) (throw 'exit s))
1682 (t (org-indent-line-to (- ind n))))
1683 (forward-line)))
1684 (buffer-string)))))
1686 (defun org-element-example-block-parser (limit affiliated)
1687 "Parse an example block.
1689 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1690 the buffer position at the beginning of the first affiliated
1691 keyword and CDR is a plist of affiliated keywords along with
1692 their value.
1694 Return a list whose CAR is `example-block' and CDR is a plist
1695 containing `:begin', `:end', `:number-lines', `:preserve-indent',
1696 `:retain-labels', `:use-labels', `:label-fmt', `:hiddenp',
1697 `:switches', `:value', `:post-blank' and `:post-affiliated'
1698 keywords."
1699 (let ((case-fold-search t))
1700 (if (not (save-excursion
1701 (re-search-forward "^[ \t]*#\\+END_EXAMPLE[ \t]*$" limit t)))
1702 ;; Incomplete block: parse it as a paragraph.
1703 (org-element-paragraph-parser limit affiliated)
1704 (let ((contents-end (match-beginning 0)))
1705 (save-excursion
1706 (let* ((switches
1707 (progn
1708 (looking-at "^[ \t]*#\\+BEGIN_EXAMPLE\\(?: +\\(.*\\)\\)?")
1709 (org-match-string-no-properties 1)))
1710 ;; Switches analysis
1711 (number-lines
1712 (cond ((not switches) nil)
1713 ((string-match "-n\\>" switches) 'new)
1714 ((string-match "+n\\>" switches) 'continued)))
1715 (preserve-indent
1716 (or org-src-preserve-indentation
1717 (and switches (string-match "-i\\>" switches))))
1718 ;; Should labels be retained in (or stripped from) example
1719 ;; blocks?
1720 (retain-labels
1721 (or (not switches)
1722 (not (string-match "-r\\>" switches))
1723 (and number-lines (string-match "-k\\>" switches))))
1724 ;; What should code-references use - labels or
1725 ;; line-numbers?
1726 (use-labels
1727 (or (not switches)
1728 (and retain-labels
1729 (not (string-match "-k\\>" switches)))))
1730 (label-fmt
1731 (and switches
1732 (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
1733 (match-string 1 switches)))
1734 ;; Standard block parsing.
1735 (begin (car affiliated))
1736 (post-affiliated (point))
1737 (block-ind (progn (skip-chars-forward " \t") (current-column)))
1738 (contents-begin (progn (forward-line) (point)))
1739 (hidden (org-invisible-p2))
1740 (value (org-element--remove-indentation
1741 (org-unescape-code-in-string
1742 (buffer-substring-no-properties
1743 contents-begin contents-end))
1744 (and preserve-indent block-ind)))
1745 (pos-before-blank (progn (goto-char contents-end)
1746 (forward-line)
1747 (point)))
1748 (end (progn (skip-chars-forward " \r\t\n" limit)
1749 (skip-chars-backward " \t")
1750 (if (bolp) (point) (line-end-position)))))
1751 (list 'example-block
1752 (nconc
1753 (list :begin begin
1754 :end end
1755 :value value
1756 :switches switches
1757 :number-lines number-lines
1758 :preserve-indent preserve-indent
1759 :retain-labels retain-labels
1760 :use-labels use-labels
1761 :label-fmt label-fmt
1762 :hiddenp hidden
1763 :post-blank (count-lines pos-before-blank end)
1764 :post-affiliated post-affiliated)
1765 (cdr affiliated)))))))))
1767 (defun org-element-example-block-interpreter (example-block contents)
1768 "Interpret EXAMPLE-BLOCK element as Org syntax.
1769 CONTENTS is nil."
1770 (let ((switches (org-element-property :switches example-block)))
1771 (concat "#+BEGIN_EXAMPLE" (and switches (concat " " switches)) "\n"
1772 (org-escape-code-in-string
1773 (org-element-property :value example-block))
1774 "#+END_EXAMPLE")))
1777 ;;;; Export Block
1779 (defun org-element-export-block-parser (limit affiliated)
1780 "Parse an export block.
1782 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1783 the buffer position at the beginning of the first affiliated
1784 keyword and CDR is a plist of affiliated keywords along with
1785 their value.
1787 Return a list whose CAR is `export-block' and CDR is a plist
1788 containing `:begin', `:end', `:type', `:hiddenp', `:value',
1789 `:post-blank' and `:post-affiliated' keywords.
1791 Assume point is at export-block beginning."
1792 (let* ((case-fold-search t)
1793 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
1794 (upcase (org-match-string-no-properties 1)))))
1795 (if (not (save-excursion
1796 (re-search-forward
1797 (format "^[ \t]*#\\+END_%s[ \t]*$" type) limit t)))
1798 ;; Incomplete block: parse it as a paragraph.
1799 (org-element-paragraph-parser limit affiliated)
1800 (let ((contents-end (match-beginning 0)))
1801 (save-excursion
1802 (let* ((begin (car affiliated))
1803 (post-affiliated (point))
1804 (contents-begin (progn (forward-line) (point)))
1805 (hidden (org-invisible-p2))
1806 (pos-before-blank (progn (goto-char contents-end)
1807 (forward-line)
1808 (point)))
1809 (end (progn (skip-chars-forward " \r\t\n" limit)
1810 (skip-chars-backward " \t")
1811 (if (bolp) (point) (line-end-position))))
1812 (value (buffer-substring-no-properties contents-begin
1813 contents-end)))
1814 (list 'export-block
1815 (nconc
1816 (list :begin begin
1817 :end end
1818 :type type
1819 :value value
1820 :hiddenp hidden
1821 :post-blank (count-lines pos-before-blank end)
1822 :post-affiliated post-affiliated)
1823 (cdr affiliated)))))))))
1825 (defun org-element-export-block-interpreter (export-block contents)
1826 "Interpret EXPORT-BLOCK element as Org syntax.
1827 CONTENTS is nil."
1828 (let ((type (org-element-property :type export-block)))
1829 (concat (format "#+BEGIN_%s\n" type)
1830 (org-element-property :value export-block)
1831 (format "#+END_%s" type))))
1834 ;;;; Fixed-width
1836 (defun org-element-fixed-width-parser (limit affiliated)
1837 "Parse a fixed-width section.
1839 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1840 the buffer position at the beginning of the first affiliated
1841 keyword and CDR is a plist of affiliated keywords along with
1842 their value.
1844 Return a list whose CAR is `fixed-width' and CDR is a plist
1845 containing `:begin', `:end', `:value', `:post-blank' and
1846 `:post-affiliated' keywords.
1848 Assume point is at the beginning of the fixed-width area."
1849 (save-excursion
1850 (let* ((begin (car affiliated))
1851 (post-affiliated (point))
1852 value
1853 (end-area
1854 (progn
1855 (while (and (< (point) limit)
1856 (looking-at "[ \t]*:\\( \\|$\\)"))
1857 ;; Accumulate text without starting colons.
1858 (setq value
1859 (concat value
1860 (buffer-substring-no-properties
1861 (match-end 0) (point-at-eol))
1862 "\n"))
1863 (forward-line))
1864 (point)))
1865 (end (progn (skip-chars-forward " \r\t\n" limit)
1866 (skip-chars-backward " \t")
1867 (if (bolp) (point) (line-end-position)))))
1868 (list 'fixed-width
1869 (nconc
1870 (list :begin begin
1871 :end end
1872 :value value
1873 :post-blank (count-lines end-area end)
1874 :post-affiliated post-affiliated)
1875 (cdr affiliated))))))
1877 (defun org-element-fixed-width-interpreter (fixed-width contents)
1878 "Interpret FIXED-WIDTH element as Org syntax.
1879 CONTENTS is nil."
1880 (let ((value (org-element-property :value fixed-width)))
1881 (and value
1882 (replace-regexp-in-string
1883 "^" ": "
1884 (if (string-match "\n\\'" value) (substring value 0 -1) value)))))
1887 ;;;; Horizontal Rule
1889 (defun org-element-horizontal-rule-parser (limit affiliated)
1890 "Parse an horizontal rule.
1892 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1893 the buffer position at the beginning of the first affiliated
1894 keyword and CDR is a plist of affiliated keywords along with
1895 their value.
1897 Return a list whose CAR is `horizontal-rule' and CDR is a plist
1898 containing `:begin', `:end', `:post-blank' and `:post-affiliated'
1899 keywords."
1900 (save-excursion
1901 (let ((begin (car affiliated))
1902 (post-affiliated (point))
1903 (post-hr (progn (forward-line) (point)))
1904 (end (progn (skip-chars-forward " \r\t\n" limit)
1905 (skip-chars-backward " \t")
1906 (if (bolp) (point) (line-end-position)))))
1907 (list 'horizontal-rule
1908 (nconc
1909 (list :begin begin
1910 :end end
1911 :post-blank (count-lines post-hr end)
1912 :post-affiliated post-affiliated)
1913 (cdr affiliated))))))
1915 (defun org-element-horizontal-rule-interpreter (horizontal-rule contents)
1916 "Interpret HORIZONTAL-RULE element as Org syntax.
1917 CONTENTS is nil."
1918 "-----")
1921 ;;;; Keyword
1923 (defun org-element-keyword-parser (limit affiliated)
1924 "Parse a keyword at point.
1926 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1927 the buffer position at the beginning of the first affiliated
1928 keyword and CDR is a plist of affiliated keywords along with
1929 their value.
1931 Return a list whose CAR is `keyword' and CDR is a plist
1932 containing `:key', `:value', `:begin', `:end', `:post-blank' and
1933 `:post-affiliated' keywords."
1934 (save-excursion
1935 (let ((begin (car affiliated))
1936 (post-affiliated (point))
1937 (key (progn (looking-at "[ \t]*#\\+\\(\\S-+*\\):")
1938 (upcase (org-match-string-no-properties 1))))
1939 (value (org-trim (buffer-substring-no-properties
1940 (match-end 0) (point-at-eol))))
1941 (pos-before-blank (progn (forward-line) (point)))
1942 (end (progn (skip-chars-forward " \r\t\n" limit)
1943 (skip-chars-backward " \t")
1944 (if (bolp) (point) (line-end-position)))))
1945 (list 'keyword
1946 (nconc
1947 (list :key key
1948 :value value
1949 :begin begin
1950 :end end
1951 :post-blank (count-lines pos-before-blank end)
1952 :post-affiliated post-affiliated)
1953 (cdr affiliated))))))
1955 (defun org-element-keyword-interpreter (keyword contents)
1956 "Interpret KEYWORD element as Org syntax.
1957 CONTENTS is nil."
1958 (format "#+%s: %s"
1959 (org-element-property :key keyword)
1960 (org-element-property :value keyword)))
1963 ;;;; Latex Environment
1965 (defun org-element-latex-environment-parser (limit affiliated)
1966 "Parse a LaTeX environment.
1968 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1969 the buffer position at the beginning of the first affiliated
1970 keyword and CDR is a plist of affiliated keywords along with
1971 their value.
1973 Return a list whose CAR is `latex-environment' and CDR is a plist
1974 containing `:begin', `:end', `:value', `:post-blank' and
1975 `:post-affiliated' keywords.
1977 Assume point is at the beginning of the latex environment."
1978 (save-excursion
1979 (let ((case-fold-search t)
1980 (code-begin (point)))
1981 (looking-at "[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}")
1982 (if (not (re-search-forward (format "^[ \t]*\\\\end{%s}[ \t]*$"
1983 (regexp-quote (match-string 1)))
1984 limit t))
1985 ;; Incomplete latex environment: parse it as a paragraph.
1986 (org-element-paragraph-parser limit affiliated)
1987 (let* ((code-end (progn (forward-line) (point)))
1988 (begin (car affiliated))
1989 (value (buffer-substring-no-properties code-begin code-end))
1990 (end (progn (skip-chars-forward " \r\t\n" limit)
1991 (skip-chars-backward " \t")
1992 (if (bolp) (point) (line-end-position)))))
1993 (list 'latex-environment
1994 (nconc
1995 (list :begin begin
1996 :end end
1997 :value value
1998 :post-blank (count-lines code-end end)
1999 :post-affiliated code-begin)
2000 (cdr affiliated))))))))
2002 (defun org-element-latex-environment-interpreter (latex-environment contents)
2003 "Interpret LATEX-ENVIRONMENT element as Org syntax.
2004 CONTENTS is nil."
2005 (org-element-property :value latex-environment))
2008 ;;;; Node Property
2010 (defun org-element-node-property-parser (limit)
2011 "Parse a node-property at point.
2013 LIMIT bounds the search.
2015 Return a list whose CAR is `node-property' and CDR is a plist
2016 containing `:key', `:value', `:begin', `:end' and `:post-blank'
2017 keywords."
2018 (save-excursion
2019 (let ((case-fold-search t)
2020 (begin (point))
2021 (key (progn (looking-at "[ \t]*:\\(.*?\\):[ \t]+\\(.*?\\)[ \t]*$")
2022 (org-match-string-no-properties 1)))
2023 (value (org-match-string-no-properties 2))
2024 (pos-before-blank (progn (forward-line) (point)))
2025 (end (progn (skip-chars-forward " \r\t\n" limit)
2026 (if (eobp) (point) (point-at-bol)))))
2027 (list 'node-property
2028 (list :key key
2029 :value value
2030 :begin begin
2031 :end end
2032 :post-blank (count-lines pos-before-blank end))))))
2034 (defun org-element-node-property-interpreter (node-property contents)
2035 "Interpret NODE-PROPERTY element as Org syntax.
2036 CONTENTS is nil."
2037 (format org-property-format
2038 (format ":%s:" (org-element-property :key node-property))
2039 (org-element-property :value node-property)))
2042 ;;;; Paragraph
2044 (defun org-element-paragraph-parser (limit affiliated)
2045 "Parse a paragraph.
2047 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2048 the buffer position at the beginning of the first affiliated
2049 keyword and CDR is a plist of affiliated keywords along with
2050 their value.
2052 Return a list whose CAR is `paragraph' and CDR is a plist
2053 containing `:begin', `:end', `:contents-begin' and
2054 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
2056 Assume point is at the beginning of the paragraph."
2057 (save-excursion
2058 (let* ((begin (car affiliated))
2059 (contents-begin (point))
2060 (before-blank
2061 (let ((case-fold-search t))
2062 (end-of-line)
2063 (if (not (re-search-forward
2064 org-element-paragraph-separate limit 'm))
2065 limit
2066 ;; A matching `org-element-paragraph-separate' is not
2067 ;; necessarily the end of the paragraph. In
2068 ;; particular, lines starting with # or : as a first
2069 ;; non-space character are ambiguous. We have check
2070 ;; if they are valid Org syntax (i.e. not an
2071 ;; incomplete keyword).
2072 (beginning-of-line)
2073 (while (not
2075 ;; There's no ambiguity for other symbols or
2076 ;; empty lines: stop here.
2077 (looking-at "[ \t]*\\(?:[^:#]\\|$\\)")
2078 ;; Stop at valid fixed-width areas.
2079 (looking-at "[ \t]*:\\(?: \\|$\\)")
2080 ;; Stop at drawers.
2081 (and (looking-at org-drawer-regexp)
2082 (save-excursion
2083 (re-search-forward
2084 "^[ \t]*:END:[ \t]*$" limit t)))
2085 ;; Stop at valid comments.
2086 (looking-at "[ \t]*#\\(?: \\|$\\)")
2087 ;; Stop at valid dynamic blocks.
2088 (and (looking-at org-dblock-start-re)
2089 (save-excursion
2090 (re-search-forward
2091 "^[ \t]*#\\+END:?[ \t]*$" limit t)))
2092 ;; Stop at valid blocks.
2093 (and (looking-at
2094 "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
2095 (save-excursion
2096 (re-search-forward
2097 (format "^[ \t]*#\\+END_%s[ \t]*$"
2098 (match-string 1))
2099 limit t)))
2100 ;; Stop at valid latex environments.
2101 (and (looking-at
2102 "^[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}[ \t]*$")
2103 (save-excursion
2104 (re-search-forward
2105 (format "^[ \t]*\\\\end{%s}[ \t]*$"
2106 (match-string 1))
2107 limit t)))
2108 ;; Stop at valid keywords.
2109 (looking-at "[ \t]*#\\+\\S-+:")
2110 ;; Skip everything else.
2111 (not
2112 (progn
2113 (end-of-line)
2114 (re-search-forward org-element-paragraph-separate
2115 limit 'm)))))
2116 (beginning-of-line)))
2117 (if (= (point) limit) limit
2118 (goto-char (line-beginning-position)))))
2119 (contents-end (progn (skip-chars-backward " \r\t\n" contents-begin)
2120 (forward-line)
2121 (point)))
2122 (end (progn (skip-chars-forward " \r\t\n" limit)
2123 (skip-chars-backward " \t")
2124 (if (bolp) (point) (line-end-position)))))
2125 (list 'paragraph
2126 (nconc
2127 (list :begin begin
2128 :end end
2129 :contents-begin contents-begin
2130 :contents-end contents-end
2131 :post-blank (count-lines before-blank end)
2132 :post-affiliated contents-begin)
2133 (cdr affiliated))))))
2135 (defun org-element-paragraph-interpreter (paragraph contents)
2136 "Interpret PARAGRAPH element as Org syntax.
2137 CONTENTS is the contents of the element."
2138 contents)
2141 ;;;; Planning
2143 (defun org-element-planning-parser (limit)
2144 "Parse a planning.
2146 LIMIT bounds the search.
2148 Return a list whose CAR is `planning' and CDR is a plist
2149 containing `:closed', `:deadline', `:scheduled', `:begin', `:end'
2150 and `:post-blank' keywords."
2151 (save-excursion
2152 (let* ((case-fold-search nil)
2153 (begin (point))
2154 (post-blank (let ((before-blank (progn (forward-line) (point))))
2155 (skip-chars-forward " \r\t\n" limit)
2156 (skip-chars-backward " \t")
2157 (unless (bolp) (end-of-line))
2158 (count-lines before-blank (point))))
2159 (end (point))
2160 closed deadline scheduled)
2161 (goto-char begin)
2162 (while (re-search-forward org-keyword-time-not-clock-regexp end t)
2163 (goto-char (match-end 1))
2164 (skip-chars-forward " \t" end)
2165 (let ((keyword (match-string 1))
2166 (time (org-element-timestamp-parser)))
2167 (cond ((equal keyword org-closed-string) (setq closed time))
2168 ((equal keyword org-deadline-string) (setq deadline time))
2169 (t (setq scheduled time)))))
2170 (list 'planning
2171 (list :closed closed
2172 :deadline deadline
2173 :scheduled scheduled
2174 :begin begin
2175 :end end
2176 :post-blank post-blank)))))
2178 (defun org-element-planning-interpreter (planning contents)
2179 "Interpret PLANNING element as Org syntax.
2180 CONTENTS is nil."
2181 (mapconcat
2182 'identity
2183 (delq nil
2184 (list (let ((deadline (org-element-property :deadline planning)))
2185 (when deadline
2186 (concat org-deadline-string " "
2187 (org-element-timestamp-interpreter deadline nil))))
2188 (let ((scheduled (org-element-property :scheduled planning)))
2189 (when scheduled
2190 (concat org-scheduled-string " "
2191 (org-element-timestamp-interpreter scheduled nil))))
2192 (let ((closed (org-element-property :closed planning)))
2193 (when closed
2194 (concat org-closed-string " "
2195 (org-element-timestamp-interpreter closed nil))))))
2196 " "))
2199 ;;;; Quote Section
2201 (defun org-element-quote-section-parser (limit)
2202 "Parse a quote section.
2204 LIMIT bounds the search.
2206 Return a list whose CAR is `quote-section' and CDR is a plist
2207 containing `:begin', `:end', `:value' and `:post-blank' keywords.
2209 Assume point is at beginning of the section."
2210 (save-excursion
2211 (let* ((begin (point))
2212 (end (progn (org-with-limited-levels (outline-next-heading))
2213 (point)))
2214 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
2215 (forward-line)
2216 (point)))
2217 (value (buffer-substring-no-properties begin pos-before-blank)))
2218 (list 'quote-section
2219 (list :begin begin
2220 :end end
2221 :value value
2222 :post-blank (count-lines pos-before-blank end))))))
2224 (defun org-element-quote-section-interpreter (quote-section contents)
2225 "Interpret QUOTE-SECTION element as Org syntax.
2226 CONTENTS is nil."
2227 (org-element-property :value quote-section))
2230 ;;;; Src Block
2232 (defun org-element-src-block-parser (limit affiliated)
2233 "Parse a src block.
2235 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2236 the buffer position at the beginning of the first affiliated
2237 keyword and CDR is a plist of affiliated keywords along with
2238 their value.
2240 Return a list whose CAR is `src-block' and CDR is a plist
2241 containing `:language', `:switches', `:parameters', `:begin',
2242 `:end', `:hiddenp', `:number-lines', `:retain-labels',
2243 `:use-labels', `:label-fmt', `:preserve-indent', `:value',
2244 `:post-blank' and `:post-affiliated' keywords.
2246 Assume point is at the beginning of the block."
2247 (let ((case-fold-search t))
2248 (if (not (save-excursion (re-search-forward "^[ \t]*#\\+END_SRC[ \t]*$"
2249 limit t)))
2250 ;; Incomplete block: parse it as a paragraph.
2251 (org-element-paragraph-parser limit affiliated)
2252 (let ((contents-end (match-beginning 0)))
2253 (save-excursion
2254 (let* ((begin (car affiliated))
2255 (post-affiliated (point))
2256 ;; Get language as a string.
2257 (language
2258 (progn
2259 (looking-at
2260 (concat "^[ \t]*#\\+BEGIN_SRC"
2261 "\\(?: +\\(\\S-+\\)\\)?"
2262 "\\(\\(?: +\\(?:-l \".*?\"\\|[-+][A-Za-z]\\)\\)+\\)?"
2263 "\\(.*\\)[ \t]*$"))
2264 (org-match-string-no-properties 1)))
2265 ;; Get switches.
2266 (switches (org-match-string-no-properties 2))
2267 ;; Get parameters.
2268 (parameters (org-match-string-no-properties 3))
2269 ;; Switches analysis
2270 (number-lines
2271 (cond ((not switches) nil)
2272 ((string-match "-n\\>" switches) 'new)
2273 ((string-match "+n\\>" switches) 'continued)))
2274 (preserve-indent (or org-src-preserve-indentation
2275 (and switches
2276 (string-match "-i\\>" switches))))
2277 (label-fmt
2278 (and switches
2279 (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
2280 (match-string 1 switches)))
2281 ;; Should labels be retained in (or stripped from)
2282 ;; src blocks?
2283 (retain-labels
2284 (or (not switches)
2285 (not (string-match "-r\\>" switches))
2286 (and number-lines (string-match "-k\\>" switches))))
2287 ;; What should code-references use - labels or
2288 ;; line-numbers?
2289 (use-labels
2290 (or (not switches)
2291 (and retain-labels
2292 (not (string-match "-k\\>" switches)))))
2293 ;; Indentation.
2294 (block-ind (progn (skip-chars-forward " \t") (current-column)))
2295 ;; Get visibility status.
2296 (hidden (progn (forward-line) (org-invisible-p2)))
2297 ;; Retrieve code.
2298 (value (org-element--remove-indentation
2299 (org-unescape-code-in-string
2300 (buffer-substring-no-properties
2301 (point) contents-end))
2302 (and preserve-indent block-ind)))
2303 (pos-before-blank (progn (goto-char contents-end)
2304 (forward-line)
2305 (point)))
2306 ;; Get position after ending blank lines.
2307 (end (progn (skip-chars-forward " \r\t\n" limit)
2308 (skip-chars-backward " \t")
2309 (if (bolp) (point) (line-end-position)))))
2310 (list 'src-block
2311 (nconc
2312 (list :language language
2313 :switches (and (org-string-nw-p switches)
2314 (org-trim switches))
2315 :parameters (and (org-string-nw-p parameters)
2316 (org-trim parameters))
2317 :begin begin
2318 :end end
2319 :number-lines number-lines
2320 :preserve-indent preserve-indent
2321 :retain-labels retain-labels
2322 :use-labels use-labels
2323 :label-fmt label-fmt
2324 :hiddenp hidden
2325 :value value
2326 :post-blank (count-lines pos-before-blank end)
2327 :post-affiliated post-affiliated)
2328 (cdr affiliated)))))))))
2330 (defun org-element-src-block-interpreter (src-block contents)
2331 "Interpret SRC-BLOCK element as Org syntax.
2332 CONTENTS is nil."
2333 (let ((lang (org-element-property :language src-block))
2334 (switches (org-element-property :switches src-block))
2335 (params (org-element-property :parameters src-block))
2336 (value (let ((val (org-element-property :value src-block)))
2337 (cond
2338 ((org-element-property :preserve-indent src-block) val)
2339 ((zerop org-edit-src-content-indentation) val)
2341 (let ((ind (make-string
2342 org-edit-src-content-indentation 32)))
2343 (replace-regexp-in-string
2344 "\\(^\\)[ \t]*\\S-" ind val nil nil 1)))))))
2345 (concat (format "#+BEGIN_SRC%s\n"
2346 (concat (and lang (concat " " lang))
2347 (and switches (concat " " switches))
2348 (and params (concat " " params))))
2349 (org-escape-code-in-string value)
2350 "#+END_SRC")))
2353 ;;;; Table
2355 (defun org-element-table-parser (limit affiliated)
2356 "Parse a table at point.
2358 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2359 the buffer position at the beginning of the first affiliated
2360 keyword and CDR is a plist of affiliated keywords along with
2361 their value.
2363 Return a list whose CAR is `table' and CDR is a plist containing
2364 `:begin', `:end', `:tblfm', `:type', `:contents-begin',
2365 `:contents-end', `:value', `:post-blank' and `:post-affiliated'
2366 keywords.
2368 Assume point is at the beginning of the table."
2369 (save-excursion
2370 (let* ((case-fold-search t)
2371 (table-begin (point))
2372 (type (if (org-at-table.el-p) 'table.el 'org))
2373 (begin (car affiliated))
2374 (table-end
2375 (if (re-search-forward org-table-any-border-regexp limit 'm)
2376 (goto-char (match-beginning 0))
2377 (point)))
2378 (tblfm (let (acc)
2379 (while (looking-at "[ \t]*#\\+TBLFM: +\\(.*\\)[ \t]*$")
2380 (push (org-match-string-no-properties 1) acc)
2381 (forward-line))
2382 acc))
2383 (pos-before-blank (point))
2384 (end (progn (skip-chars-forward " \r\t\n" limit)
2385 (skip-chars-backward " \t")
2386 (if (bolp) (point) (line-end-position)))))
2387 (list 'table
2388 (nconc
2389 (list :begin begin
2390 :end end
2391 :type type
2392 :tblfm tblfm
2393 ;; Only `org' tables have contents. `table.el' tables
2394 ;; use a `:value' property to store raw table as
2395 ;; a string.
2396 :contents-begin (and (eq type 'org) table-begin)
2397 :contents-end (and (eq type 'org) table-end)
2398 :value (and (eq type 'table.el)
2399 (buffer-substring-no-properties
2400 table-begin table-end))
2401 :post-blank (count-lines pos-before-blank end)
2402 :post-affiliated table-begin)
2403 (cdr affiliated))))))
2405 (defun org-element-table-interpreter (table contents)
2406 "Interpret TABLE element as Org syntax.
2407 CONTENTS is nil."
2408 (if (eq (org-element-property :type table) 'table.el)
2409 (org-remove-indentation (org-element-property :value table))
2410 (concat (with-temp-buffer (insert contents)
2411 (org-table-align)
2412 (buffer-string))
2413 (mapconcat (lambda (fm) (concat "#+TBLFM: " fm))
2414 (reverse (org-element-property :tblfm table))
2415 "\n"))))
2418 ;;;; Table Row
2420 (defun org-element-table-row-parser (limit)
2421 "Parse table row at point.
2423 LIMIT bounds the search.
2425 Return a list whose CAR is `table-row' and CDR is a plist
2426 containing `:begin', `:end', `:contents-begin', `:contents-end',
2427 `:type' and `:post-blank' keywords."
2428 (save-excursion
2429 (let* ((type (if (looking-at "^[ \t]*|-") 'rule 'standard))
2430 (begin (point))
2431 ;; A table rule has no contents. In that case, ensure
2432 ;; CONTENTS-BEGIN matches CONTENTS-END.
2433 (contents-begin (and (eq type 'standard)
2434 (search-forward "|")
2435 (point)))
2436 (contents-end (and (eq type 'standard)
2437 (progn
2438 (end-of-line)
2439 (skip-chars-backward " \t")
2440 (point))))
2441 (end (progn (forward-line) (point))))
2442 (list 'table-row
2443 (list :type type
2444 :begin begin
2445 :end end
2446 :contents-begin contents-begin
2447 :contents-end contents-end
2448 :post-blank 0)))))
2450 (defun org-element-table-row-interpreter (table-row contents)
2451 "Interpret TABLE-ROW element as Org syntax.
2452 CONTENTS is the contents of the table row."
2453 (if (eq (org-element-property :type table-row) 'rule) "|-"
2454 (concat "| " contents)))
2457 ;;;; Verse Block
2459 (defun org-element-verse-block-parser (limit affiliated)
2460 "Parse a verse block.
2462 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2463 the buffer position at the beginning of the first affiliated
2464 keyword and CDR is a plist of affiliated keywords along with
2465 their value.
2467 Return a list whose CAR is `verse-block' and CDR is a plist
2468 containing `:begin', `:end', `:contents-begin', `:contents-end',
2469 `:hiddenp', `:post-blank' and `:post-affiliated' keywords.
2471 Assume point is at beginning of the block."
2472 (let ((case-fold-search t))
2473 (if (not (save-excursion
2474 (re-search-forward "^[ \t]*#\\+END_VERSE[ \t]*$" limit t)))
2475 ;; Incomplete block: parse it as a paragraph.
2476 (org-element-paragraph-parser limit affiliated)
2477 (let ((contents-end (match-beginning 0)))
2478 (save-excursion
2479 (let* ((begin (car affiliated))
2480 (post-affiliated (point))
2481 (hidden (progn (forward-line) (org-invisible-p2)))
2482 (contents-begin (point))
2483 (pos-before-blank (progn (goto-char contents-end)
2484 (forward-line)
2485 (point)))
2486 (end (progn (skip-chars-forward " \r\t\n" limit)
2487 (skip-chars-backward " \t")
2488 (if (bolp) (point) (line-end-position)))))
2489 (list 'verse-block
2490 (nconc
2491 (list :begin begin
2492 :end end
2493 :contents-begin contents-begin
2494 :contents-end contents-end
2495 :hiddenp hidden
2496 :post-blank (count-lines pos-before-blank end)
2497 :post-affiliated post-affiliated)
2498 (cdr affiliated)))))))))
2500 (defun org-element-verse-block-interpreter (verse-block contents)
2501 "Interpret VERSE-BLOCK element as Org syntax.
2502 CONTENTS is verse block contents."
2503 (format "#+BEGIN_VERSE\n%s#+END_VERSE" contents))
2507 ;;; Objects
2509 ;; Unlike to elements, interstices can be found between objects.
2510 ;; That's why, along with the parser, successor functions are provided
2511 ;; for each object. Some objects share the same successor (i.e. `code'
2512 ;; and `verbatim' objects).
2514 ;; A successor must accept a single argument bounding the search. It
2515 ;; will return either a cons cell whose CAR is the object's type, as
2516 ;; a symbol, and CDR the position of its next occurrence, or nil.
2518 ;; Successors follow the naming convention:
2519 ;; org-element-NAME-successor, where NAME is the name of the
2520 ;; successor, as defined in `org-element-all-successors'.
2522 ;; Some object types (i.e. `italic') are recursive. Restrictions on
2523 ;; object types they can contain will be specified in
2524 ;; `org-element-object-restrictions'.
2526 ;; Adding a new type of object is simple. Implement a successor,
2527 ;; a parser, and an interpreter for it, all following the naming
2528 ;; convention. Register type in `org-element-all-objects' and
2529 ;; successor in `org-element-all-successors'. Maybe tweak
2530 ;; restrictions about it, and that's it.
2533 ;;;; Bold
2535 (defun org-element-bold-parser ()
2536 "Parse bold object at point.
2538 Return a list whose CAR is `bold' and CDR is a plist with
2539 `:begin', `:end', `:contents-begin' and `:contents-end' and
2540 `:post-blank' keywords.
2542 Assume point is at the first star marker."
2543 (save-excursion
2544 (unless (bolp) (backward-char 1))
2545 (looking-at org-emph-re)
2546 (let ((begin (match-beginning 2))
2547 (contents-begin (match-beginning 4))
2548 (contents-end (match-end 4))
2549 (post-blank (progn (goto-char (match-end 2))
2550 (skip-chars-forward " \t")))
2551 (end (point)))
2552 (list 'bold
2553 (list :begin begin
2554 :end end
2555 :contents-begin contents-begin
2556 :contents-end contents-end
2557 :post-blank post-blank)))))
2559 (defun org-element-bold-interpreter (bold contents)
2560 "Interpret BOLD object as Org syntax.
2561 CONTENTS is the contents of the object."
2562 (format "*%s*" contents))
2564 (defun org-element-text-markup-successor (limit)
2565 "Search for the next text-markup object.
2567 LIMIT bounds the search.
2569 Return value is a cons cell whose CAR is a symbol among `bold',
2570 `italic', `underline', `strike-through', `code' and `verbatim'
2571 and CDR is beginning position."
2572 (save-excursion
2573 (unless (bolp) (backward-char))
2574 (when (re-search-forward org-emph-re limit t)
2575 (let ((marker (match-string 3)))
2576 (cons (cond
2577 ((equal marker "*") 'bold)
2578 ((equal marker "/") 'italic)
2579 ((equal marker "_") 'underline)
2580 ((equal marker "+") 'strike-through)
2581 ((equal marker "~") 'code)
2582 ((equal marker "=") 'verbatim)
2583 (t (error "Unknown marker at %d" (match-beginning 3))))
2584 (match-beginning 2))))))
2587 ;;;; Code
2589 (defun org-element-code-parser ()
2590 "Parse code object at point.
2592 Return a list whose CAR is `code' and CDR is a plist with
2593 `:value', `:begin', `:end' and `:post-blank' keywords.
2595 Assume point is at the first tilde marker."
2596 (save-excursion
2597 (unless (bolp) (backward-char 1))
2598 (looking-at org-emph-re)
2599 (let ((begin (match-beginning 2))
2600 (value (org-match-string-no-properties 4))
2601 (post-blank (progn (goto-char (match-end 2))
2602 (skip-chars-forward " \t")))
2603 (end (point)))
2604 (list 'code
2605 (list :value value
2606 :begin begin
2607 :end end
2608 :post-blank post-blank)))))
2610 (defun org-element-code-interpreter (code contents)
2611 "Interpret CODE object as Org syntax.
2612 CONTENTS is nil."
2613 (format "~%s~" (org-element-property :value code)))
2616 ;;;; Entity
2618 (defun org-element-entity-parser ()
2619 "Parse entity at point.
2621 Return a list whose CAR is `entity' and CDR a plist with
2622 `:begin', `:end', `:latex', `:latex-math-p', `:html', `:latin1',
2623 `:utf-8', `:ascii', `:use-brackets-p' and `:post-blank' as
2624 keywords.
2626 Assume point is at the beginning of the entity."
2627 (save-excursion
2628 (looking-at "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)")
2629 (let* ((value (org-entity-get (match-string 1)))
2630 (begin (match-beginning 0))
2631 (bracketsp (string= (match-string 2) "{}"))
2632 (post-blank (progn (goto-char (match-end 1))
2633 (when bracketsp (forward-char 2))
2634 (skip-chars-forward " \t")))
2635 (end (point)))
2636 (list 'entity
2637 (list :name (car value)
2638 :latex (nth 1 value)
2639 :latex-math-p (nth 2 value)
2640 :html (nth 3 value)
2641 :ascii (nth 4 value)
2642 :latin1 (nth 5 value)
2643 :utf-8 (nth 6 value)
2644 :begin begin
2645 :end end
2646 :use-brackets-p bracketsp
2647 :post-blank post-blank)))))
2649 (defun org-element-entity-interpreter (entity contents)
2650 "Interpret ENTITY object as Org syntax.
2651 CONTENTS is nil."
2652 (concat "\\"
2653 (org-element-property :name entity)
2654 (when (org-element-property :use-brackets-p entity) "{}")))
2656 (defun org-element-latex-or-entity-successor (limit)
2657 "Search for the next latex-fragment or entity object.
2659 LIMIT bounds the search.
2661 Return value is a cons cell whose CAR is `entity' or
2662 `latex-fragment' and CDR is beginning position."
2663 (save-excursion
2664 (unless (bolp) (backward-char))
2665 (let ((matchers
2666 (remove "begin" (plist-get org-format-latex-options :matchers)))
2667 ;; ENTITY-RE matches both LaTeX commands and Org entities.
2668 (entity-re
2669 "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))
2670 (when (re-search-forward
2671 (concat (mapconcat (lambda (e) (nth 1 (assoc e org-latex-regexps)))
2672 matchers "\\|")
2673 "\\|" entity-re)
2674 limit t)
2675 (goto-char (match-beginning 0))
2676 (if (looking-at entity-re)
2677 ;; Determine if it's a real entity or a LaTeX command.
2678 (cons (if (org-entity-get (match-string 1)) 'entity 'latex-fragment)
2679 (match-beginning 0))
2680 ;; No entity nor command: point is at a LaTeX fragment.
2681 ;; Determine its type to get the correct beginning position.
2682 (cons 'latex-fragment
2683 (catch 'return
2684 (mapc (lambda (e)
2685 (when (looking-at (nth 1 (assoc e org-latex-regexps)))
2686 (throw 'return
2687 (match-beginning
2688 (nth 2 (assoc e org-latex-regexps))))))
2689 matchers)
2690 (point))))))))
2693 ;;;; Export Snippet
2695 (defun org-element-export-snippet-parser ()
2696 "Parse export snippet at point.
2698 Return a list whose CAR is `export-snippet' and CDR a plist with
2699 `:begin', `:end', `:back-end', `:value' and `:post-blank' as
2700 keywords.
2702 Assume point is at the beginning of the snippet."
2703 (save-excursion
2704 (re-search-forward "@@\\([-A-Za-z0-9]+\\):" nil t)
2705 (let* ((begin (match-beginning 0))
2706 (back-end (org-match-string-no-properties 1))
2707 (value (buffer-substring-no-properties
2708 (point)
2709 (progn (re-search-forward "@@" nil t) (match-beginning 0))))
2710 (post-blank (skip-chars-forward " \t"))
2711 (end (point)))
2712 (list 'export-snippet
2713 (list :back-end back-end
2714 :value value
2715 :begin begin
2716 :end end
2717 :post-blank post-blank)))))
2719 (defun org-element-export-snippet-interpreter (export-snippet contents)
2720 "Interpret EXPORT-SNIPPET object as Org syntax.
2721 CONTENTS is nil."
2722 (format "@@%s:%s@@"
2723 (org-element-property :back-end export-snippet)
2724 (org-element-property :value export-snippet)))
2726 (defun org-element-export-snippet-successor (limit)
2727 "Search for the next export-snippet object.
2729 LIMIT bounds the search.
2731 Return value is a cons cell whose CAR is `export-snippet' and CDR
2732 its beginning position."
2733 (save-excursion
2734 (let (beg)
2735 (when (and (re-search-forward "@@[-A-Za-z0-9]+:" limit t)
2736 (setq beg (match-beginning 0))
2737 (search-forward "@@" limit t))
2738 (cons 'export-snippet beg)))))
2741 ;;;; Footnote Reference
2743 (defun org-element-footnote-reference-parser ()
2744 "Parse footnote reference at point.
2746 Return a list whose CAR is `footnote-reference' and CDR a plist
2747 with `:label', `:type', `:inline-definition', `:begin', `:end'
2748 and `:post-blank' as keywords."
2749 (save-excursion
2750 (looking-at org-footnote-re)
2751 (let* ((begin (point))
2752 (label (or (org-match-string-no-properties 2)
2753 (org-match-string-no-properties 3)
2754 (and (match-string 1)
2755 (concat "fn:" (org-match-string-no-properties 1)))))
2756 (type (if (or (not label) (match-string 1)) 'inline 'standard))
2757 (inner-begin (match-end 0))
2758 (inner-end
2759 (let ((count 1))
2760 (forward-char)
2761 (while (and (> count 0) (re-search-forward "[][]" nil t))
2762 (if (equal (match-string 0) "[") (incf count) (decf count)))
2763 (1- (point))))
2764 (post-blank (progn (goto-char (1+ inner-end))
2765 (skip-chars-forward " \t")))
2766 (end (point))
2767 (footnote-reference
2768 (list 'footnote-reference
2769 (list :label label
2770 :type type
2771 :begin begin
2772 :end end
2773 :post-blank post-blank))))
2774 (org-element-put-property
2775 footnote-reference :inline-definition
2776 (and (eq type 'inline)
2777 (org-element-parse-secondary-string
2778 (buffer-substring inner-begin inner-end)
2779 (org-element-restriction 'footnote-reference)
2780 footnote-reference))))))
2782 (defun org-element-footnote-reference-interpreter (footnote-reference contents)
2783 "Interpret FOOTNOTE-REFERENCE object as Org syntax.
2784 CONTENTS is nil."
2785 (let ((label (or (org-element-property :label footnote-reference) "fn:"))
2786 (def
2787 (let ((inline-def
2788 (org-element-property :inline-definition footnote-reference)))
2789 (if (not inline-def) ""
2790 (concat ":" (org-element-interpret-data inline-def))))))
2791 (format "[%s]" (concat label def))))
2793 (defun org-element-footnote-reference-successor (limit)
2794 "Search for the next footnote-reference object.
2796 LIMIT bounds the search.
2798 Return value is a cons cell whose CAR is `footnote-reference' and
2799 CDR is beginning position."
2800 (save-excursion
2801 (catch 'exit
2802 (while (re-search-forward org-footnote-re limit t)
2803 (save-excursion
2804 (let ((beg (match-beginning 0))
2805 (count 1))
2806 (backward-char)
2807 (while (re-search-forward "[][]" limit t)
2808 (if (equal (match-string 0) "[") (incf count) (decf count))
2809 (when (zerop count)
2810 (throw 'exit (cons 'footnote-reference beg))))))))))
2813 ;;;; Inline Babel Call
2815 (defun org-element-inline-babel-call-parser ()
2816 "Parse inline babel call at point.
2818 Return a list whose CAR is `inline-babel-call' and CDR a plist
2819 with `:begin', `:end', `:info' and `:post-blank' as keywords.
2821 Assume point is at the beginning of the babel call."
2822 (save-excursion
2823 (unless (bolp) (backward-char))
2824 (looking-at org-babel-inline-lob-one-liner-regexp)
2825 (let ((info (save-match-data (org-babel-lob-get-info)))
2826 (begin (match-end 1))
2827 (post-blank (progn (goto-char (match-end 0))
2828 (skip-chars-forward " \t")))
2829 (end (point)))
2830 (list 'inline-babel-call
2831 (list :begin begin
2832 :end end
2833 :info info
2834 :post-blank post-blank)))))
2836 (defun org-element-inline-babel-call-interpreter (inline-babel-call contents)
2837 "Interpret INLINE-BABEL-CALL object as Org syntax.
2838 CONTENTS is nil."
2839 (let* ((babel-info (org-element-property :info inline-babel-call))
2840 (main-source (car babel-info))
2841 (post-options (nth 1 babel-info)))
2842 (concat "call_"
2843 (if (string-match "\\[\\(\\[.*?\\]\\)\\]" main-source)
2844 ;; Remove redundant square brackets.
2845 (replace-match
2846 (match-string 1 main-source) nil nil main-source)
2847 main-source)
2848 (and post-options (format "[%s]" post-options)))))
2850 (defun org-element-inline-babel-call-successor (limit)
2851 "Search for the next inline-babel-call object.
2853 LIMIT bounds the search.
2855 Return value is a cons cell whose CAR is `inline-babel-call' and
2856 CDR is beginning position."
2857 (save-excursion
2858 ;; Use a simplified version of
2859 ;; `org-babel-inline-lob-one-liner-regexp'.
2860 (when (re-search-forward
2861 "call_\\([^()\n]+?\\)\\(?:\\[.*?\\]\\)?([^\n]*?)\\(\\[.*?\\]\\)?"
2862 limit t)
2863 (cons 'inline-babel-call (match-beginning 0)))))
2866 ;;;; Inline Src Block
2868 (defun org-element-inline-src-block-parser ()
2869 "Parse inline source block at point.
2871 LIMIT bounds the search.
2873 Return a list whose CAR is `inline-src-block' and CDR a plist
2874 with `:begin', `:end', `:language', `:value', `:parameters' and
2875 `:post-blank' as keywords.
2877 Assume point is at the beginning of the inline src block."
2878 (save-excursion
2879 (unless (bolp) (backward-char))
2880 (looking-at org-babel-inline-src-block-regexp)
2881 (let ((begin (match-beginning 1))
2882 (language (org-match-string-no-properties 2))
2883 (parameters (org-match-string-no-properties 4))
2884 (value (org-match-string-no-properties 5))
2885 (post-blank (progn (goto-char (match-end 0))
2886 (skip-chars-forward " \t")))
2887 (end (point)))
2888 (list 'inline-src-block
2889 (list :language language
2890 :value value
2891 :parameters parameters
2892 :begin begin
2893 :end end
2894 :post-blank post-blank)))))
2896 (defun org-element-inline-src-block-interpreter (inline-src-block contents)
2897 "Interpret INLINE-SRC-BLOCK object as Org syntax.
2898 CONTENTS is nil."
2899 (let ((language (org-element-property :language inline-src-block))
2900 (arguments (org-element-property :parameters inline-src-block))
2901 (body (org-element-property :value inline-src-block)))
2902 (format "src_%s%s{%s}"
2903 language
2904 (if arguments (format "[%s]" arguments) "")
2905 body)))
2907 (defun org-element-inline-src-block-successor (limit)
2908 "Search for the next inline-babel-call element.
2910 LIMIT bounds the search.
2912 Return value is a cons cell whose CAR is `inline-babel-call' and
2913 CDR is beginning position."
2914 (save-excursion
2915 (unless (bolp) (backward-char))
2916 (when (re-search-forward org-babel-inline-src-block-regexp limit t)
2917 (cons 'inline-src-block (match-beginning 1)))))
2919 ;;;; Italic
2921 (defun org-element-italic-parser ()
2922 "Parse italic object at point.
2924 Return a list whose CAR is `italic' and CDR is a plist with
2925 `:begin', `:end', `:contents-begin' and `:contents-end' and
2926 `:post-blank' keywords.
2928 Assume point is at the first slash marker."
2929 (save-excursion
2930 (unless (bolp) (backward-char 1))
2931 (looking-at org-emph-re)
2932 (let ((begin (match-beginning 2))
2933 (contents-begin (match-beginning 4))
2934 (contents-end (match-end 4))
2935 (post-blank (progn (goto-char (match-end 2))
2936 (skip-chars-forward " \t")))
2937 (end (point)))
2938 (list 'italic
2939 (list :begin begin
2940 :end end
2941 :contents-begin contents-begin
2942 :contents-end contents-end
2943 :post-blank post-blank)))))
2945 (defun org-element-italic-interpreter (italic contents)
2946 "Interpret ITALIC object as Org syntax.
2947 CONTENTS is the contents of the object."
2948 (format "/%s/" contents))
2951 ;;;; Latex Fragment
2953 (defun org-element-latex-fragment-parser ()
2954 "Parse latex fragment at point.
2956 Return a list whose CAR is `latex-fragment' and CDR a plist with
2957 `:value', `:begin', `:end', and `:post-blank' as keywords.
2959 Assume point is at the beginning of the latex fragment."
2960 (save-excursion
2961 (let* ((begin (point))
2962 (substring-match
2963 (catch 'exit
2964 (mapc (lambda (e)
2965 (let ((latex-regexp (nth 1 (assoc e org-latex-regexps))))
2966 (when (or (looking-at latex-regexp)
2967 (and (not (bobp))
2968 (save-excursion
2969 (backward-char)
2970 (looking-at latex-regexp))))
2971 (throw 'exit (nth 2 (assoc e org-latex-regexps))))))
2972 (plist-get org-format-latex-options :matchers))
2973 ;; None found: it's a macro.
2974 (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")
2976 (value (match-string-no-properties substring-match))
2977 (post-blank (progn (goto-char (match-end substring-match))
2978 (skip-chars-forward " \t")))
2979 (end (point)))
2980 (list 'latex-fragment
2981 (list :value value
2982 :begin begin
2983 :end end
2984 :post-blank post-blank)))))
2986 (defun org-element-latex-fragment-interpreter (latex-fragment contents)
2987 "Interpret LATEX-FRAGMENT object as Org syntax.
2988 CONTENTS is nil."
2989 (org-element-property :value latex-fragment))
2991 ;;;; Line Break
2993 (defun org-element-line-break-parser ()
2994 "Parse line break at point.
2996 Return a list whose CAR is `line-break', and CDR a plist with
2997 `:begin', `:end' and `:post-blank' keywords.
2999 Assume point is at the beginning of the line break."
3000 (list 'line-break
3001 (list :begin (point)
3002 :end (progn (forward-line) (point))
3003 :post-blank 0)))
3005 (defun org-element-line-break-interpreter (line-break contents)
3006 "Interpret LINE-BREAK object as Org syntax.
3007 CONTENTS is nil."
3008 "\\\\\n")
3010 (defun org-element-line-break-successor (limit)
3011 "Search for the next line-break object.
3013 LIMIT bounds the search.
3015 Return value is a cons cell whose CAR is `line-break' and CDR is
3016 beginning position."
3017 (save-excursion
3018 (let ((beg (and (re-search-forward "[^\\\\]\\(\\\\\\\\\\)[ \t]*$" limit t)
3019 (goto-char (match-beginning 1)))))
3020 ;; A line break can only happen on a non-empty line.
3021 (when (and beg (re-search-backward "\\S-" (point-at-bol) t))
3022 (cons 'line-break beg)))))
3025 ;;;; Link
3027 (defun org-element-link-parser ()
3028 "Parse link at point.
3030 Return a list whose CAR is `link' and CDR a plist with `:type',
3031 `:path', `:raw-link', `:application', `:search-option', `:begin',
3032 `:end', `:contents-begin', `:contents-end' and `:post-blank' as
3033 keywords.
3035 Assume point is at the beginning of the link."
3036 (save-excursion
3037 (let ((begin (point))
3038 end contents-begin contents-end link-end post-blank path type
3039 raw-link link search-option application)
3040 (cond
3041 ;; Type 1: Text targeted from a radio target.
3042 ((and org-target-link-regexp (looking-at org-target-link-regexp))
3043 (setq type "radio"
3044 link-end (match-end 0)
3045 path (org-match-string-no-properties 0)))
3046 ;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]]
3047 ((looking-at org-bracket-link-regexp)
3048 (setq contents-begin (match-beginning 3)
3049 contents-end (match-end 3)
3050 link-end (match-end 0)
3051 ;; RAW-LINK is the original link. Expand any
3052 ;; abbreviation in it.
3053 raw-link (org-translate-link
3054 (org-link-expand-abbrev
3055 (org-match-string-no-properties 1)))
3056 link (org-link-unescape raw-link))
3057 ;; Determine TYPE of link and set PATH accordingly.
3058 (cond
3059 ;; File type.
3060 ((or (file-name-absolute-p link) (string-match "^\\.\\.?/" link))
3061 (setq type "file" path link))
3062 ;; Explicit type (http, irc, bbdb...). See `org-link-types'.
3063 ((string-match org-link-re-with-space3 link)
3064 (setq type (match-string 1 link) path (match-string 2 link)))
3065 ;; Id type: PATH is the id.
3066 ((string-match "^id:\\([-a-f0-9]+\\)" link)
3067 (setq type "id" path (match-string 1 link)))
3068 ;; Code-ref type: PATH is the name of the reference.
3069 ((string-match "^(\\(.*\\))$" link)
3070 (setq type "coderef" path (match-string 1 link)))
3071 ;; Custom-id type: PATH is the name of the custom id.
3072 ((= (aref link 0) ?#)
3073 (setq type "custom-id" path (substring link 1)))
3074 ;; Fuzzy type: Internal link either matches a target, an
3075 ;; headline name or nothing. PATH is the target or
3076 ;; headline's name.
3077 (t (setq type "fuzzy" path link))))
3078 ;; Type 3: Plain link, i.e. http://orgmode.org
3079 ((looking-at org-plain-link-re)
3080 (setq raw-link (org-match-string-no-properties 0)
3081 type (org-match-string-no-properties 1)
3082 path (org-match-string-no-properties 2)
3083 link-end (match-end 0)))
3084 ;; Type 4: Angular link, i.e. <http://orgmode.org>
3085 ((looking-at org-angle-link-re)
3086 (setq raw-link (buffer-substring-no-properties
3087 (match-beginning 1) (match-end 2))
3088 type (org-match-string-no-properties 1)
3089 path (org-match-string-no-properties 2)
3090 link-end (match-end 0))))
3091 ;; In any case, deduce end point after trailing white space from
3092 ;; LINK-END variable.
3093 (setq post-blank (progn (goto-char link-end) (skip-chars-forward " \t"))
3094 end (point))
3095 ;; Extract search option and opening application out of
3096 ;; "file"-type links.
3097 (when (member type org-element-link-type-is-file)
3098 ;; Application.
3099 (cond ((string-match "^file\\+\\(.*\\)$" type)
3100 (setq application (match-string 1 type)))
3101 ((not (string-match "^file" type))
3102 (setq application type)))
3103 ;; Extract search option from PATH.
3104 (when (string-match "::\\(.*\\)$" path)
3105 (setq search-option (match-string 1 path)
3106 path (replace-match "" nil nil path)))
3107 ;; Make sure TYPE always report "file".
3108 (setq type "file"))
3109 (list 'link
3110 (list :type type
3111 :path path
3112 :raw-link (or raw-link path)
3113 :application application
3114 :search-option search-option
3115 :begin begin
3116 :end end
3117 :contents-begin contents-begin
3118 :contents-end contents-end
3119 :post-blank post-blank)))))
3121 (defun org-element-link-interpreter (link contents)
3122 "Interpret LINK object as Org syntax.
3123 CONTENTS is the contents of the object, or nil."
3124 (let ((type (org-element-property :type link))
3125 (raw-link (org-element-property :raw-link link)))
3126 (if (string= type "radio") raw-link
3127 (format "[[%s]%s]"
3128 raw-link
3129 (if contents (format "[%s]" contents) "")))))
3131 (defun org-element-link-successor (limit)
3132 "Search for the next link object.
3134 LIMIT bounds the search.
3136 Return value is a cons cell whose CAR is `link' and CDR is
3137 beginning position."
3138 (save-excursion
3139 (let ((link-regexp
3140 (if (not org-target-link-regexp) org-any-link-re
3141 (concat org-any-link-re "\\|" org-target-link-regexp))))
3142 (when (re-search-forward link-regexp limit t)
3143 (cons 'link (match-beginning 0))))))
3145 (defun org-element-plain-link-successor (limit)
3146 "Search for the next plain link object.
3148 LIMIT bounds the search.
3150 Return value is a cons cell whose CAR is `link' and CDR is
3151 beginning position."
3152 (and (save-excursion (re-search-forward org-plain-link-re limit t))
3153 (cons 'link (match-beginning 0))))
3156 ;;;; Macro
3158 (defun org-element-macro-parser ()
3159 "Parse macro at point.
3161 Return a list whose CAR is `macro' and CDR a plist with `:key',
3162 `:args', `:begin', `:end', `:value' and `:post-blank' as
3163 keywords.
3165 Assume point is at the macro."
3166 (save-excursion
3167 (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
3168 (let ((begin (point))
3169 (key (downcase (org-match-string-no-properties 1)))
3170 (value (org-match-string-no-properties 0))
3171 (post-blank (progn (goto-char (match-end 0))
3172 (skip-chars-forward " \t")))
3173 (end (point))
3174 (args (let ((args (org-match-string-no-properties 3)))
3175 (when args
3176 ;; Do not use `org-split-string' since empty
3177 ;; strings are meaningful here.
3178 (split-string
3179 (replace-regexp-in-string
3180 "\\(\\\\*\\)\\(,\\)"
3181 (lambda (str)
3182 (let ((len (length (match-string 1 str))))
3183 (concat (make-string (/ len 2) ?\\)
3184 (if (zerop (mod len 2)) "\000" ","))))
3185 args nil t)
3186 "\000")))))
3187 (list 'macro
3188 (list :key key
3189 :value value
3190 :args args
3191 :begin begin
3192 :end end
3193 :post-blank post-blank)))))
3195 (defun org-element-macro-interpreter (macro contents)
3196 "Interpret MACRO object as Org syntax.
3197 CONTENTS is nil."
3198 (org-element-property :value macro))
3200 (defun org-element-macro-successor (limit)
3201 "Search for the next macro object.
3203 LIMIT bounds the search.
3205 Return value is cons cell whose CAR is `macro' and CDR is
3206 beginning position."
3207 (save-excursion
3208 (when (re-search-forward
3209 "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
3210 limit t)
3211 (cons 'macro (match-beginning 0)))))
3214 ;;;; Radio-target
3216 (defun org-element-radio-target-parser ()
3217 "Parse radio target at point.
3219 Return a list whose CAR is `radio-target' and CDR a plist with
3220 `:begin', `:end', `:contents-begin', `:contents-end', `:value'
3221 and `:post-blank' as keywords.
3223 Assume point is at the radio target."
3224 (save-excursion
3225 (looking-at org-radio-target-regexp)
3226 (let ((begin (point))
3227 (contents-begin (match-beginning 1))
3228 (contents-end (match-end 1))
3229 (value (org-match-string-no-properties 1))
3230 (post-blank (progn (goto-char (match-end 0))
3231 (skip-chars-forward " \t")))
3232 (end (point)))
3233 (list 'radio-target
3234 (list :begin begin
3235 :end end
3236 :contents-begin contents-begin
3237 :contents-end contents-end
3238 :post-blank post-blank
3239 :value value)))))
3241 (defun org-element-radio-target-interpreter (target contents)
3242 "Interpret TARGET object as Org syntax.
3243 CONTENTS is the contents of the object."
3244 (concat "<<<" contents ">>>"))
3246 (defun org-element-radio-target-successor (limit)
3247 "Search for the next radio-target object.
3249 LIMIT bounds the search.
3251 Return value is a cons cell whose CAR is `radio-target' and CDR
3252 is beginning position."
3253 (save-excursion
3254 (when (re-search-forward org-radio-target-regexp limit t)
3255 (cons 'radio-target (match-beginning 0)))))
3258 ;;;; Statistics Cookie
3260 (defun org-element-statistics-cookie-parser ()
3261 "Parse statistics cookie at point.
3263 Return a list whose CAR is `statistics-cookie', and CDR a plist
3264 with `:begin', `:end', `:value' and `:post-blank' keywords.
3266 Assume point is at the beginning of the statistics-cookie."
3267 (save-excursion
3268 (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
3269 (let* ((begin (point))
3270 (value (buffer-substring-no-properties
3271 (match-beginning 0) (match-end 0)))
3272 (post-blank (progn (goto-char (match-end 0))
3273 (skip-chars-forward " \t")))
3274 (end (point)))
3275 (list 'statistics-cookie
3276 (list :begin begin
3277 :end end
3278 :value value
3279 :post-blank post-blank)))))
3281 (defun org-element-statistics-cookie-interpreter (statistics-cookie contents)
3282 "Interpret STATISTICS-COOKIE object as Org syntax.
3283 CONTENTS is nil."
3284 (org-element-property :value statistics-cookie))
3286 (defun org-element-statistics-cookie-successor (limit)
3287 "Search for the next statistics cookie object.
3289 LIMIT bounds the search.
3291 Return value is a cons cell whose CAR is `statistics-cookie' and
3292 CDR is beginning position."
3293 (save-excursion
3294 (when (re-search-forward "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]" limit t)
3295 (cons 'statistics-cookie (match-beginning 0)))))
3298 ;;;; Strike-Through
3300 (defun org-element-strike-through-parser ()
3301 "Parse strike-through object at point.
3303 Return a list whose CAR is `strike-through' and CDR is a plist
3304 with `:begin', `:end', `:contents-begin' and `:contents-end' and
3305 `:post-blank' keywords.
3307 Assume point is at the first plus sign marker."
3308 (save-excursion
3309 (unless (bolp) (backward-char 1))
3310 (looking-at org-emph-re)
3311 (let ((begin (match-beginning 2))
3312 (contents-begin (match-beginning 4))
3313 (contents-end (match-end 4))
3314 (post-blank (progn (goto-char (match-end 2))
3315 (skip-chars-forward " \t")))
3316 (end (point)))
3317 (list 'strike-through
3318 (list :begin begin
3319 :end end
3320 :contents-begin contents-begin
3321 :contents-end contents-end
3322 :post-blank post-blank)))))
3324 (defun org-element-strike-through-interpreter (strike-through contents)
3325 "Interpret STRIKE-THROUGH object as Org syntax.
3326 CONTENTS is the contents of the object."
3327 (format "+%s+" contents))
3330 ;;;; Subscript
3332 (defun org-element-subscript-parser ()
3333 "Parse subscript at point.
3335 Return a list whose CAR is `subscript' and CDR a plist with
3336 `:begin', `:end', `:contents-begin', `:contents-end',
3337 `:use-brackets-p' and `:post-blank' as keywords.
3339 Assume point is at the underscore."
3340 (save-excursion
3341 (unless (bolp) (backward-char))
3342 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp)
3344 (not (looking-at org-match-substring-regexp))))
3345 (begin (match-beginning 2))
3346 (contents-begin (or (match-beginning 5)
3347 (match-beginning 3)))
3348 (contents-end (or (match-end 5) (match-end 3)))
3349 (post-blank (progn (goto-char (match-end 0))
3350 (skip-chars-forward " \t")))
3351 (end (point)))
3352 (list 'subscript
3353 (list :begin begin
3354 :end end
3355 :use-brackets-p bracketsp
3356 :contents-begin contents-begin
3357 :contents-end contents-end
3358 :post-blank post-blank)))))
3360 (defun org-element-subscript-interpreter (subscript contents)
3361 "Interpret SUBSCRIPT object as Org syntax.
3362 CONTENTS is the contents of the object."
3363 (format
3364 (if (org-element-property :use-brackets-p subscript) "_{%s}" "_%s")
3365 contents))
3367 (defun org-element-sub/superscript-successor (limit)
3368 "Search for the next sub/superscript object.
3370 LIMIT bounds the search.
3372 Return value is a cons cell whose CAR is either `subscript' or
3373 `superscript' and CDR is beginning position."
3374 (save-excursion
3375 (unless (bolp) (backward-char))
3376 (when (re-search-forward org-match-substring-regexp limit t)
3377 (cons (if (string= (match-string 2) "_") 'subscript 'superscript)
3378 (match-beginning 2)))))
3381 ;;;; Superscript
3383 (defun org-element-superscript-parser ()
3384 "Parse superscript at point.
3386 Return a list whose CAR is `superscript' and CDR a plist with
3387 `:begin', `:end', `:contents-begin', `:contents-end',
3388 `:use-brackets-p' and `:post-blank' as keywords.
3390 Assume point is at the caret."
3391 (save-excursion
3392 (unless (bolp) (backward-char))
3393 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp) t
3394 (not (looking-at org-match-substring-regexp))))
3395 (begin (match-beginning 2))
3396 (contents-begin (or (match-beginning 5)
3397 (match-beginning 3)))
3398 (contents-end (or (match-end 5) (match-end 3)))
3399 (post-blank (progn (goto-char (match-end 0))
3400 (skip-chars-forward " \t")))
3401 (end (point)))
3402 (list 'superscript
3403 (list :begin begin
3404 :end end
3405 :use-brackets-p bracketsp
3406 :contents-begin contents-begin
3407 :contents-end contents-end
3408 :post-blank post-blank)))))
3410 (defun org-element-superscript-interpreter (superscript contents)
3411 "Interpret SUPERSCRIPT object as Org syntax.
3412 CONTENTS is the contents of the object."
3413 (format
3414 (if (org-element-property :use-brackets-p superscript) "^{%s}" "^%s")
3415 contents))
3418 ;;;; Table Cell
3420 (defun org-element-table-cell-parser ()
3421 "Parse table cell at point.
3423 Return a list whose CAR is `table-cell' and CDR is a plist
3424 containing `:begin', `:end', `:contents-begin', `:contents-end'
3425 and `:post-blank' keywords."
3426 (looking-at "[ \t]*\\(.*?\\)[ \t]*|")
3427 (let* ((begin (match-beginning 0))
3428 (end (match-end 0))
3429 (contents-begin (match-beginning 1))
3430 (contents-end (match-end 1)))
3431 (list 'table-cell
3432 (list :begin begin
3433 :end end
3434 :contents-begin contents-begin
3435 :contents-end contents-end
3436 :post-blank 0))))
3438 (defun org-element-table-cell-interpreter (table-cell contents)
3439 "Interpret TABLE-CELL element as Org syntax.
3440 CONTENTS is the contents of the cell, or nil."
3441 (concat " " contents " |"))
3443 (defun org-element-table-cell-successor (limit)
3444 "Search for the next table-cell object.
3446 LIMIT bounds the search.
3448 Return value is a cons cell whose CAR is `table-cell' and CDR is
3449 beginning position."
3450 (when (looking-at "[ \t]*.*?[ \t]*|") (cons 'table-cell (point))))
3453 ;;;; Target
3455 (defun org-element-target-parser ()
3456 "Parse target at point.
3458 Return a list whose CAR is `target' and CDR a plist with
3459 `:begin', `:end', `:value' and `:post-blank' as keywords.
3461 Assume point is at the target."
3462 (save-excursion
3463 (looking-at org-target-regexp)
3464 (let ((begin (point))
3465 (value (org-match-string-no-properties 1))
3466 (post-blank (progn (goto-char (match-end 0))
3467 (skip-chars-forward " \t")))
3468 (end (point)))
3469 (list 'target
3470 (list :begin begin
3471 :end end
3472 :value value
3473 :post-blank post-blank)))))
3475 (defun org-element-target-interpreter (target contents)
3476 "Interpret TARGET object as Org syntax.
3477 CONTENTS is nil."
3478 (format "<<%s>>" (org-element-property :value target)))
3480 (defun org-element-target-successor (limit)
3481 "Search for the next target object.
3483 LIMIT bounds the search.
3485 Return value is a cons cell whose CAR is `target' and CDR is
3486 beginning position."
3487 (save-excursion
3488 (when (re-search-forward org-target-regexp limit t)
3489 (cons 'target (match-beginning 0)))))
3492 ;;;; Timestamp
3494 (defun org-element-timestamp-parser ()
3495 "Parse time stamp at point.
3497 Return a list whose CAR is `timestamp', and CDR a plist with
3498 `:type', `:begin', `:end', `:value' and `:post-blank' keywords.
3500 Assume point is at the beginning of the timestamp."
3501 (save-excursion
3502 (let* ((begin (point))
3503 (activep (eq (char-after) ?<))
3504 (raw-value
3505 (progn
3506 (looking-at "\\([<[]\\(%%\\)?.*?\\)[]>]\\(?:--\\([<[].*?[]>]\\)\\)?")
3507 (match-string-no-properties 0)))
3508 (date-start (match-string-no-properties 1))
3509 (date-end (match-string 3))
3510 (diaryp (match-beginning 2))
3511 (post-blank (progn (goto-char (match-end 0))
3512 (skip-chars-forward " \t")))
3513 (end (point))
3514 (time-range
3515 (and (not diaryp)
3516 (string-match
3517 "[012]?[0-9]:[0-5][0-9]\\(-\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)"
3518 date-start)
3519 (cons (string-to-number (match-string 2 date-start))
3520 (string-to-number (match-string 3 date-start)))))
3521 (type (cond (diaryp 'diary)
3522 ((and activep (or date-end time-range)) 'active-range)
3523 (activep 'active)
3524 ((or date-end time-range) 'inactive-range)
3525 (t 'inactive)))
3526 (repeater-props
3527 (and (not diaryp)
3528 (string-match "\\([.+]?\\+\\)\\([0-9]+\\)\\([hdwmy]\\)>"
3529 raw-value)
3530 (list
3531 :repeater-type
3532 (let ((type (match-string 1 raw-value)))
3533 (cond ((equal "++" type) 'catch-up)
3534 ((equal ".+" type) 'restart)
3535 (t 'cumulate)))
3536 :repeater-value (string-to-number (match-string 2 raw-value))
3537 :repeater-unit
3538 (case (string-to-char (match-string 3 raw-value))
3539 (?h 'hour) (?d 'day) (?w 'week) (?m 'month) (t 'year)))))
3540 year-start month-start day-start hour-start minute-start year-end
3541 month-end day-end hour-end minute-end)
3542 ;; Parse date-start.
3543 (unless diaryp
3544 (let ((date (org-parse-time-string date-start t)))
3545 (setq year-start (nth 5 date)
3546 month-start (nth 4 date)
3547 day-start (nth 3 date)
3548 hour-start (nth 2 date)
3549 minute-start (nth 1 date))))
3550 ;; Compute date-end. It can be provided directly in time-stamp,
3551 ;; or extracted from time range. Otherwise, it defaults to the
3552 ;; same values as date-start.
3553 (unless diaryp
3554 (let ((date (and date-end (org-parse-time-string date-end t))))
3555 (setq year-end (or (nth 5 date) year-start)
3556 month-end (or (nth 4 date) month-start)
3557 day-end (or (nth 3 date) day-start)
3558 hour-end (or (nth 2 date) (car time-range) hour-start)
3559 minute-end (or (nth 1 date) (cdr time-range) minute-start))))
3560 (list 'timestamp
3561 (nconc (list :type type
3562 :raw-value raw-value
3563 :year-start year-start
3564 :month-start month-start
3565 :day-start day-start
3566 :hour-start hour-start
3567 :minute-start minute-start
3568 :year-end year-end
3569 :month-end month-end
3570 :day-end day-end
3571 :hour-end hour-end
3572 :minute-end minute-end
3573 :begin begin
3574 :end end
3575 :post-blank post-blank)
3576 repeater-props)))))
3578 (defun org-element-timestamp-interpreter (timestamp contents)
3579 "Interpret TIMESTAMP object as Org syntax.
3580 CONTENTS is nil."
3581 ;; Use `:raw-value' if specified.
3582 (or (org-element-property :raw-value timestamp)
3583 ;; Otherwise, build timestamp string.
3584 (let* ((repeat-string
3585 (concat
3586 (case (org-element-property :repeater-type timestamp)
3587 (cumulate "+") (catch-up "++") (restart ".+"))
3588 (let ((val (org-element-property :repeater-value timestamp)))
3589 (and val (number-to-string val)))
3590 (case (org-element-property :repeater-unit timestamp)
3591 (hour "h") (day "d") (week "w") (month "m") (year "y"))))
3592 (build-ts-string
3593 ;; Build an Org timestamp string from TIME. ACTIVEP is
3594 ;; non-nil when time stamp is active. If WITH-TIME-P is
3595 ;; non-nil, add a time part. HOUR-END and MINUTE-END
3596 ;; specify a time range in the timestamp. REPEAT-STRING
3597 ;; is the repeater string, if any.
3598 (lambda (time activep &optional with-time-p hour-end minute-end)
3599 (let ((ts (format-time-string
3600 (funcall (if with-time-p 'cdr 'car)
3601 org-time-stamp-formats)
3602 time)))
3603 (when (and hour-end minute-end)
3604 (string-match "[012]?[0-9]:[0-5][0-9]" ts)
3605 (setq ts
3606 (replace-match
3607 (format "\\&-%02d:%02d" hour-end minute-end)
3608 nil nil ts)))
3609 (unless activep (setq ts (format "[%s]" (substring ts 1 -1))))
3610 (when (org-string-nw-p repeat-string)
3611 (setq ts (concat (substring ts 0 -1)
3613 repeat-string
3614 (substring ts -1))))
3615 ;; Return value.
3616 ts)))
3617 (type (org-element-property :type timestamp)))
3618 (case type
3619 ((active inactive)
3620 (let* ((minute-start (org-element-property :minute-start timestamp))
3621 (minute-end (org-element-property :minute-end timestamp))
3622 (hour-start (org-element-property :hour-start timestamp))
3623 (hour-end (org-element-property :hour-end timestamp))
3624 (time-range-p (and hour-start hour-end minute-start minute-end
3625 (or (/= hour-start hour-end)
3626 (/= minute-start minute-end)))))
3627 (funcall
3628 build-ts-string
3629 (encode-time 0
3630 (or minute-start 0)
3631 (or hour-start 0)
3632 (org-element-property :day-start timestamp)
3633 (org-element-property :month-start timestamp)
3634 (org-element-property :year-start timestamp))
3635 (eq type 'active)
3636 (and hour-start minute-start)
3637 (and time-range-p hour-end)
3638 (and time-range-p minute-end))))
3639 ((active-range inactive-range)
3640 (let ((minute-start (org-element-property :minute-start timestamp))
3641 (minute-end (org-element-property :minute-end timestamp))
3642 (hour-start (org-element-property :hour-start timestamp))
3643 (hour-end (org-element-property :hour-end timestamp)))
3644 (concat
3645 (funcall
3646 build-ts-string (encode-time
3648 (or minute-start 0)
3649 (or hour-start 0)
3650 (org-element-property :day-start timestamp)
3651 (org-element-property :month-start timestamp)
3652 (org-element-property :year-start timestamp))
3653 (eq type 'active-range)
3654 (and hour-start minute-start))
3655 "--"
3656 (funcall build-ts-string
3657 (encode-time 0
3658 (or minute-end 0)
3659 (or hour-end 0)
3660 (org-element-property :day-end timestamp)
3661 (org-element-property :month-end timestamp)
3662 (org-element-property :year-end timestamp))
3663 (eq type 'active-range)
3664 (and hour-end minute-end)))))))))
3666 (defun org-element-timestamp-successor (limit)
3667 "Search for the next timestamp object.
3669 LIMIT bounds the search.
3671 Return value is a cons cell whose CAR is `timestamp' and CDR is
3672 beginning position."
3673 (save-excursion
3674 (when (re-search-forward
3675 (concat org-ts-regexp-both
3676 "\\|"
3677 "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
3678 "\\|"
3679 "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
3680 limit t)
3681 (cons 'timestamp (match-beginning 0)))))
3684 ;;;; Underline
3686 (defun org-element-underline-parser ()
3687 "Parse underline object at point.
3689 Return a list whose CAR is `underline' and CDR is a plist with
3690 `:begin', `:end', `:contents-begin' and `:contents-end' and
3691 `:post-blank' keywords.
3693 Assume point is at the first underscore marker."
3694 (save-excursion
3695 (unless (bolp) (backward-char 1))
3696 (looking-at org-emph-re)
3697 (let ((begin (match-beginning 2))
3698 (contents-begin (match-beginning 4))
3699 (contents-end (match-end 4))
3700 (post-blank (progn (goto-char (match-end 2))
3701 (skip-chars-forward " \t")))
3702 (end (point)))
3703 (list 'underline
3704 (list :begin begin
3705 :end end
3706 :contents-begin contents-begin
3707 :contents-end contents-end
3708 :post-blank post-blank)))))
3710 (defun org-element-underline-interpreter (underline contents)
3711 "Interpret UNDERLINE object as Org syntax.
3712 CONTENTS is the contents of the object."
3713 (format "_%s_" contents))
3716 ;;;; Verbatim
3718 (defun org-element-verbatim-parser ()
3719 "Parse verbatim object at point.
3721 Return a list whose CAR is `verbatim' and CDR is a plist with
3722 `:value', `:begin', `:end' and `:post-blank' keywords.
3724 Assume point is at the first equal sign marker."
3725 (save-excursion
3726 (unless (bolp) (backward-char 1))
3727 (looking-at org-emph-re)
3728 (let ((begin (match-beginning 2))
3729 (value (org-match-string-no-properties 4))
3730 (post-blank (progn (goto-char (match-end 2))
3731 (skip-chars-forward " \t")))
3732 (end (point)))
3733 (list 'verbatim
3734 (list :value value
3735 :begin begin
3736 :end end
3737 :post-blank post-blank)))))
3739 (defun org-element-verbatim-interpreter (verbatim contents)
3740 "Interpret VERBATIM object as Org syntax.
3741 CONTENTS is nil."
3742 (format "=%s=" (org-element-property :value verbatim)))
3746 ;;; Parsing Element Starting At Point
3748 ;; `org-element--current-element' is the core function of this section.
3749 ;; It returns the Lisp representation of the element starting at
3750 ;; point.
3752 ;; `org-element--current-element' makes use of special modes. They
3753 ;; are activated for fixed element chaining (i.e. `plain-list' >
3754 ;; `item') or fixed conditional element chaining (i.e. `headline' >
3755 ;; `section'). Special modes are: `first-section', `item',
3756 ;; `node-property', `quote-section', `section' and `table-row'.
3758 (defun org-element--current-element
3759 (limit &optional granularity special structure)
3760 "Parse the element starting at point.
3762 LIMIT bounds the search.
3764 Return value is a list like (TYPE PROPS) where TYPE is the type
3765 of the element and PROPS a plist of properties associated to the
3766 element.
3768 Possible types are defined in `org-element-all-elements'.
3770 Optional argument GRANULARITY determines the depth of the
3771 recursion. Allowed values are `headline', `greater-element',
3772 `element', `object' or nil. When it is broader than `object' (or
3773 nil), secondary values will not be parsed, since they only
3774 contain objects.
3776 Optional argument SPECIAL, when non-nil, can be either
3777 `first-section', `item', `node-property', `quote-section',
3778 `section', and `table-row'.
3780 If STRUCTURE isn't provided but SPECIAL is set to `item', it will
3781 be computed.
3783 This function assumes point is always at the beginning of the
3784 element it has to parse."
3785 (save-excursion
3786 (let ((case-fold-search t)
3787 ;; Determine if parsing depth allows for secondary strings
3788 ;; parsing. It only applies to elements referenced in
3789 ;; `org-element-secondary-value-alist'.
3790 (raw-secondary-p (and granularity (not (eq granularity 'object)))))
3791 (cond
3792 ;; Item.
3793 ((eq special 'item)
3794 (org-element-item-parser limit structure raw-secondary-p))
3795 ;; Table Row.
3796 ((eq special 'table-row) (org-element-table-row-parser limit))
3797 ;; Node Property.
3798 ((eq special 'node-property) (org-element-node-property-parser limit))
3799 ;; Headline.
3800 ((org-with-limited-levels (org-at-heading-p))
3801 (org-element-headline-parser limit raw-secondary-p))
3802 ;; Sections (must be checked after headline).
3803 ((eq special 'section) (org-element-section-parser limit))
3804 ((eq special 'quote-section) (org-element-quote-section-parser limit))
3805 ((eq special 'first-section)
3806 (org-element-section-parser
3807 (or (save-excursion (org-with-limited-levels (outline-next-heading)))
3808 limit)))
3809 ;; When not at bol, point is at the beginning of an item or
3810 ;; a footnote definition: next item is always a paragraph.
3811 ((not (bolp)) (org-element-paragraph-parser limit (list (point))))
3812 ;; Planning and Clock.
3813 ((looking-at org-planning-or-clock-line-re)
3814 (if (equal (match-string 1) org-clock-string)
3815 (org-element-clock-parser limit)
3816 (org-element-planning-parser limit)))
3817 ;; Inlinetask.
3818 ((org-at-heading-p)
3819 (org-element-inlinetask-parser limit raw-secondary-p))
3820 ;; From there, elements can have affiliated keywords.
3821 (t (let ((affiliated (org-element--collect-affiliated-keywords limit)))
3822 (cond
3823 ;; Jumping over affiliated keywords put point off-limits.
3824 ;; Parse them as regular keywords.
3825 ((and (cdr affiliated) (>= (point) limit))
3826 (goto-char (car affiliated))
3827 (org-element-keyword-parser limit nil))
3828 ;; LaTeX Environment.
3829 ((looking-at
3830 "[ \t]*\\\\begin{[A-Za-z0-9*]+}\\(\\[.*?\\]\\|{.*?}\\)*[ \t]*$")
3831 (org-element-latex-environment-parser limit affiliated))
3832 ;; Drawer and Property Drawer.
3833 ((looking-at org-drawer-regexp)
3834 (if (equal (match-string 1) "PROPERTIES")
3835 (org-element-property-drawer-parser limit affiliated)
3836 (org-element-drawer-parser limit affiliated)))
3837 ;; Fixed Width
3838 ((looking-at "[ \t]*:\\( \\|$\\)")
3839 (org-element-fixed-width-parser limit affiliated))
3840 ;; Inline Comments, Blocks, Babel Calls, Dynamic Blocks and
3841 ;; Keywords.
3842 ((looking-at "[ \t]*#")
3843 (goto-char (match-end 0))
3844 (cond ((looking-at "\\(?: \\|$\\)")
3845 (beginning-of-line)
3846 (org-element-comment-parser limit affiliated))
3847 ((looking-at "\\+BEGIN_\\(\\S-+\\)")
3848 (beginning-of-line)
3849 (let ((parser (assoc (upcase (match-string 1))
3850 org-element-block-name-alist)))
3851 (if parser (funcall (cdr parser) limit affiliated)
3852 (org-element-special-block-parser limit affiliated))))
3853 ((looking-at "\\+CALL:")
3854 (beginning-of-line)
3855 (org-element-babel-call-parser limit affiliated))
3856 ((looking-at "\\+BEGIN:? ")
3857 (beginning-of-line)
3858 (org-element-dynamic-block-parser limit affiliated))
3859 ((looking-at "\\+\\S-+:")
3860 (beginning-of-line)
3861 (org-element-keyword-parser limit affiliated))
3863 (beginning-of-line)
3864 (org-element-paragraph-parser limit affiliated))))
3865 ;; Footnote Definition.
3866 ((looking-at org-footnote-definition-re)
3867 (org-element-footnote-definition-parser limit affiliated))
3868 ;; Horizontal Rule.
3869 ((looking-at "[ \t]*-\\{5,\\}[ \t]*$")
3870 (org-element-horizontal-rule-parser limit affiliated))
3871 ;; Diary Sexp.
3872 ((looking-at "%%(")
3873 (org-element-diary-sexp-parser limit affiliated))
3874 ;; Table.
3875 ((org-at-table-p t) (org-element-table-parser limit affiliated))
3876 ;; List.
3877 ((looking-at (org-item-re))
3878 (org-element-plain-list-parser
3879 limit affiliated (or structure (org-list-struct))))
3880 ;; Default element: Paragraph.
3881 (t (org-element-paragraph-parser limit affiliated)))))))))
3884 ;; Most elements can have affiliated keywords. When looking for an
3885 ;; element beginning, we want to move before them, as they belong to
3886 ;; that element, and, in the meantime, collect information they give
3887 ;; into appropriate properties. Hence the following function.
3889 (defun org-element--collect-affiliated-keywords (limit)
3890 "Collect affiliated keywords from point down to LIMIT.
3892 Return a list whose CAR is the position at the first of them and
3893 CDR a plist of keywords and values and move point to the
3894 beginning of the first line after them.
3896 As a special case, if element doesn't start at the beginning of
3897 the line (i.e. a paragraph starting an item), CAR is current
3898 position of point and CDR is nil."
3899 (if (not (bolp)) (list (point))
3900 (let ((case-fold-search t)
3901 (origin (point))
3902 ;; RESTRICT is the list of objects allowed in parsed
3903 ;; keywords value.
3904 (restrict (org-element-restriction 'keyword))
3905 output)
3906 (while (and (< (point) limit) (looking-at org-element--affiliated-re))
3907 (let* ((raw-kwd (upcase (match-string 1)))
3908 ;; Apply translation to RAW-KWD. From there, KWD is
3909 ;; the official keyword.
3910 (kwd (or (cdr (assoc raw-kwd
3911 org-element-keyword-translation-alist))
3912 raw-kwd))
3913 ;; Find main value for any keyword.
3914 (value
3915 (save-match-data
3916 (org-trim
3917 (buffer-substring-no-properties
3918 (match-end 0) (point-at-eol)))))
3919 ;; PARSEDP is non-nil when keyword should have its
3920 ;; value parsed.
3921 (parsedp (member kwd org-element-parsed-keywords))
3922 ;; If KWD is a dual keyword, find its secondary
3923 ;; value. Maybe parse it.
3924 (dualp (member kwd org-element-dual-keywords))
3925 (dual-value
3926 (and dualp
3927 (let ((sec (org-match-string-no-properties 2)))
3928 (if (or (not sec) (not parsedp)) sec
3929 (org-element-parse-secondary-string sec restrict)))))
3930 ;; Attribute a property name to KWD.
3931 (kwd-sym (and kwd (intern (concat ":" (downcase kwd))))))
3932 ;; Now set final shape for VALUE.
3933 (when parsedp
3934 (setq value (org-element-parse-secondary-string value restrict)))
3935 (when dualp
3936 (setq value (and (or value dual-value) (cons value dual-value))))
3937 (when (or (member kwd org-element-multiple-keywords)
3938 ;; Attributes can always appear on multiple lines.
3939 (string-match "^ATTR_" kwd))
3940 (setq value (cons value (plist-get output kwd-sym))))
3941 ;; Eventually store the new value in OUTPUT.
3942 (setq output (plist-put output kwd-sym value))
3943 ;; Move to next keyword.
3944 (forward-line)))
3945 ;; If affiliated keywords are orphaned: move back to first one.
3946 ;; They will be parsed as a paragraph.
3947 (when (looking-at "[ \t]*$") (goto-char origin) (setq output nil))
3948 ;; Return value.
3949 (cons origin output))))
3953 ;;; The Org Parser
3955 ;; The two major functions here are `org-element-parse-buffer', which
3956 ;; parses Org syntax inside the current buffer, taking into account
3957 ;; region, narrowing, or even visibility if specified, and
3958 ;; `org-element-parse-secondary-string', which parses objects within
3959 ;; a given string.
3961 ;; The (almost) almighty `org-element-map' allows to apply a function
3962 ;; on elements or objects matching some type, and accumulate the
3963 ;; resulting values. In an export situation, it also skips unneeded
3964 ;; parts of the parse tree.
3966 (defun org-element-parse-buffer (&optional granularity visible-only)
3967 "Recursively parse the buffer and return structure.
3968 If narrowing is in effect, only parse the visible part of the
3969 buffer.
3971 Optional argument GRANULARITY determines the depth of the
3972 recursion. It can be set to the following symbols:
3974 `headline' Only parse headlines.
3975 `greater-element' Don't recurse into greater elements excepted
3976 headlines and sections. Thus, elements
3977 parsed are the top-level ones.
3978 `element' Parse everything but objects and plain text.
3979 `object' Parse the complete buffer (default).
3981 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3982 elements.
3984 An element or an objects is represented as a list with the
3985 pattern (TYPE PROPERTIES CONTENTS), where :
3987 TYPE is a symbol describing the element or object. See
3988 `org-element-all-elements' and `org-element-all-objects' for an
3989 exhaustive list of such symbols. One can retrieve it with
3990 `org-element-type' function.
3992 PROPERTIES is the list of attributes attached to the element or
3993 object, as a plist. Although most of them are specific to the
3994 element or object type, all types share `:begin', `:end',
3995 `:post-blank' and `:parent' properties, which respectively
3996 refer to buffer position where the element or object starts,
3997 ends, the number of white spaces or blank lines after it, and
3998 the element or object containing it. Properties values can be
3999 obtained by using `org-element-property' function.
4001 CONTENTS is a list of elements, objects or raw strings
4002 contained in the current element or object, when applicable.
4003 One can access them with `org-element-contents' function.
4005 The Org buffer has `org-data' as type and nil as properties.
4006 `org-element-map' function can be used to find specific elements
4007 or objects within the parse tree.
4009 This function assumes that current major mode is `org-mode'."
4010 (save-excursion
4011 (goto-char (point-min))
4012 (org-skip-whitespace)
4013 (org-element--parse-elements
4014 (point-at-bol) (point-max)
4015 ;; Start in `first-section' mode so text before the first
4016 ;; headline belongs to a section.
4017 'first-section nil granularity visible-only (list 'org-data nil))))
4019 (defun org-element-parse-secondary-string (string restriction &optional parent)
4020 "Recursively parse objects in STRING and return structure.
4022 RESTRICTION is a symbol limiting the object types that will be
4023 looked after.
4025 Optional argument PARENT, when non-nil, is the element or object
4026 containing the secondary string. It is used to set correctly
4027 `:parent' property within the string."
4028 ;; Copy buffer-local variables listed in
4029 ;; `org-element-object-variables' into temporary buffer. This is
4030 ;; required since object parsing is dependent on these variables.
4031 (let ((pairs (delq nil (mapcar (lambda (var)
4032 (when (boundp var)
4033 (cons var (symbol-value var))))
4034 org-element-object-variables))))
4035 (with-temp-buffer
4036 (mapc (lambda (pair) (org-set-local (car pair) (cdr pair))) pairs)
4037 (insert string)
4038 (let ((secondary (org-element--parse-objects
4039 (point-min) (point-max) nil restriction)))
4040 (when parent
4041 (mapc (lambda (obj) (org-element-put-property obj :parent parent))
4042 secondary))
4043 secondary))))
4045 (defun org-element-map
4046 (data types fun &optional info first-match no-recursion with-affiliated)
4047 "Map a function on selected elements or objects.
4049 DATA is a parse tree, an element, an object, a string, or a list
4050 of such constructs. TYPES is a symbol or list of symbols of
4051 elements or objects types (see `org-element-all-elements' and
4052 `org-element-all-objects' for a complete list of types). FUN is
4053 the function called on the matching element or object. It has to
4054 accept one argument: the element or object itself.
4056 When optional argument INFO is non-nil, it should be a plist
4057 holding export options. In that case, parts of the parse tree
4058 not exportable according to that property list will be skipped.
4060 When optional argument FIRST-MATCH is non-nil, stop at the first
4061 match for which FUN doesn't return nil, and return that value.
4063 Optional argument NO-RECURSION is a symbol or a list of symbols
4064 representing elements or objects types. `org-element-map' won't
4065 enter any recursive element or object whose type belongs to that
4066 list. Though, FUN can still be applied on them.
4068 When optional argument WITH-AFFILIATED is non-nil, FUN will also
4069 apply to matching objects within parsed affiliated keywords (see
4070 `org-element-parsed-keywords').
4072 Nil values returned from FUN do not appear in the results.
4075 Examples:
4076 ---------
4078 Assuming TREE is a variable containing an Org buffer parse tree,
4079 the following example will return a flat list of all `src-block'
4080 and `example-block' elements in it:
4082 \(org-element-map tree '(example-block src-block) 'identity)
4084 The following snippet will find the first headline with a level
4085 of 1 and a \"phone\" tag, and will return its beginning position:
4087 \(org-element-map tree 'headline
4088 \(lambda (hl)
4089 \(and (= (org-element-property :level hl) 1)
4090 \(member \"phone\" (org-element-property :tags hl))
4091 \(org-element-property :begin hl)))
4092 nil t)
4094 The next example will return a flat list of all `plain-list' type
4095 elements in TREE that are not a sub-list themselves:
4097 \(org-element-map tree 'plain-list 'identity nil nil 'plain-list)
4099 Eventually, this example will return a flat list of all `bold'
4100 type objects containing a `latex-snippet' type object, even
4101 looking into captions:
4103 \(org-element-map tree 'bold
4104 \(lambda (b)
4105 \(and (org-element-map b 'latex-snippet 'identity nil t) b))
4106 nil nil nil t)"
4107 ;; Ensure TYPES and NO-RECURSION are a list, even of one element.
4108 (unless (listp types) (setq types (list types)))
4109 (unless (listp no-recursion) (setq no-recursion (list no-recursion)))
4110 ;; Recursion depth is determined by --CATEGORY.
4111 (let* ((--category
4112 (catch 'found
4113 (let ((category 'greater-elements))
4114 (mapc (lambda (type)
4115 (cond ((or (memq type org-element-all-objects)
4116 (eq type 'plain-text))
4117 ;; If one object is found, the function
4118 ;; has to recurse into every object.
4119 (throw 'found 'objects))
4120 ((not (memq type org-element-greater-elements))
4121 ;; If one regular element is found, the
4122 ;; function has to recurse, at least,
4123 ;; into every element it encounters.
4124 (and (not (eq category 'elements))
4125 (setq category 'elements)))))
4126 types)
4127 category)))
4128 ;; Compute properties for affiliated keywords if necessary.
4129 (--affiliated-alist
4130 (and with-affiliated
4131 (mapcar (lambda (kwd)
4132 (cons kwd (intern (concat ":" (downcase kwd)))))
4133 org-element-affiliated-keywords)))
4134 --acc
4135 --walk-tree
4136 (--walk-tree
4137 (function
4138 (lambda (--data)
4139 ;; Recursively walk DATA. INFO, if non-nil, is a plist
4140 ;; holding contextual information.
4141 (let ((--type (org-element-type --data)))
4142 (cond
4143 ((not --data))
4144 ;; Ignored element in an export context.
4145 ((and info (memq --data (plist-get info :ignore-list))))
4146 ;; List of elements or objects.
4147 ((not --type) (mapc --walk-tree --data))
4148 ;; Unconditionally enter parse trees.
4149 ((eq --type 'org-data)
4150 (mapc --walk-tree (org-element-contents --data)))
4152 ;; Check if TYPE is matching among TYPES. If so,
4153 ;; apply FUN to --DATA and accumulate return value
4154 ;; into --ACC (or exit if FIRST-MATCH is non-nil).
4155 (when (memq --type types)
4156 (let ((result (funcall fun --data)))
4157 (cond ((not result))
4158 (first-match (throw '--map-first-match result))
4159 (t (push result --acc)))))
4160 ;; If --DATA has a secondary string that can contain
4161 ;; objects with their type among TYPES, look into it.
4162 (when (and (eq --category 'objects) (not (stringp --data)))
4163 (let ((sec-prop
4164 (assq --type org-element-secondary-value-alist)))
4165 (when sec-prop
4166 (funcall --walk-tree
4167 (org-element-property (cdr sec-prop) --data)))))
4168 ;; If --DATA has any affiliated keywords and
4169 ;; WITH-AFFILIATED is non-nil, look for objects in
4170 ;; them.
4171 (when (and with-affiliated
4172 (eq --category 'objects)
4173 (memq --type org-element-all-elements))
4174 (mapc (lambda (kwd-pair)
4175 (let ((kwd (car kwd-pair))
4176 (value (org-element-property
4177 (cdr kwd-pair) --data)))
4178 ;; Pay attention to the type of value.
4179 ;; Preserve order for multiple keywords.
4180 (cond
4181 ((not value))
4182 ((and (member kwd org-element-multiple-keywords)
4183 (member kwd org-element-dual-keywords))
4184 (mapc (lambda (line)
4185 (funcall --walk-tree (cdr line))
4186 (funcall --walk-tree (car line)))
4187 (reverse value)))
4188 ((member kwd org-element-multiple-keywords)
4189 (mapc (lambda (line) (funcall --walk-tree line))
4190 (reverse value)))
4191 ((member kwd org-element-dual-keywords)
4192 (funcall --walk-tree (cdr value))
4193 (funcall --walk-tree (car value)))
4194 (t (funcall --walk-tree value)))))
4195 --affiliated-alist))
4196 ;; Determine if a recursion into --DATA is possible.
4197 (cond
4198 ;; --TYPE is explicitly removed from recursion.
4199 ((memq --type no-recursion))
4200 ;; --DATA has no contents.
4201 ((not (org-element-contents --data)))
4202 ;; Looking for greater elements but --DATA is simply
4203 ;; an element or an object.
4204 ((and (eq --category 'greater-elements)
4205 (not (memq --type org-element-greater-elements))))
4206 ;; Looking for elements but --DATA is an object.
4207 ((and (eq --category 'elements)
4208 (memq --type org-element-all-objects)))
4209 ;; In any other case, map contents.
4210 (t (mapc --walk-tree (org-element-contents --data)))))))))))
4211 (catch '--map-first-match
4212 (funcall --walk-tree data)
4213 ;; Return value in a proper order.
4214 (nreverse --acc))))
4215 (put 'org-element-map 'lisp-indent-function 2)
4217 ;; The following functions are internal parts of the parser.
4219 ;; The first one, `org-element--parse-elements' acts at the element's
4220 ;; level.
4222 ;; The second one, `org-element--parse-objects' applies on all objects
4223 ;; of a paragraph or a secondary string. It uses
4224 ;; `org-element--get-next-object-candidates' to optimize the search of
4225 ;; the next object in the buffer.
4227 ;; More precisely, that function looks for every allowed object type
4228 ;; first. Then, it discards failed searches, keeps further matches,
4229 ;; and searches again types matched behind point, for subsequent
4230 ;; calls. Thus, searching for a given type fails only once, and every
4231 ;; object is searched only once at top level (but sometimes more for
4232 ;; nested types).
4234 (defun org-element--parse-elements
4235 (beg end special structure granularity visible-only acc)
4236 "Parse elements between BEG and END positions.
4238 SPECIAL prioritize some elements over the others. It can be set
4239 to `first-section', `quote-section', `section' `item' or
4240 `table-row'.
4242 When value is `item', STRUCTURE will be used as the current list
4243 structure.
4245 GRANULARITY determines the depth of the recursion. See
4246 `org-element-parse-buffer' for more information.
4248 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
4249 elements.
4251 Elements are accumulated into ACC."
4252 (save-excursion
4253 (goto-char beg)
4254 ;; Visible only: skip invisible parts at the beginning of the
4255 ;; element.
4256 (when (and visible-only (org-invisible-p2))
4257 (goto-char (min (1+ (org-find-visible)) end)))
4258 ;; When parsing only headlines, skip any text before first one.
4259 (when (and (eq granularity 'headline) (not (org-at-heading-p)))
4260 (org-with-limited-levels (outline-next-heading)))
4261 ;; Main loop start.
4262 (while (< (point) end)
4263 ;; Find current element's type and parse it accordingly to
4264 ;; its category.
4265 (let* ((element (org-element--current-element
4266 end granularity special structure))
4267 (type (org-element-type element))
4268 (cbeg (org-element-property :contents-begin element)))
4269 (goto-char (org-element-property :end element))
4270 ;; Visible only: skip invisible parts between siblings.
4271 (when (and visible-only (org-invisible-p2))
4272 (goto-char (min (1+ (org-find-visible)) end)))
4273 ;; Fill ELEMENT contents by side-effect.
4274 (cond
4275 ;; If element has no contents, don't modify it.
4276 ((not cbeg))
4277 ;; Greater element: parse it between `contents-begin' and
4278 ;; `contents-end'. Make sure GRANULARITY allows the
4279 ;; recursion, or ELEMENT is a headline, in which case going
4280 ;; inside is mandatory, in order to get sub-level headings.
4281 ((and (memq type org-element-greater-elements)
4282 (or (memq granularity '(element object nil))
4283 (and (eq granularity 'greater-element)
4284 (eq type 'section))
4285 (eq type 'headline)))
4286 (org-element--parse-elements
4287 cbeg (org-element-property :contents-end element)
4288 ;; Possibly switch to a special mode.
4289 (case type
4290 (headline
4291 (if (org-element-property :quotedp element) 'quote-section
4292 'section))
4293 (plain-list 'item)
4294 (property-drawer 'node-property)
4295 (table 'table-row))
4296 (and (memq type '(item plain-list))
4297 (org-element-property :structure element))
4298 granularity visible-only element))
4299 ;; ELEMENT has contents. Parse objects inside, if
4300 ;; GRANULARITY allows it.
4301 ((memq granularity '(object nil))
4302 (org-element--parse-objects
4303 cbeg (org-element-property :contents-end element) element
4304 (org-element-restriction type))))
4305 (org-element-adopt-elements acc element)))
4306 ;; Return result.
4307 acc))
4309 (defun org-element--parse-objects (beg end acc restriction)
4310 "Parse objects between BEG and END and return recursive structure.
4312 Objects are accumulated in ACC.
4314 RESTRICTION is a list of object successors which are allowed in
4315 the current object."
4316 (let ((candidates 'initial))
4317 (save-excursion
4318 (goto-char beg)
4319 (while (and (< (point) end)
4320 (setq candidates (org-element--get-next-object-candidates
4321 end restriction candidates)))
4322 (let ((next-object
4323 (let ((pos (apply 'min (mapcar 'cdr candidates))))
4324 (save-excursion
4325 (goto-char pos)
4326 (funcall (intern (format "org-element-%s-parser"
4327 (car (rassq pos candidates)))))))))
4328 ;; 1. Text before any object. Untabify it.
4329 (let ((obj-beg (org-element-property :begin next-object)))
4330 (unless (= (point) obj-beg)
4331 (setq acc
4332 (org-element-adopt-elements
4334 (replace-regexp-in-string
4335 "\t" (make-string tab-width ? )
4336 (buffer-substring-no-properties (point) obj-beg))))))
4337 ;; 2. Object...
4338 (let ((obj-end (org-element-property :end next-object))
4339 (cont-beg (org-element-property :contents-begin next-object)))
4340 ;; Fill contents of NEXT-OBJECT by side-effect, if it has
4341 ;; a recursive type.
4342 (when (and cont-beg
4343 (memq (car next-object) org-element-recursive-objects))
4344 (save-restriction
4345 (narrow-to-region
4346 cont-beg
4347 (org-element-property :contents-end next-object))
4348 (org-element--parse-objects
4349 (point-min) (point-max) next-object
4350 (org-element-restriction next-object))))
4351 (setq acc (org-element-adopt-elements acc next-object))
4352 (goto-char obj-end))))
4353 ;; 3. Text after last object. Untabify it.
4354 (unless (= (point) end)
4355 (setq acc
4356 (org-element-adopt-elements
4358 (replace-regexp-in-string
4359 "\t" (make-string tab-width ? )
4360 (buffer-substring-no-properties (point) end)))))
4361 ;; Result.
4362 acc)))
4364 (defun org-element--get-next-object-candidates (limit restriction objects)
4365 "Return an alist of candidates for the next object.
4367 LIMIT bounds the search, and RESTRICTION narrows candidates to
4368 some object successors.
4370 OBJECTS is the previous candidates alist. If it is set to
4371 `initial', no search has been done before, and all symbols in
4372 RESTRICTION should be looked after.
4374 Return value is an alist whose CAR is the object type and CDR its
4375 beginning position."
4376 (delq
4378 (if (eq objects 'initial)
4379 ;; When searching for the first time, look for every successor
4380 ;; allowed in RESTRICTION.
4381 (mapcar
4382 (lambda (res)
4383 (funcall (intern (format "org-element-%s-successor" res)) limit))
4384 restriction)
4385 ;; Focus on objects returned during last search. Keep those
4386 ;; still after point. Search again objects before it.
4387 (mapcar
4388 (lambda (obj)
4389 (if (>= (cdr obj) (point)) obj
4390 (let* ((type (car obj))
4391 (succ (or (cdr (assq type org-element-object-successor-alist))
4392 type)))
4393 (and succ
4394 (funcall (intern (format "org-element-%s-successor" succ))
4395 limit)))))
4396 objects))))
4400 ;;; Towards A Bijective Process
4402 ;; The parse tree obtained with `org-element-parse-buffer' is really
4403 ;; a snapshot of the corresponding Org buffer. Therefore, it can be
4404 ;; interpreted and expanded into a string with canonical Org syntax.
4405 ;; Hence `org-element-interpret-data'.
4407 ;; The function relies internally on
4408 ;; `org-element--interpret-affiliated-keywords'.
4410 ;;;###autoload
4411 (defun org-element-interpret-data (data &optional parent)
4412 "Interpret DATA as Org syntax.
4414 DATA is a parse tree, an element, an object or a secondary string
4415 to interpret.
4417 Optional argument PARENT is used for recursive calls. It contains
4418 the element or object containing data, or nil.
4420 Return Org syntax as a string."
4421 (let* ((type (org-element-type data))
4422 (results
4423 (cond
4424 ;; Secondary string.
4425 ((not type)
4426 (mapconcat
4427 (lambda (obj) (org-element-interpret-data obj parent))
4428 data ""))
4429 ;; Full Org document.
4430 ((eq type 'org-data)
4431 (mapconcat
4432 (lambda (obj) (org-element-interpret-data obj parent))
4433 (org-element-contents data) ""))
4434 ;; Plain text: remove `:parent' text property from output.
4435 ((stringp data) (org-no-properties data))
4436 ;; Element/Object without contents.
4437 ((not (org-element-contents data))
4438 (funcall (intern (format "org-element-%s-interpreter" type))
4439 data nil))
4440 ;; Element/Object with contents.
4442 (let* ((greaterp (memq type org-element-greater-elements))
4443 (objectp (and (not greaterp)
4444 (memq type org-element-recursive-objects)))
4445 (contents
4446 (mapconcat
4447 (lambda (obj) (org-element-interpret-data obj data))
4448 (org-element-contents
4449 (if (or greaterp objectp) data
4450 ;; Elements directly containing objects must
4451 ;; have their indentation normalized first.
4452 (org-element-normalize-contents
4453 data
4454 ;; When normalizing first paragraph of an
4455 ;; item or a footnote-definition, ignore
4456 ;; first line's indentation.
4457 (and (eq type 'paragraph)
4458 (equal data (car (org-element-contents parent)))
4459 (memq (org-element-type parent)
4460 '(footnote-definition item))))))
4461 "")))
4462 (funcall (intern (format "org-element-%s-interpreter" type))
4463 data
4464 (if greaterp (org-element-normalize-contents contents)
4465 contents)))))))
4466 (if (memq type '(org-data plain-text nil)) results
4467 ;; Build white spaces. If no `:post-blank' property is
4468 ;; specified, assume its value is 0.
4469 (let ((post-blank (or (org-element-property :post-blank data) 0)))
4470 (if (memq type org-element-all-objects)
4471 (concat results (make-string post-blank 32))
4472 (concat
4473 (org-element--interpret-affiliated-keywords data)
4474 (org-element-normalize-string results)
4475 (make-string post-blank 10)))))))
4477 (defun org-element--interpret-affiliated-keywords (element)
4478 "Return ELEMENT's affiliated keywords as Org syntax.
4479 If there is no affiliated keyword, return the empty string."
4480 (let ((keyword-to-org
4481 (function
4482 (lambda (key value)
4483 (let (dual)
4484 (when (member key org-element-dual-keywords)
4485 (setq dual (cdr value) value (car value)))
4486 (concat "#+" key
4487 (and dual
4488 (format "[%s]" (org-element-interpret-data dual)))
4489 ": "
4490 (if (member key org-element-parsed-keywords)
4491 (org-element-interpret-data value)
4492 value)
4493 "\n"))))))
4494 (mapconcat
4495 (lambda (prop)
4496 (let ((value (org-element-property prop element))
4497 (keyword (upcase (substring (symbol-name prop) 1))))
4498 (when value
4499 (if (or (member keyword org-element-multiple-keywords)
4500 ;; All attribute keywords can have multiple lines.
4501 (string-match "^ATTR_" keyword))
4502 (mapconcat (lambda (line) (funcall keyword-to-org keyword line))
4503 (reverse value)
4505 (funcall keyword-to-org keyword value)))))
4506 ;; List all ELEMENT's properties matching an attribute line or an
4507 ;; affiliated keyword, but ignore translated keywords since they
4508 ;; cannot belong to the property list.
4509 (loop for prop in (nth 1 element) by 'cddr
4510 when (let ((keyword (upcase (substring (symbol-name prop) 1))))
4511 (or (string-match "^ATTR_" keyword)
4512 (and
4513 (member keyword org-element-affiliated-keywords)
4514 (not (assoc keyword
4515 org-element-keyword-translation-alist)))))
4516 collect prop)
4517 "")))
4519 ;; Because interpretation of the parse tree must return the same
4520 ;; number of blank lines between elements and the same number of white
4521 ;; space after objects, some special care must be given to white
4522 ;; spaces.
4524 ;; The first function, `org-element-normalize-string', ensures any
4525 ;; string different from the empty string will end with a single
4526 ;; newline character.
4528 ;; The second function, `org-element-normalize-contents', removes
4529 ;; global indentation from the contents of the current element.
4531 (defun org-element-normalize-string (s)
4532 "Ensure string S ends with a single newline character.
4534 If S isn't a string return it unchanged. If S is the empty
4535 string, return it. Otherwise, return a new string with a single
4536 newline character at its end."
4537 (cond
4538 ((not (stringp s)) s)
4539 ((string= "" s) "")
4540 (t (and (string-match "\\(\n[ \t]*\\)*\\'" s)
4541 (replace-match "\n" nil nil s)))))
4543 (defun org-element-normalize-contents (element &optional ignore-first)
4544 "Normalize plain text in ELEMENT's contents.
4546 ELEMENT must only contain plain text and objects.
4548 If optional argument IGNORE-FIRST is non-nil, ignore first line's
4549 indentation to compute maximal common indentation.
4551 Return the normalized element that is element with global
4552 indentation removed from its contents. The function assumes that
4553 indentation is not done with TAB characters."
4554 (let* (ind-list ; for byte-compiler
4555 collect-inds ; for byte-compiler
4556 (collect-inds
4557 (function
4558 ;; Return list of indentations within BLOB. This is done by
4559 ;; walking recursively BLOB and updating IND-LIST along the
4560 ;; way. FIRST-FLAG is non-nil when the first string hasn't
4561 ;; been seen yet. It is required as this string is the only
4562 ;; one whose indentation doesn't happen after a newline
4563 ;; character.
4564 (lambda (blob first-flag)
4565 (mapc
4566 (lambda (object)
4567 (when (and first-flag (stringp object))
4568 (setq first-flag nil)
4569 (string-match "\\`\\( *\\)" object)
4570 (let ((len (length (match-string 1 object))))
4571 ;; An indentation of zero means no string will be
4572 ;; modified. Quit the process.
4573 (if (zerop len) (throw 'zero (setq ind-list nil))
4574 (push len ind-list))))
4575 (cond
4576 ((stringp object)
4577 (let ((start 0))
4578 ;; Avoid matching blank or empty lines.
4579 (while (and (string-match "\n\\( *\\)\\(.\\)" object start)
4580 (not (equal (match-string 2 object) " ")))
4581 (setq start (match-end 0))
4582 (push (length (match-string 1 object)) ind-list))))
4583 ((memq (org-element-type object) org-element-recursive-objects)
4584 (funcall collect-inds object first-flag))))
4585 (org-element-contents blob))))))
4586 ;; Collect indentation list in ELEMENT. Possibly remove first
4587 ;; value if IGNORE-FIRST is non-nil.
4588 (catch 'zero (funcall collect-inds element (not ignore-first)))
4589 (if (not ind-list) element
4590 ;; Build ELEMENT back, replacing each string with the same
4591 ;; string minus common indentation.
4592 (let* (build ; For byte compiler.
4593 (build
4594 (function
4595 (lambda (blob mci first-flag)
4596 ;; Return BLOB with all its strings indentation
4597 ;; shortened from MCI white spaces. FIRST-FLAG is
4598 ;; non-nil when the first string hasn't been seen
4599 ;; yet.
4600 (setcdr (cdr blob)
4601 (mapcar
4602 (lambda (object)
4603 (when (and first-flag (stringp object))
4604 (setq first-flag nil)
4605 (setq object
4606 (replace-regexp-in-string
4607 (format "\\` \\{%d\\}" mci) "" object)))
4608 (cond
4609 ((stringp object)
4610 (replace-regexp-in-string
4611 (format "\n \\{%d\\}" mci) "\n" object))
4612 ((memq (org-element-type object)
4613 org-element-recursive-objects)
4614 (funcall build object mci first-flag))
4615 (t object)))
4616 (org-element-contents blob)))
4617 blob))))
4618 (funcall build element (apply 'min ind-list) (not ignore-first))))))
4622 ;;; The Toolbox
4624 ;; The first move is to implement a way to obtain the smallest element
4625 ;; containing point. This is the job of `org-element-at-point'. It
4626 ;; basically jumps back to the beginning of section containing point
4627 ;; and moves, element after element, with
4628 ;; `org-element--current-element' until the container is found. Note:
4629 ;; When using `org-element-at-point', secondary values are never
4630 ;; parsed since the function focuses on elements, not on objects.
4632 ;; At a deeper level, `org-element-context' lists all elements and
4633 ;; objects containing point.
4635 ;; `org-element-nested-p' and `org-element-swap-A-B' may be used
4636 ;; internally by navigation and manipulation tools.
4638 ;;;###autoload
4639 (defun org-element-at-point (&optional keep-trail)
4640 "Determine closest element around point.
4642 Return value is a list like (TYPE PROPS) where TYPE is the type
4643 of the element and PROPS a plist of properties associated to the
4644 element.
4646 Possible types are defined in `org-element-all-elements'.
4647 Properties depend on element or object type, but always include
4648 `:begin', `:end', `:parent' and `:post-blank' properties.
4650 As a special case, if point is at the very beginning of a list or
4651 sub-list, returned element will be that list instead of the first
4652 item. In the same way, if point is at the beginning of the first
4653 row of a table, returned element will be the table instead of the
4654 first row.
4656 If optional argument KEEP-TRAIL is non-nil, the function returns
4657 a list of elements leading to element at point. The list's CAR
4658 is always the element at point. The following positions contain
4659 element's siblings, then parents, siblings of parents, until the
4660 first element of current section."
4661 (org-with-wide-buffer
4662 ;; If at a headline, parse it. It is the sole element that
4663 ;; doesn't require to know about context. Be sure to disallow
4664 ;; secondary string parsing, though.
4665 (if (org-with-limited-levels (org-at-heading-p))
4666 (progn
4667 (beginning-of-line)
4668 (if (not keep-trail) (org-element-headline-parser (point-max) t)
4669 (list (org-element-headline-parser (point-max) t))))
4670 ;; Otherwise move at the beginning of the section containing
4671 ;; point.
4672 (catch 'exit
4673 (let ((origin (point))
4674 (end (save-excursion
4675 (org-with-limited-levels (outline-next-heading)) (point)))
4676 element type special-flag trail struct prevs parent)
4677 (org-with-limited-levels
4678 (if (org-before-first-heading-p)
4679 ;; In empty lines at buffer's beginning, return nil.
4680 (progn (goto-char (point-min))
4681 (org-skip-whitespace)
4682 (when (or (eobp) (> (line-beginning-position) origin))
4683 (throw 'exit nil)))
4684 (org-back-to-heading)
4685 (forward-line)
4686 (org-skip-whitespace)
4687 (when (> (line-beginning-position) origin)
4688 ;; In blank lines just after the headline, point still
4689 ;; belongs to the headline.
4690 (throw 'exit
4691 (progn (org-back-to-heading)
4692 (if (not keep-trail)
4693 (org-element-headline-parser (point-max) t)
4694 (list (org-element-headline-parser
4695 (point-max) t))))))))
4696 (beginning-of-line)
4697 ;; Parse successively each element, skipping those ending
4698 ;; before original position.
4699 (while t
4700 (setq element
4701 (org-element--current-element end 'element special-flag struct)
4702 type (car element))
4703 (org-element-put-property element :parent parent)
4704 (when keep-trail (push element trail))
4705 (cond
4706 ;; 1. Skip any element ending before point. Also skip
4707 ;; element ending at point when we're sure that another
4708 ;; element has started.
4709 ((let ((elem-end (org-element-property :end element)))
4710 (when (or (< elem-end origin)
4711 (and (= elem-end origin) (/= elem-end end)))
4712 (goto-char elem-end))))
4713 ;; 2. An element containing point is always the element at
4714 ;; point.
4715 ((not (memq type org-element-greater-elements))
4716 (throw 'exit (if keep-trail trail element)))
4717 ;; 3. At any other greater element type, if point is
4718 ;; within contents, move into it.
4720 (let ((cbeg (org-element-property :contents-begin element))
4721 (cend (org-element-property :contents-end element)))
4722 (if (or (not cbeg) (not cend) (> cbeg origin) (< cend origin)
4723 ;; Create an anchor for tables and plain lists:
4724 ;; when point is at the very beginning of these
4725 ;; elements, ignoring affiliated keywords,
4726 ;; target them instead of their contents.
4727 (and (= cbeg origin) (memq type '(plain-list table)))
4728 ;; When point is at contents end, do not move
4729 ;; into elements with an explicit ending, but
4730 ;; return that element instead.
4731 (and (= cend origin)
4732 (memq type
4733 '(center-block
4734 drawer dynamic-block inlinetask item
4735 plain-list property-drawer quote-block
4736 special-block))))
4737 (throw 'exit (if keep-trail trail element))
4738 (setq parent element)
4739 (case type
4740 (plain-list
4741 (setq special-flag 'item
4742 struct (org-element-property :structure element)))
4743 (item (setq special-flag nil))
4744 (property-drawer
4745 (setq special-flag 'node-property struct nil))
4746 (table (setq special-flag 'table-row struct nil))
4747 (otherwise (setq special-flag nil struct nil)))
4748 (setq end cend)
4749 (goto-char cbeg)))))))))))
4751 ;;;###autoload
4752 (defun org-element-context (&optional element)
4753 "Return closest element or object around point.
4755 Return value is a list like (TYPE PROPS) where TYPE is the type
4756 of the element or object and PROPS a plist of properties
4757 associated to it.
4759 Possible types are defined in `org-element-all-elements' and
4760 `org-element-all-objects'. Properties depend on element or
4761 object type, but always include `:begin', `:end', `:parent' and
4762 `:post-blank'.
4764 Optional argument ELEMENT, when non-nil, is the closest element
4765 containing point, as returned by `org-element-at-point'.
4766 Providing it allows for quicker computation."
4767 (org-with-wide-buffer
4768 (let* ((origin (point))
4769 (element (or element (org-element-at-point)))
4770 (type (org-element-type element))
4771 end)
4772 ;; Check if point is inside an element containing objects or at
4773 ;; a secondary string. In that case, move to beginning of the
4774 ;; element or secondary string and set END to the other side.
4775 (if (not (or (let ((post (org-element-property :post-affiliated element)))
4776 (and post (> post origin)
4777 (< (org-element-property :begin element) origin)
4778 (progn (beginning-of-line)
4779 (looking-at org-element--affiliated-re)
4780 (member (upcase (match-string 1))
4781 org-element-parsed-keywords))
4782 ;; We're at an affiliated keyword. Change
4783 ;; type to retrieve correct restrictions.
4784 (setq type 'keyword)
4785 ;; Determine if we're at main or dual value.
4786 (if (and (match-end 2) (<= origin (match-end 2)))
4787 (progn (goto-char (match-beginning 2))
4788 (setq end (match-end 2)))
4789 (goto-char (match-end 0))
4790 (setq end (line-end-position)))))
4791 (and (eq type 'item)
4792 (let ((tag (org-element-property :tag element)))
4793 (and tag
4794 (progn
4795 (beginning-of-line)
4796 (search-forward tag (point-at-eol))
4797 (goto-char (match-beginning 0))
4798 (and (>= origin (point))
4799 (<= origin
4800 ;; `1+' is required so some
4801 ;; successors can match
4802 ;; properly their object.
4803 (setq end (1+ (match-end 0)))))))))
4804 (and (memq type '(headline inlinetask))
4805 (progn (beginning-of-line)
4806 (skip-chars-forward "* ")
4807 (setq end (point-at-eol))))
4808 (and (memq type '(paragraph table-row verse-block))
4809 (let ((cbeg (org-element-property
4810 :contents-begin element))
4811 (cend (org-element-property
4812 :contents-end element)))
4813 (and cbeg cend ; cbeg is nil for table rules
4814 (>= origin cbeg)
4815 (<= origin cend)
4816 (progn (goto-char cbeg) (setq end cend)))))
4817 (and (eq type 'keyword)
4818 (let ((key (org-element-property :key element)))
4819 (and (member key org-element-document-properties)
4820 (progn (beginning-of-line)
4821 (search-forward key (line-end-position) t)
4822 (forward-char)
4823 (setq end (line-end-position))))))))
4824 element
4825 (let ((restriction (org-element-restriction type))
4826 (parent element)
4827 (candidates 'initial))
4828 (catch 'exit
4829 (while (setq candidates (org-element--get-next-object-candidates
4830 end restriction candidates))
4831 (let ((closest-cand (rassq (apply 'min (mapcar 'cdr candidates))
4832 candidates)))
4833 ;; If ORIGIN is before next object in element, there's
4834 ;; no point in looking further.
4835 (if (> (cdr closest-cand) origin) (throw 'exit parent)
4836 (let* ((object
4837 (progn (goto-char (cdr closest-cand))
4838 (funcall (intern (format "org-element-%s-parser"
4839 (car closest-cand))))))
4840 (cbeg (org-element-property :contents-begin object))
4841 (cend (org-element-property :contents-end object))
4842 (obj-end (org-element-property :end object)))
4843 (cond
4844 ;; ORIGIN is after OBJECT, so skip it.
4845 ((<= obj-end origin)
4846 (if (/= obj-end end) (goto-char obj-end)
4847 (throw 'exit
4848 (org-element-put-property
4849 object :parent parent))))
4850 ;; ORIGIN is within a non-recursive object or at
4851 ;; an object boundaries: Return that object.
4852 ((or (not cbeg) (> cbeg origin) (< cend origin))
4853 (throw 'exit
4854 (org-element-put-property object :parent parent)))
4855 ;; Otherwise, move within current object and
4856 ;; restrict search to the end of its contents.
4857 (t (goto-char cbeg)
4858 (org-element-put-property object :parent parent)
4859 (setq parent object
4860 restriction (org-element-restriction object)
4861 candidates 'initial
4862 end cend)))))))
4863 parent))))))
4865 (defun org-element-nested-p (elem-A elem-B)
4866 "Non-nil when elements ELEM-A and ELEM-B are nested."
4867 (let ((beg-A (org-element-property :begin elem-A))
4868 (beg-B (org-element-property :begin elem-B))
4869 (end-A (org-element-property :end elem-A))
4870 (end-B (org-element-property :end elem-B)))
4871 (or (and (>= beg-A beg-B) (<= end-A end-B))
4872 (and (>= beg-B beg-A) (<= end-B end-A)))))
4874 (defun org-element-swap-A-B (elem-A elem-B)
4875 "Swap elements ELEM-A and ELEM-B.
4876 Assume ELEM-B is after ELEM-A in the buffer. Leave point at the
4877 end of ELEM-A."
4878 (goto-char (org-element-property :begin elem-A))
4879 ;; There are two special cases when an element doesn't start at bol:
4880 ;; the first paragraph in an item or in a footnote definition.
4881 (let ((specialp (not (bolp))))
4882 ;; Only a paragraph without any affiliated keyword can be moved at
4883 ;; ELEM-A position in such a situation. Note that the case of
4884 ;; a footnote definition is impossible: it cannot contain two
4885 ;; paragraphs in a row because it cannot contain a blank line.
4886 (if (and specialp
4887 (or (not (eq (org-element-type elem-B) 'paragraph))
4888 (/= (org-element-property :begin elem-B)
4889 (org-element-property :contents-begin elem-B))))
4890 (error "Cannot swap elements"))
4891 ;; In a special situation, ELEM-A will have no indentation. We'll
4892 ;; give it ELEM-B's (which will in, in turn, have no indentation).
4893 (let* ((ind-B (when specialp
4894 (goto-char (org-element-property :begin elem-B))
4895 (org-get-indentation)))
4896 (beg-A (org-element-property :begin elem-A))
4897 (end-A (save-excursion
4898 (goto-char (org-element-property :end elem-A))
4899 (skip-chars-backward " \r\t\n")
4900 (point-at-eol)))
4901 (beg-B (org-element-property :begin elem-B))
4902 (end-B (save-excursion
4903 (goto-char (org-element-property :end elem-B))
4904 (skip-chars-backward " \r\t\n")
4905 (point-at-eol)))
4906 ;; Store overlays responsible for visibility status. We
4907 ;; also need to store their boundaries as they will be
4908 ;; removed from buffer.
4909 (overlays
4910 (cons
4911 (mapcar (lambda (ov) (list ov (overlay-start ov) (overlay-end ov)))
4912 (overlays-in beg-A end-A))
4913 (mapcar (lambda (ov) (list ov (overlay-start ov) (overlay-end ov)))
4914 (overlays-in beg-B end-B))))
4915 ;; Get contents.
4916 (body-A (buffer-substring beg-A end-A))
4917 (body-B (delete-and-extract-region beg-B end-B)))
4918 (goto-char beg-B)
4919 (when specialp
4920 (setq body-B (replace-regexp-in-string "\\`[ \t]*" "" body-B))
4921 (org-indent-to-column ind-B))
4922 (insert body-A)
4923 ;; Restore ex ELEM-A overlays.
4924 (let ((offset (- beg-B beg-A)))
4925 (mapc (lambda (ov)
4926 (move-overlay
4927 (car ov) (+ (nth 1 ov) offset) (+ (nth 2 ov) offset)))
4928 (car overlays))
4929 (goto-char beg-A)
4930 (delete-region beg-A end-A)
4931 (insert body-B)
4932 ;; Restore ex ELEM-B overlays.
4933 (mapc (lambda (ov)
4934 (move-overlay
4935 (car ov) (- (nth 1 ov) offset) (- (nth 2 ov) offset)))
4936 (cdr overlays)))
4937 (goto-char (org-element-property :end elem-B)))))
4939 (provide 'org-element)
4941 ;; Local variables:
4942 ;; generated-autoload-file: "org-loaddefs.el"
4943 ;; End:
4945 ;;; org-element.el ends here