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