1 ;;; org-element.el --- Parser And Applications for Org syntax
3 ;; Copyright (C) 2012-2013 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; Org syntax can be divided into three categories: "Greater
26 ;; elements", "Elements" and "Objects".
28 ;; Elements are related to the structure of the document. Indeed, all
29 ;; elements are a cover for the document: each position within belongs
30 ;; to at least one element.
32 ;; An element always starts and ends at the beginning of a line. With
33 ;; a few exceptions (`clock', `headline', `inlinetask', `item',
34 ;; `planning', `node-property', `quote-section' `section' and
35 ;; `table-row' types), it can also accept a fixed set of keywords as
36 ;; attributes. Those are called "affiliated keywords" to distinguish
37 ;; them from other keywords, which are full-fledged elements. Almost
38 ;; all affiliated keywords are referenced in
39 ;; `org-element-affiliated-keywords'; the others are export attributes
40 ;; and start with "ATTR_" prefix.
42 ;; Element containing other elements (and only elements) are called
43 ;; greater elements. Concerned types are: `center-block', `drawer',
44 ;; `dynamic-block', `footnote-definition', `headline', `inlinetask',
45 ;; `item', `plain-list', `property-drawer', `quote-block', `section'
46 ;; and `special-block'.
48 ;; Other element types are: `babel-call', `clock', `comment',
49 ;; `comment-block', `diary-sexp', `example-block', `export-block',
50 ;; `fixed-width', `horizontal-rule', `keyword', `latex-environment',
51 ;; `node-property', `paragraph', `planning', `quote-section',
52 ;; `src-block', `table', `table-row' and `verse-block'. Among them,
53 ;; `paragraph' and `verse-block' types can contain Org objects and
56 ;; Objects are related to document's contents. Some of them are
57 ;; recursive. Associated types are of the following: `bold', `code',
58 ;; `entity', `export-snippet', `footnote-reference',
59 ;; `inline-babel-call', `inline-src-block', `italic',
60 ;; `latex-fragment', `line-break', `link', `macro', `radio-target',
61 ;; `statistics-cookie', `strike-through', `subscript', `superscript',
62 ;; `table-cell', `target', `timestamp', `underline' and `verbatim'.
64 ;; Some elements also have special properties whose value can hold
65 ;; objects themselves (i.e. an item tag or a headline name). Such
66 ;; values are called "secondary strings". Any object belongs to
67 ;; either an element or a secondary string.
69 ;; Notwithstanding affiliated keywords, each greater element, element
70 ;; and object has a fixed set of properties attached to it. Among
71 ;; them, four are shared by all types: `:begin' and `:end', which
72 ;; refer to the beginning and ending buffer positions of the
73 ;; considered element or object, `:post-blank', which holds the number
74 ;; of blank lines, or white spaces, at its end and `:parent' which
75 ;; refers to the element or object containing it. Greater elements,
76 ;; elements and objects containing objects will also have
77 ;; `:contents-begin' and `:contents-end' properties to delimit
78 ;; contents. Eventually, greater elements and elements accepting
79 ;; affiliated keywords will have a `:post-affiliated' property,
80 ;; referring to the buffer position after all such keywords.
82 ;; At the lowest level, a `:parent' property is also attached to any
83 ;; string, as a text property.
85 ;; Lisp-wise, an element or an object can be represented as a list.
86 ;; It follows the pattern (TYPE PROPERTIES CONTENTS), where:
87 ;; TYPE is a symbol describing the Org element or object.
88 ;; PROPERTIES is the property list attached to it. See docstring of
89 ;; appropriate parsing function to get an exhaustive
91 ;; CONTENTS is a list of elements, objects or raw strings contained
92 ;; in the current element or object, when applicable.
94 ;; An Org buffer is a nested list of such elements and objects, whose
95 ;; type is `org-data' and properties is nil.
97 ;; The first part of this file defines Org syntax, while the second
98 ;; one provide accessors and setters functions.
100 ;; The next part implements a parser and an interpreter for each
101 ;; element and object type in Org syntax.
103 ;; The following part creates a fully recursive buffer parser. It
104 ;; also provides a tool to map a function to elements or objects
105 ;; matching some criteria in the parse tree. Functions of interest
106 ;; are `org-element-parse-buffer', `org-element-map' and, to a lesser
107 ;; extent, `org-element-parse-secondary-string'.
109 ;; The penultimate part is the cradle of an interpreter for the
110 ;; obtained parse tree: `org-element-interpret-data'.
112 ;; The library ends by furnishing `org-element-at-point' function, and
113 ;; a way to give information about document structure around point
114 ;; with `org-element-context'. A simple cache mechanism is also
115 ;; provided for these functions.
120 (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 ;; These variables really act as a control center for the parsing
134 (defconst org-element-paragraph-separate
136 ;; Headlines, inlinetasks.
137 org-outline-regexp
"\\|"
138 ;; Footnote definitions.
139 "\\[\\(?:[0-9]+\\|fn:[-_[:word:]]+\\)\\]" "\\|"
145 ;; Tables (any type).
146 "\\(?:|\\|\\+-[-+]\\)" "\\|"
147 ;; Blocks (any type), Babel calls and keywords. Note: this
148 ;; is only an indication and need some thorough check.
149 "#\\(?:[+ ]\\|$\\)" "\\|"
150 ;; Drawers (any type) and fixed-width areas. This is also
151 ;; only an indication.
154 "-\\{5,\\}[ \t]*$" "\\|"
155 ;; LaTeX environments.
156 "\\\\begin{\\([A-Za-z0-9]+\\*?\\)}" "\\|"
157 ;; Planning and Clock lines.
158 (regexp-opt (list org-scheduled-string
164 (let ((term (case org-plain-list-ordered-item-terminator
165 (?\
) ")") (?.
"\\.") (otherwise "[.)]")))
166 (alpha (and org-list-allow-alphabetical
"\\|[A-Za-z]")))
167 (concat "\\(?:[-+*]\\|\\(?:[0-9]+" alpha
"\\)" term
"\\)"
168 "\\(?:[ \t]\\|$\\)"))
170 "Regexp to separate paragraphs in an Org buffer.
171 In the case of lines starting with \"#\" and \":\", this regexp
172 is not sufficient to know if point is at a paragraph ending. See
173 `org-element-paragraph-parser' for more information.")
175 (defconst org-element-all-elements
176 '(babel-call center-block clock comment comment-block diary-sexp drawer
177 dynamic-block example-block export-block fixed-width
178 footnote-definition headline horizontal-rule inlinetask item
179 keyword latex-environment node-property paragraph plain-list
180 planning property-drawer quote-block quote-section section
181 special-block src-block table table-row verse-block
)
182 "Complete list of element types.")
184 (defconst org-element-greater-elements
185 '(center-block drawer dynamic-block footnote-definition headline inlinetask
186 item plain-list property-drawer quote-block section
188 "List of recursive element types aka Greater Elements.")
190 (defconst org-element-all-successors
191 '(export-snippet footnote-reference inline-babel-call inline-src-block
192 latex-or-entity line-break link macro plain-link radio-target
193 statistics-cookie sub
/superscript table-cell target
194 text-markup timestamp
)
195 "Complete list of successors.")
197 (defconst org-element-object-successor-alist
198 '((subscript . sub
/superscript
) (superscript . sub
/superscript
)
199 (bold . text-markup
) (code . text-markup
) (italic . text-markup
)
200 (strike-through . text-markup
) (underline . text-markup
)
201 (verbatim . text-markup
) (entity . latex-or-entity
)
202 (latex-fragment . latex-or-entity
))
203 "Alist of translations between object type and successor name.
204 Sharing the same successor comes handy when, for example, the
205 regexp matching one object can also match the other object.")
207 (defconst org-element-all-objects
208 '(bold code entity export-snippet footnote-reference inline-babel-call
209 inline-src-block italic line-break latex-fragment link macro
210 radio-target statistics-cookie strike-through subscript superscript
211 table-cell target timestamp underline verbatim
)
212 "Complete list of object types.")
214 (defconst org-element-recursive-objects
215 '(bold italic link subscript radio-target strike-through superscript
216 table-cell underline
)
217 "List of recursive object types.")
219 (defvar org-element-block-name-alist
220 '(("CENTER" . org-element-center-block-parser
)
221 ("COMMENT" . org-element-comment-block-parser
)
222 ("EXAMPLE" . org-element-example-block-parser
)
223 ("QUOTE" . org-element-quote-block-parser
)
224 ("SRC" . org-element-src-block-parser
)
225 ("VERSE" . org-element-verse-block-parser
))
226 "Alist between block names and the associated parsing function.
227 Names must be uppercase. Any block whose name has no association
228 is parsed with `org-element-special-block-parser'.")
230 (defconst org-element-link-type-is-file
231 '("file" "file+emacs" "file+sys" "docview")
232 "List of link types equivalent to \"file\".
233 Only these types can accept search options and an explicit
234 application to open them.")
236 (defconst org-element-affiliated-keywords
237 '("CAPTION" "DATA" "HEADER" "HEADERS" "LABEL" "NAME" "PLOT" "RESNAME" "RESULT"
238 "RESULTS" "SOURCE" "SRCNAME" "TBLNAME")
239 "List of affiliated keywords as strings.
240 By default, all keywords setting attributes (i.e. \"ATTR_LATEX\")
241 are affiliated keywords and need not to be in this list.")
243 (defconst org-element--affiliated-re
244 (format "[ \t]*#\\+%s:"
245 ;; Regular affiliated keywords.
246 (format "\\(%s\\|ATTR_[-_A-Za-z0-9]+\\)\\(?:\\[\\(.*\\)\\]\\)?"
247 (regexp-opt org-element-affiliated-keywords
)))
248 "Regexp matching any affiliated keyword.
250 Keyword name is put in match group 1. Moreover, if keyword
251 belongs to `org-element-dual-keywords', put the dual value in
254 Don't modify it, set `org-element-affiliated-keywords' instead.")
256 (defconst org-element-keyword-translation-alist
257 '(("DATA" .
"NAME") ("LABEL" .
"NAME") ("RESNAME" .
"NAME")
258 ("SOURCE" .
"NAME") ("SRCNAME" .
"NAME") ("TBLNAME" .
"NAME")
259 ("RESULT" .
"RESULTS") ("HEADERS" .
"HEADER"))
260 "Alist of usual translations for keywords.
261 The key is the old name and the value the new one. The property
262 holding their value will be named after the translated name.")
264 (defconst org-element-multiple-keywords
'("CAPTION" "HEADER")
265 "List of affiliated keywords that can occur more than once in an element.
267 Their value will be consed into a list of strings, which will be
268 returned as the value of the property.
270 This list is checked after translations have been applied. See
271 `org-element-keyword-translation-alist'.
273 By default, all keywords setting attributes (i.e. \"ATTR_LATEX\")
274 allow multiple occurrences and need not to be in this list.")
276 (defconst org-element-parsed-keywords
'("CAPTION")
277 "List of affiliated keywords whose value can be parsed.
279 Their value will be stored as a secondary string: a list of
282 This list is checked after translations have been applied. See
283 `org-element-keyword-translation-alist'.")
285 (defconst org-element-dual-keywords
'("CAPTION" "RESULTS")
286 "List of affiliated keywords which can have a secondary value.
288 In Org syntax, they can be written with optional square brackets
289 before the colons. For example, RESULTS keyword can be
290 associated to a hash value with the following:
292 #+RESULTS[hash-string]: some-source
294 This list is checked after translations have been applied. See
295 `org-element-keyword-translation-alist'.")
297 (defconst org-element-document-properties
'("AUTHOR" "DATE" "TITLE")
298 "List of properties associated to the whole document.
299 Any keyword in this list will have its value parsed and stored as
300 a secondary string.")
302 (defconst org-element-object-restrictions
304 (remq 'plain-link
(remq 'table-cell org-element-all-successors
)))
305 (standard-set-no-line-break (remq 'line-break standard-set
)))
306 `((bold ,@standard-set
)
307 (footnote-reference ,@standard-set
)
308 (headline ,@standard-set-no-line-break
)
309 (inlinetask ,@standard-set-no-line-break
)
310 (italic ,@standard-set
)
311 (item ,@standard-set-no-line-break
)
312 (keyword ,@standard-set
)
313 ;; Ignore all links excepted plain links in a link description.
314 ;; Also ignore radio-targets and line breaks.
315 (link export-snippet inline-babel-call inline-src-block latex-or-entity
316 macro plain-link statistics-cookie sub
/superscript text-markup
)
317 (paragraph ,@standard-set
)
318 ;; Remove any variable object from radio target as it would
319 ;; prevent it from being properly recognized.
320 (radio-target latex-or-entity sub
/superscript
)
321 (strike-through ,@standard-set
)
322 (subscript ,@standard-set
)
323 (superscript ,@standard-set
)
324 ;; Ignore inline babel call and inline src block as formulas are
325 ;; possible. Also ignore line breaks and statistics cookies.
326 (table-cell export-snippet footnote-reference latex-or-entity link macro
327 radio-target sub
/superscript target text-markup timestamp
)
328 (table-row table-cell
)
329 (underline ,@standard-set
)
330 (verse-block ,@standard-set
)))
331 "Alist of objects restrictions.
333 CAR is an element or object type containing objects and CDR is
334 a list of successors that will be called within an element or
337 For example, in a `radio-target' object, one can only find
338 entities, latex-fragments, subscript and superscript.
340 This alist also applies to secondary string. For example, an
341 `headline' type element doesn't directly contain objects, but
342 still has an entry since one of its properties (`:title') does.")
344 (defconst org-element-secondary-value-alist
345 '((headline .
:title
)
346 (inlinetask .
:title
)
348 (footnote-reference .
:inline-definition
))
349 "Alist between element types and location of secondary value.")
351 (defconst org-element-object-variables
'(org-link-abbrev-alist-local)
352 "List of buffer-local variables used when parsing objects.
353 These variables are copied to the temporary buffer created by
354 `org-export-secondary-string'.")
358 ;;; Accessors and Setters
360 ;; Provide four accessors: `org-element-type', `org-element-property'
361 ;; `org-element-contents' and `org-element-restriction'.
363 ;; Setter functions allow to modify elements by side effect. There is
364 ;; `org-element-put-property', `org-element-set-contents'. These
365 ;; low-level functions are useful to build a parse tree.
367 ;; `org-element-adopt-element', `org-element-set-element',
368 ;; `org-element-extract-element' and `org-element-insert-before' are
369 ;; high-level functions useful to modify a parse tree.
371 ;; `org-element-secondary-p' is a predicate used to know if a given
372 ;; object belongs to a secondary string.
374 (defsubst org-element-type
(element)
375 "Return type of ELEMENT.
377 The function returns the type of the element or object provided.
378 It can also return the following special value:
379 `plain-text' for a string
380 `org-data' for a complete document
381 nil in any other case."
383 ((not (consp element
)) (and (stringp element
) 'plain-text
))
384 ((symbolp (car element
)) (car element
))))
386 (defsubst org-element-property
(property element
)
387 "Extract the value from the PROPERTY of an ELEMENT."
388 (if (stringp element
) (get-text-property 0 property element
)
389 (plist-get (nth 1 element
) property
)))
391 (defsubst org-element-contents
(element)
392 "Extract contents from an ELEMENT."
393 (cond ((not (consp element
)) nil
)
394 ((symbolp (car element
)) (nthcdr 2 element
))
397 (defsubst org-element-restriction
(element)
398 "Return restriction associated to ELEMENT.
399 ELEMENT can be an element, an object or a symbol representing an
400 element or object type."
401 (cdr (assq (if (symbolp element
) element
(org-element-type element
))
402 org-element-object-restrictions
)))
404 (defsubst org-element-put-property
(element property value
)
405 "In ELEMENT set PROPERTY to VALUE.
406 Return modified element."
407 (if (stringp element
) (org-add-props element nil property value
)
408 (setcar (cdr element
) (plist-put (nth 1 element
) property value
))
411 (defsubst org-element-set-contents
(element &rest contents
)
412 "Set ELEMENT contents to CONTENTS.
413 Return modified element."
414 (cond ((not element
) (list contents
))
415 ((not (symbolp (car element
))) contents
)
416 ((cdr element
) (setcdr (cdr element
) contents
))
417 (t (nconc element contents
))))
419 (defun org-element-secondary-p (object)
420 "Non-nil when OBJECT belongs to a secondary string.
421 Return value is the property name, as a keyword, or nil."
422 (let* ((parent (org-element-property :parent object
))
423 (property (cdr (assq (org-element-type parent
)
424 org-element-secondary-value-alist
))))
426 (memq object
(org-element-property property parent
))
429 (defsubst org-element-adopt-elements
(parent &rest children
)
430 "Append elements to the contents of another element.
432 PARENT is an element or object. CHILDREN can be elements,
433 objects, or a strings.
435 The function takes care of setting `:parent' property for CHILD.
436 Return parent element."
437 ;; Link every child to PARENT. If PARENT is nil, it is a secondary
438 ;; string: parent is the list itself.
439 (mapc (lambda (child)
440 (org-element-put-property child
:parent
(or parent children
)))
442 ;; Add CHILDREN at the end of PARENT contents.
444 (apply 'org-element-set-contents
446 (nconc (org-element-contents parent
) children
)))
447 ;; Return modified PARENT element.
448 (or parent children
))
450 (defun org-element-extract-element (element)
451 "Extract ELEMENT from parse tree.
452 Remove element from the parse tree by side-effect, and return it
453 with its `:parent' property stripped out."
454 (let ((parent (org-element-property :parent element
))
455 (secondary (org-element-secondary-p element
)))
457 (org-element-put-property
459 (delq element
(org-element-property secondary parent
)))
460 (apply #'org-element-set-contents
462 (delq element
(org-element-contents parent
))))
463 ;; Return ELEMENT with its :parent removed.
464 (org-element-put-property element
:parent nil
)))
466 (defun org-element-insert-before (element location
)
467 "Insert ELEMENT before LOCATION in parse tree.
468 LOCATION is an element, object or string within the parse tree.
469 Parse tree is modified by side effect."
470 (let* ((parent (org-element-property :parent location
))
471 (property (org-element-secondary-p location
))
472 (siblings (if property
(org-element-property property parent
)
473 (org-element-contents parent
))))
474 ;; Install ELEMENT at the appropriate POSITION within SIBLINGS.
475 (cond ((or (null siblings
) (eq (car siblings
) location
))
476 (push element siblings
))
477 ((null location
) (nconc siblings
(list element
)))
478 (t (let ((previous (cadr (memq location
(reverse siblings
)))))
480 (error "No location found to insert element")
481 (let ((next (memq previous siblings
)))
482 (setcdr next
(cons element
(cdr next
))))))))
483 ;; Store SIBLINGS at appropriate place in parse tree.
484 (if property
(org-element-put-property parent property siblings
)
485 (apply #'org-element-set-contents parent siblings
))
486 ;; Set appropriate :parent property.
487 (org-element-put-property element
:parent parent
)))
489 (defun org-element-set-element (old new
)
490 "Replace element or object OLD with element or object NEW.
491 The function takes care of setting `:parent' property for NEW."
492 ;; Ensure OLD and NEW have the same parent.
493 (org-element-put-property new
:parent
(org-element-property :parent old
))
494 (if (or (memq (org-element-type old
) '(plain-text nil
))
495 (memq (org-element-type new
) '(plain-text nil
)))
496 ;; We cannot replace OLD with NEW since one of them is not an
497 ;; object or element. We take the long path.
498 (progn (org-element-insert-before new old
)
499 (org-element-extract-element old
))
500 ;; Since OLD is going to be changed into NEW by side-effect, first
501 ;; make sure that every element or object within NEW has OLD as
503 (dolist (blob (org-element-contents new
))
504 (org-element-put-property blob
:parent old
))
505 ;; Transfer contents.
506 (apply #'org-element-set-contents old
(org-element-contents new
))
507 ;; Overwrite OLD's properties with NEW's.
508 (setcar (cdr old
) (nth 1 new
))
510 (setcar old
(car new
))))
516 ;; For each greater element type, we define a parser and an
519 ;; A parser returns the element or object as the list described above.
520 ;; Most of them accepts no argument. Though, exceptions exist. Hence
521 ;; every element containing a secondary string (see
522 ;; `org-element-secondary-value-alist') will accept an optional
523 ;; argument to toggle parsing of that secondary string. Moreover,
524 ;; `item' parser requires current list's structure as its first
527 ;; An interpreter accepts two arguments: the list representation of
528 ;; the element or object, and its contents. The latter may be nil,
529 ;; depending on the element or object considered. It returns the
530 ;; appropriate Org syntax, as a string.
532 ;; Parsing functions must follow the naming convention:
533 ;; org-element-TYPE-parser, where TYPE is greater element's type, as
534 ;; defined in `org-element-greater-elements'.
536 ;; Similarly, interpreting functions must follow the naming
537 ;; convention: org-element-TYPE-interpreter.
539 ;; With the exception of `headline' and `item' types, greater elements
540 ;; cannot contain other greater elements of their own type.
542 ;; Beside implementing a parser and an interpreter, adding a new
543 ;; greater element requires to tweak `org-element--current-element'.
544 ;; Moreover, the newly defined type must be added to both
545 ;; `org-element-all-elements' and `org-element-greater-elements'.
550 (defun org-element-center-block-parser (limit affiliated
)
551 "Parse a center block.
553 LIMIT bounds the search. AFFILIATED is a list of which CAR is
554 the buffer position at the beginning of the first affiliated
555 keyword and CDR is a plist of affiliated keywords along with
558 Return a list whose CAR is `center-block' and CDR is a plist
559 containing `:begin', `:end', `:contents-begin', `:contents-end',
560 `:post-blank' and `:post-affiliated' keywords.
562 Assume point is at the beginning of the block."
563 (let ((case-fold-search t
))
564 (if (not (save-excursion
565 (re-search-forward "^[ \t]*#\\+END_CENTER[ \t]*$" limit t
)))
566 ;; Incomplete block: parse it as a paragraph.
567 (org-element-paragraph-parser limit affiliated
)
568 (let ((block-end-line (match-beginning 0)))
569 (let* ((begin (car affiliated
))
570 (post-affiliated (point))
571 ;; Empty blocks have no contents.
572 (contents-begin (progn (forward-line)
573 (and (< (point) block-end-line
)
575 (contents-end (and contents-begin block-end-line
))
576 (pos-before-blank (progn (goto-char block-end-line
)
580 (skip-chars-forward " \r\t\n" limit
)
581 (if (eobp) (point) (line-beginning-position)))))
586 :contents-begin contents-begin
587 :contents-end contents-end
588 :post-blank
(count-lines pos-before-blank end
)
589 :post-affiliated post-affiliated
)
590 (cdr affiliated
))))))))
592 (defun org-element-center-block-interpreter (center-block contents
)
593 "Interpret CENTER-BLOCK element as Org syntax.
594 CONTENTS is the contents of the element."
595 (format "#+BEGIN_CENTER\n%s#+END_CENTER" contents
))
600 (defun org-element-drawer-parser (limit affiliated
)
603 LIMIT bounds the search. AFFILIATED is a list of which CAR is
604 the buffer position at the beginning of the first affiliated
605 keyword and CDR is a plist of affiliated keywords along with
608 Return a list whose CAR is `drawer' and CDR is a plist containing
609 `:drawer-name', `:begin', `:end', `:contents-begin',
610 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
612 Assume point is at beginning of drawer."
613 (let ((case-fold-search t
))
614 (if (not (save-excursion (re-search-forward "^[ \t]*:END:[ \t]*$" limit t
)))
615 ;; Incomplete drawer: parse it as a paragraph.
616 (org-element-paragraph-parser limit affiliated
)
618 (let* ((drawer-end-line (match-beginning 0))
619 (name (progn (looking-at org-drawer-regexp
)
620 (org-match-string-no-properties 1)))
621 (begin (car affiliated
))
622 (post-affiliated (point))
623 ;; Empty drawers have no contents.
624 (contents-begin (progn (forward-line)
625 (and (< (point) drawer-end-line
)
627 (contents-end (and contents-begin drawer-end-line
))
628 (pos-before-blank (progn (goto-char drawer-end-line
)
631 (end (progn (skip-chars-forward " \r\t\n" limit
)
632 (if (eobp) (point) (line-beginning-position)))))
638 :contents-begin contents-begin
639 :contents-end contents-end
640 :post-blank
(count-lines pos-before-blank end
)
641 :post-affiliated post-affiliated
)
642 (cdr affiliated
))))))))
644 (defun org-element-drawer-interpreter (drawer contents
)
645 "Interpret DRAWER element as Org syntax.
646 CONTENTS is the contents of the element."
647 (format ":%s:\n%s:END:"
648 (org-element-property :drawer-name drawer
)
654 (defun org-element-dynamic-block-parser (limit affiliated
)
655 "Parse a dynamic block.
657 LIMIT bounds the search. AFFILIATED is a list of which CAR is
658 the buffer position at the beginning of the first affiliated
659 keyword and CDR is a plist of affiliated keywords along with
662 Return a list whose CAR is `dynamic-block' and CDR is a plist
663 containing `:block-name', `:begin', `:end', `:contents-begin',
664 `:contents-end', `:arguments', `:post-blank' and
665 `:post-affiliated' keywords.
667 Assume point is at beginning of dynamic block."
668 (let ((case-fold-search t
))
669 (if (not (save-excursion
670 (re-search-forward "^[ \t]*#\\+END:?[ \t]*$" limit t
)))
671 ;; Incomplete block: parse it as a paragraph.
672 (org-element-paragraph-parser limit affiliated
)
673 (let ((block-end-line (match-beginning 0)))
675 (let* ((name (progn (looking-at org-dblock-start-re
)
676 (org-match-string-no-properties 1)))
677 (arguments (org-match-string-no-properties 3))
678 (begin (car affiliated
))
679 (post-affiliated (point))
680 ;; Empty blocks have no contents.
681 (contents-begin (progn (forward-line)
682 (and (< (point) block-end-line
)
684 (contents-end (and contents-begin block-end-line
))
685 (pos-before-blank (progn (goto-char block-end-line
)
688 (end (progn (skip-chars-forward " \r\t\n" limit
)
689 (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-dynamic-block-interpreter (dynamic-block contents
)
703 "Interpret DYNAMIC-BLOCK element as Org syntax.
704 CONTENTS is the contents of the element."
705 (format "#+BEGIN: %s%s\n%s#+END:"
706 (org-element-property :block-name dynamic-block
)
707 (let ((args (org-element-property :arguments dynamic-block
)))
708 (and args
(concat " " args
)))
712 ;;;; Footnote Definition
714 (defun org-element-footnote-definition-parser (limit affiliated
)
715 "Parse a footnote definition.
717 LIMIT bounds the search. AFFILIATED is a list of which CAR is
718 the buffer position at the beginning of the first affiliated
719 keyword and CDR is a plist of affiliated keywords along with
722 Return a list whose CAR is `footnote-definition' and CDR is
723 a plist containing `:label', `:begin' `:end', `:contents-begin',
724 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
726 Assume point is at the beginning of the footnote definition."
728 (let* ((label (progn (looking-at org-footnote-definition-re
)
729 (org-match-string-no-properties 1)))
730 (begin (car affiliated
))
731 (post-affiliated (point))
732 (ending (save-excursion
736 (concat org-outline-regexp-bol
"\\|"
737 org-footnote-definition-re
"\\|"
738 "^\\([ \t]*\n\\)\\{2,\\}") limit
'move
))
741 (contents-begin (progn
743 (skip-chars-forward " \r\t\n" ending
)
744 (cond ((= (point) ending
) nil
)
745 ((= (line-beginning-position) begin
) (point))
746 (t (line-beginning-position)))))
747 (contents-end (and contents-begin ending
))
748 (end (progn (goto-char ending
)
749 (skip-chars-forward " \r\t\n" limit
)
750 (if (eobp) (point) (line-beginning-position)))))
751 (list 'footnote-definition
756 :contents-begin contents-begin
757 :contents-end contents-end
758 :post-blank
(count-lines ending end
)
759 :post-affiliated post-affiliated
)
760 (cdr affiliated
))))))
762 (defun org-element-footnote-definition-interpreter (footnote-definition contents
)
763 "Interpret FOOTNOTE-DEFINITION element as Org syntax.
764 CONTENTS is the contents of the footnote-definition."
765 (concat (format "[%s]" (org-element-property :label footnote-definition
))
772 (defun org-element-headline-parser (limit &optional raw-secondary-p
)
775 Return a list whose CAR is `headline' and CDR is a plist
776 containing `:raw-value', `:title', `:alt-title', `:begin',
777 `:end', `:pre-blank', `:contents-begin' and `:contents-end',
778 `:level', `:priority', `:tags', `:todo-keyword',`:todo-type',
779 `:scheduled', `:deadline', `:closed', `:quotedp', `:archivedp',
780 `:commentedp' and `:footnote-section-p' keywords.
782 The plist also contains any property set in the property drawer,
783 with its name in upper cases and colons added at the
784 beginning (i.e. `:CUSTOM_ID').
786 When RAW-SECONDARY-P is non-nil, headline's title will not be
787 parsed as a secondary string, but as a plain string instead.
789 Assume point is at beginning of the headline."
791 (let* ((components (org-heading-components))
792 (level (nth 1 components
))
793 (todo (nth 2 components
))
795 (and todo
(if (member todo org-done-keywords
) 'done
'todo
)))
796 (tags (let ((raw-tags (nth 5 components
)))
797 (and raw-tags
(org-split-string raw-tags
":"))))
798 (raw-value (or (nth 4 components
) ""))
800 (let ((case-fold-search nil
))
801 (string-match (format "^%s\\( \\|$\\)" org-quote-string
)
804 (let ((case-fold-search nil
))
805 (string-match (format "^%s\\( \\|$\\)" org-comment-string
)
807 (archivedp (member org-archive-tag tags
))
808 (footnote-section-p (and org-footnote-section
809 (string= org-footnote-section raw-value
)))
810 ;; Upcase property names. It avoids confusion between
811 ;; properties obtained through property drawer and default
812 ;; properties from the parser (e.g. `:end' and :END:)
819 (intern (concat ":" (upcase (car p
))))
821 (org-entry-properties nil
'standard
))
824 ;; Read time properties on the line below the headline.
826 (when (progn (forward-line)
827 (looking-at org-planning-or-clock-line-re
))
828 (let ((end (line-end-position)) plist
)
829 (while (re-search-forward
830 org-keyword-time-not-clock-regexp end t
)
831 (goto-char (match-end 1))
832 (skip-chars-forward " \t")
833 (let ((keyword (match-string 1))
834 (time (org-element-timestamp-parser)))
835 (cond ((equal keyword org-scheduled-string
)
836 (setq plist
(plist-put plist
:scheduled time
)))
837 ((equal keyword org-deadline-string
)
838 (setq plist
(plist-put plist
:deadline time
)))
839 (t (setq plist
(plist-put plist
:closed time
))))))
842 (end (save-excursion (goto-char (org-end-of-subtree t t
))))
843 (pos-after-head (progn (forward-line) (point)))
844 (contents-begin (save-excursion
845 (skip-chars-forward " \r\t\n" end
)
846 (and (/= (point) end
) (line-beginning-position))))
847 (contents-end (and contents-begin
848 (progn (goto-char end
)
849 (skip-chars-backward " \r\t\n")
852 ;; Clean RAW-VALUE from any quote or comment string.
853 (when (or quotedp commentedp
)
854 (let ((case-fold-search nil
))
856 (replace-regexp-in-string
858 (regexp-opt (list org-quote-string org-comment-string
))
862 ;; Clean TAGS from archive tag, if any.
863 (when archivedp
(setq tags
(delete org-archive-tag tags
)))
867 (list :raw-value raw-value
871 (if (not contents-begin
) 0
872 (count-lines pos-after-head contents-begin
))
873 :contents-begin contents-begin
874 :contents-end contents-end
876 :priority
(nth 3 components
)
880 :post-blank
(count-lines
881 (if (not contents-end
) pos-after-head
882 (goto-char contents-end
)
886 :footnote-section-p footnote-section-p
888 :commentedp commentedp
892 (let ((alt-title (org-element-property :ALT_TITLE headline
)))
894 (org-element-put-property
896 (if raw-secondary-p alt-title
897 (org-element-parse-secondary-string
898 alt-title
(org-element-restriction 'headline
) headline
)))))
899 (org-element-put-property
901 (if raw-secondary-p raw-value
902 (org-element-parse-secondary-string
903 raw-value
(org-element-restriction 'headline
) headline
)))))))
905 (defun org-element-headline-interpreter (headline contents
)
906 "Interpret HEADLINE element as Org syntax.
907 CONTENTS is the contents of the element."
908 (let* ((level (org-element-property :level headline
))
909 (todo (org-element-property :todo-keyword headline
))
910 (priority (org-element-property :priority headline
))
911 (title (org-element-interpret-data
912 (org-element-property :title headline
)))
913 (tags (let ((tag-list (if (org-element-property :archivedp headline
)
914 (cons org-archive-tag
915 (org-element-property :tags headline
))
916 (org-element-property :tags headline
))))
918 (format ":%s:" (mapconcat 'identity tag-list
":")))))
919 (commentedp (org-element-property :commentedp headline
))
920 (quotedp (org-element-property :quotedp headline
))
921 (pre-blank (or (org-element-property :pre-blank headline
) 0))
922 (heading (concat (make-string (org-reduced-level level
) ?
*)
923 (and todo
(concat " " todo
))
924 (and quotedp
(concat " " org-quote-string
))
925 (and commentedp
(concat " " org-comment-string
))
927 (format " [#%s]" (char-to-string priority
)))
928 (cond ((and org-footnote-section
929 (org-element-property
930 :footnote-section-p headline
))
931 (concat " " org-footnote-section
))
932 (title (concat " " title
))))))
937 ((zerop org-tags-column
) (format " %s" tags
))
938 ((< org-tags-column
0)
941 (max (- (+ org-tags-column
(length heading
) (length tags
))) 1)
946 (make-string (max (- org-tags-column
(length heading
)) 1) ?
)
948 (make-string (1+ pre-blank
) 10)
954 (defun org-element-inlinetask-parser (limit &optional raw-secondary-p
)
955 "Parse an inline task.
957 Return a list whose CAR is `inlinetask' and CDR is a plist
958 containing `:title', `:begin', `:end', `:contents-begin' and
959 `:contents-end', `:level', `:priority', `:raw-value', `:tags',
960 `:todo-keyword', `:todo-type', `:scheduled', `:deadline',
961 `:closed' and `:post-blank' keywords.
963 The plist also contains any property set in the property drawer,
964 with its name in upper cases and colons added at the
965 beginning (i.e. `:CUSTOM_ID').
967 When optional argument RAW-SECONDARY-P is non-nil, inline-task's
968 title will not be parsed as a secondary string, but as a plain
971 Assume point is at beginning of the inline task."
973 (let* ((begin (point))
974 (components (org-heading-components))
975 (todo (nth 2 components
))
977 (if (member todo org-done-keywords
) 'done
'todo
)))
978 (tags (let ((raw-tags (nth 5 components
)))
979 (and raw-tags
(org-split-string raw-tags
":"))))
980 (raw-value (or (nth 4 components
) ""))
981 ;; Upcase property names. It avoids confusion between
982 ;; properties obtained through property drawer and default
983 ;; properties from the parser (e.g. `:end' and :END:)
990 (intern (concat ":" (upcase (car p
))))
992 (org-entry-properties nil
'standard
))
995 ;; Read time properties on the line below the inlinetask
998 (when (progn (forward-line)
999 (looking-at org-planning-or-clock-line-re
))
1000 (let ((end (line-end-position)) plist
)
1001 (while (re-search-forward
1002 org-keyword-time-not-clock-regexp end t
)
1003 (goto-char (match-end 1))
1004 (skip-chars-forward " \t")
1005 (let ((keyword (match-string 1))
1006 (time (org-element-timestamp-parser)))
1007 (cond ((equal keyword org-scheduled-string
)
1008 (setq plist
(plist-put plist
:scheduled time
)))
1009 ((equal keyword org-deadline-string
)
1010 (setq plist
(plist-put plist
:deadline time
)))
1011 (t (setq plist
(plist-put plist
:closed time
))))))
1013 (task-end (save-excursion
1015 (and (re-search-forward "^\\*+ END" limit t
)
1016 (match-beginning 0))))
1017 (contents-begin (progn (forward-line)
1018 (and task-end
(< (point) task-end
) (point))))
1019 (contents-end (and contents-begin task-end
))
1020 (before-blank (if (not task-end
) (point)
1021 (goto-char task-end
)
1024 (end (progn (skip-chars-forward " \r\t\n" limit
)
1025 (if (eobp) (point) (line-beginning-position))))
1029 (list :raw-value raw-value
1032 :contents-begin contents-begin
1033 :contents-end contents-end
1034 :level
(nth 1 components
)
1035 :priority
(nth 3 components
)
1038 :todo-type todo-type
1039 :post-blank
(count-lines before-blank end
))
1042 (org-element-put-property
1044 (if raw-secondary-p raw-value
1045 (org-element-parse-secondary-string
1047 (org-element-restriction 'inlinetask
)
1050 (defun org-element-inlinetask-interpreter (inlinetask contents
)
1051 "Interpret INLINETASK element as Org syntax.
1052 CONTENTS is the contents of inlinetask."
1053 (let* ((level (org-element-property :level inlinetask
))
1054 (todo (org-element-property :todo-keyword inlinetask
))
1055 (priority (org-element-property :priority inlinetask
))
1056 (title (org-element-interpret-data
1057 (org-element-property :title inlinetask
)))
1058 (tags (let ((tag-list (org-element-property :tags inlinetask
)))
1060 (format ":%s:" (mapconcat 'identity tag-list
":")))))
1061 (task (concat (make-string level ?
*)
1062 (and todo
(concat " " todo
))
1064 (format " [#%s]" (char-to-string priority
)))
1065 (and title
(concat " " title
)))))
1070 ((zerop org-tags-column
) (format " %s" tags
))
1071 ((< org-tags-column
0)
1074 (max (- (+ org-tags-column
(length task
) (length tags
))) 1)
1079 (make-string (max (- org-tags-column
(length task
)) 1) ?
)
1081 ;; Prefer degenerate inlinetasks when there are no
1086 (make-string level ?
*) " END")))))
1091 (defun org-element-item-parser (limit struct
&optional raw-secondary-p
)
1094 STRUCT is the structure of the plain list.
1096 Return a list whose CAR is `item' and CDR is a plist containing
1097 `:bullet', `:begin', `:end', `:contents-begin', `:contents-end',
1098 `:checkbox', `:counter', `:tag', `:structure' and `:post-blank'
1101 When optional argument RAW-SECONDARY-P is non-nil, item's tag, if
1102 any, will not be parsed as a secondary string, but as a plain
1105 Assume point is at the beginning of the item."
1108 (looking-at org-list-full-item-re
)
1109 (let* ((begin (point))
1110 (bullet (org-match-string-no-properties 1))
1111 (checkbox (let ((box (org-match-string-no-properties 3)))
1112 (cond ((equal "[ ]" box
) 'off
)
1113 ((equal "[X]" box
) 'on
)
1114 ((equal "[-]" box
) 'trans
))))
1115 (counter (let ((c (org-match-string-no-properties 2)))
1119 ((string-match "[A-Za-z]" c
)
1120 (- (string-to-char (upcase (match-string 0 c
)))
1122 ((string-match "[0-9]+" c
)
1123 (string-to-number (match-string 0 c
)))))))
1124 (end (progn (goto-char (nth 6 (assq (point) struct
)))
1125 (unless (bolp) (forward-line))
1129 ;; Ignore tags in un-ordered lists: they are just
1130 ;; a part of item's body.
1131 (if (and (match-beginning 4)
1132 (save-match-data (string-match "[.)]" bullet
)))
1135 (skip-chars-forward " \r\t\n" limit
)
1136 ;; If first line isn't empty, contents really start
1137 ;; at the text after item's meta-data.
1138 (if (= (point-at-bol) begin
) (point) (point-at-bol))))
1139 (contents-end (progn (goto-char end
)
1140 (skip-chars-backward " \r\t\n")
1145 (list :bullet bullet
1148 ;; CONTENTS-BEGIN and CONTENTS-END may be
1149 ;; mixed up in the case of an empty item
1150 ;; separated from the next by a blank line.
1151 ;; Thus ensure the former is always the
1153 :contents-begin
(min contents-begin contents-end
)
1154 :contents-end
(max contents-begin contents-end
)
1158 :post-blank
(count-lines contents-end end
)))))
1159 (org-element-put-property
1161 (let ((raw-tag (org-list-get-tag begin struct
)))
1163 (if raw-secondary-p raw-tag
1164 (org-element-parse-secondary-string
1165 raw-tag
(org-element-restriction 'item
) item
))))))))
1167 (defun org-element-item-interpreter (item contents
)
1168 "Interpret ITEM element as Org syntax.
1169 CONTENTS is the contents of the element."
1170 (let* ((bullet (let ((bullet (org-element-property :bullet item
)))
1171 (org-list-bullet-string
1172 (cond ((not (string-match "[0-9a-zA-Z]" bullet
)) "- ")
1173 ((eq org-plain-list-ordered-item-terminator ?\
)) "1)")
1175 (checkbox (org-element-property :checkbox item
))
1176 (counter (org-element-property :counter item
))
1177 (tag (let ((tag (org-element-property :tag item
)))
1178 (and tag
(org-element-interpret-data tag
))))
1179 ;; Compute indentation.
1180 (ind (make-string (length bullet
) 32))
1181 (item-starts-with-par-p
1182 (eq (org-element-type (car (org-element-contents item
)))
1187 (and counter
(format "[@%d] " counter
))
1192 (and tag
(format "%s :: " tag
))
1194 (let ((contents (replace-regexp-in-string
1195 "\\(^\\)[ \t]*\\S-" ind contents nil nil
1)))
1196 (if item-starts-with-par-p
(org-trim contents
)
1197 (concat "\n" contents
)))))))
1202 (defun org-element--list-struct (limit)
1203 ;; Return structure of list at point. Internal function. See
1204 ;; `org-list-struct' for details.
1205 (let ((case-fold-search t
)
1207 (item-re (org-item-re))
1208 (inlinetask-re (and (featurep 'org-inlinetask
) "^\\*+ "))
1214 ;; At limit: end all items.
1217 (let ((end (progn (skip-chars-backward " \r\t\n")
1220 (dolist (item items
(sort (nconc items struct
)
1221 'car-less-than-car
))
1222 (setcar (nthcdr 6 item
) end
)))))
1223 ;; At list end: end all items.
1224 ((looking-at org-list-end-re
)
1225 (throw 'exit
(dolist (item items
(sort (nconc items struct
)
1226 'car-less-than-car
))
1227 (setcar (nthcdr 6 item
) (point)))))
1228 ;; At a new item: end previous sibling.
1229 ((looking-at item-re
)
1230 (let ((ind (save-excursion (skip-chars-forward " \t")
1232 (setq top-ind
(min top-ind ind
))
1233 (while (and items
(<= ind
(nth 1 (car items
))))
1234 (let ((item (pop items
)))
1235 (setcar (nthcdr 6 item
) (point))
1236 (push item struct
)))
1237 (push (progn (looking-at org-list-full-item-re
)
1238 (let ((bullet (match-string-no-properties 1)))
1242 (match-string-no-properties 2) ; counter
1243 (match-string-no-properties 3) ; checkbox
1245 (and (save-match-data
1246 (string-match "[-+*]" bullet
))
1247 (match-string-no-properties 4))
1248 ;; Ending position, unknown so far.
1252 ;; Skip empty lines.
1253 ((looking-at "^[ \t]*$") (forward-line))
1254 ;; Skip inline tasks and blank lines along the way.
1255 ((and inlinetask-re
(looking-at inlinetask-re
))
1257 (let ((origin (point)))
1258 (when (re-search-forward inlinetask-re limit t
)
1259 (if (looking-at "^\\*+ END[ \t]*$") (forward-line)
1260 (goto-char origin
)))))
1261 ;; At some text line. Check if it ends any previous item.
1263 (let ((ind (progn (skip-chars-forward " \t") (current-column))))
1264 (when (<= ind top-ind
)
1265 (skip-chars-backward " \r\t\n")
1267 (while (<= ind
(nth 1 (car items
)))
1268 (let ((item (pop items
)))
1269 (setcar (nthcdr 6 item
) (line-beginning-position))
1272 (throw 'exit
(sort struct
'car-less-than-car
))))))
1273 ;; Skip blocks (any type) and drawers contents.
1275 ((and (looking-at "#\\+BEGIN\\(:\\|_\\S-+\\)")
1277 (format "^[ \t]*#\\+END%s[ \t]*$"
1278 (org-match-string-no-properties 1))
1280 ((and (looking-at org-drawer-regexp
)
1281 (re-search-forward "^[ \t]*:END:[ \t]*$" limit t
))))
1282 (forward-line))))))))
1284 (defun org-element-plain-list-parser (limit affiliated structure
)
1285 "Parse a plain list.
1287 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1288 the buffer position at the beginning of the first affiliated
1289 keyword and CDR is a plist of affiliated keywords along with
1290 their value. STRUCTURE is the structure of the plain list being
1293 Return a list whose CAR is `plain-list' and CDR is a plist
1294 containing `:type', `:begin', `:end', `:contents-begin' and
1295 `:contents-end', `:structure', `:post-blank' and
1296 `:post-affiliated' keywords.
1298 Assume point is at the beginning of the list."
1300 (let* ((struct (or structure
(org-element--list-struct limit
)))
1301 (type (cond ((org-looking-at-p "[ \t]*[A-Za-z0-9]") 'ordered
)
1302 ((nth 5 (assq (point) struct
)) 'descriptive
)
1304 (contents-begin (point))
1305 (begin (car affiliated
))
1306 (contents-end (let* ((item (assq contents-begin struct
))
1309 (while (and (setq item
(assq pos struct
))
1310 (= (nth 1 item
) ind
))
1311 (setq pos
(nth 6 item
)))
1313 (end (progn (goto-char contents-end
)
1314 (skip-chars-forward " \r\t\n" limit
)
1315 (if (= (point) limit
) limit
(line-beginning-position)))))
1322 :contents-begin contents-begin
1323 :contents-end contents-end
1325 :post-blank
(count-lines contents-end end
)
1326 :post-affiliated contents-begin
)
1327 (cdr affiliated
))))))
1329 (defun org-element-plain-list-interpreter (plain-list contents
)
1330 "Interpret PLAIN-LIST element as Org syntax.
1331 CONTENTS is the contents of the element."
1334 (goto-char (point-min))
1339 ;;;; Property Drawer
1341 (defun org-element-property-drawer-parser (limit affiliated
)
1342 "Parse a property drawer.
1344 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1345 the buffer position at the beginning of the first affiliated
1346 keyword and CDR is a plist of affiliated keywords along with
1349 Return a list whose CAR is `property-drawer' and CDR is a plist
1350 containing `:begin', `:end', `:contents-begin', `:contents-end',
1351 `:post-blank' and `:post-affiliated' keywords.
1353 Assume point is at the beginning of the property drawer."
1355 (let ((case-fold-search t
))
1356 (if (not (save-excursion
1357 (re-search-forward "^[ \t]*:END:[ \t]*$" limit t
)))
1358 ;; Incomplete drawer: parse it as a paragraph.
1359 (org-element-paragraph-parser limit affiliated
)
1361 (let* ((drawer-end-line (match-beginning 0))
1362 (begin (car affiliated
))
1363 (post-affiliated (point))
1364 (contents-begin (progn (forward-line)
1365 (and (< (point) drawer-end-line
)
1367 (contents-end (and contents-begin drawer-end-line
))
1368 (pos-before-blank (progn (goto-char drawer-end-line
)
1371 (end (progn (skip-chars-forward " \r\t\n" limit
)
1372 (if (eobp) (point) (line-beginning-position)))))
1373 (list 'property-drawer
1377 :contents-begin contents-begin
1378 :contents-end contents-end
1379 :post-blank
(count-lines pos-before-blank end
)
1380 :post-affiliated post-affiliated
)
1381 (cdr affiliated
)))))))))
1383 (defun org-element-property-drawer-interpreter (property-drawer contents
)
1384 "Interpret PROPERTY-DRAWER element as Org syntax.
1385 CONTENTS is the properties within the drawer."
1386 (format ":PROPERTIES:\n%s:END:" contents
))
1391 (defun org-element-quote-block-parser (limit affiliated
)
1392 "Parse a quote block.
1394 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1395 the buffer position at the beginning of the first affiliated
1396 keyword and CDR is a plist of affiliated keywords along with
1399 Return a list whose CAR is `quote-block' and CDR is a plist
1400 containing `:begin', `:end', `:contents-begin', `:contents-end',
1401 `:post-blank' and `:post-affiliated' keywords.
1403 Assume point is at the beginning of the block."
1404 (let ((case-fold-search t
))
1405 (if (not (save-excursion
1406 (re-search-forward "^[ \t]*#\\+END_QUOTE[ \t]*$" limit t
)))
1407 ;; Incomplete block: parse it as a paragraph.
1408 (org-element-paragraph-parser limit affiliated
)
1409 (let ((block-end-line (match-beginning 0)))
1411 (let* ((begin (car affiliated
))
1412 (post-affiliated (point))
1413 ;; Empty blocks have no contents.
1414 (contents-begin (progn (forward-line)
1415 (and (< (point) block-end-line
)
1417 (contents-end (and contents-begin block-end-line
))
1418 (pos-before-blank (progn (goto-char block-end-line
)
1421 (end (progn (skip-chars-forward " \r\t\n" limit
)
1422 (if (eobp) (point) (line-beginning-position)))))
1427 :contents-begin contents-begin
1428 :contents-end contents-end
1429 :post-blank
(count-lines pos-before-blank end
)
1430 :post-affiliated post-affiliated
)
1431 (cdr affiliated
)))))))))
1433 (defun org-element-quote-block-interpreter (quote-block contents
)
1434 "Interpret QUOTE-BLOCK element as Org syntax.
1435 CONTENTS is the contents of the element."
1436 (format "#+BEGIN_QUOTE\n%s#+END_QUOTE" contents
))
1441 (defun org-element-section-parser (limit)
1444 LIMIT bounds the search.
1446 Return a list whose CAR is `section' and CDR is a plist
1447 containing `:begin', `:end', `:contents-begin', `contents-end'
1448 and `:post-blank' keywords."
1450 ;; Beginning of section is the beginning of the first non-blank
1451 ;; line after previous headline.
1452 (let ((begin (point))
1453 (end (progn (org-with-limited-levels (outline-next-heading))
1455 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
1461 :contents-begin begin
1462 :contents-end pos-before-blank
1463 :post-blank
(count-lines pos-before-blank end
))))))
1465 (defun org-element-section-interpreter (section contents
)
1466 "Interpret SECTION element as Org syntax.
1467 CONTENTS is the contents of the element."
1473 (defun org-element-special-block-parser (limit affiliated
)
1474 "Parse a special block.
1476 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1477 the buffer position at the beginning of the first affiliated
1478 keyword and CDR is a plist of affiliated keywords along with
1481 Return a list whose CAR is `special-block' and CDR is a plist
1482 containing `:type', `:begin', `:end', `:contents-begin',
1483 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
1485 Assume point is at the beginning of the block."
1486 (let* ((case-fold-search t
)
1487 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
1488 (upcase (match-string-no-properties 1)))))
1489 (if (not (save-excursion
1491 (format "^[ \t]*#\\+END_%s[ \t]*$" (regexp-quote type
))
1493 ;; Incomplete block: parse it as a paragraph.
1494 (org-element-paragraph-parser limit affiliated
)
1495 (let ((block-end-line (match-beginning 0)))
1497 (let* ((begin (car affiliated
))
1498 (post-affiliated (point))
1499 ;; Empty blocks have no contents.
1500 (contents-begin (progn (forward-line)
1501 (and (< (point) block-end-line
)
1503 (contents-end (and contents-begin block-end-line
))
1504 (pos-before-blank (progn (goto-char block-end-line
)
1507 (end (progn (skip-chars-forward " \r\t\n" limit
)
1508 (if (eobp) (point) (line-beginning-position)))))
1509 (list 'special-block
1514 :contents-begin contents-begin
1515 :contents-end contents-end
1516 :post-blank
(count-lines pos-before-blank end
)
1517 :post-affiliated post-affiliated
)
1518 (cdr affiliated
)))))))))
1520 (defun org-element-special-block-interpreter (special-block contents
)
1521 "Interpret SPECIAL-BLOCK element as Org syntax.
1522 CONTENTS is the contents of the element."
1523 (let ((block-type (org-element-property :type special-block
)))
1524 (format "#+BEGIN_%s\n%s#+END_%s" block-type contents block-type
)))
1530 ;; For each element, a parser and an interpreter are also defined.
1531 ;; Both follow the same naming convention used for greater elements.
1533 ;; Also, as for greater elements, adding a new element type is done
1534 ;; through the following steps: implement a parser and an interpreter,
1535 ;; tweak `org-element--current-element' so that it recognizes the new
1536 ;; type and add that new type to `org-element-all-elements'.
1538 ;; As a special case, when the newly defined type is a block type,
1539 ;; `org-element-block-name-alist' has to be modified accordingly.
1544 (defun org-element-babel-call-parser (limit affiliated
)
1545 "Parse a babel call.
1547 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1548 the buffer position at the beginning of the first affiliated
1549 keyword and CDR is a plist of affiliated keywords along with
1552 Return a list whose CAR is `babel-call' and CDR is a plist
1553 containing `:begin', `:end', `:value', `:post-blank' and
1554 `:post-affiliated' as keywords."
1556 (let ((begin (car affiliated
))
1557 (post-affiliated (point))
1558 (value (progn (let ((case-fold-search t
))
1559 (re-search-forward "call:[ \t]*" nil t
))
1560 (buffer-substring-no-properties (point)
1561 (line-end-position))))
1562 (pos-before-blank (progn (forward-line) (point)))
1563 (end (progn (skip-chars-forward " \r\t\n" limit
)
1564 (if (eobp) (point) (line-beginning-position)))))
1570 :post-blank
(count-lines pos-before-blank end
)
1571 :post-affiliated post-affiliated
)
1572 (cdr affiliated
))))))
1574 (defun org-element-babel-call-interpreter (babel-call contents
)
1575 "Interpret BABEL-CALL element as Org syntax.
1577 (concat "#+CALL: " (org-element-property :value babel-call
)))
1582 (defun org-element-clock-parser (limit)
1585 LIMIT bounds the search.
1587 Return a list whose CAR is `clock' and CDR is a plist containing
1588 `:status', `:value', `:time', `:begin', `:end' and `:post-blank'
1591 (let* ((case-fold-search nil
)
1593 (value (progn (search-forward org-clock-string
(line-end-position) t
)
1594 (skip-chars-forward " \t")
1595 (org-element-timestamp-parser)))
1596 (duration (and (search-forward " => " (line-end-position) t
)
1597 (progn (skip-chars-forward " \t")
1598 (looking-at "\\(\\S-+\\)[ \t]*$"))
1599 (org-match-string-no-properties 1)))
1600 (status (if duration
'closed
'running
))
1601 (post-blank (let ((before-blank (progn (forward-line) (point))))
1602 (skip-chars-forward " \r\t\n" limit
)
1603 (skip-chars-backward " \t")
1604 (unless (bolp) (end-of-line))
1605 (count-lines before-blank
(point))))
1608 (list :status status
1613 :post-blank post-blank
)))))
1615 (defun org-element-clock-interpreter (clock contents
)
1616 "Interpret CLOCK element as Org syntax.
1618 (concat org-clock-string
" "
1619 (org-element-timestamp-interpreter
1620 (org-element-property :value clock
) nil
)
1621 (let ((duration (org-element-property :duration clock
)))
1626 (org-split-string duration
":")))))))
1631 (defun org-element-comment-parser (limit affiliated
)
1634 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1635 the buffer position at the beginning of the first affiliated
1636 keyword and CDR is a plist of affiliated keywords along with
1639 Return a list whose CAR is `comment' and CDR is a plist
1640 containing `:begin', `:end', `:value', `:post-blank',
1641 `:post-affiliated' keywords.
1643 Assume point is at comment beginning."
1645 (let* ((begin (car affiliated
))
1646 (post-affiliated (point))
1647 (value (prog2 (looking-at "[ \t]*# ?")
1648 (buffer-substring-no-properties
1649 (match-end 0) (line-end-position))
1652 ;; Get comments ending.
1654 (while (and (< (point) limit
) (looking-at "[ \t]*#\\( \\|$\\)"))
1655 ;; Accumulate lines without leading hash and first
1660 (buffer-substring-no-properties
1661 (match-end 0) (line-end-position))))
1664 (end (progn (goto-char com-end
)
1665 (skip-chars-forward " \r\t\n" limit
)
1666 (if (eobp) (point) (line-beginning-position)))))
1672 :post-blank
(count-lines com-end end
)
1673 :post-affiliated post-affiliated
)
1674 (cdr affiliated
))))))
1676 (defun org-element-comment-interpreter (comment contents
)
1677 "Interpret COMMENT element as Org syntax.
1679 (replace-regexp-in-string "^" "# " (org-element-property :value comment
)))
1684 (defun org-element-comment-block-parser (limit affiliated
)
1685 "Parse an export block.
1687 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1688 the buffer position at the beginning of the first affiliated
1689 keyword and CDR is a plist of affiliated keywords along with
1692 Return a list whose CAR is `comment-block' and CDR is a plist
1693 containing `:begin', `:end', `:value', `:post-blank' and
1694 `:post-affiliated' keywords.
1696 Assume point is at comment block beginning."
1697 (let ((case-fold-search t
))
1698 (if (not (save-excursion
1699 (re-search-forward "^[ \t]*#\\+END_COMMENT[ \t]*$" limit t
)))
1700 ;; Incomplete block: parse it as a paragraph.
1701 (org-element-paragraph-parser limit affiliated
)
1702 (let ((contents-end (match-beginning 0)))
1704 (let* ((begin (car affiliated
))
1705 (post-affiliated (point))
1706 (contents-begin (progn (forward-line) (point)))
1707 (pos-before-blank (progn (goto-char contents-end
)
1710 (end (progn (skip-chars-forward " \r\t\n" limit
)
1711 (if (eobp) (point) (line-beginning-position))))
1712 (value (buffer-substring-no-properties
1713 contents-begin contents-end
)))
1714 (list 'comment-block
1719 :post-blank
(count-lines pos-before-blank end
)
1720 :post-affiliated post-affiliated
)
1721 (cdr affiliated
)))))))))
1723 (defun org-element-comment-block-interpreter (comment-block contents
)
1724 "Interpret COMMENT-BLOCK element as Org syntax.
1726 (format "#+BEGIN_COMMENT\n%s#+END_COMMENT"
1727 (org-remove-indentation (org-element-property :value comment-block
))))
1732 (defun org-element-diary-sexp-parser (limit affiliated
)
1733 "Parse a diary sexp.
1735 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1736 the buffer position at the beginning of the first affiliated
1737 keyword and CDR is a plist of affiliated keywords along with
1740 Return a list whose CAR is `diary-sexp' and CDR is a plist
1741 containing `:begin', `:end', `:value', `:post-blank' and
1742 `:post-affiliated' keywords."
1744 (let ((begin (car affiliated
))
1745 (post-affiliated (point))
1746 (value (progn (looking-at "\\(%%(.*\\)[ \t]*$")
1747 (org-match-string-no-properties 1)))
1748 (pos-before-blank (progn (forward-line) (point)))
1749 (end (progn (skip-chars-forward " \r\t\n" limit
)
1750 (if (eobp) (point) (line-beginning-position)))))
1756 :post-blank
(count-lines pos-before-blank end
)
1757 :post-affiliated post-affiliated
)
1758 (cdr affiliated
))))))
1760 (defun org-element-diary-sexp-interpreter (diary-sexp contents
)
1761 "Interpret DIARY-SEXP as Org syntax.
1763 (org-element-property :value diary-sexp
))
1768 (defun org-element-example-block-parser (limit affiliated
)
1769 "Parse an example block.
1771 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1772 the buffer position at the beginning of the first affiliated
1773 keyword and CDR is a plist of affiliated keywords along with
1776 Return a list whose CAR is `example-block' and CDR is a plist
1777 containing `:begin', `:end', `:number-lines', `:preserve-indent',
1778 `:retain-labels', `:use-labels', `:label-fmt', `:switches',
1779 `:value', `:post-blank' and `:post-affiliated' keywords."
1780 (let ((case-fold-search t
))
1781 (if (not (save-excursion
1782 (re-search-forward "^[ \t]*#\\+END_EXAMPLE[ \t]*$" limit t
)))
1783 ;; Incomplete block: parse it as a paragraph.
1784 (org-element-paragraph-parser limit affiliated
)
1785 (let ((contents-end (match-beginning 0)))
1789 (looking-at "^[ \t]*#\\+BEGIN_EXAMPLE\\(?: +\\(.*\\)\\)?")
1790 (org-match-string-no-properties 1)))
1791 ;; Switches analysis
1793 (cond ((not switches
) nil
)
1794 ((string-match "-n\\>" switches
) 'new
)
1795 ((string-match "+n\\>" switches
) 'continued
)))
1797 (and switches
(string-match "-i\\>" switches
)))
1798 ;; Should labels be retained in (or stripped from) example
1802 (not (string-match "-r\\>" switches
))
1803 (and number-lines
(string-match "-k\\>" switches
))))
1804 ;; What should code-references use - labels or
1809 (not (string-match "-k\\>" switches
)))))
1812 (string-match "-l +\"\\([^\"\n]+\\)\"" switches
)
1813 (match-string 1 switches
)))
1814 ;; Standard block parsing.
1815 (begin (car affiliated
))
1816 (post-affiliated (point))
1817 (block-ind (progn (skip-chars-forward " \t") (current-column)))
1818 (contents-begin (progn (forward-line) (point)))
1819 (value (org-element-remove-indentation
1820 (org-unescape-code-in-string
1821 (buffer-substring-no-properties
1822 contents-begin contents-end
))
1824 (pos-before-blank (progn (goto-char contents-end
)
1827 (end (progn (skip-chars-forward " \r\t\n" limit
)
1828 (if (eobp) (point) (line-beginning-position)))))
1829 (list 'example-block
1835 :number-lines number-lines
1836 :preserve-indent preserve-indent
1837 :retain-labels retain-labels
1838 :use-labels use-labels
1839 :label-fmt label-fmt
1840 :post-blank
(count-lines pos-before-blank end
)
1841 :post-affiliated post-affiliated
)
1842 (cdr affiliated
)))))))))
1844 (defun org-element-example-block-interpreter (example-block contents
)
1845 "Interpret EXAMPLE-BLOCK element as Org syntax.
1847 (let ((switches (org-element-property :switches example-block
))
1848 (value (org-element-property :value example-block
)))
1849 (concat "#+BEGIN_EXAMPLE" (and switches
(concat " " switches
)) "\n"
1850 (org-escape-code-in-string
1851 (if (or org-src-preserve-indentation
1852 (org-element-property :preserve-indent example-block
))
1854 (org-element-remove-indentation value
)))
1860 (defun org-element-export-block-parser (limit affiliated
)
1861 "Parse an export block.
1863 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1864 the buffer position at the beginning of the first affiliated
1865 keyword and CDR is a plist of affiliated keywords along with
1868 Return a list whose CAR is `export-block' and CDR is a plist
1869 containing `:begin', `:end', `:type', `:value', `:post-blank' and
1870 `:post-affiliated' keywords.
1872 Assume point is at export-block beginning."
1873 (let* ((case-fold-search t
)
1874 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
1875 (upcase (org-match-string-no-properties 1)))))
1876 (if (not (save-excursion
1878 (format "^[ \t]*#\\+END_%s[ \t]*$" type
) limit t
)))
1879 ;; Incomplete block: parse it as a paragraph.
1880 (org-element-paragraph-parser limit affiliated
)
1881 (let ((contents-end (match-beginning 0)))
1883 (let* ((begin (car affiliated
))
1884 (post-affiliated (point))
1885 (contents-begin (progn (forward-line) (point)))
1886 (pos-before-blank (progn (goto-char contents-end
)
1889 (end (progn (skip-chars-forward " \r\t\n" limit
)
1890 (if (eobp) (point) (line-beginning-position))))
1891 (value (buffer-substring-no-properties contents-begin
1899 :post-blank
(count-lines pos-before-blank end
)
1900 :post-affiliated post-affiliated
)
1901 (cdr affiliated
)))))))))
1903 (defun org-element-export-block-interpreter (export-block contents
)
1904 "Interpret EXPORT-BLOCK element as Org syntax.
1906 (let ((type (org-element-property :type export-block
)))
1907 (concat (format "#+BEGIN_%s\n" type
)
1908 (org-element-property :value export-block
)
1909 (format "#+END_%s" type
))))
1914 (defun org-element-fixed-width-parser (limit affiliated
)
1915 "Parse a fixed-width section.
1917 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1918 the buffer position at the beginning of the first affiliated
1919 keyword and CDR is a plist of affiliated keywords along with
1922 Return a list whose CAR is `fixed-width' and CDR is a plist
1923 containing `:begin', `:end', `:value', `:post-blank' and
1924 `:post-affiliated' keywords.
1926 Assume point is at the beginning of the fixed-width area."
1928 (let* ((begin (car affiliated
))
1929 (post-affiliated (point))
1933 (while (and (< (point) limit
)
1934 (looking-at "[ \t]*:\\( \\|$\\)"))
1935 ;; Accumulate text without starting colons.
1938 (buffer-substring-no-properties
1939 (match-end 0) (point-at-eol))
1943 (end (progn (skip-chars-forward " \r\t\n" limit
)
1944 (if (eobp) (point) (line-beginning-position)))))
1950 :post-blank
(count-lines end-area end
)
1951 :post-affiliated post-affiliated
)
1952 (cdr affiliated
))))))
1954 (defun org-element-fixed-width-interpreter (fixed-width contents
)
1955 "Interpret FIXED-WIDTH element as Org syntax.
1957 (let ((value (org-element-property :value fixed-width
)))
1959 (replace-regexp-in-string
1961 (if (string-match "\n\\'" value
) (substring value
0 -
1) value
)))))
1964 ;;;; Horizontal Rule
1966 (defun org-element-horizontal-rule-parser (limit affiliated
)
1967 "Parse an horizontal rule.
1969 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1970 the buffer position at the beginning of the first affiliated
1971 keyword and CDR is a plist of affiliated keywords along with
1974 Return a list whose CAR is `horizontal-rule' and CDR is a plist
1975 containing `:begin', `:end', `:post-blank' and `:post-affiliated'
1978 (let ((begin (car affiliated
))
1979 (post-affiliated (point))
1980 (post-hr (progn (forward-line) (point)))
1981 (end (progn (skip-chars-forward " \r\t\n" limit
)
1982 (if (eobp) (point) (line-beginning-position)))))
1983 (list 'horizontal-rule
1987 :post-blank
(count-lines post-hr end
)
1988 :post-affiliated post-affiliated
)
1989 (cdr affiliated
))))))
1991 (defun org-element-horizontal-rule-interpreter (horizontal-rule contents
)
1992 "Interpret HORIZONTAL-RULE element as Org syntax.
1999 (defun org-element-keyword-parser (limit affiliated
)
2000 "Parse a keyword at point.
2002 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2003 the buffer position at the beginning of the first affiliated
2004 keyword and CDR is a plist of affiliated keywords along with
2007 Return a list whose CAR is `keyword' and CDR is a plist
2008 containing `:key', `:value', `:begin', `:end', `:post-blank' and
2009 `:post-affiliated' keywords."
2011 (let ((begin (car affiliated
))
2012 (post-affiliated (point))
2013 (key (progn (looking-at "[ \t]*#\\+\\(\\S-+*\\):")
2014 (upcase (org-match-string-no-properties 1))))
2015 (value (org-trim (buffer-substring-no-properties
2016 (match-end 0) (point-at-eol))))
2017 (pos-before-blank (progn (forward-line) (point)))
2018 (end (progn (skip-chars-forward " \r\t\n" limit
)
2019 (if (eobp) (point) (line-beginning-position)))))
2026 :post-blank
(count-lines pos-before-blank end
)
2027 :post-affiliated post-affiliated
)
2028 (cdr affiliated
))))))
2030 (defun org-element-keyword-interpreter (keyword contents
)
2031 "Interpret KEYWORD element as Org syntax.
2034 (org-element-property :key keyword
)
2035 (org-element-property :value keyword
)))
2038 ;;;; Latex Environment
2040 (defun org-element-latex-environment-parser (limit affiliated
)
2041 "Parse a LaTeX environment.
2043 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2044 the buffer position at the beginning of the first affiliated
2045 keyword and CDR is a plist of affiliated keywords along with
2048 Return a list whose CAR is `latex-environment' and CDR is a plist
2049 containing `:begin', `:end', `:value', `:post-blank' and
2050 `:post-affiliated' keywords.
2052 Assume point is at the beginning of the latex environment."
2054 (let ((case-fold-search t
)
2055 (code-begin (point)))
2056 (looking-at "[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}")
2057 (if (not (re-search-forward (format "^[ \t]*\\\\end{%s}[ \t]*$"
2058 (regexp-quote (match-string 1)))
2060 ;; Incomplete latex environment: parse it as a paragraph.
2061 (org-element-paragraph-parser limit affiliated
)
2062 (let* ((code-end (progn (forward-line) (point)))
2063 (begin (car affiliated
))
2064 (value (buffer-substring-no-properties code-begin code-end
))
2065 (end (progn (skip-chars-forward " \r\t\n" limit
)
2066 (if (eobp) (point) (line-beginning-position)))))
2067 (list 'latex-environment
2072 :post-blank
(count-lines code-end end
)
2073 :post-affiliated code-begin
)
2074 (cdr affiliated
))))))))
2076 (defun org-element-latex-environment-interpreter (latex-environment contents
)
2077 "Interpret LATEX-ENVIRONMENT element as Org syntax.
2079 (org-element-property :value latex-environment
))
2084 (defun org-element-node-property-parser (limit)
2085 "Parse a node-property at point.
2087 LIMIT bounds the search.
2089 Return a list whose CAR is `node-property' and CDR is a plist
2090 containing `:key', `:value', `:begin', `:end' and `:post-blank'
2093 (looking-at org-property-re
)
2094 (let ((case-fold-search t
)
2096 (key (org-match-string-no-properties 2))
2097 (value (org-match-string-no-properties 3))
2098 (pos-before-blank (progn (forward-line) (point)))
2099 (end (progn (skip-chars-forward " \r\t\n" limit
)
2100 (if (eobp) (point) (point-at-bol)))))
2101 (list 'node-property
2106 :post-blank
(count-lines pos-before-blank end
))))))
2108 (defun org-element-node-property-interpreter (node-property contents
)
2109 "Interpret NODE-PROPERTY element as Org syntax.
2111 (format org-property-format
2112 (format ":%s:" (org-element-property :key node-property
))
2113 (org-element-property :value node-property
)))
2118 (defun org-element-paragraph-parser (limit affiliated
)
2121 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2122 the buffer position at the beginning of the first affiliated
2123 keyword and CDR is a plist of affiliated keywords along with
2126 Return a list whose CAR is `paragraph' and CDR is a plist
2127 containing `:begin', `:end', `:contents-begin' and
2128 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
2130 Assume point is at the beginning of the paragraph."
2132 (let* ((begin (car affiliated
))
2133 (contents-begin (point))
2135 (let ((case-fold-search t
))
2137 (if (not (re-search-forward
2138 org-element-paragraph-separate limit
'm
))
2140 ;; A matching `org-element-paragraph-separate' is not
2141 ;; necessarily the end of the paragraph. In
2142 ;; particular, lines starting with # or : as a first
2143 ;; non-space character are ambiguous. We have check
2144 ;; if they are valid Org syntax (i.e. not an
2145 ;; incomplete keyword).
2149 ;; There's no ambiguity for other symbols or
2150 ;; empty lines: stop here.
2151 (looking-at "[ \t]*\\(?:[^:#]\\|$\\)")
2152 ;; Stop at valid fixed-width areas.
2153 (looking-at "[ \t]*:\\(?: \\|$\\)")
2155 (and (looking-at org-drawer-regexp
)
2158 "^[ \t]*:END:[ \t]*$" limit t
)))
2159 ;; Stop at valid comments.
2160 (looking-at "[ \t]*#\\(?: \\|$\\)")
2161 ;; Stop at valid dynamic blocks.
2162 (and (looking-at org-dblock-start-re
)
2165 "^[ \t]*#\\+END:?[ \t]*$" limit t
)))
2166 ;; Stop at valid blocks.
2167 (and (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
2170 (format "^[ \t]*#\\+END_%s[ \t]*$"
2172 (org-match-string-no-properties 1)))
2174 ;; Stop at valid latex environments.
2176 "[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}")
2179 (format "^[ \t]*\\\\end{%s}[ \t]*$"
2181 (org-match-string-no-properties 1)))
2183 ;; Stop at valid keywords.
2184 (looking-at "[ \t]*#\\+\\S-+:")
2185 ;; Skip everything else.
2189 (re-search-forward org-element-paragraph-separate
2191 (beginning-of-line)))
2192 (if (= (point) limit
) limit
2193 (goto-char (line-beginning-position)))))
2194 (contents-end (progn (skip-chars-backward " \r\t\n" contents-begin
)
2197 (end (progn (skip-chars-forward " \r\t\n" limit
)
2198 (if (eobp) (point) (line-beginning-position)))))
2203 :contents-begin contents-begin
2204 :contents-end contents-end
2205 :post-blank
(count-lines before-blank end
)
2206 :post-affiliated contents-begin
)
2207 (cdr affiliated
))))))
2209 (defun org-element-paragraph-interpreter (paragraph contents
)
2210 "Interpret PARAGRAPH element as Org syntax.
2211 CONTENTS is the contents of the element."
2217 (defun org-element-planning-parser (limit)
2220 LIMIT bounds the search.
2222 Return a list whose CAR is `planning' and CDR is a plist
2223 containing `:closed', `:deadline', `:scheduled', `:begin', `:end'
2224 and `:post-blank' keywords."
2226 (let* ((case-fold-search nil
)
2228 (post-blank (let ((before-blank (progn (forward-line) (point))))
2229 (skip-chars-forward " \r\t\n" limit
)
2230 (skip-chars-backward " \t")
2231 (unless (bolp) (end-of-line))
2232 (count-lines before-blank
(point))))
2234 closed deadline scheduled
)
2236 (while (re-search-forward org-keyword-time-not-clock-regexp end t
)
2237 (goto-char (match-end 1))
2238 (skip-chars-forward " \t" end
)
2239 (let ((keyword (match-string 1))
2240 (time (org-element-timestamp-parser)))
2241 (cond ((equal keyword org-closed-string
) (setq closed time
))
2242 ((equal keyword org-deadline-string
) (setq deadline time
))
2243 (t (setq scheduled time
)))))
2245 (list :closed closed
2247 :scheduled scheduled
2250 :post-blank post-blank
)))))
2252 (defun org-element-planning-interpreter (planning contents
)
2253 "Interpret PLANNING element as Org syntax.
2258 (list (let ((deadline (org-element-property :deadline planning
)))
2260 (concat org-deadline-string
" "
2261 (org-element-timestamp-interpreter deadline nil
))))
2262 (let ((scheduled (org-element-property :scheduled planning
)))
2264 (concat org-scheduled-string
" "
2265 (org-element-timestamp-interpreter scheduled nil
))))
2266 (let ((closed (org-element-property :closed planning
)))
2268 (concat org-closed-string
" "
2269 (org-element-timestamp-interpreter closed nil
))))))
2275 (defun org-element-quote-section-parser (limit)
2276 "Parse a quote section.
2278 LIMIT bounds the search.
2280 Return a list whose CAR is `quote-section' and CDR is a plist
2281 containing `:begin', `:end', `:value' and `:post-blank' keywords.
2283 Assume point is at beginning of the section."
2285 (let* ((begin (point))
2286 (end (progn (org-with-limited-levels (outline-next-heading))
2288 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
2291 (value (buffer-substring-no-properties begin pos-before-blank
)))
2292 (list 'quote-section
2296 :post-blank
(count-lines pos-before-blank end
))))))
2298 (defun org-element-quote-section-interpreter (quote-section contents
)
2299 "Interpret QUOTE-SECTION element as Org syntax.
2301 (org-element-property :value quote-section
))
2306 (defun org-element-src-block-parser (limit affiliated
)
2309 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2310 the buffer position at the beginning of the first affiliated
2311 keyword and CDR is a plist of affiliated keywords along with
2314 Return a list whose CAR is `src-block' and CDR is a plist
2315 containing `:language', `:switches', `:parameters', `:begin',
2316 `:end', `:number-lines', `:retain-labels', `:use-labels',
2317 `:label-fmt', `:preserve-indent', `:value', `:post-blank' and
2318 `:post-affiliated' keywords.
2320 Assume point is at the beginning of the block."
2321 (let ((case-fold-search t
))
2322 (if (not (save-excursion (re-search-forward "^[ \t]*#\\+END_SRC[ \t]*$"
2324 ;; Incomplete block: parse it as a paragraph.
2325 (org-element-paragraph-parser limit affiliated
)
2326 (let ((contents-end (match-beginning 0)))
2328 (let* ((begin (car affiliated
))
2329 (post-affiliated (point))
2330 ;; Get language as a string.
2334 (concat "^[ \t]*#\\+BEGIN_SRC"
2335 "\\(?: +\\(\\S-+\\)\\)?"
2336 "\\(\\(?: +\\(?:-l \".*?\"\\|[-+][A-Za-z]\\)\\)+\\)?"
2338 (org-match-string-no-properties 1)))
2340 (switches (org-match-string-no-properties 2))
2342 (parameters (org-match-string-no-properties 3))
2343 ;; Switches analysis
2345 (cond ((not switches
) nil
)
2346 ((string-match "-n\\>" switches
) 'new
)
2347 ((string-match "+n\\>" switches
) 'continued
)))
2348 (preserve-indent (and switches
2349 (string-match "-i\\>" switches
)))
2352 (string-match "-l +\"\\([^\"\n]+\\)\"" switches
)
2353 (match-string 1 switches
)))
2354 ;; Should labels be retained in (or stripped from)
2358 (not (string-match "-r\\>" switches
))
2359 (and number-lines
(string-match "-k\\>" switches
))))
2360 ;; What should code-references use - labels or
2365 (not (string-match "-k\\>" switches
)))))
2367 (block-ind (progn (skip-chars-forward " \t") (current-column)))
2369 (value (org-element-remove-indentation
2370 (org-unescape-code-in-string
2371 (buffer-substring-no-properties
2372 (progn (forward-line) (point)) contents-end
))
2374 (pos-before-blank (progn (goto-char contents-end
)
2377 ;; Get position after ending blank lines.
2378 (end (progn (skip-chars-forward " \r\t\n" limit
)
2379 (if (eobp) (point) (line-beginning-position)))))
2382 (list :language language
2383 :switches
(and (org-string-nw-p switches
)
2384 (org-trim switches
))
2385 :parameters
(and (org-string-nw-p parameters
)
2386 (org-trim parameters
))
2389 :number-lines number-lines
2390 :preserve-indent preserve-indent
2391 :retain-labels retain-labels
2392 :use-labels use-labels
2393 :label-fmt label-fmt
2395 :post-blank
(count-lines pos-before-blank end
)
2396 :post-affiliated post-affiliated
)
2397 (cdr affiliated
)))))))))
2399 (defun org-element-src-block-interpreter (src-block contents
)
2400 "Interpret SRC-BLOCK element as Org syntax.
2402 (let ((lang (org-element-property :language src-block
))
2403 (switches (org-element-property :switches src-block
))
2404 (params (org-element-property :parameters src-block
))
2406 (let ((val (org-element-property :value src-block
)))
2408 ((or org-src-preserve-indentation
2409 (org-element-property :preserve-indent src-block
))
2411 ((zerop org-edit-src-content-indentation
) val
)
2413 (let ((ind (make-string org-edit-src-content-indentation ?\s
)))
2414 (replace-regexp-in-string
2415 "\\(^\\)[ \t]*\\S-" ind val nil nil
1)))))))
2416 (concat (format "#+BEGIN_SRC%s\n"
2417 (concat (and lang
(concat " " lang
))
2418 (and switches
(concat " " switches
))
2419 (and params
(concat " " params
))))
2420 (org-escape-code-in-string value
)
2426 (defun org-element-table-parser (limit affiliated
)
2427 "Parse a table at point.
2429 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2430 the buffer position at the beginning of the first affiliated
2431 keyword and CDR is a plist of affiliated keywords along with
2434 Return a list whose CAR is `table' and CDR is a plist containing
2435 `:begin', `:end', `:tblfm', `:type', `:contents-begin',
2436 `:contents-end', `:value', `:post-blank' and `:post-affiliated'
2439 Assume point is at the beginning of the table."
2441 (let* ((case-fold-search t
)
2442 (table-begin (point))
2443 (type (if (org-at-table.el-p
) 'table.el
'org
))
2444 (begin (car affiliated
))
2446 (if (re-search-forward org-table-any-border-regexp limit
'm
)
2447 (goto-char (match-beginning 0))
2450 (while (looking-at "[ \t]*#\\+TBLFM: +\\(.*\\)[ \t]*$")
2451 (push (org-match-string-no-properties 1) acc
)
2454 (pos-before-blank (point))
2455 (end (progn (skip-chars-forward " \r\t\n" limit
)
2456 (if (eobp) (point) (line-beginning-position)))))
2463 ;; Only `org' tables have contents. `table.el' tables
2464 ;; use a `:value' property to store raw table as
2466 :contents-begin
(and (eq type
'org
) table-begin
)
2467 :contents-end
(and (eq type
'org
) table-end
)
2468 :value
(and (eq type
'table.el
)
2469 (buffer-substring-no-properties
2470 table-begin table-end
))
2471 :post-blank
(count-lines pos-before-blank end
)
2472 :post-affiliated table-begin
)
2473 (cdr affiliated
))))))
2475 (defun org-element-table-interpreter (table contents
)
2476 "Interpret TABLE element as Org syntax.
2478 (if (eq (org-element-property :type table
) 'table.el
)
2479 (org-remove-indentation (org-element-property :value table
))
2480 (concat (with-temp-buffer (insert contents
)
2483 (mapconcat (lambda (fm) (concat "#+TBLFM: " fm
))
2484 (reverse (org-element-property :tblfm table
))
2490 (defun org-element-table-row-parser (limit)
2491 "Parse table row at point.
2493 LIMIT bounds the search.
2495 Return a list whose CAR is `table-row' and CDR is a plist
2496 containing `:begin', `:end', `:contents-begin', `:contents-end',
2497 `:type' and `:post-blank' keywords."
2499 (let* ((type (if (looking-at "^[ \t]*|-") 'rule
'standard
))
2501 ;; A table rule has no contents. In that case, ensure
2502 ;; CONTENTS-BEGIN matches CONTENTS-END.
2503 (contents-begin (and (eq type
'standard
)
2504 (search-forward "|")
2506 (contents-end (and (eq type
'standard
)
2509 (skip-chars-backward " \t")
2511 (end (progn (forward-line) (point))))
2516 :contents-begin contents-begin
2517 :contents-end contents-end
2520 (defun org-element-table-row-interpreter (table-row contents
)
2521 "Interpret TABLE-ROW element as Org syntax.
2522 CONTENTS is the contents of the table row."
2523 (if (eq (org-element-property :type table-row
) 'rule
) "|-"
2524 (concat "| " contents
)))
2529 (defun org-element-verse-block-parser (limit affiliated
)
2530 "Parse a verse block.
2532 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2533 the buffer position at the beginning of the first affiliated
2534 keyword and CDR is a plist of affiliated keywords along with
2537 Return a list whose CAR is `verse-block' and CDR is a plist
2538 containing `:begin', `:end', `:contents-begin', `:contents-end',
2539 `:post-blank' and `:post-affiliated' keywords.
2541 Assume point is at beginning of the block."
2542 (let ((case-fold-search t
))
2543 (if (not (save-excursion
2544 (re-search-forward "^[ \t]*#\\+END_VERSE[ \t]*$" limit t
)))
2545 ;; Incomplete block: parse it as a paragraph.
2546 (org-element-paragraph-parser limit affiliated
)
2547 (let ((contents-end (match-beginning 0)))
2549 (let* ((begin (car affiliated
))
2550 (post-affiliated (point))
2551 (contents-begin (progn (forward-line) (point)))
2552 (pos-before-blank (progn (goto-char contents-end
)
2555 (end (progn (skip-chars-forward " \r\t\n" limit
)
2556 (if (eobp) (point) (line-beginning-position)))))
2561 :contents-begin contents-begin
2562 :contents-end contents-end
2563 :post-blank
(count-lines pos-before-blank end
)
2564 :post-affiliated post-affiliated
)
2565 (cdr affiliated
)))))))))
2567 (defun org-element-verse-block-interpreter (verse-block contents
)
2568 "Interpret VERSE-BLOCK element as Org syntax.
2569 CONTENTS is verse block contents."
2570 (format "#+BEGIN_VERSE\n%s#+END_VERSE" contents
))
2576 ;; Unlike to elements, interstices can be found between objects.
2577 ;; That's why, along with the parser, successor functions are provided
2578 ;; for each object. Some objects share the same successor (i.e. `code'
2579 ;; and `verbatim' objects).
2581 ;; A successor must accept a single argument bounding the search. It
2582 ;; will return either a cons cell whose CAR is the object's type, as
2583 ;; a symbol, and CDR the position of its next occurrence, or nil.
2585 ;; Successors follow the naming convention:
2586 ;; org-element-NAME-successor, where NAME is the name of the
2587 ;; successor, as defined in `org-element-all-successors'.
2589 ;; Some object types (i.e. `italic') are recursive. Restrictions on
2590 ;; object types they can contain will be specified in
2591 ;; `org-element-object-restrictions'.
2593 ;; Adding a new type of object is simple. Implement a successor,
2594 ;; a parser, and an interpreter for it, all following the naming
2595 ;; convention. Register type in `org-element-all-objects' and
2596 ;; successor in `org-element-all-successors'. Maybe tweak
2597 ;; restrictions about it, and that's it.
2602 (defun org-element-bold-parser ()
2603 "Parse bold object at point.
2605 Return a list whose CAR is `bold' and CDR is a plist with
2606 `:begin', `:end', `:contents-begin' and `:contents-end' and
2607 `:post-blank' keywords.
2609 Assume point is at the first star marker."
2611 (unless (bolp) (backward-char 1))
2612 (looking-at org-emph-re
)
2613 (let ((begin (match-beginning 2))
2614 (contents-begin (match-beginning 4))
2615 (contents-end (match-end 4))
2616 (post-blank (progn (goto-char (match-end 2))
2617 (skip-chars-forward " \t")))
2622 :contents-begin contents-begin
2623 :contents-end contents-end
2624 :post-blank post-blank
)))))
2626 (defun org-element-bold-interpreter (bold contents
)
2627 "Interpret BOLD object as Org syntax.
2628 CONTENTS is the contents of the object."
2629 (format "*%s*" contents
))
2631 (defun org-element-text-markup-successor ()
2632 "Search for the next text-markup object.
2634 Return value is a cons cell whose CAR is a symbol among `bold',
2635 `italic', `underline', `strike-through', `code' and `verbatim'
2636 and CDR is beginning position."
2638 (unless (bolp) (backward-char))
2639 (when (re-search-forward org-emph-re nil t
)
2640 (let ((marker (match-string 3)))
2642 ((equal marker
"*") 'bold
)
2643 ((equal marker
"/") 'italic
)
2644 ((equal marker
"_") 'underline
)
2645 ((equal marker
"+") 'strike-through
)
2646 ((equal marker
"~") 'code
)
2647 ((equal marker
"=") 'verbatim
)
2648 (t (error "Unknown marker at %d" (match-beginning 3))))
2649 (match-beginning 2))))))
2654 (defun org-element-code-parser ()
2655 "Parse code object at point.
2657 Return a list whose CAR is `code' and CDR is a plist with
2658 `:value', `:begin', `:end' and `:post-blank' keywords.
2660 Assume point is at the first tilde marker."
2662 (unless (bolp) (backward-char 1))
2663 (looking-at org-emph-re
)
2664 (let ((begin (match-beginning 2))
2665 (value (org-match-string-no-properties 4))
2666 (post-blank (progn (goto-char (match-end 2))
2667 (skip-chars-forward " \t")))
2673 :post-blank post-blank
)))))
2675 (defun org-element-code-interpreter (code contents
)
2676 "Interpret CODE object as Org syntax.
2678 (format "~%s~" (org-element-property :value code
)))
2683 (defun org-element-entity-parser ()
2684 "Parse entity at point.
2686 Return a list whose CAR is `entity' and CDR a plist with
2687 `:begin', `:end', `:latex', `:latex-math-p', `:html', `:latin1',
2688 `:utf-8', `:ascii', `:use-brackets-p' and `:post-blank' as
2691 Assume point is at the beginning of the entity."
2693 (looking-at "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)")
2694 (let* ((value (org-entity-get (match-string 1)))
2695 (begin (match-beginning 0))
2696 (bracketsp (string= (match-string 2) "{}"))
2697 (post-blank (progn (goto-char (match-end 1))
2698 (when bracketsp
(forward-char 2))
2699 (skip-chars-forward " \t")))
2702 (list :name
(car value
)
2703 :latex
(nth 1 value
)
2704 :latex-math-p
(nth 2 value
)
2706 :ascii
(nth 4 value
)
2707 :latin1
(nth 5 value
)
2708 :utf-8
(nth 6 value
)
2711 :use-brackets-p bracketsp
2712 :post-blank post-blank
)))))
2714 (defun org-element-entity-interpreter (entity contents
)
2715 "Interpret ENTITY object as Org syntax.
2718 (org-element-property :name entity
)
2719 (when (org-element-property :use-brackets-p entity
) "{}")))
2721 (defun org-element-latex-or-entity-successor ()
2722 "Search for the next latex-fragment or entity object.
2724 Return value is a cons cell whose CAR is `entity' or
2725 `latex-fragment' and CDR is beginning position."
2727 (unless (bolp) (backward-char))
2728 (let ((matchers (cdr org-latex-regexps
))
2729 ;; ENTITY-RE matches both LaTeX commands and Org entities.
2731 "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))
2732 (when (re-search-forward
2733 (concat (mapconcat #'cadr matchers
"\\|") "\\|" entity-re
) nil t
)
2734 (goto-char (match-beginning 0))
2735 (if (looking-at entity-re
)
2736 ;; Determine if it's a real entity or a LaTeX command.
2737 (cons (if (org-entity-get (match-string 1)) 'entity
'latex-fragment
)
2738 (match-beginning 0))
2739 ;; No entity nor command: point is at a LaTeX fragment.
2740 ;; Determine its type to get the correct beginning position.
2741 (cons 'latex-fragment
2743 (dolist (e matchers
)
2744 (when (looking-at (nth 1 e
))
2745 (throw 'return
(match-beginning (nth 2 e
)))))
2751 (defun org-element-export-snippet-parser ()
2752 "Parse export snippet at point.
2754 Return a list whose CAR is `export-snippet' and CDR a plist with
2755 `:begin', `:end', `:back-end', `:value' and `:post-blank' as
2758 Assume point is at the beginning of the snippet."
2760 (re-search-forward "@@\\([-A-Za-z0-9]+\\):" nil t
)
2761 (let* ((begin (match-beginning 0))
2762 (back-end (org-match-string-no-properties 1))
2763 (value (buffer-substring-no-properties
2765 (progn (re-search-forward "@@" nil t
) (match-beginning 0))))
2766 (post-blank (skip-chars-forward " \t"))
2768 (list 'export-snippet
2769 (list :back-end back-end
2773 :post-blank post-blank
)))))
2775 (defun org-element-export-snippet-interpreter (export-snippet contents
)
2776 "Interpret EXPORT-SNIPPET object as Org syntax.
2779 (org-element-property :back-end export-snippet
)
2780 (org-element-property :value export-snippet
)))
2782 (defun org-element-export-snippet-successor ()
2783 "Search for the next export-snippet object.
2785 Return value is a cons cell whose CAR is `export-snippet' and CDR
2786 its beginning position."
2789 (when (and (re-search-forward "@@[-A-Za-z0-9]+:" nil t
)
2790 (setq beg
(match-beginning 0))
2791 (search-forward "@@" nil t
))
2792 (cons 'export-snippet beg
)))))
2795 ;;;; Footnote Reference
2797 (defun org-element-footnote-reference-parser ()
2798 "Parse footnote reference at point.
2800 Return a list whose CAR is `footnote-reference' and CDR a plist
2801 with `:label', `:type', `:inline-definition', `:begin', `:end'
2802 and `:post-blank' as keywords."
2804 (looking-at org-footnote-re
)
2805 (let* ((begin (point))
2806 (label (or (org-match-string-no-properties 2)
2807 (org-match-string-no-properties 3)
2808 (and (match-string 1)
2809 (concat "fn:" (org-match-string-no-properties 1)))))
2810 (type (if (or (not label
) (match-string 1)) 'inline
'standard
))
2811 (inner-begin (match-end 0))
2815 (while (and (> count
0) (re-search-forward "[][]" nil t
))
2816 (if (equal (match-string 0) "[") (incf count
) (decf count
)))
2818 (post-blank (progn (goto-char (1+ inner-end
))
2819 (skip-chars-forward " \t")))
2822 (list 'footnote-reference
2827 :post-blank post-blank
))))
2828 (org-element-put-property
2829 footnote-reference
:inline-definition
2830 (and (eq type
'inline
)
2831 (org-element-parse-secondary-string
2832 (buffer-substring inner-begin inner-end
)
2833 (org-element-restriction 'footnote-reference
)
2834 footnote-reference
))))))
2836 (defun org-element-footnote-reference-interpreter (footnote-reference contents
)
2837 "Interpret FOOTNOTE-REFERENCE object as Org syntax.
2839 (let ((label (or (org-element-property :label footnote-reference
) "fn:"))
2842 (org-element-property :inline-definition footnote-reference
)))
2843 (if (not inline-def
) ""
2844 (concat ":" (org-element-interpret-data inline-def
))))))
2845 (format "[%s]" (concat label def
))))
2847 (defun org-element-footnote-reference-successor ()
2848 "Search for the next footnote-reference object.
2850 Return value is a cons cell whose CAR is `footnote-reference' and
2851 CDR is beginning position."
2854 (while (re-search-forward org-footnote-re nil t
)
2856 (let ((beg (match-beginning 0))
2859 (while (re-search-forward "[][]" nil t
)
2860 (if (equal (match-string 0) "[") (incf count
) (decf count
))
2862 (throw 'exit
(cons 'footnote-reference beg
))))))))))
2865 ;;;; Inline Babel Call
2867 (defun org-element-inline-babel-call-parser ()
2868 "Parse inline babel call at point.
2870 Return a list whose CAR is `inline-babel-call' and CDR a plist
2871 with `:begin', `:end', `:value' and `:post-blank' as keywords.
2873 Assume point is at the beginning of the babel call."
2875 (unless (bolp) (backward-char))
2876 (let ((case-fold-search t
))
2877 (looking-at org-babel-inline-lob-one-liner-regexp
))
2878 (let ((begin (match-end 1))
2879 (value (buffer-substring-no-properties (match-end 1) (match-end 0)))
2880 (post-blank (progn (goto-char (match-end 0))
2881 (skip-chars-forward " \t")))
2883 (list 'inline-babel-call
2887 :post-blank post-blank
)))))
2889 (defun org-element-inline-babel-call-interpreter (inline-babel-call contents
)
2890 "Interpret INLINE-BABEL-CALL object as Org syntax.
2892 (org-element-property :value inline-babel-call
))
2894 (defun org-element-inline-babel-call-successor ()
2895 "Search for the next inline-babel-call object.
2897 Return value is a cons cell whose CAR is `inline-babel-call' and
2898 CDR is beginning position."
2900 ;; Use a simplified version of
2901 ;; `org-babel-inline-lob-one-liner-regexp'.
2902 (when (re-search-forward
2903 "call_\\([^()\n]+?\\)\\(?:\\[.*?\\]\\)?([^\n]*?)\\(\\[.*?\\]\\)?"
2905 (cons 'inline-babel-call
(match-beginning 0)))))
2908 ;;;; Inline Src Block
2910 (defun org-element-inline-src-block-parser ()
2911 "Parse inline source block at point.
2913 Return a list whose CAR is `inline-src-block' and CDR a plist
2914 with `:begin', `:end', `:language', `:value', `:parameters' and
2915 `:post-blank' as keywords.
2917 Assume point is at the beginning of the inline src block."
2919 (unless (bolp) (backward-char))
2920 (looking-at org-babel-inline-src-block-regexp
)
2921 (let ((begin (match-beginning 1))
2922 (language (org-match-string-no-properties 2))
2923 (parameters (org-match-string-no-properties 4))
2924 (value (org-match-string-no-properties 5))
2925 (post-blank (progn (goto-char (match-end 0))
2926 (skip-chars-forward " \t")))
2928 (list 'inline-src-block
2929 (list :language language
2931 :parameters parameters
2934 :post-blank post-blank
)))))
2936 (defun org-element-inline-src-block-interpreter (inline-src-block contents
)
2937 "Interpret INLINE-SRC-BLOCK object as Org syntax.
2939 (let ((language (org-element-property :language inline-src-block
))
2940 (arguments (org-element-property :parameters inline-src-block
))
2941 (body (org-element-property :value inline-src-block
)))
2942 (format "src_%s%s{%s}"
2944 (if arguments
(format "[%s]" arguments
) "")
2947 (defun org-element-inline-src-block-successor ()
2948 "Search for the next inline-babel-call element.
2950 Return value is a cons cell whose CAR is `inline-babel-call' and
2951 CDR is beginning position."
2953 (unless (bolp) (backward-char))
2954 (when (re-search-forward org-babel-inline-src-block-regexp nil t
)
2955 (cons 'inline-src-block
(match-beginning 1)))))
2959 (defun org-element-italic-parser ()
2960 "Parse italic object at point.
2962 Return a list whose CAR is `italic' and CDR is a plist with
2963 `:begin', `:end', `:contents-begin' and `:contents-end' and
2964 `:post-blank' keywords.
2966 Assume point is at the first slash marker."
2968 (unless (bolp) (backward-char 1))
2969 (looking-at org-emph-re
)
2970 (let ((begin (match-beginning 2))
2971 (contents-begin (match-beginning 4))
2972 (contents-end (match-end 4))
2973 (post-blank (progn (goto-char (match-end 2))
2974 (skip-chars-forward " \t")))
2979 :contents-begin contents-begin
2980 :contents-end contents-end
2981 :post-blank post-blank
)))))
2983 (defun org-element-italic-interpreter (italic contents
)
2984 "Interpret ITALIC object as Org syntax.
2985 CONTENTS is the contents of the object."
2986 (format "/%s/" contents
))
2991 (defun org-element-latex-fragment-parser ()
2992 "Parse LaTeX fragment at point.
2994 Return a list whose CAR is `latex-fragment' and CDR a plist with
2995 `:value', `:begin', `:end', and `:post-blank' as keywords.
2997 Assume point is at the beginning of the LaTeX fragment."
2999 (let* ((begin (point))
3002 (dolist (e (cdr org-latex-regexps
))
3003 (let ((latex-regexp (nth 1 e
)))
3004 (when (or (looking-at latex-regexp
)
3008 (looking-at latex-regexp
))))
3009 (throw 'exit
(nth 2 e
)))))
3010 ;; None found: it's a macro.
3011 (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")
3013 (value (org-match-string-no-properties substring-match
))
3014 (post-blank (progn (goto-char (match-end substring-match
))
3015 (skip-chars-forward " \t")))
3017 (list 'latex-fragment
3021 :post-blank post-blank
)))))
3023 (defun org-element-latex-fragment-interpreter (latex-fragment contents
)
3024 "Interpret LATEX-FRAGMENT object as Org syntax.
3026 (org-element-property :value latex-fragment
))
3030 (defun org-element-line-break-parser ()
3031 "Parse line break at point.
3033 Return a list whose CAR is `line-break', and CDR a plist with
3034 `:begin', `:end' and `:post-blank' keywords.
3036 Assume point is at the beginning of the line break."
3038 (list :begin
(point)
3039 :end
(progn (forward-line) (point))
3042 (defun org-element-line-break-interpreter (line-break contents
)
3043 "Interpret LINE-BREAK object as Org syntax.
3047 (defun org-element-line-break-successor ()
3048 "Search for the next line-break object.
3050 Return value is a cons cell whose CAR is `line-break' and CDR is
3051 beginning position."
3053 (let ((beg (and (re-search-forward "[^\\\\]\\(\\\\\\\\\\)[ \t]*$" nil t
)
3054 (goto-char (match-beginning 1)))))
3055 ;; A line break can only happen on a non-empty line.
3056 (when (and beg
(re-search-backward "\\S-" (point-at-bol) t
))
3057 (cons 'line-break beg
)))))
3062 (defun org-element-link-parser ()
3063 "Parse link at point.
3065 Return a list whose CAR is `link' and CDR a plist with `:type',
3066 `:path', `:raw-link', `:application', `:search-option', `:begin',
3067 `:end', `:contents-begin', `:contents-end' and `:post-blank' as
3070 Assume point is at the beginning of the link."
3072 (let ((begin (point))
3073 end contents-begin contents-end link-end post-blank path type
3074 raw-link link search-option application
)
3076 ;; Type 1: Text targeted from a radio target.
3077 ((and org-target-link-regexp
(looking-at org-target-link-regexp
))
3079 link-end
(match-end 0)
3080 path
(org-match-string-no-properties 0)))
3081 ;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]]
3082 ((looking-at org-bracket-link-regexp
)
3083 (setq contents-begin
(match-beginning 3)
3084 contents-end
(match-end 3)
3085 link-end
(match-end 0)
3086 ;; RAW-LINK is the original link. Expand any
3087 ;; abbreviation in it.
3088 raw-link
(org-translate-link
3089 (org-link-expand-abbrev
3090 (org-match-string-no-properties 1))))
3091 ;; Determine TYPE of link and set PATH accordingly.
3094 ((or (file-name-absolute-p raw-link
)
3095 (string-match "^\\.\\.?/" raw-link
))
3096 (setq type
"file" path raw-link
))
3097 ;; Explicit type (http, irc, bbdb...). See `org-link-types'.
3098 ((string-match org-link-re-with-space3 raw-link
)
3099 (setq type
(match-string 1 raw-link
) path
(match-string 2 raw-link
)))
3100 ;; Id type: PATH is the id.
3101 ((string-match "^id:\\([-a-f0-9]+\\)" raw-link
)
3102 (setq type
"id" path
(match-string 1 raw-link
)))
3103 ;; Code-ref type: PATH is the name of the reference.
3104 ((string-match "^(\\(.*\\))$" raw-link
)
3105 (setq type
"coderef" path
(match-string 1 raw-link
)))
3106 ;; Custom-id type: PATH is the name of the custom id.
3107 ((= (aref raw-link
0) ?
#)
3108 (setq type
"custom-id" path
(substring raw-link
1)))
3109 ;; Fuzzy type: Internal link either matches a target, an
3110 ;; headline name or nothing. PATH is the target or
3112 (t (setq type
"fuzzy" path raw-link
))))
3113 ;; Type 3: Plain link, i.e. http://orgmode.org
3114 ((looking-at org-plain-link-re
)
3115 (setq raw-link
(org-match-string-no-properties 0)
3116 type
(org-match-string-no-properties 1)
3117 link-end
(match-end 0)
3118 path
(org-match-string-no-properties 2)))
3119 ;; Type 4: Angular link, i.e. <http://orgmode.org>
3120 ((looking-at org-angle-link-re
)
3121 (setq raw-link
(buffer-substring-no-properties
3122 (match-beginning 1) (match-end 2))
3123 type
(org-match-string-no-properties 1)
3124 link-end
(match-end 0)
3125 path
(org-match-string-no-properties 2))))
3126 ;; In any case, deduce end point after trailing white space from
3127 ;; LINK-END variable.
3128 (setq post-blank
(progn (goto-char link-end
) (skip-chars-forward " \t"))
3130 ;; Extract search option and opening application out of
3131 ;; "file"-type links.
3132 (when (member type org-element-link-type-is-file
)
3134 (cond ((string-match "^file\\+\\(.*\\)$" type
)
3135 (setq application
(match-string 1 type
)))
3136 ((not (string-match "^file" type
))
3137 (setq application type
)))
3138 ;; Extract search option from PATH.
3139 (when (string-match "::\\(.*\\)$" path
)
3140 (setq search-option
(match-string 1 path
)
3141 path
(replace-match "" nil nil path
)))
3142 ;; Make sure TYPE always reports "file".
3147 :raw-link
(or raw-link path
)
3148 :application application
3149 :search-option search-option
3152 :contents-begin contents-begin
3153 :contents-end contents-end
3154 :post-blank post-blank
)))))
3156 (defun org-element-link-interpreter (link contents
)
3157 "Interpret LINK object as Org syntax.
3158 CONTENTS is the contents of the object, or nil."
3159 (let ((type (org-element-property :type link
))
3160 (raw-link (org-element-property :raw-link link
)))
3161 (if (string= type
"radio") raw-link
3164 (if contents
(format "[%s]" contents
) "")))))
3166 (defun org-element-link-successor ()
3167 "Search for the next link object.
3169 Return value is a cons cell whose CAR is `link' and CDR is
3170 beginning position."
3173 (if (not org-target-link-regexp
) org-any-link-re
3174 (concat org-any-link-re
"\\|" org-target-link-regexp
))))
3175 (when (re-search-forward link-regexp nil t
)
3176 (cons 'link
(match-beginning 0))))))
3178 (defun org-element-plain-link-successor ()
3179 "Search for the next plain link object.
3181 Return value is a cons cell whose CAR is `link' and CDR is
3182 beginning position."
3183 (and (save-excursion (re-search-forward org-plain-link-re nil t
))
3184 (cons 'link
(match-beginning 0))))
3189 (defun org-element-macro-parser ()
3190 "Parse macro at point.
3192 Return a list whose CAR is `macro' and CDR a plist with `:key',
3193 `:args', `:begin', `:end', `:value' and `:post-blank' as
3196 Assume point is at the macro."
3198 (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
3199 (let ((begin (point))
3200 (key (downcase (org-match-string-no-properties 1)))
3201 (value (org-match-string-no-properties 0))
3202 (post-blank (progn (goto-char (match-end 0))
3203 (skip-chars-forward " \t")))
3205 (args (let ((args (org-match-string-no-properties 3)))
3207 ;; Do not use `org-split-string' since empty
3208 ;; strings are meaningful here.
3210 (replace-regexp-in-string
3211 "\\(\\\\*\\)\\(,\\)"
3213 (let ((len (length (match-string 1 str
))))
3214 (concat (make-string (/ len
2) ?
\\)
3215 (if (zerop (mod len
2)) "\000" ","))))
3224 :post-blank post-blank
)))))
3226 (defun org-element-macro-interpreter (macro contents
)
3227 "Interpret MACRO object as Org syntax.
3229 (org-element-property :value macro
))
3231 (defun org-element-macro-successor ()
3232 "Search for the next macro object.
3234 Return value is cons cell whose CAR is `macro' and CDR is
3235 beginning position."
3237 (when (re-search-forward
3238 "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
3240 (cons 'macro
(match-beginning 0)))))
3245 (defun org-element-radio-target-parser ()
3246 "Parse radio target at point.
3248 Return a list whose CAR is `radio-target' and CDR a plist with
3249 `:begin', `:end', `:contents-begin', `:contents-end', `:value'
3250 and `:post-blank' as keywords.
3252 Assume point is at the radio target."
3254 (looking-at org-radio-target-regexp
)
3255 (let ((begin (point))
3256 (contents-begin (match-beginning 1))
3257 (contents-end (match-end 1))
3258 (value (org-match-string-no-properties 1))
3259 (post-blank (progn (goto-char (match-end 0))
3260 (skip-chars-forward " \t")))
3265 :contents-begin contents-begin
3266 :contents-end contents-end
3267 :post-blank post-blank
3270 (defun org-element-radio-target-interpreter (target contents
)
3271 "Interpret TARGET object as Org syntax.
3272 CONTENTS is the contents of the object."
3273 (concat "<<<" contents
">>>"))
3275 (defun org-element-radio-target-successor ()
3276 "Search for the next radio-target object.
3278 Return value is a cons cell whose CAR is `radio-target' and CDR
3279 is beginning position."
3281 (when (re-search-forward org-radio-target-regexp nil t
)
3282 (cons 'radio-target
(match-beginning 0)))))
3285 ;;;; Statistics Cookie
3287 (defun org-element-statistics-cookie-parser ()
3288 "Parse statistics cookie at point.
3290 Return a list whose CAR is `statistics-cookie', and CDR a plist
3291 with `:begin', `:end', `:value' and `:post-blank' keywords.
3293 Assume point is at the beginning of the statistics-cookie."
3295 (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
3296 (let* ((begin (point))
3297 (value (buffer-substring-no-properties
3298 (match-beginning 0) (match-end 0)))
3299 (post-blank (progn (goto-char (match-end 0))
3300 (skip-chars-forward " \t")))
3302 (list 'statistics-cookie
3306 :post-blank post-blank
)))))
3308 (defun org-element-statistics-cookie-interpreter (statistics-cookie contents
)
3309 "Interpret STATISTICS-COOKIE object as Org syntax.
3311 (org-element-property :value statistics-cookie
))
3313 (defun org-element-statistics-cookie-successor ()
3314 "Search for the next statistics cookie object.
3316 Return value is a cons cell whose CAR is `statistics-cookie' and
3317 CDR is beginning position."
3319 (when (re-search-forward "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]" nil t
)
3320 (cons 'statistics-cookie
(match-beginning 0)))))
3325 (defun org-element-strike-through-parser ()
3326 "Parse strike-through object at point.
3328 Return a list whose CAR is `strike-through' and CDR is a plist
3329 with `:begin', `:end', `:contents-begin' and `:contents-end' and
3330 `:post-blank' keywords.
3332 Assume point is at the first plus sign marker."
3334 (unless (bolp) (backward-char 1))
3335 (looking-at org-emph-re
)
3336 (let ((begin (match-beginning 2))
3337 (contents-begin (match-beginning 4))
3338 (contents-end (match-end 4))
3339 (post-blank (progn (goto-char (match-end 2))
3340 (skip-chars-forward " \t")))
3342 (list 'strike-through
3345 :contents-begin contents-begin
3346 :contents-end contents-end
3347 :post-blank post-blank
)))))
3349 (defun org-element-strike-through-interpreter (strike-through contents
)
3350 "Interpret STRIKE-THROUGH object as Org syntax.
3351 CONTENTS is the contents of the object."
3352 (format "+%s+" contents
))
3357 (defun org-element-subscript-parser ()
3358 "Parse subscript at point.
3360 Return a list whose CAR is `subscript' and CDR a plist with
3361 `:begin', `:end', `:contents-begin', `:contents-end',
3362 `:use-brackets-p' and `:post-blank' as keywords.
3364 Assume point is at the underscore."
3366 (unless (bolp) (backward-char))
3367 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp
)
3369 (not (looking-at org-match-substring-regexp
))))
3370 (begin (match-beginning 2))
3371 (contents-begin (or (match-beginning 5)
3372 (match-beginning 3)))
3373 (contents-end (or (match-end 5) (match-end 3)))
3374 (post-blank (progn (goto-char (match-end 0))
3375 (skip-chars-forward " \t")))
3380 :use-brackets-p bracketsp
3381 :contents-begin contents-begin
3382 :contents-end contents-end
3383 :post-blank post-blank
)))))
3385 (defun org-element-subscript-interpreter (subscript contents
)
3386 "Interpret SUBSCRIPT object as Org syntax.
3387 CONTENTS is the contents of the object."
3389 (if (org-element-property :use-brackets-p subscript
) "_{%s}" "_%s")
3392 (defun org-element-sub/superscript-successor
()
3393 "Search for the next sub/superscript object.
3395 Return value is a cons cell whose CAR is either `subscript' or
3396 `superscript' and CDR is beginning position."
3398 (unless (bolp) (backward-char))
3399 (when (re-search-forward org-match-substring-regexp nil t
)
3400 (cons (if (string= (match-string 2) "_") 'subscript
'superscript
)
3401 (match-beginning 2)))))
3406 (defun org-element-superscript-parser ()
3407 "Parse superscript at point.
3409 Return a list whose CAR is `superscript' and CDR a plist with
3410 `:begin', `:end', `:contents-begin', `:contents-end',
3411 `:use-brackets-p' and `:post-blank' as keywords.
3413 Assume point is at the caret."
3415 (unless (bolp) (backward-char))
3416 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp
) t
3417 (not (looking-at org-match-substring-regexp
))))
3418 (begin (match-beginning 2))
3419 (contents-begin (or (match-beginning 5)
3420 (match-beginning 3)))
3421 (contents-end (or (match-end 5) (match-end 3)))
3422 (post-blank (progn (goto-char (match-end 0))
3423 (skip-chars-forward " \t")))
3428 :use-brackets-p bracketsp
3429 :contents-begin contents-begin
3430 :contents-end contents-end
3431 :post-blank post-blank
)))))
3433 (defun org-element-superscript-interpreter (superscript contents
)
3434 "Interpret SUPERSCRIPT object as Org syntax.
3435 CONTENTS is the contents of the object."
3437 (if (org-element-property :use-brackets-p superscript
) "^{%s}" "^%s")
3443 (defun org-element-table-cell-parser ()
3444 "Parse table cell at point.
3446 Return a list whose CAR is `table-cell' and CDR is a plist
3447 containing `:begin', `:end', `:contents-begin', `:contents-end'
3448 and `:post-blank' keywords."
3449 (looking-at "[ \t]*\\(.*?\\)[ \t]*|")
3450 (let* ((begin (match-beginning 0))
3452 (contents-begin (match-beginning 1))
3453 (contents-end (match-end 1)))
3457 :contents-begin contents-begin
3458 :contents-end contents-end
3461 (defun org-element-table-cell-interpreter (table-cell contents
)
3462 "Interpret TABLE-CELL element as Org syntax.
3463 CONTENTS is the contents of the cell, or nil."
3464 (concat " " contents
" |"))
3466 (defun org-element-table-cell-successor ()
3467 "Search for the next table-cell object.
3469 Return value is a cons cell whose CAR is `table-cell' and CDR is
3470 beginning position."
3471 (when (looking-at "[ \t]*.*?[ \t]*|") (cons 'table-cell
(point))))
3476 (defun org-element-target-parser ()
3477 "Parse target at point.
3479 Return a list whose CAR is `target' and CDR a plist with
3480 `:begin', `:end', `:value' and `:post-blank' as keywords.
3482 Assume point is at the target."
3484 (looking-at org-target-regexp
)
3485 (let ((begin (point))
3486 (value (org-match-string-no-properties 1))
3487 (post-blank (progn (goto-char (match-end 0))
3488 (skip-chars-forward " \t")))
3494 :post-blank post-blank
)))))
3496 (defun org-element-target-interpreter (target contents
)
3497 "Interpret TARGET object as Org syntax.
3499 (format "<<%s>>" (org-element-property :value target
)))
3501 (defun org-element-target-successor ()
3502 "Search for the next target object.
3504 Return value is a cons cell whose CAR is `target' and CDR is
3505 beginning position."
3507 (when (re-search-forward org-target-regexp nil t
)
3508 (cons 'target
(match-beginning 0)))))
3513 (defun org-element-timestamp-parser ()
3514 "Parse time stamp at point.
3516 Return a list whose CAR is `timestamp', and CDR a plist with
3517 `:type', `:raw-value', `:year-start', `:month-start',
3518 `:day-start', `:hour-start', `:minute-start', `:year-end',
3519 `:month-end', `:day-end', `:hour-end', `:minute-end',
3520 `:repeater-type', `:repeater-value', `:repeater-unit',
3521 `:warning-type', `:warning-value', `:warning-unit', `:begin',
3522 `:end', `:value' and `:post-blank' keywords.
3524 Assume point is at the beginning of the timestamp."
3526 (let* ((begin (point))
3527 (activep (eq (char-after) ?
<))
3530 (looking-at "\\([<[]\\(%%\\)?.*?\\)[]>]\\(?:--\\([<[].*?[]>]\\)\\)?")
3531 (match-string-no-properties 0)))
3532 (date-start (match-string-no-properties 1))
3533 (date-end (match-string 3))
3534 (diaryp (match-beginning 2))
3535 (post-blank (progn (goto-char (match-end 0))
3536 (skip-chars-forward " \t")))
3541 "[012]?[0-9]:[0-5][0-9]\\(-\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)"
3543 (cons (string-to-number (match-string 2 date-start
))
3544 (string-to-number (match-string 3 date-start
)))))
3545 (type (cond (diaryp 'diary
)
3546 ((and activep
(or date-end time-range
)) 'active-range
)
3548 ((or date-end time-range
) 'inactive-range
)
3552 (string-match "\\([.+]?\\+\\)\\([0-9]+\\)\\([hdwmy]\\)"
3556 (let ((type (match-string 1 raw-value
)))
3557 (cond ((equal "++" type
) 'catch-up
)
3558 ((equal ".+" type
) 'restart
)
3560 :repeater-value
(string-to-number (match-string 2 raw-value
))
3562 (case (string-to-char (match-string 3 raw-value
))
3563 (?h
'hour
) (?d
'day
) (?w
'week
) (?m
'month
) (t 'year
)))))
3566 (string-match "\\(-\\)?-\\([0-9]+\\)\\([hdwmy]\\)" raw-value
)
3568 :warning-type
(if (match-string 1 raw-value
) 'first
'all
)
3569 :warning-value
(string-to-number (match-string 2 raw-value
))
3571 (case (string-to-char (match-string 3 raw-value
))
3572 (?h
'hour
) (?d
'day
) (?w
'week
) (?m
'month
) (t 'year
)))))
3573 year-start month-start day-start hour-start minute-start year-end
3574 month-end day-end hour-end minute-end
)
3575 ;; Parse date-start.
3577 (let ((date (org-parse-time-string date-start t
)))
3578 (setq year-start
(nth 5 date
)
3579 month-start
(nth 4 date
)
3580 day-start
(nth 3 date
)
3581 hour-start
(nth 2 date
)
3582 minute-start
(nth 1 date
))))
3583 ;; Compute date-end. It can be provided directly in time-stamp,
3584 ;; or extracted from time range. Otherwise, it defaults to the
3585 ;; same values as date-start.
3587 (let ((date (and date-end
(org-parse-time-string date-end t
))))
3588 (setq year-end
(or (nth 5 date
) year-start
)
3589 month-end
(or (nth 4 date
) month-start
)
3590 day-end
(or (nth 3 date
) day-start
)
3591 hour-end
(or (nth 2 date
) (car time-range
) hour-start
)
3592 minute-end
(or (nth 1 date
) (cdr time-range
) minute-start
))))
3594 (nconc (list :type type
3595 :raw-value raw-value
3596 :year-start year-start
3597 :month-start month-start
3598 :day-start day-start
3599 :hour-start hour-start
3600 :minute-start minute-start
3602 :month-end month-end
3605 :minute-end minute-end
3608 :post-blank post-blank
)
3612 (defun org-element-timestamp-interpreter (timestamp contents
)
3613 "Interpret TIMESTAMP object as Org syntax.
3615 (let* ((repeat-string
3617 (case (org-element-property :repeater-type timestamp
)
3618 (cumulate "+") (catch-up "++") (restart ".+"))
3619 (let ((val (org-element-property :repeater-value timestamp
)))
3620 (and val
(number-to-string val
)))
3621 (case (org-element-property :repeater-unit timestamp
)
3622 (hour "h") (day "d") (week "w") (month "m") (year "y"))))
3625 (case (org-element-property :warning-type timestamp
)
3628 (let ((val (org-element-property :warning-value timestamp
)))
3629 (and val
(number-to-string val
)))
3630 (case (org-element-property :warning-unit timestamp
)
3631 (hour "h") (day "d") (week "w") (month "m") (year "y"))))
3633 ;; Build an Org timestamp string from TIME. ACTIVEP is
3634 ;; non-nil when time stamp is active. If WITH-TIME-P is
3635 ;; non-nil, add a time part. HOUR-END and MINUTE-END
3636 ;; specify a time range in the timestamp. REPEAT-STRING is
3637 ;; the repeater string, if any.
3638 (lambda (time activep
&optional with-time-p hour-end minute-end
)
3639 (let ((ts (format-time-string
3640 (funcall (if with-time-p
'cdr
'car
)
3641 org-time-stamp-formats
)
3643 (when (and hour-end minute-end
)
3644 (string-match "[012]?[0-9]:[0-5][0-9]" ts
)
3647 (format "\\&-%02d:%02d" hour-end minute-end
)
3649 (unless activep
(setq ts
(format "[%s]" (substring ts
1 -
1))))
3650 (dolist (s (list repeat-string warning-string
))
3651 (when (org-string-nw-p s
)
3652 (setq ts
(concat (substring ts
0 -
1)
3655 (substring ts -
1)))))
3658 (type (org-element-property :type timestamp
)))
3661 (let* ((minute-start (org-element-property :minute-start timestamp
))
3662 (minute-end (org-element-property :minute-end timestamp
))
3663 (hour-start (org-element-property :hour-start timestamp
))
3664 (hour-end (org-element-property :hour-end timestamp
))
3665 (time-range-p (and hour-start hour-end minute-start minute-end
3666 (or (/= hour-start hour-end
)
3667 (/= minute-start minute-end
)))))
3673 (org-element-property :day-start timestamp
)
3674 (org-element-property :month-start timestamp
)
3675 (org-element-property :year-start timestamp
))
3677 (and hour-start minute-start
)
3678 (and time-range-p hour-end
)
3679 (and time-range-p minute-end
))))
3680 ((active-range inactive-range
)
3681 (let ((minute-start (org-element-property :minute-start timestamp
))
3682 (minute-end (org-element-property :minute-end timestamp
))
3683 (hour-start (org-element-property :hour-start timestamp
))
3684 (hour-end (org-element-property :hour-end timestamp
)))
3687 build-ts-string
(encode-time
3691 (org-element-property :day-start timestamp
)
3692 (org-element-property :month-start timestamp
)
3693 (org-element-property :year-start timestamp
))
3694 (eq type
'active-range
)
3695 (and hour-start minute-start
))
3697 (funcall build-ts-string
3701 (org-element-property :day-end timestamp
)
3702 (org-element-property :month-end timestamp
)
3703 (org-element-property :year-end timestamp
))
3704 (eq type
'active-range
)
3705 (and hour-end minute-end
))))))))
3707 (defun org-element-timestamp-successor ()
3708 "Search for the next timestamp object.
3710 Return value is a cons cell whose CAR is `timestamp' and CDR is
3711 beginning position."
3713 (when (re-search-forward
3714 (concat org-ts-regexp-both
3716 "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
3718 "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
3720 (cons 'timestamp
(match-beginning 0)))))
3725 (defun org-element-underline-parser ()
3726 "Parse underline object at point.
3728 Return a list whose CAR is `underline' and CDR is a plist with
3729 `:begin', `:end', `:contents-begin' and `:contents-end' and
3730 `:post-blank' keywords.
3732 Assume point is at the first underscore marker."
3734 (unless (bolp) (backward-char 1))
3735 (looking-at org-emph-re
)
3736 (let ((begin (match-beginning 2))
3737 (contents-begin (match-beginning 4))
3738 (contents-end (match-end 4))
3739 (post-blank (progn (goto-char (match-end 2))
3740 (skip-chars-forward " \t")))
3745 :contents-begin contents-begin
3746 :contents-end contents-end
3747 :post-blank post-blank
)))))
3749 (defun org-element-underline-interpreter (underline contents
)
3750 "Interpret UNDERLINE object as Org syntax.
3751 CONTENTS is the contents of the object."
3752 (format "_%s_" contents
))
3757 (defun org-element-verbatim-parser ()
3758 "Parse verbatim object at point.
3760 Return a list whose CAR is `verbatim' and CDR is a plist with
3761 `:value', `:begin', `:end' and `:post-blank' keywords.
3763 Assume point is at the first equal sign marker."
3765 (unless (bolp) (backward-char 1))
3766 (looking-at org-emph-re
)
3767 (let ((begin (match-beginning 2))
3768 (value (org-match-string-no-properties 4))
3769 (post-blank (progn (goto-char (match-end 2))
3770 (skip-chars-forward " \t")))
3776 :post-blank post-blank
)))))
3778 (defun org-element-verbatim-interpreter (verbatim contents
)
3779 "Interpret VERBATIM object as Org syntax.
3781 (format "=%s=" (org-element-property :value verbatim
)))
3785 ;;; Parsing Element Starting At Point
3787 ;; `org-element--current-element' is the core function of this section.
3788 ;; It returns the Lisp representation of the element starting at
3791 ;; `org-element--current-element' makes use of special modes. They
3792 ;; are activated for fixed element chaining (i.e. `plain-list' >
3793 ;; `item') or fixed conditional element chaining (i.e. `headline' >
3794 ;; `section'). Special modes are: `first-section', `item',
3795 ;; `node-property', `quote-section', `section' and `table-row'.
3797 (defun org-element--current-element
3798 (limit &optional granularity special structure
)
3799 "Parse the element starting at point.
3801 Return value is a list like (TYPE PROPS) where TYPE is the type
3802 of the element and PROPS a plist of properties associated to the
3805 Possible types are defined in `org-element-all-elements'.
3807 LIMIT bounds the search.
3809 Optional argument GRANULARITY determines the depth of the
3810 recursion. Allowed values are `headline', `greater-element',
3811 `element', `object' or nil. When it is broader than `object' (or
3812 nil), secondary values will not be parsed, since they only
3815 Optional argument SPECIAL, when non-nil, can be either
3816 `first-section', `item', `node-property', `quote-section',
3817 `section', and `table-row'.
3819 If STRUCTURE isn't provided but SPECIAL is set to `item', it will
3822 This function assumes point is always at the beginning of the
3823 element it has to parse."
3825 (let ((case-fold-search t
)
3826 ;; Determine if parsing depth allows for secondary strings
3827 ;; parsing. It only applies to elements referenced in
3828 ;; `org-element-secondary-value-alist'.
3829 (raw-secondary-p (and granularity
(not (eq granularity
'object
)))))
3833 (org-element-item-parser limit structure raw-secondary-p
))
3835 ((eq special
'table-row
) (org-element-table-row-parser limit
))
3837 ((eq special
'node-property
) (org-element-node-property-parser limit
))
3839 ((org-with-limited-levels (org-at-heading-p))
3840 (org-element-headline-parser limit raw-secondary-p
))
3841 ;; Sections (must be checked after headline).
3842 ((eq special
'section
) (org-element-section-parser limit
))
3843 ((eq special
'quote-section
) (org-element-quote-section-parser limit
))
3844 ((eq special
'first-section
)
3845 (org-element-section-parser
3846 (or (save-excursion (org-with-limited-levels (outline-next-heading)))
3848 ;; When not at bol, point is at the beginning of an item or
3849 ;; a footnote definition: next item is always a paragraph.
3850 ((not (bolp)) (org-element-paragraph-parser limit
(list (point))))
3851 ;; Planning and Clock.
3852 ((looking-at org-planning-or-clock-line-re
)
3853 (if (equal (match-string 1) org-clock-string
)
3854 (org-element-clock-parser limit
)
3855 (org-element-planning-parser limit
)))
3858 (org-element-inlinetask-parser limit raw-secondary-p
))
3859 ;; From there, elements can have affiliated keywords.
3860 (t (let ((affiliated (org-element--collect-affiliated-keywords limit
)))
3862 ;; Jumping over affiliated keywords put point off-limits.
3863 ;; Parse them as regular keywords.
3864 ((and (cdr affiliated
) (>= (point) limit
))
3865 (goto-char (car affiliated
))
3866 (org-element-keyword-parser limit nil
))
3867 ;; LaTeX Environment.
3869 "[ \t]*\\\\begin{[A-Za-z0-9*]+}\\(\\[.*?\\]\\|{.*?}\\)*[ \t]*$")
3870 (org-element-latex-environment-parser limit affiliated
))
3871 ;; Drawer and Property Drawer.
3872 ((looking-at org-drawer-regexp
)
3873 (if (equal (match-string 1) "PROPERTIES")
3874 (org-element-property-drawer-parser limit affiliated
)
3875 (org-element-drawer-parser limit affiliated
)))
3877 ((looking-at "[ \t]*:\\( \\|$\\)")
3878 (org-element-fixed-width-parser limit affiliated
))
3879 ;; Inline Comments, Blocks, Babel Calls, Dynamic Blocks and
3881 ((looking-at "[ \t]*#")
3882 (goto-char (match-end 0))
3883 (cond ((looking-at "\\(?: \\|$\\)")
3885 (org-element-comment-parser limit affiliated
))
3886 ((looking-at "\\+BEGIN_\\(\\S-+\\)")
3888 (let ((parser (assoc (upcase (match-string 1))
3889 org-element-block-name-alist
)))
3890 (if parser
(funcall (cdr parser
) limit affiliated
)
3891 (org-element-special-block-parser limit affiliated
))))
3892 ((looking-at "\\+CALL:")
3894 (org-element-babel-call-parser limit affiliated
))
3895 ((looking-at "\\+BEGIN:? ")
3897 (org-element-dynamic-block-parser limit affiliated
))
3898 ((looking-at "\\+\\S-+:")
3900 (org-element-keyword-parser limit affiliated
))
3903 (org-element-paragraph-parser limit affiliated
))))
3904 ;; Footnote Definition.
3905 ((looking-at org-footnote-definition-re
)
3906 (org-element-footnote-definition-parser limit affiliated
))
3908 ((looking-at "[ \t]*-\\{5,\\}[ \t]*$")
3909 (org-element-horizontal-rule-parser limit affiliated
))
3912 (org-element-diary-sexp-parser limit affiliated
))
3914 ((org-at-table-p t
) (org-element-table-parser limit affiliated
))
3916 ((looking-at (org-item-re))
3917 (org-element-plain-list-parser
3919 (or structure
(org-element--list-struct limit
))))
3920 ;; Default element: Paragraph.
3921 (t (org-element-paragraph-parser limit affiliated
)))))))))
3924 ;; Most elements can have affiliated keywords. When looking for an
3925 ;; element beginning, we want to move before them, as they belong to
3926 ;; that element, and, in the meantime, collect information they give
3927 ;; into appropriate properties. Hence the following function.
3929 (defun org-element--collect-affiliated-keywords (limit)
3930 "Collect affiliated keywords from point down to LIMIT.
3932 Return a list whose CAR is the position at the first of them and
3933 CDR a plist of keywords and values and move point to the
3934 beginning of the first line after them.
3936 As a special case, if element doesn't start at the beginning of
3937 the line (i.e. a paragraph starting an item), CAR is current
3938 position of point and CDR is nil."
3939 (if (not (bolp)) (list (point))
3940 (let ((case-fold-search t
)
3942 ;; RESTRICT is the list of objects allowed in parsed
3944 (restrict (org-element-restriction 'keyword
))
3946 (while (and (< (point) limit
) (looking-at org-element--affiliated-re
))
3947 (let* ((raw-kwd (upcase (match-string 1)))
3948 ;; Apply translation to RAW-KWD. From there, KWD is
3949 ;; the official keyword.
3950 (kwd (or (cdr (assoc raw-kwd
3951 org-element-keyword-translation-alist
))
3953 ;; Find main value for any keyword.
3957 (buffer-substring-no-properties
3958 (match-end 0) (point-at-eol)))))
3959 ;; PARSEDP is non-nil when keyword should have its
3961 (parsedp (member kwd org-element-parsed-keywords
))
3962 ;; If KWD is a dual keyword, find its secondary
3963 ;; value. Maybe parse it.
3964 (dualp (member kwd org-element-dual-keywords
))
3967 (let ((sec (org-match-string-no-properties 2)))
3968 (if (or (not sec
) (not parsedp
)) sec
3969 (org-element-parse-secondary-string sec restrict
)))))
3970 ;; Attribute a property name to KWD.
3971 (kwd-sym (and kwd
(intern (concat ":" (downcase kwd
))))))
3972 ;; Now set final shape for VALUE.
3974 (setq value
(org-element-parse-secondary-string value restrict
)))
3976 (setq value
(and (or value dual-value
) (cons value dual-value
))))
3977 (when (or (member kwd org-element-multiple-keywords
)
3978 ;; Attributes can always appear on multiple lines.
3979 (string-match "^ATTR_" kwd
))
3980 (setq value
(cons value
(plist-get output kwd-sym
))))
3981 ;; Eventually store the new value in OUTPUT.
3982 (setq output
(plist-put output kwd-sym value
))
3983 ;; Move to next keyword.
3985 ;; If affiliated keywords are orphaned: move back to first one.
3986 ;; They will be parsed as a paragraph.
3987 (when (looking-at "[ \t]*$") (goto-char origin
) (setq output nil
))
3989 (cons origin output
))))
3995 ;; The two major functions here are `org-element-parse-buffer', which
3996 ;; parses Org syntax inside the current buffer, taking into account
3997 ;; region, narrowing, or even visibility if specified, and
3998 ;; `org-element-parse-secondary-string', which parses objects within
4001 ;; The (almost) almighty `org-element-map' allows to apply a function
4002 ;; on elements or objects matching some type, and accumulate the
4003 ;; resulting values. In an export situation, it also skips unneeded
4004 ;; parts of the parse tree.
4006 (defun org-element-parse-buffer (&optional granularity visible-only
)
4007 "Recursively parse the buffer and return structure.
4008 If narrowing is in effect, only parse the visible part of the
4011 Optional argument GRANULARITY determines the depth of the
4012 recursion. It can be set to the following symbols:
4014 `headline' Only parse headlines.
4015 `greater-element' Don't recurse into greater elements excepted
4016 headlines and sections. Thus, elements
4017 parsed are the top-level ones.
4018 `element' Parse everything but objects and plain text.
4019 `object' Parse the complete buffer (default).
4021 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
4024 An element or an objects is represented as a list with the
4025 pattern (TYPE PROPERTIES CONTENTS), where :
4027 TYPE is a symbol describing the element or object. See
4028 `org-element-all-elements' and `org-element-all-objects' for an
4029 exhaustive list of such symbols. One can retrieve it with
4030 `org-element-type' function.
4032 PROPERTIES is the list of attributes attached to the element or
4033 object, as a plist. Although most of them are specific to the
4034 element or object type, all types share `:begin', `:end',
4035 `:post-blank' and `:parent' properties, which respectively
4036 refer to buffer position where the element or object starts,
4037 ends, the number of white spaces or blank lines after it, and
4038 the element or object containing it. Properties values can be
4039 obtained by using `org-element-property' function.
4041 CONTENTS is a list of elements, objects or raw strings
4042 contained in the current element or object, when applicable.
4043 One can access them with `org-element-contents' function.
4045 The Org buffer has `org-data' as type and nil as properties.
4046 `org-element-map' function can be used to find specific elements
4047 or objects within the parse tree.
4049 This function assumes that current major mode is `org-mode'."
4051 (goto-char (point-min))
4052 (org-skip-whitespace)
4053 (org-element--parse-elements
4054 (point-at-bol) (point-max)
4055 ;; Start in `first-section' mode so text before the first
4056 ;; headline belongs to a section.
4057 'first-section nil granularity visible-only
(list 'org-data nil
))))
4059 (defun org-element-parse-secondary-string (string restriction
&optional parent
)
4060 "Recursively parse objects in STRING and return structure.
4062 RESTRICTION is a symbol limiting the object types that will be
4065 Optional argument PARENT, when non-nil, is the element or object
4066 containing the secondary string. It is used to set correctly
4067 `:parent' property within the string."
4068 ;; Copy buffer-local variables listed in
4069 ;; `org-element-object-variables' into temporary buffer. This is
4070 ;; required since object parsing is dependent on these variables.
4071 (let ((pairs (delq nil
(mapcar (lambda (var)
4073 (cons var
(symbol-value var
))))
4074 org-element-object-variables
))))
4076 (mapc (lambda (pair) (org-set-local (car pair
) (cdr pair
))) pairs
)
4078 (let ((secondary (org-element--parse-objects
4079 (point-min) (point-max) nil restriction
)))
4081 (mapc (lambda (obj) (org-element-put-property obj
:parent parent
))
4085 (defun org-element-map
4086 (data types fun
&optional info first-match no-recursion with-affiliated
)
4087 "Map a function on selected elements or objects.
4089 DATA is a parse tree, an element, an object, a string, or a list
4090 of such constructs. TYPES is a symbol or list of symbols of
4091 elements or objects types (see `org-element-all-elements' and
4092 `org-element-all-objects' for a complete list of types). FUN is
4093 the function called on the matching element or object. It has to
4094 accept one argument: the element or object itself.
4096 When optional argument INFO is non-nil, it should be a plist
4097 holding export options. In that case, parts of the parse tree
4098 not exportable according to that property list will be skipped.
4100 When optional argument FIRST-MATCH is non-nil, stop at the first
4101 match for which FUN doesn't return nil, and return that value.
4103 Optional argument NO-RECURSION is a symbol or a list of symbols
4104 representing elements or objects types. `org-element-map' won't
4105 enter any recursive element or object whose type belongs to that
4106 list. Though, FUN can still be applied on them.
4108 When optional argument WITH-AFFILIATED is non-nil, FUN will also
4109 apply to matching objects within parsed affiliated keywords (see
4110 `org-element-parsed-keywords').
4112 Nil values returned from FUN do not appear in the results.
4118 Assuming TREE is a variable containing an Org buffer parse tree,
4119 the following example will return a flat list of all `src-block'
4120 and `example-block' elements in it:
4122 \(org-element-map tree '(example-block src-block) 'identity)
4124 The following snippet will find the first headline with a level
4125 of 1 and a \"phone\" tag, and will return its beginning position:
4127 \(org-element-map tree 'headline
4129 \(and (= (org-element-property :level hl) 1)
4130 \(member \"phone\" (org-element-property :tags hl))
4131 \(org-element-property :begin hl)))
4134 The next example will return a flat list of all `plain-list' type
4135 elements in TREE that are not a sub-list themselves:
4137 \(org-element-map tree 'plain-list 'identity nil nil 'plain-list)
4139 Eventually, this example will return a flat list of all `bold'
4140 type objects containing a `latex-snippet' type object, even
4141 looking into captions:
4143 \(org-element-map tree 'bold
4145 \(and (org-element-map b 'latex-snippet 'identity nil t) b))
4147 ;; Ensure TYPES and NO-RECURSION are a list, even of one element.
4148 (unless (listp types
) (setq types
(list types
)))
4149 (unless (listp no-recursion
) (setq no-recursion
(list no-recursion
)))
4150 ;; Recursion depth is determined by --CATEGORY.
4153 (let ((category 'greater-elements
))
4154 (mapc (lambda (type)
4155 (cond ((or (memq type org-element-all-objects
)
4156 (eq type
'plain-text
))
4157 ;; If one object is found, the function
4158 ;; has to recurse into every object.
4159 (throw 'found
'objects
))
4160 ((not (memq type org-element-greater-elements
))
4161 ;; If one regular element is found, the
4162 ;; function has to recurse, at least,
4163 ;; into every element it encounters.
4164 (and (not (eq category
'elements
))
4165 (setq category
'elements
)))))
4168 ;; Compute properties for affiliated keywords if necessary.
4170 (and with-affiliated
4171 (mapcar (lambda (kwd)
4172 (cons kwd
(intern (concat ":" (downcase kwd
)))))
4173 org-element-affiliated-keywords
)))
4179 ;; Recursively walk DATA. INFO, if non-nil, is a plist
4180 ;; holding contextual information.
4181 (let ((--type (org-element-type --data
)))
4184 ;; Ignored element in an export context.
4185 ((and info
(memq --data
(plist-get info
:ignore-list
))))
4186 ;; List of elements or objects.
4187 ((not --type
) (mapc --walk-tree --data
))
4188 ;; Unconditionally enter parse trees.
4189 ((eq --type
'org-data
)
4190 (mapc --walk-tree
(org-element-contents --data
)))
4192 ;; Check if TYPE is matching among TYPES. If so,
4193 ;; apply FUN to --DATA and accumulate return value
4194 ;; into --ACC (or exit if FIRST-MATCH is non-nil).
4195 (when (memq --type types
)
4196 (let ((result (funcall fun --data
)))
4197 (cond ((not result
))
4198 (first-match (throw '--map-first-match result
))
4199 (t (push result --acc
)))))
4200 ;; If --DATA has a secondary string that can contain
4201 ;; objects with their type among TYPES, look into it.
4202 (when (and (eq --category
'objects
) (not (stringp --data
)))
4204 (assq --type org-element-secondary-value-alist
)))
4206 (funcall --walk-tree
4207 (org-element-property (cdr sec-prop
) --data
)))))
4208 ;; If --DATA has any affiliated keywords and
4209 ;; WITH-AFFILIATED is non-nil, look for objects in
4211 (when (and with-affiliated
4212 (eq --category
'objects
)
4213 (memq --type org-element-all-elements
))
4214 (mapc (lambda (kwd-pair)
4215 (let ((kwd (car kwd-pair
))
4216 (value (org-element-property
4217 (cdr kwd-pair
) --data
)))
4218 ;; Pay attention to the type of value.
4219 ;; Preserve order for multiple keywords.
4222 ((and (member kwd org-element-multiple-keywords
)
4223 (member kwd org-element-dual-keywords
))
4224 (mapc (lambda (line)
4225 (funcall --walk-tree
(cdr line
))
4226 (funcall --walk-tree
(car line
)))
4228 ((member kwd org-element-multiple-keywords
)
4229 (mapc (lambda (line) (funcall --walk-tree line
))
4231 ((member kwd org-element-dual-keywords
)
4232 (funcall --walk-tree
(cdr value
))
4233 (funcall --walk-tree
(car value
)))
4234 (t (funcall --walk-tree value
)))))
4235 --affiliated-alist
))
4236 ;; Determine if a recursion into --DATA is possible.
4238 ;; --TYPE is explicitly removed from recursion.
4239 ((memq --type no-recursion
))
4240 ;; --DATA has no contents.
4241 ((not (org-element-contents --data
)))
4242 ;; Looking for greater elements but --DATA is simply
4243 ;; an element or an object.
4244 ((and (eq --category
'greater-elements
)
4245 (not (memq --type org-element-greater-elements
))))
4246 ;; Looking for elements but --DATA is an object.
4247 ((and (eq --category
'elements
)
4248 (memq --type org-element-all-objects
)))
4249 ;; In any other case, map contents.
4250 (t (mapc --walk-tree
(org-element-contents --data
)))))))))))
4251 (catch '--map-first-match
4252 (funcall --walk-tree data
)
4253 ;; Return value in a proper order.
4255 (put 'org-element-map
'lisp-indent-function
2)
4257 ;; The following functions are internal parts of the parser.
4259 ;; The first one, `org-element--parse-elements' acts at the element's
4262 ;; The second one, `org-element--parse-objects' applies on all objects
4263 ;; of a paragraph or a secondary string. It uses
4264 ;; `org-element--get-next-object-candidates' to optimize the search of
4265 ;; the next object in the buffer.
4267 ;; More precisely, that function looks for every allowed object type
4268 ;; first. Then, it discards failed searches, keeps further matches,
4269 ;; and searches again types matched behind point, for subsequent
4270 ;; calls. Thus, searching for a given type fails only once, and every
4271 ;; object is searched only once at top level (but sometimes more for
4274 (defun org-element--parse-elements
4275 (beg end special structure granularity visible-only acc
)
4276 "Parse elements between BEG and END positions.
4278 SPECIAL prioritize some elements over the others. It can be set
4279 to `first-section', `quote-section', `section' `item' or
4282 When value is `item', STRUCTURE will be used as the current list
4285 GRANULARITY determines the depth of the recursion. See
4286 `org-element-parse-buffer' for more information.
4288 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
4291 Elements are accumulated into ACC."
4294 ;; Visible only: skip invisible parts at the beginning of the
4296 (when (and visible-only
(org-invisible-p2))
4297 (goto-char (min (1+ (org-find-visible)) end
)))
4298 ;; When parsing only headlines, skip any text before first one.
4299 (when (and (eq granularity
'headline
) (not (org-at-heading-p)))
4300 (org-with-limited-levels (outline-next-heading)))
4302 (while (< (point) end
)
4303 ;; Find current element's type and parse it accordingly to
4305 (let* ((element (org-element--current-element
4306 end granularity special structure
))
4307 (type (org-element-type element
))
4308 (cbeg (org-element-property :contents-begin element
)))
4309 (goto-char (org-element-property :end element
))
4310 ;; Visible only: skip invisible parts between siblings.
4311 (when (and visible-only
(org-invisible-p2))
4312 (goto-char (min (1+ (org-find-visible)) end
)))
4313 ;; Fill ELEMENT contents by side-effect.
4315 ;; If element has no contents, don't modify it.
4317 ;; Greater element: parse it between `contents-begin' and
4318 ;; `contents-end'. Make sure GRANULARITY allows the
4319 ;; recursion, or ELEMENT is a headline, in which case going
4320 ;; inside is mandatory, in order to get sub-level headings.
4321 ((and (memq type org-element-greater-elements
)
4322 (or (memq granularity
'(element object nil
))
4323 (and (eq granularity
'greater-element
)
4325 (eq type
'headline
)))
4326 (org-element--parse-elements
4327 cbeg
(org-element-property :contents-end element
)
4328 ;; Possibly switch to a special mode.
4331 (if (org-element-property :quotedp element
) 'quote-section
4334 (property-drawer 'node-property
)
4336 (and (memq type
'(item plain-list
))
4337 (org-element-property :structure element
))
4338 granularity visible-only element
))
4339 ;; ELEMENT has contents. Parse objects inside, if
4340 ;; GRANULARITY allows it.
4341 ((memq granularity
'(object nil
))
4342 (org-element--parse-objects
4343 cbeg
(org-element-property :contents-end element
) element
4344 (org-element-restriction type
))))
4345 (org-element-adopt-elements acc element
)))
4349 (defun org-element--parse-objects (beg end acc restriction
)
4350 "Parse objects between BEG and END and return recursive structure.
4352 Objects are accumulated in ACC.
4354 RESTRICTION is a list of object successors which are allowed in
4355 the current object."
4356 (let ((candidates 'initial
))
4359 (narrow-to-region beg end
)
4360 (goto-char (point-min))
4361 (while (and (not (eobp))
4363 (org-element--get-next-object-candidates
4364 restriction candidates
)))
4366 (let ((pos (apply 'min
(mapcar 'cdr candidates
))))
4369 (funcall (intern (format "org-element-%s-parser"
4370 (car (rassq pos candidates
)))))))))
4371 ;; 1. Text before any object. Untabify it.
4372 (let ((obj-beg (org-element-property :begin next-object
)))
4373 (unless (= (point) obj-beg
)
4375 (org-element-adopt-elements
4377 (replace-regexp-in-string
4378 "\t" (make-string tab-width ?
)
4379 (buffer-substring-no-properties (point) obj-beg
))))))
4381 (let ((obj-end (org-element-property :end next-object
))
4382 (cont-beg (org-element-property :contents-begin next-object
)))
4383 ;; Fill contents of NEXT-OBJECT by side-effect, if it has
4384 ;; a recursive type.
4386 (memq (car next-object
) org-element-recursive-objects
))
4387 (org-element--parse-objects
4388 cont-beg
(org-element-property :contents-end next-object
)
4389 next-object
(org-element-restriction next-object
)))
4390 (setq acc
(org-element-adopt-elements acc next-object
))
4391 (goto-char obj-end
))))
4392 ;; 3. Text after last object. Untabify it.
4395 (org-element-adopt-elements
4397 (replace-regexp-in-string
4398 "\t" (make-string tab-width ?
)
4399 (buffer-substring-no-properties (point) end
)))))
4403 (defun org-element--get-next-object-candidates (restriction objects
)
4404 "Return an alist of candidates for the next object.
4406 RESTRICTION is a list of object types, as symbols. Only
4407 candidates with such types are looked after.
4409 OBJECTS is the previous candidates alist. If it is set to
4410 `initial', no search has been done before, and all symbols in
4411 RESTRICTION should be looked after.
4413 Return value is an alist whose CAR is the object type and CDR its
4414 beginning position."
4417 (if (eq objects
'initial
)
4418 ;; When searching for the first time, look for every successor
4419 ;; allowed in RESTRICTION.
4422 (funcall (intern (format "org-element-%s-successor" res
))))
4424 ;; Focus on objects returned during last search. Keep those
4425 ;; still after point. Search again objects before it.
4428 (if (>= (cdr obj
) (point)) obj
4429 (let* ((type (car obj
))
4430 (succ (or (cdr (assq type org-element-object-successor-alist
))
4433 (funcall (intern (format "org-element-%s-successor" succ
)))))))
4438 ;;; Towards A Bijective Process
4440 ;; The parse tree obtained with `org-element-parse-buffer' is really
4441 ;; a snapshot of the corresponding Org buffer. Therefore, it can be
4442 ;; interpreted and expanded into a string with canonical Org syntax.
4443 ;; Hence `org-element-interpret-data'.
4445 ;; The function relies internally on
4446 ;; `org-element--interpret-affiliated-keywords'.
4449 (defun org-element-interpret-data (data &optional pseudo-objects
)
4450 "Interpret DATA as Org syntax.
4452 DATA is a parse tree, an element, an object or a secondary string
4455 Optional argument PSEUDO-OBJECTS is a list of symbols defining
4456 new types that should be treated as objects. An unknown type not
4457 belonging to this list is seen as a pseudo-element instead. Both
4458 pseudo-objects and pseudo-elements are transparent entities, i.e.
4459 only their contents are interpreted.
4461 Return Org syntax as a string."
4462 (org-element--interpret-data-1 data nil pseudo-objects
))
4464 (defun org-element--interpret-data-1 (data parent pseudo-objects
)
4465 "Interpret DATA as Org syntax.
4467 DATA is a parse tree, an element, an object or a secondary string
4468 to interpret. PARENT is used for recursive calls. It contains
4469 the element or object containing data, or nil. PSEUDO-OBJECTS
4470 are list of symbols defining new element or object types.
4471 Unknown types that don't belong to this list are treated as
4472 pseudo-elements instead.
4474 Return Org syntax as a string."
4475 (let* ((type (org-element-type data
))
4476 ;; Find interpreter for current object or element. If it
4477 ;; doesn't exist (e.g. this is a pseudo object or element),
4478 ;; return contents, if any.
4480 (let ((fun (intern (format "org-element-%s-interpreter" type
))))
4481 (if (fboundp fun
) fun
(lambda (data contents
) contents
))))
4484 ;; Secondary string.
4488 (org-element--interpret-data-1 obj parent pseudo-objects
))
4490 ;; Full Org document.
4491 ((eq type
'org-data
)
4494 (org-element--interpret-data-1 obj parent pseudo-objects
))
4495 (org-element-contents data
) ""))
4496 ;; Plain text: remove `:parent' text property from output.
4497 ((stringp data
) (org-no-properties data
))
4498 ;; Element or object without contents.
4499 ((not (org-element-contents data
)) (funcall interpret data nil
))
4500 ;; Element or object with contents.
4502 (funcall interpret data
4503 ;; Recursively interpret contents.
4506 (org-element--interpret-data-1 obj data pseudo-objects
))
4507 (org-element-contents
4508 (if (not (memq type
'(paragraph verse-block
)))
4510 ;; Fix indentation of elements containing
4511 ;; objects. We ignore `table-row' elements
4512 ;; as they are one line long anyway.
4513 (org-element-normalize-contents
4515 ;; When normalizing first paragraph of an
4516 ;; item or a footnote-definition, ignore
4517 ;; first line's indentation.
4518 (and (eq type
'paragraph
)
4519 (equal data
(car (org-element-contents parent
)))
4520 (memq (org-element-type parent
)
4521 '(footnote-definition item
))))))
4523 (if (memq type
'(org-data plain-text nil
)) results
4524 ;; Build white spaces. If no `:post-blank' property is
4525 ;; specified, assume its value is 0.
4526 (let ((post-blank (or (org-element-property :post-blank data
) 0)))
4527 (if (or (memq type org-element-all-objects
)
4528 (memq type pseudo-objects
))
4529 (concat results
(make-string post-blank ?\s
))
4531 (org-element--interpret-affiliated-keywords data
)
4532 (org-element-normalize-string results
)
4533 (make-string post-blank ?
\n)))))))
4535 (defun org-element--interpret-affiliated-keywords (element)
4536 "Return ELEMENT's affiliated keywords as Org syntax.
4537 If there is no affiliated keyword, return the empty string."
4538 (let ((keyword-to-org
4542 (when (member key org-element-dual-keywords
)
4543 (setq dual
(cdr value
) value
(car value
)))
4546 (format "[%s]" (org-element-interpret-data dual
)))
4548 (if (member key org-element-parsed-keywords
)
4549 (org-element-interpret-data value
)
4554 (let ((value (org-element-property prop element
))
4555 (keyword (upcase (substring (symbol-name prop
) 1))))
4557 (if (or (member keyword org-element-multiple-keywords
)
4558 ;; All attribute keywords can have multiple lines.
4559 (string-match "^ATTR_" keyword
))
4560 (mapconcat (lambda (line) (funcall keyword-to-org keyword line
))
4563 (funcall keyword-to-org keyword value
)))))
4564 ;; List all ELEMENT's properties matching an attribute line or an
4565 ;; affiliated keyword, but ignore translated keywords since they
4566 ;; cannot belong to the property list.
4567 (loop for prop in
(nth 1 element
) by
'cddr
4568 when
(let ((keyword (upcase (substring (symbol-name prop
) 1))))
4569 (or (string-match "^ATTR_" keyword
)
4571 (member keyword org-element-affiliated-keywords
)
4573 org-element-keyword-translation-alist
)))))
4577 ;; Because interpretation of the parse tree must return the same
4578 ;; number of blank lines between elements and the same number of white
4579 ;; space after objects, some special care must be given to white
4582 ;; The first function, `org-element-normalize-string', ensures any
4583 ;; string different from the empty string will end with a single
4584 ;; newline character.
4586 ;; The second function, `org-element-normalize-contents', removes
4587 ;; global indentation from the contents of the current element.
4589 (defun org-element-normalize-string (s)
4590 "Ensure string S ends with a single newline character.
4592 If S isn't a string return it unchanged. If S is the empty
4593 string, return it. Otherwise, return a new string with a single
4594 newline character at its end."
4596 ((not (stringp s
)) s
)
4598 (t (and (string-match "\\(\n[ \t]*\\)*\\'" s
)
4599 (replace-match "\n" nil nil s
)))))
4601 (defun org-element-normalize-contents (element &optional ignore-first
)
4602 "Normalize plain text in ELEMENT's contents.
4604 ELEMENT must only contain plain text and objects.
4606 If optional argument IGNORE-FIRST is non-nil, ignore first line's
4607 indentation to compute maximal common indentation.
4609 Return the normalized element that is element with global
4610 indentation removed from its contents. The function assumes that
4611 indentation is not done with TAB characters."
4612 (let* (ind-list ; for byte-compiler
4613 collect-inds
; for byte-compiler
4616 ;; Return list of indentations within BLOB. This is done by
4617 ;; walking recursively BLOB and updating IND-LIST along the
4618 ;; way. FIRST-FLAG is non-nil when the first string hasn't
4619 ;; been seen yet. It is required as this string is the only
4620 ;; one whose indentation doesn't happen after a newline
4622 (lambda (blob first-flag
)
4625 (when (and first-flag
(stringp object
))
4626 (setq first-flag nil
)
4627 (string-match "\\`\\( *\\)" object
)
4628 (let ((len (length (match-string 1 object
))))
4629 ;; An indentation of zero means no string will be
4630 ;; modified. Quit the process.
4631 (if (zerop len
) (throw 'zero
(setq ind-list nil
))
4632 (push len ind-list
))))
4636 ;; Avoid matching blank or empty lines.
4637 (while (and (string-match "\n\\( *\\)\\(.\\)" object start
)
4638 (not (equal (match-string 2 object
) " ")))
4639 (setq start
(match-end 0))
4640 (push (length (match-string 1 object
)) ind-list
))))
4641 ((memq (org-element-type object
) org-element-recursive-objects
)
4642 (funcall collect-inds object first-flag
))))
4643 (org-element-contents blob
))))))
4644 ;; Collect indentation list in ELEMENT. Possibly remove first
4645 ;; value if IGNORE-FIRST is non-nil.
4646 (catch 'zero
(funcall collect-inds element
(not ignore-first
)))
4647 (if (not ind-list
) element
4648 ;; Build ELEMENT back, replacing each string with the same
4649 ;; string minus common indentation.
4650 (let* (build ; For byte compiler.
4653 (lambda (blob mci first-flag
)
4654 ;; Return BLOB with all its strings indentation
4655 ;; shortened from MCI white spaces. FIRST-FLAG is
4656 ;; non-nil when the first string hasn't been seen
4661 (when (and first-flag
(stringp object
))
4662 (setq first-flag nil
)
4664 (replace-regexp-in-string
4665 (format "\\` \\{%d\\}" mci
) "" object
)))
4668 (replace-regexp-in-string
4669 (format "\n \\{%d\\}" mci
) "\n" object
))
4670 ((memq (org-element-type object
)
4671 org-element-recursive-objects
)
4672 (funcall build object mci first-flag
))
4674 (org-element-contents blob
)))
4676 (funcall build element
(apply 'min ind-list
) (not ignore-first
))))))
4682 ;; The first move is to implement a way to obtain the smallest element
4683 ;; containing point. This is the job of `org-element-at-point'. It
4684 ;; basically jumps back to the beginning of section containing point
4685 ;; and proceed, one element after the other, with
4686 ;; `org-element--current-element' until the container is found. Note:
4687 ;; When using `org-element-at-point', secondary values are never
4688 ;; parsed since the function focuses on elements, not on objects.
4690 ;; At a deeper level, `org-element-context' lists all elements and
4691 ;; objects containing point.
4693 ;; Both functions benefit from a simple caching mechanism. It is
4694 ;; enabled by default, but can be disabled globally with
4695 ;; `org-element-use-cache'. Also `org-element-cache-reset' clears or
4696 ;; initializes cache for current buffer. Values are retrieved and put
4697 ;; into cache with respectively, `org-element-cache-get' and
4698 ;; `org-element-cache-put'. `org-element--cache-sync-idle-time' and
4699 ;; `org-element--cache-merge-changes-threshold' are used internally to
4700 ;; control caching behaviour.
4702 ;; Eventually `org-element-nested-p' and `org-element-swap-A-B' may be
4703 ;; used internally by navigation and manipulation tools.
4705 (defvar org-element-use-cache t
4706 "Non nil when Org parser should cache its results.")
4708 (defvar org-element--cache nil
4709 "Hash table used as a cache for parser.
4710 Key is a buffer position and value is a cons cell with the
4713 \(ELEMENT . OBJECTS-DATA)
4715 where ELEMENT is the element starting at the key and OBJECTS-DATA
4716 is an alist where each association is:
4718 \(POS CANDIDATES . OBJECTS)
4720 where POS is a buffer position, CANDIDATES is the last know list
4721 of successors (see `org-element--get-next-object-candidates') in
4722 container starting at POS and OBJECTS is a list of objects known
4723 to live within that container, from farthest to closest.
4725 In the following example, \\alpha, bold object and \\beta start
4726 at, respectively, positions 1, 7 and 8,
4730 If the paragraph is completely parsed, OBJECTS-DATA will be
4732 \((1 nil BOLD-OBJECT ENTITY-OBJECT)
4733 \(8 nil ENTITY-OBJECT))
4735 whereas in a partially parsed paragraph, it could be
4737 \((1 ((entity . 1) (bold . 7)) ENTITY-OBJECT))
4739 This cache is used in both `org-element-at-point' and
4740 `org-element-context'. The former uses ELEMENT only and the
4741 latter OBJECTS-DATA only.")
4743 (defvar org-element--cache-sync-idle-time
0.5
4744 "Number of seconds of idle time wait before syncing buffer cache.
4745 Syncing also happens when current modification is too distant
4746 from the stored one (for more information, see
4747 `org-element--cache-merge-changes-threshold').")
4749 (defvar org-element--cache-merge-changes-threshold
200
4750 "Number of characters triggering cache syncing.
4752 The cache mechanism only stores one buffer modification at any
4753 given time. When another change happens, it replaces it with
4754 a change containing both the stored modification and the current
4755 one. This is a trade-off, as merging them prevents another
4756 syncing, but every element between them is then lost.
4758 This variable determines the maximum size, in characters, we
4759 accept to lose in order to avoid syncing the cache.")
4761 (defvar org-element--cache-status nil
4762 "Contains data about cache validity for current buffer.
4764 Value is a vector of seven elements,
4766 [ACTIVEP BEGIN END OFFSET TIMER PREVIOUS-STATE]
4768 ACTIVEP is a boolean non-nil when changes described in the other
4769 slots are valid for current buffer.
4771 BEGIN and END are the beginning and ending position of the area
4772 for which cache cannot be trusted.
4774 OFFSET it an integer specifying the number to add to position of
4775 elements after that area.
4777 TIMER is a timer used to apply these changes to cache when Emacs
4780 PREVIOUS-STATE is a symbol referring to the state of the buffer
4781 before a change happens. It is used to know if sensitive
4782 areas (block boundaries, headlines) were modified. It can be set
4783 to nil, `headline' or `other'.")
4786 (defun org-element-cache-reset (&optional all
)
4787 "Reset cache in current buffer.
4788 When optional argument ALL is non-nil, reset cache in all Org
4789 buffers. This function will do nothing if
4790 `org-element-use-cache' is nil."
4792 (when org-element-use-cache
4793 (dolist (buffer (if all
(buffer-list) (list (current-buffer))))
4794 (with-current-buffer buffer
4795 (when (derived-mode-p 'org-mode
)
4796 (if (org-bound-and-true-p org-element--cache
)
4797 (clrhash org-element--cache
)
4798 (org-set-local 'org-element--cache
4799 (make-hash-table :size
5003 :test
'eq
)))
4800 (org-set-local 'org-element--cache-status
(make-vector 6 nil
))
4801 (add-hook 'before-change-functions
4802 'org-element--cache-before-change nil t
)
4803 (add-hook 'after-change-functions
4804 'org-element--cache-record-change nil t
))))))
4806 (defsubst org-element--cache-pending-changes-p
()
4807 "Non-nil when changes are not integrated in cache yet."
4808 (and org-element--cache-status
4809 (aref org-element--cache-status
0)))
4811 (defsubst org-element--cache-push-change
(beg end offset
)
4812 "Push change to current buffer staging area.
4813 BEG and END and the beginning and ending position of the
4814 modification area. OFFSET is the size of the change, as an
4816 (aset org-element--cache-status
1 beg
)
4817 (aset org-element--cache-status
2 end
)
4818 (aset org-element--cache-status
3 offset
)
4819 (let ((timer (aref org-element--cache-status
4)))
4820 (if timer
(timer-activate-when-idle timer t
)
4821 (aset org-element--cache-status
4
4822 (run-with-idle-timer org-element--cache-sync-idle-time
4824 #'org-element--cache-sync
4825 (current-buffer)))))
4826 (aset org-element--cache-status
0 t
))
4828 (defsubst org-element--cache-cancel-changes
()
4829 "Remove any cache change set for current buffer."
4830 (let ((timer (aref org-element--cache-status
4)))
4831 (and timer
(cancel-timer timer
)))
4832 (aset org-element--cache-status
0 nil
))
4834 (defsubst org-element--cache-get-key
(element)
4835 "Return expected key for ELEMENT in cache."
4836 (let ((begin (org-element-property :begin element
)))
4837 (if (and (memq (org-element-type element
) '(item table-row
))
4838 (= (org-element-property :contents-begin
4839 (org-element-property :parent element
))
4841 ;; Special key for first item (resp. table-row) in a plain
4842 ;; list (resp. table).
4846 (defsubst org-element-cache-get
(pos &optional type
)
4847 "Return data stored at key POS in current buffer cache.
4848 When optional argument TYPE is `element', retrieve the element
4849 starting at POS. When it is `objects', return the list of object
4850 types along with their beginning position within that element.
4851 Otherwise, return the full data. In any case, return nil if no
4852 data is found, or if caching is not allowed."
4853 (when (and org-element-use-cache org-element--cache
)
4854 ;; If there are pending changes, first sync them.
4855 (when (org-element--cache-pending-changes-p)
4856 (org-element--cache-sync (current-buffer)))
4857 (let ((data (gethash pos org-element--cache
)))
4859 (element (car data
))
4860 (objects (cdr data
))
4861 (otherwise data
)))))
4863 (defsubst org-element-cache-put
(pos data
)
4864 "Store data in current buffer's cache, if allowed.
4865 POS is a buffer position, which will be used as a key. DATA is
4866 the value to store. Nothing will be stored if
4867 `org-element-use-cache' is nil. Return DATA in any case."
4868 (if (not org-element-use-cache
) data
4869 (unless org-element--cache
(org-element-cache-reset))
4870 (puthash pos data org-element--cache
)))
4872 (defsubst org-element--cache-shift-positions
(element offset
)
4873 "Shift ELEMENT properties relative to buffer positions by OFFSET.
4874 Properties containing buffer positions are `:begin', `:end',
4875 `:contents-begin', `:contents-end' and `:structure'. They are
4876 modified by side-effect. Return modified element."
4877 (let ((properties (nth 1 element
)))
4878 ;; Shift :structure property for the first plain list only: it is
4879 ;; the only one that really matters and it prevents from shifting
4880 ;; it more than once.
4881 (when (and (eq (org-element-type element
) 'plain-list
)
4882 (not (eq (org-element-type (plist-get properties
:parent
))
4884 (dolist (item (plist-get properties
:structure
))
4885 (incf (car item
) offset
)
4886 (incf (nth 6 item
) offset
)))
4887 (plist-put properties
:begin
(+ (plist-get properties
:begin
) offset
))
4888 (plist-put properties
:end
(+ (plist-get properties
:end
) offset
))
4889 (dolist (key '(:contents-begin
:contents-end
:post-affiliated
))
4890 (let ((value (plist-get properties key
)))
4891 (and value
(plist-put properties key
(+ offset value
))))))
4894 (defconst org-element--cache-opening-line
4895 (concat "^[ \t]*\\(?:"
4896 "#\\+BEGIN[:_]" "\\|"
4897 "\\\\begin{[A-Za-z0-9]+\\*?}" "\\|"
4900 "Regexp matching an element opening line.
4901 When such a line is modified, modifications may propagate after
4902 modified area. In that situation, every element between that
4903 area and next section is removed from cache.")
4905 (defconst org-element--cache-closing-line
4906 (concat "^[ \t]*\\(?:"
4907 "#\\+END\\(?:_\\|:?[ \t]*$\\)" "\\|"
4908 "\\\\end{[A-Za-z0-9]+\\*?}[ \t]*$" "\\|"
4911 "Regexp matching an element closing line.
4912 When such a line is modified, modifications may propagate before
4913 modified area. In that situation, every element between that
4914 area and previous section is removed from cache.")
4916 (defun org-element--cache-before-change (beg end
)
4917 "Request extension of area going to be modified if needed.
4918 BEG and END are the beginning and end of the range of changed
4919 text. See `before-change-functions' for more information."
4920 (let ((inhibit-quit t
))
4921 (org-with-wide-buffer
4925 (bottom (save-excursion (goto-char end
) (line-end-position)))
4927 ;; A sensitive line is a headline or a block (or drawer,
4928 ;; or latex-environment) boundary. Inserting one can
4929 ;; modify buffer drastically both above and below that
4930 ;; line, possibly making cache invalid. Therefore, we
4931 ;; need to pay special attention to changes happening to
4934 "\\(" (org-with-limited-levels org-outline-regexp-bol
) "\\)" "\\|"
4935 org-element--cache-closing-line
"\\|"
4936 org-element--cache-opening-line
)))
4938 (aset org-element--cache-status
5
4939 (cond ((not (re-search-forward sensitive-re bottom t
)) nil
)
4940 ((and (match-beginning 1)
4941 (progn (goto-char bottom
)
4942 (or (not (re-search-backward sensitive-re
4944 (match-beginning 1))))
4948 (defun org-element--cache-record-change (beg end pre
)
4949 "Update buffer modifications for current buffer.
4951 BEG and END are the beginning and end of the range of changed
4952 text, and the length in bytes of the pre-change text replaced by
4953 that range. See `after-change-functions' for more information.
4955 If there are already pending changes, try to merge them into
4956 a bigger change record. If that's not possible, the function
4957 will first synchronize cache with previous change and store the
4959 (let ((inhibit-quit t
))
4960 (when (and org-element-use-cache org-element--cache
)
4961 (org-with-wide-buffer
4965 (bottom (save-excursion (goto-char end
) (line-end-position))))
4966 (org-with-limited-levels
4968 ;; Determine if modified area needs to be extended,
4969 ;; according to both previous and current state. We make
4970 ;; a special case for headline editing: if a headline is
4971 ;; modified but not removed, do not extend.
4972 (when (let ((previous-state (aref org-element--cache-status
5))
4974 (concat "\\(" org-outline-regexp-bol
"\\)" "\\|"
4975 org-element--cache-closing-line
"\\|"
4976 org-element--cache-opening-line
)))
4977 (cond ((eq previous-state
'other
))
4978 ((not (re-search-forward sensitive-re bottom t
))
4979 (eq previous-state
'headline
))
4980 ((match-beginning 1)
4981 (or (not (eq previous-state
'headline
))
4982 (and (progn (goto-char bottom
)
4984 sensitive-re
(match-end 1) t
))
4985 (not (match-beginning 1)))))
4987 ;; Effectively extend modified area.
4988 (setq top
(progn (goto-char top
)
4989 (outline-previous-heading)
4990 ;; Headline above is inclusive.
4992 (setq bottom
(progn (goto-char bottom
)
4993 (outline-next-heading)
4994 ;; Headline below is exclusive.
4995 (if (eobp) (point) (1- (point))))))))
4997 (let ((offset (- end beg pre
)))
4998 (if (not (org-element--cache-pending-changes-p))
4999 ;; No pending changes. Store the new ones.
5000 (org-element--cache-push-change top
(- bottom offset
) offset
)
5001 (let* ((current-start (aref org-element--cache-status
1))
5002 (current-end (+ (aref org-element--cache-status
2)
5003 (aref org-element--cache-status
3)))
5004 (gap (max (- beg current-end
) (- current-start end
))))
5005 (if (> gap org-element--cache-merge-changes-threshold
)
5006 ;; If we cannot merge two change sets (i.e. they
5007 ;; modify distinct buffer parts) first apply current
5008 ;; change set and store new one. This way, there is
5009 ;; never more than one pending change set, which
5010 ;; avoids handling costly merges.
5011 (progn (org-element--cache-sync (current-buffer))
5012 (org-element--cache-push-change
5013 top
(- bottom offset
) offset
))
5014 ;; Change sets can be merged. We can expand the area
5015 ;; that requires an update, and postpone the sync.
5016 (timer-activate-when-idle (aref org-element--cache-status
4) t
)
5017 (aset org-element--cache-status
0 t
)
5018 (aset org-element--cache-status
1 (min top current-start
))
5019 (aset org-element--cache-status
2
5020 (- (max current-end bottom
) offset
))
5021 (incf (aref org-element--cache-status
3) offset
))))))))))
5023 (defun org-element--cache-sync (buffer)
5024 "Synchronize cache with recent modification in BUFFER.
5025 Elements ending before modification area are kept in cache.
5026 Elements starting after modification area have their position
5027 shifted by the size of the modification. Every other element is
5028 removed from the cache."
5029 (when (buffer-live-p buffer
)
5030 (with-current-buffer buffer
5031 (when (org-element--cache-pending-changes-p)
5032 (let ((inhibit-quit t
)
5033 (beg (aref org-element--cache-status
1))
5034 (end (aref org-element--cache-status
2))
5035 (offset (aref org-element--cache-status
3))
5038 #'(lambda (key value
)
5040 ((memq key new-keys
))
5042 ;; Shift every element starting after END by OFFSET.
5043 ;; We also need to shift keys, since they refer to
5044 ;; buffer positions.
5046 ;; Upon shifting a key a conflict can occur if the
5047 ;; shifted key also refers to some element in the
5048 ;; cache. In this case, we temporarily associate
5049 ;; both elements, as a cons cell, to the shifted key,
5050 ;; following the pattern (SHIFTED . CURRENT).
5052 ;; Such a conflict can only occur if shifted key hash
5053 ;; hasn't been processed by `maphash' yet.
5054 (unless (zerop offset
)
5055 (let* ((conflictp (consp (caar value
)))
5056 (value-to-shift (if conflictp
(cdr value
) value
)))
5057 ;; Shift element part.
5058 (org-element--cache-shift-positions (car value-to-shift
) offset
)
5059 ;; Shift objects part.
5060 (dolist (object-data (cdr value-to-shift
))
5061 (incf (car object-data
) offset
)
5062 (dolist (successor (nth 1 object-data
))
5063 (incf (cdr successor
) offset
))
5064 (dolist (object (cddr object-data
))
5065 (org-element--cache-shift-positions object offset
)))
5066 ;; Shift key-value pair.
5067 (let* ((new-key (+ key offset
))
5068 (new-value (gethash new-key org-element--cache
)))
5069 ;; Put new value to shifted key.
5071 ;; If one already exists, do not overwrite it:
5072 ;; store it as the car of a cons cell instead,
5073 ;; and handle it when `maphash' reaches
5076 ;; If there is no element stored at NEW-KEY or
5077 ;; if NEW-KEY is going to be removed anyway
5078 ;; (i.e., it is before END), just store new
5079 ;; value there and make sure it will not be
5080 ;; processed again by storing NEW-KEY in
5083 (if (and new-value
(> new-key end
))
5084 (cons value-to-shift new-value
)
5085 (push new-key new-keys
)
5088 ;; If current value contains two elements, car
5089 ;; should be the new value, since cdr has been
5092 (puthash key
(car value
) org-element--cache
)
5093 (remhash key org-element--cache
))))))
5094 ;; Remove every element between BEG and END, since
5095 ;; this is where changes happened.
5096 ((>= key beg
) (remhash key org-element--cache
))
5097 ;; Preserve any element ending before BEG. If it
5098 ;; overlaps the BEG-END area, remove it.
5100 (let ((element (car value
)))
5101 (if (>= (org-element-property :end element
) beg
)
5102 (remhash key org-element--cache
)
5103 ;; Special case: footnote definitions and plain
5104 ;; lists can end with blank lines. Modifying
5105 ;; those can also alter last element inside. We
5106 ;; must therefore remove them from cache.
5107 (let ((parent (org-element-property :parent element
)))
5108 (when (and parent
(eq (org-element-type parent
) 'item
))
5109 (setq parent
(org-element-property :parent parent
)))
5110 (when (and (memq (org-element-type parent
)
5111 '(footnote-definition plain-list
))
5112 (>= (org-element-property :end parent
) beg
)
5113 (= (org-element-property :contents-end parent
)
5114 (org-element-property :end element
)))
5115 (remhash key org-element--cache
))))))))
5117 ;; Signal cache as up-to-date.
5118 (org-element--cache-cancel-changes))))))
5121 (defun org-element-at-point ()
5122 "Determine closest element around point.
5124 Return value is a list like (TYPE PROPS) where TYPE is the type
5125 of the element and PROPS a plist of properties associated to the
5128 Possible types are defined in `org-element-all-elements'.
5129 Properties depend on element or object type, but always include
5130 `:begin', `:end', `:parent' and `:post-blank' properties.
5132 As a special case, if point is at the very beginning of a list or
5133 sub-list, returned element will be that list instead of the first
5134 item. In the same way, if point is at the beginning of the first
5135 row of a table, returned element will be the table instead of the
5137 (org-with-wide-buffer
5138 (let ((origin (point)) element parent end
)
5140 (skip-chars-backward " \r\t\n")
5143 ((org-with-limited-levels (org-at-heading-p))
5145 (or (org-element-cache-get (point) 'element
)
5146 (car (org-element-cache-put
5148 (list (org-element-headline-parser (point-max) t
))))))
5151 (when org-element-use-cache
5152 ;; Opportunistic shortcut. Instead of going back to
5153 ;; headline above (or beginning of buffer) and descending
5154 ;; again, first try to find a known element above current
5155 ;; position. Give up after 3 tries or when we hit
5156 ;; a headline (or beginning of buffer).
5158 (skip-chars-backward " \r\t\n")
5160 (unless (re-search-backward org-element-paragraph-separate nil t
)
5161 (throw 'loop
(goto-char (point-min))))
5162 (cond ((not (org-string-match-p "\\S-" (match-string 0)))
5163 (when (bobp) (throw 'loop nil
))
5164 ;; An element cannot start at a headline, so check
5165 ;; first non-blank line below.
5166 (skip-chars-forward " \r\t\n" origin
)
5167 (beginning-of-line))
5168 ((org-looking-at-p org-element--affiliated-re
)
5169 ;; At an affiliated keyword, make sure to move to
5171 (if (re-search-backward "^[ \t]*[^#]" nil t
)
5173 (throw 'loop
(goto-char (point-min)))))
5174 ((org-looking-at-p "^[ \t]*:\\(?: \\|$\\)")
5175 ;; At a fixed width area or a property drawer, reach
5176 ;; the beginning of the element.
5177 (if (re-search-backward "^[ \t]*[^:]" nil t
)
5179 (throw 'loop
(goto-char (point-min))))))
5180 (when (org-with-limited-levels (org-at-heading-p))
5181 ;; Tough luck: we're back at a headline above. Move to
5182 ;; beginning of section.
5184 (skip-chars-forward " \r\t\n")
5187 (let ((cached (org-element-cache-get (point) 'element
)))
5188 ;; Search successful: we know an element before point
5189 ;; which is not an headline. If it has a common
5190 ;; ancestor with ORIGIN, set this ancestor as the
5191 ;; current parent and the element as the one to check.
5192 ;; Otherwise, move at top level and start parsing right
5193 ;; after its broader ancestor.
5195 (let ((cache-end (org-element-property :end cached
)))
5196 (if (or (> cache-end origin
)
5197 (and (= cache-end origin
) (= (point-max) origin
)))
5198 (setq element cached
5199 parent
(org-element-property :parent cached
)
5201 (goto-char cache-end
)
5203 (while (and (setq up
(org-element-property :parent up
))
5204 (<= (org-element-property :end up
) origin
))
5205 (goto-char (org-element-property :end up
)))
5208 parent
(org-element-property :parent up
)
5209 end
(org-element-property :end up
))))))
5210 (throw 'loop nil
)))))
5211 ;; Opportunistic search failed. Move back to beginning of
5212 ;; section in current headline, if any, or to first non-empty
5213 ;; line in buffer otherwise.
5214 (org-with-limited-levels (outline-previous-heading))
5215 (unless (bobp) (forward-line))
5216 (skip-chars-forward " \r\t\n")
5217 (beginning-of-line))
5218 ;; Now we are at the beginning of an element, start parsing.
5220 (save-excursion (org-with-limited-levels (outline-next-heading))
5221 (setq end
(point))))
5222 (let (type special-flag struct
)
5223 ;; Parse successively each element, skipping those ending
5224 ;; before original position.
5229 (let* ((pos (if (and (memq special-flag
'(item table-row
))
5230 (memq type
'(plain-list table
)))
5231 ;; First item (resp. row) in
5232 ;; plain list (resp. table) gets
5233 ;; a special key in cache.
5236 (cached (org-element-cache-get pos
'element
)))
5239 (let ((element (org-element--current-element
5240 end
'element special-flag struct
)))
5241 (when (derived-mode-p 'org-mode
)
5242 (org-element-cache-put pos
(cons element nil
)))
5243 (org-element-put-property element
:parent parent
)))
5244 ;; When changes happened in the middle of
5245 ;; a list, its structure ends up being
5246 ;; invalid. Therefore, we make sure to use
5248 ((and struct
(memq (org-element-type cached
)
5249 '(item plain-list
)))
5250 (org-element-put-property cached
:structure struct
))
5252 (setq type
(org-element-type element
))
5254 ;; 1. Skip any element ending before point. Also skip
5255 ;; element ending at point when we're sure that
5256 ;; another element has started.
5257 ((let ((elem-end (org-element-property :end element
)))
5258 (when (or (< elem-end origin
)
5259 (and (= elem-end origin
) (/= elem-end end
)))
5260 (goto-char elem-end
)))
5262 ;; 2. An element containing point is always the element at
5264 ((not (memq type org-element-greater-elements
))
5265 (throw 'exit element
))
5266 ;; 3. At any other greater element type, if point is
5267 ;; within contents, move into it.
5269 (let ((cbeg (org-element-property :contents-begin element
))
5270 (cend (org-element-property :contents-end element
)))
5271 (if (or (not cbeg
) (not cend
) (> cbeg origin
) (< cend origin
)
5272 ;; Create an anchor for tables and plain
5273 ;; lists: when point is at the very beginning
5274 ;; of these elements, ignoring affiliated
5275 ;; keywords, target them instead of their
5277 (and (= cbeg origin
) (memq type
'(plain-list table
)))
5278 ;; When point is at contents end, do not move
5279 ;; into elements with an explicit ending, but
5280 ;; return that element instead.
5281 (and (= cend origin
)
5284 drawer dynamic-block inlinetask
5285 property-drawer quote-block
5287 ;; Corner case: if a list ends at
5288 ;; the end of a buffer without
5289 ;; a final new line, return last
5290 ;; element in last item instead.
5291 (and (memq type
'(item plain-list
))
5292 (progn (goto-char cend
)
5293 (or (bolp) (not (eobp))))))))
5294 (throw 'exit element
)
5297 (setq special-flag
'item
5298 struct
(org-element-property :structure element
)))
5299 (item (setq special-flag nil
))
5301 (setq special-flag
'node-property struct nil
))
5302 (table (setq special-flag
'table-row struct nil
))
5303 (otherwise (setq special-flag nil struct nil
)))
5304 (setq parent element element nil end cend
)
5305 (goto-char cbeg
)))))))))))))
5308 (defun org-element-context (&optional element
)
5309 "Return closest element or object around point.
5311 Return value is a list like (TYPE PROPS) where TYPE is the type
5312 of the element or object and PROPS a plist of properties
5315 Possible types are defined in `org-element-all-elements' and
5316 `org-element-all-objects'. Properties depend on element or
5317 object type, but always include `:begin', `:end', `:parent' and
5320 Optional argument ELEMENT, when non-nil, is the closest element
5321 containing point, as returned by `org-element-at-point'.
5322 Providing it allows for quicker computation."
5323 (catch 'objects-forbidden
5324 (org-with-wide-buffer
5325 (let* ((origin (point))
5326 (element (or element
(org-element-at-point)))
5327 (type (org-element-type element
)))
5328 ;; If point is inside an element containing objects or
5329 ;; a secondary string, narrow buffer to the container and
5330 ;; proceed with parsing. Otherwise, return ELEMENT.
5332 ;; At a parsed affiliated keyword, check if we're inside main
5334 ((let ((post (org-element-property :post-affiliated element
)))
5335 (and post
(< origin post
)))
5337 (let ((case-fold-search t
)) (looking-at org-element--affiliated-re
))
5339 ((not (member-ignore-case (match-string 1)
5340 org-element-parsed-keywords
))
5341 (throw 'objects-forbidden element
))
5342 ((< (match-end 0) origin
)
5343 (narrow-to-region (match-end 0) (line-end-position)))
5344 ((and (match-beginning 2)
5345 (>= origin
(match-beginning 2))
5346 (< origin
(match-end 2)))
5347 (narrow-to-region (match-beginning 2) (match-end 2)))
5348 (t (throw 'objects-forbidden element
)))
5349 ;; Also change type to retrieve correct restrictions.
5350 (setq type
'keyword
))
5351 ;; At an item, objects can only be located within tag, if any.
5353 (let ((tag (org-element-property :tag element
)))
5354 (if (not tag
) (throw 'objects-forbidden element
)
5356 (search-forward tag
(line-end-position))
5357 (goto-char (match-beginning 0))
5358 (if (and (>= origin
(point)) (< origin
(match-end 0)))
5359 (narrow-to-region (point) (match-end 0))
5360 (throw 'objects-forbidden element
)))))
5361 ;; At an headline or inlinetask, objects are in title.
5362 ((memq type
'(headline inlinetask
))
5363 (goto-char (org-element-property :begin element
))
5364 (skip-chars-forward "* ")
5365 (if (and (>= origin
(point)) (< origin
(line-end-position)))
5366 (narrow-to-region (point) (line-end-position))
5367 (throw 'objects-forbidden element
)))
5368 ;; At a paragraph, a table-row or a verse block, objects are
5369 ;; located within their contents.
5370 ((memq type
'(paragraph table-row verse-block
))
5371 (let ((cbeg (org-element-property :contents-begin element
))
5372 (cend (org-element-property :contents-end element
)))
5373 ;; CBEG is nil for table rules.
5374 (if (and cbeg cend
(>= origin cbeg
) (< origin cend
))
5375 (narrow-to-region cbeg cend
)
5376 (throw 'objects-forbidden element
))))
5377 ;; At a parsed keyword, objects are located within value.
5379 (if (not (member (org-element-property :key element
)
5380 org-element-document-properties
))
5381 (throw 'objects-forbidden element
)
5383 (search-forward ":")
5384 (if (and (>= origin
(point)) (< origin
(line-end-position)))
5385 (narrow-to-region (point) (line-end-position))
5386 (throw 'objects-forbidden element
))))
5387 ;; All other locations cannot contain objects: bail out.
5388 (t (throw 'objects-forbidden element
)))
5389 (goto-char (point-min))
5390 (let* ((restriction (org-element-restriction type
))
5392 (candidates 'initial
)
5393 (cache-key (org-element--cache-get-key element
))
5394 (cache (org-element-cache-get cache-key
'objects
))
5395 objects-data next update-cache-flag
)
5399 ;; Get list of next object candidates in CANDIDATES.
5400 ;; When entering for the first time PARENT, grab it
5401 ;; from cache, if available, or compute it. Then,
5402 ;; for each subsequent iteration in PARENT, always
5403 ;; compute it since we're beyond cache anyway.
5404 (when (and (not next
) org-element-use-cache
)
5405 (let ((data (assq (point) cache
)))
5406 (if data
(setq candidates
(nth 1 (setq objects-data data
)))
5407 (push (setq objects-data
(list (point) 'initial
))
5409 (when (or next
(eq 'initial candidates
))
5411 (org-element--get-next-object-candidates
5412 restriction candidates
))
5413 (when org-element-use-cache
5414 (setcar (cdr objects-data
) candidates
)
5415 (or update-cache-flag
(setq update-cache-flag t
))))
5416 ;; Compare ORIGIN with next object starting position,
5419 ;; If ORIGIN is lesser or if there is no object
5420 ;; following, look for a previous object that might
5421 ;; contain it in cache. If there is no cache, we
5422 ;; didn't miss any object so simply return PARENT.
5424 ;; If ORIGIN is greater or equal, parse next
5425 ;; candidate for further processing.
5428 (rassq (apply #'min
(mapcar #'cdr candidates
))
5430 (if (or (not closest
) (> (cdr closest
) origin
))
5432 (dolist (obj (cddr objects-data
) (throw 'exit parent
))
5433 (when (<= (org-element-property :begin obj
) origin
)
5434 (if (<= (org-element-property :end obj
) origin
)
5435 ;; Object ends before ORIGIN and we
5436 ;; know next one in cache starts
5437 ;; after it: bail out.
5438 (throw 'exit parent
)
5439 (throw 'found
(setq next obj
))))))
5440 (goto-char (cdr closest
))
5442 (funcall (intern (format "org-element-%s-parser"
5444 (when org-element-use-cache
5445 (push next
(cddr objects-data
))
5446 (or update-cache-flag
(setq update-cache-flag t
)))))
5447 ;; Process NEXT to know if we need to skip it, return
5448 ;; it or move into it.
5449 (let ((cbeg (org-element-property :contents-begin next
))
5450 (cend (org-element-property :contents-end next
))
5451 (obj-end (org-element-property :end next
)))
5453 ;; ORIGIN is after NEXT, so skip it.
5454 ((<= obj-end origin
) (goto-char obj-end
))
5455 ;; ORIGIN is within a non-recursive next or
5456 ;; at an object boundaries: Return that object.
5457 ((or (not cbeg
) (< origin cbeg
) (>= origin cend
))
5459 (org-element-put-property next
:parent parent
)))
5460 ;; Otherwise, move into NEXT and reset flags as we
5463 (narrow-to-region (point) cend
)
5464 (org-element-put-property next
:parent parent
)
5466 restriction
(org-element-restriction next
)
5469 candidates
'initial
))))))
5470 ;; Update cache if required.
5471 (when (and update-cache-flag
(derived-mode-p 'org-mode
))
5472 (org-element-cache-put cache-key
(cons element cache
)))))))))
5474 (defun org-element-nested-p (elem-A elem-B
)
5475 "Non-nil when elements ELEM-A and ELEM-B are nested."
5476 (let ((beg-A (org-element-property :begin elem-A
))
5477 (beg-B (org-element-property :begin elem-B
))
5478 (end-A (org-element-property :end elem-A
))
5479 (end-B (org-element-property :end elem-B
)))
5480 (or (and (>= beg-A beg-B
) (<= end-A end-B
))
5481 (and (>= beg-B beg-A
) (<= end-B end-A
)))))
5483 (defun org-element-swap-A-B (elem-A elem-B
)
5484 "Swap elements ELEM-A and ELEM-B.
5485 Assume ELEM-B is after ELEM-A in the buffer. Leave point at the
5487 (goto-char (org-element-property :begin elem-A
))
5488 ;; There are two special cases when an element doesn't start at bol:
5489 ;; the first paragraph in an item or in a footnote definition.
5490 (let ((specialp (not (bolp))))
5491 ;; Only a paragraph without any affiliated keyword can be moved at
5492 ;; ELEM-A position in such a situation. Note that the case of
5493 ;; a footnote definition is impossible: it cannot contain two
5494 ;; paragraphs in a row because it cannot contain a blank line.
5496 (or (not (eq (org-element-type elem-B
) 'paragraph
))
5497 (/= (org-element-property :begin elem-B
)
5498 (org-element-property :contents-begin elem-B
))))
5499 (error "Cannot swap elements"))
5500 ;; In a special situation, ELEM-A will have no indentation. We'll
5501 ;; give it ELEM-B's (which will in, in turn, have no indentation).
5502 (let* ((ind-B (when specialp
5503 (goto-char (org-element-property :begin elem-B
))
5504 (org-get-indentation)))
5505 (beg-A (org-element-property :begin elem-A
))
5506 (end-A (save-excursion
5507 (goto-char (org-element-property :end elem-A
))
5508 (skip-chars-backward " \r\t\n")
5510 (beg-B (org-element-property :begin elem-B
))
5511 (end-B (save-excursion
5512 (goto-char (org-element-property :end elem-B
))
5513 (skip-chars-backward " \r\t\n")
5515 ;; Store overlays responsible for visibility status. We
5516 ;; also need to store their boundaries as they will be
5517 ;; removed from buffer.
5520 (mapcar (lambda (ov) (list ov
(overlay-start ov
) (overlay-end ov
)))
5521 (overlays-in beg-A end-A
))
5522 (mapcar (lambda (ov) (list ov
(overlay-start ov
) (overlay-end ov
)))
5523 (overlays-in beg-B end-B
))))
5525 (body-A (buffer-substring beg-A end-A
))
5526 (body-B (delete-and-extract-region beg-B end-B
)))
5529 (setq body-B
(replace-regexp-in-string "\\`[ \t]*" "" body-B
))
5530 (org-indent-to-column ind-B
))
5532 ;; Restore ex ELEM-A overlays.
5533 (let ((offset (- beg-B beg-A
)))
5536 (car ov
) (+ (nth 1 ov
) offset
) (+ (nth 2 ov
) offset
)))
5539 (delete-region beg-A end-A
)
5541 ;; Restore ex ELEM-B overlays.
5544 (car ov
) (- (nth 1 ov
) offset
) (- (nth 2 ov
) offset
)))
5546 (goto-char (org-element-property :end elem-B
)))))
5548 (defun org-element-remove-indentation (s &optional n
)
5549 "Remove maximum common indentation in string S and return it.
5550 When optional argument N is a positive integer, remove exactly
5551 that much characters from indentation, if possible, or return
5552 S as-is otherwise. Unlike to `org-remove-indentation', this
5553 function doesn't call `untabify' on S."
5557 (goto-char (point-min))
5558 ;; Find maximum common indentation, if not specified.
5560 (let ((min-ind (point-max)))
5562 (while (re-search-forward "^[ \t]*\\S-" nil t
)
5563 (let ((ind (1- (current-column))))
5564 (if (zerop ind
) (throw 'exit s
)
5565 (setq min-ind
(min min-ind ind
))))))
5568 ;; Remove exactly N indentation, but give up if not possible.
5570 (let ((ind (progn (skip-chars-forward " \t") (current-column))))
5571 (cond ((eolp) (delete-region (line-beginning-position) (point)))
5572 ((< ind n
) (throw 'exit s
))
5573 (t (org-indent-line-to (- ind n
))))
5578 (provide 'org-element
)
5581 ;; generated-autoload-file: "org-loaddefs.el"
5584 ;;; org-element.el ends here