Merge branch 'maint'
[org-mode.git] / lisp / org-element.el
blobba80c2a8b8191d2f64dab7c011c5516492279c4b
1 ;;; org-element.el --- Parser And Applications for Org syntax
3 ;; Copyright (C) 2012-2014 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', `section' and `table-row' types), it
35 ;; can also accept a fixed set of keywords as attributes. Those are
36 ;; called "affiliated keywords" to distinguish them from other
37 ;; keywords, which are full-fledged elements. Almost all affiliated
38 ;; keywords are referenced in `org-element-affiliated-keywords'; the
39 ;; others are export attributes and start with "ATTR_" prefix.
41 ;; Element containing other elements (and only elements) are called
42 ;; greater elements. Concerned types are: `center-block', `drawer',
43 ;; `dynamic-block', `footnote-definition', `headline', `inlinetask',
44 ;; `item', `plain-list', `property-drawer', `quote-block', `section'
45 ;; and `special-block'.
47 ;; Other element types are: `babel-call', `clock', `comment',
48 ;; `comment-block', `diary-sexp', `example-block', `export-block',
49 ;; `fixed-width', `horizontal-rule', `keyword', `latex-environment',
50 ;; `node-property', `paragraph', `planning', `src-block', `table',
51 ;; `table-row' and `verse-block'. Among them, `paragraph' and
52 ;; `verse-block' types can contain Org objects and plain text.
54 ;; Objects are related to document's contents. Some of them are
55 ;; recursive. Associated types are of the following: `bold', `code',
56 ;; `entity', `export-snippet', `footnote-reference',
57 ;; `inline-babel-call', `inline-src-block', `italic',
58 ;; `latex-fragment', `line-break', `link', `macro', `radio-target',
59 ;; `statistics-cookie', `strike-through', `subscript', `superscript',
60 ;; `table-cell', `target', `timestamp', `underline' and `verbatim'.
62 ;; Some elements also have special properties whose value can hold
63 ;; objects themselves (i.e. an item tag or a headline name). Such
64 ;; values are called "secondary strings". Any object belongs to
65 ;; either an element or a secondary string.
67 ;; Notwithstanding affiliated keywords, each greater element, element
68 ;; and object has a fixed set of properties attached to it. Among
69 ;; them, four are shared by all types: `:begin' and `:end', which
70 ;; refer to the beginning and ending buffer positions of the
71 ;; considered element or object, `:post-blank', which holds the number
72 ;; of blank lines, or white spaces, at its end and `:parent' which
73 ;; refers to the element or object containing it. Greater elements,
74 ;; elements and objects containing objects will also have
75 ;; `:contents-begin' and `:contents-end' properties to delimit
76 ;; contents. Eventually, greater elements and elements accepting
77 ;; affiliated keywords will have a `:post-affiliated' property,
78 ;; referring to the buffer position after all such keywords.
80 ;; At the lowest level, a `:parent' property is also attached to any
81 ;; string, as a text property.
83 ;; Lisp-wise, an element or an object can be represented as a list.
84 ;; It follows the pattern (TYPE PROPERTIES CONTENTS), where:
85 ;; TYPE is a symbol describing the Org element or object.
86 ;; PROPERTIES is the property list attached to it. See docstring of
87 ;; appropriate parsing function to get an exhaustive
88 ;; list.
89 ;; CONTENTS is a list of elements, objects or raw strings contained
90 ;; in the current element or object, when applicable.
92 ;; An Org buffer is a nested list of such elements and objects, whose
93 ;; type is `org-data' and properties is nil.
95 ;; The first part of this file defines Org syntax, while the second
96 ;; one provide accessors and setters functions.
98 ;; The next part implements a parser and an interpreter for each
99 ;; element and object type in Org syntax.
101 ;; The following part creates a fully recursive buffer parser. It
102 ;; also provides a tool to map a function to elements or objects
103 ;; matching some criteria in the parse tree. Functions of interest
104 ;; are `org-element-parse-buffer', `org-element-map' and, to a lesser
105 ;; extent, `org-element-parse-secondary-string'.
107 ;; The penultimate part is the cradle of an interpreter for the
108 ;; obtained parse tree: `org-element-interpret-data'.
110 ;; The library ends by furnishing `org-element-at-point' function, and
111 ;; a way to give information about document structure around point
112 ;; with `org-element-context'. A cache mechanism is also provided for
113 ;; these functions.
116 ;;; Code:
118 (eval-when-compile (require 'cl))
119 (require 'org)
120 (require 'avl-tree)
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 and keywords. Note: this
147 ;; is only an indication and need some thorough check.
148 "#\\(?:[+ ]\\|$\\)" "\\|"
149 ;; Drawers (any type) and fixed-width areas. This is also
150 ;; only an indication.
151 ":" "\\|"
152 ;; Horizontal rules.
153 "-\\{5,\\}[ \t]*$" "\\|"
154 ;; LaTeX environments.
155 "\\\\begin{\\([A-Za-z0-9]+\\*?\\)}" "\\|"
156 ;; Planning and Clock lines.
157 (regexp-opt (list org-scheduled-string
158 org-deadline-string
159 org-closed-string
160 org-clock-string))
161 "\\|"
162 ;; Lists.
163 (let ((term (case org-plain-list-ordered-item-terminator
164 (?\) ")") (?. "\\.") (otherwise "[.)]")))
165 (alpha (and org-list-allow-alphabetical "\\|[A-Za-z]")))
166 (concat "\\(?:[-+*]\\|\\(?:[0-9]+" alpha "\\)" term "\\)"
167 "\\(?:[ \t]\\|$\\)"))
168 "\\)\\)")
169 "Regexp to separate paragraphs in an Org buffer.
170 In the case of lines starting with \"#\" and \":\", this regexp
171 is not sufficient to know if point is at a paragraph ending. See
172 `org-element-paragraph-parser' for more information.")
174 (defconst org-element-all-elements
175 '(babel-call center-block clock comment comment-block diary-sexp drawer
176 dynamic-block example-block export-block fixed-width
177 footnote-definition headline horizontal-rule inlinetask item
178 keyword latex-environment node-property paragraph plain-list
179 planning property-drawer quote-block section
180 special-block src-block table table-row verse-block)
181 "Complete list of element types.")
183 (defconst org-element-greater-elements
184 '(center-block drawer dynamic-block footnote-definition headline inlinetask
185 item plain-list property-drawer quote-block section
186 special-block table)
187 "List of recursive element types aka Greater Elements.")
189 (defconst org-element-all-successors
190 '(export-snippet footnote-reference inline-babel-call inline-src-block
191 latex-or-entity line-break link macro plain-link radio-target
192 statistics-cookie sub/superscript table-cell target
193 text-markup timestamp)
194 "Complete list of successors.")
196 (defconst org-element-object-successor-alist
197 '((subscript . sub/superscript) (superscript . sub/superscript)
198 (bold . text-markup) (code . text-markup) (italic . text-markup)
199 (strike-through . text-markup) (underline . text-markup)
200 (verbatim . text-markup) (entity . latex-or-entity)
201 (latex-fragment . latex-or-entity))
202 "Alist of translations between object type and successor name.
203 Sharing the same successor comes handy when, for example, the
204 regexp matching one object can also match the other object.")
206 (defconst org-element-all-objects
207 '(bold code entity export-snippet footnote-reference inline-babel-call
208 inline-src-block italic line-break latex-fragment link macro
209 radio-target statistics-cookie strike-through subscript superscript
210 table-cell target timestamp underline verbatim)
211 "Complete list of object types.")
213 (defconst org-element-recursive-objects
214 '(bold italic link subscript radio-target strike-through superscript
215 table-cell underline)
216 "List of recursive object types.")
218 (defvar org-element-block-name-alist
219 '(("CENTER" . org-element-center-block-parser)
220 ("COMMENT" . org-element-comment-block-parser)
221 ("EXAMPLE" . org-element-example-block-parser)
222 ("QUOTE" . org-element-quote-block-parser)
223 ("SRC" . org-element-src-block-parser)
224 ("VERSE" . org-element-verse-block-parser))
225 "Alist between block names and the associated parsing function.
226 Names must be uppercase. Any block whose name has no association
227 is parsed with `org-element-special-block-parser'.")
229 (defconst org-element-link-type-is-file
230 '("file" "file+emacs" "file+sys" "docview")
231 "List of link types equivalent to \"file\".
232 Only these types can accept search options and an explicit
233 application to open them.")
235 (defconst org-element-affiliated-keywords
236 '("CAPTION" "DATA" "HEADER" "HEADERS" "LABEL" "NAME" "PLOT" "RESNAME" "RESULT"
237 "RESULTS" "SOURCE" "SRCNAME" "TBLNAME")
238 "List of affiliated keywords as strings.
239 By default, all keywords setting attributes (i.e. \"ATTR_LATEX\")
240 are affiliated keywords and need not to be in this list.")
242 (defconst org-element-keyword-translation-alist
243 '(("DATA" . "NAME") ("LABEL" . "NAME") ("RESNAME" . "NAME")
244 ("SOURCE" . "NAME") ("SRCNAME" . "NAME") ("TBLNAME" . "NAME")
245 ("RESULT" . "RESULTS") ("HEADERS" . "HEADER"))
246 "Alist of usual translations for keywords.
247 The key is the old name and the value the new one. The property
248 holding their value will be named after the translated name.")
250 (defconst org-element-multiple-keywords '("CAPTION" "HEADER")
251 "List of affiliated keywords that can occur more than once in an element.
253 Their value will be consed into a list of strings, which will be
254 returned as the value of the property.
256 This list is checked after translations have been applied. See
257 `org-element-keyword-translation-alist'.
259 By default, all keywords setting attributes (i.e. \"ATTR_LATEX\")
260 allow multiple occurrences and need not to be in this list.")
262 (defconst org-element-parsed-keywords '("CAPTION")
263 "List of affiliated keywords whose value can be parsed.
265 Their value will be stored as a secondary string: a list of
266 strings and objects.
268 This list is checked after translations have been applied. See
269 `org-element-keyword-translation-alist'.")
271 (defconst org-element-dual-keywords '("CAPTION" "RESULTS")
272 "List of affiliated keywords which can have a secondary value.
274 In Org syntax, they can be written with optional square brackets
275 before the colons. For example, RESULTS keyword can be
276 associated to a hash value with the following:
278 #+RESULTS[hash-string]: some-source
280 This list is checked after translations have been applied. See
281 `org-element-keyword-translation-alist'.")
283 (defconst org-element-document-properties '("AUTHOR" "DATE" "TITLE")
284 "List of properties associated to the whole document.
285 Any keyword in this list will have its value parsed and stored as
286 a secondary string.")
288 (defconst org-element--affiliated-re
289 (format "[ \t]*#\\+\\(?:%s\\):\\(?: \\|$\\)"
290 (concat
291 ;; Dual affiliated keywords.
292 (format "\\(?1:%s\\)\\(?:\\[\\(.*\\)\\]\\)?"
293 (regexp-opt org-element-dual-keywords))
294 "\\|"
295 ;; Regular affiliated keywords.
296 (format "\\(?1:%s\\)"
297 (regexp-opt
298 (org-remove-if
299 #'(lambda (keyword)
300 (member keyword org-element-dual-keywords))
301 org-element-affiliated-keywords)))
302 "\\|"
303 ;; Export attributes.
304 "\\(?1:ATTR_[-_A-Za-z0-9]+\\)"))
305 "Regexp matching any affiliated keyword.
307 Keyword name is put in match group 1. Moreover, if keyword
308 belongs to `org-element-dual-keywords', put the dual value in
309 match group 2.
311 Don't modify it, set `org-element-affiliated-keywords' instead.")
313 (defconst org-element-object-restrictions
314 (let* ((standard-set
315 (remq 'plain-link (remq 'table-cell org-element-all-successors)))
316 (standard-set-no-line-break (remq 'line-break standard-set)))
317 `((bold ,@standard-set)
318 (footnote-reference ,@standard-set)
319 (headline ,@standard-set-no-line-break)
320 (inlinetask ,@standard-set-no-line-break)
321 (italic ,@standard-set)
322 (item ,@standard-set-no-line-break)
323 (keyword ,@standard-set)
324 ;; Ignore all links excepted plain links in a link description.
325 ;; Also ignore radio-targets and line breaks.
326 (link export-snippet inline-babel-call inline-src-block latex-or-entity
327 macro plain-link statistics-cookie sub/superscript text-markup)
328 (paragraph ,@standard-set)
329 ;; Remove any variable object from radio target as it would
330 ;; prevent it from being properly recognized.
331 (radio-target latex-or-entity sub/superscript)
332 (strike-through ,@standard-set)
333 (subscript ,@standard-set)
334 (superscript ,@standard-set)
335 ;; Ignore inline babel call and inline src block as formulas are
336 ;; possible. Also ignore line breaks and statistics cookies.
337 (table-cell export-snippet footnote-reference latex-or-entity link macro
338 radio-target sub/superscript target text-markup timestamp)
339 (table-row table-cell)
340 (underline ,@standard-set)
341 (verse-block ,@standard-set)))
342 "Alist of objects restrictions.
344 CAR is an element or object type containing objects and CDR is
345 a list of successors that will be called within an element or
346 object of such type.
348 For example, in a `radio-target' object, one can only find
349 entities, latex-fragments, subscript and superscript.
351 This alist also applies to secondary string. For example, an
352 `headline' type element doesn't directly contain objects, but
353 still has an entry since one of its properties (`:title') does.")
355 (defconst org-element-secondary-value-alist
356 '((headline . :title)
357 (inlinetask . :title)
358 (item . :tag)
359 (footnote-reference . :inline-definition))
360 "Alist between element types and location of secondary value.")
362 (defconst org-element-object-variables '(org-link-abbrev-alist-local)
363 "List of buffer-local variables used when parsing objects.
364 These variables are copied to the temporary buffer created by
365 `org-export-secondary-string'.")
369 ;;; Accessors and Setters
371 ;; Provide four accessors: `org-element-type', `org-element-property'
372 ;; `org-element-contents' and `org-element-restriction'.
374 ;; Setter functions allow to modify elements by side effect. There is
375 ;; `org-element-put-property', `org-element-set-contents'. These
376 ;; low-level functions are useful to build a parse tree.
378 ;; `org-element-adopt-element', `org-element-set-element',
379 ;; `org-element-extract-element' and `org-element-insert-before' are
380 ;; high-level functions useful to modify a parse tree.
382 ;; `org-element-secondary-p' is a predicate used to know if a given
383 ;; object belongs to a secondary string.
385 (defsubst org-element-type (element)
386 "Return type of ELEMENT.
388 The function returns the type of the element or object provided.
389 It can also return the following special value:
390 `plain-text' for a string
391 `org-data' for a complete document
392 nil in any other case."
393 (cond
394 ((not (consp element)) (and (stringp element) 'plain-text))
395 ((symbolp (car element)) (car element))))
397 (defsubst org-element-property (property element)
398 "Extract the value from the PROPERTY of an ELEMENT."
399 (if (stringp element) (get-text-property 0 property element)
400 (plist-get (nth 1 element) property)))
402 (defsubst org-element-contents (element)
403 "Extract contents from an ELEMENT."
404 (cond ((not (consp element)) nil)
405 ((symbolp (car element)) (nthcdr 2 element))
406 (t element)))
408 (defsubst org-element-restriction (element)
409 "Return restriction associated to ELEMENT.
410 ELEMENT can be an element, an object or a symbol representing an
411 element or object type."
412 (cdr (assq (if (symbolp element) element (org-element-type element))
413 org-element-object-restrictions)))
415 (defsubst org-element-put-property (element property value)
416 "In ELEMENT set PROPERTY to VALUE.
417 Return modified element."
418 (if (stringp element) (org-add-props element nil property value)
419 (setcar (cdr element) (plist-put (nth 1 element) property value))
420 element))
422 (defsubst org-element-set-contents (element &rest contents)
423 "Set ELEMENT contents to CONTENTS.
424 Return modified element."
425 (cond ((not element) (list contents))
426 ((not (symbolp (car element))) contents)
427 ((cdr element) (setcdr (cdr element) contents))
428 (t (nconc element contents))))
430 (defun org-element-secondary-p (object)
431 "Non-nil when OBJECT belongs to a secondary string.
432 Return value is the property name, as a keyword, or nil."
433 (let* ((parent (org-element-property :parent object))
434 (property (cdr (assq (org-element-type parent)
435 org-element-secondary-value-alist))))
436 (and property
437 (memq object (org-element-property property parent))
438 property)))
440 (defsubst org-element-adopt-elements (parent &rest children)
441 "Append elements to the contents of another element.
443 PARENT is an element or object. CHILDREN can be elements,
444 objects, or a strings.
446 The function takes care of setting `:parent' property for CHILD.
447 Return parent element."
448 ;; Link every child to PARENT. If PARENT is nil, it is a secondary
449 ;; string: parent is the list itself.
450 (mapc (lambda (child)
451 (org-element-put-property child :parent (or parent children)))
452 children)
453 ;; Add CHILDREN at the end of PARENT contents.
454 (when parent
455 (apply 'org-element-set-contents
456 parent
457 (nconc (org-element-contents parent) children)))
458 ;; Return modified PARENT element.
459 (or parent children))
461 (defun org-element-extract-element (element)
462 "Extract ELEMENT from parse tree.
463 Remove element from the parse tree by side-effect, and return it
464 with its `:parent' property stripped out."
465 (let ((parent (org-element-property :parent element))
466 (secondary (org-element-secondary-p element)))
467 (if secondary
468 (org-element-put-property
469 parent secondary
470 (delq element (org-element-property secondary parent)))
471 (apply #'org-element-set-contents
472 parent
473 (delq element (org-element-contents parent))))
474 ;; Return ELEMENT with its :parent removed.
475 (org-element-put-property element :parent nil)))
477 (defun org-element-insert-before (element location)
478 "Insert ELEMENT before LOCATION in parse tree.
479 LOCATION is an element, object or string within the parse tree.
480 Parse tree is modified by side effect."
481 (let* ((parent (org-element-property :parent location))
482 (property (org-element-secondary-p location))
483 (siblings (if property (org-element-property property parent)
484 (org-element-contents parent)))
485 ;; Special case: LOCATION is the first element of an
486 ;; independent secondary string (e.g. :title property). Add
487 ;; ELEMENT in-place.
488 (specialp (and (not property)
489 (eq siblings parent)
490 (eq (car parent) location))))
491 ;; Install ELEMENT at the appropriate POSITION within SIBLINGS.
492 (cond (specialp)
493 ((or (null siblings) (eq (car siblings) location))
494 (push element siblings))
495 ((null location) (nconc siblings (list element)))
496 (t (let ((previous (cadr (memq location (reverse siblings)))))
497 (if (not previous)
498 (error "No location found to insert element")
499 (let ((next (memq previous siblings)))
500 (setcdr next (cons element (cdr next))))))))
501 ;; Store SIBLINGS at appropriate place in parse tree.
502 (cond
503 (specialp (setcdr parent (copy-sequence parent)) (setcar parent element))
504 (property (org-element-put-property parent property siblings))
505 (t (apply #'org-element-set-contents parent siblings)))
506 ;; Set appropriate :parent property.
507 (org-element-put-property element :parent parent)))
509 (defun org-element-set-element (old new)
510 "Replace element or object OLD with element or object NEW.
511 The function takes care of setting `:parent' property for NEW."
512 ;; Ensure OLD and NEW have the same parent.
513 (org-element-put-property new :parent (org-element-property :parent old))
514 (if (or (memq (org-element-type old) '(plain-text nil))
515 (memq (org-element-type new) '(plain-text nil)))
516 ;; We cannot replace OLD with NEW since one of them is not an
517 ;; object or element. We take the long path.
518 (progn (org-element-insert-before new old)
519 (org-element-extract-element old))
520 ;; Since OLD is going to be changed into NEW by side-effect, first
521 ;; make sure that every element or object within NEW has OLD as
522 ;; parent.
523 (dolist (blob (org-element-contents new))
524 (org-element-put-property blob :parent old))
525 ;; Transfer contents.
526 (apply #'org-element-set-contents old (org-element-contents new))
527 ;; Overwrite OLD's properties with NEW's.
528 (setcar (cdr old) (nth 1 new))
529 ;; Transfer type.
530 (setcar old (car new))))
534 ;;; Greater elements
536 ;; For each greater element type, we define a parser and an
537 ;; interpreter.
539 ;; A parser returns the element or object as the list described above.
540 ;; Most of them accepts no argument. Though, exceptions exist. Hence
541 ;; every element containing a secondary string (see
542 ;; `org-element-secondary-value-alist') will accept an optional
543 ;; argument to toggle parsing of that secondary string. Moreover,
544 ;; `item' parser requires current list's structure as its first
545 ;; element.
547 ;; An interpreter accepts two arguments: the list representation of
548 ;; the element or object, and its contents. The latter may be nil,
549 ;; depending on the element or object considered. It returns the
550 ;; appropriate Org syntax, as a string.
552 ;; Parsing functions must follow the naming convention:
553 ;; org-element-TYPE-parser, where TYPE is greater element's type, as
554 ;; defined in `org-element-greater-elements'.
556 ;; Similarly, interpreting functions must follow the naming
557 ;; convention: org-element-TYPE-interpreter.
559 ;; With the exception of `headline' and `item' types, greater elements
560 ;; cannot contain other greater elements of their own type.
562 ;; Beside implementing a parser and an interpreter, adding a new
563 ;; greater element requires to tweak `org-element--current-element'.
564 ;; Moreover, the newly defined type must be added to both
565 ;; `org-element-all-elements' and `org-element-greater-elements'.
568 ;;;; Center Block
570 (defun org-element-center-block-parser (limit affiliated)
571 "Parse a center block.
573 LIMIT bounds the search. AFFILIATED is a list of which CAR is
574 the buffer position at the beginning of the first affiliated
575 keyword and CDR is a plist of affiliated keywords along with
576 their value.
578 Return a list whose CAR is `center-block' and CDR is a plist
579 containing `:begin', `:end', `:contents-begin', `:contents-end',
580 `:post-blank' and `:post-affiliated' keywords.
582 Assume point is at the beginning of the block."
583 (let ((case-fold-search t))
584 (if (not (save-excursion
585 (re-search-forward "^[ \t]*#\\+END_CENTER[ \t]*$" limit t)))
586 ;; Incomplete block: parse it as a paragraph.
587 (org-element-paragraph-parser limit affiliated)
588 (let ((block-end-line (match-beginning 0)))
589 (let* ((begin (car affiliated))
590 (post-affiliated (point))
591 ;; Empty blocks have no contents.
592 (contents-begin (progn (forward-line)
593 (and (< (point) block-end-line)
594 (point))))
595 (contents-end (and contents-begin block-end-line))
596 (pos-before-blank (progn (goto-char block-end-line)
597 (forward-line)
598 (point)))
599 (end (save-excursion
600 (skip-chars-forward " \r\t\n" limit)
601 (if (eobp) (point) (line-beginning-position)))))
602 (list 'center-block
603 (nconc
604 (list :begin begin
605 :end end
606 :contents-begin contents-begin
607 :contents-end contents-end
608 :post-blank (count-lines pos-before-blank end)
609 :post-affiliated post-affiliated)
610 (cdr affiliated))))))))
612 (defun org-element-center-block-interpreter (center-block contents)
613 "Interpret CENTER-BLOCK element as Org syntax.
614 CONTENTS is the contents of the element."
615 (format "#+BEGIN_CENTER\n%s#+END_CENTER" contents))
618 ;;;; Drawer
620 (defun org-element-drawer-parser (limit affiliated)
621 "Parse a drawer.
623 LIMIT bounds the search. AFFILIATED is a list of which CAR is
624 the buffer position at the beginning of the first affiliated
625 keyword and CDR is a plist of affiliated keywords along with
626 their value.
628 Return a list whose CAR is `drawer' and CDR is a plist containing
629 `:drawer-name', `:begin', `:end', `:contents-begin',
630 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
632 Assume point is at beginning of drawer."
633 (let ((case-fold-search t))
634 (if (not (save-excursion (re-search-forward "^[ \t]*:END:[ \t]*$" limit t)))
635 ;; Incomplete drawer: parse it as a paragraph.
636 (org-element-paragraph-parser limit affiliated)
637 (save-excursion
638 (let* ((drawer-end-line (match-beginning 0))
639 (name (progn (looking-at org-drawer-regexp)
640 (org-match-string-no-properties 1)))
641 (begin (car affiliated))
642 (post-affiliated (point))
643 ;; Empty drawers have no contents.
644 (contents-begin (progn (forward-line)
645 (and (< (point) drawer-end-line)
646 (point))))
647 (contents-end (and contents-begin drawer-end-line))
648 (pos-before-blank (progn (goto-char drawer-end-line)
649 (forward-line)
650 (point)))
651 (end (progn (skip-chars-forward " \r\t\n" limit)
652 (if (eobp) (point) (line-beginning-position)))))
653 (list 'drawer
654 (nconc
655 (list :begin begin
656 :end end
657 :drawer-name name
658 :contents-begin contents-begin
659 :contents-end contents-end
660 :post-blank (count-lines pos-before-blank end)
661 :post-affiliated post-affiliated)
662 (cdr affiliated))))))))
664 (defun org-element-drawer-interpreter (drawer contents)
665 "Interpret DRAWER element as Org syntax.
666 CONTENTS is the contents of the element."
667 (format ":%s:\n%s:END:"
668 (org-element-property :drawer-name drawer)
669 contents))
672 ;;;; Dynamic Block
674 (defun org-element-dynamic-block-parser (limit affiliated)
675 "Parse a dynamic block.
677 LIMIT bounds the search. AFFILIATED is a list of which CAR is
678 the buffer position at the beginning of the first affiliated
679 keyword and CDR is a plist of affiliated keywords along with
680 their value.
682 Return a list whose CAR is `dynamic-block' and CDR is a plist
683 containing `:block-name', `:begin', `:end', `:contents-begin',
684 `:contents-end', `:arguments', `:post-blank' and
685 `:post-affiliated' keywords.
687 Assume point is at beginning of dynamic block."
688 (let ((case-fold-search t))
689 (if (not (save-excursion
690 (re-search-forward "^[ \t]*#\\+END:?[ \t]*$" limit t)))
691 ;; Incomplete block: parse it as a paragraph.
692 (org-element-paragraph-parser limit affiliated)
693 (let ((block-end-line (match-beginning 0)))
694 (save-excursion
695 (let* ((name (progn (looking-at org-dblock-start-re)
696 (org-match-string-no-properties 1)))
697 (arguments (org-match-string-no-properties 3))
698 (begin (car affiliated))
699 (post-affiliated (point))
700 ;; Empty blocks have no contents.
701 (contents-begin (progn (forward-line)
702 (and (< (point) block-end-line)
703 (point))))
704 (contents-end (and contents-begin block-end-line))
705 (pos-before-blank (progn (goto-char block-end-line)
706 (forward-line)
707 (point)))
708 (end (progn (skip-chars-forward " \r\t\n" limit)
709 (if (eobp) (point) (line-beginning-position)))))
710 (list 'dynamic-block
711 (nconc
712 (list :begin begin
713 :end end
714 :block-name name
715 :arguments arguments
716 :contents-begin contents-begin
717 :contents-end contents-end
718 :post-blank (count-lines pos-before-blank end)
719 :post-affiliated post-affiliated)
720 (cdr affiliated)))))))))
722 (defun org-element-dynamic-block-interpreter (dynamic-block contents)
723 "Interpret DYNAMIC-BLOCK element as Org syntax.
724 CONTENTS is the contents of the element."
725 (format "#+BEGIN: %s%s\n%s#+END:"
726 (org-element-property :block-name dynamic-block)
727 (let ((args (org-element-property :arguments dynamic-block)))
728 (and args (concat " " args)))
729 contents))
732 ;;;; Footnote Definition
734 (defun org-element-footnote-definition-parser (limit affiliated)
735 "Parse a footnote definition.
737 LIMIT bounds the search. AFFILIATED is a list of which CAR is
738 the buffer position at the beginning of the first affiliated
739 keyword and CDR is a plist of affiliated keywords along with
740 their value.
742 Return a list whose CAR is `footnote-definition' and CDR is
743 a plist containing `:label', `:begin' `:end', `:contents-begin',
744 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
746 Assume point is at the beginning of the footnote definition."
747 (save-excursion
748 (let* ((label (progn (looking-at org-footnote-definition-re)
749 (org-match-string-no-properties 1)))
750 (begin (car affiliated))
751 (post-affiliated (point))
752 (ending (save-excursion
753 (if (progn
754 (end-of-line)
755 (re-search-forward
756 (concat org-outline-regexp-bol "\\|"
757 org-footnote-definition-re "\\|"
758 "^\\([ \t]*\n\\)\\{2,\\}") limit 'move))
759 (match-beginning 0)
760 (point))))
761 (contents-begin (progn
762 (search-forward "]")
763 (skip-chars-forward " \r\t\n" ending)
764 (cond ((= (point) ending) nil)
765 ((= (line-beginning-position) begin) (point))
766 (t (line-beginning-position)))))
767 (contents-end (and contents-begin ending))
768 (end (progn (goto-char ending)
769 (skip-chars-forward " \r\t\n" limit)
770 (if (eobp) (point) (line-beginning-position)))))
771 (list 'footnote-definition
772 (nconc
773 (list :label label
774 :begin begin
775 :end end
776 :contents-begin contents-begin
777 :contents-end contents-end
778 :post-blank (count-lines ending end)
779 :post-affiliated post-affiliated)
780 (cdr affiliated))))))
782 (defun org-element-footnote-definition-interpreter (footnote-definition contents)
783 "Interpret FOOTNOTE-DEFINITION element as Org syntax.
784 CONTENTS is the contents of the footnote-definition."
785 (concat (format "[%s]" (org-element-property :label footnote-definition))
787 contents))
790 ;;;; Headline
792 (defun org-element-headline-parser (limit &optional raw-secondary-p)
793 "Parse a headline.
795 Return a list whose CAR is `headline' and CDR is a plist
796 containing `:raw-value', `:title', `:alt-title', `:begin',
797 `:end', `:pre-blank', `:contents-begin' and `:contents-end',
798 `:level', `:priority', `:tags', `:todo-keyword',`:todo-type',
799 `:scheduled', `:deadline', `:closed', `:archivedp', `:commentedp'
800 and `:footnote-section-p' keywords.
802 The plist also contains any property set in the property drawer,
803 with its name in upper cases and colons added at the
804 beginning (i.e. `:CUSTOM_ID').
806 When RAW-SECONDARY-P is non-nil, headline's title will not be
807 parsed as a secondary string, but as a plain string instead.
809 Assume point is at beginning of the headline."
810 (save-excursion
811 (let* ((components (org-heading-components))
812 (level (nth 1 components))
813 (todo (nth 2 components))
814 (todo-type
815 (and todo (if (member todo org-done-keywords) 'done 'todo)))
816 (tags (let ((raw-tags (nth 5 components)))
817 (and raw-tags (org-split-string raw-tags ":"))))
818 (raw-value (or (nth 4 components) ""))
819 (commentedp
820 (let ((case-fold-search nil))
821 (string-match (format "^%s\\( \\|$\\)" org-comment-string)
822 raw-value)))
823 (archivedp (member org-archive-tag tags))
824 (footnote-section-p (and org-footnote-section
825 (string= org-footnote-section raw-value)))
826 ;; Upcase property names. It avoids confusion between
827 ;; properties obtained through property drawer and default
828 ;; properties from the parser (e.g. `:end' and :END:)
829 (standard-props
830 (let (plist)
831 (mapc
832 (lambda (p)
833 (setq plist
834 (plist-put plist
835 (intern (concat ":" (upcase (car p))))
836 (cdr p))))
837 (org-entry-properties nil 'standard))
838 plist))
839 (time-props
840 ;; Read time properties on the line below the headline.
841 (save-excursion
842 (when (progn (forward-line)
843 (looking-at org-planning-or-clock-line-re))
844 (let ((end (line-end-position)) plist)
845 (while (re-search-forward
846 org-keyword-time-not-clock-regexp end t)
847 (goto-char (match-end 1))
848 (skip-chars-forward " \t")
849 (let ((keyword (match-string 1))
850 (time (org-element-timestamp-parser)))
851 (cond ((equal keyword org-scheduled-string)
852 (setq plist (plist-put plist :scheduled time)))
853 ((equal keyword org-deadline-string)
854 (setq plist (plist-put plist :deadline time)))
855 (t (setq plist (plist-put plist :closed time))))))
856 plist))))
857 (begin (point))
858 (end (save-excursion (goto-char (org-end-of-subtree t t))))
859 (pos-after-head (progn (forward-line) (point)))
860 (contents-begin (save-excursion
861 (skip-chars-forward " \r\t\n" end)
862 (and (/= (point) end) (line-beginning-position))))
863 (contents-end (and contents-begin
864 (progn (goto-char end)
865 (skip-chars-backward " \r\t\n")
866 (forward-line)
867 (point)))))
868 ;; Clean RAW-VALUE from any comment string.
869 (when commentedp
870 (let ((case-fold-search nil))
871 (setq raw-value
872 (replace-regexp-in-string
873 (concat (regexp-quote org-comment-string) "\\(?: \\|$\\)")
875 raw-value))))
876 ;; Clean TAGS from archive tag, if any.
877 (when archivedp (setq tags (delete org-archive-tag tags)))
878 (let ((headline
879 (list 'headline
880 (nconc
881 (list :raw-value raw-value
882 :begin begin
883 :end end
884 :pre-blank
885 (if (not contents-begin) 0
886 (count-lines pos-after-head contents-begin))
887 :contents-begin contents-begin
888 :contents-end contents-end
889 :level level
890 :priority (nth 3 components)
891 :tags tags
892 :todo-keyword todo
893 :todo-type todo-type
894 :post-blank (count-lines
895 (if (not contents-end) pos-after-head
896 (goto-char contents-end)
897 (forward-line)
898 (point))
899 end)
900 :footnote-section-p footnote-section-p
901 :archivedp archivedp
902 :commentedp commentedp)
903 time-props
904 standard-props))))
905 (let ((alt-title (org-element-property :ALT_TITLE headline)))
906 (when alt-title
907 (org-element-put-property
908 headline :alt-title
909 (if raw-secondary-p alt-title
910 (org-element-parse-secondary-string
911 alt-title (org-element-restriction 'headline) headline)))))
912 (org-element-put-property
913 headline :title
914 (if raw-secondary-p raw-value
915 (org-element-parse-secondary-string
916 raw-value (org-element-restriction 'headline) headline)))))))
918 (defun org-element-headline-interpreter (headline contents)
919 "Interpret HEADLINE element as Org syntax.
920 CONTENTS is the contents of the element."
921 (let* ((level (org-element-property :level headline))
922 (todo (org-element-property :todo-keyword headline))
923 (priority (org-element-property :priority headline))
924 (title (org-element-interpret-data
925 (org-element-property :title headline)))
926 (tags (let ((tag-list (if (org-element-property :archivedp headline)
927 (cons org-archive-tag
928 (org-element-property :tags headline))
929 (org-element-property :tags headline))))
930 (and tag-list
931 (format ":%s:" (mapconcat 'identity tag-list ":")))))
932 (commentedp (org-element-property :commentedp headline))
933 (pre-blank (or (org-element-property :pre-blank headline) 0))
934 (heading (concat (make-string (org-reduced-level level) ?*)
935 (and todo (concat " " todo))
936 (and commentedp (concat " " org-comment-string))
937 (and priority
938 (format " [#%s]" (char-to-string priority)))
939 (cond ((and org-footnote-section
940 (org-element-property
941 :footnote-section-p headline))
942 (concat " " org-footnote-section))
943 (title (concat " " title))))))
944 (concat heading
945 ;; Align tags.
946 (when tags
947 (cond
948 ((zerop org-tags-column) (format " %s" tags))
949 ((< org-tags-column 0)
950 (concat
951 (make-string
952 (max (- (+ org-tags-column (length heading) (length tags))) 1)
954 tags))
956 (concat
957 (make-string (max (- org-tags-column (length heading)) 1) ? )
958 tags))))
959 (make-string (1+ pre-blank) 10)
960 contents)))
963 ;;;; Inlinetask
965 (defun org-element-inlinetask-parser (limit &optional raw-secondary-p)
966 "Parse an inline task.
968 Return a list whose CAR is `inlinetask' and CDR is a plist
969 containing `:title', `:begin', `:end', `:contents-begin' and
970 `:contents-end', `:level', `:priority', `:raw-value', `:tags',
971 `:todo-keyword', `:todo-type', `:scheduled', `:deadline',
972 `:closed' and `:post-blank' keywords.
974 The plist also contains any property set in the property drawer,
975 with its name in upper cases and colons added at the
976 beginning (i.e. `:CUSTOM_ID').
978 When optional argument RAW-SECONDARY-P is non-nil, inline-task's
979 title will not be parsed as a secondary string, but as a plain
980 string instead.
982 Assume point is at beginning of the inline task."
983 (save-excursion
984 (let* ((begin (point))
985 (components (org-heading-components))
986 (todo (nth 2 components))
987 (todo-type (and todo
988 (if (member todo org-done-keywords) 'done 'todo)))
989 (tags (let ((raw-tags (nth 5 components)))
990 (and raw-tags (org-split-string raw-tags ":"))))
991 (raw-value (or (nth 4 components) ""))
992 ;; Upcase property names. It avoids confusion between
993 ;; properties obtained through property drawer and default
994 ;; properties from the parser (e.g. `:end' and :END:)
995 (standard-props
996 (let (plist)
997 (mapc
998 (lambda (p)
999 (setq plist
1000 (plist-put plist
1001 (intern (concat ":" (upcase (car p))))
1002 (cdr p))))
1003 (org-entry-properties nil 'standard))
1004 plist))
1005 (time-props
1006 ;; Read time properties on the line below the inlinetask
1007 ;; opening string.
1008 (save-excursion
1009 (when (progn (forward-line)
1010 (looking-at org-planning-or-clock-line-re))
1011 (let ((end (line-end-position)) plist)
1012 (while (re-search-forward
1013 org-keyword-time-not-clock-regexp end t)
1014 (goto-char (match-end 1))
1015 (skip-chars-forward " \t")
1016 (let ((keyword (match-string 1))
1017 (time (org-element-timestamp-parser)))
1018 (cond ((equal keyword org-scheduled-string)
1019 (setq plist (plist-put plist :scheduled time)))
1020 ((equal keyword org-deadline-string)
1021 (setq plist (plist-put plist :deadline time)))
1022 (t (setq plist (plist-put plist :closed time))))))
1023 plist))))
1024 (task-end (save-excursion
1025 (end-of-line)
1026 (and (re-search-forward org-outline-regexp-bol limit t)
1027 (org-looking-at-p "END[ \t]*$")
1028 (line-beginning-position))))
1029 (contents-begin (progn (forward-line)
1030 (and task-end (< (point) task-end) (point))))
1031 (contents-end (and contents-begin task-end))
1032 (before-blank (if (not task-end) (point)
1033 (goto-char task-end)
1034 (forward-line)
1035 (point)))
1036 (end (progn (skip-chars-forward " \r\t\n" limit)
1037 (if (eobp) (point) (line-beginning-position))))
1038 (inlinetask
1039 (list 'inlinetask
1040 (nconc
1041 (list :raw-value raw-value
1042 :begin begin
1043 :end end
1044 :contents-begin contents-begin
1045 :contents-end contents-end
1046 :level (nth 1 components)
1047 :priority (nth 3 components)
1048 :tags tags
1049 :todo-keyword todo
1050 :todo-type todo-type
1051 :post-blank (count-lines before-blank end))
1052 time-props
1053 standard-props))))
1054 (org-element-put-property
1055 inlinetask :title
1056 (if raw-secondary-p raw-value
1057 (org-element-parse-secondary-string
1058 raw-value
1059 (org-element-restriction 'inlinetask)
1060 inlinetask))))))
1062 (defun org-element-inlinetask-interpreter (inlinetask contents)
1063 "Interpret INLINETASK element as Org syntax.
1064 CONTENTS is the contents of inlinetask."
1065 (let* ((level (org-element-property :level inlinetask))
1066 (todo (org-element-property :todo-keyword inlinetask))
1067 (priority (org-element-property :priority inlinetask))
1068 (title (org-element-interpret-data
1069 (org-element-property :title inlinetask)))
1070 (tags (let ((tag-list (org-element-property :tags inlinetask)))
1071 (and tag-list
1072 (format ":%s:" (mapconcat 'identity tag-list ":")))))
1073 (task (concat (make-string level ?*)
1074 (and todo (concat " " todo))
1075 (and priority
1076 (format " [#%s]" (char-to-string priority)))
1077 (and title (concat " " title)))))
1078 (concat task
1079 ;; Align tags.
1080 (when tags
1081 (cond
1082 ((zerop org-tags-column) (format " %s" tags))
1083 ((< org-tags-column 0)
1084 (concat
1085 (make-string
1086 (max (- (+ org-tags-column (length task) (length tags))) 1)
1088 tags))
1090 (concat
1091 (make-string (max (- org-tags-column (length task)) 1) ? )
1092 tags))))
1093 ;; Prefer degenerate inlinetasks when there are no
1094 ;; contents.
1095 (when contents
1096 (concat "\n"
1097 contents
1098 (make-string level ?*) " END")))))
1101 ;;;; Item
1103 (defun org-element-item-parser (limit struct &optional raw-secondary-p)
1104 "Parse an item.
1106 STRUCT is the structure of the plain list.
1108 Return a list whose CAR is `item' and CDR is a plist containing
1109 `:bullet', `:begin', `:end', `:contents-begin', `:contents-end',
1110 `:checkbox', `:counter', `:tag', `:structure' and `:post-blank'
1111 keywords.
1113 When optional argument RAW-SECONDARY-P is non-nil, item's tag, if
1114 any, will not be parsed as a secondary string, but as a plain
1115 string instead.
1117 Assume point is at the beginning of the item."
1118 (save-excursion
1119 (beginning-of-line)
1120 (looking-at org-list-full-item-re)
1121 (let* ((begin (point))
1122 (bullet (org-match-string-no-properties 1))
1123 (checkbox (let ((box (org-match-string-no-properties 3)))
1124 (cond ((equal "[ ]" box) 'off)
1125 ((equal "[X]" box) 'on)
1126 ((equal "[-]" box) 'trans))))
1127 (counter (let ((c (org-match-string-no-properties 2)))
1128 (save-match-data
1129 (cond
1130 ((not c) nil)
1131 ((string-match "[A-Za-z]" c)
1132 (- (string-to-char (upcase (match-string 0 c)))
1133 64))
1134 ((string-match "[0-9]+" c)
1135 (string-to-number (match-string 0 c)))))))
1136 (end (progn (goto-char (nth 6 (assq (point) struct)))
1137 (unless (bolp) (forward-line))
1138 (point)))
1139 (contents-begin
1140 (progn (goto-char
1141 ;; Ignore tags in un-ordered lists: they are just
1142 ;; a part of item's body.
1143 (if (and (match-beginning 4)
1144 (save-match-data (string-match "[.)]" bullet)))
1145 (match-beginning 4)
1146 (match-end 0)))
1147 (skip-chars-forward " \r\t\n" limit)
1148 ;; If first line isn't empty, contents really start
1149 ;; at the text after item's meta-data.
1150 (if (= (point-at-bol) begin) (point) (point-at-bol))))
1151 (contents-end (progn (goto-char end)
1152 (skip-chars-backward " \r\t\n")
1153 (forward-line)
1154 (point)))
1155 (item
1156 (list 'item
1157 (list :bullet bullet
1158 :begin begin
1159 :end end
1160 ;; CONTENTS-BEGIN and CONTENTS-END may be
1161 ;; mixed up in the case of an empty item
1162 ;; separated from the next by a blank line.
1163 ;; Thus ensure the former is always the
1164 ;; smallest.
1165 :contents-begin (min contents-begin contents-end)
1166 :contents-end (max contents-begin contents-end)
1167 :checkbox checkbox
1168 :counter counter
1169 :structure struct
1170 :post-blank (count-lines contents-end end)))))
1171 (org-element-put-property
1172 item :tag
1173 (let ((raw-tag (org-list-get-tag begin struct)))
1174 (and raw-tag
1175 (if raw-secondary-p raw-tag
1176 (org-element-parse-secondary-string
1177 raw-tag (org-element-restriction 'item) item))))))))
1179 (defun org-element-item-interpreter (item contents)
1180 "Interpret ITEM element as Org syntax.
1181 CONTENTS is the contents of the element."
1182 (let* ((bullet (let ((bullet (org-element-property :bullet item)))
1183 (org-list-bullet-string
1184 (cond ((not (string-match "[0-9a-zA-Z]" bullet)) "- ")
1185 ((eq org-plain-list-ordered-item-terminator ?\)) "1)")
1186 (t "1.")))))
1187 (checkbox (org-element-property :checkbox item))
1188 (counter (org-element-property :counter item))
1189 (tag (let ((tag (org-element-property :tag item)))
1190 (and tag (org-element-interpret-data tag))))
1191 ;; Compute indentation.
1192 (ind (make-string (length bullet) 32))
1193 (item-starts-with-par-p
1194 (eq (org-element-type (car (org-element-contents item)))
1195 'paragraph)))
1196 ;; Indent contents.
1197 (concat
1198 bullet
1199 (and counter (format "[@%d] " counter))
1200 (case checkbox
1201 (on "[X] ")
1202 (off "[ ] ")
1203 (trans "[-] "))
1204 (and tag (format "%s :: " tag))
1205 (when contents
1206 (let ((contents (replace-regexp-in-string
1207 "\\(^\\)[ \t]*\\S-" ind contents nil nil 1)))
1208 (if item-starts-with-par-p (org-trim contents)
1209 (concat "\n" contents)))))))
1212 ;;;; Plain List
1214 (defun org-element--list-struct (limit)
1215 ;; Return structure of list at point. Internal function. See
1216 ;; `org-list-struct' for details.
1217 (let ((case-fold-search t)
1218 (top-ind limit)
1219 (item-re (org-item-re))
1220 (inlinetask-re (and (featurep 'org-inlinetask) "^\\*+ "))
1221 items struct)
1222 (save-excursion
1223 (catch 'exit
1224 (while t
1225 (cond
1226 ;; At limit: end all items.
1227 ((>= (point) limit)
1228 (throw 'exit
1229 (let ((end (progn (skip-chars-backward " \r\t\n")
1230 (forward-line)
1231 (point))))
1232 (dolist (item items (sort (nconc items struct)
1233 'car-less-than-car))
1234 (setcar (nthcdr 6 item) end)))))
1235 ;; At list end: end all items.
1236 ((looking-at org-list-end-re)
1237 (throw 'exit (dolist (item items (sort (nconc items struct)
1238 'car-less-than-car))
1239 (setcar (nthcdr 6 item) (point)))))
1240 ;; At a new item: end previous sibling.
1241 ((looking-at item-re)
1242 (let ((ind (save-excursion (skip-chars-forward " \t")
1243 (current-column))))
1244 (setq top-ind (min top-ind ind))
1245 (while (and items (<= ind (nth 1 (car items))))
1246 (let ((item (pop items)))
1247 (setcar (nthcdr 6 item) (point))
1248 (push item struct)))
1249 (push (progn (looking-at org-list-full-item-re)
1250 (let ((bullet (match-string-no-properties 1)))
1251 (list (point)
1253 bullet
1254 (match-string-no-properties 2) ; counter
1255 (match-string-no-properties 3) ; checkbox
1256 ;; Description tag.
1257 (and (save-match-data
1258 (string-match "[-+*]" bullet))
1259 (match-string-no-properties 4))
1260 ;; Ending position, unknown so far.
1261 nil)))
1262 items))
1263 (forward-line 1))
1264 ;; Skip empty lines.
1265 ((looking-at "^[ \t]*$") (forward-line))
1266 ;; Skip inline tasks and blank lines along the way.
1267 ((and inlinetask-re (looking-at inlinetask-re))
1268 (forward-line)
1269 (let ((origin (point)))
1270 (when (re-search-forward inlinetask-re limit t)
1271 (if (org-looking-at-p "END[ \t]*$") (forward-line)
1272 (goto-char origin)))))
1273 ;; At some text line. Check if it ends any previous item.
1275 (let ((ind (progn (skip-chars-forward " \t") (current-column))))
1276 (when (<= ind top-ind)
1277 (skip-chars-backward " \r\t\n")
1278 (forward-line))
1279 (while (<= ind (nth 1 (car items)))
1280 (let ((item (pop items)))
1281 (setcar (nthcdr 6 item) (line-beginning-position))
1282 (push item struct)
1283 (unless items
1284 (throw 'exit (sort struct 'car-less-than-car))))))
1285 ;; Skip blocks (any type) and drawers contents.
1286 (cond
1287 ((and (looking-at "#\\+BEGIN\\(:\\|_\\S-+\\)")
1288 (re-search-forward
1289 (format "^[ \t]*#\\+END%s[ \t]*$"
1290 (org-match-string-no-properties 1))
1291 limit t)))
1292 ((and (looking-at org-drawer-regexp)
1293 (re-search-forward "^[ \t]*:END:[ \t]*$" limit t))))
1294 (forward-line))))))))
1296 (defun org-element-plain-list-parser (limit affiliated structure)
1297 "Parse a plain list.
1299 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1300 the buffer position at the beginning of the first affiliated
1301 keyword and CDR is a plist of affiliated keywords along with
1302 their value. STRUCTURE is the structure of the plain list being
1303 parsed.
1305 Return a list whose CAR is `plain-list' and CDR is a plist
1306 containing `:type', `:begin', `:end', `:contents-begin' and
1307 `:contents-end', `:structure', `:post-blank' and
1308 `:post-affiliated' keywords.
1310 Assume point is at the beginning of the list."
1311 (save-excursion
1312 (let* ((struct (or structure (org-element--list-struct limit)))
1313 (type (cond ((org-looking-at-p "[ \t]*[A-Za-z0-9]") 'ordered)
1314 ((nth 5 (assq (point) struct)) 'descriptive)
1315 (t 'unordered)))
1316 (contents-begin (point))
1317 (begin (car affiliated))
1318 (contents-end (let* ((item (assq contents-begin struct))
1319 (ind (nth 1 item))
1320 (pos (nth 6 item)))
1321 (while (and (setq item (assq pos struct))
1322 (= (nth 1 item) ind))
1323 (setq pos (nth 6 item)))
1324 pos))
1325 (end (progn (goto-char contents-end)
1326 (skip-chars-forward " \r\t\n" limit)
1327 (if (= (point) limit) limit (line-beginning-position)))))
1328 ;; Return value.
1329 (list 'plain-list
1330 (nconc
1331 (list :type type
1332 :begin begin
1333 :end end
1334 :contents-begin contents-begin
1335 :contents-end contents-end
1336 :structure struct
1337 :post-blank (count-lines contents-end end)
1338 :post-affiliated contents-begin)
1339 (cdr affiliated))))))
1341 (defun org-element-plain-list-interpreter (plain-list contents)
1342 "Interpret PLAIN-LIST element as Org syntax.
1343 CONTENTS is the contents of the element."
1344 (with-temp-buffer
1345 (insert contents)
1346 (goto-char (point-min))
1347 (org-list-repair)
1348 (buffer-string)))
1351 ;;;; Property Drawer
1353 (defun org-element-property-drawer-parser (limit affiliated)
1354 "Parse a property drawer.
1356 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1357 the buffer position at the beginning of the first affiliated
1358 keyword and CDR is a plist of affiliated keywords along with
1359 their value.
1361 Return a list whose CAR is `property-drawer' and CDR is a plist
1362 containing `:begin', `:end', `:contents-begin', `:contents-end',
1363 `:post-blank' and `:post-affiliated' keywords.
1365 Assume point is at the beginning of the property drawer."
1366 (save-excursion
1367 (let ((case-fold-search t))
1368 (if (not (save-excursion
1369 (re-search-forward "^[ \t]*:END:[ \t]*$" limit t)))
1370 ;; Incomplete drawer: parse it as a paragraph.
1371 (org-element-paragraph-parser limit affiliated)
1372 (save-excursion
1373 (let* ((drawer-end-line (match-beginning 0))
1374 (begin (car affiliated))
1375 (post-affiliated (point))
1376 (contents-begin (progn (forward-line)
1377 (and (< (point) drawer-end-line)
1378 (point))))
1379 (contents-end (and contents-begin drawer-end-line))
1380 (pos-before-blank (progn (goto-char drawer-end-line)
1381 (forward-line)
1382 (point)))
1383 (end (progn (skip-chars-forward " \r\t\n" limit)
1384 (if (eobp) (point) (line-beginning-position)))))
1385 (list 'property-drawer
1386 (nconc
1387 (list :begin begin
1388 :end end
1389 :contents-begin contents-begin
1390 :contents-end contents-end
1391 :post-blank (count-lines pos-before-blank end)
1392 :post-affiliated post-affiliated)
1393 (cdr affiliated)))))))))
1395 (defun org-element-property-drawer-interpreter (property-drawer contents)
1396 "Interpret PROPERTY-DRAWER element as Org syntax.
1397 CONTENTS is the properties within the drawer."
1398 (format ":PROPERTIES:\n%s:END:" contents))
1401 ;;;; Quote Block
1403 (defun org-element-quote-block-parser (limit affiliated)
1404 "Parse a quote block.
1406 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1407 the buffer position at the beginning of the first affiliated
1408 keyword and CDR is a plist of affiliated keywords along with
1409 their value.
1411 Return a list whose CAR is `quote-block' and CDR is a plist
1412 containing `:begin', `:end', `:contents-begin', `:contents-end',
1413 `:post-blank' and `:post-affiliated' keywords.
1415 Assume point is at the beginning of the block."
1416 (let ((case-fold-search t))
1417 (if (not (save-excursion
1418 (re-search-forward "^[ \t]*#\\+END_QUOTE[ \t]*$" limit t)))
1419 ;; Incomplete block: parse it as a paragraph.
1420 (org-element-paragraph-parser limit affiliated)
1421 (let ((block-end-line (match-beginning 0)))
1422 (save-excursion
1423 (let* ((begin (car affiliated))
1424 (post-affiliated (point))
1425 ;; Empty blocks have no contents.
1426 (contents-begin (progn (forward-line)
1427 (and (< (point) block-end-line)
1428 (point))))
1429 (contents-end (and contents-begin block-end-line))
1430 (pos-before-blank (progn (goto-char block-end-line)
1431 (forward-line)
1432 (point)))
1433 (end (progn (skip-chars-forward " \r\t\n" limit)
1434 (if (eobp) (point) (line-beginning-position)))))
1435 (list 'quote-block
1436 (nconc
1437 (list :begin begin
1438 :end end
1439 :contents-begin contents-begin
1440 :contents-end contents-end
1441 :post-blank (count-lines pos-before-blank end)
1442 :post-affiliated post-affiliated)
1443 (cdr affiliated)))))))))
1445 (defun org-element-quote-block-interpreter (quote-block contents)
1446 "Interpret QUOTE-BLOCK element as Org syntax.
1447 CONTENTS is the contents of the element."
1448 (format "#+BEGIN_QUOTE\n%s#+END_QUOTE" contents))
1451 ;;;; Section
1453 (defun org-element-section-parser (limit)
1454 "Parse a section.
1456 LIMIT bounds the search.
1458 Return a list whose CAR is `section' and CDR is a plist
1459 containing `:begin', `:end', `:contents-begin', `contents-end'
1460 and `:post-blank' keywords."
1461 (save-excursion
1462 ;; Beginning of section is the beginning of the first non-blank
1463 ;; line after previous headline.
1464 (let ((begin (point))
1465 (end (progn (org-with-limited-levels (outline-next-heading))
1466 (point)))
1467 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
1468 (forward-line)
1469 (point))))
1470 (list 'section
1471 (list :begin begin
1472 :end end
1473 :contents-begin begin
1474 :contents-end pos-before-blank
1475 :post-blank (count-lines pos-before-blank end))))))
1477 (defun org-element-section-interpreter (section contents)
1478 "Interpret SECTION element as Org syntax.
1479 CONTENTS is the contents of the element."
1480 contents)
1483 ;;;; Special Block
1485 (defun org-element-special-block-parser (limit affiliated)
1486 "Parse a special block.
1488 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1489 the buffer position at the beginning of the first affiliated
1490 keyword and CDR is a plist of affiliated keywords along with
1491 their value.
1493 Return a list whose CAR is `special-block' and CDR is a plist
1494 containing `:type', `:begin', `:end', `:contents-begin',
1495 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
1497 Assume point is at the beginning of the block."
1498 (let* ((case-fold-search t)
1499 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
1500 (upcase (match-string-no-properties 1)))))
1501 (if (not (save-excursion
1502 (re-search-forward
1503 (format "^[ \t]*#\\+END_%s[ \t]*$" (regexp-quote type))
1504 limit t)))
1505 ;; Incomplete block: parse it as a paragraph.
1506 (org-element-paragraph-parser limit affiliated)
1507 (let ((block-end-line (match-beginning 0)))
1508 (save-excursion
1509 (let* ((begin (car affiliated))
1510 (post-affiliated (point))
1511 ;; Empty blocks have no contents.
1512 (contents-begin (progn (forward-line)
1513 (and (< (point) block-end-line)
1514 (point))))
1515 (contents-end (and contents-begin block-end-line))
1516 (pos-before-blank (progn (goto-char block-end-line)
1517 (forward-line)
1518 (point)))
1519 (end (progn (skip-chars-forward " \r\t\n" limit)
1520 (if (eobp) (point) (line-beginning-position)))))
1521 (list 'special-block
1522 (nconc
1523 (list :type type
1524 :begin begin
1525 :end end
1526 :contents-begin contents-begin
1527 :contents-end contents-end
1528 :post-blank (count-lines pos-before-blank end)
1529 :post-affiliated post-affiliated)
1530 (cdr affiliated)))))))))
1532 (defun org-element-special-block-interpreter (special-block contents)
1533 "Interpret SPECIAL-BLOCK element as Org syntax.
1534 CONTENTS is the contents of the element."
1535 (let ((block-type (org-element-property :type special-block)))
1536 (format "#+BEGIN_%s\n%s#+END_%s" block-type contents block-type)))
1540 ;;; Elements
1542 ;; For each element, a parser and an interpreter are also defined.
1543 ;; Both follow the same naming convention used for greater elements.
1545 ;; Also, as for greater elements, adding a new element type is done
1546 ;; through the following steps: implement a parser and an interpreter,
1547 ;; tweak `org-element--current-element' so that it recognizes the new
1548 ;; type and add that new type to `org-element-all-elements'.
1550 ;; As a special case, when the newly defined type is a block type,
1551 ;; `org-element-block-name-alist' has to be modified accordingly.
1554 ;;;; Babel Call
1556 (defun org-element-babel-call-parser (limit affiliated)
1557 "Parse a babel call.
1559 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1560 the buffer position at the beginning of the first affiliated
1561 keyword and CDR is a plist of affiliated keywords along with
1562 their value.
1564 Return a list whose CAR is `babel-call' and CDR is a plist
1565 containing `:begin', `:end', `:value', `:post-blank' and
1566 `:post-affiliated' as keywords."
1567 (save-excursion
1568 (let ((begin (car affiliated))
1569 (post-affiliated (point))
1570 (value (progn (let ((case-fold-search t))
1571 (re-search-forward "call:[ \t]*" nil t))
1572 (buffer-substring-no-properties (point)
1573 (line-end-position))))
1574 (pos-before-blank (progn (forward-line) (point)))
1575 (end (progn (skip-chars-forward " \r\t\n" limit)
1576 (if (eobp) (point) (line-beginning-position)))))
1577 (list 'babel-call
1578 (nconc
1579 (list :begin begin
1580 :end end
1581 :value value
1582 :post-blank (count-lines pos-before-blank end)
1583 :post-affiliated post-affiliated)
1584 (cdr affiliated))))))
1586 (defun org-element-babel-call-interpreter (babel-call contents)
1587 "Interpret BABEL-CALL element as Org syntax.
1588 CONTENTS is nil."
1589 (concat "#+CALL: " (org-element-property :value babel-call)))
1592 ;;;; Clock
1594 (defun org-element-clock-parser (limit)
1595 "Parse a clock.
1597 LIMIT bounds the search.
1599 Return a list whose CAR is `clock' and CDR is a plist containing
1600 `:status', `:value', `:time', `:begin', `:end' and `:post-blank'
1601 as keywords."
1602 (save-excursion
1603 (let* ((case-fold-search nil)
1604 (begin (point))
1605 (value (progn (search-forward org-clock-string (line-end-position) t)
1606 (skip-chars-forward " \t")
1607 (org-element-timestamp-parser)))
1608 (duration (and (search-forward " => " (line-end-position) t)
1609 (progn (skip-chars-forward " \t")
1610 (looking-at "\\(\\S-+\\)[ \t]*$"))
1611 (org-match-string-no-properties 1)))
1612 (status (if duration 'closed 'running))
1613 (post-blank (let ((before-blank (progn (forward-line) (point))))
1614 (skip-chars-forward " \r\t\n" limit)
1615 (skip-chars-backward " \t")
1616 (unless (bolp) (end-of-line))
1617 (count-lines before-blank (point))))
1618 (end (point)))
1619 (list 'clock
1620 (list :status status
1621 :value value
1622 :duration duration
1623 :begin begin
1624 :end end
1625 :post-blank post-blank)))))
1627 (defun org-element-clock-interpreter (clock contents)
1628 "Interpret CLOCK element as Org syntax.
1629 CONTENTS is nil."
1630 (concat org-clock-string " "
1631 (org-element-timestamp-interpreter
1632 (org-element-property :value clock) nil)
1633 (let ((duration (org-element-property :duration clock)))
1634 (and duration
1635 (concat " => "
1636 (apply 'format
1637 "%2s:%02s"
1638 (org-split-string duration ":")))))))
1641 ;;;; Comment
1643 (defun org-element-comment-parser (limit affiliated)
1644 "Parse a comment.
1646 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1647 the buffer position at the beginning of the first affiliated
1648 keyword and CDR is a plist of affiliated keywords along with
1649 their value.
1651 Return a list whose CAR is `comment' and CDR is a plist
1652 containing `:begin', `:end', `:value', `:post-blank',
1653 `:post-affiliated' keywords.
1655 Assume point is at comment beginning."
1656 (save-excursion
1657 (let* ((begin (car affiliated))
1658 (post-affiliated (point))
1659 (value (prog2 (looking-at "[ \t]*# ?")
1660 (buffer-substring-no-properties
1661 (match-end 0) (line-end-position))
1662 (forward-line)))
1663 (com-end
1664 ;; Get comments ending.
1665 (progn
1666 (while (and (< (point) limit) (looking-at "[ \t]*#\\( \\|$\\)"))
1667 ;; Accumulate lines without leading hash and first
1668 ;; whitespace.
1669 (setq value
1670 (concat value
1671 "\n"
1672 (buffer-substring-no-properties
1673 (match-end 0) (line-end-position))))
1674 (forward-line))
1675 (point)))
1676 (end (progn (goto-char com-end)
1677 (skip-chars-forward " \r\t\n" limit)
1678 (if (eobp) (point) (line-beginning-position)))))
1679 (list 'comment
1680 (nconc
1681 (list :begin begin
1682 :end end
1683 :value value
1684 :post-blank (count-lines com-end end)
1685 :post-affiliated post-affiliated)
1686 (cdr affiliated))))))
1688 (defun org-element-comment-interpreter (comment contents)
1689 "Interpret COMMENT element as Org syntax.
1690 CONTENTS is nil."
1691 (replace-regexp-in-string "^" "# " (org-element-property :value comment)))
1694 ;;;; Comment Block
1696 (defun org-element-comment-block-parser (limit affiliated)
1697 "Parse an export block.
1699 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1700 the buffer position at the beginning of the first affiliated
1701 keyword and CDR is a plist of affiliated keywords along with
1702 their value.
1704 Return a list whose CAR is `comment-block' and CDR is a plist
1705 containing `:begin', `:end', `:value', `:post-blank' and
1706 `:post-affiliated' keywords.
1708 Assume point is at comment block beginning."
1709 (let ((case-fold-search t))
1710 (if (not (save-excursion
1711 (re-search-forward "^[ \t]*#\\+END_COMMENT[ \t]*$" limit t)))
1712 ;; Incomplete block: parse it as a paragraph.
1713 (org-element-paragraph-parser limit affiliated)
1714 (let ((contents-end (match-beginning 0)))
1715 (save-excursion
1716 (let* ((begin (car affiliated))
1717 (post-affiliated (point))
1718 (contents-begin (progn (forward-line) (point)))
1719 (pos-before-blank (progn (goto-char contents-end)
1720 (forward-line)
1721 (point)))
1722 (end (progn (skip-chars-forward " \r\t\n" limit)
1723 (if (eobp) (point) (line-beginning-position))))
1724 (value (buffer-substring-no-properties
1725 contents-begin contents-end)))
1726 (list 'comment-block
1727 (nconc
1728 (list :begin begin
1729 :end end
1730 :value value
1731 :post-blank (count-lines pos-before-blank end)
1732 :post-affiliated post-affiliated)
1733 (cdr affiliated)))))))))
1735 (defun org-element-comment-block-interpreter (comment-block contents)
1736 "Interpret COMMENT-BLOCK element as Org syntax.
1737 CONTENTS is nil."
1738 (format "#+BEGIN_COMMENT\n%s#+END_COMMENT"
1739 (org-remove-indentation (org-element-property :value comment-block))))
1742 ;;;; Diary Sexp
1744 (defun org-element-diary-sexp-parser (limit affiliated)
1745 "Parse a diary sexp.
1747 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1748 the buffer position at the beginning of the first affiliated
1749 keyword and CDR is a plist of affiliated keywords along with
1750 their value.
1752 Return a list whose CAR is `diary-sexp' and CDR is a plist
1753 containing `:begin', `:end', `:value', `:post-blank' and
1754 `:post-affiliated' keywords."
1755 (save-excursion
1756 (let ((begin (car affiliated))
1757 (post-affiliated (point))
1758 (value (progn (looking-at "\\(%%(.*\\)[ \t]*$")
1759 (org-match-string-no-properties 1)))
1760 (pos-before-blank (progn (forward-line) (point)))
1761 (end (progn (skip-chars-forward " \r\t\n" limit)
1762 (if (eobp) (point) (line-beginning-position)))))
1763 (list 'diary-sexp
1764 (nconc
1765 (list :value value
1766 :begin begin
1767 :end end
1768 :post-blank (count-lines pos-before-blank end)
1769 :post-affiliated post-affiliated)
1770 (cdr affiliated))))))
1772 (defun org-element-diary-sexp-interpreter (diary-sexp contents)
1773 "Interpret DIARY-SEXP as Org syntax.
1774 CONTENTS is nil."
1775 (org-element-property :value diary-sexp))
1778 ;;;; Example Block
1780 (defun org-element-example-block-parser (limit affiliated)
1781 "Parse an example block.
1783 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1784 the buffer position at the beginning of the first affiliated
1785 keyword and CDR is a plist of affiliated keywords along with
1786 their value.
1788 Return a list whose CAR is `example-block' and CDR is a plist
1789 containing `:begin', `:end', `:number-lines', `:preserve-indent',
1790 `:retain-labels', `:use-labels', `:label-fmt', `:switches',
1791 `:value', `:post-blank' and `:post-affiliated' keywords."
1792 (let ((case-fold-search t))
1793 (if (not (save-excursion
1794 (re-search-forward "^[ \t]*#\\+END_EXAMPLE[ \t]*$" limit t)))
1795 ;; Incomplete block: parse it as a paragraph.
1796 (org-element-paragraph-parser limit affiliated)
1797 (let ((contents-end (match-beginning 0)))
1798 (save-excursion
1799 (let* ((switches
1800 (progn
1801 (looking-at "^[ \t]*#\\+BEGIN_EXAMPLE\\(?: +\\(.*\\)\\)?")
1802 (org-match-string-no-properties 1)))
1803 ;; Switches analysis
1804 (number-lines
1805 (cond ((not switches) nil)
1806 ((string-match "-n\\>" switches) 'new)
1807 ((string-match "+n\\>" switches) 'continued)))
1808 (preserve-indent
1809 (and switches (string-match "-i\\>" switches)))
1810 ;; Should labels be retained in (or stripped from) example
1811 ;; blocks?
1812 (retain-labels
1813 (or (not switches)
1814 (not (string-match "-r\\>" switches))
1815 (and number-lines (string-match "-k\\>" switches))))
1816 ;; What should code-references use - labels or
1817 ;; line-numbers?
1818 (use-labels
1819 (or (not switches)
1820 (and retain-labels
1821 (not (string-match "-k\\>" switches)))))
1822 (label-fmt
1823 (and switches
1824 (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
1825 (match-string 1 switches)))
1826 ;; Standard block parsing.
1827 (begin (car affiliated))
1828 (post-affiliated (point))
1829 (block-ind (progn (skip-chars-forward " \t") (current-column)))
1830 (contents-begin (progn (forward-line) (point)))
1831 (value (org-element-remove-indentation
1832 (org-unescape-code-in-string
1833 (buffer-substring-no-properties
1834 contents-begin contents-end))
1835 block-ind))
1836 (pos-before-blank (progn (goto-char contents-end)
1837 (forward-line)
1838 (point)))
1839 (end (progn (skip-chars-forward " \r\t\n" limit)
1840 (if (eobp) (point) (line-beginning-position)))))
1841 (list 'example-block
1842 (nconc
1843 (list :begin begin
1844 :end end
1845 :value value
1846 :switches switches
1847 :number-lines number-lines
1848 :preserve-indent preserve-indent
1849 :retain-labels retain-labels
1850 :use-labels use-labels
1851 :label-fmt label-fmt
1852 :post-blank (count-lines pos-before-blank end)
1853 :post-affiliated post-affiliated)
1854 (cdr affiliated)))))))))
1856 (defun org-element-example-block-interpreter (example-block contents)
1857 "Interpret EXAMPLE-BLOCK element as Org syntax.
1858 CONTENTS is nil."
1859 (let ((switches (org-element-property :switches example-block))
1860 (value (org-element-property :value example-block)))
1861 (concat "#+BEGIN_EXAMPLE" (and switches (concat " " switches)) "\n"
1862 (org-escape-code-in-string
1863 (if (or org-src-preserve-indentation
1864 (org-element-property :preserve-indent example-block))
1865 value
1866 (org-element-remove-indentation value)))
1867 "#+END_EXAMPLE")))
1870 ;;;; Export Block
1872 (defun org-element-export-block-parser (limit affiliated)
1873 "Parse an export block.
1875 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1876 the buffer position at the beginning of the first affiliated
1877 keyword and CDR is a plist of affiliated keywords along with
1878 their value.
1880 Return a list whose CAR is `export-block' and CDR is a plist
1881 containing `:begin', `:end', `:type', `:value', `:post-blank' and
1882 `:post-affiliated' keywords.
1884 Assume point is at export-block beginning."
1885 (let* ((case-fold-search t)
1886 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
1887 (upcase (org-match-string-no-properties 1)))))
1888 (if (not (save-excursion
1889 (re-search-forward
1890 (format "^[ \t]*#\\+END_%s[ \t]*$" type) limit t)))
1891 ;; Incomplete block: parse it as a paragraph.
1892 (org-element-paragraph-parser limit affiliated)
1893 (let ((contents-end (match-beginning 0)))
1894 (save-excursion
1895 (let* ((begin (car affiliated))
1896 (post-affiliated (point))
1897 (contents-begin (progn (forward-line) (point)))
1898 (pos-before-blank (progn (goto-char contents-end)
1899 (forward-line)
1900 (point)))
1901 (end (progn (skip-chars-forward " \r\t\n" limit)
1902 (if (eobp) (point) (line-beginning-position))))
1903 (value (buffer-substring-no-properties contents-begin
1904 contents-end)))
1905 (list 'export-block
1906 (nconc
1907 (list :begin begin
1908 :end end
1909 :type type
1910 :value value
1911 :post-blank (count-lines pos-before-blank end)
1912 :post-affiliated post-affiliated)
1913 (cdr affiliated)))))))))
1915 (defun org-element-export-block-interpreter (export-block contents)
1916 "Interpret EXPORT-BLOCK element as Org syntax.
1917 CONTENTS is nil."
1918 (let ((type (org-element-property :type export-block)))
1919 (concat (format "#+BEGIN_%s\n" type)
1920 (org-element-property :value export-block)
1921 (format "#+END_%s" type))))
1924 ;;;; Fixed-width
1926 (defun org-element-fixed-width-parser (limit affiliated)
1927 "Parse a fixed-width section.
1929 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1930 the buffer position at the beginning of the first affiliated
1931 keyword and CDR is a plist of affiliated keywords along with
1932 their value.
1934 Return a list whose CAR is `fixed-width' and CDR is a plist
1935 containing `:begin', `:end', `:value', `:post-blank' and
1936 `:post-affiliated' keywords.
1938 Assume point is at the beginning of the fixed-width area."
1939 (save-excursion
1940 (let* ((begin (car affiliated))
1941 (post-affiliated (point))
1942 value
1943 (end-area
1944 (progn
1945 (while (and (< (point) limit)
1946 (looking-at "[ \t]*:\\( \\|$\\)"))
1947 ;; Accumulate text without starting colons.
1948 (setq value
1949 (concat value
1950 (buffer-substring-no-properties
1951 (match-end 0) (point-at-eol))
1952 "\n"))
1953 (forward-line))
1954 (point)))
1955 (end (progn (skip-chars-forward " \r\t\n" limit)
1956 (if (eobp) (point) (line-beginning-position)))))
1957 (list 'fixed-width
1958 (nconc
1959 (list :begin begin
1960 :end end
1961 :value value
1962 :post-blank (count-lines end-area end)
1963 :post-affiliated post-affiliated)
1964 (cdr affiliated))))))
1966 (defun org-element-fixed-width-interpreter (fixed-width contents)
1967 "Interpret FIXED-WIDTH element as Org syntax.
1968 CONTENTS is nil."
1969 (let ((value (org-element-property :value fixed-width)))
1970 (and value
1971 (replace-regexp-in-string
1972 "^" ": "
1973 (if (string-match "\n\\'" value) (substring value 0 -1) value)))))
1976 ;;;; Horizontal Rule
1978 (defun org-element-horizontal-rule-parser (limit affiliated)
1979 "Parse an horizontal rule.
1981 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1982 the buffer position at the beginning of the first affiliated
1983 keyword and CDR is a plist of affiliated keywords along with
1984 their value.
1986 Return a list whose CAR is `horizontal-rule' and CDR is a plist
1987 containing `:begin', `:end', `:post-blank' and `:post-affiliated'
1988 keywords."
1989 (save-excursion
1990 (let ((begin (car affiliated))
1991 (post-affiliated (point))
1992 (post-hr (progn (forward-line) (point)))
1993 (end (progn (skip-chars-forward " \r\t\n" limit)
1994 (if (eobp) (point) (line-beginning-position)))))
1995 (list 'horizontal-rule
1996 (nconc
1997 (list :begin begin
1998 :end end
1999 :post-blank (count-lines post-hr end)
2000 :post-affiliated post-affiliated)
2001 (cdr affiliated))))))
2003 (defun org-element-horizontal-rule-interpreter (horizontal-rule contents)
2004 "Interpret HORIZONTAL-RULE element as Org syntax.
2005 CONTENTS is nil."
2006 "-----")
2009 ;;;; Keyword
2011 (defun org-element-keyword-parser (limit affiliated)
2012 "Parse a keyword at point.
2014 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2015 the buffer position at the beginning of the first affiliated
2016 keyword and CDR is a plist of affiliated keywords along with
2017 their value.
2019 Return a list whose CAR is `keyword' and CDR is a plist
2020 containing `:key', `:value', `:begin', `:end', `:post-blank' and
2021 `:post-affiliated' keywords."
2022 (save-excursion
2023 ;; An orphaned affiliated keyword is considered as a regular
2024 ;; keyword. In this case AFFILIATED is nil, so we take care of
2025 ;; this corner case.
2026 (let ((begin (or (car affiliated) (point)))
2027 (post-affiliated (point))
2028 (key (progn (looking-at "[ \t]*#\\+\\(\\S-+*\\):")
2029 (upcase (org-match-string-no-properties 1))))
2030 (value (org-trim (buffer-substring-no-properties
2031 (match-end 0) (point-at-eol))))
2032 (pos-before-blank (progn (forward-line) (point)))
2033 (end (progn (skip-chars-forward " \r\t\n" limit)
2034 (if (eobp) (point) (line-beginning-position)))))
2035 (list 'keyword
2036 (nconc
2037 (list :key key
2038 :value value
2039 :begin begin
2040 :end end
2041 :post-blank (count-lines pos-before-blank end)
2042 :post-affiliated post-affiliated)
2043 (cdr affiliated))))))
2045 (defun org-element-keyword-interpreter (keyword contents)
2046 "Interpret KEYWORD element as Org syntax.
2047 CONTENTS is nil."
2048 (format "#+%s: %s"
2049 (org-element-property :key keyword)
2050 (org-element-property :value keyword)))
2053 ;;;; Latex Environment
2055 (defun org-element-latex-environment-parser (limit affiliated)
2056 "Parse a LaTeX environment.
2058 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2059 the buffer position at the beginning of the first affiliated
2060 keyword and CDR is a plist of affiliated keywords along with
2061 their value.
2063 Return a list whose CAR is `latex-environment' and CDR is a plist
2064 containing `:begin', `:end', `:value', `:post-blank' and
2065 `:post-affiliated' keywords.
2067 Assume point is at the beginning of the latex environment."
2068 (save-excursion
2069 (let ((case-fold-search t)
2070 (code-begin (point)))
2071 (looking-at "[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}")
2072 (if (not (re-search-forward (format "^[ \t]*\\\\end{%s}[ \t]*$"
2073 (regexp-quote (match-string 1)))
2074 limit t))
2075 ;; Incomplete latex environment: parse it as a paragraph.
2076 (org-element-paragraph-parser limit affiliated)
2077 (let* ((code-end (progn (forward-line) (point)))
2078 (begin (car affiliated))
2079 (value (buffer-substring-no-properties code-begin code-end))
2080 (end (progn (skip-chars-forward " \r\t\n" limit)
2081 (if (eobp) (point) (line-beginning-position)))))
2082 (list 'latex-environment
2083 (nconc
2084 (list :begin begin
2085 :end end
2086 :value value
2087 :post-blank (count-lines code-end end)
2088 :post-affiliated code-begin)
2089 (cdr affiliated))))))))
2091 (defun org-element-latex-environment-interpreter (latex-environment contents)
2092 "Interpret LATEX-ENVIRONMENT element as Org syntax.
2093 CONTENTS is nil."
2094 (org-element-property :value latex-environment))
2097 ;;;; Node Property
2099 (defun org-element-node-property-parser (limit)
2100 "Parse a node-property at point.
2102 LIMIT bounds the search.
2104 Return a list whose CAR is `node-property' and CDR is a plist
2105 containing `:key', `:value', `:begin', `:end' and `:post-blank'
2106 keywords."
2107 (save-excursion
2108 (looking-at org-property-re)
2109 (let ((case-fold-search t)
2110 (begin (point))
2111 (key (org-match-string-no-properties 2))
2112 (value (org-match-string-no-properties 3))
2113 (pos-before-blank (progn (forward-line) (point)))
2114 (end (progn (skip-chars-forward " \r\t\n" limit)
2115 (if (eobp) (point) (point-at-bol)))))
2116 (list 'node-property
2117 (list :key key
2118 :value value
2119 :begin begin
2120 :end end
2121 :post-blank (count-lines pos-before-blank end))))))
2123 (defun org-element-node-property-interpreter (node-property contents)
2124 "Interpret NODE-PROPERTY element as Org syntax.
2125 CONTENTS is nil."
2126 (format org-property-format
2127 (format ":%s:" (org-element-property :key node-property))
2128 (org-element-property :value node-property)))
2131 ;;;; Paragraph
2133 (defun org-element-paragraph-parser (limit affiliated)
2134 "Parse a paragraph.
2136 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2137 the buffer position at the beginning of the first affiliated
2138 keyword and CDR is a plist of affiliated keywords along with
2139 their value.
2141 Return a list whose CAR is `paragraph' and CDR is a plist
2142 containing `:begin', `:end', `:contents-begin' and
2143 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
2145 Assume point is at the beginning of the paragraph."
2146 (save-excursion
2147 (let* ((begin (car affiliated))
2148 (contents-begin (point))
2149 (before-blank
2150 (let ((case-fold-search t))
2151 (end-of-line)
2152 (if (not (re-search-forward
2153 org-element-paragraph-separate limit 'm))
2154 limit
2155 ;; A matching `org-element-paragraph-separate' is not
2156 ;; necessarily the end of the paragraph. In
2157 ;; particular, lines starting with # or : as a first
2158 ;; non-space character are ambiguous. We have check
2159 ;; if they are valid Org syntax (i.e. not an
2160 ;; incomplete keyword).
2161 (beginning-of-line)
2162 (while (not
2164 ;; There's no ambiguity for other symbols or
2165 ;; empty lines: stop here.
2166 (looking-at "[ \t]*\\(?:[^:#]\\|$\\)")
2167 ;; Stop at valid fixed-width areas.
2168 (looking-at "[ \t]*:\\(?: \\|$\\)")
2169 ;; Stop at drawers.
2170 (and (looking-at org-drawer-regexp)
2171 (save-excursion
2172 (re-search-forward
2173 "^[ \t]*:END:[ \t]*$" limit t)))
2174 ;; Stop at valid comments.
2175 (looking-at "[ \t]*#\\(?: \\|$\\)")
2176 ;; Stop at valid dynamic blocks.
2177 (and (looking-at org-dblock-start-re)
2178 (save-excursion
2179 (re-search-forward
2180 "^[ \t]*#\\+END:?[ \t]*$" limit t)))
2181 ;; Stop at valid blocks.
2182 (and (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
2183 (save-excursion
2184 (re-search-forward
2185 (format "^[ \t]*#\\+END_%s[ \t]*$"
2186 (regexp-quote
2187 (org-match-string-no-properties 1)))
2188 limit t)))
2189 ;; Stop at valid latex environments.
2190 (and (looking-at
2191 "[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}")
2192 (save-excursion
2193 (re-search-forward
2194 (format "^[ \t]*\\\\end{%s}[ \t]*$"
2195 (regexp-quote
2196 (org-match-string-no-properties 1)))
2197 limit t)))
2198 ;; Stop at valid keywords.
2199 (looking-at "[ \t]*#\\+\\S-+:")
2200 ;; Skip everything else.
2201 (not
2202 (progn
2203 (end-of-line)
2204 (re-search-forward org-element-paragraph-separate
2205 limit 'm)))))
2206 (beginning-of-line)))
2207 (if (= (point) limit) limit
2208 (goto-char (line-beginning-position)))))
2209 (contents-end (progn (skip-chars-backward " \r\t\n" contents-begin)
2210 (forward-line)
2211 (point)))
2212 (end (progn (skip-chars-forward " \r\t\n" limit)
2213 (if (eobp) (point) (line-beginning-position)))))
2214 (list 'paragraph
2215 (nconc
2216 (list :begin begin
2217 :end end
2218 :contents-begin contents-begin
2219 :contents-end contents-end
2220 :post-blank (count-lines before-blank end)
2221 :post-affiliated contents-begin)
2222 (cdr affiliated))))))
2224 (defun org-element-paragraph-interpreter (paragraph contents)
2225 "Interpret PARAGRAPH element as Org syntax.
2226 CONTENTS is the contents of the element."
2227 contents)
2230 ;;;; Planning
2232 (defun org-element-planning-parser (limit)
2233 "Parse a planning.
2235 LIMIT bounds the search.
2237 Return a list whose CAR is `planning' and CDR is a plist
2238 containing `:closed', `:deadline', `:scheduled', `:begin', `:end'
2239 and `:post-blank' keywords."
2240 (save-excursion
2241 (let* ((case-fold-search nil)
2242 (begin (point))
2243 (post-blank (let ((before-blank (progn (forward-line) (point))))
2244 (skip-chars-forward " \r\t\n" limit)
2245 (skip-chars-backward " \t")
2246 (unless (bolp) (end-of-line))
2247 (count-lines before-blank (point))))
2248 (end (point))
2249 closed deadline scheduled)
2250 (goto-char begin)
2251 (while (re-search-forward org-keyword-time-not-clock-regexp end t)
2252 (goto-char (match-end 1))
2253 (skip-chars-forward " \t" end)
2254 (let ((keyword (match-string 1))
2255 (time (org-element-timestamp-parser)))
2256 (cond ((equal keyword org-closed-string) (setq closed time))
2257 ((equal keyword org-deadline-string) (setq deadline time))
2258 (t (setq scheduled time)))))
2259 (list 'planning
2260 (list :closed closed
2261 :deadline deadline
2262 :scheduled scheduled
2263 :begin begin
2264 :end end
2265 :post-blank post-blank)))))
2267 (defun org-element-planning-interpreter (planning contents)
2268 "Interpret PLANNING element as Org syntax.
2269 CONTENTS is nil."
2270 (mapconcat
2271 'identity
2272 (delq nil
2273 (list (let ((deadline (org-element-property :deadline planning)))
2274 (when deadline
2275 (concat org-deadline-string " "
2276 (org-element-timestamp-interpreter deadline nil))))
2277 (let ((scheduled (org-element-property :scheduled planning)))
2278 (when scheduled
2279 (concat org-scheduled-string " "
2280 (org-element-timestamp-interpreter scheduled nil))))
2281 (let ((closed (org-element-property :closed planning)))
2282 (when closed
2283 (concat org-closed-string " "
2284 (org-element-timestamp-interpreter closed nil))))))
2285 " "))
2288 ;;;; Src Block
2290 (defun org-element-src-block-parser (limit affiliated)
2291 "Parse a src block.
2293 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2294 the buffer position at the beginning of the first affiliated
2295 keyword and CDR is a plist of affiliated keywords along with
2296 their value.
2298 Return a list whose CAR is `src-block' and CDR is a plist
2299 containing `:language', `:switches', `:parameters', `:begin',
2300 `:end', `:number-lines', `:retain-labels', `:use-labels',
2301 `:label-fmt', `:preserve-indent', `:value', `:post-blank' and
2302 `:post-affiliated' keywords.
2304 Assume point is at the beginning of the block."
2305 (let ((case-fold-search t))
2306 (if (not (save-excursion (re-search-forward "^[ \t]*#\\+END_SRC[ \t]*$"
2307 limit t)))
2308 ;; Incomplete block: parse it as a paragraph.
2309 (org-element-paragraph-parser limit affiliated)
2310 (let ((contents-end (match-beginning 0)))
2311 (save-excursion
2312 (let* ((begin (car affiliated))
2313 (post-affiliated (point))
2314 ;; Get language as a string.
2315 (language
2316 (progn
2317 (looking-at
2318 (concat "^[ \t]*#\\+BEGIN_SRC"
2319 "\\(?: +\\(\\S-+\\)\\)?"
2320 "\\(\\(?: +\\(?:-l \".*?\"\\|[-+][A-Za-z]\\)\\)+\\)?"
2321 "\\(.*\\)[ \t]*$"))
2322 (org-match-string-no-properties 1)))
2323 ;; Get switches.
2324 (switches (org-match-string-no-properties 2))
2325 ;; Get parameters.
2326 (parameters (org-match-string-no-properties 3))
2327 ;; Switches analysis
2328 (number-lines
2329 (cond ((not switches) nil)
2330 ((string-match "-n\\>" switches) 'new)
2331 ((string-match "+n\\>" switches) 'continued)))
2332 (preserve-indent (and switches
2333 (string-match "-i\\>" switches)))
2334 (label-fmt
2335 (and switches
2336 (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
2337 (match-string 1 switches)))
2338 ;; Should labels be retained in (or stripped from)
2339 ;; src blocks?
2340 (retain-labels
2341 (or (not switches)
2342 (not (string-match "-r\\>" switches))
2343 (and number-lines (string-match "-k\\>" switches))))
2344 ;; What should code-references use - labels or
2345 ;; line-numbers?
2346 (use-labels
2347 (or (not switches)
2348 (and retain-labels
2349 (not (string-match "-k\\>" switches)))))
2350 ;; Indentation.
2351 (block-ind (progn (skip-chars-forward " \t") (current-column)))
2352 ;; Retrieve code.
2353 (value (org-element-remove-indentation
2354 (org-unescape-code-in-string
2355 (buffer-substring-no-properties
2356 (progn (forward-line) (point)) contents-end))
2357 block-ind))
2358 (pos-before-blank (progn (goto-char contents-end)
2359 (forward-line)
2360 (point)))
2361 ;; Get position after ending blank lines.
2362 (end (progn (skip-chars-forward " \r\t\n" limit)
2363 (if (eobp) (point) (line-beginning-position)))))
2364 (list 'src-block
2365 (nconc
2366 (list :language language
2367 :switches (and (org-string-nw-p switches)
2368 (org-trim switches))
2369 :parameters (and (org-string-nw-p parameters)
2370 (org-trim parameters))
2371 :begin begin
2372 :end end
2373 :number-lines number-lines
2374 :preserve-indent preserve-indent
2375 :retain-labels retain-labels
2376 :use-labels use-labels
2377 :label-fmt label-fmt
2378 :value value
2379 :post-blank (count-lines pos-before-blank end)
2380 :post-affiliated post-affiliated)
2381 (cdr affiliated)))))))))
2383 (defun org-element-src-block-interpreter (src-block contents)
2384 "Interpret SRC-BLOCK element as Org syntax.
2385 CONTENTS is nil."
2386 (let ((lang (org-element-property :language src-block))
2387 (switches (org-element-property :switches src-block))
2388 (params (org-element-property :parameters src-block))
2389 (value
2390 (let ((val (org-element-property :value src-block)))
2391 (cond
2392 ((or org-src-preserve-indentation
2393 (org-element-property :preserve-indent src-block))
2394 val)
2395 ((zerop org-edit-src-content-indentation) val)
2397 (let ((ind (make-string org-edit-src-content-indentation ?\s)))
2398 (replace-regexp-in-string
2399 "\\(^\\)[ \t]*\\S-" ind val nil nil 1)))))))
2400 (concat (format "#+BEGIN_SRC%s\n"
2401 (concat (and lang (concat " " lang))
2402 (and switches (concat " " switches))
2403 (and params (concat " " params))))
2404 (org-escape-code-in-string value)
2405 "#+END_SRC")))
2408 ;;;; Table
2410 (defun org-element-table-parser (limit affiliated)
2411 "Parse a table at point.
2413 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2414 the buffer position at the beginning of the first affiliated
2415 keyword and CDR is a plist of affiliated keywords along with
2416 their value.
2418 Return a list whose CAR is `table' and CDR is a plist containing
2419 `:begin', `:end', `:tblfm', `:type', `:contents-begin',
2420 `:contents-end', `:value', `:post-blank' and `:post-affiliated'
2421 keywords.
2423 Assume point is at the beginning of the table."
2424 (save-excursion
2425 (let* ((case-fold-search t)
2426 (table-begin (point))
2427 (type (if (org-at-table.el-p) 'table.el 'org))
2428 (begin (car affiliated))
2429 (table-end
2430 (if (re-search-forward org-table-any-border-regexp limit 'm)
2431 (goto-char (match-beginning 0))
2432 (point)))
2433 (tblfm (let (acc)
2434 (while (looking-at "[ \t]*#\\+TBLFM: +\\(.*\\)[ \t]*$")
2435 (push (org-match-string-no-properties 1) acc)
2436 (forward-line))
2437 acc))
2438 (pos-before-blank (point))
2439 (end (progn (skip-chars-forward " \r\t\n" limit)
2440 (if (eobp) (point) (line-beginning-position)))))
2441 (list 'table
2442 (nconc
2443 (list :begin begin
2444 :end end
2445 :type type
2446 :tblfm tblfm
2447 ;; Only `org' tables have contents. `table.el' tables
2448 ;; use a `:value' property to store raw table as
2449 ;; a string.
2450 :contents-begin (and (eq type 'org) table-begin)
2451 :contents-end (and (eq type 'org) table-end)
2452 :value (and (eq type 'table.el)
2453 (buffer-substring-no-properties
2454 table-begin table-end))
2455 :post-blank (count-lines pos-before-blank end)
2456 :post-affiliated table-begin)
2457 (cdr affiliated))))))
2459 (defun org-element-table-interpreter (table contents)
2460 "Interpret TABLE element as Org syntax.
2461 CONTENTS is nil."
2462 (if (eq (org-element-property :type table) 'table.el)
2463 (org-remove-indentation (org-element-property :value table))
2464 (concat (with-temp-buffer (insert contents)
2465 (org-table-align)
2466 (buffer-string))
2467 (mapconcat (lambda (fm) (concat "#+TBLFM: " fm))
2468 (reverse (org-element-property :tblfm table))
2469 "\n"))))
2472 ;;;; Table Row
2474 (defun org-element-table-row-parser (limit)
2475 "Parse table row at point.
2477 LIMIT bounds the search.
2479 Return a list whose CAR is `table-row' and CDR is a plist
2480 containing `:begin', `:end', `:contents-begin', `:contents-end',
2481 `:type' and `:post-blank' keywords."
2482 (save-excursion
2483 (let* ((type (if (looking-at "^[ \t]*|-") 'rule 'standard))
2484 (begin (point))
2485 ;; A table rule has no contents. In that case, ensure
2486 ;; CONTENTS-BEGIN matches CONTENTS-END.
2487 (contents-begin (and (eq type 'standard)
2488 (search-forward "|")
2489 (point)))
2490 (contents-end (and (eq type 'standard)
2491 (progn
2492 (end-of-line)
2493 (skip-chars-backward " \t")
2494 (point))))
2495 (end (progn (forward-line) (point))))
2496 (list 'table-row
2497 (list :type type
2498 :begin begin
2499 :end end
2500 :contents-begin contents-begin
2501 :contents-end contents-end
2502 :post-blank 0)))))
2504 (defun org-element-table-row-interpreter (table-row contents)
2505 "Interpret TABLE-ROW element as Org syntax.
2506 CONTENTS is the contents of the table row."
2507 (if (eq (org-element-property :type table-row) 'rule) "|-"
2508 (concat "| " contents)))
2511 ;;;; Verse Block
2513 (defun org-element-verse-block-parser (limit affiliated)
2514 "Parse a verse block.
2516 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2517 the buffer position at the beginning of the first affiliated
2518 keyword and CDR is a plist of affiliated keywords along with
2519 their value.
2521 Return a list whose CAR is `verse-block' and CDR is a plist
2522 containing `:begin', `:end', `:contents-begin', `:contents-end',
2523 `:post-blank' and `:post-affiliated' keywords.
2525 Assume point is at beginning of the block."
2526 (let ((case-fold-search t))
2527 (if (not (save-excursion
2528 (re-search-forward "^[ \t]*#\\+END_VERSE[ \t]*$" limit t)))
2529 ;; Incomplete block: parse it as a paragraph.
2530 (org-element-paragraph-parser limit affiliated)
2531 (let ((contents-end (match-beginning 0)))
2532 (save-excursion
2533 (let* ((begin (car affiliated))
2534 (post-affiliated (point))
2535 (contents-begin (progn (forward-line) (point)))
2536 (pos-before-blank (progn (goto-char contents-end)
2537 (forward-line)
2538 (point)))
2539 (end (progn (skip-chars-forward " \r\t\n" limit)
2540 (if (eobp) (point) (line-beginning-position)))))
2541 (list 'verse-block
2542 (nconc
2543 (list :begin begin
2544 :end end
2545 :contents-begin contents-begin
2546 :contents-end contents-end
2547 :post-blank (count-lines pos-before-blank end)
2548 :post-affiliated post-affiliated)
2549 (cdr affiliated)))))))))
2551 (defun org-element-verse-block-interpreter (verse-block contents)
2552 "Interpret VERSE-BLOCK element as Org syntax.
2553 CONTENTS is verse block contents."
2554 (format "#+BEGIN_VERSE\n%s#+END_VERSE" contents))
2558 ;;; Objects
2560 ;; Unlike to elements, interstices can be found between objects.
2561 ;; That's why, along with the parser, successor functions are provided
2562 ;; for each object. Some objects share the same successor (i.e. `code'
2563 ;; and `verbatim' objects).
2565 ;; A successor must accept a single argument bounding the search. It
2566 ;; will return either a cons cell whose CAR is the object's type, as
2567 ;; a symbol, and CDR the position of its next occurrence, or nil.
2569 ;; Successors follow the naming convention:
2570 ;; org-element-NAME-successor, where NAME is the name of the
2571 ;; successor, as defined in `org-element-all-successors'.
2573 ;; Some object types (i.e. `italic') are recursive. Restrictions on
2574 ;; object types they can contain will be specified in
2575 ;; `org-element-object-restrictions'.
2577 ;; Adding a new type of object is simple. Implement a successor,
2578 ;; a parser, and an interpreter for it, all following the naming
2579 ;; convention. Register type in `org-element-all-objects' and
2580 ;; successor in `org-element-all-successors'. Maybe tweak
2581 ;; restrictions about it, and that's it.
2584 ;;;; Bold
2586 (defun org-element-bold-parser ()
2587 "Parse bold object at point.
2589 Return a list whose CAR is `bold' and CDR is a plist with
2590 `:begin', `:end', `:contents-begin' and `:contents-end' and
2591 `:post-blank' keywords.
2593 Assume point is at the first star marker."
2594 (save-excursion
2595 (unless (bolp) (backward-char 1))
2596 (looking-at org-emph-re)
2597 (let ((begin (match-beginning 2))
2598 (contents-begin (match-beginning 4))
2599 (contents-end (match-end 4))
2600 (post-blank (progn (goto-char (match-end 2))
2601 (skip-chars-forward " \t")))
2602 (end (point)))
2603 (list 'bold
2604 (list :begin begin
2605 :end end
2606 :contents-begin contents-begin
2607 :contents-end contents-end
2608 :post-blank post-blank)))))
2610 (defun org-element-bold-interpreter (bold contents)
2611 "Interpret BOLD object as Org syntax.
2612 CONTENTS is the contents of the object."
2613 (format "*%s*" contents))
2615 (defun org-element-text-markup-successor ()
2616 "Search for the next text-markup object.
2618 Return value is a cons cell whose CAR is a symbol among `bold',
2619 `italic', `underline', `strike-through', `code' and `verbatim'
2620 and CDR is beginning position."
2621 (save-excursion
2622 (unless (bolp) (backward-char))
2623 (when (re-search-forward org-emph-re nil t)
2624 (let ((marker (match-string 3)))
2625 (cons (cond
2626 ((equal marker "*") 'bold)
2627 ((equal marker "/") 'italic)
2628 ((equal marker "_") 'underline)
2629 ((equal marker "+") 'strike-through)
2630 ((equal marker "~") 'code)
2631 ((equal marker "=") 'verbatim)
2632 (t (error "Unknown marker at %d" (match-beginning 3))))
2633 (match-beginning 2))))))
2636 ;;;; Code
2638 (defun org-element-code-parser ()
2639 "Parse code object at point.
2641 Return a list whose CAR is `code' and CDR is a plist with
2642 `:value', `:begin', `:end' and `:post-blank' keywords.
2644 Assume point is at the first tilde marker."
2645 (save-excursion
2646 (unless (bolp) (backward-char 1))
2647 (looking-at org-emph-re)
2648 (let ((begin (match-beginning 2))
2649 (value (org-match-string-no-properties 4))
2650 (post-blank (progn (goto-char (match-end 2))
2651 (skip-chars-forward " \t")))
2652 (end (point)))
2653 (list 'code
2654 (list :value value
2655 :begin begin
2656 :end end
2657 :post-blank post-blank)))))
2659 (defun org-element-code-interpreter (code contents)
2660 "Interpret CODE object as Org syntax.
2661 CONTENTS is nil."
2662 (format "~%s~" (org-element-property :value code)))
2665 ;;;; Entity
2667 (defun org-element-entity-parser ()
2668 "Parse entity at point.
2670 Return a list whose CAR is `entity' and CDR a plist with
2671 `:begin', `:end', `:latex', `:latex-math-p', `:html', `:latin1',
2672 `:utf-8', `:ascii', `:use-brackets-p' and `:post-blank' as
2673 keywords.
2675 Assume point is at the beginning of the entity."
2676 (save-excursion
2677 (looking-at "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)")
2678 (let* ((value (org-entity-get (match-string 1)))
2679 (begin (match-beginning 0))
2680 (bracketsp (string= (match-string 2) "{}"))
2681 (post-blank (progn (goto-char (match-end 1))
2682 (when bracketsp (forward-char 2))
2683 (skip-chars-forward " \t")))
2684 (end (point)))
2685 (list 'entity
2686 (list :name (car value)
2687 :latex (nth 1 value)
2688 :latex-math-p (nth 2 value)
2689 :html (nth 3 value)
2690 :ascii (nth 4 value)
2691 :latin1 (nth 5 value)
2692 :utf-8 (nth 6 value)
2693 :begin begin
2694 :end end
2695 :use-brackets-p bracketsp
2696 :post-blank post-blank)))))
2698 (defun org-element-entity-interpreter (entity contents)
2699 "Interpret ENTITY object as Org syntax.
2700 CONTENTS is nil."
2701 (concat "\\"
2702 (org-element-property :name entity)
2703 (when (org-element-property :use-brackets-p entity) "{}")))
2705 (defun org-element-latex-or-entity-successor ()
2706 "Search for the next latex-fragment or entity object.
2708 Return value is a cons cell whose CAR is `entity' or
2709 `latex-fragment' and CDR is beginning position."
2710 (save-excursion
2711 (unless (bolp) (backward-char))
2712 (let ((matchers (cdr org-latex-regexps))
2713 ;; ENTITY-RE matches both LaTeX commands and Org entities.
2714 (entity-re
2715 "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))
2716 (when (re-search-forward
2717 (concat (mapconcat #'cadr matchers "\\|") "\\|" entity-re) nil t)
2718 (goto-char (match-beginning 0))
2719 (if (looking-at entity-re)
2720 ;; Determine if it's a real entity or a LaTeX command.
2721 (cons (if (org-entity-get (match-string 1)) 'entity 'latex-fragment)
2722 (match-beginning 0))
2723 ;; No entity nor command: point is at a LaTeX fragment.
2724 ;; Determine its type to get the correct beginning position.
2725 (cons 'latex-fragment
2726 (catch 'return
2727 (dolist (e matchers)
2728 (when (looking-at (nth 1 e))
2729 (throw 'return (match-beginning (nth 2 e)))))
2730 (point))))))))
2733 ;;;; Export Snippet
2735 (defun org-element-export-snippet-parser ()
2736 "Parse export snippet at point.
2738 Return a list whose CAR is `export-snippet' and CDR a plist with
2739 `:begin', `:end', `:back-end', `:value' and `:post-blank' as
2740 keywords.
2742 Assume point is at the beginning of the snippet."
2743 (save-excursion
2744 (re-search-forward "@@\\([-A-Za-z0-9]+\\):" nil t)
2745 (let* ((begin (match-beginning 0))
2746 (back-end (org-match-string-no-properties 1))
2747 (value (buffer-substring-no-properties
2748 (point)
2749 (progn (re-search-forward "@@" nil t) (match-beginning 0))))
2750 (post-blank (skip-chars-forward " \t"))
2751 (end (point)))
2752 (list 'export-snippet
2753 (list :back-end back-end
2754 :value value
2755 :begin begin
2756 :end end
2757 :post-blank post-blank)))))
2759 (defun org-element-export-snippet-interpreter (export-snippet contents)
2760 "Interpret EXPORT-SNIPPET object as Org syntax.
2761 CONTENTS is nil."
2762 (format "@@%s:%s@@"
2763 (org-element-property :back-end export-snippet)
2764 (org-element-property :value export-snippet)))
2766 (defun org-element-export-snippet-successor ()
2767 "Search for the next export-snippet object.
2769 Return value is a cons cell whose CAR is `export-snippet' and CDR
2770 its beginning position."
2771 (save-excursion
2772 (let (beg)
2773 (when (and (re-search-forward "@@[-A-Za-z0-9]+:" nil t)
2774 (setq beg (match-beginning 0))
2775 (search-forward "@@" nil t))
2776 (cons 'export-snippet beg)))))
2779 ;;;; Footnote Reference
2781 (defun org-element-footnote-reference-parser ()
2782 "Parse footnote reference at point.
2784 Return a list whose CAR is `footnote-reference' and CDR a plist
2785 with `:label', `:type', `:inline-definition', `:begin', `:end'
2786 and `:post-blank' as keywords."
2787 (save-excursion
2788 (looking-at org-footnote-re)
2789 (let* ((begin (point))
2790 (label (or (org-match-string-no-properties 2)
2791 (org-match-string-no-properties 3)
2792 (and (match-string 1)
2793 (concat "fn:" (org-match-string-no-properties 1)))))
2794 (type (if (or (not label) (match-string 1)) 'inline 'standard))
2795 (inner-begin (match-end 0))
2796 (inner-end
2797 (let ((count 1))
2798 (forward-char)
2799 (while (and (> count 0) (re-search-forward "[][]" nil t))
2800 (if (equal (match-string 0) "[") (incf count) (decf count)))
2801 (1- (point))))
2802 (post-blank (progn (goto-char (1+ inner-end))
2803 (skip-chars-forward " \t")))
2804 (end (point))
2805 (footnote-reference
2806 (list 'footnote-reference
2807 (list :label label
2808 :type type
2809 :begin begin
2810 :end end
2811 :post-blank post-blank))))
2812 (org-element-put-property
2813 footnote-reference :inline-definition
2814 (and (eq type 'inline)
2815 (org-element-parse-secondary-string
2816 (buffer-substring inner-begin inner-end)
2817 (org-element-restriction 'footnote-reference)
2818 footnote-reference))))))
2820 (defun org-element-footnote-reference-interpreter (footnote-reference contents)
2821 "Interpret FOOTNOTE-REFERENCE object as Org syntax.
2822 CONTENTS is nil."
2823 (let ((label (or (org-element-property :label footnote-reference) "fn:"))
2824 (def
2825 (let ((inline-def
2826 (org-element-property :inline-definition footnote-reference)))
2827 (if (not inline-def) ""
2828 (concat ":" (org-element-interpret-data inline-def))))))
2829 (format "[%s]" (concat label def))))
2831 (defun org-element-footnote-reference-successor ()
2832 "Search for the next footnote-reference object.
2834 Return value is a cons cell whose CAR is `footnote-reference' and
2835 CDR is beginning position."
2836 (save-excursion
2837 (catch 'exit
2838 (while (re-search-forward org-footnote-re nil t)
2839 (save-excursion
2840 (let ((beg (match-beginning 0))
2841 (count 1))
2842 (backward-char)
2843 (while (re-search-forward "[][]" nil t)
2844 (if (equal (match-string 0) "[") (incf count) (decf count))
2845 (when (zerop count)
2846 (throw 'exit (cons 'footnote-reference beg))))))))))
2849 ;;;; Inline Babel Call
2851 (defun org-element-inline-babel-call-parser ()
2852 "Parse inline babel call at point.
2854 Return a list whose CAR is `inline-babel-call' and CDR a plist
2855 with `:begin', `:end', `:value' and `:post-blank' as keywords.
2857 Assume point is at the beginning of the babel call."
2858 (save-excursion
2859 (unless (bolp) (backward-char))
2860 (let ((case-fold-search t))
2861 (looking-at org-babel-inline-lob-one-liner-regexp))
2862 (let ((begin (match-end 1))
2863 (value (buffer-substring-no-properties (match-end 1) (match-end 0)))
2864 (post-blank (progn (goto-char (match-end 0))
2865 (skip-chars-forward " \t")))
2866 (end (point)))
2867 (list 'inline-babel-call
2868 (list :begin begin
2869 :end end
2870 :value value
2871 :post-blank post-blank)))))
2873 (defun org-element-inline-babel-call-interpreter (inline-babel-call contents)
2874 "Interpret INLINE-BABEL-CALL object as Org syntax.
2875 CONTENTS is nil."
2876 (org-element-property :value inline-babel-call))
2878 (defun org-element-inline-babel-call-successor ()
2879 "Search for the next inline-babel-call object.
2881 Return value is a cons cell whose CAR is `inline-babel-call' and
2882 CDR is beginning position."
2883 (save-excursion
2884 (when (re-search-forward org-babel-inline-lob-one-liner-regexp nil t)
2885 (cons 'inline-babel-call (match-end 1)))))
2888 ;;;; Inline Src Block
2890 (defun org-element-inline-src-block-parser ()
2891 "Parse inline source block at point.
2893 Return a list whose CAR is `inline-src-block' and CDR a plist
2894 with `:begin', `:end', `:language', `:value', `:parameters' and
2895 `:post-blank' as keywords.
2897 Assume point is at the beginning of the inline src block."
2898 (save-excursion
2899 (unless (bolp) (backward-char))
2900 (looking-at org-babel-inline-src-block-regexp)
2901 (let ((begin (match-beginning 1))
2902 (language (org-match-string-no-properties 2))
2903 (parameters (org-match-string-no-properties 4))
2904 (value (org-match-string-no-properties 5))
2905 (post-blank (progn (goto-char (match-end 0))
2906 (skip-chars-forward " \t")))
2907 (end (point)))
2908 (list 'inline-src-block
2909 (list :language language
2910 :value value
2911 :parameters parameters
2912 :begin begin
2913 :end end
2914 :post-blank post-blank)))))
2916 (defun org-element-inline-src-block-interpreter (inline-src-block contents)
2917 "Interpret INLINE-SRC-BLOCK object as Org syntax.
2918 CONTENTS is nil."
2919 (let ((language (org-element-property :language inline-src-block))
2920 (arguments (org-element-property :parameters inline-src-block))
2921 (body (org-element-property :value inline-src-block)))
2922 (format "src_%s%s{%s}"
2923 language
2924 (if arguments (format "[%s]" arguments) "")
2925 body)))
2927 (defun org-element-inline-src-block-successor ()
2928 "Search for the next inline-babel-call element.
2930 Return value is a cons cell whose CAR is `inline-babel-call' and
2931 CDR is beginning position."
2932 (save-excursion
2933 (unless (bolp) (backward-char))
2934 (when (re-search-forward org-babel-inline-src-block-regexp nil t)
2935 (cons 'inline-src-block (match-beginning 1)))))
2937 ;;;; Italic
2939 (defun org-element-italic-parser ()
2940 "Parse italic object at point.
2942 Return a list whose CAR is `italic' and CDR is a plist with
2943 `:begin', `:end', `:contents-begin' and `:contents-end' and
2944 `:post-blank' keywords.
2946 Assume point is at the first slash marker."
2947 (save-excursion
2948 (unless (bolp) (backward-char 1))
2949 (looking-at org-emph-re)
2950 (let ((begin (match-beginning 2))
2951 (contents-begin (match-beginning 4))
2952 (contents-end (match-end 4))
2953 (post-blank (progn (goto-char (match-end 2))
2954 (skip-chars-forward " \t")))
2955 (end (point)))
2956 (list 'italic
2957 (list :begin begin
2958 :end end
2959 :contents-begin contents-begin
2960 :contents-end contents-end
2961 :post-blank post-blank)))))
2963 (defun org-element-italic-interpreter (italic contents)
2964 "Interpret ITALIC object as Org syntax.
2965 CONTENTS is the contents of the object."
2966 (format "/%s/" contents))
2969 ;;;; Latex Fragment
2971 (defun org-element-latex-fragment-parser ()
2972 "Parse LaTeX fragment at point.
2974 Return a list whose CAR is `latex-fragment' and CDR a plist with
2975 `:value', `:begin', `:end', and `:post-blank' as keywords.
2977 Assume point is at the beginning of the LaTeX fragment."
2978 (save-excursion
2979 (let* ((begin (point))
2980 (substring-match
2981 (catch 'exit
2982 (dolist (e (cdr org-latex-regexps))
2983 (let ((latex-regexp (nth 1 e)))
2984 (when (or (looking-at latex-regexp)
2985 (and (not (bobp))
2986 (save-excursion
2987 (backward-char)
2988 (looking-at latex-regexp))))
2989 (throw 'exit (nth 2 e)))))
2990 ;; None found: it's a macro.
2991 (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")
2993 (value (org-match-string-no-properties substring-match))
2994 (post-blank (progn (goto-char (match-end substring-match))
2995 (skip-chars-forward " \t")))
2996 (end (point)))
2997 (list 'latex-fragment
2998 (list :value value
2999 :begin begin
3000 :end end
3001 :post-blank post-blank)))))
3003 (defun org-element-latex-fragment-interpreter (latex-fragment contents)
3004 "Interpret LATEX-FRAGMENT object as Org syntax.
3005 CONTENTS is nil."
3006 (org-element-property :value latex-fragment))
3008 ;;;; Line Break
3010 (defun org-element-line-break-parser ()
3011 "Parse line break at point.
3013 Return a list whose CAR is `line-break', and CDR a plist with
3014 `:begin', `:end' and `:post-blank' keywords.
3016 Assume point is at the beginning of the line break."
3017 (list 'line-break
3018 (list :begin (point)
3019 :end (progn (forward-line) (point))
3020 :post-blank 0)))
3022 (defun org-element-line-break-interpreter (line-break contents)
3023 "Interpret LINE-BREAK object as Org syntax.
3024 CONTENTS is nil."
3025 "\\\\\n")
3027 (defun org-element-line-break-successor ()
3028 "Search for the next line-break object.
3030 Return value is a cons cell whose CAR is `line-break' and CDR is
3031 beginning position."
3032 (save-excursion
3033 (let ((beg (and (re-search-forward "[^\\\\]\\(\\\\\\\\\\)[ \t]*$" nil t)
3034 (goto-char (match-beginning 1)))))
3035 ;; A line break can only happen on a non-empty line.
3036 (when (and beg (re-search-backward "\\S-" (point-at-bol) t))
3037 (cons 'line-break beg)))))
3040 ;;;; Link
3042 (defun org-element-link-parser ()
3043 "Parse link at point.
3045 Return a list whose CAR is `link' and CDR a plist with `:type',
3046 `:path', `:raw-link', `:application', `:search-option', `:begin',
3047 `:end', `:contents-begin', `:contents-end' and `:post-blank' as
3048 keywords.
3050 Assume point is at the beginning of the link."
3051 (save-excursion
3052 (let ((begin (point))
3053 end contents-begin contents-end link-end post-blank path type
3054 raw-link link search-option application)
3055 (cond
3056 ;; Type 1: Text targeted from a radio target.
3057 ((and org-target-link-regexp (looking-at org-target-link-regexp))
3058 (setq type "radio"
3059 link-end (match-end 0)
3060 path (org-match-string-no-properties 0)))
3061 ;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]]
3062 ((looking-at org-bracket-link-regexp)
3063 (setq contents-begin (match-beginning 3)
3064 contents-end (match-end 3)
3065 link-end (match-end 0)
3066 ;; RAW-LINK is the original link. Expand any
3067 ;; abbreviation in it.
3068 raw-link (org-translate-link
3069 (org-link-expand-abbrev
3070 (org-match-string-no-properties 1))))
3071 ;; Determine TYPE of link and set PATH accordingly.
3072 (cond
3073 ;; File type.
3074 ((or (file-name-absolute-p raw-link)
3075 (string-match "^\\.\\.?/" raw-link))
3076 (setq type "file" path raw-link))
3077 ;; Explicit type (http, irc, bbdb...). See `org-link-types'.
3078 ((string-match org-link-re-with-space3 raw-link)
3079 (setq type (match-string 1 raw-link) path (match-string 2 raw-link)))
3080 ;; Id type: PATH is the id.
3081 ((string-match "^id:\\([-a-f0-9]+\\)" raw-link)
3082 (setq type "id" path (match-string 1 raw-link)))
3083 ;; Code-ref type: PATH is the name of the reference.
3084 ((string-match "^(\\(.*\\))$" raw-link)
3085 (setq type "coderef" path (match-string 1 raw-link)))
3086 ;; Custom-id type: PATH is the name of the custom id.
3087 ((= (aref raw-link 0) ?#)
3088 (setq type "custom-id" path (substring raw-link 1)))
3089 ;; Fuzzy type: Internal link either matches a target, an
3090 ;; headline name or nothing. PATH is the target or
3091 ;; headline's name.
3092 (t (setq type "fuzzy" path raw-link))))
3093 ;; Type 3: Plain link, i.e. http://orgmode.org
3094 ((looking-at org-plain-link-re)
3095 (setq raw-link (org-match-string-no-properties 0)
3096 type (org-match-string-no-properties 1)
3097 link-end (match-end 0)
3098 path (org-match-string-no-properties 2)))
3099 ;; Type 4: Angular link, i.e. <http://orgmode.org>
3100 ((looking-at org-angle-link-re)
3101 (setq raw-link (buffer-substring-no-properties
3102 (match-beginning 1) (match-end 2))
3103 type (org-match-string-no-properties 1)
3104 link-end (match-end 0)
3105 path (org-match-string-no-properties 2))))
3106 ;; In any case, deduce end point after trailing white space from
3107 ;; LINK-END variable.
3108 (setq post-blank (progn (goto-char link-end) (skip-chars-forward " \t"))
3109 end (point))
3110 ;; Extract search option and opening application out of
3111 ;; "file"-type links.
3112 (when (member type org-element-link-type-is-file)
3113 ;; Application.
3114 (cond ((string-match "^file\\+\\(.*\\)$" type)
3115 (setq application (match-string 1 type)))
3116 ((not (string-match "^file" type))
3117 (setq application type)))
3118 ;; Extract search option from PATH.
3119 (when (string-match "::\\(.*\\)$" path)
3120 (setq search-option (match-string 1 path)
3121 path (replace-match "" nil nil path)))
3122 ;; Make sure TYPE always reports "file".
3123 (setq type "file"))
3124 (list 'link
3125 (list :type type
3126 :path path
3127 :raw-link (or raw-link path)
3128 :application application
3129 :search-option search-option
3130 :begin begin
3131 :end end
3132 :contents-begin contents-begin
3133 :contents-end contents-end
3134 :post-blank post-blank)))))
3136 (defun org-element-link-interpreter (link contents)
3137 "Interpret LINK object as Org syntax.
3138 CONTENTS is the contents of the object, or nil."
3139 (let ((type (org-element-property :type link))
3140 (raw-link (org-element-property :raw-link link)))
3141 (if (string= type "radio") raw-link
3142 (format "[[%s]%s]"
3143 raw-link
3144 (if contents (format "[%s]" contents) "")))))
3146 (defun org-element-link-successor ()
3147 "Search for the next link object.
3149 Return value is a cons cell whose CAR is `link' and CDR is
3150 beginning position."
3151 (save-excursion
3152 (let ((link-regexp
3153 (if (not org-target-link-regexp) org-any-link-re
3154 (concat org-any-link-re "\\|" org-target-link-regexp))))
3155 (when (re-search-forward link-regexp nil t)
3156 (cons 'link (match-beginning 0))))))
3158 (defun org-element-plain-link-successor ()
3159 "Search for the next plain link object.
3161 Return value is a cons cell whose CAR is `link' and CDR is
3162 beginning position."
3163 (and (save-excursion (re-search-forward org-plain-link-re nil t))
3164 (cons 'link (match-beginning 0))))
3167 ;;;; Macro
3169 (defun org-element-macro-parser ()
3170 "Parse macro at point.
3172 Return a list whose CAR is `macro' and CDR a plist with `:key',
3173 `:args', `:begin', `:end', `:value' and `:post-blank' as
3174 keywords.
3176 Assume point is at the macro."
3177 (save-excursion
3178 (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
3179 (let ((begin (point))
3180 (key (downcase (org-match-string-no-properties 1)))
3181 (value (org-match-string-no-properties 0))
3182 (post-blank (progn (goto-char (match-end 0))
3183 (skip-chars-forward " \t")))
3184 (end (point))
3185 (args (let ((args (org-match-string-no-properties 3)))
3186 (when args
3187 ;; Do not use `org-split-string' since empty
3188 ;; strings are meaningful here.
3189 (split-string
3190 (replace-regexp-in-string
3191 "\\(\\\\*\\)\\(,\\)"
3192 (lambda (str)
3193 (let ((len (length (match-string 1 str))))
3194 (concat (make-string (/ len 2) ?\\)
3195 (if (zerop (mod len 2)) "\000" ","))))
3196 args nil t)
3197 "\000")))))
3198 (list 'macro
3199 (list :key key
3200 :value value
3201 :args args
3202 :begin begin
3203 :end end
3204 :post-blank post-blank)))))
3206 (defun org-element-macro-interpreter (macro contents)
3207 "Interpret MACRO object as Org syntax.
3208 CONTENTS is nil."
3209 (org-element-property :value macro))
3211 (defun org-element-macro-successor ()
3212 "Search for the next macro object.
3214 Return value is cons cell whose CAR is `macro' and CDR is
3215 beginning position."
3216 (save-excursion
3217 (when (re-search-forward
3218 "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
3219 nil t)
3220 (cons 'macro (match-beginning 0)))))
3223 ;;;; Radio-target
3225 (defun org-element-radio-target-parser ()
3226 "Parse radio target at point.
3228 Return a list whose CAR is `radio-target' and CDR a plist with
3229 `:begin', `:end', `:contents-begin', `:contents-end', `:value'
3230 and `:post-blank' as keywords.
3232 Assume point is at the radio target."
3233 (save-excursion
3234 (looking-at org-radio-target-regexp)
3235 (let ((begin (point))
3236 (contents-begin (match-beginning 1))
3237 (contents-end (match-end 1))
3238 (value (org-match-string-no-properties 1))
3239 (post-blank (progn (goto-char (match-end 0))
3240 (skip-chars-forward " \t")))
3241 (end (point)))
3242 (list 'radio-target
3243 (list :begin begin
3244 :end end
3245 :contents-begin contents-begin
3246 :contents-end contents-end
3247 :post-blank post-blank
3248 :value value)))))
3250 (defun org-element-radio-target-interpreter (target contents)
3251 "Interpret TARGET object as Org syntax.
3252 CONTENTS is the contents of the object."
3253 (concat "<<<" contents ">>>"))
3255 (defun org-element-radio-target-successor ()
3256 "Search for the next radio-target object.
3258 Return value is a cons cell whose CAR is `radio-target' and CDR
3259 is beginning position."
3260 (save-excursion
3261 (when (re-search-forward org-radio-target-regexp nil t)
3262 (cons 'radio-target (match-beginning 0)))))
3265 ;;;; Statistics Cookie
3267 (defun org-element-statistics-cookie-parser ()
3268 "Parse statistics cookie at point.
3270 Return a list whose CAR is `statistics-cookie', and CDR a plist
3271 with `:begin', `:end', `:value' and `:post-blank' keywords.
3273 Assume point is at the beginning of the statistics-cookie."
3274 (save-excursion
3275 (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
3276 (let* ((begin (point))
3277 (value (buffer-substring-no-properties
3278 (match-beginning 0) (match-end 0)))
3279 (post-blank (progn (goto-char (match-end 0))
3280 (skip-chars-forward " \t")))
3281 (end (point)))
3282 (list 'statistics-cookie
3283 (list :begin begin
3284 :end end
3285 :value value
3286 :post-blank post-blank)))))
3288 (defun org-element-statistics-cookie-interpreter (statistics-cookie contents)
3289 "Interpret STATISTICS-COOKIE object as Org syntax.
3290 CONTENTS is nil."
3291 (org-element-property :value statistics-cookie))
3293 (defun org-element-statistics-cookie-successor ()
3294 "Search for the next statistics cookie object.
3296 Return value is a cons cell whose CAR is `statistics-cookie' and
3297 CDR is beginning position."
3298 (save-excursion
3299 (when (re-search-forward "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]" nil t)
3300 (cons 'statistics-cookie (match-beginning 0)))))
3303 ;;;; Strike-Through
3305 (defun org-element-strike-through-parser ()
3306 "Parse strike-through object at point.
3308 Return a list whose CAR is `strike-through' and CDR is a plist
3309 with `:begin', `:end', `:contents-begin' and `:contents-end' and
3310 `:post-blank' keywords.
3312 Assume point is at the first plus sign marker."
3313 (save-excursion
3314 (unless (bolp) (backward-char 1))
3315 (looking-at org-emph-re)
3316 (let ((begin (match-beginning 2))
3317 (contents-begin (match-beginning 4))
3318 (contents-end (match-end 4))
3319 (post-blank (progn (goto-char (match-end 2))
3320 (skip-chars-forward " \t")))
3321 (end (point)))
3322 (list 'strike-through
3323 (list :begin begin
3324 :end end
3325 :contents-begin contents-begin
3326 :contents-end contents-end
3327 :post-blank post-blank)))))
3329 (defun org-element-strike-through-interpreter (strike-through contents)
3330 "Interpret STRIKE-THROUGH object as Org syntax.
3331 CONTENTS is the contents of the object."
3332 (format "+%s+" contents))
3335 ;;;; Subscript
3337 (defun org-element-subscript-parser ()
3338 "Parse subscript at point.
3340 Return a list whose CAR is `subscript' and CDR a plist with
3341 `:begin', `:end', `:contents-begin', `:contents-end',
3342 `:use-brackets-p' and `:post-blank' as keywords.
3344 Assume point is at the underscore."
3345 (save-excursion
3346 (unless (bolp) (backward-char))
3347 (looking-at org-match-substring-regexp)
3348 (let ((bracketsp (match-beginning 4))
3349 (begin (match-beginning 2))
3350 (contents-begin (or (match-beginning 4)
3351 (match-beginning 3)))
3352 (contents-end (or (match-end 4) (match-end 3)))
3353 (post-blank (progn (goto-char (match-end 0))
3354 (skip-chars-forward " \t")))
3355 (end (point)))
3356 (list 'subscript
3357 (list :begin begin
3358 :end end
3359 :use-brackets-p bracketsp
3360 :contents-begin contents-begin
3361 :contents-end contents-end
3362 :post-blank post-blank)))))
3364 (defun org-element-subscript-interpreter (subscript contents)
3365 "Interpret SUBSCRIPT object as Org syntax.
3366 CONTENTS is the contents of the object."
3367 (format
3368 (if (org-element-property :use-brackets-p subscript) "_{%s}" "_%s")
3369 contents))
3371 (defun org-element-sub/superscript-successor ()
3372 "Search for the next sub/superscript object.
3374 Return value is a cons cell whose CAR is either `subscript' or
3375 `superscript' and CDR is beginning position."
3376 (save-excursion
3377 (unless (bolp) (backward-char))
3378 (when (re-search-forward org-match-substring-regexp nil t)
3379 (cons (if (string= (match-string 2) "_") 'subscript 'superscript)
3380 (match-beginning 2)))))
3383 ;;;; Superscript
3385 (defun org-element-superscript-parser ()
3386 "Parse superscript at point.
3388 Return a list whose CAR is `superscript' and CDR a plist with
3389 `:begin', `:end', `:contents-begin', `:contents-end',
3390 `:use-brackets-p' and `:post-blank' as keywords.
3392 Assume point is at the caret."
3393 (save-excursion
3394 (unless (bolp) (backward-char))
3395 (looking-at org-match-substring-regexp)
3396 (let ((bracketsp (match-beginning 4))
3397 (begin (match-beginning 2))
3398 (contents-begin (or (match-beginning 4)
3399 (match-beginning 3)))
3400 (contents-end (or (match-end 4) (match-end 3)))
3401 (post-blank (progn (goto-char (match-end 0))
3402 (skip-chars-forward " \t")))
3403 (end (point)))
3404 (list 'superscript
3405 (list :begin begin
3406 :end end
3407 :use-brackets-p bracketsp
3408 :contents-begin contents-begin
3409 :contents-end contents-end
3410 :post-blank post-blank)))))
3412 (defun org-element-superscript-interpreter (superscript contents)
3413 "Interpret SUPERSCRIPT object as Org syntax.
3414 CONTENTS is the contents of the object."
3415 (format
3416 (if (org-element-property :use-brackets-p superscript) "^{%s}" "^%s")
3417 contents))
3420 ;;;; Table Cell
3422 (defun org-element-table-cell-parser ()
3423 "Parse table cell at point.
3425 Return a list whose CAR is `table-cell' and CDR is a plist
3426 containing `:begin', `:end', `:contents-begin', `:contents-end'
3427 and `:post-blank' keywords."
3428 (looking-at "[ \t]*\\(.*?\\)[ \t]*|")
3429 (let* ((begin (match-beginning 0))
3430 (end (match-end 0))
3431 (contents-begin (match-beginning 1))
3432 (contents-end (match-end 1)))
3433 (list 'table-cell
3434 (list :begin begin
3435 :end end
3436 :contents-begin contents-begin
3437 :contents-end contents-end
3438 :post-blank 0))))
3440 (defun org-element-table-cell-interpreter (table-cell contents)
3441 "Interpret TABLE-CELL element as Org syntax.
3442 CONTENTS is the contents of the cell, or nil."
3443 (concat " " contents " |"))
3445 (defun org-element-table-cell-successor ()
3446 "Search for the next table-cell object.
3448 Return value is a cons cell whose CAR is `table-cell' and CDR is
3449 beginning position."
3450 (when (looking-at "[ \t]*.*?[ \t]*|") (cons 'table-cell (point))))
3453 ;;;; Target
3455 (defun org-element-target-parser ()
3456 "Parse target at point.
3458 Return a list whose CAR is `target' and CDR a plist with
3459 `:begin', `:end', `:value' and `:post-blank' as keywords.
3461 Assume point is at the target."
3462 (save-excursion
3463 (looking-at org-target-regexp)
3464 (let ((begin (point))
3465 (value (org-match-string-no-properties 1))
3466 (post-blank (progn (goto-char (match-end 0))
3467 (skip-chars-forward " \t")))
3468 (end (point)))
3469 (list 'target
3470 (list :begin begin
3471 :end end
3472 :value value
3473 :post-blank post-blank)))))
3475 (defun org-element-target-interpreter (target contents)
3476 "Interpret TARGET object as Org syntax.
3477 CONTENTS is nil."
3478 (format "<<%s>>" (org-element-property :value target)))
3480 (defun org-element-target-successor ()
3481 "Search for the next target object.
3483 Return value is a cons cell whose CAR is `target' and CDR is
3484 beginning position."
3485 (save-excursion
3486 (when (re-search-forward org-target-regexp nil t)
3487 (cons 'target (match-beginning 0)))))
3490 ;;;; Timestamp
3492 (defun org-element-timestamp-parser ()
3493 "Parse time stamp at point.
3495 Return a list whose CAR is `timestamp', and CDR a plist with
3496 `:type', `:raw-value', `:year-start', `:month-start',
3497 `:day-start', `:hour-start', `:minute-start', `:year-end',
3498 `:month-end', `:day-end', `:hour-end', `:minute-end',
3499 `:repeater-type', `:repeater-value', `:repeater-unit',
3500 `:warning-type', `:warning-value', `:warning-unit', `:begin',
3501 `:end', `:value' and `:post-blank' keywords.
3503 Assume point is at the beginning of the timestamp."
3504 (save-excursion
3505 (let* ((begin (point))
3506 (activep (eq (char-after) ?<))
3507 (raw-value
3508 (progn
3509 (looking-at "\\([<[]\\(%%\\)?.*?\\)[]>]\\(?:--\\([<[].*?[]>]\\)\\)?")
3510 (match-string-no-properties 0)))
3511 (date-start (match-string-no-properties 1))
3512 (date-end (match-string 3))
3513 (diaryp (match-beginning 2))
3514 (post-blank (progn (goto-char (match-end 0))
3515 (skip-chars-forward " \t")))
3516 (end (point))
3517 (time-range
3518 (and (not diaryp)
3519 (string-match
3520 "[012]?[0-9]:[0-5][0-9]\\(-\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)"
3521 date-start)
3522 (cons (string-to-number (match-string 2 date-start))
3523 (string-to-number (match-string 3 date-start)))))
3524 (type (cond (diaryp 'diary)
3525 ((and activep (or date-end time-range)) 'active-range)
3526 (activep 'active)
3527 ((or date-end time-range) 'inactive-range)
3528 (t 'inactive)))
3529 (repeater-props
3530 (and (not diaryp)
3531 (string-match "\\([.+]?\\+\\)\\([0-9]+\\)\\([hdwmy]\\)"
3532 raw-value)
3533 (list
3534 :repeater-type
3535 (let ((type (match-string 1 raw-value)))
3536 (cond ((equal "++" type) 'catch-up)
3537 ((equal ".+" type) 'restart)
3538 (t 'cumulate)))
3539 :repeater-value (string-to-number (match-string 2 raw-value))
3540 :repeater-unit
3541 (case (string-to-char (match-string 3 raw-value))
3542 (?h 'hour) (?d 'day) (?w 'week) (?m 'month) (t 'year)))))
3543 (warning-props
3544 (and (not diaryp)
3545 (string-match "\\(-\\)?-\\([0-9]+\\)\\([hdwmy]\\)" raw-value)
3546 (list
3547 :warning-type (if (match-string 1 raw-value) 'first 'all)
3548 :warning-value (string-to-number (match-string 2 raw-value))
3549 :warning-unit
3550 (case (string-to-char (match-string 3 raw-value))
3551 (?h 'hour) (?d 'day) (?w 'week) (?m 'month) (t 'year)))))
3552 year-start month-start day-start hour-start minute-start year-end
3553 month-end day-end hour-end minute-end)
3554 ;; Parse date-start.
3555 (unless diaryp
3556 (let ((date (org-parse-time-string date-start t)))
3557 (setq year-start (nth 5 date)
3558 month-start (nth 4 date)
3559 day-start (nth 3 date)
3560 hour-start (nth 2 date)
3561 minute-start (nth 1 date))))
3562 ;; Compute date-end. It can be provided directly in time-stamp,
3563 ;; or extracted from time range. Otherwise, it defaults to the
3564 ;; same values as date-start.
3565 (unless diaryp
3566 (let ((date (and date-end (org-parse-time-string date-end t))))
3567 (setq year-end (or (nth 5 date) year-start)
3568 month-end (or (nth 4 date) month-start)
3569 day-end (or (nth 3 date) day-start)
3570 hour-end (or (nth 2 date) (car time-range) hour-start)
3571 minute-end (or (nth 1 date) (cdr time-range) minute-start))))
3572 (list 'timestamp
3573 (nconc (list :type type
3574 :raw-value raw-value
3575 :year-start year-start
3576 :month-start month-start
3577 :day-start day-start
3578 :hour-start hour-start
3579 :minute-start minute-start
3580 :year-end year-end
3581 :month-end month-end
3582 :day-end day-end
3583 :hour-end hour-end
3584 :minute-end minute-end
3585 :begin begin
3586 :end end
3587 :post-blank post-blank)
3588 repeater-props
3589 warning-props)))))
3591 (defun org-element-timestamp-interpreter (timestamp contents)
3592 "Interpret TIMESTAMP object as Org syntax.
3593 CONTENTS is nil."
3594 (let* ((repeat-string
3595 (concat
3596 (case (org-element-property :repeater-type timestamp)
3597 (cumulate "+") (catch-up "++") (restart ".+"))
3598 (let ((val (org-element-property :repeater-value timestamp)))
3599 (and val (number-to-string val)))
3600 (case (org-element-property :repeater-unit timestamp)
3601 (hour "h") (day "d") (week "w") (month "m") (year "y"))))
3602 (warning-string
3603 (concat
3604 (case (org-element-property :warning-type timestamp)
3605 (first "--")
3606 (all "-"))
3607 (let ((val (org-element-property :warning-value timestamp)))
3608 (and val (number-to-string val)))
3609 (case (org-element-property :warning-unit timestamp)
3610 (hour "h") (day "d") (week "w") (month "m") (year "y"))))
3611 (build-ts-string
3612 ;; Build an Org timestamp string from TIME. ACTIVEP is
3613 ;; non-nil when time stamp is active. If WITH-TIME-P is
3614 ;; non-nil, add a time part. HOUR-END and MINUTE-END
3615 ;; specify a time range in the timestamp. REPEAT-STRING is
3616 ;; the repeater string, if any.
3617 (lambda (time activep &optional with-time-p hour-end minute-end)
3618 (let ((ts (format-time-string
3619 (funcall (if with-time-p 'cdr 'car)
3620 org-time-stamp-formats)
3621 time)))
3622 (when (and hour-end minute-end)
3623 (string-match "[012]?[0-9]:[0-5][0-9]" ts)
3624 (setq ts
3625 (replace-match
3626 (format "\\&-%02d:%02d" hour-end minute-end)
3627 nil nil ts)))
3628 (unless activep (setq ts (format "[%s]" (substring ts 1 -1))))
3629 (dolist (s (list repeat-string warning-string))
3630 (when (org-string-nw-p s)
3631 (setq ts (concat (substring ts 0 -1)
3634 (substring ts -1)))))
3635 ;; Return value.
3636 ts)))
3637 (type (org-element-property :type timestamp)))
3638 (case type
3639 ((active inactive)
3640 (let* ((minute-start (org-element-property :minute-start timestamp))
3641 (minute-end (org-element-property :minute-end timestamp))
3642 (hour-start (org-element-property :hour-start timestamp))
3643 (hour-end (org-element-property :hour-end timestamp))
3644 (time-range-p (and hour-start hour-end minute-start minute-end
3645 (or (/= hour-start hour-end)
3646 (/= minute-start minute-end)))))
3647 (funcall
3648 build-ts-string
3649 (encode-time 0
3650 (or minute-start 0)
3651 (or hour-start 0)
3652 (org-element-property :day-start timestamp)
3653 (org-element-property :month-start timestamp)
3654 (org-element-property :year-start timestamp))
3655 (eq type 'active)
3656 (and hour-start minute-start)
3657 (and time-range-p hour-end)
3658 (and time-range-p minute-end))))
3659 ((active-range inactive-range)
3660 (let ((minute-start (org-element-property :minute-start timestamp))
3661 (minute-end (org-element-property :minute-end timestamp))
3662 (hour-start (org-element-property :hour-start timestamp))
3663 (hour-end (org-element-property :hour-end timestamp)))
3664 (concat
3665 (funcall
3666 build-ts-string (encode-time
3668 (or minute-start 0)
3669 (or hour-start 0)
3670 (org-element-property :day-start timestamp)
3671 (org-element-property :month-start timestamp)
3672 (org-element-property :year-start timestamp))
3673 (eq type 'active-range)
3674 (and hour-start minute-start))
3675 "--"
3676 (funcall build-ts-string
3677 (encode-time 0
3678 (or minute-end 0)
3679 (or hour-end 0)
3680 (org-element-property :day-end timestamp)
3681 (org-element-property :month-end timestamp)
3682 (org-element-property :year-end timestamp))
3683 (eq type 'active-range)
3684 (and hour-end minute-end))))))))
3686 (defun org-element-timestamp-successor ()
3687 "Search for the next timestamp object.
3689 Return value is a cons cell whose CAR is `timestamp' and CDR is
3690 beginning position."
3691 (save-excursion
3692 (when (re-search-forward
3693 (concat org-ts-regexp-both
3694 "\\|"
3695 "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
3696 "\\|"
3697 "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
3698 nil t)
3699 (cons 'timestamp (match-beginning 0)))))
3702 ;;;; Underline
3704 (defun org-element-underline-parser ()
3705 "Parse underline object at point.
3707 Return a list whose CAR is `underline' and CDR is a plist with
3708 `:begin', `:end', `:contents-begin' and `:contents-end' and
3709 `:post-blank' keywords.
3711 Assume point is at the first underscore marker."
3712 (save-excursion
3713 (unless (bolp) (backward-char 1))
3714 (looking-at org-emph-re)
3715 (let ((begin (match-beginning 2))
3716 (contents-begin (match-beginning 4))
3717 (contents-end (match-end 4))
3718 (post-blank (progn (goto-char (match-end 2))
3719 (skip-chars-forward " \t")))
3720 (end (point)))
3721 (list 'underline
3722 (list :begin begin
3723 :end end
3724 :contents-begin contents-begin
3725 :contents-end contents-end
3726 :post-blank post-blank)))))
3728 (defun org-element-underline-interpreter (underline contents)
3729 "Interpret UNDERLINE object as Org syntax.
3730 CONTENTS is the contents of the object."
3731 (format "_%s_" contents))
3734 ;;;; Verbatim
3736 (defun org-element-verbatim-parser ()
3737 "Parse verbatim object at point.
3739 Return a list whose CAR is `verbatim' and CDR is a plist with
3740 `:value', `:begin', `:end' and `:post-blank' keywords.
3742 Assume point is at the first equal sign marker."
3743 (save-excursion
3744 (unless (bolp) (backward-char 1))
3745 (looking-at org-emph-re)
3746 (let ((begin (match-beginning 2))
3747 (value (org-match-string-no-properties 4))
3748 (post-blank (progn (goto-char (match-end 2))
3749 (skip-chars-forward " \t")))
3750 (end (point)))
3751 (list 'verbatim
3752 (list :value value
3753 :begin begin
3754 :end end
3755 :post-blank post-blank)))))
3757 (defun org-element-verbatim-interpreter (verbatim contents)
3758 "Interpret VERBATIM object as Org syntax.
3759 CONTENTS is nil."
3760 (format "=%s=" (org-element-property :value verbatim)))
3764 ;;; Parsing Element Starting At Point
3766 ;; `org-element--current-element' is the core function of this section.
3767 ;; It returns the Lisp representation of the element starting at
3768 ;; point.
3770 ;; `org-element--current-element' makes use of special modes. They
3771 ;; are activated for fixed element chaining (i.e. `plain-list' >
3772 ;; `item') or fixed conditional element chaining (i.e. `headline' >
3773 ;; `section'). Special modes are: `first-section', `item',
3774 ;; `node-property', `section' and `table-row'.
3776 (defun org-element--current-element
3777 (limit &optional granularity special structure)
3778 "Parse the element starting at point.
3780 Return value is a list like (TYPE PROPS) where TYPE is the type
3781 of the element and PROPS a plist of properties associated to the
3782 element.
3784 Possible types are defined in `org-element-all-elements'.
3786 LIMIT bounds the search.
3788 Optional argument GRANULARITY determines the depth of the
3789 recursion. Allowed values are `headline', `greater-element',
3790 `element', `object' or nil. When it is broader than `object' (or
3791 nil), secondary values will not be parsed, since they only
3792 contain objects.
3794 Optional argument SPECIAL, when non-nil, can be either
3795 `first-section', `item', `node-property', `section', and
3796 `table-row'.
3798 If STRUCTURE isn't provided but SPECIAL is set to `item', it will
3799 be computed.
3801 This function assumes point is always at the beginning of the
3802 element it has to parse."
3803 (save-excursion
3804 (let ((case-fold-search t)
3805 ;; Determine if parsing depth allows for secondary strings
3806 ;; parsing. It only applies to elements referenced in
3807 ;; `org-element-secondary-value-alist'.
3808 (raw-secondary-p (and granularity (not (eq granularity 'object)))))
3809 (cond
3810 ;; Item.
3811 ((eq special 'item)
3812 (org-element-item-parser limit structure raw-secondary-p))
3813 ;; Table Row.
3814 ((eq special 'table-row) (org-element-table-row-parser limit))
3815 ;; Node Property.
3816 ((eq special 'node-property) (org-element-node-property-parser limit))
3817 ;; Headline.
3818 ((org-with-limited-levels (org-at-heading-p))
3819 (org-element-headline-parser limit raw-secondary-p))
3820 ;; Sections (must be checked after headline).
3821 ((eq special 'section) (org-element-section-parser limit))
3822 ((eq special 'first-section)
3823 (org-element-section-parser
3824 (or (save-excursion (org-with-limited-levels (outline-next-heading)))
3825 limit)))
3826 ;; When not at bol, point is at the beginning of an item or
3827 ;; a footnote definition: next item is always a paragraph.
3828 ((not (bolp)) (org-element-paragraph-parser limit (list (point))))
3829 ;; Planning and Clock.
3830 ((looking-at org-planning-or-clock-line-re)
3831 (if (equal (match-string 1) org-clock-string)
3832 (org-element-clock-parser limit)
3833 (org-element-planning-parser limit)))
3834 ;; Inlinetask.
3835 ((org-at-heading-p)
3836 (org-element-inlinetask-parser limit raw-secondary-p))
3837 ;; From there, elements can have affiliated keywords.
3838 (t (let ((affiliated (org-element--collect-affiliated-keywords limit)))
3839 (cond
3840 ;; Jumping over affiliated keywords put point off-limits.
3841 ;; Parse them as regular keywords.
3842 ((and (cdr affiliated) (>= (point) limit))
3843 (goto-char (car affiliated))
3844 (org-element-keyword-parser limit nil))
3845 ;; LaTeX Environment.
3846 ((looking-at
3847 "[ \t]*\\\\begin{[A-Za-z0-9*]+}\\(\\[.*?\\]\\|{.*?}\\)*[ \t]*$")
3848 (org-element-latex-environment-parser limit affiliated))
3849 ;; Drawer and Property Drawer.
3850 ((looking-at org-drawer-regexp)
3851 (if (equal (match-string 1) "PROPERTIES")
3852 (org-element-property-drawer-parser limit affiliated)
3853 (org-element-drawer-parser limit affiliated)))
3854 ;; Fixed Width
3855 ((looking-at "[ \t]*:\\( \\|$\\)")
3856 (org-element-fixed-width-parser limit affiliated))
3857 ;; Inline Comments, Blocks, Babel Calls, Dynamic Blocks and
3858 ;; Keywords.
3859 ((looking-at "[ \t]*#")
3860 (goto-char (match-end 0))
3861 (cond ((looking-at "\\(?: \\|$\\)")
3862 (beginning-of-line)
3863 (org-element-comment-parser limit affiliated))
3864 ((looking-at "\\+BEGIN_\\(\\S-+\\)")
3865 (beginning-of-line)
3866 (let ((parser (assoc (upcase (match-string 1))
3867 org-element-block-name-alist)))
3868 (if parser (funcall (cdr parser) limit affiliated)
3869 (org-element-special-block-parser limit affiliated))))
3870 ((looking-at "\\+CALL:")
3871 (beginning-of-line)
3872 (org-element-babel-call-parser limit affiliated))
3873 ((looking-at "\\+BEGIN:? ")
3874 (beginning-of-line)
3875 (org-element-dynamic-block-parser limit affiliated))
3876 ((looking-at "\\+\\S-+:")
3877 (beginning-of-line)
3878 (org-element-keyword-parser limit affiliated))
3880 (beginning-of-line)
3881 (org-element-paragraph-parser limit affiliated))))
3882 ;; Footnote Definition.
3883 ((looking-at org-footnote-definition-re)
3884 (org-element-footnote-definition-parser limit affiliated))
3885 ;; Horizontal Rule.
3886 ((looking-at "[ \t]*-\\{5,\\}[ \t]*$")
3887 (org-element-horizontal-rule-parser limit affiliated))
3888 ;; Diary Sexp.
3889 ((looking-at "%%(")
3890 (org-element-diary-sexp-parser limit affiliated))
3891 ;; Table.
3892 ((org-at-table-p t) (org-element-table-parser limit affiliated))
3893 ;; List.
3894 ((looking-at (org-item-re))
3895 (org-element-plain-list-parser
3896 limit affiliated
3897 (or structure (org-element--list-struct limit))))
3898 ;; Default element: Paragraph.
3899 (t (org-element-paragraph-parser limit affiliated)))))))))
3902 ;; Most elements can have affiliated keywords. When looking for an
3903 ;; element beginning, we want to move before them, as they belong to
3904 ;; that element, and, in the meantime, collect information they give
3905 ;; into appropriate properties. Hence the following function.
3907 (defun org-element--collect-affiliated-keywords (limit)
3908 "Collect affiliated keywords from point down to LIMIT.
3910 Return a list whose CAR is the position at the first of them and
3911 CDR a plist of keywords and values and move point to the
3912 beginning of the first line after them.
3914 As a special case, if element doesn't start at the beginning of
3915 the line (i.e. a paragraph starting an item), CAR is current
3916 position of point and CDR is nil."
3917 (if (not (bolp)) (list (point))
3918 (let ((case-fold-search t)
3919 (origin (point))
3920 ;; RESTRICT is the list of objects allowed in parsed
3921 ;; keywords value.
3922 (restrict (org-element-restriction 'keyword))
3923 output)
3924 (while (and (< (point) limit) (looking-at org-element--affiliated-re))
3925 (let* ((raw-kwd (upcase (match-string 1)))
3926 ;; Apply translation to RAW-KWD. From there, KWD is
3927 ;; the official keyword.
3928 (kwd (or (cdr (assoc raw-kwd
3929 org-element-keyword-translation-alist))
3930 raw-kwd))
3931 ;; Find main value for any keyword.
3932 (value
3933 (save-match-data
3934 (org-trim
3935 (buffer-substring-no-properties
3936 (match-end 0) (point-at-eol)))))
3937 ;; PARSEDP is non-nil when keyword should have its
3938 ;; value parsed.
3939 (parsedp (member kwd org-element-parsed-keywords))
3940 ;; If KWD is a dual keyword, find its secondary
3941 ;; value. Maybe parse it.
3942 (dualp (member kwd org-element-dual-keywords))
3943 (dual-value
3944 (and dualp
3945 (let ((sec (org-match-string-no-properties 2)))
3946 (if (or (not sec) (not parsedp)) sec
3947 (org-element-parse-secondary-string sec restrict)))))
3948 ;; Attribute a property name to KWD.
3949 (kwd-sym (and kwd (intern (concat ":" (downcase kwd))))))
3950 ;; Now set final shape for VALUE.
3951 (when parsedp
3952 (setq value (org-element-parse-secondary-string value restrict)))
3953 (when dualp
3954 (setq value (and (or value dual-value) (cons value dual-value))))
3955 (when (or (member kwd org-element-multiple-keywords)
3956 ;; Attributes can always appear on multiple lines.
3957 (string-match "^ATTR_" kwd))
3958 (setq value (cons value (plist-get output kwd-sym))))
3959 ;; Eventually store the new value in OUTPUT.
3960 (setq output (plist-put output kwd-sym value))
3961 ;; Move to next keyword.
3962 (forward-line)))
3963 ;; If affiliated keywords are orphaned: move back to first one.
3964 ;; They will be parsed as a paragraph.
3965 (when (looking-at "[ \t]*$") (goto-char origin) (setq output nil))
3966 ;; Return value.
3967 (cons origin output))))
3971 ;;; The Org Parser
3973 ;; The two major functions here are `org-element-parse-buffer', which
3974 ;; parses Org syntax inside the current buffer, taking into account
3975 ;; region, narrowing, or even visibility if specified, and
3976 ;; `org-element-parse-secondary-string', which parses objects within
3977 ;; a given string.
3979 ;; The (almost) almighty `org-element-map' allows to apply a function
3980 ;; on elements or objects matching some type, and accumulate the
3981 ;; resulting values. In an export situation, it also skips unneeded
3982 ;; parts of the parse tree.
3984 (defun org-element-parse-buffer (&optional granularity visible-only)
3985 "Recursively parse the buffer and return structure.
3986 If narrowing is in effect, only parse the visible part of the
3987 buffer.
3989 Optional argument GRANULARITY determines the depth of the
3990 recursion. It can be set to the following symbols:
3992 `headline' Only parse headlines.
3993 `greater-element' Don't recurse into greater elements excepted
3994 headlines and sections. Thus, elements
3995 parsed are the top-level ones.
3996 `element' Parse everything but objects and plain text.
3997 `object' Parse the complete buffer (default).
3999 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
4000 elements.
4002 An element or an objects is represented as a list with the
4003 pattern (TYPE PROPERTIES CONTENTS), where :
4005 TYPE is a symbol describing the element or object. See
4006 `org-element-all-elements' and `org-element-all-objects' for an
4007 exhaustive list of such symbols. One can retrieve it with
4008 `org-element-type' function.
4010 PROPERTIES is the list of attributes attached to the element or
4011 object, as a plist. Although most of them are specific to the
4012 element or object type, all types share `:begin', `:end',
4013 `:post-blank' and `:parent' properties, which respectively
4014 refer to buffer position where the element or object starts,
4015 ends, the number of white spaces or blank lines after it, and
4016 the element or object containing it. Properties values can be
4017 obtained by using `org-element-property' function.
4019 CONTENTS is a list of elements, objects or raw strings
4020 contained in the current element or object, when applicable.
4021 One can access them with `org-element-contents' function.
4023 The Org buffer has `org-data' as type and nil as properties.
4024 `org-element-map' function can be used to find specific elements
4025 or objects within the parse tree.
4027 This function assumes that current major mode is `org-mode'."
4028 (save-excursion
4029 (goto-char (point-min))
4030 (org-skip-whitespace)
4031 (org-element--parse-elements
4032 (point-at-bol) (point-max)
4033 ;; Start in `first-section' mode so text before the first
4034 ;; headline belongs to a section.
4035 'first-section nil granularity visible-only (list 'org-data nil))))
4037 (defun org-element-parse-secondary-string (string restriction &optional parent)
4038 "Recursively parse objects in STRING and return structure.
4040 RESTRICTION is a symbol limiting the object types that will be
4041 looked after.
4043 Optional argument PARENT, when non-nil, is the element or object
4044 containing the secondary string. It is used to set correctly
4045 `:parent' property within the string."
4046 ;; Copy buffer-local variables listed in
4047 ;; `org-element-object-variables' into temporary buffer. This is
4048 ;; required since object parsing is dependent on these variables.
4049 (let ((pairs (delq nil (mapcar (lambda (var)
4050 (when (boundp var)
4051 (cons var (symbol-value var))))
4052 org-element-object-variables))))
4053 (with-temp-buffer
4054 (mapc (lambda (pair) (org-set-local (car pair) (cdr pair))) pairs)
4055 (insert string)
4056 (let ((secondary (org-element--parse-objects
4057 (point-min) (point-max) nil restriction)))
4058 (when parent
4059 (mapc (lambda (obj) (org-element-put-property obj :parent parent))
4060 secondary))
4061 secondary))))
4063 (defun org-element-map
4064 (data types fun &optional info first-match no-recursion with-affiliated)
4065 "Map a function on selected elements or objects.
4067 DATA is a parse tree, an element, an object, a string, or a list
4068 of such constructs. TYPES is a symbol or list of symbols of
4069 elements or objects types (see `org-element-all-elements' and
4070 `org-element-all-objects' for a complete list of types). FUN is
4071 the function called on the matching element or object. It has to
4072 accept one argument: the element or object itself.
4074 When optional argument INFO is non-nil, it should be a plist
4075 holding export options. In that case, parts of the parse tree
4076 not exportable according to that property list will be skipped.
4078 When optional argument FIRST-MATCH is non-nil, stop at the first
4079 match for which FUN doesn't return nil, and return that value.
4081 Optional argument NO-RECURSION is a symbol or a list of symbols
4082 representing elements or objects types. `org-element-map' won't
4083 enter any recursive element or object whose type belongs to that
4084 list. Though, FUN can still be applied on them.
4086 When optional argument WITH-AFFILIATED is non-nil, FUN will also
4087 apply to matching objects within parsed affiliated keywords (see
4088 `org-element-parsed-keywords').
4090 Nil values returned from FUN do not appear in the results.
4093 Examples:
4094 ---------
4096 Assuming TREE is a variable containing an Org buffer parse tree,
4097 the following example will return a flat list of all `src-block'
4098 and `example-block' elements in it:
4100 \(org-element-map tree '(example-block src-block) 'identity)
4102 The following snippet will find the first headline with a level
4103 of 1 and a \"phone\" tag, and will return its beginning position:
4105 \(org-element-map tree 'headline
4106 \(lambda (hl)
4107 \(and (= (org-element-property :level hl) 1)
4108 \(member \"phone\" (org-element-property :tags hl))
4109 \(org-element-property :begin hl)))
4110 nil t)
4112 The next example will return a flat list of all `plain-list' type
4113 elements in TREE that are not a sub-list themselves:
4115 \(org-element-map tree 'plain-list 'identity nil nil 'plain-list)
4117 Eventually, this example will return a flat list of all `bold'
4118 type objects containing a `latex-snippet' type object, even
4119 looking into captions:
4121 \(org-element-map tree 'bold
4122 \(lambda (b)
4123 \(and (org-element-map b 'latex-snippet 'identity nil t) b))
4124 nil nil nil t)"
4125 ;; Ensure TYPES and NO-RECURSION are a list, even of one element.
4126 (unless (listp types) (setq types (list types)))
4127 (unless (listp no-recursion) (setq no-recursion (list no-recursion)))
4128 ;; Recursion depth is determined by --CATEGORY.
4129 (let* ((--category
4130 (catch 'found
4131 (let ((category 'greater-elements))
4132 (mapc (lambda (type)
4133 (cond ((or (memq type org-element-all-objects)
4134 (eq type 'plain-text))
4135 ;; If one object is found, the function
4136 ;; has to recurse into every object.
4137 (throw 'found 'objects))
4138 ((not (memq type org-element-greater-elements))
4139 ;; If one regular element is found, the
4140 ;; function has to recurse, at least,
4141 ;; into every element it encounters.
4142 (and (not (eq category 'elements))
4143 (setq category 'elements)))))
4144 types)
4145 category)))
4146 ;; Compute properties for affiliated keywords if necessary.
4147 (--affiliated-alist
4148 (and with-affiliated
4149 (mapcar (lambda (kwd)
4150 (cons kwd (intern (concat ":" (downcase kwd)))))
4151 org-element-affiliated-keywords)))
4152 --acc
4153 --walk-tree
4154 (--walk-tree
4155 (function
4156 (lambda (--data)
4157 ;; Recursively walk DATA. INFO, if non-nil, is a plist
4158 ;; holding contextual information.
4159 (let ((--type (org-element-type --data)))
4160 (cond
4161 ((not --data))
4162 ;; Ignored element in an export context.
4163 ((and info (memq --data (plist-get info :ignore-list))))
4164 ;; List of elements or objects.
4165 ((not --type) (mapc --walk-tree --data))
4166 ;; Unconditionally enter parse trees.
4167 ((eq --type 'org-data)
4168 (mapc --walk-tree (org-element-contents --data)))
4170 ;; Check if TYPE is matching among TYPES. If so,
4171 ;; apply FUN to --DATA and accumulate return value
4172 ;; into --ACC (or exit if FIRST-MATCH is non-nil).
4173 (when (memq --type types)
4174 (let ((result (funcall fun --data)))
4175 (cond ((not result))
4176 (first-match (throw '--map-first-match result))
4177 (t (push result --acc)))))
4178 ;; If --DATA has a secondary string that can contain
4179 ;; objects with their type among TYPES, look into it.
4180 (when (and (eq --category 'objects) (not (stringp --data)))
4181 (let ((sec-prop
4182 (assq --type org-element-secondary-value-alist)))
4183 (when sec-prop
4184 (funcall --walk-tree
4185 (org-element-property (cdr sec-prop) --data)))))
4186 ;; If --DATA has any affiliated keywords and
4187 ;; WITH-AFFILIATED is non-nil, look for objects in
4188 ;; them.
4189 (when (and with-affiliated
4190 (eq --category 'objects)
4191 (memq --type org-element-all-elements))
4192 (mapc (lambda (kwd-pair)
4193 (let ((kwd (car kwd-pair))
4194 (value (org-element-property
4195 (cdr kwd-pair) --data)))
4196 ;; Pay attention to the type of value.
4197 ;; Preserve order for multiple keywords.
4198 (cond
4199 ((not value))
4200 ((and (member kwd org-element-multiple-keywords)
4201 (member kwd org-element-dual-keywords))
4202 (mapc (lambda (line)
4203 (funcall --walk-tree (cdr line))
4204 (funcall --walk-tree (car line)))
4205 (reverse value)))
4206 ((member kwd org-element-multiple-keywords)
4207 (mapc (lambda (line) (funcall --walk-tree line))
4208 (reverse value)))
4209 ((member kwd org-element-dual-keywords)
4210 (funcall --walk-tree (cdr value))
4211 (funcall --walk-tree (car value)))
4212 (t (funcall --walk-tree value)))))
4213 --affiliated-alist))
4214 ;; Determine if a recursion into --DATA is possible.
4215 (cond
4216 ;; --TYPE is explicitly removed from recursion.
4217 ((memq --type no-recursion))
4218 ;; --DATA has no contents.
4219 ((not (org-element-contents --data)))
4220 ;; Looking for greater elements but --DATA is simply
4221 ;; an element or an object.
4222 ((and (eq --category 'greater-elements)
4223 (not (memq --type org-element-greater-elements))))
4224 ;; Looking for elements but --DATA is an object.
4225 ((and (eq --category 'elements)
4226 (memq --type org-element-all-objects)))
4227 ;; In any other case, map contents.
4228 (t (mapc --walk-tree (org-element-contents --data)))))))))))
4229 (catch '--map-first-match
4230 (funcall --walk-tree data)
4231 ;; Return value in a proper order.
4232 (nreverse --acc))))
4233 (put 'org-element-map 'lisp-indent-function 2)
4235 ;; The following functions are internal parts of the parser.
4237 ;; The first one, `org-element--parse-elements' acts at the element's
4238 ;; level.
4240 ;; The second one, `org-element--parse-objects' applies on all objects
4241 ;; of a paragraph or a secondary string. It uses
4242 ;; `org-element--get-next-object-candidates' to optimize the search of
4243 ;; the next object in the buffer.
4245 ;; More precisely, that function looks for every allowed object type
4246 ;; first. Then, it discards failed searches, keeps further matches,
4247 ;; and searches again types matched behind point, for subsequent
4248 ;; calls. Thus, searching for a given type fails only once, and every
4249 ;; object is searched only once at top level (but sometimes more for
4250 ;; nested types).
4252 (defun org-element--parse-elements
4253 (beg end special structure granularity visible-only acc)
4254 "Parse elements between BEG and END positions.
4256 SPECIAL prioritize some elements over the others. It can be set
4257 to `first-section', `section' `item' or `table-row'.
4259 When value is `item', STRUCTURE will be used as the current list
4260 structure.
4262 GRANULARITY determines the depth of the recursion. See
4263 `org-element-parse-buffer' for more information.
4265 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
4266 elements.
4268 Elements are accumulated into ACC."
4269 (save-excursion
4270 (goto-char beg)
4271 ;; Visible only: skip invisible parts at the beginning of the
4272 ;; element.
4273 (when (and visible-only (org-invisible-p2))
4274 (goto-char (min (1+ (org-find-visible)) end)))
4275 ;; When parsing only headlines, skip any text before first one.
4276 (when (and (eq granularity 'headline) (not (org-at-heading-p)))
4277 (org-with-limited-levels (outline-next-heading)))
4278 ;; Main loop start.
4279 (while (< (point) end)
4280 ;; Find current element's type and parse it accordingly to
4281 ;; its category.
4282 (let* ((element (org-element--current-element
4283 end granularity special structure))
4284 (type (org-element-type element))
4285 (cbeg (org-element-property :contents-begin element)))
4286 (goto-char (org-element-property :end element))
4287 ;; Visible only: skip invisible parts between siblings.
4288 (when (and visible-only (org-invisible-p2))
4289 (goto-char (min (1+ (org-find-visible)) end)))
4290 ;; Fill ELEMENT contents by side-effect.
4291 (cond
4292 ;; If element has no contents, don't modify it.
4293 ((not cbeg))
4294 ;; Greater element: parse it between `contents-begin' and
4295 ;; `contents-end'. Make sure GRANULARITY allows the
4296 ;; recursion, or ELEMENT is a headline, in which case going
4297 ;; inside is mandatory, in order to get sub-level headings.
4298 ((and (memq type org-element-greater-elements)
4299 (or (memq granularity '(element object nil))
4300 (and (eq granularity 'greater-element)
4301 (eq type 'section))
4302 (eq type 'headline)))
4303 (org-element--parse-elements
4304 cbeg (org-element-property :contents-end element)
4305 ;; Possibly switch to a special mode.
4306 (case type
4307 (headline 'section)
4308 (plain-list 'item)
4309 (property-drawer 'node-property)
4310 (table 'table-row))
4311 (and (memq type '(item plain-list))
4312 (org-element-property :structure element))
4313 granularity visible-only element))
4314 ;; ELEMENT has contents. Parse objects inside, if
4315 ;; GRANULARITY allows it.
4316 ((memq granularity '(object nil))
4317 (org-element--parse-objects
4318 cbeg (org-element-property :contents-end element) element
4319 (org-element-restriction type))))
4320 (org-element-adopt-elements acc element)))
4321 ;; Return result.
4322 acc))
4324 (defun org-element--parse-objects (beg end acc restriction)
4325 "Parse objects between BEG and END and return recursive structure.
4327 Objects are accumulated in ACC.
4329 RESTRICTION is a list of object successors which are allowed in
4330 the current object."
4331 (let ((candidates 'initial))
4332 (save-excursion
4333 (save-restriction
4334 (narrow-to-region beg end)
4335 (goto-char (point-min))
4336 (while (and (not (eobp))
4337 (setq candidates
4338 (org-element--get-next-object-candidates
4339 restriction candidates)))
4340 (let ((next-object
4341 (let ((pos (apply 'min (mapcar 'cdr candidates))))
4342 (save-excursion
4343 (goto-char pos)
4344 (funcall (intern (format "org-element-%s-parser"
4345 (car (rassq pos candidates)))))))))
4346 ;; 1. Text before any object. Untabify it.
4347 (let ((obj-beg (org-element-property :begin next-object)))
4348 (unless (= (point) obj-beg)
4349 (setq acc
4350 (org-element-adopt-elements
4352 (replace-regexp-in-string
4353 "\t" (make-string tab-width ? )
4354 (buffer-substring-no-properties (point) obj-beg))))))
4355 ;; 2. Object...
4356 (let ((obj-end (org-element-property :end next-object))
4357 (cont-beg (org-element-property :contents-begin next-object)))
4358 ;; Fill contents of NEXT-OBJECT by side-effect, if it has
4359 ;; a recursive type.
4360 (when (and cont-beg
4361 (memq (car next-object) org-element-recursive-objects))
4362 (org-element--parse-objects
4363 cont-beg (org-element-property :contents-end next-object)
4364 next-object (org-element-restriction next-object)))
4365 (setq acc (org-element-adopt-elements acc next-object))
4366 (goto-char obj-end))))
4367 ;; 3. Text after last object. Untabify it.
4368 (unless (eobp)
4369 (setq acc
4370 (org-element-adopt-elements
4372 (replace-regexp-in-string
4373 "\t" (make-string tab-width ? )
4374 (buffer-substring-no-properties (point) end)))))
4375 ;; Result.
4376 acc))))
4378 (defun org-element--get-next-object-candidates (restriction objects)
4379 "Return an alist of candidates for the next object.
4381 RESTRICTION is a list of object types, as symbols. Only
4382 candidates with such types are looked after.
4384 OBJECTS is the previous candidates alist. If it is set to
4385 `initial', no search has been done before, and all symbols in
4386 RESTRICTION should be looked after.
4388 Return value is an alist whose CAR is the object type and CDR its
4389 beginning position."
4390 (delq
4392 (if (eq objects 'initial)
4393 ;; When searching for the first time, look for every successor
4394 ;; allowed in RESTRICTION.
4395 (mapcar
4396 (lambda (res)
4397 (funcall (intern (format "org-element-%s-successor" res))))
4398 restriction)
4399 ;; Focus on objects returned during last search. Keep those
4400 ;; still after point. Search again objects before it.
4401 (mapcar
4402 (lambda (obj)
4403 (if (>= (cdr obj) (point)) obj
4404 (let* ((type (car obj))
4405 (succ (or (cdr (assq type org-element-object-successor-alist))
4406 type)))
4407 (and succ
4408 (funcall (intern (format "org-element-%s-successor" succ)))))))
4409 objects))))
4413 ;;; Towards A Bijective Process
4415 ;; The parse tree obtained with `org-element-parse-buffer' is really
4416 ;; a snapshot of the corresponding Org buffer. Therefore, it can be
4417 ;; interpreted and expanded into a string with canonical Org syntax.
4418 ;; Hence `org-element-interpret-data'.
4420 ;; The function relies internally on
4421 ;; `org-element--interpret-affiliated-keywords'.
4423 ;;;###autoload
4424 (defun org-element-interpret-data (data &optional pseudo-objects)
4425 "Interpret DATA as Org syntax.
4427 DATA is a parse tree, an element, an object or a secondary string
4428 to interpret.
4430 Optional argument PSEUDO-OBJECTS is a list of symbols defining
4431 new types that should be treated as objects. An unknown type not
4432 belonging to this list is seen as a pseudo-element instead. Both
4433 pseudo-objects and pseudo-elements are transparent entities, i.e.
4434 only their contents are interpreted.
4436 Return Org syntax as a string."
4437 (org-element--interpret-data-1 data nil pseudo-objects))
4439 (defun org-element--interpret-data-1 (data parent pseudo-objects)
4440 "Interpret DATA as Org syntax.
4442 DATA is a parse tree, an element, an object or a secondary string
4443 to interpret. PARENT is used for recursive calls. It contains
4444 the element or object containing data, or nil. PSEUDO-OBJECTS
4445 are list of symbols defining new element or object types.
4446 Unknown types that don't belong to this list are treated as
4447 pseudo-elements instead.
4449 Return Org syntax as a string."
4450 (let* ((type (org-element-type data))
4451 ;; Find interpreter for current object or element. If it
4452 ;; doesn't exist (e.g. this is a pseudo object or element),
4453 ;; return contents, if any.
4454 (interpret
4455 (let ((fun (intern (format "org-element-%s-interpreter" type))))
4456 (if (fboundp fun) fun (lambda (data contents) contents))))
4457 (results
4458 (cond
4459 ;; Secondary string.
4460 ((not type)
4461 (mapconcat
4462 (lambda (obj)
4463 (org-element--interpret-data-1 obj parent pseudo-objects))
4464 data ""))
4465 ;; Full Org document.
4466 ((eq type 'org-data)
4467 (mapconcat
4468 (lambda (obj)
4469 (org-element--interpret-data-1 obj parent pseudo-objects))
4470 (org-element-contents data) ""))
4471 ;; Plain text: return it.
4472 ((stringp data) data)
4473 ;; Element or object without contents.
4474 ((not (org-element-contents data)) (funcall interpret data nil))
4475 ;; Element or object with contents.
4477 (funcall interpret data
4478 ;; Recursively interpret contents.
4479 (mapconcat
4480 (lambda (obj)
4481 (org-element--interpret-data-1 obj data pseudo-objects))
4482 (org-element-contents
4483 (if (not (memq type '(paragraph verse-block)))
4484 data
4485 ;; Fix indentation of elements containing
4486 ;; objects. We ignore `table-row' elements
4487 ;; as they are one line long anyway.
4488 (org-element-normalize-contents
4489 data
4490 ;; When normalizing first paragraph of an
4491 ;; item or a footnote-definition, ignore
4492 ;; first line's indentation.
4493 (and (eq type 'paragraph)
4494 (equal data (car (org-element-contents parent)))
4495 (memq (org-element-type parent)
4496 '(footnote-definition item))))))
4497 ""))))))
4498 (if (memq type '(org-data plain-text nil)) results
4499 ;; Build white spaces. If no `:post-blank' property is
4500 ;; specified, assume its value is 0.
4501 (let ((post-blank (or (org-element-property :post-blank data) 0)))
4502 (if (or (memq type org-element-all-objects)
4503 (memq type pseudo-objects))
4504 (concat results (make-string post-blank ?\s))
4505 (concat
4506 (org-element--interpret-affiliated-keywords data)
4507 (org-element-normalize-string results)
4508 (make-string post-blank ?\n)))))))
4510 (defun org-element--interpret-affiliated-keywords (element)
4511 "Return ELEMENT's affiliated keywords as Org syntax.
4512 If there is no affiliated keyword, return the empty string."
4513 (let ((keyword-to-org
4514 (function
4515 (lambda (key value)
4516 (let (dual)
4517 (when (member key org-element-dual-keywords)
4518 (setq dual (cdr value) value (car value)))
4519 (concat "#+" key
4520 (and dual
4521 (format "[%s]" (org-element-interpret-data dual)))
4522 ": "
4523 (if (member key org-element-parsed-keywords)
4524 (org-element-interpret-data value)
4525 value)
4526 "\n"))))))
4527 (mapconcat
4528 (lambda (prop)
4529 (let ((value (org-element-property prop element))
4530 (keyword (upcase (substring (symbol-name prop) 1))))
4531 (when value
4532 (if (or (member keyword org-element-multiple-keywords)
4533 ;; All attribute keywords can have multiple lines.
4534 (string-match "^ATTR_" keyword))
4535 (mapconcat (lambda (line) (funcall keyword-to-org keyword line))
4536 (reverse value)
4538 (funcall keyword-to-org keyword value)))))
4539 ;; List all ELEMENT's properties matching an attribute line or an
4540 ;; affiliated keyword, but ignore translated keywords since they
4541 ;; cannot belong to the property list.
4542 (loop for prop in (nth 1 element) by 'cddr
4543 when (let ((keyword (upcase (substring (symbol-name prop) 1))))
4544 (or (string-match "^ATTR_" keyword)
4545 (and
4546 (member keyword org-element-affiliated-keywords)
4547 (not (assoc keyword
4548 org-element-keyword-translation-alist)))))
4549 collect prop)
4550 "")))
4552 ;; Because interpretation of the parse tree must return the same
4553 ;; number of blank lines between elements and the same number of white
4554 ;; space after objects, some special care must be given to white
4555 ;; spaces.
4557 ;; The first function, `org-element-normalize-string', ensures any
4558 ;; string different from the empty string will end with a single
4559 ;; newline character.
4561 ;; The second function, `org-element-normalize-contents', removes
4562 ;; global indentation from the contents of the current element.
4564 (defun org-element-normalize-string (s)
4565 "Ensure string S ends with a single newline character.
4567 If S isn't a string return it unchanged. If S is the empty
4568 string, return it. Otherwise, return a new string with a single
4569 newline character at its end."
4570 (cond
4571 ((not (stringp s)) s)
4572 ((string= "" s) "")
4573 (t (and (string-match "\\(\n[ \t]*\\)*\\'" s)
4574 (replace-match "\n" nil nil s)))))
4576 (defun org-element-normalize-contents (element &optional ignore-first)
4577 "Normalize plain text in ELEMENT's contents.
4579 ELEMENT must only contain plain text and objects.
4581 If optional argument IGNORE-FIRST is non-nil, ignore first line's
4582 indentation to compute maximal common indentation.
4584 Return the normalized element that is element with global
4585 indentation removed from its contents. The function assumes that
4586 indentation is not done with TAB characters."
4587 (let* ((min-ind most-positive-fixnum)
4588 find-min-ind ; For byte-compiler.
4589 (find-min-ind
4590 (function
4591 ;; Return minimal common indentation within BLOB. This is
4592 ;; done by walking recursively BLOB and updating MIN-IND
4593 ;; along the way. FIRST-FLAG is non-nil when the first
4594 ;; string hasn't been seen yet. It is required as this
4595 ;; string is the only one whose indentation doesn't happen
4596 ;; after a newline character.
4597 (lambda (blob first-flag)
4598 (dolist (object (org-element-contents blob))
4599 (when (and first-flag (stringp object))
4600 (setq first-flag nil)
4601 (string-match "\\`\\( *\\)" object)
4602 (let ((len (length (match-string 1 object))))
4603 ;; An indentation of zero means no string will be
4604 ;; modified. Quit the process.
4605 (if (zerop len) (throw 'zero (setq min-ind 0))
4606 (setq min-ind (min len min-ind)))))
4607 (cond
4608 ((stringp object)
4609 (dolist (line (delq "" (cdr (org-split-string object " *\n"))))
4610 (setq min-ind (min (org-get-indentation line) min-ind))))
4611 ((memq (org-element-type object) org-element-recursive-objects)
4612 (funcall find-min-ind object first-flag))))))))
4613 ;; Find minimal indentation in ELEMENT.
4614 (catch 'zero (funcall find-min-ind element (not ignore-first)))
4615 (if (or (zerop min-ind) (= min-ind most-positive-fixnum)) element
4616 ;; Build ELEMENT back, replacing each string with the same
4617 ;; string minus common indentation.
4618 (let* (build ; For byte compiler.
4619 (build
4620 (function
4621 (lambda (blob first-flag)
4622 ;; Return BLOB with all its strings indentation
4623 ;; shortened from MIN-IND white spaces. FIRST-FLAG
4624 ;; is non-nil when the first string hasn't been seen
4625 ;; yet.
4626 (setcdr (cdr blob)
4627 (mapcar
4628 #'(lambda (object)
4629 (when (and first-flag (stringp object))
4630 (setq first-flag nil)
4631 (setq object
4632 (replace-regexp-in-string
4633 (format "\\` \\{%d\\}" min-ind)
4634 "" object)))
4635 (cond
4636 ((stringp object)
4637 (replace-regexp-in-string
4638 (format "\n \\{%d\\}" min-ind) "\n" object))
4639 ((memq (org-element-type object)
4640 org-element-recursive-objects)
4641 (funcall build object first-flag))
4642 (t object)))
4643 (org-element-contents blob)))
4644 blob))))
4645 (funcall build element (not ignore-first))))))
4649 ;;; Cache
4651 ;; Implement a caching mechanism for `org-element-at-point' and
4652 ;; `org-element-context', which see.
4654 ;; A single public function is provided: `org-element-cache-reset'.
4656 ;; Cache is enabled by default, but can be disabled globally with
4657 ;; `org-element-use-cache'. `org-element-cache-sync-idle-time',
4658 ;; org-element-cache-sync-duration' and `org-element-cache-sync-break'
4659 ;; can be tweaked to control caching behaviour.
4661 ;; Internally, parsed elements are stored in an AVL tree,
4662 ;; `org-element--cache'. This tree is updated lazily: whenever
4663 ;; a change happens to the buffer, a synchronization request is
4664 ;; registered in `org-element--cache-sync-requests' (see
4665 ;; `org-element--cache-submit-request'). During idle time, requests
4666 ;; are processed by `org-element--cache-sync'. Synchronization also
4667 ;; happens when an element is required from the cache. In this case,
4668 ;; the process stops as soon as the needed element is up-to-date.
4670 ;; A synchronization request can only apply on a synchronized part of
4671 ;; the cache. Therefore, the cache is updated at least to the
4672 ;; location where the new request applies. Thus, requests are ordered
4673 ;; from left to right and all elements starting before the first
4674 ;; request are correct. This property is used by functions like
4675 ;; `org-element--cache-find' to retrieve elements in the part of the
4676 ;; cache that can be trusted.
4678 ;; A request applies to every element, starting from its original
4679 ;; location (or key, see below). When a request is processed, it
4680 ;; moves forward and may collide the next one. In this case, both
4681 ;; requests are merged into a new one that starts from that element.
4682 ;; As a consequence, the whole synchronization complexity does not
4683 ;; depend on the number of pending requests, but on the number of
4684 ;; elements the very first request will be applied on.
4686 ;; Elements cannot be accessed through their beginning position, which
4687 ;; may or may not be up-to-date. Instead, each element in the tree is
4688 ;; associated to a key, obtained with `org-element--cache-key'. This
4689 ;; mechanism is robust enough to preserve total order among elements
4690 ;; even when the tree is only partially synchronized.
4692 ;; Objects contained in an element are stored in a hash table,
4693 ;; `org-element--cache-objects'.
4696 (defvar org-element-use-cache t
4697 "Non nil when Org parser should cache its results.
4698 This is mostly for debugging purpose.")
4700 (defvar org-element-cache-sync-idle-time 0.4
4701 "Length, in seconds, of idle time before syncing cache.")
4703 (defvar org-element-cache-sync-duration (seconds-to-time 0.04)
4704 "Maximum duration, as a time value, for a cache synchronization.
4705 If the synchronization is not over after this delay, the process
4706 pauses and resumes after `org-element-cache-sync-break'
4707 seconds.")
4709 (defvar org-element-cache-sync-break (seconds-to-time 0.2)
4710 "Duration, as a time value, of the pause between synchronizations.
4711 See `org-element-cache-sync-duration' for more information.")
4714 ;;;; Data Structure
4716 (defvar org-element--cache nil
4717 "AVL tree used to cache elements.
4718 Each node of the tree contains an element. Comparison is done
4719 with `org-element--cache-compare'. This cache is used in
4720 `org-element-at-point'.")
4722 (defvar org-element--cache-objects nil
4723 "Hash table used as to cache objects.
4724 Key is an element, as returned by `org-element-at-point', and
4725 value is an alist where each association is:
4727 \(POS CANDIDATES . OBJECTS)
4729 where POS is a buffer position, CANDIDATES is the last know list
4730 of successors (see `org-element--get-next-object-candidates') in
4731 container starting at POS and OBJECTS is a list of objects known
4732 to live within that container, from farthest to closest.
4734 In the following example, \\alpha, bold object and \\beta start
4735 at, respectively, positions 1, 7 and 8,
4737 \\alpha *\\beta*
4739 If the paragraph is completely parsed, OBJECTS-DATA will be
4741 \((1 nil BOLD-OBJECT ENTITY-OBJECT)
4742 \(8 nil ENTITY-OBJECT))
4744 whereas in a partially parsed paragraph, it could be
4746 \((1 ((entity . 1) (bold . 7)) ENTITY-OBJECT))
4748 This cache is used in `org-element-context'.")
4750 (defvar org-element--cache-sync-requests nil
4751 "List of pending synchronization requests.
4753 A request is a vector with the following pattern:
4755 \[NEXT END OFFSET PARENT PHASE]
4757 Processing a synchronization request consists in three phases:
4759 0. Delete modified elements,
4760 1. Fill missing area in cache,
4761 2. Shift positions and re-parent elements after the changes.
4763 During phase 0, NEXT is the key of the first element to be
4764 removed and END is buffer position delimiting the modifications.
4765 Every element starting between these are removed. PARENT is an
4766 element to be removed. Every element contained in it will also
4767 be removed.
4769 During phase 1, NEXT is the key of the next known element in
4770 cache. Parse buffer between that element and the one before it
4771 in order to determine the parent of the next element. Set PARENT
4772 to the element containing NEXT.
4774 During phase 2, NEXT is the key of the next element to shift in
4775 the parse tree. All elements starting from this one have their
4776 properties relatives to buffer positions shifted by integer
4777 OFFSET and, if they belong to element PARENT, are adopted by it.
4779 PHASE specifies the phase number, as an integer.")
4781 (defvar org-element--cache-sync-timer nil
4782 "Timer used for cache synchronization.")
4784 (defvar org-element--cache-sync-keys nil
4785 "Hash table used to store keys during synchronization.
4786 See `org-element--cache-key' for more information.")
4788 (defsubst org-element--cache-key (element)
4789 "Return a unique key for ELEMENT in cache tree.
4791 Keys are used to keep a total order among elements in the cache.
4792 Comparison is done with `org-element--cache-key-less-p'.
4794 When no synchronization is taking place, a key is simply the
4795 beginning position of the element, or that position plus one in
4796 the case of an first item (respectively row) in
4797 a list (respectively a table).
4799 During a synchronization, the key is the one the element had when
4800 the cache was synchronized for the last time. Elements added to
4801 cache during the synchronization get a new key generated with
4802 `org-element--cache-generate-key'.
4804 Such keys are stored in `org-element--cache-sync-keys'. The hash
4805 table is cleared once the synchronization is complete."
4806 (or (gethash element org-element--cache-sync-keys)
4807 (let* ((begin (org-element-property :begin element))
4808 ;; Increase beginning position of items (respectively
4809 ;; table rows) by one, so the first item can get
4810 ;; a different key from its parent list (respectively
4811 ;; table).
4812 (key (if (memq (org-element-type element) '(item table-row))
4813 (1+ begin)
4814 begin)))
4815 (if org-element--cache-sync-requests
4816 (puthash element key org-element--cache-sync-keys)
4817 key))))
4819 (defconst org-element--cache-default-key (ash most-positive-fixnum -1)
4820 "Default value for a new key level.
4821 See `org-element--cache-generate-key' for more information.")
4823 (defun org-element--cache-generate-key (lower upper)
4824 "Generate a key between LOWER and UPPER.
4826 LOWER and UPPER are integers or lists, possibly empty.
4828 If LOWER and UPPER are equals, return LOWER. Otherwise, return
4829 a unique key, as an integer or a list of integers, according to
4830 the following rules:
4832 - LOWER and UPPER are compared level-wise until values differ.
4834 - If, at a given level, LOWER and UPPER differ from more than
4835 2, the new key shares all the levels above with LOWER and
4836 gets a new level. Its value is the mean between LOWER and
4837 UPPER.
4839 \(1 2) + (1 4) --> (1 3)
4841 - If LOWER has no value to compare with, it is assumed that its
4842 value is 0:
4844 \(1 1) + (1 1 2) --> (1 1 1)
4846 Likewise, if UPPER is short of levels, the current value is
4847 `most-positive-fixnum'.
4849 - If they differ from only one, the new key inherits from
4850 current LOWER lever and has a new level at the value
4851 `org-element--cache-default-key'.
4853 \(1 2) + (1 3) --> (1 2 org-element--cache-default-key)
4855 - If the key is only one level long, it is returned as an
4856 integer.
4858 \(1 2) + (3 2) --> 2"
4859 (if (equal lower upper) lower
4860 (let ((lower (if (integerp lower) (list lower) lower))
4861 (upper (if (integerp upper) (list upper) upper))
4862 key)
4863 (catch 'exit
4864 (while (and lower upper)
4865 (let ((lower-level (car lower))
4866 (upper-level (car upper)))
4867 (cond
4868 ((= lower-level upper-level)
4869 (push lower-level key)
4870 (setq lower (cdr lower) upper (cdr upper)))
4871 ((= (- upper-level lower-level) 1)
4872 (push lower-level key)
4873 (setq lower (cdr lower))
4874 (while (and lower (= (car lower) most-positive-fixnum))
4875 (push most-positive-fixnum key)
4876 (setq lower (cdr lower)))
4877 (push (if lower
4878 (let ((n (car lower)))
4879 (+ (ash (if (zerop (mod n 2)) n (1+ n)) -1)
4880 org-element--cache-default-key))
4881 org-element--cache-default-key)
4882 key)
4883 (throw 'exit t))
4885 (push (let ((n (car lower)))
4886 (+ (ash (if (zerop (mod n 2)) n (1+ n)) -1)
4887 (ash (car upper) -1)))
4888 key)
4889 (throw 'exit t)))))
4890 (cond
4891 ((not lower)
4892 (while (and upper (zerop (car upper)))
4893 (push 0 key)
4894 (setq upper (cdr upper)))
4895 ;; (n) is equivalent to (n 0 0 0 0 ...) so we want to avoid
4896 ;; ending on a sequence of 0.
4897 (if (= (car upper) 1)
4898 (progn (push 0 key)
4899 (push org-element--cache-default-key key))
4900 (push (if upper (ash (car upper) -1) org-element--cache-default-key)
4901 key)))
4902 ((not upper)
4903 (while (and lower (= (car lower) most-positive-fixnum))
4904 (push most-positive-fixnum key)
4905 (setq lower (cdr lower)))
4906 (push (if lower
4907 (let ((n (car lower)))
4908 (+ (ash (if (zerop (mod n 2)) n (1+ n)) -1)
4909 org-element--cache-default-key))
4910 org-element--cache-default-key)
4911 key))))
4912 ;; Ensure we don't return a list with a single element.
4913 (if (cdr key) (nreverse key) (car key)))))
4915 (defsubst org-element--cache-key-less-p (a b)
4916 "Non-nil if key A is less than key B.
4917 A and B are either integers or lists of integers, as returned by
4918 `org-element--cache-key'."
4919 (if (integerp a) (if (integerp b) (< a b) (<= a (car b)))
4920 (if (integerp b) (< (car a) b)
4921 (catch 'exit
4922 (while (and a b)
4923 (cond ((car-less-than-car a b) (throw 'exit t))
4924 ((car-less-than-car b a) (throw 'exit nil))
4925 (t (setq a (cdr a) b (cdr b)))))
4926 ;; If A is empty, either keys are equal (B is also empty) or
4927 ;; B is less than A (B is longer). Therefore return nil.
4929 ;; If A is not empty, B is necessarily empty and A is less
4930 ;; than B (A is longer). Therefore, return a non-nil value.
4931 a))))
4933 (defun org-element--cache-compare (a b)
4934 "Non-nil when element A is located before element B."
4935 (org-element--cache-key-less-p (org-element--cache-key a)
4936 (org-element--cache-key b)))
4938 (defsubst org-element--cache-root ()
4939 "Return root value in cache.
4940 This function assumes `org-element--cache' is a valid AVL tree."
4941 (avl-tree--node-left (avl-tree--dummyroot org-element--cache)))
4944 ;;;; Tools
4946 (defsubst org-element--cache-active-p ()
4947 "Non-nil when cache is active in current buffer."
4948 (and org-element-use-cache
4949 (or (derived-mode-p 'org-mode) orgstruct-mode)))
4951 (defun org-element--cache-find (pos &optional side)
4952 "Find element in cache starting at POS or before.
4954 POS refers to a buffer position.
4956 When optional argument SIDE is non-nil, the function checks for
4957 elements starting at or past POS instead. If SIDE is `both', the
4958 function returns a cons cell where car is the first element
4959 starting at or before POS and cdr the first element starting
4960 after POS.
4962 The function can only find elements in the synchronized part of
4963 the cache."
4964 (let ((limit (and org-element--cache-sync-requests
4965 (aref (car org-element--cache-sync-requests) 0)))
4966 (node (org-element--cache-root))
4967 lower upper)
4968 (while node
4969 (let* ((element (avl-tree--node-data node))
4970 (begin (org-element-property :begin element)))
4971 (cond
4972 ((and limit
4973 (not (org-element--cache-key-less-p
4974 (org-element--cache-key element) limit)))
4975 (setq node (avl-tree--node-left node)))
4976 ((> begin pos)
4977 (setq upper element
4978 node (avl-tree--node-left node)))
4979 ((< begin pos)
4980 (setq lower element
4981 node (avl-tree--node-right node)))
4982 ;; We found an element in cache starting at POS. If `side'
4983 ;; is `both' we also want the next one in order to generate
4984 ;; a key in-between.
4986 ;; If the element is the first row or item in a table or
4987 ;; a plain list, we always return the table or the plain
4988 ;; list.
4990 ;; In any other case, we return the element found.
4991 ((eq side 'both)
4992 (setq lower element)
4993 (setq node (avl-tree--node-right node)))
4994 ((and (memq (org-element-type element) '(item table-row))
4995 (let ((parent (org-element-property :parent element)))
4996 (and (= (org-element-property :begin element)
4997 (org-element-property :contents-begin parent))
4998 (setq node nil
4999 lower parent
5000 upper parent)))))
5002 (setq node nil
5003 lower element
5004 upper element)))))
5005 (case side
5006 (both (cons lower upper))
5007 ((nil) lower)
5008 (otherwise upper))))
5010 (defun org-element--cache-put (element &optional data)
5011 "Store ELEMENT in current buffer's cache, if allowed.
5012 When optional argument DATA is non-nil, assume is it object data
5013 relative to ELEMENT and store it in the objects cache."
5014 (when (org-element--cache-active-p)
5015 (if data (puthash element data org-element--cache-objects)
5016 (when org-element--cache-sync-requests
5017 ;; During synchronization, first build an appropriate key for
5018 ;; the new element so `avl-tree-enter' can insert it at the
5019 ;; right spot in the cache.
5020 (let ((keys (org-element--cache-find
5021 (org-element-property :begin element) 'both)))
5022 (puthash element
5023 (org-element--cache-generate-key
5024 (and (car keys) (org-element--cache-key (car keys)))
5025 (cond ((cdr keys) (org-element--cache-key (cdr keys)))
5026 (org-element--cache-sync-requests
5027 (aref (car org-element--cache-sync-requests) 0))))
5028 org-element--cache-sync-keys)))
5029 (avl-tree-enter org-element--cache element))))
5032 ;;;; Synchronization
5034 (defsubst org-element--cache-set-timer (buffer)
5035 "Set idle timer for cache synchronization in BUFFER."
5036 (when org-element--cache-sync-timer
5037 (cancel-timer org-element--cache-sync-timer))
5038 (setq org-element--cache-sync-timer
5039 (run-with-idle-timer
5040 (let ((idle (current-idle-time)))
5041 (if idle (time-add idle org-element-cache-sync-break)
5042 org-element-cache-sync-idle-time))
5044 #'org-element--cache-sync
5045 buffer)))
5047 (defsubst org-element--cache-interrupt-p (time-limit)
5048 "Non-nil when synchronization process should be interrupted.
5049 TIME-LIMIT is a time value or nil."
5050 (and time-limit
5051 (or (input-pending-p)
5052 (time-less-p time-limit (current-time)))))
5054 (defsubst org-element--cache-shift-positions (element offset &optional props)
5055 "Shift ELEMENT properties relative to buffer positions by OFFSET.
5057 Properties containing buffer positions are `:begin', `:end',
5058 `:contents-begin', `:contents-end' and `:structure'. When
5059 optional argument PROPS is a list of keywords, only shift
5060 properties provided in that list.
5062 Properties are modified by side-effect."
5063 (let ((properties (nth 1 element)))
5064 ;; Shift `:structure' property for the first plain list only: it
5065 ;; is the only one that really matters and it prevents from
5066 ;; shifting it more than once.
5067 (when (and (or (not props) (memq :structure props))
5068 (eq (org-element-type element) 'plain-list)
5069 (not (eq (org-element-type (plist-get properties :parent))
5070 'item)))
5071 (dolist (item (plist-get properties :structure))
5072 (incf (car item) offset)
5073 (incf (nth 6 item) offset)))
5074 (dolist (key '(:begin :contents-begin :contents-end :end :post-affiliated))
5075 (let ((value (and (or (not props) (memq key props))
5076 (plist-get properties key))))
5077 (and value (plist-put properties key (+ offset value)))))))
5079 (defun org-element--cache-sync (buffer &optional threshold)
5080 "Synchronize cache with recent modification in BUFFER.
5081 When optional argument THRESHOLD is non-nil, do the
5082 synchronization for all elements starting before or at threshold,
5083 then exit. Otherwise, synchronize cache for as long as
5084 `org-element-cache-sync-duration' or until Emacs leaves idle
5085 state."
5086 (when (buffer-live-p buffer)
5087 (with-current-buffer buffer
5088 (let ((inhibit-quit t) request next)
5089 (when org-element--cache-sync-timer
5090 (cancel-timer org-element--cache-sync-timer))
5091 (catch 'interrupt
5092 (while org-element--cache-sync-requests
5093 (setq request (car org-element--cache-sync-requests)
5094 next (nth 1 org-element--cache-sync-requests))
5095 (or (org-element--cache-process-request
5096 request
5097 (and next (aref next 0))
5098 threshold
5099 (and (not threshold)
5100 (time-add (current-time)
5101 org-element-cache-sync-duration)))
5102 (throw 'interrupt t))
5103 ;; Request processed. Merge current and next offsets and
5104 ;; transfer phase number and ending position.
5105 (when next
5106 (incf (aref next 2) (aref request 2))
5107 (aset next 1 (aref request 1))
5108 (aset next 4 (aref request 4)))
5109 (setq org-element--cache-sync-requests
5110 (cdr org-element--cache-sync-requests))))
5111 ;; If more requests are awaiting, set idle timer accordingly.
5112 ;; Otherwise, reset keys.
5113 (if org-element--cache-sync-requests
5114 (org-element--cache-set-timer buffer)
5115 (clrhash org-element--cache-sync-keys))))))
5117 (defun org-element--cache-process-request (request next threshold time-limit)
5118 "Process synchronization REQUEST for all entries before NEXT.
5120 REQUEST is a vector, built by `org-element--cache-submit-request'.
5122 NEXT is a cache key, as returned by `org-element--cache-key'.
5124 When non-nil, THRESHOLD is a buffer position. Synchronization
5125 stops as soon as a shifted element begins after it.
5127 When non-nil, TIME-LIMIT is a time value. Synchronization stops
5128 after this time or when Emacs exits idle state.
5130 Return nil if the process stops before completing the request,
5131 t otherwise."
5132 (catch 'quit
5133 (when (= (aref request 4) 0)
5134 ;; Phase 1.
5136 ;; Delete all elements starting after BEG, but not after buffer
5137 ;; position END or past element with key NEXT.
5139 ;; As an exception, also delete elements starting after
5140 ;; modifications but included in an element altered by
5141 ;; modifications (orphans).
5143 ;; At each iteration, we start again at tree root since
5144 ;; a deletion modifies structure of the balanced tree.
5145 (catch 'end-phase
5146 (let ((beg (aref request 0))
5147 (end (aref request 1))
5148 (deleted-parent (aref request 3)))
5149 (while t
5150 (when (org-element--cache-interrupt-p time-limit)
5151 (aset request 3 deleted-parent)
5152 (throw 'quit nil))
5153 ;; Find first element in cache with key BEG or after it.
5154 ;; We don't use `org-element--cache-find' because it
5155 ;; couldn't reach orphaned elements past NEXT. Moreover,
5156 ;; BEG is a key, not a buffer position.
5157 (let ((node (org-element--cache-root)) data data-key)
5158 (while node
5159 (let* ((element (avl-tree--node-data node))
5160 (key (org-element--cache-key element)))
5161 (cond
5162 ((org-element--cache-key-less-p key beg)
5163 (setq node (avl-tree--node-right node)))
5164 ((org-element--cache-key-less-p beg key)
5165 (setq data element
5166 data-key key
5167 node (avl-tree--node-left node)))
5168 (t (setq data element
5169 data-key key
5170 node nil)))))
5171 (if (not data) (throw 'quit t)
5172 (let ((pos (org-element-property :begin data)))
5173 (cond
5174 ;; Remove orphaned elements.
5175 ((and deleted-parent
5176 (let ((up data))
5177 (while (and
5178 (setq up (org-element-property :parent up))
5179 (not (eq up deleted-parent))))
5180 up))
5181 (avl-tree-delete org-element--cache data))
5182 ((or (and next
5183 (not (org-element--cache-key-less-p data-key
5184 next)))
5185 (> pos end))
5186 (aset request 0 data-key)
5187 (aset request 1 pos)
5188 (aset request 4 1)
5189 (throw 'end-phase nil))
5190 (t (avl-tree-delete org-element--cache data)
5191 (when (= (org-element-property :end data) end)
5192 (setq deleted-parent data)))))))))))
5193 (when (= (aref request 4) 1)
5194 ;; Phase 2.
5196 ;; Phase 1 left a hole in the parse tree. Some elements after
5197 ;; it could have parents within. For example, in the following
5198 ;; buffer:
5201 ;; - item
5204 ;; Paragraph1
5206 ;; Paragraph2
5209 ;; if we remove a blank line between "item" and "Paragraph1",
5210 ;; everything down to "Paragraph2" is removed from cache. But
5211 ;; the paragraph now belongs to the list, and its `:parent'
5212 ;; property no longer is accurate.
5214 ;; Therefore we need to parse again elements in the hole, or at
5215 ;; least in its last section, so that we can re-parent
5216 ;; subsequent elements, during phase 3.
5218 ;; Note that we only need to get the parent from the first
5219 ;; element in cache after the hole.
5221 ;; Also, this part can be delayed if we don't need to retrieve
5222 ;; an element after the hole.
5223 (catch 'end-phase
5224 ;; Next element will start at its beginning position plus
5225 ;; offset, since it hasn't been shifted yet. Therefore, LIMIT
5226 ;; contains the real beginning position of the first element
5227 ;; to shift and re-parent.
5228 (when (equal (aref request 0) next) (throw 'quit t))
5229 (let ((limit (+ (aref request 1) (aref request 2))))
5230 (when (and threshold (< threshold limit)) (throw 'quit nil))
5231 (let ((parent (org-element--parse-to limit t time-limit)))
5232 (if (eq parent 'interrupted) (throw 'quit nil)
5233 (aset request 3 parent)
5234 (aset request 4 2)
5235 (throw 'end-phase nil))))))
5236 ;; Phase 3.
5238 ;; Shift all elements starting from key START, but before NEXT, by
5239 ;; OFFSET, and re-parent them when appropriate.
5241 ;; Elements are modified by side-effect so the tree structure
5242 ;; remains intact.
5244 ;; Once THRESHOLD, if any, is reached, or once there is an input
5245 ;; pending, exit. Before leaving, the current synchronization
5246 ;; request is updated.
5247 (let ((start (aref request 0))
5248 (offset (aref request 2))
5249 (parent (aref request 3))
5250 (node (org-element--cache-root))
5251 (stack (list nil))
5252 (leftp t)
5253 exit-flag)
5254 ;; No re-parenting nor shifting planned: request is over.
5255 (when (and (not parent) (zerop offset)) (throw 'quit t))
5256 (while node
5257 (let* ((data (avl-tree--node-data node))
5258 (key (org-element--cache-key data)))
5259 (if (and leftp (avl-tree--node-left node)
5260 (not (org-element--cache-key-less-p key start)))
5261 (progn (push node stack)
5262 (setq node (avl-tree--node-left node)))
5263 (unless (org-element--cache-key-less-p key start)
5264 ;; We reached NEXT. Request is complete.
5265 (when (equal key next) (throw 'quit t))
5266 ;; Handle interruption request. Update current request.
5267 (when (or exit-flag (org-element--cache-interrupt-p time-limit))
5268 (aset request 0 key)
5269 (aset request 3 parent)
5270 (throw 'quit nil))
5271 ;; Shift element.
5272 (unless (zerop offset)
5273 (org-element--cache-shift-positions data offset)
5274 ;; Shift associated objects data, if any.
5275 (dolist (object-data (gethash data org-element--cache-objects))
5276 (incf (car object-data) offset)
5277 (dolist (successor (nth 1 object-data))
5278 (incf (cdr successor) offset))
5279 (dolist (object (cddr object-data))
5280 (org-element--cache-shift-positions object offset))))
5281 (let ((begin (org-element-property :begin data)))
5282 ;; Re-parent it.
5283 (while (and parent
5284 (<= (org-element-property :end parent) begin))
5285 (setq parent (org-element-property :parent parent)))
5286 (cond (parent (org-element-put-property data :parent parent))
5287 ((zerop offset) (throw 'quit t)))
5288 ;; Cache is up-to-date past THRESHOLD. Request
5289 ;; interruption.
5290 (when (and threshold (> begin threshold)) (setq exit-flag t))))
5291 (setq node (if (setq leftp (avl-tree--node-right node))
5292 (avl-tree--node-right node)
5293 (pop stack))))))
5294 ;; We reached end of tree: synchronization complete.
5295 t)))
5297 (defun org-element--parse-to (pos &optional syncp time-limit)
5298 "Parse elements in current section, down to POS.
5300 Start parsing from the closest between the last known element in
5301 cache or headline above. Return the smallest element containing
5302 POS.
5304 When optional argument SYNCP is non-nil, return the parent of the
5305 element containing POS instead. In that case, it is also
5306 possible to provide TIME-LIMIT, which is a time value specifying
5307 when the parsing should stop. The function returns `interrupted'
5308 if the process stopped before finding the expected result."
5309 (catch 'exit
5310 (org-with-wide-buffer
5311 (goto-char pos)
5312 (let* ((cached (and (org-element--cache-active-p)
5313 (org-element--cache-find pos nil)))
5314 (begin (org-element-property :begin cached))
5315 element next)
5316 (cond
5317 ;; Nothing in cache before point: start parsing from first
5318 ;; element following headline above, or first element in
5319 ;; buffer.
5320 ((not cached)
5321 (when (org-with-limited-levels (outline-previous-heading))
5322 (forward-line))
5323 (skip-chars-forward " \r\t\n")
5324 (beginning-of-line))
5325 ;; Cache returned exact match: return it.
5326 ((= pos begin)
5327 (throw 'exit (if syncp (org-element-property :parent cached) cached)))
5328 ;; There's a headline between cached value and POS: cached
5329 ;; value is invalid. Start parsing from first element
5330 ;; following the headline.
5331 ((re-search-backward
5332 (org-with-limited-levels org-outline-regexp-bol) begin t)
5333 (forward-line)
5334 (skip-chars-forward " \r\t\n")
5335 (beginning-of-line))
5336 ;; Check if CACHED or any of its ancestors contain point.
5338 ;; If there is such an element, we inspect it in order to know
5339 ;; if we return it or if we need to parse its contents.
5340 ;; Otherwise, we just start parsing from current location,
5341 ;; which is right after the top-most element containing
5342 ;; CACHED.
5344 ;; As a special case, if POS is at the end of the buffer, we
5345 ;; want to return the innermost element ending there.
5347 ;; Also, if we find an ancestor and discover that we need to
5348 ;; parse its contents, make sure we don't start from
5349 ;; `:contents-begin', as we would otherwise go past CACHED
5350 ;; again. Instead, in that situation, we will resume parsing
5351 ;; from NEXT, which is located after CACHED or its higher
5352 ;; ancestor not containing point.
5354 (let ((up cached)
5355 (pos (if (= (point-max) pos) (1- pos) pos)))
5356 (goto-char (or (org-element-property :contents-begin cached) begin))
5357 (while (let ((end (org-element-property :end up)))
5358 (and (<= end pos)
5359 (goto-char end)
5360 (setq up (org-element-property :parent up)))))
5361 (cond ((not up))
5362 ((eobp) (setq element up))
5363 (t (setq element up next (point)))))))
5364 ;; Parse successively each element until we reach POS.
5365 (let ((end (or (org-element-property :end element)
5366 (save-excursion
5367 (org-with-limited-levels (outline-next-heading))
5368 (point))))
5369 (parent element)
5370 special-flag)
5371 (while t
5372 (when syncp
5373 (cond ((= (point) pos) (throw 'exit parent))
5374 ((org-element--cache-interrupt-p time-limit)
5375 (throw 'exit 'interrupted))))
5376 (unless element
5377 (setq element (org-element--current-element
5378 end 'element special-flag
5379 (org-element-property :structure parent)))
5380 (org-element-put-property element :parent parent)
5381 (org-element--cache-put element))
5382 (let ((elem-end (org-element-property :end element))
5383 (type (org-element-type element)))
5384 (cond
5385 ;; Skip any element ending before point. Also skip
5386 ;; element ending at point (unless it is also the end of
5387 ;; buffer) since we're sure that another element begins
5388 ;; after it.
5389 ((and (<= elem-end pos) (/= (point-max) elem-end))
5390 (goto-char elem-end))
5391 ;; A non-greater element contains point: return it.
5392 ((not (memq type org-element-greater-elements))
5393 (throw 'exit element))
5394 ;; Otherwise, we have to decide if ELEMENT really
5395 ;; contains POS. In that case we start parsing from
5396 ;; contents' beginning.
5398 ;; If POS is at contents' beginning but it is also at
5399 ;; the beginning of the first item in a list or a table.
5400 ;; In that case, we need to create an anchor for that
5401 ;; list or table, so return it.
5403 ;; Also, if POS is at the end of the buffer, no element
5404 ;; can start after it, but more than one may end there.
5405 ;; Arbitrarily, we choose to return the innermost of
5406 ;; such elements.
5407 ((let ((cbeg (org-element-property :contents-begin element))
5408 (cend (org-element-property :contents-end element)))
5409 (when (or syncp
5410 (and cbeg cend
5411 (or (< cbeg pos)
5412 (and (= cbeg pos)
5413 (not (memq type '(plain-list table)))))
5414 (or (> cend pos)
5415 (and (= cend pos) (= (point-max) pos)))))
5416 (goto-char (or next cbeg))
5417 (setq next nil
5418 special-flag (case type
5419 (plain-list 'item)
5420 (property-drawer 'node-property)
5421 (table 'table-row))
5422 parent element
5423 end cend))))
5424 ;; Otherwise, return ELEMENT as it is the smallest
5425 ;; element containing POS.
5426 (t (throw 'exit element))))
5427 (setq element nil)))))))
5430 ;;;; Staging Buffer Changes
5432 (defconst org-element--cache-opening-line
5433 (concat "^[ \t]*\\(?:"
5434 "#\\+BEGIN[:_]" "\\|"
5435 "\\\\begin{[A-Za-z0-9]+\\*?}" "\\|"
5436 ":\\S-+:[ \t]*$"
5437 "\\)")
5438 "Regexp matching an element opening line.
5439 When such a line is modified, modifications may propagate after
5440 modified area. In that situation, every element between that
5441 area and next section is removed from cache.")
5443 (defconst org-element--cache-closing-line
5444 (concat "^[ \t]*\\(?:"
5445 "#\\+END\\(?:_\\|:?[ \t]*$\\)" "\\|"
5446 "\\\\end{[A-Za-z0-9]+\\*?}[ \t]*$" "\\|"
5447 ":END:[ \t]*$"
5448 "\\)")
5449 "Regexp matching an element closing line.
5450 When such a line is modified, modifications may propagate before
5451 modified area. In that situation, every element between that
5452 area and previous section is removed from cache.")
5454 (defvar org-element--cache-change-warning nil
5455 "Non-nil when a sensitive line is about to be changed.
5456 It is a symbol among nil, t and `headline'.")
5458 (defun org-element--cache-before-change (beg end)
5459 "Request extension of area going to be modified if needed.
5460 BEG and END are the beginning and end of the range of changed
5461 text. See `before-change-functions' for more information."
5462 (let ((inhibit-quit t))
5463 ;; Make sure buffer positions in cache are correct until END.
5464 (org-element--cache-sync (current-buffer) end)
5465 (org-with-wide-buffer
5466 (goto-char beg)
5467 (beginning-of-line)
5468 (let ((top (point))
5469 (bottom (save-excursion (goto-char end) (line-end-position)))
5470 (sensitive-re
5471 ;; A sensitive line is a headline or a block (or drawer,
5472 ;; or latex-environment) boundary. Inserting one can
5473 ;; modify buffer drastically both above and below that
5474 ;; line, possibly making cache invalid. Therefore, we
5475 ;; need to pay attention to changes happening to them.
5476 (concat
5477 "\\(" (org-with-limited-levels org-outline-regexp-bol) "\\)" "\\|"
5478 org-element--cache-closing-line "\\|"
5479 org-element--cache-opening-line)))
5480 (save-match-data
5481 (setq org-element--cache-change-warning
5482 (cond ((not (re-search-forward sensitive-re bottom t)) nil)
5483 ((and (match-beginning 1)
5484 (progn (goto-char bottom)
5485 (or (not (re-search-backward sensitive-re
5486 (match-end 1) t))
5487 (match-beginning 1))))
5488 'headline)
5489 (t))))))))
5491 (defun org-element--cache-after-change (beg end pre)
5492 "Update buffer modifications for current buffer.
5493 BEG and END are the beginning and end of the range of changed
5494 text, and the length in bytes of the pre-change text replaced by
5495 that range. See `after-change-functions' for more information."
5496 (let ((inhibit-quit t))
5497 (when (org-element--cache-active-p)
5498 (org-with-wide-buffer
5499 (goto-char beg)
5500 (beginning-of-line)
5501 (let ((top (point))
5502 (bottom (save-excursion (goto-char end) (line-end-position))))
5503 (org-with-limited-levels
5504 (save-match-data
5505 ;; Determine if modified area needs to be extended,
5506 ;; according to both previous and current state. We make
5507 ;; a special case for headline editing: if a headline is
5508 ;; modified but not removed, do not extend.
5509 (when (let ((previous-state org-element--cache-change-warning)
5510 (sensitive-re
5511 (concat "\\(" org-outline-regexp-bol "\\)" "\\|"
5512 org-element--cache-closing-line "\\|"
5513 org-element--cache-opening-line))
5514 (case-fold-search t))
5515 (cond ((eq previous-state t))
5516 ((not (re-search-forward sensitive-re bottom t))
5517 (eq previous-state 'headline))
5518 ((match-beginning 1)
5519 (or (not (eq previous-state 'headline))
5520 (and (progn (goto-char bottom)
5521 (re-search-backward
5522 sensitive-re (match-end 1) t))
5523 (not (match-beginning 1)))))
5524 (t)))
5525 ;; Effectively extend modified area.
5526 (setq top (progn (goto-char top)
5527 (when (outline-previous-heading) (forward-line))
5528 (point)))
5529 (setq bottom (progn (goto-char bottom)
5530 (if (outline-next-heading) (1- (point))
5531 (point)))))))
5532 ;; Store synchronization request.
5533 (let ((offset (- end beg pre)))
5534 (org-element--cache-submit-request top (- bottom offset) offset))))
5535 ;; Activate a timer to process the request during idle time.
5536 (org-element--cache-set-timer (current-buffer)))))
5538 (defun org-element--cache-submit-request (beg end offset)
5539 "Submit a new cache synchronization request for current buffer.
5540 BEG and END are buffer positions delimiting the minimal area
5541 where cache data should be removed. OFFSET is the size of the
5542 change, as an integer."
5543 (let ((first-element
5544 ;; Find the position of the first element in cache to remove.
5546 ;; Partially modified elements will be removed during request
5547 ;; processing. As an exception, greater elements around the
5548 ;; changes that are robust to contents modifications are
5549 ;; preserved.
5551 ;; We look just before BEG because an element ending at BEG
5552 ;; needs to be removed too.
5553 (let* ((elements (org-element--cache-find (1- beg) 'both))
5554 (before (car elements))
5555 (after (cdr elements)))
5556 (if (not before) after
5557 (let ((up before))
5558 (while (setq up (org-element-property :parent up))
5559 (if (and (memq (org-element-type up)
5560 '(center-block
5561 drawer dynamic-block inlinetask
5562 property-drawer quote-block special-block))
5563 (<= (org-element-property :contents-begin up) beg)
5564 (> (org-element-property :contents-end up) end))
5565 ;; UP is a greater element that is wrapped around
5566 ;; the changes. We only need to extend its
5567 ;; ending boundaries and those of all its
5568 ;; parents.
5569 (while up
5570 (org-element--cache-shift-positions
5571 up offset '(:contents-end :end))
5572 (setq up (org-element-property :parent up)))
5573 (setq before up)))
5574 ;; We're at top level element containing ELEMENT: if
5575 ;; it's altered by buffer modifications, it is first
5576 ;; element in cache to be removed. Otherwise, that
5577 ;; first element is the following one.
5578 (if (< (org-element-property :end before) beg) after before))))))
5579 (cond
5580 ;; Changes happened before the first known element. Shift the
5581 ;; rest of the cache.
5582 ((and first-element (> (org-element-property :begin first-element) end))
5583 (push (vector (org-element--cache-key first-element) nil offset nil 2)
5584 org-element--cache-sync-requests))
5585 ;; There is at least an element to remove. Find position past
5586 ;; every element containing END.
5587 (first-element
5588 (if (> (org-element-property :end first-element) end)
5589 (setq end (org-element-property :end first-element))
5590 (let ((element (org-element--cache-find end)))
5591 (setq end (org-element-property :end element))
5592 (let ((up element))
5593 (while (and (setq up (org-element-property :parent up))
5594 (>= (org-element-property :begin up) beg))
5595 (setq end (org-element-property :end up))))))
5596 (push (vector (org-element--cache-key first-element) end offset nil 0)
5597 org-element--cache-sync-requests))
5598 ;; No element to remove. No need to re-parent either. Simply
5599 ;; shift additional elements, if any, by OFFSET.
5600 (org-element--cache-sync-requests
5601 (incf (aref (car org-element--cache-sync-requests) 2) offset)))))
5604 ;;;; Public Functions
5606 ;;;###autoload
5607 (defun org-element-cache-reset (&optional all)
5608 "Reset cache in current buffer.
5609 When optional argument ALL is non-nil, reset cache in all Org
5610 buffers. This function will do nothing if
5611 `org-element-use-cache' is nil."
5612 (interactive "P")
5613 (dolist (buffer (if all (buffer-list) (list (current-buffer))))
5614 (with-current-buffer buffer
5615 (when (org-element--cache-active-p)
5616 (org-set-local 'org-element--cache
5617 (avl-tree-create #'org-element--cache-compare))
5618 (org-set-local 'org-element--cache-objects
5619 (make-hash-table :weakness 'key :test #'eq))
5620 (org-set-local 'org-element--cache-sync-keys
5621 (make-hash-table :weakness 'key :test #'eq))
5622 (org-set-local 'org-element--cache-change-warning nil)
5623 (org-set-local 'org-element--cache-sync-requests nil)
5624 (org-set-local 'org-element--cache-sync-timer nil)
5625 (add-hook 'before-change-functions
5626 #'org-element--cache-before-change nil t)
5627 (add-hook 'after-change-functions
5628 #'org-element--cache-after-change nil t)))))
5632 ;;; The Toolbox
5634 ;; The first move is to implement a way to obtain the smallest element
5635 ;; containing point. This is the job of `org-element-at-point'. It
5636 ;; basically jumps back to the beginning of section containing point
5637 ;; and proceed, one element after the other, with
5638 ;; `org-element--current-element' until the container is found. Note:
5639 ;; When using `org-element-at-point', secondary values are never
5640 ;; parsed since the function focuses on elements, not on objects.
5642 ;; At a deeper level, `org-element-context' lists all elements and
5643 ;; objects containing point.
5645 ;; `org-element-nested-p' and `org-element-swap-A-B' may be used
5646 ;; internally by navigation and manipulation tools.
5649 ;;;###autoload
5650 (defun org-element-at-point ()
5651 "Determine closest element around point.
5653 Return value is a list like (TYPE PROPS) where TYPE is the type
5654 of the element and PROPS a plist of properties associated to the
5655 element.
5657 Possible types are defined in `org-element-all-elements'.
5658 Properties depend on element or object type, but always include
5659 `:begin', `:end', `:parent' and `:post-blank' properties.
5661 As a special case, if point is at the very beginning of the first
5662 item in a list or sub-list, returned element will be that list
5663 instead of the item. Likewise, if point is at the beginning of
5664 the first row of a table, returned element will be the table
5665 instead of the first row.
5667 When point is at the end of the buffer, return the innermost
5668 element ending there."
5669 (org-with-wide-buffer
5670 (let ((origin (point)))
5671 (end-of-line)
5672 (skip-chars-backward " \r\t\n")
5673 (cond
5674 ;; Within blank lines at the beginning of buffer, return nil.
5675 ((bobp) nil)
5676 ;; Within blank lines right after a headline, return that
5677 ;; headline.
5678 ((org-with-limited-levels (org-at-heading-p))
5679 (beginning-of-line)
5680 (org-element-headline-parser (point-max) t))
5681 ;; Otherwise parse until we find element containing ORIGIN.
5683 (when (org-element--cache-active-p)
5684 (if (not org-element--cache) (org-element-cache-reset)
5685 (org-element--cache-sync (current-buffer) origin)))
5686 (org-element--parse-to origin))))))
5688 ;;;###autoload
5689 (defun org-element-context (&optional element)
5690 "Return closest element or object around point.
5692 Return value is a list like (TYPE PROPS) where TYPE is the type
5693 of the element or object and PROPS a plist of properties
5694 associated to it.
5696 Possible types are defined in `org-element-all-elements' and
5697 `org-element-all-objects'. Properties depend on element or
5698 object type, but always include `:begin', `:end', `:parent' and
5699 `:post-blank'.
5701 Optional argument ELEMENT, when non-nil, is the closest element
5702 containing point, as returned by `org-element-at-point'.
5703 Providing it allows for quicker computation."
5704 (catch 'objects-forbidden
5705 (org-with-wide-buffer
5706 (let* ((origin (point))
5707 (element (or element (org-element-at-point)))
5708 (type (org-element-type element)))
5709 ;; If point is inside an element containing objects or
5710 ;; a secondary string, narrow buffer to the container and
5711 ;; proceed with parsing. Otherwise, return ELEMENT.
5712 (cond
5713 ;; At a parsed affiliated keyword, check if we're inside main
5714 ;; or dual value.
5715 ((let ((post (org-element-property :post-affiliated element)))
5716 (and post (< origin post)))
5717 (beginning-of-line)
5718 (let ((case-fold-search t)) (looking-at org-element--affiliated-re))
5719 (cond
5720 ((not (member-ignore-case (match-string 1)
5721 org-element-parsed-keywords))
5722 (throw 'objects-forbidden element))
5723 ((< (match-end 0) origin)
5724 (narrow-to-region (match-end 0) (line-end-position)))
5725 ((and (match-beginning 2)
5726 (>= origin (match-beginning 2))
5727 (< origin (match-end 2)))
5728 (narrow-to-region (match-beginning 2) (match-end 2)))
5729 (t (throw 'objects-forbidden element)))
5730 ;; Also change type to retrieve correct restrictions.
5731 (setq type 'keyword))
5732 ;; At an item, objects can only be located within tag, if any.
5733 ((eq type 'item)
5734 (let ((tag (org-element-property :tag element)))
5735 (if (not tag) (throw 'objects-forbidden element)
5736 (beginning-of-line)
5737 (search-forward tag (line-end-position))
5738 (goto-char (match-beginning 0))
5739 (if (and (>= origin (point)) (< origin (match-end 0)))
5740 (narrow-to-region (point) (match-end 0))
5741 (throw 'objects-forbidden element)))))
5742 ;; At an headline or inlinetask, objects are in title.
5743 ((memq type '(headline inlinetask))
5744 (goto-char (org-element-property :begin element))
5745 (skip-chars-forward "* ")
5746 (if (and (>= origin (point)) (< origin (line-end-position)))
5747 (narrow-to-region (point) (line-end-position))
5748 (throw 'objects-forbidden element)))
5749 ;; At a paragraph, a table-row or a verse block, objects are
5750 ;; located within their contents.
5751 ((memq type '(paragraph table-row verse-block))
5752 (let ((cbeg (org-element-property :contents-begin element))
5753 (cend (org-element-property :contents-end element)))
5754 ;; CBEG is nil for table rules.
5755 (if (and cbeg cend (>= origin cbeg)
5756 (or (< origin cend) (and (= origin cend) (eobp))))
5757 (narrow-to-region cbeg cend)
5758 (throw 'objects-forbidden element))))
5759 ;; At a parsed keyword, objects are located within value.
5760 ((eq type 'keyword)
5761 (if (not (member (org-element-property :key element)
5762 org-element-document-properties))
5763 (throw 'objects-forbidden element)
5764 (beginning-of-line)
5765 (search-forward ":")
5766 (if (and (>= origin (point)) (< origin (line-end-position)))
5767 (narrow-to-region (point) (line-end-position))
5768 (throw 'objects-forbidden element))))
5769 ;; At a planning line, if point is at a timestamp, return it,
5770 ;; otherwise, return element.
5771 ((eq type 'planning)
5772 (dolist (p '(:closed :deadline :scheduled))
5773 (let ((timestamp (org-element-property p element)))
5774 (when (and timestamp
5775 (<= (org-element-property :begin timestamp) origin)
5776 (> (org-element-property :end timestamp) origin))
5777 (throw 'objects-forbidden timestamp))))
5778 ;; All other locations cannot contain objects: bail out.
5779 (throw 'objects-forbidden element))
5780 (t (throw 'objects-forbidden element)))
5781 (goto-char (point-min))
5782 (let* ((restriction (org-element-restriction type))
5783 (parent element)
5784 (candidates 'initial)
5785 (cache (and (org-element--cache-active-p)
5786 (gethash element org-element--cache-objects)))
5787 objects-data next)
5788 (prog1
5789 (catch 'exit
5790 (while t
5791 ;; Get list of next object candidates in CANDIDATES.
5792 ;; When entering for the first time PARENT, grab it
5793 ;; from cache, if available, or compute it. Then,
5794 ;; for each subsequent iteration in PARENT, always
5795 ;; compute it since we're beyond cache anyway.
5796 (unless next
5797 (let ((data (assq (point) cache)))
5798 (if data (setq candidates (nth 1 (setq objects-data data)))
5799 (push (setq objects-data (list (point) 'initial))
5800 cache))))
5801 (when (or next (eq 'initial candidates))
5802 (setq candidates
5803 (org-element--get-next-object-candidates
5804 restriction candidates))
5805 (setcar (cdr objects-data) candidates))
5806 ;; Compare ORIGIN with next object starting position,
5807 ;; if any.
5809 ;; If ORIGIN is lesser or if there is no object
5810 ;; following, look for a previous object that might
5811 ;; contain it in cache. If there is no cache, we
5812 ;; didn't miss any object so simply return PARENT.
5814 ;; If ORIGIN is greater or equal, parse next
5815 ;; candidate for further processing.
5816 (let ((closest
5817 (and candidates
5818 (rassq (apply #'min (mapcar #'cdr candidates))
5819 candidates))))
5820 (if (or (not closest) (> (cdr closest) origin))
5821 (catch 'found
5822 (dolist (obj (cddr objects-data) (throw 'exit parent))
5823 (when (<= (org-element-property :begin obj) origin)
5824 (let ((end (org-element-property :end obj)))
5825 (cond
5826 ((> end origin) (throw 'found (setq next obj)))
5827 ((and (= end origin) (= (point-max) end))
5828 (org-element-put-property obj :parent parent)
5829 (throw 'exit obj))
5830 (t (throw 'exit parent)))))))
5831 (goto-char (cdr closest))
5832 (setq next
5833 (funcall (intern (format "org-element-%s-parser"
5834 (car closest)))))
5835 (push next (cddr objects-data))))
5836 ;; Process NEXT to know if we need to skip it, return
5837 ;; it or move into it.
5838 (let ((cbeg (org-element-property :contents-begin next))
5839 (cend (org-element-property :contents-end next))
5840 (obj-end (org-element-property :end next)))
5841 (cond
5842 ;; ORIGIN is after NEXT, so skip it.
5843 ((<= obj-end origin) (goto-char obj-end))
5844 ;; ORIGIN is within a non-recursive next or
5845 ;; at an object boundaries: Return that object.
5846 ((or (not cbeg) (< origin cbeg) (>= origin cend))
5847 (org-element-put-property next :parent parent)
5848 (throw 'exit next))
5849 ;; Otherwise, move into NEXT and reset flags as we
5850 ;; shift parent.
5851 (t (goto-char cbeg)
5852 (narrow-to-region (point) cend)
5853 (org-element-put-property next :parent parent)
5854 (setq parent next
5855 restriction (org-element-restriction next)
5856 next nil
5857 objects-data nil
5858 candidates 'initial))))))
5859 ;; Store results in cache, if applicable.
5860 (org-element--cache-put element cache)))))))
5862 (defun org-element-nested-p (elem-A elem-B)
5863 "Non-nil when elements ELEM-A and ELEM-B are nested."
5864 (let ((beg-A (org-element-property :begin elem-A))
5865 (beg-B (org-element-property :begin elem-B))
5866 (end-A (org-element-property :end elem-A))
5867 (end-B (org-element-property :end elem-B)))
5868 (or (and (>= beg-A beg-B) (<= end-A end-B))
5869 (and (>= beg-B beg-A) (<= end-B end-A)))))
5871 (defun org-element-swap-A-B (elem-A elem-B)
5872 "Swap elements ELEM-A and ELEM-B.
5873 Assume ELEM-B is after ELEM-A in the buffer. Leave point at the
5874 end of ELEM-A."
5875 (goto-char (org-element-property :begin elem-A))
5876 ;; There are two special cases when an element doesn't start at bol:
5877 ;; the first paragraph in an item or in a footnote definition.
5878 (let ((specialp (not (bolp))))
5879 ;; Only a paragraph without any affiliated keyword can be moved at
5880 ;; ELEM-A position in such a situation. Note that the case of
5881 ;; a footnote definition is impossible: it cannot contain two
5882 ;; paragraphs in a row because it cannot contain a blank line.
5883 (if (and specialp
5884 (or (not (eq (org-element-type elem-B) 'paragraph))
5885 (/= (org-element-property :begin elem-B)
5886 (org-element-property :contents-begin elem-B))))
5887 (error "Cannot swap elements"))
5888 ;; In a special situation, ELEM-A will have no indentation. We'll
5889 ;; give it ELEM-B's (which will in, in turn, have no indentation).
5890 (let* ((ind-B (when specialp
5891 (goto-char (org-element-property :begin elem-B))
5892 (org-get-indentation)))
5893 (beg-A (org-element-property :begin elem-A))
5894 (end-A (save-excursion
5895 (goto-char (org-element-property :end elem-A))
5896 (skip-chars-backward " \r\t\n")
5897 (point-at-eol)))
5898 (beg-B (org-element-property :begin elem-B))
5899 (end-B (save-excursion
5900 (goto-char (org-element-property :end elem-B))
5901 (skip-chars-backward " \r\t\n")
5902 (point-at-eol)))
5903 ;; Store overlays responsible for visibility status. We
5904 ;; also need to store their boundaries as they will be
5905 ;; removed from buffer.
5906 (overlays
5907 (cons
5908 (mapcar (lambda (ov) (list ov (overlay-start ov) (overlay-end ov)))
5909 (overlays-in beg-A end-A))
5910 (mapcar (lambda (ov) (list ov (overlay-start ov) (overlay-end ov)))
5911 (overlays-in beg-B end-B))))
5912 ;; Get contents.
5913 (body-A (buffer-substring beg-A end-A))
5914 (body-B (delete-and-extract-region beg-B end-B)))
5915 (goto-char beg-B)
5916 (when specialp
5917 (setq body-B (replace-regexp-in-string "\\`[ \t]*" "" body-B))
5918 (org-indent-to-column ind-B))
5919 (insert body-A)
5920 ;; Restore ex ELEM-A overlays.
5921 (let ((offset (- beg-B beg-A)))
5922 (mapc (lambda (ov)
5923 (move-overlay
5924 (car ov) (+ (nth 1 ov) offset) (+ (nth 2 ov) offset)))
5925 (car overlays))
5926 (goto-char beg-A)
5927 (delete-region beg-A end-A)
5928 (insert body-B)
5929 ;; Restore ex ELEM-B overlays.
5930 (mapc (lambda (ov)
5931 (move-overlay
5932 (car ov) (- (nth 1 ov) offset) (- (nth 2 ov) offset)))
5933 (cdr overlays)))
5934 (goto-char (org-element-property :end elem-B)))))
5936 (defun org-element-remove-indentation (s &optional n)
5937 "Remove maximum common indentation in string S and return it.
5938 When optional argument N is a positive integer, remove exactly
5939 that much characters from indentation, if possible, or return
5940 S as-is otherwise. Unlike to `org-remove-indentation', this
5941 function doesn't call `untabify' on S."
5942 (catch 'exit
5943 (with-temp-buffer
5944 (insert s)
5945 (goto-char (point-min))
5946 ;; Find maximum common indentation, if not specified.
5947 (setq n (or n
5948 (let ((min-ind (point-max)))
5949 (save-excursion
5950 (while (re-search-forward "^[ \t]*\\S-" nil t)
5951 (let ((ind (1- (current-column))))
5952 (if (zerop ind) (throw 'exit s)
5953 (setq min-ind (min min-ind ind))))))
5954 min-ind)))
5955 (if (zerop n) s
5956 ;; Remove exactly N indentation, but give up if not possible.
5957 (while (not (eobp))
5958 (let ((ind (progn (skip-chars-forward " \t") (current-column))))
5959 (cond ((eolp) (delete-region (line-beginning-position) (point)))
5960 ((< ind n) (throw 'exit s))
5961 (t (org-indent-line-to (- ind n))))
5962 (forward-line)))
5963 (buffer-string)))))
5967 (provide 'org-element)
5969 ;; Local variables:
5970 ;; generated-autoload-file: "org-loaddefs.el"
5971 ;; End:
5973 ;;; org-element.el ends here