org-element: Fix infloop in `org-element-context'
[org-mode.git] / lisp / org-element.el
blobe2aee7c91a6c5e230394baf4a7e3ca513838b9ad
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 (e.g. 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 (e.g., \"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 (e.g., \"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 (e.g., `: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 (e.g., `: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]*$" (match-string 1))
1290 limit t)))
1291 ((and (looking-at org-drawer-regexp)
1292 (re-search-forward "^[ \t]*:END:[ \t]*$" limit t))))
1293 (forward-line))))))))
1295 (defun org-element-plain-list-parser (limit affiliated structure)
1296 "Parse a plain list.
1298 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1299 the buffer position at the beginning of the first affiliated
1300 keyword and CDR is a plist of affiliated keywords along with
1301 their value. STRUCTURE is the structure of the plain list being
1302 parsed.
1304 Return a list whose CAR is `plain-list' and CDR is a plist
1305 containing `:type', `:begin', `:end', `:contents-begin' and
1306 `:contents-end', `:structure', `:post-blank' and
1307 `:post-affiliated' keywords.
1309 Assume point is at the beginning of the list."
1310 (save-excursion
1311 (let* ((struct (or structure (org-element--list-struct limit)))
1312 (type (cond ((org-looking-at-p "[ \t]*[A-Za-z0-9]") 'ordered)
1313 ((nth 5 (assq (point) struct)) 'descriptive)
1314 (t 'unordered)))
1315 (contents-begin (point))
1316 (begin (car affiliated))
1317 (contents-end (let* ((item (assq contents-begin struct))
1318 (ind (nth 1 item))
1319 (pos (nth 6 item)))
1320 (while (and (setq item (assq pos struct))
1321 (= (nth 1 item) ind))
1322 (setq pos (nth 6 item)))
1323 pos))
1324 (end (progn (goto-char contents-end)
1325 (skip-chars-forward " \r\t\n" limit)
1326 (if (= (point) limit) limit (line-beginning-position)))))
1327 ;; Return value.
1328 (list 'plain-list
1329 (nconc
1330 (list :type type
1331 :begin begin
1332 :end end
1333 :contents-begin contents-begin
1334 :contents-end contents-end
1335 :structure struct
1336 :post-blank (count-lines contents-end end)
1337 :post-affiliated contents-begin)
1338 (cdr affiliated))))))
1340 (defun org-element-plain-list-interpreter (plain-list contents)
1341 "Interpret PLAIN-LIST element as Org syntax.
1342 CONTENTS is the contents of the element."
1343 (with-temp-buffer
1344 (insert contents)
1345 (goto-char (point-min))
1346 (org-list-repair)
1347 (buffer-string)))
1350 ;;;; Property Drawer
1352 (defun org-element-property-drawer-parser (limit affiliated)
1353 "Parse a property drawer.
1355 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1356 the buffer position at the beginning of the first affiliated
1357 keyword and CDR is a plist of affiliated keywords along with
1358 their value.
1360 Return a list whose CAR is `property-drawer' and CDR is a plist
1361 containing `:begin', `:end', `:contents-begin', `:contents-end',
1362 `:post-blank' and `:post-affiliated' keywords.
1364 Assume point is at the beginning of the property drawer."
1365 (save-excursion
1366 (let ((case-fold-search t))
1367 (if (not (save-excursion
1368 (re-search-forward "^[ \t]*:END:[ \t]*$" limit t)))
1369 ;; Incomplete drawer: parse it as a paragraph.
1370 (org-element-paragraph-parser limit affiliated)
1371 (save-excursion
1372 (let* ((drawer-end-line (match-beginning 0))
1373 (begin (car affiliated))
1374 (post-affiliated (point))
1375 (contents-begin (progn (forward-line)
1376 (and (< (point) drawer-end-line)
1377 (point))))
1378 (contents-end (and contents-begin drawer-end-line))
1379 (pos-before-blank (progn (goto-char drawer-end-line)
1380 (forward-line)
1381 (point)))
1382 (end (progn (skip-chars-forward " \r\t\n" limit)
1383 (if (eobp) (point) (line-beginning-position)))))
1384 (list 'property-drawer
1385 (nconc
1386 (list :begin begin
1387 :end end
1388 :contents-begin contents-begin
1389 :contents-end contents-end
1390 :post-blank (count-lines pos-before-blank end)
1391 :post-affiliated post-affiliated)
1392 (cdr affiliated)))))))))
1394 (defun org-element-property-drawer-interpreter (property-drawer contents)
1395 "Interpret PROPERTY-DRAWER element as Org syntax.
1396 CONTENTS is the properties within the drawer."
1397 (format ":PROPERTIES:\n%s:END:" contents))
1400 ;;;; Quote Block
1402 (defun org-element-quote-block-parser (limit affiliated)
1403 "Parse a quote block.
1405 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1406 the buffer position at the beginning of the first affiliated
1407 keyword and CDR is a plist of affiliated keywords along with
1408 their value.
1410 Return a list whose CAR is `quote-block' and CDR is a plist
1411 containing `:begin', `:end', `:contents-begin', `:contents-end',
1412 `:post-blank' and `:post-affiliated' keywords.
1414 Assume point is at the beginning of the block."
1415 (let ((case-fold-search t))
1416 (if (not (save-excursion
1417 (re-search-forward "^[ \t]*#\\+END_QUOTE[ \t]*$" limit t)))
1418 ;; Incomplete block: parse it as a paragraph.
1419 (org-element-paragraph-parser limit affiliated)
1420 (let ((block-end-line (match-beginning 0)))
1421 (save-excursion
1422 (let* ((begin (car affiliated))
1423 (post-affiliated (point))
1424 ;; Empty blocks have no contents.
1425 (contents-begin (progn (forward-line)
1426 (and (< (point) block-end-line)
1427 (point))))
1428 (contents-end (and contents-begin block-end-line))
1429 (pos-before-blank (progn (goto-char block-end-line)
1430 (forward-line)
1431 (point)))
1432 (end (progn (skip-chars-forward " \r\t\n" limit)
1433 (if (eobp) (point) (line-beginning-position)))))
1434 (list 'quote-block
1435 (nconc
1436 (list :begin begin
1437 :end end
1438 :contents-begin contents-begin
1439 :contents-end contents-end
1440 :post-blank (count-lines pos-before-blank end)
1441 :post-affiliated post-affiliated)
1442 (cdr affiliated)))))))))
1444 (defun org-element-quote-block-interpreter (quote-block contents)
1445 "Interpret QUOTE-BLOCK element as Org syntax.
1446 CONTENTS is the contents of the element."
1447 (format "#+BEGIN_QUOTE\n%s#+END_QUOTE" contents))
1450 ;;;; Section
1452 (defun org-element-section-parser (limit)
1453 "Parse a section.
1455 LIMIT bounds the search.
1457 Return a list whose CAR is `section' and CDR is a plist
1458 containing `:begin', `:end', `:contents-begin', `contents-end'
1459 and `:post-blank' keywords."
1460 (save-excursion
1461 ;; Beginning of section is the beginning of the first non-blank
1462 ;; line after previous headline.
1463 (let ((begin (point))
1464 (end (progn (org-with-limited-levels (outline-next-heading))
1465 (point)))
1466 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
1467 (forward-line)
1468 (point))))
1469 (list 'section
1470 (list :begin begin
1471 :end end
1472 :contents-begin begin
1473 :contents-end pos-before-blank
1474 :post-blank (count-lines pos-before-blank end))))))
1476 (defun org-element-section-interpreter (section contents)
1477 "Interpret SECTION element as Org syntax.
1478 CONTENTS is the contents of the element."
1479 contents)
1482 ;;;; Special Block
1484 (defun org-element-special-block-parser (limit affiliated)
1485 "Parse a special block.
1487 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1488 the buffer position at the beginning of the first affiliated
1489 keyword and CDR is a plist of affiliated keywords along with
1490 their value.
1492 Return a list whose CAR is `special-block' and CDR is a plist
1493 containing `:type', `:begin', `:end', `:contents-begin',
1494 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
1496 Assume point is at the beginning of the block."
1497 (let* ((case-fold-search t)
1498 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
1499 (upcase (match-string-no-properties 1)))))
1500 (if (not (save-excursion
1501 (re-search-forward
1502 (format "^[ \t]*#\\+END_%s[ \t]*$" (regexp-quote type))
1503 limit t)))
1504 ;; Incomplete block: parse it as a paragraph.
1505 (org-element-paragraph-parser limit affiliated)
1506 (let ((block-end-line (match-beginning 0)))
1507 (save-excursion
1508 (let* ((begin (car affiliated))
1509 (post-affiliated (point))
1510 ;; Empty blocks have no contents.
1511 (contents-begin (progn (forward-line)
1512 (and (< (point) block-end-line)
1513 (point))))
1514 (contents-end (and contents-begin block-end-line))
1515 (pos-before-blank (progn (goto-char block-end-line)
1516 (forward-line)
1517 (point)))
1518 (end (progn (skip-chars-forward " \r\t\n" limit)
1519 (if (eobp) (point) (line-beginning-position)))))
1520 (list 'special-block
1521 (nconc
1522 (list :type type
1523 :begin begin
1524 :end end
1525 :contents-begin contents-begin
1526 :contents-end contents-end
1527 :post-blank (count-lines pos-before-blank end)
1528 :post-affiliated post-affiliated)
1529 (cdr affiliated)))))))))
1531 (defun org-element-special-block-interpreter (special-block contents)
1532 "Interpret SPECIAL-BLOCK element as Org syntax.
1533 CONTENTS is the contents of the element."
1534 (let ((block-type (org-element-property :type special-block)))
1535 (format "#+BEGIN_%s\n%s#+END_%s" block-type contents block-type)))
1539 ;;; Elements
1541 ;; For each element, a parser and an interpreter are also defined.
1542 ;; Both follow the same naming convention used for greater elements.
1544 ;; Also, as for greater elements, adding a new element type is done
1545 ;; through the following steps: implement a parser and an interpreter,
1546 ;; tweak `org-element--current-element' so that it recognizes the new
1547 ;; type and add that new type to `org-element-all-elements'.
1549 ;; As a special case, when the newly defined type is a block type,
1550 ;; `org-element-block-name-alist' has to be modified accordingly.
1553 ;;;; Babel Call
1555 (defun org-element-babel-call-parser (limit affiliated)
1556 "Parse a babel call.
1558 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1559 the buffer position at the beginning of the first affiliated
1560 keyword and CDR is a plist of affiliated keywords along with
1561 their value.
1563 Return a list whose CAR is `babel-call' and CDR is a plist
1564 containing `:begin', `:end', `:value', `:post-blank' and
1565 `:post-affiliated' as keywords."
1566 (save-excursion
1567 (let ((begin (car affiliated))
1568 (post-affiliated (point))
1569 (value (progn (let ((case-fold-search t))
1570 (re-search-forward "call:[ \t]*" nil t))
1571 (buffer-substring-no-properties (point)
1572 (line-end-position))))
1573 (pos-before-blank (progn (forward-line) (point)))
1574 (end (progn (skip-chars-forward " \r\t\n" limit)
1575 (if (eobp) (point) (line-beginning-position)))))
1576 (list 'babel-call
1577 (nconc
1578 (list :begin begin
1579 :end end
1580 :value value
1581 :post-blank (count-lines pos-before-blank end)
1582 :post-affiliated post-affiliated)
1583 (cdr affiliated))))))
1585 (defun org-element-babel-call-interpreter (babel-call contents)
1586 "Interpret BABEL-CALL element as Org syntax.
1587 CONTENTS is nil."
1588 (concat "#+CALL: " (org-element-property :value babel-call)))
1591 ;;;; Clock
1593 (defun org-element-clock-parser (limit)
1594 "Parse a clock.
1596 LIMIT bounds the search.
1598 Return a list whose CAR is `clock' and CDR is a plist containing
1599 `:status', `:value', `:time', `:begin', `:end' and `:post-blank'
1600 as keywords."
1601 (save-excursion
1602 (let* ((case-fold-search nil)
1603 (begin (point))
1604 (value (progn (search-forward org-clock-string (line-end-position) t)
1605 (skip-chars-forward " \t")
1606 (org-element-timestamp-parser)))
1607 (duration (and (search-forward " => " (line-end-position) t)
1608 (progn (skip-chars-forward " \t")
1609 (looking-at "\\(\\S-+\\)[ \t]*$"))
1610 (org-match-string-no-properties 1)))
1611 (status (if duration 'closed 'running))
1612 (post-blank (let ((before-blank (progn (forward-line) (point))))
1613 (skip-chars-forward " \r\t\n" limit)
1614 (skip-chars-backward " \t")
1615 (unless (bolp) (end-of-line))
1616 (count-lines before-blank (point))))
1617 (end (point)))
1618 (list 'clock
1619 (list :status status
1620 :value value
1621 :duration duration
1622 :begin begin
1623 :end end
1624 :post-blank post-blank)))))
1626 (defun org-element-clock-interpreter (clock contents)
1627 "Interpret CLOCK element as Org syntax.
1628 CONTENTS is nil."
1629 (concat org-clock-string " "
1630 (org-element-timestamp-interpreter
1631 (org-element-property :value clock) nil)
1632 (let ((duration (org-element-property :duration clock)))
1633 (and duration
1634 (concat " => "
1635 (apply 'format
1636 "%2s:%02s"
1637 (org-split-string duration ":")))))))
1640 ;;;; Comment
1642 (defun org-element-comment-parser (limit affiliated)
1643 "Parse a comment.
1645 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1646 the buffer position at the beginning of the first affiliated
1647 keyword and CDR is a plist of affiliated keywords along with
1648 their value.
1650 Return a list whose CAR is `comment' and CDR is a plist
1651 containing `:begin', `:end', `:value', `:post-blank',
1652 `:post-affiliated' keywords.
1654 Assume point is at comment beginning."
1655 (save-excursion
1656 (let* ((begin (car affiliated))
1657 (post-affiliated (point))
1658 (value (prog2 (looking-at "[ \t]*# ?")
1659 (buffer-substring-no-properties
1660 (match-end 0) (line-end-position))
1661 (forward-line)))
1662 (com-end
1663 ;; Get comments ending.
1664 (progn
1665 (while (and (< (point) limit) (looking-at "[ \t]*#\\( \\|$\\)"))
1666 ;; Accumulate lines without leading hash and first
1667 ;; whitespace.
1668 (setq value
1669 (concat value
1670 "\n"
1671 (buffer-substring-no-properties
1672 (match-end 0) (line-end-position))))
1673 (forward-line))
1674 (point)))
1675 (end (progn (goto-char com-end)
1676 (skip-chars-forward " \r\t\n" limit)
1677 (if (eobp) (point) (line-beginning-position)))))
1678 (list 'comment
1679 (nconc
1680 (list :begin begin
1681 :end end
1682 :value value
1683 :post-blank (count-lines com-end end)
1684 :post-affiliated post-affiliated)
1685 (cdr affiliated))))))
1687 (defun org-element-comment-interpreter (comment contents)
1688 "Interpret COMMENT element as Org syntax.
1689 CONTENTS is nil."
1690 (replace-regexp-in-string "^" "# " (org-element-property :value comment)))
1693 ;;;; Comment Block
1695 (defun org-element-comment-block-parser (limit affiliated)
1696 "Parse an export block.
1698 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1699 the buffer position at the beginning of the first affiliated
1700 keyword and CDR is a plist of affiliated keywords along with
1701 their value.
1703 Return a list whose CAR is `comment-block' and CDR is a plist
1704 containing `:begin', `:end', `:value', `:post-blank' and
1705 `:post-affiliated' keywords.
1707 Assume point is at comment block beginning."
1708 (let ((case-fold-search t))
1709 (if (not (save-excursion
1710 (re-search-forward "^[ \t]*#\\+END_COMMENT[ \t]*$" limit t)))
1711 ;; Incomplete block: parse it as a paragraph.
1712 (org-element-paragraph-parser limit affiliated)
1713 (let ((contents-end (match-beginning 0)))
1714 (save-excursion
1715 (let* ((begin (car affiliated))
1716 (post-affiliated (point))
1717 (contents-begin (progn (forward-line) (point)))
1718 (pos-before-blank (progn (goto-char contents-end)
1719 (forward-line)
1720 (point)))
1721 (end (progn (skip-chars-forward " \r\t\n" limit)
1722 (if (eobp) (point) (line-beginning-position))))
1723 (value (buffer-substring-no-properties
1724 contents-begin contents-end)))
1725 (list 'comment-block
1726 (nconc
1727 (list :begin begin
1728 :end end
1729 :value value
1730 :post-blank (count-lines pos-before-blank end)
1731 :post-affiliated post-affiliated)
1732 (cdr affiliated)))))))))
1734 (defun org-element-comment-block-interpreter (comment-block contents)
1735 "Interpret COMMENT-BLOCK element as Org syntax.
1736 CONTENTS is nil."
1737 (format "#+BEGIN_COMMENT\n%s#+END_COMMENT"
1738 (org-remove-indentation (org-element-property :value comment-block))))
1741 ;;;; Diary Sexp
1743 (defun org-element-diary-sexp-parser (limit affiliated)
1744 "Parse a diary sexp.
1746 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1747 the buffer position at the beginning of the first affiliated
1748 keyword and CDR is a plist of affiliated keywords along with
1749 their value.
1751 Return a list whose CAR is `diary-sexp' and CDR is a plist
1752 containing `:begin', `:end', `:value', `:post-blank' and
1753 `:post-affiliated' keywords."
1754 (save-excursion
1755 (let ((begin (car affiliated))
1756 (post-affiliated (point))
1757 (value (progn (looking-at "\\(%%(.*\\)[ \t]*$")
1758 (org-match-string-no-properties 1)))
1759 (pos-before-blank (progn (forward-line) (point)))
1760 (end (progn (skip-chars-forward " \r\t\n" limit)
1761 (if (eobp) (point) (line-beginning-position)))))
1762 (list 'diary-sexp
1763 (nconc
1764 (list :value value
1765 :begin begin
1766 :end end
1767 :post-blank (count-lines pos-before-blank end)
1768 :post-affiliated post-affiliated)
1769 (cdr affiliated))))))
1771 (defun org-element-diary-sexp-interpreter (diary-sexp contents)
1772 "Interpret DIARY-SEXP as Org syntax.
1773 CONTENTS is nil."
1774 (org-element-property :value diary-sexp))
1777 ;;;; Example Block
1779 (defun org-element-example-block-parser (limit affiliated)
1780 "Parse an example block.
1782 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1783 the buffer position at the beginning of the first affiliated
1784 keyword and CDR is a plist of affiliated keywords along with
1785 their value.
1787 Return a list whose CAR is `example-block' and CDR is a plist
1788 containing `:begin', `:end', `:number-lines', `:preserve-indent',
1789 `:retain-labels', `:use-labels', `:label-fmt', `:switches',
1790 `:value', `:post-blank' and `:post-affiliated' keywords."
1791 (let ((case-fold-search t))
1792 (if (not (save-excursion
1793 (re-search-forward "^[ \t]*#\\+END_EXAMPLE[ \t]*$" limit t)))
1794 ;; Incomplete block: parse it as a paragraph.
1795 (org-element-paragraph-parser limit affiliated)
1796 (let ((contents-end (match-beginning 0)))
1797 (save-excursion
1798 (let* ((switches
1799 (progn
1800 (looking-at "^[ \t]*#\\+BEGIN_EXAMPLE\\(?: +\\(.*\\)\\)?")
1801 (org-match-string-no-properties 1)))
1802 ;; Switches analysis
1803 (number-lines
1804 (cond ((not switches) nil)
1805 ((string-match "-n\\>" switches) 'new)
1806 ((string-match "+n\\>" switches) 'continued)))
1807 (preserve-indent
1808 (and switches (string-match "-i\\>" switches)))
1809 ;; Should labels be retained in (or stripped from) example
1810 ;; blocks?
1811 (retain-labels
1812 (or (not switches)
1813 (not (string-match "-r\\>" switches))
1814 (and number-lines (string-match "-k\\>" switches))))
1815 ;; What should code-references use - labels or
1816 ;; line-numbers?
1817 (use-labels
1818 (or (not switches)
1819 (and retain-labels
1820 (not (string-match "-k\\>" switches)))))
1821 (label-fmt
1822 (and switches
1823 (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
1824 (match-string 1 switches)))
1825 ;; Standard block parsing.
1826 (begin (car affiliated))
1827 (post-affiliated (point))
1828 (block-ind (progn (skip-chars-forward " \t") (current-column)))
1829 (contents-begin (progn (forward-line) (point)))
1830 (value (org-element-remove-indentation
1831 (org-unescape-code-in-string
1832 (buffer-substring-no-properties
1833 contents-begin contents-end))
1834 block-ind))
1835 (pos-before-blank (progn (goto-char contents-end)
1836 (forward-line)
1837 (point)))
1838 (end (progn (skip-chars-forward " \r\t\n" limit)
1839 (if (eobp) (point) (line-beginning-position)))))
1840 (list 'example-block
1841 (nconc
1842 (list :begin begin
1843 :end end
1844 :value value
1845 :switches switches
1846 :number-lines number-lines
1847 :preserve-indent preserve-indent
1848 :retain-labels retain-labels
1849 :use-labels use-labels
1850 :label-fmt label-fmt
1851 :post-blank (count-lines pos-before-blank end)
1852 :post-affiliated post-affiliated)
1853 (cdr affiliated)))))))))
1855 (defun org-element-example-block-interpreter (example-block contents)
1856 "Interpret EXAMPLE-BLOCK element as Org syntax.
1857 CONTENTS is nil."
1858 (let ((switches (org-element-property :switches example-block))
1859 (value (org-element-property :value example-block)))
1860 (concat "#+BEGIN_EXAMPLE" (and switches (concat " " switches)) "\n"
1861 (org-escape-code-in-string
1862 (if (or org-src-preserve-indentation
1863 (org-element-property :preserve-indent example-block))
1864 value
1865 (org-element-remove-indentation value)))
1866 "#+END_EXAMPLE")))
1869 ;;;; Export Block
1871 (defun org-element-export-block-parser (limit affiliated)
1872 "Parse an export block.
1874 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1875 the buffer position at the beginning of the first affiliated
1876 keyword and CDR is a plist of affiliated keywords along with
1877 their value.
1879 Return a list whose CAR is `export-block' and CDR is a plist
1880 containing `:begin', `:end', `:type', `:value', `:post-blank' and
1881 `:post-affiliated' keywords.
1883 Assume point is at export-block beginning."
1884 (let* ((case-fold-search t)
1885 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
1886 (upcase (org-match-string-no-properties 1)))))
1887 (if (not (save-excursion
1888 (re-search-forward
1889 (format "^[ \t]*#\\+END_%s[ \t]*$" type) limit t)))
1890 ;; Incomplete block: parse it as a paragraph.
1891 (org-element-paragraph-parser limit affiliated)
1892 (let ((contents-end (match-beginning 0)))
1893 (save-excursion
1894 (let* ((begin (car affiliated))
1895 (post-affiliated (point))
1896 (contents-begin (progn (forward-line) (point)))
1897 (pos-before-blank (progn (goto-char contents-end)
1898 (forward-line)
1899 (point)))
1900 (end (progn (skip-chars-forward " \r\t\n" limit)
1901 (if (eobp) (point) (line-beginning-position))))
1902 (value (buffer-substring-no-properties contents-begin
1903 contents-end)))
1904 (list 'export-block
1905 (nconc
1906 (list :begin begin
1907 :end end
1908 :type type
1909 :value value
1910 :post-blank (count-lines pos-before-blank end)
1911 :post-affiliated post-affiliated)
1912 (cdr affiliated)))))))))
1914 (defun org-element-export-block-interpreter (export-block contents)
1915 "Interpret EXPORT-BLOCK element as Org syntax.
1916 CONTENTS is nil."
1917 (let ((type (org-element-property :type export-block)))
1918 (concat (format "#+BEGIN_%s\n" type)
1919 (org-element-property :value export-block)
1920 (format "#+END_%s" type))))
1923 ;;;; Fixed-width
1925 (defun org-element-fixed-width-parser (limit affiliated)
1926 "Parse a fixed-width section.
1928 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1929 the buffer position at the beginning of the first affiliated
1930 keyword and CDR is a plist of affiliated keywords along with
1931 their value.
1933 Return a list whose CAR is `fixed-width' and CDR is a plist
1934 containing `:begin', `:end', `:value', `:post-blank' and
1935 `:post-affiliated' keywords.
1937 Assume point is at the beginning of the fixed-width area."
1938 (save-excursion
1939 (let* ((begin (car affiliated))
1940 (post-affiliated (point))
1941 value
1942 (end-area
1943 (progn
1944 (while (and (< (point) limit)
1945 (looking-at "[ \t]*:\\( \\|$\\)"))
1946 ;; Accumulate text without starting colons.
1947 (setq value
1948 (concat value
1949 (buffer-substring-no-properties
1950 (match-end 0) (point-at-eol))
1951 "\n"))
1952 (forward-line))
1953 (point)))
1954 (end (progn (skip-chars-forward " \r\t\n" limit)
1955 (if (eobp) (point) (line-beginning-position)))))
1956 (list 'fixed-width
1957 (nconc
1958 (list :begin begin
1959 :end end
1960 :value value
1961 :post-blank (count-lines end-area end)
1962 :post-affiliated post-affiliated)
1963 (cdr affiliated))))))
1965 (defun org-element-fixed-width-interpreter (fixed-width contents)
1966 "Interpret FIXED-WIDTH element as Org syntax.
1967 CONTENTS is nil."
1968 (let ((value (org-element-property :value fixed-width)))
1969 (and value
1970 (replace-regexp-in-string
1971 "^" ": "
1972 (if (string-match "\n\\'" value) (substring value 0 -1) value)))))
1975 ;;;; Horizontal Rule
1977 (defun org-element-horizontal-rule-parser (limit affiliated)
1978 "Parse an horizontal rule.
1980 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1981 the buffer position at the beginning of the first affiliated
1982 keyword and CDR is a plist of affiliated keywords along with
1983 their value.
1985 Return a list whose CAR is `horizontal-rule' and CDR is a plist
1986 containing `:begin', `:end', `:post-blank' and `:post-affiliated'
1987 keywords."
1988 (save-excursion
1989 (let ((begin (car affiliated))
1990 (post-affiliated (point))
1991 (post-hr (progn (forward-line) (point)))
1992 (end (progn (skip-chars-forward " \r\t\n" limit)
1993 (if (eobp) (point) (line-beginning-position)))))
1994 (list 'horizontal-rule
1995 (nconc
1996 (list :begin begin
1997 :end end
1998 :post-blank (count-lines post-hr end)
1999 :post-affiliated post-affiliated)
2000 (cdr affiliated))))))
2002 (defun org-element-horizontal-rule-interpreter (horizontal-rule contents)
2003 "Interpret HORIZONTAL-RULE element as Org syntax.
2004 CONTENTS is nil."
2005 "-----")
2008 ;;;; Keyword
2010 (defun org-element-keyword-parser (limit affiliated)
2011 "Parse a keyword at point.
2013 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2014 the buffer position at the beginning of the first affiliated
2015 keyword and CDR is a plist of affiliated keywords along with
2016 their value.
2018 Return a list whose CAR is `keyword' and CDR is a plist
2019 containing `:key', `:value', `:begin', `:end', `:post-blank' and
2020 `:post-affiliated' keywords."
2021 (save-excursion
2022 ;; An orphaned affiliated keyword is considered as a regular
2023 ;; keyword. In this case AFFILIATED is nil, so we take care of
2024 ;; this corner case.
2025 (let ((begin (or (car affiliated) (point)))
2026 (post-affiliated (point))
2027 (key (progn (looking-at "[ \t]*#\\+\\(\\S-+*\\):")
2028 (upcase (org-match-string-no-properties 1))))
2029 (value (org-trim (buffer-substring-no-properties
2030 (match-end 0) (point-at-eol))))
2031 (pos-before-blank (progn (forward-line) (point)))
2032 (end (progn (skip-chars-forward " \r\t\n" limit)
2033 (if (eobp) (point) (line-beginning-position)))))
2034 (list 'keyword
2035 (nconc
2036 (list :key key
2037 :value value
2038 :begin begin
2039 :end end
2040 :post-blank (count-lines pos-before-blank end)
2041 :post-affiliated post-affiliated)
2042 (cdr affiliated))))))
2044 (defun org-element-keyword-interpreter (keyword contents)
2045 "Interpret KEYWORD element as Org syntax.
2046 CONTENTS is nil."
2047 (format "#+%s: %s"
2048 (org-element-property :key keyword)
2049 (org-element-property :value keyword)))
2052 ;;;; Latex Environment
2054 (defun org-element-latex-environment-parser (limit affiliated)
2055 "Parse a LaTeX environment.
2057 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2058 the buffer position at the beginning of the first affiliated
2059 keyword and CDR is a plist of affiliated keywords along with
2060 their value.
2062 Return a list whose CAR is `latex-environment' and CDR is a plist
2063 containing `:begin', `:end', `:value', `:post-blank' and
2064 `:post-affiliated' keywords.
2066 Assume point is at the beginning of the latex environment."
2067 (save-excursion
2068 (let ((case-fold-search t)
2069 (code-begin (point)))
2070 (looking-at "[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}")
2071 (if (not (re-search-forward (format "^[ \t]*\\\\end{%s}[ \t]*$"
2072 (regexp-quote (match-string 1)))
2073 limit t))
2074 ;; Incomplete latex environment: parse it as a paragraph.
2075 (org-element-paragraph-parser limit affiliated)
2076 (let* ((code-end (progn (forward-line) (point)))
2077 (begin (car affiliated))
2078 (value (buffer-substring-no-properties code-begin code-end))
2079 (end (progn (skip-chars-forward " \r\t\n" limit)
2080 (if (eobp) (point) (line-beginning-position)))))
2081 (list 'latex-environment
2082 (nconc
2083 (list :begin begin
2084 :end end
2085 :value value
2086 :post-blank (count-lines code-end end)
2087 :post-affiliated code-begin)
2088 (cdr affiliated))))))))
2090 (defun org-element-latex-environment-interpreter (latex-environment contents)
2091 "Interpret LATEX-ENVIRONMENT element as Org syntax.
2092 CONTENTS is nil."
2093 (org-element-property :value latex-environment))
2096 ;;;; Node Property
2098 (defun org-element-node-property-parser (limit)
2099 "Parse a node-property at point.
2101 LIMIT bounds the search.
2103 Return a list whose CAR is `node-property' and CDR is a plist
2104 containing `:key', `:value', `:begin', `:end' and `:post-blank'
2105 keywords."
2106 (save-excursion
2107 (looking-at org-property-re)
2108 (let ((case-fold-search t)
2109 (begin (point))
2110 (key (org-match-string-no-properties 2))
2111 (value (org-match-string-no-properties 3))
2112 (pos-before-blank (progn (forward-line) (point)))
2113 (end (progn (skip-chars-forward " \r\t\n" limit)
2114 (if (eobp) (point) (point-at-bol)))))
2115 (list 'node-property
2116 (list :key key
2117 :value value
2118 :begin begin
2119 :end end
2120 :post-blank (count-lines pos-before-blank end))))))
2122 (defun org-element-node-property-interpreter (node-property contents)
2123 "Interpret NODE-PROPERTY element as Org syntax.
2124 CONTENTS is nil."
2125 (format org-property-format
2126 (format ":%s:" (org-element-property :key node-property))
2127 (org-element-property :value node-property)))
2130 ;;;; Paragraph
2132 (defun org-element-paragraph-parser (limit affiliated)
2133 "Parse a paragraph.
2135 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2136 the buffer position at the beginning of the first affiliated
2137 keyword and CDR is a plist of affiliated keywords along with
2138 their value.
2140 Return a list whose CAR is `paragraph' and CDR is a plist
2141 containing `:begin', `:end', `:contents-begin' and
2142 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
2144 Assume point is at the beginning of the paragraph."
2145 (save-excursion
2146 (let* ((begin (car affiliated))
2147 (contents-begin (point))
2148 (before-blank
2149 (let ((case-fold-search t))
2150 (end-of-line)
2151 (if (not (re-search-forward
2152 org-element-paragraph-separate limit 'm))
2153 limit
2154 ;; A matching `org-element-paragraph-separate' is not
2155 ;; necessarily the end of the paragraph. In
2156 ;; particular, lines starting with # or : as a first
2157 ;; non-space character are ambiguous. We have to
2158 ;; check if they are valid Org syntax (e.g., not an
2159 ;; incomplete keyword).
2160 (beginning-of-line)
2161 (while (not
2163 ;; There's no ambiguity for other symbols or
2164 ;; empty lines: stop here.
2165 (looking-at "[ \t]*\\(?:[^:#]\\|$\\)")
2166 ;; Stop at valid fixed-width areas.
2167 (looking-at "[ \t]*:\\(?: \\|$\\)")
2168 ;; Stop at drawers.
2169 (and (looking-at org-drawer-regexp)
2170 (save-excursion
2171 (re-search-forward
2172 "^[ \t]*:END:[ \t]*$" limit t)))
2173 ;; Stop at valid comments.
2174 (looking-at "[ \t]*#\\(?: \\|$\\)")
2175 ;; Stop at valid dynamic blocks.
2176 (and (looking-at org-dblock-start-re)
2177 (save-excursion
2178 (re-search-forward
2179 "^[ \t]*#\\+END:?[ \t]*$" limit t)))
2180 ;; Stop at valid blocks.
2181 (and (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
2182 (save-excursion
2183 (re-search-forward
2184 (format "^[ \t]*#\\+END_%s[ \t]*$"
2185 (regexp-quote
2186 (org-match-string-no-properties 1)))
2187 limit t)))
2188 ;; Stop at valid latex environments.
2189 (and (looking-at
2190 "[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}")
2191 (save-excursion
2192 (re-search-forward
2193 (format "^[ \t]*\\\\end{%s}[ \t]*$"
2194 (regexp-quote
2195 (org-match-string-no-properties 1)))
2196 limit t)))
2197 ;; Stop at valid keywords.
2198 (looking-at "[ \t]*#\\+\\S-+:")
2199 ;; Skip everything else.
2200 (not
2201 (progn
2202 (end-of-line)
2203 (re-search-forward org-element-paragraph-separate
2204 limit 'm)))))
2205 (beginning-of-line)))
2206 (if (= (point) limit) limit
2207 (goto-char (line-beginning-position)))))
2208 (contents-end (progn (skip-chars-backward " \r\t\n" contents-begin)
2209 (forward-line)
2210 (point)))
2211 (end (progn (skip-chars-forward " \r\t\n" limit)
2212 (if (eobp) (point) (line-beginning-position)))))
2213 (list 'paragraph
2214 (nconc
2215 (list :begin begin
2216 :end end
2217 :contents-begin contents-begin
2218 :contents-end contents-end
2219 :post-blank (count-lines before-blank end)
2220 :post-affiliated contents-begin)
2221 (cdr affiliated))))))
2223 (defun org-element-paragraph-interpreter (paragraph contents)
2224 "Interpret PARAGRAPH element as Org syntax.
2225 CONTENTS is the contents of the element."
2226 contents)
2229 ;;;; Planning
2231 (defun org-element-planning-parser (limit)
2232 "Parse a planning.
2234 LIMIT bounds the search.
2236 Return a list whose CAR is `planning' and CDR is a plist
2237 containing `:closed', `:deadline', `:scheduled', `:begin', `:end'
2238 and `:post-blank' keywords."
2239 (save-excursion
2240 (let* ((case-fold-search nil)
2241 (begin (point))
2242 (post-blank (let ((before-blank (progn (forward-line) (point))))
2243 (skip-chars-forward " \r\t\n" limit)
2244 (skip-chars-backward " \t")
2245 (unless (bolp) (end-of-line))
2246 (count-lines before-blank (point))))
2247 (end (point))
2248 closed deadline scheduled)
2249 (goto-char begin)
2250 (while (re-search-forward org-keyword-time-not-clock-regexp end t)
2251 (goto-char (match-end 1))
2252 (skip-chars-forward " \t" end)
2253 (let ((keyword (match-string 1))
2254 (time (org-element-timestamp-parser)))
2255 (cond ((equal keyword org-closed-string) (setq closed time))
2256 ((equal keyword org-deadline-string) (setq deadline time))
2257 (t (setq scheduled time)))))
2258 (list 'planning
2259 (list :closed closed
2260 :deadline deadline
2261 :scheduled scheduled
2262 :begin begin
2263 :end end
2264 :post-blank post-blank)))))
2266 (defun org-element-planning-interpreter (planning contents)
2267 "Interpret PLANNING element as Org syntax.
2268 CONTENTS is nil."
2269 (mapconcat
2270 'identity
2271 (delq nil
2272 (list (let ((deadline (org-element-property :deadline planning)))
2273 (when deadline
2274 (concat org-deadline-string " "
2275 (org-element-timestamp-interpreter deadline nil))))
2276 (let ((scheduled (org-element-property :scheduled planning)))
2277 (when scheduled
2278 (concat org-scheduled-string " "
2279 (org-element-timestamp-interpreter scheduled nil))))
2280 (let ((closed (org-element-property :closed planning)))
2281 (when closed
2282 (concat org-closed-string " "
2283 (org-element-timestamp-interpreter closed nil))))))
2284 " "))
2287 ;;;; Src Block
2289 (defun org-element-src-block-parser (limit affiliated)
2290 "Parse a src block.
2292 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2293 the buffer position at the beginning of the first affiliated
2294 keyword and CDR is a plist of affiliated keywords along with
2295 their value.
2297 Return a list whose CAR is `src-block' and CDR is a plist
2298 containing `:language', `:switches', `:parameters', `:begin',
2299 `:end', `:number-lines', `:retain-labels', `:use-labels',
2300 `:label-fmt', `:preserve-indent', `:value', `:post-blank' and
2301 `:post-affiliated' keywords.
2303 Assume point is at the beginning of the block."
2304 (let ((case-fold-search t))
2305 (if (not (save-excursion (re-search-forward "^[ \t]*#\\+END_SRC[ \t]*$"
2306 limit t)))
2307 ;; Incomplete block: parse it as a paragraph.
2308 (org-element-paragraph-parser limit affiliated)
2309 (let ((contents-end (match-beginning 0)))
2310 (save-excursion
2311 (let* ((begin (car affiliated))
2312 (post-affiliated (point))
2313 ;; Get language as a string.
2314 (language
2315 (progn
2316 (looking-at
2317 (concat "^[ \t]*#\\+BEGIN_SRC"
2318 "\\(?: +\\(\\S-+\\)\\)?"
2319 "\\(\\(?: +\\(?:-l \".*?\"\\|[-+][A-Za-z]\\)\\)+\\)?"
2320 "\\(.*\\)[ \t]*$"))
2321 (org-match-string-no-properties 1)))
2322 ;; Get switches.
2323 (switches (org-match-string-no-properties 2))
2324 ;; Get parameters.
2325 (parameters (org-match-string-no-properties 3))
2326 ;; Switches analysis
2327 (number-lines
2328 (cond ((not switches) nil)
2329 ((string-match "-n\\>" switches) 'new)
2330 ((string-match "+n\\>" switches) 'continued)))
2331 (preserve-indent (and switches
2332 (string-match "-i\\>" switches)))
2333 (label-fmt
2334 (and switches
2335 (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
2336 (match-string 1 switches)))
2337 ;; Should labels be retained in (or stripped from)
2338 ;; src blocks?
2339 (retain-labels
2340 (or (not switches)
2341 (not (string-match "-r\\>" switches))
2342 (and number-lines (string-match "-k\\>" switches))))
2343 ;; What should code-references use - labels or
2344 ;; line-numbers?
2345 (use-labels
2346 (or (not switches)
2347 (and retain-labels
2348 (not (string-match "-k\\>" switches)))))
2349 ;; Indentation.
2350 (block-ind (progn (skip-chars-forward " \t") (current-column)))
2351 ;; Retrieve code.
2352 (value (org-element-remove-indentation
2353 (org-unescape-code-in-string
2354 (buffer-substring-no-properties
2355 (progn (forward-line) (point)) contents-end))
2356 block-ind))
2357 (pos-before-blank (progn (goto-char contents-end)
2358 (forward-line)
2359 (point)))
2360 ;; Get position after ending blank lines.
2361 (end (progn (skip-chars-forward " \r\t\n" limit)
2362 (if (eobp) (point) (line-beginning-position)))))
2363 (list 'src-block
2364 (nconc
2365 (list :language language
2366 :switches (and (org-string-nw-p switches)
2367 (org-trim switches))
2368 :parameters (and (org-string-nw-p parameters)
2369 (org-trim parameters))
2370 :begin begin
2371 :end end
2372 :number-lines number-lines
2373 :preserve-indent preserve-indent
2374 :retain-labels retain-labels
2375 :use-labels use-labels
2376 :label-fmt label-fmt
2377 :value value
2378 :post-blank (count-lines pos-before-blank end)
2379 :post-affiliated post-affiliated)
2380 (cdr affiliated)))))))))
2382 (defun org-element-src-block-interpreter (src-block contents)
2383 "Interpret SRC-BLOCK element as Org syntax.
2384 CONTENTS is nil."
2385 (let ((lang (org-element-property :language src-block))
2386 (switches (org-element-property :switches src-block))
2387 (params (org-element-property :parameters src-block))
2388 (value
2389 (let ((val (org-element-property :value src-block)))
2390 (cond
2391 ((or org-src-preserve-indentation
2392 (org-element-property :preserve-indent src-block))
2393 val)
2394 ((zerop org-edit-src-content-indentation) val)
2396 (let ((ind (make-string org-edit-src-content-indentation ?\s)))
2397 (replace-regexp-in-string
2398 "\\(^\\)[ \t]*\\S-" ind val nil nil 1)))))))
2399 (concat (format "#+BEGIN_SRC%s\n"
2400 (concat (and lang (concat " " lang))
2401 (and switches (concat " " switches))
2402 (and params (concat " " params))))
2403 (org-escape-code-in-string value)
2404 "#+END_SRC")))
2407 ;;;; Table
2409 (defun org-element-table-parser (limit affiliated)
2410 "Parse a table at point.
2412 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2413 the buffer position at the beginning of the first affiliated
2414 keyword and CDR is a plist of affiliated keywords along with
2415 their value.
2417 Return a list whose CAR is `table' and CDR is a plist containing
2418 `:begin', `:end', `:tblfm', `:type', `:contents-begin',
2419 `:contents-end', `:value', `:post-blank' and `:post-affiliated'
2420 keywords.
2422 Assume point is at the beginning of the table."
2423 (save-excursion
2424 (let* ((case-fold-search t)
2425 (table-begin (point))
2426 (type (if (org-at-table.el-p) 'table.el 'org))
2427 (begin (car affiliated))
2428 (table-end
2429 (if (re-search-forward org-table-any-border-regexp limit 'm)
2430 (goto-char (match-beginning 0))
2431 (point)))
2432 (tblfm (let (acc)
2433 (while (looking-at "[ \t]*#\\+TBLFM: +\\(.*\\)[ \t]*$")
2434 (push (org-match-string-no-properties 1) acc)
2435 (forward-line))
2436 acc))
2437 (pos-before-blank (point))
2438 (end (progn (skip-chars-forward " \r\t\n" limit)
2439 (if (eobp) (point) (line-beginning-position)))))
2440 (list 'table
2441 (nconc
2442 (list :begin begin
2443 :end end
2444 :type type
2445 :tblfm tblfm
2446 ;; Only `org' tables have contents. `table.el' tables
2447 ;; use a `:value' property to store raw table as
2448 ;; a string.
2449 :contents-begin (and (eq type 'org) table-begin)
2450 :contents-end (and (eq type 'org) table-end)
2451 :value (and (eq type 'table.el)
2452 (buffer-substring-no-properties
2453 table-begin table-end))
2454 :post-blank (count-lines pos-before-blank end)
2455 :post-affiliated table-begin)
2456 (cdr affiliated))))))
2458 (defun org-element-table-interpreter (table contents)
2459 "Interpret TABLE element as Org syntax.
2460 CONTENTS is nil."
2461 (if (eq (org-element-property :type table) 'table.el)
2462 (org-remove-indentation (org-element-property :value table))
2463 (concat (with-temp-buffer (insert contents)
2464 (org-table-align)
2465 (buffer-string))
2466 (mapconcat (lambda (fm) (concat "#+TBLFM: " fm))
2467 (reverse (org-element-property :tblfm table))
2468 "\n"))))
2471 ;;;; Table Row
2473 (defun org-element-table-row-parser (limit)
2474 "Parse table row at point.
2476 LIMIT bounds the search.
2478 Return a list whose CAR is `table-row' and CDR is a plist
2479 containing `:begin', `:end', `:contents-begin', `:contents-end',
2480 `:type' and `:post-blank' keywords."
2481 (save-excursion
2482 (let* ((type (if (looking-at "^[ \t]*|-") 'rule 'standard))
2483 (begin (point))
2484 ;; A table rule has no contents. In that case, ensure
2485 ;; CONTENTS-BEGIN matches CONTENTS-END.
2486 (contents-begin (and (eq type 'standard)
2487 (search-forward "|")
2488 (point)))
2489 (contents-end (and (eq type 'standard)
2490 (progn
2491 (end-of-line)
2492 (skip-chars-backward " \t")
2493 (point))))
2494 (end (progn (forward-line) (point))))
2495 (list 'table-row
2496 (list :type type
2497 :begin begin
2498 :end end
2499 :contents-begin contents-begin
2500 :contents-end contents-end
2501 :post-blank 0)))))
2503 (defun org-element-table-row-interpreter (table-row contents)
2504 "Interpret TABLE-ROW element as Org syntax.
2505 CONTENTS is the contents of the table row."
2506 (if (eq (org-element-property :type table-row) 'rule) "|-"
2507 (concat "| " contents)))
2510 ;;;; Verse Block
2512 (defun org-element-verse-block-parser (limit affiliated)
2513 "Parse a verse block.
2515 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2516 the buffer position at the beginning of the first affiliated
2517 keyword and CDR is a plist of affiliated keywords along with
2518 their value.
2520 Return a list whose CAR is `verse-block' and CDR is a plist
2521 containing `:begin', `:end', `:contents-begin', `:contents-end',
2522 `:post-blank' and `:post-affiliated' keywords.
2524 Assume point is at beginning of the block."
2525 (let ((case-fold-search t))
2526 (if (not (save-excursion
2527 (re-search-forward "^[ \t]*#\\+END_VERSE[ \t]*$" limit t)))
2528 ;; Incomplete block: parse it as a paragraph.
2529 (org-element-paragraph-parser limit affiliated)
2530 (let ((contents-end (match-beginning 0)))
2531 (save-excursion
2532 (let* ((begin (car affiliated))
2533 (post-affiliated (point))
2534 (contents-begin (progn (forward-line) (point)))
2535 (pos-before-blank (progn (goto-char contents-end)
2536 (forward-line)
2537 (point)))
2538 (end (progn (skip-chars-forward " \r\t\n" limit)
2539 (if (eobp) (point) (line-beginning-position)))))
2540 (list 'verse-block
2541 (nconc
2542 (list :begin begin
2543 :end end
2544 :contents-begin contents-begin
2545 :contents-end contents-end
2546 :post-blank (count-lines pos-before-blank end)
2547 :post-affiliated post-affiliated)
2548 (cdr affiliated)))))))))
2550 (defun org-element-verse-block-interpreter (verse-block contents)
2551 "Interpret VERSE-BLOCK element as Org syntax.
2552 CONTENTS is verse block contents."
2553 (format "#+BEGIN_VERSE\n%s#+END_VERSE" contents))
2557 ;;; Objects
2559 ;; Unlike to elements, interstices can be found between objects.
2560 ;; That's why, along with the parser, successor functions are provided
2561 ;; for each object. Some objects share the same successor (e.g.,
2562 ;; `code' and `verbatim' objects).
2564 ;; A successor must accept a single argument bounding the search. It
2565 ;; will return either a cons cell whose CAR is the object's type, as
2566 ;; a symbol, and CDR the position of its next occurrence, or nil.
2568 ;; Successors follow the naming convention:
2569 ;; org-element-NAME-successor, where NAME is the name of the
2570 ;; successor, as defined in `org-element-all-successors'.
2572 ;; Some object types (e.g., `italic') are recursive. Restrictions on
2573 ;; object types they can contain will be specified in
2574 ;; `org-element-object-restrictions'.
2576 ;; Adding a new type of object is simple. Implement a successor,
2577 ;; a parser, and an interpreter for it, all following the naming
2578 ;; convention. Register type in `org-element-all-objects' and
2579 ;; successor in `org-element-all-successors'. Maybe tweak
2580 ;; restrictions about it, and that's it.
2583 ;;;; Bold
2585 (defun org-element-bold-parser ()
2586 "Parse bold object at point.
2588 Return a list whose CAR is `bold' and CDR is a plist with
2589 `:begin', `:end', `:contents-begin' and `:contents-end' and
2590 `:post-blank' keywords.
2592 Assume point is at the first star marker."
2593 (save-excursion
2594 (unless (bolp) (backward-char 1))
2595 (looking-at org-emph-re)
2596 (let ((begin (match-beginning 2))
2597 (contents-begin (match-beginning 4))
2598 (contents-end (match-end 4))
2599 (post-blank (progn (goto-char (match-end 2))
2600 (skip-chars-forward " \t")))
2601 (end (point)))
2602 (list 'bold
2603 (list :begin begin
2604 :end end
2605 :contents-begin contents-begin
2606 :contents-end contents-end
2607 :post-blank post-blank)))))
2609 (defun org-element-bold-interpreter (bold contents)
2610 "Interpret BOLD object as Org syntax.
2611 CONTENTS is the contents of the object."
2612 (format "*%s*" contents))
2614 (defun org-element-text-markup-successor ()
2615 "Search for the next text-markup object.
2617 Return value is a cons cell whose CAR is a symbol among `bold',
2618 `italic', `underline', `strike-through', `code' and `verbatim'
2619 and CDR is beginning position."
2620 (save-excursion
2621 (unless (bolp) (backward-char))
2622 (when (re-search-forward org-emph-re nil t)
2623 (let ((marker (match-string 3)))
2624 (cons (cond
2625 ((equal marker "*") 'bold)
2626 ((equal marker "/") 'italic)
2627 ((equal marker "_") 'underline)
2628 ((equal marker "+") 'strike-through)
2629 ((equal marker "~") 'code)
2630 ((equal marker "=") 'verbatim)
2631 (t (error "Unknown marker at %d" (match-beginning 3))))
2632 (match-beginning 2))))))
2635 ;;;; Code
2637 (defun org-element-code-parser ()
2638 "Parse code object at point.
2640 Return a list whose CAR is `code' and CDR is a plist with
2641 `:value', `:begin', `:end' and `:post-blank' keywords.
2643 Assume point is at the first tilde marker."
2644 (save-excursion
2645 (unless (bolp) (backward-char 1))
2646 (looking-at org-emph-re)
2647 (let ((begin (match-beginning 2))
2648 (value (org-match-string-no-properties 4))
2649 (post-blank (progn (goto-char (match-end 2))
2650 (skip-chars-forward " \t")))
2651 (end (point)))
2652 (list 'code
2653 (list :value value
2654 :begin begin
2655 :end end
2656 :post-blank post-blank)))))
2658 (defun org-element-code-interpreter (code contents)
2659 "Interpret CODE object as Org syntax.
2660 CONTENTS is nil."
2661 (format "~%s~" (org-element-property :value code)))
2664 ;;;; Entity
2666 (defun org-element-entity-parser ()
2667 "Parse entity at point.
2669 Return a list whose CAR is `entity' and CDR a plist with
2670 `:begin', `:end', `:latex', `:latex-math-p', `:html', `:latin1',
2671 `:utf-8', `:ascii', `:use-brackets-p' and `:post-blank' as
2672 keywords.
2674 Assume point is at the beginning of the entity."
2675 (save-excursion
2676 (looking-at "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)")
2677 (let* ((value (org-entity-get (match-string 1)))
2678 (begin (match-beginning 0))
2679 (bracketsp (string= (match-string 2) "{}"))
2680 (post-blank (progn (goto-char (match-end 1))
2681 (when bracketsp (forward-char 2))
2682 (skip-chars-forward " \t")))
2683 (end (point)))
2684 (list 'entity
2685 (list :name (car value)
2686 :latex (nth 1 value)
2687 :latex-math-p (nth 2 value)
2688 :html (nth 3 value)
2689 :ascii (nth 4 value)
2690 :latin1 (nth 5 value)
2691 :utf-8 (nth 6 value)
2692 :begin begin
2693 :end end
2694 :use-brackets-p bracketsp
2695 :post-blank post-blank)))))
2697 (defun org-element-entity-interpreter (entity contents)
2698 "Interpret ENTITY object as Org syntax.
2699 CONTENTS is nil."
2700 (concat "\\"
2701 (org-element-property :name entity)
2702 (when (org-element-property :use-brackets-p entity) "{}")))
2704 (defun org-element-latex-or-entity-successor ()
2705 "Search for the next latex-fragment or entity object.
2707 Return value is a cons cell whose CAR is `entity' or
2708 `latex-fragment' and CDR is beginning position."
2709 (save-excursion
2710 (unless (bolp) (backward-char))
2711 (let ((matchers (cdr org-latex-regexps))
2712 ;; ENTITY-RE matches both LaTeX commands and Org entities.
2713 (entity-re
2714 "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))
2715 (when (re-search-forward
2716 (concat (mapconcat #'cadr matchers "\\|") "\\|" entity-re) nil t)
2717 (goto-char (match-beginning 0))
2718 (if (looking-at entity-re)
2719 ;; Determine if it's a real entity or a LaTeX command.
2720 (cons (if (org-entity-get (match-string 1)) 'entity 'latex-fragment)
2721 (match-beginning 0))
2722 ;; No entity nor command: point is at a LaTeX fragment.
2723 ;; Determine its type to get the correct beginning position.
2724 (cons 'latex-fragment
2725 (catch 'return
2726 (dolist (e matchers)
2727 (when (looking-at (nth 1 e))
2728 (throw 'return (match-beginning (nth 2 e)))))
2729 (point))))))))
2732 ;;;; Export Snippet
2734 (defun org-element-export-snippet-parser ()
2735 "Parse export snippet at point.
2737 Return a list whose CAR is `export-snippet' and CDR a plist with
2738 `:begin', `:end', `:back-end', `:value' and `:post-blank' as
2739 keywords.
2741 Assume point is at the beginning of the snippet."
2742 (save-excursion
2743 (re-search-forward "@@\\([-A-Za-z0-9]+\\):" nil t)
2744 (let* ((begin (match-beginning 0))
2745 (back-end (org-match-string-no-properties 1))
2746 (value (buffer-substring-no-properties
2747 (point)
2748 (progn (re-search-forward "@@" nil t) (match-beginning 0))))
2749 (post-blank (skip-chars-forward " \t"))
2750 (end (point)))
2751 (list 'export-snippet
2752 (list :back-end back-end
2753 :value value
2754 :begin begin
2755 :end end
2756 :post-blank post-blank)))))
2758 (defun org-element-export-snippet-interpreter (export-snippet contents)
2759 "Interpret EXPORT-SNIPPET object as Org syntax.
2760 CONTENTS is nil."
2761 (format "@@%s:%s@@"
2762 (org-element-property :back-end export-snippet)
2763 (org-element-property :value export-snippet)))
2765 (defun org-element-export-snippet-successor ()
2766 "Search for the next export-snippet object.
2768 Return value is a cons cell whose CAR is `export-snippet' and CDR
2769 its beginning position."
2770 (save-excursion
2771 (let (beg)
2772 (when (and (re-search-forward "@@[-A-Za-z0-9]+:" nil t)
2773 (setq beg (match-beginning 0))
2774 (search-forward "@@" nil t))
2775 (cons 'export-snippet beg)))))
2778 ;;;; Footnote Reference
2780 (defun org-element-footnote-reference-parser ()
2781 "Parse footnote reference at point.
2783 Return a list whose CAR is `footnote-reference' and CDR a plist
2784 with `:label', `:type', `:inline-definition', `:begin', `:end'
2785 and `:post-blank' as keywords."
2786 (save-excursion
2787 (looking-at org-footnote-re)
2788 (let* ((begin (point))
2789 (label (or (org-match-string-no-properties 2)
2790 (org-match-string-no-properties 3)
2791 (and (match-string 1)
2792 (concat "fn:" (org-match-string-no-properties 1)))))
2793 (type (if (or (not label) (match-string 1)) 'inline 'standard))
2794 (inner-begin (match-end 0))
2795 (inner-end
2796 (let ((count 1))
2797 (forward-char)
2798 (while (and (> count 0) (re-search-forward "[][]" nil t))
2799 (if (equal (match-string 0) "[") (incf count) (decf count)))
2800 (1- (point))))
2801 (post-blank (progn (goto-char (1+ inner-end))
2802 (skip-chars-forward " \t")))
2803 (end (point))
2804 (footnote-reference
2805 (list 'footnote-reference
2806 (list :label label
2807 :type type
2808 :begin begin
2809 :end end
2810 :post-blank post-blank))))
2811 (org-element-put-property
2812 footnote-reference :inline-definition
2813 (and (eq type 'inline)
2814 (org-element-parse-secondary-string
2815 (buffer-substring inner-begin inner-end)
2816 (org-element-restriction 'footnote-reference)
2817 footnote-reference))))))
2819 (defun org-element-footnote-reference-interpreter (footnote-reference contents)
2820 "Interpret FOOTNOTE-REFERENCE object as Org syntax.
2821 CONTENTS is nil."
2822 (let ((label (or (org-element-property :label footnote-reference) "fn:"))
2823 (def
2824 (let ((inline-def
2825 (org-element-property :inline-definition footnote-reference)))
2826 (if (not inline-def) ""
2827 (concat ":" (org-element-interpret-data inline-def))))))
2828 (format "[%s]" (concat label def))))
2830 (defun org-element-footnote-reference-successor ()
2831 "Search for the next footnote-reference object.
2833 Return value is a cons cell whose CAR is `footnote-reference' and
2834 CDR is beginning position."
2835 (save-excursion
2836 (catch 'exit
2837 (while (re-search-forward org-footnote-re nil t)
2838 (save-excursion
2839 (let ((beg (match-beginning 0))
2840 (count 1))
2841 (backward-char)
2842 (while (re-search-forward "[][]" nil t)
2843 (if (equal (match-string 0) "[") (incf count) (decf count))
2844 (when (zerop count)
2845 (throw 'exit (cons 'footnote-reference beg))))))))))
2848 ;;;; Inline Babel Call
2850 (defun org-element-inline-babel-call-parser ()
2851 "Parse inline babel call at point.
2853 Return a list whose CAR is `inline-babel-call' and CDR a plist
2854 with `:begin', `:end', `:value' and `:post-blank' as keywords.
2856 Assume point is at the beginning of the babel call."
2857 (save-excursion
2858 (unless (bolp) (backward-char))
2859 (let ((case-fold-search t))
2860 (looking-at org-babel-inline-lob-one-liner-regexp))
2861 (let ((begin (match-end 1))
2862 (value (buffer-substring-no-properties (match-end 1) (match-end 0)))
2863 (post-blank (progn (goto-char (match-end 0))
2864 (skip-chars-forward " \t")))
2865 (end (point)))
2866 (list 'inline-babel-call
2867 (list :begin begin
2868 :end end
2869 :value value
2870 :post-blank post-blank)))))
2872 (defun org-element-inline-babel-call-interpreter (inline-babel-call contents)
2873 "Interpret INLINE-BABEL-CALL object as Org syntax.
2874 CONTENTS is nil."
2875 (org-element-property :value inline-babel-call))
2877 (defun org-element-inline-babel-call-successor ()
2878 "Search for the next inline-babel-call object.
2880 Return value is a cons cell whose CAR is `inline-babel-call' and
2881 CDR is beginning position."
2882 (save-excursion
2883 (catch 'exit
2884 (while (search-forward "call_" nil t)
2885 (save-excursion
2886 (goto-char (match-beginning 0))
2887 (when (looking-at org-babel-inline-lob-one-liner-regexp)
2888 (throw 'exit (cons 'inline-babel-call (point)))))))))
2891 ;;;; Inline Src Block
2893 (defun org-element-inline-src-block-parser ()
2894 "Parse inline source block at point.
2896 Return a list whose CAR is `inline-src-block' and CDR a plist
2897 with `:begin', `:end', `:language', `:value', `:parameters' and
2898 `:post-blank' as keywords.
2900 Assume point is at the beginning of the inline src block."
2901 (save-excursion
2902 (unless (bolp) (backward-char))
2903 (looking-at org-babel-inline-src-block-regexp)
2904 (let ((begin (match-beginning 1))
2905 (language (org-match-string-no-properties 2))
2906 (parameters (org-match-string-no-properties 4))
2907 (value (org-match-string-no-properties 5))
2908 (post-blank (progn (goto-char (match-end 0))
2909 (skip-chars-forward " \t")))
2910 (end (point)))
2911 (list 'inline-src-block
2912 (list :language language
2913 :value value
2914 :parameters parameters
2915 :begin begin
2916 :end end
2917 :post-blank post-blank)))))
2919 (defun org-element-inline-src-block-interpreter (inline-src-block contents)
2920 "Interpret INLINE-SRC-BLOCK object as Org syntax.
2921 CONTENTS is nil."
2922 (let ((language (org-element-property :language inline-src-block))
2923 (arguments (org-element-property :parameters inline-src-block))
2924 (body (org-element-property :value inline-src-block)))
2925 (format "src_%s%s{%s}"
2926 language
2927 (if arguments (format "[%s]" arguments) "")
2928 body)))
2930 (defun org-element-inline-src-block-successor ()
2931 "Search for the next inline-babel-call element.
2933 Return value is a cons cell whose CAR is `inline-babel-call' and
2934 CDR is beginning position."
2935 (save-excursion
2936 (unless (bolp) (backward-char))
2937 (when (re-search-forward org-babel-inline-src-block-regexp nil t)
2938 (cons 'inline-src-block (match-beginning 1)))))
2940 ;;;; Italic
2942 (defun org-element-italic-parser ()
2943 "Parse italic object at point.
2945 Return a list whose CAR is `italic' and CDR is a plist with
2946 `:begin', `:end', `:contents-begin' and `:contents-end' and
2947 `:post-blank' keywords.
2949 Assume point is at the first slash marker."
2950 (save-excursion
2951 (unless (bolp) (backward-char 1))
2952 (looking-at org-emph-re)
2953 (let ((begin (match-beginning 2))
2954 (contents-begin (match-beginning 4))
2955 (contents-end (match-end 4))
2956 (post-blank (progn (goto-char (match-end 2))
2957 (skip-chars-forward " \t")))
2958 (end (point)))
2959 (list 'italic
2960 (list :begin begin
2961 :end end
2962 :contents-begin contents-begin
2963 :contents-end contents-end
2964 :post-blank post-blank)))))
2966 (defun org-element-italic-interpreter (italic contents)
2967 "Interpret ITALIC object as Org syntax.
2968 CONTENTS is the contents of the object."
2969 (format "/%s/" contents))
2972 ;;;; Latex Fragment
2974 (defun org-element-latex-fragment-parser ()
2975 "Parse LaTeX fragment at point.
2977 Return a list whose CAR is `latex-fragment' and CDR a plist with
2978 `:value', `:begin', `:end', and `:post-blank' as keywords.
2980 Assume point is at the beginning of the LaTeX fragment."
2981 (save-excursion
2982 (let* ((begin (point))
2983 (substring-match
2984 (catch 'exit
2985 (dolist (e (cdr org-latex-regexps))
2986 (let ((latex-regexp (nth 1 e)))
2987 (when (or (looking-at latex-regexp)
2988 (and (not (bobp))
2989 (save-excursion
2990 (backward-char)
2991 (looking-at latex-regexp))))
2992 (throw 'exit (nth 2 e)))))
2993 ;; None found: it's a macro.
2994 (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")
2996 (value (org-match-string-no-properties substring-match))
2997 (post-blank (progn (goto-char (match-end substring-match))
2998 (skip-chars-forward " \t")))
2999 (end (point)))
3000 (list 'latex-fragment
3001 (list :value value
3002 :begin begin
3003 :end end
3004 :post-blank post-blank)))))
3006 (defun org-element-latex-fragment-interpreter (latex-fragment contents)
3007 "Interpret LATEX-FRAGMENT object as Org syntax.
3008 CONTENTS is nil."
3009 (org-element-property :value latex-fragment))
3011 ;;;; Line Break
3013 (defun org-element-line-break-parser ()
3014 "Parse line break at point.
3016 Return a list whose CAR is `line-break', and CDR a plist with
3017 `:begin', `:end' and `:post-blank' keywords.
3019 Assume point is at the beginning of the line break."
3020 (list 'line-break
3021 (list :begin (point)
3022 :end (progn (forward-line) (point))
3023 :post-blank 0)))
3025 (defun org-element-line-break-interpreter (line-break contents)
3026 "Interpret LINE-BREAK object as Org syntax.
3027 CONTENTS is nil."
3028 "\\\\\n")
3030 (defun org-element-line-break-successor ()
3031 "Search for the next line-break object.
3033 Return value is a cons cell whose CAR is `line-break' and CDR is
3034 beginning position."
3035 (save-excursion
3036 (let ((beg (and (re-search-forward "[^\\\\]\\(\\\\\\\\\\)[ \t]*$" nil t)
3037 (goto-char (match-beginning 1)))))
3038 ;; A line break can only happen on a non-empty line.
3039 (when (and beg (re-search-backward "\\S-" (point-at-bol) t))
3040 (cons 'line-break beg)))))
3043 ;;;; Link
3045 (defun org-element-link-parser ()
3046 "Parse link at point.
3048 Return a list whose CAR is `link' and CDR a plist with `:type',
3049 `:path', `:raw-link', `:application', `:search-option', `:begin',
3050 `:end', `:contents-begin', `:contents-end' and `:post-blank' as
3051 keywords.
3053 Assume point is at the beginning of the link."
3054 (save-excursion
3055 (let ((begin (point))
3056 end contents-begin contents-end link-end post-blank path type
3057 raw-link link search-option application)
3058 (cond
3059 ;; Type 1: Text targeted from a radio target.
3060 ((and org-target-link-regexp (looking-at org-target-link-regexp))
3061 (setq type "radio"
3062 link-end (match-end 0)
3063 path (org-match-string-no-properties 0)))
3064 ;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]]
3065 ((looking-at org-bracket-link-regexp)
3066 (setq contents-begin (match-beginning 3)
3067 contents-end (match-end 3)
3068 link-end (match-end 0)
3069 ;; RAW-LINK is the original link. Expand any
3070 ;; abbreviation in it.
3071 raw-link (org-translate-link
3072 (org-link-expand-abbrev
3073 (org-match-string-no-properties 1))))
3074 ;; Determine TYPE of link and set PATH accordingly.
3075 (cond
3076 ;; File type.
3077 ((or (file-name-absolute-p raw-link)
3078 (string-match "^\\.\\.?/" raw-link))
3079 (setq type "file" path raw-link))
3080 ;; Explicit type (http, irc, bbdb...). See `org-link-types'.
3081 ((string-match org-link-re-with-space3 raw-link)
3082 (setq type (match-string 1 raw-link) path (match-string 2 raw-link)))
3083 ;; Id type: PATH is the id.
3084 ((string-match "^id:\\([-a-f0-9]+\\)" raw-link)
3085 (setq type "id" path (match-string 1 raw-link)))
3086 ;; Code-ref type: PATH is the name of the reference.
3087 ((string-match "^(\\(.*\\))$" raw-link)
3088 (setq type "coderef" path (match-string 1 raw-link)))
3089 ;; Custom-id type: PATH is the name of the custom id.
3090 ((= (aref raw-link 0) ?#)
3091 (setq type "custom-id" path (substring raw-link 1)))
3092 ;; Fuzzy type: Internal link either matches a target, an
3093 ;; headline name or nothing. PATH is the target or
3094 ;; headline's name.
3095 (t (setq type "fuzzy" path raw-link))))
3096 ;; Type 3: Plain link, e.g., http://orgmode.org
3097 ((looking-at org-plain-link-re)
3098 (setq raw-link (org-match-string-no-properties 0)
3099 type (org-match-string-no-properties 1)
3100 link-end (match-end 0)
3101 path (org-match-string-no-properties 2)))
3102 ;; Type 4: Angular link, e.g., <http://orgmode.org>
3103 ((looking-at org-angle-link-re)
3104 (setq raw-link (buffer-substring-no-properties
3105 (match-beginning 1) (match-end 2))
3106 type (org-match-string-no-properties 1)
3107 link-end (match-end 0)
3108 path (org-match-string-no-properties 2))))
3109 ;; In any case, deduce end point after trailing white space from
3110 ;; LINK-END variable.
3111 (setq post-blank (progn (goto-char link-end) (skip-chars-forward " \t"))
3112 end (point))
3113 ;; Extract search option and opening application out of
3114 ;; "file"-type links.
3115 (when (member type org-element-link-type-is-file)
3116 ;; Application.
3117 (cond ((string-match "^file\\+\\(.*\\)$" type)
3118 (setq application (match-string 1 type)))
3119 ((not (string-match "^file" type))
3120 (setq application type)))
3121 ;; Extract search option from PATH.
3122 (when (string-match "::\\(.*\\)$" path)
3123 (setq search-option (match-string 1 path)
3124 path (replace-match "" nil nil path)))
3125 ;; Make sure TYPE always reports "file".
3126 (setq type "file"))
3127 (list 'link
3128 (list :type type
3129 :path path
3130 :raw-link (or raw-link path)
3131 :application application
3132 :search-option search-option
3133 :begin begin
3134 :end end
3135 :contents-begin contents-begin
3136 :contents-end contents-end
3137 :post-blank post-blank)))))
3139 (defun org-element-link-interpreter (link contents)
3140 "Interpret LINK object as Org syntax.
3141 CONTENTS is the contents of the object, or nil."
3142 (let ((type (org-element-property :type link))
3143 (raw-link (org-element-property :raw-link link)))
3144 (if (string= type "radio") raw-link
3145 (format "[[%s]%s]"
3146 raw-link
3147 (if contents (format "[%s]" contents) "")))))
3149 (defun org-element-link-successor ()
3150 "Search for the next link object.
3152 Return value is a cons cell whose CAR is `link' and CDR is
3153 beginning position."
3154 (save-excursion
3155 (let ((link-regexp
3156 (if (not org-target-link-regexp) org-any-link-re
3157 (concat org-any-link-re "\\|" org-target-link-regexp))))
3158 (when (re-search-forward link-regexp nil t)
3159 (cons 'link (match-beginning 0))))))
3161 (defun org-element-plain-link-successor ()
3162 "Search for the next plain link object.
3164 Return value is a cons cell whose CAR is `link' and CDR is
3165 beginning position."
3166 (and (save-excursion (re-search-forward org-plain-link-re nil t))
3167 (cons 'link (match-beginning 0))))
3170 ;;;; Macro
3172 (defun org-element-macro-parser ()
3173 "Parse macro at point.
3175 Return a list whose CAR is `macro' and CDR a plist with `:key',
3176 `:args', `:begin', `:end', `:value' and `:post-blank' as
3177 keywords.
3179 Assume point is at the macro."
3180 (save-excursion
3181 (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
3182 (let ((begin (point))
3183 (key (downcase (org-match-string-no-properties 1)))
3184 (value (org-match-string-no-properties 0))
3185 (post-blank (progn (goto-char (match-end 0))
3186 (skip-chars-forward " \t")))
3187 (end (point))
3188 (args (let ((args (org-match-string-no-properties 3)))
3189 (when args
3190 ;; Do not use `org-split-string' since empty
3191 ;; strings are meaningful here.
3192 (split-string
3193 (replace-regexp-in-string
3194 "\\(\\\\*\\)\\(,\\)"
3195 (lambda (str)
3196 (let ((len (length (match-string 1 str))))
3197 (concat (make-string (/ len 2) ?\\)
3198 (if (zerop (mod len 2)) "\000" ","))))
3199 args nil t)
3200 "\000")))))
3201 (list 'macro
3202 (list :key key
3203 :value value
3204 :args args
3205 :begin begin
3206 :end end
3207 :post-blank post-blank)))))
3209 (defun org-element-macro-interpreter (macro contents)
3210 "Interpret MACRO object as Org syntax.
3211 CONTENTS is nil."
3212 (org-element-property :value macro))
3214 (defun org-element-macro-successor ()
3215 "Search for the next macro object.
3217 Return value is cons cell whose CAR is `macro' and CDR is
3218 beginning position."
3219 (save-excursion
3220 (when (re-search-forward
3221 "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
3222 nil t)
3223 (cons 'macro (match-beginning 0)))))
3226 ;;;; Radio-target
3228 (defun org-element-radio-target-parser ()
3229 "Parse radio target at point.
3231 Return a list whose CAR is `radio-target' and CDR a plist with
3232 `:begin', `:end', `:contents-begin', `:contents-end', `:value'
3233 and `:post-blank' as keywords.
3235 Assume point is at the radio target."
3236 (save-excursion
3237 (looking-at org-radio-target-regexp)
3238 (let ((begin (point))
3239 (contents-begin (match-beginning 1))
3240 (contents-end (match-end 1))
3241 (value (org-match-string-no-properties 1))
3242 (post-blank (progn (goto-char (match-end 0))
3243 (skip-chars-forward " \t")))
3244 (end (point)))
3245 (list 'radio-target
3246 (list :begin begin
3247 :end end
3248 :contents-begin contents-begin
3249 :contents-end contents-end
3250 :post-blank post-blank
3251 :value value)))))
3253 (defun org-element-radio-target-interpreter (target contents)
3254 "Interpret TARGET object as Org syntax.
3255 CONTENTS is the contents of the object."
3256 (concat "<<<" contents ">>>"))
3258 (defun org-element-radio-target-successor ()
3259 "Search for the next radio-target object.
3261 Return value is a cons cell whose CAR is `radio-target' and CDR
3262 is beginning position."
3263 (save-excursion
3264 (when (re-search-forward org-radio-target-regexp nil t)
3265 (cons 'radio-target (match-beginning 0)))))
3268 ;;;; Statistics Cookie
3270 (defun org-element-statistics-cookie-parser ()
3271 "Parse statistics cookie at point.
3273 Return a list whose CAR is `statistics-cookie', and CDR a plist
3274 with `:begin', `:end', `:value' and `:post-blank' keywords.
3276 Assume point is at the beginning of the statistics-cookie."
3277 (save-excursion
3278 (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
3279 (let* ((begin (point))
3280 (value (buffer-substring-no-properties
3281 (match-beginning 0) (match-end 0)))
3282 (post-blank (progn (goto-char (match-end 0))
3283 (skip-chars-forward " \t")))
3284 (end (point)))
3285 (list 'statistics-cookie
3286 (list :begin begin
3287 :end end
3288 :value value
3289 :post-blank post-blank)))))
3291 (defun org-element-statistics-cookie-interpreter (statistics-cookie contents)
3292 "Interpret STATISTICS-COOKIE object as Org syntax.
3293 CONTENTS is nil."
3294 (org-element-property :value statistics-cookie))
3296 (defun org-element-statistics-cookie-successor ()
3297 "Search for the next statistics cookie object.
3299 Return value is a cons cell whose CAR is `statistics-cookie' and
3300 CDR is beginning position."
3301 (save-excursion
3302 (when (re-search-forward "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]" nil t)
3303 (cons 'statistics-cookie (match-beginning 0)))))
3306 ;;;; Strike-Through
3308 (defun org-element-strike-through-parser ()
3309 "Parse strike-through object at point.
3311 Return a list whose CAR is `strike-through' and CDR is a plist
3312 with `:begin', `:end', `:contents-begin' and `:contents-end' and
3313 `:post-blank' keywords.
3315 Assume point is at the first plus sign marker."
3316 (save-excursion
3317 (unless (bolp) (backward-char 1))
3318 (looking-at org-emph-re)
3319 (let ((begin (match-beginning 2))
3320 (contents-begin (match-beginning 4))
3321 (contents-end (match-end 4))
3322 (post-blank (progn (goto-char (match-end 2))
3323 (skip-chars-forward " \t")))
3324 (end (point)))
3325 (list 'strike-through
3326 (list :begin begin
3327 :end end
3328 :contents-begin contents-begin
3329 :contents-end contents-end
3330 :post-blank post-blank)))))
3332 (defun org-element-strike-through-interpreter (strike-through contents)
3333 "Interpret STRIKE-THROUGH object as Org syntax.
3334 CONTENTS is the contents of the object."
3335 (format "+%s+" contents))
3338 ;;;; Subscript
3340 (defun org-element-subscript-parser ()
3341 "Parse subscript at point.
3343 Return a list whose CAR is `subscript' and CDR a plist with
3344 `:begin', `:end', `:contents-begin', `:contents-end',
3345 `:use-brackets-p' and `:post-blank' as keywords.
3347 Assume point is at the underscore."
3348 (save-excursion
3349 (unless (bolp) (backward-char))
3350 (looking-at org-match-substring-regexp)
3351 (let ((bracketsp (match-beginning 4))
3352 (begin (match-beginning 2))
3353 (contents-begin (or (match-beginning 4)
3354 (match-beginning 3)))
3355 (contents-end (or (match-end 4) (match-end 3)))
3356 (post-blank (progn (goto-char (match-end 0))
3357 (skip-chars-forward " \t")))
3358 (end (point)))
3359 (list 'subscript
3360 (list :begin begin
3361 :end end
3362 :use-brackets-p bracketsp
3363 :contents-begin contents-begin
3364 :contents-end contents-end
3365 :post-blank post-blank)))))
3367 (defun org-element-subscript-interpreter (subscript contents)
3368 "Interpret SUBSCRIPT object as Org syntax.
3369 CONTENTS is the contents of the object."
3370 (format
3371 (if (org-element-property :use-brackets-p subscript) "_{%s}" "_%s")
3372 contents))
3374 (defun org-element-sub/superscript-successor ()
3375 "Search for the next sub/superscript object.
3377 Return value is a cons cell whose CAR is either `subscript' or
3378 `superscript' and CDR is beginning position."
3379 (save-excursion
3380 (unless (bolp) (backward-char))
3381 (when (re-search-forward org-match-substring-regexp nil t)
3382 (cons (if (string= (match-string 2) "_") 'subscript 'superscript)
3383 (match-beginning 2)))))
3386 ;;;; Superscript
3388 (defun org-element-superscript-parser ()
3389 "Parse superscript at point.
3391 Return a list whose CAR is `superscript' and CDR a plist with
3392 `:begin', `:end', `:contents-begin', `:contents-end',
3393 `:use-brackets-p' and `:post-blank' as keywords.
3395 Assume point is at the caret."
3396 (save-excursion
3397 (unless (bolp) (backward-char))
3398 (looking-at org-match-substring-regexp)
3399 (let ((bracketsp (match-beginning 4))
3400 (begin (match-beginning 2))
3401 (contents-begin (or (match-beginning 4)
3402 (match-beginning 3)))
3403 (contents-end (or (match-end 4) (match-end 3)))
3404 (post-blank (progn (goto-char (match-end 0))
3405 (skip-chars-forward " \t")))
3406 (end (point)))
3407 (list 'superscript
3408 (list :begin begin
3409 :end end
3410 :use-brackets-p bracketsp
3411 :contents-begin contents-begin
3412 :contents-end contents-end
3413 :post-blank post-blank)))))
3415 (defun org-element-superscript-interpreter (superscript contents)
3416 "Interpret SUPERSCRIPT object as Org syntax.
3417 CONTENTS is the contents of the object."
3418 (format
3419 (if (org-element-property :use-brackets-p superscript) "^{%s}" "^%s")
3420 contents))
3423 ;;;; Table Cell
3425 (defun org-element-table-cell-parser ()
3426 "Parse table cell at point.
3428 Return a list whose CAR is `table-cell' and CDR is a plist
3429 containing `:begin', `:end', `:contents-begin', `:contents-end'
3430 and `:post-blank' keywords."
3431 (looking-at "[ \t]*\\(.*?\\)[ \t]*|")
3432 (let* ((begin (match-beginning 0))
3433 (end (match-end 0))
3434 (contents-begin (match-beginning 1))
3435 (contents-end (match-end 1)))
3436 (list 'table-cell
3437 (list :begin begin
3438 :end end
3439 :contents-begin contents-begin
3440 :contents-end contents-end
3441 :post-blank 0))))
3443 (defun org-element-table-cell-interpreter (table-cell contents)
3444 "Interpret TABLE-CELL element as Org syntax.
3445 CONTENTS is the contents of the cell, or nil."
3446 (concat " " contents " |"))
3448 (defun org-element-table-cell-successor ()
3449 "Search for the next table-cell object.
3451 Return value is a cons cell whose CAR is `table-cell' and CDR is
3452 beginning position."
3453 (when (looking-at "[ \t]*.*?[ \t]*|") (cons 'table-cell (point))))
3456 ;;;; Target
3458 (defun org-element-target-parser ()
3459 "Parse target at point.
3461 Return a list whose CAR is `target' and CDR a plist with
3462 `:begin', `:end', `:value' and `:post-blank' as keywords.
3464 Assume point is at the target."
3465 (save-excursion
3466 (looking-at org-target-regexp)
3467 (let ((begin (point))
3468 (value (org-match-string-no-properties 1))
3469 (post-blank (progn (goto-char (match-end 0))
3470 (skip-chars-forward " \t")))
3471 (end (point)))
3472 (list 'target
3473 (list :begin begin
3474 :end end
3475 :value value
3476 :post-blank post-blank)))))
3478 (defun org-element-target-interpreter (target contents)
3479 "Interpret TARGET object as Org syntax.
3480 CONTENTS is nil."
3481 (format "<<%s>>" (org-element-property :value target)))
3483 (defun org-element-target-successor ()
3484 "Search for the next target object.
3486 Return value is a cons cell whose CAR is `target' and CDR is
3487 beginning position."
3488 (save-excursion
3489 (when (re-search-forward org-target-regexp nil t)
3490 (cons 'target (match-beginning 0)))))
3493 ;;;; Timestamp
3495 (defun org-element-timestamp-parser ()
3496 "Parse time stamp at point.
3498 Return a list whose CAR is `timestamp', and CDR a plist with
3499 `:type', `:raw-value', `:year-start', `:month-start',
3500 `:day-start', `:hour-start', `:minute-start', `:year-end',
3501 `:month-end', `:day-end', `:hour-end', `:minute-end',
3502 `:repeater-type', `:repeater-value', `:repeater-unit',
3503 `:warning-type', `:warning-value', `:warning-unit', `:begin',
3504 `:end', `:value' and `:post-blank' keywords.
3506 Assume point is at the beginning of the timestamp."
3507 (save-excursion
3508 (let* ((begin (point))
3509 (activep (eq (char-after) ?<))
3510 (raw-value
3511 (progn
3512 (looking-at "\\([<[]\\(%%\\)?.*?\\)[]>]\\(?:--\\([<[].*?[]>]\\)\\)?")
3513 (match-string-no-properties 0)))
3514 (date-start (match-string-no-properties 1))
3515 (date-end (match-string 3))
3516 (diaryp (match-beginning 2))
3517 (post-blank (progn (goto-char (match-end 0))
3518 (skip-chars-forward " \t")))
3519 (end (point))
3520 (time-range
3521 (and (not diaryp)
3522 (string-match
3523 "[012]?[0-9]:[0-5][0-9]\\(-\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)"
3524 date-start)
3525 (cons (string-to-number (match-string 2 date-start))
3526 (string-to-number (match-string 3 date-start)))))
3527 (type (cond (diaryp 'diary)
3528 ((and activep (or date-end time-range)) 'active-range)
3529 (activep 'active)
3530 ((or date-end time-range) 'inactive-range)
3531 (t 'inactive)))
3532 (repeater-props
3533 (and (not diaryp)
3534 (string-match "\\([.+]?\\+\\)\\([0-9]+\\)\\([hdwmy]\\)"
3535 raw-value)
3536 (list
3537 :repeater-type
3538 (let ((type (match-string 1 raw-value)))
3539 (cond ((equal "++" type) 'catch-up)
3540 ((equal ".+" type) 'restart)
3541 (t 'cumulate)))
3542 :repeater-value (string-to-number (match-string 2 raw-value))
3543 :repeater-unit
3544 (case (string-to-char (match-string 3 raw-value))
3545 (?h 'hour) (?d 'day) (?w 'week) (?m 'month) (t 'year)))))
3546 (warning-props
3547 (and (not diaryp)
3548 (string-match "\\(-\\)?-\\([0-9]+\\)\\([hdwmy]\\)" raw-value)
3549 (list
3550 :warning-type (if (match-string 1 raw-value) 'first 'all)
3551 :warning-value (string-to-number (match-string 2 raw-value))
3552 :warning-unit
3553 (case (string-to-char (match-string 3 raw-value))
3554 (?h 'hour) (?d 'day) (?w 'week) (?m 'month) (t 'year)))))
3555 year-start month-start day-start hour-start minute-start year-end
3556 month-end day-end hour-end minute-end)
3557 ;; Parse date-start.
3558 (unless diaryp
3559 (let ((date (org-parse-time-string date-start t)))
3560 (setq year-start (nth 5 date)
3561 month-start (nth 4 date)
3562 day-start (nth 3 date)
3563 hour-start (nth 2 date)
3564 minute-start (nth 1 date))))
3565 ;; Compute date-end. It can be provided directly in time-stamp,
3566 ;; or extracted from time range. Otherwise, it defaults to the
3567 ;; same values as date-start.
3568 (unless diaryp
3569 (let ((date (and date-end (org-parse-time-string date-end t))))
3570 (setq year-end (or (nth 5 date) year-start)
3571 month-end (or (nth 4 date) month-start)
3572 day-end (or (nth 3 date) day-start)
3573 hour-end (or (nth 2 date) (car time-range) hour-start)
3574 minute-end (or (nth 1 date) (cdr time-range) minute-start))))
3575 (list 'timestamp
3576 (nconc (list :type type
3577 :raw-value raw-value
3578 :year-start year-start
3579 :month-start month-start
3580 :day-start day-start
3581 :hour-start hour-start
3582 :minute-start minute-start
3583 :year-end year-end
3584 :month-end month-end
3585 :day-end day-end
3586 :hour-end hour-end
3587 :minute-end minute-end
3588 :begin begin
3589 :end end
3590 :post-blank post-blank)
3591 repeater-props
3592 warning-props)))))
3594 (defun org-element-timestamp-interpreter (timestamp contents)
3595 "Interpret TIMESTAMP object as Org syntax.
3596 CONTENTS is nil."
3597 (let* ((repeat-string
3598 (concat
3599 (case (org-element-property :repeater-type timestamp)
3600 (cumulate "+") (catch-up "++") (restart ".+"))
3601 (let ((val (org-element-property :repeater-value timestamp)))
3602 (and val (number-to-string val)))
3603 (case (org-element-property :repeater-unit timestamp)
3604 (hour "h") (day "d") (week "w") (month "m") (year "y"))))
3605 (warning-string
3606 (concat
3607 (case (org-element-property :warning-type timestamp)
3608 (first "--")
3609 (all "-"))
3610 (let ((val (org-element-property :warning-value timestamp)))
3611 (and val (number-to-string val)))
3612 (case (org-element-property :warning-unit timestamp)
3613 (hour "h") (day "d") (week "w") (month "m") (year "y"))))
3614 (build-ts-string
3615 ;; Build an Org timestamp string from TIME. ACTIVEP is
3616 ;; non-nil when time stamp is active. If WITH-TIME-P is
3617 ;; non-nil, add a time part. HOUR-END and MINUTE-END
3618 ;; specify a time range in the timestamp. REPEAT-STRING is
3619 ;; the repeater string, if any.
3620 (lambda (time activep &optional with-time-p hour-end minute-end)
3621 (let ((ts (format-time-string
3622 (funcall (if with-time-p 'cdr 'car)
3623 org-time-stamp-formats)
3624 time)))
3625 (when (and hour-end minute-end)
3626 (string-match "[012]?[0-9]:[0-5][0-9]" ts)
3627 (setq ts
3628 (replace-match
3629 (format "\\&-%02d:%02d" hour-end minute-end)
3630 nil nil ts)))
3631 (unless activep (setq ts (format "[%s]" (substring ts 1 -1))))
3632 (dolist (s (list repeat-string warning-string))
3633 (when (org-string-nw-p s)
3634 (setq ts (concat (substring ts 0 -1)
3637 (substring ts -1)))))
3638 ;; Return value.
3639 ts)))
3640 (type (org-element-property :type timestamp)))
3641 (case type
3642 ((active inactive)
3643 (let* ((minute-start (org-element-property :minute-start timestamp))
3644 (minute-end (org-element-property :minute-end timestamp))
3645 (hour-start (org-element-property :hour-start timestamp))
3646 (hour-end (org-element-property :hour-end timestamp))
3647 (time-range-p (and hour-start hour-end minute-start minute-end
3648 (or (/= hour-start hour-end)
3649 (/= minute-start minute-end)))))
3650 (funcall
3651 build-ts-string
3652 (encode-time 0
3653 (or minute-start 0)
3654 (or hour-start 0)
3655 (org-element-property :day-start timestamp)
3656 (org-element-property :month-start timestamp)
3657 (org-element-property :year-start timestamp))
3658 (eq type 'active)
3659 (and hour-start minute-start)
3660 (and time-range-p hour-end)
3661 (and time-range-p minute-end))))
3662 ((active-range inactive-range)
3663 (let ((minute-start (org-element-property :minute-start timestamp))
3664 (minute-end (org-element-property :minute-end timestamp))
3665 (hour-start (org-element-property :hour-start timestamp))
3666 (hour-end (org-element-property :hour-end timestamp)))
3667 (concat
3668 (funcall
3669 build-ts-string (encode-time
3671 (or minute-start 0)
3672 (or hour-start 0)
3673 (org-element-property :day-start timestamp)
3674 (org-element-property :month-start timestamp)
3675 (org-element-property :year-start timestamp))
3676 (eq type 'active-range)
3677 (and hour-start minute-start))
3678 "--"
3679 (funcall build-ts-string
3680 (encode-time 0
3681 (or minute-end 0)
3682 (or hour-end 0)
3683 (org-element-property :day-end timestamp)
3684 (org-element-property :month-end timestamp)
3685 (org-element-property :year-end timestamp))
3686 (eq type 'active-range)
3687 (and hour-end minute-end))))))))
3689 (defun org-element-timestamp-successor ()
3690 "Search for the next timestamp object.
3692 Return value is a cons cell whose CAR is `timestamp' and CDR is
3693 beginning position."
3694 (save-excursion
3695 (when (re-search-forward
3696 (concat org-ts-regexp-both
3697 "\\|"
3698 "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
3699 "\\|"
3700 "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
3701 nil t)
3702 (cons 'timestamp (match-beginning 0)))))
3705 ;;;; Underline
3707 (defun org-element-underline-parser ()
3708 "Parse underline object at point.
3710 Return a list whose CAR is `underline' and CDR is a plist with
3711 `:begin', `:end', `:contents-begin' and `:contents-end' and
3712 `:post-blank' keywords.
3714 Assume point is at the first underscore marker."
3715 (save-excursion
3716 (unless (bolp) (backward-char 1))
3717 (looking-at org-emph-re)
3718 (let ((begin (match-beginning 2))
3719 (contents-begin (match-beginning 4))
3720 (contents-end (match-end 4))
3721 (post-blank (progn (goto-char (match-end 2))
3722 (skip-chars-forward " \t")))
3723 (end (point)))
3724 (list 'underline
3725 (list :begin begin
3726 :end end
3727 :contents-begin contents-begin
3728 :contents-end contents-end
3729 :post-blank post-blank)))))
3731 (defun org-element-underline-interpreter (underline contents)
3732 "Interpret UNDERLINE object as Org syntax.
3733 CONTENTS is the contents of the object."
3734 (format "_%s_" contents))
3737 ;;;; Verbatim
3739 (defun org-element-verbatim-parser ()
3740 "Parse verbatim object at point.
3742 Return a list whose CAR is `verbatim' and CDR is a plist with
3743 `:value', `:begin', `:end' and `:post-blank' keywords.
3745 Assume point is at the first equal sign marker."
3746 (save-excursion
3747 (unless (bolp) (backward-char 1))
3748 (looking-at org-emph-re)
3749 (let ((begin (match-beginning 2))
3750 (value (org-match-string-no-properties 4))
3751 (post-blank (progn (goto-char (match-end 2))
3752 (skip-chars-forward " \t")))
3753 (end (point)))
3754 (list 'verbatim
3755 (list :value value
3756 :begin begin
3757 :end end
3758 :post-blank post-blank)))))
3760 (defun org-element-verbatim-interpreter (verbatim contents)
3761 "Interpret VERBATIM object as Org syntax.
3762 CONTENTS is nil."
3763 (format "=%s=" (org-element-property :value verbatim)))
3767 ;;; Parsing Element Starting At Point
3769 ;; `org-element--current-element' is the core function of this section.
3770 ;; It returns the Lisp representation of the element starting at
3771 ;; point.
3773 ;; `org-element--current-element' makes use of special modes. They
3774 ;; are activated for fixed element chaining (e.g., `plain-list' >
3775 ;; `item') or fixed conditional element chaining (e.g., `headline' >
3776 ;; `section'). Special modes are: `first-section', `item',
3777 ;; `node-property', `section' and `table-row'.
3779 (defun org-element--current-element
3780 (limit &optional granularity special structure)
3781 "Parse the element starting at point.
3783 Return value is a list like (TYPE PROPS) where TYPE is the type
3784 of the element and PROPS a plist of properties associated to the
3785 element.
3787 Possible types are defined in `org-element-all-elements'.
3789 LIMIT bounds the search.
3791 Optional argument GRANULARITY determines the depth of the
3792 recursion. Allowed values are `headline', `greater-element',
3793 `element', `object' or nil. When it is broader than `object' (or
3794 nil), secondary values will not be parsed, since they only
3795 contain objects.
3797 Optional argument SPECIAL, when non-nil, can be either
3798 `first-section', `item', `node-property', `section', and
3799 `table-row'.
3801 If STRUCTURE isn't provided but SPECIAL is set to `item', it will
3802 be computed.
3804 This function assumes point is always at the beginning of the
3805 element it has to parse."
3806 (save-excursion
3807 (let ((case-fold-search t)
3808 ;; Determine if parsing depth allows for secondary strings
3809 ;; parsing. It only applies to elements referenced in
3810 ;; `org-element-secondary-value-alist'.
3811 (raw-secondary-p (and granularity (not (eq granularity 'object)))))
3812 (cond
3813 ;; Item.
3814 ((eq special 'item)
3815 (org-element-item-parser limit structure raw-secondary-p))
3816 ;; Table Row.
3817 ((eq special 'table-row) (org-element-table-row-parser limit))
3818 ;; Node Property.
3819 ((eq special 'node-property) (org-element-node-property-parser limit))
3820 ;; Headline.
3821 ((org-with-limited-levels (org-at-heading-p))
3822 (org-element-headline-parser limit raw-secondary-p))
3823 ;; Sections (must be checked after headline).
3824 ((eq special 'section) (org-element-section-parser limit))
3825 ((eq special 'first-section)
3826 (org-element-section-parser
3827 (or (save-excursion (org-with-limited-levels (outline-next-heading)))
3828 limit)))
3829 ;; When not at bol, point is at the beginning of an item or
3830 ;; a footnote definition: next item is always a paragraph.
3831 ((not (bolp)) (org-element-paragraph-parser limit (list (point))))
3832 ;; Planning and Clock.
3833 ((looking-at org-planning-or-clock-line-re)
3834 (if (equal (match-string 1) org-clock-string)
3835 (org-element-clock-parser limit)
3836 (org-element-planning-parser limit)))
3837 ;; Inlinetask.
3838 ((org-at-heading-p)
3839 (org-element-inlinetask-parser limit raw-secondary-p))
3840 ;; From there, elements can have affiliated keywords.
3841 (t (let ((affiliated (org-element--collect-affiliated-keywords limit)))
3842 (cond
3843 ;; Jumping over affiliated keywords put point off-limits.
3844 ;; Parse them as regular keywords.
3845 ((and (cdr affiliated) (>= (point) limit))
3846 (goto-char (car affiliated))
3847 (org-element-keyword-parser limit nil))
3848 ;; LaTeX Environment.
3849 ((looking-at
3850 "[ \t]*\\\\begin{[A-Za-z0-9*]+}\\(\\[.*?\\]\\|{.*?}\\)*[ \t]*$")
3851 (org-element-latex-environment-parser limit affiliated))
3852 ;; Drawer and Property Drawer.
3853 ((looking-at org-drawer-regexp)
3854 (if (equal (match-string 1) "PROPERTIES")
3855 (org-element-property-drawer-parser limit affiliated)
3856 (org-element-drawer-parser limit affiliated)))
3857 ;; Fixed Width
3858 ((looking-at "[ \t]*:\\( \\|$\\)")
3859 (org-element-fixed-width-parser limit affiliated))
3860 ;; Inline Comments, Blocks, Babel Calls, Dynamic Blocks and
3861 ;; Keywords.
3862 ((looking-at "[ \t]*#")
3863 (goto-char (match-end 0))
3864 (cond ((looking-at "\\(?: \\|$\\)")
3865 (beginning-of-line)
3866 (org-element-comment-parser limit affiliated))
3867 ((looking-at "\\+BEGIN_\\(\\S-+\\)")
3868 (beginning-of-line)
3869 (let ((parser (assoc (upcase (match-string 1))
3870 org-element-block-name-alist)))
3871 (if parser (funcall (cdr parser) limit affiliated)
3872 (org-element-special-block-parser limit affiliated))))
3873 ((looking-at "\\+CALL:")
3874 (beginning-of-line)
3875 (org-element-babel-call-parser limit affiliated))
3876 ((looking-at "\\+BEGIN:? ")
3877 (beginning-of-line)
3878 (org-element-dynamic-block-parser limit affiliated))
3879 ((looking-at "\\+\\S-+:")
3880 (beginning-of-line)
3881 (org-element-keyword-parser limit affiliated))
3883 (beginning-of-line)
3884 (org-element-paragraph-parser limit affiliated))))
3885 ;; Footnote Definition.
3886 ((looking-at org-footnote-definition-re)
3887 (org-element-footnote-definition-parser limit affiliated))
3888 ;; Horizontal Rule.
3889 ((looking-at "[ \t]*-\\{5,\\}[ \t]*$")
3890 (org-element-horizontal-rule-parser limit affiliated))
3891 ;; Diary Sexp.
3892 ((looking-at "%%(")
3893 (org-element-diary-sexp-parser limit affiliated))
3894 ;; Table.
3895 ((org-at-table-p t) (org-element-table-parser limit affiliated))
3896 ;; List.
3897 ((looking-at (org-item-re))
3898 (org-element-plain-list-parser
3899 limit affiliated
3900 (or structure (org-element--list-struct limit))))
3901 ;; Default element: Paragraph.
3902 (t (org-element-paragraph-parser limit affiliated)))))))))
3905 ;; Most elements can have affiliated keywords. When looking for an
3906 ;; element beginning, we want to move before them, as they belong to
3907 ;; that element, and, in the meantime, collect information they give
3908 ;; into appropriate properties. Hence the following function.
3910 (defun org-element--collect-affiliated-keywords (limit)
3911 "Collect affiliated keywords from point down to LIMIT.
3913 Return a list whose CAR is the position at the first of them and
3914 CDR a plist of keywords and values and move point to the
3915 beginning of the first line after them.
3917 As a special case, if element doesn't start at the beginning of
3918 the line (e.g., a paragraph starting an item), CAR is current
3919 position of point and CDR is nil."
3920 (if (not (bolp)) (list (point))
3921 (let ((case-fold-search t)
3922 (origin (point))
3923 ;; RESTRICT is the list of objects allowed in parsed
3924 ;; keywords value.
3925 (restrict (org-element-restriction 'keyword))
3926 output)
3927 (while (and (< (point) limit) (looking-at org-element--affiliated-re))
3928 (let* ((raw-kwd (upcase (match-string 1)))
3929 ;; Apply translation to RAW-KWD. From there, KWD is
3930 ;; the official keyword.
3931 (kwd (or (cdr (assoc raw-kwd
3932 org-element-keyword-translation-alist))
3933 raw-kwd))
3934 ;; Find main value for any keyword.
3935 (value
3936 (save-match-data
3937 (org-trim
3938 (buffer-substring-no-properties
3939 (match-end 0) (point-at-eol)))))
3940 ;; PARSEDP is non-nil when keyword should have its
3941 ;; value parsed.
3942 (parsedp (member kwd org-element-parsed-keywords))
3943 ;; If KWD is a dual keyword, find its secondary
3944 ;; value. Maybe parse it.
3945 (dualp (member kwd org-element-dual-keywords))
3946 (dual-value
3947 (and dualp
3948 (let ((sec (org-match-string-no-properties 2)))
3949 (if (or (not sec) (not parsedp)) sec
3950 (org-element-parse-secondary-string sec restrict)))))
3951 ;; Attribute a property name to KWD.
3952 (kwd-sym (and kwd (intern (concat ":" (downcase kwd))))))
3953 ;; Now set final shape for VALUE.
3954 (when parsedp
3955 (setq value (org-element-parse-secondary-string value restrict)))
3956 (when dualp
3957 (setq value (and (or value dual-value) (cons value dual-value))))
3958 (when (or (member kwd org-element-multiple-keywords)
3959 ;; Attributes can always appear on multiple lines.
3960 (string-match "^ATTR_" kwd))
3961 (setq value (cons value (plist-get output kwd-sym))))
3962 ;; Eventually store the new value in OUTPUT.
3963 (setq output (plist-put output kwd-sym value))
3964 ;; Move to next keyword.
3965 (forward-line)))
3966 ;; If affiliated keywords are orphaned: move back to first one.
3967 ;; They will be parsed as a paragraph.
3968 (when (looking-at "[ \t]*$") (goto-char origin) (setq output nil))
3969 ;; Return value.
3970 (cons origin output))))
3974 ;;; The Org Parser
3976 ;; The two major functions here are `org-element-parse-buffer', which
3977 ;; parses Org syntax inside the current buffer, taking into account
3978 ;; region, narrowing, or even visibility if specified, and
3979 ;; `org-element-parse-secondary-string', which parses objects within
3980 ;; a given string.
3982 ;; The (almost) almighty `org-element-map' allows to apply a function
3983 ;; on elements or objects matching some type, and accumulate the
3984 ;; resulting values. In an export situation, it also skips unneeded
3985 ;; parts of the parse tree.
3987 (defun org-element-parse-buffer (&optional granularity visible-only)
3988 "Recursively parse the buffer and return structure.
3989 If narrowing is in effect, only parse the visible part of the
3990 buffer.
3992 Optional argument GRANULARITY determines the depth of the
3993 recursion. It can be set to the following symbols:
3995 `headline' Only parse headlines.
3996 `greater-element' Don't recurse into greater elements excepted
3997 headlines and sections. Thus, elements
3998 parsed are the top-level ones.
3999 `element' Parse everything but objects and plain text.
4000 `object' Parse the complete buffer (default).
4002 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
4003 elements.
4005 An element or an objects is represented as a list with the
4006 pattern (TYPE PROPERTIES CONTENTS), where :
4008 TYPE is a symbol describing the element or object. See
4009 `org-element-all-elements' and `org-element-all-objects' for an
4010 exhaustive list of such symbols. One can retrieve it with
4011 `org-element-type' function.
4013 PROPERTIES is the list of attributes attached to the element or
4014 object, as a plist. Although most of them are specific to the
4015 element or object type, all types share `:begin', `:end',
4016 `:post-blank' and `:parent' properties, which respectively
4017 refer to buffer position where the element or object starts,
4018 ends, the number of white spaces or blank lines after it, and
4019 the element or object containing it. Properties values can be
4020 obtained by using `org-element-property' function.
4022 CONTENTS is a list of elements, objects or raw strings
4023 contained in the current element or object, when applicable.
4024 One can access them with `org-element-contents' function.
4026 The Org buffer has `org-data' as type and nil as properties.
4027 `org-element-map' function can be used to find specific elements
4028 or objects within the parse tree.
4030 This function assumes that current major mode is `org-mode'."
4031 (save-excursion
4032 (goto-char (point-min))
4033 (org-skip-whitespace)
4034 (org-element--parse-elements
4035 (point-at-bol) (point-max)
4036 ;; Start in `first-section' mode so text before the first
4037 ;; headline belongs to a section.
4038 'first-section nil granularity visible-only (list 'org-data nil))))
4040 (defun org-element-parse-secondary-string (string restriction &optional parent)
4041 "Recursively parse objects in STRING and return structure.
4043 RESTRICTION is a symbol limiting the object types that will be
4044 looked after.
4046 Optional argument PARENT, when non-nil, is the element or object
4047 containing the secondary string. It is used to set correctly
4048 `:parent' property within the string."
4049 ;; Copy buffer-local variables listed in
4050 ;; `org-element-object-variables' into temporary buffer. This is
4051 ;; required since object parsing is dependent on these variables.
4052 (let ((pairs (delq nil (mapcar (lambda (var)
4053 (when (boundp var)
4054 (cons var (symbol-value var))))
4055 org-element-object-variables))))
4056 (with-temp-buffer
4057 (mapc (lambda (pair) (org-set-local (car pair) (cdr pair))) pairs)
4058 (insert string)
4059 (let ((secondary (org-element--parse-objects
4060 (point-min) (point-max) nil restriction)))
4061 (when parent
4062 (mapc (lambda (obj) (org-element-put-property obj :parent parent))
4063 secondary))
4064 secondary))))
4066 (defun org-element-map
4067 (data types fun &optional info first-match no-recursion with-affiliated)
4068 "Map a function on selected elements or objects.
4070 DATA is a parse tree, an element, an object, a string, or a list
4071 of such constructs. TYPES is a symbol or list of symbols of
4072 elements or objects types (see `org-element-all-elements' and
4073 `org-element-all-objects' for a complete list of types). FUN is
4074 the function called on the matching element or object. It has to
4075 accept one argument: the element or object itself.
4077 When optional argument INFO is non-nil, it should be a plist
4078 holding export options. In that case, parts of the parse tree
4079 not exportable according to that property list will be skipped.
4081 When optional argument FIRST-MATCH is non-nil, stop at the first
4082 match for which FUN doesn't return nil, and return that value.
4084 Optional argument NO-RECURSION is a symbol or a list of symbols
4085 representing elements or objects types. `org-element-map' won't
4086 enter any recursive element or object whose type belongs to that
4087 list. Though, FUN can still be applied on them.
4089 When optional argument WITH-AFFILIATED is non-nil, FUN will also
4090 apply to matching objects within parsed affiliated keywords (see
4091 `org-element-parsed-keywords').
4093 Nil values returned from FUN do not appear in the results.
4096 Examples:
4097 ---------
4099 Assuming TREE is a variable containing an Org buffer parse tree,
4100 the following example will return a flat list of all `src-block'
4101 and `example-block' elements in it:
4103 \(org-element-map tree '(example-block src-block) 'identity)
4105 The following snippet will find the first headline with a level
4106 of 1 and a \"phone\" tag, and will return its beginning position:
4108 \(org-element-map tree 'headline
4109 \(lambda (hl)
4110 \(and (= (org-element-property :level hl) 1)
4111 \(member \"phone\" (org-element-property :tags hl))
4112 \(org-element-property :begin hl)))
4113 nil t)
4115 The next example will return a flat list of all `plain-list' type
4116 elements in TREE that are not a sub-list themselves:
4118 \(org-element-map tree 'plain-list 'identity nil nil 'plain-list)
4120 Eventually, this example will return a flat list of all `bold'
4121 type objects containing a `latex-snippet' type object, even
4122 looking into captions:
4124 \(org-element-map tree 'bold
4125 \(lambda (b)
4126 \(and (org-element-map b 'latex-snippet 'identity nil t) b))
4127 nil nil nil t)"
4128 ;; Ensure TYPES and NO-RECURSION are a list, even of one element.
4129 (unless (listp types) (setq types (list types)))
4130 (unless (listp no-recursion) (setq no-recursion (list no-recursion)))
4131 ;; Recursion depth is determined by --CATEGORY.
4132 (let* ((--category
4133 (catch 'found
4134 (let ((category 'greater-elements))
4135 (mapc (lambda (type)
4136 (cond ((or (memq type org-element-all-objects)
4137 (eq type 'plain-text))
4138 ;; If one object is found, the function
4139 ;; has to recurse into every object.
4140 (throw 'found 'objects))
4141 ((not (memq type org-element-greater-elements))
4142 ;; If one regular element is found, the
4143 ;; function has to recurse, at least,
4144 ;; into every element it encounters.
4145 (and (not (eq category 'elements))
4146 (setq category 'elements)))))
4147 types)
4148 category)))
4149 ;; Compute properties for affiliated keywords if necessary.
4150 (--affiliated-alist
4151 (and with-affiliated
4152 (mapcar (lambda (kwd)
4153 (cons kwd (intern (concat ":" (downcase kwd)))))
4154 org-element-affiliated-keywords)))
4155 --acc
4156 --walk-tree
4157 (--walk-tree
4158 (function
4159 (lambda (--data)
4160 ;; Recursively walk DATA. INFO, if non-nil, is a plist
4161 ;; holding contextual information.
4162 (let ((--type (org-element-type --data)))
4163 (cond
4164 ((not --data))
4165 ;; Ignored element in an export context.
4166 ((and info (memq --data (plist-get info :ignore-list))))
4167 ;; List of elements or objects.
4168 ((not --type) (mapc --walk-tree --data))
4169 ;; Unconditionally enter parse trees.
4170 ((eq --type 'org-data)
4171 (mapc --walk-tree (org-element-contents --data)))
4173 ;; Check if TYPE is matching among TYPES. If so,
4174 ;; apply FUN to --DATA and accumulate return value
4175 ;; into --ACC (or exit if FIRST-MATCH is non-nil).
4176 (when (memq --type types)
4177 (let ((result (funcall fun --data)))
4178 (cond ((not result))
4179 (first-match (throw '--map-first-match result))
4180 (t (push result --acc)))))
4181 ;; If --DATA has a secondary string that can contain
4182 ;; objects with their type among TYPES, look into it.
4183 (when (and (eq --category 'objects) (not (stringp --data)))
4184 (let ((sec-prop
4185 (assq --type org-element-secondary-value-alist)))
4186 (when sec-prop
4187 (funcall --walk-tree
4188 (org-element-property (cdr sec-prop) --data)))))
4189 ;; If --DATA has any affiliated keywords and
4190 ;; WITH-AFFILIATED is non-nil, look for objects in
4191 ;; them.
4192 (when (and with-affiliated
4193 (eq --category 'objects)
4194 (memq --type org-element-all-elements))
4195 (mapc (lambda (kwd-pair)
4196 (let ((kwd (car kwd-pair))
4197 (value (org-element-property
4198 (cdr kwd-pair) --data)))
4199 ;; Pay attention to the type of value.
4200 ;; Preserve order for multiple keywords.
4201 (cond
4202 ((not value))
4203 ((and (member kwd org-element-multiple-keywords)
4204 (member kwd org-element-dual-keywords))
4205 (mapc (lambda (line)
4206 (funcall --walk-tree (cdr line))
4207 (funcall --walk-tree (car line)))
4208 (reverse value)))
4209 ((member kwd org-element-multiple-keywords)
4210 (mapc (lambda (line) (funcall --walk-tree line))
4211 (reverse value)))
4212 ((member kwd org-element-dual-keywords)
4213 (funcall --walk-tree (cdr value))
4214 (funcall --walk-tree (car value)))
4215 (t (funcall --walk-tree value)))))
4216 --affiliated-alist))
4217 ;; Determine if a recursion into --DATA is possible.
4218 (cond
4219 ;; --TYPE is explicitly removed from recursion.
4220 ((memq --type no-recursion))
4221 ;; --DATA has no contents.
4222 ((not (org-element-contents --data)))
4223 ;; Looking for greater elements but --DATA is simply
4224 ;; an element or an object.
4225 ((and (eq --category 'greater-elements)
4226 (not (memq --type org-element-greater-elements))))
4227 ;; Looking for elements but --DATA is an object.
4228 ((and (eq --category 'elements)
4229 (memq --type org-element-all-objects)))
4230 ;; In any other case, map contents.
4231 (t (mapc --walk-tree (org-element-contents --data)))))))))))
4232 (catch '--map-first-match
4233 (funcall --walk-tree data)
4234 ;; Return value in a proper order.
4235 (nreverse --acc))))
4236 (put 'org-element-map 'lisp-indent-function 2)
4238 ;; The following functions are internal parts of the parser.
4240 ;; The first one, `org-element--parse-elements' acts at the element's
4241 ;; level.
4243 ;; The second one, `org-element--parse-objects' applies on all objects
4244 ;; of a paragraph or a secondary string. It uses
4245 ;; `org-element--get-next-object-candidates' to optimize the search of
4246 ;; the next object in the buffer.
4248 ;; More precisely, that function looks for every allowed object type
4249 ;; first. Then, it discards failed searches, keeps further matches,
4250 ;; and searches again types matched behind point, for subsequent
4251 ;; calls. Thus, searching for a given type fails only once, and every
4252 ;; object is searched only once at top level (but sometimes more for
4253 ;; nested types).
4255 (defun org-element--parse-elements
4256 (beg end special structure granularity visible-only acc)
4257 "Parse elements between BEG and END positions.
4259 SPECIAL prioritize some elements over the others. It can be set
4260 to `first-section', `section' `item' or `table-row'.
4262 When value is `item', STRUCTURE will be used as the current list
4263 structure.
4265 GRANULARITY determines the depth of the recursion. See
4266 `org-element-parse-buffer' for more information.
4268 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
4269 elements.
4271 Elements are accumulated into ACC."
4272 (save-excursion
4273 (goto-char beg)
4274 ;; Visible only: skip invisible parts at the beginning of the
4275 ;; element.
4276 (when (and visible-only (org-invisible-p2))
4277 (goto-char (min (1+ (org-find-visible)) end)))
4278 ;; When parsing only headlines, skip any text before first one.
4279 (when (and (eq granularity 'headline) (not (org-at-heading-p)))
4280 (org-with-limited-levels (outline-next-heading)))
4281 ;; Main loop start.
4282 (while (< (point) end)
4283 ;; Find current element's type and parse it accordingly to
4284 ;; its category.
4285 (let* ((element (org-element--current-element
4286 end granularity special structure))
4287 (type (org-element-type element))
4288 (cbeg (org-element-property :contents-begin element)))
4289 (goto-char (org-element-property :end element))
4290 ;; Visible only: skip invisible parts between siblings.
4291 (when (and visible-only (org-invisible-p2))
4292 (goto-char (min (1+ (org-find-visible)) end)))
4293 ;; Fill ELEMENT contents by side-effect.
4294 (cond
4295 ;; If element has no contents, don't modify it.
4296 ((not cbeg))
4297 ;; Greater element: parse it between `contents-begin' and
4298 ;; `contents-end'. Make sure GRANULARITY allows the
4299 ;; recursion, or ELEMENT is a headline, in which case going
4300 ;; inside is mandatory, in order to get sub-level headings.
4301 ((and (memq type org-element-greater-elements)
4302 (or (memq granularity '(element object nil))
4303 (and (eq granularity 'greater-element)
4304 (eq type 'section))
4305 (eq type 'headline)))
4306 (org-element--parse-elements
4307 cbeg (org-element-property :contents-end element)
4308 ;; Possibly switch to a special mode.
4309 (case type
4310 (headline 'section)
4311 (plain-list 'item)
4312 (property-drawer 'node-property)
4313 (table 'table-row))
4314 (and (memq type '(item plain-list))
4315 (org-element-property :structure element))
4316 granularity visible-only element))
4317 ;; ELEMENT has contents. Parse objects inside, if
4318 ;; GRANULARITY allows it.
4319 ((memq granularity '(object nil))
4320 (org-element--parse-objects
4321 cbeg (org-element-property :contents-end element) element
4322 (org-element-restriction type))))
4323 (org-element-adopt-elements acc element)))
4324 ;; Return result.
4325 acc))
4327 (defun org-element--parse-objects (beg end acc restriction)
4328 "Parse objects between BEG and END and return recursive structure.
4330 Objects are accumulated in ACC.
4332 RESTRICTION is a list of object successors which are allowed in
4333 the current object."
4334 (let ((candidates 'initial))
4335 (save-excursion
4336 (save-restriction
4337 (narrow-to-region beg end)
4338 (goto-char (point-min))
4339 (while (and (not (eobp))
4340 (setq candidates
4341 (org-element--get-next-object-candidates
4342 restriction candidates)))
4343 (let ((next-object
4344 (let ((pos (apply 'min (mapcar 'cdr candidates))))
4345 (save-excursion
4346 (goto-char pos)
4347 (funcall (intern (format "org-element-%s-parser"
4348 (car (rassq pos candidates)))))))))
4349 ;; 1. Text before any object. Untabify it.
4350 (let ((obj-beg (org-element-property :begin next-object)))
4351 (unless (= (point) obj-beg)
4352 (setq acc
4353 (org-element-adopt-elements
4355 (replace-regexp-in-string
4356 "\t" (make-string tab-width ? )
4357 (buffer-substring-no-properties (point) obj-beg))))))
4358 ;; 2. Object...
4359 (let ((obj-end (org-element-property :end next-object))
4360 (cont-beg (org-element-property :contents-begin next-object)))
4361 ;; Fill contents of NEXT-OBJECT by side-effect, if it has
4362 ;; a recursive type.
4363 (when (and cont-beg
4364 (memq (car next-object) org-element-recursive-objects))
4365 (org-element--parse-objects
4366 cont-beg (org-element-property :contents-end next-object)
4367 next-object (org-element-restriction next-object)))
4368 (setq acc (org-element-adopt-elements acc next-object))
4369 (goto-char obj-end))))
4370 ;; 3. Text after last object. Untabify it.
4371 (unless (eobp)
4372 (setq acc
4373 (org-element-adopt-elements
4375 (replace-regexp-in-string
4376 "\t" (make-string tab-width ? )
4377 (buffer-substring-no-properties (point) end)))))
4378 ;; Result.
4379 acc))))
4381 (defun org-element--get-next-object-candidates (restriction objects)
4382 "Return an alist of candidates for the next object.
4384 RESTRICTION is a list of object types, as symbols. Only
4385 candidates with such types are looked after.
4387 OBJECTS is the previous candidates alist. If it is set to
4388 `initial', no search has been done before, and all symbols in
4389 RESTRICTION should be looked after.
4391 Return value is an alist whose CAR is the object type and CDR its
4392 beginning position."
4393 (delq
4395 (if (eq objects 'initial)
4396 ;; When searching for the first time, look for every successor
4397 ;; allowed in RESTRICTION.
4398 (mapcar
4399 (lambda (res)
4400 (funcall (intern (format "org-element-%s-successor" res))))
4401 restriction)
4402 ;; Focus on objects returned during last search. Keep those
4403 ;; still after point. Search again objects before it.
4404 (mapcar
4405 (lambda (obj)
4406 (if (>= (cdr obj) (point)) obj
4407 (let* ((type (car obj))
4408 (succ (or (cdr (assq type org-element-object-successor-alist))
4409 type)))
4410 (and succ
4411 (funcall (intern (format "org-element-%s-successor" succ)))))))
4412 objects))))
4416 ;;; Towards A Bijective Process
4418 ;; The parse tree obtained with `org-element-parse-buffer' is really
4419 ;; a snapshot of the corresponding Org buffer. Therefore, it can be
4420 ;; interpreted and expanded into a string with canonical Org syntax.
4421 ;; Hence `org-element-interpret-data'.
4423 ;; The function relies internally on
4424 ;; `org-element--interpret-affiliated-keywords'.
4426 ;;;###autoload
4427 (defun org-element-interpret-data (data &optional pseudo-objects)
4428 "Interpret DATA as Org syntax.
4430 DATA is a parse tree, an element, an object or a secondary string
4431 to interpret.
4433 Optional argument PSEUDO-OBJECTS is a list of symbols defining
4434 new types that should be treated as objects. An unknown type not
4435 belonging to this list is seen as a pseudo-element instead. Both
4436 pseudo-objects and pseudo-elements are transparent entities, i.e.
4437 only their contents are interpreted.
4439 Return Org syntax as a string."
4440 (org-element--interpret-data-1 data nil pseudo-objects))
4442 (defun org-element--interpret-data-1 (data parent pseudo-objects)
4443 "Interpret DATA as Org syntax.
4445 DATA is a parse tree, an element, an object or a secondary string
4446 to interpret. PARENT is used for recursive calls. It contains
4447 the element or object containing data, or nil. PSEUDO-OBJECTS
4448 are list of symbols defining new element or object types.
4449 Unknown types that don't belong to this list are treated as
4450 pseudo-elements instead.
4452 Return Org syntax as a string."
4453 (let* ((type (org-element-type data))
4454 ;; Find interpreter for current object or element. If it
4455 ;; doesn't exist (e.g. this is a pseudo object or element),
4456 ;; return contents, if any.
4457 (interpret
4458 (let ((fun (intern (format "org-element-%s-interpreter" type))))
4459 (if (fboundp fun) fun (lambda (data contents) contents))))
4460 (results
4461 (cond
4462 ;; Secondary string.
4463 ((not type)
4464 (mapconcat
4465 (lambda (obj)
4466 (org-element--interpret-data-1 obj parent pseudo-objects))
4467 data ""))
4468 ;; Full Org document.
4469 ((eq type 'org-data)
4470 (mapconcat
4471 (lambda (obj)
4472 (org-element--interpret-data-1 obj parent pseudo-objects))
4473 (org-element-contents data) ""))
4474 ;; Plain text: return it.
4475 ((stringp data) data)
4476 ;; Element or object without contents.
4477 ((not (org-element-contents data)) (funcall interpret data nil))
4478 ;; Element or object with contents.
4480 (funcall interpret data
4481 ;; Recursively interpret contents.
4482 (mapconcat
4483 (lambda (obj)
4484 (org-element--interpret-data-1 obj data pseudo-objects))
4485 (org-element-contents
4486 (if (not (memq type '(paragraph verse-block)))
4487 data
4488 ;; Fix indentation of elements containing
4489 ;; objects. We ignore `table-row' elements
4490 ;; as they are one line long anyway.
4491 (org-element-normalize-contents
4492 data
4493 ;; When normalizing first paragraph of an
4494 ;; item or a footnote-definition, ignore
4495 ;; first line's indentation.
4496 (and (eq type 'paragraph)
4497 (equal data (car (org-element-contents parent)))
4498 (memq (org-element-type parent)
4499 '(footnote-definition item))))))
4500 ""))))))
4501 (if (memq type '(org-data plain-text nil)) results
4502 ;; Build white spaces. If no `:post-blank' property is
4503 ;; specified, assume its value is 0.
4504 (let ((post-blank (or (org-element-property :post-blank data) 0)))
4505 (if (or (memq type org-element-all-objects)
4506 (memq type pseudo-objects))
4507 (concat results (make-string post-blank ?\s))
4508 (concat
4509 (org-element--interpret-affiliated-keywords data)
4510 (org-element-normalize-string results)
4511 (make-string post-blank ?\n)))))))
4513 (defun org-element--interpret-affiliated-keywords (element)
4514 "Return ELEMENT's affiliated keywords as Org syntax.
4515 If there is no affiliated keyword, return the empty string."
4516 (let ((keyword-to-org
4517 (function
4518 (lambda (key value)
4519 (let (dual)
4520 (when (member key org-element-dual-keywords)
4521 (setq dual (cdr value) value (car value)))
4522 (concat "#+" key
4523 (and dual
4524 (format "[%s]" (org-element-interpret-data dual)))
4525 ": "
4526 (if (member key org-element-parsed-keywords)
4527 (org-element-interpret-data value)
4528 value)
4529 "\n"))))))
4530 (mapconcat
4531 (lambda (prop)
4532 (let ((value (org-element-property prop element))
4533 (keyword (upcase (substring (symbol-name prop) 1))))
4534 (when value
4535 (if (or (member keyword org-element-multiple-keywords)
4536 ;; All attribute keywords can have multiple lines.
4537 (string-match "^ATTR_" keyword))
4538 (mapconcat (lambda (line) (funcall keyword-to-org keyword line))
4539 (reverse value)
4541 (funcall keyword-to-org keyword value)))))
4542 ;; List all ELEMENT's properties matching an attribute line or an
4543 ;; affiliated keyword, but ignore translated keywords since they
4544 ;; cannot belong to the property list.
4545 (loop for prop in (nth 1 element) by 'cddr
4546 when (let ((keyword (upcase (substring (symbol-name prop) 1))))
4547 (or (string-match "^ATTR_" keyword)
4548 (and
4549 (member keyword org-element-affiliated-keywords)
4550 (not (assoc keyword
4551 org-element-keyword-translation-alist)))))
4552 collect prop)
4553 "")))
4555 ;; Because interpretation of the parse tree must return the same
4556 ;; number of blank lines between elements and the same number of white
4557 ;; space after objects, some special care must be given to white
4558 ;; spaces.
4560 ;; The first function, `org-element-normalize-string', ensures any
4561 ;; string different from the empty string will end with a single
4562 ;; newline character.
4564 ;; The second function, `org-element-normalize-contents', removes
4565 ;; global indentation from the contents of the current element.
4567 (defun org-element-normalize-string (s)
4568 "Ensure string S ends with a single newline character.
4570 If S isn't a string return it unchanged. If S is the empty
4571 string, return it. Otherwise, return a new string with a single
4572 newline character at its end."
4573 (cond
4574 ((not (stringp s)) s)
4575 ((string= "" s) "")
4576 (t (and (string-match "\\(\n[ \t]*\\)*\\'" s)
4577 (replace-match "\n" nil nil s)))))
4579 (defun org-element-normalize-contents (element &optional ignore-first)
4580 "Normalize plain text in ELEMENT's contents.
4582 ELEMENT must only contain plain text and objects.
4584 If optional argument IGNORE-FIRST is non-nil, ignore first line's
4585 indentation to compute maximal common indentation.
4587 Return the normalized element that is element with global
4588 indentation removed from its contents. The function assumes that
4589 indentation is not done with TAB characters."
4590 (let* ((min-ind most-positive-fixnum)
4591 find-min-ind ; For byte-compiler.
4592 (find-min-ind
4593 (function
4594 ;; Return minimal common indentation within BLOB. This is
4595 ;; done by walking recursively BLOB and updating MIN-IND
4596 ;; along the way. FIRST-FLAG is non-nil when the first
4597 ;; string hasn't been seen yet. It is required as this
4598 ;; string is the only one whose indentation doesn't happen
4599 ;; after a newline character.
4600 (lambda (blob first-flag)
4601 (dolist (object (org-element-contents blob))
4602 (when (and first-flag (stringp object))
4603 (setq first-flag nil)
4604 (string-match "\\`\\( *\\)" object)
4605 (let ((len (length (match-string 1 object))))
4606 ;; An indentation of zero means no string will be
4607 ;; modified. Quit the process.
4608 (if (zerop len) (throw 'zero (setq min-ind 0))
4609 (setq min-ind (min len min-ind)))))
4610 (cond
4611 ((stringp object)
4612 (dolist (line (delq "" (cdr (org-split-string object " *\n"))))
4613 (setq min-ind (min (org-get-indentation line) min-ind))))
4614 ((memq (org-element-type object) org-element-recursive-objects)
4615 (funcall find-min-ind object first-flag))))))))
4616 ;; Find minimal indentation in ELEMENT.
4617 (catch 'zero (funcall find-min-ind element (not ignore-first)))
4618 (if (or (zerop min-ind) (= min-ind most-positive-fixnum)) element
4619 ;; Build ELEMENT back, replacing each string with the same
4620 ;; string minus common indentation.
4621 (let* (build ; For byte compiler.
4622 (build
4623 (function
4624 (lambda (blob first-flag)
4625 ;; Return BLOB with all its strings indentation
4626 ;; shortened from MIN-IND white spaces. FIRST-FLAG
4627 ;; is non-nil when the first string hasn't been seen
4628 ;; yet.
4629 (setcdr (cdr blob)
4630 (mapcar
4631 #'(lambda (object)
4632 (when (and first-flag (stringp object))
4633 (setq first-flag nil)
4634 (setq object
4635 (replace-regexp-in-string
4636 (format "\\` \\{%d\\}" min-ind)
4637 "" object)))
4638 (cond
4639 ((stringp object)
4640 (replace-regexp-in-string
4641 (format "\n \\{%d\\}" min-ind) "\n" object))
4642 ((memq (org-element-type object)
4643 org-element-recursive-objects)
4644 (funcall build object first-flag))
4645 (t object)))
4646 (org-element-contents blob)))
4647 blob))))
4648 (funcall build element (not ignore-first))))))
4652 ;;; Cache
4654 ;; Implement a caching mechanism for `org-element-at-point' and
4655 ;; `org-element-context', which see.
4657 ;; A single public function is provided: `org-element-cache-reset'.
4659 ;; Cache is enabled by default, but can be disabled globally with
4660 ;; `org-element-use-cache'. `org-element-cache-sync-idle-time',
4661 ;; org-element-cache-sync-duration' and `org-element-cache-sync-break'
4662 ;; can be tweaked to control caching behaviour.
4664 ;; Internally, parsed elements are stored in an AVL tree,
4665 ;; `org-element--cache'. This tree is updated lazily: whenever
4666 ;; a change happens to the buffer, a synchronization request is
4667 ;; registered in `org-element--cache-sync-requests' (see
4668 ;; `org-element--cache-submit-request'). During idle time, requests
4669 ;; are processed by `org-element--cache-sync'. Synchronization also
4670 ;; happens when an element is required from the cache. In this case,
4671 ;; the process stops as soon as the needed element is up-to-date.
4673 ;; A synchronization request can only apply on a synchronized part of
4674 ;; the cache. Therefore, the cache is updated at least to the
4675 ;; location where the new request applies. Thus, requests are ordered
4676 ;; from left to right and all elements starting before the first
4677 ;; request are correct. This property is used by functions like
4678 ;; `org-element--cache-find' to retrieve elements in the part of the
4679 ;; cache that can be trusted.
4681 ;; A request applies to every element, starting from its original
4682 ;; location (or key, see below). When a request is processed, it
4683 ;; moves forward and may collide the next one. In this case, both
4684 ;; requests are merged into a new one that starts from that element.
4685 ;; As a consequence, the whole synchronization complexity does not
4686 ;; depend on the number of pending requests, but on the number of
4687 ;; elements the very first request will be applied on.
4689 ;; Elements cannot be accessed through their beginning position, which
4690 ;; may or may not be up-to-date. Instead, each element in the tree is
4691 ;; associated to a key, obtained with `org-element--cache-key'. This
4692 ;; mechanism is robust enough to preserve total order among elements
4693 ;; even when the tree is only partially synchronized.
4695 ;; Objects contained in an element are stored in a hash table,
4696 ;; `org-element--cache-objects'.
4699 (defvar org-element-use-cache t
4700 "Non nil when Org parser should cache its results.
4701 This is mostly for debugging purpose.")
4703 (defvar org-element-cache-sync-idle-time 0.4
4704 "Length, in seconds, of idle time before syncing cache.")
4706 (defvar org-element-cache-sync-duration (seconds-to-time 0.04)
4707 "Maximum duration, as a time value, for a cache synchronization.
4708 If the synchronization is not over after this delay, the process
4709 pauses and resumes after `org-element-cache-sync-break'
4710 seconds.")
4712 (defvar org-element-cache-sync-break (seconds-to-time 0.2)
4713 "Duration, as a time value, of the pause between synchronizations.
4714 See `org-element-cache-sync-duration' for more information.")
4717 ;;;; Data Structure
4719 (defvar org-element--cache nil
4720 "AVL tree used to cache elements.
4721 Each node of the tree contains an element. Comparison is done
4722 with `org-element--cache-compare'. This cache is used in
4723 `org-element-at-point'.")
4725 (defvar org-element--cache-objects nil
4726 "Hash table used as to cache objects.
4727 Key is an element, as returned by `org-element-at-point', and
4728 value is an alist where each association is:
4730 \(POS CANDIDATES . OBJECTS)
4732 where POS is a buffer position, CANDIDATES is the last know list
4733 of successors (see `org-element--get-next-object-candidates') in
4734 container starting at POS and OBJECTS is a list of objects known
4735 to live within that container, from farthest to closest.
4737 In the following example, \\alpha, bold object and \\beta start
4738 at, respectively, positions 1, 7 and 8,
4740 \\alpha *\\beta*
4742 If the paragraph is completely parsed, OBJECTS-DATA will be
4744 \((1 nil BOLD-OBJECT ENTITY-OBJECT)
4745 \(8 nil ENTITY-OBJECT))
4747 whereas in a partially parsed paragraph, it could be
4749 \((1 ((entity . 1) (bold . 7)) ENTITY-OBJECT))
4751 This cache is used in `org-element-context'.")
4753 (defvar org-element--cache-sync-requests nil
4754 "List of pending synchronization requests.
4756 A request is a vector with the following pattern:
4758 \[NEXT END OFFSET PARENT PHASE]
4760 Processing a synchronization request consists in three phases:
4762 0. Delete modified elements,
4763 1. Fill missing area in cache,
4764 2. Shift positions and re-parent elements after the changes.
4766 During phase 0, NEXT is the key of the first element to be
4767 removed and END is buffer position delimiting the modifications.
4768 Every element starting between these are removed. PARENT is an
4769 element to be removed. Every element contained in it will also
4770 be removed.
4772 During phase 1, NEXT is the key of the next known element in
4773 cache. Parse buffer between that element and the one before it
4774 in order to determine the parent of the next element. Set PARENT
4775 to the element containing NEXT.
4777 During phase 2, NEXT is the key of the next element to shift in
4778 the parse tree. All elements starting from this one have their
4779 properties relatives to buffer positions shifted by integer
4780 OFFSET and, if they belong to element PARENT, are adopted by it.
4782 PHASE specifies the phase number, as an integer.")
4784 (defvar org-element--cache-sync-timer nil
4785 "Timer used for cache synchronization.")
4787 (defvar org-element--cache-sync-keys nil
4788 "Hash table used to store keys during synchronization.
4789 See `org-element--cache-key' for more information.")
4791 (defsubst org-element--cache-key (element)
4792 "Return a unique key for ELEMENT in cache tree.
4794 Keys are used to keep a total order among elements in the cache.
4795 Comparison is done with `org-element--cache-key-less-p'.
4797 When no synchronization is taking place, a key is simply the
4798 beginning position of the element, or that position plus one in
4799 the case of an first item (respectively row) in
4800 a list (respectively a table).
4802 During a synchronization, the key is the one the element had when
4803 the cache was synchronized for the last time. Elements added to
4804 cache during the synchronization get a new key generated with
4805 `org-element--cache-generate-key'.
4807 Such keys are stored in `org-element--cache-sync-keys'. The hash
4808 table is cleared once the synchronization is complete."
4809 (or (gethash element org-element--cache-sync-keys)
4810 (let* ((begin (org-element-property :begin element))
4811 ;; Increase beginning position of items (respectively
4812 ;; table rows) by one, so the first item can get
4813 ;; a different key from its parent list (respectively
4814 ;; table).
4815 (key (if (memq (org-element-type element) '(item table-row))
4816 (1+ begin)
4817 begin)))
4818 (if org-element--cache-sync-requests
4819 (puthash element key org-element--cache-sync-keys)
4820 key))))
4822 (defconst org-element--cache-default-key (ash most-positive-fixnum -1)
4823 "Default value for a new key level.
4824 See `org-element--cache-generate-key' for more information.")
4826 (defun org-element--cache-generate-key (lower upper)
4827 "Generate a key between LOWER and UPPER.
4829 LOWER and UPPER are integers or lists, possibly empty.
4831 If LOWER and UPPER are equals, return LOWER. Otherwise, return
4832 a unique key, as an integer or a list of integers, according to
4833 the following rules:
4835 - LOWER and UPPER are compared level-wise until values differ.
4837 - If, at a given level, LOWER and UPPER differ from more than
4838 2, the new key shares all the levels above with LOWER and
4839 gets a new level. Its value is the mean between LOWER and
4840 UPPER.
4842 \(1 2) + (1 4) --> (1 3)
4844 - If LOWER has no value to compare with, it is assumed that its
4845 value is 0:
4847 \(1 1) + (1 1 2) --> (1 1 1)
4849 Likewise, if UPPER is short of levels, the current value is
4850 `most-positive-fixnum'.
4852 - If they differ from only one, the new key inherits from
4853 current LOWER lever and has a new level at the value
4854 `org-element--cache-default-key'.
4856 \(1 2) + (1 3) --> (1 2 org-element--cache-default-key)
4858 - If the key is only one level long, it is returned as an
4859 integer.
4861 \(1 2) + (3 2) --> 2"
4862 (if (equal lower upper) lower
4863 (let ((lower (if (integerp lower) (list lower) lower))
4864 (upper (if (integerp upper) (list upper) upper))
4865 key)
4866 (catch 'exit
4867 (while (and lower upper)
4868 (let ((lower-level (car lower))
4869 (upper-level (car upper)))
4870 (cond
4871 ((= lower-level upper-level)
4872 (push lower-level key)
4873 (setq lower (cdr lower) upper (cdr upper)))
4874 ((= (- upper-level lower-level) 1)
4875 (push lower-level key)
4876 (setq lower (cdr lower))
4877 (while (and lower (= (car lower) most-positive-fixnum))
4878 (push most-positive-fixnum key)
4879 (setq lower (cdr lower)))
4880 (push (if lower
4881 (let ((n (car lower)))
4882 (+ (ash (if (zerop (mod n 2)) n (1+ n)) -1)
4883 org-element--cache-default-key))
4884 org-element--cache-default-key)
4885 key)
4886 (throw 'exit t))
4888 (push (let ((n (car lower)))
4889 (+ (ash (if (zerop (mod n 2)) n (1+ n)) -1)
4890 (ash (car upper) -1)))
4891 key)
4892 (throw 'exit t)))))
4893 (cond
4894 ((not lower)
4895 (while (and upper (zerop (car upper)))
4896 (push 0 key)
4897 (setq upper (cdr upper)))
4898 ;; (n) is equivalent to (n 0 0 0 0 ...) so we want to avoid
4899 ;; ending on a sequence of 0.
4900 (if (= (car upper) 1)
4901 (progn (push 0 key)
4902 (push org-element--cache-default-key key))
4903 (push (if upper (ash (car upper) -1) org-element--cache-default-key)
4904 key)))
4905 ((not upper)
4906 (while (and lower (= (car lower) most-positive-fixnum))
4907 (push most-positive-fixnum key)
4908 (setq lower (cdr lower)))
4909 (push (if lower
4910 (let ((n (car lower)))
4911 (+ (ash (if (zerop (mod n 2)) n (1+ n)) -1)
4912 org-element--cache-default-key))
4913 org-element--cache-default-key)
4914 key))))
4915 ;; Ensure we don't return a list with a single element.
4916 (if (cdr key) (nreverse key) (car key)))))
4918 (defsubst org-element--cache-key-less-p (a b)
4919 "Non-nil if key A is less than key B.
4920 A and B are either integers or lists of integers, as returned by
4921 `org-element--cache-key'."
4922 (if (integerp a) (if (integerp b) (< a b) (<= a (car b)))
4923 (if (integerp b) (< (car a) b)
4924 (catch 'exit
4925 (while (and a b)
4926 (cond ((car-less-than-car a b) (throw 'exit t))
4927 ((car-less-than-car b a) (throw 'exit nil))
4928 (t (setq a (cdr a) b (cdr b)))))
4929 ;; If A is empty, either keys are equal (B is also empty) or
4930 ;; B is less than A (B is longer). Therefore return nil.
4932 ;; If A is not empty, B is necessarily empty and A is less
4933 ;; than B (A is longer). Therefore, return a non-nil value.
4934 a))))
4936 (defun org-element--cache-compare (a b)
4937 "Non-nil when element A is located before element B."
4938 (org-element--cache-key-less-p (org-element--cache-key a)
4939 (org-element--cache-key b)))
4941 (defsubst org-element--cache-root ()
4942 "Return root value in cache.
4943 This function assumes `org-element--cache' is a valid AVL tree."
4944 (avl-tree--node-left (avl-tree--dummyroot org-element--cache)))
4947 ;;;; Tools
4949 (defsubst org-element--cache-active-p ()
4950 "Non-nil when cache is active in current buffer."
4951 (and org-element-use-cache
4952 (or (derived-mode-p 'org-mode) orgstruct-mode)))
4954 (defun org-element--cache-find (pos &optional side)
4955 "Find element in cache starting at POS or before.
4957 POS refers to a buffer position.
4959 When optional argument SIDE is non-nil, the function checks for
4960 elements starting at or past POS instead. If SIDE is `both', the
4961 function returns a cons cell where car is the first element
4962 starting at or before POS and cdr the first element starting
4963 after POS.
4965 The function can only find elements in the synchronized part of
4966 the cache."
4967 (let ((limit (and org-element--cache-sync-requests
4968 (aref (car org-element--cache-sync-requests) 0)))
4969 (node (org-element--cache-root))
4970 lower upper)
4971 (while node
4972 (let* ((element (avl-tree--node-data node))
4973 (begin (org-element-property :begin element)))
4974 (cond
4975 ((and limit
4976 (not (org-element--cache-key-less-p
4977 (org-element--cache-key element) limit)))
4978 (setq node (avl-tree--node-left node)))
4979 ((> begin pos)
4980 (setq upper element
4981 node (avl-tree--node-left node)))
4982 ((< begin pos)
4983 (setq lower element
4984 node (avl-tree--node-right node)))
4985 ;; We found an element in cache starting at POS. If `side'
4986 ;; is `both' we also want the next one in order to generate
4987 ;; a key in-between.
4989 ;; If the element is the first row or item in a table or
4990 ;; a plain list, we always return the table or the plain
4991 ;; list.
4993 ;; In any other case, we return the element found.
4994 ((eq side 'both)
4995 (setq lower element)
4996 (setq node (avl-tree--node-right node)))
4997 ((and (memq (org-element-type element) '(item table-row))
4998 (let ((parent (org-element-property :parent element)))
4999 (and (= (org-element-property :begin element)
5000 (org-element-property :contents-begin parent))
5001 (setq node nil
5002 lower parent
5003 upper parent)))))
5005 (setq node nil
5006 lower element
5007 upper element)))))
5008 (case side
5009 (both (cons lower upper))
5010 ((nil) lower)
5011 (otherwise upper))))
5013 (defun org-element--cache-put (element &optional data)
5014 "Store ELEMENT in current buffer's cache, if allowed.
5015 When optional argument DATA is non-nil, assume is it object data
5016 relative to ELEMENT and store it in the objects cache."
5017 (cond ((not (org-element--cache-active-p)) nil)
5018 ((not data)
5019 (when org-element--cache-sync-requests
5020 ;; During synchronization, first build an appropriate key
5021 ;; for the new element so `avl-tree-enter' can insert it at
5022 ;; the right spot in the cache.
5023 (let ((keys (org-element--cache-find
5024 (org-element-property :begin element) 'both)))
5025 (puthash element
5026 (org-element--cache-generate-key
5027 (and (car keys) (org-element--cache-key (car keys)))
5028 (cond ((cdr keys) (org-element--cache-key (cdr keys)))
5029 (org-element--cache-sync-requests
5030 (aref (car org-element--cache-sync-requests) 0))))
5031 org-element--cache-sync-keys)))
5032 (avl-tree-enter org-element--cache element))
5033 ;; Headlines are not stored in cache, so objects in titles are
5034 ;; not stored either.
5035 ((eq (org-element-type element) 'headline) nil)
5036 (t (puthash element data org-element--cache-objects))))
5039 ;;;; Synchronization
5041 (defsubst org-element--cache-set-timer (buffer)
5042 "Set idle timer for cache synchronization in BUFFER."
5043 (when org-element--cache-sync-timer
5044 (cancel-timer org-element--cache-sync-timer))
5045 (setq org-element--cache-sync-timer
5046 (run-with-idle-timer
5047 (let ((idle (current-idle-time)))
5048 (if idle (time-add idle org-element-cache-sync-break)
5049 org-element-cache-sync-idle-time))
5051 #'org-element--cache-sync
5052 buffer)))
5054 (defsubst org-element--cache-interrupt-p (time-limit)
5055 "Non-nil when synchronization process should be interrupted.
5056 TIME-LIMIT is a time value or nil."
5057 (and time-limit
5058 (or (input-pending-p)
5059 (time-less-p time-limit (current-time)))))
5061 (defsubst org-element--cache-shift-positions (element offset &optional props)
5062 "Shift ELEMENT properties relative to buffer positions by OFFSET.
5064 Properties containing buffer positions are `:begin', `:end',
5065 `:contents-begin', `:contents-end' and `:structure'. When
5066 optional argument PROPS is a list of keywords, only shift
5067 properties provided in that list.
5069 Properties are modified by side-effect."
5070 (let ((properties (nth 1 element)))
5071 ;; Shift `:structure' property for the first plain list only: it
5072 ;; is the only one that really matters and it prevents from
5073 ;; shifting it more than once.
5074 (when (and (or (not props) (memq :structure props))
5075 (eq (org-element-type element) 'plain-list)
5076 (not (eq (org-element-type (plist-get properties :parent))
5077 'item)))
5078 (dolist (item (plist-get properties :structure))
5079 (incf (car item) offset)
5080 (incf (nth 6 item) offset)))
5081 (dolist (key '(:begin :contents-begin :contents-end :end :post-affiliated))
5082 (let ((value (and (or (not props) (memq key props))
5083 (plist-get properties key))))
5084 (and value (plist-put properties key (+ offset value)))))))
5086 (defun org-element--cache-sync (buffer &optional threshold)
5087 "Synchronize cache with recent modification in BUFFER.
5088 When optional argument THRESHOLD is non-nil, do the
5089 synchronization for all elements starting before or at threshold,
5090 then exit. Otherwise, synchronize cache for as long as
5091 `org-element-cache-sync-duration' or until Emacs leaves idle
5092 state."
5093 (when (buffer-live-p buffer)
5094 (with-current-buffer buffer
5095 (let ((inhibit-quit t) request next)
5096 (when org-element--cache-sync-timer
5097 (cancel-timer org-element--cache-sync-timer))
5098 (catch 'interrupt
5099 (while org-element--cache-sync-requests
5100 (setq request (car org-element--cache-sync-requests)
5101 next (nth 1 org-element--cache-sync-requests))
5102 (or (org-element--cache-process-request
5103 request
5104 (and next (aref next 0))
5105 threshold
5106 (and (not threshold)
5107 (time-add (current-time)
5108 org-element-cache-sync-duration)))
5109 (throw 'interrupt t))
5110 ;; Request processed. Merge current and next offsets and
5111 ;; transfer phase number and ending position.
5112 (when next
5113 (incf (aref next 2) (aref request 2))
5114 (aset next 1 (aref request 1))
5115 (aset next 4 (aref request 4)))
5116 (setq org-element--cache-sync-requests
5117 (cdr org-element--cache-sync-requests))))
5118 ;; If more requests are awaiting, set idle timer accordingly.
5119 ;; Otherwise, reset keys.
5120 (if org-element--cache-sync-requests
5121 (org-element--cache-set-timer buffer)
5122 (clrhash org-element--cache-sync-keys))))))
5124 (defun org-element--cache-process-request (request next threshold time-limit)
5125 "Process synchronization REQUEST for all entries before NEXT.
5127 REQUEST is a vector, built by `org-element--cache-submit-request'.
5129 NEXT is a cache key, as returned by `org-element--cache-key'.
5131 When non-nil, THRESHOLD is a buffer position. Synchronization
5132 stops as soon as a shifted element begins after it.
5134 When non-nil, TIME-LIMIT is a time value. Synchronization stops
5135 after this time or when Emacs exits idle state.
5137 Return nil if the process stops before completing the request,
5138 t otherwise."
5139 (catch 'quit
5140 (when (= (aref request 4) 0)
5141 ;; Phase 1.
5143 ;; Delete all elements starting after BEG, but not after buffer
5144 ;; position END or past element with key NEXT.
5146 ;; As an exception, also delete elements starting after
5147 ;; modifications but included in an element altered by
5148 ;; modifications (orphans).
5150 ;; At each iteration, we start again at tree root since
5151 ;; a deletion modifies structure of the balanced tree.
5152 (catch 'end-phase
5153 (let ((beg (aref request 0))
5154 (end (aref request 1))
5155 (deleted-parent (aref request 3)))
5156 (while t
5157 (when (org-element--cache-interrupt-p time-limit)
5158 (aset request 3 deleted-parent)
5159 (throw 'quit nil))
5160 ;; Find first element in cache with key BEG or after it.
5161 ;; We don't use `org-element--cache-find' because it
5162 ;; couldn't reach orphaned elements past NEXT. Moreover,
5163 ;; BEG is a key, not a buffer position.
5164 (let ((node (org-element--cache-root)) data data-key)
5165 (while node
5166 (let* ((element (avl-tree--node-data node))
5167 (key (org-element--cache-key element)))
5168 (cond
5169 ((org-element--cache-key-less-p key beg)
5170 (setq node (avl-tree--node-right node)))
5171 ((org-element--cache-key-less-p beg key)
5172 (setq data element
5173 data-key key
5174 node (avl-tree--node-left node)))
5175 (t (setq data element
5176 data-key key
5177 node nil)))))
5178 (if (not data) (throw 'quit t)
5179 (let ((pos (org-element-property :begin data)))
5180 (cond
5181 ;; Remove orphaned elements.
5182 ((and deleted-parent
5183 (let ((up data))
5184 (while (and
5185 (setq up (org-element-property :parent up))
5186 (not (eq up deleted-parent))))
5187 up))
5188 (avl-tree-delete org-element--cache data))
5189 ((or (and next
5190 (not (org-element--cache-key-less-p data-key
5191 next)))
5192 (> pos end))
5193 (aset request 0 data-key)
5194 (aset request 1 pos)
5195 (aset request 4 1)
5196 (throw 'end-phase nil))
5197 (t (avl-tree-delete org-element--cache data)
5198 (when (= (org-element-property :end data) end)
5199 (setq deleted-parent data)))))))))))
5200 (when (= (aref request 4) 1)
5201 ;; Phase 2.
5203 ;; Phase 1 left a hole in the parse tree. Some elements after
5204 ;; it could have parents within. For example, in the following
5205 ;; buffer:
5208 ;; - item
5211 ;; Paragraph1
5213 ;; Paragraph2
5216 ;; if we remove a blank line between "item" and "Paragraph1",
5217 ;; everything down to "Paragraph2" is removed from cache. But
5218 ;; the paragraph now belongs to the list, and its `:parent'
5219 ;; property no longer is accurate.
5221 ;; Therefore we need to parse again elements in the hole, or at
5222 ;; least in its last section, so that we can re-parent
5223 ;; subsequent elements, during phase 3.
5225 ;; Note that we only need to get the parent from the first
5226 ;; element in cache after the hole.
5228 ;; Also, this part can be delayed if we don't need to retrieve
5229 ;; an element after the hole.
5230 (catch 'end-phase
5231 ;; Next element will start at its beginning position plus
5232 ;; offset, since it hasn't been shifted yet. Therefore, LIMIT
5233 ;; contains the real beginning position of the first element
5234 ;; to shift and re-parent.
5235 (when (equal (aref request 0) next) (throw 'quit t))
5236 (let ((limit (+ (aref request 1) (aref request 2))))
5237 (when (and threshold (< threshold limit)) (throw 'quit nil))
5238 (let ((parent (org-element--parse-to limit t time-limit)))
5239 (if (eq parent 'interrupted) (throw 'quit nil)
5240 (aset request 3 parent)
5241 (aset request 4 2)
5242 (throw 'end-phase nil))))))
5243 ;; Phase 3.
5245 ;; Shift all elements starting from key START, but before NEXT, by
5246 ;; OFFSET, and re-parent them when appropriate.
5248 ;; Elements are modified by side-effect so the tree structure
5249 ;; remains intact.
5251 ;; Once THRESHOLD, if any, is reached, or once there is an input
5252 ;; pending, exit. Before leaving, the current synchronization
5253 ;; request is updated.
5254 (let ((start (aref request 0))
5255 (offset (aref request 2))
5256 (parent (aref request 3))
5257 (node (org-element--cache-root))
5258 (stack (list nil))
5259 (leftp t)
5260 exit-flag)
5261 ;; No re-parenting nor shifting planned: request is over.
5262 (when (and (not parent) (zerop offset)) (throw 'quit t))
5263 (while node
5264 (let* ((data (avl-tree--node-data node))
5265 (key (org-element--cache-key data)))
5266 (if (and leftp (avl-tree--node-left node)
5267 (not (org-element--cache-key-less-p key start)))
5268 (progn (push node stack)
5269 (setq node (avl-tree--node-left node)))
5270 (unless (org-element--cache-key-less-p key start)
5271 ;; We reached NEXT. Request is complete.
5272 (when (equal key next) (throw 'quit t))
5273 ;; Handle interruption request. Update current request.
5274 (when (or exit-flag (org-element--cache-interrupt-p time-limit))
5275 (aset request 0 key)
5276 (aset request 3 parent)
5277 (throw 'quit nil))
5278 ;; Shift element.
5279 (unless (zerop offset)
5280 (org-element--cache-shift-positions data offset)
5281 ;; Shift associated objects data, if any.
5282 (dolist (object-data (gethash data org-element--cache-objects))
5283 (incf (car object-data) offset)
5284 (dolist (successor (nth 1 object-data))
5285 (incf (cdr successor) offset))
5286 (dolist (object (cddr object-data))
5287 (org-element--cache-shift-positions object offset))))
5288 (let ((begin (org-element-property :begin data)))
5289 ;; Re-parent it.
5290 (while (and parent
5291 (<= (org-element-property :end parent) begin))
5292 (setq parent (org-element-property :parent parent)))
5293 (cond (parent (org-element-put-property data :parent parent))
5294 ((zerop offset) (throw 'quit t)))
5295 ;; Cache is up-to-date past THRESHOLD. Request
5296 ;; interruption.
5297 (when (and threshold (> begin threshold)) (setq exit-flag t))))
5298 (setq node (if (setq leftp (avl-tree--node-right node))
5299 (avl-tree--node-right node)
5300 (pop stack))))))
5301 ;; We reached end of tree: synchronization complete.
5302 t)))
5304 (defun org-element--parse-to (pos &optional syncp time-limit)
5305 "Parse elements in current section, down to POS.
5307 Start parsing from the closest between the last known element in
5308 cache or headline above. Return the smallest element containing
5309 POS.
5311 When optional argument SYNCP is non-nil, return the parent of the
5312 element containing POS instead. In that case, it is also
5313 possible to provide TIME-LIMIT, which is a time value specifying
5314 when the parsing should stop. The function returns `interrupted'
5315 if the process stopped before finding the expected result."
5316 (catch 'exit
5317 (org-with-wide-buffer
5318 (goto-char pos)
5319 (let* ((cached (and (org-element--cache-active-p)
5320 (org-element--cache-find pos nil)))
5321 (begin (org-element-property :begin cached))
5322 element next)
5323 (cond
5324 ;; Nothing in cache before point: start parsing from first
5325 ;; element following headline above, or first element in
5326 ;; buffer.
5327 ((not cached)
5328 (when (org-with-limited-levels (outline-previous-heading))
5329 (forward-line))
5330 (skip-chars-forward " \r\t\n")
5331 (beginning-of-line))
5332 ;; Cache returned exact match: return it.
5333 ((= pos begin)
5334 (throw 'exit (if syncp (org-element-property :parent cached) cached)))
5335 ;; There's a headline between cached value and POS: cached
5336 ;; value is invalid. Start parsing from first element
5337 ;; following the headline.
5338 ((re-search-backward
5339 (org-with-limited-levels org-outline-regexp-bol) begin t)
5340 (forward-line)
5341 (skip-chars-forward " \r\t\n")
5342 (beginning-of-line))
5343 ;; Check if CACHED or any of its ancestors contain point.
5345 ;; If there is such an element, we inspect it in order to know
5346 ;; if we return it or if we need to parse its contents.
5347 ;; Otherwise, we just start parsing from current location,
5348 ;; which is right after the top-most element containing
5349 ;; CACHED.
5351 ;; As a special case, if POS is at the end of the buffer, we
5352 ;; want to return the innermost element ending there.
5354 ;; Also, if we find an ancestor and discover that we need to
5355 ;; parse its contents, make sure we don't start from
5356 ;; `:contents-begin', as we would otherwise go past CACHED
5357 ;; again. Instead, in that situation, we will resume parsing
5358 ;; from NEXT, which is located after CACHED or its higher
5359 ;; ancestor not containing point.
5361 (let ((up cached)
5362 (pos (if (= (point-max) pos) (1- pos) pos)))
5363 (goto-char (or (org-element-property :contents-begin cached) begin))
5364 (while (let ((end (org-element-property :end up)))
5365 (and (<= end pos)
5366 (goto-char end)
5367 (setq up (org-element-property :parent up)))))
5368 (cond ((not up))
5369 ((eobp) (setq element up))
5370 (t (setq element up next (point)))))))
5371 ;; Parse successively each element until we reach POS.
5372 (let ((end (or (org-element-property :end element)
5373 (save-excursion
5374 (org-with-limited-levels (outline-next-heading))
5375 (point))))
5376 (parent element)
5377 special-flag)
5378 (while t
5379 (when syncp
5380 (cond ((= (point) pos) (throw 'exit parent))
5381 ((org-element--cache-interrupt-p time-limit)
5382 (throw 'exit 'interrupted))))
5383 (unless element
5384 (setq element (org-element--current-element
5385 end 'element special-flag
5386 (org-element-property :structure parent)))
5387 (org-element-put-property element :parent parent)
5388 (org-element--cache-put element))
5389 (let ((elem-end (org-element-property :end element))
5390 (type (org-element-type element)))
5391 (cond
5392 ;; Skip any element ending before point. Also skip
5393 ;; element ending at point (unless it is also the end of
5394 ;; buffer) since we're sure that another element begins
5395 ;; after it.
5396 ((and (<= elem-end pos) (/= (point-max) elem-end))
5397 (goto-char elem-end))
5398 ;; A non-greater element contains point: return it.
5399 ((not (memq type org-element-greater-elements))
5400 (throw 'exit element))
5401 ;; Otherwise, we have to decide if ELEMENT really
5402 ;; contains POS. In that case we start parsing from
5403 ;; contents' beginning.
5405 ;; If POS is at contents' beginning but it is also at
5406 ;; the beginning of the first item in a list or a table.
5407 ;; In that case, we need to create an anchor for that
5408 ;; list or table, so return it.
5410 ;; Also, if POS is at the end of the buffer, no element
5411 ;; can start after it, but more than one may end there.
5412 ;; Arbitrarily, we choose to return the innermost of
5413 ;; such elements.
5414 ((let ((cbeg (org-element-property :contents-begin element))
5415 (cend (org-element-property :contents-end element)))
5416 (when (or syncp
5417 (and cbeg cend
5418 (or (< cbeg pos)
5419 (and (= cbeg pos)
5420 (not (memq type '(plain-list table)))))
5421 (or (> cend pos)
5422 (and (= cend pos) (= (point-max) pos)))))
5423 (goto-char (or next cbeg))
5424 (setq next nil
5425 special-flag (case type
5426 (plain-list 'item)
5427 (property-drawer 'node-property)
5428 (table 'table-row))
5429 parent element
5430 end cend))))
5431 ;; Otherwise, return ELEMENT as it is the smallest
5432 ;; element containing POS.
5433 (t (throw 'exit element))))
5434 (setq element nil)))))))
5437 ;;;; Staging Buffer Changes
5439 (defconst org-element--cache-opening-line
5440 (concat "^[ \t]*\\(?:"
5441 "#\\+BEGIN[:_]" "\\|"
5442 "\\\\begin{[A-Za-z0-9]+\\*?}" "\\|"
5443 ":\\S-+:[ \t]*$"
5444 "\\)")
5445 "Regexp matching an element opening line.
5446 When such a line is modified, modifications may propagate after
5447 modified area. In that situation, every element between that
5448 area and next section is removed from cache.")
5450 (defconst org-element--cache-closing-line
5451 (concat "^[ \t]*\\(?:"
5452 "#\\+END\\(?:_\\|:?[ \t]*$\\)" "\\|"
5453 "\\\\end{[A-Za-z0-9]+\\*?}[ \t]*$" "\\|"
5454 ":END:[ \t]*$"
5455 "\\)")
5456 "Regexp matching an element closing line.
5457 When such a line is modified, modifications may propagate before
5458 modified area. In that situation, every element between that
5459 area and previous section is removed from cache.")
5461 (defvar org-element--cache-change-warning nil
5462 "Non-nil when a sensitive line is about to be changed.
5463 It is a symbol among nil, t and `headline'.")
5465 (defun org-element--cache-before-change (beg end)
5466 "Request extension of area going to be modified if needed.
5467 BEG and END are the beginning and end of the range of changed
5468 text. See `before-change-functions' for more information."
5469 (let ((inhibit-quit t))
5470 ;; Make sure buffer positions in cache are correct until END.
5471 (save-match-data
5472 (org-element--cache-sync (current-buffer) end)
5473 (org-with-wide-buffer
5474 (goto-char beg)
5475 (beginning-of-line)
5476 (let ((top (point))
5477 (bottom (save-excursion (goto-char end) (line-end-position)))
5478 (sensitive-re
5479 ;; A sensitive line is a headline or a block (or drawer,
5480 ;; or latex-environment) boundary. Inserting one can
5481 ;; modify buffer drastically both above and below that
5482 ;; line, possibly making cache invalid. Therefore, we
5483 ;; need to pay attention to changes happening to them.
5484 (concat
5485 "\\(" (org-with-limited-levels org-outline-regexp-bol) "\\)" "\\|"
5486 org-element--cache-closing-line "\\|"
5487 org-element--cache-opening-line)))
5488 (setq org-element--cache-change-warning
5489 (cond ((not (re-search-forward sensitive-re bottom t)) nil)
5490 ((and (match-beginning 1)
5491 (progn (goto-char bottom)
5492 (or (not (re-search-backward sensitive-re
5493 (match-end 1) t))
5494 (match-beginning 1))))
5495 'headline)
5496 (t))))))))
5498 (defun org-element--cache-after-change (beg end pre)
5499 "Update buffer modifications for current buffer.
5500 BEG and END are the beginning and end of the range of changed
5501 text, and the length in bytes of the pre-change text replaced by
5502 that range. See `after-change-functions' for more information."
5503 (let ((inhibit-quit t))
5504 (when (org-element--cache-active-p)
5505 (org-with-wide-buffer
5506 (goto-char beg)
5507 (beginning-of-line)
5508 (let ((top (point))
5509 (bottom (save-excursion (goto-char end) (line-end-position))))
5510 (org-with-limited-levels
5511 (save-match-data
5512 ;; Determine if modified area needs to be extended,
5513 ;; according to both previous and current state. We make
5514 ;; a special case for headline editing: if a headline is
5515 ;; modified but not removed, do not extend.
5516 (when (let ((previous-state org-element--cache-change-warning)
5517 (sensitive-re
5518 (concat "\\(" org-outline-regexp-bol "\\)" "\\|"
5519 org-element--cache-closing-line "\\|"
5520 org-element--cache-opening-line))
5521 (case-fold-search t))
5522 (cond ((eq previous-state t))
5523 ((not (re-search-forward sensitive-re bottom t))
5524 (eq previous-state 'headline))
5525 ((match-beginning 1)
5526 (or (not (eq previous-state 'headline))
5527 (and (progn (goto-char bottom)
5528 (re-search-backward
5529 sensitive-re (match-end 1) t))
5530 (not (match-beginning 1)))))
5531 (t)))
5532 ;; Effectively extend modified area.
5533 (setq top (progn (goto-char top)
5534 (when (outline-previous-heading) (forward-line))
5535 (point)))
5536 (setq bottom (progn (goto-char bottom)
5537 (if (outline-next-heading) (1- (point))
5538 (point)))))))
5539 ;; Store synchronization request.
5540 (let ((offset (- end beg pre)))
5541 (org-element--cache-submit-request top (- bottom offset) offset))))
5542 ;; Activate a timer to process the request during idle time.
5543 (org-element--cache-set-timer (current-buffer)))))
5545 (defun org-element--cache-submit-request (beg end offset)
5546 "Submit a new cache synchronization request for current buffer.
5547 BEG and END are buffer positions delimiting the minimal area
5548 where cache data should be removed. OFFSET is the size of the
5549 change, as an integer."
5550 (let ((first-element
5551 ;; Find the position of the first element in cache to remove.
5553 ;; Partially modified elements will be removed during request
5554 ;; processing. As an exception, greater elements around the
5555 ;; changes that are robust to contents modifications are
5556 ;; preserved.
5558 ;; We look just before BEG because an element ending at BEG
5559 ;; needs to be removed too.
5560 (let* ((elements (org-element--cache-find (1- beg) 'both))
5561 (before (car elements))
5562 (after (cdr elements)))
5563 (if (not before) after
5564 (let ((up before))
5565 (while (setq up (org-element-property :parent up))
5566 (if (and (memq (org-element-type up)
5567 '(center-block
5568 drawer dynamic-block inlinetask
5569 property-drawer quote-block special-block))
5570 (<= (org-element-property :contents-begin up) beg)
5571 (> (org-element-property :contents-end up) end))
5572 ;; UP is a greater element that is wrapped around
5573 ;; the changes. We only need to extend its
5574 ;; ending boundaries and those of all its
5575 ;; parents.
5576 (while up
5577 (org-element--cache-shift-positions
5578 up offset '(:contents-end :end))
5579 (setq up (org-element-property :parent up)))
5580 (setq before up)))
5581 ;; We're at top level element containing ELEMENT: if
5582 ;; it's altered by buffer modifications, it is first
5583 ;; element in cache to be removed. Otherwise, that
5584 ;; first element is the following one.
5585 (if (< (org-element-property :end before) beg) after before))))))
5586 (cond
5587 ;; Changes happened before the first known element. Shift the
5588 ;; rest of the cache.
5589 ((and first-element (> (org-element-property :begin first-element) end))
5590 (push (vector (org-element--cache-key first-element) nil offset nil 2)
5591 org-element--cache-sync-requests))
5592 ;; There is at least an element to remove. Find position past
5593 ;; every element containing END.
5594 (first-element
5595 (if (> (org-element-property :end first-element) end)
5596 (setq end (org-element-property :end first-element))
5597 (let ((element (org-element--cache-find end)))
5598 (setq end (org-element-property :end element))
5599 (let ((up element))
5600 (while (and (setq up (org-element-property :parent up))
5601 (>= (org-element-property :begin up) beg))
5602 (setq end (org-element-property :end up))))))
5603 (push (vector (org-element--cache-key first-element) end offset nil 0)
5604 org-element--cache-sync-requests))
5605 ;; No element to remove. No need to re-parent either. Simply
5606 ;; shift additional elements, if any, by OFFSET.
5607 (org-element--cache-sync-requests
5608 (incf (aref (car org-element--cache-sync-requests) 2) offset)))))
5611 ;;;; Public Functions
5613 ;;;###autoload
5614 (defun org-element-cache-reset (&optional all)
5615 "Reset cache in current buffer.
5616 When optional argument ALL is non-nil, reset cache in all Org
5617 buffers. This function will do nothing if
5618 `org-element-use-cache' is nil."
5619 (interactive "P")
5620 (dolist (buffer (if all (buffer-list) (list (current-buffer))))
5621 (with-current-buffer buffer
5622 (when (org-element--cache-active-p)
5623 (org-set-local 'org-element--cache
5624 (avl-tree-create #'org-element--cache-compare))
5625 (org-set-local 'org-element--cache-objects
5626 (make-hash-table :weakness 'key :test #'eq))
5627 (org-set-local 'org-element--cache-sync-keys
5628 (make-hash-table :weakness 'key :test #'eq))
5629 (org-set-local 'org-element--cache-change-warning nil)
5630 (org-set-local 'org-element--cache-sync-requests nil)
5631 (org-set-local 'org-element--cache-sync-timer nil)
5632 (add-hook 'before-change-functions
5633 #'org-element--cache-before-change nil t)
5634 (add-hook 'after-change-functions
5635 #'org-element--cache-after-change nil t)))))
5639 ;;; The Toolbox
5641 ;; The first move is to implement a way to obtain the smallest element
5642 ;; containing point. This is the job of `org-element-at-point'. It
5643 ;; basically jumps back to the beginning of section containing point
5644 ;; and proceed, one element after the other, with
5645 ;; `org-element--current-element' until the container is found. Note:
5646 ;; When using `org-element-at-point', secondary values are never
5647 ;; parsed since the function focuses on elements, not on objects.
5649 ;; At a deeper level, `org-element-context' lists all elements and
5650 ;; objects containing point.
5652 ;; `org-element-nested-p' and `org-element-swap-A-B' may be used
5653 ;; internally by navigation and manipulation tools.
5656 ;;;###autoload
5657 (defun org-element-at-point ()
5658 "Determine closest element around point.
5660 Return value is a list like (TYPE PROPS) where TYPE is the type
5661 of the element and PROPS a plist of properties associated to the
5662 element.
5664 Possible types are defined in `org-element-all-elements'.
5665 Properties depend on element or object type, but always include
5666 `:begin', `:end', `:parent' and `:post-blank' properties.
5668 As a special case, if point is at the very beginning of the first
5669 item in a list or sub-list, returned element will be that list
5670 instead of the item. Likewise, if point is at the beginning of
5671 the first row of a table, returned element will be the table
5672 instead of the first row.
5674 When point is at the end of the buffer, return the innermost
5675 element ending there."
5676 (org-with-wide-buffer
5677 (let ((origin (point)))
5678 (end-of-line)
5679 (skip-chars-backward " \r\t\n")
5680 (cond
5681 ;; Within blank lines at the beginning of buffer, return nil.
5682 ((bobp) nil)
5683 ;; Within blank lines right after a headline, return that
5684 ;; headline.
5685 ((org-with-limited-levels (org-at-heading-p))
5686 (beginning-of-line)
5687 (org-element-headline-parser (point-max) t))
5688 ;; Otherwise parse until we find element containing ORIGIN.
5690 (when (org-element--cache-active-p)
5691 (if (not org-element--cache) (org-element-cache-reset)
5692 (org-element--cache-sync (current-buffer) origin)))
5693 (org-element--parse-to origin))))))
5695 ;;;###autoload
5696 (defun org-element-context (&optional element)
5697 "Return closest element or object around point.
5699 Return value is a list like (TYPE PROPS) where TYPE is the type
5700 of the element or object and PROPS a plist of properties
5701 associated to it.
5703 Possible types are defined in `org-element-all-elements' and
5704 `org-element-all-objects'. Properties depend on element or
5705 object type, but always include `:begin', `:end', `:parent' and
5706 `:post-blank'.
5708 Optional argument ELEMENT, when non-nil, is the closest element
5709 containing point, as returned by `org-element-at-point'.
5710 Providing it allows for quicker computation."
5711 (catch 'objects-forbidden
5712 (org-with-wide-buffer
5713 (let* ((origin (point))
5714 (element (or element (org-element-at-point)))
5715 (type (org-element-type element)))
5716 ;; If point is inside an element containing objects or
5717 ;; a secondary string, narrow buffer to the container and
5718 ;; proceed with parsing. Otherwise, return ELEMENT.
5719 (cond
5720 ;; At a parsed affiliated keyword, check if we're inside main
5721 ;; or dual value.
5722 ((let ((post (org-element-property :post-affiliated element)))
5723 (and post (< origin post)))
5724 (beginning-of-line)
5725 (let ((case-fold-search t)) (looking-at org-element--affiliated-re))
5726 (cond
5727 ((not (member-ignore-case (match-string 1)
5728 org-element-parsed-keywords))
5729 (throw 'objects-forbidden element))
5730 ((< (match-end 0) origin)
5731 (narrow-to-region (match-end 0) (line-end-position)))
5732 ((and (match-beginning 2)
5733 (>= origin (match-beginning 2))
5734 (< origin (match-end 2)))
5735 (narrow-to-region (match-beginning 2) (match-end 2)))
5736 (t (throw 'objects-forbidden element)))
5737 ;; Also change type to retrieve correct restrictions.
5738 (setq type 'keyword))
5739 ;; At an item, objects can only be located within tag, if any.
5740 ((eq type 'item)
5741 (let ((tag (org-element-property :tag element)))
5742 (if (not tag) (throw 'objects-forbidden element)
5743 (beginning-of-line)
5744 (search-forward tag (line-end-position))
5745 (goto-char (match-beginning 0))
5746 (if (and (>= origin (point)) (< origin (match-end 0)))
5747 (narrow-to-region (point) (match-end 0))
5748 (throw 'objects-forbidden element)))))
5749 ;; At an headline or inlinetask, objects are in title.
5750 ((memq type '(headline inlinetask))
5751 (goto-char (org-element-property :begin element))
5752 (skip-chars-forward "*")
5753 (if (and (> origin (point)) (< origin (line-end-position)))
5754 (narrow-to-region (point) (line-end-position))
5755 (throw 'objects-forbidden element)))
5756 ;; At a paragraph, a table-row or a verse block, objects are
5757 ;; located within their contents.
5758 ((memq type '(paragraph table-row verse-block))
5759 (let ((cbeg (org-element-property :contents-begin element))
5760 (cend (org-element-property :contents-end element)))
5761 ;; CBEG is nil for table rules.
5762 (if (and cbeg cend (>= origin cbeg)
5763 (or (< origin cend) (and (= origin cend) (eobp))))
5764 (narrow-to-region cbeg cend)
5765 (throw 'objects-forbidden element))))
5766 ;; At a parsed keyword, objects are located within value.
5767 ((eq type 'keyword)
5768 (if (not (member (org-element-property :key element)
5769 org-element-document-properties))
5770 (throw 'objects-forbidden element)
5771 (beginning-of-line)
5772 (search-forward ":")
5773 (if (and (>= origin (point)) (< origin (line-end-position)))
5774 (narrow-to-region (point) (line-end-position))
5775 (throw 'objects-forbidden element))))
5776 ;; At a planning line, if point is at a timestamp, return it,
5777 ;; otherwise, return element.
5778 ((eq type 'planning)
5779 (dolist (p '(:closed :deadline :scheduled))
5780 (let ((timestamp (org-element-property p element)))
5781 (when (and timestamp
5782 (<= (org-element-property :begin timestamp) origin)
5783 (> (org-element-property :end timestamp) origin))
5784 (throw 'objects-forbidden timestamp))))
5785 ;; All other locations cannot contain objects: bail out.
5786 (throw 'objects-forbidden element))
5787 (t (throw 'objects-forbidden element)))
5788 (goto-char (point-min))
5789 (let* ((restriction (org-element-restriction type))
5790 (parent element)
5791 (candidates 'initial)
5792 (cache (cond ((not (org-element--cache-active-p)) nil)
5793 (org-element--cache-objects
5794 (gethash element org-element--cache-objects))
5795 (t (org-element-cache-reset) nil)))
5796 objects-data next)
5797 (prog1
5798 (catch 'exit
5799 (while t
5800 ;; Get list of next object candidates in CANDIDATES.
5801 ;; When entering for the first time PARENT, grab it
5802 ;; from cache, if available, or compute it. Then,
5803 ;; for each subsequent iteration in PARENT, always
5804 ;; compute it since we're beyond cache anyway.
5805 (unless next
5806 (let* ((key
5807 ;; Increase key of objects contained in
5808 ;; table cells by one so they cannot get
5809 ;; the same key as the cell itself.
5810 (if (eq (org-element-type parent) 'table-cell)
5811 (1+ (point))
5812 (point)))
5813 (data (assq key cache)))
5814 (if data (setq candidates (nth 1 (setq objects-data data)))
5815 (push (setq objects-data (list key 'initial))
5816 cache))))
5817 (when (or next (eq 'initial candidates))
5818 (setq candidates
5819 (org-element--get-next-object-candidates
5820 restriction candidates))
5821 (setcar (cdr objects-data) candidates))
5822 ;; Compare ORIGIN with next object starting position,
5823 ;; if any.
5825 ;; If ORIGIN is lesser or if there is no object
5826 ;; following, look for a previous object that might
5827 ;; contain it in cache. If there is no cache, we
5828 ;; didn't miss any object so simply return PARENT.
5830 ;; If ORIGIN is greater or equal, parse next
5831 ;; candidate for further processing.
5832 (let ((closest
5833 (and candidates
5834 (rassq (apply #'min (mapcar #'cdr candidates))
5835 candidates))))
5836 (if (or (not closest) (> (cdr closest) origin))
5837 (catch 'found
5838 (dolist (obj (cddr objects-data) (throw 'exit parent))
5839 (when (<= (org-element-property :begin obj) origin)
5840 (let ((end (org-element-property :end obj)))
5841 (cond
5842 ((> end origin) (throw 'found (setq next obj)))
5843 ((and (= end origin) (= (point-max) end))
5844 (org-element-put-property obj :parent parent)
5845 (throw 'exit obj))
5846 (t (throw 'exit parent)))))))
5847 (goto-char (cdr closest))
5848 (setq next
5849 (funcall (intern (format "org-element-%s-parser"
5850 (car closest)))))
5851 (push next (cddr objects-data))))
5852 ;; Process NEXT to know if we need to skip it, return
5853 ;; it or move into it.
5854 (let ((cbeg (org-element-property :contents-begin next))
5855 (cend (org-element-property :contents-end next))
5856 (obj-end (org-element-property :end next)))
5857 (cond
5858 ;; ORIGIN is after NEXT, so skip it.
5859 ((<= obj-end origin) (goto-char obj-end))
5860 ;; ORIGIN is within a non-recursive next or
5861 ;; at an object boundaries: Return that object.
5862 ((or (not cbeg) (< origin cbeg) (>= origin cend))
5863 (org-element-put-property next :parent parent)
5864 (throw 'exit next))
5865 ;; Otherwise, move into NEXT and reset flags as we
5866 ;; shift parent.
5867 (t (goto-char cbeg)
5868 (narrow-to-region (point) cend)
5869 (org-element-put-property next :parent parent)
5870 (setq parent next
5871 restriction (org-element-restriction next)
5872 next nil
5873 objects-data nil
5874 candidates 'initial))))))
5875 ;; Store results in cache, if applicable.
5876 (org-element--cache-put element cache)))))))
5878 (defun org-element-nested-p (elem-A elem-B)
5879 "Non-nil when elements ELEM-A and ELEM-B are nested."
5880 (let ((beg-A (org-element-property :begin elem-A))
5881 (beg-B (org-element-property :begin elem-B))
5882 (end-A (org-element-property :end elem-A))
5883 (end-B (org-element-property :end elem-B)))
5884 (or (and (>= beg-A beg-B) (<= end-A end-B))
5885 (and (>= beg-B beg-A) (<= end-B end-A)))))
5887 (defun org-element-swap-A-B (elem-A elem-B)
5888 "Swap elements ELEM-A and ELEM-B.
5889 Assume ELEM-B is after ELEM-A in the buffer. Leave point at the
5890 end of ELEM-A."
5891 (goto-char (org-element-property :begin elem-A))
5892 ;; There are two special cases when an element doesn't start at bol:
5893 ;; the first paragraph in an item or in a footnote definition.
5894 (let ((specialp (not (bolp))))
5895 ;; Only a paragraph without any affiliated keyword can be moved at
5896 ;; ELEM-A position in such a situation. Note that the case of
5897 ;; a footnote definition is impossible: it cannot contain two
5898 ;; paragraphs in a row because it cannot contain a blank line.
5899 (if (and specialp
5900 (or (not (eq (org-element-type elem-B) 'paragraph))
5901 (/= (org-element-property :begin elem-B)
5902 (org-element-property :contents-begin elem-B))))
5903 (error "Cannot swap elements"))
5904 ;; In a special situation, ELEM-A will have no indentation. We'll
5905 ;; give it ELEM-B's (which will in, in turn, have no indentation).
5906 (let* ((ind-B (when specialp
5907 (goto-char (org-element-property :begin elem-B))
5908 (org-get-indentation)))
5909 (beg-A (org-element-property :begin elem-A))
5910 (end-A (save-excursion
5911 (goto-char (org-element-property :end elem-A))
5912 (skip-chars-backward " \r\t\n")
5913 (point-at-eol)))
5914 (beg-B (org-element-property :begin elem-B))
5915 (end-B (save-excursion
5916 (goto-char (org-element-property :end elem-B))
5917 (skip-chars-backward " \r\t\n")
5918 (point-at-eol)))
5919 ;; Store overlays responsible for visibility status. We
5920 ;; also need to store their boundaries as they will be
5921 ;; removed from buffer.
5922 (overlays
5923 (cons
5924 (mapcar (lambda (ov) (list ov (overlay-start ov) (overlay-end ov)))
5925 (overlays-in beg-A end-A))
5926 (mapcar (lambda (ov) (list ov (overlay-start ov) (overlay-end ov)))
5927 (overlays-in beg-B end-B))))
5928 ;; Get contents.
5929 (body-A (buffer-substring beg-A end-A))
5930 (body-B (delete-and-extract-region beg-B end-B)))
5931 (goto-char beg-B)
5932 (when specialp
5933 (setq body-B (replace-regexp-in-string "\\`[ \t]*" "" body-B))
5934 (org-indent-to-column ind-B))
5935 (insert body-A)
5936 ;; Restore ex ELEM-A overlays.
5937 (let ((offset (- beg-B beg-A)))
5938 (mapc (lambda (ov)
5939 (move-overlay
5940 (car ov) (+ (nth 1 ov) offset) (+ (nth 2 ov) offset)))
5941 (car overlays))
5942 (goto-char beg-A)
5943 (delete-region beg-A end-A)
5944 (insert body-B)
5945 ;; Restore ex ELEM-B overlays.
5946 (mapc (lambda (ov)
5947 (move-overlay
5948 (car ov) (- (nth 1 ov) offset) (- (nth 2 ov) offset)))
5949 (cdr overlays)))
5950 (goto-char (org-element-property :end elem-B)))))
5952 (defun org-element-remove-indentation (s &optional n)
5953 "Remove maximum common indentation in string S and return it.
5954 When optional argument N is a positive integer, remove exactly
5955 that much characters from indentation, if possible, or return
5956 S as-is otherwise. Unlike to `org-remove-indentation', this
5957 function doesn't call `untabify' on S."
5958 (catch 'exit
5959 (with-temp-buffer
5960 (insert s)
5961 (goto-char (point-min))
5962 ;; Find maximum common indentation, if not specified.
5963 (setq n (or n
5964 (let ((min-ind (point-max)))
5965 (save-excursion
5966 (while (re-search-forward "^[ \t]*\\S-" nil t)
5967 (let ((ind (1- (current-column))))
5968 (if (zerop ind) (throw 'exit s)
5969 (setq min-ind (min min-ind ind))))))
5970 min-ind)))
5971 (if (zerop n) s
5972 ;; Remove exactly N indentation, but give up if not possible.
5973 (while (not (eobp))
5974 (let ((ind (progn (skip-chars-forward " \t") (current-column))))
5975 (cond ((eolp) (delete-region (line-beginning-position) (point)))
5976 ((< ind n) (throw 'exit s))
5977 (t (org-indent-line-to (- ind n))))
5978 (forward-line)))
5979 (buffer-string)))))
5983 (provide 'org-element)
5985 ;; Local variables:
5986 ;; generated-autoload-file: "org-loaddefs.el"
5987 ;; End:
5989 ;;; org-element.el ends here