1 ;;; org-element.el --- Parser And Applications for Org syntax
3 ;; Copyright (C) 2012 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 an 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 ;; and elements containing objects will also have `:contents-begin'
77 ;; and `:contents-end' properties to delimit contents.
79 ;; At the lowest level, a `:parent' property is also attached to any
80 ;; string, as a text property.
82 ;; Lisp-wise, an element or an object can be represented as a list.
83 ;; It follows the pattern (TYPE PROPERTIES CONTENTS), where:
84 ;; TYPE is a symbol describing the Org element or object.
85 ;; PROPERTIES is the property list attached to it. See docstring of
86 ;; appropriate parsing function to get an exhaustive
88 ;; CONTENTS is a list of elements, objects or raw strings contained
89 ;; in the current element or object, when applicable.
91 ;; An Org buffer is a nested list of such elements and objects, whose
92 ;; type is `org-data' and properties is nil.
94 ;; The first part of this file defines Org syntax, while the second
95 ;; one provide accessors and setters functions.
97 ;; The next part implements a parser and an interpreter for each
98 ;; element and object type in Org syntax.
100 ;; The following part creates a fully recursive buffer parser. It
101 ;; also provides a tool to map a function to elements or objects
102 ;; matching some criteria in the parse tree. Functions of interest
103 ;; are `org-element-parse-buffer', `org-element-map' and, to a lesser
104 ;; extent, `org-element-parse-secondary-string'.
106 ;; The penultimate part is the cradle of an interpreter for the
107 ;; obtained parse tree: `org-element-interpret-data'.
109 ;; The library ends by furnishing `org-element-at-point' function, and
110 ;; a way to give information about document structure around point
111 ;; with `org-element-context'.
116 (eval-when-compile (require 'cl
))
121 ;;; Definitions And Rules
123 ;; Define elements, greater elements and specify recursive objects,
124 ;; along with the affiliated keywords recognized. Also set up
125 ;; restrictions on recursive objects combinations.
127 ;; These variables really act as a control center for the parsing
130 (defconst org-element-paragraph-separate
132 ;; Headlines, inlinetasks.
133 org-outline-regexp
"\\|"
134 ;; Footnote definitions.
135 "\\[\\(?:[0-9]+\\|fn:[-_[:word:]]+\\)\\]" "\\|"
141 ;; Tables (any type).
142 "\\(?:|\\|\\+-[-+]\\)" "\\|"
143 ;; Blocks (any type), Babel calls, drawers (any type),
144 ;; fixed-width areas and keywords. Note: this is only an
145 ;; indication and need some thorough check.
148 "-\\{5,\\}[ \t]*$" "\\|"
149 ;; LaTeX environments.
150 "\\\\begin{\\([A-Za-z0-9]+\\*?\\)}" "\\|"
151 ;; Planning and Clock lines.
152 (regexp-opt (list org-scheduled-string
158 (let ((term (case org-plain-list-ordered-item-terminator
159 (?\
) ")") (?.
"\\.") (otherwise "[.)]")))
160 (alpha (and org-alphabetical-lists
"\\|[A-Za-z]")))
161 (concat "\\(?:[-+*]\\|\\(?:[0-9]+" alpha
"\\)" term
"\\)"
162 "\\(?:[ \t]\\|$\\)"))
164 "Regexp to separate paragraphs in an Org buffer.
165 In the case of lines starting with \"#\" and \":\", this regexp
166 is not sufficient to know if point is at a paragraph ending. See
167 `org-element-paragraph-parser' for more information.")
169 (defconst org-element-all-elements
170 '(babel-call center-block clock comment comment-block diary-sexp drawer
171 dynamic-block example-block export-block fixed-width
172 footnote-definition headline horizontal-rule inlinetask item
173 keyword latex-environment node-property paragraph plain-list
174 planning property-drawer quote-block quote-section section
175 special-block src-block table table-row verse-block
)
176 "Complete list of element types.")
178 (defconst org-element-greater-elements
179 '(center-block drawer dynamic-block footnote-definition headline inlinetask
180 item plain-list property-drawer quote-block section
182 "List of recursive element types aka Greater Elements.")
184 (defconst org-element-all-successors
185 '(export-snippet footnote-reference inline-babel-call inline-src-block
186 latex-or-entity line-break link macro radio-target
187 statistics-cookie sub
/superscript table-cell target
188 text-markup timestamp
)
189 "Complete list of successors.")
191 (defconst org-element-object-successor-alist
192 '((subscript . sub
/superscript
) (superscript . sub
/superscript
)
193 (bold . text-markup
) (code . text-markup
) (italic . text-markup
)
194 (strike-through . text-markup
) (underline . text-markup
)
195 (verbatim . text-markup
) (entity . latex-or-entity
)
196 (latex-fragment . latex-or-entity
))
197 "Alist of translations between object type and successor name.
199 Sharing the same successor comes handy when, for example, the
200 regexp matching one object can also match the other object.")
202 (defconst org-element-all-objects
203 '(bold code entity export-snippet footnote-reference inline-babel-call
204 inline-src-block italic line-break latex-fragment link macro
205 radio-target statistics-cookie strike-through subscript superscript
206 table-cell target timestamp underline verbatim
)
207 "Complete list of object types.")
209 (defconst org-element-recursive-objects
210 '(bold italic link subscript radio-target strike-through superscript
211 table-cell underline
)
212 "List of recursive object types.")
214 (defconst org-element-block-name-alist
215 '(("CENTER" . org-element-center-block-parser
)
216 ("COMMENT" . org-element-comment-block-parser
)
217 ("EXAMPLE" . org-element-example-block-parser
)
218 ("QUOTE" . org-element-quote-block-parser
)
219 ("SRC" . org-element-src-block-parser
)
220 ("VERSE" . org-element-verse-block-parser
))
221 "Alist between block names and the associated parsing function.
222 Names must be uppercase. Any block whose name has no association
223 is parsed with `org-element-special-block-parser'.")
225 (defconst org-element-link-type-is-file
226 '("file" "file+emacs" "file+sys" "docview")
227 "List of link types equivalent to \"file\".
228 Only these types can accept search options and an explicit
229 application to open them.")
231 (defconst org-element-affiliated-keywords
232 '("CAPTION" "DATA" "HEADER" "HEADERS" "LABEL" "NAME" "PLOT" "RESNAME" "RESULT"
233 "RESULTS" "SOURCE" "SRCNAME" "TBLNAME")
234 "List of affiliated keywords as strings.
235 By default, all keywords setting attributes (i.e. \"ATTR_LATEX\")
236 are affiliated keywords and need not to be in this list.")
238 (defconst org-element--affiliated-re
239 (format "[ \t]*#\\+%s:"
240 ;; Regular affiliated keywords.
241 (format "\\(%s\\|ATTR_[-_A-Za-z0-9]+\\)\\(?:\\[\\(.*\\)\\]\\)?"
242 (regexp-opt org-element-affiliated-keywords
)))
243 "Regexp matching any affiliated keyword.
245 Keyword name is put in match group 1. Moreover, if keyword
246 belongs to `org-element-dual-keywords', put the dual value in
249 Don't modify it, set `org-element-affiliated-keywords' instead.")
251 (defconst org-element-keyword-translation-alist
252 '(("DATA" .
"NAME") ("LABEL" .
"NAME") ("RESNAME" .
"NAME")
253 ("SOURCE" .
"NAME") ("SRCNAME" .
"NAME") ("TBLNAME" .
"NAME")
254 ("RESULT" .
"RESULTS") ("HEADERS" .
"HEADER"))
255 "Alist of usual translations for keywords.
256 The key is the old name and the value the new one. The property
257 holding their value will be named after the translated name.")
259 (defconst org-element-multiple-keywords
'("CAPTION" "HEADER")
260 "List of affiliated keywords that can occur more than once in an element.
262 Their value will be consed into a list of strings, which will be
263 returned as the value of the property.
265 This list is checked after translations have been applied. See
266 `org-element-keyword-translation-alist'.
268 By default, all keywords setting attributes (i.e. \"ATTR_LATEX\")
269 allow multiple occurrences and need not to be in this list.")
271 (defconst org-element-parsed-keywords
'("CAPTION")
272 "List of affiliated keywords whose value can be parsed.
274 Their value will be stored as a secondary string: a list of
277 This list is checked after translations have been applied. See
278 `org-element-keyword-translation-alist'.")
280 (defconst org-element-dual-keywords
'("CAPTION" "RESULTS")
281 "List of affiliated keywords which can have a secondary value.
283 In Org syntax, they can be written with optional square brackets
284 before the colons. For example, RESULTS keyword can be
285 associated to a hash value with the following:
287 #+RESULTS[hash-string]: some-source
289 This list is checked after translations have been applied. See
290 `org-element-keyword-translation-alist'.")
292 (defconst org-element-document-properties
'("AUTHOR" "DATE" "TITLE")
293 "List of properties associated to the whole document.
294 Any keyword in this list will have its value parsed and stored as
295 a secondary string.")
297 (defconst org-element-object-restrictions
298 '((bold export-snippet inline-babel-call inline-src-block latex-or-entity link
299 radio-target sub
/superscript target text-markup timestamp
)
300 (footnote-reference export-snippet footnote-reference inline-babel-call
301 inline-src-block latex-or-entity line-break link macro
302 radio-target sub
/superscript target text-markup
304 (headline inline-babel-call inline-src-block latex-or-entity link macro
305 radio-target statistics-cookie sub
/superscript target text-markup
307 (inlinetask inline-babel-call inline-src-block latex-or-entity link macro
308 radio-target sub
/superscript target text-markup timestamp
)
309 (italic export-snippet inline-babel-call inline-src-block latex-or-entity
310 link radio-target sub
/superscript target text-markup timestamp
)
311 (item export-snippet footnote-reference inline-babel-call latex-or-entity
312 link macro radio-target sub
/superscript target text-markup
)
313 (keyword inline-babel-call inline-src-block latex-or-entity link macro
314 sub
/superscript text-markup
)
315 (link export-snippet inline-babel-call inline-src-block latex-or-entity link
316 sub
/superscript text-markup
)
317 (paragraph export-snippet footnote-reference inline-babel-call
318 inline-src-block latex-or-entity line-break link macro
319 radio-target statistics-cookie sub
/superscript target text-markup
321 (radio-target export-snippet latex-or-entity sub
/superscript
)
322 (strike-through export-snippet inline-babel-call inline-src-block
323 latex-or-entity link radio-target sub
/superscript target
324 text-markup timestamp
)
325 (subscript export-snippet inline-babel-call inline-src-block latex-or-entity
326 sub
/superscript target text-markup
)
327 (superscript export-snippet inline-babel-call inline-src-block
328 latex-or-entity sub
/superscript target text-markup
)
329 (table-cell export-snippet latex-or-entity link macro radio-target
330 sub
/superscript target text-markup timestamp
)
331 (table-row table-cell
)
332 (underline export-snippet inline-babel-call inline-src-block latex-or-entity
333 link radio-target sub
/superscript target text-markup timestamp
)
334 (verse-block footnote-reference inline-babel-call inline-src-block
335 latex-or-entity line-break link macro radio-target
336 sub
/superscript target text-markup timestamp
))
337 "Alist of objects restrictions.
339 CAR is an element or object type containing objects and CDR is
340 a list of successors that will be called within an element or
343 For example, in a `radio-target' object, one can only find
344 entities, export snippets, latex-fragments, subscript and
347 This alist also applies to secondary string. For example, an
348 `headline' type element doesn't directly contain objects, but
349 still has an entry since one of its properties (`:title') does.")
351 (defconst org-element-secondary-value-alist
352 '((headline .
:title
)
353 (inlinetask .
:title
)
355 (footnote-reference .
:inline-definition
))
356 "Alist between element types and location of secondary value.")
360 ;;; Accessors and Setters
362 ;; Provide four accessors: `org-element-type', `org-element-property'
363 ;; `org-element-contents' and `org-element-restriction'.
365 ;; Setter functions allow to modify elements by side effect. There is
366 ;; `org-element-put-property', `org-element-set-contents',
367 ;; `org-element-set-element' and `org-element-adopt-element'. Note
368 ;; that `org-element-set-element' and `org-element-adopt-elements' are
369 ;; higher level functions since also update `:parent' property.
371 (defsubst org-element-type
(element)
372 "Return type of ELEMENT.
374 The function returns the type of the element or object provided.
375 It can also return the following special value:
376 `plain-text' for a string
377 `org-data' for a complete document
378 nil in any other case."
380 ((not (consp element
)) (and (stringp element
) 'plain-text
))
381 ((symbolp (car element
)) (car element
))))
383 (defsubst org-element-property
(property element
)
384 "Extract the value from the PROPERTY of an ELEMENT."
385 (if (stringp element
) (get-text-property 0 property element
)
386 (plist-get (nth 1 element
) property
)))
388 (defsubst org-element-contents
(element)
389 "Extract contents from an ELEMENT."
390 (and (consp element
) (nthcdr 2 element
)))
392 (defsubst org-element-restriction
(element)
393 "Return restriction associated to ELEMENT.
394 ELEMENT can be an element, an object or a symbol representing an
395 element or object type."
396 (cdr (assq (if (symbolp element
) element
(org-element-type element
))
397 org-element-object-restrictions
)))
399 (defsubst org-element-put-property
(element property value
)
400 "In ELEMENT set PROPERTY to VALUE.
401 Return modified element."
402 (if (stringp element
) (org-add-props element nil property value
)
403 (setcar (cdr element
) (plist-put (nth 1 element
) property value
))
406 (defsubst org-element-set-contents
(element &rest contents
)
407 "Set ELEMENT contents to CONTENTS.
408 Return modified element."
409 (cond ((not element
) (list contents
))
410 ((cdr element
) (setcdr (cdr element
) contents
))
411 (t (nconc element contents
))))
413 (defsubst org-element-set-element
(old new
)
414 "Replace element or object OLD with element or object NEW.
415 The function takes care of setting `:parent' property for NEW."
416 ;; Since OLD is going to be changed into NEW by side-effect, first
417 ;; make sure that every element or object within NEW has OLD as
419 (mapc (lambda (blob) (org-element-put-property blob
:parent old
))
420 (org-element-contents new
))
421 ;; Transfer contents.
422 (apply 'org-element-set-contents old
(org-element-contents new
))
423 ;; Ensure NEW has same parent as OLD, then overwrite OLD properties
425 (org-element-put-property new
:parent
(org-element-property :parent old
))
426 (setcar (cdr old
) (nth 1 new
))
428 (setcar old
(car new
)))
430 (defsubst org-element-adopt-elements
(parent &rest children
)
431 "Append elements to the contents of another element.
433 PARENT is an element or object. CHILDREN can be elements,
434 objects, or a strings.
436 The function takes care of setting `:parent' property for CHILD.
437 Return parent element."
438 (if (not parent
) children
439 ;; Link every child to PARENT.
440 (mapc (lambda (child) (org-element-put-property child
:parent parent
))
442 ;; Add CHILDREN at the end of PARENT contents.
443 (apply 'org-element-set-contents
445 (nconc (org-element-contents parent
) children
))
446 ;; Return modified PARENT element.
453 ;; For each greater element type, we define a parser and an
456 ;; A parser returns the element or object as the list described above.
457 ;; Most of them accepts no argument. Though, exceptions exist. Hence
458 ;; every element containing a secondary string (see
459 ;; `org-element-secondary-value-alist') will accept an optional
460 ;; argument to toggle parsing of that secondary string. Moreover,
461 ;; `item' parser requires current list's structure as its first
464 ;; An interpreter accepts two arguments: the list representation of
465 ;; the element or object, and its contents. The latter may be nil,
466 ;; depending on the element or object considered. It returns the
467 ;; appropriate Org syntax, as a string.
469 ;; Parsing functions must follow the naming convention:
470 ;; org-element-TYPE-parser, where TYPE is greater element's type, as
471 ;; defined in `org-element-greater-elements'.
473 ;; Similarly, interpreting functions must follow the naming
474 ;; convention: org-element-TYPE-interpreter.
476 ;; With the exception of `headline' and `item' types, greater elements
477 ;; cannot contain other greater elements of their own type.
479 ;; Beside implementing a parser and an interpreter, adding a new
480 ;; greater element requires to tweak `org-element--current-element'.
481 ;; Moreover, the newly defined type must be added to both
482 ;; `org-element-all-elements' and `org-element-greater-elements'.
487 (defun org-element-center-block-parser (limit affiliated
)
488 "Parse a center block.
490 LIMIT bounds the search. AFFILIATED is a list of which CAR is
491 the buffer position at the beginning of the first affiliated
492 keyword and CDR is a plist of affiliated keywords along with
495 Return a list whose CAR is `center-block' and CDR is a plist
496 containing `:begin', `:end', `:hiddenp', `:contents-begin',
497 `:contents-end' and `:post-blank' keywords.
499 Assume point is at the beginning of the block."
500 (let ((case-fold-search t
))
501 (if (not (save-excursion
502 (re-search-forward "^[ \t]*#\\+END_CENTER[ \t]*$" limit t
)))
503 ;; Incomplete block: parse it as a paragraph.
504 (org-element-paragraph-parser limit affiliated
)
505 (let ((block-end-line (match-beginning 0)))
506 (let* ((begin (car affiliated
))
507 ;; Empty blocks have no contents.
508 (contents-begin (progn (forward-line)
509 (and (< (point) block-end-line
)
511 (contents-end (and contents-begin block-end-line
))
512 (hidden (org-invisible-p2))
513 (pos-before-blank (progn (goto-char block-end-line
)
516 (end (save-excursion (skip-chars-forward " \r\t\n" limit
)
517 (skip-chars-backward " \t")
518 (if (bolp) (point) (line-end-position)))))
524 :contents-begin contents-begin
525 :contents-end contents-end
526 :post-blank
(count-lines pos-before-blank end
))
527 (cdr affiliated
))))))))
529 (defun org-element-center-block-interpreter (center-block contents
)
530 "Interpret CENTER-BLOCK element as Org syntax.
531 CONTENTS is the contents of the element."
532 (format "#+BEGIN_CENTER\n%s#+END_CENTER" contents
))
537 (defun org-element-drawer-parser (limit affiliated
)
540 LIMIT bounds the search. AFFILIATED is a list of which CAR is
541 the buffer position at the beginning of the first affiliated
542 keyword and CDR is a plist of affiliated keywords along with
545 Return a list whose CAR is `drawer' and CDR is a plist containing
546 `:drawer-name', `:begin', `:end', `:hiddenp', `:contents-begin',
547 `:contents-end' and `:post-blank' keywords.
549 Assume point is at beginning of drawer."
550 (let ((case-fold-search t
))
551 (if (not (save-excursion (re-search-forward "^[ \t]*:END:[ \t]*$" limit t
)))
552 ;; Incomplete drawer: parse it as a paragraph.
553 (org-element-paragraph-parser limit affiliated
)
555 (let* ((drawer-end-line (match-beginning 0))
556 (name (progn (looking-at org-drawer-regexp
)
557 (org-match-string-no-properties 1)))
558 (begin (car affiliated
))
559 ;; Empty drawers have no contents.
560 (contents-begin (progn (forward-line)
561 (and (< (point) drawer-end-line
)
563 (contents-end (and contents-begin drawer-end-line
))
564 (hidden (org-invisible-p2))
565 (pos-before-blank (progn (goto-char drawer-end-line
)
568 (end (progn (skip-chars-forward " \r\t\n" limit
)
569 (skip-chars-backward " \t")
570 (if (bolp) (point) (line-end-position)))))
577 :contents-begin contents-begin
578 :contents-end contents-end
579 :post-blank
(count-lines pos-before-blank end
))
580 (cdr affiliated
))))))))
582 (defun org-element-drawer-interpreter (drawer contents
)
583 "Interpret DRAWER element as Org syntax.
584 CONTENTS is the contents of the element."
585 (format ":%s:\n%s:END:"
586 (org-element-property :drawer-name drawer
)
592 (defun org-element-dynamic-block-parser (limit affiliated
)
593 "Parse a dynamic block.
595 LIMIT bounds the search. AFFILIATED is a list of which CAR is
596 the buffer position at the beginning of the first affiliated
597 keyword and CDR is a plist of affiliated keywords along with
600 Return a list whose CAR is `dynamic-block' and CDR is a plist
601 containing `:block-name', `:begin', `:end', `:hiddenp',
602 `:contents-begin', `:contents-end', `:arguments' and
603 `:post-blank' keywords.
605 Assume point is at beginning of dynamic block."
606 (let ((case-fold-search t
))
607 (if (not (save-excursion
608 (re-search-forward "^[ \t]*#\\+END:?[ \t]*$" limit t
)))
609 ;; Incomplete block: parse it as a paragraph.
610 (org-element-paragraph-parser limit affiliated
)
611 (let ((block-end-line (match-beginning 0)))
613 (let* ((name (progn (looking-at org-dblock-start-re
)
614 (org-match-string-no-properties 1)))
615 (arguments (org-match-string-no-properties 3))
616 (begin (car affiliated
))
617 ;; Empty blocks have no contents.
618 (contents-begin (progn (forward-line)
619 (and (< (point) block-end-line
)
621 (contents-end (and contents-begin block-end-line
))
622 (hidden (org-invisible-p2))
623 (pos-before-blank (progn (goto-char block-end-line
)
626 (end (progn (skip-chars-forward " \r\t\n" limit
)
627 (skip-chars-backward " \t")
628 (if (bolp) (point) (line-end-position)))))
636 :contents-begin contents-begin
637 :contents-end contents-end
638 :post-blank
(count-lines pos-before-blank end
))
639 (cdr affiliated
)))))))))
641 (defun org-element-dynamic-block-interpreter (dynamic-block contents
)
642 "Interpret DYNAMIC-BLOCK element as Org syntax.
643 CONTENTS is the contents of the element."
644 (format "#+BEGIN: %s%s\n%s#+END:"
645 (org-element-property :block-name dynamic-block
)
646 (let ((args (org-element-property :arguments dynamic-block
)))
647 (and args
(concat " " args
)))
651 ;;;; Footnote Definition
653 (defun org-element-footnote-definition-parser (limit affiliated
)
654 "Parse a footnote definition.
656 LIMIT bounds the search. AFFILIATED is a list of which CAR is
657 the buffer position at the beginning of the first affiliated
658 keyword and CDR is a plist of affiliated keywords along with
661 Return a list whose CAR is `footnote-definition' and CDR is
662 a plist containing `:label', `:begin' `:end', `:contents-begin',
663 `:contents-end' and `:post-blank' keywords.
665 Assume point is at the beginning of the footnote definition."
667 (let* ((label (progn (looking-at org-footnote-definition-re
)
668 (org-match-string-no-properties 1)))
669 (begin (car affiliated
))
670 (ending (save-excursion
674 (concat org-outline-regexp-bol
"\\|"
675 org-footnote-definition-re
"\\|"
676 "^[ \t]*$") limit
'move
))
679 (contents-begin (progn (search-forward "]")
680 (skip-chars-forward " \r\t\n" ending
)
681 (and (/= (point) ending
) (point))))
682 (contents-end (and contents-begin ending
))
683 (end (progn (goto-char ending
)
684 (skip-chars-forward " \r\t\n" limit
)
685 (skip-chars-backward " \t")
686 (if (bolp) (point) (line-end-position)))))
687 (list 'footnote-definition
692 :contents-begin contents-begin
693 :contents-end contents-end
694 :post-blank
(count-lines ending end
))
695 (cdr affiliated
))))))
697 (defun org-element-footnote-definition-interpreter (footnote-definition contents
)
698 "Interpret FOOTNOTE-DEFINITION element as Org syntax.
699 CONTENTS is the contents of the footnote-definition."
700 (concat (format "[%s]" (org-element-property :label footnote-definition
))
707 (defun org-element-headline-parser (limit &optional raw-secondary-p
)
710 Return a list whose CAR is `headline' and CDR is a plist
711 containing `:raw-value', `:title', `:begin', `:end',
712 `:pre-blank', `:hiddenp', `:contents-begin' and `:contents-end',
713 `:level', `:priority', `:tags', `:todo-keyword',`:todo-type',
714 `:scheduled', `:deadline', `:timestamp', `:clock', `:category',
715 `:quotedp', `:archivedp', `:commentedp' and `:footnote-section-p'
718 The plist also contains any property set in the property drawer,
719 with its name in lowercase, the underscores replaced with hyphens
720 and colons at the beginning (i.e. `:custom-id').
722 When RAW-SECONDARY-P is non-nil, headline's title will not be
723 parsed as a secondary string, but as a plain string instead.
725 Assume point is at beginning of the headline."
727 (let* ((components (org-heading-components))
728 (level (nth 1 components
))
729 (todo (nth 2 components
))
731 (and todo
(if (member todo org-done-keywords
) 'done
'todo
)))
732 (tags (let ((raw-tags (nth 5 components
)))
733 (and raw-tags
(org-split-string raw-tags
":"))))
734 (raw-value (or (nth 4 components
) ""))
736 (let ((case-fold-search nil
))
737 (string-match (format "^%s\\( \\|$\\)" org-quote-string
)
740 (let ((case-fold-search nil
))
741 (string-match (format "^%s\\( \\|$\\)" org-comment-string
)
743 (archivedp (member org-archive-tag tags
))
744 (footnote-section-p (and org-footnote-section
745 (string= org-footnote-section raw-value
)))
746 ;; Normalize property names: ":SOME_PROP:" becomes
748 (standard-props (let (plist)
751 (let ((p-name (downcase (car p
))))
752 (while (string-match "_" p-name
)
754 (replace-match "-" nil nil p-name
)))
755 (setq p-name
(intern (concat ":" p-name
)))
757 (plist-put plist p-name
(cdr p
)))))
758 (org-entry-properties nil
'standard
))
760 (time-props (org-entry-properties nil
'special
"CLOCK"))
761 (scheduled (cdr (assoc "SCHEDULED" time-props
)))
762 (deadline (cdr (assoc "DEADLINE" time-props
)))
763 (clock (cdr (assoc "CLOCK" time-props
)))
764 (timestamp (cdr (assoc "TIMESTAMP" time-props
)))
766 (end (save-excursion (goto-char (org-end-of-subtree t t
))))
767 (pos-after-head (progn (forward-line) (point)))
768 (contents-begin (save-excursion
769 (skip-chars-forward " \r\t\n" end
)
770 (and (/= (point) end
) (line-beginning-position))))
771 (hidden (org-invisible-p2))
772 (contents-end (and contents-begin
773 (progn (goto-char end
)
774 (skip-chars-backward " \r\t\n")
777 ;; Clean RAW-VALUE from any quote or comment string.
778 (when (or quotedp commentedp
)
779 (let ((case-fold-search nil
))
781 (replace-regexp-in-string
783 (regexp-opt (list org-quote-string org-comment-string
))
787 ;; Clean TAGS from archive tag, if any.
788 (when archivedp
(setq tags
(delete org-archive-tag tags
)))
792 (list :raw-value raw-value
796 (if (not contents-begin
) 0
797 (count-lines pos-after-head contents-begin
))
799 :contents-begin contents-begin
800 :contents-end contents-end
802 :priority
(nth 3 components
)
810 :post-blank
(count-lines
811 (if (not contents-end
) pos-after-head
812 (goto-char contents-end
)
816 :footnote-section-p footnote-section-p
818 :commentedp commentedp
821 (org-element-put-property
823 (if raw-secondary-p raw-value
824 (org-element-parse-secondary-string
825 raw-value
(org-element-restriction 'headline
) headline
)))))))
827 (defun org-element-headline-interpreter (headline contents
)
828 "Interpret HEADLINE element as Org syntax.
829 CONTENTS is the contents of the element."
830 (let* ((level (org-element-property :level headline
))
831 (todo (org-element-property :todo-keyword headline
))
832 (priority (org-element-property :priority headline
))
833 (title (org-element-interpret-data
834 (org-element-property :title headline
)))
835 (tags (let ((tag-list (if (org-element-property :archivedp headline
)
836 (cons org-archive-tag
837 (org-element-property :tags headline
))
838 (org-element-property :tags headline
))))
840 (format ":%s:" (mapconcat 'identity tag-list
":")))))
841 (commentedp (org-element-property :commentedp headline
))
842 (quotedp (org-element-property :quotedp headline
))
843 (pre-blank (or (org-element-property :pre-blank headline
) 0))
844 (heading (concat (make-string level ?
*)
845 (and todo
(concat " " todo
))
846 (and quotedp
(concat " " org-quote-string
))
847 (and commentedp
(concat " " org-comment-string
))
849 (format " [#%s]" (char-to-string priority
)))
850 (cond ((and org-footnote-section
851 (org-element-property
852 :footnote-section-p headline
))
853 (concat " " org-footnote-section
))
854 (title (concat " " title
))))))
859 ((zerop org-tags-column
) (format " %s" tags
))
860 ((< org-tags-column
0)
863 (max (- (+ org-tags-column
(length heading
) (length tags
))) 1)
868 (make-string (max (- org-tags-column
(length heading
)) 1) ?
)
870 (make-string (1+ pre-blank
) 10)
876 (defun org-element-inlinetask-parser (limit &optional raw-secondary-p
)
877 "Parse an inline task.
879 Return a list whose CAR is `inlinetask' and CDR is a plist
880 containing `:title', `:begin', `:end', `:hiddenp',
881 `:contents-begin' and `:contents-end', `:level', `:priority',
882 `:raw-value', `:tags', `:todo-keyword', `:todo-type',
883 `:scheduled', `:deadline', `:timestamp', `:clock' and
884 `:post-blank' keywords.
886 The plist also contains any property set in the property drawer,
887 with its name in lowercase, the underscores replaced with hyphens
888 and colons at the beginning (i.e. `:custom-id').
890 When optional argument RAW-SECONDARY-P is non-nil, inline-task's
891 title will not be parsed as a secondary string, but as a plain
894 Assume point is at beginning of the inline task."
896 (let* ((keywords (org-element--collect-affiliated-keywords))
897 (begin (car keywords
))
898 (components (org-heading-components))
899 (todo (nth 2 components
))
901 (if (member todo org-done-keywords
) 'done
'todo
)))
902 (tags (let ((raw-tags (nth 5 components
)))
903 (and raw-tags
(org-split-string raw-tags
":"))))
904 (raw-value (or (nth 4 components
) ""))
905 ;; Normalize property names: ":SOME_PROP:" becomes
907 (standard-props (let (plist)
910 (let ((p-name (downcase (car p
))))
911 (while (string-match "_" p-name
)
913 (replace-match "-" nil nil p-name
)))
914 (setq p-name
(intern (concat ":" p-name
)))
916 (plist-put plist p-name
(cdr p
)))))
917 (org-entry-properties nil
'standard
))
919 (time-props (org-entry-properties nil
'special
"CLOCK"))
920 (scheduled (cdr (assoc "SCHEDULED" time-props
)))
921 (deadline (cdr (assoc "DEADLINE" time-props
)))
922 (clock (cdr (assoc "CLOCK" time-props
)))
923 (timestamp (cdr (assoc "TIMESTAMP" time-props
)))
924 (task-end (save-excursion
926 (and (re-search-forward "^\\*+ END" limit t
)
927 (match-beginning 0))))
928 (contents-begin (progn (forward-line)
929 (and task-end
(< (point) task-end
) (point))))
930 (hidden (and contents-begin
(org-invisible-p2)))
931 (contents-end (and contents-begin task-end
))
932 (before-blank (if (not task-end
) (point)
936 (end (progn (skip-chars-forward " \r\t\n" limit
)
937 (skip-chars-backward " \t")
938 (if (bolp) (point) (line-end-position))))
942 (list :raw-value raw-value
946 :contents-begin contents-begin
947 :contents-end contents-end
948 :level
(nth 1 components
)
949 :priority
(nth 3 components
)
957 :post-blank
(count-lines before-blank end
))
960 (org-element-put-property
962 (if raw-secondary-p raw-value
963 (org-element-parse-secondary-string
965 (org-element-restriction 'inlinetask
)
968 (defun org-element-inlinetask-interpreter (inlinetask contents
)
969 "Interpret INLINETASK element as Org syntax.
970 CONTENTS is the contents of inlinetask."
971 (let* ((level (org-element-property :level inlinetask
))
972 (todo (org-element-property :todo-keyword inlinetask
))
973 (priority (org-element-property :priority inlinetask
))
974 (title (org-element-interpret-data
975 (org-element-property :title inlinetask
)))
976 (tags (let ((tag-list (org-element-property :tags inlinetask
)))
978 (format ":%s:" (mapconcat 'identity tag-list
":")))))
979 (task (concat (make-string level ?
*)
980 (and todo
(concat " " todo
))
982 (format " [#%s]" (char-to-string priority
)))
983 (and title
(concat " " title
)))))
988 ((zerop org-tags-column
) (format " %s" tags
))
989 ((< org-tags-column
0)
992 (max (- (+ org-tags-column
(length task
) (length tags
))) 1)
997 (make-string (max (- org-tags-column
(length task
)) 1) ?
)
999 ;; Prefer degenerate inlinetasks when there are no
1004 (make-string level ?
*) " END")))))
1009 (defun org-element-item-parser (limit struct
&optional raw-secondary-p
)
1012 STRUCT is the structure of the plain list.
1014 Return a list whose CAR is `item' and CDR is a plist containing
1015 `:bullet', `:begin', `:end', `:contents-begin', `:contents-end',
1016 `:checkbox', `:counter', `:tag', `:structure', `:hiddenp' and
1017 `:post-blank' keywords.
1019 When optional argument RAW-SECONDARY-P is non-nil, item's tag, if
1020 any, will not be parsed as a secondary string, but as a plain
1023 Assume point is at the beginning of the item."
1026 (looking-at org-list-full-item-re
)
1027 (let* ((begin (point))
1028 (bullet (org-match-string-no-properties 1))
1029 (checkbox (let ((box (org-match-string-no-properties 3)))
1030 (cond ((equal "[ ]" box
) 'off
)
1031 ((equal "[X]" box
) 'on
)
1032 ((equal "[-]" box
) 'trans
))))
1033 (counter (let ((c (org-match-string-no-properties 2)))
1037 ((string-match "[A-Za-z]" c
)
1038 (- (string-to-char (upcase (match-string 0 c
)))
1040 ((string-match "[0-9]+" c
)
1041 (string-to-number (match-string 0 c
)))))))
1042 (end (save-excursion (goto-char (org-list-get-item-end begin struct
))
1043 (unless (bolp) (forward-line))
1047 ;; Ignore tags in un-ordered lists: they are just
1048 ;; a part of item's body.
1049 (if (and (match-beginning 4)
1050 (save-match-data (string-match "[.)]" bullet
)))
1053 (skip-chars-forward " \r\t\n" limit
)
1054 ;; If first line isn't empty, contents really start
1055 ;; at the text after item's meta-data.
1056 (if (= (point-at-bol) begin
) (point) (point-at-bol))))
1057 (hidden (progn (forward-line)
1058 (and (not (= (point) end
)) (org-invisible-p2))))
1059 (contents-end (progn (goto-char end
)
1060 (skip-chars-backward " \r\t\n")
1065 (list :bullet bullet
1068 ;; CONTENTS-BEGIN and CONTENTS-END may be
1069 ;; mixed up in the case of an empty item
1070 ;; separated from the next by a blank line.
1071 ;; Thus ensure the former is always the
1073 :contents-begin
(min contents-begin contents-end
)
1074 :contents-end
(max contents-begin contents-end
)
1079 :post-blank
(count-lines contents-end end
)))))
1080 (org-element-put-property
1082 (let ((raw-tag (org-list-get-tag begin struct
)))
1084 (if raw-secondary-p raw-tag
1085 (org-element-parse-secondary-string
1086 raw-tag
(org-element-restriction 'item
) item
))))))))
1088 (defun org-element-item-interpreter (item contents
)
1089 "Interpret ITEM element as Org syntax.
1090 CONTENTS is the contents of the element."
1091 (let* ((bullet (org-list-bullet-string (org-element-property :bullet item
)))
1092 (checkbox (org-element-property :checkbox item
))
1093 (counter (org-element-property :counter item
))
1094 (tag (let ((tag (org-element-property :tag item
)))
1095 (and tag
(org-element-interpret-data tag
))))
1096 ;; Compute indentation.
1097 (ind (make-string (length bullet
) 32))
1098 (item-starts-with-par-p
1099 (eq (org-element-type (car (org-element-contents item
)))
1104 (and counter
(format "[@%d] " counter
))
1109 (and tag
(format "%s :: " tag
))
1110 (let ((contents (replace-regexp-in-string
1111 "\\(^\\)[ \t]*\\S-" ind contents nil nil
1)))
1112 (if item-starts-with-par-p
(org-trim contents
)
1113 (concat "\n" contents
))))))
1118 (defun org-element-plain-list-parser (limit affiliated structure
)
1119 "Parse a plain list.
1121 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1122 the buffer position at the beginning of the first affiliated
1123 keyword and CDR is a plist of affiliated keywords along with
1124 their value. STRUCTURE is the structure of the plain list being
1127 Return a list whose CAR is `plain-list' and CDR is a plist
1128 containing `:type', `:begin', `:end', `:contents-begin' and
1129 `:contents-end', `:structure' and `:post-blank' keywords.
1131 Assume point is at the beginning of the list."
1133 (let* ((struct (or structure
(org-list-struct)))
1134 (prevs (org-list-prevs-alist struct
))
1135 (parents (org-list-parents-alist struct
))
1136 (type (org-list-get-list-type (point) struct prevs
))
1137 (contents-begin (point))
1138 (begin (car affiliated
))
1140 (progn (goto-char (org-list-get-list-end (point) struct prevs
))
1141 (unless (bolp) (forward-line))
1143 (end (progn (skip-chars-forward " \r\t\n" limit
)
1144 (skip-chars-backward " \t")
1145 (if (bolp) (point) (line-end-position)))))
1152 :contents-begin contents-begin
1153 :contents-end contents-end
1155 :post-blank
(count-lines contents-end end
))
1156 (cdr affiliated
))))))
1158 (defun org-element-plain-list-interpreter (plain-list contents
)
1159 "Interpret PLAIN-LIST element as Org syntax.
1160 CONTENTS is the contents of the element."
1163 (goto-char (point-min))
1168 ;;;; Property Drawer
1170 (defun org-element-property-drawer-parser (limit affiliated
)
1171 "Parse a property drawer.
1173 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1174 the buffer position at the beginning of the first affiliated
1175 keyword and CDR is a plist of affiliated keywords along with
1178 Return a list whose CAR is `property-drawer' and CDR is a plist
1179 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1180 `:contents-end' and `:post-blank' keywords.
1182 Assume point is at the beginning of the property drawer."
1184 (let ((case-fold-search t
))
1185 (if (not (save-excursion
1186 (re-search-forward "^[ \t]*:END:[ \t]*$" limit t
)))
1187 ;; Incomplete drawer: parse it as a paragraph.
1188 (org-element-paragraph-parser limit affiliated
)
1190 (let* ((drawer-end-line (match-beginning 0))
1191 (begin (car affiliated
))
1192 (contents-begin (progn (forward-line)
1193 (and (< (point) drawer-end-line
)
1195 (contents-end (and contents-begin drawer-end-line
))
1196 (hidden (org-invisible-p2))
1197 (pos-before-blank (progn (goto-char drawer-end-line
)
1200 (end (progn (skip-chars-forward " \r\t\n" limit
)
1201 (skip-chars-backward " \t")
1202 (if (bolp) (point) (line-end-position)))))
1203 (list 'property-drawer
1208 :contents-begin contents-begin
1209 :contents-end contents-end
1210 :post-blank
(count-lines pos-before-blank end
))
1211 (cdr affiliated
)))))))))
1213 (defun org-element-property-drawer-interpreter (property-drawer contents
)
1214 "Interpret PROPERTY-DRAWER element as Org syntax.
1215 CONTENTS is the properties within the drawer."
1216 (format ":PROPERTIES:\n%s:END:" contents
))
1221 (defun org-element-quote-block-parser (limit affiliated
)
1222 "Parse a quote block.
1224 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1225 the buffer position at the beginning of the first affiliated
1226 keyword and CDR is a plist of affiliated keywords along with
1229 Return a list whose CAR is `quote-block' and CDR is a plist
1230 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1231 `:contents-end' and `:post-blank' keywords.
1233 Assume point is at the beginning of the block."
1234 (let ((case-fold-search t
))
1235 (if (not (save-excursion
1236 (re-search-forward "^[ \t]*#\\+END_QUOTE[ \t]*$" limit t
)))
1237 ;; Incomplete block: parse it as a paragraph.
1238 (org-element-paragraph-parser limit affiliated
)
1239 (let ((block-end-line (match-beginning 0)))
1241 (let* ((begin (car affiliated
))
1242 ;; Empty blocks have no contents.
1243 (contents-begin (progn (forward-line)
1244 (and (< (point) block-end-line
)
1246 (contents-end (and contents-begin block-end-line
))
1247 (hidden (org-invisible-p2))
1248 (pos-before-blank (progn (goto-char block-end-line
)
1251 (end (progn (skip-chars-forward " \r\t\n" limit
)
1252 (skip-chars-backward " \t")
1253 (if (bolp) (point) (line-end-position)))))
1259 :contents-begin contents-begin
1260 :contents-end contents-end
1261 :post-blank
(count-lines pos-before-blank end
))
1262 (cdr affiliated
)))))))))
1264 (defun org-element-quote-block-interpreter (quote-block contents
)
1265 "Interpret QUOTE-BLOCK element as Org syntax.
1266 CONTENTS is the contents of the element."
1267 (format "#+BEGIN_QUOTE\n%s#+END_QUOTE" contents
))
1272 (defun org-element-section-parser (limit)
1275 LIMIT bounds the search.
1277 Return a list whose CAR is `section' and CDR is a plist
1278 containing `:begin', `:end', `:contents-begin', `contents-end'
1279 and `:post-blank' keywords."
1281 ;; Beginning of section is the beginning of the first non-blank
1282 ;; line after previous headline.
1283 (let ((begin (point))
1284 (end (progn (org-with-limited-levels (outline-next-heading))
1286 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
1292 :contents-begin begin
1293 :contents-end pos-before-blank
1294 :post-blank
(count-lines pos-before-blank end
))))))
1296 (defun org-element-section-interpreter (section contents
)
1297 "Interpret SECTION element as Org syntax.
1298 CONTENTS is the contents of the element."
1304 (defun org-element-special-block-parser (limit affiliated
)
1305 "Parse a special block.
1307 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1308 the buffer position at the beginning of the first affiliated
1309 keyword and CDR is a plist of affiliated keywords along with
1312 Return a list whose CAR is `special-block' and CDR is a plist
1313 containing `:type', `:begin', `:end', `:hiddenp',
1314 `:contents-begin', `:contents-end' and `:post-blank' keywords.
1316 Assume point is at the beginning of the block."
1317 (let* ((case-fold-search t
)
1318 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(S-+\\)")
1319 (upcase (match-string-no-properties 1)))))
1320 (if (not (save-excursion
1322 (format "^[ \t]*#\\+END_%s[ \t]*$" type
) limit t
)))
1323 ;; Incomplete block: parse it as a paragraph.
1324 (org-element-paragraph-parser limit affiliated
)
1325 (let ((block-end-line (match-beginning 0)))
1327 (let* ((begin (car affiliated
))
1328 ;; Empty blocks have no contents.
1329 (contents-begin (progn (forward-line)
1330 (and (< (point) block-end-line
)
1332 (contents-end (and contents-begin block-end-line
))
1333 (hidden (org-invisible-p2))
1334 (pos-before-blank (progn (goto-char block-end-line
)
1337 (end (progn (skip-chars-forward " \r\t\n" limit
)
1338 (skip-chars-backward " \t")
1339 (if (bolp) (point) (line-end-position)))))
1340 (list 'special-block
1346 :contents-begin contents-begin
1347 :contents-end contents-end
1348 :post-blank
(count-lines pos-before-blank end
))
1349 (cdr affiliated
)))))))))
1351 (defun org-element-special-block-interpreter (special-block contents
)
1352 "Interpret SPECIAL-BLOCK element as Org syntax.
1353 CONTENTS is the contents of the element."
1354 (let ((block-type (org-element-property :type special-block
)))
1355 (format "#+BEGIN_%s\n%s#+END_%s" block-type contents block-type
)))
1361 ;; For each element, a parser and an interpreter are also defined.
1362 ;; Both follow the same naming convention used for greater elements.
1364 ;; Also, as for greater elements, adding a new element type is done
1365 ;; through the following steps: implement a parser and an interpreter,
1366 ;; tweak `org-element--current-element' so that it recognizes the new
1367 ;; type and add that new type to `org-element-all-elements'.
1369 ;; As a special case, when the newly defined type is a block type,
1370 ;; `org-element-block-name-alist' has to be modified accordingly.
1375 (defun org-element-babel-call-parser (limit affiliated
)
1376 "Parse a babel call.
1378 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1379 the buffer position at the beginning of the first affiliated
1380 keyword and CDR is a plist of affiliated keywords along with
1383 Return a list whose CAR is `babel-call' and CDR is a plist
1384 containing `:begin', `:end', `:info' and `:post-blank' as
1387 (let ((case-fold-search t
)
1388 (info (progn (looking-at org-babel-block-lob-one-liner-regexp
)
1389 (org-babel-lob-get-info)))
1390 (begin (car affiliated
))
1391 (pos-before-blank (progn (forward-line) (point)))
1392 (end (progn (skip-chars-forward " \r\t\n" limit
)
1393 (skip-chars-backward " \t")
1394 (if (bolp) (point) (line-end-position)))))
1400 :post-blank
(count-lines pos-before-blank end
))
1401 (cdr affiliated
))))))
1403 (defun org-element-babel-call-interpreter (babel-call contents
)
1404 "Interpret BABEL-CALL element as Org syntax.
1406 (let* ((babel-info (org-element-property :info babel-call
))
1407 (main (car babel-info
))
1408 (post-options (nth 1 babel-info
)))
1410 (if (not (string-match "\\[\\(\\[.*?\\]\\)\\]" main
)) main
1411 ;; Remove redundant square brackets.
1412 (replace-match (match-string 1 main
) nil nil main
))
1413 (and post-options
(format "[%s]" post-options
)))))
1418 (defun org-element-clock-parser (limit)
1421 LIMIT bounds the search.
1423 Return a list whose CAR is `clock' and CDR is a plist containing
1424 `:status', `:value', `:time', `:begin', `:end' and `:post-blank'
1427 (let* ((case-fold-search nil
)
1429 (value (progn (search-forward org-clock-string
(line-end-position) t
)
1430 (org-skip-whitespace)
1431 (looking-at "\\[.*\\]")
1432 (org-match-string-no-properties 0)))
1433 (time (and (progn (goto-char (match-end 0))
1434 (looking-at " +=> +\\(\\S-+\\)[ \t]*$"))
1435 (org-match-string-no-properties 1)))
1436 (status (if time
'closed
'running
))
1437 (post-blank (let ((before-blank (progn (forward-line) (point))))
1438 (skip-chars-forward " \r\t\n" limit
)
1439 (skip-chars-backward " \t")
1440 (unless (bolp) (end-of-line))
1441 (count-lines before-blank
(point))))
1444 (list :status status
1449 :post-blank post-blank
)))))
1451 (defun org-element-clock-interpreter (clock contents
)
1452 "Interpret CLOCK element as Org syntax.
1454 (concat org-clock-string
" "
1455 (org-element-property :value clock
)
1456 (let ((time (org-element-property :time clock
)))
1461 (org-split-string time
":")))))))
1466 (defun org-element-comment-parser (limit affiliated
)
1469 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1470 the buffer position at the beginning of the first affiliated
1471 keyword and CDR is a plist of affiliated keywords along with
1474 Return a list whose CAR is `comment' and CDR is a plist
1475 containing `:begin', `:end', `:value' and `:post-blank'
1478 Assume point is at comment beginning."
1480 (let* ((begin (car affiliated
))
1481 (value (prog2 (looking-at "[ \t]*# ?")
1482 (buffer-substring-no-properties
1483 (match-end 0) (line-end-position))
1486 ;; Get comments ending.
1488 (while (and (< (point) limit
) (looking-at "[ \t]*#\\( \\|$\\)"))
1489 ;; Accumulate lines without leading hash and first
1494 (buffer-substring-no-properties
1495 (match-end 0) (line-end-position))))
1498 (end (progn (goto-char com-end
)
1499 (skip-chars-forward " \r\t\n" limit
)
1500 (skip-chars-backward " \t")
1501 (if (bolp) (point) (line-end-position)))))
1507 :post-blank
(count-lines com-end end
))
1508 (cdr affiliated
))))))
1510 (defun org-element-comment-interpreter (comment contents
)
1511 "Interpret COMMENT element as Org syntax.
1513 (replace-regexp-in-string "^" "# " (org-element-property :value comment
)))
1518 (defun org-element-comment-block-parser (limit affiliated
)
1519 "Parse an export block.
1521 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1522 the buffer position at the beginning of the first affiliated
1523 keyword and CDR is a plist of affiliated keywords along with
1526 Return a list whose CAR is `comment-block' and CDR is a plist
1527 containing `:begin', `:end', `:hiddenp', `:value' and
1528 `:post-blank' keywords.
1530 Assume point is at comment block beginning."
1531 (let ((case-fold-search t
))
1532 (if (not (save-excursion
1533 (re-search-forward "^[ \t]*#\\+END_COMMENT[ \t]*$" limit t
)))
1534 ;; Incomplete block: parse it as a paragraph.
1535 (org-element-paragraph-parser limit affiliated
)
1536 (let ((contents-end (match-beginning 0)))
1538 (let* ((begin (car affiliated
))
1539 (contents-begin (progn (forward-line) (point)))
1540 (hidden (org-invisible-p2))
1541 (pos-before-blank (progn (goto-char contents-end
)
1544 (end (progn (skip-chars-forward " \r\t\n" limit
)
1545 (skip-chars-backward " \t")
1546 (if (bolp) (point) (line-end-position))))
1547 (value (buffer-substring-no-properties
1548 contents-begin contents-end
)))
1549 (list 'comment-block
1555 :post-blank
(count-lines pos-before-blank end
))
1556 (cdr affiliated
)))))))))
1558 (defun org-element-comment-block-interpreter (comment-block contents
)
1559 "Interpret COMMENT-BLOCK element as Org syntax.
1561 (format "#+BEGIN_COMMENT\n%s#+END_COMMENT"
1562 (org-remove-indentation (org-element-property :value comment-block
))))
1567 (defun org-element-diary-sexp-parser (limit affiliated
)
1568 "Parse a diary sexp.
1570 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1571 the buffer position at the beginning of the first affiliated
1572 keyword and CDR is a plist of affiliated keywords along with
1575 Return a list whose CAR is `diary-sexp' and CDR is a plist
1576 containing `:begin', `:end', `:value' and `:post-blank'
1579 (let ((begin (car affiliated
))
1580 (value (progn (looking-at "\\(%%(.*\\)[ \t]*$")
1581 (org-match-string-no-properties 1)))
1582 (pos-before-blank (progn (forward-line) (point)))
1583 (end (progn (skip-chars-forward " \r\t\n" limit
)
1584 (skip-chars-backward " \t")
1585 (if (bolp) (point) (line-end-position)))))
1591 :post-blank
(count-lines pos-before-blank end
))
1592 (cdr affiliated
))))))
1594 (defun org-element-diary-sexp-interpreter (diary-sexp contents
)
1595 "Interpret DIARY-SEXP as Org syntax.
1597 (org-element-property :value diary-sexp
))
1602 (defun org-element-example-block-parser (limit affiliated
)
1603 "Parse an example block.
1605 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1606 the buffer position at the beginning of the first affiliated
1607 keyword and CDR is a plist of affiliated keywords along with
1610 Return a list whose CAR is `example-block' and CDR is a plist
1611 containing `:begin', `:end', `:number-lines', `:preserve-indent',
1612 `:retain-labels', `:use-labels', `:label-fmt', `:hiddenp',
1613 `:switches', `:value' and `:post-blank' keywords."
1614 (let ((case-fold-search t
))
1615 (if (not (save-excursion
1616 (re-search-forward "^[ \t]*#\\+END_EXAMPLE[ \t]*$" limit t
)))
1617 ;; Incomplete block: parse it as a paragraph.
1618 (org-element-paragraph-parser limit affiliated
)
1619 (let ((contents-end (match-beginning 0)))
1622 (progn (looking-at "^[ \t]*#\\+BEGIN_EXAMPLE\\(?: +\\(.*\\)\\)?")
1623 (org-match-string-no-properties 1)))
1624 ;; Switches analysis
1625 (number-lines (cond ((not switches
) nil
)
1626 ((string-match "-n\\>" switches
) 'new
)
1627 ((string-match "+n\\>" switches
) 'continued
)))
1628 (preserve-indent (and switches
(string-match "-i\\>" switches
)))
1629 ;; Should labels be retained in (or stripped from) example
1633 (not (string-match "-r\\>" switches
))
1634 (and number-lines
(string-match "-k\\>" switches
))))
1635 ;; What should code-references use - labels or
1639 (and retain-labels
(not (string-match "-k\\>" switches
)))))
1640 (label-fmt (and switches
1641 (string-match "-l +\"\\([^\"\n]+\\)\"" switches
)
1642 (match-string 1 switches
)))
1643 ;; Standard block parsing.
1644 (begin (car affiliated
))
1645 (contents-begin (progn (forward-line) (point)))
1646 (hidden (org-invisible-p2))
1647 (value (org-unescape-code-in-string
1648 (buffer-substring-no-properties
1649 contents-begin contents-end
)))
1650 (pos-before-blank (progn (goto-char contents-end
)
1653 (end (progn (skip-chars-forward " \r\t\n" limit
)
1654 (skip-chars-backward " \t")
1655 (if (bolp) (point) (line-end-position)))))
1656 (list 'example-block
1662 :number-lines number-lines
1663 :preserve-indent preserve-indent
1664 :retain-labels retain-labels
1665 :use-labels use-labels
1666 :label-fmt label-fmt
1668 :post-blank
(count-lines pos-before-blank end
))
1669 (cdr affiliated
)))))))))
1671 (defun org-element-example-block-interpreter (example-block contents
)
1672 "Interpret EXAMPLE-BLOCK element as Org syntax.
1674 (let ((switches (org-element-property :switches example-block
)))
1675 (concat "#+BEGIN_EXAMPLE" (and switches
(concat " " switches
)) "\n"
1676 (org-remove-indentation
1677 (org-escape-code-in-string
1678 (org-element-property :value example-block
)))
1684 (defun org-element-export-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 `export-block' and CDR is a plist
1693 containing `:begin', `:end', `:type', `:hiddenp', `:value' and
1694 `:post-blank' keywords.
1696 Assume point is at export-block beginning."
1697 (let* ((case-fold-search t
)
1698 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
1699 (upcase (org-match-string-no-properties 1)))))
1700 (if (not (save-excursion
1702 (format "^[ \t]*#\\+END_%s[ \t]*$" type
) limit t
)))
1703 ;; Incomplete block: parse it as a paragraph.
1704 (org-element-paragraph-parser limit affiliated
)
1705 (let ((contents-end (match-beginning 0)))
1707 (let* ((begin (car affiliated
))
1708 (contents-begin (progn (forward-line) (point)))
1709 (hidden (org-invisible-p2))
1710 (pos-before-blank (progn (goto-char contents-end
)
1713 (end (progn (skip-chars-forward " \r\t\n" limit
)
1714 (skip-chars-backward " \t")
1715 (if (bolp) (point) (line-end-position))))
1716 (value (buffer-substring-no-properties contents-begin
1725 :post-blank
(count-lines pos-before-blank end
))
1726 (cdr affiliated
)))))))))
1728 (defun org-element-export-block-interpreter (export-block contents
)
1729 "Interpret EXPORT-BLOCK element as Org syntax.
1731 (let ((type (org-element-property :type export-block
)))
1732 (concat (format "#+BEGIN_%s\n" type
)
1733 (org-element-property :value export-block
)
1734 (format "#+END_%s" type
))))
1739 (defun org-element-fixed-width-parser (limit affiliated
)
1740 "Parse a fixed-width section.
1742 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1743 the buffer position at the beginning of the first affiliated
1744 keyword and CDR is a plist of affiliated keywords along with
1747 Return a list whose CAR is `fixed-width' and CDR is a plist
1748 containing `:begin', `:end', `:value' and `:post-blank' keywords.
1750 Assume point is at the beginning of the fixed-width area."
1752 (let* ((begin (car affiliated
))
1756 (while (and (< (point) limit
)
1757 (looking-at "[ \t]*:\\( \\|$\\)"))
1758 ;; Accumulate text without starting colons.
1761 (buffer-substring-no-properties
1762 (match-end 0) (point-at-eol))
1766 (end (progn (skip-chars-forward " \r\t\n" limit
)
1767 (skip-chars-backward " \t")
1768 (if (bolp) (point) (line-end-position)))))
1774 :post-blank
(count-lines end-area end
))
1775 (cdr affiliated
))))))
1777 (defun org-element-fixed-width-interpreter (fixed-width contents
)
1778 "Interpret FIXED-WIDTH element as Org syntax.
1780 (replace-regexp-in-string
1781 "^" ": " (substring (org-element-property :value fixed-width
) 0 -
1)))
1784 ;;;; Horizontal Rule
1786 (defun org-element-horizontal-rule-parser (limit affiliated
)
1787 "Parse an horizontal rule.
1789 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1790 the buffer position at the beginning of the first affiliated
1791 keyword and CDR is a plist of affiliated keywords along with
1794 Return a list whose CAR is `horizontal-rule' and CDR is a plist
1795 containing `:begin', `:end' and `:post-blank' keywords."
1797 (let ((begin (car affiliated
))
1798 (post-hr (progn (forward-line) (point)))
1799 (end (progn (skip-chars-forward " \r\t\n" limit
)
1800 (skip-chars-backward " \t")
1801 (if (bolp) (point) (line-end-position)))))
1802 (list 'horizontal-rule
1806 :post-blank
(count-lines post-hr end
))
1807 (cdr affiliated
))))))
1809 (defun org-element-horizontal-rule-interpreter (horizontal-rule contents
)
1810 "Interpret HORIZONTAL-RULE element as Org syntax.
1817 (defun org-element-keyword-parser (limit affiliated
)
1818 "Parse a keyword at point.
1820 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1821 the buffer position at the beginning of the first affiliated
1822 keyword and CDR is a plist of affiliated keywords along with
1825 Return a list whose CAR is `keyword' and CDR is a plist
1826 containing `:key', `:value', `:begin', `:end' and `:post-blank'
1829 (let ((begin (car affiliated
))
1830 (key (progn (looking-at "[ \t]*#\\+\\(\\S-+*\\):")
1831 (upcase (org-match-string-no-properties 1))))
1832 (value (org-trim (buffer-substring-no-properties
1833 (match-end 0) (point-at-eol))))
1834 (pos-before-blank (progn (forward-line) (point)))
1835 (end (progn (skip-chars-forward " \r\t\n" limit
)
1836 (skip-chars-backward " \t")
1837 (if (bolp) (point) (line-end-position)))))
1844 :post-blank
(count-lines pos-before-blank end
))
1845 (cdr affiliated
))))))
1847 (defun org-element-keyword-interpreter (keyword contents
)
1848 "Interpret KEYWORD element as Org syntax.
1851 (org-element-property :key keyword
)
1852 (org-element-property :value keyword
)))
1855 ;;;; Latex Environment
1857 (defun org-element-latex-environment-parser (limit affiliated
)
1858 "Parse a LaTeX environment.
1860 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1861 the buffer position at the beginning of the first affiliated
1862 keyword and CDR is a plist of affiliated keywords along with
1865 Return a list whose CAR is `latex-environment' and CDR is a plist
1866 containing `:begin', `:end', `:value' and `:post-blank'
1869 Assume point is at the beginning of the latex environment."
1871 (let ((case-fold-search t
)
1872 (code-begin (point)))
1873 (looking-at "[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}")
1874 (if (not (re-search-forward (format "^[ \t]*\\\\end{%s}[ \t]*$"
1875 (regexp-quote (match-string 1)))
1877 ;; Incomplete latex environment: parse it as a paragraph.
1878 (org-element-paragraph-parser limit affiliated
)
1879 (let* ((code-end (progn (forward-line) (point)))
1880 (begin (car affiliated
))
1881 (value (buffer-substring-no-properties code-begin code-end
))
1882 (end (progn (skip-chars-forward " \r\t\n" limit
)
1883 (skip-chars-backward " \t")
1884 (if (bolp) (point) (line-end-position)))))
1885 (list 'latex-environment
1890 :post-blank
(count-lines code-end end
))
1891 (cdr affiliated
))))))))
1893 (defun org-element-latex-environment-interpreter (latex-environment contents
)
1894 "Interpret LATEX-ENVIRONMENT element as Org syntax.
1896 (org-element-property :value latex-environment
))
1901 (defun org-element-node-property-parser (limit)
1902 "Parse a node-property at point.
1904 LIMIT bounds the search.
1906 Return a list whose CAR is `node-property' and CDR is a plist
1907 containing `:key', `:value', `:begin', `:end' and `:post-blank'
1910 (let ((case-fold-search t
)
1912 (key (progn (looking-at "[ \t]*:\\(.*?\\):[ \t]+\\(.*?\\)[ \t]*$")
1913 (org-match-string-no-properties 1)))
1914 (value (org-match-string-no-properties 2))
1915 (pos-before-blank (progn (forward-line) (point)))
1916 (end (progn (skip-chars-forward " \r\t\n" limit
)
1917 (if (eobp) (point) (point-at-bol)))))
1918 (list 'node-property
1923 :post-blank
(count-lines pos-before-blank end
))))))
1925 (defun org-element-node-property-interpreter (node-property contents
)
1926 "Interpret NODE-PROPERTY element as Org syntax.
1928 (format org-property-format
1929 (format ":%s:" (org-element-property :key node-property
))
1930 (org-element-property :value node-property
)))
1935 (defun org-element-paragraph-parser (limit affiliated
)
1938 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1939 the buffer position at the beginning of the first affiliated
1940 keyword and CDR is a plist of affiliated keywords along with
1943 Return a list whose CAR is `paragraph' and CDR is a plist
1944 containing `:begin', `:end', `:contents-begin' and
1945 `:contents-end' and `:post-blank' keywords.
1947 Assume point is at the beginning of the paragraph."
1949 (let* ((begin (car affiliated
))
1950 (contents-begin (point))
1952 (let ((case-fold-search t
))
1954 (if (not (re-search-forward
1955 org-element-paragraph-separate limit
'm
))
1957 ;; A matching `org-element-paragraph-separate' is not
1958 ;; necessarily the end of the paragraph. In
1959 ;; particular, lines starting with # or : as a first
1960 ;; non-space character are ambiguous. We have check
1961 ;; if they are valid Org syntax (i.e. not an
1962 ;; incomplete keyword).
1966 ;; There's no ambiguity for other symbols or
1967 ;; empty lines: stop here.
1968 (looking-at "[ \t]*\\(?:[^:#]\\|$\\)")
1969 ;; Stop at valid fixed-width areas.
1970 (looking-at "[ \t]*:\\(?: \\|$\\)")
1972 (and (looking-at org-drawer-regexp
)
1975 "^[ \t]*:END:[ \t]*$" limit t
)))
1976 ;; Stop at valid comments.
1977 (looking-at "[ \t]*#\\(?: \\|$\\)")
1978 ;; Stop at valid dynamic blocks.
1979 (and (looking-at org-dblock-start-re
)
1982 "^[ \t]*#\\+END:?[ \t]*$" limit t
)))
1983 ;; Stop at valid blocks.
1985 "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
1988 (format "^[ \t]*#\\+END_%s[ \t]*$"
1991 ;; Stop at valid latex environments.
1993 "^[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}[ \t]*$")
1996 (format "^[ \t]*\\\\end{%s}[ \t]*$"
1999 ;; Stop at valid keywords.
2000 (looking-at "[ \t]*#\\+\\S-+:")
2001 ;; Skip everything else.
2005 (re-search-forward org-element-paragraph-separate
2007 (beginning-of-line)))
2008 (if (= (point) limit
) limit
2009 (goto-char (line-beginning-position)))))
2010 (contents-end (progn (skip-chars-backward " \r\t\n" contents-begin
)
2013 (end (progn (skip-chars-forward " \r\t\n" limit
)
2014 (skip-chars-backward " \t")
2015 (if (bolp) (point) (line-end-position)))))
2020 :contents-begin contents-begin
2021 :contents-end contents-end
2022 :post-blank
(count-lines before-blank end
))
2023 (cdr affiliated
))))))
2025 (defun org-element-paragraph-interpreter (paragraph contents
)
2026 "Interpret PARAGRAPH element as Org syntax.
2027 CONTENTS is the contents of the element."
2033 (defun org-element-planning-parser (limit)
2036 LIMIT bounds the search.
2038 Return a list whose CAR is `planning' and CDR is a plist
2039 containing `:closed', `:deadline', `:scheduled', `:begin', `:end'
2040 and `:post-blank' keywords."
2042 (let* ((case-fold-search nil
)
2044 (post-blank (let ((before-blank (progn (forward-line) (point))))
2045 (skip-chars-forward " \r\t\n" limit
)
2046 (skip-chars-backward " \t")
2047 (unless (bolp) (end-of-line))
2048 (count-lines before-blank
(point))))
2050 closed deadline scheduled
)
2052 (while (re-search-forward org-keyword-time-not-clock-regexp
2053 (line-end-position) t
)
2054 (goto-char (match-end 1))
2055 (org-skip-whitespace)
2056 (let ((time (buffer-substring-no-properties
2057 (1+ (point)) (1- (match-end 0))))
2058 (keyword (match-string 1)))
2059 (cond ((equal keyword org-closed-string
) (setq closed time
))
2060 ((equal keyword org-deadline-string
) (setq deadline time
))
2061 (t (setq scheduled time
)))))
2063 (list :closed closed
2065 :scheduled scheduled
2068 :post-blank post-blank
)))))
2070 (defun org-element-planning-interpreter (planning contents
)
2071 "Interpret PLANNING element as Org syntax.
2076 (list (let ((closed (org-element-property :closed planning
)))
2077 (when closed
(concat org-closed-string
" [" closed
"]")))
2078 (let ((deadline (org-element-property :deadline planning
)))
2079 (when deadline
(concat org-deadline-string
" <" deadline
">")))
2080 (let ((scheduled (org-element-property :scheduled planning
)))
2082 (concat org-scheduled-string
" <" scheduled
">")))))
2088 (defun org-element-quote-section-parser (limit)
2089 "Parse a quote section.
2091 LIMIT bounds the search.
2093 Return a list whose CAR is `quote-section' and CDR is a plist
2094 containing `:begin', `:end', `:value' and `:post-blank' keywords.
2096 Assume point is at beginning of the section."
2098 (let* ((begin (point))
2099 (end (progn (org-with-limited-levels (outline-next-heading))
2101 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
2104 (value (buffer-substring-no-properties begin pos-before-blank
)))
2105 (list 'quote-section
2109 :post-blank
(count-lines pos-before-blank end
))))))
2111 (defun org-element-quote-section-interpreter (quote-section contents
)
2112 "Interpret QUOTE-SECTION element as Org syntax.
2114 (org-element-property :value quote-section
))
2119 (defun org-element-src-block-parser (limit affiliated
)
2122 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2123 the buffer position at the beginning of the first affiliated
2124 keyword and CDR is a plist of affiliated keywords along with
2127 Return a list whose CAR is `src-block' and CDR is a plist
2128 containing `:language', `:switches', `:parameters', `:begin',
2129 `:end', `:hiddenp', `:number-lines', `:retain-labels',
2130 `:use-labels', `:label-fmt', `:preserve-indent', `:value' and
2131 `:post-blank' keywords.
2133 Assume point is at the beginning of the block."
2134 (let ((case-fold-search t
))
2135 (if (not (save-excursion (re-search-forward "^[ \t]*#\\+END_SRC[ \t]*$"
2137 ;; Incomplete block: parse it as a paragraph.
2138 (org-element-paragraph-parser limit affiliated
)
2139 (let ((contents-end (match-beginning 0)))
2141 (let* ((begin (car affiliated
))
2142 ;; Get language as a string.
2146 (concat "^[ \t]*#\\+BEGIN_SRC"
2147 "\\(?: +\\(\\S-+\\)\\)?"
2148 "\\(\\(?: +\\(?:-l \".*?\"\\|[-+][A-Za-z]\\)\\)+\\)?"
2150 (org-match-string-no-properties 1)))
2152 (switches (org-match-string-no-properties 2))
2154 (parameters (org-match-string-no-properties 3))
2155 ;; Switches analysis
2156 (number-lines (cond ((not switches
) nil
)
2157 ((string-match "-n\\>" switches
) 'new
)
2158 ((string-match "+n\\>" switches
) 'continued
)))
2159 (preserve-indent (and switches
(string-match "-i\\>" switches
)))
2160 (label-fmt (and switches
2161 (string-match "-l +\"\\([^\"\n]+\\)\"" switches
)
2162 (match-string 1 switches
)))
2163 ;; Should labels be retained in (or stripped from)
2167 (not (string-match "-r\\>" switches
))
2168 (and number-lines
(string-match "-k\\>" switches
))))
2169 ;; What should code-references use - labels or
2173 (and retain-labels
(not (string-match "-k\\>" switches
)))))
2174 ;; Get visibility status.
2175 (hidden (progn (forward-line) (org-invisible-p2)))
2177 (value (org-unescape-code-in-string
2178 (buffer-substring-no-properties (point) contents-end
)))
2179 (pos-before-blank (progn (goto-char contents-end
)
2182 ;; Get position after ending blank lines.
2183 (end (progn (skip-chars-forward " \r\t\n" limit
)
2184 (skip-chars-backward " \t")
2185 (if (bolp) (point) (line-end-position)))))
2188 (list :language language
2189 :switches
(and (org-string-nw-p switches
)
2190 (org-trim switches
))
2191 :parameters
(and (org-string-nw-p parameters
)
2192 (org-trim parameters
))
2195 :number-lines number-lines
2196 :preserve-indent preserve-indent
2197 :retain-labels retain-labels
2198 :use-labels use-labels
2199 :label-fmt label-fmt
2202 :post-blank
(count-lines pos-before-blank end
))
2203 (cdr affiliated
)))))))))
2205 (defun org-element-src-block-interpreter (src-block contents
)
2206 "Interpret SRC-BLOCK element as Org syntax.
2208 (let ((lang (org-element-property :language src-block
))
2209 (switches (org-element-property :switches src-block
))
2210 (params (org-element-property :parameters src-block
))
2211 (value (let ((val (org-element-property :value src-block
)))
2213 (org-src-preserve-indentation val
)
2214 ((zerop org-edit-src-content-indentation
)
2215 (org-remove-indentation val
))
2217 (let ((ind (make-string
2218 org-edit-src-content-indentation
32)))
2219 (replace-regexp-in-string
2220 "\\(^\\)[ \t]*\\S-" ind
2221 (org-remove-indentation val
) nil nil
1)))))))
2222 (concat (format "#+BEGIN_SRC%s\n"
2223 (concat (and lang
(concat " " lang
))
2224 (and switches
(concat " " switches
))
2225 (and params
(concat " " params
))))
2226 (org-escape-code-in-string value
)
2232 (defun org-element-table-parser (limit affiliated
)
2233 "Parse a table at point.
2235 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2236 the buffer position at the beginning of the first affiliated
2237 keyword and CDR is a plist of affiliated keywords along with
2240 Return a list whose CAR is `table' and CDR is a plist containing
2241 `:begin', `:end', `:tblfm', `:type', `:contents-begin',
2242 `:contents-end', `:value' and `:post-blank' keywords.
2244 Assume point is at the beginning of the table."
2246 (let* ((case-fold-search t
)
2247 (table-begin (point))
2248 (type (if (org-at-table.el-p
) 'table.el
'org
))
2249 (begin (car affiliated
))
2251 (if (re-search-forward org-table-any-border-regexp limit
'm
)
2252 (goto-char (match-beginning 0))
2255 (while (looking-at "[ \t]*#\\+TBLFM: +\\(.*\\)[ \t]*$")
2256 (push (org-match-string-no-properties 1) acc
)
2259 (pos-before-blank (point))
2260 (end (progn (skip-chars-forward " \r\t\n" limit
)
2261 (skip-chars-backward " \t")
2262 (if (bolp) (point) (line-end-position)))))
2269 ;; Only `org' tables have contents. `table.el' tables
2270 ;; use a `:value' property to store raw table as
2272 :contents-begin
(and (eq type
'org
) table-begin
)
2273 :contents-end
(and (eq type
'org
) table-end
)
2274 :value
(and (eq type
'table.el
)
2275 (buffer-substring-no-properties
2276 table-begin table-end
))
2277 :post-blank
(count-lines pos-before-blank end
))
2278 (cdr affiliated
))))))
2280 (defun org-element-table-interpreter (table contents
)
2281 "Interpret TABLE element as Org syntax.
2283 (if (eq (org-element-property :type table
) 'table.el
)
2284 (org-remove-indentation (org-element-property :value table
))
2285 (concat (with-temp-buffer (insert contents
)
2288 (mapconcat (lambda (fm) (concat "#+TBLFM: " fm
))
2289 (reverse (org-element-property :tblfm table
))
2295 (defun org-element-table-row-parser (limit)
2296 "Parse table row at point.
2298 LIMIT bounds the search.
2300 Return a list whose CAR is `table-row' and CDR is a plist
2301 containing `:begin', `:end', `:contents-begin', `:contents-end',
2302 `:type' and `:post-blank' keywords."
2304 (let* ((type (if (looking-at "^[ \t]*|-") 'rule
'standard
))
2306 ;; A table rule has no contents. In that case, ensure
2307 ;; CONTENTS-BEGIN matches CONTENTS-END.
2308 (contents-begin (and (eq type
'standard
)
2309 (search-forward "|")
2311 (contents-end (and (eq type
'standard
)
2314 (skip-chars-backward " \t")
2316 (end (progn (forward-line) (point))))
2321 :contents-begin contents-begin
2322 :contents-end contents-end
2325 (defun org-element-table-row-interpreter (table-row contents
)
2326 "Interpret TABLE-ROW element as Org syntax.
2327 CONTENTS is the contents of the table row."
2328 (if (eq (org-element-property :type table-row
) 'rule
) "|-"
2329 (concat "| " contents
)))
2334 (defun org-element-verse-block-parser (limit affiliated
)
2335 "Parse a verse block.
2337 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2338 the buffer position at the beginning of the first affiliated
2339 keyword and CDR is a plist of affiliated keywords along with
2342 Return a list whose CAR is `verse-block' and CDR is a plist
2343 containing `:begin', `:end', `:contents-begin', `:contents-end',
2344 `:hiddenp' and `:post-blank' keywords.
2346 Assume point is at beginning of the block."
2347 (let ((case-fold-search t
))
2348 (if (not (save-excursion
2349 (re-search-forward "^[ \t]*#\\+END_VERSE[ \t]*$" limit t
)))
2350 ;; Incomplete block: parse it as a paragraph.
2351 (org-element-paragraph-parser limit affiliated
)
2352 (let ((contents-end (match-beginning 0)))
2354 (let* ((begin (car affiliated
))
2355 (hidden (progn (forward-line) (org-invisible-p2)))
2356 (contents-begin (point))
2357 (pos-before-blank (progn (goto-char contents-end
)
2360 (end (progn (skip-chars-forward " \r\t\n" limit
)
2361 (skip-chars-backward " \t")
2362 (if (bolp) (point) (line-end-position)))))
2367 :contents-begin contents-begin
2368 :contents-end contents-end
2370 :post-blank
(count-lines pos-before-blank end
))
2371 (cdr affiliated
)))))))))
2373 (defun org-element-verse-block-interpreter (verse-block contents
)
2374 "Interpret VERSE-BLOCK element as Org syntax.
2375 CONTENTS is verse block contents."
2376 (format "#+BEGIN_VERSE\n%s#+END_VERSE" contents
))
2382 ;; Unlike to elements, interstices can be found between objects.
2383 ;; That's why, along with the parser, successor functions are provided
2384 ;; for each object. Some objects share the same successor (i.e. `code'
2385 ;; and `verbatim' objects).
2387 ;; A successor must accept a single argument bounding the search. It
2388 ;; will return either a cons cell whose CAR is the object's type, as
2389 ;; a symbol, and CDR the position of its next occurrence, or nil.
2391 ;; Successors follow the naming convention:
2392 ;; org-element-NAME-successor, where NAME is the name of the
2393 ;; successor, as defined in `org-element-all-successors'.
2395 ;; Some object types (i.e. `italic') are recursive. Restrictions on
2396 ;; object types they can contain will be specified in
2397 ;; `org-element-object-restrictions'.
2399 ;; Adding a new type of object is simple. Implement a successor,
2400 ;; a parser, and an interpreter for it, all following the naming
2401 ;; convention. Register type in `org-element-all-objects' and
2402 ;; successor in `org-element-all-successors'. Maybe tweak
2403 ;; restrictions about it, and that's it.
2408 (defun org-element-bold-parser ()
2409 "Parse bold object at point.
2411 Return a list whose CAR is `bold' and CDR is a plist with
2412 `:begin', `:end', `:contents-begin' and `:contents-end' and
2413 `:post-blank' keywords.
2415 Assume point is at the first star marker."
2417 (unless (bolp) (backward-char 1))
2418 (looking-at org-emph-re
)
2419 (let ((begin (match-beginning 2))
2420 (contents-begin (match-beginning 4))
2421 (contents-end (match-end 4))
2422 (post-blank (progn (goto-char (match-end 2))
2423 (skip-chars-forward " \t")))
2428 :contents-begin contents-begin
2429 :contents-end contents-end
2430 :post-blank post-blank
)))))
2432 (defun org-element-bold-interpreter (bold contents
)
2433 "Interpret BOLD object as Org syntax.
2434 CONTENTS is the contents of the object."
2435 (format "*%s*" contents
))
2437 (defun org-element-text-markup-successor (limit)
2438 "Search for the next text-markup object.
2440 LIMIT bounds the search.
2442 Return value is a cons cell whose CAR is a symbol among `bold',
2443 `italic', `underline', `strike-through', `code' and `verbatim'
2444 and CDR is beginning position."
2446 (unless (bolp) (backward-char))
2447 (when (re-search-forward org-emph-re limit t
)
2448 (let ((marker (match-string 3)))
2450 ((equal marker
"*") 'bold
)
2451 ((equal marker
"/") 'italic
)
2452 ((equal marker
"_") 'underline
)
2453 ((equal marker
"+") 'strike-through
)
2454 ((equal marker
"~") 'code
)
2455 ((equal marker
"=") 'verbatim
)
2456 (t (error "Unknown marker at %d" (match-beginning 3))))
2457 (match-beginning 2))))))
2462 (defun org-element-code-parser ()
2463 "Parse code object at point.
2465 Return a list whose CAR is `code' and CDR is a plist with
2466 `:value', `:begin', `:end' and `:post-blank' keywords.
2468 Assume point is at the first tilde marker."
2470 (unless (bolp) (backward-char 1))
2471 (looking-at org-emph-re
)
2472 (let ((begin (match-beginning 2))
2473 (value (org-match-string-no-properties 4))
2474 (post-blank (progn (goto-char (match-end 2))
2475 (skip-chars-forward " \t")))
2481 :post-blank post-blank
)))))
2483 (defun org-element-code-interpreter (code contents
)
2484 "Interpret CODE object as Org syntax.
2486 (format "~%s~" (org-element-property :value code
)))
2491 (defun org-element-entity-parser ()
2492 "Parse entity at point.
2494 Return a list whose CAR is `entity' and CDR a plist with
2495 `:begin', `:end', `:latex', `:latex-math-p', `:html', `:latin1',
2496 `:utf-8', `:ascii', `:use-brackets-p' and `:post-blank' as
2499 Assume point is at the beginning of the entity."
2501 (looking-at "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)")
2502 (let* ((value (org-entity-get (match-string 1)))
2503 (begin (match-beginning 0))
2504 (bracketsp (string= (match-string 2) "{}"))
2505 (post-blank (progn (goto-char (match-end 1))
2506 (when bracketsp
(forward-char 2))
2507 (skip-chars-forward " \t")))
2510 (list :name
(car value
)
2511 :latex
(nth 1 value
)
2512 :latex-math-p
(nth 2 value
)
2514 :ascii
(nth 4 value
)
2515 :latin1
(nth 5 value
)
2516 :utf-8
(nth 6 value
)
2519 :use-brackets-p bracketsp
2520 :post-blank post-blank
)))))
2522 (defun org-element-entity-interpreter (entity contents
)
2523 "Interpret ENTITY object as Org syntax.
2526 (org-element-property :name entity
)
2527 (when (org-element-property :use-brackets-p entity
) "{}")))
2529 (defun org-element-latex-or-entity-successor (limit)
2530 "Search for the next latex-fragment or entity object.
2532 LIMIT bounds the search.
2534 Return value is a cons cell whose CAR is `entity' or
2535 `latex-fragment' and CDR is beginning position."
2537 (unless (bolp) (backward-char))
2539 (remove "begin" (plist-get org-format-latex-options
:matchers
)))
2540 ;; ENTITY-RE matches both LaTeX commands and Org entities.
2542 "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))
2543 (when (re-search-forward
2544 (concat (mapconcat (lambda (e) (nth 1 (assoc e org-latex-regexps
)))
2548 (goto-char (match-beginning 0))
2549 (if (looking-at entity-re
)
2550 ;; Determine if it's a real entity or a LaTeX command.
2551 (cons (if (org-entity-get (match-string 1)) 'entity
'latex-fragment
)
2552 (match-beginning 0))
2553 ;; No entity nor command: point is at a LaTeX fragment.
2554 ;; Determine its type to get the correct beginning position.
2555 (cons 'latex-fragment
2558 (when (looking-at (nth 1 (assoc e org-latex-regexps
)))
2561 (nth 2 (assoc e org-latex-regexps
))))))
2568 (defun org-element-export-snippet-parser ()
2569 "Parse export snippet at point.
2571 Return a list whose CAR is `export-snippet' and CDR a plist with
2572 `:begin', `:end', `:back-end', `:value' and `:post-blank' as
2575 Assume point is at the beginning of the snippet."
2577 (re-search-forward "@@\\([-A-Za-z0-9]+\\):" nil t
)
2578 (let* ((begin (match-beginning 0))
2579 (back-end (org-match-string-no-properties 1))
2580 (value (buffer-substring-no-properties
2582 (progn (re-search-forward "@@" nil t
) (match-beginning 0))))
2583 (post-blank (skip-chars-forward " \t"))
2585 (list 'export-snippet
2586 (list :back-end back-end
2590 :post-blank post-blank
)))))
2592 (defun org-element-export-snippet-interpreter (export-snippet contents
)
2593 "Interpret EXPORT-SNIPPET object as Org syntax.
2596 (org-element-property :back-end export-snippet
)
2597 (org-element-property :value export-snippet
)))
2599 (defun org-element-export-snippet-successor (limit)
2600 "Search for the next export-snippet object.
2602 LIMIT bounds the search.
2604 Return value is a cons cell whose CAR is `export-snippet' and CDR
2605 its beginning position."
2608 (when (and (re-search-forward "@@[-A-Za-z0-9]+:" limit t
)
2609 (setq beg
(match-beginning 0))
2610 (search-forward "@@" limit t
))
2611 (cons 'export-snippet beg
)))))
2614 ;;;; Footnote Reference
2616 (defun org-element-footnote-reference-parser ()
2617 "Parse footnote reference at point.
2619 Return a list whose CAR is `footnote-reference' and CDR a plist
2620 with `:label', `:type', `:inline-definition', `:begin', `:end'
2621 and `:post-blank' as keywords."
2623 (looking-at org-footnote-re
)
2624 (let* ((begin (point))
2625 (label (or (org-match-string-no-properties 2)
2626 (org-match-string-no-properties 3)
2627 (and (match-string 1)
2628 (concat "fn:" (org-match-string-no-properties 1)))))
2629 (type (if (or (not label
) (match-string 1)) 'inline
'standard
))
2630 (inner-begin (match-end 0))
2634 (while (and (> count
0) (re-search-forward "[][]" nil t
))
2635 (if (equal (match-string 0) "[") (incf count
) (decf count
)))
2637 (post-blank (progn (goto-char (1+ inner-end
))
2638 (skip-chars-forward " \t")))
2641 (list 'footnote-reference
2646 :post-blank post-blank
))))
2647 (org-element-put-property
2648 footnote-reference
:inline-definition
2649 (and (eq type
'inline
)
2650 (org-element-parse-secondary-string
2651 (buffer-substring inner-begin inner-end
)
2652 (org-element-restriction 'footnote-reference
)
2653 footnote-reference
))))))
2655 (defun org-element-footnote-reference-interpreter (footnote-reference contents
)
2656 "Interpret FOOTNOTE-REFERENCE object as Org syntax.
2658 (let ((label (or (org-element-property :label footnote-reference
) "fn:"))
2661 (org-element-property :inline-definition footnote-reference
)))
2662 (if (not inline-def
) ""
2663 (concat ":" (org-element-interpret-data inline-def
))))))
2664 (format "[%s]" (concat label def
))))
2666 (defun org-element-footnote-reference-successor (limit)
2667 "Search for the next footnote-reference object.
2669 LIMIT bounds the search.
2671 Return value is a cons cell whose CAR is `footnote-reference' and
2672 CDR is beginning position."
2675 (while (re-search-forward org-footnote-re limit t
)
2677 (let ((beg (match-beginning 0))
2680 (while (re-search-forward "[][]" limit t
)
2681 (if (equal (match-string 0) "[") (incf count
) (decf count
))
2683 (throw 'exit
(cons 'footnote-reference beg
))))))))))
2686 ;;;; Inline Babel Call
2688 (defun org-element-inline-babel-call-parser ()
2689 "Parse inline babel call at point.
2691 Return a list whose CAR is `inline-babel-call' and CDR a plist
2692 with `:begin', `:end', `:info' and `:post-blank' as keywords.
2694 Assume point is at the beginning of the babel call."
2696 (unless (bolp) (backward-char))
2697 (looking-at org-babel-inline-lob-one-liner-regexp
)
2698 (let ((info (save-match-data (org-babel-lob-get-info)))
2699 (begin (match-end 1))
2700 (post-blank (progn (goto-char (match-end 0))
2701 (skip-chars-forward " \t")))
2703 (list 'inline-babel-call
2707 :post-blank post-blank
)))))
2709 (defun org-element-inline-babel-call-interpreter (inline-babel-call contents
)
2710 "Interpret INLINE-BABEL-CALL object as Org syntax.
2712 (let* ((babel-info (org-element-property :info inline-babel-call
))
2713 (main-source (car babel-info
))
2714 (post-options (nth 1 babel-info
)))
2716 (if (string-match "\\[\\(\\[.*?\\]\\)\\]" main-source
)
2717 ;; Remove redundant square brackets.
2719 (match-string 1 main-source
) nil nil main-source
)
2721 (and post-options
(format "[%s]" post-options
)))))
2723 (defun org-element-inline-babel-call-successor (limit)
2724 "Search for the next inline-babel-call object.
2726 LIMIT bounds the search.
2728 Return value is a cons cell whose CAR is `inline-babel-call' and
2729 CDR is beginning position."
2731 ;; Use a simplified version of
2732 ;; `org-babel-inline-lob-one-liner-regexp'.
2733 (when (re-search-forward
2734 "call_\\([^()\n]+?\\)\\(?:\\[.*?\\]\\)?([^\n]*?)\\(\\[.*?\\]\\)?"
2736 (cons 'inline-babel-call
(match-beginning 0)))))
2739 ;;;; Inline Src Block
2741 (defun org-element-inline-src-block-parser ()
2742 "Parse inline source block at point.
2744 LIMIT bounds the search.
2746 Return a list whose CAR is `inline-src-block' and CDR a plist
2747 with `:begin', `:end', `:language', `:value', `:parameters' and
2748 `:post-blank' as keywords.
2750 Assume point is at the beginning of the inline src block."
2752 (unless (bolp) (backward-char))
2753 (looking-at org-babel-inline-src-block-regexp
)
2754 (let ((begin (match-beginning 1))
2755 (language (org-match-string-no-properties 2))
2756 (parameters (org-match-string-no-properties 4))
2757 (value (org-match-string-no-properties 5))
2758 (post-blank (progn (goto-char (match-end 0))
2759 (skip-chars-forward " \t")))
2761 (list 'inline-src-block
2762 (list :language language
2764 :parameters parameters
2767 :post-blank post-blank
)))))
2769 (defun org-element-inline-src-block-interpreter (inline-src-block contents
)
2770 "Interpret INLINE-SRC-BLOCK object as Org syntax.
2772 (let ((language (org-element-property :language inline-src-block
))
2773 (arguments (org-element-property :parameters inline-src-block
))
2774 (body (org-element-property :value inline-src-block
)))
2775 (format "src_%s%s{%s}"
2777 (if arguments
(format "[%s]" arguments
) "")
2780 (defun org-element-inline-src-block-successor (limit)
2781 "Search for the next inline-babel-call element.
2783 LIMIT bounds the search.
2785 Return value is a cons cell whose CAR is `inline-babel-call' and
2786 CDR is beginning position."
2788 (unless (bolp) (backward-char))
2789 (when (re-search-forward org-babel-inline-src-block-regexp limit t
)
2790 (cons 'inline-src-block
(match-beginning 1)))))
2794 (defun org-element-italic-parser ()
2795 "Parse italic object at point.
2797 Return a list whose CAR is `italic' and CDR is a plist with
2798 `:begin', `:end', `:contents-begin' and `:contents-end' and
2799 `:post-blank' keywords.
2801 Assume point is at the first slash marker."
2803 (unless (bolp) (backward-char 1))
2804 (looking-at org-emph-re
)
2805 (let ((begin (match-beginning 2))
2806 (contents-begin (match-beginning 4))
2807 (contents-end (match-end 4))
2808 (post-blank (progn (goto-char (match-end 2))
2809 (skip-chars-forward " \t")))
2814 :contents-begin contents-begin
2815 :contents-end contents-end
2816 :post-blank post-blank
)))))
2818 (defun org-element-italic-interpreter (italic contents
)
2819 "Interpret ITALIC object as Org syntax.
2820 CONTENTS is the contents of the object."
2821 (format "/%s/" contents
))
2826 (defun org-element-latex-fragment-parser ()
2827 "Parse latex fragment at point.
2829 Return a list whose CAR is `latex-fragment' and CDR a plist with
2830 `:value', `:begin', `:end', and `:post-blank' as keywords.
2832 Assume point is at the beginning of the latex fragment."
2834 (let* ((begin (point))
2838 (let ((latex-regexp (nth 1 (assoc e org-latex-regexps
))))
2839 (when (or (looking-at latex-regexp
)
2843 (looking-at latex-regexp
))))
2844 (throw 'exit
(nth 2 (assoc e org-latex-regexps
))))))
2845 (plist-get org-format-latex-options
:matchers
))
2846 ;; None found: it's a macro.
2847 (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")
2849 (value (match-string-no-properties substring-match
))
2850 (post-blank (progn (goto-char (match-end substring-match
))
2851 (skip-chars-forward " \t")))
2853 (list 'latex-fragment
2857 :post-blank post-blank
)))))
2859 (defun org-element-latex-fragment-interpreter (latex-fragment contents
)
2860 "Interpret LATEX-FRAGMENT object as Org syntax.
2862 (org-element-property :value latex-fragment
))
2866 (defun org-element-line-break-parser ()
2867 "Parse line break at point.
2869 Return a list whose CAR is `line-break', and CDR a plist with
2870 `:begin', `:end' and `:post-blank' keywords.
2872 Assume point is at the beginning of the line break."
2873 (list 'line-break
(list :begin
(point) :end
(point-at-eol) :post-blank
0)))
2875 (defun org-element-line-break-interpreter (line-break contents
)
2876 "Interpret LINE-BREAK object as Org syntax.
2880 (defun org-element-line-break-successor (limit)
2881 "Search for the next line-break object.
2883 LIMIT bounds the search.
2885 Return value is a cons cell whose CAR is `line-break' and CDR is
2886 beginning position."
2888 (let ((beg (and (re-search-forward "[^\\\\]\\(\\\\\\\\\\)[ \t]*$" limit t
)
2889 (goto-char (match-beginning 1)))))
2890 ;; A line break can only happen on a non-empty line.
2891 (when (and beg
(re-search-backward "\\S-" (point-at-bol) t
))
2892 (cons 'line-break beg
)))))
2897 (defun org-element-link-parser ()
2898 "Parse link at point.
2900 Return a list whose CAR is `link' and CDR a plist with `:type',
2901 `:path', `:raw-link', `:application', `:search-option', `:begin',
2902 `:end', `:contents-begin', `:contents-end' and `:post-blank' as
2905 Assume point is at the beginning of the link."
2907 (let ((begin (point))
2908 end contents-begin contents-end link-end post-blank path type
2909 raw-link link search-option application
)
2911 ;; Type 1: Text targeted from a radio target.
2912 ((and org-target-link-regexp
(looking-at org-target-link-regexp
))
2914 link-end
(match-end 0)
2915 path
(org-match-string-no-properties 0)))
2916 ;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]]
2917 ((looking-at org-bracket-link-regexp
)
2918 (setq contents-begin
(match-beginning 3)
2919 contents-end
(match-end 3)
2920 link-end
(match-end 0)
2921 ;; RAW-LINK is the original link.
2922 raw-link
(org-match-string-no-properties 1)
2923 link
(org-translate-link
2924 (org-link-expand-abbrev
2925 (org-link-unescape raw-link
))))
2926 ;; Determine TYPE of link and set PATH accordingly.
2929 ((or (file-name-absolute-p link
) (string-match "^\\.\\.?/" link
))
2930 (setq type
"file" path link
))
2931 ;; Explicit type (http, irc, bbdb...). See `org-link-types'.
2932 ((string-match org-link-re-with-space3 link
)
2933 (setq type
(match-string 1 link
) path
(match-string 2 link
)))
2934 ;; Id type: PATH is the id.
2935 ((string-match "^id:\\([-a-f0-9]+\\)" link
)
2936 (setq type
"id" path
(match-string 1 link
)))
2937 ;; Code-ref type: PATH is the name of the reference.
2938 ((string-match "^(\\(.*\\))$" link
)
2939 (setq type
"coderef" path
(match-string 1 link
)))
2940 ;; Custom-id type: PATH is the name of the custom id.
2941 ((= (aref link
0) ?
#)
2942 (setq type
"custom-id" path
(substring link
1)))
2943 ;; Fuzzy type: Internal link either matches a target, an
2944 ;; headline name or nothing. PATH is the target or
2946 (t (setq type
"fuzzy" path link
))))
2947 ;; Type 3: Plain link, i.e. http://orgmode.org
2948 ((looking-at org-plain-link-re
)
2949 (setq raw-link
(org-match-string-no-properties 0)
2950 type
(org-match-string-no-properties 1)
2951 path
(org-match-string-no-properties 2)
2952 link-end
(match-end 0)))
2953 ;; Type 4: Angular link, i.e. <http://orgmode.org>
2954 ((looking-at org-angle-link-re
)
2955 (setq raw-link
(buffer-substring-no-properties
2956 (match-beginning 1) (match-end 2))
2957 type
(org-match-string-no-properties 1)
2958 path
(org-match-string-no-properties 2)
2959 link-end
(match-end 0))))
2960 ;; In any case, deduce end point after trailing white space from
2961 ;; LINK-END variable.
2962 (setq post-blank
(progn (goto-char link-end
) (skip-chars-forward " \t"))
2964 ;; Extract search option and opening application out of
2965 ;; "file"-type links.
2966 (when (member type org-element-link-type-is-file
)
2968 (cond ((string-match "^file\\+\\(.*\\)$" type
)
2969 (setq application
(match-string 1 type
)))
2970 ((not (string-match "^file" type
))
2971 (setq application type
)))
2972 ;; Extract search option from PATH.
2973 (when (string-match "::\\(.*\\)$" path
)
2974 (setq search-option
(match-string 1 path
)
2975 path
(replace-match "" nil nil path
)))
2976 ;; Make sure TYPE always report "file".
2981 :raw-link
(or raw-link path
)
2982 :application application
2983 :search-option search-option
2986 :contents-begin contents-begin
2987 :contents-end contents-end
2988 :post-blank post-blank
)))))
2990 (defun org-element-link-interpreter (link contents
)
2991 "Interpret LINK object as Org syntax.
2992 CONTENTS is the contents of the object, or nil."
2993 (let ((type (org-element-property :type link
))
2994 (raw-link (org-element-property :raw-link link
)))
2995 (if (string= type
"radio") raw-link
2998 (if contents
(format "[%s]" contents
) "")))))
3000 (defun org-element-link-successor (limit)
3001 "Search for the next link object.
3003 LIMIT bounds the search.
3005 Return value is a cons cell whose CAR is `link' and CDR is
3006 beginning position."
3009 (if (not org-target-link-regexp
) org-any-link-re
3010 (concat org-any-link-re
"\\|" org-target-link-regexp
))))
3011 (when (re-search-forward link-regexp limit t
)
3012 (cons 'link
(match-beginning 0))))))
3017 (defun org-element-macro-parser ()
3018 "Parse macro at point.
3020 Return a list whose CAR is `macro' and CDR a plist with `:key',
3021 `:args', `:begin', `:end', `:value' and `:post-blank' as
3024 Assume point is at the macro."
3026 (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
3027 (let ((begin (point))
3028 (key (downcase (org-match-string-no-properties 1)))
3029 (value (org-match-string-no-properties 0))
3030 (post-blank (progn (goto-char (match-end 0))
3031 (skip-chars-forward " \t")))
3033 (args (let ((args (org-match-string-no-properties 3)) args2
)
3035 (setq args
(org-split-string args
","))
3037 (while (string-match "\\\\\\'" (car args
))
3038 ;; Repair bad splits.
3039 (setcar (cdr args
) (concat (substring (car args
) 0 -
1)
3042 (push (pop args
) args2
))
3043 (mapcar 'org-trim
(nreverse args2
))))))
3050 :post-blank post-blank
)))))
3052 (defun org-element-macro-interpreter (macro contents
)
3053 "Interpret MACRO object as Org syntax.
3055 (org-element-property :value macro
))
3057 (defun org-element-macro-successor (limit)
3058 "Search for the next macro object.
3060 LIMIT bounds the search.
3062 Return value is cons cell whose CAR is `macro' and CDR is
3063 beginning position."
3065 (when (re-search-forward
3066 "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
3068 (cons 'macro
(match-beginning 0)))))
3073 (defun org-element-radio-target-parser ()
3074 "Parse radio target at point.
3076 Return a list whose CAR is `radio-target' and CDR a plist with
3077 `:begin', `:end', `:contents-begin', `:contents-end', `:value'
3078 and `:post-blank' as keywords.
3080 Assume point is at the radio target."
3082 (looking-at org-radio-target-regexp
)
3083 (let ((begin (point))
3084 (contents-begin (match-beginning 1))
3085 (contents-end (match-end 1))
3086 (value (org-match-string-no-properties 1))
3087 (post-blank (progn (goto-char (match-end 0))
3088 (skip-chars-forward " \t")))
3093 :contents-begin contents-begin
3094 :contents-end contents-end
3095 :post-blank post-blank
3098 (defun org-element-radio-target-interpreter (target contents
)
3099 "Interpret TARGET object as Org syntax.
3100 CONTENTS is the contents of the object."
3101 (concat "<<<" contents
">>>"))
3103 (defun org-element-radio-target-successor (limit)
3104 "Search for the next radio-target object.
3106 LIMIT bounds the search.
3108 Return value is a cons cell whose CAR is `radio-target' and CDR
3109 is beginning position."
3111 (when (re-search-forward org-radio-target-regexp limit t
)
3112 (cons 'radio-target
(match-beginning 0)))))
3115 ;;;; Statistics Cookie
3117 (defun org-element-statistics-cookie-parser ()
3118 "Parse statistics cookie at point.
3120 Return a list whose CAR is `statistics-cookie', and CDR a plist
3121 with `:begin', `:end', `:value' and `:post-blank' keywords.
3123 Assume point is at the beginning of the statistics-cookie."
3125 (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
3126 (let* ((begin (point))
3127 (value (buffer-substring-no-properties
3128 (match-beginning 0) (match-end 0)))
3129 (post-blank (progn (goto-char (match-end 0))
3130 (skip-chars-forward " \t")))
3132 (list 'statistics-cookie
3136 :post-blank post-blank
)))))
3138 (defun org-element-statistics-cookie-interpreter (statistics-cookie contents
)
3139 "Interpret STATISTICS-COOKIE object as Org syntax.
3141 (org-element-property :value statistics-cookie
))
3143 (defun org-element-statistics-cookie-successor (limit)
3144 "Search for the next statistics cookie object.
3146 LIMIT bounds the search.
3148 Return value is a cons cell whose CAR is `statistics-cookie' and
3149 CDR is beginning position."
3151 (when (re-search-forward "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]" limit t
)
3152 (cons 'statistics-cookie
(match-beginning 0)))))
3157 (defun org-element-strike-through-parser ()
3158 "Parse strike-through object at point.
3160 Return a list whose CAR is `strike-through' and CDR is a plist
3161 with `:begin', `:end', `:contents-begin' and `:contents-end' and
3162 `:post-blank' keywords.
3164 Assume point is at the first plus sign marker."
3166 (unless (bolp) (backward-char 1))
3167 (looking-at org-emph-re
)
3168 (let ((begin (match-beginning 2))
3169 (contents-begin (match-beginning 4))
3170 (contents-end (match-end 4))
3171 (post-blank (progn (goto-char (match-end 2))
3172 (skip-chars-forward " \t")))
3174 (list 'strike-through
3177 :contents-begin contents-begin
3178 :contents-end contents-end
3179 :post-blank post-blank
)))))
3181 (defun org-element-strike-through-interpreter (strike-through contents
)
3182 "Interpret STRIKE-THROUGH object as Org syntax.
3183 CONTENTS is the contents of the object."
3184 (format "+%s+" contents
))
3189 (defun org-element-subscript-parser ()
3190 "Parse subscript at point.
3192 Return a list whose CAR is `subscript' and CDR a plist with
3193 `:begin', `:end', `:contents-begin', `:contents-end',
3194 `:use-brackets-p' and `:post-blank' as keywords.
3196 Assume point is at the underscore."
3198 (unless (bolp) (backward-char))
3199 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp
)
3201 (not (looking-at org-match-substring-regexp
))))
3202 (begin (match-beginning 2))
3203 (contents-begin (or (match-beginning 5)
3204 (match-beginning 3)))
3205 (contents-end (or (match-end 5) (match-end 3)))
3206 (post-blank (progn (goto-char (match-end 0))
3207 (skip-chars-forward " \t")))
3212 :use-brackets-p bracketsp
3213 :contents-begin contents-begin
3214 :contents-end contents-end
3215 :post-blank post-blank
)))))
3217 (defun org-element-subscript-interpreter (subscript contents
)
3218 "Interpret SUBSCRIPT object as Org syntax.
3219 CONTENTS is the contents of the object."
3221 (if (org-element-property :use-brackets-p subscript
) "_{%s}" "_%s")
3224 (defun org-element-sub/superscript-successor
(limit)
3225 "Search for the next sub/superscript object.
3227 LIMIT bounds the search.
3229 Return value is a cons cell whose CAR is either `subscript' or
3230 `superscript' and CDR is beginning position."
3232 (unless (bolp) (backward-char))
3233 (when (re-search-forward org-match-substring-regexp limit t
)
3234 (cons (if (string= (match-string 2) "_") 'subscript
'superscript
)
3235 (match-beginning 2)))))
3240 (defun org-element-superscript-parser ()
3241 "Parse superscript at point.
3243 Return a list whose CAR is `superscript' and CDR a plist with
3244 `:begin', `:end', `:contents-begin', `:contents-end',
3245 `:use-brackets-p' and `:post-blank' as keywords.
3247 Assume point is at the caret."
3249 (unless (bolp) (backward-char))
3250 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp
) t
3251 (not (looking-at org-match-substring-regexp
))))
3252 (begin (match-beginning 2))
3253 (contents-begin (or (match-beginning 5)
3254 (match-beginning 3)))
3255 (contents-end (or (match-end 5) (match-end 3)))
3256 (post-blank (progn (goto-char (match-end 0))
3257 (skip-chars-forward " \t")))
3262 :use-brackets-p bracketsp
3263 :contents-begin contents-begin
3264 :contents-end contents-end
3265 :post-blank post-blank
)))))
3267 (defun org-element-superscript-interpreter (superscript contents
)
3268 "Interpret SUPERSCRIPT object as Org syntax.
3269 CONTENTS is the contents of the object."
3271 (if (org-element-property :use-brackets-p superscript
) "^{%s}" "^%s")
3277 (defun org-element-table-cell-parser ()
3278 "Parse table cell at point.
3280 Return a list whose CAR is `table-cell' and CDR is a plist
3281 containing `:begin', `:end', `:contents-begin', `:contents-end'
3282 and `:post-blank' keywords."
3283 (looking-at "[ \t]*\\(.*?\\)[ \t]*|")
3284 (let* ((begin (match-beginning 0))
3286 (contents-begin (match-beginning 1))
3287 (contents-end (match-end 1)))
3291 :contents-begin contents-begin
3292 :contents-end contents-end
3295 (defun org-element-table-cell-interpreter (table-cell contents
)
3296 "Interpret TABLE-CELL element as Org syntax.
3297 CONTENTS is the contents of the cell, or nil."
3298 (concat " " contents
" |"))
3300 (defun org-element-table-cell-successor (limit)
3301 "Search for the next table-cell object.
3303 LIMIT bounds the search.
3305 Return value is a cons cell whose CAR is `table-cell' and CDR is
3306 beginning position."
3307 (when (looking-at "[ \t]*.*?[ \t]+|") (cons 'table-cell
(point))))
3312 (defun org-element-target-parser ()
3313 "Parse target at point.
3315 Return a list whose CAR is `target' and CDR a plist with
3316 `:begin', `:end', `:value' and `:post-blank' as keywords.
3318 Assume point is at the target."
3320 (looking-at org-target-regexp
)
3321 (let ((begin (point))
3322 (value (org-match-string-no-properties 1))
3323 (post-blank (progn (goto-char (match-end 0))
3324 (skip-chars-forward " \t")))
3330 :post-blank post-blank
)))))
3332 (defun org-element-target-interpreter (target contents
)
3333 "Interpret TARGET object as Org syntax.
3335 (format "<<%s>>" (org-element-property :value target
)))
3337 (defun org-element-target-successor (limit)
3338 "Search for the next target object.
3340 LIMIT bounds the search.
3342 Return value is a cons cell whose CAR is `target' and CDR is
3343 beginning position."
3345 (when (re-search-forward org-target-regexp limit t
)
3346 (cons 'target
(match-beginning 0)))))
3351 (defun org-element-timestamp-parser ()
3352 "Parse time stamp at point.
3354 Return a list whose CAR is `timestamp', and CDR a plist with
3355 `:type', `:begin', `:end', `:value' and `:post-blank' keywords.
3357 Assume point is at the beginning of the timestamp."
3359 (let* ((begin (point))
3360 (activep (eq (char-after) ?
<))
3363 (looking-at "[<[]\\(\\(%%\\)?.*?\\)[]>]\\(?:--[<[]\\(.*?\\)[]>]\\)?")
3364 (match-string-no-properties 1)))
3365 (range-end (match-string-no-properties 3))
3366 (type (cond ((match-string 2) 'diary
)
3367 ((and activep range-end
) 'active-range
)
3369 (range-end 'inactive-range
)
3371 (post-blank (progn (goto-char (match-end 0))
3372 (skip-chars-forward " \t")))
3377 :range-end range-end
3380 :post-blank post-blank
)))))
3382 (defun org-element-timestamp-interpreter (timestamp contents
)
3383 "Interpret TIMESTAMP object as Org syntax.
3385 (let ((type (org-element-property :type timestamp
) ))
3387 (format (if (memq type
'(inactive inactive-range
)) "[%s]" "<%s>")
3388 (org-element-property :value timestamp
))
3389 (let ((range-end (org-element-property :range-end timestamp
)))
3392 (format (if (eq type
'inactive-range
) "[%s]" "<%s>")
3395 (defun org-element-timestamp-successor (limit)
3396 "Search for the next timestamp object.
3398 LIMIT bounds the search.
3400 Return value is a cons cell whose CAR is `timestamp' and CDR is
3401 beginning position."
3403 (when (re-search-forward
3404 (concat org-ts-regexp-both
3406 "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
3408 "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
3410 (cons 'timestamp
(match-beginning 0)))))
3415 (defun org-element-underline-parser ()
3416 "Parse underline object at point.
3418 Return a list whose CAR is `underline' and CDR is a plist with
3419 `:begin', `:end', `:contents-begin' and `:contents-end' and
3420 `:post-blank' keywords.
3422 Assume point is at the first underscore marker."
3424 (unless (bolp) (backward-char 1))
3425 (looking-at org-emph-re
)
3426 (let ((begin (match-beginning 2))
3427 (contents-begin (match-beginning 4))
3428 (contents-end (match-end 4))
3429 (post-blank (progn (goto-char (match-end 2))
3430 (skip-chars-forward " \t")))
3435 :contents-begin contents-begin
3436 :contents-end contents-end
3437 :post-blank post-blank
)))))
3439 (defun org-element-underline-interpreter (underline contents
)
3440 "Interpret UNDERLINE object as Org syntax.
3441 CONTENTS is the contents of the object."
3442 (format "_%s_" contents
))
3447 (defun org-element-verbatim-parser ()
3448 "Parse verbatim object at point.
3450 Return a list whose CAR is `verbatim' and CDR is a plist with
3451 `:value', `:begin', `:end' and `:post-blank' keywords.
3453 Assume point is at the first equal sign marker."
3455 (unless (bolp) (backward-char 1))
3456 (looking-at org-emph-re
)
3457 (let ((begin (match-beginning 2))
3458 (value (org-match-string-no-properties 4))
3459 (post-blank (progn (goto-char (match-end 2))
3460 (skip-chars-forward " \t")))
3466 :post-blank post-blank
)))))
3468 (defun org-element-verbatim-interpreter (verbatim contents
)
3469 "Interpret VERBATIM object as Org syntax.
3471 (format "=%s=" (org-element-property :value verbatim
)))
3475 ;;; Parsing Element Starting At Point
3477 ;; `org-element--current-element' is the core function of this section.
3478 ;; It returns the Lisp representation of the element starting at
3481 ;; `org-element--current-element' makes use of special modes. They
3482 ;; are activated for fixed element chaining (i.e. `plain-list' >
3483 ;; `item') or fixed conditional element chaining (i.e. `headline' >
3484 ;; `section'). Special modes are: `first-section', `item',
3485 ;; `node-property', `quote-section', `section' and `table-row'.
3487 (defun org-element--current-element
3488 (limit &optional granularity special structure
)
3489 "Parse the element starting at point.
3491 LIMIT bounds the search.
3493 Return value is a list like (TYPE PROPS) where TYPE is the type
3494 of the element and PROPS a plist of properties associated to the
3497 Possible types are defined in `org-element-all-elements'.
3499 Optional argument GRANULARITY determines the depth of the
3500 recursion. Allowed values are `headline', `greater-element',
3501 `element', `object' or nil. When it is broader than `object' (or
3502 nil), secondary values will not be parsed, since they only
3505 Optional argument SPECIAL, when non-nil, can be either
3506 `first-section', `item', `node-property', `quote-section',
3507 `section', and `table-row'.
3509 If STRUCTURE isn't provided but SPECIAL is set to `item', it will
3512 This function assumes point is always at the beginning of the
3513 element it has to parse."
3515 (let ((case-fold-search t
)
3516 ;; Determine if parsing depth allows for secondary strings
3517 ;; parsing. It only applies to elements referenced in
3518 ;; `org-element-secondary-value-alist'.
3519 (raw-secondary-p (and granularity
(not (eq granularity
'object
)))))
3523 (org-element-item-parser limit structure raw-secondary-p
))
3525 ((eq special
'table-row
) (org-element-table-row-parser limit
))
3527 ((eq special
'node-property
) (org-element-node-property-parser limit
))
3529 ((org-with-limited-levels (org-at-heading-p))
3530 (org-element-headline-parser limit raw-secondary-p
))
3531 ;; Sections (must be checked after headline).
3532 ((eq special
'section
) (org-element-section-parser limit
))
3533 ((eq special
'quote-section
) (org-element-quote-section-parser limit
))
3534 ((eq special
'first-section
)
3535 (org-element-section-parser
3536 (or (save-excursion (org-with-limited-levels (outline-next-heading)))
3538 ;; When not at bol, point is at the beginning of an item or
3539 ;; a footnote definition: next item is always a paragraph.
3540 ((not (bolp)) (org-element-paragraph-parser limit
(list (point))))
3541 ;; Planning and Clock.
3542 ((and (looking-at org-planning-or-clock-line-re
))
3543 (if (equal (match-string 1) org-clock-string
)
3544 (org-element-clock-parser limit
)
3545 (org-element-planning-parser limit
)))
3548 (org-element-inlinetask-parser limit raw-secondary-p
))
3549 ;; From there, elements can have affiliated keywords.
3550 (t (let ((affiliated (org-element--collect-affiliated-keywords)))
3552 ;; LaTeX Environment.
3553 ((looking-at "[ \t]*\\\\begin{\\([A-Za-z0-9*]+\\)}[ \t]*$")
3554 (org-element-latex-environment-parser limit affiliated
))
3555 ;; Drawer and Property Drawer.
3556 ((looking-at org-drawer-regexp
)
3557 (if (equal (match-string 1) "PROPERTIES")
3558 (org-element-property-drawer-parser limit affiliated
)
3559 (org-element-drawer-parser limit affiliated
)))
3561 ((looking-at "[ \t]*:\\( \\|$\\)")
3562 (org-element-fixed-width-parser limit affiliated
))
3563 ;; Inline Comments, Blocks, Babel Calls, Dynamic Blocks and
3565 ((looking-at "[ \t]*#")
3566 (goto-char (match-end 0))
3567 (cond ((looking-at "\\(?: \\|$\\)")
3569 (org-element-comment-parser limit affiliated
))
3570 ((looking-at "\\+BEGIN_\\(\\S-+\\)")
3572 (let ((parser (assoc (upcase (match-string 1))
3573 org-element-block-name-alist
)))
3574 (if parser
(funcall (cdr parser
) limit affiliated
)
3575 (org-element-special-block-parser limit affiliated
))))
3576 ((looking-at "\\+CALL:")
3578 (org-element-babel-call-parser limit affiliated
))
3579 ((looking-at "\\+BEGIN:? ")
3581 (org-element-dynamic-block-parser limit affiliated
))
3582 ((looking-at "\\+\\S-+:")
3584 (org-element-keyword-parser limit affiliated
))
3587 (org-element-paragraph-parser limit affiliated
))))
3588 ;; Footnote Definition.
3589 ((looking-at org-footnote-definition-re
)
3590 (org-element-footnote-definition-parser limit affiliated
))
3592 ((looking-at "[ \t]*-\\{5,\\}[ \t]*$")
3593 (org-element-horizontal-rule-parser limit affiliated
))
3596 (org-element-diary-sexp-parser limit affiliated
))
3598 ((org-at-table-p t
) (org-element-table-parser limit affiliated
))
3600 ((looking-at (org-item-re))
3601 (org-element-plain-list-parser
3602 limit affiliated
(or structure
(org-list-struct))))
3603 ;; Default element: Paragraph.
3604 (t (org-element-paragraph-parser limit affiliated
)))))))))
3607 ;; Most elements can have affiliated keywords. When looking for an
3608 ;; element beginning, we want to move before them, as they belong to
3609 ;; that element, and, in the meantime, collect information they give
3610 ;; into appropriate properties. Hence the following function.
3612 (defun org-element--collect-affiliated-keywords ()
3613 "Collect affiliated keywords from point.
3615 Return a list whose CAR is the position at the first of them and
3616 CDR a plist of keywords and values and move point to the
3617 beginning of the first line after them.
3619 As a special case, if element doesn't start at the beginning of
3620 the line (i.e. a paragraph starting an item), CAR is current
3621 position of point and CDR is nil."
3622 (if (not (bolp)) (list (point))
3623 (let ((case-fold-search t
)
3625 ;; RESTRICT is the list of objects allowed in parsed
3627 (restrict (org-element-restriction 'keyword
))
3629 (while (and (not (eobp)) (looking-at org-element--affiliated-re
))
3630 (let* ((raw-kwd (upcase (match-string 1)))
3631 ;; Apply translation to RAW-KWD. From there, KWD is
3632 ;; the official keyword.
3633 (kwd (or (cdr (assoc raw-kwd
3634 org-element-keyword-translation-alist
))
3636 ;; Find main value for any keyword.
3640 (buffer-substring-no-properties
3641 (match-end 0) (point-at-eol)))))
3642 ;; PARSEDP is non-nil when keyword should have its
3644 (parsedp (member kwd org-element-parsed-keywords
))
3645 ;; If KWD is a dual keyword, find its secondary
3646 ;; value. Maybe parse it.
3647 (dualp (member kwd org-element-dual-keywords
))
3650 (let ((sec (org-match-string-no-properties 2)))
3651 (if (or (not sec
) (not parsedp
)) sec
3652 (org-element-parse-secondary-string sec restrict
)))))
3653 ;; Attribute a property name to KWD.
3654 (kwd-sym (and kwd
(intern (concat ":" (downcase kwd
))))))
3655 ;; Now set final shape for VALUE.
3657 (setq value
(org-element-parse-secondary-string value restrict
)))
3658 (when dualp
(setq value
(and value
(cons value dual-value
))))
3659 (when (or (member kwd org-element-multiple-keywords
)
3660 ;; Attributes can always appear on multiple lines.
3661 (string-match "^ATTR_" kwd
))
3662 (setq value
(cons value
(plist-get output kwd-sym
))))
3663 ;; Eventually store the new value in OUTPUT.
3664 (setq output
(plist-put output kwd-sym value
))
3665 ;; Move to next keyword.
3667 ;; If affiliated keywords are orphaned: move back to first one.
3668 ;; They will be parsed as a paragraph.
3669 (when (looking-at "[ \t]*$") (goto-char origin
) (setq output nil
))
3671 (cons origin output
))))
3677 ;; The two major functions here are `org-element-parse-buffer', which
3678 ;; parses Org syntax inside the current buffer, taking into account
3679 ;; region, narrowing, or even visibility if specified, and
3680 ;; `org-element-parse-secondary-string', which parses objects within
3683 ;; The (almost) almighty `org-element-map' allows to apply a function
3684 ;; on elements or objects matching some type, and accumulate the
3685 ;; resulting values. In an export situation, it also skips unneeded
3686 ;; parts of the parse tree.
3688 (defun org-element-parse-buffer (&optional granularity visible-only
)
3689 "Recursively parse the buffer and return structure.
3690 If narrowing is in effect, only parse the visible part of the
3693 Optional argument GRANULARITY determines the depth of the
3694 recursion. It can be set to the following symbols:
3696 `headline' Only parse headlines.
3697 `greater-element' Don't recurse into greater elements excepted
3698 headlines and sections. Thus, elements
3699 parsed are the top-level ones.
3700 `element' Parse everything but objects and plain text.
3701 `object' Parse the complete buffer (default).
3703 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3706 Assume buffer is in Org mode."
3708 (goto-char (point-min))
3709 (org-skip-whitespace)
3710 (org-element--parse-elements
3711 (point-at-bol) (point-max)
3712 ;; Start in `first-section' mode so text before the first
3713 ;; headline belongs to a section.
3714 'first-section nil granularity visible-only
(list 'org-data nil
))))
3716 (defun org-element-parse-secondary-string (string restriction
&optional parent
)
3717 "Recursively parse objects in STRING and return structure.
3719 RESTRICTION is a symbol limiting the object types that will be
3722 Optional argument PARENT, when non-nil, is the element or object
3723 containing the secondary string. It is used to set correctly
3724 `:parent' property within the string."
3727 (let ((secondary (org-element--parse-objects
3728 (point-min) (point-max) nil restriction
)))
3729 (mapc (lambda (obj) (org-element-put-property obj
:parent parent
))
3732 (defun org-element-map
3733 (data types fun
&optional info first-match no-recursion with-affiliated
)
3734 "Map a function on selected elements or objects.
3736 DATA is the parsed tree, as returned by, i.e,
3737 `org-element-parse-buffer'. TYPES is a symbol or list of symbols
3738 of elements or objects types. FUN is the function called on the
3739 matching element or object. It must accept one arguments: the
3740 element or object itself.
3742 When optional argument INFO is non-nil, it should be a plist
3743 holding export options. In that case, parts of the parse tree
3744 not exportable according to that property list will be skipped.
3746 When optional argument FIRST-MATCH is non-nil, stop at the first
3747 match for which FUN doesn't return nil, and return that value.
3749 Optional argument NO-RECURSION is a symbol or a list of symbols
3750 representing elements or objects types. `org-element-map' won't
3751 enter any recursive element or object whose type belongs to that
3752 list. Though, FUN can still be applied on them.
3754 When optional argument WITH-AFFILIATED is non-nil, also move into
3755 affiliated keywords to find objects.
3757 Nil values returned from FUN do not appear in the results."
3758 ;; Ensure TYPES and NO-RECURSION are a list, even of one element.
3759 (unless (listp types
) (setq types
(list types
)))
3760 (unless (listp no-recursion
) (setq no-recursion
(list no-recursion
)))
3761 ;; Recursion depth is determined by --CATEGORY.
3764 (let ((category 'greater-elements
))
3765 (mapc (lambda (type)
3766 (cond ((or (memq type org-element-all-objects
)
3767 (eq type
'plain-text
))
3768 ;; If one object is found, the function
3769 ;; has to recurse into every object.
3770 (throw 'found
'objects
))
3771 ((not (memq type org-element-greater-elements
))
3772 ;; If one regular element is found, the
3773 ;; function has to recurse, at least,
3774 ;; into every element it encounters.
3775 (and (not (eq category
'elements
))
3776 (setq category
'elements
)))))
3779 ;; Compute properties for affiliated keywords if necessary.
3781 (and with-affiliated
3782 (mapcar (lambda (kwd)
3783 (cons kwd
(intern (concat ":" (downcase kwd
)))))
3784 org-element-affiliated-keywords
)))
3790 ;; Recursively walk DATA. INFO, if non-nil, is a plist
3791 ;; holding contextual information.
3792 (let ((--type (org-element-type --data
)))
3795 ;; Ignored element in an export context.
3796 ((and info
(memq --data
(plist-get info
:ignore-list
))))
3797 ;; Secondary string: only objects can be found there.
3799 (when (eq --category
'objects
) (mapc --walk-tree --data
)))
3800 ;; Unconditionally enter parse trees.
3801 ((eq --type
'org-data
)
3802 (mapc --walk-tree
(org-element-contents --data
)))
3804 ;; Check if TYPE is matching among TYPES. If so,
3805 ;; apply FUN to --DATA and accumulate return value
3806 ;; into --ACC (or exit if FIRST-MATCH is non-nil).
3807 (when (memq --type types
)
3808 (let ((result (funcall fun --data
)))
3809 (cond ((not result
))
3810 (first-match (throw '--map-first-match result
))
3811 (t (push result --acc
)))))
3812 ;; If --DATA has a secondary string that can contain
3813 ;; objects with their type among TYPES, look into it.
3814 (when (and (eq --category
'objects
) (not (stringp --data
)))
3816 (assq --type org-element-secondary-value-alist
)))
3818 (funcall --walk-tree
3819 (org-element-property (cdr sec-prop
) --data
)))))
3820 ;; If --DATA has any affiliated keywords and
3821 ;; WITH-AFFILIATED is non-nil, look for objects in
3823 (when (and with-affiliated
3824 (eq --category
'objects
)
3825 (memq --type org-element-all-elements
))
3826 (mapc (lambda (kwd-pair)
3827 (let ((kwd (car kwd-pair
))
3828 (value (org-element-property
3829 (cdr kwd-pair
) --data
)))
3830 ;; Pay attention to the type of value.
3831 ;; Preserve order for multiple keywords.
3834 ((and (member kwd org-element-multiple-keywords
)
3835 (member kwd org-element-dual-keywords
))
3836 (mapc (lambda (line)
3837 (funcall --walk-tree
(cdr line
))
3838 (funcall --walk-tree
(car line
)))
3840 ((member kwd org-element-multiple-keywords
)
3841 (mapc (lambda (line) (funcall --walk-tree line
))
3843 ((member kwd org-element-dual-keywords
)
3844 (funcall --walk-tree
(cdr value
))
3845 (funcall --walk-tree
(car value
)))
3846 (t (funcall --walk-tree value
)))))
3847 --affiliated-alist
))
3848 ;; Determine if a recursion into --DATA is possible.
3850 ;; --TYPE is explicitly removed from recursion.
3851 ((memq --type no-recursion
))
3852 ;; --DATA has no contents.
3853 ((not (org-element-contents --data
)))
3854 ;; Looking for greater elements but --DATA is simply
3855 ;; an element or an object.
3856 ((and (eq --category
'greater-elements
)
3857 (not (memq --type org-element-greater-elements
))))
3858 ;; Looking for elements but --DATA is an object.
3859 ((and (eq --category
'elements
)
3860 (memq --type org-element-all-objects
)))
3861 ;; In any other case, map contents.
3862 (t (mapc --walk-tree
(org-element-contents --data
)))))))))))
3863 (catch '--map-first-match
3864 (funcall --walk-tree data
)
3865 ;; Return value in a proper order.
3868 ;; The following functions are internal parts of the parser.
3870 ;; The first one, `org-element--parse-elements' acts at the element's
3873 ;; The second one, `org-element--parse-objects' applies on all objects
3874 ;; of a paragraph or a secondary string. It uses
3875 ;; `org-element--get-next-object-candidates' to optimize the search of
3876 ;; the next object in the buffer.
3878 ;; More precisely, that function looks for every allowed object type
3879 ;; first. Then, it discards failed searches, keeps further matches,
3880 ;; and searches again types matched behind point, for subsequent
3881 ;; calls. Thus, searching for a given type fails only once, and every
3882 ;; object is searched only once at top level (but sometimes more for
3885 (defun org-element--parse-elements
3886 (beg end special structure granularity visible-only acc
)
3887 "Parse elements between BEG and END positions.
3889 SPECIAL prioritize some elements over the others. It can be set
3890 to `first-section', `quote-section', `section' `item' or
3893 When value is `item', STRUCTURE will be used as the current list
3896 GRANULARITY determines the depth of the recursion. See
3897 `org-element-parse-buffer' for more information.
3899 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3902 Elements are accumulated into ACC."
3905 ;; When parsing only headlines, skip any text before first one.
3906 (when (and (eq granularity
'headline
) (not (org-at-heading-p)))
3907 (org-with-limited-levels (outline-next-heading)))
3909 (while (< (point) end
)
3910 ;; Find current element's type and parse it accordingly to
3912 (let* ((element (org-element--current-element
3913 end granularity special structure
))
3914 (type (org-element-type element
))
3915 (cbeg (org-element-property :contents-begin element
)))
3916 (goto-char (org-element-property :end element
))
3917 ;; Fill ELEMENT contents by side-effect.
3919 ;; If VISIBLE-ONLY is true and element is hidden or if it has
3920 ;; no contents, don't modify it.
3921 ((or (and visible-only
(org-element-property :hiddenp element
))
3923 ;; Greater element: parse it between `contents-begin' and
3924 ;; `contents-end'. Make sure GRANULARITY allows the
3925 ;; recursion, or ELEMENT is an headline, in which case going
3926 ;; inside is mandatory, in order to get sub-level headings.
3927 ((and (memq type org-element-greater-elements
)
3928 (or (memq granularity
'(element object nil
))
3929 (and (eq granularity
'greater-element
)
3931 (eq type
'headline
)))
3932 (org-element--parse-elements
3933 cbeg
(org-element-property :contents-end element
)
3934 ;; Possibly switch to a special mode.
3937 (if (org-element-property :quotedp element
) 'quote-section
3940 (property-drawer 'node-property
)
3942 (org-element-property :structure element
)
3943 granularity visible-only element
))
3944 ;; ELEMENT has contents. Parse objects inside, if
3945 ;; GRANULARITY allows it.
3946 ((memq granularity
'(object nil
))
3947 (org-element--parse-objects
3948 cbeg
(org-element-property :contents-end element
) element
3949 (org-element-restriction type
))))
3950 (org-element-adopt-elements acc element
)))
3954 (defun org-element--parse-objects (beg end acc restriction
)
3955 "Parse objects between BEG and END and return recursive structure.
3957 Objects are accumulated in ACC.
3959 RESTRICTION is a list of object types which are allowed in the
3964 (while (and (< (point) end
)
3965 (setq candidates
(org-element--get-next-object-candidates
3966 end restriction candidates
)))
3968 (let ((pos (apply 'min
(mapcar 'cdr candidates
))))
3971 (funcall (intern (format "org-element-%s-parser"
3972 (car (rassq pos candidates
)))))))))
3973 ;; 1. Text before any object. Untabify it.
3974 (let ((obj-beg (org-element-property :begin next-object
)))
3975 (unless (= (point) obj-beg
)
3977 (org-element-adopt-elements
3979 (replace-regexp-in-string
3980 "\t" (make-string tab-width ?
)
3981 (buffer-substring-no-properties (point) obj-beg
))))))
3983 (let ((obj-end (org-element-property :end next-object
))
3984 (cont-beg (org-element-property :contents-begin next-object
)))
3985 ;; Fill contents of NEXT-OBJECT by side-effect, if it has
3986 ;; a recursive type.
3988 (memq (car next-object
) org-element-recursive-objects
))
3992 (org-element-property :contents-end next-object
))
3993 (org-element--parse-objects
3994 (point-min) (point-max) next-object
3995 (org-element-restriction next-object
))))
3996 (setq acc
(org-element-adopt-elements acc next-object
))
3997 (goto-char obj-end
))))
3998 ;; 3. Text after last object. Untabify it.
3999 (unless (= (point) end
)
4001 (org-element-adopt-elements
4003 (replace-regexp-in-string
4004 "\t" (make-string tab-width ?
)
4005 (buffer-substring-no-properties (point) end
)))))
4009 (defun org-element--get-next-object-candidates (limit restriction objects
)
4010 "Return an alist of candidates for the next object.
4012 LIMIT bounds the search, and RESTRICTION narrows candidates to
4015 Return value is an alist whose CAR is position and CDR the object
4018 OBJECTS is the previous candidates alist."
4019 ;; Filter out any object found but not belonging to RESTRICTION.
4023 (let ((type (car obj
)))
4024 (memq (or (cdr (assq type org-element-object-successor-alist
))
4028 (let (next-candidates types-to-search
)
4029 ;; If no previous result, search every object type in RESTRICTION.
4030 ;; Otherwise, keep potential candidates (old objects located after
4031 ;; point) and ask to search again those which had matched before.
4032 (if (not objects
) (setq types-to-search restriction
)
4034 (if (< (cdr obj
) (point)) (push (car obj
) types-to-search
)
4035 (push obj next-candidates
)))
4037 ;; Call the appropriate successor function for each type to search
4038 ;; and accumulate matches.
4041 (let* ((successor-fun
4043 (format "org-element-%s-successor"
4044 (or (cdr (assq type org-element-object-successor-alist
))
4046 (obj (funcall successor-fun limit
)))
4047 (and obj
(push obj next-candidates
))))
4054 ;;; Towards A Bijective Process
4056 ;; The parse tree obtained with `org-element-parse-buffer' is really
4057 ;; a snapshot of the corresponding Org buffer. Therefore, it can be
4058 ;; interpreted and expanded into a string with canonical Org syntax.
4059 ;; Hence `org-element-interpret-data'.
4061 ;; The function relies internally on
4062 ;; `org-element--interpret-affiliated-keywords'.
4065 (defun org-element-interpret-data (data &optional parent
)
4066 "Interpret DATA as Org syntax.
4068 DATA is a parse tree, an element, an object or a secondary string
4071 Optional argument PARENT is used for recursive calls. It contains
4072 the element or object containing data, or nil.
4074 Return Org syntax as a string."
4075 (let* ((type (org-element-type data
))
4078 ;; Secondary string.
4081 (lambda (obj) (org-element-interpret-data obj parent
))
4083 ;; Full Org document.
4084 ((eq type
'org-data
)
4086 (lambda (obj) (org-element-interpret-data obj parent
))
4087 (org-element-contents data
) ""))
4089 ((stringp data
) data
)
4090 ;; Element/Object without contents.
4091 ((not (org-element-contents data
))
4092 (funcall (intern (format "org-element-%s-interpreter" type
))
4094 ;; Element/Object with contents.
4096 (let* ((greaterp (memq type org-element-greater-elements
))
4097 (objectp (and (not greaterp
)
4098 (memq type org-element-recursive-objects
)))
4101 (lambda (obj) (org-element-interpret-data obj data
))
4102 (org-element-contents
4103 (if (or greaterp objectp
) data
4104 ;; Elements directly containing objects must
4105 ;; have their indentation normalized first.
4106 (org-element-normalize-contents
4108 ;; When normalizing first paragraph of an
4109 ;; item or a footnote-definition, ignore
4110 ;; first line's indentation.
4111 (and (eq type
'paragraph
)
4112 (equal data
(car (org-element-contents parent
)))
4113 (memq (org-element-type parent
)
4114 '(footnote-definition item
))))))
4116 (funcall (intern (format "org-element-%s-interpreter" type
))
4118 (if greaterp
(org-element-normalize-contents contents
)
4120 (if (memq type
'(org-data plain-text nil
)) results
4121 ;; Build white spaces. If no `:post-blank' property is
4122 ;; specified, assume its value is 0.
4123 (let ((post-blank (or (org-element-property :post-blank data
) 0)))
4124 (if (memq type org-element-all-objects
)
4125 (concat results
(make-string post-blank
32))
4127 (org-element--interpret-affiliated-keywords data
)
4128 (org-element-normalize-string results
)
4129 (make-string post-blank
10)))))))
4131 (defun org-element--interpret-affiliated-keywords (element)
4132 "Return ELEMENT's affiliated keywords as Org syntax.
4133 If there is no affiliated keyword, return the empty string."
4134 (let ((keyword-to-org
4138 (when (member key org-element-dual-keywords
)
4139 (setq dual
(cdr value
) value
(car value
)))
4142 (format "[%s]" (org-element-interpret-data dual
)))
4144 (if (member key org-element-parsed-keywords
)
4145 (org-element-interpret-data value
)
4150 (let ((value (org-element-property prop element
))
4151 (keyword (upcase (substring (symbol-name prop
) 1))))
4153 (if (or (member keyword org-element-multiple-keywords
)
4154 ;; All attribute keywords can have multiple lines.
4155 (string-match "^ATTR_" keyword
))
4156 (mapconcat (lambda (line) (funcall keyword-to-org keyword line
))
4159 (funcall keyword-to-org keyword value
)))))
4160 ;; List all ELEMENT's properties matching an attribute line or an
4161 ;; affiliated keyword, but ignore translated keywords since they
4162 ;; cannot belong to the property list.
4163 (loop for prop in
(nth 1 element
) by
'cddr
4164 when
(let ((keyword (upcase (substring (symbol-name prop
) 1))))
4165 (or (string-match "^ATTR_" keyword
)
4167 (member keyword org-element-affiliated-keywords
)
4169 org-element-keyword-translation-alist
)))))
4173 ;; Because interpretation of the parse tree must return the same
4174 ;; number of blank lines between elements and the same number of white
4175 ;; space after objects, some special care must be given to white
4178 ;; The first function, `org-element-normalize-string', ensures any
4179 ;; string different from the empty string will end with a single
4180 ;; newline character.
4182 ;; The second function, `org-element-normalize-contents', removes
4183 ;; global indentation from the contents of the current element.
4185 (defun org-element-normalize-string (s)
4186 "Ensure string S ends with a single newline character.
4188 If S isn't a string return it unchanged. If S is the empty
4189 string, return it. Otherwise, return a new string with a single
4190 newline character at its end."
4192 ((not (stringp s
)) s
)
4194 (t (and (string-match "\\(\n[ \t]*\\)*\\'" s
)
4195 (replace-match "\n" nil nil s
)))))
4197 (defun org-element-normalize-contents (element &optional ignore-first
)
4198 "Normalize plain text in ELEMENT's contents.
4200 ELEMENT must only contain plain text and objects.
4202 If optional argument IGNORE-FIRST is non-nil, ignore first line's
4203 indentation to compute maximal common indentation.
4205 Return the normalized element that is element with global
4206 indentation removed from its contents. The function assumes that
4207 indentation is not done with TAB characters."
4208 (let* (ind-list ; for byte-compiler
4209 collect-inds
; for byte-compiler
4212 ;; Return list of indentations within BLOB. This is done by
4213 ;; walking recursively BLOB and updating IND-LIST along the
4214 ;; way. FIRST-FLAG is non-nil when the first string hasn't
4215 ;; been seen yet. It is required as this string is the only
4216 ;; one whose indentation doesn't happen after a newline
4218 (lambda (blob first-flag
)
4221 (when (and first-flag
(stringp object
))
4222 (setq first-flag nil
)
4223 (string-match "\\`\\( *\\)" object
)
4224 (let ((len (length (match-string 1 object
))))
4225 ;; An indentation of zero means no string will be
4226 ;; modified. Quit the process.
4227 (if (zerop len
) (throw 'zero
(setq ind-list nil
))
4228 (push len ind-list
))))
4232 ;; Avoid matching blank or empty lines.
4233 (while (and (string-match "\n\\( *\\)\\(.\\)" object start
)
4234 (not (equal (match-string 2 object
) " ")))
4235 (setq start
(match-end 0))
4236 (push (length (match-string 1 object
)) ind-list
))))
4237 ((memq (org-element-type object
) org-element-recursive-objects
)
4238 (funcall collect-inds object first-flag
))))
4239 (org-element-contents blob
))))))
4240 ;; Collect indentation list in ELEMENT. Possibly remove first
4241 ;; value if IGNORE-FIRST is non-nil.
4242 (catch 'zero
(funcall collect-inds element
(not ignore-first
)))
4243 (if (not ind-list
) element
4244 ;; Build ELEMENT back, replacing each string with the same
4245 ;; string minus common indentation.
4246 (let* (build ; For byte compiler.
4249 (lambda (blob mci first-flag
)
4250 ;; Return BLOB with all its strings indentation
4251 ;; shortened from MCI white spaces. FIRST-FLAG is
4252 ;; non-nil when the first string hasn't been seen
4257 (when (and first-flag
(stringp object
))
4258 (setq first-flag nil
)
4260 (replace-regexp-in-string
4261 (format "\\` \\{%d\\}" mci
) "" object
)))
4264 (replace-regexp-in-string
4265 (format "\n \\{%d\\}" mci
) "\n" object
))
4266 ((memq (org-element-type object
)
4267 org-element-recursive-objects
)
4268 (funcall build object mci first-flag
))
4270 (org-element-contents blob
)))
4272 (funcall build element
(apply 'min ind-list
) (not ignore-first
))))))
4278 ;; The first move is to implement a way to obtain the smallest element
4279 ;; containing point. This is the job of `org-element-at-point'. It
4280 ;; basically jumps back to the beginning of section containing point
4281 ;; and moves, element after element, with
4282 ;; `org-element--current-element' until the container is found. Note:
4283 ;; When using `org-element-at-point', secondary values are never
4284 ;; parsed since the function focuses on elements, not on objects.
4286 ;; At a deeper level, `org-element-context' lists all elements and
4287 ;; objects containing point.
4289 ;; `org-element-nested-p' and `org-element-swap-A-B' may be used
4290 ;; internally by navigation and manipulation tools.
4293 (defun org-element-at-point (&optional keep-trail
)
4294 "Determine closest element around point.
4296 Return value is a list like (TYPE PROPS) where TYPE is the type
4297 of the element and PROPS a plist of properties associated to the
4300 Possible types are defined in `org-element-all-elements'.
4301 Properties depend on element or object type, but always
4302 include :begin, :end, :parent and :post-blank properties.
4304 As a special case, if point is at the very beginning of a list or
4305 sub-list, returned element will be that list instead of the first
4306 item. In the same way, if point is at the beginning of the first
4307 row of a table, returned element will be the table instead of the
4310 If optional argument KEEP-TRAIL is non-nil, the function returns
4311 a list of of elements leading to element at point. The list's
4312 CAR is always the element at point. Following positions contain
4313 element's siblings, then parents, siblings of parents, until the
4314 first element of current section."
4315 (org-with-wide-buffer
4316 ;; If at an headline, parse it. It is the sole element that
4317 ;; doesn't require to know about context. Be sure to disallow
4318 ;; secondary string parsing, though.
4319 (if (org-with-limited-levels (org-at-heading-p))
4322 (if (not keep-trail
) (org-element-headline-parser (point-max) t
)
4323 (list (org-element-headline-parser (point-max) t
))))
4324 ;; Otherwise move at the beginning of the section containing
4326 (let ((origin (point))
4327 (end (save-excursion
4328 (org-with-limited-levels (outline-next-heading)) (point)))
4329 element type special-flag trail struct prevs parent
)
4330 (org-with-limited-levels
4331 (if (org-with-limited-levels (org-before-first-heading-p))
4332 (goto-char (point-min))
4333 (org-back-to-heading)
4335 (org-skip-whitespace)
4337 ;; Parse successively each element, skipping those ending
4338 ;; before original position.
4342 (org-element--current-element end
'element special-flag struct
)
4344 (org-element-put-property element
:parent parent
)
4345 (when keep-trail
(push element trail
))
4347 ;; 1. Skip any element ending before point. Also skip
4348 ;; element ending at point when we're sure that another
4349 ;; element has started.
4350 ((let ((elem-end (org-element-property :end element
)))
4351 (when (or (< elem-end origin
)
4352 (and (= elem-end origin
) (/= elem-end end
)))
4353 (goto-char elem-end
))))
4354 ;; 2. An element containing point is always the element at
4356 ((not (memq type org-element-greater-elements
))
4357 (throw 'exit
(if keep-trail trail element
)))
4358 ;; 3. At any other greater element type, if point is
4359 ;; within contents, move into it.
4361 (let ((cbeg (org-element-property :contents-begin element
))
4362 (cend (org-element-property :contents-end element
)))
4363 (if (or (not cbeg
) (not cend
) (> cbeg origin
) (< cend origin
)
4364 ;; Create an anchor for tables and plain lists:
4365 ;; when point is at the very beginning of these
4366 ;; elements, ignoring affiliated keywords,
4367 ;; target them instead of their contents.
4368 (and (= cbeg origin
) (memq type
'(plain-list table
)))
4369 ;; When point is at contents end, do not move
4370 ;; into elements with an explicit ending, but
4371 ;; return that element instead.
4372 (and (= cend origin
)
4375 drawer dynamic-block inlinetask item
4376 plain-list property-drawer quote-block
4378 (throw 'exit
(if keep-trail trail element
))
4379 (setq parent element
)
4382 (setq special-flag
'item
4383 struct
(org-element-property :structure element
)))
4384 (property-drawer (setq special-flag
'node-property
))
4385 (table (setq special-flag
'table-row
))
4386 (otherwise (setq special-flag nil
)))
4388 (goto-char cbeg
)))))))))))
4391 (defun org-element-context ()
4392 "Return closest element or object around point.
4394 Return value is a list like (TYPE PROPS) where TYPE is the type
4395 of the element or object and PROPS a plist of properties
4398 Possible types are defined in `org-element-all-elements' and
4399 `org-element-all-objects'. Properties depend on element or
4400 object type, but always include :begin, :end, :parent
4401 and :post-blank properties."
4402 (org-with-wide-buffer
4403 (let* ((origin (point))
4404 (element (org-element-at-point))
4405 (type (car element
))
4407 ;; Check if point is inside an element containing objects or at
4408 ;; a secondary string. In that case, move to beginning of the
4409 ;; element or secondary string and set END to the other side.
4410 (if (not (or (and (eq type
'item
)
4411 (let ((tag (org-element-property :tag element
)))
4415 (search-forward tag
(point-at-eol))
4416 (goto-char (match-beginning 0))
4417 (and (>= origin
(point))
4419 ;; `1+' is required so some
4420 ;; successors can match
4421 ;; properly their object.
4422 (setq end
(1+ (match-end 0)))))))))
4423 (and (memq type
'(headline inlinetask
))
4424 (progn (beginning-of-line)
4425 (skip-chars-forward "* ")
4426 (setq end
(point-at-eol))))
4427 (and (memq type
'(paragraph table-row verse-block
))
4428 (let ((cbeg (org-element-property
4429 :contents-begin element
))
4430 (cend (org-element-property
4431 :contents-end element
)))
4432 (and (>= origin cbeg
)
4434 (progn (goto-char cbeg
) (setq end cend
)))))))
4436 (let ((restriction (org-element-restriction element
))
4440 (while (setq candidates
(org-element--get-next-object-candidates
4441 end restriction candidates
))
4442 (let ((closest-cand (rassq (apply 'min
(mapcar 'cdr candidates
))
4444 ;; If ORIGIN is before next object in element, there's
4445 ;; no point in looking further.
4446 (if (> (cdr closest-cand
) origin
) (throw 'exit parent
)
4448 (progn (goto-char (cdr closest-cand
))
4449 (funcall (intern (format "org-element-%s-parser"
4450 (car closest-cand
))))))
4451 (cbeg (org-element-property :contents-begin object
))
4452 (cend (org-element-property :contents-end object
)))
4454 ;; ORIGIN is after OBJECT, so skip it.
4455 ((< (org-element-property :end object
) origin
)
4456 (goto-char (org-element-property :end object
)))
4457 ;; ORIGIN is within a non-recursive object or at an
4458 ;; object boundaries: Return that object.
4459 ((or (not cbeg
) (> cbeg origin
) (< cend origin
))
4461 (org-element-put-property object
:parent parent
)))
4462 ;; Otherwise, move within current object and restrict
4463 ;; search to the end of its contents.
4465 (org-element-put-property object
:parent parent
)
4467 restriction
(org-element-restriction object
)
4471 (defsubst org-element-nested-p
(elem-A elem-B
)
4472 "Non-nil when elements ELEM-A and ELEM-B are nested."
4473 (let ((beg-A (org-element-property :begin elem-A
))
4474 (beg-B (org-element-property :begin elem-B
))
4475 (end-A (org-element-property :end elem-A
))
4476 (end-B (org-element-property :end elem-B
)))
4477 (or (and (>= beg-A beg-B
) (<= end-A end-B
))
4478 (and (>= beg-B beg-A
) (<= end-B end-A
)))))
4480 (defun org-element-swap-A-B (elem-A elem-B
)
4481 "Swap elements ELEM-A and ELEM-B.
4482 Assume ELEM-B is after ELEM-A in the buffer. Leave point at the
4484 (goto-char (org-element-property :begin elem-A
))
4485 ;; There are two special cases when an element doesn't start at bol:
4486 ;; the first paragraph in an item or in a footnote definition.
4487 (let ((specialp (not (bolp))))
4488 ;; Only a paragraph without any affiliated keyword can be moved at
4489 ;; ELEM-A position in such a situation. Note that the case of
4490 ;; a footnote definition is impossible: it cannot contain two
4491 ;; paragraphs in a row because it cannot contain a blank line.
4493 (or (not (eq (org-element-type elem-B
) 'paragraph
))
4494 (/= (org-element-property :begin elem-B
)
4495 (org-element-property :contents-begin elem-B
))))
4496 (error "Cannot swap elements"))
4497 ;; In a special situation, ELEM-A will have no indentation. We'll
4498 ;; give it ELEM-B's (which will in, in turn, have no indentation).
4499 (let* ((ind-B (when specialp
4500 (goto-char (org-element-property :begin elem-B
))
4501 (org-get-indentation)))
4502 (beg-A (org-element-property :begin elem-A
))
4503 (end-A (save-excursion
4504 (goto-char (org-element-property :end elem-A
))
4505 (skip-chars-backward " \r\t\n")
4507 (beg-B (org-element-property :begin elem-B
))
4508 (end-B (save-excursion
4509 (goto-char (org-element-property :end elem-B
))
4510 (skip-chars-backward " \r\t\n")
4512 ;; Store overlays responsible for visibility status. We
4513 ;; also need to store their boundaries as they will be
4514 ;; removed from buffer.
4517 (mapcar (lambda (ov) (list ov
(overlay-start ov
) (overlay-end ov
)))
4518 (overlays-in beg-A end-A
))
4519 (mapcar (lambda (ov) (list ov
(overlay-start ov
) (overlay-end ov
)))
4520 (overlays-in beg-B end-B
))))
4522 (body-A (buffer-substring beg-A end-A
))
4523 (body-B (delete-and-extract-region beg-B end-B
)))
4526 (setq body-B
(replace-regexp-in-string "\\`[ \t]*" "" body-B
))
4527 (org-indent-to-column ind-B
))
4529 ;; Restore ex ELEM-A overlays.
4530 (let ((offset (- beg-B beg-A
)))
4533 (car ov
) (+ (nth 1 ov
) offset
) (+ (nth 2 ov
) offset
)))
4536 (delete-region beg-A end-A
)
4538 ;; Restore ex ELEM-B overlays.
4541 (car ov
) (- (nth 1 ov
) offset
) (- (nth 2 ov
) offset
)))
4543 (goto-char (org-element-property :end elem-B
)))))
4545 (provide 'org-element
)
4548 ;; generated-autoload-file: "org-loaddefs.el"
4551 ;;; org-element.el ends here