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/>.
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', `property-drawer', `node-property', `section' and
35 ;; `table-row' types), it can also accept a fixed set of keywords as
36 ;; attributes. Those are called "affiliated keywords" to distinguish
37 ;; them from other keywords, which are full-fledged elements. Almost
38 ;; all affiliated keywords are referenced in
39 ;; `org-element-affiliated-keywords'; the others are export attributes
40 ;; and start with "ATTR_" prefix.
42 ;; Element containing other elements (and only elements) are called
43 ;; greater elements. Concerned types are: `center-block', `drawer',
44 ;; `dynamic-block', `footnote-definition', `headline', `inlinetask',
45 ;; `item', `plain-list', `property-drawer', `quote-block', `section'
46 ;; and `special-block'.
48 ;; Other element types are: `babel-call', `clock', `comment',
49 ;; `comment-block', `diary-sexp', `example-block', `export-block',
50 ;; `fixed-width', `horizontal-rule', `keyword', `latex-environment',
51 ;; `node-property', `paragraph', `planning', `src-block', `table',
52 ;; `table-row' and `verse-block'. Among them, `paragraph' and
53 ;; `verse-block' types can contain Org objects and plain text.
55 ;; Objects are related to document's contents. Some of them are
56 ;; recursive. Associated types are of the following: `bold', `code',
57 ;; `entity', `export-snippet', `footnote-reference',
58 ;; `inline-babel-call', `inline-src-block', `italic',
59 ;; `latex-fragment', `line-break', `link', `macro', `radio-target',
60 ;; `statistics-cookie', `strike-through', `subscript', `superscript',
61 ;; `table-cell', `target', `timestamp', `underline' and `verbatim'.
63 ;; Some elements also have special properties whose value can hold
64 ;; objects themselves (e.g. an item tag or a headline name). Such
65 ;; values are called "secondary strings". Any object belongs to
66 ;; either an element or a secondary string.
68 ;; Notwithstanding affiliated keywords, each greater element, element
69 ;; and object has a fixed set of properties attached to it. Among
70 ;; them, four are shared by all types: `:begin' and `:end', which
71 ;; refer to the beginning and ending buffer positions of the
72 ;; considered element or object, `:post-blank', which holds the number
73 ;; of blank lines, or white spaces, at its end and `:parent' which
74 ;; refers to the element or object containing it. Greater elements,
75 ;; elements and objects containing objects will also have
76 ;; `:contents-begin' and `:contents-end' properties to delimit
77 ;; contents. Eventually, All elements have a `:post-affiliated'
78 ;; property referring to the buffer position after all affiliated
79 ;; keywords, if any, or to their beginning position otherwise.
81 ;; At the lowest level, a `:parent' property is also attached to any
82 ;; string, as a text property.
84 ;; Lisp-wise, an element or an object can be represented as a list.
85 ;; It follows the pattern (TYPE PROPERTIES CONTENTS), where:
86 ;; TYPE is a symbol describing the Org element or object.
87 ;; PROPERTIES is the property list attached to it. See docstring of
88 ;; appropriate parsing function to get an exhaustive
90 ;; CONTENTS is a list of elements, objects or raw strings contained
91 ;; in the current element or object, when applicable.
93 ;; An Org buffer is a nested list of such elements and objects, whose
94 ;; type is `org-data' and properties is nil.
96 ;; The first part of this file defines Org syntax, while the second
97 ;; one provide accessors and setters functions.
99 ;; The next part implements a parser and an interpreter for each
100 ;; element and object type in Org syntax.
102 ;; The following part creates a fully recursive buffer parser. It
103 ;; also provides a tool to map a function to elements or objects
104 ;; matching some criteria in the parse tree. Functions of interest
105 ;; are `org-element-parse-buffer', `org-element-map' and, to a lesser
106 ;; extent, `org-element-parse-secondary-string'.
108 ;; The penultimate part is the cradle of an interpreter for the
109 ;; obtained parse tree: `org-element-interpret-data'.
111 ;; The library ends by furnishing `org-element-at-point' function, and
112 ;; a way to give information about document structure around point
113 ;; with `org-element-context'. A cache mechanism is also provided for
119 (eval-when-compile (require 'cl
))
125 ;;; Definitions And Rules
127 ;; Define elements, greater elements and specify recursive objects,
128 ;; along with the affiliated keywords recognized. Also set up
129 ;; restrictions on recursive objects combinations.
131 ;; `org-element-update-syntax' builds proper syntax regexps according
134 (defvar org-element-paragraph-separate nil
135 "Regexp to separate paragraphs in an Org buffer.
136 In the case of lines starting with \"#\" and \":\", this regexp
137 is not sufficient to know if point is at a paragraph ending. See
138 `org-element-paragraph-parser' for more information.")
140 (defvar org-element--object-regexp nil
141 "Regexp possibly matching the beginning of an object.
142 This regexp allows false positives. Dedicated parser (e.g.,
143 `org-export-bold-parser') will take care of further filtering.
144 Radio links are not matched by this regexp, as they are treated
145 specially in `org-element--object-lex'.")
147 (defun org-element--set-regexps ()
148 "Build variable syntax regexps."
149 (setq org-element-paragraph-separate
151 ;; Headlines, inlinetasks.
152 org-outline-regexp
"\\|"
153 ;; Footnote definitions.
154 "\\[\\(?:[0-9]+\\|fn:[-_[:word:]]+\\)\\]" "\\|"
160 ;; Tables (any type).
161 "\\(?:|\\|\\+-[-+]\\)" "\\|"
162 ;; Blocks (any type), Babel calls and keywords. This
163 ;; is only an indication and need some thorough check.
164 "#\\(?:[+ ]\\|$\\)" "\\|"
165 ;; Drawers (any type) and fixed-width areas. This is
166 ;; also only an indication.
169 "-\\{5,\\}[ \t]*$" "\\|"
170 ;; LaTeX environments.
171 "\\\\begin{\\([A-Za-z0-9]+\\*?\\)}" "\\|"
173 (regexp-quote org-clock-string
) "\\|"
175 (let ((term (case org-plain-list-ordered-item-terminator
176 (?\
) ")") (?.
"\\.") (otherwise "[.)]")))
177 (alpha (and org-list-allow-alphabetical
"\\|[A-Za-z]")))
178 (concat "\\(?:[-+*]\\|\\(?:[0-9]+" alpha
"\\)" term
"\\)"
179 "\\(?:[ \t]\\|$\\)"))
181 org-element--object-regexp
182 (mapconcat #'identity
183 (let ((link-types (regexp-opt org-link-types
)))
186 "\\(?:[_^][-{(*+.,[:alnum:]]\\)"
187 ;; Bold, code, italic, strike-through, underline
191 (nth 2 org-emphasis-regexp-components
)))
193 (concat "\\<" link-types
":")
194 ;; Objects starting with "[": regular link,
195 ;; footnote reference, statistics cookie,
196 ;; timestamp (inactive).
197 "\\[\\(?:fn:\\|\\(?:[0-9]\\|\\(?:%\\|/[0-9]*\\)\\]\\)\\|\\[\\)"
198 ;; Objects starting with "@": export snippets.
200 ;; Objects starting with "{": macro.
202 ;; Objects starting with "<" : timestamp
203 ;; (active, diary), target, radio target and
205 (concat "<\\(?:%%\\|<\\|[0-9]\\|" link-types
"\\)")
206 ;; Objects starting with "$": latex fragment.
208 ;; Objects starting with "\": line break,
209 ;; entity, latex fragment.
210 "\\\\\\(?:[a-zA-Z[(]\\|\\\\[ \t]*$\\)"
211 ;; Objects starting with raw text: inline Babel
212 ;; source block, inline Babel call.
213 "\\(?:call\\|src\\)_"))
216 (org-element--set-regexps)
219 (defun org-element-update-syntax ()
220 "Update parser internals."
222 (org-element--set-regexps)
223 (org-element-cache-reset 'all
))
225 (defconst org-element-all-elements
226 '(babel-call center-block clock comment comment-block diary-sexp drawer
227 dynamic-block example-block export-block fixed-width
228 footnote-definition headline horizontal-rule inlinetask item
229 keyword latex-environment node-property paragraph plain-list
230 planning property-drawer quote-block section
231 special-block src-block table table-row verse-block
)
232 "Complete list of element types.")
234 (defconst org-element-greater-elements
235 '(center-block drawer dynamic-block footnote-definition headline inlinetask
236 item plain-list property-drawer quote-block section
238 "List of recursive element types aka Greater Elements.")
240 (defconst org-element-all-objects
241 '(bold code entity export-snippet footnote-reference inline-babel-call
242 inline-src-block italic line-break latex-fragment link macro
243 radio-target statistics-cookie strike-through subscript superscript
244 table-cell target timestamp underline verbatim
)
245 "Complete list of object types.")
247 (defconst org-element-recursive-objects
248 '(bold footnote-reference italic link subscript radio-target strike-through
249 superscript table-cell underline
)
250 "List of recursive object types.")
252 (defvar org-element-block-name-alist
253 '(("CENTER" . org-element-center-block-parser
)
254 ("COMMENT" . org-element-comment-block-parser
)
255 ("EXAMPLE" . org-element-example-block-parser
)
256 ("QUOTE" . org-element-quote-block-parser
)
257 ("SRC" . org-element-src-block-parser
)
258 ("VERSE" . org-element-verse-block-parser
))
259 "Alist between block names and the associated parsing function.
260 Names must be uppercase. Any block whose name has no association
261 is parsed with `org-element-special-block-parser'.")
263 (defconst org-element-link-type-is-file
264 '("file" "file+emacs" "file+sys" "docview")
265 "List of link types equivalent to \"file\".
266 Only these types can accept search options and an explicit
267 application to open them.")
269 (defconst org-element-affiliated-keywords
270 '("CAPTION" "DATA" "HEADER" "HEADERS" "LABEL" "NAME" "PLOT" "RESNAME" "RESULT"
271 "RESULTS" "SOURCE" "SRCNAME" "TBLNAME")
272 "List of affiliated keywords as strings.
273 By default, all keywords setting attributes (e.g., \"ATTR_LATEX\")
274 are affiliated keywords and need not to be in this list.")
276 (defconst org-element-keyword-translation-alist
277 '(("DATA" .
"NAME") ("LABEL" .
"NAME") ("RESNAME" .
"NAME")
278 ("SOURCE" .
"NAME") ("SRCNAME" .
"NAME") ("TBLNAME" .
"NAME")
279 ("RESULT" .
"RESULTS") ("HEADERS" .
"HEADER"))
280 "Alist of usual translations for keywords.
281 The key is the old name and the value the new one. The property
282 holding their value will be named after the translated name.")
284 (defconst org-element-multiple-keywords
'("CAPTION" "HEADER")
285 "List of affiliated keywords that can occur more than once in an element.
287 Their value will be consed into a list of strings, which will be
288 returned as the value of the property.
290 This list is checked after translations have been applied. See
291 `org-element-keyword-translation-alist'.
293 By default, all keywords setting attributes (e.g., \"ATTR_LATEX\")
294 allow multiple occurrences and need not to be in this list.")
296 (defconst org-element-parsed-keywords
'("CAPTION")
297 "List of affiliated keywords whose value can be parsed.
299 Their value will be stored as a secondary string: a list of
302 This list is checked after translations have been applied. See
303 `org-element-keyword-translation-alist'.")
305 (defconst org-element--parsed-properties-alist
306 (mapcar (lambda (k) (cons k
(intern (concat ":" (downcase k
)))))
307 org-element-parsed-keywords
)
308 "Alist of parsed keywords and associated properties.
309 This is generated from `org-element-parsed-keywords', which
312 (defconst org-element-dual-keywords
'("CAPTION" "RESULTS")
313 "List of affiliated keywords which can have a secondary value.
315 In Org syntax, they can be written with optional square brackets
316 before the colons. For example, RESULTS keyword can be
317 associated to a hash value with the following:
319 #+RESULTS[hash-string]: some-source
321 This list is checked after translations have been applied. See
322 `org-element-keyword-translation-alist'.")
324 (defconst org-element-document-properties
'("AUTHOR" "DATE" "TITLE")
325 "List of properties associated to the whole document.
326 Any keyword in this list will have its value parsed and stored as
327 a secondary string.")
329 (defconst org-element--affiliated-re
330 (format "[ \t]*#\\+\\(?:%s\\):\\(?: \\|$\\)"
332 ;; Dual affiliated keywords.
333 (format "\\(?1:%s\\)\\(?:\\[\\(.*\\)\\]\\)?"
334 (regexp-opt org-element-dual-keywords
))
336 ;; Regular affiliated keywords.
337 (format "\\(?1:%s\\)"
341 (member keyword org-element-dual-keywords
))
342 org-element-affiliated-keywords
)))
344 ;; Export attributes.
345 "\\(?1:ATTR_[-_A-Za-z0-9]+\\)"))
346 "Regexp matching any affiliated keyword.
348 Keyword name is put in match group 1. Moreover, if keyword
349 belongs to `org-element-dual-keywords', put the dual value in
352 Don't modify it, set `org-element-affiliated-keywords' instead.")
354 (defconst org-element-object-restrictions
355 (let* ((standard-set (remq 'table-cell org-element-all-objects
))
356 (standard-set-no-line-break (remq 'line-break standard-set
)))
357 `((bold ,@standard-set
)
358 (footnote-reference ,@standard-set
)
359 (headline ,@standard-set-no-line-break
)
360 (inlinetask ,@standard-set-no-line-break
)
361 (italic ,@standard-set
)
362 (item ,@standard-set-no-line-break
)
363 (keyword ,@standard-set
)
364 ;; Ignore all links excepted plain links in a link description.
365 ;; Also ignore radio-targets and line breaks.
366 (link bold code entity export-snippet inline-babel-call inline-src-block
367 italic latex-fragment macro plain-link statistics-cookie
368 strike-through subscript superscript underline verbatim
)
369 (paragraph ,@standard-set
)
370 ;; Remove any variable object from radio target as it would
371 ;; prevent it from being properly recognized.
372 (radio-target bold code entity italic latex-fragment strike-through
373 subscript superscript underline superscript
)
374 (strike-through ,@standard-set
)
375 (subscript ,@standard-set
)
376 (superscript ,@standard-set
)
377 ;; Ignore inline babel call and inline src block as formulas are
378 ;; possible. Also ignore line breaks and statistics cookies.
379 (table-cell bold code entity export-snippet footnote-reference italic
380 latex-fragment link macro radio-target strike-through
381 subscript superscript target timestamp underline verbatim
)
382 (table-row table-cell
)
383 (underline ,@standard-set
)
384 (verse-block ,@standard-set
)))
385 "Alist of objects restrictions.
387 key is an element or object type containing objects and value is
388 a list of types that can be contained within an element or object
391 For example, in a `radio-target' object, one can only find
392 entities, latex-fragments, subscript, superscript and text
395 This alist also applies to secondary string. For example, an
396 `headline' type element doesn't directly contain objects, but
397 still has an entry since one of its properties (`:title') does.")
399 (defconst org-element-secondary-value-alist
400 '((headline .
:title
)
401 (inlinetask .
:title
)
403 "Alist between element types and location of secondary value.")
407 ;;; Accessors and Setters
409 ;; Provide four accessors: `org-element-type', `org-element-property'
410 ;; `org-element-contents' and `org-element-restriction'.
412 ;; Setter functions allow to modify elements by side effect. There is
413 ;; `org-element-put-property', `org-element-set-contents'. These
414 ;; low-level functions are useful to build a parse tree.
416 ;; `org-element-adopt-element', `org-element-set-element',
417 ;; `org-element-extract-element' and `org-element-insert-before' are
418 ;; high-level functions useful to modify a parse tree.
420 ;; `org-element-secondary-p' is a predicate used to know if a given
421 ;; object belongs to a secondary string.
423 (defsubst org-element-type
(element)
424 "Return type of ELEMENT.
426 The function returns the type of the element or object provided.
427 It can also return the following special value:
428 `plain-text' for a string
429 `org-data' for a complete document
430 nil in any other case."
432 ((not (consp element
)) (and (stringp element
) 'plain-text
))
433 ((symbolp (car element
)) (car element
))))
435 (defsubst org-element-property
(property element
)
436 "Extract the value from the PROPERTY of an ELEMENT."
437 (if (stringp element
) (get-text-property 0 property element
)
438 (plist-get (nth 1 element
) property
)))
440 (defsubst org-element-contents
(element)
441 "Extract contents from an ELEMENT."
442 (cond ((not (consp element
)) nil
)
443 ((symbolp (car element
)) (nthcdr 2 element
))
446 (defsubst org-element-restriction
(element)
447 "Return restriction associated to ELEMENT.
448 ELEMENT can be an element, an object or a symbol representing an
449 element or object type."
450 (cdr (assq (if (symbolp element
) element
(org-element-type element
))
451 org-element-object-restrictions
)))
453 (defsubst org-element-put-property
(element property value
)
454 "In ELEMENT set PROPERTY to VALUE.
455 Return modified element."
456 (if (stringp element
) (org-add-props element nil property value
)
457 (setcar (cdr element
) (plist-put (nth 1 element
) property value
))
460 (defsubst org-element-set-contents
(element &rest contents
)
461 "Set ELEMENT contents to CONTENTS.
462 Return modified element."
463 (cond ((not element
) (list contents
))
464 ((not (symbolp (car element
))) contents
)
465 ((cdr element
) (setcdr (cdr element
) contents
))
466 (t (nconc element contents
))))
468 (defun org-element-secondary-p (object)
469 "Non-nil when OBJECT belongs to a secondary string.
470 Return value is the property name, as a keyword, or nil."
471 (let* ((parent (org-element-property :parent object
))
472 (property (cdr (assq (org-element-type parent
)
473 org-element-secondary-value-alist
))))
475 (memq object
(org-element-property property parent
))
478 (defsubst org-element-adopt-elements
(parent &rest children
)
479 "Append elements to the contents of another element.
481 PARENT is an element or object. CHILDREN can be elements,
482 objects, or a strings.
484 The function takes care of setting `:parent' property for CHILD.
485 Return parent element."
486 ;; Link every child to PARENT. If PARENT is nil, it is a secondary
487 ;; string: parent is the list itself.
488 (mapc (lambda (child)
489 (org-element-put-property child
:parent
(or parent children
)))
491 ;; Add CHILDREN at the end of PARENT contents.
493 (apply 'org-element-set-contents
495 (nconc (org-element-contents parent
) children
)))
496 ;; Return modified PARENT element.
497 (or parent children
))
499 (defun org-element-extract-element (element)
500 "Extract ELEMENT from parse tree.
501 Remove element from the parse tree by side-effect, and return it
502 with its `:parent' property stripped out."
503 (let ((parent (org-element-property :parent element
))
504 (secondary (org-element-secondary-p element
)))
506 (org-element-put-property
508 (delq element
(org-element-property secondary parent
)))
509 (apply #'org-element-set-contents
511 (delq element
(org-element-contents parent
))))
512 ;; Return ELEMENT with its :parent removed.
513 (org-element-put-property element
:parent nil
)))
515 (defun org-element-insert-before (element location
)
516 "Insert ELEMENT before LOCATION in parse tree.
517 LOCATION is an element, object or string within the parse tree.
518 Parse tree is modified by side effect."
519 (let* ((parent (org-element-property :parent location
))
520 (property (org-element-secondary-p location
))
521 (siblings (if property
(org-element-property property parent
)
522 (org-element-contents parent
)))
523 ;; Special case: LOCATION is the first element of an
524 ;; independent secondary string (e.g. :title property). Add
526 (specialp (and (not property
)
528 (eq (car parent
) location
))))
529 ;; Install ELEMENT at the appropriate POSITION within SIBLINGS.
531 ((or (null siblings
) (eq (car siblings
) location
))
532 (push element siblings
))
533 ((null location
) (nconc siblings
(list element
)))
534 (t (let ((previous (cadr (memq location
(reverse siblings
)))))
536 (error "No location found to insert element")
537 (let ((next (memq previous siblings
)))
538 (setcdr next
(cons element
(cdr next
))))))))
539 ;; Store SIBLINGS at appropriate place in parse tree.
541 (specialp (setcdr parent
(copy-sequence parent
)) (setcar parent element
))
542 (property (org-element-put-property parent property siblings
))
543 (t (apply #'org-element-set-contents parent siblings
)))
544 ;; Set appropriate :parent property.
545 (org-element-put-property element
:parent parent
)))
547 (defun org-element-set-element (old new
)
548 "Replace element or object OLD with element or object NEW.
549 The function takes care of setting `:parent' property for NEW."
550 ;; Ensure OLD and NEW have the same parent.
551 (org-element-put-property new
:parent
(org-element-property :parent old
))
552 (if (or (memq (org-element-type old
) '(plain-text nil
))
553 (memq (org-element-type new
) '(plain-text nil
)))
554 ;; We cannot replace OLD with NEW since one of them is not an
555 ;; object or element. We take the long path.
556 (progn (org-element-insert-before new old
)
557 (org-element-extract-element old
))
558 ;; Since OLD is going to be changed into NEW by side-effect, first
559 ;; make sure that every element or object within NEW has OLD as
561 (dolist (blob (org-element-contents new
))
562 (org-element-put-property blob
:parent old
))
563 ;; Transfer contents.
564 (apply #'org-element-set-contents old
(org-element-contents new
))
565 ;; Overwrite OLD's properties with NEW's.
566 (setcar (cdr old
) (nth 1 new
))
568 (setcar old
(car new
))))
574 ;; For each greater element type, we define a parser and an
577 ;; A parser returns the element or object as the list described above.
578 ;; Most of them accepts no argument. Though, exceptions exist. Hence
579 ;; every element containing a secondary string (see
580 ;; `org-element-secondary-value-alist') will accept an optional
581 ;; argument to toggle parsing of that secondary string. Moreover,
582 ;; `item' parser requires current list's structure as its first
585 ;; An interpreter accepts two arguments: the list representation of
586 ;; the element or object, and its contents. The latter may be nil,
587 ;; depending on the element or object considered. It returns the
588 ;; appropriate Org syntax, as a string.
590 ;; Parsing functions must follow the naming convention:
591 ;; org-element-TYPE-parser, where TYPE is greater element's type, as
592 ;; defined in `org-element-greater-elements'.
594 ;; Similarly, interpreting functions must follow the naming
595 ;; convention: org-element-TYPE-interpreter.
597 ;; With the exception of `headline' and `item' types, greater elements
598 ;; cannot contain other greater elements of their own type.
600 ;; Beside implementing a parser and an interpreter, adding a new
601 ;; greater element requires to tweak `org-element--current-element'.
602 ;; Moreover, the newly defined type must be added to both
603 ;; `org-element-all-elements' and `org-element-greater-elements'.
608 (defun org-element-center-block-parser (limit affiliated
)
609 "Parse a center block.
611 LIMIT bounds the search. AFFILIATED is a list of which CAR is
612 the buffer position at the beginning of the first affiliated
613 keyword and CDR is a plist of affiliated keywords along with
616 Return a list whose CAR is `center-block' and CDR is a plist
617 containing `:begin', `:end', `:contents-begin', `:contents-end',
618 `:post-blank' and `:post-affiliated' keywords.
620 Assume point is at the beginning of the block."
621 (let ((case-fold-search t
))
622 (if (not (save-excursion
623 (re-search-forward "^[ \t]*#\\+END_CENTER[ \t]*$" limit t
)))
624 ;; Incomplete block: parse it as a paragraph.
625 (org-element-paragraph-parser limit affiliated
)
626 (let ((block-end-line (match-beginning 0)))
627 (let* ((begin (car affiliated
))
628 (post-affiliated (point))
629 ;; Empty blocks have no contents.
630 (contents-begin (progn (forward-line)
631 (and (< (point) block-end-line
)
633 (contents-end (and contents-begin block-end-line
))
634 (pos-before-blank (progn (goto-char block-end-line
)
638 (skip-chars-forward " \r\t\n" limit
)
639 (if (eobp) (point) (line-beginning-position)))))
644 :contents-begin contents-begin
645 :contents-end contents-end
646 :post-blank
(count-lines pos-before-blank end
)
647 :post-affiliated post-affiliated
)
648 (cdr affiliated
))))))))
650 (defun org-element-center-block-interpreter (center-block contents
)
651 "Interpret CENTER-BLOCK element as Org syntax.
652 CONTENTS is the contents of the element."
653 (format "#+BEGIN_CENTER\n%s#+END_CENTER" contents
))
658 (defun org-element-drawer-parser (limit affiliated
)
661 LIMIT bounds the search. AFFILIATED is a list of which CAR is
662 the buffer position at the beginning of the first affiliated
663 keyword and CDR is a plist of affiliated keywords along with
666 Return a list whose CAR is `drawer' and CDR is a plist containing
667 `:drawer-name', `:begin', `:end', `:contents-begin',
668 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
670 Assume point is at beginning of drawer."
671 (let ((case-fold-search t
))
672 (if (not (save-excursion (re-search-forward "^[ \t]*:END:[ \t]*$" limit t
)))
673 ;; Incomplete drawer: parse it as a paragraph.
674 (org-element-paragraph-parser limit affiliated
)
676 (let* ((drawer-end-line (match-beginning 0))
677 (name (progn (looking-at org-drawer-regexp
)
678 (org-match-string-no-properties 1)))
679 (begin (car affiliated
))
680 (post-affiliated (point))
681 ;; Empty drawers have no contents.
682 (contents-begin (progn (forward-line)
683 (and (< (point) drawer-end-line
)
685 (contents-end (and contents-begin drawer-end-line
))
686 (pos-before-blank (progn (goto-char drawer-end-line
)
689 (end (progn (skip-chars-forward " \r\t\n" limit
)
690 (if (eobp) (point) (line-beginning-position)))))
696 :contents-begin contents-begin
697 :contents-end contents-end
698 :post-blank
(count-lines pos-before-blank end
)
699 :post-affiliated post-affiliated
)
700 (cdr affiliated
))))))))
702 (defun org-element-drawer-interpreter (drawer contents
)
703 "Interpret DRAWER element as Org syntax.
704 CONTENTS is the contents of the element."
705 (format ":%s:\n%s:END:"
706 (org-element-property :drawer-name drawer
)
712 (defun org-element-dynamic-block-parser (limit affiliated
)
713 "Parse a dynamic block.
715 LIMIT bounds the search. AFFILIATED is a list of which CAR is
716 the buffer position at the beginning of the first affiliated
717 keyword and CDR is a plist of affiliated keywords along with
720 Return a list whose CAR is `dynamic-block' and CDR is a plist
721 containing `:block-name', `:begin', `:end', `:contents-begin',
722 `:contents-end', `:arguments', `:post-blank' and
723 `:post-affiliated' keywords.
725 Assume point is at beginning of dynamic block."
726 (let ((case-fold-search t
))
727 (if (not (save-excursion
728 (re-search-forward "^[ \t]*#\\+END:?[ \t]*$" limit t
)))
729 ;; Incomplete block: parse it as a paragraph.
730 (org-element-paragraph-parser limit affiliated
)
731 (let ((block-end-line (match-beginning 0)))
733 (let* ((name (progn (looking-at org-dblock-start-re
)
734 (org-match-string-no-properties 1)))
735 (arguments (org-match-string-no-properties 3))
736 (begin (car affiliated
))
737 (post-affiliated (point))
738 ;; Empty blocks have no contents.
739 (contents-begin (progn (forward-line)
740 (and (< (point) block-end-line
)
742 (contents-end (and contents-begin block-end-line
))
743 (pos-before-blank (progn (goto-char block-end-line
)
746 (end (progn (skip-chars-forward " \r\t\n" limit
)
747 (if (eobp) (point) (line-beginning-position)))))
754 :contents-begin contents-begin
755 :contents-end contents-end
756 :post-blank
(count-lines pos-before-blank end
)
757 :post-affiliated post-affiliated
)
758 (cdr affiliated
)))))))))
760 (defun org-element-dynamic-block-interpreter (dynamic-block contents
)
761 "Interpret DYNAMIC-BLOCK element as Org syntax.
762 CONTENTS is the contents of the element."
763 (format "#+BEGIN: %s%s\n%s#+END:"
764 (org-element-property :block-name dynamic-block
)
765 (let ((args (org-element-property :arguments dynamic-block
)))
766 (and args
(concat " " args
)))
770 ;;;; Footnote Definition
772 (defun org-element-footnote-definition-parser (limit affiliated
)
773 "Parse a footnote definition.
775 LIMIT bounds the search. AFFILIATED is a list of which CAR is
776 the buffer position at the beginning of the first affiliated
777 keyword and CDR is a plist of affiliated keywords along with
780 Return a list whose CAR is `footnote-definition' and CDR is
781 a plist containing `:label', `:begin' `:end', `:contents-begin',
782 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
784 Assume point is at the beginning of the footnote definition."
786 (let* ((label (progn (looking-at org-footnote-definition-re
)
787 (org-match-string-no-properties 1)))
788 (begin (car affiliated
))
789 (post-affiliated (point))
790 (ending (save-excursion
794 (concat org-outline-regexp-bol
"\\|"
795 org-footnote-definition-re
"\\|"
796 "^\\([ \t]*\n\\)\\{2,\\}") limit
'move
))
799 (contents-begin (progn
801 (skip-chars-forward " \r\t\n" ending
)
802 (cond ((= (point) ending
) nil
)
803 ((= (line-beginning-position) begin
) (point))
804 (t (line-beginning-position)))))
805 (contents-end (and contents-begin ending
))
806 (end (progn (goto-char ending
)
807 (skip-chars-forward " \r\t\n" limit
)
808 (if (eobp) (point) (line-beginning-position)))))
809 (list 'footnote-definition
814 :contents-begin contents-begin
815 :contents-end contents-end
816 :post-blank
(count-lines ending end
)
817 :post-affiliated post-affiliated
)
818 (cdr affiliated
))))))
820 (defun org-element-footnote-definition-interpreter (footnote-definition contents
)
821 "Interpret FOOTNOTE-DEFINITION element as Org syntax.
822 CONTENTS is the contents of the footnote-definition."
823 (concat (format "[%s]" (org-element-property :label footnote-definition
))
830 (defun org-element--get-node-properties ()
831 "Return node properties associated to headline at point.
832 Upcase property names. It avoids confusion between properties
833 obtained through property drawer and default properties from the
834 parser (e.g. `:end' and :END:). Return value is a plist."
837 (when (org-looking-at-p org-planning-line-re
) (forward-line))
838 (when (looking-at org-property-drawer-re
)
840 (let ((end (match-end 0)) properties
)
841 (while (< (line-end-position) end
)
842 (looking-at org-property-re
)
843 (push (org-match-string-no-properties 3) properties
)
844 (push (intern (concat ":" (upcase (match-string 2)))) properties
)
848 (defun org-element--get-time-properties ()
849 "Return time properties associated to headline at point.
850 Return value is a plist."
852 (when (progn (forward-line) (looking-at org-planning-line-re
))
853 (let ((end (line-end-position)) plist
)
854 (while (re-search-forward org-keyword-time-not-clock-regexp end t
)
855 (goto-char (match-end 1))
856 (skip-chars-forward " \t")
857 (let ((keyword (match-string 1))
858 (time (org-element-timestamp-parser)))
859 (cond ((equal keyword org-scheduled-string
)
860 (setq plist
(plist-put plist
:scheduled time
)))
861 ((equal keyword org-deadline-string
)
862 (setq plist
(plist-put plist
:deadline time
)))
863 (t (setq plist
(plist-put plist
:closed time
))))))
866 (defun org-element-headline-parser (limit &optional raw-secondary-p
)
869 Return a list whose CAR is `headline' and CDR is a plist
870 containing `:raw-value', `:title', `:alt-title', `:begin',
871 `:end', `:pre-blank', `:contents-begin' and `:contents-end',
872 `:level', `:priority', `:tags', `:todo-keyword',`:todo-type',
873 `:scheduled', `:deadline', `:closed', `:archivedp', `:commentedp'
874 `:footnote-section-p', `:post-blank' and `:post-affiliated'
877 The plist also contains any property set in the property drawer,
878 with its name in upper cases and colons added at the
879 beginning (e.g., `:CUSTOM_ID').
881 LIMIT is a buffer position bounding the search.
883 When RAW-SECONDARY-P is non-nil, headline's title will not be
884 parsed as a secondary string, but as a plain string instead.
886 Assume point is at beginning of the headline."
888 (let* ((components (org-heading-components))
889 (level (nth 1 components
))
890 (todo (nth 2 components
))
892 (and todo
(if (member todo org-done-keywords
) 'done
'todo
)))
893 (tags (let ((raw-tags (nth 5 components
)))
894 (and raw-tags
(org-split-string raw-tags
":"))))
895 (raw-value (or (nth 4 components
) ""))
897 (let ((case-fold-search nil
))
898 (string-match (format "^%s\\( \\|$\\)" org-comment-string
)
900 (archivedp (member org-archive-tag tags
))
901 (footnote-section-p (and org-footnote-section
902 (string= org-footnote-section raw-value
)))
903 (standard-props (org-element--get-node-properties))
904 (time-props (org-element--get-time-properties))
906 (end (min (save-excursion (org-end-of-subtree t t
)) limit
))
907 (pos-after-head (progn (forward-line) (point)))
908 (contents-begin (save-excursion
909 (skip-chars-forward " \r\t\n" end
)
910 (and (/= (point) end
) (line-beginning-position))))
911 (contents-end (and contents-begin
912 (progn (goto-char end
)
913 (skip-chars-backward " \r\t\n")
916 ;; Clean RAW-VALUE from any comment string.
918 (let ((case-fold-search nil
))
920 (replace-regexp-in-string
921 (concat (regexp-quote org-comment-string
) "\\(?: \\|$\\)")
924 ;; Clean TAGS from archive tag, if any.
925 (when archivedp
(setq tags
(delete org-archive-tag tags
)))
929 (list :raw-value raw-value
933 (if (not contents-begin
) 0
934 (count-lines pos-after-head contents-begin
))
935 :contents-begin contents-begin
936 :contents-end contents-end
938 :priority
(nth 3 components
)
942 :post-blank
(count-lines
943 (or contents-end pos-after-head
)
945 :footnote-section-p footnote-section-p
947 :commentedp commentedp
948 :post-affiliated begin
)
951 (let ((alt-title (org-element-property :ALT_TITLE headline
)))
953 (org-element-put-property
955 (if raw-secondary-p alt-title
956 (org-element-parse-secondary-string
957 alt-title
(org-element-restriction 'headline
) headline
)))))
958 (org-element-put-property
960 (if raw-secondary-p raw-value
961 (org-element-parse-secondary-string
962 raw-value
(org-element-restriction 'headline
) headline
)))))))
964 (defun org-element-headline-interpreter (headline contents
)
965 "Interpret HEADLINE element as Org syntax.
966 CONTENTS is the contents of the element."
967 (let* ((level (org-element-property :level headline
))
968 (todo (org-element-property :todo-keyword headline
))
969 (priority (org-element-property :priority headline
))
970 (title (org-element-interpret-data
971 (org-element-property :title headline
)))
972 (tags (let ((tag-list (if (org-element-property :archivedp headline
)
973 (cons org-archive-tag
974 (org-element-property :tags headline
))
975 (org-element-property :tags headline
))))
977 (format ":%s:" (mapconcat #'identity tag-list
":")))))
978 (commentedp (org-element-property :commentedp headline
))
979 (pre-blank (or (org-element-property :pre-blank headline
) 0))
981 (concat (make-string (if org-odd-levels-only
(1- (* level
2)) level
)
983 (and todo
(concat " " todo
))
984 (and commentedp
(concat " " org-comment-string
))
985 (and priority
(format " [#%s]" (char-to-string priority
)))
987 (if (and org-footnote-section
988 (org-element-property :footnote-section-p headline
))
996 ((zerop org-tags-column
) (format " %s" tags
))
997 ((< org-tags-column
0)
1000 (max (- (+ org-tags-column
(length heading
) (length tags
))) 1)
1005 (make-string (max (- org-tags-column
(length heading
)) 1) ?\s
)
1007 (make-string (1+ pre-blank
) ?
\n)
1013 (defun org-element-inlinetask-parser (limit &optional raw-secondary-p
)
1014 "Parse an inline task.
1016 Return a list whose CAR is `inlinetask' and CDR is a plist
1017 containing `:title', `:begin', `:end', `:contents-begin' and
1018 `:contents-end', `:level', `:priority', `:raw-value', `:tags',
1019 `:todo-keyword', `:todo-type', `:scheduled', `:deadline',
1020 `:closed', `:post-blank' and `:post-affiliated' keywords.
1022 The plist also contains any property set in the property drawer,
1023 with its name in upper cases and colons added at the
1024 beginning (e.g., `:CUSTOM_ID').
1026 When optional argument RAW-SECONDARY-P is non-nil, inline-task's
1027 title will not be parsed as a secondary string, but as a plain
1030 Assume point is at beginning of the inline task."
1032 (let* ((begin (point))
1033 (components (org-heading-components))
1034 (todo (nth 2 components
))
1035 (todo-type (and todo
1036 (if (member todo org-done-keywords
) 'done
'todo
)))
1037 (tags (let ((raw-tags (nth 5 components
)))
1038 (and raw-tags
(org-split-string raw-tags
":"))))
1039 (raw-value (or (nth 4 components
) ""))
1040 (task-end (save-excursion
1042 (and (re-search-forward org-outline-regexp-bol limit t
)
1043 (org-looking-at-p "END[ \t]*$")
1044 (line-beginning-position))))
1045 (standard-props (and task-end
(org-element--get-node-properties)))
1046 (time-props (and task-end
(org-element--get-time-properties)))
1047 (contents-begin (progn (forward-line)
1048 (and task-end
(< (point) task-end
) (point))))
1049 (contents-end (and contents-begin task-end
))
1050 (before-blank (if (not task-end
) (point)
1051 (goto-char task-end
)
1054 (end (progn (skip-chars-forward " \r\t\n" limit
)
1055 (if (eobp) (point) (line-beginning-position))))
1059 (list :raw-value raw-value
1062 :contents-begin contents-begin
1063 :contents-end contents-end
1064 :level
(nth 1 components
)
1065 :priority
(nth 3 components
)
1068 :todo-type todo-type
1069 :post-blank
(count-lines before-blank end
)
1070 :post-affiliated begin
)
1073 (org-element-put-property
1075 (if raw-secondary-p raw-value
1076 (org-element-parse-secondary-string
1078 (org-element-restriction 'inlinetask
)
1081 (defun org-element-inlinetask-interpreter (inlinetask contents
)
1082 "Interpret INLINETASK element as Org syntax.
1083 CONTENTS is the contents of inlinetask."
1084 (let* ((level (org-element-property :level inlinetask
))
1085 (todo (org-element-property :todo-keyword inlinetask
))
1086 (priority (org-element-property :priority inlinetask
))
1087 (title (org-element-interpret-data
1088 (org-element-property :title inlinetask
)))
1089 (tags (let ((tag-list (org-element-property :tags inlinetask
)))
1091 (format ":%s:" (mapconcat 'identity tag-list
":")))))
1092 (task (concat (make-string level ?
*)
1093 (and todo
(concat " " todo
))
1095 (format " [#%s]" (char-to-string priority
)))
1096 (and title
(concat " " title
)))))
1101 ((zerop org-tags-column
) (format " %s" tags
))
1102 ((< org-tags-column
0)
1105 (max (- (+ org-tags-column
(length task
) (length tags
))) 1)
1110 (make-string (max (- org-tags-column
(length task
)) 1) ?
)
1112 ;; Prefer degenerate inlinetasks when there are no
1117 (make-string level ?
*) " END")))))
1122 (defun org-element-item-parser (limit struct
&optional raw-secondary-p
)
1125 STRUCT is the structure of the plain list.
1127 Return a list whose CAR is `item' and CDR is a plist containing
1128 `:bullet', `:begin', `:end', `:contents-begin', `:contents-end',
1129 `:checkbox', `:counter', `:tag', `:structure', `:post-blank' and
1130 `:post-affiliated' keywords.
1132 When optional argument RAW-SECONDARY-P is non-nil, item's tag, if
1133 any, will not be parsed as a secondary string, but as a plain
1136 Assume point is at the beginning of the item."
1139 (looking-at org-list-full-item-re
)
1140 (let* ((begin (point))
1141 (bullet (org-match-string-no-properties 1))
1142 (checkbox (let ((box (org-match-string-no-properties 3)))
1143 (cond ((equal "[ ]" box
) 'off
)
1144 ((equal "[X]" box
) 'on
)
1145 ((equal "[-]" box
) 'trans
))))
1146 (counter (let ((c (org-match-string-no-properties 2)))
1150 ((string-match "[A-Za-z]" c
)
1151 (- (string-to-char (upcase (match-string 0 c
)))
1153 ((string-match "[0-9]+" c
)
1154 (string-to-number (match-string 0 c
)))))))
1155 (end (progn (goto-char (nth 6 (assq (point) struct
)))
1156 (unless (bolp) (forward-line))
1160 ;; Ignore tags in un-ordered lists: they are just
1161 ;; a part of item's body.
1162 (if (and (match-beginning 4)
1163 (save-match-data (string-match "[.)]" bullet
)))
1166 (skip-chars-forward " \r\t\n" limit
)
1167 ;; If first line isn't empty, contents really start
1168 ;; at the text after item's meta-data.
1169 (if (= (point-at-bol) begin
) (point) (point-at-bol))))
1170 (contents-end (progn (goto-char end
)
1171 (skip-chars-backward " \r\t\n")
1176 (list :bullet bullet
1179 ;; CONTENTS-BEGIN and CONTENTS-END may be
1180 ;; mixed up in the case of an empty item
1181 ;; separated from the next by a blank line.
1182 ;; Thus ensure the former is always the
1184 :contents-begin
(min contents-begin contents-end
)
1185 :contents-end
(max contents-begin contents-end
)
1189 :post-blank
(count-lines contents-end end
)
1190 :post-affiliated begin
))))
1191 (org-element-put-property
1193 (let ((raw-tag (org-list-get-tag begin struct
)))
1195 (if raw-secondary-p raw-tag
1196 (org-element-parse-secondary-string
1197 raw-tag
(org-element-restriction 'item
) item
))))))))
1199 (defun org-element-item-interpreter (item contents
)
1200 "Interpret ITEM element as Org syntax.
1201 CONTENTS is the contents of the element."
1202 (let* ((bullet (let ((bullet (org-element-property :bullet item
)))
1203 (org-list-bullet-string
1204 (cond ((not (string-match "[0-9a-zA-Z]" bullet
)) "- ")
1205 ((eq org-plain-list-ordered-item-terminator ?\
)) "1)")
1207 (checkbox (org-element-property :checkbox item
))
1208 (counter (org-element-property :counter item
))
1209 (tag (let ((tag (org-element-property :tag item
)))
1210 (and tag
(org-element-interpret-data tag
))))
1211 ;; Compute indentation.
1212 (ind (make-string (length bullet
) 32))
1213 (item-starts-with-par-p
1214 (eq (org-element-type (car (org-element-contents item
)))
1219 (and counter
(format "[@%d] " counter
))
1224 (and tag
(format "%s :: " tag
))
1226 (let ((contents (replace-regexp-in-string
1227 "\\(^\\)[ \t]*\\S-" ind contents nil nil
1)))
1228 (if item-starts-with-par-p
(org-trim contents
)
1229 (concat "\n" contents
)))))))
1234 (defun org-element--list-struct (limit)
1235 ;; Return structure of list at point. Internal function. See
1236 ;; `org-list-struct' for details.
1237 (let ((case-fold-search t
)
1239 (item-re (org-item-re))
1240 (inlinetask-re (and (featurep 'org-inlinetask
) "^\\*+ "))
1246 ;; At limit: end all items.
1249 (let ((end (progn (skip-chars-backward " \r\t\n")
1252 (dolist (item items
(sort (nconc items struct
)
1253 'car-less-than-car
))
1254 (setcar (nthcdr 6 item
) end
)))))
1255 ;; At list end: end all items.
1256 ((looking-at org-list-end-re
)
1257 (throw 'exit
(dolist (item items
(sort (nconc items struct
)
1258 'car-less-than-car
))
1259 (setcar (nthcdr 6 item
) (point)))))
1260 ;; At a new item: end previous sibling.
1261 ((looking-at item-re
)
1262 (let ((ind (save-excursion (skip-chars-forward " \t")
1264 (setq top-ind
(min top-ind ind
))
1265 (while (and items
(<= ind
(nth 1 (car items
))))
1266 (let ((item (pop items
)))
1267 (setcar (nthcdr 6 item
) (point))
1268 (push item struct
)))
1269 (push (progn (looking-at org-list-full-item-re
)
1270 (let ((bullet (match-string-no-properties 1)))
1274 (match-string-no-properties 2) ; counter
1275 (match-string-no-properties 3) ; checkbox
1277 (and (save-match-data
1278 (string-match "[-+*]" bullet
))
1279 (match-string-no-properties 4))
1280 ;; Ending position, unknown so far.
1284 ;; Skip empty lines.
1285 ((looking-at "^[ \t]*$") (forward-line))
1286 ;; Skip inline tasks and blank lines along the way.
1287 ((and inlinetask-re
(looking-at inlinetask-re
))
1289 (let ((origin (point)))
1290 (when (re-search-forward inlinetask-re limit t
)
1291 (if (org-looking-at-p "END[ \t]*$") (forward-line)
1292 (goto-char origin
)))))
1293 ;; At some text line. Check if it ends any previous item.
1295 (let ((ind (progn (skip-chars-forward " \t") (current-column))))
1296 (when (<= ind top-ind
)
1297 (skip-chars-backward " \r\t\n")
1299 (while (<= ind
(nth 1 (car items
)))
1300 (let ((item (pop items
)))
1301 (setcar (nthcdr 6 item
) (line-beginning-position))
1304 (throw 'exit
(sort struct
'car-less-than-car
))))))
1305 ;; Skip blocks (any type) and drawers contents.
1307 ((and (looking-at "#\\+BEGIN\\(:\\|_\\S-+\\)")
1309 (format "^[ \t]*#\\+END%s[ \t]*$" (match-string 1))
1311 ((and (looking-at org-drawer-regexp
)
1312 (re-search-forward "^[ \t]*:END:[ \t]*$" limit t
))))
1313 (forward-line))))))))
1315 (defun org-element-plain-list-parser (limit affiliated structure
)
1316 "Parse a plain list.
1318 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1319 the buffer position at the beginning of the first affiliated
1320 keyword and CDR is a plist of affiliated keywords along with
1321 their value. STRUCTURE is the structure of the plain list being
1324 Return a list whose CAR is `plain-list' and CDR is a plist
1325 containing `:type', `:begin', `:end', `:contents-begin' and
1326 `:contents-end', `:structure', `:post-blank' and
1327 `:post-affiliated' keywords.
1329 Assume point is at the beginning of the list."
1331 (let* ((struct (or structure
(org-element--list-struct limit
)))
1332 (type (cond ((org-looking-at-p "[ \t]*[A-Za-z0-9]") 'ordered
)
1333 ((nth 5 (assq (point) struct
)) 'descriptive
)
1335 (contents-begin (point))
1336 (begin (car affiliated
))
1337 (contents-end (let* ((item (assq contents-begin struct
))
1340 (while (and (setq item
(assq pos struct
))
1341 (= (nth 1 item
) ind
))
1342 (setq pos
(nth 6 item
)))
1344 (end (progn (goto-char contents-end
)
1345 (skip-chars-forward " \r\t\n" limit
)
1346 (if (= (point) limit
) limit
(line-beginning-position)))))
1353 :contents-begin contents-begin
1354 :contents-end contents-end
1356 :post-blank
(count-lines contents-end end
)
1357 :post-affiliated contents-begin
)
1358 (cdr affiliated
))))))
1360 (defun org-element-plain-list-interpreter (plain-list contents
)
1361 "Interpret PLAIN-LIST element as Org syntax.
1362 CONTENTS is the contents of the element."
1365 (goto-char (point-min))
1370 ;;;; Property Drawer
1372 (defun org-element-property-drawer-parser (limit)
1373 "Parse a property drawer.
1375 LIMIT bounds the search.
1377 Return a list whose car is `property-drawer' and cdr is a plist
1378 containing `:begin', `:end', `:contents-begin', `:contents-end',
1379 `:post-blank' and `:post-affiliated' keywords.
1381 Assume point is at the beginning of the property drawer."
1383 (let ((case-fold-search t
)
1385 (contents-begin (line-beginning-position 2)))
1386 (re-search-forward "^[ \t]*:END:[ \t]*$" limit t
)
1387 (let ((contents-end (and (> (match-beginning 0) contents-begin
)
1388 (match-beginning 0)))
1389 (before-blank (progn (forward-line) (point)))
1390 (end (progn (skip-chars-forward " \r\t\n" limit
)
1391 (if (eobp) (point) (line-beginning-position)))))
1392 (list 'property-drawer
1395 :contents-begin
(and contents-end contents-begin
)
1396 :contents-end contents-end
1397 :post-blank
(count-lines before-blank end
)
1398 :post-affiliated begin
))))))
1400 (defun org-element-property-drawer-interpreter (property-drawer contents
)
1401 "Interpret PROPERTY-DRAWER element as Org syntax.
1402 CONTENTS is the properties within the drawer."
1403 (format ":PROPERTIES:\n%s:END:" contents
))
1408 (defun org-element-quote-block-parser (limit affiliated
)
1409 "Parse a quote block.
1411 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1412 the buffer position at the beginning of the first affiliated
1413 keyword and CDR is a plist of affiliated keywords along with
1416 Return a list whose CAR is `quote-block' and CDR is a plist
1417 containing `:begin', `:end', `:contents-begin', `:contents-end',
1418 `:post-blank' and `:post-affiliated' keywords.
1420 Assume point is at the beginning of the block."
1421 (let ((case-fold-search t
))
1422 (if (not (save-excursion
1423 (re-search-forward "^[ \t]*#\\+END_QUOTE[ \t]*$" limit t
)))
1424 ;; Incomplete block: parse it as a paragraph.
1425 (org-element-paragraph-parser limit affiliated
)
1426 (let ((block-end-line (match-beginning 0)))
1428 (let* ((begin (car affiliated
))
1429 (post-affiliated (point))
1430 ;; Empty blocks have no contents.
1431 (contents-begin (progn (forward-line)
1432 (and (< (point) block-end-line
)
1434 (contents-end (and contents-begin block-end-line
))
1435 (pos-before-blank (progn (goto-char block-end-line
)
1438 (end (progn (skip-chars-forward " \r\t\n" limit
)
1439 (if (eobp) (point) (line-beginning-position)))))
1444 :contents-begin contents-begin
1445 :contents-end contents-end
1446 :post-blank
(count-lines pos-before-blank end
)
1447 :post-affiliated post-affiliated
)
1448 (cdr affiliated
)))))))))
1450 (defun org-element-quote-block-interpreter (quote-block contents
)
1451 "Interpret QUOTE-BLOCK element as Org syntax.
1452 CONTENTS is the contents of the element."
1453 (format "#+BEGIN_QUOTE\n%s#+END_QUOTE" contents
))
1458 (defun org-element-section-parser (limit)
1461 LIMIT bounds the search.
1463 Return a list whose CAR is `section' and CDR is a plist
1464 containing `:begin', `:end', `:contents-begin', `contents-end',
1465 `:post-blank' and `:post-affiliated' keywords."
1467 ;; Beginning of section is the beginning of the first non-blank
1468 ;; line after previous headline.
1469 (let ((begin (point))
1470 (end (progn (org-with-limited-levels (outline-next-heading))
1472 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
1478 :contents-begin begin
1479 :contents-end pos-before-blank
1480 :post-blank
(count-lines pos-before-blank end
)
1481 :post-affiliated begin
)))))
1483 (defun org-element-section-interpreter (section contents
)
1484 "Interpret SECTION element as Org syntax.
1485 CONTENTS is the contents of the element."
1491 (defun org-element-special-block-parser (limit affiliated
)
1492 "Parse a special block.
1494 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1495 the buffer position at the beginning of the first affiliated
1496 keyword and CDR is a plist of affiliated keywords along with
1499 Return a list whose CAR is `special-block' and CDR is a plist
1500 containing `:type', `:begin', `:end', `:contents-begin',
1501 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
1503 Assume point is at the beginning of the block."
1504 (let* ((case-fold-search t
)
1505 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
1506 (match-string-no-properties 1))))
1507 (if (not (save-excursion
1509 (format "^[ \t]*#\\+END_%s[ \t]*$" (regexp-quote type
))
1511 ;; Incomplete block: parse it as a paragraph.
1512 (org-element-paragraph-parser limit affiliated
)
1513 (let ((block-end-line (match-beginning 0)))
1515 (let* ((begin (car affiliated
))
1516 (post-affiliated (point))
1517 ;; Empty blocks have no contents.
1518 (contents-begin (progn (forward-line)
1519 (and (< (point) block-end-line
)
1521 (contents-end (and contents-begin block-end-line
))
1522 (pos-before-blank (progn (goto-char block-end-line
)
1525 (end (progn (skip-chars-forward " \r\t\n" limit
)
1526 (if (eobp) (point) (line-beginning-position)))))
1527 (list 'special-block
1532 :contents-begin contents-begin
1533 :contents-end contents-end
1534 :post-blank
(count-lines pos-before-blank end
)
1535 :post-affiliated post-affiliated
)
1536 (cdr affiliated
)))))))))
1538 (defun org-element-special-block-interpreter (special-block contents
)
1539 "Interpret SPECIAL-BLOCK element as Org syntax.
1540 CONTENTS is the contents of the element."
1541 (let ((block-type (org-element-property :type special-block
)))
1542 (format "#+BEGIN_%s\n%s#+END_%s" block-type contents block-type
)))
1548 ;; For each element, a parser and an interpreter are also defined.
1549 ;; Both follow the same naming convention used for greater elements.
1551 ;; Also, as for greater elements, adding a new element type is done
1552 ;; through the following steps: implement a parser and an interpreter,
1553 ;; tweak `org-element--current-element' so that it recognizes the new
1554 ;; type and add that new type to `org-element-all-elements'.
1556 ;; As a special case, when the newly defined type is a block type,
1557 ;; `org-element-block-name-alist' has to be modified accordingly.
1562 (defun org-element-babel-call-parser (limit affiliated
)
1563 "Parse a babel call.
1565 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1566 the buffer position at the beginning of the first affiliated
1567 keyword and CDR is a plist of affiliated keywords along with
1570 Return a list whose CAR is `babel-call' and CDR is a plist
1571 containing `:begin', `:end', `:value', `:post-blank' and
1572 `:post-affiliated' as keywords."
1574 (let ((begin (car affiliated
))
1575 (post-affiliated (point))
1576 (value (progn (let ((case-fold-search t
))
1577 (re-search-forward "call:[ \t]*" nil t
))
1578 (buffer-substring-no-properties (point)
1579 (line-end-position))))
1580 (pos-before-blank (progn (forward-line) (point)))
1581 (end (progn (skip-chars-forward " \r\t\n" limit
)
1582 (if (eobp) (point) (line-beginning-position)))))
1588 :post-blank
(count-lines pos-before-blank end
)
1589 :post-affiliated post-affiliated
)
1590 (cdr affiliated
))))))
1592 (defun org-element-babel-call-interpreter (babel-call contents
)
1593 "Interpret BABEL-CALL element as Org syntax.
1595 (concat "#+CALL: " (org-element-property :value babel-call
)))
1600 (defun org-element-clock-parser (limit)
1603 LIMIT bounds the search.
1605 Return a list whose CAR is `clock' and CDR is a plist containing
1606 `:status', `:value', `:time', `:begin', `:end', `:post-blank' and
1607 `:post-affiliated' as keywords."
1609 (let* ((case-fold-search nil
)
1611 (value (progn (search-forward org-clock-string
(line-end-position) t
)
1612 (skip-chars-forward " \t")
1613 (org-element-timestamp-parser)))
1614 (duration (and (search-forward " => " (line-end-position) t
)
1615 (progn (skip-chars-forward " \t")
1616 (looking-at "\\(\\S-+\\)[ \t]*$"))
1617 (org-match-string-no-properties 1)))
1618 (status (if duration
'closed
'running
))
1619 (post-blank (let ((before-blank (progn (forward-line) (point))))
1620 (skip-chars-forward " \r\t\n" limit
)
1621 (skip-chars-backward " \t")
1622 (unless (bolp) (end-of-line))
1623 (count-lines before-blank
(point))))
1626 (list :status status
1631 :post-blank post-blank
1632 :post-affiliated begin
)))))
1634 (defun org-element-clock-interpreter (clock contents
)
1635 "Interpret CLOCK element as Org syntax.
1637 (concat org-clock-string
" "
1638 (org-element-timestamp-interpreter
1639 (org-element-property :value clock
) nil
)
1640 (let ((duration (org-element-property :duration clock
)))
1645 (org-split-string duration
":")))))))
1650 (defun org-element-comment-parser (limit affiliated
)
1653 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1654 the buffer position at the beginning of the first affiliated
1655 keyword and CDR is a plist of affiliated keywords along with
1658 Return a list whose CAR is `comment' and CDR is a plist
1659 containing `:begin', `:end', `:value', `:post-blank',
1660 `:post-affiliated' keywords.
1662 Assume point is at comment beginning."
1664 (let* ((begin (car affiliated
))
1665 (post-affiliated (point))
1666 (value (prog2 (looking-at "[ \t]*# ?")
1667 (buffer-substring-no-properties
1668 (match-end 0) (line-end-position))
1671 ;; Get comments ending.
1673 (while (and (< (point) limit
) (looking-at "[ \t]*#\\( \\|$\\)"))
1674 ;; Accumulate lines without leading hash and first
1679 (buffer-substring-no-properties
1680 (match-end 0) (line-end-position))))
1683 (end (progn (goto-char com-end
)
1684 (skip-chars-forward " \r\t\n" limit
)
1685 (if (eobp) (point) (line-beginning-position)))))
1691 :post-blank
(count-lines com-end end
)
1692 :post-affiliated post-affiliated
)
1693 (cdr affiliated
))))))
1695 (defun org-element-comment-interpreter (comment contents
)
1696 "Interpret COMMENT element as Org syntax.
1698 (replace-regexp-in-string "^" "# " (org-element-property :value comment
)))
1703 (defun org-element-comment-block-parser (limit affiliated
)
1704 "Parse an export block.
1706 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1707 the buffer position at the beginning of the first affiliated
1708 keyword and CDR is a plist of affiliated keywords along with
1711 Return a list whose CAR is `comment-block' and CDR is a plist
1712 containing `:begin', `:end', `:value', `:post-blank' and
1713 `:post-affiliated' keywords.
1715 Assume point is at comment block beginning."
1716 (let ((case-fold-search t
))
1717 (if (not (save-excursion
1718 (re-search-forward "^[ \t]*#\\+END_COMMENT[ \t]*$" limit t
)))
1719 ;; Incomplete block: parse it as a paragraph.
1720 (org-element-paragraph-parser limit affiliated
)
1721 (let ((contents-end (match-beginning 0)))
1723 (let* ((begin (car affiliated
))
1724 (post-affiliated (point))
1725 (contents-begin (progn (forward-line) (point)))
1726 (pos-before-blank (progn (goto-char contents-end
)
1729 (end (progn (skip-chars-forward " \r\t\n" limit
)
1730 (if (eobp) (point) (line-beginning-position))))
1731 (value (buffer-substring-no-properties
1732 contents-begin contents-end
)))
1733 (list 'comment-block
1738 :post-blank
(count-lines pos-before-blank end
)
1739 :post-affiliated post-affiliated
)
1740 (cdr affiliated
)))))))))
1742 (defun org-element-comment-block-interpreter (comment-block contents
)
1743 "Interpret COMMENT-BLOCK element as Org syntax.
1745 (format "#+BEGIN_COMMENT\n%s#+END_COMMENT"
1746 (org-element-normalize-string
1747 (org-remove-indentation
1748 (org-element-property :value comment-block
)))))
1753 (defun org-element-diary-sexp-parser (limit affiliated
)
1754 "Parse a diary sexp.
1756 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1757 the buffer position at the beginning of the first affiliated
1758 keyword and CDR is a plist of affiliated keywords along with
1761 Return a list whose CAR is `diary-sexp' and CDR is a plist
1762 containing `:begin', `:end', `:value', `:post-blank' and
1763 `:post-affiliated' keywords."
1765 (let ((begin (car affiliated
))
1766 (post-affiliated (point))
1767 (value (progn (looking-at "\\(%%(.*\\)[ \t]*$")
1768 (org-match-string-no-properties 1)))
1769 (pos-before-blank (progn (forward-line) (point)))
1770 (end (progn (skip-chars-forward " \r\t\n" limit
)
1771 (if (eobp) (point) (line-beginning-position)))))
1777 :post-blank
(count-lines pos-before-blank end
)
1778 :post-affiliated post-affiliated
)
1779 (cdr affiliated
))))))
1781 (defun org-element-diary-sexp-interpreter (diary-sexp contents
)
1782 "Interpret DIARY-SEXP as Org syntax.
1784 (org-element-property :value diary-sexp
))
1789 (defun org-element-example-block-parser (limit affiliated
)
1790 "Parse an example block.
1792 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1793 the buffer position at the beginning of the first affiliated
1794 keyword and CDR is a plist of affiliated keywords along with
1797 Return a list whose CAR is `example-block' and CDR is a plist
1798 containing `:begin', `:end', `:number-lines', `:preserve-indent',
1799 `:retain-labels', `:use-labels', `:label-fmt', `:switches',
1800 `:value', `:post-blank' and `:post-affiliated' keywords."
1801 (let ((case-fold-search t
))
1802 (if (not (save-excursion
1803 (re-search-forward "^[ \t]*#\\+END_EXAMPLE[ \t]*$" limit t
)))
1804 ;; Incomplete block: parse it as a paragraph.
1805 (org-element-paragraph-parser limit affiliated
)
1806 (let ((contents-end (match-beginning 0)))
1810 (looking-at "^[ \t]*#\\+BEGIN_EXAMPLE\\(?: +\\(.*\\)\\)?")
1811 (org-match-string-no-properties 1)))
1812 ;; Switches analysis
1814 (cond ((not switches
) nil
)
1815 ((string-match "-n\\>" switches
) 'new
)
1816 ((string-match "+n\\>" switches
) 'continued
)))
1818 (and switches
(string-match "-i\\>" switches
)))
1819 ;; Should labels be retained in (or stripped from) example
1823 (not (string-match "-r\\>" switches
))
1824 (and number-lines
(string-match "-k\\>" switches
))))
1825 ;; What should code-references use - labels or
1830 (not (string-match "-k\\>" switches
)))))
1833 (string-match "-l +\"\\([^\"\n]+\\)\"" switches
)
1834 (match-string 1 switches
)))
1835 ;; Standard block parsing.
1836 (begin (car affiliated
))
1837 (post-affiliated (point))
1838 (block-ind (progn (skip-chars-forward " \t") (current-column)))
1839 (contents-begin (progn (forward-line) (point)))
1840 (value (org-element-remove-indentation
1841 (org-unescape-code-in-string
1842 (buffer-substring-no-properties
1843 contents-begin contents-end
))
1845 (pos-before-blank (progn (goto-char contents-end
)
1848 (end (progn (skip-chars-forward " \r\t\n" limit
)
1849 (if (eobp) (point) (line-beginning-position)))))
1850 (list 'example-block
1856 :number-lines number-lines
1857 :preserve-indent preserve-indent
1858 :retain-labels retain-labels
1859 :use-labels use-labels
1860 :label-fmt label-fmt
1861 :post-blank
(count-lines pos-before-blank end
)
1862 :post-affiliated post-affiliated
)
1863 (cdr affiliated
)))))))))
1865 (defun org-element-example-block-interpreter (example-block contents
)
1866 "Interpret EXAMPLE-BLOCK element as Org syntax.
1868 (let ((switches (org-element-property :switches example-block
))
1869 (value (org-element-property :value example-block
)))
1870 (concat "#+BEGIN_EXAMPLE" (and switches
(concat " " switches
)) "\n"
1871 (org-element-normalize-string
1872 (org-escape-code-in-string
1873 (if (or org-src-preserve-indentation
1874 (org-element-property :preserve-indent example-block
))
1876 (org-element-remove-indentation value
))))
1882 (defun org-element-export-block-parser (limit affiliated
)
1883 "Parse an export block.
1885 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1886 the buffer position at the beginning of the first affiliated
1887 keyword and CDR is a plist of affiliated keywords along with
1890 Return a list whose CAR is `export-block' and CDR is a plist
1891 containing `:begin', `:end', `:type', `:value', `:post-blank' and
1892 `:post-affiliated' keywords.
1894 Assume point is at export-block beginning."
1895 (let* ((case-fold-search t
)
1896 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
1897 (upcase (org-match-string-no-properties 1)))))
1898 (if (not (save-excursion
1900 (format "^[ \t]*#\\+END_%s[ \t]*$" type
) limit t
)))
1901 ;; Incomplete block: parse it as a paragraph.
1902 (org-element-paragraph-parser limit affiliated
)
1903 (let ((contents-end (match-beginning 0)))
1905 (let* ((begin (car affiliated
))
1906 (post-affiliated (point))
1907 (contents-begin (progn (forward-line) (point)))
1908 (pos-before-blank (progn (goto-char contents-end
)
1911 (end (progn (skip-chars-forward " \r\t\n" limit
)
1912 (if (eobp) (point) (line-beginning-position))))
1913 (value (buffer-substring-no-properties contents-begin
1921 :post-blank
(count-lines pos-before-blank end
)
1922 :post-affiliated post-affiliated
)
1923 (cdr affiliated
)))))))))
1925 (defun org-element-export-block-interpreter (export-block contents
)
1926 "Interpret EXPORT-BLOCK element as Org syntax.
1928 (let ((type (org-element-property :type export-block
)))
1929 (concat (format "#+BEGIN_%s\n" type
)
1930 (org-element-property :value export-block
)
1931 (format "#+END_%s" type
))))
1936 (defun org-element-fixed-width-parser (limit affiliated
)
1937 "Parse a fixed-width section.
1939 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1940 the buffer position at the beginning of the first affiliated
1941 keyword and CDR is a plist of affiliated keywords along with
1944 Return a list whose CAR is `fixed-width' and CDR is a plist
1945 containing `:begin', `:end', `:value', `:post-blank' and
1946 `:post-affiliated' keywords.
1948 Assume point is at the beginning of the fixed-width area."
1950 (let* ((begin (car affiliated
))
1951 (post-affiliated (point))
1955 (while (and (< (point) limit
)
1956 (looking-at "[ \t]*:\\( \\|$\\)"))
1957 ;; Accumulate text without starting colons.
1960 (buffer-substring-no-properties
1961 (match-end 0) (point-at-eol))
1965 (end (progn (skip-chars-forward " \r\t\n" limit
)
1966 (if (eobp) (point) (line-beginning-position)))))
1972 :post-blank
(count-lines end-area end
)
1973 :post-affiliated post-affiliated
)
1974 (cdr affiliated
))))))
1976 (defun org-element-fixed-width-interpreter (fixed-width contents
)
1977 "Interpret FIXED-WIDTH element as Org syntax.
1979 (let ((value (org-element-property :value fixed-width
)))
1981 (replace-regexp-in-string
1983 (if (string-match "\n\\'" value
) (substring value
0 -
1) value
)))))
1986 ;;;; Horizontal Rule
1988 (defun org-element-horizontal-rule-parser (limit affiliated
)
1989 "Parse an horizontal rule.
1991 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1992 the buffer position at the beginning of the first affiliated
1993 keyword and CDR is a plist of affiliated keywords along with
1996 Return a list whose CAR is `horizontal-rule' and CDR is a plist
1997 containing `:begin', `:end', `:post-blank' and `:post-affiliated'
2000 (let ((begin (car affiliated
))
2001 (post-affiliated (point))
2002 (post-hr (progn (forward-line) (point)))
2003 (end (progn (skip-chars-forward " \r\t\n" limit
)
2004 (if (eobp) (point) (line-beginning-position)))))
2005 (list 'horizontal-rule
2009 :post-blank
(count-lines post-hr end
)
2010 :post-affiliated post-affiliated
)
2011 (cdr affiliated
))))))
2013 (defun org-element-horizontal-rule-interpreter (horizontal-rule contents
)
2014 "Interpret HORIZONTAL-RULE element as Org syntax.
2021 (defun org-element-keyword-parser (limit affiliated
)
2022 "Parse a keyword at point.
2024 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2025 the buffer position at the beginning of the first affiliated
2026 keyword and CDR is a plist of affiliated keywords along with
2029 Return a list whose CAR is `keyword' and CDR is a plist
2030 containing `:key', `:value', `:begin', `:end', `:post-blank' and
2031 `:post-affiliated' keywords."
2033 ;; An orphaned affiliated keyword is considered as a regular
2034 ;; keyword. In this case AFFILIATED is nil, so we take care of
2035 ;; this corner case.
2036 (let ((begin (or (car affiliated
) (point)))
2037 (post-affiliated (point))
2038 (key (progn (looking-at "[ \t]*#\\+\\(\\S-+*\\):")
2039 (upcase (org-match-string-no-properties 1))))
2040 (value (org-trim (buffer-substring-no-properties
2041 (match-end 0) (point-at-eol))))
2042 (pos-before-blank (progn (forward-line) (point)))
2043 (end (progn (skip-chars-forward " \r\t\n" limit
)
2044 (if (eobp) (point) (line-beginning-position)))))
2051 :post-blank
(count-lines pos-before-blank end
)
2052 :post-affiliated post-affiliated
)
2053 (cdr affiliated
))))))
2055 (defun org-element-keyword-interpreter (keyword contents
)
2056 "Interpret KEYWORD element as Org syntax.
2059 (org-element-property :key keyword
)
2060 (org-element-property :value keyword
)))
2063 ;;;; Latex Environment
2065 (defconst org-element--latex-begin-environment
2066 "^[ \t]*\\\\begin{\\([A-Za-z0-9*]+\\)}"
2067 "Regexp matching the beginning of a LaTeX environment.
2068 The environment is captured by the first group.
2070 See also `org-element--latex-end-environment'.")
2072 (defconst org-element--latex-end-environment
2073 "\\\\end{%s}[ \t]*$"
2074 "Format string matching the ending of a LaTeX environment.
2075 See also `org-element--latex-begin-environment'.")
2077 (defun org-element-latex-environment-parser (limit affiliated
)
2078 "Parse a LaTeX environment.
2080 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2081 the buffer position at the beginning of the first affiliated
2082 keyword and CDR is a plist of affiliated keywords along with
2085 Return a list whose CAR is `latex-environment' and CDR is a plist
2086 containing `:begin', `:end', `:value', `:post-blank' and
2087 `:post-affiliated' keywords.
2089 Assume point is at the beginning of the latex environment."
2091 (let ((case-fold-search t
)
2092 (code-begin (point)))
2093 (looking-at org-element--latex-begin-environment
)
2094 (if (not (re-search-forward (format org-element--latex-end-environment
2095 (regexp-quote (match-string 1)))
2097 ;; Incomplete latex environment: parse it as a paragraph.
2098 (org-element-paragraph-parser limit affiliated
)
2099 (let* ((code-end (progn (forward-line) (point)))
2100 (begin (car affiliated
))
2101 (value (buffer-substring-no-properties code-begin code-end
))
2102 (end (progn (skip-chars-forward " \r\t\n" limit
)
2103 (if (eobp) (point) (line-beginning-position)))))
2104 (list 'latex-environment
2109 :post-blank
(count-lines code-end end
)
2110 :post-affiliated code-begin
)
2111 (cdr affiliated
))))))))
2113 (defun org-element-latex-environment-interpreter (latex-environment contents
)
2114 "Interpret LATEX-ENVIRONMENT element as Org syntax.
2116 (org-element-property :value latex-environment
))
2121 (defun org-element-node-property-parser (limit)
2122 "Parse a node-property at point.
2124 LIMIT bounds the search.
2126 Return a list whose CAR is `node-property' and CDR is a plist
2127 containing `:key', `:value', `:begin', `:end', `:post-blank' and
2128 `:post-affiliated' keywords."
2129 (looking-at org-property-re
)
2130 (let ((case-fold-search t
)
2132 (key (org-match-string-no-properties 2))
2133 (value (org-match-string-no-properties 3))
2134 (end (save-excursion
2136 (if (re-search-forward org-property-re limit t
)
2137 (line-beginning-position)
2139 (list 'node-property
2145 :post-affiliated begin
))))
2147 (defun org-element-node-property-interpreter (node-property contents
)
2148 "Interpret NODE-PROPERTY element as Org syntax.
2150 (format org-property-format
2151 (format ":%s:" (org-element-property :key node-property
))
2152 (or (org-element-property :value node-property
) "")))
2157 (defun org-element-paragraph-parser (limit affiliated
)
2160 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2161 the buffer position at the beginning of the first affiliated
2162 keyword and CDR is a plist of affiliated keywords along with
2165 Return a list whose CAR is `paragraph' and CDR is a plist
2166 containing `:begin', `:end', `:contents-begin' and
2167 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
2169 Assume point is at the beginning of the paragraph."
2171 (let* ((begin (car affiliated
))
2172 (contents-begin (point))
2174 (let ((case-fold-search t
))
2176 (if (not (re-search-forward
2177 org-element-paragraph-separate limit
'm
))
2179 ;; A matching `org-element-paragraph-separate' is not
2180 ;; necessarily the end of the paragraph. In
2181 ;; particular, lines starting with # or : as a first
2182 ;; non-space character are ambiguous. We have to
2183 ;; check if they are valid Org syntax (e.g., not an
2184 ;; incomplete keyword).
2188 ;; There's no ambiguity for other symbols or
2189 ;; empty lines: stop here.
2190 (looking-at "[ \t]*\\(?:[^:#]\\|$\\)")
2191 ;; Stop at valid fixed-width areas.
2192 (looking-at "[ \t]*:\\(?: \\|$\\)")
2194 (and (looking-at org-drawer-regexp
)
2197 "^[ \t]*:END:[ \t]*$" limit t
)))
2198 ;; Stop at valid comments.
2199 (looking-at "[ \t]*#\\(?: \\|$\\)")
2200 ;; Stop at valid dynamic blocks.
2201 (and (looking-at org-dblock-start-re
)
2204 "^[ \t]*#\\+END:?[ \t]*$" limit t
)))
2205 ;; Stop at valid blocks.
2206 (and (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
2209 (format "^[ \t]*#\\+END_%s[ \t]*$"
2211 (org-match-string-no-properties 1)))
2213 ;; Stop at valid latex environments.
2214 (and (looking-at org-element--latex-begin-environment
)
2217 (format org-element--latex-end-environment
2219 (org-match-string-no-properties 1)))
2221 ;; Stop at valid keywords.
2222 (looking-at "[ \t]*#\\+\\S-+:")
2223 ;; Skip everything else.
2227 (re-search-forward org-element-paragraph-separate
2229 (beginning-of-line)))
2230 (if (= (point) limit
) limit
2231 (goto-char (line-beginning-position)))))
2232 (contents-end (progn (skip-chars-backward " \r\t\n" contents-begin
)
2235 (end (progn (skip-chars-forward " \r\t\n" limit
)
2236 (if (eobp) (point) (line-beginning-position)))))
2241 :contents-begin contents-begin
2242 :contents-end contents-end
2243 :post-blank
(count-lines before-blank end
)
2244 :post-affiliated contents-begin
)
2245 (cdr affiliated
))))))
2247 (defun org-element-paragraph-interpreter (paragraph contents
)
2248 "Interpret PARAGRAPH element as Org syntax.
2249 CONTENTS is the contents of the element."
2255 (defun org-element-planning-parser (limit)
2258 LIMIT bounds the search.
2260 Return a list whose CAR is `planning' and CDR is a plist
2261 containing `:closed', `:deadline', `:scheduled', `:begin',
2262 `:end', `:post-blank' and `:post-affiliated' keywords."
2264 (let* ((case-fold-search nil
)
2266 (post-blank (let ((before-blank (progn (forward-line) (point))))
2267 (skip-chars-forward " \r\t\n" limit
)
2268 (skip-chars-backward " \t")
2269 (unless (bolp) (end-of-line))
2270 (count-lines before-blank
(point))))
2272 closed deadline scheduled
)
2274 (while (re-search-forward org-keyword-time-not-clock-regexp end t
)
2275 (goto-char (match-end 1))
2276 (skip-chars-forward " \t" end
)
2277 (let ((keyword (match-string 1))
2278 (time (org-element-timestamp-parser)))
2279 (cond ((equal keyword org-closed-string
) (setq closed time
))
2280 ((equal keyword org-deadline-string
) (setq deadline time
))
2281 (t (setq scheduled time
)))))
2283 (list :closed closed
2285 :scheduled scheduled
2288 :post-blank post-blank
2289 :post-affiliated begin
)))))
2291 (defun org-element-planning-interpreter (planning contents
)
2292 "Interpret PLANNING element as Org syntax.
2297 (list (let ((deadline (org-element-property :deadline planning
)))
2299 (concat org-deadline-string
" "
2300 (org-element-timestamp-interpreter deadline nil
))))
2301 (let ((scheduled (org-element-property :scheduled planning
)))
2303 (concat org-scheduled-string
" "
2304 (org-element-timestamp-interpreter scheduled nil
))))
2305 (let ((closed (org-element-property :closed planning
)))
2307 (concat org-closed-string
" "
2308 (org-element-timestamp-interpreter closed nil
))))))
2314 (defun org-element-src-block-parser (limit affiliated
)
2317 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2318 the buffer position at the beginning of the first affiliated
2319 keyword and CDR is a plist of affiliated keywords along with
2322 Return a list whose CAR is `src-block' and CDR is a plist
2323 containing `:language', `:switches', `:parameters', `:begin',
2324 `:end', `:number-lines', `:retain-labels', `:use-labels',
2325 `:label-fmt', `:preserve-indent', `:value', `:post-blank' and
2326 `:post-affiliated' keywords.
2328 Assume point is at the beginning of the block."
2329 (let ((case-fold-search t
))
2330 (if (not (save-excursion (re-search-forward "^[ \t]*#\\+END_SRC[ \t]*$"
2332 ;; Incomplete block: parse it as a paragraph.
2333 (org-element-paragraph-parser limit affiliated
)
2334 (let ((contents-end (match-beginning 0)))
2336 (let* ((begin (car affiliated
))
2337 (post-affiliated (point))
2338 ;; Get language as a string.
2342 (concat "^[ \t]*#\\+BEGIN_SRC"
2343 "\\(?: +\\(\\S-+\\)\\)?"
2344 "\\(\\(?: +\\(?:-l \".*?\"\\|[-+][A-Za-z]\\)\\)+\\)?"
2346 (org-match-string-no-properties 1)))
2348 (switches (org-match-string-no-properties 2))
2350 (parameters (org-match-string-no-properties 3))
2351 ;; Switches analysis
2353 (cond ((not switches
) nil
)
2354 ((string-match "-n\\>" switches
) 'new
)
2355 ((string-match "+n\\>" switches
) 'continued
)))
2356 (preserve-indent (and switches
2357 (string-match "-i\\>" switches
)))
2360 (string-match "-l +\"\\([^\"\n]+\\)\"" switches
)
2361 (match-string 1 switches
)))
2362 ;; Should labels be retained in (or stripped from)
2366 (not (string-match "-r\\>" switches
))
2367 (and number-lines
(string-match "-k\\>" switches
))))
2368 ;; What should code-references use - labels or
2373 (not (string-match "-k\\>" switches
)))))
2375 (block-ind (progn (skip-chars-forward " \t") (current-column)))
2377 (value (org-element-remove-indentation
2378 (org-unescape-code-in-string
2379 (buffer-substring-no-properties
2380 (progn (forward-line) (point)) contents-end
))
2382 (pos-before-blank (progn (goto-char contents-end
)
2385 ;; Get position after ending blank lines.
2386 (end (progn (skip-chars-forward " \r\t\n" limit
)
2387 (if (eobp) (point) (line-beginning-position)))))
2390 (list :language language
2391 :switches
(and (org-string-nw-p switches
)
2392 (org-trim switches
))
2393 :parameters
(and (org-string-nw-p parameters
)
2394 (org-trim parameters
))
2397 :number-lines number-lines
2398 :preserve-indent preserve-indent
2399 :retain-labels retain-labels
2400 :use-labels use-labels
2401 :label-fmt label-fmt
2403 :post-blank
(count-lines pos-before-blank end
)
2404 :post-affiliated post-affiliated
)
2405 (cdr affiliated
)))))))))
2407 (defun org-element-src-block-interpreter (src-block contents
)
2408 "Interpret SRC-BLOCK element as Org syntax.
2410 (let ((lang (org-element-property :language src-block
))
2411 (switches (org-element-property :switches src-block
))
2412 (params (org-element-property :parameters src-block
))
2414 (let ((val (org-element-property :value src-block
)))
2416 ((or org-src-preserve-indentation
2417 (org-element-property :preserve-indent src-block
))
2419 ((zerop org-edit-src-content-indentation
) val
)
2421 (let ((ind (make-string org-edit-src-content-indentation ?\s
)))
2422 (replace-regexp-in-string
2423 "\\(^\\)[ \t]*\\S-" ind val nil nil
1)))))))
2424 (concat (format "#+BEGIN_SRC%s\n"
2425 (concat (and lang
(concat " " lang
))
2426 (and switches
(concat " " switches
))
2427 (and params
(concat " " params
))))
2428 (org-element-normalize-string (org-escape-code-in-string value
))
2434 (defun org-element-table-parser (limit affiliated
)
2435 "Parse a table at point.
2437 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2438 the buffer position at the beginning of the first affiliated
2439 keyword and CDR is a plist of affiliated keywords along with
2442 Return a list whose CAR is `table' and CDR is a plist containing
2443 `:begin', `:end', `:tblfm', `:type', `:contents-begin',
2444 `:contents-end', `:value', `:post-blank' and `:post-affiliated'
2447 Assume point is at the beginning of the table."
2449 (let* ((case-fold-search t
)
2450 (table-begin (point))
2451 (type (if (org-at-table.el-p
) 'table.el
'org
))
2452 (begin (car affiliated
))
2454 (if (re-search-forward org-table-any-border-regexp limit
'm
)
2455 (goto-char (match-beginning 0))
2458 (while (looking-at "[ \t]*#\\+TBLFM: +\\(.*\\)[ \t]*$")
2459 (push (org-match-string-no-properties 1) acc
)
2462 (pos-before-blank (point))
2463 (end (progn (skip-chars-forward " \r\t\n" limit
)
2464 (if (eobp) (point) (line-beginning-position)))))
2471 ;; Only `org' tables have contents. `table.el' tables
2472 ;; use a `:value' property to store raw table as
2474 :contents-begin
(and (eq type
'org
) table-begin
)
2475 :contents-end
(and (eq type
'org
) table-end
)
2476 :value
(and (eq type
'table.el
)
2477 (buffer-substring-no-properties
2478 table-begin table-end
))
2479 :post-blank
(count-lines pos-before-blank end
)
2480 :post-affiliated table-begin
)
2481 (cdr affiliated
))))))
2483 (defun org-element-table-interpreter (table contents
)
2484 "Interpret TABLE element as Org syntax.
2485 CONTENTS is a string, if table's type is `org', or nil."
2486 (if (eq (org-element-property :type table
) 'table.el
)
2487 (org-remove-indentation (org-element-property :value table
))
2488 (concat (with-temp-buffer (insert contents
)
2491 (mapconcat (lambda (fm) (concat "#+TBLFM: " fm
))
2492 (reverse (org-element-property :tblfm table
))
2498 (defun org-element-table-row-parser (limit)
2499 "Parse table row at point.
2501 LIMIT bounds the search.
2503 Return a list whose CAR is `table-row' and CDR is a plist
2504 containing `:begin', `:end', `:contents-begin', `:contents-end',
2505 `:type', `:post-blank' and `:post-affiliated' keywords."
2507 (let* ((type (if (looking-at "^[ \t]*|-") 'rule
'standard
))
2509 ;; A table rule has no contents. In that case, ensure
2510 ;; CONTENTS-BEGIN matches CONTENTS-END.
2511 (contents-begin (and (eq type
'standard
)
2512 (search-forward "|")
2514 (contents-end (and (eq type
'standard
)
2517 (skip-chars-backward " \t")
2519 (end (progn (forward-line) (point))))
2524 :contents-begin contents-begin
2525 :contents-end contents-end
2527 :post-affiliated begin
)))))
2529 (defun org-element-table-row-interpreter (table-row contents
)
2530 "Interpret TABLE-ROW element as Org syntax.
2531 CONTENTS is the contents of the table row."
2532 (if (eq (org-element-property :type table-row
) 'rule
) "|-"
2533 (concat "| " contents
)))
2538 (defun org-element-verse-block-parser (limit affiliated
)
2539 "Parse a verse block.
2541 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2542 the buffer position at the beginning of the first affiliated
2543 keyword and CDR is a plist of affiliated keywords along with
2546 Return a list whose CAR is `verse-block' and CDR is a plist
2547 containing `:begin', `:end', `:contents-begin', `:contents-end',
2548 `:post-blank' and `:post-affiliated' keywords.
2550 Assume point is at beginning of the block."
2551 (let ((case-fold-search t
))
2552 (if (not (save-excursion
2553 (re-search-forward "^[ \t]*#\\+END_VERSE[ \t]*$" limit t
)))
2554 ;; Incomplete block: parse it as a paragraph.
2555 (org-element-paragraph-parser limit affiliated
)
2556 (let ((contents-end (match-beginning 0)))
2558 (let* ((begin (car affiliated
))
2559 (post-affiliated (point))
2560 (contents-begin (progn (forward-line) (point)))
2561 (pos-before-blank (progn (goto-char contents-end
)
2564 (end (progn (skip-chars-forward " \r\t\n" limit
)
2565 (if (eobp) (point) (line-beginning-position)))))
2570 :contents-begin contents-begin
2571 :contents-end contents-end
2572 :post-blank
(count-lines pos-before-blank end
)
2573 :post-affiliated post-affiliated
)
2574 (cdr affiliated
)))))))))
2576 (defun org-element-verse-block-interpreter (verse-block contents
)
2577 "Interpret VERSE-BLOCK element as Org syntax.
2578 CONTENTS is verse block contents."
2579 (format "#+BEGIN_VERSE\n%s#+END_VERSE" contents
))
2585 ;; Unlike to elements, raw text can be found between objects. Hence,
2586 ;; `org-element--object-lex' is provided to find the next object in
2589 ;; Some object types (e.g., `italic') are recursive. Restrictions on
2590 ;; object types they can contain will be specified in
2591 ;; `org-element-object-restrictions'.
2593 ;; Creating a new type of object requires to alter
2594 ;; `org-element--object-regexp' and `org-element--object-lex', add the
2595 ;; new type in `org-element-all-objects', and possibly add
2596 ;; restrictions in `org-element-object-restrictions'.
2600 (defun org-element-bold-parser ()
2601 "Parse bold object at point, if any.
2603 When at a bold object, return a list whose car is `bold' and cdr
2604 is a plist with `:begin', `:end', `:contents-begin' and
2605 `:contents-end' and `:post-blank' keywords. Otherwise, return
2608 Assume point is at the first star marker."
2610 (unless (bolp) (backward-char 1))
2611 (when (looking-at org-emph-re
)
2612 (let ((begin (match-beginning 2))
2613 (contents-begin (match-beginning 4))
2614 (contents-end (match-end 4))
2615 (post-blank (progn (goto-char (match-end 2))
2616 (skip-chars-forward " \t")))
2621 :contents-begin contents-begin
2622 :contents-end contents-end
2623 :post-blank post-blank
))))))
2625 (defun org-element-bold-interpreter (bold contents
)
2626 "Interpret BOLD object as Org syntax.
2627 CONTENTS is the contents of the object."
2628 (format "*%s*" contents
))
2633 (defun org-element-code-parser ()
2634 "Parse code object at point, if any.
2636 When at a code object, return a list whose car is `code' and cdr
2637 is a plist with `:value', `:begin', `:end' and `:post-blank'
2638 keywords. Otherwise, return nil.
2640 Assume point is at the first tilde marker."
2642 (unless (bolp) (backward-char 1))
2643 (when (looking-at org-emph-re
)
2644 (let ((begin (match-beginning 2))
2645 (value (org-match-string-no-properties 4))
2646 (post-blank (progn (goto-char (match-end 2))
2647 (skip-chars-forward " \t")))
2653 :post-blank post-blank
))))))
2655 (defun org-element-code-interpreter (code contents
)
2656 "Interpret CODE object as Org syntax.
2658 (format "~%s~" (org-element-property :value code
)))
2663 (defun org-element-entity-parser ()
2664 "Parse entity at point, if any.
2666 When at an entity, return a list whose car is `entity' and cdr
2667 a plist with `:begin', `:end', `:latex', `:latex-math-p',
2668 `:html', `:latin1', `:utf-8', `:ascii', `:use-brackets-p' and
2669 `:post-blank' as keywords. Otherwise, return nil.
2671 Assume point is at the beginning of the entity."
2673 (when (looking-at "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)")
2675 (let* ((value (or (org-entity-get (match-string 1))
2676 (throw 'no-object nil
)))
2677 (begin (match-beginning 0))
2678 (bracketsp (string= (match-string 2) "{}"))
2679 (post-blank (progn (goto-char (match-end 1))
2680 (when bracketsp
(forward-char 2))
2681 (skip-chars-forward " \t")))
2684 (list :name
(car value
)
2685 :latex
(nth 1 value
)
2686 :latex-math-p
(nth 2 value
)
2688 :ascii
(nth 4 value
)
2689 :latin1
(nth 5 value
)
2690 :utf-8
(nth 6 value
)
2693 :use-brackets-p bracketsp
2694 :post-blank post-blank
)))))))
2696 (defun org-element-entity-interpreter (entity contents
)
2697 "Interpret ENTITY object as Org syntax.
2700 (org-element-property :name entity
)
2701 (when (org-element-property :use-brackets-p entity
) "{}")))
2706 (defun org-element-export-snippet-parser ()
2707 "Parse export snippet at point.
2709 When at an export snippet, return a list whose car is
2710 `export-snippet' and cdr a plist with `:begin', `:end',
2711 `:back-end', `:value' and `:post-blank' as keywords. Otherwise,
2714 Assume point is at the beginning of the snippet."
2717 (when (and (looking-at "@@\\([-A-Za-z0-9]+\\):")
2719 (save-match-data (goto-char (match-end 0))
2720 (re-search-forward "@@" nil t
)
2721 (match-beginning 0))))
2722 (let* ((begin (match-beginning 0))
2723 (back-end (org-match-string-no-properties 1))
2724 (value (buffer-substring-no-properties
2725 (match-end 0) contents-end
))
2726 (post-blank (skip-chars-forward " \t"))
2728 (list 'export-snippet
2729 (list :back-end back-end
2733 :post-blank post-blank
)))))))
2735 (defun org-element-export-snippet-interpreter (export-snippet contents
)
2736 "Interpret EXPORT-SNIPPET object as Org syntax.
2739 (org-element-property :back-end export-snippet
)
2740 (org-element-property :value export-snippet
)))
2743 ;;;; Footnote Reference
2745 (defun org-element-footnote-reference-parser ()
2746 "Parse footnote reference at point, if any.
2748 When at a footnote reference, return a list whose car is
2749 `footnote-reference' and cdr a plist with `:label', `:type',
2750 `:begin', `:end', `:content-begin', `:contents-end' and
2751 `:post-blank' as keywords. Otherwise, return nil."
2753 (when (looking-at org-footnote-re
)
2755 (let* ((begin (point))
2757 (or (org-match-string-no-properties 2)
2758 (org-match-string-no-properties 3)
2759 (and (match-string 1)
2760 (concat "fn:" (org-match-string-no-properties 1)))))
2761 (type (if (or (not label
) (match-string 1)) 'inline
'standard
))
2762 (inner-begin (match-end 0))
2766 (while (and (> count
0) (re-search-forward "[][]" nil t
))
2767 (if (equal (match-string 0) "[") (incf count
) (decf count
)))
2768 (unless (zerop count
) (throw 'no-object nil
))
2770 (post-blank (progn (goto-char (1+ inner-end
))
2771 (skip-chars-forward " \t")))
2773 (list 'footnote-reference
2778 :contents-begin
(and (eq type
'inline
) inner-begin
)
2779 :contents-end
(and (eq type
'inline
) inner-end
)
2780 :post-blank post-blank
)))))))
2782 (defun org-element-footnote-reference-interpreter (footnote-reference contents
)
2783 "Interpret FOOTNOTE-REFERENCE object as Org syntax.
2784 CONTENTS is its definition, when inline, or nil."
2786 (concat (or (org-element-property :label footnote-reference
) "fn:")
2787 (and contents
(concat ":" contents
)))))
2790 ;;;; Inline Babel Call
2792 (defun org-element-inline-babel-call-parser ()
2793 "Parse inline babel call at point, if any.
2795 When at an inline babel call, return a list whose car is
2796 `inline-babel-call' and cdr a plist with `:begin', `:end',
2797 `:value' and `:post-blank' as keywords. Otherwise, return nil.
2799 Assume point is at the beginning of the babel call."
2801 (unless (bolp) (backward-char))
2802 (when (let ((case-fold-search t
))
2803 (looking-at org-babel-inline-lob-one-liner-regexp
))
2804 (let ((begin (match-end 1))
2805 (value (buffer-substring-no-properties (match-end 1) (match-end 0)))
2806 (post-blank (progn (goto-char (match-end 0))
2807 (skip-chars-forward " \t")))
2809 (list 'inline-babel-call
2813 :post-blank post-blank
))))))
2815 (defun org-element-inline-babel-call-interpreter (inline-babel-call contents
)
2816 "Interpret INLINE-BABEL-CALL object as Org syntax.
2818 (org-element-property :value inline-babel-call
))
2821 ;;;; Inline Src Block
2823 (defun org-element-inline-src-block-parser ()
2824 "Parse inline source block at point, if any.
2826 When at an inline source block, return a list whose car is
2827 `inline-src-block' and cdr a plist with `:begin', `:end',
2828 `:language', `:value', `:parameters' and `:post-blank' as
2829 keywords. Otherwise, return nil.
2831 Assume point is at the beginning of the inline src block."
2833 (unless (bolp) (backward-char))
2834 (when (looking-at org-babel-inline-src-block-regexp
)
2835 (let ((begin (match-beginning 1))
2836 (language (org-match-string-no-properties 2))
2837 (parameters (org-match-string-no-properties 4))
2838 (value (org-match-string-no-properties 5))
2839 (post-blank (progn (goto-char (match-end 0))
2840 (skip-chars-forward " \t")))
2842 (list 'inline-src-block
2843 (list :language language
2845 :parameters parameters
2848 :post-blank post-blank
))))))
2850 (defun org-element-inline-src-block-interpreter (inline-src-block contents
)
2851 "Interpret INLINE-SRC-BLOCK object as Org syntax.
2853 (let ((language (org-element-property :language inline-src-block
))
2854 (arguments (org-element-property :parameters inline-src-block
))
2855 (body (org-element-property :value inline-src-block
)))
2856 (format "src_%s%s{%s}"
2858 (if arguments
(format "[%s]" arguments
) "")
2863 (defun org-element-italic-parser ()
2864 "Parse italic object at point, if any.
2866 When at an italic object, return a list whose car is `italic' and
2867 cdr is a plist with `:begin', `:end', `:contents-begin' and
2868 `:contents-end' and `:post-blank' keywords. Otherwise, return
2871 Assume point is at the first slash marker."
2873 (unless (bolp) (backward-char 1))
2874 (when (looking-at org-emph-re
)
2875 (let ((begin (match-beginning 2))
2876 (contents-begin (match-beginning 4))
2877 (contents-end (match-end 4))
2878 (post-blank (progn (goto-char (match-end 2))
2879 (skip-chars-forward " \t")))
2884 :contents-begin contents-begin
2885 :contents-end contents-end
2886 :post-blank post-blank
))))))
2888 (defun org-element-italic-interpreter (italic contents
)
2889 "Interpret ITALIC object as Org syntax.
2890 CONTENTS is the contents of the object."
2891 (format "/%s/" contents
))
2896 (defun org-element-latex-fragment-parser ()
2897 "Parse LaTeX fragment at point, if any.
2899 When at a LaTeX fragment, return a list whose car is
2900 `latex-fragment' and cdr a plist with `:value', `:begin', `:end',
2901 and `:post-blank' as keywords. Otherwise, return nil.
2903 Assume point is at the beginning of the LaTeX fragment."
2906 (let* ((begin (point))
2908 (if (eq (char-after) ?$
)
2909 (if (eq (char-after (1+ (point))) ?$
)
2910 (search-forward "$$" nil t
2)
2911 (and (not (eq (char-before) ?$
))
2912 (search-forward "$" nil t
2)
2913 (not (memq (char-before (match-beginning 0))
2914 '(?\s ?
\t ?
\n ?
, ?.
)))
2915 (looking-at "\\([- \t.,?;:'\"]\\|$\\)")
2917 (case (char-after (1+ (point)))
2918 (?\
( (search-forward "\\)" nil t
))
2919 (?\
[ (search-forward "\\]" nil t
))
2922 (and (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")
2924 (post-blank (if (not after-fragment
) (throw 'no-object nil
)
2925 (goto-char after-fragment
)
2926 (skip-chars-forward " \t")))
2928 (list 'latex-fragment
2929 (list :value
(buffer-substring-no-properties begin after-fragment
)
2932 :post-blank post-blank
))))))
2934 (defun org-element-latex-fragment-interpreter (latex-fragment contents
)
2935 "Interpret LATEX-FRAGMENT object as Org syntax.
2937 (org-element-property :value latex-fragment
))
2941 (defun org-element-line-break-parser ()
2942 "Parse line break at point, if any.
2944 When at a line break, return a list whose car is `line-break',
2945 and cdr a plist with `:begin', `:end' and `:post-blank' keywords.
2946 Otherwise, return nil.
2948 Assume point is at the beginning of the line break."
2949 (when (and (org-looking-at-p "\\\\\\\\[ \t]*$")
2950 (not (eq (char-before) ?
\\)))
2952 (list :begin
(point)
2953 :end
(progn (forward-line) (point))
2956 (defun org-element-line-break-interpreter (line-break contents
)
2957 "Interpret LINE-BREAK object as Org syntax.
2964 (defun org-element-link-parser ()
2965 "Parse link at point, if any.
2967 When at a link, return a list whose car is `link' and cdr a plist
2968 with `:type', `:path', `:raw-link', `:application',
2969 `:search-option', `:begin', `:end', `:contents-begin',
2970 `:contents-end' and `:post-blank' as keywords. Otherwise, return
2973 Assume point is at the beginning of the link."
2975 (let ((begin (point))
2976 end contents-begin contents-end link-end post-blank path type
2977 raw-link link search-option application
)
2979 ;; Type 1: Text targeted from a radio target.
2980 ((and org-target-link-regexp
2981 (save-excursion (or (bolp) (backward-char))
2982 (looking-at org-target-link-regexp
)))
2984 link-end
(match-end 1)
2985 path
(org-match-string-no-properties 1)
2986 contents-begin
(match-beginning 1)
2987 contents-end
(match-end 1)))
2988 ;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]]
2989 ((looking-at org-bracket-link-regexp
)
2990 (setq contents-begin
(match-beginning 3)
2991 contents-end
(match-end 3)
2992 link-end
(match-end 0)
2993 ;; RAW-LINK is the original link. Expand any
2994 ;; abbreviation in it.
2995 raw-link
(org-translate-link
2996 (org-link-expand-abbrev
2997 (org-match-string-no-properties 1))))
2998 ;; Determine TYPE of link and set PATH accordingly.
3001 ((or (file-name-absolute-p raw-link
)
3002 (string-match "\\`\\.\\.?/" raw-link
))
3003 (setq type
"file" path raw-link
))
3004 ;; Explicit type (http, irc, bbdb...). See `org-link-types'.
3005 ((string-match org-link-types-re raw-link
)
3006 (setq type
(match-string 1 raw-link
)
3007 ;; According to RFC 3986, extra whitespace should be
3008 ;; ignored when a URI is extracted.
3009 path
(replace-regexp-in-string
3010 "[ \t]*\n[ \t]*" "" (substring raw-link
(match-end 0)))))
3011 ;; Id type: PATH is the id.
3012 ((string-match "\\`id:\\([-a-f0-9]+\\)" raw-link
)
3013 (setq type
"id" path
(match-string 1 raw-link
)))
3014 ;; Code-ref type: PATH is the name of the reference.
3015 ((string-match "\\`(\\(.*\\))\\'" raw-link
)
3016 (setq type
"coderef" path
(match-string 1 raw-link
)))
3017 ;; Custom-id type: PATH is the name of the custom id.
3018 ((= (aref raw-link
0) ?
#)
3019 (setq type
"custom-id" path
(substring raw-link
1)))
3020 ;; Fuzzy type: Internal link either matches a target, an
3021 ;; headline name or nothing. PATH is the target or
3023 (t (setq type
"fuzzy" path raw-link
))))
3024 ;; Type 3: Plain link, e.g., http://orgmode.org
3025 ((looking-at org-plain-link-re
)
3026 (setq raw-link
(org-match-string-no-properties 0)
3027 type
(org-match-string-no-properties 1)
3028 link-end
(match-end 0)
3029 path
(org-match-string-no-properties 2)))
3030 ;; Type 4: Angular link, e.g., <http://orgmode.org>
3031 ((looking-at org-angle-link-re
)
3032 (setq raw-link
(buffer-substring-no-properties
3033 (match-beginning 1) (match-end 2))
3034 type
(org-match-string-no-properties 1)
3035 link-end
(match-end 0)
3036 path
(org-match-string-no-properties 2)))
3037 (t (throw 'no-object nil
)))
3038 ;; In any case, deduce end point after trailing white space from
3039 ;; LINK-END variable.
3041 (setq post-blank
(progn (goto-char link-end
) (skip-chars-forward " \t"))
3043 ;; Special "file" type link processing.
3044 (when (member type org-element-link-type-is-file
)
3045 ;; Extract opening application and search option.
3046 (cond ((string-match "^file\\+\\(.*\\)$" type
)
3047 (setq application
(match-string 1 type
)))
3048 ((not (string-match "^file" type
))
3049 (setq application type
)))
3050 (when (string-match "::\\(.*\\)\\'" path
)
3051 (setq search-option
(match-string 1 path
)
3052 path
(replace-match "" nil nil path
)))
3054 (when (and (file-name-absolute-p path
)
3055 (not (org-string-match-p "\\`[/~]/" path
)))
3056 (setq path
(concat "//" path
)))
3057 ;; Make sure TYPE always reports "file".
3062 :raw-link
(or raw-link path
)
3063 :application application
3064 :search-option search-option
3067 :contents-begin contents-begin
3068 :contents-end contents-end
3069 :post-blank post-blank
))))))
3071 (defun org-element-link-interpreter (link contents
)
3072 "Interpret LINK object as Org syntax.
3073 CONTENTS is the contents of the object, or nil."
3074 (let ((type (org-element-property :type link
))
3075 (raw-link (org-element-property :raw-link link
)))
3076 (if (string= type
"radio") raw-link
3079 (if contents
(format "[%s]" contents
) "")))))
3084 (defun org-element-macro-parser ()
3085 "Parse macro at point, if any.
3087 When at a macro, return a list whose car is `macro' and cdr
3088 a plist with `:key', `:args', `:begin', `:end', `:value' and
3089 `:post-blank' as keywords. Otherwise, return nil.
3091 Assume point is at the macro."
3093 (when (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
3094 (let ((begin (point))
3095 (key (downcase (org-match-string-no-properties 1)))
3096 (value (org-match-string-no-properties 0))
3097 (post-blank (progn (goto-char (match-end 0))
3098 (skip-chars-forward " \t")))
3100 (args (let ((args (org-match-string-no-properties 3)))
3101 (and args
(org-macro-extract-arguments args
)))))
3108 :post-blank post-blank
))))))
3110 (defun org-element-macro-interpreter (macro contents
)
3111 "Interpret MACRO object as Org syntax.
3113 (org-element-property :value macro
))
3118 (defun org-element-radio-target-parser ()
3119 "Parse radio target at point, if any.
3121 When at a radio target, return a list whose car is `radio-target'
3122 and cdr a plist with `:begin', `:end', `:contents-begin',
3123 `:contents-end', `:value' and `:post-blank' as keywords.
3124 Otherwise, return nil.
3126 Assume point is at the radio target."
3128 (when (looking-at org-radio-target-regexp
)
3129 (let ((begin (point))
3130 (contents-begin (match-beginning 1))
3131 (contents-end (match-end 1))
3132 (value (org-match-string-no-properties 1))
3133 (post-blank (progn (goto-char (match-end 0))
3134 (skip-chars-forward " \t")))
3139 :contents-begin contents-begin
3140 :contents-end contents-end
3141 :post-blank post-blank
3144 (defun org-element-radio-target-interpreter (target contents
)
3145 "Interpret TARGET object as Org syntax.
3146 CONTENTS is the contents of the object."
3147 (concat "<<<" contents
">>>"))
3150 ;;;; Statistics Cookie
3152 (defun org-element-statistics-cookie-parser ()
3153 "Parse statistics cookie at point, if any.
3155 When at a statistics cookie, return a list whose car is
3156 `statistics-cookie', and cdr a plist with `:begin', `:end',
3157 `:value' and `:post-blank' keywords. Otherwise, return nil.
3159 Assume point is at the beginning of the statistics-cookie."
3161 (when (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
3162 (let* ((begin (point))
3163 (value (buffer-substring-no-properties
3164 (match-beginning 0) (match-end 0)))
3165 (post-blank (progn (goto-char (match-end 0))
3166 (skip-chars-forward " \t")))
3168 (list 'statistics-cookie
3172 :post-blank post-blank
))))))
3174 (defun org-element-statistics-cookie-interpreter (statistics-cookie contents
)
3175 "Interpret STATISTICS-COOKIE object as Org syntax.
3177 (org-element-property :value statistics-cookie
))
3182 (defun org-element-strike-through-parser ()
3183 "Parse strike-through object at point, if any.
3185 When at a strike-through object, return a list whose car is
3186 `strike-through' and cdr is a plist with `:begin', `:end',
3187 `:contents-begin' and `:contents-end' and `:post-blank' keywords.
3188 Otherwise, return nil.
3190 Assume point is at the first plus sign marker."
3192 (unless (bolp) (backward-char 1))
3193 (when (looking-at org-emph-re
)
3194 (let ((begin (match-beginning 2))
3195 (contents-begin (match-beginning 4))
3196 (contents-end (match-end 4))
3197 (post-blank (progn (goto-char (match-end 2))
3198 (skip-chars-forward " \t")))
3200 (list 'strike-through
3203 :contents-begin contents-begin
3204 :contents-end contents-end
3205 :post-blank post-blank
))))))
3207 (defun org-element-strike-through-interpreter (strike-through contents
)
3208 "Interpret STRIKE-THROUGH object as Org syntax.
3209 CONTENTS is the contents of the object."
3210 (format "+%s+" contents
))
3215 (defun org-element-subscript-parser ()
3216 "Parse subscript at point, if any.
3218 When at a subscript object, return a list whose car is
3219 `subscript' and cdr a plist with `:begin', `:end',
3220 `:contents-begin', `:contents-end', `:use-brackets-p' and
3221 `:post-blank' as keywords. Otherwise, return nil.
3223 Assume point is at the underscore."
3225 (unless (bolp) (backward-char))
3226 (when (looking-at org-match-substring-regexp
)
3227 (let ((bracketsp (match-beginning 4))
3228 (begin (match-beginning 2))
3229 (contents-begin (or (match-beginning 4)
3230 (match-beginning 3)))
3231 (contents-end (or (match-end 4) (match-end 3)))
3232 (post-blank (progn (goto-char (match-end 0))
3233 (skip-chars-forward " \t")))
3238 :use-brackets-p bracketsp
3239 :contents-begin contents-begin
3240 :contents-end contents-end
3241 :post-blank post-blank
))))))
3243 (defun org-element-subscript-interpreter (subscript contents
)
3244 "Interpret SUBSCRIPT object as Org syntax.
3245 CONTENTS is the contents of the object."
3247 (if (org-element-property :use-brackets-p subscript
) "_{%s}" "_%s")
3253 (defun org-element-superscript-parser ()
3254 "Parse superscript at point, if any.
3256 When at a superscript object, return a list whose car is
3257 `superscript' and cdr a plist with `:begin', `:end',
3258 `:contents-begin', `:contents-end', `:use-brackets-p' and
3259 `:post-blank' as keywords. Otherwise, return nil.
3261 Assume point is at the caret."
3263 (unless (bolp) (backward-char))
3264 (when (looking-at org-match-substring-regexp
)
3265 (let ((bracketsp (match-beginning 4))
3266 (begin (match-beginning 2))
3267 (contents-begin (or (match-beginning 4)
3268 (match-beginning 3)))
3269 (contents-end (or (match-end 4) (match-end 3)))
3270 (post-blank (progn (goto-char (match-end 0))
3271 (skip-chars-forward " \t")))
3276 :use-brackets-p bracketsp
3277 :contents-begin contents-begin
3278 :contents-end contents-end
3279 :post-blank post-blank
))))))
3281 (defun org-element-superscript-interpreter (superscript contents
)
3282 "Interpret SUPERSCRIPT object as Org syntax.
3283 CONTENTS is the contents of the object."
3285 (if (org-element-property :use-brackets-p superscript
) "^{%s}" "^%s")
3291 (defun org-element-table-cell-parser ()
3292 "Parse table cell at point.
3293 Return a list whose car is `table-cell' and cdr is a plist
3294 containing `:begin', `:end', `:contents-begin', `:contents-end'
3295 and `:post-blank' keywords."
3296 (looking-at "[ \t]*\\(.*?\\)[ \t]*\\(?:|\\|$\\)")
3297 (let* ((begin (match-beginning 0))
3299 (contents-begin (match-beginning 1))
3300 (contents-end (match-end 1)))
3304 :contents-begin contents-begin
3305 :contents-end contents-end
3308 (defun org-element-table-cell-interpreter (table-cell contents
)
3309 "Interpret TABLE-CELL element as Org syntax.
3310 CONTENTS is the contents of the cell, or nil."
3311 (concat " " contents
" |"))
3316 (defun org-element-target-parser ()
3317 "Parse target at point, if any.
3319 When at a target, return a list whose car is `target' and cdr
3320 a plist with `:begin', `:end', `:value' and `:post-blank' as
3321 keywords. Otherwise, return nil.
3323 Assume point is at the target."
3325 (when (looking-at org-target-regexp
)
3326 (let ((begin (point))
3327 (value (org-match-string-no-properties 1))
3328 (post-blank (progn (goto-char (match-end 0))
3329 (skip-chars-forward " \t")))
3335 :post-blank post-blank
))))))
3337 (defun org-element-target-interpreter (target contents
)
3338 "Interpret TARGET object as Org syntax.
3340 (format "<<%s>>" (org-element-property :value target
)))
3345 (defconst org-element--timestamp-regexp
3346 (concat org-ts-regexp-both
3348 "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
3350 "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
3351 "Regexp matching any timestamp type object.")
3353 (defun org-element-timestamp-parser ()
3354 "Parse time stamp at point, if any.
3356 When at a time stamp, return a list whose car is `timestamp', and
3357 cdr a plist with `:type', `:raw-value', `:year-start',
3358 `:month-start', `:day-start', `:hour-start', `:minute-start',
3359 `:year-end', `:month-end', `:day-end', `:hour-end',
3360 `:minute-end', `:repeater-type', `:repeater-value',
3361 `:repeater-unit', `:warning-type', `:warning-value',
3362 `:warning-unit', `:begin', `:end' and `:post-blank' keywords.
3363 Otherwise, return nil.
3365 Assume point is at the beginning of the timestamp."
3366 (when (org-looking-at-p org-element--timestamp-regexp
)
3368 (let* ((begin (point))
3369 (activep (eq (char-after) ?
<))
3372 (looking-at "\\([<[]\\(%%\\)?.*?\\)[]>]\\(?:--\\([<[].*?[]>]\\)\\)?")
3373 (match-string-no-properties 0)))
3374 (date-start (match-string-no-properties 1))
3375 (date-end (match-string 3))
3376 (diaryp (match-beginning 2))
3377 (post-blank (progn (goto-char (match-end 0))
3378 (skip-chars-forward " \t")))
3383 "[012]?[0-9]:[0-5][0-9]\\(-\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)"
3385 (cons (string-to-number (match-string 2 date-start
))
3386 (string-to-number (match-string 3 date-start
)))))
3387 (type (cond (diaryp 'diary
)
3388 ((and activep
(or date-end time-range
)) 'active-range
)
3390 ((or date-end time-range
) 'inactive-range
)
3394 (string-match "\\([.+]?\\+\\)\\([0-9]+\\)\\([hdwmy]\\)"
3398 (let ((type (match-string 1 raw-value
)))
3399 (cond ((equal "++" type
) 'catch-up
)
3400 ((equal ".+" type
) 'restart
)
3402 :repeater-value
(string-to-number (match-string 2 raw-value
))
3404 (case (string-to-char (match-string 3 raw-value
))
3405 (?h
'hour
) (?d
'day
) (?w
'week
) (?m
'month
) (t 'year
)))))
3408 (string-match "\\(-\\)?-\\([0-9]+\\)\\([hdwmy]\\)" raw-value
)
3410 :warning-type
(if (match-string 1 raw-value
) 'first
'all
)
3411 :warning-value
(string-to-number (match-string 2 raw-value
))
3413 (case (string-to-char (match-string 3 raw-value
))
3414 (?h
'hour
) (?d
'day
) (?w
'week
) (?m
'month
) (t 'year
)))))
3415 year-start month-start day-start hour-start minute-start year-end
3416 month-end day-end hour-end minute-end
)
3417 ;; Parse date-start.
3419 (let ((date (org-parse-time-string date-start t
)))
3420 (setq year-start
(nth 5 date
)
3421 month-start
(nth 4 date
)
3422 day-start
(nth 3 date
)
3423 hour-start
(nth 2 date
)
3424 minute-start
(nth 1 date
))))
3425 ;; Compute date-end. It can be provided directly in time-stamp,
3426 ;; or extracted from time range. Otherwise, it defaults to the
3427 ;; same values as date-start.
3429 (let ((date (and date-end
(org-parse-time-string date-end t
))))
3430 (setq year-end
(or (nth 5 date
) year-start
)
3431 month-end
(or (nth 4 date
) month-start
)
3432 day-end
(or (nth 3 date
) day-start
)
3433 hour-end
(or (nth 2 date
) (car time-range
) hour-start
)
3434 minute-end
(or (nth 1 date
) (cdr time-range
) minute-start
))))
3436 (nconc (list :type type
3437 :raw-value raw-value
3438 :year-start year-start
3439 :month-start month-start
3440 :day-start day-start
3441 :hour-start hour-start
3442 :minute-start minute-start
3444 :month-end month-end
3447 :minute-end minute-end
3450 :post-blank post-blank
)
3454 (defun org-element-timestamp-interpreter (timestamp contents
)
3455 "Interpret TIMESTAMP object as Org syntax.
3457 (let* ((repeat-string
3459 (case (org-element-property :repeater-type timestamp
)
3460 (cumulate "+") (catch-up "++") (restart ".+"))
3461 (let ((val (org-element-property :repeater-value timestamp
)))
3462 (and val
(number-to-string val
)))
3463 (case (org-element-property :repeater-unit timestamp
)
3464 (hour "h") (day "d") (week "w") (month "m") (year "y"))))
3467 (case (org-element-property :warning-type timestamp
)
3470 (let ((val (org-element-property :warning-value timestamp
)))
3471 (and val
(number-to-string val
)))
3472 (case (org-element-property :warning-unit timestamp
)
3473 (hour "h") (day "d") (week "w") (month "m") (year "y"))))
3475 ;; Build an Org timestamp string from TIME. ACTIVEP is
3476 ;; non-nil when time stamp is active. If WITH-TIME-P is
3477 ;; non-nil, add a time part. HOUR-END and MINUTE-END
3478 ;; specify a time range in the timestamp. REPEAT-STRING is
3479 ;; the repeater string, if any.
3480 (lambda (time activep
&optional with-time-p hour-end minute-end
)
3481 (let ((ts (format-time-string
3482 (funcall (if with-time-p
'cdr
'car
)
3483 org-time-stamp-formats
)
3485 (when (and hour-end minute-end
)
3486 (string-match "[012]?[0-9]:[0-5][0-9]" ts
)
3489 (format "\\&-%02d:%02d" hour-end minute-end
)
3491 (unless activep
(setq ts
(format "[%s]" (substring ts
1 -
1))))
3492 (dolist (s (list repeat-string warning-string
))
3493 (when (org-string-nw-p s
)
3494 (setq ts
(concat (substring ts
0 -
1)
3497 (substring ts -
1)))))
3500 (type (org-element-property :type timestamp
)))
3503 (let* ((minute-start (org-element-property :minute-start timestamp
))
3504 (minute-end (org-element-property :minute-end timestamp
))
3505 (hour-start (org-element-property :hour-start timestamp
))
3506 (hour-end (org-element-property :hour-end timestamp
))
3507 (time-range-p (and hour-start hour-end minute-start minute-end
3508 (or (/= hour-start hour-end
)
3509 (/= minute-start minute-end
)))))
3515 (org-element-property :day-start timestamp
)
3516 (org-element-property :month-start timestamp
)
3517 (org-element-property :year-start timestamp
))
3519 (and hour-start minute-start
)
3520 (and time-range-p hour-end
)
3521 (and time-range-p minute-end
))))
3522 ((active-range inactive-range
)
3523 (let ((minute-start (org-element-property :minute-start timestamp
))
3524 (minute-end (org-element-property :minute-end timestamp
))
3525 (hour-start (org-element-property :hour-start timestamp
))
3526 (hour-end (org-element-property :hour-end timestamp
)))
3529 build-ts-string
(encode-time
3533 (org-element-property :day-start timestamp
)
3534 (org-element-property :month-start timestamp
)
3535 (org-element-property :year-start timestamp
))
3536 (eq type
'active-range
)
3537 (and hour-start minute-start
))
3539 (funcall build-ts-string
3543 (org-element-property :day-end timestamp
)
3544 (org-element-property :month-end timestamp
)
3545 (org-element-property :year-end timestamp
))
3546 (eq type
'active-range
)
3547 (and hour-end minute-end
)))))
3548 (otherwise (org-element-property :raw-value timestamp
)))))
3553 (defun org-element-underline-parser ()
3554 "Parse underline object at point, if any.
3556 When at an underline object, return a list whose car is
3557 `underline' and cdr is a plist with `:begin', `:end',
3558 `:contents-begin' and `:contents-end' and `:post-blank' keywords.
3559 Otherwise, return nil.
3561 Assume point is at the first underscore marker."
3563 (unless (bolp) (backward-char 1))
3564 (when (looking-at org-emph-re
)
3565 (let ((begin (match-beginning 2))
3566 (contents-begin (match-beginning 4))
3567 (contents-end (match-end 4))
3568 (post-blank (progn (goto-char (match-end 2))
3569 (skip-chars-forward " \t")))
3574 :contents-begin contents-begin
3575 :contents-end contents-end
3576 :post-blank post-blank
))))))
3578 (defun org-element-underline-interpreter (underline contents
)
3579 "Interpret UNDERLINE object as Org syntax.
3580 CONTENTS is the contents of the object."
3581 (format "_%s_" contents
))
3586 (defun org-element-verbatim-parser ()
3587 "Parse verbatim object at point, if any.
3589 When at a verbatim object, return a list whose car is `verbatim'
3590 and cdr is a plist with `:value', `:begin', `:end' and
3591 `:post-blank' keywords. Otherwise, return nil.
3593 Assume point is at the first equal sign marker."
3595 (unless (bolp) (backward-char 1))
3596 (when (looking-at org-emph-re
)
3597 (let ((begin (match-beginning 2))
3598 (value (org-match-string-no-properties 4))
3599 (post-blank (progn (goto-char (match-end 2))
3600 (skip-chars-forward " \t")))
3606 :post-blank post-blank
))))))
3608 (defun org-element-verbatim-interpreter (verbatim contents
)
3609 "Interpret VERBATIM object as Org syntax.
3611 (format "=%s=" (org-element-property :value verbatim
)))
3615 ;;; Parsing Element Starting At Point
3617 ;; `org-element--current-element' is the core function of this section.
3618 ;; It returns the Lisp representation of the element starting at
3621 ;; `org-element--current-element' makes use of special modes. They
3622 ;; are activated for fixed element chaining (e.g., `plain-list' >
3623 ;; `item') or fixed conditional element chaining (e.g., `headline' >
3624 ;; `section'). Special modes are: `first-section', `item',
3625 ;; `node-property', `section' and `table-row'.
3627 (defun org-element--current-element (limit &optional granularity mode structure
)
3628 "Parse the element starting at point.
3630 Return value is a list like (TYPE PROPS) where TYPE is the type
3631 of the element and PROPS a plist of properties associated to the
3634 Possible types are defined in `org-element-all-elements'.
3636 LIMIT bounds the search.
3638 Optional argument GRANULARITY determines the depth of the
3639 recursion. Allowed values are `headline', `greater-element',
3640 `element', `object' or nil. When it is broader than `object' (or
3641 nil), secondary values will not be parsed, since they only
3644 Optional argument MODE, when non-nil, can be either
3645 `first-section', `section', `planning', `item', `node-property'
3648 If STRUCTURE isn't provided but MODE is set to `item', it will be
3651 This function assumes point is always at the beginning of the
3652 element it has to parse."
3654 (let ((case-fold-search t
)
3655 ;; Determine if parsing depth allows for secondary strings
3656 ;; parsing. It only applies to elements referenced in
3657 ;; `org-element-secondary-value-alist'.
3658 (raw-secondary-p (and granularity
(not (eq granularity
'object
)))))
3662 (org-element-item-parser limit structure raw-secondary-p
))
3664 ((eq mode
'table-row
) (org-element-table-row-parser limit
))
3666 ((eq mode
'node-property
) (org-element-node-property-parser limit
))
3668 ((org-with-limited-levels (org-at-heading-p))
3669 (org-element-headline-parser limit raw-secondary-p
))
3670 ;; Sections (must be checked after headline).
3671 ((eq mode
'section
) (org-element-section-parser limit
))
3672 ((eq mode
'first-section
)
3673 (org-element-section-parser
3674 (or (save-excursion (org-with-limited-levels (outline-next-heading)))
3677 ((and (eq mode
'planning
) (looking-at org-planning-line-re
))
3678 (org-element-planning-parser limit
))
3680 ((and (memq mode
'(planning property-drawer
))
3681 (looking-at org-property-drawer-re
))
3682 (org-element-property-drawer-parser limit
))
3683 ;; When not at bol, point is at the beginning of an item or
3684 ;; a footnote definition: next item is always a paragraph.
3685 ((not (bolp)) (org-element-paragraph-parser limit
(list (point))))
3687 ((looking-at org-clock-line-re
) (org-element-clock-parser limit
))
3690 (org-element-inlinetask-parser limit raw-secondary-p
))
3691 ;; From there, elements can have affiliated keywords.
3692 (t (let ((affiliated (org-element--collect-affiliated-keywords limit
)))
3694 ;; Jumping over affiliated keywords put point off-limits.
3695 ;; Parse them as regular keywords.
3696 ((and (cdr affiliated
) (>= (point) limit
))
3697 (goto-char (car affiliated
))
3698 (org-element-keyword-parser limit nil
))
3699 ;; LaTeX Environment.
3700 ((looking-at org-element--latex-begin-environment
)
3701 (org-element-latex-environment-parser limit affiliated
))
3702 ;; Drawer and Property Drawer.
3703 ((looking-at org-drawer-regexp
)
3704 (org-element-drawer-parser limit affiliated
))
3706 ((looking-at "[ \t]*:\\( \\|$\\)")
3707 (org-element-fixed-width-parser limit affiliated
))
3708 ;; Inline Comments, Blocks, Babel Calls, Dynamic Blocks and
3710 ((looking-at "[ \t]*#")
3711 (goto-char (match-end 0))
3712 (cond ((looking-at "\\(?: \\|$\\)")
3714 (org-element-comment-parser limit affiliated
))
3715 ((looking-at "\\+BEGIN_\\(\\S-+\\)")
3717 (let ((parser (assoc (upcase (match-string 1))
3718 org-element-block-name-alist
)))
3719 (if parser
(funcall (cdr parser
) limit affiliated
)
3720 (org-element-special-block-parser limit affiliated
))))
3721 ((looking-at "\\+CALL:")
3723 (org-element-babel-call-parser limit affiliated
))
3724 ((looking-at "\\+BEGIN:? ")
3726 (org-element-dynamic-block-parser limit affiliated
))
3727 ((looking-at "\\+\\S-+:")
3729 (org-element-keyword-parser limit affiliated
))
3732 (org-element-paragraph-parser limit affiliated
))))
3733 ;; Footnote Definition.
3734 ((looking-at org-footnote-definition-re
)
3735 (org-element-footnote-definition-parser limit affiliated
))
3737 ((looking-at "[ \t]*-\\{5,\\}[ \t]*$")
3738 (org-element-horizontal-rule-parser limit affiliated
))
3741 (org-element-diary-sexp-parser limit affiliated
))
3743 ((org-at-table-p t
) (org-element-table-parser limit affiliated
))
3745 ((looking-at (org-item-re))
3746 (org-element-plain-list-parser
3748 (or structure
(org-element--list-struct limit
))))
3749 ;; Default element: Paragraph.
3750 (t (org-element-paragraph-parser limit affiliated
)))))))))
3753 ;; Most elements can have affiliated keywords. When looking for an
3754 ;; element beginning, we want to move before them, as they belong to
3755 ;; that element, and, in the meantime, collect information they give
3756 ;; into appropriate properties. Hence the following function.
3758 (defun org-element--collect-affiliated-keywords (limit)
3759 "Collect affiliated keywords from point down to LIMIT.
3761 Return a list whose CAR is the position at the first of them and
3762 CDR a plist of keywords and values and move point to the
3763 beginning of the first line after them.
3765 As a special case, if element doesn't start at the beginning of
3766 the line (e.g., a paragraph starting an item), CAR is current
3767 position of point and CDR is nil."
3768 (if (not (bolp)) (list (point))
3769 (let ((case-fold-search t
)
3771 ;; RESTRICT is the list of objects allowed in parsed
3773 (restrict (org-element-restriction 'keyword
))
3775 (while (and (< (point) limit
) (looking-at org-element--affiliated-re
))
3776 (let* ((raw-kwd (upcase (match-string 1)))
3777 ;; Apply translation to RAW-KWD. From there, KWD is
3778 ;; the official keyword.
3779 (kwd (or (cdr (assoc raw-kwd
3780 org-element-keyword-translation-alist
))
3782 ;; Find main value for any keyword.
3786 (buffer-substring-no-properties
3787 (match-end 0) (point-at-eol)))))
3788 ;; PARSEDP is non-nil when keyword should have its
3790 (parsedp (member kwd org-element-parsed-keywords
))
3791 ;; If KWD is a dual keyword, find its secondary
3792 ;; value. Maybe parse it.
3793 (dualp (member kwd org-element-dual-keywords
))
3796 (let ((sec (org-match-string-no-properties 2)))
3797 (if (or (not sec
) (not parsedp
)) sec
3798 (org-element-parse-secondary-string sec restrict
)))))
3799 ;; Attribute a property name to KWD.
3800 (kwd-sym (and kwd
(intern (concat ":" (downcase kwd
))))))
3801 ;; Now set final shape for VALUE.
3803 (setq value
(org-element-parse-secondary-string value restrict
)))
3805 (setq value
(and (or value dual-value
) (cons value dual-value
))))
3806 (when (or (member kwd org-element-multiple-keywords
)
3807 ;; Attributes can always appear on multiple lines.
3808 (string-match "^ATTR_" kwd
))
3809 (setq value
(cons value
(plist-get output kwd-sym
))))
3810 ;; Eventually store the new value in OUTPUT.
3811 (setq output
(plist-put output kwd-sym value
))
3812 ;; Move to next keyword.
3814 ;; If affiliated keywords are orphaned: move back to first one.
3815 ;; They will be parsed as a paragraph.
3816 (when (looking-at "[ \t]*$") (goto-char origin
) (setq output nil
))
3818 (cons origin output
))))
3824 ;; The two major functions here are `org-element-parse-buffer', which
3825 ;; parses Org syntax inside the current buffer, taking into account
3826 ;; region, narrowing, or even visibility if specified, and
3827 ;; `org-element-parse-secondary-string', which parses objects within
3830 ;; The (almost) almighty `org-element-map' allows to apply a function
3831 ;; on elements or objects matching some type, and accumulate the
3832 ;; resulting values. In an export situation, it also skips unneeded
3833 ;; parts of the parse tree.
3835 (defun org-element-parse-buffer (&optional granularity visible-only
)
3836 "Recursively parse the buffer and return structure.
3837 If narrowing is in effect, only parse the visible part of the
3840 Optional argument GRANULARITY determines the depth of the
3841 recursion. It can be set to the following symbols:
3843 `headline' Only parse headlines.
3844 `greater-element' Don't recurse into greater elements excepted
3845 headlines and sections. Thus, elements
3846 parsed are the top-level ones.
3847 `element' Parse everything but objects and plain text.
3848 `object' Parse the complete buffer (default).
3850 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3853 An element or an objects is represented as a list with the
3854 pattern (TYPE PROPERTIES CONTENTS), where :
3856 TYPE is a symbol describing the element or object. See
3857 `org-element-all-elements' and `org-element-all-objects' for an
3858 exhaustive list of such symbols. One can retrieve it with
3859 `org-element-type' function.
3861 PROPERTIES is the list of attributes attached to the element or
3862 object, as a plist. Although most of them are specific to the
3863 element or object type, all types share `:begin', `:end',
3864 `:post-blank' and `:parent' properties, which respectively
3865 refer to buffer position where the element or object starts,
3866 ends, the number of white spaces or blank lines after it, and
3867 the element or object containing it. Properties values can be
3868 obtained by using `org-element-property' function.
3870 CONTENTS is a list of elements, objects or raw strings
3871 contained in the current element or object, when applicable.
3872 One can access them with `org-element-contents' function.
3874 The Org buffer has `org-data' as type and nil as properties.
3875 `org-element-map' function can be used to find specific elements
3876 or objects within the parse tree.
3878 This function assumes that current major mode is `org-mode'."
3880 (goto-char (point-min))
3881 (org-skip-whitespace)
3882 (org-element--parse-elements
3883 (point-at-bol) (point-max)
3884 ;; Start in `first-section' mode so text before the first
3885 ;; headline belongs to a section.
3886 'first-section nil granularity visible-only
(list 'org-data nil
))))
3888 (defun org-element-parse-secondary-string (string restriction
&optional parent
)
3889 "Recursively parse objects in STRING and return structure.
3891 RESTRICTION is a symbol limiting the object types that will be
3894 Optional argument PARENT, when non-nil, is the element or object
3895 containing the secondary string. It is used to set correctly
3896 `:parent' property within the string."
3897 (let ((local-variables (buffer-local-variables)))
3899 (dolist (v local-variables
)
3901 (if (symbolp v
) (makunbound v
)
3902 (org-set-local (car v
) (cdr v
)))))
3904 (restore-buffer-modified-p nil
)
3905 (let ((secondary (org-element--parse-objects
3906 (point-min) (point-max) nil restriction
)))
3908 (dolist (o secondary
) (org-element-put-property o
:parent parent
)))
3911 (defun org-element-map
3912 (data types fun
&optional info first-match no-recursion with-affiliated
)
3913 "Map a function on selected elements or objects.
3915 DATA is a parse tree, an element, an object, a string, or a list
3916 of such constructs. TYPES is a symbol or list of symbols of
3917 elements or objects types (see `org-element-all-elements' and
3918 `org-element-all-objects' for a complete list of types). FUN is
3919 the function called on the matching element or object. It has to
3920 accept one argument: the element or object itself.
3922 When optional argument INFO is non-nil, it should be a plist
3923 holding export options. In that case, parts of the parse tree
3924 not exportable according to that property list will be skipped.
3926 When optional argument FIRST-MATCH is non-nil, stop at the first
3927 match for which FUN doesn't return nil, and return that value.
3929 Optional argument NO-RECURSION is a symbol or a list of symbols
3930 representing elements or objects types. `org-element-map' won't
3931 enter any recursive element or object whose type belongs to that
3932 list. Though, FUN can still be applied on them.
3934 When optional argument WITH-AFFILIATED is non-nil, FUN will also
3935 apply to matching objects within parsed affiliated keywords (see
3936 `org-element-parsed-keywords').
3938 Nil values returned from FUN do not appear in the results.
3944 Assuming TREE is a variable containing an Org buffer parse tree,
3945 the following example will return a flat list of all `src-block'
3946 and `example-block' elements in it:
3948 \(org-element-map tree '(example-block src-block) #'identity)
3950 The following snippet will find the first headline with a level
3951 of 1 and a \"phone\" tag, and will return its beginning position:
3953 \(org-element-map tree 'headline
3955 \(and (= (org-element-property :level hl) 1)
3956 \(member \"phone\" (org-element-property :tags hl))
3957 \(org-element-property :begin hl)))
3960 The next example will return a flat list of all `plain-list' type
3961 elements in TREE that are not a sub-list themselves:
3963 \(org-element-map tree 'plain-list #'identity nil nil 'plain-list)
3965 Eventually, this example will return a flat list of all `bold'
3966 type objects containing a `latex-snippet' type object, even
3967 looking into captions:
3969 \(org-element-map tree 'bold
3971 \(and (org-element-map b 'latex-snippet #'identity nil t) b))
3973 ;; Ensure TYPES and NO-RECURSION are a list, even of one element.
3974 (let* ((types (if (listp types
) types
(list types
)))
3975 (no-recursion (if (listp no-recursion
) no-recursion
3976 (list no-recursion
)))
3977 ;; Recursion depth is determined by --CATEGORY.
3980 (let ((category 'greater-elements
)
3981 (all-objects (cons 'plain-text org-element-all-objects
)))
3982 (dolist (type types category
)
3983 (cond ((memq type all-objects
)
3984 ;; If one object is found, the function has to
3985 ;; recurse into every object.
3986 (throw 'found
'objects
))
3987 ((not (memq type org-element-greater-elements
))
3988 ;; If one regular element is found, the
3989 ;; function has to recurse, at least, into
3990 ;; every element it encounters.
3991 (and (not (eq category
'elements
))
3992 (setq category
'elements
))))))))
3997 ;; Recursively walk DATA. INFO, if non-nil, is a plist
3998 ;; holding contextual information.
3999 (let ((--type (org-element-type --data
)))
4002 ;; Ignored element in an export context.
4003 ((and info
(memq --data
(plist-get info
:ignore-list
))))
4004 ;; List of elements or objects.
4005 ((not --type
) (mapc --walk-tree --data
))
4006 ;; Unconditionally enter parse trees.
4007 ((eq --type
'org-data
)
4008 (mapc --walk-tree
(org-element-contents --data
)))
4010 ;; Check if TYPE is matching among TYPES. If so,
4011 ;; apply FUN to --DATA and accumulate return value
4012 ;; into --ACC (or exit if FIRST-MATCH is non-nil).
4013 (when (memq --type types
)
4014 (let ((result (funcall fun --data
)))
4015 (cond ((not result
))
4016 (first-match (throw '--map-first-match result
))
4017 (t (push result --acc
)))))
4018 ;; If --DATA has a secondary string that can contain
4019 ;; objects with their type among TYPES, look into it.
4020 (when (and (eq --category
'objects
) (not (stringp --data
)))
4022 (assq --type org-element-secondary-value-alist
)))
4024 (funcall --walk-tree
4025 (org-element-property (cdr sec-prop
) --data
)))))
4026 ;; If --DATA has any parsed affiliated keywords and
4027 ;; WITH-AFFILIATED is non-nil, look for objects in
4029 (when (and with-affiliated
4030 (eq --category
'objects
)
4031 (memq --type org-element-all-elements
))
4032 (dolist (kwd-pair org-element--parsed-properties-alist
)
4033 (let ((kwd (car kwd-pair
))
4034 (value (org-element-property (cdr kwd-pair
) --data
)))
4035 ;; Pay attention to the type of parsed keyword.
4036 ;; In particular, preserve order for multiple
4040 ((member kwd org-element-dual-keywords
)
4042 (line (if (member kwd org-element-multiple-keywords
)
4045 (funcall --walk-tree
(cdr line
))
4046 (funcall --walk-tree
(car line
))))
4047 ((member kwd org-element-multiple-keywords
)
4048 (mapc --walk-tree
(reverse value
)))
4049 (t (funcall --walk-tree value
))))))
4050 ;; Determine if a recursion into --DATA is possible.
4052 ;; --TYPE is explicitly removed from recursion.
4053 ((memq --type no-recursion
))
4054 ;; --DATA has no contents.
4055 ((not (org-element-contents --data
)))
4056 ;; Looking for greater elements but --DATA is simply
4057 ;; an element or an object.
4058 ((and (eq --category
'greater-elements
)
4059 (not (memq --type org-element-greater-elements
))))
4060 ;; Looking for elements but --DATA is an object.
4061 ((and (eq --category
'elements
)
4062 (memq --type org-element-all-objects
)))
4063 ;; In any other case, map contents.
4064 (t (mapc --walk-tree
(org-element-contents --data
))))))))))
4065 (catch '--map-first-match
4066 (funcall --walk-tree data
)
4067 ;; Return value in a proper order.
4069 (put 'org-element-map
'lisp-indent-function
2)
4071 ;; The following functions are internal parts of the parser.
4073 ;; The first one, `org-element--parse-elements' acts at the element's
4076 ;; The second one, `org-element--parse-objects' applies on all objects
4077 ;; of a paragraph or a secondary string. It calls
4078 ;; `org-element--object-lex' to find the next object in the current
4081 (defsubst org-element--next-mode
(type parentp
)
4082 "Return next special mode according to TYPE, or nil.
4083 TYPE is a symbol representing the type of an element or object
4084 containing next element if PARENTP is non-nil, or before it
4085 otherwise. Modes can be either `first-section', `item',
4086 `node-property', `planning', `property-drawer', `section',
4087 `table-row' or nil."
4092 (property-drawer 'node-property
)
4097 (node-property 'node-property
)
4098 (planning 'property-drawer
)
4099 (table-row 'table-row
))))
4101 (defun org-element--parse-elements
4102 (beg end mode structure granularity visible-only acc
)
4103 "Parse elements between BEG and END positions.
4105 MODE prioritizes some elements over the others. It can be set to
4106 `first-section', `section', `planning', `item', `node-property'
4109 When value is `item', STRUCTURE will be used as the current list
4112 GRANULARITY determines the depth of the recursion. See
4113 `org-element-parse-buffer' for more information.
4115 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
4118 Elements are accumulated into ACC."
4121 ;; Visible only: skip invisible parts at the beginning of the
4123 (when (and visible-only
(org-invisible-p2))
4124 (goto-char (min (1+ (org-find-visible)) end
)))
4125 ;; When parsing only headlines, skip any text before first one.
4126 (when (and (eq granularity
'headline
) (not (org-at-heading-p)))
4127 (org-with-limited-levels (outline-next-heading)))
4129 (while (< (point) end
)
4130 ;; Find current element's type and parse it accordingly to
4132 (let* ((element (org-element--current-element
4133 end granularity mode structure
))
4134 (type (org-element-type element
))
4135 (cbeg (org-element-property :contents-begin element
)))
4136 (goto-char (org-element-property :end element
))
4137 ;; Visible only: skip invisible parts between siblings.
4138 (when (and visible-only
(org-invisible-p2))
4139 (goto-char (min (1+ (org-find-visible)) end
)))
4140 ;; Fill ELEMENT contents by side-effect.
4142 ;; If element has no contents, don't modify it.
4144 ;; Greater element: parse it between `contents-begin' and
4145 ;; `contents-end'. Make sure GRANULARITY allows the
4146 ;; recursion, or ELEMENT is a headline, in which case going
4147 ;; inside is mandatory, in order to get sub-level headings.
4148 ((and (memq type org-element-greater-elements
)
4149 (or (memq granularity
'(element object nil
))
4150 (and (eq granularity
'greater-element
)
4152 (eq type
'headline
)))
4153 (org-element--parse-elements
4154 cbeg
(org-element-property :contents-end element
)
4155 ;; Possibly switch to a special mode.
4156 (org-element--next-mode type t
)
4157 (and (memq type
'(item plain-list
))
4158 (org-element-property :structure element
))
4159 granularity visible-only element
))
4160 ;; ELEMENT has contents. Parse objects inside, if
4161 ;; GRANULARITY allows it.
4162 ((memq granularity
'(object nil
))
4163 (org-element--parse-objects
4164 cbeg
(org-element-property :contents-end element
) element
4165 (org-element-restriction type
))))
4166 (org-element-adopt-elements acc element
)
4168 (setq mode
(org-element--next-mode type nil
))))
4172 (defun org-element--object-lex (restriction)
4173 "Return next object in current buffer or nil.
4174 RESTRICTION is a list of object types, as symbols, that should be
4175 looked after. This function assumes that the buffer is narrowed
4176 to an appropriate container (e.g., a paragraph)."
4177 (if (memq 'table-cell restriction
) (org-element-table-cell-parser)
4179 (let ((limit (and org-target-link-regexp
4181 (or (bolp) (backward-char))
4182 (re-search-forward org-target-link-regexp nil t
))
4183 (match-beginning 1)))
4185 (while (and (not found
)
4186 (re-search-forward org-element--object-regexp limit t
))
4187 (goto-char (match-beginning 0))
4188 (let ((result (match-string 0)))
4191 ((eq (compare-strings result nil nil
"call_" nil nil t
) t
)
4192 (and (memq 'inline-babel-call restriction
)
4193 (org-element-inline-babel-call-parser)))
4194 ((eq (compare-strings result nil nil
"src_" nil nil t
) t
)
4195 (and (memq 'inline-src-block restriction
)
4196 (org-element-inline-src-block-parser)))
4199 (?^
(and (memq 'superscript restriction
)
4200 (org-element-superscript-parser)))
4201 (?_
(or (and (memq 'subscript restriction
)
4202 (org-element-subscript-parser))
4203 (and (memq 'underline restriction
)
4204 (org-element-underline-parser))))
4205 (?
* (and (memq 'bold restriction
)
4206 (org-element-bold-parser)))
4207 (?
/ (and (memq 'italic restriction
)
4208 (org-element-italic-parser)))
4209 (?~
(and (memq 'code restriction
)
4210 (org-element-code-parser)))
4211 (?
= (and (memq 'verbatim restriction
)
4212 (org-element-verbatim-parser)))
4213 (?
+ (and (memq 'strike-through restriction
)
4214 (org-element-strike-through-parser)))
4215 (?
@ (and (memq 'export-snippet restriction
)
4216 (org-element-export-snippet-parser)))
4217 (?
{ (and (memq 'macro restriction
)
4218 (org-element-macro-parser)))
4219 (?$
(and (memq 'latex-fragment restriction
)
4220 (org-element-latex-fragment-parser)))
4222 (if (eq (aref result
1) ?
<)
4223 (or (and (memq 'radio-target restriction
)
4224 (org-element-radio-target-parser))
4225 (and (memq 'target restriction
)
4226 (org-element-target-parser)))
4227 (or (and (memq 'timestamp restriction
)
4228 (org-element-timestamp-parser))
4229 (and (memq 'link restriction
)
4230 (org-element-link-parser)))))
4232 (if (eq (aref result
1) ?
\\)
4233 (and (memq 'line-break restriction
)
4234 (org-element-line-break-parser))
4235 (or (and (memq 'entity restriction
)
4236 (org-element-entity-parser))
4237 (and (memq 'latex-fragment restriction
)
4238 (org-element-latex-fragment-parser)))))
4240 (if (eq (aref result
1) ?\
[)
4241 (and (memq 'link restriction
)
4242 (org-element-link-parser))
4243 (or (and (memq 'footnote-reference restriction
)
4244 (org-element-footnote-reference-parser))
4245 (and (memq 'timestamp restriction
)
4246 (org-element-timestamp-parser))
4247 (and (memq 'statistics-cookie restriction
)
4248 (org-element-statistics-cookie-parser)))))
4249 ;; This is probably a plain link.
4250 (otherwise (and (or (memq 'link restriction
)
4251 (memq 'plain-link restriction
))
4252 (org-element-link-parser)))))))
4253 (or (eobp) (forward-char))))
4256 ((and limit
(memq 'link restriction
))
4257 (goto-char limit
) (org-element-link-parser)))))))
4259 (defun org-element--parse-objects (beg end acc restriction
)
4260 "Parse objects between BEG and END and return recursive structure.
4262 Objects are accumulated in ACC.
4264 RESTRICTION is a list of object successors which are allowed in
4265 the current object."
4268 (narrow-to-region beg end
)
4269 (goto-char (point-min))
4271 (while (and (not (eobp))
4272 (setq next-object
(org-element--object-lex restriction
)))
4273 ;; 1. Text before any object. Untabify it.
4274 (let ((obj-beg (org-element-property :begin next-object
)))
4275 (unless (= (point) obj-beg
)
4277 (org-element-adopt-elements
4279 (replace-regexp-in-string
4280 "\t" (make-string tab-width ?
)
4281 (buffer-substring-no-properties (point) obj-beg
))))))
4283 (let ((obj-end (org-element-property :end next-object
))
4284 (cont-beg (org-element-property :contents-begin next-object
)))
4285 ;; Fill contents of NEXT-OBJECT by side-effect, if it has
4286 ;; a recursive type.
4288 (memq (car next-object
) org-element-recursive-objects
))
4289 (org-element--parse-objects
4290 cont-beg
(org-element-property :contents-end next-object
)
4291 next-object
(org-element-restriction next-object
)))
4292 (setq acc
(org-element-adopt-elements acc next-object
))
4293 (goto-char obj-end
))))
4294 ;; 3. Text after last object. Untabify it.
4297 (org-element-adopt-elements
4299 (replace-regexp-in-string
4300 "\t" (make-string tab-width ?
)
4301 (buffer-substring-no-properties (point) end
)))))
4307 ;;; Towards A Bijective Process
4309 ;; The parse tree obtained with `org-element-parse-buffer' is really
4310 ;; a snapshot of the corresponding Org buffer. Therefore, it can be
4311 ;; interpreted and expanded into a string with canonical Org syntax.
4312 ;; Hence `org-element-interpret-data'.
4314 ;; The function relies internally on
4315 ;; `org-element--interpret-affiliated-keywords'.
4318 (defun org-element-interpret-data (data &optional pseudo-objects
)
4319 "Interpret DATA as Org syntax.
4321 DATA is a parse tree, an element, an object or a secondary string
4324 Optional argument PSEUDO-OBJECTS is a list of symbols defining
4325 new types that should be treated as objects. An unknown type not
4326 belonging to this list is seen as a pseudo-element instead. Both
4327 pseudo-objects and pseudo-elements are transparent entities, i.e.
4328 only their contents are interpreted.
4330 Return Org syntax as a string."
4331 (org-element--interpret-data-1 data nil pseudo-objects
))
4333 (defun org-element--interpret-data-1 (data parent pseudo-objects
)
4334 "Interpret DATA as Org syntax.
4336 DATA is a parse tree, an element, an object or a secondary string
4337 to interpret. PARENT is used for recursive calls. It contains
4338 the element or object containing data, or nil. PSEUDO-OBJECTS
4339 are list of symbols defining new element or object types.
4340 Unknown types that don't belong to this list are treated as
4341 pseudo-elements instead.
4343 Return Org syntax as a string."
4344 (let* ((type (org-element-type data
))
4345 ;; Find interpreter for current object or element. If it
4346 ;; doesn't exist (e.g. this is a pseudo object or element),
4347 ;; return contents, if any.
4349 (let ((fun (intern (format "org-element-%s-interpreter" type
))))
4350 (if (fboundp fun
) fun
(lambda (data contents
) contents
))))
4353 ;; Secondary string.
4357 (org-element--interpret-data-1 obj parent pseudo-objects
))
4359 ;; Full Org document.
4360 ((eq type
'org-data
)
4363 (org-element--interpret-data-1 obj parent pseudo-objects
))
4364 (org-element-contents data
) ""))
4365 ;; Plain text: return it.
4366 ((stringp data
) data
)
4367 ;; Element or object without contents.
4368 ((not (org-element-contents data
)) (funcall interpret data nil
))
4369 ;; Element or object with contents.
4371 (funcall interpret data
4372 ;; Recursively interpret contents.
4375 (org-element--interpret-data-1 obj data pseudo-objects
))
4376 (org-element-contents
4377 (if (not (memq type
'(paragraph verse-block
)))
4379 ;; Fix indentation of elements containing
4380 ;; objects. We ignore `table-row' elements
4381 ;; as they are one line long anyway.
4382 (org-element-normalize-contents
4384 ;; When normalizing first paragraph of an
4385 ;; item or a footnote-definition, ignore
4386 ;; first line's indentation.
4387 (and (eq type
'paragraph
)
4388 (equal data
(car (org-element-contents parent
)))
4389 (memq (org-element-type parent
)
4390 '(footnote-definition item
))))))
4392 (if (memq type
'(org-data plain-text nil
)) results
4393 ;; Build white spaces. If no `:post-blank' property is
4394 ;; specified, assume its value is 0.
4395 (let ((post-blank (or (org-element-property :post-blank data
) 0)))
4396 (if (or (memq type org-element-all-objects
)
4397 (memq type pseudo-objects
))
4398 (concat results
(make-string post-blank ?\s
))
4400 (org-element--interpret-affiliated-keywords data
)
4401 (org-element-normalize-string results
)
4402 (make-string post-blank ?
\n)))))))
4404 (defun org-element--interpret-affiliated-keywords (element)
4405 "Return ELEMENT's affiliated keywords as Org syntax.
4406 If there is no affiliated keyword, return the empty string."
4407 (let ((keyword-to-org
4411 (when (member key org-element-dual-keywords
)
4412 (setq dual
(cdr value
) value
(car value
)))
4415 (format "[%s]" (org-element-interpret-data dual
)))
4417 (if (member key org-element-parsed-keywords
)
4418 (org-element-interpret-data value
)
4423 (let ((value (org-element-property prop element
))
4424 (keyword (upcase (substring (symbol-name prop
) 1))))
4426 (if (or (member keyword org-element-multiple-keywords
)
4427 ;; All attribute keywords can have multiple lines.
4428 (string-match "^ATTR_" keyword
))
4429 (mapconcat (lambda (line) (funcall keyword-to-org keyword line
))
4432 (funcall keyword-to-org keyword value
)))))
4433 ;; List all ELEMENT's properties matching an attribute line or an
4434 ;; affiliated keyword, but ignore translated keywords since they
4435 ;; cannot belong to the property list.
4436 (loop for prop in
(nth 1 element
) by
'cddr
4437 when
(let ((keyword (upcase (substring (symbol-name prop
) 1))))
4438 (or (string-match "^ATTR_" keyword
)
4440 (member keyword org-element-affiliated-keywords
)
4442 org-element-keyword-translation-alist
)))))
4446 ;; Because interpretation of the parse tree must return the same
4447 ;; number of blank lines between elements and the same number of white
4448 ;; space after objects, some special care must be given to white
4451 ;; The first function, `org-element-normalize-string', ensures any
4452 ;; string different from the empty string will end with a single
4453 ;; newline character.
4455 ;; The second function, `org-element-normalize-contents', removes
4456 ;; global indentation from the contents of the current element.
4458 (defun org-element-normalize-string (s)
4459 "Ensure string S ends with a single newline character.
4461 If S isn't a string return it unchanged. If S is the empty
4462 string, return it. Otherwise, return a new string with a single
4463 newline character at its end."
4465 ((not (stringp s
)) s
)
4467 (t (and (string-match "\\(\n[ \t]*\\)*\\'" s
)
4468 (replace-match "\n" nil nil s
)))))
4470 (defun org-element-normalize-contents (element &optional ignore-first
)
4471 "Normalize plain text in ELEMENT's contents.
4473 ELEMENT must only contain plain text and objects.
4475 If optional argument IGNORE-FIRST is non-nil, ignore first line's
4476 indentation to compute maximal common indentation.
4478 Return the normalized element that is element with global
4479 indentation removed from its contents. The function assumes that
4480 indentation is not done with TAB characters."
4481 (let* ((min-ind most-positive-fixnum
)
4482 find-min-ind
; For byte-compiler.
4484 ;; Return minimal common indentation within BLOB. This is
4485 ;; done by walking recursively BLOB and updating MIN-IND
4486 ;; along the way. FIRST-FLAG is non-nil when the first
4487 ;; string hasn't been seen yet. It is required as this
4488 ;; string is the only one whose indentation doesn't happen
4489 ;; after a newline character.
4490 (lambda (blob first-flag
)
4491 (dolist (object (org-element-contents blob
))
4492 (when (and first-flag
(stringp object
))
4493 (setq first-flag nil
)
4494 (string-match "\\` *" object
)
4495 (let ((len (match-end 0)))
4496 ;; An indentation of zero means no string will be
4497 ;; modified. Quit the process.
4498 (if (zerop len
) (throw 'zero
(setq min-ind
0))
4499 (setq min-ind
(min len min-ind
)))))
4502 (dolist (line (cdr (org-split-string object
" *\n")))
4503 (unless (string= line
"")
4504 (setq min-ind
(min (org-get-indentation line
) min-ind
)))))
4505 ((memq (org-element-type object
) org-element-recursive-objects
)
4506 (funcall find-min-ind object first-flag
)))))))
4507 ;; Find minimal indentation in ELEMENT.
4508 (catch 'zero
(funcall find-min-ind element
(not ignore-first
)))
4509 (if (or (zerop min-ind
) (= min-ind most-positive-fixnum
)) element
4510 ;; Build ELEMENT back, replacing each string with the same
4511 ;; string minus common indentation.
4512 (let* (build ; For byte compiler.
4515 (lambda (blob first-flag
)
4516 ;; Return BLOB with all its strings indentation
4517 ;; shortened from MIN-IND white spaces. FIRST-FLAG
4518 ;; is non-nil when the first string hasn't been seen
4523 (when (and first-flag
(stringp object
))
4524 (setq first-flag nil
)
4526 (replace-regexp-in-string
4527 (format "\\` \\{%d\\}" min-ind
)
4531 (replace-regexp-in-string
4532 (format "\n \\{%d\\}" min-ind
) "\n" object
))
4533 ((memq (org-element-type object
)
4534 org-element-recursive-objects
)
4535 (funcall build object first-flag
))
4537 (org-element-contents blob
)))
4539 (funcall build element
(not ignore-first
))))))
4545 ;; Implement a caching mechanism for `org-element-at-point' and
4546 ;; `org-element-context', which see.
4548 ;; A single public function is provided: `org-element-cache-reset'.
4550 ;; Cache is enabled by default, but can be disabled globally with
4551 ;; `org-element-use-cache'. `org-element-cache-sync-idle-time',
4552 ;; org-element-cache-sync-duration' and `org-element-cache-sync-break'
4553 ;; can be tweaked to control caching behaviour.
4555 ;; Internally, parsed elements are stored in an AVL tree,
4556 ;; `org-element--cache'. This tree is updated lazily: whenever
4557 ;; a change happens to the buffer, a synchronization request is
4558 ;; registered in `org-element--cache-sync-requests' (see
4559 ;; `org-element--cache-submit-request'). During idle time, requests
4560 ;; are processed by `org-element--cache-sync'. Synchronization also
4561 ;; happens when an element is required from the cache. In this case,
4562 ;; the process stops as soon as the needed element is up-to-date.
4564 ;; A synchronization request can only apply on a synchronized part of
4565 ;; the cache. Therefore, the cache is updated at least to the
4566 ;; location where the new request applies. Thus, requests are ordered
4567 ;; from left to right and all elements starting before the first
4568 ;; request are correct. This property is used by functions like
4569 ;; `org-element--cache-find' to retrieve elements in the part of the
4570 ;; cache that can be trusted.
4572 ;; A request applies to every element, starting from its original
4573 ;; location (or key, see below). When a request is processed, it
4574 ;; moves forward and may collide the next one. In this case, both
4575 ;; requests are merged into a new one that starts from that element.
4576 ;; As a consequence, the whole synchronization complexity does not
4577 ;; depend on the number of pending requests, but on the number of
4578 ;; elements the very first request will be applied on.
4580 ;; Elements cannot be accessed through their beginning position, which
4581 ;; may or may not be up-to-date. Instead, each element in the tree is
4582 ;; associated to a key, obtained with `org-element--cache-key'. This
4583 ;; mechanism is robust enough to preserve total order among elements
4584 ;; even when the tree is only partially synchronized.
4586 ;; Objects contained in an element are stored in a hash table,
4587 ;; `org-element--cache-objects'.
4590 (defvar org-element-use-cache t
4591 "Non nil when Org parser should cache its results.
4592 This is mostly for debugging purpose.")
4594 (defvar org-element-cache-sync-idle-time
0.6
4595 "Length, in seconds, of idle time before syncing cache.")
4597 (defvar org-element-cache-sync-duration
(seconds-to-time 0.04)
4598 "Maximum duration, as a time value, for a cache synchronization.
4599 If the synchronization is not over after this delay, the process
4600 pauses and resumes after `org-element-cache-sync-break'
4603 (defvar org-element-cache-sync-break
(seconds-to-time 0.3)
4604 "Duration, as a time value, of the pause between synchronizations.
4605 See `org-element-cache-sync-duration' for more information.")
4610 (defvar org-element--cache nil
4611 "AVL tree used to cache elements.
4612 Each node of the tree contains an element. Comparison is done
4613 with `org-element--cache-compare'. This cache is used in
4614 `org-element-at-point'.")
4616 (defvar org-element--cache-objects nil
4617 "Hash table used as to cache objects.
4618 Key is an element, as returned by `org-element-at-point', and
4619 value is an alist where each association is:
4621 \(PARENT COMPLETEP . OBJECTS)
4623 where PARENT is an element or object, COMPLETEP is a boolean,
4624 non-nil when all direct children of parent are already cached and
4625 OBJECTS is a list of such children, as objects, from farthest to
4628 In the following example, \\alpha, bold object and \\beta are
4629 contained within a paragraph
4633 If the paragraph is completely parsed, OBJECTS-DATA will be
4635 \((PARAGRAPH t BOLD-OBJECT ENTITY-OBJECT)
4636 \(BOLD-OBJECT t ENTITY-OBJECT))
4638 whereas in a partially parsed paragraph, it could be
4640 \((PARAGRAPH nil ENTITY-OBJECT))
4642 This cache is used in `org-element-context'.")
4644 (defvar org-element--cache-sync-requests nil
4645 "List of pending synchronization requests.
4647 A request is a vector with the following pattern:
4649 \[NEXT BEG END OFFSET OUTREACH PARENT PHASE]
4651 Processing a synchronization request consists of three phases:
4653 0. Delete modified elements,
4654 1. Fill missing area in cache,
4655 2. Shift positions and re-parent elements after the changes.
4657 During phase 0, NEXT is the key of the first element to be
4658 removed, BEG and END is buffer position delimiting the
4659 modifications. Elements starting between them (inclusive) are
4660 removed and so are those contained within OUTREACH. PARENT, when
4661 non-nil, is the parent of the first element to be removed.
4663 During phase 1, NEXT is the key of the next known element in
4664 cache and BEG its beginning position. Parse buffer between that
4665 element and the one before it in order to determine the parent of
4666 the next element. Set PARENT to the element containing NEXT.
4668 During phase 2, NEXT is the key of the next element to shift in
4669 the parse tree. All elements starting from this one have their
4670 properties relatives to buffer positions shifted by integer
4671 OFFSET and, if they belong to element PARENT, are adopted by it.
4673 PHASE specifies the phase number, as an integer.")
4675 (defvar org-element--cache-sync-timer nil
4676 "Timer used for cache synchronization.")
4678 (defvar org-element--cache-sync-keys nil
4679 "Hash table used to store keys during synchronization.
4680 See `org-element--cache-key' for more information.")
4682 (defsubst org-element--cache-key
(element)
4683 "Return a unique key for ELEMENT in cache tree.
4685 Keys are used to keep a total order among elements in the cache.
4686 Comparison is done with `org-element--cache-key-less-p'.
4688 When no synchronization is taking place, a key is simply the
4689 beginning position of the element, or that position plus one in
4690 the case of an first item (respectively row) in
4691 a list (respectively a table).
4693 During a synchronization, the key is the one the element had when
4694 the cache was synchronized for the last time. Elements added to
4695 cache during the synchronization get a new key generated with
4696 `org-element--cache-generate-key'.
4698 Such keys are stored in `org-element--cache-sync-keys'. The hash
4699 table is cleared once the synchronization is complete."
4700 (or (gethash element org-element--cache-sync-keys
)
4701 (let* ((begin (org-element-property :begin element
))
4702 ;; Increase beginning position of items (respectively
4703 ;; table rows) by one, so the first item can get
4704 ;; a different key from its parent list (respectively
4706 (key (if (memq (org-element-type element
) '(item table-row
))
4709 (if org-element--cache-sync-requests
4710 (puthash element key org-element--cache-sync-keys
)
4713 (defun org-element--cache-generate-key (lower upper
)
4714 "Generate a key between LOWER and UPPER.
4716 LOWER and UPPER are integers or lists, possibly empty.
4718 If LOWER and UPPER are equals, return LOWER. Otherwise, return
4719 a unique key, as an integer or a list of integers, according to
4720 the following rules:
4722 - LOWER and UPPER are compared level-wise until values differ.
4724 - If, at a given level, LOWER and UPPER differ from more than
4725 2, the new key shares all the levels above with LOWER and
4726 gets a new level. Its value is the mean between LOWER and
4729 \(1 2) + (1 4) --> (1 3)
4731 - If LOWER has no value to compare with, it is assumed that its
4732 value is `most-negative-fixnum'. E.g.,
4740 where m is `most-negative-fixnum'. Likewise, if UPPER is
4741 short of levels, the current value is `most-positive-fixnum'.
4743 - If they differ from only one, the new key inherits from
4744 current LOWER level and fork it at the next level. E.g.,
4752 where M is `most-positive-fixnum'.
4754 - If the key is only one level long, it is returned as an
4757 \(1 2) + (3 2) --> 2
4759 When they are not equals, the function assumes that LOWER is
4760 lesser than UPPER, per `org-element--cache-key-less-p'."
4761 (if (equal lower upper
) lower
4762 (let ((lower (if (integerp lower
) (list lower
) lower
))
4763 (upper (if (integerp upper
) (list upper
) upper
))
4767 (let ((min (or (car lower
) most-negative-fixnum
))
4768 (max (cond (skip-upper most-positive-fixnum
)
4770 (t most-positive-fixnum
))))
4771 (if (< (1+ min
) max
)
4772 (let ((mean (+ (ash min -
1) (ash max -
1) (logand min max
1))))
4773 (throw 'exit
(if key
(nreverse (cons mean key
)) mean
)))
4774 (when (and (< min max
) (not skip-upper
))
4775 ;; When at a given level, LOWER and UPPER differ from
4776 ;; 1, ignore UPPER altogether. Instead create a key
4777 ;; between LOWER and the greatest key with the same
4778 ;; prefix as LOWER so far.
4779 (setq skip-upper t
))
4781 (setq lower
(cdr lower
) upper
(cdr upper
)))))))))
4783 (defsubst org-element--cache-key-less-p
(a b
)
4784 "Non-nil if key A is less than key B.
4785 A and B are either integers or lists of integers, as returned by
4786 `org-element--cache-key'."
4787 (if (integerp a
) (if (integerp b
) (< a b
) (<= a
(car b
)))
4788 (if (integerp b
) (< (car a
) b
)
4791 (cond ((car-less-than-car a b
) (throw 'exit t
))
4792 ((car-less-than-car b a
) (throw 'exit nil
))
4793 (t (setq a
(cdr a
) b
(cdr b
)))))
4794 ;; If A is empty, either keys are equal (B is also empty) and
4795 ;; we return nil, or A is lesser than B (B is longer) and we
4796 ;; return a non-nil value.
4798 ;; If A is not empty, B is necessarily empty and A is greater
4799 ;; than B (A is longer). Therefore, return nil.
4800 (and (null a
) b
)))))
4802 (defun org-element--cache-compare (a b
)
4803 "Non-nil when element A is located before element B."
4804 (org-element--cache-key-less-p (org-element--cache-key a
)
4805 (org-element--cache-key b
)))
4807 (defsubst org-element--cache-root
()
4808 "Return root value in cache.
4809 This function assumes `org-element--cache' is a valid AVL tree."
4810 (avl-tree--node-left (avl-tree--dummyroot org-element--cache
)))
4815 (defsubst org-element--cache-active-p
()
4816 "Non-nil when cache is active in current buffer."
4817 (and org-element-use-cache
4818 (or (derived-mode-p 'org-mode
) orgstruct-mode
)))
4820 (defun org-element--cache-find (pos &optional side
)
4821 "Find element in cache starting at POS or before.
4823 POS refers to a buffer position.
4825 When optional argument SIDE is non-nil, the function checks for
4826 elements starting at or past POS instead. If SIDE is `both', the
4827 function returns a cons cell where car is the first element
4828 starting at or before POS and cdr the first element starting
4831 The function can only find elements in the synchronized part of
4833 (let ((limit (and org-element--cache-sync-requests
4834 (aref (car org-element--cache-sync-requests
) 0)))
4835 (node (org-element--cache-root))
4838 (let* ((element (avl-tree--node-data node
))
4839 (begin (org-element-property :begin element
)))
4842 (not (org-element--cache-key-less-p
4843 (org-element--cache-key element
) limit
)))
4844 (setq node
(avl-tree--node-left node
)))
4847 node
(avl-tree--node-left node
)))
4850 node
(avl-tree--node-right node
)))
4851 ;; We found an element in cache starting at POS. If `side'
4852 ;; is `both' we also want the next one in order to generate
4853 ;; a key in-between.
4855 ;; If the element is the first row or item in a table or
4856 ;; a plain list, we always return the table or the plain
4859 ;; In any other case, we return the element found.
4861 (setq lower element
)
4862 (setq node
(avl-tree--node-right node
)))
4863 ((and (memq (org-element-type element
) '(item table-row
))
4864 (let ((parent (org-element-property :parent element
)))
4865 (and (= (org-element-property :begin element
)
4866 (org-element-property :contents-begin parent
))
4875 (both (cons lower upper
))
4877 (otherwise upper
))))
4879 (defun org-element--cache-put (element &optional data
)
4880 "Store ELEMENT in current buffer's cache, if allowed.
4881 When optional argument DATA is non-nil, assume is it object data
4882 relative to ELEMENT and store it in the objects cache."
4883 (cond ((not (org-element--cache-active-p)) nil
)
4885 (when org-element--cache-sync-requests
4886 ;; During synchronization, first build an appropriate key
4887 ;; for the new element so `avl-tree-enter' can insert it at
4888 ;; the right spot in the cache.
4889 (let ((keys (org-element--cache-find
4890 (org-element-property :begin element
) 'both
)))
4892 (org-element--cache-generate-key
4893 (and (car keys
) (org-element--cache-key (car keys
)))
4894 (cond ((cdr keys
) (org-element--cache-key (cdr keys
)))
4895 (org-element--cache-sync-requests
4896 (aref (car org-element--cache-sync-requests
) 0))))
4897 org-element--cache-sync-keys
)))
4898 (avl-tree-enter org-element--cache element
))
4899 ;; Headlines are not stored in cache, so objects in titles are
4900 ;; not stored either.
4901 ((eq (org-element-type element
) 'headline
) nil
)
4902 (t (puthash element data org-element--cache-objects
))))
4904 (defsubst org-element--cache-remove
(element)
4905 "Remove ELEMENT from cache.
4906 Assume ELEMENT belongs to cache and that a cache is active."
4907 (avl-tree-delete org-element--cache element
)
4908 (remhash element org-element--cache-objects
))
4911 ;;;; Synchronization
4913 (defsubst org-element--cache-set-timer
(buffer)
4914 "Set idle timer for cache synchronization in BUFFER."
4915 (when org-element--cache-sync-timer
4916 (cancel-timer org-element--cache-sync-timer
))
4917 (setq org-element--cache-sync-timer
4918 (run-with-idle-timer
4919 (let ((idle (current-idle-time)))
4920 (if idle
(time-add idle org-element-cache-sync-break
)
4921 org-element-cache-sync-idle-time
))
4923 #'org-element--cache-sync
4926 (defsubst org-element--cache-interrupt-p
(time-limit)
4927 "Non-nil when synchronization process should be interrupted.
4928 TIME-LIMIT is a time value or nil."
4930 (or (input-pending-p)
4931 (time-less-p time-limit
(current-time)))))
4933 (defsubst org-element--cache-shift-positions
(element offset
&optional props
)
4934 "Shift ELEMENT properties relative to buffer positions by OFFSET.
4936 Properties containing buffer positions are `:begin', `:end',
4937 `:contents-begin', `:contents-end' and `:structure'. When
4938 optional argument PROPS is a list of keywords, only shift
4939 properties provided in that list.
4941 Properties are modified by side-effect."
4942 (let ((properties (nth 1 element
)))
4943 ;; Shift `:structure' property for the first plain list only: it
4944 ;; is the only one that really matters and it prevents from
4945 ;; shifting it more than once.
4946 (when (and (or (not props
) (memq :structure props
))
4947 (eq (org-element-type element
) 'plain-list
)
4948 (not (eq (org-element-type (plist-get properties
:parent
))
4950 (dolist (item (plist-get properties
:structure
))
4951 (incf (car item
) offset
)
4952 (incf (nth 6 item
) offset
)))
4953 (dolist (key '(:begin
:contents-begin
:contents-end
:end
:post-affiliated
))
4954 (let ((value (and (or (not props
) (memq key props
))
4955 (plist-get properties key
))))
4956 (and value
(plist-put properties key
(+ offset value
)))))))
4958 (defun org-element--cache-sync (buffer &optional threshold future-change
)
4959 "Synchronize cache with recent modification in BUFFER.
4961 When optional argument THRESHOLD is non-nil, do the
4962 synchronization for all elements starting before or at threshold,
4963 then exit. Otherwise, synchronize cache for as long as
4964 `org-element-cache-sync-duration' or until Emacs leaves idle
4967 FUTURE-CHANGE, when non-nil, is a buffer position where changes
4968 not registered yet in the cache are going to happen. It is used
4969 in `org-element--cache-submit-request', where cache is partially
4970 updated before current modification are actually submitted."
4971 (when (buffer-live-p buffer
)
4972 (with-current-buffer buffer
4973 (let ((inhibit-quit t
) request next
)
4974 (when org-element--cache-sync-timer
4975 (cancel-timer org-element--cache-sync-timer
))
4977 (while org-element--cache-sync-requests
4978 (setq request
(car org-element--cache-sync-requests
)
4979 next
(nth 1 org-element--cache-sync-requests
))
4980 (org-element--cache-process-request
4982 (and next
(aref next
0))
4984 (and (not threshold
)
4985 (time-add (current-time)
4986 org-element-cache-sync-duration
))
4988 ;; Request processed. Merge current and next offsets and
4989 ;; transfer ending position.
4991 (incf (aref next
3) (aref request
3))
4992 (aset next
2 (aref request
2)))
4993 (setq org-element--cache-sync-requests
4994 (cdr org-element--cache-sync-requests
))))
4995 ;; If more requests are awaiting, set idle timer accordingly.
4996 ;; Otherwise, reset keys.
4997 (if org-element--cache-sync-requests
4998 (org-element--cache-set-timer buffer
)
4999 (clrhash org-element--cache-sync-keys
))))))
5001 (defun org-element--cache-process-request
5002 (request next threshold time-limit future-change
)
5003 "Process synchronization REQUEST for all entries before NEXT.
5005 REQUEST is a vector, built by `org-element--cache-submit-request'.
5007 NEXT is a cache key, as returned by `org-element--cache-key'.
5009 When non-nil, THRESHOLD is a buffer position. Synchronization
5010 stops as soon as a shifted element begins after it.
5012 When non-nil, TIME-LIMIT is a time value. Synchronization stops
5013 after this time or when Emacs exits idle state.
5015 When non-nil, FUTURE-CHANGE is a buffer position where changes
5016 not registered yet in the cache are going to happen. See
5017 `org-element--cache-submit-request' for more information.
5019 Throw `interrupt' if the process stops before completing the
5022 (when (= (aref request
6) 0)
5025 ;; Delete all elements starting after BEG, but not after buffer
5026 ;; position END or past element with key NEXT.
5028 ;; At each iteration, we start again at tree root since
5029 ;; a deletion modifies structure of the balanced tree.
5031 (let ((beg (aref request
0))
5032 (end (aref request
2))
5033 (outreach (aref request
4)))
5035 (when (org-element--cache-interrupt-p time-limit
)
5036 (throw 'interrupt nil
))
5037 ;; Find first element in cache with key BEG or after it.
5038 (let ((node (org-element--cache-root)) data data-key
)
5040 (let* ((element (avl-tree--node-data node
))
5041 (key (org-element--cache-key element
)))
5043 ((org-element--cache-key-less-p key beg
)
5044 (setq node
(avl-tree--node-right node
)))
5045 ((org-element--cache-key-less-p beg key
)
5048 node
(avl-tree--node-left node
)))
5049 (t (setq data element
5053 (let ((pos (org-element-property :begin data
)))
5054 (if (if (or (not next
)
5055 (org-element--cache-key-less-p data-key next
))
5058 (while (and up
(not (eq up outreach
)))
5059 (setq up
(org-element-property :parent up
)))
5061 (org-element--cache-remove data
)
5062 (aset request
0 data-key
)
5063 (aset request
1 pos
)
5065 (throw 'end-phase nil
)))
5066 ;; No element starting after modifications left in
5067 ;; cache: further processing is futile.
5068 (throw 'quit t
)))))))
5069 (when (= (aref request
6) 1)
5072 ;; Phase 0 left a hole in the cache. Some elements after it
5073 ;; could have parents within. For example, in the following
5083 ;; if we remove a blank line between "item" and "Paragraph1",
5084 ;; everything down to "Paragraph2" is removed from cache. But
5085 ;; the paragraph now belongs to the list, and its `:parent'
5086 ;; property no longer is accurate.
5088 ;; Therefore we need to parse again elements in the hole, or at
5089 ;; least in its last section, so that we can re-parent
5090 ;; subsequent elements, during phase 2.
5092 ;; Note that we only need to get the parent from the first
5093 ;; element in cache after the hole.
5095 ;; When next key is lesser or equal to the current one, delegate
5096 ;; phase 1 processing to next request in order to preserve key
5097 ;; order among requests.
5098 (let ((key (aref request
0)))
5099 (when (and next
(not (org-element--cache-key-less-p key next
)))
5100 (let ((next-request (nth 1 org-element--cache-sync-requests
)))
5101 (aset next-request
0 key
)
5102 (aset next-request
1 (aref request
1))
5103 (aset next-request
6 1))
5105 ;; Next element will start at its beginning position plus
5106 ;; offset, since it hasn't been shifted yet. Therefore, LIMIT
5107 ;; contains the real beginning position of the first element to
5108 ;; shift and re-parent.
5109 (let ((limit (+ (aref request
1) (aref request
3))))
5110 (cond ((and threshold
(> limit threshold
)) (throw 'interrupt nil
))
5111 ((and future-change
(>= limit future-change
))
5112 ;; Changes are going to happen around this element and
5113 ;; they will trigger another phase 1 request. Skip the
5117 (let ((parent (org-element--parse-to limit t time-limit
)))
5118 (aset request
5 parent
)
5119 (aset request
6 2))))))
5122 ;; Shift all elements starting from key START, but before NEXT, by
5123 ;; OFFSET, and re-parent them when appropriate.
5125 ;; Elements are modified by side-effect so the tree structure
5128 ;; Once THRESHOLD, if any, is reached, or once there is an input
5129 ;; pending, exit. Before leaving, the current synchronization
5130 ;; request is updated.
5131 (let ((start (aref request
0))
5132 (offset (aref request
3))
5133 (parent (aref request
5))
5134 (node (org-element--cache-root))
5138 ;; No re-parenting nor shifting planned: request is over.
5139 (when (and (not parent
) (zerop offset
)) (throw 'quit t
))
5141 (let* ((data (avl-tree--node-data node
))
5142 (key (org-element--cache-key data
)))
5143 (if (and leftp
(avl-tree--node-left node
)
5144 (not (org-element--cache-key-less-p key start
)))
5145 (progn (push node stack
)
5146 (setq node
(avl-tree--node-left node
)))
5147 (unless (org-element--cache-key-less-p key start
)
5148 ;; We reached NEXT. Request is complete.
5149 (when (equal key next
) (throw 'quit t
))
5150 ;; Handle interruption request. Update current request.
5151 (when (or exit-flag
(org-element--cache-interrupt-p time-limit
))
5152 (aset request
0 key
)
5153 (aset request
5 parent
)
5154 (throw 'interrupt nil
))
5156 (unless (zerop offset
)
5157 (org-element--cache-shift-positions data offset
)
5158 ;; Shift associated objects data, if any.
5159 (dolist (object-data (gethash data org-element--cache-objects
))
5160 (dolist (object (cddr object-data
))
5161 (org-element--cache-shift-positions object offset
))))
5162 (let ((begin (org-element-property :begin data
)))
5163 ;; Update PARENT and re-parent DATA, only when
5164 ;; necessary. Propagate new structures for lists.
5166 (<= (org-element-property :end parent
) begin
))
5167 (setq parent
(org-element-property :parent parent
)))
5168 (cond ((and (not parent
) (zerop offset
)) (throw 'quit nil
))
5170 (let ((p (org-element-property :parent data
)))
5172 (< (org-element-property :begin p
)
5173 (org-element-property :begin parent
)))))
5174 (org-element-put-property data
:parent parent
)
5175 (let ((s (org-element-property :structure parent
)))
5176 (when (and s
(org-element-property :structure data
))
5177 (org-element-put-property data
:structure s
)))))
5178 ;; Cache is up-to-date past THRESHOLD. Request
5180 (when (and threshold
(> begin threshold
)) (setq exit-flag t
))))
5181 (setq node
(if (setq leftp
(avl-tree--node-right node
))
5182 (avl-tree--node-right node
)
5184 ;; We reached end of tree: synchronization complete.
5187 (defun org-element--parse-to (pos &optional syncp time-limit
)
5188 "Parse elements in current section, down to POS.
5190 Start parsing from the closest between the last known element in
5191 cache or headline above. Return the smallest element containing
5194 When optional argument SYNCP is non-nil, return the parent of the
5195 element containing POS instead. In that case, it is also
5196 possible to provide TIME-LIMIT, which is a time value specifying
5197 when the parsing should stop. The function throws `interrupt' if
5198 the process stopped before finding the expected result."
5200 (org-with-wide-buffer
5202 (let* ((cached (and (org-element--cache-active-p)
5203 (org-element--cache-find pos nil
)))
5204 (begin (org-element-property :begin cached
))
5207 ;; Nothing in cache before point: start parsing from first
5208 ;; element following headline above, or first element in
5211 (when (org-with-limited-levels (outline-previous-heading))
5212 (setq mode
'planning
)
5214 (skip-chars-forward " \r\t\n")
5215 (beginning-of-line))
5216 ;; Cache returned exact match: return it.
5218 (throw 'exit
(if syncp
(org-element-property :parent cached
) cached
)))
5219 ;; There's a headline between cached value and POS: cached
5220 ;; value is invalid. Start parsing from first element
5221 ;; following the headline.
5222 ((re-search-backward
5223 (org-with-limited-levels org-outline-regexp-bol
) begin t
)
5225 (skip-chars-forward " \r\t\n")
5227 (setq mode
'planning
))
5228 ;; Check if CACHED or any of its ancestors contain point.
5230 ;; If there is such an element, we inspect it in order to know
5231 ;; if we return it or if we need to parse its contents.
5232 ;; Otherwise, we just start parsing from current location,
5233 ;; which is right after the top-most element containing
5236 ;; As a special case, if POS is at the end of the buffer, we
5237 ;; want to return the innermost element ending there.
5239 ;; Also, if we find an ancestor and discover that we need to
5240 ;; parse its contents, make sure we don't start from
5241 ;; `:contents-begin', as we would otherwise go past CACHED
5242 ;; again. Instead, in that situation, we will resume parsing
5243 ;; from NEXT, which is located after CACHED or its higher
5244 ;; ancestor not containing point.
5247 (pos (if (= (point-max) pos
) (1- pos
) pos
)))
5248 (goto-char (or (org-element-property :contents-begin cached
) begin
))
5249 (while (let ((end (org-element-property :end up
)))
5252 (setq up
(org-element-property :parent up
)))))
5254 ((eobp) (setq element up
))
5255 (t (setq element up next
(point)))))))
5256 ;; Parse successively each element until we reach POS.
5257 (let ((end (or (org-element-property :end element
)
5259 (org-with-limited-levels (outline-next-heading))
5264 (cond ((= (point) pos
) (throw 'exit parent
))
5265 ((org-element--cache-interrupt-p time-limit
)
5266 (throw 'interrupt nil
))))
5268 (setq element
(org-element--current-element
5270 (org-element-property :structure parent
)))
5271 (org-element-put-property element
:parent parent
)
5272 (org-element--cache-put element
))
5273 (let ((elem-end (org-element-property :end element
))
5274 (type (org-element-type element
)))
5276 ;; Skip any element ending before point. Also skip
5277 ;; element ending at point (unless it is also the end of
5278 ;; buffer) since we're sure that another element begins
5280 ((and (<= elem-end pos
) (/= (point-max) elem-end
))
5281 (goto-char elem-end
)
5282 (setq mode
(org-element--next-mode type nil
)))
5283 ;; A non-greater element contains point: return it.
5284 ((not (memq type org-element-greater-elements
))
5285 (throw 'exit element
))
5286 ;; Otherwise, we have to decide if ELEMENT really
5287 ;; contains POS. In that case we start parsing from
5288 ;; contents' beginning.
5290 ;; If POS is at contents' beginning but it is also at
5291 ;; the beginning of the first item in a list or a table.
5292 ;; In that case, we need to create an anchor for that
5293 ;; list or table, so return it.
5295 ;; Also, if POS is at the end of the buffer, no element
5296 ;; can start after it, but more than one may end there.
5297 ;; Arbitrarily, we choose to return the innermost of
5299 ((let ((cbeg (org-element-property :contents-begin element
))
5300 (cend (org-element-property :contents-end element
)))
5305 (not (memq type
'(plain-list table
)))))
5307 (and (= cend pos
) (= (point-max) pos
)))))
5308 (goto-char (or next cbeg
))
5310 mode
(org-element--next-mode type t
)
5313 ;; Otherwise, return ELEMENT as it is the smallest
5314 ;; element containing POS.
5315 (t (throw 'exit element
))))
5316 (setq element nil
)))))))
5319 ;;;; Staging Buffer Changes
5321 (defconst org-element--cache-sensitive-re
5323 org-outline-regexp-bol
"\\|"
5326 "#\\+\\(?:BEGIN[:_]\\|END\\(?:_\\|:?[ \t]*$\\)\\)" "\\|"
5327 ;; LaTeX environments.
5328 "\\\\\\(?:begin{[A-Za-z0-9*]+}\\|end{[A-Za-z0-9*]+}[ \t]*$\\)" "\\|"
5330 ":\\(?:\\w\\|[-_]\\)+:[ \t]*$"
5332 "Regexp matching a sensitive line, structure wise.
5333 A sensitive line is a headline, inlinetask, block, drawer, or
5334 latex-environment boundary. When such a line is modified,
5335 structure changes in the document may propagate in the whole
5336 section, possibly making cache invalid.")
5338 (defvar org-element--cache-change-warning nil
5339 "Non-nil when a sensitive line is about to be changed.
5340 It is a symbol among nil, t and `headline'.")
5342 (defun org-element--cache-before-change (beg end
)
5343 "Request extension of area going to be modified if needed.
5344 BEG and END are the beginning and end of the range of changed
5345 text. See `before-change-functions' for more information."
5346 (when (org-element--cache-active-p)
5347 (org-with-wide-buffer
5350 (let ((bottom (save-excursion (goto-char end
) (line-end-position))))
5351 (setq org-element--cache-change-warning
5353 (if (and (org-with-limited-levels (org-at-heading-p))
5354 (= (line-end-position) bottom
))
5356 (let ((case-fold-search t
))
5358 org-element--cache-sensitive-re bottom t
)))))))))
5360 (defun org-element--cache-after-change (beg end pre
)
5361 "Update buffer modifications for current buffer.
5362 BEG and END are the beginning and end of the range of changed
5363 text, and the length in bytes of the pre-change text replaced by
5364 that range. See `after-change-functions' for more information."
5365 (when (org-element--cache-active-p)
5366 (org-with-wide-buffer
5371 (bottom (save-excursion (goto-char end
) (line-end-position))))
5372 ;; Determine if modified area needs to be extended, according
5373 ;; to both previous and current state. We make a special
5374 ;; case for headline editing: if a headline is modified but
5375 ;; not removed, do not extend.
5376 (when (case org-element--cache-change-warning
5379 (not (and (org-with-limited-levels (org-at-heading-p))
5380 (= (line-end-position) bottom
))))
5382 (let ((case-fold-search t
))
5384 org-element--cache-sensitive-re bottom t
))))
5385 ;; Effectively extend modified area.
5386 (org-with-limited-levels
5387 (setq top
(progn (goto-char top
)
5388 (when (outline-previous-heading) (forward-line))
5390 (setq bottom
(progn (goto-char bottom
)
5391 (if (outline-next-heading) (1- (point))
5393 ;; Store synchronization request.
5394 (let ((offset (- end beg pre
)))
5395 (org-element--cache-submit-request top
(- bottom offset
) offset
)))))
5396 ;; Activate a timer to process the request during idle time.
5397 (org-element--cache-set-timer (current-buffer))))
5399 (defun org-element--cache-for-removal (beg end offset
)
5400 "Return first element to remove from cache.
5402 BEG and END are buffer positions delimiting buffer modifications.
5403 OFFSET is the size of the changes.
5405 Returned element is usually the first element in cache containing
5406 any position between BEG and END. As an exception, greater
5407 elements around the changes that are robust to contents
5408 modifications are preserved and updated according to the
5410 (let* ((elements (org-element--cache-find (1- beg
) 'both
))
5411 (before (car elements
))
5412 (after (cdr elements
)))
5413 (if (not before
) after
5417 (if (and (memq (org-element-type up
)
5418 '(center-block drawer dynamic-block
5419 quote-block special-block
))
5420 (let ((cbeg (org-element-property :contents-begin up
)))
5423 (> (org-element-property :contents-end up
) end
))))
5424 ;; UP is a robust greater element containing changes.
5425 ;; We only need to extend its ending boundaries.
5426 (org-element--cache-shift-positions
5427 up offset
'(:contents-end
:end
))
5429 (when robust-flag
(setq robust-flag nil
)))
5430 (setq up
(org-element-property :parent up
)))
5431 ;; We're at top level element containing ELEMENT: if it's
5432 ;; altered by buffer modifications, it is first element in
5433 ;; cache to be removed. Otherwise, that first element is the
5436 ;; As a special case, do not remove BEFORE if it is a robust
5437 ;; container for current changes.
5438 (if (or (< (org-element-property :end before
) beg
) robust-flag
) after
5441 (defun org-element--cache-submit-request (beg end offset
)
5442 "Submit a new cache synchronization request for current buffer.
5443 BEG and END are buffer positions delimiting the minimal area
5444 where cache data should be removed. OFFSET is the size of the
5445 change, as an integer."
5446 (let ((next (car org-element--cache-sync-requests
))
5447 delete-to delete-from
)
5449 (zerop (aref next
6))
5450 (> (setq delete-to
(+ (aref next
2) (aref next
3))) end
)
5451 (<= (setq delete-from
(aref next
1)) end
))
5452 ;; Current changes can be merged with first sync request: we
5453 ;; can save a partial cache synchronization.
5455 (incf (aref next
3) offset
)
5456 ;; If last change happened within area to be removed, extend
5457 ;; boundaries of robust parents, if any. Otherwise, find
5458 ;; first element to remove and update request accordingly.
5459 (if (> beg delete-from
)
5460 (let ((up (aref next
5)))
5462 (org-element--cache-shift-positions
5463 up offset
'(:contents-end
:end
))
5464 (setq up
(org-element-property :parent up
))))
5465 (let ((first (org-element--cache-for-removal beg delete-to offset
)))
5467 (aset next
0 (org-element--cache-key first
))
5468 (aset next
1 (org-element-property :begin first
))
5469 (aset next
5 (org-element-property :parent first
))))))
5470 ;; Ensure cache is correct up to END. Also make sure that NEXT,
5471 ;; if any, is no longer a 0-phase request, thus ensuring that
5472 ;; phases are properly ordered. We need to provide OFFSET as
5473 ;; optional parameter since current modifications are not known
5474 ;; yet to the otherwise correct part of the cache (i.e, before
5475 ;; the first request).
5476 (when next
(org-element--cache-sync (current-buffer) end beg
))
5477 (let ((first (org-element--cache-for-removal beg end offset
)))
5479 (push (let ((beg (org-element-property :begin first
))
5480 (key (org-element--cache-key first
)))
5482 ;; When changes happen before the first known
5483 ;; element, re-parent and shift the rest of the
5485 ((> beg end
) (vector key beg nil offset nil nil
1))
5486 ;; Otherwise, we find the first non robust
5487 ;; element containing END. All elements between
5488 ;; FIRST and this one are to be removed.
5490 ;; Among them, some could be located outside the
5491 ;; synchronized part of the cache, in which case
5492 ;; comparing buffer positions to find them is
5493 ;; useless. Instead, we store the element
5494 ;; containing them in the request itself. All
5495 ;; its children will be removed.
5496 ((let ((first-end (org-element-property :end first
)))
5497 (and (> first-end end
)
5498 (vector key beg first-end offset first
5499 (org-element-property :parent first
) 0))))
5501 (let* ((element (org-element--cache-find end
))
5502 (end (org-element-property :end element
))
5504 (while (and (setq up
(org-element-property :parent up
))
5505 (>= (org-element-property :begin up
) beg
))
5506 (setq end
(org-element-property :end up
)
5508 (vector key beg end offset element
5509 (org-element-property :parent first
) 0)))))
5510 org-element--cache-sync-requests
)
5511 ;; No element to remove. No need to re-parent either.
5512 ;; Simply shift additional elements, if any, by OFFSET.
5513 (when org-element--cache-sync-requests
5514 (incf (aref (car org-element--cache-sync-requests
) 3) offset
)))))))
5517 ;;;; Public Functions
5520 (defun org-element-cache-reset (&optional all
)
5521 "Reset cache in current buffer.
5522 When optional argument ALL is non-nil, reset cache in all Org
5525 (dolist (buffer (if all
(buffer-list) (list (current-buffer))))
5526 (with-current-buffer buffer
5527 (when (org-element--cache-active-p)
5528 (org-set-local 'org-element--cache
5529 (avl-tree-create #'org-element--cache-compare
))
5530 (org-set-local 'org-element--cache-objects
(make-hash-table :test
#'eq
))
5531 (org-set-local 'org-element--cache-sync-keys
5532 (make-hash-table :weakness
'key
:test
#'eq
))
5533 (org-set-local 'org-element--cache-change-warning nil
)
5534 (org-set-local 'org-element--cache-sync-requests nil
)
5535 (org-set-local 'org-element--cache-sync-timer nil
)
5536 (add-hook 'before-change-functions
5537 #'org-element--cache-before-change nil t
)
5538 (add-hook 'after-change-functions
5539 #'org-element--cache-after-change nil t
)))))
5542 (defun org-element-cache-refresh (pos)
5543 "Refresh cache at position POS."
5544 (when (org-element--cache-active-p)
5545 (org-element--cache-sync (current-buffer) pos
)
5546 (org-element--cache-submit-request pos pos
0)
5547 (org-element--cache-set-timer (current-buffer))))
5553 ;; The first move is to implement a way to obtain the smallest element
5554 ;; containing point. This is the job of `org-element-at-point'. It
5555 ;; basically jumps back to the beginning of section containing point
5556 ;; and proceed, one element after the other, with
5557 ;; `org-element--current-element' until the container is found. Note:
5558 ;; When using `org-element-at-point', secondary values are never
5559 ;; parsed since the function focuses on elements, not on objects.
5561 ;; At a deeper level, `org-element-context' lists all elements and
5562 ;; objects containing point.
5564 ;; `org-element-nested-p' and `org-element-swap-A-B' may be used
5565 ;; internally by navigation and manipulation tools.
5569 (defun org-element-at-point ()
5570 "Determine closest element around point.
5572 Return value is a list like (TYPE PROPS) where TYPE is the type
5573 of the element and PROPS a plist of properties associated to the
5576 Possible types are defined in `org-element-all-elements'.
5577 Properties depend on element or object type, but always include
5578 `:begin', `:end', `:parent' and `:post-blank' properties.
5580 As a special case, if point is at the very beginning of the first
5581 item in a list or sub-list, returned element will be that list
5582 instead of the item. Likewise, if point is at the beginning of
5583 the first row of a table, returned element will be the table
5584 instead of the first row.
5586 When point is at the end of the buffer, return the innermost
5587 element ending there."
5588 (org-with-wide-buffer
5589 (let ((origin (point)))
5591 (skip-chars-backward " \r\t\n")
5593 ;; Within blank lines at the beginning of buffer, return nil.
5595 ;; Within blank lines right after a headline, return that
5597 ((org-with-limited-levels (org-at-heading-p))
5599 (org-element-headline-parser (point-max) t
))
5600 ;; Otherwise parse until we find element containing ORIGIN.
5602 (when (org-element--cache-active-p)
5603 (if (not org-element--cache
) (org-element-cache-reset)
5604 (org-element--cache-sync (current-buffer) origin
)))
5605 (org-element--parse-to origin
))))))
5608 (defun org-element-context (&optional element
)
5609 "Return smallest element or object around point.
5611 Return value is a list like (TYPE PROPS) where TYPE is the type
5612 of the element or object and PROPS a plist of properties
5615 Possible types are defined in `org-element-all-elements' and
5616 `org-element-all-objects'. Properties depend on element or
5617 object type, but always include `:begin', `:end', `:parent' and
5620 As a special case, if point is right after an object and not at
5621 the beginning of any other object, return that object.
5623 Optional argument ELEMENT, when non-nil, is the closest element
5624 containing point, as returned by `org-element-at-point'.
5625 Providing it allows for quicker computation."
5626 (catch 'objects-forbidden
5627 (org-with-wide-buffer
5628 (let* ((pos (point))
5629 (element (or element
(org-element-at-point)))
5630 (type (org-element-type element
)))
5631 ;; If point is inside an element containing objects or
5632 ;; a secondary string, narrow buffer to the container and
5633 ;; proceed with parsing. Otherwise, return ELEMENT.
5635 ;; At a parsed affiliated keyword, check if we're inside main
5637 ((let ((post (org-element-property :post-affiliated element
)))
5638 (and post
(< pos post
)))
5640 (let ((case-fold-search t
)) (looking-at org-element--affiliated-re
))
5642 ((not (member-ignore-case (match-string 1)
5643 org-element-parsed-keywords
))
5644 (throw 'objects-forbidden element
))
5645 ((< (match-end 0) pos
)
5646 (narrow-to-region (match-end 0) (line-end-position)))
5647 ((and (match-beginning 2)
5648 (>= pos
(match-beginning 2))
5649 (< pos
(match-end 2)))
5650 (narrow-to-region (match-beginning 2) (match-end 2)))
5651 (t (throw 'objects-forbidden element
)))
5652 ;; Also change type to retrieve correct restrictions.
5653 (setq type
'keyword
))
5654 ;; At an item, objects can only be located within tag, if any.
5656 (let ((tag (org-element-property :tag element
)))
5657 (if (not tag
) (throw 'objects-forbidden element
)
5659 (search-forward tag
(line-end-position))
5660 (goto-char (match-beginning 0))
5661 (if (and (>= pos
(point)) (< pos
(match-end 0)))
5662 (narrow-to-region (point) (match-end 0))
5663 (throw 'objects-forbidden element
)))))
5664 ;; At an headline or inlinetask, objects are in title.
5665 ((memq type
'(headline inlinetask
))
5666 (goto-char (org-element-property :begin element
))
5667 (skip-chars-forward "*")
5668 (if (and (> pos
(point)) (< pos
(line-end-position)))
5669 (narrow-to-region (point) (line-end-position))
5670 (throw 'objects-forbidden element
)))
5671 ;; At a paragraph, a table-row or a verse block, objects are
5672 ;; located within their contents.
5673 ((memq type
'(paragraph table-row verse-block
))
5674 (let ((cbeg (org-element-property :contents-begin element
))
5675 (cend (org-element-property :contents-end element
)))
5676 ;; CBEG is nil for table rules.
5677 (if (and cbeg cend
(>= pos cbeg
)
5678 (or (< pos cend
) (and (= pos cend
) (eobp))))
5679 (narrow-to-region cbeg cend
)
5680 (throw 'objects-forbidden element
))))
5681 ;; At a parsed keyword, objects are located within value.
5683 (if (not (member (org-element-property :key element
)
5684 org-element-document-properties
))
5685 (throw 'objects-forbidden element
)
5686 (goto-char (org-element-property :begin element
))
5687 (search-forward ":")
5688 (if (and (>= pos
(point)) (< pos
(line-end-position)))
5689 (narrow-to-region (point) (line-end-position))
5690 (throw 'objects-forbidden element
))))
5691 ;; At a planning line, if point is at a timestamp, return it,
5692 ;; otherwise, return element.
5693 ((eq type
'planning
)
5694 (dolist (p '(:closed
:deadline
:scheduled
))
5695 (let ((timestamp (org-element-property p element
)))
5696 (when (and timestamp
5697 (<= (org-element-property :begin timestamp
) pos
)
5698 (> (org-element-property :end timestamp
) pos
))
5699 (throw 'objects-forbidden timestamp
))))
5700 ;; All other locations cannot contain objects: bail out.
5701 (throw 'objects-forbidden element
))
5702 (t (throw 'objects-forbidden element
)))
5703 (goto-char (point-min))
5704 (let ((restriction (org-element-restriction type
))
5706 (cache (cond ((not (org-element--cache-active-p)) nil
)
5707 (org-element--cache-objects
5708 (gethash element org-element--cache-objects
))
5709 (t (org-element-cache-reset) nil
)))
5710 next object-data last
)
5714 ;; When entering PARENT for the first time, get list
5715 ;; of objects within known so far. Store it in
5718 (let ((data (assq parent cache
)))
5719 (if data
(setq object-data data
)
5720 (push (setq object-data
(list parent nil
)) cache
))))
5721 ;; Find NEXT object for analysis.
5723 ;; If NEXT is non-nil, we already exhausted the
5724 ;; cache so we can parse buffer to find the object
5726 (if next
(setq next
(org-element--object-lex restriction
))
5727 ;; Otherwise, check if cache can help us.
5728 (let ((objects (cddr object-data
))
5729 (completep (nth 1 object-data
)))
5731 ((and (not objects
) completep
) (throw 'exit parent
))
5733 (setq next
(org-element--object-lex restriction
)))
5736 (org-element-property :end
(car objects
))))
5737 (if (>= cache-limit pos
)
5738 ;; Cache contains the information needed.
5739 (dolist (object objects
(throw 'exit parent
))
5740 (when (<= (org-element-property :begin object
)
5742 (if (>= (org-element-property :end object
)
5744 (throw 'found
(setq next object
))
5745 (throw 'exit parent
))))
5746 (goto-char cache-limit
)
5748 (org-element--object-lex restriction
))))))))
5749 ;; If we have a new object to analyze, store it in
5750 ;; cache. Otherwise record that there is nothing
5751 ;; more to parse in this element at this depth.
5753 (progn (org-element-put-property next
:parent parent
)
5754 (push next
(cddr object-data
)))
5755 (setcar (cdr object-data
) t
)))
5756 ;; Process NEXT, if any, in order to know if we need
5757 ;; to skip it, return it or move into it.
5758 (if (or (not next
) (> (org-element-property :begin next
) pos
))
5759 (throw 'exit
(or last parent
))
5760 (let ((end (org-element-property :end next
))
5761 (cbeg (org-element-property :contents-begin next
))
5762 (cend (org-element-property :contents-end next
)))
5764 ;; Skip objects ending before point. Also skip
5765 ;; objects ending at point unless it is also the
5766 ;; end of buffer, since we want to return the
5767 ;; innermost object.
5768 ((and (<= end pos
) (/= (point-max) end
))
5770 ;; For convenience, when object ends at POS,
5771 ;; without any space, store it in LAST, as we
5772 ;; will return it if no object starts here.
5773 (when (and (= end pos
)
5774 (not (memq (char-before) '(?\s ?
\t))))
5776 ;; If POS is within a container object, move
5777 ;; into that object.
5781 ;; At contents' end, if there is no
5782 ;; space before point, also move into
5783 ;; object, for consistency with
5784 ;; convenience feature above.
5786 (or (= (point-max) pos
)
5787 (not (memq (char-before pos
)
5790 (narrow-to-region (point) cend
)
5792 restriction
(org-element-restriction next
)
5795 ;; Otherwise, return NEXT.
5796 (t (throw 'exit next
)))))))
5797 ;; Store results in cache, if applicable.
5798 (org-element--cache-put element cache
)))))))
5800 (defun org-element-lineage (blob &optional types with-self
)
5801 "List all ancestors of a given element or object.
5803 BLOB is an object or element.
5805 When optional argument TYPES is a list of symbols, return the
5806 first element or object in the lineage whose type belongs to that
5809 When optional argument WITH-SELF is non-nil, lineage includes
5810 BLOB itself as the first element, and TYPES, if provided, also
5813 When BLOB is obtained through `org-element-context' or
5814 `org-element-at-point', only ancestors from its section can be
5815 found. There is no such limitation when BLOB belongs to a full
5817 (let ((up (if with-self blob
(org-element-property :parent blob
)))
5819 (while (and up
(not (memq (org-element-type up
) types
)))
5820 (unless types
(push up ancestors
))
5821 (setq up
(org-element-property :parent up
)))
5822 (if types up
(nreverse ancestors
))))
5824 (defun org-element-nested-p (elem-A elem-B
)
5825 "Non-nil when elements ELEM-A and ELEM-B are nested."
5826 (let ((beg-A (org-element-property :begin elem-A
))
5827 (beg-B (org-element-property :begin elem-B
))
5828 (end-A (org-element-property :end elem-A
))
5829 (end-B (org-element-property :end elem-B
)))
5830 (or (and (>= beg-A beg-B
) (<= end-A end-B
))
5831 (and (>= beg-B beg-A
) (<= end-B end-A
)))))
5833 (defun org-element-swap-A-B (elem-A elem-B
)
5834 "Swap elements ELEM-A and ELEM-B.
5835 Assume ELEM-B is after ELEM-A in the buffer. Leave point at the
5837 (goto-char (org-element-property :begin elem-A
))
5838 ;; There are two special cases when an element doesn't start at bol:
5839 ;; the first paragraph in an item or in a footnote definition.
5840 (let ((specialp (not (bolp))))
5841 ;; Only a paragraph without any affiliated keyword can be moved at
5842 ;; ELEM-A position in such a situation. Note that the case of
5843 ;; a footnote definition is impossible: it cannot contain two
5844 ;; paragraphs in a row because it cannot contain a blank line.
5846 (or (not (eq (org-element-type elem-B
) 'paragraph
))
5847 (/= (org-element-property :begin elem-B
)
5848 (org-element-property :contents-begin elem-B
))))
5849 (error "Cannot swap elements"))
5850 ;; In a special situation, ELEM-A will have no indentation. We'll
5851 ;; give it ELEM-B's (which will in, in turn, have no indentation).
5852 (let* ((ind-B (when specialp
5853 (goto-char (org-element-property :begin elem-B
))
5854 (org-get-indentation)))
5855 (beg-A (org-element-property :begin elem-A
))
5856 (end-A (save-excursion
5857 (goto-char (org-element-property :end elem-A
))
5858 (skip-chars-backward " \r\t\n")
5860 (beg-B (org-element-property :begin elem-B
))
5861 (end-B (save-excursion
5862 (goto-char (org-element-property :end elem-B
))
5863 (skip-chars-backward " \r\t\n")
5865 ;; Store overlays responsible for visibility status. We
5866 ;; also need to store their boundaries as they will be
5867 ;; removed from buffer.
5870 (mapcar (lambda (ov) (list ov
(overlay-start ov
) (overlay-end ov
)))
5871 (overlays-in beg-A end-A
))
5872 (mapcar (lambda (ov) (list ov
(overlay-start ov
) (overlay-end ov
)))
5873 (overlays-in beg-B end-B
))))
5875 (body-A (buffer-substring beg-A end-A
))
5876 (body-B (delete-and-extract-region beg-B end-B
)))
5879 (setq body-B
(replace-regexp-in-string "\\`[ \t]*" "" body-B
))
5880 (org-indent-to-column ind-B
))
5882 ;; Restore ex ELEM-A overlays.
5883 (let ((offset (- beg-B beg-A
)))
5886 (car ov
) (+ (nth 1 ov
) offset
) (+ (nth 2 ov
) offset
)))
5889 (delete-region beg-A end-A
)
5891 ;; Restore ex ELEM-B overlays.
5894 (car ov
) (- (nth 1 ov
) offset
) (- (nth 2 ov
) offset
)))
5896 (goto-char (org-element-property :end elem-B
)))))
5898 (defun org-element-remove-indentation (s &optional n
)
5899 "Remove maximum common indentation in string S and return it.
5900 When optional argument N is a positive integer, remove exactly
5901 that much characters from indentation, if possible, or return
5902 S as-is otherwise. Unlike to `org-remove-indentation', this
5903 function doesn't call `untabify' on S."
5907 (goto-char (point-min))
5908 ;; Find maximum common indentation, if not specified.
5910 (let ((min-ind (point-max)))
5912 (while (re-search-forward "^[ \t]*\\S-" nil t
)
5913 (let ((ind (1- (current-column))))
5914 (if (zerop ind
) (throw 'exit s
)
5915 (setq min-ind
(min min-ind ind
))))))
5918 ;; Remove exactly N indentation, but give up if not possible.
5920 (let ((ind (progn (skip-chars-forward " \t") (current-column))))
5921 (cond ((eolp) (delete-region (line-beginning-position) (point)))
5922 ((< ind n
) (throw 'exit s
))
5923 (t (org-indent-line-to (- ind n
))))
5929 (provide 'org-element
)
5932 ;; generated-autoload-file: "org-loaddefs.el"
5935 ;;; org-element.el ends here