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