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