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